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. 正式表达式判断私有 IP 地址

    正式表达式判断私有 IP 地址   ^1(((0|27)(.(([1-9]?|1[0-9])[0-9]|2([0-4][0-9]|5[0-5])))|(72.(1[6-9]|2[0-9]|3[01]) ...

  2. JHipster简介

    JHipster简介 JHipster或者称Java Hipster,是一个应用代码产生器,能够创建Spring Boot + AngularJS的应用.开源项目地址:JHipster/Github. ...

  3. fdisk 分区及 swap

    使用虚拟机创建挂载点 一,创建 windows 硬盘        创建  

  4. 与大家分享学习微信小程序开发的一些心得

    因为我也才开始学习微信小程序不久,下文也是现在的一时之言,大家有不同的想法也可以在评论里共同交流讨论,希望文章能给大家提供一点点帮助. 最近接触到了一些前端框架,像Vue.js,React,发现小程序 ...

  5. pyqt5 -——菜单和工具栏

    一. 状态栏 # -*- coding: utf-8 -*-# @Time : 2018/12/22 12:37# @Author : Bo# @Email : mat_wu@163.com# @Fi ...

  6. 用java输入分数,得出分数等级

    import java.util.Scanner;public class F {  public static void main(String[] args) {  // TODO 自动生成的方法 ...

  7. 为什么对string调用swap会导致迭代器失效

    一般来说,swap操作将容器内容交换不会导致容器的指针.引用.迭代器失效. 但当容器类型为array和string时除外. 原因在于:SSO  (Short String Optimization 指 ...

  8. 跳板机 jumpserver

    http://docs.jumpserver.org/zh/docs/introduce.html 启动脚本 #!/bin/bash set -e export LANG=zh_CN.UTF- # 项 ...

  9. Cannot forward to error page for request ......

    今天现场报了以下问题: Cannot forward to error page for request [/order/search] as the response has already bee ...

  10. Suse linux enterprise 11添加设置中文输入法的方法

    Suse中输入法的设置没有在控制中心中,而是在应用程序里默认会安装好的SCIM输入法设置里边添加. 打开SCIM输入法设置->输入法引擎->全局设置,有很多国家的输入法可以选择,想要的找到 ...