LeetCode题解-05(搜索、贪心)


目录
  • LeetCode题解
    • chap-13:搜索
      • section13-1: DFS
        • 1、电话号码的字母组合
        • 2、括号生成
        • 3、解数独
        • 4、组合总和
        • 5、组合总和 II【×】
        • 6、组合总和 III
        • 7、全排列
        • 8、全排列 II
        • 9、N 皇后
        • 10、N皇后 II
        • 11、组合
        • 12、子集【×】
        • 13、子集 II
        • 14、单词搜索
        • 15、复原 IP 地址
        • 16、被围绕的区域
        • 17、克隆图
        • 18、复制带随机指针的链表
        • 19、删除无效的括号【×】
        • 20、字典序排数
        • 21、太平洋大西洋水流问题
        • 22、扁平化多级双向链表【×】
        • 23、岛屿的周长
        • 24、字母大小写全排列【×】
        • 25、将数组拆分成斐波那契序列【×】
      • section13-2: BFS
        • 1、单词接龙
        • 2、单词接龙 II BFS+DFS
        • 3、最小基因变化
        • 4、员工的重要性
    • chap-14:贪心
        • 1、跳跃游戏
        • 2、跳跃游戏 II
        • 3、加油站
        • 4、去除重复字母 栈+贪心
        • 5、按要求补齐数组【×】
        • 6、摆动序列
        • 7、移掉 K 位数字
        • 8、分发饼干
        • 9、IPO
        • 10、种花问题
        • 11、Dota2 参议院
        • 12、分割数组为连续子序列
        • 13、翻转矩阵后的得分
        • 14、重构字符串
        • 15、单调递增的数字
        • 16、柠檬水找零
        • 17、可以到达的最远建筑
        • 18、销售价值减少的颜色球
        • 19、找出最具竞争力的子序列
      • section14-1: 区间贪心
        • 1、无重叠区间【最大不相交区间数量】
        • 2、用最少数量的箭引爆气球【区间选点】
        • 3、视频拼接【区间覆盖】

LeetCode题解

chap-13:搜索

section13-1: DFS

1、电话号码的字母组合

class Solution {
public:
    vector dict = {"","","abc","def","ghi","jkl","mno",
                           "pqrs","tuv","wxyz"};
    vector res;
    vector letterCombinations(string digits) {
        if(digits.empty()) return res;
        dfs(digits,0,"");
        return  res;
    }
    void dfs(string& digits,int u,string path){
        if(u == digits.size()) res.push_back(path);
        else
            for(auto &c:dict[digits[u] - '0']){
                dfs(digits,u+1,path + c);
            }
    }
};

2、括号生成

class Solution {
public:
    vector res;
    vector generateParenthesis(int n) {
        dfs(n,0,0,"");
        return res;
    }
    void dfs(int n,int lc, int rc, string s){
        if(lc == n && rc == n) {res.push_back(s);return;}
        if(lc < n) dfs(n,lc+1,rc,s+'(');
        if(rc < lc && rc < n) dfs(n,lc,rc+1,s+')');
    }
};

3、解数独

class Solution {
public:
    bool col[9][9];
    bool row[9][9];
    bool cell[3][3][9];
    void solveSudoku(vector>& board) {
        memset(row, 0, sizeof row);
        memset(col, 0, sizeof col);
        memset(cell, 0, sizeof cell);
        for (int i = 0; i < 9; i ++ )
            for (int j = 0; j < 9; j ++ )
                if (board[i][j] != '.') {
                    int t = board[i][j] - '1';
                    row[i][t] = col[j][t] = cell[i / 3][j / 3][t] = true;
                }

        dfs(board,0,0);
    }
    bool dfs(vector>&board,int x,int y){
        if (y == 9) x ++, y = 0;
        if (x == 9) return true;
        if (board[x][y] != '.') return dfs(board, x, y + 1);
        for (int i = 0; i < 9; i ++ )
            if (!row[x][i] && !col[y][i] && !cell[x / 3][y / 3][i]) {
                board[x][y] = '1' + i;
                row[x][i] = col[y][i] = cell[x / 3][y / 3][i] = true;
                if (dfs(board, x, y + 1)) return true;
                board[x][y] = '.';
                row[x][i] = col[y][i] = cell[x / 3][y / 3][i] = false;
            }

        return false;
    }
};

4、组合总和

class Solution {
public:
    vector> ans;
    vector path;
    vector> combinationSum(vector& cs, int t) {
        dfs(cs,0,t);
        return ans;
    }
    void dfs(vector& cs, int u,int t){
        if(!t) {ans.push_back(path); return;}
        for(int i=u;i

5、组合总和 II【×】

class Solution {
public:
    vector> ans;
    vector path;
    vector> combinationSum2(vector& cs, int t) {
        sort(cs.begin(), cs.end());
        dfs(cs,0,t);
        return ans;
    }
    void dfs(vector& cs,int u,int t){
        if(t==0) {ans.push_back(path); return;}
        if(u == cs.size()) return;
        int len = u;
        while(len> ans;
    vector path;
    vector> combinationSum2(vector& cs, int t) {
        sort(cs.begin(),cs.end());
        dfs(cs,0,t);
        return ans;
    }

    void dfs(vector& cs,int u,int t){
        if(!t){ans.push_back(path); return;}
        if(u == cs.size()) return;
        for(int i=u;i= 0){
                if(i>u && cs[i-1] == cs[i]) continue; // 去重
                path.push_back(cs[i]);
                dfs(cs,i+1,t-cs[i]);
                path.pop_back();
            }
        }
    }
};

6、组合总和 III

class Solution {
public:
    vector> ans;
    vector path;
    int cs[9] = {1,2,3,4,5,6,7,8,9};
    int k;
    vector> combinationSum3(int k_, int t) {
        k = k_;
        dfs(0,0,t);
        return ans;
    }
    void dfs(int start,int u,int t){
        if(!t && u == k) {ans.push_back(path); return;}
        if(start > 9) return;
        for(int i=start;i<9;i++){
            if(t-cs[i] >= 0){
                path.push_back(cs[i]);
                dfs(i+1,u+1,t-cs[i]);
                path.pop_back();
            }
        }
    }
};

7、全排列

class Solution {
public:
    vector> ans;
    vector path;
    vector f;
    vector> permute(vector& nums) {
        f = vector(nums.size(),false);
        dfs(nums);
        return ans;
    }
    void dfs(vector&nums){
        if(path.size() == nums.size()) {
            ans.push_back(path); return;
        }
        for(int i=0;i

8、全排列 II

class Solution {
public:
    vector> ans;
    vector path;
    vector f;
    vector> permuteUnique(vector& nums) {
        f = vector(nums.size(),false);
        path = vector(nums.size());
        sort(nums.begin(),nums.end());
        dfs(nums,0);
        return ans;
    }
    void dfs(vector&nums,int idx){
        if(idx == nums.size()) ans.push_back(path);
        else{
            for(int i=0;i

9、N 皇后

class Solution {
public:
    vector path;
    vector> ans;
    vector col;
    vector dig, anti_dig;
    vector> solveNQueens(int n) {
        path = vector(n,string(n,'.'));
        col = vector(n,false);
        dig = anti_dig = vector(2*n,false);
        dfs(0, n);
        return ans;
    }
    void dfs(int u, int n){
        if(u == n) ans.push_back(path);
        else
            for(int i=0;i

10、N皇后 II

class Solution {
public:
    vector col,dig,vdig;
    int res = 0;
    int totalNQueens(int n) {
        col = vector(n);
        dig = vdig = vector(2*n);
        dfs(0,n);
        return res;
    }
    void dfs(int row,int n){
        if(row == n) res+=1;
        else{
            for(int j = 0;j

11、组合

class Solution {
public:
    vector> ans;
    vector path;
    vector> combine(int n, int k) {
        dfs(1,n,k);
        return ans;
    }
    void dfs(int u,int n,int k){
        if(path.size() == k) ans.push_back(path);
        else{
            for(int i=u;i<=n;i++) {
                path.push_back(i);
                dfs(i+1,n,k);
                path.pop_back();
            }
        }
    }
};

12、子集【×】

class Solution {
public:
    vector> ans;
    vector path;
    vector> subsets(vector& nums) {
        dfs(nums, 0);
        return ans;
    }
    void dfs(vector& nums, int u){
        if(u == nums.size()) ans.push_back(path);
        else{
            path.push_back(nums[u]);
            dfs(nums,u+1);
            path.pop_back();
            dfs(nums,u+1);
        }
    }
};

// 二进制位表示 取与不取
class Solution {
public:
    vector> subsets(vector& nums) {
        vector> ans;
        int n = nums.size();
        for(int i=0;i<(1< temp;
            for(int j=0;j>j & 1) temp.push_back(nums[j]);
            ans.push_back(temp);
        }
        return ans;
    }
};

13、子集 II

class Solution {
public:
    vector> ans;
    vector path;
    vector> subsetsWithDup(vector& nums) {
        sort(nums.begin(),nums.end());
        dfs(nums,0);
        return ans;
    }
    void dfs(vector&nums, int u){
        if(u == nums.size()) ans.push_back(path);
        else{
            int k = u;
            while(k> ans;
    vector path;
    unordered_map hash;
    vector> subsetsWithDup(vector& nums) {
        for(auto &a:nums) hash[a]++; // 记录数集总数
        dfs(-10);
        return ans;
    }
    void dfs(int u){
        if(u > 10) ans.push_back(path);
        else{
            for(int i=0;i

14、单词搜索

class Solution {
public:
    string word;
    int col[4] = {-1,0,1,0};
    int row[4] = {0,-1,0,1};
    bool ans = false;
    bool exist(vector>& board, string word_) {
        word = word_;
        for(int x=0;x>&board,int x,int y,int s){
        if(s == word.size()) ans = true;
        else
            for(int i=0;i<4;i++){
                int dx = row[i]+x, dy = col[i]+y;
                if(dx>=0 && dx=0 && dy

15、复原 IP 地址

class Solution {
public:
    vector ans;
    vector path;
    vector restoreIpAddresses(string s) {
        dfs(s,0,0);
        return ans;
    }
    void dfs(string&s,int u,int tot){
        if(u == s.size()){
           if(tot == 4){
               string p;
               for(auto t:path) p+=to_string(t)+'.';
               p.pop_back(); ans.push_back(p);
           }
           return;
        }
        if(tot > 4) return;
        int t = 0;
        for(int i=u;i=0 && t<256){
                path.push_back(t);
                dfs(s,i+1,tot+1);
                path.pop_back();
            }else break;
            if(!t) break;
        }
    }
};

16、被围绕的区域

class Solution {
public:
    vector> board;
    int m,n;
    int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
    void solve(vector>& board1) {
        board = board1;
        n = board.size(); m = board[0].size();
        for(int i = 0;i=0 && x =0 && y

17、克隆图

class Solution {
public:
    unordered_map hash;
    Node* cloneGraph(Node* node) {
        if(!node) return NULL;
        dfs(node);
        for(auto [s,t]:hash){
            for(auto neb:s->neighbors)
                t->neighbors.push_back(hash[neb]);
        }
        return hash[node];
    }
    void dfs(Node* node){
        hash[node] = new Node(node->val);
        for(auto neb:node->neighbors){
            if(!hash.count(neb))
                dfs(neb);
        }
    }
};

18、复制带随机指针的链表

class Solution {
public:
    unordered_map hash;
    Node* copyRandomList(Node* head) {
        if(head == NULL) return NULL;
        dfs(head);
        for(auto [s,d]:hash){
            d->next = s->next ? hash[s->next] : NULL;
            d->random = s->random ? hash[s->random] : NULL;
        }
        return hash[head];
    }
    void dfs(Node* h){
        if(h) {
            hash[h] = new Node(h->val);
            h = h->next;
            dfs(h);
        }
    }
};

19、删除无效的括号【×】

class Solution {
public:
    vector ans;

    vector removeInvalidParentheses(string s) {
        int l = 0, r = 0;
        for (auto x: s)
            if (x == '(') l ++ ;
            else if (x == ')') {
                if (l == 0) r ++ ;
                else l -- ;
            }

        dfs(s, 0, "", 0, l, r);
        return ans;
    }

    void dfs(string& s, int u, string path, int cnt, int l, int r) {
        if (u == s.size()) {
            if (!cnt) ans.push_back(path);
            return;
        }

        if (s[u] != '(' && s[u] != ')') dfs(s, u + 1, path + s[u], cnt, l, r);
        else if (s[u] == '(') {
            int k = u;
            while (k < s.size() && s[k] == '(') k ++ ;
            l -= k - u;
            for (int i = k - u; i >= 0; i -- ) {
                if (l >= 0) dfs(s, k, path, cnt, l, r);
                path += '(';
                cnt ++, l ++ ;
            }
        } else if (s[u] == ')') {
            int k = u;
            while (k < s.size() && s[k] == ')') k ++ ;
            r -= k - u;
            for (int i = k - u; i >= 0; i -- ) {
                if (cnt >= 0 && r >= 0) dfs(s, k, path, cnt, l, r);
                path += ')';
                cnt --, r ++ ;
            }
        }
    }
};

20、字典序排数

class Solution {
public:
    vector ans;
    vector lexicalOrder(int n) {
        for(int i=1;i<=9 && i<=n;i++)
            dfs(i,n);
        return ans;
    }
    void dfs(int x,int n){
        if(x <= n){
            ans.push_back(x);
            for(int j=x*10;j<=n && j<=x*10+9;j++)
                dfs(j,n);
        }
    }
};

21、太平洋大西洋水流问题

class Solution {
public:
    int m,n;
    vector> st;
    int dx[4] = {-1,0,1,0}, dy[4] = {0,-1,0,1};
    void dfs(vector>&hs, int x,int y,int t){
        if(st[x][y] & t) return;
        st[x][y]|=t;
        for(int i=0;i<4;i++){
            int a = dx[i]+x, b = dy[i]+y;
            if(a>=0 && a=0 && b=hs[x][y])
                dfs(hs,a,b,t);
        }
    }
    vector> pacificAtlantic(vector>& hs) {
        vector> ans;        
        m = hs.size(), n = hs[0].size();
        if(m == 0 || n == 0) return ans;
        st = vector>(m,vector(n));
        for(int i=0;i

22、扁平化多级双向链表【×】

class Solution {
public:
    Node* flatten(Node* head) {
        auto res = dfs(head);
        return res[0];
    }
    vector dfs(Node* h){
        if(!h) return {NULL, NULL};
        auto cur=h, tail=h;
        while(cur){
            tail = cur;
            if(cur->child){
                auto t = dfs(cur->child);
                cur->child = NULL;
                if(cur->next) cur->next->prev = t[1];
                t[1]->next = cur->next;                
                cur->next = t[0];
                t[0]->prev = cur;
                cur = t[1]->next;
                tail = t[1];
            }else cur = cur->next;
        }
        return {h,tail};
    }
};

23、岛屿的周长

class Solution {
public:
    int m,n;
    int ans=0;
    int dx[4]={-1,0,1,0}, dy[4]={0,-1,0,1};
    int islandPerimeter(vector>& grid) {
        m = grid.size(), n = grid[0].size();
        for(int i=0;i>& grid,int x,int y){
        grid[x][y]=2;
        for(int i=0;i<4;i++){
            int a=dx[i]+x, b=dy[i]+y;
            if(a>=0 && a=0 && b

24、字母大小写全排列【×】

class Solution {
public:
    vector ans;    
    vector letterCasePermutation(string s) {
        dfs(s,0,"");
        return ans;
    }
    void dfs(string& s,int u,string path){
        if(u == s.size()) ans.push_back(path);
        else{
            dfs(s,u+1,path+s[u]);
            if(s[u]>='A'){
                s[u]^=32;
                dfs(s,u+1,path+s[u]);
            }
        }
    }
};

25、将数组拆分成斐波那契序列【×】

typedef long long ll;
class Solution {
public:
    vector get(string& s,ll a,ll b){
        vector res = {(int)a,(int)b};
        string t = to_string(a) + to_string(b);
        while(t.size() < s.size()){
            auto c = a+b;
            if(c > INT_MAX) return{};
            res.push_back(c);
            t+=to_string(c);
            if(t != s.substr(0,t.size()))
                return{};
            a = b; b = c;
        }
        if(t!=s) return{};
        return res;
    }
    vector splitIntoFibonacci(string s) {
        // 逐一枚举前两个数
        for(int i=1;i<=10 && i

[Go Back~](# LeetCode题解)

section13-2: BFS

1、单词接龙

class Solution {
public:
    int ladderLength(string begin, string end, vector& wordList) {
        unordered_set s;
        for(auto word:wordList) s.insert(word);
        if(!s.count(end)) return 0;
        unordered_map dist;
        dist[begin] = 1;
        queue q;
        q.push(begin);
        while(q.size()){
            string t = q.front();
            string r = t;
            q.pop();
            for(int i =0 ;i

2、单词接龙 II BFS+DFS

class Solution {
public:
    unordered_set S;
    unordered_map dist;
    queue q;
    vector> ans;
    vector path;
    string beginWord;

    vector> findLadders(string _beginWord, string endWord, vector& wordList) {
        for (auto word: wordList) S.insert(word);
        beginWord = _beginWord;
        dist[beginWord] = 0;
        q.push(beginWord);
        while (q.size()) {
            auto t = q.front();
            q.pop();
            string r = t;
            for (int i = 0; i < t.size(); i ++ ) {
                t = r;
                for (char j = 'a'; j <= 'z'; j ++ ) {
                    t[i] = j;
                    if (S.count(t) && dist.count(t) == 0) {
                        dist[t] = dist[r] + 1;
                        q.push(t);
                    }
                }
            }
        }
        if (dist.count(endWord) == 0) return ans;
        path.push_back(endWord);
        dfs(endWord);
        return ans;
    }

    void dfs(string t) {
        if (t == beginWord) {
            reverse(path.begin(), path.end());
            ans.push_back(path);
            reverse(path.begin(), path.end());
        } else {
            string r = t;
            for (int i = 0; i < t.size(); i ++ ) {
                t = r;
                for (char j = 'a'; j <= 'z'; j ++ ) {
                    t[i] = j;
                    if (dist.count(t) && dist[t] + 1 == dist[r]) {
                        path.push_back(t);
                        dfs(t);
                        path.pop_back();
                    }
                }
            }
        }
    }
};

3、最小基因变化

class Solution {
public:
    int minMutation(string start, string end, vector& bank) {
        unordered_set S(bank.begin(),bank.end());
        char help[4] = {'A','C','G','T'};
        unordered_map hash;
        queue q;
        hash[start] = 1;
        q.push(start);
        while(q.size()){
            auto t = q.front(); q.pop();
            for(int i=0;i

4、员工的重要性

class Solution {
public:
    unordered_map hash;
    unordered_set ids;
    int ans;
    int getImportance(vector employees, int id) {
        for(auto t:employees) hash[t->id] = t; 
        ans = 0;
        dfs(hash[id]);
        for(auto t:ids)
            ans+=hash[t]->importance;
        return ans;
    }
    void dfs(Employee* p){
        ids.insert(p->id);
        for(auto id:p->subordinates)
            if(ids.count(id) == 0)
                dfs(hash[id]);
    }
};

[Go Back~](# LeetCode题解)

chap-14:贪心

1、跳跃游戏

class Solution {
public:
    bool canJump(vector& nums) {
        int idx=nums[0];
        for(int i=1;i idx) return false;
            idx = max(idx,i+nums[i]);
        }
        return true;
    }
};

2、跳跃游戏 II

class Solution {
public:
    int jump(vector& nums) {
        int ans=0;
        for(int i=0,tot=0,next=0;i=nums.size()-1) break;
            }
        }
        return ans;
    }
};

// acwing参考
class Solution {
public:
    int jump(vector& nums) {
        int ans = 0, cur = 0, dis = 0;
        while (dis < (int)nums.size() - 1) {
            int next = 0;
            while (cur <= dis) {
                next = max(next, cur + nums[cur]);
                cur++;
            }
            ans++;
            dis = next;
        }
        return ans;
    }
};

3、加油站

class Solution {
public:
    int canCompleteCircuit(vector& gas, vector& cost) {
        int start=0, used=0, rest=0;
        for(int i=0;i=0?start:-1;
    }
};

4、去除重复字母 栈+贪心

class Solution {
public:
    string removeDuplicateLetters(string s) {
        string ans;
        unordered_map f;
        unordered_map last;
        for(int i=0;i s[i] && last[ans.back()] > i){
                f[ans.back()] = false;
                ans.pop_back();                
            }
            ans+=s[i];
            f[s[i]] = true;
        }
        return ans;
    }
};

5、按要求补齐数组【×】

class Solution {
public:
    int minPatches(vector& nums, int n) {
        int ans=0,cur=0;
        long long miss=1;
        while(miss<=n){
            if(cur

6、摆动序列

class Solution {
public:
    int wiggleMaxLength(vector& nums) {
        // 注意第一个点的处理
        int ans=1, flag=0, t=nums[0];
        for(int i=1;i=0){
                flag = -1;
                ans++;
            }else if(nums[i]-t>0 && flag <=0){
                flag = 1;
                ans++;
            }
            t = nums[i];
        }
        return ans;
    }
};

7、移掉 K 位数字

class Solution {
public:
    string removeKdigits(string num, int k) {
        if(k == num.size()) return "0";
        string ans;
        int i=0;
        while(i num[i] && k>0){
                ans.pop_back();
                k--;
            }
            ans.push_back(num[i++]);            
        }
        while(k--) ans.pop_back();
        k=0; while(k

8、分发饼干

class Solution {
public:
    int findContentChildren(vector& g, vector& s) {
        int ans=0;
        sort(g.begin(),g.end());
        sort(s.begin(),s.end());
        for(int i=0,j=0;i

9、IPO

class Solution {
public:
    int findMaximizedCapital(int k, int w, vector& p, vector& c) {
        // 贪心+大根堆
        vector> vp;
        for(int i=0;i q;
        int cur=0;
        while(k--){
            while(cur

10、种花问题

class Solution {
public:
    bool canPlaceFlowers(vector& f, int n) {
        f.push_back(0);
        for(int i=0;i

11、Dota2 参议院

class Solution {
public:
    string predictPartyVictory(string senate) {
        // 模拟 队列
        queue r,d;
        for(int i=0;i

12、分割数组为连续子序列

class Solution {
public:
    bool isPossible(vector& nums) {
        unordered_map cnt;
        unordered_map chain;
        for(auto num:nums) cnt[num]++;
        for(auto num:nums){
            if(!cnt[num]) continue;
            if(chain[num-1]>0){
                cnt[num]--;
                chain[num-1]--; chain[num]++;
            }else if(cnt[num+1]>0 && cnt[num+2]>0){
                cnt[num]--;
                chain[num+2]++; 
                cnt[num+1]--; cnt[num+2]--;
            }else return false;
        }
        return true;
    }
};

// 官方思路
class Solution {
public:
    bool isPossible(vector& nums) {
        // 小堆根+贪心
        unordered_map,greater>> hash;
        for(auto t:nums){
            if(hash.count(t-1) && hash[t-1].size()){
                auto q = hash[t-1].top(); hash[t-1].pop();
                hash[t].push(q+1);
            }else hash[t].push(1);
        }
        for(auto &[k,v]:hash){
            if(v.size()){
                if(v.top() < 3) return false;
            }
        }
        return true;
    }
};

13、翻转矩阵后的得分

class Solution {
public:
    int matrixScore(vector>& grid) {
        // 模拟
        int n=grid.size(), m=grid[0].size();
        int ans=0;
        for(int i=0;i

14、重构字符串

class Solution {
public:
    string reorganizeString(string s) {
        int hash[26]={0};
        for(auto c:s) hash[c-'a']++;
        auto cmp = [&](const char &a, const char &b){
            return hash[a-'a'] < hash[b-'a'];
        };
        priority_queue,decltype(cmp)> q(cmp);
        for(int i=0;i<26;i++) {
            if(hash[i] > (s.size()+1)/2) return "";
            else if(hash[i]){
                q.push('a'+i);
            }
        }
        string ans; char t1,t2;
        while(q.size()>1){
            t1 = q.top();q.pop();
            t2 = q.top();q.pop();
            hash[t1-'a']--; hash[t2-'a']--;
            ans+=t1; ans+=t2;
            if(hash[t1-'a']) q.push(t1);
            if(hash[t2-'a']) q.push(t2);
        }
        if(q.size()) ans+=q.top(), q.pop();
        return ans;
    }
};

15、单调递增的数字

class Solution {
public:
    int monotoneIncreasingDigits(int n) {
        int dig[10]={0};
        int cur=0,pos=0,ans=0;
        while(n){dig[cur++] = n%10; n/=10;}
        for(int i=1;idig[i-1]){
                dig[i]--;
                pos=i;
            }
        }
        while(pos) {dig[pos-1]=9; pos--;}
        while(cur) {ans = ans*10 + dig[cur-1]; cur--;}
        return ans;
    }
};

16、柠檬水找零

class Solution {
public:
    bool lemonadeChange(vector& bills) {
        int five = 0, ten = 0;
        for (int i : bills) {
            if (i == 5) five++;
            else if (i == 10) {five--; ten++;}
            else if (ten > 0) {ten--; five--;} 
            else five -= 3;
            if (five < 0) return false;
        }
        return true;
    }
};

17、可以到达的最远建筑

class Solution {
public:
    int furthestBuilding(vector& hs, int bs, int ls) {
        priority_queue q;
        int t=0;
        for(int i=hs.size()-1;i>0;i--) hs[i]-=hs[i-1];
        int i=1;
        for(;ibs) return i-1;
            if(ls) {
                if(t+hs[i]<=bs) {t+=hs[i]; q.push(hs[i]);}
                else{
                    if(q.size() && q.top()& heights, int bricks, int ladders) {
        priority_queue, greater> q;
        for (int i = 1; i < heights.size(); i++) {
            if (heights[i] <= heights[i - 1]) continue;
            int d = heights[i] - heights[i - 1];
            if (ladders > 0) {
                ladders--; q.push(d); // 有梯子时先考虑梯子
            } else {
                if (!q.empty() && d > q.top()) {
                    if (bricks < q.top())  return i - 1;
                    bricks -= q.top();
                    q.pop();
                    q.push(d);
                } else {
                    if (bricks < d)  return i - 1;
                    bricks -= d;
                }
            }
        }
        return n - 1;
    }
};

18、销售价值减少的颜色球

#define LL long long

class Solution {
public:
    int maxProfit(vector& inventory, int orders) {
        const int mod = 1000000007;
        const int n = inventory.size() + 1;

        inventory.push_back(0);
        sort(inventory.begin(), inventory.end());

        LL ans = 0;
        for (int i = n - 1; i >= 1; i--) {
            if (orders >= (LL)(inventory[i] - inventory[i - 1]) * (n - i)) {
                orders -= (inventory[i] - inventory[i - 1]) * (n - i);

                int s = inventory[i - 1] + 1, t = inventory[i], num = t - s + 1;
                ans = (ans + (LL)(s + t) * num / 2 % mod * (n - i)) % mod;
            } else {
                int num = orders / (n - i);
                int t = inventory[i], s = t - num + 1;
                ans = (ans + (LL)(s + t) * num / 2 % mod * (n - i)) % mod;

                int r = orders % (n - i);
                ans = (ans + (LL)(r) * (t - num)) % mod;
                break;
            }
        }
        return ans;
    }
};

19、找出最具竞争力的子序列

class Solution {
public:
    vector mostCompetitive(vector& nums, int k) {
        const int n = nums.size();
        deque q;
        vector ans;

        for (int i = 0; i < n; i++) {
            while (!q.empty() && nums[i] < nums[q.back()])
                q.pop_back();
            q.push_back(i);
            if (i >= n - k) {
                ans.push_back(nums[q.front()]);
                q.pop_front();
            }
        }
        return ans;
    }
};

// 使用vector模拟队列
class Solution {
public:
    vector mostCompetitive(vector& nums, int k) {
        const int n = nums.size();
        vector ans;

        for (int i = 0; i < n; i++) {
            while (!ans.empty() && ans.size() + n - i > k && nums[i] < ans.back())
                ans.pop_back();

            if (ans.size() < k)
                ans.push_back(nums[i]);
        }
        return ans;
    }
};

[Go Back~](# LeetCode题解)

section14-1: 区间贪心

1、无重叠区间【最大不相交区间数量】

class Solution {
public:
    int eraseOverlapIntervals(vector>& intervals) {
        int ans=0;
        auto cmp=[&](const vector&a, const vector&b){
            if(a[1]==b[1]) return (a[1]-a[0])<(b[1]-b[0]);
            else return a[1]=last) last = t[1];
            else ans++;
        }
        return ans;
    }
};

2、用最少数量的箭引爆气球【区间选点】

class Solution {
public:
    int findMinArrowShots(vector>& points) {
        auto cmp = [&](const vector&a, const vector&b){
            return a[1]= s){
                s = max(s,points[i][0]);
                e = min(e,points[i][1]);
            }else{
                ans++;
                s = points[i][0];
                e = points[i][1];
            }
        }
        return ans+1;
    }
};

3、视频拼接【区间覆盖】

class Solution {
public:
    int videoStitching(vector>& clips, int time) {
        auto cmp = [&](const vector&a, const vector&b){
            return a[0]s) return -1;
            int e = 0;
            while(i=time) return ans;
        }
        return -1;
    }
};

[Go Back~](# LeetCode题解)