Skip to content

Commit 06bd19c

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents 9d14ded + d975b97 commit 06bd19c

File tree

21 files changed

+722
-1
lines changed

21 files changed

+722
-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

contains-duplicate/5YoonCheol.java

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+
}

contains-duplicate/JisooPyo.kt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package leetcode_study
2+
3+
import io.kotest.matchers.shouldBe
4+
import org.junit.jupiter.api.Test
5+
6+
/**
7+
* Leetcode
8+
* 217. Contains Duplicate
9+
* Easy
10+
*/
11+
class ContainsDuplicate {
12+
/**
13+
* Runtime: 17 ms(Beats: 80.99 %)
14+
* Time Complexity: O(n)
15+
* - 배열 순회
16+
*
17+
* Memory: 50.63 MB(Beats: 70.32 %)
18+
* Space Complexity: O(n)
19+
* - HashSet에 최악의 경우 배열 원소 모두 저장
20+
*/
21+
fun containsDuplicate(nums: IntArray): Boolean {
22+
val set = hashSetOf<Int>()
23+
for (i in nums) {
24+
if (set.contains(i)) {
25+
return true
26+
}
27+
set.add(i)
28+
}
29+
return false
30+
}
31+
32+
@Test
33+
fun test() {
34+
containsDuplicate(intArrayOf(1, 2, 3, 1)) shouldBe true
35+
containsDuplicate(intArrayOf(1, 2, 3, 4)) shouldBe false
36+
containsDuplicate(intArrayOf(1, 1, 1, 3, 3, 4, 3, 2, 4, 2)) shouldBe true
37+
}
38+
}

contains-duplicate/dusunax.py

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+

