Skip to content

Commit bd7878b

Browse files
committed
test: 3602, 3603, 3604, 3605, 3606, 3607, 3608, 3609 solution
go
1 parent 63dfe2e commit bd7878b

File tree

10 files changed

+408
-21
lines changed

10 files changed

+408
-21
lines changed

golang/problems_test.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
package golang
22

33
import (
4-
"leetCode/problems/problems_3597"
5-
"leetCode/problems/problems_3598"
6-
"leetCode/problems/problems_3599"
7-
"leetCode/problems/problems_3600"
4+
"leetCode/problems/problems_3602"
5+
"leetCode/problems/problems_3603"
6+
"leetCode/problems/problems_3604"
7+
"leetCode/problems/problems_3605"
8+
"leetCode/problems/problems_3606"
9+
"leetCode/problems/problems_3607"
10+
"leetCode/problems/problems_3608"
11+
"leetCode/problems/problems_3609"
812
"testing"
913
)
1014

1115
func TestSolutions(t *testing.T) {
12-
TestEach(t, "3597", "problems", problem3597.Solve)
13-
TestEach(t, "3598", "problems", problem3598.Solve)
14-
TestEach(t, "3599", "problems", problem3599.Solve)
15-
TestEach(t, "3600", "problems", problem3600.Solve)
16+
TestEach(t, "3602", "problems", problem3602.Solve)
17+
TestEach(t, "3603", "problems", problem3603.Solve)
18+
TestEach(t, "3604", "problems", problem3604.Solve)
19+
TestEach(t, "3605", "problems", problem3605.Solve)
20+
TestEach(t, "3606", "problems", problem3606.Solve)
21+
TestEach(t, "3607", "problems", problem3607.Solve)
22+
TestEach(t, "3608", "problems", problem3608.Solve)
23+
TestEach(t, "3609", "problems", problem3609.Solve)
1624
}

golang/solution_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package golang
22

33
import (
4-
problem "leetCode/problems/problems_3609"
4+
problem "leetCode/problems/problems_1865"
55
"testing"
66
)
77

88
func TestSolution(t *testing.T) {
9-
TestEach(t, "3609", "problems", problem.Solve)
9+
TestEach(t, "1865", "problems", problem.Solve)
1010
}

problems/problems_3602/solution.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
11
package problem3602
22

33
import (
4+
"bytes"
45
"encoding/json"
56
"log"
7+
"slices"
68
"strings"
79
)
810

11+
const hex36 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
12+
913
func concatHex36(n int) string {
10-
14+
x := n * n
15+
y := x * n
16+
var buf bytes.Buffer
17+
for y > 0 {
18+
buf.WriteByte(hex36[y%36])
19+
y /= 36
20+
}
21+
for x > 0 {
22+
buf.WriteByte(hex36[x%16])
23+
x /= 16
24+
}
25+
// reverse the buffer to get the correct order
26+
slices.Reverse(buf.Bytes())
27+
return buf.String()
1128
}
1229

