Skip to content

Commit 390832a

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents c68a5b0 + 22fea49 commit 390832a

File tree

104 files changed

+3623
-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.

104 files changed

+3623
-0
lines changed

โ€Ž3sum/yoonthecoder.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
var threeSum = function (nums) {
2+
nums.sort((a, b) => a - b);
3+
const results = [];
4+
for (let i = 0; i < nums.length - 2; i++) {
5+
if (i > 0 && nums[i] === nums[i - 1]) continue;
6+
let left = i + 1;
7+
let right = nums.length - 1;
8+
while (left < right) {
9+
const currSum = nums[i] + nums[left] + nums[right];
10+
11+
if (currSum == 0) {
12+
results.push([nums[i], nums[left], nums[right]]);
13+
left++;
14+
right--;
15+
// to avoid duplicates
16+
while (left < right && nums[left] === nums[left - 1]) left++;
17+
while (left < right && nums[right] === nums[right + 1]) right--;
18+
} else if (currSum < 0) {
19+
left++;
20+
} else right--;
21+
}
22+
}
23+
return results;
24+
};
25+
26+
// Time complexity: O(n^2);
27+
// Space complexity: O(n)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package leetcode_study
2+
3+
/*
4+
* ๋‘ ๊ฑฐ๋ž˜์ผ ์‚ฌ์ด ์ตœ๊ณ ์˜ ์ˆ˜์ต์„ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ
5+
* ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n^2)
6+
* -> ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ ๊ฑฐ๋ž˜์ผ ์‚ฌ์ด ์ตœ๊ณ  ๊ฐ€๊ฒฉ์„ ๊ตฌํ•˜๋Š” ๋กœ์ง: O(n^2)
7+
* ๊ณต๊ฐ„ ๋ณต์žก๋„: O(1)
8+
* ์•„๋ž˜ ๋กœ์ง์€ ์‹œ๊ฐ„ ์ดˆ๊ณผ ๋ฐœ์ƒ O(n^2)์˜ ๋ณต์žก๋„๋ฅผ ์ค„์—ฌ์•ผํ•จ.
9+
* */
10+
fun maxProfit(prices: IntArray): Int {
11+
var result = Int.MIN_VALUE
12+
13+
for (i in prices.indices) {
14+
for (j in i + 1 until prices.size) {
15+
if (prices[i] < prices[j]) {
16+
if (result < prices[j] - prices[i]) {
17+
result = prices[j] - prices[i]
18+
}
19+
}
20+
}
21+
}
22+
if (result == Int.MIN_VALUE) return 0
23+
return result
24+
}
25+
26+
/*
27+
* ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์„ ์ €์žฅํ•˜๋Š” ๋ณ€์ˆ˜์™€ ๊ฐ€์žฅ ํฐ ์ˆ˜์ต์„ ๊ฐ–๋Š” ๋ณ€์ˆ˜๋ฅผ ๋‘๊ณ  ๋ฌธ์ œ ํ•ด๊ฒฐ
28+
* ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n)
29+
* ๊ณต๊ฐ„ ๋ณต์žก๋„: O(1)
30+
* */
31+
fun maxProfit2(prices: IntArray): Int {
32+
var minValue = Int.MAX_VALUE
33+
var maxValue = 0
34+
35+
for (price in prices) {
36+
if (price < minValue) {
37+
minValue = price
38+
} else if (price - minValue > maxValue) {
39+
maxValue = price - minValue
40+
}
41+
}
42+
return maxValue
43+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n)
2+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(1)
3+
4+
// ์ตœ์†Œ๊ฐ’์„ ๊ณ„์† ๊ฐฑ์‹ ํ•˜๋ฉด์„œ ์ตœ๋Œ€ ์ด์ต์„ ๊ณ„์‚ฐ
5+
6+
/**
7+
* @param {number[]} prices
8+
* @return {number}
9+
*/
10+
var maxProfit = function (prices) {
11+
let minPrice = Infinity;
12+
let maxProfit = 0;
13+
14+
for (let price of prices) {
15+
if (price < minPrice) {
16+
minPrice = price;
17+
} else {
18+
maxProfit = Math.max(maxProfit, price - minPrice);
19+
}
20+
}
21+
22+
return maxProfit;
23+
};
24+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# ์‹œ๊ฐ„ ๋ณต์žก๋„ : O(n)
2+
# ๊ณต๊ฐ„ ๋ณต์žก๋„ : O(1)
3+
class Solution:
4+
def maxProfit(self, prices: List[int]) -> int:
5+
min_price = float('inf')
6+
max_profit = 0
7+
8+
for price in prices:
9+
min_price = min(min_price, price)
10+
max_profit = max(max_profit, price - min_price)
11+
12+
return max_profit
13+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
๏ปฟ #ํ•ด์„
2+
#prices list๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ ์ตœ์†Ÿ๊ฐ’ min_val์„ ์—…๋ฐ์ดํŠธ ํ•œ๋‹ค
3+
#ํ˜„์žฌ prices[n]๊ณผ min_val์˜ ์ฐจ๋ฅผ ์—…๋ฐ์ดํŠธ ํ•ด์ค€๋‹ค.
4+
5+
6+
#Big O
7+
#N: prices ์˜ ํฌ๊ธฐ
8+
9+
#Time Complexity: O(N)
10+
#- for loop : prices์˜ ์›์†Œ ๊ฐฏ์ˆ˜๋งŒํผ ์ˆœํšŒํ•˜๋ฏ€๋กœ O(N)
11+
12+
13+
#Space Complexity: O(1)
14+
#- min_val, answer : ๋ณ€์ˆ˜๋Š” ์ƒ์ˆ˜์ด๋ฏ€๋กœ O(1)
15+
class Solution(object):
16+
def maxProfit(self, prices):
17+
18+
#Initialize variables
19+
min_val = prices[0]
20+
answer = 0
21+
22+
for n in range(len(prices)):
23+
min_val= min(min_val,prices[n]) #Update min value of the prices list
24+
answer = max(prices[n]-min_val,answer) #Update max value of prices[n] - min value
25+
26+
return answer
27+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @param {number[]} prices
3+
* @return {number}
4+
*/
5+
6+
// TC : O(n)
7+
// SC : O(1)
8+
9+
var maxProfit = function (prices) {
10+
if (prices.length === 1) {
11+
return 0;
12+
}
13+
14+
// Two variables (profitMax and priceMin) are used to store the maximum profit and minimum price seen, which require O(1) space.
15+
let profitMax = 0;
16+
let priceMin = prices[0];
17+
18+
for (const price of prices) {
19+
const profit = price - priceMin;
20+
profitMax = Math.max(profit, profitMax);
21+
priceMin = Math.min(price, priceMin);
22+
}
23+
24+
return profitMax;
25+
};
26+
27+
// Why Constants Are Ignored in Big-O
28+
// In Big-O notation, O(2) is simplified to O(1) because constants are irrelevant in asymptotic analysis.
29+
// Big-O focuses on how resource usage scales with input size, not fixed values.
30+
31+
// Using 2 variables: O(1)
32+
// Using 10 variables: O(1)
33+
// Using 100 variables: O(1)
34+
35+
// What Space Complexity Looks Like for Larger Growth
36+
// O(n): Memory grows linearly with the input size (e.g., storing an array of n elements).
37+
// O(n^2): Memory grows quadratically (e.g., a 2D matrix with n*n elements).
38+
// ๐‘‚(log ๐‘›): Memory grows logarithmically (e.g., recursive calls in binary search).
39+
// O(1): Fixed memory usage, regardless of input size (e.g., using a fixed number of variables).
40+
41+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
Constraints:
3+
1. 1 <= prices.length <= 10^5
4+
2. 0 <= prices[i] <= 10^4
5+
6+
Time Complexity: O(n)
7+
8+
Space Complexity: O(1)
9+
10+
ํ’€์ด ๋ฐฉ๋ฒ•:
11+
- ๋ฐฐ์—ด์„ ํ•œ ๋ฒˆ ์ˆœํšŒํ•˜๋ฉด์„œ:
12+
1. ํ˜„์žฌ๊นŒ์ง€์˜ ์ตœ์†Œ ๊ฐ€๊ฒฉ(min_price)์„ ๊ณ„์† ๊ฐฑ์‹ 
13+
2. ํ˜„์žฌ ๊ฐ€๊ฒฉ์—์„œ ์ตœ์†Œ ๊ฐ€๊ฒฉ์„ ๋บ€ ๊ฐ’(ํ˜„์žฌ ๊ฐ€๋Šฅํ•œ ์ด์ต)๊ณผ ๊ธฐ์กด ์ตœ๋Œ€ ์ด์ต์„ ๋น„๊ตํ•˜์—ฌ ๋” ํฐ ๊ฐ’์„ ์ €์žฅ
14+
- ์ด ๋ฐฉ์‹์œผ๋กœ ๊ฐ ์‹œ์ ์—์„œ ๊ฐ€๋Šฅํ•œ ์ตœ๋Œ€ ์ด์ต์„ ๊ณ„์‚ฐํ•จ
15+
16+
To Do:
17+
- ๋‹ค๋ฅธ ์ ‘๊ทผ ๋ฐฉ๋ฒ• ์ฐพ์•„๋ณด๊ธฐ (Two Pointers, Dynamic Programming)
18+
"""
19+
20+
class Solution:
21+
def maxProfit(self, prices: List[int]) -> int:
22+
min_price = prices[0]
23+
max_profit = 0
24+
25+
for i in range(1, len(prices)):
26+
min_price = min(min_price, prices[i])
27+
max_profit = max(max_profit, prices[i] - min_price)
28+
29+
return max_profit
30+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* ์‹œ๊ฐ„ ๋ณต์žก๋„: prices.length๋งŒํผ ์ˆœํšŒํ•˜๋ฏ€๋กœ O(n)
3+
* ๊ณต๊ฐ„ ๋ณต์žก๋„: ์ƒ์ˆ˜ ํฌ๊ธฐ์˜ ๋ณ€์ˆ˜๋งŒ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ O(1)
4+
*/
5+
/**
6+
* @param {number[]} prices
7+
* @return {number}
8+
*/
9+
var maxProfit = function (prices) {
10+
let maxProfit = 0;
11+
let min = prices[0];
12+
13+
for (let i = 0; i < prices.length; i++) {
14+
maxProfit = Math.max(maxProfit, prices[i] - min);
15+
min = Math.min(min, prices[i]);
16+
}
17+
return maxProfit;
18+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/
3+
* Runtime: 6ms, Memory: 62.30MB
4+
*
5+
* Time Complexity: O(N)
6+
* Space Complexity: O(1)
7+
*/
8+
9+
function maxProfit(prices: number[]): number {
10+
let buyingPrice = prices[0];
11+
let profit = 0;
12+
13+
for (const price of prices) {
14+
buyingPrice = Math.min(price, buyingPrice);
15+
profit = Math.max(profit, price - buyingPrice);
16+
}
17+
18+
return profit;
19+
}
20+
21+
maxProfit([7, 1, 5, 3, 6, 4]);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public int maxProfit(int[] prices) {
3+
int result = 0;
4+
int maxProfit = 0;
5+
int buyStock = prices[0];
6+
7+
// ํ•œ๋ฒˆ ๋Œ๋ฉด์„œ ๋‚˜๋ณด๋‹ค ์ž‘์€ ๊ฒƒ์ด ๋‚˜์˜ฌ ๋•Œ๊นŒ์ง€ ์ด์ต์„ ๋ณธ๋‹ค
8+
9+
10+
for (int price: prices) {
11+
// ๋‚˜๋ณด๋‹ค ์ž‘์€ ๊ฒŒ ๋‚˜์˜ค๋ฉด MAX ์ด์ต์„ ๊ฐฑ์‹ ํ•˜๊ณ  ๊ฑฐ๊ธฐ๋ถ€์„œ ๋‹ค์‹œ ์‹œ์ž‘ํ•œ๋‹ค.
12+
if (price < buyStock) {
13+
result = Math.max(result, maxProfit);
14+
maxProfit = 0;
15+
buyStock = price;
16+
} else {
17+
maxProfit = Math.max(price - buyStock, maxProfit);
18+
}
19+
}
20+
21+
return Math.max(result, maxProfit);
22+
}
23+
}
24+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Big-O ์˜ˆ์ธก
2+
# Time : O(n)
3+
# Space : O(1)
4+
class Solution:
5+
def maxProfit(self, prices: List[int]) -> int:
6+
min_price = prices[0]
7+
profit = 0
8+
9+
for price in prices[1:]:
10+
if min_price > price:
11+
min_price = price
12+
13+
profit = max(profit, price - min_price)
14+
return profit
15+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @param {number[]} prices
3+
* @return {number}
4+
*/
5+
var maxProfit = function(prices) {
6+
let answer = 0
7+
let leftMin = prices[0]
8+
9+
for(let i = 1; i < prices.length; ++i) {
10+
answer = Math.max(answer, prices[i] - leftMin);
11+
leftMin = Math.min(prices[i], leftMin);
12+
}
13+
14+
return answer;
15+
};
16+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// ํˆฌํฌ์ธํ„ฐ๋ฅผ ์“ธ ํ•„์š” ์—†์Œ. ๊ทธ๋ƒฅ ์ตœ์†Œ๊ฐ’์„ ์ฐพ์•„ ๋นผ์ฃผ๋ฉด O(N)์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.
2+
// ํˆฌํฌ์ธํ„ฐ์™€ ํฐ ์ฐจ์ด๊ฐ€ ์—†๋Š” ์ด์œ ๋Š” ํˆฌํฌ์ธํ„ฐ๋„ O(N)์ด๊ธฐ ๋•Œ๋ฌธ. ์•„์ฃผ ์•ฝ๊ฐ„์˜ ๊ณต๊ฐ„๋ณต์žก๋„ ์ฐจ์ด๋งŒ ์žˆ์„ ๋“ฏ
3+
// ๊ฒฐ๊ณผ๋Š” ํฐ ์ฐจ์ด ์—†์œผ๋‚˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ž์ฒด๊ฐ€ ๋” ์‰ฝ๋‹ค.
4+
class Solution {
5+
public int maxProfit(int[] prices) {
6+
int minPrice = Integer.MAX_VALUE;
7+
int maxProfit = 0;
8+
9+
for (int price : prices) {
10+
if (price < minPrice) {
11+
minPrice = price;
12+
} else {
13+
maxProfit = Math.max(maxProfit, price - minPrice);
14+
}
15+
}
16+
17+
return maxProfit;
18+
}
19+
}
20+
21+
// ์ฒ˜์Œ ์ƒ๊ฐํ•œ ํˆฌํฌ์ธํ„ฐ ๋ฐฉ์‹
22+
class Solution {
23+
public int maxProfit(int[] prices) {
24+
int left = 0;
25+
int right = 1;
26+
int maxProfit = 0;
27+
28+
while (right < prices.length) {
29+
if (prices[left] < prices[right]) {
30+
int profit = prices[right] - prices[left];
31+
maxProfit = Math.max(maxProfit, profit);
32+
} else {
33+
left = right;
34+
}
35+
right++;
36+
}
37+
38+
return maxProfit;
39+
}
40+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
# O(n), n = len(prices)
3+
def maxProfit(self, prices: List[int]) -> int:
4+
min_price = float('inf')
5+
profit = 0
6+
for price in prices:
7+
min_price = min(min_price, price)
8+
profit = max(profit, price - min_price)
9+
return profit
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# O(N) times, O(1) spaces
2+
# price[i] ๊ฐ€๊ฒฉ์œผ๋กœ ํŒ”์•„์„œ ๊ฐ€์žฅ ์ˆ˜์ต์„ ๋งŽ์ด ๋‚ด๋ ค๋ฉด, i-1๋ฒˆ์งธ๊นŒ์ง€ ์ค‘ ๊ฐ€์žฅ ๊ฐ’์ด ์‹ผ ๋‚ ์งœ์—์„œ ์ฃผ์‹์„ ์‚ฌ๋ฉด ๋œ๋‹ค
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+
profit = price - min_price
10+
max_profit = max(max_profit, profit)
11+
min_price = min(min_price, price)
12+
13+
return max_profit

0 commit comments

Comments
ย (0)