赞
踩
1005. Maximize Sum Of Array After K Negations
Given an integer array nums
and an integer k
, modify the array in the following way:
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
- class Solution:
- def largestSumAfterKNegations(self, nums: List[int], k: int) -> int:
- nums.sort(key = lambda x : abs(x))
-
- for i in range(len(nums)-1, -1, -1):
- if k > 0 and nums[i] < 0:
- nums[i] *= -1
- k -= 1
-
- if k % 2 == 1:
- nums[0] *= -1
-
- return sum(nums)
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:
- class Solution:
- def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
- curSum = 0 # 当前累计的剩余油量
- totalSum = 0 # 总剩余油量
- start = 0 # 起始位置
-
- for i in range(len(gas)):
- curSum += gas[i] - cost[i]
- totalSum += gas[i] - cost[i]
-
- if curSum < 0: # 当前累计剩余油量curSum小于0
- start = i + 1 # 起始位置更新为i+1
- curSum = 0 # curSum重新从0开始累计
-
- if totalSum < 0:
- return -1 # 总剩余油量totalSum小于0,说明无法环绕一圈
- return start
violent solution:
index = (i + 1) % len( ) # make a list to be a head-tail connected list (circular route).
- class Solution:
- def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
- for i in range(len(cost)):
- rest = gas[i] - cost[i] # 记录剩余油量
- index = (i + 1) % len(cost) # 下一个加油站的索引
-
- while rest > 0 and index != i: # 模拟以i为起点行驶一圈(如果有rest==0,那么答案就不唯一了)
- rest += gas[index] - cost[index] # 更新剩余油量
- index = (index + 1) % len(cost) # 更新下一个加油站的索引
-
- if rest >= 0 and index == i: # 如果以i为起点跑一圈,剩余油量>=0,并且回到起始位置
- return i # 返回起始位置i
-
- return -1 # 所有起始位置都无法环绕一圈,返回-1
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:
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)
- class Solution:
- def candy(self, ratings: List[int]) -> int:
- candyVec = [1] * len(ratings)
-
- # 从前向后遍历,处理右侧比左侧评分高的情况
- for i in range(1, len(ratings)):
- if ratings[i] > ratings[i - 1]:
- candyVec[i] = candyVec[i - 1] + 1
-
- # 从后向前遍历,处理左侧比右侧评分高的情况
- for i in range(len(ratings) - 2, -1, -1):
- if ratings[i] > ratings[i + 1]:
- candyVec[i] = max(candyVec[i], candyVec[i + 1] + 1)
-
- # 统计结果
- result = sum(candyVec)
- return result
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。