Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 43644b6

Browse files
committedApr 25, 2025
Add solution #1665
1 parent 9b70089 commit 43644b6

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,461 LeetCode solutions in JavaScript
1+
# 1,462 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1283,6 +1283,7 @@
12831283
1662|[Check If Two String Arrays are Equivalent](./solutions/1662-check-if-two-string-arrays-are-equivalent.js)|Easy|
12841284
1663|[Smallest String With A Given Numeric Value](./solutions/1663-smallest-string-with-a-given-numeric-value.js)|Medium|
12851285
1664|[Ways to Make a Fair Array](./solutions/1664-ways-to-make-a-fair-array.js)|Medium|
1286+
1665|[Minimum Initial Energy to Finish Tasks](./solutions/1665-minimum-initial-energy-to-finish-tasks.js)|Hard|
12861287
1668|[Maximum Repeating Substring](./solutions/1668-maximum-repeating-substring.js)|Easy|
12871288
1669|[Merge In Between Linked Lists](./solutions/1669-merge-in-between-linked-lists.js)|Medium|
12881289
1672|[Richest Customer Wealth](./solutions/1672-richest-customer-wealth.js)|Easy|
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* 1665. Minimum Initial Energy to Finish Tasks
3+
* https://leetcode.com/problems/minimum-initial-energy-to-finish-tasks/
4+
* Difficulty: Hard
5+
*
6+
* You are given an array tasks where tasks[i] = [actuali, minimumi]:
7+
* - actuali is the actual amount of energy you spend to finish the ith task.
8+
* - minimumi is the minimum amount of energy you require to begin the ith task.
9+
*
10+
* For example, if the task is [10, 12] and your current energy is 11, you cannot start
11+
* this task. However, if your current energy is 13, you can complete this task, and your
12+
* energy will be 3 after finishing it.
13+
*
14+
* You can finish the tasks in any order you like.
15+
*
16+
* Return the minimum initial amount of energy you will need to finish all the tasks.
17+
*/
18+
19+
/**
20+
* @param {number[][]} tasks
21+
* @return {number}
22+
*/
23+
var minimumEffort = function(tasks) {
24+
tasks.sort((a, b) => (b[1] - b[0]) - (a[1] - a[0]));
25+
let result = 0;
26+
let currentEnergy = 0;
27+
28+
for (const [actual, minimum] of tasks) {
29+
if (currentEnergy < minimum) {
30+
result += minimum - currentEnergy;
31+
currentEnergy = minimum;
32+
}
33+
currentEnergy -= actual;
34+
}
35+
36+
return result;
37+
};

0 commit comments

Comments
 (0)
Please sign in to comment.