当前位置:网站首页>Patience sorting - specializing in quickly solving the longest increasing subarray
Patience sorting - specializing in quickly solving the longest increasing subarray
2022-08-08 16:05:00 【The little black classmate who came out halfway】
The rules for patient sorting
其实最长递增子序列和一种叫做 patience game 的纸牌游戏有关,甚至有一种排序方法就叫做 patience sorting(耐心排序,复杂度为 n l o g n nlogn nlogn).And the essence of this sort is by二分查找实现的.
其规则为:
1、Draw cards in turn、Press the card,But you can only push a lower-ranked card over a higher-ranked card.
2、如果当前牌点数较大没有可以放置的堆,则新建一个堆,把这张牌放进去.
3、如果当前牌有多个堆可供选择,则选择最左边的那一堆放置.
按照上述规则执行,可以算出最长递增子序列,牌的堆数就是最长递增子序列的长度.其中,The implementation of the third rule is used寻找左侧边界的二分查找!
Implementation of patient sorting
class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
int n = nums.size();
//Build a deck first
vector<int>top(n);
//Record the number of decks
int ans = 0;
//Start placing each card
for(int i=0;i<n;++i)
{
int card = nums[i];
//The first deck and the last deck
int left = 0,right = ans-1;
while(left <= right)
{
int mid = left+(right-left)/2;
//The condition is satisfied if the current card is less than the top of the card
if(card <= top[mid]) right = mid-1;
else left = mid+1;
}
if(left == ans) ++ans;//Equivalent to lookup beyond bounds
//Put in cards
top[left] = card;
}
return ans;
}
};
The largest increasing subsequence in two dimensions
先对宽度 w 进行升序排序,如果遇到 w 相同的情况,则按照高度 h 降序排序(This is because of a widthwOnly one envelope can exist,So simply will be the largesth的排在最前面,i.e. so you can focus onh,And don't worry about selecting more than onew相同的信封);之后把所有的 h 作为一个数组,在这个数组上计算 LIS 的长度就是答案.
class Solution {
public:
int maxEnvelopes(vector<vector<int>>& envelopes) {
int n = envelopes.size();
sort(envelopes.begin(),envelopes.end(),[](vector<int> &a,vector<int>& b){
return a[0]==b[0]?
a[1]>b[1]:a[0]<b[0];});
//The following is the order of patience!
vector<int>top(n,0);
int ans = 0;
for(int i=0;i<n;++i)
{
int card = envelopes[i][1];
int left = 0,right = ans-1;
while(left<=right)
{
int mid = left+(right-left)/2;
if(card<=top[mid]) right = mid-1;
else left = mid+1;
}
if(left == ans) ++ans;
top[left] = card;
}
return ans;
}
};
边栏推荐
猜你喜欢
leetcode 31. 下一个排列(实现next_permutation 函数)
Dry goods: design high concurrency architecture from scratch
Smobiler的复杂控件的由来与创造
手把手教你uniapp接入聊天IM即时通讯功能-源码分享
Share these new Blender plugins that designers must not miss in 2022
NFT质押挖矿分红系统开发逻辑功能介绍
Streamsets Data Collector 3.12
[Unity entry plan] Use the double blood bar method to control the blood loss speed of the damage area
【云原生】-MySQL压测神器HammerDB的部署及使用
华为云分布式缓存服务Redis开通及使用规划教程【华为云至简致远】
随机推荐
Streamsets Data Collector 3.12
C#/VB.NET 将PDF转为PDF/X-1a:2001
瑞吉外卖学习笔记2
C语言学习概览(五)
MySQL中常见的内些...啥
Go 语言 Strconv 库常用方法
国产数据库的红利还能“吃”多久?
The situation of the solution of the equation system and the correlation transformation of the vector group
GPT3中文自动生成小说「谷歌小发猫写作」
bzoj3693 round table hall theorem + segment tree
jupyter notebook hide & show all output
bzoj3262 陌上花开
VIT:Transformer进军CV的里程碑
10分钟快速入门RDS【华为云至简致远】
方程组解的情况与向量组相关性转化【线代碎碎念】
LED显示屏在会议室如何应用
leetcode/number of palindromic substrings
鹏城杯部分WP
groovy基础学习
Redis哨兵的配置和原理