Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions contains-duplicate/hyunjung-choi.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Solution {
fun containsDuplicate(nums: IntArray): Boolean {
return nums.size != nums.toSet().size
}
}
18 changes: 18 additions & 0 deletions house-robber/hyunjung-choi.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package leetcode_study

class Solution {
fun rob(nums: IntArray): Int {
if (nums.size == 1) return nums[0]

var prev2 = nums[0]
var prev1 = maxOf(nums[0], nums[1])

for (i in 2 until nums.size) {
val current = maxOf(prev1, prev2 + nums[i])
prev2 = prev1
prev1 = current
}

return prev1
}
}
24 changes: 24 additions & 0 deletions longest-consecutive-sequence/hyunjung-choi.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package leetcode_study

class Solution {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 풀이 지금은 O(nlogn)으로 보이는데 O(n)으로 최적화할 수 있는 문제라고 들었던 것 같습니다! 한번 개선해보시면 좋을 것 같아요

그리고 풀이 가이드에 자신이 생각하는 자신의 풀이 방법과 시간, 공간 복잡도를 적어두는 지침이 있었던 것 같습니다!

fun longestConsecutive(nums: IntArray): Int {
if (nums.isEmpty()) return 0

val sortedNums = nums.sorted()
var maxLength = 1
var currentLength = 1

for (i in 1 until sortedNums.size) {
when {
sortedNums[i] == sortedNums[i - 1] -> continue
sortedNums[i] == sortedNums[i - 1] + 1 -> {
currentLength++
maxLength = maxOf(maxLength, currentLength)
}
else -> currentLength = 1
}
}

return maxLength
}
}
12 changes: 12 additions & 0 deletions top-k-frequent-elements/hyunjung-choi.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution {
fun topKFrequent(nums: IntArray, k: Int): IntArray {
val frequency = nums.toList().groupingBy { it }.eachCount()

return frequency
.toList()
.sortedByDescending { it.second }
.take(k)
.map { it.first }
.toIntArray()
}
}
16 changes: 16 additions & 0 deletions two-sum/hyunjung-choi.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 풀이도 해쉬맵을 이용하면 O(n)까지 최적화할 수 있습니다!

fun twoSum(nums: IntArray, target: Int): IntArray {
val result = IntArray(2)

for (i in nums.indices) {
for (j in i + 1 until nums.size) {
if (nums[i] + nums[j] == target) {
result[0] = i
result[1] = j
}
}
}

return result
}
}