From a8ea181cca40fb73d9cd5edc41519e30cdcec645 Mon Sep 17 00:00:00 2001 From: Ashwagandha-coder Date: Mon, 28 Apr 2025 15:36:05 +0100 Subject: [PATCH 1/3] add 45 --- .../java/com/github/contest/dp/DpLeetcode.kt | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/contest/src/main/java/com/github/contest/dp/DpLeetcode.kt b/contest/src/main/java/com/github/contest/dp/DpLeetcode.kt index 3edd750e..cb633014 100644 --- a/contest/src/main/java/com/github/contest/dp/DpLeetcode.kt +++ b/contest/src/main/java/com/github/contest/dp/DpLeetcode.kt @@ -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 +} + From a73b0d44e528e3564ab477ef10046e0bc2185619 Mon Sep 17 00:00:00 2001 From: Ashwagandha-coder Date: Mon, 28 Apr 2025 15:57:26 +0100 Subject: [PATCH 2/3] add 189 --- .../com/github/contest/array/ArrayLeetcode.kt | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/contest/src/main/java/com/github/contest/array/ArrayLeetcode.kt b/contest/src/main/java/com/github/contest/array/ArrayLeetcode.kt index ff335c50..6e7c35e9 100644 --- a/contest/src/main/java/com/github/contest/array/ArrayLeetcode.kt +++ b/contest/src/main/java/com/github/contest/array/ArrayLeetcode.kt @@ -178,7 +178,34 @@ fun maximumTripletValue(nums: IntArray): Long { return res } +/** + * 189. Rotate Array + */ + +fun rotate(nums: IntArray, k: Int): Unit { + if (k % nums.size != 0) { + val cache = mutableListOf() + 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] + } + } +} From 0491963680223f012da032587593f131170912a9 Mon Sep 17 00:00:00 2001 From: Ashwagandha-coder Date: Mon, 28 Apr 2025 15:57:44 +0100 Subject: [PATCH 3/3] small change --- contest/src/main/java/com/github/contest/array/ArrayLeetcode.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contest/src/main/java/com/github/contest/array/ArrayLeetcode.kt b/contest/src/main/java/com/github/contest/array/ArrayLeetcode.kt index 6e7c35e9..aaaf4573 100644 --- a/contest/src/main/java/com/github/contest/array/ArrayLeetcode.kt +++ b/contest/src/main/java/com/github/contest/array/ArrayLeetcode.kt @@ -182,7 +182,7 @@ fun maximumTripletValue(nums: IntArray): Long { * 189. Rotate Array */ -fun rotate(nums: IntArray, k: Int): Unit { +fun rotate(nums: IntArray, k: Int) { if (k % nums.size != 0) { val cache = mutableListOf() val bound = when {