var maxProfit = function(prices) { //計算每天賣出的價格 let nums = [] for(let i = 0, j = 1; i < prices.length - 1;i++,j++) nums.push(prices[j] - prices[i])
console.log(nums)
//只取正數 let sum = 0; for(let i = 0; i < nums.length; i++) if(nums[i] > 0) sum += nums[i]
return sum; }
精簡化, 這裡用 Math.max 塞零進去,正數直接往上加, 負數就直接給零
1 2 3 4 5 6 7 8 9
var maxProfit = function(prices) { //計算每天賣出的價格 let nums = [] let result = 0 for(let i = 0, j = 1; i < prices.length - 1;i++,j++) result += Math.max(prices[j] - prices[i], 0)
return result; }
LeetCode 55 CanJump
1 2 3 4 5 6 7 8 9
var canJump = function(nums) { if (nums.length == 1) return true; let cover = 0; for(let i = 0; i <= cover; i++){ cover = Math.max(i + nums[i],cover); if (cover >= nums.length - 1) return true } return false };
LeetCode 1005. Maximize Sum Of Array After K Negations
這裡雷點就是 js 的 sort預設的排序順序是根據字串的 Unicode 編碼位置(code points)而定 會搞得你不要不要的, 所以先執行 nums.sort((a, b) => a - b) 才能正常數字排序 排序完成後對 負數 的取反, 接著找出變化後 array 最小值, 然後用 k % 2 計算餘數 (這樣可以不用 while) 來取反, 最後加總即可
//取最小值取反 let minValue = Math.min(...nums); let minIndex = nums.indexOf(minValue); if (k % 2 == 1) nums[minIndex] = -nums[minIndex];
//加總 let sum = 0; for(let i = 0; i < nums.length; i++) sum += nums[i]; return sum; };
LeetCode 134 加油站
這題還滿難想的, 當 sum 小於 0 時, 把 start 位置設定到下個站點, 然後 sum 歸零從新計算
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
var canCompleteCircuit = function(gas, cost) { let total = 0 for(let i = 0; i < gas.length; i++) total += gas[i] - cost[i] let sum = 0; let start = 0; for(let i = 0; i < gas.length; i++) { sum += gas[i] - cost[i] //貪心精華所在 if(sum < 0) { start = i + 1 sum = 0 } } if (total < 0) return -1 return start }
var lemonadeChange = function(bills) { let five = 0; let ten = 0; for (let i = 0; i < bills.length; i++) { if (bills[i] == 5) { five++ } if (bills[i] == 10) { if (five == 0) return false else { five-- ten++ } } if (bills[i] == 20) { if (ten > 0 && five > 0) { five-- ten-- } else if (ten == 0 && five >= 3) { five -=3 } else { return false }