Package g0901_1000.s0969_pancake_sorting
Class Solution
java.lang.Object
g0901_1000.s0969_pancake_sorting.Solution
969 - Pancake Sorting.<p>Medium</p>
<p>Given an array of integers <code>arr</code>, sort the array by performing a series of <strong>pancake flips</strong>.</p>
<p>In one pancake flip we do the following steps:</p>
<ul>
<li>Choose an integer <code>k</code> where <code>1 <= k <= arr.length</code>.</li>
<li>Reverse the sub-array <code>arr[0...k-1]</code> ( <strong>0-indexed</strong> ).</li>
</ul>
<p>For example, if <code>arr = [3,2,1,4]</code> and we performed a pancake flip choosing <code>k = 3</code>, we reverse the sub-array <code>[3,2,1]</code>, so <code>arr = [1,2,3,4]</code> after the pancake flip at <code>k = 3</code>.</p>
<p>Return <em>an array of the</em> <code>k</code><em>-values corresponding to a sequence of pancake flips that sort</em> <code>arr</code>. Any valid answer that sorts the array within <code>10 * arr.length</code> flips will be judged as correct.</p>
<p><strong>Example 1:</strong></p>
<p><strong>Input:</strong> arr = [3,2,4,1]</p>
<p><strong>Output:</strong> [4,2,4,3]</p>
<p><strong>Explanation:</strong></p>
<p>We perform 4 pancake flips, with k values 4, 2, 4, and 3.</p>
<p>Starting state: arr = [3, 2, 4, 1]</p>
<p>After 1st flip (k = 4): arr = [1, 4, 2, 3]</p>
<p>After 2nd flip (k = 2): arr = [4, 1, 2, 3]</p>
<p>After 3rd flip (k = 4): arr = [3, 2, 1, 4]</p>
<p>After 4th flip (k = 3): arr = [1, 2, 3, 4], which is sorted.</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> arr = [1,2,3]</p>
<p><strong>Output:</strong> []</p>
<p><strong>Explanation:</strong> The input is already sorted, so there is no need to flip anything.</p>
<p>Note that other answers, such as [3, 3], would also be accepted.</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 100</code></li>
<li><code>1 <= arr[i] <= arr.length</code></li>
<li>All integers in <code>arr</code> are unique (i.e. <code>arr</code> is a permutation of the integers from <code>1</code> to <code>arr.length</code>).</li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
pancakeSort
-