Skip to content

Commit 3f8adfa

Browse files
Merge branch 'DaleStudy:main' into main
2 parents 4fbabfc + aaecd51 commit 3f8adfa

File tree

19 files changed

+494
-1
lines changed

19 files changed

+494
-1
lines changed

β€Ž.github/workflows/integration.yamlβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ jobs:
8585
- name: Check filename rules
8686
if: ${{ steps.pr-labels.outputs.has_maintenance != 'true' }}
8787
run: |
88-
files=$(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.sha }} | tr -d '"')
88+
files=$(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }} | tr -d '"')
8989
pr_author="${{ github.event.pull_request.user.login }}"
9090
success=true
9191
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
/**
3+
* μ‹œκ°„ λ³΅μž‘λ„: O(N)
4+
* 곡간 λ³΅μž‘λ„: O(N)
5+
*/
6+
public boolean containsDuplicate(int[] nums) {
7+
Set<Integer> set = new HashSet<>();
8+
9+
for (int num : nums) {
10+
if (set.contains(num)) return true;
11+
set.add(num);
12+
}
13+
14+
return false;
15+
}
16+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import java.util.HashSet;
2+
import java.util.Set;
3+
4+
class SolutionGotprgmer {
5+
// ν•΄λ‹Ή λ¬Έμ œλŠ” μ–΄λŠ ν•œ μˆ«μžκ°€ 2κ°œμ΄μƒ μ‘΄μž¬ν•  경우 trueλ₯Ό κ·Έλ ‡μ§€ μ•Šμ„ 경우, falseλ₯Ό λ°˜ν™˜ν•˜λŠ” λ¬Έμ œμ΄λ‹€.
6+
// set을 μ‚¬μš©ν•΄μ„œ set에 이미 값이 μ‘΄μž¬ν•œλ‹€λ©΄ κ°œμˆ˜κ°€ 2 μ΄μƒμ΄λ―€λ‘œ true κ·Έλ ‡μ§€ μ•ŠμœΌλ©΄ falseλ₯Ό 좜λ ₯ν•œλ‹€.
7+
8+
// 각 μˆ«μžλ“€μ„ μ €μž₯ν•΄μ„œ set으둜 관리 -> distinctNums
9+
// nums의 각 숫자인 checkNum을 distinctNums에 λ„£μ–΄μ€€λ‹€.
10+
// λ§Œμ•½ checkNum이 이미 distinctNums에 μ‘΄μž¬ν•œλ‹€λ©΄ ansλ₯Ό true둜 λ§Œλ“€μ–΄μ£Όκ³  닡을 좜λ ₯ν•œλ‹€.
11+
12+
13+
// μ‹œκ°„λ³΅μž‘λ„ -> O(n)
14+
// κ³΅κ°„λ³΅μž‘λ„ -> O(n)
15+
static Set<Integer> distinctNums;
16+
public boolean containsDuplicate(int[] nums) {
17+
distinctNums = new HashSet<>();
18+
boolean ans = false;
19+
for (int checkNum : nums) {
20+
if (distinctNums.contains(checkNum)) {
21+
ans = true;
22+
break;
23+
};
24+
distinctNums.add(checkNum);
25+
}
26+
return ans;
27+
}
28+
29+
30+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'''
2+
# Leetcode 217. Contains Duplicate
3+
4+
use set to store distinct elements πŸ—‚οΈ
5+
6+
## Time and Space Complexity
7+
8+
```
9+
TC: O(n)
10+
SC: O(n)
11+
```
12+
13+
### TC is O(n):
14+
- iterating through the list just once to convert it to a set.
15+
16+
### SC is O(n):
17+
- creating a set to store the distinct elements of the list.
18+
'''
19+
20+
class Solution:
21+
def containsDuplicate(self, nums: List[int]) -> bool:
22+
return len(nums) != len(set(nums))
23+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""
2+
# Time Complexity: O(N)
3+
- N번 순회
4+
# Space Compelexity: O(N)
5+
- μ΅œμ•…μ˜ 경우 (μ€‘λ³΅λœ 값이 없을 경우) N개 μ €μž₯
6+
"""
7+
class Solution:
8+
def containsDuplicate(self, nums: List[int]) -> bool:
9+
num_dict = {}
10+
for num in nums:
11+
if num not in num_dict:
12+
num_dict[num] = True
13+
else:
14+
return True
15+
return False

β€Žhouse-robber/5YoonCheol.javaβ€Ž

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public int rob(int[] nums) {
3+
//λ°°μ—΄ 길이 0이면 ν„Έ 수 μžˆλŠ” 집이 μ—†μŒ.
4+
if (nums.length == 0) return 0;
5+
//λ°°μ—΄ 길이가 1이면 ν•œ μ§‘λ§Œ ν„Έ 수 있음.
6+
if (nums.length == 1) return nums[0];
7+
8+
//동적 κ³„νšλ²•μœΌλ‘œ 풀이
9+
int[] dp = new int[nums.length];
10+
dp[0] = nums[0];
11+
dp[1] = Math.max(nums[0], nums[1]);
12+
13+
//λ°°μ—΄ 크기가 2이상일 경우 μ΅œλŒ€ κΈˆμ•‘μ˜ λ²”μœ„ ν™•μž₯
14+
for (int i = 2; i < nums.length; i++) {
15+
dp[i] = Math.max(dp[i - 2] + nums[i], dp[i - 1]);
16+
}
17+
return dp[nums.length - 1];
18+
}
19+
}

β€Žhouse-robber/dusunax.pyβ€Ž

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'''
2+
# Leetcode 198. House Robber
3+
4+
use **dynamic programming** to solve this problem. (bottom-up approach) 🧩
5+
6+
choose bottom-up approach for less space complexity.
7+
8+
## DP relation
9+
10+
```
11+
dp[i] = max(dp[i - 1], dp[i - 2] + nums[i])
12+
```
13+
14+
- **dp[i - 1]:** skip and take the value from the previous house
15+
- **dp[i - 2]:** rob the current house, add its value to the maximum money from two houses before
16+
17+
## Time and Space Complexity
18+
19+
```
20+
TC: O(n)
21+
SC: O(n)
22+
```
23+
24+
### TC is O(n):
25+
- iterating through the list just once to calculate the maximum money. = O(n)
26+
27+
### SC is O(n):
28+
- using a list to store the maximum money at each house. = O(n)
29+
30+
'''
31+
32+
class Solution:
33+
def rob(self, nums: List[int]) -> int:
34+
if len(nums) == 1:
35+
return nums[0]
36+
37+
dp = [0] * len(nums)
38+
dp[0] = nums[0]
39+
dp[1] = max(nums[0], nums[1])
40+
41+
for i in range(2, len(nums)):
42+
dp[i] = max(dp[i - 1], dp[i - 2] + nums[i])
43+
44+
return dp[-1]

β€Žhouse-robber/thispath98.pyβ€Ž

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
# Time Complexity: O(N)
3+
- N개의 개수λ₯Ό κ°€μ§€λŠ” dp 리슀트λ₯Ό λ§Œλ“€κ³ , 이λ₯Ό 순회
4+
# Space Compelexity: O(N)
5+
- N개의 dp 리슀트 μ €μž₯
6+
"""
7+
class Solution:
8+
def rob(self, nums: List[int]) -> int:
9+
if len(nums) == 1:
10+
return nums[0]
11+
12+
dp = [0 for _ in range(len(nums))]
13+
dp[0] = nums[0]
14+
dp[1] = max(nums[0], nums[1])
15+
16+
for i in range(len(nums) - 2):
17+
dp[i + 2] = max(dp[i] + nums[i + 2], dp[i + 1])
18+
19+
return max(dp[-2], dp[-1])
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
# Time Complexity: O(N)
3+
- lce_dict 생성: N
4+
- N개의 key에 λŒ€ν•˜μ—¬ μˆœνšŒν•˜λ©΄μ„œ κ°’ 확인: N
5+
# Space Compelexity: O(k)
6+
- μ€‘λ³΅λ˜μ§€ μ•Šμ€ key k개 μ €μž₯
7+
"""
8+
class Solution:
9+
def longestConsecutive(self, nums: List[int]) -> int:
10+
if not nums:
11+
return 0
12+
13+
lce_dict = {}
14+
for num in nums:
15+
lce_dict[num] = True
16+
17+
answer = 0
18+
for num in nums:
19+
cur_lce = 1
20+
if lce_dict.pop(num, None) is None:
21+
continue
22+
23+
down_num = num - 1
24+
while down_num in lce_dict:
25+
cur_lce += 1
26+
lce_dict.pop(down_num)
27+
down_num -= 1
28+
29+
up_num = num + 1
30+
while up_num in lce_dict:
31+
cur_lce += 1
32+
lce_dict.pop(up_num)
33+
up_num += 1
34+
35+
answer = answer if answer > cur_lce else cur_lce
36+
37+
return answer
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
public int longestConsecutive(int[] nums) {
5+
if (nums == null || nums.length == 0) return 0;
6+
7+
//λͺ¨λ“  μš”μ†Œ HashSet μ‚½μž…
8+
HashSet<Integer> set = new HashSet<>();
9+
for (int num : nums) {
10+
set.add(num);
11+
}
12+
13+
int longest = 0;
14+
15+
// μ‹œμž‘μ§€μ  체크
16+
for (int num : nums) {
17+
//λ°°μ—΄ μš”μ†Œλ³΄λ‹€ 1 μž‘μ€ 수 κ°€ μ—†λŠ” 경우 μƒˆλ‘œμš΄ μ‹œμž‘ 지점이 됨
18+
if (!set.contains(num - 1)) {
19+
int start = num;
20+
int currentLength = 1;
21+
22+
// 1μ”© μ¦κ°€μ‹œν‚€λ©΄μ„œ μ—°μ†λœ 수의 개수 탐색
23+
while (set.contains(start + 1)) {
24+
start++;
25+
currentLength++;
26+
}
27+
// κΈ°μ‘΄ longest와 ν˜„μž¬ μ—°μ†λœ 수λ₯Ό 비ꡐ
28+
longest = Math.max(longest, currentLength);
29+
}
30+
}
31+
32+
return longest;
33+
}
34+
}

0 commit comments

Comments
Β (0)