day29 第八章 贪心算法 part03

134. 加油站

"可以换一个思路,首先如果总油量减去总消耗大于等于零那么一定可以跑完一圈,说明 各个站点的加油站 剩油量resti相加一定是大于等于零的。

每个加油站的剩余量resti为gasi - costi。

i从0开始累加resti,和记为curSum,一旦curSum小于零,说明0, i区间都不能作为起始位置,因为这个区间选择任何一个位置作为起点,到i这里都会断油,那么起始位置从i+1算起,再从0计算curSum。" ----代码随想录

python 复制代码
class Solution:
    def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
        rest = []
        n = len(gas)
        for i in range(n):
            rest.append(gas[i]-cost[i])
        
        if sum(rest) < 0:
            return -1

        cur_sum = 0
        start = 0
        for i in range(n):
            cur_sum += rest[i]
            if cur_sum < 0:
                cur_sum = 0
                start = i+1
                # print('start=', start)
        
        if start>=n:
            return -1
        else:
            return start

135. 分发糖果

记得拆分,从左往右和从右往左
"本题涉及到一个思想,就是想处理好一边再处理另一边,不要两边想着一起兼顾,后面还会有题目用到这个思路" --代码随想录

python 复制代码
class Solution:
    def candy(self, ratings: List[int]) -> int:
        n = len(ratings)
        candy_l = [1] * n
        candy_r = [1] * n
        # candy_l
        # candy_r[-1] = 1
        # from left to right, compare to the previous child
        for i in range(1, n):
            if ratings[i] > ratings[i-1]:
                candy_l[i] = candy_l[i-1]+1
        
        # from right to left, compare to the next child
        for i in range(n-1, 0, -1):
            if ratings[i] < ratings[i-1]:
                candy_r[i-1] = candy_r[i] + 1

        candy = []
        for i in range(n):
            candy.append(max(candy_l[i], candy_r[i]))

        # print('candy_l:', candy_l)
        # print('candy_r:', candy_r)
        # print('candy:', candy)

        result = sum(candy) 
        return result

860.柠檬水找零

python 复制代码
class Solution:
    def lemonadeChange(self, bills: List[int]) -> bool:
        changes = {5: 0, 10: 0, 20: 0}
        for bill in bills:
            changes[bill] += 1
            if bill == 5:
                continue
            elif bill == 10:
                if changes[5] == 0:
                    return False
                changes[5] -= 1
            else:
                if changes[10] > 0 and changes[5] > 0:
                    changes[5] -= 1
                    changes[10] -= 1
                elif changes[5] >= 3:
                    changes[5] -= 3
                else:
                    return False
        return True
        

406.根据身高重建队列

list的insert方法时间复杂度是O(n)。

要注意语言,尽量深耕一门语言。

python 复制代码
class Solution:
    def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
        people.sort(key=lambda x: (-x[0], x[1])) # 第一个数从大到小排,第二个数从小到大排
        que = []

        for p in people:
            que.insert(p[1], p)
        return que
相关推荐
青少儿编程课堂1 小时前
蓝桥杯青少组图形化真题:校园无人机编队灯光秀解析(全国青少年信息素养大赛·电子学会12月图形化考级同源考点)
c++·python·算法·bfs·信息学竞赛
Shadow(⊙o⊙)1 小时前
专题六:模拟算法
算法
民乐团扒谱机16 小时前
【超详细】基频音高提取算法之SWIPE 算法深度解析:频谱匹配的精度极限与工程边界
人工智能·算法
饮水桶_T16 小时前
C++ stack & queue复习笔记
开发语言·笔记·算法
wy_hhxx17 小时前
[Leetcode] 滑动窗口
算法·leetcode·滑动窗口
码途行者17 小时前
每天10分钟学Go:第20课 泛型
c++·算法·golang
AIgorithmGEEK19 小时前
算法·哈希表|从“逐个比”到“按 key 算位置”
数据结构·c++·算法·leetcode·hash
风落青芜21 小时前
排序算法(二):选择排序、堆排序与冒泡排序
c语言·开发语言·数据结构·算法·排序算法
Navigator_Z1 天前
LeetCode //C - 1319. Number of Operations to Make Network Connected
c语言·算法·leetcode