Skip to content

Commit c66bbbc

Browse files
committed
2 parents 85fb4cf + 2eac014 commit c66bbbc

File tree

59 files changed

+2209
-20
lines changed

Some content is hidden

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

59 files changed

+2209
-20
lines changed

โ€Ž3sum/sungjinwi.cppโ€Ž

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
ํ’€์ด :
3+
nums ๋ฐฐ์—ด์„ ์ •๋ ฌ์‹œํ‚จ ํ›„ ๋ฐ˜๋ณต๋˜๋Š” ๊ฐ’์„ ๊ฑด๋„ˆ๋›ฐ๋ฉฐ ๋‘ ํฌ์ธํ„ฐ ๊ธฐ๋ฒ•์„ ์‚ฌ์šฉํ•œ๋‹ค
4+
iํฌ์ธํ„ฐ์™€ left, right ํฌ์ธํ„ฐ์˜ ๊ฐ’์˜ ํ•ฉ์ด 0๋ณด๋‹ค ์ž‘์œผ๋ฉด left++, ํฌ๋ฉด right--
5+
0์ด๋ฉด ans์— ์ €์žฅํ•˜๊ณ  left++, right--ํ•˜๋Š” ๋กœ์ง์„ left < right์ธ ๋™์•ˆ ๋ฐ˜๋ณตํ•œ๋‹ค
6+
7+
nums์˜ ๊ธธ์ด N
8+
9+
TC : O(N^2)
10+
์™ธ๋ถ€ ๋ฐ˜๋ณต๋ฌธ N * ๋‚ด๋ถ€ ๋ฐ˜๋ณต๋ฌธ N
11+
12+
SC : O(1) (ans ์ œ์™ธ)
13+
left, right, threeSum 3๊ฐœ์˜ ๋ณ€์ˆ˜๋งŒ ์‚ฌ์šฉํ•œ๋‹ค
14+
*/
15+
16+
#include <vector>
17+
#include <algorithm>
18+
using namespace std;
19+
20+
class Solution {
21+
public:
22+
vector<vector<int>> threeSum(vector<int>& nums) {
23+
vector<vector<int>> ans;
24+
int left;
25+
int right;
26+
int threeSum;
27+
28+
sort(nums.begin(), nums.end());
29+
for (int i = 0; i < nums.size() - 2; i++)
30+
{
31+
// iํฌ์ธํ„ฐ ์ค‘๋ณต ์ œ๊ฑฐ
32+
if (i > 0 && nums[i] == nums[i - 1])
33+
continue ;
34+
35+
left = i + 1;
36+
right = nums.size() - 1;
37+
while (left < right)
38+
{
39+
threeSum = nums[i] + nums[left] + nums[right];
40+
if (threeSum < 0)
41+
left++;
42+
else if(threeSum > 0)
43+
right--;
44+
else
45+
{
46+
ans.push_back({nums[i], nums[left], nums[right]});
47+
// leftํฌ์ธํ„ฐ ์ค‘๋ณต ์ œ๊ฑฐ
48+
while (left < right && nums[left] == nums[left + 1])
49+
left++;
50+
// right ํฌ์ธํ„ฐ ์ค‘๋ณต ์ œ๊ฑฐ
51+
while (left < right && nums[right] == nums[right - 1])
52+
right--;
53+
left++;
54+
right--;
55+
}
56+
}
57+
}
58+
return ans;
59+
}
60+
};
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
ํ’€์ด :
3+
ํ˜„์žฌ์˜ price์— ๋„๋‹ฌํ•˜๊ธฐ ์ „ ๊ฐ€์žฅ ์ž‘์€ price๋ฅผ min_cur๋กœ ์—…๋ฐ์ดํŠธ
4+
price - min_cur๊ฐ€ ์ €์žฅ๋˜์žˆ๋Š” max_profit๋ณด๋‹ค ํฌ๋ฉด ๊ฐ’์„ ์—…๋ฐ์ดํŠธ
5+
6+
prices์˜ ๊ฐœ์ˆ˜ N
7+
8+
TC : O(N)
9+
10+
SC : O(1)
11+
*/
12+
13+
14+
#include <vector>
15+
using namespace std;
16+
17+
class Solution {
18+
public:
19+
int maxProfit(vector<int>& prices) {
20+
int min_cur = prices[0];
21+
int max_profit = 0;
22+
23+
for (int& price : prices)
24+
{
25+
if (price < min_cur)
26+
{
27+
min_cur = price;
28+
continue ;
29+
}
30+
31+
int profit = price - min_cur;
32+
if (profit > max_profit)
33+
max_profit = profit;
34+
}
35+
return max_profit;
36+
}
37+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""
2+
TC : O(N)
3+
for๋ฌธ ํ•œ ๋ฒˆ => O(N)
4+
5+
SC : O(1)
6+
๋ณ€์ˆ˜ 3๊ฐœ ์„ ์–ธ ์ด์™ธ์— ์ถ”๊ฐ€์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”๋ชจ๋ฆฌ ์—†์œผ๋ฏ€๋กœ
7+
"""
8+
9+
class Solution:
10+
def maxProfit(self, prices: List[int]) -> int:
11+
max_profit = 0
12+
buy = prices[0]
13+
sell = prices[0]
14+
for price in prices:
15+
if price < buy:
16+
buy = price
17+
sell = price
18+
if price > sell:
19+
sell = price
20+
max_profit = max(max_profit, sell - buy)
21+
return max_profit
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
ํ’€์ด :
3+
level์„ ์ธ์ž๋กœ ํ•จ๊ป˜ ๋„˜๊ฒจ์„œ level์— ํ•ด๋‹นํ•˜๋Š” ์ธ๋ฑ์Šค์— append
4+
5+
node ๊ฐœ์ˆ˜ N
6+
7+
TC : O(N)
8+
๋ชจ๋“  node์— ๋Œ€ํ•ด dfsํ˜ธ์ถœ
9+
10+
SC : O(N)
11+
dfs ํ•จ์ˆ˜ ํ˜ธ์ถœ ์Šคํƒ์ด ๋…ธ๋“œ ๊ฐœ์ˆ˜ N๋งŒํผ
12+
"""
13+
14+
# Definition for a binary tree node.
15+
# class TreeNode:
16+
# def __init__(self, val=0, left=None, right=None):
17+
# self.val = val
18+
# self.left = left
19+
# self.right = right
20+
class Solution:
21+
def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
22+
ans = []
23+
24+
def dfs(node: Optional[TreeNode], level: int) -> None:
25+
if not node :
26+
return
27+
if len(ans) < level + 1 :
28+
ans.append([node.val])
29+
else:
30+
ans[level].append(node.val)
31+
dfs(node.left, level + 1)
32+
dfs(node.right, level + 1)
33+
34+
dfs(root, 0)
35+
36+
return ans
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
ํ’€์ด :
3+
n์ด 1๊ณผ 2์ผ ๋–„๋Š” ๋”ฐ๋กœ ์ฒ˜๋ฆฌ, ๊ทธ ์™ธ์— n๋ฒˆ์งธ๋Š” prv(n - 2๋ฒˆ์งธ) + cur(n -1๋ฒˆ์งธ)๋กœ ๊ฐ’์„ ์—…๋ฐ์ดํŠธ ํ•˜๋ฉฐ n๊นŒ์ง€ ๋”ํ•ด๋‚˜๊ฐ
4+
5+
TC : O(N)
6+
n์˜ ํฌ๊ธฐ์— ๋ฐ˜๋ณต๋ฌธ์ด ๋น„๋ก€ํ•œ๋‹ค
7+
8+
SC : O(1)
9+
n์˜ ํฌ๊ธฐ์™€ ์ƒ๊ด€์—†์ด 3๊ฐœ์˜ ๋ณ€์ˆ˜ ์‚ฌ์šฉ
10+
*/
11+
12+
class Solution {
13+
public:
14+
int climbStairs(int n) {
15+
if (n == 1)
16+
return 1;
17+
if (n == 2)
18+
return 2;
19+
20+
int prv = 1;
21+
int cur = 2;
22+
int tmp;
23+
for (int i = 3; i <= n; i++)
24+
{
25+
tmp = cur;
26+
cur = cur + prv;
27+
prv = tmp;
28+
}
29+
return cur;
30+
}
31+
};

โ€Žclone-graph/sungjinwi.pyโ€Ž

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""
2+
ํ’€์ด :
3+
์žฌ๊ท€๋ฅผ ์ด์šฉํ•ด์„œ dfsํ’€์ด
4+
node๋ฅผ ๋ณต์ œํ•˜๊ณ  ๋…ธ๋“œ์˜ ์ด์›ƒ๋œ ๋…ธ๋“œ์— ๋Œ€ํ•ด์„œ ์žฌ๊ท€ํ•จ์ˆ˜ ํ˜ธ์ถœ์„ ํ†ตํ•ด ์™„์„ฑํ•œ๋‹ค
5+
6+
clones ๋”•์…”๋„ˆ๋ฆฌ์— ์ด๋ฏธ ๋ณต์‚ฌ๋œ node๋“ค์„ ์ €์žฅํ•ด์„œ ์ด๋ฏธ ๋ณต์ œ๋œ node์— ๋Œ€ํ•ด
7+
ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ๋ฐ”๋กœ return
8+
9+
๋…ธ๋“œ์˜ ์ˆ˜ : V(์ •์  : Vertex) ์ด์›ƒ์˜ ์ˆ˜ : E(๊ฐ„์„  : Edge)๋ผ๊ณ  ํ•  ๋•Œ
10+
11+
TC : O(V + E)
12+
๋…ธ๋“œ์™€ ์ด์›ƒ์— ๋Œ€ํ•ด์„œ ์ˆœํšŒํ•˜๋ฏ€๋กœ
13+
14+
SC : O(V + E)
15+
ํ•ด์‹œํ…Œ์ด๋ธ”์˜ ํฌ๊ธฐ๊ฐ€ ๋…ธ๋“œ์˜ ์ˆ˜์— ๋น„๋ก€ํ•ด์„œ ์ปค์ง€๊ณ 
16+
dfs์˜ ํ˜ธ์ถœ์Šคํƒ์€ ์ด์›ƒ์˜ ์ˆ˜๋งŒํผ ์Œ“์ด๋ฏ€๋กœ
17+
"""
18+
19+
"""
20+
# Definition for a Node.
21+
class Node:
22+
def __init__(self, val = 0, neighbors = None):
23+
self.val = val
24+
self.neighbors = neighbors if neighbors is not None else []
25+
"""
26+
from typing import Optional
27+
28+
class Solution:
29+
def cloneGraph(self, node: Optional['Node']) -> Optional['Node']:
30+
if not node :
31+
return None
32+
33+
clones = {}
34+
35+
def dfs(node : Optional['Node']) -> Optional['Node']:
36+
if node in clones :
37+
return clones[node]
38+
clone = Node(node.val)
39+
clones[node] = clone
40+
for nei in node.neighbors :
41+
clone.neighbors.append(dfs(nei))
42+
return clone
43+
44+
return dfs(node)

โ€Žcoin-change/sungjinwi.cppโ€Ž

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
ํ’€์ด :
3+
bottom-up dp ํ™œ์šฉ, ๋‚ฎ์€ ์ˆ˜๋ถ€ํ„ฐ amount๊นŒ์ง€ ์ฐจ๋ก€๋กœ ํ•ด๋‹น ๊ธˆ์•ก์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ์ตœ์†Œ ๋™์ „ ๊ฐœ์ˆ˜๋ฅผ ์—…๋ฐ์ดํŠธ.
4+
amount๊ฐ€ 0์ด๋ฉด ๋™์ „ ๊ฐœ์ˆ˜ 0์ด๋ฏ€๋กœ dp[0] = 0์œผ๋กœ ์ดˆ๊ธฐํ™”
5+
๊ทธ ์™ธ์˜ ์ดˆ๊ธฐ๊ฐ’์€ amount + 1๋กœ ์„ค์ •
6+
(1์งœ๋ฆฌ ๋™์ „์œผ๋กœ ์ฑ„์šฐ๋ฉด dp[amount] ์ตœ๋Œ“๊ฐ’ == amount์ด๋ฏ€๋กœ amount + 1 ๊ทธ๋Œ€๋กœ์ด๋ฉด ์ฑ„์šธ ์ˆ˜ ์—†๋Š” ์ผ€์ด์Šค)
7+
8+
coin ์ข…๋ฅ˜ : C, amount ํฌ๊ธฐ : A
9+
10+
TC : O(A * C)
11+
amount์˜ ํฌ๊ธฐ * coin ์ข…๋ฅ˜๋งŒํผ ๋ฐ˜๋ณต๋ฌธ
12+
13+
SC : O(A)
14+
dp๋ฐฐ์—ด์˜ ํฌ๊ธฐ๋Š” amount ํฌ๊ธฐ์— ๋น„๋ก€
15+
*/
16+
17+
class Solution {
18+
public:
19+
int coinChange(vector<int>& coins, int amount) {
20+
vector<int> dp(amount + 1, amount + 1);
21+
22+
dp[0] = 0;
23+
for (int i = 0; i <= amount; i++)
24+
{
25+
for (auto coin : coins)
26+
{
27+
if (i - coin >= 0)
28+
dp[i] = min(dp[i - coin] + 1, dp[i]);
29+
}
30+
}
31+
if (dp[amount] == amount + 1)
32+
return -1;
33+
else
34+
return dp[amount];
35+
}
36+
};
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
ํ’€์ด :
3+
target + 1๊ฐœ์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋Š” ์‚ผ์ค‘๋ฒกํ„ฐ dp๋ฅผ ๋งŒ๋“ ๋‹ค
4+
dp[n] = dp[n - candidate]์˜ ๊ฐ ์กฐํ•ฉ์— candidate๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๋กœ์ง์œผ๋กœ ์Œ“์•„๋‚˜๊ฐˆ ๊ฒƒ์ด๋‹ค
5+
dp[n - c]๊ฐ€ [comb1, comb2]์ผ ๋•Œ dp[n]์€ [comb1.push_back(c), comb2.push_back[2]]
6+
7+
dp[0]์€ ์—ฐ์‚ฐ์„ ์œ„ํ•ด ๋นˆ ์ด์ค‘ ๋ฒกํ„ฐ๋กœ ์ดˆ๊ธฐํ™” ( dp[n] = dp[n - n] = dp[0] --> [[].push_back(n)])
8+
9+
targetํฌ๊ธฐ : T, candidate ๊ฐฏ์ˆ˜ : N
10+
11+
TC : O(T * N)
12+
13+
SC : O(T * N)
14+
*/
15+
16+
#include <vector>
17+
using namespace std;
18+
19+
class Solution {
20+
public:
21+
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
22+
vector<vector<vector<int>>> dp(target + 1);
23+
dp[0] = {{}};
24+
for (int candidate : candidates)
25+
{
26+
for (int num = candidate; num <= target; num++)
27+
{
28+
for (auto& combination : dp[num - candidate])
29+
{
30+
vector<int> new_comb = combination;
31+
new_comb.push_back(candidate);
32+
dp[num].push_back(new_comb);
33+
}
34+
}
35+
}
36+
return dp[target];
37+
}
38+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
ํ’€์ด :
3+
์ตœ๋Œ€ ๋„“์ด๋Š” ์–‘ ๋ ๊ธฐ๋‘ฅ ๊ธธ์ด ์ค‘ ์งง์€ ์ชฝ์„ ๊ธฐ์ค€์œผ๋กœ ์ •ํ•ด์ง„๋‹ค
4+
์–‘ ๋์—์„œ ๊ธฐ๋‘ฅ์„ ์‹œ์ž‘ํ•˜๊ณ  ๋‘˜ ์ค‘ ์งง์€ ์ชฝ์„ ์•ˆ์ชฝ์œผ๋กœ ์ด๋™ํ•˜๋ฉด์„œ ์ตœ๋Œ€ ๋„“์ด๋ฅผ ์ฐพ๋Š”๋‹ค
5+
6+
height์˜ ๊ฐœ์ˆ˜ : N
7+
8+
TC : O(N)
9+
10+
SC : O(1)
11+
*/
12+
13+
class Solution {
14+
public:
15+
int maxArea(vector<int>& height) {
16+
int left = 0;
17+
int right = height.size() - 1;
18+
int max_area = 0;
19+
20+
while (left < right)
21+
{
22+
int cur_area = (right - left) * min(height[left], height[right]);
23+
if (cur_area > max_area)
24+
max_area = cur_area;
25+
if (height[left] <= height[right])
26+
left++;
27+
else
28+
right--;
29+
}
30+
return max_area;
31+
}
32+
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""
2+
ํ’€์ด :
3+
left, right ์ปค์„œ๋ฅผ ์–‘ ๋์— ๋†“๊ณ  ์กฐ๊ฑด์— ๋”ฐ๋ผ ๋ฐ˜๋Œ€ํŽธ์œผ๋กœ ์ด๋™์‹œํ‚ค๋ฉฐ ๋ฌผ ์–‘ ๋น„๊ต
4+
๋‘˜ ์ค‘ ๋” ๋†’์€ ๋†’์ด๋ฅผ ์ด๋™์‹œํ‚ค๋ฉด ๋ฌด์กฐ๊ฑด ๋ฌผ์˜ ์–‘์ด ์ค„์–ด๋“ค๊ธฐ ๋•Œ๋ฌธ์— ๋” ๋‚ฎ๊ฑฐ๋‚˜ ๊ฐ™์€ ์ปค์„œ๋ฅผ ์ด๋™์‹œํ‚ค๋ฉฐ ์—…๋ฐ์ดํŠธ
5+
๋‘˜์ด ๋งŒ๋‚˜๋ฉด ๋น„๊ต ์ข…๋ฃŒ
6+
7+
8+
len(height) : N
9+
TC : O(N)
10+
l, r์˜ ์ด๋™์ด ์ „์ฒด height ๊ฐœ์ˆ˜๋งŒํผ ์ผ์–ด๋‚˜๋ฏ€๋กœ
11+
SC : O(1)
12+
"""
13+
14+
class Solution:
15+
def maxArea(self, height: List[int]) -> int:
16+
l = 0
17+
r = len(height) - 1
18+
max_area = 0
19+
while (l != r) :
20+
cur_area = (r - l) * min(height[l], height[r])
21+
max_area = max(cur_area, max_area)
22+
if (height[l] >= height[r]) :
23+
r -= 1
24+
else :
25+
l += 1
26+
return max_area

0 commit comments

Comments
ย (0)