var findContentChildren = function(g, s) {
g.sort((a, b) => a - b);
s.sort((a, b) => a - b);
let sum = 0, i = 0, j = 0;
while (i < g.length && j < s.length) {
if (s[j] >= g[i]) {
sum++;
i++;
j++;
}
else j++;
}
return sum;
};
var findContentChildren = function(g, s) {
g.sort((a, b) => a - b);
s.sort((a, b) => a - b);
let res = 0, index = s.length - 1;
for (let i = g.length - 1; i >= 0; i--) {
if (index >= 0 && s[index] >= g[i]) {
res++;
index--;
}
}
return res;
};
var wiggleMaxLength = function(nums) {
if (nums.length <= 1) return nums.length;
let curDiff = 0, preDiff = 0;
let res = 1;
for (let i = 0; i < nums.length - 1; i++) {
curDiff = nums[i + 1] - nums[i];
if ((curDiff > 0 && preDiff <= 0) || (curDiff < 0 && preDiff >= 0)) {
res++;
preDiff = curDiff;
}
}
return res;
};
var maxSubArray = function(nums) {
let result = -Infinity
let count = 0
for(let i = 0; i < nums.length; i++) {
count += nums[i]
if(count > result) {
result = count
}
if(count < 0) {
count = 0
}
}
return result
};
假如第0天买入,第3天卖出,那么利润为:prices[3] - prices[0]。
相当于(prices[3] - prices[2]) + (prices[2] - prices[1]) + (prices[1] - prices[0])。
此时就是把利润分解为每天为单位的维度,而不是从0天到第3天整体去考虑!
我们就只要收集每天的正收入,累加即可得最大收益
var maxProfit = function(prices) {
let res = 0;
for (let i = 1; i < prices.length; i++) {
res += Math.max(0, prices[i] - prices[i - 1]);
}
return res;
};
var canJump = function(nums) {
let cover = 0;
for (let i = 0; i <= cover; i++) {
cover = Math.max(cover, nums[i] + i);
if (cover >= nums.length - 1) return true;
}
return false;
};
var jump = function(nums) {
let res = 0, curDistance = 0, nextDistance = 0;
if (nums.length === 1) return 0;
for (let i = 0; i < nums.length; i++) {
nextDistance = Math.max(nextDistance, nums[i] + i);
if (i === curDistance) {
if (curDistance !== nums.length - 1) {
res++;
curDistance = nextDistance;
if (curDistance >= nums.length - 1) break;
} else break;
}
}
return res;
};
var jump = function(nums) {
let curIndex = 0
let nextIndex = 0
let steps = 0
for(let i = 0; i < nums.length - 1; i++) {
nextIndex = Math.max(nums[i] + i, nextIndex)
if(i === curIndex) {
curIndex = nextIndex
steps++
}
}
return steps
};
var largestSumAfterKNegations = function (nums, k) {
let res = 0;
nums.sort((a, b) => a - b);
for (let i = 0; i < nums.length; i++) {
if (nums[i] < 0 && k > 0) {
nums[i] *= -1;
k--;
}
res += nums[i];
}
if (k === 0) {
return res;
} else {
if (k % 2 === 0) {
return res;
} else {
return res - 2 * Math.min(...nums);
}
}
};
// 版本二 (优化: 一次遍历)
var largestSumAfterKNegations = function(nums, k) {
nums.sort((a, b) => Math.abs(b) - Math.abs(a)); // 排序
let sum = 0;
for(let i = 0; i < nums.length; i++) {
if(nums[i] < 0 && k-- > 0) { // 负数取反(k 数量足够时)
nums[i] = -nums[i];
}
sum += nums[i]; // 求和
}
if(k % 2 > 0) { // k 有多余的(k若消耗完则应为 -1)
sum -= 2 * nums[nums.length - 1]; // 减去两倍的最小值(因为之前加过一次)
}
return sum;
};
var canCompleteCircuit = function(gas, cost) {
for(let i = 0; i < cost.length; i++) {
let rest = gas[i] - cost[i] //记录剩余油量
// 以i为起点行驶一圈,index为下一个目的地
let index = (i + 1) % cost.length
while(rest > 0 && index !== i) {
rest += gas[index] - cost[index]
index = (index + 1) % cost.length
}
if(rest >= 0 && index === i) return i
}
return -1
};
情况一:如果gas的总和小于cost总和,那么无论从哪里出发,一定是跑不了一圈的
情况二:rest[i] = gas[i]-cost[i]为一天剩下的油,i从0开始计算累加到最后一站,如果累加没有出现负数,说明从0出发,油就没有断过,那么0就是起点。
情况三:如果累加的最小值是负数,汽车就要从非0节点出发,从后向前,看哪个节点能这个负数填平,能把这个负数填平的节点就是出发节点。
例如:例1的rest(gas - cost)数组为[-2, -2, -2, 3, 3],min = -6,我们需要从后往前找出一段能够填补min的一段路,即一段路亏油,我们要找一段路存储到足够他亏的油
var canCompleteCircuit = function(gas, cost) {
let curSum = 0, min = Infinity;
for (let i = 0; i < cost.length; i++) {
curSum += gas[i] - cost[i];
if (curSum < min) min = curSum;
}
if (curSum < 0) return -1;
if (min > 0) return 0;
for (let i = cost.length - 1; i >= 0; i--) {
min += gas[i] - cost[i];
if (min >= 0) {
return i;
}
}
return -1;
};
一次是从左到右遍历,只比较右边孩子评分比左边大的情况。
一次是从右到左遍历,只比较左边孩子评分比右边大的情况。
var candy = function(ratings) {
let candys = new Array(ratings.length).fill(1);
for (let i = 1; i < candys.length; i++) {
if (ratings[i] > ratings[i - 1]) {
candys[i] = candys[i - 1] + 1;
}
}
for (let i = candys.length - 2; i >= 0; i--) {
if (ratings[i] > ratings[i + 1]) {
candys[i] = Math.max(candys[i], candys[i + 1] + 1);
}
}
return candys.reduce((a, b) => {
return a + b;
})
};
var lemonadeChange = function(bills) {
let five = 0, ten = 0;
for (let i = 0; i < bills.length; i++) {
if (bills[i] === 5) five++;
if (bills[i] === 10) {
if (five) {
five--;
ten++;
} else {
return false;
}
}
if (bills[i] === 20) {
if (ten && five) {
ten--;
five--;
} else if (five >= 3) {
five -= 3;
} else {
return false;
}
}
}
return true;
};
var reconstructQueue = function(people) {
let que = [];
people.sort((a, b) => {
if (a[0] !== b[0]) {
return b[0] - a[0];
} else {
return a[1] - b[1];
}
})
for (let i = 0; i < people.length; i++) {
que.splice(people[i][1], 0, people[i]);
}
return que;
};
var findMinArrowShots = function(points) {
let res = 1;
points.sort((a, b) => {
return a[0] - b[0];
})
for (let i = 1; i < points.length; i++) {
if (points[i][0] > points[i - 1][1]) {
res++;
} else {
points[i][1] = Math.min(points[i - 1][1], points[i][1]);
}
}
return res;
};
var eraseOverlapIntervals = function(intervals) {
intervals.sort((a, b) => a[1] - b[1]);
let end = intervals[0][1], count = 1;
for (let i = 1; i < intervals.length; i++) {
if (end <= intervals[i][0]) {
end = intervals[i][1];
count++;
}
}
return intervals.length - count;
};
var partitionLabels = function(s) {
let hash = {};
let res = [];
for (let i = 0; i < s.length; i++) {
hash[s[i]] = i; // // 统计每一个字符最后出现的位置
}
let right = 0, left = 0;
for (let i = 0; i < s.length; i++) {
right = Math.max(right, hash[s[i]]); // 找到字符出现的最远边界
if (i === right) {
res.push(right - left + 1);
left = i + 1;
}
}
return res;
};
var merge = function(intervals) {
let res = [];
intervals.sort((a, b) => a[0] - b[0]);
let pre = intervals[0];
for (let i = 1; i < intervals.length; i++) {
let cur = intervals[i];
if (cur[0] > pre[1]) {
res.push(pre);
pre = cur;
} else {
pre[1] = Math.max(cur[1], pre[1]);
}
}
res.push(pre);
return res;
};
var monotoneIncreasingDigits = function(n) {
let flag;
n = n.toString();
n = n.split('').map((item) => +item);
for (let i = n.length - 1; i > 0; i--) {
if (n[i] < n[i - 1]) {
flag = i;
n[i - 1]--;
n[i] = 9;
}
}
for (let i = flag; i < n.length; i++) {
n[i] = 9;
}
return +n.join('');
};