Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f8a9ca2

Browse files
committed
[02/26] 實際02/16, 84_largestRectangleArea, 沒有stack 會timeout,hard先放著
1 parent f98675c commit f8a9ca2

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

‎src/84_largestRectangleArea/algo.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package algo
2+
3+
func largestRectangleArea_v0(heights []int) int {
4+
// 當是一個超級大的正長方形,會Time Limit Exceeded
5+
area := 0
6+
for i:=0;i<len(heights);i++{
7+
v := dfs(&heights, i, heights[i])
8+
if v >area{
9+
area = v
10+
}
11+
}
12+
return area
13+
}
14+
func dfs(heights *[]int, idx, target int) int{
15+
// left
16+
left_idx := idx-1
17+
left := 0
18+
for left_idx >=0 && (*heights)[left_idx] >= target{
19+
left += target
20+
left_idx --
21+
}
22+
right_idx := idx+1
23+
right := 0
24+
for right_idx < len(*heights) && (*heights)[right_idx] >= target{
25+
right += target
26+
right_idx++
27+
}
28+
return left+right+target
29+
}
30+
31+
func largestRectangleArea(heights []int) int {
32+
// 當是一個超級大的正長方形,會Twime Limit Exceeded
33+
area := 0
34+
for i:=0;i<len(heights);i++{
35+
v := dfs(&heights, i, heights[i])
36+
if v >area{
37+
area = v
38+
}
39+
}
40+
return area
41+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package algo
2+
import "testing"
3+
4+
func TestLargestRectangleArea(t *testing.T){
5+
heights := []int{2,1,5,6,2,3}
6+
actual := largestRectangleArea(heights)
7+
8+
if actual == 10{
9+
t.Log("Success")
10+
} else{
11+
t.Error("Fail")
12+
}
13+
14+
}

‎src/84_largestRectangleArea/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module algo
2+
3+
go 1.17

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /