1665. Minimum Initial Energy to Finish Tasks Hard

@problem@discussion
#Array#Greedy#Sorting



1/**
2 * [1665] Minimum Initial Energy to Finish Tasks
3 *
4 * You are given an array tasks where tasks[i] = [actuali, minimumi]:
5 * 
6 * 	actuali is the actual amount of energy you spend to finish the i^th task.
7 * 	minimumi is the minimum amount of energy you require to begin the i^th task.
8 * 
9 * For example, if the task is [10, 12] and your current energy is 11, you cannot start this task. However, if your current energy is 13, you can complete this task, and your energy will be 3 after finishing it.
10 * You can finish the tasks in any order you like.
11 * Return the minimum initial amount of energy you will need to finish all the tasks.
12 *  
13 * Example 1:
14 * 
15 * Input: tasks = [[1,2],[2,4],[4,8]]
16 * Output: 8
17 * Explanation:
18 * Starting with 8 energy, we finish the tasks in the following order:
19 *     - 3rd task. Now energy = 8 - 4 = 4.
20 *     - 2nd task. Now energy = 4 - 2 = 2.
21 *     - 1st task. Now energy = 2 - 1 = 1.
22 * Notice that even though we have leftover energy, starting with 7 energy does not work because we cannot do the 3rd task.
23 * Example 2:
24 * 
25 * Input: tasks = [[1,3],[2,4],[10,11],[10,12],[8,9]]
26 * Output: 32
27 * Explanation:
28 * Starting with 32 energy, we finish the tasks in the following order:
29 *     - 1st task. Now energy = 32 - 1 = 31.
30 *     - 2nd task. Now energy = 31 - 2 = 29.
31 *     - 3rd task. Now energy = 29 - 10 = 19.
32 *     - 4th task. Now energy = 19 - 10 = 9.
33 *     - 5th task. Now energy = 9 - 8 = 1.
34 * Example 3:
35 * 
36 * Input: tasks = [[1,7],[2,8],[3,9],[4,10],[5,11],[6,12]]
37 * Output: 27
38 * Explanation:
39 * Starting with 27 energy, we finish the tasks in the following order:
40 *     - 5th task. Now energy = 27 - 5 = 22.
41 *     - 2nd task. Now energy = 22 - 2 = 20.
42 *     - 3rd task. Now energy = 20 - 3 = 17.
43 *     - 1st task. Now energy = 17 - 1 = 16.
44 *     - 4th task. Now energy = 16 - 4 = 12.
45 *     - 6th task. Now energy = 12 - 6 = 6.
46 * 
47 *  
48 * Constraints:
49 * 
50 * 	1 <= tasks.length <= 10^5
51 * 	1 <= actual​i <= minimumi <= 10^4
52 * 
53 */
54pub struct Solution {}
55
56// problem: https://leetcode.com/problems/minimum-initial-energy-to-finish-tasks/
57// discuss: https://leetcode.com/problems/minimum-initial-energy-to-finish-tasks/discuss/?currentPage=1&orderBy=most_votes&query=
58
59// submission codes start here
60
61impl Solution {
62    pub fn minimum_effort(tasks: Vec<Vec<i32>>) -> i32 {
63        0
64    }
65}
66
67// submission codes end
68
69#[cfg(test)]
70mod tests {
71    use super::*;
72
73    #[test]
74    fn test_1665() {
75    }
76}
77


Back
© 2025 bowen.ge All Rights Reserved.