969. Pancake Sorting Medium

@problem@discussion
#Array#Two Pointers#Greedy#Sorting



1/**
2 * [969] Pancake Sorting
3 *
4 * Given an array of integers arr, sort the array by performing a series of pancake flips.
5 * In one pancake flip we do the following steps:
6 * 
7 * 	Choose an integer k where 1 <= k <= arr.length.
8 * 	Reverse the sub-array arr[0...k-1] (0-indexed).
9 * 
10 * For example, if arr = [3,2,1,4] and we performed a pancake flip choosing k = 3, we reverse the sub-array [3,2,1], so arr = [<u>1</u>,<u>2</u>,<u>3</u>,4] after the pancake flip at k = 3.
11 * Return an array of the k-values corresponding to a sequence of pancake flips that sort arr. Any valid answer that sorts the array within 10 * arr.length flips will be judged as correct.
12 *  
13 * Example 1:
14 * 
15 * Input: arr = [3,2,4,1]
16 * Output: [4,2,4,3]
17 * Explanation: 
18 * We perform 4 pancake flips, with k values 4, 2, 4, and 3.
19 * Starting state: arr = [3, 2, 4, 1]
20 * After 1st flip (k = 4): arr = [<u>1</u>, <u>4</u>, <u>2</u>, <u>3</u>]
21 * After 2nd flip (k = 2): arr = [<u>4</u>, <u>1</u>, 2, 3]
22 * After 3rd flip (k = 4): arr = [<u>3</u>, <u>2</u>, <u>1</u>, <u>4</u>]
23 * After 4th flip (k = 3): arr = [<u>1</u>, <u>2</u>, <u>3</u>, 4], which is sorted.
24 * 
25 * Example 2:
26 * 
27 * Input: arr = [1,2,3]
28 * Output: []
29 * Explanation: The input is already sorted, so there is no need to flip anything.
30 * Note that other answers, such as [3, 3], would also be accepted.
31 * 
32 *  
33 * Constraints:
34 * 
35 * 	1 <= arr.length <= 100
36 * 	1 <= arr[i] <= arr.length
37 * 	All integers in arr are unique (i.e. arr is a permutation of the integers from 1 to arr.length).
38 * 
39 */
40pub struct Solution {}
41
42// problem: https://leetcode.com/problems/pancake-sorting/
43// discuss: https://leetcode.com/problems/pancake-sorting/discuss/?currentPage=1&orderBy=most_votes&query=
44
45// submission codes start here
46
47impl Solution {
48    pub fn pancake_sort(arr: Vec<i32>) -> Vec<i32> {
49        vec![]
50    }
51}
52
53// submission codes end
54
55#[cfg(test)]
56mod tests {
57    use super::*;
58
59    #[test]
60    fn test_969() {
61    }
62}
63


Back
© 2025 bowen.ge All Rights Reserved.