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
7 changes: 7 additions & 0 deletions contains-duplicate/gmlwls96.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package leetcode_study

class Solution {
fun containsDuplicate(nums: IntArray): Boolean {
return nums.toSet().size != nums.size
}
}
16 changes: 16 additions & 0 deletions house-robber/gmlwls96.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package leetcode_study

import java.lang.Integer.max

class Solution {
fun rob(nums: IntArray): Int {
return max(rob_recursion(nums, 0), rob_recursion(nums, 1))
}

private fun rob_recursion(nums: IntArray, index: Int): Int {
if (index >= nums.size) {
return 0
}
return nums[index] + Integer.max(rob_recursion(nums, index + 2), rob_recursion(nums, index + 3))
}
}
18 changes: 18 additions & 0 deletions longest-consecutive-sequence/gmlwls96.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package leetcode_study

class Solution {
fun longestConsecutive(nums: IntArray): Int {
nums.sort()
val consecutiveArray = IntArray(nums.size)
var maxCount = 0
for (i in nums.lastIndex - 1 downTo (0)) {
if (nums[i] + 1 == nums[i + 1]) {
consecutiveArray[i] += consecutiveArray[i + 1] + 1
if (consecutiveArray[i] > maxCount) {
maxCount = consecutiveArray[i]
}
}
}
return maxCount + 1
}
}
15 changes: 15 additions & 0 deletions top-k-frequent-elements/gmlwls96.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
fun topKFrequent(nums: IntArray, k: Int): IntArray {
val answer = IntArray(k)
val set = nums.toSet()
val mutableList = mutableListOf<Pair<Int, Int>>()
set.forEach { num ->
mutableList.add(num to nums.count { it == num })
}
mutableList.sortByDescending { it.second }
for (i in 0 until k) {
answer[i] = mutableList[i].first
}
return answer
}
}
12 changes: 12 additions & 0 deletions valid-palindrome/gmlwls96.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution {
fun isPalindrome(s: String): Boolean {
val filterStr = s
.lowercase()
.filter { it in 'a'..'z' }
if (filterStr.isEmpty()) return true
for (i in 0..filterStr.lastIndex / 2) {
if (filterStr[i] != filterStr[filterStr.lastIndex - i]) return false
}
return true
}
}
Loading