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. LeetCode 144. Binary Tree Preorder Traversal 二叉树的前序遍历 C++

    Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [,,] \ / Ou ...

  2. jquery选择器问题(找东西超级有用)

    $("[class='slider-container theme-green']").css('width','100%');就这么一行代码,很简单,这样就很容易找到唯一元素

  3. Oracle降低高水位先(转载)

    Oracle  降低高水位线的方法 高水位(HIGH WARTER MARK,HWM)好比水库中储水的水位,用于描述数据库中段的扩展方式.高水位对全表扫描方式有着至关重要的影响.当使用DELETE删除 ...

  4. HTML5 前端将 dom 元素转化为 Word,EXCEL 或者图片 并实现下载

    < 一 >  word  1,依赖于 jquery.html.word.js 插件 => https://blog-static.cnblogs.com/files/lovling/ ...

  5. 如何配置mysql的超时时间

    http://bigdata.51cto.com/art/201710/555377.htm

  6. 几种解决方法:idea 找不到符号或找不到包

    一. idea找不到符号,可能是因为编码问题,所以,在File->settings->Editor->File Encodings-找到编码设置,更改为项目的编码要求,一般都为utf ...

  7. 1503.02531-Distilling the Knowledge in a Neural Network.md

    原来交叉熵还有一个tempature,这个tempature有如下的定义: \[ q_i=\frac{e^{z_i/T}}{\sum_j{e^{z_j/T}}} \] 其中T就是tempature,一 ...

  8. python中os模块操作目录与文件名小结

    (1). 创建目录: SigleDir = 'sigle_layer' MultiDir = 'D:\\Web\\multi_layer' 创建单层目录: os.mkdir(SigleDir) 创建多 ...

  9. 安装VMware tools

    1.发现在vmware中无法复制粘贴,经查询后Centos精简版是没有VMware tools的.2.df是用来看磁盘空间使用情况的.3.rpm包的格式:name+version(主版本+此版本+修正 ...

  10. 今天遇到一个怪异的问题,maven生成项目war包中有一个Jar包不是我指定的版本,运行时会找不到符号,o(╥﹏╥)o

    我要求的jar包: 这是我parent项目中pom文件的依赖管理 这是我要生成war包那个工程最后依赖的jar包,这个时候它们的版本号还是一致的 最后项目生成的: 下图是Dmaven.test.ski ...