Leetcode刷题笔记题解(C++):小红书. 倒卖战利品

Leetcode刷题笔记题解(C++):小红书. 倒卖战利品_第1张图片

Leetcode刷题笔记题解(C++):小红书. 倒卖战利品_第2张图片

 

讲一下思路吧,

把宝物作为一个结构体,含有x和h属性,将结构体数组依x按从小到大进行排序,如果x相等,则y小的靠前 一点,然后完成了排序。

接着在y排序中寻找最长递增的序列长度。(题目意思可能是没有两个x,h都相等的宝物,如果有还要多考虑一下)

代码如下:

#include
#include
#include
using namespace std;
int max_length(vector &vec);
struct baowu{
    int x;
    int h;
};
//结构体数组排序的依据
bool sortall(baowu &b1,baowu &b2){
    if(b1.x==b2.x) return b1.h>n;
    //存放结构体数组
    vectortemp(n);
    for(int i=0;i>temp[i].x>>temp[i].h;
    }
    //对结构体数组进行排序
    sort(temp.begin(), temp.end(),sortall);
    //单独拿出来排序之后的y属性
    vectorli(n);
    for(int i=0;i &vec){
    int length=vec.size();
    if(length<=1){
        return length;
    }
    vectortemp(length);
    temp[0]=vec[0];
    int end=0;
    for(int i=1;itemp[end]){
            end++;
            temp[end]=vec[i];
        }else{
            int left=0,right=end;
            while(left> 1);
                if (temp[mid] < vec[i]) {
                    left = mid + 1;
                } else {
                    right = mid;
                }
            }
            temp[left]=vec[i];
        }
    }
    end++;
    return end;
}

 

你可能感兴趣的:(Leetcode算法题解,c++,算法,排序算法,数据结构)