the last one in Weekly Contest 216.
Difficulty : Hard
Related Topics : Greedy
You are given an array
tasks
wheretasks[i] = [actuali, minimumi]
:
actuali
is the actual amount of energy you spend to finish thei
th
task.minimumi
is the minimum amount of energy you require to begin thei
th
task.For example, if the task is
[10, 12]
and your current energy is11
, you cannot start this task. However, if your current energy is13
, you can complete this task, and your energy will be3
after finishing it.You can finish the tasks in any order you like.
Return the minimum initial amount of energy you will need to finish all the tasks.
Input: tasks = [[1,2],[2,4],[4,8]] Output: 8 Explanation: Starting with 8 energy, we finish the tasks in the following order: - 3rd task. Now energy = 8 - 4 = 4. - 2nd task. Now energy = 4 - 2 = 2. - 1st task. Now energy = 2 - 1 = 1. Notice that even though we have leftover energy, starting with 7 energy does not work because we cannot do the 3rd task.
Input: tasks = [[1,3],[2,4],[10,11],[10,12],[8,9]] Output: 32 Explanation: Starting with 32 energy, we finish the tasks in the following order: - 1st task. Now energy = 32 - 1 = 31. - 2nd task. Now energy = 31 - 2 = 29. - 3rd task. Now energy = 29 - 10 = 19. - 4th task. Now energy = 19 - 10 = 9. - 5th task. Now energy = 9 - 8 = 1.
Input: tasks = [[1,7],[2,8],[3,9],[4,10],[5,11],[6,12]] Output: 27 Explanation: Starting with 27 energy, we finish the tasks in the following order: - 5th task. Now energy = 27 - 5 = 22. - 2nd task. Now energy = 22 - 2 = 20. - 3rd task. Now energy = 20 - 3 = 17. - 1st task. Now energy = 17 - 1 = 16. - 4th task. Now energy = 16 - 4 = 12. - 6th task. Now energy = 12 - 6 = 6.
1 <= tasks.length <= 10^5
1 <= actuali <= minimumi <= 10^4
- mine
- Java
- ``
- ``
- Java
- the most votes
Runtime: 22 ms, faster than 84.30%, Memory Usage: 96.4 MB, less than 84.98% of Java online submissions
// O(N * logN)time // O(1)space public int minimumEffort(int[][] A) { int res = 0; Arrays.sort(A, (a1, a2) -> (a1[1] - a1[0]) - (a2[1] - a2[0])); for (int[] a : A) { res = Math.max(res + a[0], a[1]); } return res; }