Skip to content

Commit 862bf32

Browse files
committed
codility
1 parent 990fb14 commit 862bf32

File tree

19 files changed

+1057
-18
lines changed

19 files changed

+1057
-18
lines changed

Diff for: Codility/Lesson/0007.Stacks-and-Queues/Brackets/README.md

+53-1
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,56 @@ Copyright 2009–2021 by Codility Limited. All Rights Reserved. Unauthorized cop
2828
## 解題思路
2929
將左括號都放入stack. 遇到右括號時將stack pop出來並檢查pop出來的左括號是否跟右括號配對.
3030
## 來源
31-
https://app.codility.com/programmers/lessons/7-stacks_and_queues/brackets/
31+
https://app.codility.com/programmers/lessons/7-stacks_and_queues/brackets/
32+
33+
## 解答
34+
https://github.com/kimi0230/LeetcodeGolang/blob/master/Codility/Lesson/0007.Stacks-and-Queues/Brackets/Brackets.go
35+
36+
37+
```go
38+
package Brackets
39+
40+
import (
41+
"LeetcodeGolang/Utility/structures"
42+
)
43+
44+
func Solution(S string) int {
45+
if len(S) == 0 {
46+
return 1
47+
}
48+
if len(S)%2 != 0 {
49+
return 0
50+
}
51+
52+
BracketMap := map[string]string{
53+
")": "(",
54+
"]": "[",
55+
"}": "{",
56+
}
57+
58+
stack := structures.NewArrayStack()
59+
for _, v := range S {
60+
val := string(v)
61+
if val == "(" || val == "[" || val == "{" {
62+
stack.Push(val)
63+
} else if val == ")" || val == "]" || val == "}" {
64+
if stack.IsEmpty() {
65+
return 0
66+
}
67+
68+
topVal := stack.Top()
69+
if topVal == BracketMap[val] {
70+
stack.Pop()
71+
} else {
72+
// 找不到可配對的括號
73+
return 0
74+
}
75+
}
76+
}
77+
if stack.IsEmpty() {
78+
return 1
79+
} else {
80+
return 0
81+
}
82+
}
83+
```

Diff for: Codility/Lesson/0007.Stacks-and-Queues/Fish/README.md

+36-1
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,39 @@ A: 活魚的大小, B: 魚游的方向. 如果於相遇的話大魚會吃掉小
5151
如果值為0且stack為空, 代表沒遇到下游的魚所以活魚++
5252

5353
## 來源
54-
https://app.codility.com/programmers/lessons/7-stacks_and_queues/fish/
54+
https://app.codility.com/programmers/lessons/7-stacks_and_queues/fish/
55+
56+
## 解答
57+
https://github.com/kimi0230/LeetcodeGolang/blob/master/Codility/Lesson/0007.Stacks-and-Queues/Fish/Fish.go
58+
59+
60+
```go
61+
package Fish
62+
63+
import "LeetcodeGolang/Utility/structures"
64+
65+
func Solution(A []int, B []int) int {
66+
stack := structures.NewArrayStack()
67+
aliveFish := 0
68+
for idx, val := range B {
69+
if val == 1 {
70+
stack.Push(A[idx])
71+
} else {
72+
// 繼續往下游
73+
for !stack.IsEmpty() {
74+
if stack.Top().(int) < A[idx] {
75+
// stack的魚比遇到的魚還小, stack被吃掉
76+
stack.Pop()
77+
} else {
78+
break
79+
}
80+
}
81+
if stack.IsEmpty() {
82+
aliveFish++
83+
}
84+
}
85+
}
86+
return aliveFish + stack.Size()
87+
}
88+
89+
```

Diff for: Codility/Lesson/0007.Stacks-and-Queues/Nesting/README.md

+35-1
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,38 @@ Copyright 2009–2021 by Codility Limited. All Rights Reserved. Unauthorized cop
2929
與Bracket類似
3030

3131
## 來源
32-
https://app.codility.com/programmers/lessons/7-stacks_and_queues/nesting/
32+
https://app.codility.com/programmers/lessons/7-stacks_and_queues/nesting/
33+
34+
## 解答
35+
https://github.com/kimi0230/LeetcodeGolang/blob/master/Codility/Lesson/0007.Stacks-and-Queues/Nesting/Nesting.go
36+
37+
38+
```go
39+
package Nesting
40+
41+
import "LeetcodeGolang/Utility/structures"
42+
43+
func Solution(S string) int {
44+
if len(S) == 0 {
45+
return 1
46+
}
47+
if len(S)%2 != 0 {
48+
return 0
49+
}
50+
51+
stack := structures.NewArrayStack()
52+
for _, v := range S {
53+
val := string(v)
54+
if val == "(" {
55+
stack.Push(val)
56+
} else if val == ")" {
57+
stack.Pop()
58+
}
59+
}
60+
if stack.IsEmpty() {
61+
return 1
62+
} else {
63+
return 0
64+
}
65+
}
66+
```

