2 Star 0 Fork 0

ShiboJiang / per_leetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
121.买卖股票的最佳时机.py 944 Bytes
一键复制 编辑 原始数据 按行查看 历史
姜世博 提交于 2021-09-22 23:35 . add 121
#
# @lc app=leetcode.cn id=121 lang=python3
#
# [121] 买卖股票的最佳时机
#
# @lc code=start
class Solution:
def maxProfit1(self, prices: List[int]) -> int:
nums = prices
sub_len = 2
max_fit = 0
while sub_len <= len(nums):
sub_list_id = 0
for sub_list_id in range(len(nums)-sub_len+1):
sub_list = list()
for num,tp_data in enumerate(nums[sub_list_id:]):
sub_list.append(tp_data)
if (num+1) == sub_len:
break
# Get sub list sum and compare
tp_vle = sub_list[-1] - sub_list[0]
if tp_vle > max_fit:
max_fit = tp_vle
sub_list_id = sub_list_id + 1
sub_len = sub_len + 1
# max fit post process
if max_fit < 0:
max_fit = 0
return max_fit
# @lc code=end
1
https://gitee.com/qq353838430/per_leetcode.git
git@gitee.com:qq353838430/per_leetcode.git
qq353838430
per_leetcode
per_leetcode
master

搜索帮助