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

0 commit comments

Comments
Β (0)