代码随想录算法训练营第34天 |1005、134(收藏)、135(收藏)

总结

1.学会在原来K上操作
2.while循环和for循环使用条件,while更适合在环形中使用

1005.K次取反后最大化的数组和

题目链接:

leecode-1005

自己想

1.自己想代码:

class Solution:
    def largestSumAfterKNegations(self, nums: List[int], k: int) -> int:
        #先排序
        nums.sort()
        count = 0
        if nums[0] >= 0 and k % 2:
            return -nums[0] + sum(nums[1:])
            
        for i in range(0,k):
            if i < len(nums) and nums[i] < 0:
                nums[i] = -nums[i]
            elif  i >= len(nums) or nums[i] >=0 :
                a = k - i 
                if not a%2:
                    return sum(nums)
                else:
                    return (-min(nums))*2 + sum(nums)

            

        return sum(nums)
看完题解

1.有两个思路需要借鉴,第一个就是在原始k上进行操作,就不需要去判断len(nums)与i的大小了,同时也不用进行a = k-i的操作了。
2.我想到如果k大于len(nums),那么就要对绝对值最小的数进行取反,与其这样,不如一开始就按照绝对值最小进行排序

A.sort(key = lambda x:abs(x),reversed= True)
完整代码

134. 加油站

题目链接:

leecode-134

自己想

1.暴力解法
暴力解法考验代码的技巧,
1)比如while 和 for 的使用。while循环适合解决环形问题,而for只是从头遍历到尾

看完题解

1.贪心,只要到第i号加油站的剩余量累计和<0,那么[0,i]都不能作为起点!!!,只能从i+1开始尝试,同时要重置累计和为0

完整代码
class Solution:
    def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
    	###暴力解法
        # sum_gas = sum(gas)
        # sum_cost = sum(cost)
        # if sum_gas < sum_cost:
        #     return -1

        # res = 0
        # index = 0
        # for i in range(len(gas)):
        #     res = gas[i] - cost[i]
        #     index = (i+1) % len(gas)
        #     while res > 0 and index != i:
        #         res += gas[index] - cost[index]
        #         index = (index+1)%len(gas)

        #     if index == i and res >= 0:
        #         return index
        # return -1
        ###贪心算法
        sum_gas = sum(gas)
        sum_cost = sum(cost)
        if sum_gas < sum_cost:
            return -1
        res = 0
        count = 0
        for i in range(len(gas)):
            res += gas[i] - cost[i]
            if res < 0 :
                count = i+1
                res = 0

        
        return count

135. 分发糖果

题目链接:

leecode-分发糖果

自己想

1.是自己想出来的,但是感觉写的有些复杂。主要思路先从左到右处理,然后再从右到左处理

看完题解
完整代码
class Solution(object):
    def candy(self, ratings):
        """
        :type ratings: List[int]
        :rtype: int
        """
        temp_list = []
        count_1 = len(ratings)
        count_2 = [1]*len(ratings)
        for i in range (1,len(ratings)):
            if ratings[i-1] > ratings[i] and i-1 not in temp_list:
                temp_list.append(i-1)
                count_2[i-1] = count_2[i] + 1
            elif ratings[i-1] < ratings[i]:
                count_2[i] = count_2[i-1] + 1
                temp_list.append(i)
        # print(count_2)
        temp_list = []
        for i in range(len(ratings) - 2,-1,-1):
            if ratings[i+1] > ratings[i] and i+1 not in temp_list and count_2[i+1] <= count_2[i]:
                temp_list.append(i+1)
                count_2[i+1] = count_2[i] + 1   
            elif ratings[i+1] < ratings[i] and i not in temp_list and count_2[i+1] >= count_2[i]:
                count_2[i] = count_2[i+1] + 1   
                temp_list.append(i)

            count_1 += count_2[i]#这句话有bug的原因是因为在第一个if里修改的是count_2[i+1],而不是count_2[i]
        # print(count_2)
        count_1 = sum(count_2)
        return count_1

题目链接:

自己想
看完题解
完整代码
在这里插入代码片
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值