From 35c9a69292b806907ad34206b1c7f3314a01c430 Mon Sep 17 00:00:00 2001 From: Jinho Hyeon Date: Mon, 23 Dec 2024 21:49:10 +0900 Subject: [PATCH] week03 --- combination-sum/neverlish.go | 45 +++++++++++++++ maximum-subarray/neverlish.go | 51 +++++++++++++++++ product-of-array-except-self/neverlish.go | 69 +++++++++++++++++++++++ reverse-bits/neverlish.go | 22 ++++++++ two-sum/neverlish.go | 49 ++++++++++++++++ 5 files changed, 236 insertions(+) create mode 100644 combination-sum/neverlish.go create mode 100644 maximum-subarray/neverlish.go create mode 100644 product-of-array-except-self/neverlish.go create mode 100644 reverse-bits/neverlish.go create mode 100644 two-sum/neverlish.go diff --git a/combination-sum/neverlish.go b/combination-sum/neverlish.go new file mode 100644 index 000000000..3e74ee415 --- /dev/null +++ b/combination-sum/neverlish.go @@ -0,0 +1,45 @@ +// 시간복잡도: O(n^2) +// 공간복잡도: O(n^2) + +package main + +import "testing" + +func TestCombinationSum(t *testing.T) { + result1 := combinationSum([]int{2, 3, 6, 7}, 7) + + if len(result1) != 2 { + t.Errorf("Expected 2, got %d", len(result1)) + } + + if len(result1[0]) != 3 && len(result1[1]) != 1 { + t.Errorf("Expected [[7], [2, 2, 3]], got %v", result1) + } + + result2 := combinationSum([]int{2, 3, 5}, 8) + + if len(result2) != 3 { + t.Errorf("Expected 3, got %d", len(result2)) + } + + if len(result2[0]) != 2 && len(result2[1]) != 3 && len(result2[2]) != 3 { + t.Errorf("Expected [[2, 2, 2, 2], [2, 3, 3], [3, 5]], got %v", result2) + } +} + +func combinationSum(candidates []int, target int) [][]int { + dp := make([][][]int, target+1) + + dp[0] = [][]int{{}} + + for _, candidate := range candidates { + for i := candidate; i <= target; i++ { + for _, combination := range dp[i-candidate] { + newCombination := append([]int{candidate}, combination...) + dp[i] = append(dp[i], newCombination) + } + } + } + + return dp[target] +} diff --git a/maximum-subarray/neverlish.go b/maximum-subarray/neverlish.go new file mode 100644 index 000000000..5f0b7b876 --- /dev/null +++ b/maximum-subarray/neverlish.go @@ -0,0 +1,51 @@ +// 시간복잡도: O(n) +// 공간복잡도: O(n) + +package main + +import "testing" + +func TestMaxSubArray(t *testing.T) { + result1 := maxSubArray([]int{-2,1,-3,4,-1,2,1,-5,4}) + + if result1 != 6 { + t.Errorf("Expected 6, got %d", result1) + } + + result2 := maxSubArray([]int{1}) + + if result2 != 1 { + t.Errorf("Expected 1, got %d", result2) + } + + result3 := maxSubArray([]int{5,4,-1,7,8}) + + if result3 != 23 { + t.Errorf("Expected 23, got %d", result3) + } +} + +func max(nums ...int) int { + result := nums[0] + + for _, num := range nums[1:] { + if num > result { + result = num + } + } + + return result +} + +func maxSubArray(nums []int) int { + dp := make([]int, len(nums)) + + dp[0] = nums[0] + + for index, num := range nums[1:] { + dp[index+1] = max(0, dp[index]) + num + + } + + return max(dp...) +} diff --git a/product-of-array-except-self/neverlish.go b/product-of-array-except-self/neverlish.go new file mode 100644 index 000000000..4d8f079c3 --- /dev/null +++ b/product-of-array-except-self/neverlish.go @@ -0,0 +1,69 @@ +// 시간복잡도 : O(n) +// 공간복잡도 : O(1) + +package main + +import "testing" + +func TestProductExceptSelf(t *testing.T) { + test1 := productExceptSelf([]int{1, 2, 3, 4}) + if len(test1) != 4 { + t.Errorf("Expected 4, got %d", len(test1)) + } + + if test1[0] != 24 && test1[1] != 12 && test1[2] != 8 && test1[3] != 6 { + t.Errorf("Expected [24, 12, 8, 6], got %v", test1) + } + + test2 := productExceptSelf([]int{0, 0}) + + if len(test2) != 2 { + t.Errorf("Expected 2, got %d", len(test2)) + } + + if test2[0] != 0 && test2[1] != 0 { + t.Errorf("Expected [0, 0], got %v", test2) + } + + test3 := productExceptSelf([]int{-1,1,0,-3,3}) + if len(test3) != 5 { + t.Errorf("Expected 5, got %d", len(test3)) + } + + if test3[0] != 0 && test3[1] != 0 && test3[2] != 9 && test3[3] != 0 && test3[4] != 0 { + t.Errorf("Expected [0, 0, 9, 0, 0], got %v", test3) + } +} + +func productExceptSelf(nums []int) []int { + zeroCount := 0 + product := 1 + + for _, num := range nums { + if num == 0 { + zeroCount++ + } else { + product *= num + } + } + + result := make([]int, len(nums)) + + if zeroCount > 1 { + return result + } + + for i, num := range nums { + if zeroCount == 1 { + if num == 0 { + result[i] = product + } else { + result[i] = 0 + } + } else { + result[i] = product / num + } + } + + return result +} diff --git a/reverse-bits/neverlish.go b/reverse-bits/neverlish.go new file mode 100644 index 000000000..8405bd9be --- /dev/null +++ b/reverse-bits/neverlish.go @@ -0,0 +1,22 @@ +// 시간복잡도: O(1) +// 공간복잡도: O(1) + +package main + +func reverseBits(num uint32) uint32 { + stack := []uint32{} + + for i := 0; i < 32; i++ { + stack = append(stack, num&1) + num >>= 1 + } + + result := uint32(0) + + for i := 0; i < 32; i++ { + result <<= 1 + result |= stack[i] + } + + return result +} diff --git a/two-sum/neverlish.go b/two-sum/neverlish.go new file mode 100644 index 000000000..b8bc6f067 --- /dev/null +++ b/two-sum/neverlish.go @@ -0,0 +1,49 @@ +// 시간복잡도: O(n) +// 공간복잡도: O(n) + +package main + +import "testing" + +func TestTwoSum(t *testing.T) { + result1 := twoSum([]int{2, 7, 11, 15}, 9) + + if len(result1) != 2 { + t.Errorf("Expected 2, got %d", len(result1)) + } + + if result1[0] != 0 && result1[1] != 1 { + t.Errorf("Expected [0, 1], got %v", result1) + } + + result2 := twoSum([]int{3, 2, 4}, 6) + + if len(result2) != 2 { + t.Errorf("Expected 2, got %d", len(result2)) + } + + if result2[0] != 1 && result2[1] != 2 { + t.Errorf("Expected [1, 2], got %v", result2) + } + + result3 := twoSum([]int{3, 3}, 6) + if len(result3) != 2 { + t.Errorf("Expected 2, got %d", len(result3)) + } + + if result3[0] != 0 && result3[1] != 1 { + t.Errorf("Expected [0, 1], got %v", result3) + } +} + +func twoSum(nums []int, target int) []int { + seen := map[int]int{} + for i, num := range nums { + complement := target - num + if _, ok := seen[complement]; ok { + return []int{seen[complement], i} + } + seen[num] = i + } + return nil +}