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

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. 在myeclipse中使用./和../遇到的问题

    今天用ajax验证的时候,ajax的代码一直不起作用,我在浏览器里打开了开发者模式,错误的原因是找不到"jquery-1.8.3.min.js",但是我的目录结构都没有问题. &l ...

  2. C++内存管理-new,delete,new[],placement new的简单使用

    技术在于交流.沟通,本文为博主原创文章转载请注明出处并保持作品的完整性 首先,我们先看一下C++应用程序,使用memory的途径如下图所示 C++应用程序中申请内存基于分配器的实现(std::allo ...

  3. c++中关于预编译头的设置问题

    在运行代码时会遇到缺少预编译pch.c 或者stadfx.h之类的, 这个时候,先查看有没有包含, 然后看一下预编译头设置中, 是否正确设置.

  4. 团队项目(MVP------新能源汽车无线充电管理网站)(个人任务1)

    个人任务:1.设计问卷调查了解电动车目前的市场需求情况 2.收集问卷,并且进行总结和分析 3.后台管理系统界面的登录和注册界面的编写(主要用到html,css,javascript,其中用户的合法性检 ...

  5. sed语法2

    sed命令是一个面向字符流的非交互式编辑器,也就是说sed不允许用户与它进行交互操作.sed是按行来处理文本内容的.在shell中,使用sed来批量修改文本内容是非常方便的. sed命令的选项 sed ...

  6. CreateThread给线程函数传递的参数

      HANDLE WINAPI CreateThread ( __in_opt LPSECURITY_ATTRIBUTES lpThreadAttributes, // 指向SECURITY_ATTR ...

  7. Chromium Settings页面修改

    /********************************************************************** * Chromium Settings页面修改 * 说明 ...

  8. 群等变网络的pytorch实现

    CNN对于旋转不具有等变性,对于平移有等变性,data augmentation的提出就是为了解决这个问题,但是data augmentation需要很大的模型容量,更多的迭代次数才能够在训练数据集合 ...

  9. 转发 C# Win32 API程序控制鼠标的操作

    命名空间:using System.Runtime.InteropServices; 在程序中添加: [DllImport("User32")]public extern stat ...

  10. PTA——洗牌

    PTA 7-43 Shuffling Machine #include<stdio.h> int main() { int i,n,*result; scanf("%d" ...