Skip to content

Commit 772e847

Browse files
2 parents 6f574b1 + a8b654c commit 772e847

File tree

446 files changed

+17816
-313
lines changed

Some content is hidden

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

446 files changed

+17816
-313
lines changed

3sum/iam-edwin.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import java.util.ArrayList;
2+
import java.util.HashMap;
3+
import java.util.HashSet;
4+
import java.util.List;
5+
import java.util.Map;
6+
import java.util.Set;
7+
8+
class Solution {
9+
public List<List<Integer>> threeSum(int[] nums) {
10+
List<List<Integer>> result = new ArrayList<>();
11+
12+
Map<Integer, Integer> counter = new HashMap<>();
13+
for (int num : nums) {
14+
int count = counter.getOrDefault(num, 0);
15+
counter.put(num, count + 1);
16+
}
17+
18+
Set<Integer> keySet = new HashSet<>(counter.keySet());
19+
for (int num1 : keySet) {
20+
int num1Count = counter.get(num1);
21+
if (num1Count > 1) {
22+
counter.put(num1, num1Count - 1);
23+
} else {
24+
counter.remove(num1);
25+
}
26+
27+
for (int num2 : counter.keySet()) {
28+
int num3 = -num1 - num2;
29+
int count = counter.getOrDefault(num3, 0);
30+
if (((num2 == num3 && count >= 2) || (num2 != num3 && count >= 1)) && num3 >= num2) {
31+
result.add(List.of(num1, num2, num3));
32+
}
33+
}
34+
35+
counter.remove(num1);
36+
}
37+
38+
return result;
39+
}
40+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Time Complexity: O(n), where n is the length of the prices array
2+
// Space Complexity: O(1)
3+
4+
function maxProfit(prices: number[]): number {
5+
// input: an array prices, prices[i] = the stock price of ith day
6+
// output: the maximum profit || 0
7+
8+
// prices = [7, 1, 5, 3, 6, 4]
9+
// buy = 1, sell = 6, profit = 6 -1 = 5
10+
11+
let minBuy = prices[0];
12+
let maxProfit = 0;
13+
14+
for (let i = 1; i < prices.length; i++) {
15+
minBuy = Math.min(prices[i], minBuy);
16+
maxProfit = Math.max(prices[i] - minBuy, maxProfit);
17+
}
18+
19+
return maxProfit;
20+
}
21+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// time: O(n) space: O(n)
2+
class Solution {
3+
func maxProfit(_ prices: [Int]) -> Int {
4+
guard !prices.isEmpty else { return 0 }
5+
var result = [Int]()
6+
var current = prices[0]
7+
8+
for price in prices {
9+
if current > price {
10+
current = price
11+
continue
12+
}
13+
result.append(price - current)
14+
}
15+
return result.max() ?? 0
16+
}
17+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @param {number[]} prices
3+
* @return {number}
4+
*/
5+
var maxProfit = function (prices) {
6+
// 가장 작은 수
7+
let minNum = prices[0];
8+
// 차이 값
9+
let maxProfit = 0;
10+
for (let i = 1; i < prices.length; i++) {
11+
minNum = Math.min(minNum, prices[i - 1]); // 이전꺼 중 가장 작은 수
12+
maxProfit = Math.max(maxProfit, prices[i] - minNum);
13+
}
14+
return maxProfit;
15+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// NOTE: tc --> O(n)
2+
class Solution {
3+
public int maxProfit(int[] prices) {
4+
5+
int curMax = 0;
6+
int gMax = 0;
7+
8+
if(prices.length == 0) return 0;
9+
10+
int sell = prices[0];
11+
for(int i = 1; i < prices.length; i++) {
12+
curMax = Math.max(0, prices[i] - sell);
13+
14+
// NOTE: 새롭게 시작하는게 더 좋은경우
15+
if(curMax == 0) {
16+
sell = prices[i];
17+
}
18+
19+
gMax = Math.max(curMax, gMax);
20+
}
21+
22+
return gMax;
23+
}
24+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from typing import List
2+
class Solution:
3+
"""
4+
- Time Complexity: O(n), n = len(prices)
5+
- Space Complexity: O(1)
6+
"""
7+
def maxProfit(self, prices: List[int]) -> int:
8+
min_price = float("inf")
9+
max_profit = 0
10+
11+
for price in prices:
12+
min_price = min(min_price, price)
13+
max_profit = max(max_profit, price - min_price)
14+
15+
return max_profit
16+
17+
tc = [
18+
([7,1,5,3,6,4], 5),
19+
([7,6,4,3,1], 0)
20+
]
21+
22+
for i, (prices, e) in enumerate(tc, 1):
23+
sol = Solution()
24+
r = sol.maxProfit(prices)
25+
print(f"TC {i} is Passed!" if r == e else f"TC {i} is Failed! - Expected: {e}, Result: {r}")
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# 연관 링크
2+
- [문제 풀이 스케줄](https://github.com/orgs/DaleStudy/projects/6/views/5)
3+
- [답안 코드 제출법](https://github.com/DaleStudy/leetcode-study/wiki/%EB%8B%B5%EC%95%88-%EC%A0%9C%EC%B6%9C-%EA%B0%80%EC%9D%B4%EB%93%9C)
4+
5+
# Problem
6+
- 문제 링크 : https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
7+
- 문제 이름 : Best Time to Buy and Sell Stock
8+
- 문제 번호 : 121
9+
- 난이도 : easy
10+
- 카테고리 :
11+
12+
# 문제 설명
13+
14+
15+
# 아이디어
16+
- 어떤 방법으로 접근했는지 서술
17+
- 포스 vs 최적화 아이디어 차이 등
18+
- 잡도에 대한 고려
19+
20+
# ✅ 코드 (Solution)
21+
22+
```cpp
23+
class Solution {
24+
public:
25+
int maxProfit(vector<int>& prices) {
26+
int localMax = prices[prices.size()-1];
27+
int res = 0;
28+
for(int i=prices.size()-2;i>=0;i--){
29+
localMax = max(localMax, prices[i]);
30+
res = max(res, localMax-prices[i]);
31+
}
32+
33+
return res;
34+
}
35+
};
36+
```
37+
38+
# 🔍 코드 설명
39+
40+
- local max를 설정
41+
42+
# 최적화 포인트 (Optimality Discussion)
43+
• 최적화한 이유와 원리
44+
• 더 줄일 수 있는 여지는 있는가?
45+
• 기존 방법 대비 얼마나 효율적이었는지
46+
47+
# 🧪 테스트 & 엣지 케이스
48+
49+
# 📚 관련 지식 복습
50+
51+
# 🔁 회고
52+
53+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# TC: O(N), SC: O(1)
2+
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+
max_profit = max(price - min_price, max_profit)
10+
min_price = min(price, min_price)
11+
return max_profit
12+
13+
# TS 풀이
14+
# 배열 요소(숫자값)을 직접 순회하려면 for ... of 사용 혹은 forEach
15+
# for ... in -> 인덱스를 가져옴
16+
17+
# function maxProfit(prices: number[]): number {
18+
# let max_profit: number = 0;
19+
# let min_price: number = prices[0];
20+
21+
# for (let price of prices) {
22+
# max_profit = Math.max(max_profit, price - min_price);
23+
# min_price = Math.min(min_price, price);
24+
# }
25+
# return max_profit;
26+
# };
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
time complexity : O(n)
3+
space complexity : O(1)
4+
*/
5+
function maxProfit(prices: number[]): number {
6+
let left = 0
7+
let right = 0
8+
let maxProfit = 0
9+
10+
while (right < prices.length) {
11+
const curProfit = prices[right] - prices[left]
12+
if (prices[left] < prices[right]) {
13+
maxProfit = Math.max(curProfit, maxProfit)
14+
} else {
15+
left = right
16+
}
17+
right += 1
18+
}
19+
return maxProfit
20+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var maxProfit = function(prices) {
2+
let minPrice = Infinity;
3+
let maxProfit = 0;
4+
5+
for (let 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)