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. HttpClient使用代理IP

    在爬取网页的时候,有的网站会有反爬虫措施,导致服务器请求拒接,可以使用代理IP来访问,解决请求拒绝的问题 代理IP分 透明代理.匿名代理.混淆代理.高匿代理 1.透明代理(Transparent Pr ...

  2. 设计一函数,求整数区间[a,b]和[c,d]的交集

    问题: 设计一函数,求整数区间[a,b]和[c,d]的交集.(c/c++.Java.Javascript.C#.Python)  1.Python: def calcMixed(a,b,c,d): r ...

  3. Spring使用Quartz定时调度Job无法Autowired注入Service的解决方案

    1)自定义JobFactory,通过spring的AutowireCapableBeanFactory进行注入,例如: public class MyJobFactory extends  org.s ...

  4. 201772020113 李清华《面向对象程序设计(java)》第18周学习总结

    1.实验目的与要求 (1) 综合掌握java基本程序结构: (2) 综合掌握java面向对象程序设计特点: (3) 综合掌握java GUI 程序设计结构: (4) 综合掌握java多线程编程模型: ...

  5. mysql监控执行的sql语句

    转载 https://blog.csdn.net/nzjdsds/article/details/77513869 MySQL用SQL开启general_log并设置路径 2017年08月24日 00 ...

  6. Django中manger/QuerySet类与mysql数据库的查询

    Django中的单表操作 1.精确查询 #查询的结果返回是容器Query Set的函数(Query Set模型类)​# 1. all()   查询的所有的符合条件的结果,支持正向索引,支持索引切片,不 ...

  7. MySql:SELECT 语句(五)正则表达式的使用

    关键字:REGEXP REGEXP 语句形式和 LIKE 语句相似,REGEXP 后面跟正则表达式.如果需要区分大小写,可以在 REGEXP 后加关键字 BINARY. 所有的正则表达式的规则都可以在 ...

  8. webstorm没有及时将改动保存到文件盘的问题

    webpack经常监听不到webstorm的改动,即使手动ctrl+s了,导致无法触发编译,去google查了下,发现webstorm有一个“save write”的功能,见下图: 这选项的作用应该是 ...

  9. Spring再接触 集合注入

    beans.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="htt ...

  10. SQL游标在递归是的时候提示 "游标" 名称已经存在的问题

    游标的语法: DECLARE cursor_name CURSOR [ LOCAL | GLOBAL ] [ FORWARD_ONLY | SCROLL ] [ STATIC | KEYSET | D ...