File tree 2 files changed +48
-1
lines changed
2 files changed +48
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,443 LeetCode solutions in JavaScript
1
+ # 1,444 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcodejavascript.com ] ( https://leetcodejavascript.com )
4
4
1266
1266
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|
1267
1267
1640|[ Check Array Formation Through Concatenation] ( ./solutions/1640-check-array-formation-through-concatenation.js ) |Easy|
1268
1268
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|
1269
1270
1657|[ Determine if Two Strings Are Close] ( ./solutions/1657-determine-if-two-strings-are-close.js ) |Medium|
1270
1271
1668|[ Maximum Repeating Substring] ( ./solutions/1668-maximum-repeating-substring.js ) |Easy|
1271
1272
1669|[ Merge In Between Linked Lists] ( ./solutions/1669-merge-in-between-linked-lists.js ) |Medium|
Original file line number Diff line number Diff line change
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
+
You can’t perform that action at this time.
0 commit comments