[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 <= 100A[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 ...
随机推荐
- AutoCAD神器! AutoCAD自动切换中英文输入法插件(ZDSRF)
AutoCAD神器! AutoCAD自动切换中英文输入法插件 (一)功能特点: CAD命令中只能输入英文字符,不能输入中文,在文字编辑.文字输入.尺寸编辑中经常需要输入中文,此时就需要频繁的切换输入法 ...
- Python之while循环
1.While循环基础 2.While循环进阶 3.其他
- 在思科模拟器上配置AAA认证
1.实验拓扑 2.检测用户之间连通性 PC2 ping PC-A PC-C ping PC-A 3.路由及服务器配置 R1:在路由器R1上配置一个本地用户账号并且利用本地AAA通过console ...
- [UE4]快速移动,给单位向量加一个力
一.(Vector_End- Vector_Start ).Normalize,获取从起始位置指向目标位置的单位向量. 二.给单位向量乘以一个浮点数,即给向量加一个力,是往向量方向移动 每一帧往目标点 ...
- C#添加VisionPro控件问题
安装完VisionPro8.2版本以后,用VS2013调用时,没有默认在工具箱内,那么就需要手动添加了,选择菜单栏---->工具----->选择工具箱项,如下图所示: ...
- (转)SQLServer_十步优化SQL Server中的数据访问一
原文地址:http://tech.it168.com/a2009/1125/814/000000814758_all.shtml 第一步:应用正确的索引 我之所以先从索引谈起是因为采用正确的索引会使生 ...
- eclipse连接mysql数据库
我这里在eclipse新建一个maven 项目做测试 首先我们要在本地电脑安装了mysql数据库和mysql驱动包 我的mysql数据库是通过phpstudy自带的 这个是驱动包 window–> ...
- vector用法
转:http://www.cnblogs.com/wang7/archive/2012/04/27/2474138.html 在c++中,vector是一个十分有用的容器,下面对这个容器做一下总结. ...
- sql获取时间段内的所有日期
,'2015-01-01 00:00:00') h INTO vinson_h DELETE vinson_h DECLARE @h int SELECT @h=DATEDIFF(HOUR,'2015 ...
- 数据库alert报错:ORA-00202、ORA-15081、ORA-27072
思路分析: 1.发现数据库宕机,检查alert日志发现如下出现控制文件:I/O错误 Thu Apr 11 06:40:14 2019WARNING: Read Failed. group:2 disk ...