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. 1 <= A.length <= 100
    2. A[i] is a permutation of [1, 2, ..., A.length]

Idea 1. Similar to heapsort, find the maximum value, flip it first to get it to the head, then flip it to put it in the right place (the end of array), continue for the 2nd maximum value.

Time complexity: O(n^2)

Space complexity: O(1)

 class Solution {
private void reverse(int[] A, int left, int right) {
for(int i = left, j = right; i < j; ++i, --j) {
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}
public List<Integer> pancakeSort(int[] A) {
List<Integer> result = new ArrayList<>(); for(int max = A.length; max >= 1; --max){
for(int i = 0; i < max; ++i) {
if(A[i] == max) {
reverse(A, 0, i);
reverse(A, 0, max-1);
result.add(i+1);
result.add(max);
break;
}
}
}
return result;
}
}

网上看到的找到max break loop 的另一种写法,简洁些,就是看着还是不习惯

 class Solution {
private void reverse(int[] A, int left, int right) {
for(int i = left, j = right; i < j; ++i, --j) {
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}
public List<Integer> pancakeSort(int[] A) {
List<Integer> result = new ArrayList<>(); for(int max = A.length, i; max >= 1; --max){
for(i = 0; A[i] != max; ++i); reverse(A, 0, i);
reverse(A, 0, max-1);
result.add(i+1);
result.add(max); }
return result;
}
}

Idea 1.b 不需要改变数组,先sort, 记住reverse后的index, i = k - i, 官方解法

Time complexity: O(n^2)

Space complexity: O(n)

 class Solution {
public List<Integer> pancakeSort(int[] A) {
int n = A.length;
List<Integer> indexA = new ArrayList<>();
for(int i = 0; i < n; ++i) {
indexA.add(i);
} Collections.sort(indexA, (a, b) -> Integer.compare(A[b], A[a])); List<Integer> result = new ArrayList<>(); for(int i: indexA) {
for(int k: result) {
if(i <= k-1) {
i = k-1 - i;
}
}
result.add(i+1);
result.add(n--);
}
return result;
}
}

Pancake Sorting LT969的更多相关文章

  1. [Swift]LeetCode969.煎饼排序 | Pancake Sorting

    Given an array A, we can perform a pancake flip: We choose some positive integer k <= A.length, t ...

  2. [Solution] 969. Pancake Sorting

    Difficulty: Medium Problem Given an array A, we can perform a pancake flip: We choose some positive ...

  3. 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 ...

  4. LC 969. Pancake Sorting

    Given an array A, we can perform a pancake flip: We choose some positive integer k <= A.length, t ...

  5. 【leetcode】969. Pancake Sorting

    题目如下: Given an array A, we can perform a pancake flip: We choose some positive integer k <= A.len ...

  6. 【LeetCode】969. Pancake Sorting 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 模拟法 日期 题目地址:https://leetco ...

  7. Leetcode 969. Pancake Sorting

    每次找到当前最大数,转两下把最大数转到最右边.重复这个操作,直到都转完. 时间复杂度O(n**2) class Solution(object): def pancakeSort(self, A): ...

  8. 【LeetCode】Pancake Sorting(煎饼排序)

    这道题是LeetCode里的第969道题. 题目要求: 给定数组 A,我们可以对其进行煎饼翻转:我们选择一些正整数 k <= A.length,然后反转 A 的前 k 个元素的顺序.我们要执行零 ...

  9. pancake的排序- 1.3 一摞烙饼的排序 《编程之美》读书笔记03

    问题:     星期五的晚上,一帮同事在希格玛大厦附近的“硬盘酒吧”多喝了几杯.程序员多喝了几杯之后谈什么呢?自然是算法问题.有个同事说:“我以前在餐馆打工,顾客经常点非常多的烙饼.店里的饼大小不一, ...

随机推荐

  1. windows环境下安装rabbitmq及配置可视化管理界面

    1.环境 windows7 64位 rabbitmq3.7.9 erlang10.22.先安装erlang,后安装rabbitmq.下载地址: rabbitmq http://www.rabbitmq ...

  2. HTML5 动画用 animation transform transition 做的两个例子

    1,左右移动,自我翻转的圆 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...

  3. 如何系统的学习Java

    初学者记住一点,学习Java一定是连续性的且循序渐进的“系统化”学习,首先我给你提供一个优秀Java工程师的学习路线. web前端方面:html.css,Java.jQuery.xml解析.Boots ...

  4. Spark sql 在yarn-cluster模式下找不到表

    在hive里建一个数据库test,在数据库里建了一张表user,然后在Spark程序中使用Spark sql读取这张表 "select * form test.user" 当部署模 ...

  5. lamp之apache配置https访问

    配置apache 使用https 注:怕其他人由于路径的原因出问题,首先声明一下,本人apache的安装目录为 : /usr/local/httpd2.4.25,如果不是,请参考进行配置 注: 对于如 ...

  6. 快速干掉Windows Defender

    1.快捷键Win+R,调出"运行"对话框,输入"gpedit.msc",打开组策略编辑器: 2.展开"计算机配置"→"管理模板&q ...

  7. xshell下mysql数据库只导出表结构不导出数据

    操作系统:linux: 使用软件:xshell.winscp 进入系统之后输入命令: mysqldump -u 用户名 - p 密码 -d 数据库名 > aaa.sql 注意字符间的空格. 之后 ...

  8. 【C++】C++中的string类的用法总结

    相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果离开了MFC框架,还有没有这样使用起来非常方便的类呢?答案是肯 ...

  9. eclipse与idea快捷键对比以及idea debug、git快捷键

    eclipse与idea快捷键 表格中的空格都是忘记了~ 功能 eclipse idea 生成返回值对象 alt+shift+L ctrl+alt+V 找到启动类   ctrl+alt+Home 类的 ...

  10. 什么是云?Iaas,Paas和SaaS

    周围的朋友听说我是做云相关的,总是爱问啥是云?别不是虚幻的概念吧.云计算当然不是虚幻的概念,“云”其实是互联网的一个隐喻,简单地说,云计算是通过Internet(“云”)交付计算服务——服务器.存储. ...