๋ฌธ์ ํด๊ฒฐ ๊ณผ์
โ๏ธ ๋๋ง์ ๋ฐฉ์์ผ๋ก ๋ฌธ์ ์ ๋ฆฌํ๊ธฐ (=์ฒด๊ณํ)
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/
Best Time to Buy and Sell Stock II - LeetCode
Can you solve this real interview question? Best Time to Buy and Sell Stock II - You are given an integer array prices where prices[i] is the price of a given stock on the ith day. On each day, you may decide to buy and/or sell the stock. You can only hold
leetcode.com
์ฃผ์ด์ง ์ฃผ์ ๊ฐ๊ฒฉ์ ๋ณด๊ณ ์ป์ ์ ์๋ ์ด์ต์ ์ดํฉ์ ๊ตฌํ๋ผ. (๊ธฐ๊ฐ ๋ด์ ์ฌ๋ฌ๋ฒ ์ฌ๊ณ ํ ์ ์๋ค.)
- ์
๋ ฅ
- nums | int arrray := ์ ์ ๋ฐฐ์ด, | prices | ∈ [1, 3*10^4]
- ์ถ๋ ฅ
- k | int := ์ด์ต์ ์ดํฉ
- ์กฐ๊ฑด
- | prices[i] | ∈ [0, 10^4]
๐ ์ด๋ค ์์ผ๋ก ์ ๊ทผํ๋๊ฐ?
์ด์ Best Time to Buy and Sell Stock ๋ฌธ์ ์ ์ ์ฌํ์ฌ ํด๋น ํ์ด๋ฅผ ๋ ์ฌ๋ ธ๋ค.
๋ค๋ฅธ ์ ์ ์ด์ ๋ฌธ์ ๋ ํ ๋ฒ๋ง ์ฌ๊ณ ํ ์ ์์๊ณ , ์ง๊ธ์ ์ฌ๋ฌ๋ฒ ์ฌ๊ณ ํ ์ ์์ด ์ด์ต์ ์ด ํฉ์ ๊ตฌํ๋ ๊ฒ์ด๋ค.
๋ฐ๋ผ์ 0๋ณด๋ค ํฐ ์ด์ต์ด ๋ฐ์ํ๋ค๋ฉด ์ด์ต์ ๋์ ํด์ผ ํ๊ณ , ์ฌ๋ฌ๋ฒ ๋งค์ํ ์ ์๋๋ก ๋งค์์ก์ ๊ฐฑ์ ํด์ผ ํ๋ค.
๐ ์๊ณ ๋ฆฌ์ฆ ์ ํ: ์ ๋ ฅ์ ํฌ๊ธฐ๋ฅผ ๋ฐํ์ผ๋ก
prices์ ์ต๋ ๊ธธ์ด๊ฐ 30,000์ด๋ฏ๋ก ๋ฐ๋ณต๋ฌธ์ ํ๋๋ง ์ฌ์ฉํด์ผ ํ๋ค.
๐ ์์ฌ ์ฝ๋
๋งค์์ก = 10000 (ํฐ ๊ฐ)
์ต์ข
์ด์ต = 0
์ค๋ ํ์์ ๋ ์ด์ต = 0
for (0๋ถํฐ, ๋ง์ง๋ง๊น์ง, 1์ฉ ์ฆ๊ฐ) {
if (prices[i]๊ฐ ๋งค์์ก๋ณด๋ค ์๋ค๋ฉด) {
๋งค์์ก์ prices[i]๋ก ๊ฐฑ์
}
์ค๋ ํ์์ ๋ ์ด์ต ๊ณ์ฐ (prices[i] - ๋งค์์ก)
if (์ด์ต ๋ฐ์ํ๋ค๋ฉด) {
์ต์ข
์ด์ต += ์ค๋ ํ์์ ๋ ์ด์ต
๋งค์์ก์ ๋งค๋์ก์ผ๋ก ๊ฐฑ์
}
}
๐ฏ ๊ตฌํ ์ฝ๋
class Solution {
public int maxProfit(int[] prices) {
int mn = Integer.MAX_VALUE; // ๋งค์์ก
int ans = 0; // ์ต์ข
์ด์ต
int pist = 0; // profit if sold today: ์ค๋ ํ์์ ๋ ์ด์ต
for (int i=0; i<prices.length; i++) {
if (mn > prices[i]) {
mn = prices[i];
}
pist = prices[i] - mn;
if (pist > 0) { // ์ค๋ ํ์์ ๋ ์ด์ต์ด ๋ฐ์ํ๋ค๋ฉด
ans += pist; // ์ด์ต ๋์
mn = prices[i]; // โ
๋งค์์ก์ ๋งค๋์ก์ผ๋ก ์ด๊ธฐํ
}
}
return ans;
}
}
๋ด ์ฝ๋๋ฅผ ํ๊ตฌํด๋ณด๊ธฐ
โฑ ์ฑ๋ฅ ์ธก์ (์๊ฐ๋ณต์ก๋/๊ณต๊ฐ๋ณต์ก๋)
๋จ์ผ for๋ฌธ์ ์ฌ์ฉํ์ฌ ์๊ฐ ๋ณต์ก๋๋ O(N)์ด๋ค.
โ๏ธ ์ฑ๋ฅ ํ๊ณ
N์ ์ต๋ 30,000์ด๋ฏ๋ก ๊ฐ๋นํ ์ ์๋ ๋ฒ์์ด๋ค.
๋ณดํต 1์ด ์ ํ์ด๋ผ๊ณ ํ ๋, O(N)์ ์ฝ 1์ต๊น์ง O(N^2)์ ์ฝ 1๋ง๊น์ง ๊ฐ๋นํ ์ ์๋ค.
๋ฐ๋ผ์ ํด๋น ๋ฌธ์ ๋ฅผ 2์ค for๋ฌธ์ผ๋ก ๊ตฌํํ๋ค๋ฉด ํฐ์ง๊ฒ ๋ ๊ฒ์ด๋ค.
๐ก ๋ฌด์์/์ด๋ป๊ฒ ๊ฐ์ ํ ์ ์์๊น?
ํ๋ ์ผ์ด์ค๊น์ง ํต๊ณผํ ์๋ ์์์ง๋ง ๋ง์กฑ์ค๋ฝ์ง๋ ์์ ์ฝ๋์ด๋ค.
๋์ค์ ๋ค์ ๋ณด์์ ๋ ์ด๋ป๊ฒ ์ด๋ฐ ์ฝ๋๊ฐ ๋์๋์ง ๊ณฐ๊ณฐํ ์๊ฐํด ๋ณด์์ผ ํ ๊ฒ ๊ฐ๊ธฐ ๋๋ฌธ์ด๋ค.
์ฑ๋ฅ์ ๋์์ง ์์ง๋ง ๋ค๋ฅธ ์ฌ๋์ด ๋ณด์์ ๋์๋ ์ดํดํ๊ธฐ ์ฝ๋๋ก ๋ ์ง๊ด์ ์ผ๋ก ๋ณ๊ฒฝํ๋ฉด ์ข์ ๋ฏ ํ๋ค.
๋ค๋ฅธ ์ฌ๋์ ์ฝ๋๋ฅผ ํ๊ตฌํด๋ณด๊ธฐ
๐ฏ ๊ตฌํ ์ฝ๋
๋ด ์ฝ๋๋ณด๋ค ์ง๊ด์ ์ธ ์ฝ๋๋ผ๊ณ ์๊ฐํ๋ค.
class Solution {
public int maxProfit(int[] prices) {
ArrayList<Integer> priceGain = new ArrayList<Integer>();
for(int i=0 ; i<prices.length-1; i++){
if(prices[i] < prices[i+1]){ // ์ฃผ์ ๊ฐ๊ฒฉ์ด ์ฆ๊ฐํ๋ค๋ฉด
priceGain.add(prices[i+1] - prices[i]); // ์ด์ต ์ถ๊ฐ
}
}
return priceGain.stream().mapToInt(n->n).sum();
}
}
โฑ ์ฑ๋ฅ ์ธก์ (์๊ฐ๋ณต์ก๋/๊ณต๊ฐ๋ณต์ก๋)
๋ง์ฐฌ๊ฐ์ง๋ก ๋จ์ผ for๋ฌธ์ ์ฌ์ฉํ์ฌ ์๊ฐ ๋ณต์ก๋๋ O(N)์ด๋ค.
๋๋ ์
๊ฐ๋ฅํ๋ค๋ฉด, ์์ ๋ง์ ๋
ผ๋ฆฌ๋ณด๋ค๋ ๋ชจ๋๊ฐ ์ง๊ด์ ์ผ๋ก ์ดํดํ ์ ์๋ ์ฝ๋๋ฅผ ์ง๊ณ ์ถ๋ค. ๋ฌผ๋ก ์คํ๋๋๊ฒ ์ฐ์ ์ด๊ธด ํ๊ฒ ์ง๋ง!
'๐ซ ETC > Leetcode' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Week 1-2. [Two Pointers] 125. Valid Palindrome (Java) (0) | 2023.08.26 |
---|---|
Week 1-1. [Array / String] 55. Jump Game (0) | 2023.08.24 |
Week 1-1. [Array / String] 121. Best Time to Buy and Sell Stock (0) | 2023.08.24 |
Week 1-1. [Array / String] 189. Rotate Array (0) | 2023.08.24 |
Week 1-1. [Array / String] 169. Majority Element (0) | 2023.08.23 |