Skip to content

Commit f611f28

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents 98062a1 + a61ca49 commit f611f28

File tree

263 files changed

+7798
-10
lines changed

Some content is hidden

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

263 files changed

+7798
-10
lines changed

3sum/devyejin.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution:
2+
def threeSum(self, nums: List[int]) -> List[List[int]]:
3+
nums.sort() # O(n)
4+
5+
n = len(nums)
6+
result = []
7+
8+
for i in range(n - 2):
9+
# target을 잡을때도 이전에 구했다면 패스
10+
if i > 0 and nums[i] == nums[i - 1]:
11+
continue
12+
13+
target = - nums[i]
14+
left, right = i + 1, n - 1
15+
16+
while left < right:
17+
two_sum = nums[left] + nums[right]
18+
19+
if two_sum == target:
20+
result.append([nums[i], nums[left], nums[right]])
21+
22+
while left < right and nums[left] == nums[left + 1]:
23+
left += 1
24+
25+
while left < right and nums[right] == nums[right - 1]:
26+
right -= 1
27+
28+
left += 1
29+
right -= 1
30+
31+
elif two_sum < target:
32+
left += 1
33+
else:
34+
right -= 1
35+
36+
return result

3sum/sora0319.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,4 @@ public List<List<Integer>> threeSum(int[] nums) {
3535
}
3636
}
3737

38+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// TC: O(n)
2+
// SC: O(1)
3+
impl Solution {
4+
pub fn max_profit(prices: Vec<i32>) -> i32 {
5+
let mut min_price = i32::MAX;
6+
let mut max_profit = 0;
7+
for price in prices {
8+
if price < min_price {
9+
min_price = price;
10+
}
11+
if price - min_price > max_profit {
12+
max_profit = price - min_price;
13+
}
14+
}
15+
max_profit
16+
}
17+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function maxProfit(prices: number[]): number {
2+
3+
// Runtime: 1ms
4+
// Memory: 66.23MB
5+
let minPrice: number = prices[0];
6+
let maxProfit: number = 0;
7+
8+
for (let i = 1; i < prices.length; i++) {
9+
// 1. compare the number later than now
10+
if (prices[i] < minPrice) {
11+
// 2. if there's bigger number later, set it as the standard
12+
minPrice = prices[i];
13+
} else {
14+
maxProfit = Math.max(maxProfit, prices[i] - minPrice);
15+
}
16+
}
17+
return maxProfit;
18+
19+
// What I was planning to do: Recursion
20+
// Result: referring other's code
21+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
func maxProfit(_ prices: [Int]) -> Int {
3+
var lowPrice: Int = prices[0]
4+
var maxProfit: Int = 0
5+
for i in 0..<prices.endIndex {
6+
lowPrice = min(lowPrice, prices[i])
7+
maxProfit = max(maxProfit, prices[i] - lowPrice)
8+
}
9+
return maxProfit
10+
}
11+
}
12+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# time complexity : O(n)
2+
# space complexity : O(1)
3+
from typing import List
4+
5+
6+
class Solution:
7+
def maxProfit(self, prices: List[int]) -> int:
8+
n = len(prices)
9+
min_price = prices[0]
10+
max_profit = 0
11+
12+
for i in range(1, n):
13+
min_price = min(min_price, prices[i])
14+
max_profit = max(max_profit, prices[i] - min_price)
15+
16+
return max_profit
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var maxProfit = function (prices) {
2+
let minPrice = Infinity;
3+
let maxProfit = 0;
4+
5+
for (let price of prices) {
6+
// 가장 싼 가격을 갱신
7+
if (price < minPrice) {
8+
minPrice = price;
9+
} else {
10+
// 최대 이익을 갱신
11+
maxProfit = Math.max(maxProfit, price - minPrice);
12+
}
13+
}
14+
15+
return maxProfit;
16+
};

best-time-to-buy-and-sell-stock/hi-rachel.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
1-
# TC: O(N), SC: O(1)
1+
"""
2+
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/
3+
4+
You are given an array prices where prices[i] is the price of a given stock on the ith day.
5+
6+
You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.
7+
8+
Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.
9+
10+
TC: O(N), SC: O(1)
11+
"""
12+
13+
from typing import List
214

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

820
for price in prices:
9-
max_profit = max(price - min_price, max_profit)
1021
min_price = min(price, min_price)
22+
max_profit = max(price - min_price, max_profit)
23+
1124
return max_profit
1225

1326
# TS 풀이
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution:
2+
"""
3+
1. 브루트포스
4+
2중 for문이라서 O(n^2)임.
5+
prices의 길이가 10^5이므로 10^10이 되면서 시간초과가 발생
6+
7+
2. 이분탐색으로 가능할까 했지만 DP를 사용해야 하는 문제 같음
8+
시간복잡도는 O(n)이 나옴
9+
"""
10+
"""
11+
def maxProfit(self, prices: List[int]) -> int:
12+
max_profit = 0
13+
for i in range(len(prices)-1):
14+
for j in range(i, len(prices)):
15+
profit = prices[j] - prices[i]
16+
max_profit = max(max_profit, profit)
17+
return max_profit
18+
"""
19+
def maxProfit(self, prices: List[int]) -> int:
20+
max_profit = 0
21+
min_price = prices[0]
22+
for price in prices:
23+
max_profit = max(max_profit, price - min_price)
24+
min_price = min(price, min_price)
25+
return max_profit
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function maxProfit(prices: number[]): number {
2+
let minPrice = Infinity;
3+
let maxProfit = 0;
4+
5+
for (const price of prices) {
6+
if (price < minPrice) {
7+
minPrice = price;
8+
} else {
9+
maxProfit = Math.max(maxProfit, price - minPrice);
10+
}
11+
}
12+
13+
return maxProfit;
14+
}

0 commit comments

Comments
 (0)