LeetCode刷题 Day34
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
iand replacenums[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.
Example 1:
Input: nums = [4,2,3], k = 1
Output: 5
Explanation: Choose index 1 and nums becomes [4,-2,3].
Example 2:
Input: nums = [3,-1,0,2], k = 3
Output: 6
Explanation: Choose indices (1, 2, 2) and nums becomes [3,1,0,2].
Example 3:
Input: nums = [2,-3,-1,5,-4], k = 2
Output: 13
Explanation: Choose indices (1, 4) and nums becomes [2,3,-1,5,4].
思路:
- 按照绝对值从大到小排序
- 将负值首先改为正值,k--
- 当所有负值都为正值之后,再将排序后数组的最末端的值反复进行正负值的修改,直到k = 0
代码:
var largestSumAfterKNegations = function(nums, k) {
nums.sort((a, b) => Math.abs(b) - Math.abs(a));
for (let i = 0; i < nums.length; i++) {
if (nums[i] < 0 && k > 0) {
nums[i] = -1*nums[i];
k--;
}
}
while (k > 0) {
nums[nums.length - 1] = -1 * nums[nums.length - 1];
k--;
}
return nums.reduce((a, b) => a + b);
};
时间复杂度: O(nlogn) 空间复杂度: O(n)
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
Example 1:
Input: gas = [1,2,3,4,5], cost = [3,4,5,1,2]
Output: 3
Explanation:
Start at station 3 (index 3) and fill up with 4 unit of gas. Your tank = 0 + 4 = 4
Travel to station 4. Your tank = 4 - 1 + 5 = 8
Travel to station 0. Your tank = 8 - 2 + 1 = 7
Travel to station 1. Your tank = 7 - 3 + 2 = 6
Travel to station 2. Your tank = 6 - 4 + 3 = 5
Travel to station 3. The cost is 5. Your gas is just enough to travel back to station 3.
Therefore, return 3 as the starting index.
Example 2:
Input: gas = [2,3,4], cost = [3,4,3]
Output: -1
Explanation:
You can't start at station 0 or 1, as there is not enough gas to travel to the next station.
Let's start at station 2 and fill up with 4 unit of gas. Your tank = 0 + 4 = 4
Travel to station 0. Your tank = 4 - 3 + 2 = 3
Travel to station 1. Your tank = 3 - 3 + 3 = 3
You cannot travel back to station 2, as it requires 4 unit of gas but you only have 3.
Therefore, you can't travel around the circuit once no matter where you start.
思路:
- 情况一:如果gas的总和小于cost总和,那么无论从哪里出发,一定是跑不了一圈的
- 情况二:rest[i] = gas[i]-cost[i]为一天剩下的油,i从0开始计算累加到最后一站,如果累加没有出现负数,说明从0出发,油就没有断过,那么0就是起点。
- 情况三:如果累加的最小值是负数,汽车就要从非0节点出发,从后向前,看哪个节点能这个负数填平,能把这个负数填平的节点就是出发节点。
代码:
var canCompleteCircuit = function(gas, cost) {
let diffSum = 0;
let index = 0;
let gasSum = 0;
let costSum = 0;
for (let i = 0; i < gas.length; i++) {
gasSum += gas[i];
costSum += cost[i];
diffSum += (gas[i] - cost[i]);
if (diffSum < 0) {
index = i + 1;
diffSum = 0;
}
}
if (gasSum < costSum) return -1;
return index;
};
时间复杂度: O(n) 空间复杂度: O(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.
Example 1:
Input: ratings = [1,0,2]
Output: 5
Explanation: You can allocate to the first, second and third child with 2, 1, 2 candies respectively.
Example 2:
Input: ratings = [1,2,2]
Output: 4
Explanation: You can allocate to the first, second and third child with 1, 2, 1 candies respectively.
The third child gets 1 candy because it satisfies the above two conditions.
思路:
- 两次遍历,从左向右,然后从右向左,这样可以获取一个局部的极值
- 从左向右过程中,当发现curr index的value比上一个index value高时,当前index value = candy[i - 1] + 1;
- 从右向左的时候,当发现i - 1的rate比i的rate高,且 candy[i - 1] <= candy[i],则 i - 1的value为 candy[i] + 1
代码:
var candy = function(ratings) {
let candys = Array(ratings.length).fill(1);
for (let i = 1; i < ratings.length; i++) {
if (ratings[i] > ratings[i - 1]) {
candys[i] = candys[i - 1] + 1;
}
}
for (let i = ratings.length - 1; i >= 1 ; i--) {
if (ratings[i - 1] > ratings[i] && candys[i - 1] <= candys[i]) candys[i - 1] = candys[i] + 1;
}
return candys.reduce((a, b) => a + b);
};
时间复杂度: O(n) 空间复杂度: O(n)