All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0901_1000.s0969_pancake_sorting.Solution.kt Maven / Gradle / Ivy

There is a newer version: 1.30
Show newest version
package g0901_1000.s0969_pancake_sorting

// #Medium #Array #Sorting #Greedy #Two_Pointers
// #2023_05_06_Time_169_ms_(100.00%)_Space_36.8_MB_(12.50%)

class Solution {
    fun pancakeSort(arr: IntArray): List {
        val result: MutableList = ArrayList()
        for (i in arr.size downTo 1) {
            var max = Int.MIN_VALUE
            var index = 0
            for (j in 0 until i) {
                if (max < arr[j]) {
                    index = j + 1
                    max = arr[j]
                }
            }
            result.add(index)
            reverse(arr, index - 1)
            result.add(i)
            reverse(arr, i - 1)
        }
        return result
    }

    private fun reverse(arr: IntArray, index: Int) {
        for (i in 0..(index - 1) / 2) {
            val temp = arr[i]
            arr[i] = arr[index - i]
            arr[index - i] = temp
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy