From d511a4f66cd998ca9ea22b65daa91a24bbfcba94 Mon Sep 17 00:00:00 2001 From: Jinho Hyeon Date: Sun, 19 Jan 2025 09:55:22 +0900 Subject: [PATCH 1/2] week 06 --- container-with-most-water/neverlish.go | 37 ++++++++++++++++ longest-increasing-subsequence/neverlish.go | 44 +++++++++++++++++++ valid-parentheses/neverlish.go | 48 +++++++++++++++++++++ 3 files changed, 129 insertions(+) create mode 100644 container-with-most-water/neverlish.go create mode 100644 longest-increasing-subsequence/neverlish.go create mode 100644 valid-parentheses/neverlish.go diff --git a/container-with-most-water/neverlish.go b/container-with-most-water/neverlish.go new file mode 100644 index 000000000..532a82f59 --- /dev/null +++ b/container-with-most-water/neverlish.go @@ -0,0 +1,37 @@ +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..a3eb0fbeb --- /dev/null +++ b/longest-increasing-subsequence/neverlish.go @@ -0,0 +1,44 @@ +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..467f5bf33 --- /dev/null +++ b/valid-parentheses/neverlish.go @@ -0,0 +1,48 @@ +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 +} From 28a6355d93ba5251dee7ae8689f285be52696fd1 Mon Sep 17 00:00:00 2001 From: Jinho Hyeon Date: Sun, 19 Jan 2025 09:59:33 +0900 Subject: [PATCH 2/2] =?UTF-8?q?=EB=B3=B5=EC=9E=A1=EB=8F=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- container-with-most-water/neverlish.go | 3 +++ longest-increasing-subsequence/neverlish.go | 3 +++ valid-parentheses/neverlish.go | 3 +++ 3 files changed, 9 insertions(+) diff --git a/container-with-most-water/neverlish.go b/container-with-most-water/neverlish.go index 532a82f59..a10273214 100644 --- a/container-with-most-water/neverlish.go +++ b/container-with-most-water/neverlish.go @@ -1,3 +1,6 @@ +// 시간복잡도: O(n) +// 공간복잡도: O(1) + package main import "testing" diff --git a/longest-increasing-subsequence/neverlish.go b/longest-increasing-subsequence/neverlish.go index a3eb0fbeb..5de5d684a 100644 --- a/longest-increasing-subsequence/neverlish.go +++ b/longest-increasing-subsequence/neverlish.go @@ -1,3 +1,6 @@ +// 시간복잡도: O(n^2) +// 공간복잡도: O(n) + package main import "testing" diff --git a/valid-parentheses/neverlish.go b/valid-parentheses/neverlish.go index 467f5bf33..12f0098bf 100644 --- a/valid-parentheses/neverlish.go +++ b/valid-parentheses/neverlish.go @@ -1,3 +1,6 @@ +// 시간복잡도: O(n) +// 공간복잡도: O(n) + package main import "testing"