Diff for: Codility/Lesson/0007.Stacks-and-Queues/StoneWall/README.md

+26-1
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,29 @@ H[N-1]大表牆N-1米處到最右的高度
4141

4242
## 來源
4343
* https://app.codility.com/programmers/lessons/7-stacks_and_queues/stone_wall/
44-
* https://github.com/Anfany/Codility-Lessons-By-Python3/blob/master/L7_Stacks%20and%20Queues/7.4%20StoneWall.md
44+
* https://github.com/Anfany/Codility-Lessons-By-Python3/blob/master/L7_Stacks%20and%20Queues/7.4%20StoneWall.md
45+
46+
## 解答
47+
https://github.com/kimi0230/LeetcodeGolang/blob/master/Codility/Lesson/0007.Stacks-and-Queues/StoneWall/StoneWall.go
48+
49+
50+
```go
51+
package StoneWall
52+
53+
import "LeetcodeGolang/Utility/structures"
54+
55+
func Solution(H []int) int {
56+
stack := structures.NewArrayStack()
57+
result := 0
58+
for _, v := range H {
59+
for !stack.IsEmpty() && stack.Top().(int) > v {
60+
stack.Pop()
61+
}
62+
if stack.IsEmpty() || stack.Top().(int) < v {
63+
stack.Push(v)
64+
result++
65+
}
66+
}
67+
return result
68+
}
69+
```

Diff for: Codility/Lesson/0008.Leader/Dominator/README.md

+47-1
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,50 @@ Copyright 2009–2021 by Codility Limited. All Rights Reserved. Unauthorized cop
3838
是的話返回此數任一個index, 反之返回-1
3939

4040
## 來源
41-
https://app.codility.com/programmers/lessons/8-leader/dominator/
41+
https://app.codility.com/programmers/lessons/8-leader/dominator/
42+
43+
## 解答
44+
https://github.com/kimi0230/LeetcodeGolang/blob/master/Codility/Lesson/0008.Leader/Dominator/Dominator.go
45+
46+
47+
```go
48+
package Dominator
49+
50+
import (
51+
"math"
52+
)
53+
54+
func Solution(A []int) int {
55+
mapInt := make(map[int]int, len(A))
56+
57+
for _, v := range A {
58+
if _, ok := mapInt[v]; !ok {
59+
mapInt[v] = 1
60+
} else {
61+
mapInt[v]++
62+
}
63+
}
64+
65+
maxCount := 0
66+
maxVal := 0
67+
for k, v := range mapInt {
68+
if v > maxCount {
69+
maxCount = v
70+
maxVal = k
71+
}
72+
}
73+
minIndex := -1
74+
for k, v := range A {
75+
if v == maxVal {
76+
minIndex = k
77+
break
78+
}
79+
}
80+
81+
if maxCount > int(math.Floor(float64(len(A))/2.0)) {
82+
return minIndex
83+
} else {
84+
return -1
85+
}
86+
}
87+
```

Diff for: Codility/Lesson/0008.Leader/EquiLeader/README.md

+44-1
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,47 @@ Copyright 2009–2021 by Codility Limited. All Rights Reserved. Unauthorized cop
5656

5757
## 來源
5858
* https://app.codility.com/programmers/lessons/8-leader/equi_leader/
59-
* https://github.com/Anfany/Codility-Lessons-By-Python3/blob/master/L8_Leader/8.1%20EquiLeader.md
59+
* https://github.com/Anfany/Codility-Lessons-By-Python3/blob/master/L8_Leader/8.1%20EquiLeader.md
60+
61+
## 解答
62+
https://github.com/kimi0230/LeetcodeGolang/blob/master/Codility/Lesson/0008.Leader/EquiLeader/EquiLeader.go
63+
64+
65+
```go
66+
package EquiLeader
67+
68+
func Solution(A []int) int {
69+
leaderDict := make(map[int]int)
70+
for i := 0; i < len(A); i++ {
71+
if _, ok := leaderDict[A[i]]; ok {
72+
leaderDict[A[i]]++
73+
} else {
74+
leaderDict[A[i]] = 1
75+
}
76+
}
77+
78+
leader := 0
79+
times := 0
80+
for k, v := range leaderDict {
81+
if v > times {
82+
times = v
83+
leader = k
84+
}
85+
}
86+
87+
equiCount := 0
88+
count := 0 // 超頻數已出現的次數
89+
90+
for index, v := range A {
91+
if v == leader {
92+
count++
93+
}
94+
if count > (index+1)/2 && (times-count) > (len(A)-(index+1))/2 {
95+
equiCount++
96+
}
97+
98+
}
99+
100+
return equiCount
101+
}
102+
```

Diff for: Codility/Lesson/0009.Maximum-Slice-Problem/MaxDoubleSliceSum/README.md

+42-1
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,45 @@ A[X+1]到A[Y-1] + A[Y+1]到A[Z-1] 最大的和
6161

