leetcode300. Longest Increasing Subsequence

https://leetcode.com/problems/longest-increasing-subsequence/

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

Example:

Input: [10,9,2,5,3,7,101,18]
Output: 4 
Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4. 

Note:

  • 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.

nlogn的最长上升子序列~~

二分找的是大于等于的最小值(这个具体看题目意思~)

终于自己写对二分啦

class Solution {
public:
    int s[100000];
    int bi_search(int x, int l, int r) {
        while(l < r) {
            int mid = (l + r)/2;
            //printf("l=%d,r=%d,mid=%d,s[mid]=%d\n",l,r,mid,s[mid]);
            if(s[mid] >= x)
                r = mid;
            else
                l = mid + 1;
        }
        return r;
    }
    int lengthOfLIS(vector<int>& nums) {
        if (nums.size() == 0)
            return 0;
        int tot = 0;
        s[tot] = nums[0];
        for (int i = 1; i < nums.size(); i ++) {
            if (nums[i] > s[tot]) {
                s[++tot] = nums[i];
            } else {
                int pos = bi_search(nums[i], 0, tot);//lower_bound(s, s+tot, nums[i])-s;
                //printf("pos=%d,tot=%d,i=%d,nums[i]=%d\n",pos,tot,i,nums[i]);
                s[pos] = nums[i];
            }
        }
        return tot+1;
    }
};

 

全部评论

相关推荐

点赞 评论 收藏
分享
05-12 17:00
门头沟学院 Java
king122:你的项目描述至少要分点呀,要实习的话,你的描述可以使用什么技术,实现了什么难点,达成了哪些数字指标,这个数字指标尽量是真实的,这样面试应该会多很多,就这样自己包装一下,包装不好可以找我,我有几个大厂最近做过的实习项目也可以包装一下
点赞 评论 收藏
分享
06-26 10:08
门头沟学院 C++
北京Golang实习,一个月4700,吃住都不报,公司位置在海淀。请问友友怎么看呢?如果要租房的话有什么建议吗
码农索隆:租房肯定是合租了,剩下的钱,差不多够正常吃饭了,看看能不能学到东西吧
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务