leetcode300
本题使用回溯法,深度优先搜索。使用隐式条件来进行加速。
public class Solution
{
int bestp = ;
int[] x;
Dictionary<int, int> dic = new Dictionary<int, int>(); void Backtrack(int[] nums, int t)
{
if (t >= nums.Length)
{
var sum = ;
for (int i = ; i < nums.Length; i++)
{
if (x[i] == )
{
//Console.Write(nums[i]+" ");
sum++;
}
}
//Console.WriteLine();
bestp = Math.Max(bestp, sum);
return;
} if (dic.Count == || dic.LastOrDefault().Value < nums[t])
{
x[t] = ;
dic.Add(t, nums[t]);
Backtrack(nums, t + );
dic.Remove(t);
}
if (dic.Count + nums.Length - (t + ) > bestp)
{
x[t] = ;
Backtrack(nums, t + );
}
} public int LengthOfLIS(int[] nums)
{
if (nums.Length < )
{
return nums.Length;
} x = new int[nums.Length];
Backtrack(nums, ); return bestp;
}
}
补充一个使用动态规划的方法,使用python实现,但是效率不是很高:
class Solution:
def lengthOfLIS(self, nums: 'List[int]') -> 'int':
n = len(nums)
if n==0:
return 0
maxnum = 0
dp = [1] * n
for i in range(n):
for j in range(i):
if nums[i] > nums[j]:
dp[i] = max(dp[i],dp[j] + 1)
print(dp[i])
maxnum = max(maxnum,dp[i])
return maxnum
思路分析:双层循环,时间复杂度是O(n^2)。
dp[i]表示在nums中,以nums[i]为结尾的自增子序列的长度。
第13行是在外层循环,每次循环结束的时候更新,全局的最长自增子序列的长度,也就是所求。
内层循环,是从当前位置i,向前寻找[0,i-1]闭区间。如果在nums中,i前面有一个元素j,满足nums[i] > nums[j],则可以在以j为结尾的自增子序列上,增加1的长度,构成新的自增子序列,而dp[i]只保存这些可能构成的新自增子序列中最大的长度。
补充一个java的实现,使用二分查找加速查询,提升效率
class Solution {
public int lengthOfLIS(int[] nums) {
int n = nums.length;
int[] tails = new int[n];
int len = ;
for (int num : nums) {
int index = binarySearch(tails, len, num);
tails[index] = num;
if (index == len) {
len++;
}
}
return len;
}
private int binarySearch(int[] tails, int len, int key) {
int l = , h = len;
while (l < h) {
int mid = l + (h - l) / ;
if (tails[mid] == key) {
return mid;
} else if (tails[mid] > key) {
h = mid;
} else {
l = mid + ;
}
}
return l;
}
}

leetcode300的更多相关文章
- [Swift]LeetCode300. 最长上升子序列 | Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...
- LeetCode-300.Longst Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...
- leetcode300. Longest Increasing Subsequence 最长递增子序列 、674. Longest Continuous Increasing Subsequence
Longest Increasing Subsequence 最长递增子序列 子序列不是数组中连续的数. dp表达的意思是以i结尾的最长子序列,而不是前i个数字的最长子序列. 初始化是dp所有的都为1 ...
- LeetCode300. Longest Increasing Subsequence
Description Given an unsorted array of integers, find the length of longest increasing subsequence. ...
- LeetCode--300. 最长递增子序列
题目:给定一个无序的整数数组,找到其中最长上升子序列的长度. 示例: 输入: [10,9,2,5,3,7,101,18] 输出: 4 解释: 最长的上升子序列是 [2,3,7,101],它的长度是 4 ...
- Leetcode300. Longest Increasing Subsequence最长上升子序列
给定一个无序的整数数组,找到其中最长上升子序列的长度. 示例: 输入: [10,9,2,5,3,7,101,18] 输出: 4 解释: 最长的上升子序列是 [2,3,7,101],它的长度是 4. 说 ...
- leetcode & lintcode for bug-free
刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...
- leetcode 78,236,300
---恢复内容开始--- 2018.3.16目前已刷27题,打卡记录有意思的题目. leetcode78 subsets 思路1:DFS遍历子集,每遇到一个数就把该数加上原来的子集变成新的子集. cl ...
- leetcode & lintcode 题解
刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...
随机推荐
- SharePoint REST API - 使用REST接口对列表设置自定义权限
博客地址:http://blog.csdn.net/FoxDave SharePoint网站.列表和列表项都属于SecurableObject类型.默认情况下,一个安全对象继承父级的权限.对一个对 ...
- 测试同学必备抓包工具--charles之mock数据
charles中有三个是我经常用到来mock数据的. 一. 打断点--Breakpoints 1. 先切换查看‘Structure’模式 2. 找到目标链接,对其父级进行打断点,如图 3. 取消掉再次 ...
- 测试同学必备抓包工具--charles之安装
1,下载charles,官网:https://www.charlesproxy.com/ 2,下载完成,先试着用一下,网址访问百度看看... 注意,windows proxy如果勾选,则代表可以抓取网 ...
- jQuery-1.样式篇
jQuery对象与DOM对象 对于才开始接触jQuery库的初学者,我们需要清楚认识一点: jQuery对象与DOM对象是不一样的 可能一时半会分不清楚哪些是jQuery对象,哪些是DOM对象,下面重 ...
- kettle在本地执行向远程hdfs执行转换错误"Couldn't open file hdfs"
kettle在本地执行向远程hdfs执行转换时,会出现以下错误: ToHDFS.0 - ERROR (version 7.1.0.0-12, build 1 from 2017-05-16 17.18 ...
- Python全栈之路----常用模块----datetime模块详解
相比于time模块,datetime模块的接口则更直观,更容易调用. datetime模块定义了下面这几个类: datetime.date:表示日期的类,常用的属性有year,month,day: d ...
- hdu5003 Osu!排序实现水题
Osu! is a famous music game that attracts a lot of people. In osu!, there is a performance scoring s ...
- spring IOC 和AOP 方面
spring 的2大核心 是Ioc 和 aop spring的依赖注入:在程序运行期间,由外部容器动态的将依赖对象注入到组件中 IOC: 实例化spring容器的二种方法 第一种:在类路径下寻找配 ...
- Java 的 volatile 修饰符
volatile 修饰符,用于多线程同步 volatile 修饰的成员变量在每次被线程访问时,都强制从共享内存中重新读取该成员变量的值.而且,当成员变量发生变化时,会强制线程将变化值回写到共享内存.这 ...
- Spring生态研习【二】:SpEL(Spring Expression Language)
1. SpEL功能简介 它是spring生态里面的一个功能强大的描述语言,支在在运行期间对象图里面的数据查询和数据操作.语法和标准的EL一样,但是支持一些额外的功能特性,最显著的就是方法调用以及基本字 ...