Skip to content

Commit 7b12b36

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents 94d448b + de1491f commit 7b12b36

File tree

123 files changed

+4296
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

123 files changed

+4296
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* [Problem]: [121] Best Time to Buy and Sell Stock
3+
*
4+
* (https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/)
5+
*/
6+
function maxProfit(prices: number[]): number {
7+
//์‹œ๊ฐ„๋ณต์žก๋„: O(n^2);
8+
//๊ณต๊ฐ„๋ณต์žก๋„: O(1);
9+
// Time Limit Exceeded
10+
function doublyLoopFunc(prices: number[]): number {
11+
let result = 0;
12+
for (let i = 0; i < prices.length; i++) {
13+
for (let j = i + 1; j < prices.length; j++) {
14+
let profit = prices[j] - prices[i];
15+
result = Math.max(profit, result);
16+
}
17+
}
18+
19+
return result;
20+
}
21+
22+
// ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n)
23+
// ๊ณต๊ฐ„ ๋ณต์žก๋„: O(1)
24+
function twoPointerFunc(prices: number[]): number {
25+
let minPrice = prices[0];
26+
let maxProfit = 0;
27+
28+
for (let i = 1; i < prices.length; i++) {
29+
if (prices[i] < minPrice) {
30+
minPrice = prices[i];
31+
} else {
32+
maxProfit = Math.max(maxProfit, prices[i] - minPrice);
33+
}
34+
}
35+
36+
return maxProfit;
37+
}
38+
39+
return twoPointerFunc(prices);
40+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n) - ๋ฐฐ์—ด์„ ํ•œ ๋ฒˆ๋งŒ ์ˆœํšŒํ•จ
3+
* ๊ณต๊ฐ„ ๋ณต์žก๋„: O(1) - ์ถ”๊ฐ€ ๋ฐฐ์—ด ์—†์ด ๋ณ€์ˆ˜๋งŒ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ ์ž…๋ ฅ ํฌ๊ธฐ์™€ ๋ฌด๊ด€ํ•œ ์ƒ์ˆ˜ ๊ณต๊ฐ„
4+
*/
5+
/**
6+
* @param {number[]} prices
7+
* @return {number}
8+
*/
9+
var maxProfit = function (prices) {
10+
let minPrice = prices[0]; // ์ง€๊ธˆ๊นŒ์ง€ ๋ณธ ๊ฐ€์žฅ ๋‚ฎ์€ ๊ฐ€๊ฒฉ
11+
let maxProfit = 0; // ์ตœ๋Œ€ ์ด์ต
12+
for (let i = 1; i < prices.length; i++) {
13+
if (prices[i] > minPrice) {
14+
// ํ˜„์žฌ ๊ฐ€๊ฒฉ์ด minPrice๋ณด๋‹ค ํฐ ๊ฒฝ์šฐ์— ์ด์ต ๊ฐฑ์‹ 
15+
maxProfit = Math.max(maxProfit, prices[i] - minPrice);
16+
} else {
17+
// ๊ทธ๋ ‡์ง€ ์•Š๋‹ค๋ฉด ์ตœ์†Œ ๊ฐ€๊ฒฉ ๊ฐฑ์‹ 
18+
minPrice = prices[i];
19+
}
20+
}
21+
return maxProfit;
22+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Time Complexity: O(n), where n is the length of the prices array
2+
// Space Complexity: O(1)
3+
4+
function maxProfit(prices: number[]): number {
5+
// input: an array prices, prices[i] = the stock price of ith day
6+
// output: the maximum profit || 0
7+
8+
// prices = [7, 1, 5, 3, 6, 4]
9+
// buy = 1, sell = 6, profit = 6 -1 = 5
10+
11+
let minBuy = prices[0];
12+
let maxProfit = 0;
13+
14+
for (let i = 1; i < prices.length; i++) {
15+
minBuy = Math.min(prices[i], minBuy);
16+
maxProfit = Math.max(prices[i] - minBuy, maxProfit);
17+
}
18+
19+
return maxProfit;
20+
}
21+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* ์ฃผ์‹ ๊ฐ€๊ฒฉ์ด ์ฃผ์–ด์ง€๋Š” prices ๋ฐฐ์—ด์ด ์žˆ์„ ๋•Œ ์ตœ๋Œ€ ์ฃผ์‹ ์ด์ต์„ ๊ตฌํ•˜์‹œ์˜ค
3+
* ์ฃผ์‹์„ ์‚ฐ ๋‚ ์งœ์—๋Š” ํŒ” ์ˆ˜ ์—†์œผ๋ฉฐ ๋ฐ˜๋“œ์‹œ ์‚ฐ ๋‚ ์งœ์˜ ์ดํ›„ ๋‚ ์งœ๋ถ€ํ„ฐ(๋ฏธ๋ž˜๋ถ€ํ„ฐ) ํŒ” ์ˆ˜ ์žˆ๋‹ค.
4+
*/
5+
class Solution {
6+
public int maxProfit(int[] prices) {
7+
int maxProfit = 0;
8+
int min = prices[0];
9+
// ๊ตณ์ด DP ๋ฐฐ์—ด ์“ฐ์ง€ ์•Š๊ณ  ๊ณ„์‚ฐ, ๊ณต๊ฐ„ ๋ณต์žก๋„ ๋‚ฎ์ถ”๊ธฐ
10+
for (int i = 0; i < prices.length; i++) {
11+
int profit = prices[i] - min;
12+
maxProfit = Math.max(profit, maxProfit);
13+
min = Math.min(prices[i], min);
14+
}
15+
return maxProfit;
16+
}
17+
18+
// public int maxProfit(int[] prices) {
19+
// // ์ตœ์ € ๊ตฌ๋งค
20+
// int[] dp = new int[prices.length];
21+
// dp[0] = prices[0];
22+
// for (int i = 1; i < prices.length; i++) {
23+
// dp[i] = Math.min(prices[i], dp[i - 1]);
24+
// }
25+
// // ์ตœ์ € ๊ตฌ๋งค ๋ฐฐ์—ด ๊ธฐ์ค€์œผ๋กœ ๋‹น์ผ ์ตœ๋Œ€ ์ด์ต ๊ณ„์‚ฐ
26+
// int profit = 0;
27+
// for (int i = 1; i < prices.length; i++) {
28+
// profit = Math.max(prices[i] - dp[i - 1], profit);
29+
// }
30+
// return profit;
31+
// }
32+
}
33+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
[๋ฌธ์ œํ’€์ด]
3+
- ์ž‘์€์ˆ˜๋ฅผ ๋‘๊ณ , ํฐ์ˆ˜์— ๋บ€ ๊ฐ’์„ ๊ตฌํ•˜์ž.
4+
time: O(N), space: O(1)
5+
6+
[ํšŒ๊ณ ]
7+
์ด๋ฒˆ ๋ฌธ์ œ๋Š” ๋‚œ์ด๋„๊ฐ€ easy์ธ ๋•๋ถ„์— ๋ฌด๋ฆฌ์—†์ด ํ’€์—ˆ๋˜ ๊ฒƒ ๊ฐ™๋‹ค.
8+
*/
9+
class Solution {
10+
public int maxProfit(int[] prices) {
11+
int min = prices[0];
12+
int max = 0;
13+
for (int i = 1; i < prices.length; i++) {
14+
min = Math.min(min, prices[i]);
15+
max = Math.max(max, prices[i] - min);
16+
}
17+
return max;
18+
}
19+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from typing import List
2+
class Solution:
3+
"""
4+
- Time Complexity: O(n), n = len(prices)
5+
- Space Complexity: O(1)
6+
"""
7+
def maxProfit(self, prices: List[int]) -> int:
8+
min_price = float("inf")
9+
max_profit = 0
10+
11+
for price in prices:
12+
min_price = min(min_price, price)
13+
max_profit = max(max_profit, price - min_price)
14+
15+
return max_profit
16+
17+
tc = [
18+
([7,1,5,3,6,4], 5),
19+
([7,6,4,3,1], 0)
20+
]
21+
22+
for i, (prices, e) in enumerate(tc, 1):
23+
sol = Solution()
24+
r = sol.maxProfit(prices)
25+
print(f"TC {i} is Passed!" if r == e else f"TC {i} is Failed! - Expected: {e}, Result: {r}")
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {number[]} prices
3+
* @return {number}
4+
*/
5+
var maxProfit = function (prices) {
6+
let minPrice = Infinity;
7+
let maxProfit = 0;
8+
9+
for (let i = 0; i < prices.length; i++) {
10+
if (prices[i] < minPrice) {
11+
minPrice = prices[i]; // ์ง€๊ธˆ๊นŒ์ง€ ๊ฐ€์žฅ ์‹ผ ๋‚ 
12+
} else if (prices[i] - minPrice > maxProfit) {
13+
maxProfit = prices[i] - minPrice; // ํ˜„์žฌ ์ด์ต์ด ์ตœ๋Œ€ ์ด์ต๋ณด๋‹ค ํด ๋•Œ maxProfit ๊ฐฑ์‹ 
14+
}
15+
}
16+
17+
return maxProfit;
18+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* ์‹œ๊ฐ„ ๋ณต์žก๋„ O(n)
3+
* ๊ณต๊ฐ„ ๋ณต์žก๋„ O(1)
4+
*
5+
* ๊ทธ๋ฆฌ๋”” ์•Œ๊ณ ๋ฆฌ์ฆ˜
6+
* ํ˜„์žฌ๊นŒ์ง€์˜ ์ตœ์ € ๊ฐ€๊ฒฉ์„ ๊ธฐ์–ตํ•˜๊ณ , ๊ทธ ๊ฐ€๊ฒฉ์— ์ƒ€์„ ๋•Œ์˜ ์ด์ต์„ ๊ณ„์† ๊ณ„์‚ฐํ•˜์—ฌ ์ตœ๋Œ€ ์ด์ต์„ ๊ตฌํ•จ
7+
*/
8+
9+
/**
10+
* @param {number[]} prices
11+
* @return {number}
12+
*/
13+
var maxProfit = function (prices) {
14+
let minPrice = prices[0]; // ์ตœ์ € ๊ฐ€๊ฒฉ ์ดˆ๊ธฐํ™” (์ฒซ ๋‚  ๊ฐ€๊ฒฉ)
15+
let maxProfit = 0; // ์ตœ๋Œ€ ์ด์ต ์ดˆ๊ธฐํ™” (์•„์ง ์ด์ต ์—†์Œ)
16+
17+
// ๋‘ ๋ฒˆ์งธ ๋‚ ๋ถ€ํ„ฐ
18+
for (let i = 1; i < prices.length; i++) {
19+
minPrice = Math.min(minPrice, prices[i]); // ์ตœ์ € ๊ฐ€๊ฒฉ ๊ฐฑ์‹ 
20+
maxProfit = Math.max(maxProfit, prices[i] - minPrice); // ์ตœ๋Œ€ ์ด์ต ๊ฐฑ์‹ 
21+
}
22+
23+
return maxProfit;
24+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
func maxProfit(_ prices: [Int]) -> Int {
3+
guard var anchor = prices.first, prices.count > 1 else {
4+
return 0
5+
}
6+
7+
var result = 0
8+
for price in prices {
9+
if price < anchor {
10+
anchor = price
11+
} else if price - anchor > result {
12+
result = price - anchor
13+
}
14+
}
15+
16+
return result
17+
}
18+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# TC: O(N), SC: O(1)
2+
3+
class Solution:
4+
def maxProfit(self, prices: List[int]) -> int:
5+
max_profit = 0
6+
min_price = prices[0]
7+
8+
for price in prices:
9+
max_profit = max(price - min_price, max_profit)
10+
min_price = min(price, min_price)
11+
return max_profit
12+
13+
# TS ํ’€์ด
14+
# ๋ฐฐ์—ด ์š”์†Œ(์ˆซ์ž๊ฐ’)์„ ์ง์ ‘ ์ˆœํšŒํ•˜๋ ค๋ฉด for ... of ์‚ฌ์šฉ ํ˜น์€ forEach
15+
# for ... in -> ์ธ๋ฑ์Šค๋ฅผ ๊ฐ€์ ธ์˜ด
16+
17+
# function maxProfit(prices: number[]): number {
18+
# let max_profit: number = 0;
19+
# let min_price: number = prices[0];
20+
21+
# for (let price of prices) {
22+
# max_profit = Math.max(max_profit, price - min_price);
23+
# min_price = Math.min(min_price, price);
24+
# }
25+
# return max_profit;
26+
# };

0 commit comments

Comments
ย (0)