Skip to content

Commit 20057b9

Browse files
committed
1 parent d7e67cd commit 20057b9

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

src/solution/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,3 +1255,4 @@ mod s1659_maximize_grid_happiness;
12551255
mod s1662_check_if_two_string_arrays_are_equivalent;
12561256
mod s1663_smallest_string_with_a_given_numeric_value;
12571257
mod s1664_ways_to_make_a_fair_array;
1258+
mod s1665_minimum_initial_energy_to_finish_tasks;
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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+
*/
54+
pub 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+
61+
impl Solution {
62+
// Credit: https://leetcode.com/problems/minimum-initial-energy-to-finish-tasks/solutions/1000272/rust-sorted-greedy-o-n-log-n/
63+
pub fn minimum_effort(tasks: Vec<Vec<i32>>) -> i32 {
64+
let mut tasks = tasks;
65+
66+
tasks.sort_unstable_by_key(|task| task[1] - task[0]);
67+
68+
let mut result = 0;
69+
for task in tasks {
70+
result += task[0];
71+
if result < task[1] {
72+
result += (task[1] - result);
73+
}
74+
}
75+
76+
result
77+
}
78+
}
79+
80+
// submission codes end
81+
82+
#[cfg(test)]
83+
mod tests {
84+
use super::*;
85+
86+
#[test]
87+
fn test_1665_example_1() {
88+
let tasks = vec![vec![1, 2], vec![2, 4], vec![4, 8]];
89+
90+
let result = 8;
91+
92+
assert_eq!(Solution::minimum_effort(tasks), result);
93+
}
94+
95+
#[test]
96+
fn test_1665_example_2() {
97+
let tasks = vec![
98+
vec![1, 3],
99+
vec![2, 4],
100+
vec![10, 11],
101+
vec![10, 12],
102+
vec![8, 9],
103+
];
104+
105+
let result = 32;
106+
107+
assert_eq!(Solution::minimum_effort(tasks), result);
108+
}
109+
110+
#[test]
111+
fn test_1665_example_3() {
112+
let tasks = vec![
113+
vec![1, 7],
114+
vec![2, 8],
115+
vec![3, 9],
116+
vec![4, 10],
117+
vec![5, 11],
118+
vec![6, 12],
119+
];
120+
121+
let result = 27;
122+
123+
assert_eq!(Solution::minimum_effort(tasks), result);
124+
}
125+
}

0 commit comments

Comments
 (0)