Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd376196

Browse files
add 1642
1 parentd619a6d commitd376196

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+
packagecom.fishercoder.solutions;
2+
3+
importjava.util.PriorityQueue;
4+
5+
publicclass_1642 {
6+
publicstaticclassSolution1 {
7+
publicintfurthestBuilding(int[]heights,intbricks,intladders) {
8+
PriorityQueue<Integer>minHeap =newPriorityQueue<>();
9+
inti =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+
intdiff =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+
intdiff =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+
returni;
30+
}
31+
}
32+
i++;
33+
}
34+
returni;
35+
}
36+
}
37+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._1642;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticorg.junit.Assert.assertEquals;
8+
9+
publicclass_1642Test {
10+
privatestatic_1642.Solution1solution1;
11+
12+
@BeforeClass
13+
publicstaticvoidsetUp() {
14+
solution1 =new_1642.Solution1();
15+
}
16+
17+
@Test
18+
publicvoidtest1() {
19+
assertEquals(4,solution1.furthestBuilding(newint[]{4,2,7,6,9,14,12},5,1));
20+
}
21+
22+
@Test
23+
publicvoidtest2() {
24+
assertEquals(7,solution1.furthestBuilding(newint[]{4,12,2,7,3,18,20,3,19},10,2));
25+
}
26+
27+
@Test
28+
publicvoidtest3() {
29+
assertEquals(3,solution1.furthestBuilding(newint[]{14,3,19,3},17,0));
30+
}
31+
32+
@Test
33+
publicvoidtest4() {
34+
assertEquals(6,solution1.furthestBuilding(newint[]{17,16,5,10,10,14,7},74,6));
35+
}
36+
37+
@Test
38+
publicvoidtest5() {
39+
assertEquals(1,solution1.furthestBuilding(newint[]{7,5,13},0,0));
40+
}
41+
42+
@Test
43+
publicvoidtest6() {
44+
assertEquals(3,solution1.furthestBuilding(newint[]{2,7,9,12},5,1));
45+
}
46+
47+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp