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;
    }
};

 

全部评论

相关推荐

Southyeung:我说一下我的看法(有冒犯实属抱歉):(1)简历不太美观,给我一种看都不想看的感觉,感觉字体还是排版问题;(2)numpy就一个基础包,机器学习算法是什么鬼?我感觉你把svm那些写上去都要好一点。(2)课程不要写,没人看,换成获奖经历;(3)项目太少了,至少2-3个,是在不行把网上学习的也写上去。
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
06-27 20:15
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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