Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions best-time-to-buy-and-sell-stock/hozzijeong.js
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

현재 가격에서 앞에 있을 가격들 중 최고가를 정하여 답을 갱신하는군요!

지금은 시간복잡도가 O(n^2)이 나오지만, 여기서 현재 값이 어떻냐에 따라 분기를 나누어서 O(n)까지 줄일 수 있습니다

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @param {number[]} prices
* @return {number}
*/
var maxProfit = function(prices) {
let maxGap = 0;

for(let i =0; i < prices.length; i++){
const price = prices[i];
const restList = prices.slice(i);

const max = Math.max(...restList);

maxGap = Math.max(max-price, maxGap)
}

return maxGap
};