【Lintcode】1645. Least Subsequences
题目地址:
https://www.lintcode.com/problem/1645/
给定一个长nnn的数组AAA,问AAA最少能分解为多少个严格递减的子序列之并。
在AAA上定义偏序关系<<<为i<j,A[i]≤A[j]i<j, A[i]\le A[j]i<j,A[i]≤A[j],那么本题其实就是求AAA的最少的反链分解的个数,由偏序集分解定理,其等价于求最长非严格上升子序列的长度,可以用动态规划,也可以直接计算,算法参考https://blog.csdn.net/qq_46105170/article/details/108616895。代码如下:
import java.util.List;public class Solution {/*** @param A: The original array.* @return: Count the minimum number of subarrays.*/public int LeastSubsequences(List<Integer> A) {// Write your code here.int[] f = new int[A.size()];int idx = 0;for (int i = 0; i < A.size(); i++) {int x = A.get(i);int pos = binarySearch(f, idx, x);// 如果诸反链结尾都不存在大于x的数,则新开一个反链,否则将x拼到第一个大于x的反链末尾if (pos == -1) {f[idx++] = x;} else {f[pos] = x;}}return idx;}// 二分找到f中的第一个大于t的数的位置,若不存在则返回-1private int binarySearch(int[] f, int idx, int t) {int l = 0, r = idx - 1;while (l < r) {int m = l + (r - l >> 1);if (f[m] > t) {r = m;} else {l = m + 1;}}return f[l] > t ? l : -1;}
}
时间复杂度O(nlogn)O(n\log n)O(nlogn),空间O(n)O(n)O(n)。
【Lintcode】1645. Least Subsequences相关推荐
- 【LintCode】算法题 1443. 最长AB子串
描述 给你一个只由字母'A'和'B'组成的字符串s,找一个最长的子串,要求这个子串里面'A'和'B'的数目相等,输出该子串的长度. 这个子串可以为空. s的长度n满足 2<=n<=1000 ...
- 【lintcode】树形数据结构之Maxtree, Tree iterator, remove bst node, 优先队列之动态中位数Median, 矩阵dfs之word search II,最大连
解析 max ksubarray sum: 最大和 of 连续子序列 => 最大和 of k份连续子序列 属于dp,30行代码搞定,注意一些边界. substr diff: 无queu ...
- 【leetcode】940. Distinct Subsequences II
题目如下: Given a string S, count the number of distinct, non-empty subsequences of S . Since the result ...
- 【Lintcode】1413. Tree
题目地址: https://www.lintcode.com/problem/tree/description 给定一个无向图,其有树性质,并且顶点编号为111的为树根(也就是这个图是个有根树).要求 ...
- 【Lintcode】1375. Substring With At Least K Distinct Characters
题目地址: https://www.lintcode.com/problem/substring-with-at-least-k-distinct-characters/description 给定一 ...
- 【Lintcode】1647. Path Search
题目地址: https://www.lintcode.com/problem/path-search/description 给定一个无向图,再给定两个点SSS和TTT,求所有SSS到TTT的简单路径 ...
- 【Lintcode】1799. Sequence Summation
题目地址: https://www.lintcode.com/problem/sequence-summation/description 给定三个数i,j,ki,j,ki,j,k,计算i+(i+1) ...
- 【Lintcode】1367. Police Distance
题目地址: https://www.lintcode.com/problem/1367/ 给定一个m×nm\times nm×n的矩阵,只含1,0,−11,0,-11,0,−1,111表示警察,000 ...
- 【Lintcode】444. Graph Valid Tree II
题目地址: https://www.lintcode.com/problem/444/ 要求设计一个数据结构,可以做如下两个操作: 1.void addEdge(int a, int b)在aaa与b ...
最新文章
- mysql5.5数据库名_mysql5.5数据库名
- 菜鸟学exchange之一:exchange邮件系统的简单安装和部署
- [POJ2184] Cow Exhibition
- win7装64位oracle和PLSQL
- C# App.config全攻略
- python 空指针_Python ctypes模块:扩展指针数组时进行NULL指针访问
- acm常见算法及例题
- 4.Transfer Learning
- js动态添加控件服务器响应,JS实现动态给标签控件添加事件的方法示例
- jeecg 3.5.2 新版本4种首页风格 【经典风格,shortcut风格,ACE bootstrap风格,云桌面风格】
- 生成报告配置xml_升职加薪利器:Python+Pytest框架在Jenkins上生成Allure测试报告
- 获取Windows mobile 开始菜单里最近打开的程序(Recent Programs)
- java事务的类型——面试被问到
- Stencil Buffer(模板缓冲区)
- SaltStack Runners
- 关于在IE浏览器下按钮或者链接点击时出现outline的解决方法
- html如何让图片边缘模糊化,CSS3对图片照片进行边缘模糊处理
- latex设置times new roman新罗马字体
- 恺撒密码算法 Java实现
- php在线留言,PHP在线提交留言直接发到邮箱
热门文章
- DHCP:(2)思科交换机上部署DHCP服务以及DHCP中继功能
- 【Pytorch】计算矩阵中向量之间的两两相似性
- 【VMCloud云平台】SCAP(三)初始配置
- org.jboss.deployment.DeploymentException: URL file:/X:/workspace/JBoss_AS/XXX.war/ deployment failed
- 使用最大似然估计对NFL球队排名
- windwos分区合并磁盘
- C#访问大华网络摄像头
- leetcode 1217. Play with Chips
- Kettle: 合并记录
- 使用XPath的爬虫