File tree Expand file tree Collapse file tree 2 files changed +45
-1
lines changed
Expand file tree Collapse file tree 2 files changed +45
-1
lines changed Original file line number Diff line number Diff line change 33#
44# [154] 寻找旋转排序数组中的最小值 II
55#
6-
76# @lc code=start
87class 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
Original file line number Diff line number Diff line change 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+
You can’t perform that action at this time.
0 commit comments