Skip to content

Commit

Permalink
Merge branch 'DaleStudy:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
jungsiroo authored Jan 19, 2025
2 parents 39f82b3 + 31704db commit 58124b5
Show file tree
Hide file tree
Showing 317 changed files with 12,218 additions and 1 deletion.
27 changes: 27 additions & 0 deletions 3sum/yoonthecoder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
var threeSum = function (nums) {
nums.sort((a, b) => a - b);
const results = [];
for (let i = 0; i < nums.length - 2; i++) {
if (i > 0 && nums[i] === nums[i - 1]) continue;
let left = i + 1;
let right = nums.length - 1;
while (left < right) {
const currSum = nums[i] + nums[left] + nums[right];

if (currSum == 0) {
results.push([nums[i], nums[left], nums[right]]);
left++;
right--;
// to avoid duplicates
while (left < right && nums[left] === nums[left - 1]) left++;
while (left < right && nums[right] === nums[right + 1]) right--;
} else if (currSum < 0) {
left++;
} else right--;
}
}
return results;
};

// Time complexity: O(n^2);
// Space complexity: O(n)
40 changes: 40 additions & 0 deletions best-time-to-buy-and-sell-stock/Chaedie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""
Solution: 1) 2์ค‘ ํฌ๋ฌธ์„ ๋Œ๋ฉด์„œ max๊ฐ’์„ ๊ตฌํ•œ๋‹ค.
Time: O(n^2)
Space: O(1)
Time Limit Exceeded
"""


class Solution:
def maxProfit(self, prices: List[int]) -> int:
result = 0
for l in range(len(prices) - 1):
for r in range(l + 1, len(prices)):
result = max(result, prices[r] - prices[l])

return result


"""
Solution:
1) prices๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ max_profit ์„ ์ฐพ๋Š”๋‹ค.
2) profit ์€ current price - min_profit๋กœ ๊ตฌํ•œ๋‹ค.
Time: O(n)
Space: O(1)
"""


class Solution:
def maxProfit(self, prices: List[int]) -> int:
n = len(prices)
max_profit = 0
min_price = prices[0]

for i in range(1, len(prices)):
profit = prices[i] - min_price
max_profit = max(max_profit, profit)
min_price = min(prices[i], min_price)
return max_profit
43 changes: 43 additions & 0 deletions best-time-to-buy-and-sell-stock/EcoFriendlyAppleSu.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package leetcode_study

/*
* ๋‘ ๊ฑฐ๋ž˜์ผ ์‚ฌ์ด ์ตœ๊ณ ์˜ ์ˆ˜์ต์„ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ
* ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n^2)
* -> ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ ๊ฑฐ๋ž˜์ผ ์‚ฌ์ด ์ตœ๊ณ  ๊ฐ€๊ฒฉ์„ ๊ตฌํ•˜๋Š” ๋กœ์ง: O(n^2)
* ๊ณต๊ฐ„ ๋ณต์žก๋„: O(1)
* ์•„๋ž˜ ๋กœ์ง์€ ์‹œ๊ฐ„ ์ดˆ๊ณผ ๋ฐœ์ƒ O(n^2)์˜ ๋ณต์žก๋„๋ฅผ ์ค„์—ฌ์•ผํ•จ.
* */
fun maxProfit(prices: IntArray): Int {
var result = Int.MIN_VALUE

for (i in prices.indices) {
for (j in i + 1 until prices.size) {
if (prices[i] < prices[j]) {
if (result < prices[j] - prices[i]) {
result = prices[j] - prices[i]
}
}
}
}
if (result == Int.MIN_VALUE) return 0
return result
}

/*
* ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์„ ์ €์žฅํ•˜๋Š” ๋ณ€์ˆ˜์™€ ๊ฐ€์žฅ ํฐ ์ˆ˜์ต์„ ๊ฐ–๋Š” ๋ณ€์ˆ˜๋ฅผ ๋‘๊ณ  ๋ฌธ์ œ ํ•ด๊ฒฐ
* ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n)
* ๊ณต๊ฐ„ ๋ณต์žก๋„: O(1)
* */
fun maxProfit2(prices: IntArray): Int {
var minValue = Int.MAX_VALUE
var maxValue = 0

for (price in prices) {
if (price < minValue) {
minValue = price
} else if (price - minValue > maxValue) {
maxValue = price - minValue
}
}
return maxValue
}
30 changes: 30 additions & 0 deletions best-time-to-buy-and-sell-stock/GangBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Solution {
public int maxProfit(int[] prices) {
/**
1. understanding
- price[i]: i th day's stock price
- to maximize profit, choose a single day to buy, and future day to sell.
- return maximum profit
- [7, 1, 5, 3, 6, 4] -> [0, 0, 4, 4, 5, 5]
- [7, 6, 4, 3, 1] -> [0, 0, 0, 0, 0]
2. strategy
- profit = (sell price) - (buy price)
3. complexity
- time: O(N)
- space: O(1)
*/
int minPrice = prices[0];
for (int i = 0; i <prices.length; i++) {
int tmp = prices[i];
if (i == 0) {
prices[i] = 0;
} else {
prices[i] = Math.max(prices[i-1], prices[i] - minPrice);
}
minPrice = Math.min(minPrice, tmp);
}

return prices[prices.length - 1];
}
}

24 changes: 24 additions & 0 deletions best-time-to-buy-and-sell-stock/HerrineKim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n)
// ๊ณต๊ฐ„๋ณต์žก๋„: O(1)

// ์ตœ์†Œ๊ฐ’์„ ๊ณ„์† ๊ฐฑ์‹ ํ•˜๋ฉด์„œ ์ตœ๋Œ€ ์ด์ต์„ ๊ณ„์‚ฐ

/**
* @param {number[]} prices
* @return {number}
*/
var maxProfit = function (prices) {
let minPrice = Infinity;
let maxProfit = 0;

for (let price of prices) {
if (price < minPrice) {
minPrice = price;
} else {
maxProfit = Math.max(maxProfit, price - minPrice);
}
}

return maxProfit;
};

13 changes: 13 additions & 0 deletions best-time-to-buy-and-sell-stock/HodaeSsi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# ์‹œ๊ฐ„ ๋ณต์žก๋„ : O(n)
# ๊ณต๊ฐ„ ๋ณต์žก๋„ : O(1)
class Solution:
def maxProfit(self, prices: List[int]) -> int:
min_price = float('inf')
max_profit = 0

for price in prices:
min_price = min(min_price, price)
max_profit = max(max_profit, price - min_price)

return max_profit

27 changes: 27 additions & 0 deletions best-time-to-buy-and-sell-stock/Jay-Mo-99.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
๏ปฟ #ํ•ด์„
#prices list๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ ์ตœ์†Ÿ๊ฐ’ min_val์„ ์—…๋ฐ์ดํŠธ ํ•œ๋‹ค
#ํ˜„์žฌ prices[n]๊ณผ min_val์˜ ์ฐจ๋ฅผ ์—…๋ฐ์ดํŠธ ํ•ด์ค€๋‹ค.


#Big O
#N: prices ์˜ ํฌ๊ธฐ

#Time Complexity: O(N)
#- for loop : prices์˜ ์›์†Œ ๊ฐฏ์ˆ˜๋งŒํผ ์ˆœํšŒํ•˜๋ฏ€๋กœ O(N)


#Space Complexity: O(1)
#- min_val, answer : ๋ณ€์ˆ˜๋Š” ์ƒ์ˆ˜์ด๋ฏ€๋กœ O(1)
class Solution(object):
def maxProfit(self, prices):

#Initialize variables
min_val = prices[0]
answer = 0

for n in range(len(prices)):
min_val= min(min_val,prices[n]) #Update min value of the prices list
answer = max(prices[n]-min_val,answer) #Update max value of prices[n] - min value

return answer

41 changes: 41 additions & 0 deletions best-time-to-buy-and-sell-stock/Jeehay28.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* @param {number[]} prices
* @return {number}
*/

// TC : O(n)
// SC : O(1)

var maxProfit = function (prices) {
if (prices.length === 1) {
return 0;
}

// Two variables (profitMax and priceMin) are used to store the maximum profit and minimum price seen, which require O(1) space.
let profitMax = 0;
let priceMin = prices[0];

for (const price of prices) {
const profit = price - priceMin;
profitMax = Math.max(profit, profitMax);
priceMin = Math.min(price, priceMin);
}

return profitMax;
};

// Why Constants Are Ignored in Big-O
// In Big-O notation, O(2) is simplified to O(1) because constants are irrelevant in asymptotic analysis.
// Big-O focuses on how resource usage scales with input size, not fixed values.

// Using 2 variables: O(1)
// Using 10 variables: O(1)
// Using 100 variables: O(1)

// What Space Complexity Looks Like for Larger Growth
// O(n): Memory grows linearly with the input size (e.g., storing an array of n elements).
// O(n^2): Memory grows quadratically (e.g., a 2D matrix with n*n elements).
// ๐‘‚(log ๐‘›): Memory grows logarithmically (e.g., recursive calls in binary search).
// O(1): Fixed memory usage, regardless of input size (e.g., using a fixed number of variables).


30 changes: 30 additions & 0 deletions best-time-to-buy-and-sell-stock/KwonNayeon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""
Constraints:
1. 1 <= prices.length <= 10^5
2. 0 <= prices[i] <= 10^4
Time Complexity: O(n)
Space Complexity: O(1)
ํ’€์ด ๋ฐฉ๋ฒ•:
- ๋ฐฐ์—ด์„ ํ•œ ๋ฒˆ ์ˆœํšŒํ•˜๋ฉด์„œ:
1. ํ˜„์žฌ๊นŒ์ง€์˜ ์ตœ์†Œ ๊ฐ€๊ฒฉ(min_price)์„ ๊ณ„์† ๊ฐฑ์‹ 
2. ํ˜„์žฌ ๊ฐ€๊ฒฉ์—์„œ ์ตœ์†Œ ๊ฐ€๊ฒฉ์„ ๋บ€ ๊ฐ’(ํ˜„์žฌ ๊ฐ€๋Šฅํ•œ ์ด์ต)๊ณผ ๊ธฐ์กด ์ตœ๋Œ€ ์ด์ต์„ ๋น„๊ตํ•˜์—ฌ ๋” ํฐ ๊ฐ’์„ ์ €์žฅ
- ์ด ๋ฐฉ์‹์œผ๋กœ ๊ฐ ์‹œ์ ์—์„œ ๊ฐ€๋Šฅํ•œ ์ตœ๋Œ€ ์ด์ต์„ ๊ณ„์‚ฐํ•จ
To Do:
- ๋‹ค๋ฅธ ์ ‘๊ทผ ๋ฐฉ๋ฒ• ์ฐพ์•„๋ณด๊ธฐ (Two Pointers, Dynamic Programming)
"""

