File tree Expand file tree Collapse file tree 2 files changed +55
-0
lines changed Expand file tree Collapse file tree 2 files changed +55
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments