• [数组中等题] LeetCode 969. 煎饼排序


    LeetCode 969. 煎饼排序

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

    将数组元素状态分为 “已排序元素” 和 “未排序元素”。
    经过 (arr 数组长度 - 1) 次循环即可将 arr 数组排序完成,每次循环的目标是将 “未排序元素” 中的最大值通过反转,使其放在 “未排序元素” 的末尾,并将其置为 “已排序元素” 的状态。

    定义 notSortedCount 为 “未排序元素” 的长度, 初始值为 arr 数组的长度。

    • 首先找到 “未排序元素” 中最大值。
    • 若该最大值已经在 “未排序元素” 的末尾, 继续下一次循环。
    • 否则, 先将该最大值反转到 “未排序元素” 的开头; 然后再反转数组 [0 ~ noSortedCount-1], 将最大值放在 “未排序元素” 的末尾。
    • 最后将该最大值置为 “已排序元素” 的状态,然后 notSortedCount--, 当 notSortedCount1 时, 排序完成。

    举例
    Input: arr = [3, 2, 4, 1]
    Output: [3, 4, 2, 3, 2]

    每轮反转的过程:
    第一轮:[4, 2, 3, 1]
    第二轮:[1, 3, 2, 4]
    第三轮:[3, 1, 2, 4]
    第四轮:[2, 1, 3, 4]
    第五轮:[1, 2, 3, 4]

    时间复杂度: O ( n 2 ) O(n^2) O(n2)
    空间复杂度: O ( 1 ) O(1) O(1)

    Solution

    class Solution {
    public:
        // 反转前 k 个数
        void reverseK(vector<int>& arr, int k) {
            int left = 0, right = k - 1;
            int t = 0;
            while (left < right) {
                t = arr[left];
                arr[left] = arr[right];
                arr[right] = t;
                left++;
                right--;
            }
        }
        
        vector<int> pancakeSort(vector<int>& arr) {
            vector<int> ans;
            int notSortedCount = arr.size(); 
            int maxIndex = 0;
    
            while (notSortedCount > 1) {
                maxIndex = 0;
                for (int i = 1; i < notSortedCount; ++i) {
                    if (arr[i] > arr[maxIndex]) {
                        maxIndex = i;
                    }
                }
                // 若 maxIndex 已经在 "未排序元素" 的末尾, 继续下一次循环
                if (maxIndex == notSortedCount - 1) {
                    notSortedCount--;
                    continue;
                }
                // 若最大值索引不为 0, 将 [0 ~ maxIndex] 元素反转, 使最大值移动到 "未排序元素" 的开头
                if (maxIndex != 0) {
                    reverseK(arr, maxIndex + 1); 
                    ans.emplace_back(maxIndex + 1);
                }
                // 将最大值反转到 "未排序元素" 的末尾
                reverseK(arr, notSortedCount);
                ans.emplace_back(notSortedCount);
                notSortedCount--;
            }
            return ans;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
  • 相关阅读:
    【云原生】nacos权限制认证
    react组件通信
    Ambire 第一次治理投票:WALLET 质押者选择新的燃烧率和锁定期
    【设计模式】Java设计模式 - 备忘录模式
    QT实战项目1——无边框窗口拖拽和阴影
    开发手账(一)
    [dp]Matryoshka Doll 2022杭电多校第9场 1007
    Javaweb基础-前端工程化学习笔记
    把数组排成最小的数_数组中的逆序对(归并统计法)_数字在升序数组中出现的次数_丑数(剑指offer)
    C语言实现冒泡排序、选择排序、快速排序
  • 原文地址:https://blog.csdn.net/qq_39906884/article/details/126198389