Leetcode 1009 Solution

This article provides solution to leetcode question 1009 (pancake-sorting)

https://leetcode.com/problems/pancake-sorting

Solution

class Solution(object): def pancakeSort(self, A): """ :type A: List[int] :rtype: List[int] """ r = len(A) - 1 ans = []
while r > 0: pos = 0 for i in range(0, r + 1): if r + 1 == A[i]: pos = i
if pos != r: if pos != 0: A = list(reversed(A[0:pos + 1])) + A[pos + 1:] A = list(reversed(A[0:r + 1])) + A[r + 1:]
if pos != 0: ans.append(pos + 1) ans.append(r + 1)
r -= 1
return ans