1330
func Solve(inputJsonValues string) any {

problems/problems_3603/solution.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,30 @@ package problem3603
33
import (
44
"encoding/json"
55
"log"
6+
"math"
67
"strings"
78
)
89

910
func minCost(m int, n int, waitCost [][]int) int64 {
10-
11+
dp := make([][]int64, m)
12+
for i := range dp {
13+
dp[i] = make([]int64, n)
14+
for j := range dp[i] {
15+
dp[i][j] = math.MaxInt64
16+
}
17+
}
18+
dp[0][0] = 1
19+
for i := range m {
20+
for j := range n {
21+
if i < m-1 {
22+
dp[i+1][j] = min(dp[i+1][j], dp[i][j]+int64(i+2)*int64(j+1)+int64(waitCost[i+1][j]))
23+
}
24+
if j < n-1 {
25+
dp[i][j+1] = min(dp[i][j+1], dp[i][j]+int64(i+1)*int64(j+2)+int64(waitCost[i][j+1]))
26+
}
27+
}
28+
}
29+
return dp[m-1][n-1] - int64(waitCost[m-1][n-1])
1130
}
1231

1332
func Solve(inputJsonValues string) any {

problems/problems_3604/solution.go

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,65 @@
11
package problem3604
22

33
import (
4+
"container/heap"
45
"encoding/json"
56
"log"
7+
"math"
68
"strings"
79
)
810

911
func minTime(n int, edges [][]int) int {
10-
12+
graph := make([][][]int, n)
13+
for _, edge := range edges {
14+
graph[edge[0]] = append(graph[edge[0]], []int{edge[1], edge[2], edge[3]})
15+
}
16+
h := &hp{[]int{0, 0}} // {time, node}
17+
heap.Init(h)
18+
dist := make([]int, n)
19+
for i := range dist {
20+
dist[i] = math.MaxInt32
21+
}
22+
dist[0] = 0
23+
for h.Len() > 0 {
24+
cur := heap.Pop(h).([]int)
25+
time, node := cur[0], cur[1]
26+
if node == n-1 {
27+
return time
28+
}
29+
if time > dist[node] {
30+
continue
31+
}
32+
for _, next := range graph[node] {
33+
nextNode, start, end := next[0], next[1], next[2]
34+
if time <= end {
35+
nextTime := max(time, start) + 1
36+
if nextTime < dist[nextNode] {
37+
dist[nextNode] = nextTime
38+
heap.Push(h, []int{nextTime, nextNode})
39+
}
40+
}
41+
}
42+
}
43+
return -1
44+
}
45+
46+
type hp [][]int
47+
48+
func (h hp) Len() int { return len(h) }
49+
func (h hp) Less(i, j int) bool { return h[i][0] < h[j][0] }
50+
func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
51+
func (h *hp) Push(x any) {
52+
*h = append(*h, x.([]int))
53+
}
54+
func (h *hp) Pop() any {
55+
if len(*h) == 0 {
56+
return nil
57+
}
58+
old := *h
59+
n := len(old)
60+
x := old[n-1]
61+
*h = old[0 : n-1]
62+
return x
1163
}
1264

1365
func Solve(inputJsonValues string) any {

problems/problems_3605/solution.go

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,79 @@ import (
77
)
88

99
func minStable(nums []int, maxC int) int {
10-
10+
n := len(nums)
11+
gs := make([][][]int, n)
12+
for i, num := range nums {
13+
if i > 0 {
14+
gs[i] = make([][]int, len(gs[i-1]))
15+
for j := range gs[i-1] {
16+
gs[i][j] = make([]int, len(gs[i-1][j]))
17+
copy(gs[i][j], gs[i-1][j])
18+
}
19+
}
20+
gs[i] = append(gs[i], []int{num, i})
21+
j := 0
22+
for _, p := range gs[i] {
23+
p[0] = gcd(p[0], num)
24+
if gs[i][j][0] != p[0] {
25+
j++
26+
gs[i][j] = p
27+
}
28+
}
29+
gs[i] = gs[i][:j+1]
30+
}
31+
32+
check := func(k int) bool {
33+
count := 0
34+
if k == 0 {
35+
for _, num := range nums {
36+
if num != 1 {
37+
count++
38+
if count > maxC {
39+
return false
40+
}
41+
}
42+
}
43+
} else {
44+
for i := n - 1; i >= 0; {
45+
found := false
46+
for j := len(gs[i]) - 1; j >= 0; j-- {
47+
v, idx := gs[i][j][0], gs[i][j][1]
48+
if v != 1 && i-idx+1 > k {
49+
count++
50+
if count > maxC {
51+
return false
52+
}
53+
i = max(i-k, idx) - 1
54+
found = true
55+
break
56+
}
57+
}
58+
if !found {
59+
i--
60+
}
61+
}
62+
}
63+
return true
64+
}
65+
66+
left, right := 0, n
67+
for left < right {
68+
mid := (left + right) / 2
69+
if check(mid) {
70+
right = mid
71+
} else {
72+
left = mid + 1
73+
}
74+
}
75+
return left
76+
}
77+
78+
func gcd(a, b int) int {
79+
for a != 0 {
80+
a, b = b%a, a
81+
}
82+
return b
1183
}
1284

1385
func Solve(inputJsonValues string) any {

problems/problems_3606/solution.go

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,52 @@ package problem3606
33
import (
44
"encoding/json"
55
"log"
6+
"sort"
67
"strings"
78
)
89

9-
func validateCoupons(code []string, businessLine []string, isActive []bool) []string {
10-
10+
var BUSINESS = map[string]int{
11+
"electronics": 0,
12+
"grocery": 1,
13+
"pharmacy": 2,
14+
"restaurant": 3,
15+
}
16+
17+
func validateCoupons(code []string, businessLine []string, isActive []bool) (result []string) {
18+
var valids []int
19+
for i, c := range code {
20+
if !isActive[i] {
21+
continue
22+
}
23+
if _, ok := BUSINESS[businessLine[i]]; !ok {
24+
continue
25+
}
26+
if len(c) == 0 {
27+
continue
28+
}
29+
valid := true
30+
for _, char := range c {
31+
// check is alnumeric
32+
if !(('0' <= char && char <= '9') || ('A' <= char && char <= 'Z') || ('a' <= char && char <= 'z') || char == '_') {
33+
valid = false
34+
break
35+
}
36+
}
37+
if valid {
38+
valids = append(valids, i)
39+
}
40+
}
41+
sort.Slice(valids, func(i, j int) bool {
42+
b0, b1 := BUSINESS[businessLine[valids[i]]], BUSINESS[businessLine[valids[j]]]
43+
if b0 != b1 {
44+
return b0 < b1
45+
}
46+
return code[valids[i]] < code[valids[j]]
47+
})
48+
for _, idx := range valids {
49+
result = append(result, code[idx])
50+
}
51+
return
1152
}
1253

1354
func Solve(inputJsonValues string) any {

0 commit comments

Comments
 (0)