class Solution:
def maxProfit(self, prices: List[int]) -> int:
min_price = prices[0]
max_profit = 0

for i in range(1, len(prices)):
min_price = min(min_price, prices[i])
max_profit = max(max_profit, prices[i] - min_price)

return max_profit

15 changes: 15 additions & 0 deletions best-time-to-buy-and-sell-stock/Real-Reason.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package leetcode_study

import kotlin.math.max
import kotlin.math.min
fun maxProfit(prices: IntArray): Int {
var minPrice = prices[0]
var maxProfit = 0

for (i in 0 until prices.size) {
maxProfit = max(maxProfit, prices[i] - minPrice)
minPrice = min(minPrice, prices[i])
}

return maxProfit
}
19 changes: 19 additions & 0 deletions best-time-to-buy-and-sell-stock/YeomChaeeun.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* ์ตœ๋Œ€ ์ˆ˜์ต์„ ๊ตฌํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜
* ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ณต์žก๋„
* - ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n)
* - ๊ณต๊ฐ„ ๋ณต์žก๋„: O(1)
* @param prices
*/
function maxProfit(prices: number[]): number {
let min = prices[0]
let total = 0

for(let i = 1 ; i < prices.length ; i++) {
min = Math.min(min, prices[i])
// console.log(dp[i],'===', dp[i-1], '===', prices[i])
total = Math.max(total, prices[i] - min)
}

return total
}
28 changes: 28 additions & 0 deletions best-time-to-buy-and-sell-stock/Yjason-K.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* ์ตœ๋Œ€ ์ด์ต์„ ๊ณ„์‚ฐํ•˜๋Š” ํ•จ์ˆ˜
* @param {number[]} prices
* @returns {number}
*
* ์‹œ๊ฐ„ ๋ณต์žก๋„ : O(n) (n: ์ฃผ์‹ ๊ฐ€๊ฒฉ ๋ฐฐ์—ด์˜ ๊ธธ์ด)
* ๊ณต๊ฐ„ ๋ณต์žก๋„ : 0(1) (์ถ”๊ฐ€ ์ž๋ฃŒ๊ตฌ์กฐ X)
*/
function maxProfit(prices: number[]): number {
let minPrice = 100001; // ์ง€๊ธˆ๊นŒ์ง€์˜ ์ตœ์†Œ ๊ฐ€๊ฒฉ
let maxProfit = 0; // ์ตœ๋Œ€ ์ด์ต

for (let price of prices) {
// ์ตœ์†Œ ๊ฐ€๊ฒฉ ๊ฐฑ์‹ 
if (price < minPrice) {
minPrice = price;
}

// ํ˜„์žฌ ๊ฐ€๊ฒฉ์—์„œ ์ตœ์†Œ ๊ฐ€๊ฒฉ์„ ๋บ€ ์ด์ต์ด ์ตœ๋Œ€ ์ด์ต๋ณด๋‹ค ํฌ๋‹ค๋ฉด ๊ฐฑ์‹ 
const potentialProfit = price - minPrice;
if (potentialProfit > maxProfit) {
maxProfit = potentialProfit;
}
}

return maxProfit;
}

