Skip to content

Commit 1dca2df

Browse files
committed
leetcode
1 parent 8542b94 commit 1dca2df

File tree

1 file changed

+31
-70
lines changed

1 file changed

+31
-70
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,38 @@
1-
// package main
2-
3-
// var mem = map[int]int{}
4-
5-
// func steps(n int) int {
6-
// if n <= 1 {
7-
// return -1
8-
// }
9-
// if ret, ok := mem[n]; ok {
10-
// return ret
11-
// }
12-
// if n == 2 {
13-
// return 1
14-
// }
15-
// if n%3 == 0 {
16-
// mem[n] = n / 3
17-
// return n / 3
18-
// }
19-
// out := 1 + steps(n-2)
20-
// mem[n] = out
21-
// return out
22-
// }
23-
24-
// func minimumRounds(tasks []int) int {
25-
// dic := map[int]int{}
26-
// for _, t := range tasks {
27-
// dic[t]++
28-
// }
29-
// out := 0
30-
// for _, n := range dic {
31-
// s := steps(n)
32-
// if s == -1 {
33-
// return -1
34-
// }
35-
// out += s
36-
// }
37-
// return out
38-
// }
39-
401
package main
412

42-
import "sort"
3+
var mem = map[int]int{}
434

44-
func minimumRounds(tasks []int) int {
45-
sort.Ints(tasks)
46-
var result int = 0
47-
for i := 0; i < len(tasks); i++ {
48-
var j int = i + 1
49-
for j < len(tasks) && tasks[j] == tasks[i] {
50-
j++
51-
if j == i+1 {
52-
return -1
53-
}
54-
}
55-
56-
result += (j - i + 2) / 3
57-
i = j
5+
func steps(n int) int {
6+
if n <= 1 {
7+
return -1
8+
}
9+
if ret, ok := mem[n]; ok {
10+
return ret
5811
}
59-
return result
12+
if n == 2 {
13+
return 1
14+
}
15+
if n%3 == 0 {
16+
mem[n] = n / 3
17+
return n / 3
18+
}
19+
out := 1 + steps(n-2)
20+
mem[n] = out
21+
return out
6022
}
6123

62-
/*
63-
64-
65-
int minimumRounds(List<int> tasks) {
66-
tasks.sort();
67-
int res = 0;
68-
for (int i = 0; i < tasks.length;) {
69-
int j = i + 1;
70-
while (j < tasks.length && tasks[j] == tasks[i]) j++;
71-
if (j == i + 1) return -1;
72-
res += (j - i + 2) ~/ 3;
73-
i = j;
74-
}
75-
return res;
24+
func minimumRounds(tasks []int) int {
25+
dic := map[int]int{}
26+
for _, t := range tasks {
27+
dic[t]++
28+
}
29+
out := 0
30+
for _, n := range dic {
31+
s := steps(n)
32+
if s == -1 {
33+
return -1
34+
}
35+
out += s
36+
}
37+
return out
7638
}
77-
*/

0 commit comments

Comments
 (0)