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

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. centos 升级python3

    升级pip3 wget --no-check-certificate https://bootstrap.pypa.io/get-pip.py 升级python3 yum install epel-r ...

  2. 自动化测试-6.selenium的css定位

    前言 大部分人在使用selenium定位元素时,用的是xpath定位,因为xpath基本能解决定位的需求.css定位往往被忽略掉了,其实css定位也有它的价值,css定位更快,语法更简洁.这一篇css ...

  3. keras中VGG19预训练模型的使用

    keras提供了VGG19在ImageNet上的预训练权重模型文件,其他可用的模型还有VGG16.Xception.ResNet50.InceptionV3 4个. VGG19在keras中的定义: ...

  4. controller向layout传值

    Yii2,layout中使用Controller的值,Controller向layout传值的两种方式. yii2中在通过Controller向layout中传值,layout中访问Controlle ...

  5. scrapy框架之递归解析和post请求

    递归爬取解析多页页面数据 scrapy核心组件工作流程 scrapy的post请求发送 1.递归爬取解析多页页面数据 - 需求:将糗事百科所有页码的作者和段子内容数据进行爬取切持久化存储 - 需求分析 ...

  6. 通过位运算求两个数的和(求解leetcode:371. Sum of Two Integers)

    昨天在leetcode做题的时候做到了371,原题是这样的: 371. Sum of Two Integers Calculate the sum of two integers a and b, b ...

  7. spring中@Value("${key}")值原样输出${key}分析与解决

    问题: 最近发现一个项目中,在类中通过@Value("${key}")获取配置文件中变量值突然不行了,直接输出${key},示例代码如下: java类中: import org.s ...

  8. icomoon:生成字体图标的方法并应用

    字体图标任意缩放不会失真,也大大减少请求数量,非常好用. 在线生成工具:https://icomoon.io/app/#/select 在线SVG图库(阿里),  用于导入:http://www.ic ...

  9. [转]c#快捷键

    c#快捷键(成为高手必备) CTRL + SHIFT + B生成解决方案 CTRL + F7 生成编译 CTRL + O 打开文件 CTRL + SHIFT + O打开项目 CTRL + SHIFT ...

  10. eclipse开启时报错问题

    eclipse启动时报如下错误: Unable to read workbench state.Workbench UI layout will be reset 不能找到正式的工作台,工作台UI的布 ...