Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e2102ac

Browse files
author
侯利朋
committed
1124 err
1 parent 733a1c1 commit e2102ac

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* @lc app=leetcode.cn id=1124 lang=golang
3+
*
4+
* [1124] 表现良好的最长时间段
5+
*/
6+
// 典型错误,思路错误,可以右前缀0
7+
// [6,9,9]
8+
func longestWPI(hours []int) int {
9+
// 统计窗口中的劳累-不劳累天数,负值直接舍去
10+
rtn := 0
11+
curMax := 0
12+
time := 0 // 劳累-不劳累
13+
for i := 0; i < len(hours); i++ {
14+
if hours[i] > 8 {
15+
time++
16+
} else {
17+
time--
18+
}
19+
if time > 0 {
20+
curMax++
21+
if curMax > rtn {
22+
rtn = curMax
23+
}
24+
} else {
25+
time = 0
26+
curMax = 0
27+
}
28+
}
29+
if curMax > rtn {
30+
rtn = curMax
31+
}
32+
return rtn
33+
}

921.使括号有效的最少添加.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* @lc app=leetcode.cn id=921 lang=golang
3+
*
4+
* [921] 使括号有效的最少添加
5+
*/
6+
// 0 ms
7+
func minAddToMakeValid(S string) int {
8+
rtn := 0
9+
stack := 0 // 左括号的数量,没必要真的保存起来,计数就行
10+
for i := 0; i < len(S); i++ {
11+
if S[i] == '(' {
12+
stack++
13+
} else {
14+
if stack == 0 {
15+
rtn++
16+
} else {
17+
stack--
18+
}
19+
}
20+
}
21+
return rtn + stack
22+
}

0 commit comments

Comments
 (0)