-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #921 from neverlish/w6
[jinho] week 06
- Loading branch information
Showing
3 changed files
with
138 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,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 | ||
} |
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,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 | ||
|
||
} |
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 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 | ||
} |