diff --git a/container-with-most-water/neverlish.go b/container-with-most-water/neverlish.go new file mode 100644 index 000000000..a10273214 --- /dev/null +++ b/container-with-most-water/neverlish.go @@ -0,0 +1,40 @@ +// 시간복잡도: O(n) +// 공간복잡도: O(1) + +package main + +import "testing" + +func TestMaxArea(t *testing.T) { + result1 := maxArea([]int{1, 8, 6, 2, 5, 4, 8, 3, 7}) + + if result1 != 49 { + t.Errorf("Expected 49, but got %v", result1) + } + + result2 := maxArea([]int{1, 1}) + + if result2 != 1 { + t.Errorf("Expected 1, but got %v", result2) + } +} + +func maxArea(height []int) int { + result := 0 + + pointer_left := 0 + pointer_right := len(height) - 1 + + for pointer_left < pointer_right { + area := (pointer_right - pointer_left) * min(height[pointer_left], height[pointer_right]) + result = max(result, area) + + if height[pointer_left] < height[pointer_right] { + pointer_left++ + } else { + pointer_right-- + } + } + + return result +} diff --git a/longest-increasing-subsequence/neverlish.go b/longest-increasing-subsequence/neverlish.go new file mode 100644 index 000000000..5de5d684a --- /dev/null +++ b/longest-increasing-subsequence/neverlish.go @@ -0,0 +1,47 @@ +// 시간복잡도: O(n^2) +// 공간복잡도: O(n) + +package main + +import "testing" + +func TestLengthOfLIS(t *testing.T) { + result1 := lengthOfLIS([]int{10, 9, 2, 5, 3, 7, 101, 18}) + + if result1 != 4 { + t.Errorf("Expected 4, but got %v", result1) + } + + result2 := lengthOfLIS([]int{0, 1, 0, 3, 2, 3}) + + if result2 != 4 { + t.Errorf("Expected 4, but got %v", result2) + } + + result3 := lengthOfLIS([]int{7, 7, 7, 7, 7, 7, 7}) + + if result3 != 1 { + t.Errorf("Expected 1, but got %v", result3) + } +} + +func lengthOfLIS(nums []int) int { + result := 0 + + dp := make([]int, len(nums)) + + for i := 0; i < len(nums); i++ { + dp[i] = 1 + + for j := 0; j < i; j++ { + if nums[i] > nums[j] { + dp[i] = max(dp[i], dp[j]+1) + } + } + + result = max(result, dp[i]) + } + + return result + +} diff --git a/valid-parentheses/neverlish.go b/valid-parentheses/neverlish.go new file mode 100644 index 000000000..12f0098bf --- /dev/null +++ b/valid-parentheses/neverlish.go @@ -0,0 +1,51 @@ +// 시간복잡도: O(n) +// 공간복잡도: O(n) + +package main + +import "testing" + +func TestIsValid(t *testing.T) { + result1 := isValid("()") + + if result1 != true { + t.Errorf("Expected true, but got %v", result1) + } + + result2 := isValid("()[]{}") + if result2 != true { + t.Errorf("Expected true, but got %v", result2) + } + + result3 := isValid("(]") + if result3 != false { + t.Errorf("Expected false, but got %v", result3) + } + + result4 := isValid("([)]") + if result4 != false { + t.Errorf("Expected false, but got %v", result4) + } +} + +func isValid(s string) bool { + stack := make([]rune, 0) + + for _, c := range s { + switch c { + case '(': + stack = append(stack, ')') + case '{': + stack = append(stack, '}') + case '[': + stack = append(stack, ']') + default: + if len(stack) == 0 || stack[len(stack)-1] != c { + return false + } + stack = stack[:len(stack)-1] + } + } + + return len(stack) == 0 +}