1642. Furthest Building You Can Reach Medium

@problem@discussion
#Array#Greedy#Heap (Priority Queue)



1/**
2 * [1642] Furthest Building You Can Reach
3 *
4 * You are given an integer array heights representing the heights of buildings, some bricks, and some ladders.
5 * You start your journey from building 0 and move to the next building by possibly using bricks or ladders.
6 * While moving from building i to building i+1 (0-indexed),
7 * 
8 * 	If the current building's height is greater than or equal to the next building's height, you do not need a ladder or bricks.
9 * 	If the current building's height is less than the next building's height, you can either use one ladder or (h[i+1] - h[i]) bricks.
10 * 
11 * Return the furthest building index (0-indexed) you can reach if you use the given ladders and bricks optimally.
12 *  
13 * Example 1:
14 * <img alt="" src="https://assets.leetcode.com/uploads/2020/10/27/q4.gif" style="width: 562px; height: 561px;" />
15 * Input: heights = [4,2,7,6,9,14,12], bricks = 5, ladders = 1
16 * Output: 4
17 * Explanation: Starting at building 0, you can follow these steps:
18 * - Go to building 1 without using ladders nor bricks since 4 >= 2.
19 * - Go to building 2 using 5 bricks. You must use either bricks or ladders because 2 < 7.
20 * - Go to building 3 without using ladders nor bricks since 7 >= 6.
21 * - Go to building 4 using your only ladder. You must use either bricks or ladders because 6 < 9.
22 * It is impossible to go beyond building 4 because you do not have any more bricks or ladders.
23 * 
24 * Example 2:
25 * 
26 * Input: heights = [4,12,2,7,3,18,20,3,19], bricks = 10, ladders = 2
27 * Output: 7
28 * 
29 * Example 3:
30 * 
31 * Input: heights = [14,3,19,3], bricks = 17, ladders = 0
32 * Output: 3
33 * 
34 *  
35 * Constraints:
36 * 
37 * 	1 <= heights.length <= 10^5
38 * 	1 <= heights[i] <= 10^6
39 * 	0 <= bricks <= 10^9
40 * 	0 <= ladders <= heights.length
41 * 
42 */
43pub struct Solution {}
44
45// problem: https://leetcode.com/problems/furthest-building-you-can-reach/
46// discuss: https://leetcode.com/problems/furthest-building-you-can-reach/discuss/?currentPage=1&orderBy=most_votes&query=
47
48// submission codes start here
49
50impl Solution {
51    pub fn furthest_building(heights: Vec<i32>, bricks: i32, ladders: i32) -> i32 {
52        0
53    }
54}
55
56// submission codes end
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61
62    #[test]
63    fn test_1642() {
64    }
65}
66


Back
© 2025 bowen.ge All Rights Reserved.