contains-duplicate/jeldo.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Solution:
2+
# O(n)
3+
def containsDuplicate(self, nums: list[int]) -> bool:
4+
return len(nums) != len(set(nums)) # O(n)

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/JisooPyo.kt

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package leetcode_study
2+
3+
import io.kotest.matchers.shouldBe
4+
import org.junit.jupiter.api.Test
5+
import kotlin.math.max
6+
7+
/**
8+
* Leetcode
9+
* 198. House Robber
10+
* Medium
11+
*
12+
* 사용된 알고리즘: Dynamic Programming
13+
*
14+
* i번째 집에서 얻을 수 있는 최대 금액은 다음 두 가지 중 큰 값입니다.
15+
* - (i-2)번째 집까지의 최대 금액 + 현재 집의 금액
16+
* - (i-1)번째 집까지의 최대 금액
17+
*/
18+
class HouseRobber {
19+
/**
20+
* Runtime: 0 ms(Beats: 100.00 %)
21+
* Time Complexity: O(n)
22+
*
23+
* Memory: 34.65 MB(Beats: 40.50 %)
24+
* Space Complexity: O(n)
25+
*/
26+
fun rob(nums: IntArray): Int {
27+
if (nums.size == 1) {
28+
return nums[0]
29+
}
30+
if (nums.size == 2) {
31+
return max(nums[0], nums[1])
32+
}
33+
val dp = IntArray(nums.size)
34+
dp[0] = nums[0]
35+
dp[1] = max(nums[0], nums[1])
36+
for (i in 2 until nums.size) {
37+
dp[i] = max(dp[i - 2] + nums[i], dp[i - 1])
38+
}
39+
return dp[nums.size - 1]
40+
}
41+
42+
/**
43+
* 공간 복잡도를 개선
44+
* Runtime: 0 ms(Beats: 100.00 %)
45+
* Time Complexity: O(n)
46+
*
47+
* Memory: 34.95 MB(Beats: 36.98 %)
48+
* Space Complexity: O(1)
49+
*/
50+
fun rob2(nums: IntArray): Int {
51+
if (nums.size == 1) return nums[0]
52+
if (nums.size == 2) return max(nums[0], nums[1])
53+
54+
var twoBack = nums[0]
55+
var oneBack = max(nums[0], nums[1])
56+
var current = oneBack
57+
58+
for (i in 2 until nums.size) {
59+
current = max(twoBack + nums[i], oneBack)
60+
twoBack = oneBack
61+
oneBack = current
62+
}
63+
64+
return current
65+
}
66+
67+
@Test
68+
fun test() {
69+
rob(intArrayOf(1, 2, 3, 1)) shouldBe 4
70+
rob(intArrayOf(2, 7, 9, 3, 1)) shouldBe 12
71+
rob2(intArrayOf(1, 2, 3, 1)) shouldBe 4
72+
rob2(intArrayOf(2, 7, 9, 3, 1)) shouldBe 12
73+
}
74+
}

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/jeldo.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
# O(n)
3+
def rob(self, nums: list[int]) -> int:
4+
if len(nums) <= 2:
5+
return max(nums)
6+
nums[2] += nums[0]
7+
for i in range(3, len(nums)):
8+
nums[i] += max(nums[i-3], nums[i-2])
9+
return max(nums[-1], nums[-2])
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+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package leetcode_study
2+
3+
import io.kotest.matchers.shouldBe
4+
import org.junit.jupiter.api.Test
5+
import kotlin.math.max
6+
7+
/**
8+
* Leetcode
9+
* 128. Longest Consecutive Sequence
10+
* Medium
11+
*/
12+
class LongestConsecutiveSequence {
13+
/**
14+
* Runtime: 58 ms(Beats: 79.06 %)
15+
* Time Complexity: O(n)
16+
* - while 루프의 총 반복 횟수는 n을 넘을 수 없다.
17+
*
18+
* Memory: 62.65 MB(Beats: 10.48 %)
19+
* Space Complexity: O(n)
20+
*/
21+
fun longestConsecutive(nums: IntArray): Int {
22+
val numsSet: MutableSet<Int> = nums.toHashSet()
23+
val startSet: MutableSet<Int> = hashSetOf()
24+
25+
// 수열의 시작점이 될 수 있는 수를 찾는다.
26+
for (num in numsSet) {
27+
if (!numsSet.contains(num - 1)) {
28+
startSet.add(num)
29+
}
30+
}
31+
var answer = 0
32+
for (start in startSet) {
33+
// 수열의 시작점부터 몇 개 까지 numsSet에 있는지 확인한다.
34+
var count = 0
35+
var first = start
36+
while (numsSet.contains(first)) {
37+
first++
38+
count++
39+
}
40+
// 최대 수열의 개수를 업데이트한다.
41+
answer = max(answer, count)
42+
}
43+
return answer
44+
}
45+
46+
/**
47+
* 위 풀이에서 startSet을 제거하여 공간적으로 효율적인 풀이
48+
* Runtime: 63 ms(Beats: 65.70 %)
49+
* Time Complexity: O(n)
50+
*
51+
* Memory: 58.47 MB(Beats: 70.81 %)
52+
* Space Complexity: O(n)
53+
*/
54+
fun longestConsecutive2(nums: IntArray): Int {
55+
val numsSet = nums.toHashSet()
56+
var maxLength = 0
57+
58+
for (num in numsSet) {
59+
if (!numsSet.contains(num - 1)) {
60+
var currentNum = num
61+
var currentLength = 0
62+
63+
while (numsSet.contains(currentNum)) {
64+
currentLength++
65+
currentNum++
66+
}
67+
maxLength = max(maxLength, currentLength)
68+
}
69+
}
70+
return maxLength
71+
}
72+
73+
@Test
74+
fun test() {
75+
longestConsecutive(intArrayOf(100, 4, 200, 1, 3, 2)) shouldBe 4
76+
longestConsecutive(intArrayOf(0, 3, 7, 2, 5, 8, 4, 6, 0, 1)) shouldBe 9
77+
78+
longestConsecutive2(intArrayOf(100, 4, 200, 1, 3, 2)) shouldBe 4
79+
longestConsecutive2(intArrayOf(0, 3, 7, 2, 5, 8, 4, 6, 0, 1)) shouldBe 9
80+
}
81+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'''
2+
# Leetcode 128. Longest Consecutive Sequence
3+
4+
keep time complexity O(n) by iterating through the set and accessing elements in O(1) time. ⚖️
5+
6+
## Time and Space Complexity
7+
```
8+
TC: O(n)
9+
SC: O(n)
10+
```
11+
12+
### TC is O(n):
13+
- iterating through the set. O(n)
14+
- accessing elements in the set. O(1)
15+
- while loop incrementing `current_num` while `current_num + 1 in nums_set`. O(1)
16+
17+
### SC is O(n):
18+
- creating a set from the list of numbers. O(n)
19+
'''
20+
21+
class Solution:
22+
def longestConsecutive(self, nums: List[int]) -> int:
23+
nums_set = set(nums) # O(n)
24+
longest_sequence = 0
25+
26+
for num in nums_set: # O(n)
27+
if (num - 1) not in nums_set:
28+
current_num = num
29+
current_sequence = 1
30+
31+
while current_num + 1 in nums_set: # O(1)
32+
current_num += 1
33+
current_sequence += 1
34+
35+
longest_sequence = max(current_sequence, longest_sequence)
36+
37+
return longest_sequence
38+

0 commit comments

Comments
 (0)