Skip to content

Commit 7011519

Browse files
Merge pull request #1684 from hyunjung-choi/main
[hyunjung-choi] WEEK 01 solutions
2 parents 512e1f1 + f43803c commit 7011519

File tree

5 files changed

+75
-0
lines changed

5 files changed

+75
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution {
2+
fun containsDuplicate(nums: IntArray): Boolean {
3+
return nums.size != nums.toSet().size
4+
}
5+
}

house-robber/hyunjung-choi.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package leetcode_study
2+
3+
class Solution {
4+
fun rob(nums: IntArray): Int {
5+
if (nums.size == 1) return nums[0]
6+
7+
var prev2 = nums[0]
8+
var prev1 = maxOf(nums[0], nums[1])
9+
10+
for (i in 2 until nums.size) {
11+
val current = maxOf(prev1, prev2 + nums[i])
12+
prev2 = prev1
13+
prev1 = current
14+
}
15+
16+
return prev1
17+
}
18+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package leetcode_study
2+
3+
class Solution {
4+
fun longestConsecutive(nums: IntArray): Int {
5+
if (nums.isEmpty()) return 0
6+
7+
val sortedNums = nums.sorted()
8+
var maxLength = 1
9+
var currentLength = 1
10+
11+
for (i in 1 until sortedNums.size) {
12+
when {
13+
sortedNums[i] == sortedNums[i - 1] -> continue
14+
sortedNums[i] == sortedNums[i - 1] + 1 -> {
15+
currentLength++
16+
maxLength = maxOf(maxLength, currentLength)
17+
}
18+
else -> currentLength = 1
19+
}
20+
}
21+
22+
return maxLength
23+
}
24+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
fun topKFrequent(nums: IntArray, k: Int): IntArray {
3+
val frequency = nums.toList().groupingBy { it }.eachCount()
4+
5+
return frequency
6+
.toList()
7+
.sortedByDescending { it.second }
8+
.take(k)
9+
.map { it.first }
10+
.toIntArray()
11+
}
12+
}

two-sum/hyunjung-choi.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
fun twoSum(nums: IntArray, target: Int): IntArray {
3+
val result = IntArray(2)
4+
5+
for (i in nums.indices) {
6+
for (j in i + 1 until nums.size) {
7+
if (nums[i] + nums[j] == target) {
8+
result[0] = i
9+
result[1] = j
10+
}
11+
}
12+
}
13+
14+
return result
15+
}
16+
}

0 commit comments

Comments
 (0)