Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions .github/workflows/integration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,14 @@ jobs:

echo "## 파일명 규칙 위반" >> $GITHUB_STEP_SUMMARY
for file in $files; do
if [ -f "$file" ]; then
# 파일명만 추출 (경로 제외)
filename=$(basename "$file")

# 파일명만 추출 (경로 제외)
filename=$(basename "$file")

# 파일명이 GitHub계정명인지 확인
shopt -s nocasematch
if [[ ! "$filename" = "$pr_author"* ]]; then
echo "- $file" >> $GITHUB_STEP_SUMMARY
success=false
fi
# 파일명이 GitHub계정명인지 확인
shopt -s nocasematch
if [[ ! "$filename" = "$pr_author"* ]]; then
echo "- $file" >> $GITHUB_STEP_SUMMARY
success=false
fi
done

Expand Down
38 changes: 38 additions & 0 deletions contains-duplicate/JisooPyo.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package leetcode_study

import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.Test

/**
* Leetcode
* 217. Contains Duplicate
* Easy
*/
class ContainsDuplicate {
/**
* Runtime: 17 ms(Beats: 80.99 %)
* Time Complexity: O(n)
* - 배열 순회
*
* Memory: 50.63 MB(Beats: 70.32 %)
* Space Complexity: O(n)
* - HashSet에 최악의 경우 배열 원소 모두 저장
*/
fun containsDuplicate(nums: IntArray): Boolean {
val set = hashSetOf<Int>()
for (i in nums) {
if (set.contains(i)) {
return true
}
set.add(i)
}
return false
}

@Test
fun test() {
containsDuplicate(intArrayOf(1, 2, 3, 1)) shouldBe true
containsDuplicate(intArrayOf(1, 2, 3, 4)) shouldBe false
containsDuplicate(intArrayOf(1, 1, 1, 3, 3, 4, 3, 2, 4, 2)) shouldBe true
}
}
23 changes: 23 additions & 0 deletions contains-duplicate/dusunax.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'''
# Leetcode 217. Contains Duplicate

use set to store distinct elements 🗂️

## Time and Space Complexity

```
TC: O(n)
SC: O(n)
```

### TC is O(n):
- iterating through the list just once to convert it to a set.

### SC is O(n):
- creating a set to store the distinct elements of the list.
'''

class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
return len(nums) != len(set(nums))

4 changes: 4 additions & 0 deletions contains-duplicate/jeldo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Solution:
# O(n)
def containsDuplicate(self, nums: list[int]) -> bool:
return len(nums) != len(set(nums)) # O(n)
74 changes: 74 additions & 0 deletions house-robber/JisooPyo.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package leetcode_study

import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.Test
import kotlin.math.max

/**
* Leetcode
* 198. House Robber
* Medium
*
* 사용된 알고리즘: Dynamic Programming
*
* i번째 집에서 얻을 수 있는 최대 금액은 다음 두 가지 중 큰 값입니다.
* - (i-2)번째 집까지의 최대 금액 + 현재 집의 금액
* - (i-1)번째 집까지의 최대 금액
*/
class HouseRobber {
/**
* Runtime: 0 ms(Beats: 100.00 %)
* Time Complexity: O(n)
*
* Memory: 34.65 MB(Beats: 40.50 %)
* Space Complexity: O(n)
*/
fun rob(nums: IntArray): Int {
if (nums.size == 1) {
return nums[0]
}
if (nums.size == 2) {
return max(nums[0], nums[1])
}
val dp = IntArray(nums.size)
dp[0] = nums[0]
dp[1] = max(nums[0], nums[1])
for (i in 2 until nums.size) {
dp[i] = max(dp[i - 2] + nums[i], dp[i - 1])
}
return dp[nums.size - 1]
}

/**
* 공간 복잡도를 개선
* Runtime: 0 ms(Beats: 100.00 %)
* Time Complexity: O(n)
*
* Memory: 34.95 MB(Beats: 36.98 %)
* Space Complexity: O(1)
*/
fun rob2(nums: IntArray): Int {
if (nums.size == 1) return nums[0]
if (nums.size == 2) return max(nums[0], nums[1])

var twoBack = nums[0]
var oneBack = max(nums[0], nums[1])
var current = oneBack

for (i in 2 until nums.size) {
current = max(twoBack + nums[i], oneBack)
twoBack = oneBack
oneBack = current
}

return current
}

@Test
fun test() {
rob(intArrayOf(1, 2, 3, 1)) shouldBe 4
rob(intArrayOf(2, 7, 9, 3, 1)) shouldBe 12
rob2(intArrayOf(1, 2, 3, 1)) shouldBe 4
rob2(intArrayOf(2, 7, 9, 3, 1)) shouldBe 12
}
}
44 changes: 44 additions & 0 deletions house-robber/dusunax.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'''
# Leetcode 198. House Robber

use **dynamic programming** to solve this problem. (bottom-up approach) 🧩

choose bottom-up approach for less space complexity.

## DP relation

```
dp[i] = max(dp[i - 1], dp[i - 2] + nums[i])
```

- **dp[i - 1]:** skip and take the value from the previous house
- **dp[i - 2]:** rob the current house, add its value to the maximum money from two houses before

## Time and Space Complexity

```
TC: O(n)
SC: O(n)
```

### TC is O(n):
- iterating through the list just once to calculate the maximum money. = O(n)

### SC is O(n):
- using a list to store the maximum money at each house. = O(n)

'''

