Skip to content

Commit ec2bf68

Browse files
committed
Add solution #1642
1 parent 5ffeef8 commit ec2bf68

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,443 LeetCode solutions in JavaScript
1+
# 1,444 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1266,6 +1266,7 @@
12661266
1639|[Number of Ways to Form a Target String Given a Dictionary](./solutions/1639-number-of-ways-to-form-a-target-string-given-a-dictionary.js)|Hard|
12671267
1640|[Check Array Formation Through Concatenation](./solutions/1640-check-array-formation-through-concatenation.js)|Easy|
12681268
1641|[Count Sorted Vowel Strings](./solutions/1641-count-sorted-vowel-strings.js)|Medium|
1269+
1642|[Furthest Building You Can Reach](./solutions/1642-furthest-building-you-can-reach.js)|Medium|
12691270
1657|[Determine if Two Strings Are Close](./solutions/1657-determine-if-two-strings-are-close.js)|Medium|
12701271
1668|[Maximum Repeating Substring](./solutions/1668-maximum-repeating-substring.js)|Easy|
12711272
1669|[Merge In Between Linked Lists](./solutions/1669-merge-in-between-linked-lists.js)|Medium|
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* 1642. Furthest Building You Can Reach
3+
* https://leetcode.com/problems/furthest-building-you-can-reach/
4+
* Difficulty: Medium
5+
*
6+
* You are given an integer array heights representing the heights of buildings, some bricks,
7+
* and some ladders.
8+
*
9+
* You start your journey from building 0 and move to the next building by possibly using
10+
* bricks or ladders.
11+
*
12+
* While moving from building i to building i+1 (0-indexed):
13+
* - If the current building's height is greater than or equal to the next building's height,
14+
* you do not need a ladder or bricks.
15+
* - If the current building's height is less than the next building's height, you can either
16+
* use one ladder or (h[i+1] - h[i]) bricks.
17+
*
18+
* Return the furthest building index (0-indexed) you can reach if you use the given ladders
19+
* and bricks optimally.
20+
*/
21+
22+
/**
23+
* @param {number[]} heights
24+
* @param {number} bricks
25+
* @param {number} ladders
26+
* @return {number}
27+
*/
28+
var furthestBuilding = function(heights, bricks, ladders) {
29+
const minHeap = new MinPriorityQueue();
30+
31+
for (let i = 0; i < heights.length - 1; i++) {
32+
const climb = heights[i + 1] - heights[i];
33+
if (climb > 0) {
34+
minHeap.push(climb);
35+
if (minHeap.size() > ladders) {
36+
bricks -= minHeap.pop();
37+
if (bricks < 0) {
38+
return i;
39+
}
40+
}
41+
}
42+
}
43+
44+
return heights.length - 1;
45+
};
46+

0 commit comments

Comments
 (0)