Skip to content

Commit d376196

Browse files
add 1642
1 parent d619a6d commit d376196

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ _If you like this project, please leave me a star._ ★
9797
|1656|[Design an Ordered Stream](https://leetcode.com/problems/design-an-ordered-stream/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1656.java) ||Easy|Array, Design|
9898
|1652|[Defuse the Bomb](https://leetcode.com/problems/defuse-the-bomb/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1652.java) ||Easy|Array|
9999
|1646|[Get Maximum in Generated Array](https://leetcode.com/problems/get-maximum-in-generated-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1646.java) ||Easy|Array|
100+
|1642|[Furthest Building You Can Reach](https://leetcode.com/problems/furthest-building-you-can-reach/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1642.java) ||Medium|Binary Search, Heap|
100101
|1641|[Count Sorted Vowel Strings](https://leetcode.com/problems/count-sorted-vowel-strings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1641.java) |[:tv:](https://youtu.be/gdH4yfgfwiU)|Medium|Math, DP, Backtracking|
101102
|1640|[Check Array Formation Through Concatenation](https://leetcode.com/problems/check-array-formation-through-concatenation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1640.java) ||Easy|Array, Sort|
102103
|1637|[Widest Vertical Area Between Two Points Containing No Points](https://leetcode.com/problems/widest-vertical-area-between-two-points-containing-no-points/)|[Javascript](./javascript/_1637.js)| | Medium | Sort |
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.PriorityQueue;
4+
5+
public class _1642 {
6+
public static class Solution1 {
7+
public int furthestBuilding(int[] heights, int bricks, int ladders) {
8+
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
9+
int i = 0;
10+
//we'll assume to use ladders for the first l jumps and adjust it afterwards
11+
for (; i < heights.length - 1 && minHeap.size() < ladders; i++) {
12+
int diff = heights[i + 1] - heights[i];
13+
if (diff > 0) {
14+
minHeap.offer(diff);
15+
}
16+
}
17+
//now ladders have been used up, we'll use bricks to jump
18+
while (i < heights.length - 1) {
19+
int diff = heights[i + 1] - heights[i];
20+
if (diff > 0) {
21+
//we'll check if the last one that cost a ladder could actually be filled with some bricks
22+
if (!minHeap.isEmpty() && minHeap.peek() < diff) {
23+
bricks -= minHeap.poll();
24+
minHeap.offer(diff);
25+
} else {
26+
bricks -= diff;
27+
}
28+
if (bricks < 0) {
29+
return i;
30+
}
31+
}
32+
i++;
33+
}
34+
return i;
35+
}
36+
}
37+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1642;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1642Test {
10+
private static _1642.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setUp() {
14+
solution1 = new _1642.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(4, solution1.furthestBuilding(new int[]{4, 2, 7, 6, 9, 14, 12}, 5, 1));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(7, solution1.furthestBuilding(new int[]{4, 12, 2, 7, 3, 18, 20, 3, 19}, 10, 2));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(3, solution1.furthestBuilding(new int[]{14, 3, 19, 3}, 17, 0));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals(6, solution1.furthestBuilding(new int[]{17, 16, 5, 10, 10, 14, 7}, 74, 6));
35+
}
36+
37+
@Test
38+
public void test5() {
39+
assertEquals(1, solution1.furthestBuilding(new int[]{7, 5, 13}, 0, 0));
40+
}
41+
42+
@Test
43+
public void test6() {
44+
assertEquals(3, solution1.furthestBuilding(new int[]{2, 7, 9, 12}, 5, 1));
45+
}
46+
47+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy