-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'DaleStudy:main' into main
- Loading branch information
Showing
317 changed files
with
12,218 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
Oops, something went wrong.