728x90
[Javascript] (LeetCode) 122. Best Time to Buy and Sell Stock II (Medium)
💡 구글에 Javascript 풀이가 많이 없거나, 배운 점이 있으면 포스팅합니다.
내 풀이
/**
* @param {number[]} prices
* @return {number}
*/
var maxProfit = function(prices) {
// diff함수에 다 담아서
// +값 다 더하기
const diffs = [];
for (let i = 0; i < prices.length - 1; i++) {
let diff = prices[i + 1] - prices[i];
diff > 0 ? diffs.push(diff) : null;
}
return diffs.reduce((acc, cur) => acc += cur, 0);
};
- 출렁이더라도 매일 매일매일 값을 비교해서 ‘+’일 때 다 더해주면 답이 나오더라.
'Coding Test > LeetCode' 카테고리의 다른 글
[Javascript] (LeetCode) 387. First Unique Character in a String (Easy) (0) | 2022.07.26 |
---|---|
[Javascript] (LeetCode) 7. Reverse Integer (Medium) (0) | 2022.07.25 |
[Javascript] (LeetCode)136. Single Number (Easy) (0) | 2022.07.25 |
[Javascript] (LeetCode) 217. Contains Duplicate (Easy) (0) | 2022.07.24 |
[Javascript] (LeetCode)121. Best time to buy and sell stock (Easy) (0) | 2022.07.21 |
[Javascript] (LeetCode) 26. Remove Duplicates from Sorted Array (Easy) (0) | 2022.07.20 |
[Javascript] (LeetCode) 19. Remove Nth Node From End of List (Medium) (0) | 2022.07.14 |
[Javascript] LeetCode 876. Middle of the Linked List (0) | 2022.07.14 |
댓글