-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3683-EarliestTimeToFinishOneTask.go
More file actions
68 lines (59 loc) · 1.81 KB
/
3683-EarliestTimeToFinishOneTask.go
File metadata and controls
68 lines (59 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
// 3683. Earliest Time to Finish One Task
// You are given a 2D integer array tasks where tasks[i] = [si, ti].
// Each [si, ti] in tasks represents a task with start time si that takes ti units of time to finish.
// Return the earliest time at which at least one task is finished.
// Example 1:
// Input: tasks = [[1,6],[2,3]]
// Output: 5
// Explanation:
// The first task starts at time t = 1 and finishes at time 1 + 6 = 7.
// The second task finishes at time 2 + 3 = 5.
// You can finish one task at time 5.
// Example 2:
// Input: tasks = [[100,100],[100,100],[100,100]]
// Output: 200
// Explanation:
// All three tasks finish at time 100 + 100 = 200.
// Constraints:
// 1 <= tasks.length <= 100
// tasks[i] = [si, ti]
// 1 <= si, ti <= 100
import "fmt"
func earliestTime(tasks [][]int) int {
res, sum := 0, 0
if len(tasks) == 1 {
for _, t := range tasks[0] {
sum += t
}
return sum
}
for _, t := range tasks[0] {
res += t
}
min := func (x, y int) int { if x < y { return x; }; return y; }
for i := 1; i < len(tasks); i++ {
sum = 0
for _, t := range tasks[i] {
sum += t
}
res = min(res, sum)
}
return res
}
func main() {
// Example 1:
// Input: tasks = [[1,6],[2,3]]
// Output: 5
// Explanation:
// The first task starts at time t = 1 and finishes at time 1 + 6 = 7.
// The second task finishes at time 2 + 3 = 5.
// You can finish one task at time 5.
fmt.Println(earliestTime([][]int{{1,6},{2,3}})) // 5
// Example 2:
// Input: tasks = [[100,100],[100,100],[100,100]]
// Output: 200
// Explanation:
// All three tasks finish at time 100 + 100 = 200.
fmt.Println(earliestTime([][]int{{100,100},{100,100},{100,100}})) // 200
}