Skip to content

Commit b61dba0

Browse files
committed
add new files
1 parent aa0d535 commit b61dba0

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

713. Subarray Product Less Than K.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
/**
4+
滑动窗口
5+
*/
6+
7+
func numSubarrayProductLessThanK(nums []int, k int) int {
8+
if k <= 1 {
9+
return 0
10+
}
11+
count, left, one := 0, 0, 1
12+
for right := 0; right < len(nums); right++ {
13+
one *= nums[right]
14+
for one >= k {
15+
one /= nums[left]
16+
left++
17+
}
18+
count += right - left + 1
19+
}
20+
return count
21+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
func maxProfit(prices []int, fee int) int {
4+
cash, hold := 0, -prices[0]
5+
for i := 1; i < len(prices); i++ {
6+
cash = max(cash, hold+prices[i]-fee)
7+
hold = max(hold, cash-prices[i])
8+
}
9+
return cash
10+
}
11+
12+
func max(x, y int) int {
13+
if x > y {
14+
return x
15+
}
16+
return y
17+
}

868. Binary Gap.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ func binaryGap(N int) int {
77
return 0
88
}
99

10-
for ; N > 0; N >>= 1 {
11-
if N&1 == 1 {
10+
for ; N > 0; N >>= 1 { //N不断的右移
11+
if N&1 == 1 { //判断是否等于1
1212
if max == 0 {
1313
max = 1
1414
}

0 commit comments

Comments
 (0)