![JAR search and dependency download from the Maven repository](/logo.png)
g1001_1100.s1053_previous_permutation_with_one_swap.Solution.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-kotlin Show documentation
Show all versions of leetcode-in-kotlin Show documentation
Kotlin-based LeetCode algorithm problem solutions, regularly updated
package g1001_1100.s1053_previous_permutation_with_one_swap
// #Medium #Array #Greedy #2023_05_30_Time_338_ms_(25.00%)_Space_71.2_MB_(25.00%)
class Solution {
fun prevPermOpt1(arr: IntArray): IntArray {
for (i in arr.indices.reversed()) {
var diff = Int.MAX_VALUE
var index = i
for (j in i + 1 until arr.size) {
if (arr[i] - arr[j] in 1 until diff) {
diff = arr[i] - arr[j]
index = j
}
}
if (diff != Int.MAX_VALUE) {
val temp = arr[i]
arr[i] = arr[index]
arr[index] = temp
break
}
}
return arr
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy