当前位置:网站首页>969. Pancake sorting
969. Pancake sorting
2022-07-06 16:08:00 【mrbone9】
Address :
Power button
https://leetcode-cn.com/problems/pancake-sorting/
subject :
Give you an array of integers arr , Please use Pancake flip Finish sorting the array .
The execution process of a pancake flip is as follows :
Choose an integer k ,1 <= k <= arr.length
Anti rotor array arr[0...k-1]( Subscript from 0 Start )
for example ,arr = [3,2,1,4] , choice k = 3 Make a pancake flip , Anti rotor array [3,2,1] , obtain arr = [1,2,3,4] .
Returning as an array enables arr Corresponding to the orderly pancake flipping operation k Value sequence . Any array that is sorted and flipped a few times 10 * arr.length All valid answers within the range will be judged to be correct .
Example 1:
| Input :[3,2,4,1] Output :[4,2,4,3] explain : We execute 4 Turn the pancake over again ,k Values, respectively 4,2,4, and 3. The initial state arr = [3, 2, 4, 1] After the first flip (k = 4):arr = [1, 4, 2, 3] After the second flip (k = 2):arr = [4, 1, 2, 3] After the third flip (k = 4):arr = [3, 2, 1, 4] After the fourth flip (k = 3):arr = [1, 2, 3, 4], Sorting is now complete . |
Example 2:
| Input :[1,2,3] Output :[] explain : The input has been sorted , So there's no need to flip anything . Please note that , Other possible answers , Such as [3,3] , Will also be judged to be correct . |
Tips :
| 1 <= arr.length <= 100 1 <= arr[i] <= arr.length arr All integers in are different from each other ( namely ,arr It's from 1 To arr.length An arrangement of integers ) |
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/pancake-sorting
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Ideas :
First of all, we need to find a “ The formula ”, You can deduce the final result on paper before you see how to write the code
You can know the title :
1. Array element values are not equal , It means that there is a maximum , minimum value
2. If the results are in ascending order
3. Flip yes 0~k-1, It means that if you put the minimum value first , Then the subsequent flipping is in vain
So we need to find Maximum , Make it to the end through operation , In this way, the whole string becomes a substring
Then repeat the action to converge to an element , Achieve ascending order
What if we put the maximum value at the end ?
Example : 3,2,4,1
1. Find the maximum
3,2,4,1 , Its coordinates idx = 2, The maximum coordinate of the current string maxIdx = 3
2. Flip k = idx+1 = 3
4,2,3,1 , The maximum value is [0]
3. Flip k = maxIdx + 1 = 4
1,3,2,4 , The maximum value is placed at the end , At this time, we need to reduce the range of strings maxIdx = 2
So circular , The resulting 1,2,3,4
The optimization part is , If the maximum coordinate found is just at the end , Then directly reduce the string range , No flipping is required
Method 1 、 Cycle the maximum value to the end
void dispArr(int *arr, int arrSize)
{
printf("arr = [");
for(int i=0; i<arrSize; i++)
printf("%d ", arr[i]);
printf("]\n\n");
}
int findMaxnumIdx(int *arr, int lastIdx)
{
int max = arr[0];
int maxIdx = 0;
for(int i=0; i<=lastIdx; i++)
{
if(max < arr[i])
{
maxIdx = i;
max = arr[i];
}
}
return maxIdx;
}
void reversArr(int *arr, int lastIdx)
{
int l = 0, r = lastIdx;
while(l < r)
{
int tmp = arr[l];
arr[l] = arr[r];
arr[r] = tmp;
l++;
r--;
}
}
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* pancakeSort(int* arr, int arrSize, int* returnSize){
int i = 0;
int k, cnt = 0;
int rlen = arrSize * 2;
int *ret = (int *)malloc(sizeof(int) * rlen);
int maxIdx = arrSize - 1;
while(maxIdx != 0)
{
int idx = findMaxnumIdx(arr, maxIdx);
//printf("idx=%d, maxIdx=%d\n", idx, maxIdx);
if(idx == maxIdx)
{
maxIdx--;
rlen--;
continue;
}
//dispArr(arr, arrSize);
reversArr(arr, idx);
//dispArr(arr, arrSize);
reversArr(arr, maxIdx);
//dispArr(arr, arrSize);
k = idx + 1;
//printf("k=%d\n\n", k);
ret[i++] = k;
k = maxIdx + 1;
//printf("k=%d\n\n", k);
ret[i++] = k;
cnt+=2;
maxIdx--;
}
if(rlen != cnt)
{
rlen = cnt;
ret = (int *)realloc(ret, sizeof(int) * rlen);
}
*returnSize = rlen;
return ret;
}边栏推荐
- Interesting drink
- 【高老师软件需求分析】20级云班课习题答案合集
- Quick to typescript Guide
- 7-1 懂的都懂 (20 分)
- STM32 how to use stlink download program: light LED running light (Library version)
- CS zero foundation introductory learning record
- HDU - 6024 building shops (girls' competition)
- Auto.js入门
- Web based photo digital printing website
- Penetration test (7) -- vulnerability scanning tool Nessus
猜你喜欢

Penetration test (8) -- official document of burp Suite Pro

2078. Two houses with different colors and the farthest distance

【练习-7】Crossword Answers

C language must memorize code Encyclopedia

基于web的照片数码冲印网站

1010 things that college students majoring in it must do before graduation

Write web games in C language

Ball Dropping

信息安全-史诗级漏洞Log4j的漏洞机理和防范措施

B - 代码派对(女生赛)
随机推荐
Information security - threat detection engine - common rule engine base performance comparison
Research Report of exterior wall insulation system (ewis) industry - market status analysis and development prospect prediction
[teacher Gao UML software modeling foundation] collection of exercises and answers for level 20 cloud class
渗透测试 ( 5 ) --- 扫描之王 nmap、渗透测试工具实战技巧合集
栈的经典应用—括号匹配问题
Nodejs+vue online fresh flower shop sales information system express+mysql
1323. Maximum number of 6 and 9
Gartner: five suggestions on best practices for zero trust network access
Differential (one-dimensional, two-dimensional, three-dimensional) Blue Bridge Cup three body attack
渗透测试 2 --- XSS、CSRF、文件上传、文件包含、反序列化漏洞
China's peripheral catheter market trend report, technological innovation and market forecast
【练习-9】Zombie’s Treasure Chest
Luogu P1102 A-B number pair (dichotomy, map, double pointer)
E. Breaking the Wall
【练习-11】4 Values whose Sum is 0(和为0的4个值)
Write web games in C language
Socket communication
Find 3-friendly Integers
Auto. Getting started with JS
[exercise 4-1] cake distribution