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+

0 commit comments

Comments
Β (0)