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
27 changes: 27 additions & 0 deletions contest/src/main/java/com/github/contest/array/ArrayLeetcode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,34 @@ fun maximumTripletValue(nums: IntArray): Long {
return res
}

/**
* 189. Rotate Array
*/

fun rotate(nums: IntArray, k: Int) {
if (k % nums.size != 0) {
val cache = mutableListOf<Int>()
val bound = when {
k > nums.size -> {
val temp = k % nums.size
nums.size - temp
}

else -> nums.size - k
}
for (i in bound until nums.size) {
cache.add(nums[i])
}

for (i in 0 until bound) {
cache.add(nums[i])
}

for (i in 0 until cache.size) {
nums[i] = cache[i]
}
}
}



25 changes: 25 additions & 0 deletions contest/src/main/java/com/github/contest/dp/DpLeetcode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,31 @@ fun canJump(nums: IntArray): Boolean {
return true
}

/**
* 45. Jump Game II
*/

fun jump(nums: IntArray): Int {
var jumps = 0
var currentEnd = 0
var farthest = 0

for (i in 0 until nums.size - 1) {
farthest = maxOf(farthest, i + nums[i])

if (i == currentEnd) {
jumps++
currentEnd = farthest

if (currentEnd >= nums.size - 1) {
break
}
}
}

return jumps
}




Expand Down