Skip to content

Commit

Permalink
week03 (#768)
Browse files Browse the repository at this point in the history
  • Loading branch information
neverlish authored Dec 28, 2024
1 parent 1cb3c15 commit ba86e4c
Show file tree
Hide file tree
Showing 5 changed files with 236 additions and 0 deletions.
45 changes: 45 additions & 0 deletions combination-sum/neverlish.go
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]
}
51 changes: 51 additions & 0 deletions maximum-subarray/neverlish.go
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...)
}
69 changes: 69 additions & 0 deletions product-of-array-except-self/neverlish.go
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
}
22 changes: 22 additions & 0 deletions reverse-bits/neverlish.go
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
}
49 changes: 49 additions & 0 deletions two-sum/neverlish.go
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
}

0 comments on commit ba86e4c

Please sign in to comment.