6262
## 來源
6363
* https://app.codility.com/programmers/lessons/9-maximum_slice_problem/max_double_slice_sum/
64-
* https://github.com/Anfany/Codility-Lessons-By-Python3/blob/master/L9_Maximum%20Slice%20Problem/9.3%20%20MaxDoubleSliceSum.md
64+
* https://github.com/Anfany/Codility-Lessons-By-Python3/blob/master/L9_Maximum%20Slice%20Problem/9.3%20%20MaxDoubleSliceSum.md
65+
66+
## 解答
67+
https://github.com/kimi0230/LeetcodeGolang/blob/master/Codility/Lesson/0009.Maximum-Slice-Problem/MaxDoubleSliceSum/MaxDoubleSliceSum.go
68+
69+
70+
```go
71+
package MaxDoubleSliceSum
72+
73+
import (
74+
"math"
75+
)
76+
77+
func Solution(A []int) int {
78+
if len(A) < 4 {
79+
return 0
80+
}
81+
N := len(A) - 2
82+
forwardSum := make([]int, N)
83+
reverseSum := make([]int, N)
84+
85+
// 0 ≤ X < Y < Z < N,
86+
// A[X + 1] + A[X + 2] + ... + A[Y − 1] + A[Y + 1] + A[Y + 2] + ... + A[Z − 1].
87+
// A : [ 3, 2, 6, -1, 4, 5, -1, 2]
88+
// forwardSum : [ 0, 2, 8, 7, 11, 16]
89+
// reverseSum : [14, 8, 9, 5, 0, 0]
90+
for i := 0; i < N-1; i++ {
91+
forwardVal := A[i+1]
92+
reverseVal := A[N-i]
93+
94+
forwardSum[i+1] = int(math.Max(0, float64(forwardVal)+float64(forwardSum[i])))
95+
reverseSum[N-i-2] = int(math.Max(0, float64(reverseVal)+float64(reverseSum[N-i-1])))
96+
}
97+
98+
combineMax := math.MinInt64
99+
for i := 0; i < N; i++ {
100+
combineMax = int(math.Max(float64(combineMax), float64(forwardSum[i])+float64(reverseSum[i])))
101+
}
102+
103+
return combineMax
104+
}
105+
```

Diff for: Codility/Lesson/0009.Maximum-Slice-Problem/MaxProfit/README.md

+26-1
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,29 @@ Copyright 2009–2021 by Codility Limited. All Rights Reserved. Unauthorized cop
4343
尋遍整個array, 找出最小的買入金額, 同時計算當前的賣出金額-最小買入金額, 得出最大利潤
4444

4545
## 來源
46-
https://app.codility.com/programmers/lessons/9-maximum_slice_problem/max_profit/
46+
https://app.codility.com/programmers/lessons/9-maximum_slice_problem/max_profit/
47+
48+
## 解答
49+
https://github.com/kimi0230/LeetcodeGolang/blob/master/Codility/Lesson/0009.Maximum-Slice-Problem/MaxProfit/MaxProfit.go
50+
51+
52+
```go
53+
package MaxProfit
54+
55+
import (
56+
"math"
57+
)
58+
59+
func Solution(A []int) int {
60+
minBuyPrice := math.MaxFloat64
61+
maxProfit := 0.0
62+
63+
for _, v := range A {
64+
minBuyPrice = math.Min(minBuyPrice, float64(v))
65+
maxProfit = math.Max(maxProfit, float64(v)-minBuyPrice)
66+
}
67+
68+
return int(maxProfit)
69+
}
70+
71+
```

Diff for: Codility/Lesson/0009.Maximum-Slice-Problem/MaxSliceSum/README.md

+26-1
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,29 @@ Copyright 2009–2021 by Codility Limited. All Rights Reserved. Unauthorized cop
3434
當下的值跟當下的值加上先前的和, 取最大值. 再將剛剛算出的最大值跟紀錄中的最大值比較,取最大值
3535

3636
## 來源
37-
https://app.codility.com/programmers/lessons/9-maximum_slice_problem/max_slice_sum/
37+
https://app.codility.com/programmers/lessons/9-maximum_slice_problem/max_slice_sum/
38+
39+
## 解答
40+
https://github.com/kimi0230/LeetcodeGolang/blob/master/Codility/Lesson/0009.Maximum-Slice-Problem/MaxSliceSum/MaxSliceSum.go
41+
42+
43+
```go
44+
package MaxSliceSum
45+
46+
import (
47+
"math"
48+
)
49+
50+
func Solution(A []int) int {
51+
if len(A) == 1 {
52+
return A[0]
53+
}
54+
result := math.MinInt64
55+
sum := math.MinInt64
56+
for i := 0; i < len(A); i++ {
57+
sum = int(math.Max(float64(A[i]), float64(A[i])+float64(sum)))
58+
result = int(math.Max(float64(sum), float64(result)))
59+
}
60+
return result
61+
}
62+
```

0 commit comments

Comments
 (0)