Skip to content

Commit 54760c5

Browse files
authored
Create 1665-minimum-initial-energy-to-finish-tasks.js
1 parent 42d2d63 commit 54760c5

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @param {number[][]} tasks
3+
* @return {number}
4+
*/
5+
const minimumEffort = function (a) {
6+
let low = 0,
7+
high = 1e9
8+
for (let x of a) low = Math.max(low, x[1])
9+
a.sort((lhs, rhs) => (lhs[1] - lhs[0] > rhs[1] - rhs[0] ? -1 : 1))
10+
let n = a.length
11+
while (low != high) {
12+
let mid = low + ((high - low) >> 1)
13+
let found = false
14+
let rem = mid
15+
for (let i = 0; i < n; ++i) {
16+
if (rem < a[i][1]) {
17+
found = true
18+
break
19+
}
20+
rem -= a[i][0]
21+
}
22+
if (found) {
23+
low = mid + 1
24+
} else {
25+
high = mid
26+
}
27+
}
28+
return high
29+
}

0 commit comments

Comments
 (0)