leetcode 刷题 134 135
【摘要】
class Solution: def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int: m = s = 0 s_min = gas[0] for i in range(len(gas)): s += gas[i] - cost[i] if s < s_min: s_m...
class Solution: def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int: m = s = 0 s_min = gas[0] for i in range(len(gas)): s += gas[i] - cost[i] if s < s_min: s_min = s m = i return (m + 1) % len(gas) if s >= 0 else -1
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
class Solution: def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int: start = end = tank = 0 for _ in range(len(gas)): change = gas[end] - cost[end] if tank + change < 0: start = (start - 1) % len(gas) tank += gas[start] - cost[start] else: tank += change end = (end + 1) % len(gas) return -1 if tank < 0 else start
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
class Solution: def candy(self, ratings): """ :type ratings: List[int] :rtype: int """ result=[1]*len(ratings) for i in range(1,len(ratings)): if ratings[i]>ratings[i-1]: result[i]=result[i-1]+1 for i in range(len(ratings)-1,0,-1): if ratings[i-1]>ratings[i]: result[i-1]=max([result[i-1],result[i]+1]) return sum(result)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
class Solution: def candy(self, ratings: List[int]) -> int: """ :type ratings: List[int] :rtype: int """ n = len(ratings) child = [1] * n for r, i in sorted((r, i) for i, r in enumerate(ratings)): lower = [child[j]+1 for j in (i-1, i+1) if -1 < j < n and ratings[j] < r] child[i] = max(lower or [1]) return sum(child)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
文章来源: maoli.blog.csdn.net,作者:刘润森!,版权归原作者所有,如需转载,请联系作者。
原文链接:maoli.blog.csdn.net/article/details/90743765
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)