118th LeetCode Weekly Contest Pancake Sorting

Given an array A, we can perform a pancake flip: We choose some positive integer k <= A.length, then reverse the order of the first kelements of A.  We want to perform zero or more pancake flips (doing them one after another in succession) to sort the array A.

Return the k-values corresponding to a sequence of pancake flips that sort A.  Any valid answer that sorts the array within 10 * A.lengthflips will be judged as correct.

Example 1:

Input: [3,2,4,1]
Output: [4,2,4,3]
Explanation:
We perform 4 pancake flips, with k values 4, 2, 4, and 3.
Starting state: A = [3, 2, 4, 1]
After 1st flip (k=4): A = [1, 4, 2, 3]
After 2nd flip (k=2): A = [4, 1, 2, 3]
After 3rd flip (k=4): A = [3, 2, 1, 4]
After 4th flip (k=3): A = [1, 2, 3, 4], which is sorted.

Example 2:

Input: [1,2,3]
Output: []
Explanation: The input is already sorted, so there is no need to flip anything.
Note that other answers, such as [3, 3], would also be accepted.

Note:

  1. 1 <= A.length <= 100
  2. A[i] is a permutation of [1, 2, ..., A.length]

对于这种题倒着处理。

主要是看两种情况,

3 1 2

要把2放后面,这1 2 3变成2 1 3,再全部倒着来一次3 1 2就OK

2 3 1这种,就直接全部倒着来就行。

class Solution {
public:
    int arr[400];
    void reverse1(int from,int to) {
            while(from<to){
                int tmp=arr[from];
                arr[from++]=arr[to];
                arr[to--]=tmp;
            }
    }
    int Find(int num,int last){
        for(int i=0;i<last;i++){
            if(arr[i]==num){
                return i+1;
            }
        }
        return 0;
    }
    void x(int len){
        for(int i=0;i<len;i++){
            cout<<arr[i]<<" ";
        }
        cout<<endl;
    }

    vector<int> pancakeSort(vector<int>& A) {
        vector<int>Ve = A;
        vector<int>Vee;
        sort(Ve.begin(),Ve.end());

        int Size = A.size();

        for(int i=0;i<Size;i++){
            arr[i] = Ve[i];
        }

        for(int i=0;i<Size;i++){
           int address = Find(A[Size - i - 1],Size-i);
           if(address == 1){
                //cout<<address<<" A"<<endl;
                reverse1(0,Size - i - 1);
                //x(Size);
                if(Size - i == 1){
                    continue;
                }
                Vee.push_back(Size - i);

           }else if(address == Size - i){
                //cout<<address<<" B "<<Size - i<<endl;
                continue;
           }else{
               //cout<<address<<" C"<<endl;
                reverse1(0,address - 1);
                //x(Size);
               if(address == 1){
                    continue;
                }
                Vee.push_back(address);
                reverse1(0,Size - i - 1);
                //x(Size);
               if(Size - i == 1){
                    continue;
                }
                Vee.push_back(Size - i);
           }
        }
        reverse(Vee.begin(),Vee.end());
        return Vee;
    }
};

原文地址:https://www.cnblogs.com/yinghualuowu/p/10230810.html

时间: 2024-10-07 18:41:57

118th LeetCode Weekly Contest Pancake Sorting的相关文章

Leetcode Weekly Contest 86

Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个由整数组成的 N × N 矩阵,其中有多少个 3 × 3 的 "幻方" 子矩阵?(每个子矩阵都是连续的). 直接模拟即可,本来是签到题,由于粗心,浪费了时间. 1 class Solution { 2 public: 3 int numMagicSquaresInside(vector&l

LeetCode Weekly Contest 118

要死要死,第一题竟然错误8次,心态崩了呀,自己没有考虑清楚,STL用的也不是很熟,一直犯错. 第二题也是在室友的帮助下完成的,心态崩了. 970. Powerful Integers Given two non-negative integers x and y, an integer is powerful if it is equal to x^i + y^j for some integers i >= 0 and j >= 0. Return a list of all powerfu

【leetcode】969. Pancake Sorting

题目如下: Given an array A, we can perform a pancake flip: We choose some positive integer k <= A.length, then reverse the order of the first k elements of A.  We want to perform zero or more pancake flips (doing them one after another in succession) to

Leetcode Weekly Contest 152

退役老人现在连leetcode都不会做了 = = 今天早上做了leetcode第三题题目看错了,加上比赛中间还在调投稿的实验,一心二用直接gg 总结下教训就是 本渣现在做题连题目都看不清就开始做.开始写题之前应当把样例过一遍,然后自己再造1-2个例子,然后再开始做 A题:统计素数的个数(素数筛或者sqrt(n)判断都可以),然后分别计算count! class Solution { public: int numPrimeArrangements(int n) { vector<int> ha

[Leetcode Weekly Contest]174

链接:LeetCode [Leetcode]5328. 方阵中战斗力最弱的 K 行 给你一个大小为?m?* n?的方阵?mat,方阵由若干军人和平民组成,分别用 0 和 1 表示. 请你返回方阵中战斗力最弱的?k?行的索引,按从最弱到最强排序. 如果第?i?行的军人数量少于第?j?行,或者两行军人数量相同但 i 小于 j,那么我们认为第 i 行的战斗力比第 j 行弱. 军人 总是 排在一行中的靠前位置,也就是说 1 总是出现在 0 之前. 输入:\(mat = [[1,1,0,0,0], [1,

108th LeetCode Weekly Contest Binary Subarrays With Sum

In an array A of 0s and 1s, how many non-empty subarrays have sum S? Example 1: Input: A = [1,0,1,0,1], S = 2 Output: 4 Explanation: The 4 subarrays are bolded below: [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] Note: A.length <= 30000 0 <= S <

108th LeetCode Weekly Contest Minimum Falling Path Sum

Given a square array of integers A, we want the minimum sum of a falling path through A. A falling path starts at any element in the first row, and chooses one element from each row.  The next row's choice must be in a column that is different from t

113th LeetCode Weekly Contest Flip Equivalent Binary Trees

For a binary tree T, we can define a flip operation as follows: choose any node, and swap the left and right child subtrees. A binary tree X is flip equivalent to a binary tree Y if and only if we can make X equal to Y after some number of flip opera

113th LeetCode Weekly Contest Largest Time for Given Digits

Given an array of 4 digits, return the largest 24 hour time that can be made. The smallest 24 hour time is 00:00, and the largest is 23:59.  Starting from 00:00, a time is larger if more time has elapsed since midnight. Return the answer as a string