当前位置:   article > 正文

1005. Maximize Sum Of Array After K Negations 134. Gas Station 135. Candy_如果有rest==0,那么答案就不唯一了

如果有rest==0,那么答案就不唯一了

1005. Maximize Sum Of Array After K Negations

Given an integer array nums and an integer k, modify the array in the following way:

  • choose an index i and replace nums[i] with -nums[i].

You should apply this process exactly k times. You may choose the same index i multiple times.

Return the largest possible sum of the array after modifying it in this way.

1. nums.sort(key = lambda x : abs(x))  : Sort by absolute value, only the order are sorted ,value has not changed.   (You can add reverse = True in parentheses)

2. If k is still left over , and k is odd (k%2 == 1) . nums[0] *= -1

  1. class Solution:
  2. def largestSumAfterKNegations(self, nums: List[int], k: int) -> int:
  3. nums.sort(key = lambda x : abs(x))
  4. for i in range(len(nums)-1, -1, -1):
  5. if k > 0 and nums[i] < 0:
  6. nums[i] *= -1
  7. k -= 1
  8. if k % 2 == 1:
  9. nums[0] *= -1
  10. return sum(nums)

 

134. Gas Station

There are n gas stations along a circular route, where the amount of gas at the ith station is gas[i].

You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from the ith station to its next (i + 1)th station. You begin the journey with an empty tank at one of the gas stations.

Given two integer arrays gas and cost, return the starting gas station's index if you can travel around the circuit once in the clockwise direction, otherwise return -1. If there exists a solution, it is guaranteed to be unique

 

i starts from 0 to accumulate rest[i], and the sum is recorded as curSum, once curSum is less than zero, it means that none of the interval [0, i] can be used as the starting position, because any position chosen as the starting point of this interval will break the oil up to i. Then the starting position is counted from i+1, and then curSum is calculated from zero.

greedy:

  1. class Solution:
  2. def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
  3. curSum = 0 # 当前累计的剩余油量
  4. totalSum = 0 # 总剩余油量
  5. start = 0 # 起始位置
  6. for i in range(len(gas)):
  7. curSum += gas[i] - cost[i]
  8. totalSum += gas[i] - cost[i]
  9. if curSum < 0: # 当前累计剩余油量curSum小于0
  10. start = i + 1 # 起始位置更新为i+1
  11. curSum = 0 # curSum重新从0开始累计
  12. if totalSum < 0:
  13. return -1 # 总剩余油量totalSum小于0,说明无法环绕一圈
  14. return start

violent solution:

index = (i + 1) % len( ) # make a list to be a head-tail connected list (circular route).

  1. class Solution:
  2. def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
  3. for i in range(len(cost)):
  4. rest = gas[i] - cost[i] # 记录剩余油量
  5. index = (i + 1) % len(cost) # 下一个加油站的索引
  6. while rest > 0 and index != i: # 模拟以i为起点行驶一圈(如果有rest==0,那么答案就不唯一了)
  7. rest += gas[index] - cost[index] # 更新剩余油量
  8. index = (index + 1) % len(cost) # 更新下一个加油站的索引
  9. if rest >= 0 and index == i: # 如果以i为起点跑一圈,剩余油量>=0,并且回到起始位置
  10. return i # 返回起始位置i
  11. return -1 # 所有起始位置都无法环绕一圈,返回-1

 

 135. Candy

There are n children standing in a line. Each child is assigned a rating value评价 given in the integer array ratings.

You are giving candies to these children subjected to the following requirements:

  • Each child must have at least one candy.
  • Children with a higher rating get more candies than their neighbors.

Return the minimum number of candies you need to have to distribute the candies to the children.

 

using twice greedy:  left ->right  ,  right ->left 

 Time complexity: O(n)
Space complexity: O(n)

  1. class Solution:
  2. def candy(self, ratings: List[int]) -> int:
  3. candyVec = [1] * len(ratings)
  4. # 从前向后遍历,处理右侧比左侧评分高的情况
  5. for i in range(1, len(ratings)):
  6. if ratings[i] > ratings[i - 1]:
  7. candyVec[i] = candyVec[i - 1] + 1
  8. # 从后向前遍历,处理左侧比右侧评分高的情况
  9. for i in range(len(ratings) - 2, -1, -1):
  10. if ratings[i] > ratings[i + 1]:
  11. candyVec[i] = max(candyVec[i], candyVec[i + 1] + 1)
  12. # 统计结果
  13. result = sum(candyVec)
  14. return result

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/493549
推荐阅读
相关标签
  

闽ICP备14008679号