Leetcode 300. Longest Increasing Subsequence
来源:互联网 发布:贵州省人口老龄化数据 编辑:程序博客网 时间:2024/05/29 02:34
Given an unsorted array of integers, find the length of longest increasing subsequence.
For example,
Given [10, 9, 2, 5, 3, 7, 101, 18]
,
The longest increasing subsequence is [2, 3, 7, 101]
, therefore the length is 4
. Note that there may be more than one LIS combination, it is only necessary for you to return the length.
Your algorithm should run in O(n2) complexity.
O (n ^ 2)
public int lengthOfLIS(int[] nums) { if (nums == null || nums.length == 0) return 0; int[] dp = new int[nums.length]; int max = 1; Arrays.fill(dp, 1); for (int i = 1; i < nums.length; i++) { for (int j = 0; j < i; j++) { if (nums[j] < nums[i]) { dp[i] = Math.max(dp[j] + 1, dp[i]); max = Math.max(max, dp[i]); } } } return max; }
O (n log n)
public int lengthOfLIS(int[] nums) { int[] dp = new int[nums.length]; int len = 0; for (int num : nums) { int i = Arrays.binarySearch(dp, 0, len, num); if (i < 0) { i = -(i + 1); } dp[i] = num; if (i == len) { len++; } } return len; }
public static int binarySearch(int[] a, int fromIndex, int toIndex, int key)
Searches a range of the specified array of ints for the specified value using the binary search algorithm. The range must be sorted (as by the
sort(int[], int, int)
method) prior to making this call. If it is not sorted, the results are undefined. If the range contains multiple elements with the specified value, there is no guarantee which one will be found.- Parameters:
a
- the array to be searchedfromIndex
- the index of the first element (inclusive) to be searchedtoIndex
- the index of the last element (exclusive) to be searchedkey
- the value to be searched for- Returns:
- index of the search key, if it is contained in the array within the specified range; otherwise, (-(insertion point) - 1). The insertion point is defined as the point at which the key would be inserted into the array: the index of the first element in the range greater than the key, or toIndex if all elements in the range are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.
阅读全文
0 1
- Leetcode-300.Longest Increasing Subsequence
- 300. Longest Increasing Subsequence LeetCode
- [leetcode] 300. Longest Increasing Subsequence
- leetcode 300. Longest Increasing Subsequence
- LeetCode *** 300. Longest Increasing Subsequence
- LeetCode 300. Longest Increasing Subsequence
- LeetCode-300.Longest Increasing Subsequence
- [leetcode] 300. Longest Increasing Subsequence
- leetcode 300. Longest Increasing Subsequence
- LeetCode 300. Longest Increasing Subsequence
- leetcode 300. Longest Increasing Subsequence
- [LeetCode]--300. Longest Increasing Subsequence
- [leetcode] 300. Longest Increasing Subsequence
- LeetCode 300. Longest Increasing Subsequence
- 【LeetCode】300. Longest Increasing Subsequence
- 【LeetCode】300. Longest Increasing Subsequence
- Leetcode 300. Longest Increasing Subsequence
- 【LeetCode】300. Longest Increasing Subsequence
- 搜索专题: HDU1027Ignatius and the Princess II
- .gitignore文件配置和无效解决办法
- 史上最简单的git教程
- css_day05_margin,line-height相关小知识点
- Knight Tournament
- Leetcode 300. Longest Increasing Subsequence
- Linux下QT中执行shell命令
- 多属性决策
- 监督学习与无监督学习
- 动态代码创建布局
- 排序算法java实现
- 软件测试专用名词
- LeetCode
- 数学-初等-完全平方(和)差公式