Skip to content

Commit cff1aa3

Browse files
Merge pull request #183
add new problems 28.04
2 parents 45e876a + 0491963 commit cff1aa3

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

contest/src/main/java/com/github/contest/array/ArrayLeetcode.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,34 @@ fun maximumTripletValue(nums: IntArray): Long {
178178
return res
179179
}
180180

181+
/**
182+
* 189. Rotate Array
183+
*/
184+
185+
fun rotate(nums: IntArray, k: Int) {
186+
if (k % nums.size != 0) {
187+
val cache = mutableListOf<Int>()
188+
val bound = when {
189+
k > nums.size -> {
190+
val temp = k % nums.size
191+
nums.size - temp
192+
}
193+
194+
else -> nums.size - k
195+
}
196+
for (i in bound until nums.size) {
197+
cache.add(nums[i])
198+
}
199+
200+
for (i in 0 until bound) {
201+
cache.add(nums[i])
202+
}
181203

204+
for (i in 0 until cache.size) {
205+
nums[i] = cache[i]
206+
}
207+
}
208+
}
182209

183210

184211

contest/src/main/java/com/github/contest/dp/DpLeetcode.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,31 @@ fun canJump(nums: IntArray): Boolean {
597597
return true
598598
}
599599

600+
/**
601+
* 45. Jump Game II
602+
*/
603+
604+
fun jump(nums: IntArray): Int {
605+
var jumps = 0
606+
var currentEnd = 0
607+
var farthest = 0
608+
609+
for (i in 0 until nums.size - 1) {
610+
farthest = maxOf(farthest, i + nums[i])
611+
612+
if (i == currentEnd) {
613+
jumps++
614+
currentEnd = farthest
615+
616+
if (currentEnd >= nums.size - 1) {
617+
break
618+
}
619+
}
620+
}
621+
622+
return jumps
623+
}
624+
600625

601626

602627

0 commit comments

Comments
 (0)