738.单调递增的数字
题目链接:https://leetcode.cn/problems/monotone-increasing-digits/
class Solution:
def monotoneIncreasingDigits(self, n: int) -> int:
n = list(str(n))
for i in range(len(n)-1,0,-1):
if int(n[i]) < int(n[i-1]):
n[i-1] = str(int(n[i-1])-1)
n[i:] = '9'*(len(n)-i)
return int(''.join(n))
714.买卖股票的最佳时机含手续费
题目链接:https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/
一定要和 122.买卖股票的最佳时机II 区分开
一开始做题我直接用之前写的122的思路来写 但很快也发现了思维漏洞
class Solution:
def maxProfit(self, prices: List[int], fee: int) -> int:
profit = 0
min_price = prices[0]
for i in range(1, len(prices)):
if prices[i] < min_price:
min_price = prices[i]
elif prices[i] > (min_price + fee):
profit += prices[i] - min_price - fee
min_price = prices[i] - fee
else:
continue
return profit