class Solution:
def rob(self, nums: List[int]) -> int:
if len(nums) == 1:
return nums[0]

dp = [0] * len(nums)
dp[0] = nums[0]
dp[1] = max(nums[0], nums[1])

for i in range(2, len(nums)):
dp[i] = max(dp[i - 1], dp[i - 2] + nums[i])

return dp[-1]
9 changes: 9 additions & 0 deletions house-robber/jeldo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Solution:
# O(n)
def rob(self, nums: list[int]) -> int:
if len(nums) <= 2:
return max(nums)
nums[2] += nums[0]
for i in range(3, len(nums)):
nums[i] += max(nums[i-3], nums[i-2])
return max(nums[-1], nums[-2])
81 changes: 81 additions & 0 deletions longest-consecutive-sequence/JisooPyo.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package leetcode_study

import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.Test
import kotlin.math.max

/**
* Leetcode
* 128. Longest Consecutive Sequence
* Medium
*/
class LongestConsecutiveSequence {
/**
* Runtime: 58 ms(Beats: 79.06 %)
* Time Complexity: O(n)
* - while 루프의 총 반복 횟수는 n을 넘을 수 없다.
*
* Memory: 62.65 MB(Beats: 10.48 %)
* Space Complexity: O(n)
*/
fun longestConsecutive(nums: IntArray): Int {
val numsSet: MutableSet<Int> = nums.toHashSet()
val startSet: MutableSet<Int> = hashSetOf()

// 수열의 시작점이 될 수 있는 수를 찾는다.
for (num in numsSet) {
if (!numsSet.contains(num - 1)) {
startSet.add(num)
}
}
var answer = 0
for (start in startSet) {
// 수열의 시작점부터 몇 개 까지 numsSet에 있는지 확인한다.
var count = 0
var first = start
while (numsSet.contains(first)) {
first++
count++
}
// 최대 수열의 개수를 업데이트한다.
answer = max(answer, count)
}
return answer
}

/**
* 위 풀이에서 startSet을 제거하여 공간적으로 효율적인 풀이
* Runtime: 63 ms(Beats: 65.70 %)
* Time Complexity: O(n)
*
* Memory: 58.47 MB(Beats: 70.81 %)
* Space Complexity: O(n)
*/
fun longestConsecutive2(nums: IntArray): Int {
val numsSet = nums.toHashSet()
var maxLength = 0

for (num in numsSet) {
if (!numsSet.contains(num - 1)) {
var currentNum = num
var currentLength = 0

while (numsSet.contains(currentNum)) {
currentLength++
currentNum++
}
maxLength = max(maxLength, currentLength)
}
}
return maxLength
}

@Test
fun test() {
longestConsecutive(intArrayOf(100, 4, 200, 1, 3, 2)) shouldBe 4
longestConsecutive(intArrayOf(0, 3, 7, 2, 5, 8, 4, 6, 0, 1)) shouldBe 9

longestConsecutive2(intArrayOf(100, 4, 200, 1, 3, 2)) shouldBe 4
longestConsecutive2(intArrayOf(0, 3, 7, 2, 5, 8, 4, 6, 0, 1)) shouldBe 9
}
}
38 changes: 38 additions & 0 deletions longest-consecutive-sequence/dusunax.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'''
# Leetcode 128. Longest Consecutive Sequence

keep time complexity O(n) by iterating through the set and accessing elements in O(1) time. ⚖️

## Time and Space Complexity
```
TC: O(n)
SC: O(n)
```

### TC is O(n):
- iterating through the set. O(n)
- accessing elements in the set. O(1)
- while loop incrementing `current_num` while `current_num + 1 in nums_set`. O(1)

### SC is O(n):
- creating a set from the list of numbers. O(n)
'''

class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
nums_set = set(nums) # O(n)
longest_sequence = 0

for num in nums_set: # O(n)
if (num - 1) not in nums_set:
current_num = num
current_sequence = 1

while current_num + 1 in nums_set: # O(1)
current_num += 1
current_sequence += 1

longest_sequence = max(current_sequence, longest_sequence)

return longest_sequence

13 changes: 13 additions & 0 deletions longest-consecutive-sequence/jeldo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution:
# O(n)
def longestConsecutive(self, nums: list[int]) -> int:
max_length = 0
nums_set = set(nums)
for n in nums_set:
if n - 1 not in nums_set:
length = 0
while n + length in nums_set:
length += 1
max_length = max(max_length, length)

return max_length
Loading
Loading