19 changes: 19 additions & 0 deletions best-time-to-buy-and-sell-stock/aa601.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# ์‹œ๊ฐ„๋ณต์žก๋„ O(n)
# ๊ณต๊ฐ„๋ณต์žก๋„ O(1)
class Solution:
def maxProfit(self, prices: list[int]) -> int:
min_p = prices[0] # ์ตœ์†Œ ๊ฐ€๊ฒฉ ์„ค์ • : ๋ฐฐ์—ด์˜ ์ฒซ ๋ฒˆ์งธ ๊ฐ€๊ฒฉ
cur = 0
max_p = 0
for n in prices:
if n < min_p: # ํ˜„์žฌ ๊ฐ€๊ฒฉ์ด ์ตœ์†Œ ๊ฐ€๊ฒฉ๋ณด๋‹ค ์ž‘๋‹ค๋ฉด ์ตœ์†Œ๊ฐ€๊ฒฉ ๊ฐฑ์‹ 
min_p = n
cur = n - min_p # ํ˜„์žฌ ์ด์ต ๊ณ„์‚ฐ
if max_p < cur: # ํ˜„์žฌ ์ด์ต๊ณผ ์ตœ๋Œ€๋กœ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ์ด์ต ๋น„๊ต
max_p = cur # ์ตœ๋Œ€ ์ด์ต ๊ฐฑ์‹ ์‹ 

return max_p




18 changes: 18 additions & 0 deletions best-time-to-buy-and-sell-stock/anniemon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* ์‹œ๊ฐ„ ๋ณต์žก๋„: prices.length๋งŒํผ ์ˆœํšŒํ•˜๋ฏ€๋กœ O(n)
* ๊ณต๊ฐ„ ๋ณต์žก๋„: ์ƒ์ˆ˜ ํฌ๊ธฐ์˜ ๋ณ€์ˆ˜๋งŒ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ O(1)
*/
/**
* @param {number[]} prices
* @return {number}
*/
var maxProfit = function (prices) {
let maxProfit = 0;
let min = prices[0];

for (let i = 0; i < prices.length; i++) {
maxProfit = Math.max(maxProfit, prices[i] - min);
min = Math.min(min, prices[i]);
}
return maxProfit;
};
Loading

0 comments on commit 58124b5

Please sign in to comment.