Skip to content

Commit d511a4f

Browse files
committed
week 06
1 parent 16e6492 commit d511a4f

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package main
2+
3+
import "testing"
4+
5+
func TestMaxArea(t *testing.T) {
6+
result1 := maxArea([]int{1, 8, 6, 2, 5, 4, 8, 3, 7})
7+
8+
if result1 != 49 {
9+
t.Errorf("Expected 49, but got %v", result1)
10+
}
11+
12+
result2 := maxArea([]int{1, 1})
13+
14+
if result2 != 1 {
15+
t.Errorf("Expected 1, but got %v", result2)
16+
}
17+
}
18+
19+
func maxArea(height []int) int {
20+
result := 0
21+
22+
pointer_left := 0
23+
pointer_right := len(height) - 1
24+
25+
for pointer_left < pointer_right {
26+
area := (pointer_right - pointer_left) * min(height[pointer_left], height[pointer_right])
27+
result = max(result, area)
28+
29+
if height[pointer_left] < height[pointer_right] {
30+
pointer_left++
31+
} else {
32+
pointer_right--
33+
}
34+
}
35+
36+
return result
37+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package main
2+
3+
import "testing"
4+
5+
func TestLengthOfLIS(t *testing.T) {
6+
result1 := lengthOfLIS([]int{10, 9, 2, 5, 3, 7, 101, 18})
7+
8+
if result1 != 4 {
9+
t.Errorf("Expected 4, but got %v", result1)
10+
}
11+
12+
result2 := lengthOfLIS([]int{0, 1, 0, 3, 2, 3})
13+
14+
if result2 != 4 {
15+
t.Errorf("Expected 4, but got %v", result2)
16+
}
17+
18+
result3 := lengthOfLIS([]int{7, 7, 7, 7, 7, 7, 7})
19+
20+
if result3 != 1 {
21+
t.Errorf("Expected 1, but got %v", result3)
22+
}
23+
}
24+
25+
func lengthOfLIS(nums []int) int {
26+
result := 0
27+
28+
dp := make([]int, len(nums))
29+
30+
for i := 0; i < len(nums); i++ {
31+
dp[i] = 1
32+
33+
for j := 0; j < i; j++ {
34+
if nums[i] > nums[j] {
35+
dp[i] = max(dp[i], dp[j]+1)
36+
}
37+
}
38+
39+
result = max(result, dp[i])
40+
}
41+
42+
return result
43+
44+
}

valid-parentheses/neverlish.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package main
2+
3+
import "testing"
4+
5+
func TestIsValid(t *testing.T) {
6+
result1 := isValid("()")
7+
8+
if result1 != true {
9+
t.Errorf("Expected true, but got %v", result1)
10+
}
11+
12+
result2 := isValid("()[]{}")
13+
if result2 != true {
14+
t.Errorf("Expected true, but got %v", result2)
15+
}
16+
17+
result3 := isValid("(]")
18+
if result3 != false {
19+
t.Errorf("Expected false, but got %v", result3)
20+
}
21+
22+
result4 := isValid("([)]")
23+
if result4 != false {
24+
t.Errorf("Expected false, but got %v", result4)
25+
}
26+
}
27+
28+
func isValid(s string) bool {
29+
stack := make([]rune, 0)
30+
31+
for _, c := range s {
32+
switch c {
33+
case '(':
34+
stack = append(stack, ')')
35+
case '{':
36+
stack = append(stack, '}')
37+
case '[':
38+
stack = append(stack, ']')
39+
default:
40+
if len(stack) == 0 || stack[len(stack)-1] != c {
41+
return false
42+
}
43+
stack = stack[:len(stack)-1]
44+
}
45+
}
46+
47+
return len(stack) == 0
48+
}

0 commit comments

Comments
 (0)