[Solution] 969. Pancake Sorting
- Difficulty: Medium
Problem
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 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.length
flips 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 <= A.length <= 100
A[i]
is a permutation of[1, 2, ..., A.length]
Related Topics
Array, Sort
Solution
题目大意是给一个数组,用翻转数组前 N 项的方式对其进行排序。由于是翻转前 N 项,所以可以让数组中最大的数通过此法先放到数组末尾,从后往前完成排序。又由于题目指出了给出的数组一定是从 1 到 N 的全排列,因此可以简单地定最大值 max
为 A.length
,每排出一个让 max--
,当 max == 1
时停止操作。代码如下:
public class Solution
{
public IList<int> PancakeSort(int[] A)
{
int max = A.Length;
List<int> ret = new List<int>();
while(max != 1)
{
int i = Array.IndexOf(A, max);
if(i == max - 1)
{
// 情况一:max 已经有序,直接进行下一个循环
max--;
continue;
}
else if(i == 0)
{
// 情况二:max 为数组的首个元素,进行一次前 max 个元素翻转
ret.Add(max);
ArrayReverse(A, max);
}
else
{
// 其余情况:进行两步操作,第一步将其放到数组首位,第二步归位
ret.Add(i + 1);
ret.Add(max);
ArrayReverse(A, i + 1);
ArrayReverse(A, max);
}
max--;
}
return ret;
}
static void ArrayReverse<T>(T[] arr, int len)
{
int left = 0, right = len - 1;
while (left < right)
{
T temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left++;
right--;
}
}
}
当然,题目只是要求找到一组可行解即可(而且翻转次数不超过 10 倍的数组长度),这个条件是比较宽泛的,题解做法可以保证一定能解出来,且翻转次数应该是小于 2 倍数组长度(没有验证,如有错误还请指出)。若是题目改成使用最少的翻转次数完成排序,则题目就变成了所谓的“烙饼问题”,这篇文章对这个问题给出了相应的解答。
[Solution] 969. Pancake Sorting的更多相关文章
- LC 969. Pancake Sorting
Given an array A, we can perform a pancake flip: We choose some positive integer k <= A.length, t ...
- 【leetcode】969. Pancake Sorting
题目如下: Given an array A, we can perform a pancake flip: We choose some positive integer k <= A.len ...
- 【LeetCode】969. Pancake Sorting 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 模拟法 日期 题目地址:https://leetco ...
- Leetcode 969. Pancake Sorting
每次找到当前最大数,转两下把最大数转到最右边.重复这个操作,直到都转完. 时间复杂度O(n**2) class Solution(object): def pancakeSort(self, A): ...
- [Swift]LeetCode969.煎饼排序 | Pancake Sorting
Given an array A, we can perform a pancake flip: We choose some positive integer k <= A.length, t ...
- Pancake Sorting LT969
Given an array A, we can perform a pancake flip: We choose some positive integer k <= A.length, t ...
- 118th LeetCode Weekly Contest Pancake Sorting
Given an array A, we can perform a pancake flip: We choose some positive integer k <= A.length, t ...
- 【LeetCode】Pancake Sorting(煎饼排序)
这道题是LeetCode里的第969道题. 题目要求: 给定数组 A,我们可以对其进行煎饼翻转:我们选择一些正整数 k <= A.length,然后反转 A 的前 k 个元素的顺序.我们要执行零 ...
- 【LEETCODE】57、数组分类,适中级别,题目:969、442、695
package y2019.Algorithm.array.medium; import java.util.ArrayList; import java.util.List; /** * @Proj ...
随机推荐
- python基础知识17---装饰器2
函数式编程复习: def map_test(func,array): array_new=[] for i in array: array_new.append(func(i)) return arr ...
- 【TFS 2010配置】总是提示:Error [ System Checks ] TF255466
服务器环境: Windows Server 2008 软件环境: 安装了360杀毒软件 以下为解决方案: 在验证是否可以安装 SharePoint 时的提示,Error [ System Checks ...
- 并发之痛 Thread,Goroutine,Actor
转自:http://jolestar.com/parallel-programming-model-thread-goroutine-actor/ 先梳理下两个概念,几乎所有讲并发的文章都要先讲这两个 ...
- 阿里云服务器 centos7 中继邮箱+转发服务 详细配置
阿里云centos7 邮箱转发服务配置 一.文档编写目的: 网络拓扑图: 阿里云服务器屏蔽掉25端口后,内网服务器如何通过跳板机发送邮件到外网邮箱. 如果是可联网的阿里云机器,可以直接配置mailx使 ...
- SAS 统计某个数据集各个字段频数,并汇集到一个表中
/*统计表的字段*/ PROC CONTENTS DATA=SASHELP.CLASS NOPRINT OUT=CA(KEEP=NAME); RUN; /*提取表的变量名*/ PROC SQL NOP ...
- 《深度探索C++对象模型》读书笔记(二)
第三章:Data语意学 这一章主要讲了类中的数据在内存中是如何分配的,包括(多重)继承和多态. 让我们首先从一段代码开始: class X{}; class Y :virtual public X{} ...
- Verilog语言
for循环应用 1.复位寄存器组 例如有32个寄存器,需要异步复位 always@(posedge clk or negedge rst_n) begin if (rst_n == 1'b0) beg ...
- mysql双机热备的实现
转:http://blog.csdn.net/qq394829044/article/details/53203645 Mysql数据库没有增量备份的机制,当数据量太大的时候备份是一个很大的问题.还好 ...
- Linq to SQL -- Select、Distinct和Count、Sum、Min、Max、Avg
Select/Distinct操作符 适用场景:o(∩_∩)o… 查询呗. 说明:和SQL命令中的select作用相似但位置不同,查询表达式中的select及所接子句是放在表达式最后并把子句中的变量也 ...
- Spring获取URL相关信息
获取请求的URL:request.getRequestURL().toString(); 获取上下文名称(项目名称):request.getContextPath()