Week 1-1. [Array / String] 121. Best Time to Buy and Sell Stock
๋ฌธ์ ํด๊ฒฐ ๊ณผ์
โ๏ธ ๋๋ง์ ๋ฐฉ์์ผ๋ก ๋ฌธ์ ์ ๋ฆฌํ๊ธฐ (=์ฒด๊ณํ)
Best Time to Buy and Sell Stock - LeetCode
Can you solve this real interview question? Best Time to Buy and Sell Stock - You are given an array prices where prices[i] is the price of a given stock on the ith day. You want to maximize your profit by choosing a single day to buy one stock and choosin
leetcode.com
์ฃผ์ด์ง ์ฃผ์ ๊ฐ๊ฒฉ์ ๋ณด๊ณ ์ป์ ์ ์๋ ์ต๋ ์ด์ต(๋งค๋๊ฐ๊ฒฉ - ๋งค์๊ฐ๊ฒฉ)์ ๊ตฌํ๋ผ.
- ์
๋ ฅ
- prices | int arrray := ํด๋น ๋ ์ง์ ์ฃผ์ ๊ฐ๊ฒฉ์ ๋ํ๋ด๋ ๋ฐฐ์ด, | prices | ∈ [1, 10^5]
- ์ถ๋ ฅ
- k | int := ์ต๋ ์ด์ต
- ์กฐ๊ฑด
- | prices[i] | ∈ [0, 10^4]
๐ ์ด๋ค ์์ผ๋ก ์ ๊ทผํ๋๊ฐ?
๋งจ ์ฒ์์๋ 2์ค for๋ฌธ์ ์ด์ฉํด ํ๋์ฉ ๋งค์์ผ๋ก ์ก๊ณ , ๋งค์์ผ ์ดํ๋ฅผ ์ํํ๋ฉฐ ์ต๋ ์ด์ต์ ๊ตฌํ๋ค.
ํ์ง๋ง prices์ ์ต๋ ๊ธธ์ด๊ฐ 100,000์ผ๋ก ๋น์ฐํ๊ฒ๋ Time Limit Exceeded๊ฐ ๋ฐ์ํ๋ค.
class Solution {
public int maxProfit(int[] prices) {
int ans = 0;
for (int i=0; i<prices.length; i++) {
for (int j=i+1; j<prices.length; j++) {
if (prices[j] - prices[i] > ans) {
ans = prices[j] - prices[i];
}
}
}
return ans;
}
}
์ดํ Two Pointer ๋ฐฉ์์ ์๊ฐํ๋ค.
์ผ์ชฝ์์๋ถํฐ lp๋ฅผ ์ด์ฉํด ์ํํ๊ณ , ์ค๋ฅธ์ชฝ์์๋ถํฐ rp๋ฅผ ์ด์ฉํด ์ํํ๋ฉด์ max ๊ฐ๊ณผ min ๊ฐ์ ์ฐพ๋ ๊ฒ์ด๋ค.
์๋ ๊ตฌํ ์ฝ๋๊ฐ ๊ทธ๊ฒ์ธ๋ฐ, ๊ฒฐ๊ณผ์ ์ผ๋ก ํ ์คํธ ์ผ์ด์ค์๋ ์ฑ๊ณตํ์ง๋ง ํ๋ ์ผ์ด์ค์์ ์คํจํ๋ค.
๐ ์๊ณ ๋ฆฌ์ฆ ์ ํ: ์ ๋ ฅ์ ํฌ๊ธฐ๋ฅผ ๋ฐํ์ผ๋ก
prices์ ์ต๋ ๊ธธ์ด๊ฐ 100,000์ด๋ฏ๋ก ๋ฐ๋ณต๋ฌธ์ ํ๋๋ง ์ฌ์ฉํด์ผ ํ๋ค.
๐ ์์ฌ ์ฝ๋
lp = 0
rp = ๋งจ ๋ง์ง๋ง ์ธ๋ฑ์ค
mn = ์ผ์ชฝ๋ถํฐ ์ต์๊ฐ (๋งค์)
mx = ์ค๋ฅธ์ชฝ๋ถํฐ ์ต๋๊ฐ (๋งค๋)
while (lp <= rp) {
prices[lp]๊ฐ ์ต์๊ฐ์ด๋ผ๋ฉด ์ต์๊ฐ ๊ฐฑ์
prices[rp]๊ฐ ์ต๋๊ฐ์ด๋ผ๋ฉด ์ต๋๊ฐ ๊ฐฑ์
lp++
rp++
}
์ต์๊ฐ์ด ์ต๋๊ฐ๋ณด๋ค ํฌ๋ค๋ฉด 0 ๋ฐํ (์ด์ต์ ์ป์ ์ ์๋ ๊ฒ)
์๋๋ผ๋ฉด (rp-lp) ๋ฐํ
๐ฏ ๊ตฌํ ์ฝ๋
์๋ ์ฝ๋๋ ํจ์คํ์ง ๋ชปํ๋ ์ฝ๋์ด๋ค.
lp++ ๊ณผ rp-- ์ ํ๋ฉด์ ๊ฒฐ๊ตญ ์ค์์ผ๋ก ๋ชจ์ด๋๋ฐ, ๋งค์์ก๊ณผ ๋งค๋์ก์ด ์ข์ธก ํน์ ์ฐ์ธก์ ๋ชจ์ฌ์์ ๋ ํ ์คํธ ์ผ์ด์ค๋ฅผ ํต๊ณผํ์ง ๋ชปํ๋ค.
class Solution {
public int maxProfit(int[] prices) {
int lp = 0;
int rp = prices.length-1;
int mx = -1; // ๋งค๋์ก
int mn = 1000; // ๋งค์์ก
int ans = 0;
while (lp <= rp) {
if (prices[lp] < mn) {
mn = prices[lp];
}
if (prices[rp] > mx) {
mx = prices[rp];
}
lp++;
rp--;
}
ans = mx - mn;
if (ans <= 0) {
ans = 0;
}
return ans;
}
}
๋ด ์ฝ๋๋ฅผ ํ๊ตฌํด๋ณด๊ธฐ
โฑ ์ฑ๋ฅ ์ธก์ (์๊ฐ๋ณต์ก๋/๊ณต๊ฐ๋ณต์ก๋)
ํต๊ณผํ์ง ๋ชปํ ์ฝ๋๋ผ ์ฑ๋ฅ ์ธก์ ์ด ํฌ๊ฒ ์๋ฏธ๋ ์์ง๋ง, while๋ฌธ์ ์ฌ์ฉํ์์ผ๋ฏ๋ก ์๊ฐ ๋ณต์ก๋๋ O(N)์ด๋ค.
โ๏ธ ์ฑ๋ฅ ํ๊ณ
N์ ์ต๋ 100,000์ด๋ฏ๋ก ๊ฐ๋นํ ์ ์๋ ๋ฒ์์ด๋ค.
๋ณดํต 1์ด ์ ํ์ด๋ผ๊ณ ํ ๋, O(N)์ ์ฝ 1์ต๊น์ง O(N^2)์ ์ฝ 1๋ง๊น์ง ๊ฐ๋นํ ์ ์๋ค.
๋ฐ๋ผ์ ํด๋น ๋ฌธ์ ๋ฅผ 2์ค for๋ฌธ์ผ๋ก ๊ตฌํํ๋ค๋ฉด ํฐ์ง๊ฒ ๋ ๊ฒ์ด๋ค.
๐ก ๋ฌด์์/์ด๋ป๊ฒ ๊ฐ์ ํ ์ ์์๊น?
๋ฐ๋ณต๋ฌธ์ ํ ๋ฒ๋ง ์ฌ์ฉํ๋ฉด์ ์ต๋ ์ด์ต์ ๊ณ์ฐํ ์ ์๋๋ก ๊ฐ์ ํด์ผ ํ๋ค.
๋ค๋ฅธ ์ฌ๋์ ์ฝ๋๋ฅผ ํ๊ตฌํด๋ณด๊ธฐ
๐ฏ ๊ตฌํ ์ฝ๋
prices๋ฅผ ์ํํ๋ฉด์ ๋งค์์ก(mn, ์ต์ ๊ธ์ก) ๊ฐฑ์ & ์ด์ต(pist, ๋งค๋์ก - ๋งค์์ก)์ ๊ณ์ฐํ์ฌ ์ต๋ ์ด์ต์ ๊ตฌํ๋ค.
class Solution {
public int maxProfit(int[] prices) {
int mn = Integer.MAX_VALUE; // ๋งค์์ก (์ต์ ๊ธ์ก)
int pist = 0; // profit if sold today: ์ค๋ ํ์์ ๋ ์ด์ต
int ans = 0; // ์ต์ข
์ด์ต
for (int i=0; i<prices.length; i++) {
if (prices[i] < mn) {
mn = prices[i]; // ๋งค์์ก ๊ฐฑ์
}
// prices[i]๋ฅผ ๋งค๋์ก์ด๋ผ๊ณ ๊ฐ์ ํ๊ณ ์ด์ต ๊ณ์ฐ
pist = prices[i] - mn;
if (ans < pist) { // ์ค๋ ํ์์ ๋ ์ด์ต์ด ๋ ํฌ๋ค๋ฉด ์ต์ข
์ด์ต ๊ฐฑ์
ans = pist;
}
}
return ans;
}
}
โฑ ์ฑ๋ฅ ์ธก์ (์๊ฐ๋ณต์ก๋/๊ณต๊ฐ๋ณต์ก๋)
๋จ์ผ for๋ฌธ์ ์ฌ์ฉํ์ฌ ์๊ฐ ๋ณต์ก๋๋ O(N)์ด๋ค.
๋๋ ์
๋จ์ผ for๋ฌธ์ ์ฌ์ฉํ๋๋ฐ ์ด์ ์ ๋ง์ถ์ด ๊ณ ๋ฏผํ๋ค๋ณด๋ ๊ณ์ํด์ Two Pointer๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ์๋ฐ์ ๋ ์ค๋ฅด์ง ์์๋ค.
์ด๋์ ๊ฐ ํ์ด๋ณธ ๋ฌธ์ ์ ํ์ด์๋๋ฐ ๋๊น์ง ํ์ง ๋ชปํด ์์ฌ์ ๋ค. ๊ธฐ์ต์ด ํ๋ ค์ง ๋ ์ฏค ๊ผญ ๋ค์ ํ๋ฒ ํ์ด๋ณผ ๋ฌธ์ ์ด๋ค.