Skip to content

Commit ba86e4c

Browse files
authored
week03 (#768)
1 parent 1cb3c15 commit ba86e4c

File tree

5 files changed

+236
-0
lines changed

5 files changed

+236
-0
lines changed

combination-sum/neverlish.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// 시간복잡도: O(n^2)
2+
// 공간복잡도: O(n^2)
3+
4+
package main
5+
6+
import "testing"
7+
8+
func TestCombinationSum(t *testing.T) {
9+
result1 := combinationSum([]int{2, 3, 6, 7}, 7)
10+
11+
if len(result1) != 2 {
12+
t.Errorf("Expected 2, got %d", len(result1))
13+
}
14+
15+
if len(result1[0]) != 3 && len(result1[1]) != 1 {
16+
t.Errorf("Expected [[7], [2, 2, 3]], got %v", result1)
17+
}
18+
19+
result2 := combinationSum([]int{2, 3, 5}, 8)
20+
21+
if len(result2) != 3 {
22+
t.Errorf("Expected 3, got %d", len(result2))
23+
}
24+
25+
if len(result2[0]) != 2 && len(result2[1]) != 3 && len(result2[2]) != 3 {
26+
t.Errorf("Expected [[2, 2, 2, 2], [2, 3, 3], [3, 5]], got %v", result2)
27+
}
28+
}
29+
30+
func combinationSum(candidates []int, target int) [][]int {
31+
dp := make([][][]int, target+1)
32+
33+
dp[0] = [][]int{{}}
34+
35+
for _, candidate := range candidates {
36+
for i := candidate; i <= target; i++ {
37+
for _, combination := range dp[i-candidate] {
38+
newCombination := append([]int{candidate}, combination...)
39+
dp[i] = append(dp[i], newCombination)
40+
}
41+
}
42+
}
43+
44+
return dp[target]
45+
}

maximum-subarray/neverlish.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// 시간복잡도: O(n)
2+
// 공간복잡도: O(n)
3+
4+
package main
5+
6+
import "testing"
7+
8+
func TestMaxSubArray(t *testing.T) {
9+
result1 := maxSubArray([]int{-2,1,-3,4,-1,2,1,-5,4})
10+
11+
if result1 != 6 {
12+
t.Errorf("Expected 6, got %d", result1)
13+
}
14+
15+
result2 := maxSubArray([]int{1})
16+
17+
if result2 != 1 {
18+
t.Errorf("Expected 1, got %d", result2)
19+
}
20+
21+
result3 := maxSubArray([]int{5,4,-1,7,8})
22+
23+
if result3 != 23 {
24+
t.Errorf("Expected 23, got %d", result3)
25+
}
26+
}
27+
28+
func max(nums ...int) int {
29+
result := nums[0]
30+
31+
for _, num := range nums[1:] {
32+
if num > result {
33+
result = num
34+
}
35+
}
36+
37+
return result
38+
}
39+
40+
func maxSubArray(nums []int) int {
41+
dp := make([]int, len(nums))
42+
43+
dp[0] = nums[0]
44+
45+
for index, num := range nums[1:] {
46+
dp[index+1] = max(0, dp[index]) + num
47+
48+
}
49+
50+
return max(dp...)
51+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// 시간복잡도 : O(n)
2+
// 공간복잡도 : O(1)
3+
4+
package main
5+
6+
import "testing"
7+
8+
func TestProductExceptSelf(t *testing.T) {
9+
test1 := productExceptSelf([]int{1, 2, 3, 4})
10+
if len(test1) != 4 {
11+
t.Errorf("Expected 4, got %d", len(test1))
12+
}
13+
14+
if test1[0] != 24 && test1[1] != 12 && test1[2] != 8 && test1[3] != 6 {
15+
t.Errorf("Expected [24, 12, 8, 6], got %v", test1)
16+
}
17+
18+
test2 := productExceptSelf([]int{0, 0})
19+
20+
if len(test2) != 2 {
21+
t.Errorf("Expected 2, got %d", len(test2))
22+
}
23+
24+
if test2[0] != 0 && test2[1] != 0 {
25+
t.Errorf("Expected [0, 0], got %v", test2)
26+
}
27+
28+
test3 := productExceptSelf([]int{-1,1,0,-3,3})
29+
if len(test3) != 5 {
30+
t.Errorf("Expected 5, got %d", len(test3))
31+
}
32+
33+
if test3[0] != 0 && test3[1] != 0 && test3[2] != 9 && test3[3] != 0 && test3[4] != 0 {
34+
t.Errorf("Expected [0, 0, 9, 0, 0], got %v", test3)
35+
}
36+
}
37+
38+
func productExceptSelf(nums []int) []int {
39+
zeroCount := 0
40+
product := 1
41+
42+
for _, num := range nums {
43+
if num == 0 {
44+
zeroCount++
45+
} else {
46+
product *= num
47+
}
48+
}
49+
50+
result := make([]int, len(nums))
51+
52+
if zeroCount > 1 {
53+
return result
54+
}
55+
56+
for i, num := range nums {
57+
if zeroCount == 1 {
58+
if num == 0 {
59+
result[i] = product
60+
} else {
61+
result[i] = 0
62+
}
63+
} else {
64+
result[i] = product / num
65+
}
66+
}
67+
68+
return result
69+
}

reverse-bits/neverlish.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// 시간복잡도: O(1)
2+
// 공간복잡도: O(1)
3+
4+
package main
5+
6+
func reverseBits(num uint32) uint32 {
7+
stack := []uint32{}
8+
9+
for i := 0; i < 32; i++ {
10+
stack = append(stack, num&1)
11+
num >>= 1
12+
}
13+
14+
result := uint32(0)
15+
16+
for i := 0; i < 32; i++ {
17+
result <<= 1
18+
result |= stack[i]
19+
}
20+
21+
return result
22+
}

two-sum/neverlish.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// 시간복잡도: O(n)
2+
// 공간복잡도: O(n)
3+
4+
package main
5+
6+
import "testing"
7+
8+
func TestTwoSum(t *testing.T) {
9+
result1 := twoSum([]int{2, 7, 11, 15}, 9)
10+
11+
if len(result1) != 2 {
12+
t.Errorf("Expected 2, got %d", len(result1))
13+
}
14+
15+
if result1[0] != 0 && result1[1] != 1 {
16+
t.Errorf("Expected [0, 1], got %v", result1)
17+
}
18+
19+
result2 := twoSum([]int{3, 2, 4}, 6)
20+
21+
if len(result2) != 2 {
22+
t.Errorf("Expected 2, got %d", len(result2))
23+
}
24+
25+
if result2[0] != 1 && result2[1] != 2 {
26+
t.Errorf("Expected [1, 2], got %v", result2)
27+
}
28+
29+
result3 := twoSum([]int{3, 3}, 6)
30+
if len(result3) != 2 {
31+
t.Errorf("Expected 2, got %d", len(result3))
32+
}
33+
34+
if result3[0] != 0 && result3[1] != 1 {
35+
t.Errorf("Expected [0, 1], got %v", result3)
36+
}
37+
}
38+
39+
func twoSum(nums []int, target int) []int {
40+
seen := map[int]int{}
41+
for i, num := range nums {
42+
complement := target - num
43+
if _, ok := seen[complement]; ok {
44+
return []int{seen[complement], i}
45+
}
46+
seen[num] = i
47+
}
48+
return nil
49+
}

0 commit comments

Comments
 (0)