Skip to content

Commit

Permalink
Merge pull request #921 from neverlish/w6
Browse files Browse the repository at this point in the history
[jinho] week 06
  • Loading branch information
SamTheKorean authored Jan 19, 2025
2 parents e3aa241 + 28a6355 commit 0b25e76
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 0 deletions.
40 changes: 40 additions & 0 deletions container-with-most-water/neverlish.go
Original file line number Diff line number Diff line change
@@ -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
}
47 changes: 47 additions & 0 deletions longest-increasing-subsequence/neverlish.go
Original file line number Diff line number Diff line change
@@ -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

}
51 changes: 51 additions & 0 deletions valid-parentheses/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 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
}

0 comments on commit 0b25e76

Please sign in to comment.