Skip to content

Commit d0d1fb8

Browse files
authored
Merge pull request DaleStudy#662 from gmlwls96/main
[gmlwls96] Week1
2 parents 4d1540b + 67d0f73 commit d0d1fb8

File tree

5 files changed

+68
-0
lines changed

5 files changed

+68
-0
lines changed

contains-duplicate/gmlwls96.kt

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

house-robber/gmlwls96.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package leetcode_study
2+
3+
import java.lang.Integer.max
4+
5+
class Solution {
6+
fun rob(nums: IntArray): Int {
7+
return max(rob_recursion(nums, 0), rob_recursion(nums, 1))
8+
}
9+
10+
private fun rob_recursion(nums: IntArray, index: Int): Int {
11+
if (index >= nums.size) {
12+
return 0
13+
}
14+
return nums[index] + Integer.max(rob_recursion(nums, index + 2), rob_recursion(nums, index + 3))
15+
}
16+
}
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 longestConsecutive(nums: IntArray): Int {
5+
nums.sort()
6+
val consecutiveArray = IntArray(nums.size)
7+
var maxCount = 0
8+
for (i in nums.lastIndex - 1 downTo (0)) {
9+
if (nums[i] + 1 == nums[i + 1]) {
10+
consecutiveArray[i] += consecutiveArray[i + 1] + 1
11+
if (consecutiveArray[i] > maxCount) {
12+
maxCount = consecutiveArray[i]
13+
}
14+
}
15+
}
16+
return maxCount + 1
17+
}
18+
}

top-k-frequent-elements/gmlwls96.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
fun topKFrequent(nums: IntArray, k: Int): IntArray {
3+
val answer = IntArray(k)
4+
val set = nums.toSet()
5+
val mutableList = mutableListOf<Pair<Int, Int>>()
6+
set.forEach { num ->
7+
mutableList.add(num to nums.count { it == num })
8+
}
9+
mutableList.sortByDescending { it.second }
10+
for (i in 0 until k) {
11+
answer[i] = mutableList[i].first
12+
}
13+
return answer
14+
}
15+
}

valid-palindrome/gmlwls96.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
fun isPalindrome(s: String): Boolean {
3+
val filterStr = s
4+
.lowercase()
5+
.filter { it in 'a'..'z' }
6+
if (filterStr.isEmpty()) return true
7+
for (i in 0..filterStr.lastIndex / 2) {
8+
if (filterStr[i] != filterStr[filterStr.lastIndex - i]) return false
9+
}
10+
return true
11+
}
12+
}

0 commit comments

Comments
 (0)