Skip to content

Commit a90d17e

Browse files
committed
update
1 parent 566dab9 commit a90d17e

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

code/154.寻找旋转排序数组中的最小值-ii.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,26 @@
33
#
44
# [154] 寻找旋转排序数组中的最小值 II
55
#
6-
76
# @lc code=start
87
class Solution:
98
def findMin(self, nums: List[int]) -> int:
9+
s = 0
10+
e = len(nums) - 1
11+
while s < e:
12+
m = (s + e) // 2
13+
n = nums[m]
14+
n1, n2 = nums[s], nums[e]
15+
if n1 <= n < n2:
16+
e = m -1
17+
elif n1 < n > n2:
18+
s = m + 1
19+
elif n1 == n or n2 == n:
20+
if n2 == n:
21+
e -= 1
22+
if n1 == n:
23+
s += 1
24+
else:
25+
e = m
26+
27+
return nums[s]
1028
# @lc code=end
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#
2+
# @lc app=leetcode.cn id=188 lang=python3
3+
#
4+
# [188] 买卖股票的最佳时机 IV
5+
#
6+
from typing import List
7+
# @lc code=start
8+
class Solution:
9+
def maxProfit(self, k: int, prices: List[int]) -> int:
10+
bs = [0] * (2 * k)
11+
for i in range(k):
12+
bs[i * 2] = -prices[0]
13+
14+
for price in prices[1:]:
15+
for i in range(k):
16+
bse = [bs[i * 2]]
17+
if i != 0:
18+
bse.append(bs[i * 2 - 1] - price)
19+
else:
20+
bse.append(-price)
21+
22+
bs[i * 2] = max(bse)
23+
bs[i * 2 + 1] = max([bs[i * 2] + price, bs[i * 2 + 1]])
24+
return max(bs)
25+
# @lc code=end
26+

0 commit comments

Comments
 (0)