本题使用回溯法,深度优先搜索。使用隐式条件来进行加速。

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的更多相关文章

  1. [Swift]LeetCode300. 最长上升子序列 | Longest Increasing Subsequence

    Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...

  2. LeetCode-300.Longst Increasing Subsequence

    Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...

  3. leetcode300. Longest Increasing Subsequence 最长递增子序列 、674. Longest Continuous Increasing Subsequence

    Longest Increasing Subsequence 最长递增子序列 子序列不是数组中连续的数. dp表达的意思是以i结尾的最长子序列,而不是前i个数字的最长子序列. 初始化是dp所有的都为1 ...

  4. LeetCode300. Longest Increasing Subsequence

    Description Given an unsorted array of integers, find the length of longest increasing subsequence. ...

  5. LeetCode--300. 最长递增子序列

    题目:给定一个无序的整数数组,找到其中最长上升子序列的长度. 示例: 输入: [10,9,2,5,3,7,101,18] 输出: 4 解释: 最长的上升子序列是 [2,3,7,101],它的长度是 4 ...

  6. Leetcode300. Longest Increasing Subsequence最长上升子序列

    给定一个无序的整数数组,找到其中最长上升子序列的长度. 示例: 输入: [10,9,2,5,3,7,101,18] 输出: 4 解释: 最长的上升子序列是 [2,3,7,101],它的长度是 4. 说 ...

  7. leetcode & lintcode for bug-free

    刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...

  8. leetcode 78,236,300

    ---恢复内容开始--- 2018.3.16目前已刷27题,打卡记录有意思的题目. leetcode78 subsets 思路1:DFS遍历子集,每遇到一个数就把该数加上原来的子集变成新的子集. cl ...

  9. leetcode & lintcode 题解

    刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...

随机推荐

  1. 发布-订阅消息系统Kafka简介

    转载请注明出处:http://www.cnblogs.com/BYRans/ Kafka是由Apache软件基金会开发的一个开源流处理平台,由Scala和Java编写.Kafka是一种高吞吐量的分布式 ...

  2. openFileDialog的使用

    这两天应用了一下openFileDialog,做的是上传的功能,在打开页面的时候进行的一系列操作虽说远远没有asp.net的上传控件好使,但是学习起来也是蛮还用的,下面是一个简单的应用 //点击浏览按 ...

  3. 剑指Offer 18. 二叉树的镜像 (二叉树)

    题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 输入描述: 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ ...

  4. jQuery-3.事件篇---表单事件

    jQuery表单事件之blur与focus事件 在之前2.8与2.9节我们学过了表单处理事件focusin事件与focusout事件,同样用于处理表单焦点的事件还有blur与focus事件 它们之间的 ...

  5. Python全栈之路----函数----嵌套函数

    函数内部可以再次定义函数 要执行函数,必须调用 def func1(): print('alex') def func2(): print('eric') func2() #如果没有这一句,不会pri ...

  6. 什么是node

    node 编辑 锁定讨论999 本词条缺少概述图,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧!   node(结点):网络连接的端点,或两条(或多条)线路的连接点.结点可以是处理器.控制器或 ...

  7. 隔离级别简介 (mysql)

    隔离级别:一个事务必须与由其他事务进行的资源或数据更改相隔离的程度.隔离级别从允许的并发副作用(例如,脏读或虚拟读取)的角度进行描述. 定义 编辑 事务指定一个隔离级别,该隔离级别定义一个事务必须与由 ...

  8. Eclipse 使用 VS Emulator for android 调试环境配置 步骤

    模拟器启动器地址:C:\Program Files (x86)\Microsoft Emulator Manager\1.0\emulatorcmd.exe 获取模拟器ID命令:emulatorcmd ...

  9. 【SpringBoot】搜索框架ElasticSearch介绍和整合SpringBoot

    ========================12章 搜索框架ElasticSearch介绍和整合SpringBoot ============================= 加入小D课堂技术交 ...

  10. python中的copy.copy和copy.deepcopy

    一个例子就搞清楚 import copy a = [1, 2, 3, 4, ['a', 'b']] #原始对象 b = a #赋值,传对象的引用 c = copy.copy(a) #对象拷贝,浅拷贝 ...