diff --git a/contains-duplicate/hyunjung-choi.kt b/contains-duplicate/hyunjung-choi.kt new file mode 100644 index 0000000000..1bce83e1ea --- /dev/null +++ b/contains-duplicate/hyunjung-choi.kt @@ -0,0 +1,5 @@ +class Solution { + fun containsDuplicate(nums: IntArray): Boolean { + return nums.size != nums.toSet().size + } +} diff --git a/house-robber/hyunjung-choi.kt b/house-robber/hyunjung-choi.kt new file mode 100644 index 0000000000..b75c8ef0a1 --- /dev/null +++ b/house-robber/hyunjung-choi.kt @@ -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 + } +} diff --git a/longest-consecutive-sequence/hyunjung-choi.kt b/longest-consecutive-sequence/hyunjung-choi.kt new file mode 100644 index 0000000000..a13ceea786 --- /dev/null +++ b/longest-consecutive-sequence/hyunjung-choi.kt @@ -0,0 +1,24 @@ +package leetcode_study + +class Solution { + 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 + } +} diff --git a/top-k-frequent-elements/hyunjung-choi.kt b/top-k-frequent-elements/hyunjung-choi.kt new file mode 100644 index 0000000000..f79f6e8856 --- /dev/null +++ b/top-k-frequent-elements/hyunjung-choi.kt @@ -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() + } +} diff --git a/two-sum/hyunjung-choi.kt b/two-sum/hyunjung-choi.kt new file mode 100644 index 0000000000..856ce5dec2 --- /dev/null +++ b/two-sum/hyunjung-choi.kt @@ -0,0 +1,16 @@ +class Solution { + 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 + } +}