-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
236 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |