当前位置:网站首页>Leetcode969: pancake sorting (medium, dynamic programming)
Leetcode969: pancake sorting (medium, dynamic programming)
2022-06-24 02:05:00 【Slow ploughing of stupid cattle】
Catalog
1. Title Description
See Leetcode969: Pancake sorting (medium, DFS).
The first two solutions adopt the general algorithm framework , One is DFS, The other is BFS(Leetcode969: Pancake sorting (medium, BFS)), It turned out to be embarrassing .DFS and BFS Of course, it is a heavy weapon with general combat power like a heavy tank , Based on these frameworks, many problems can be solved . But for a particular problem like this one , It feels like a golden marble hitting a bird , It takes a lot of ammunition, and the effect is not good . This is a small problem , After carefully analyzing the meaning of the question and get There is a very simple and clear solution to the key points , Although this solution is usually not universal . But after careful analysis, we can find that this problem is suitable to be solved by dynamic programming .
2. Problem solving analysis
In fact, this problem can be regarded as an ascending sort problem by moving the position of elements , It's just a question :
- The operation of moving position is limited , You can only reverse the problems of the previous elements
- To give a sequence of position adjustment operations
First, consider moving the maximum value in the current sequence to the end of the list , This can be achieved in two steps :
- Rearrange the subsequence up to the maximum element upside down , Adjust the largest element to the front
- Rearrange the whole sequence upside down , Adjust the largest element to the back
Of course , If the largest element is already at the end , There is no need to adjust .
When the maximum value is in place , Next, you only need to consider subsequences other than the maximum element .
therefore , This problem can be regarded as a dynamic programming problem .
Based on the above analysis , We can also know , It takes up to two steps to adjust the position of each element . Therefore, the upper limit of the total number of steps required is twice the length of the input sequence !
3. Code implementation
from typing import List
from collections import deque
import time
class Solution:
def pancakeSort(self, arr: List[int]) -> List[int]:
def flip(k):
for l in range(k//2):
arr[l], arr[k-1-l] = arr[k-1-l], arr[l]
kSeq = []
def dp(n):
# print('dp({0}), {1}'.format(n,arr[:n]))
if n == 1: # baseline case
return
for k in range(n):
if arr[k] == n:
if k == n-1: # The maximum number is already in its proper position
break
else:
if k > 0:
flip(k+1)
kSeq.append(k+1)
flip(n)
kSeq.append(n)
break
dp(n-1)
dp(len(arr))
return kSeq Back to the general catalogue of this note series : Slow ploughing by stupid cattle Leetcode Problem solving notes ( Dynamic update ...)
https://chenxiaoyuan.blog.csdn.net/article/details/123040889
边栏推荐
- NTP synchronization clock server server and client settings
- In only three steps, this large manufacturing enterprise has achieved full operational improvement with data
- What are the categories of code signing certificates? What are the differences between different types of certificates?
- Go language core 36 lecture (go language practice and application I) -- learning notes
- [technical grass planting] how to batch check your server status? Cloud probe panel setup tutorial
- Echo framework: add tracing Middleware
- layer 3 switch
- How to generate EAN13 serial number barcode
- The core battlefield of China US AI arms race: trillion level pre training model
- No serializer found for class ** and no propert no properties discovered to create BeanSerializer
猜你喜欢

application. Yaml configuring multiple running environments

Stm32g474 infrared receiving based on irtim peripherals

layer 3 switch
![[SQL injection 12] user agent injection foundation and Practice (based on burpsuite tool and sqli labs LESS18 target machine platform)](/img/c8/f6c2a62b8ab8fa88bd2b3d8f35f592.jpg)
[SQL injection 12] user agent injection foundation and Practice (based on burpsuite tool and sqli labs LESS18 target machine platform)

Review of AI hotspots this week: the Gan compression method consumes less than 1/9 of the computing power, and the open source generator turns your photos into hand drawn photos

BIM model example

Introduction to development model + test model
![[SQL injection 13] referer injection foundation and Practice (based on burpseuite tool and sqli labs less19 target platform)](/img/b5/a8c4bbaf868dd20b7dc9449d2a4378.jpg)
[SQL injection 13] referer injection foundation and Practice (based on burpseuite tool and sqli labs less19 target platform)

It's too difficult for me. Ali has had 7 rounds of interviews (5 years of experience and won the offer of P7 post)

How to fill in and register e-mail, and open mass mailing software for free
随机推荐
From idea to finished product, the necessary process of APP product development
WordPress site quickly integrates Tencent's digital identity management and control platform CIAM to realize login authentication without development
Global and Chinese gallium industry market panoramic survey and investment strategy proposal report 2022-2028
What are the categories of code signing certificates? What are the differences between different types of certificates?
Shopify has quietly taken the second place in e-commerce in North America. Is independent station the "magic weapon" to win?
5、 Build freestyle projects and related knowledge
What is the relationship between cloud desktop and cloud server? How to understand the relationship between the two
163 mailbox login portal display, enterprise mailbox computer version login portal
[expense center] demand & problem feedback week is coming! Feedback wins a good gift!
Global and Chinese dealox industry development status and demand trend forecast report 2022-2028
[new features] Tencent cloud lightweight ECS will soon support attaching data disks!!!
Collation of commonly used glusterfs commands
Global and Chinese dealox industry development status and demand trend forecast report 2022-2028
5、 Array base
Thorough and thorough analysis of factory method mode
How to build a video website? How much does it cost to build a video website?
Devops learning notes (II)
Live broadcast of the double 11 King bombing! Must buy good things introduction, come on~
SAP WM displays the standard report lx09 of TR item
Talk about 15 tips of SQL optimization