从零开始的刷LeetCode生活 第35期 351-370

在这里插入图片描述

class SummaryRanges {
public:
    /** Initialize your data structure here. */
    map<int, int>m;
    SummaryRanges() {
        
    }
    
    void addNum(int val) {
        if(m.count(val)) return;
        m[val] = val;
        int min = val, max = val;
        if(m.count(val - 1)) min = m[val - 1]; //更新左端点
        if(m.count(val + 1)) max = m[val + 1];
        m[max] = min;
        m[min] = max;
    }
    
    vector<vector<int>> getIntervals() {
        vector<vector<int>> res;
        int min_v = -1;
        for(auto t : m)
            if(t.first > min_v)
            {
                res.push_back({t.first, t.second});
                min_v = t.second;
            }
        return res;
    }
};

/**
 * Your SummaryRanges object will be instantiated and called as such:
 * SummaryRanges* obj = new SummaryRanges();
 * obj->addNum(val);
 * vector<vector<int>> param_2 = obj->getIntervals();
 */

在这里插入图片描述

class Solution {
public:
   int maxEnvelopes(vector<vector<int>>& envelopes) {
       sort(envelopes.begin(), envelopes.end());
       int n = envelopes.size();
       vector<int> f(n);
       int res = 0;
       for(int i = 0; i < n; i ++)
       {
           f[i] = 1;
           for(int j = 0; j < i; j ++)
               if(envelopes[i][0] > envelopes[j][0] && envelopes[i][1] > envelopes[j][1])
               f[i] = max(f[i], f[j] + 1);
           res = max(res, f[i]);
       }
       return res;
   }
};

在这里插入图片描述

class Twitter {
public:
    /** Initialize your data structure here. */
    unordered_map<int, vector<pair<int, int>>>posts;
    unordered_map<int, unordered_set<int>> follows;
    int id = 0;
    Twitter() {
        
    }
    
    /** Compose a new tweet. */
    void postTweet(int userId, int tweetId) {
        posts[userId].push_back(make_pair(id ++, tweetId));
    }
    
    /** Retrieve the 10 most recent tweet ids in the user's news feed. Each item in the news feed must be posted by users who the user followed or by the user herself. Tweets must be ordered from most recent to least recent. */
    vector<int> getNewsFeed(int userId) {
        vector<pair<int, int>>ps;
        for(auto x : posts[userId]) ps.push_back(x);
        for(auto follow : follows[userId])
            for(auto x : posts[follow])
                ps.push_back(x);
        sort(ps.rbegin(), ps.rend());
        vector<int>res;
        for(int i = 0; i < 10 && i < ps.size(); i ++)
            res.push_back(ps[i].second);
        return res;
    }
    
    /** Follower follows a followee. If the operation is invalid, it should be a no-op. */
    void follow(int followerId, int followeeId) {
        if(followerId != followeeId)
            follows[followerId].insert(followeeId);
    }
    
    /** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
    void unfollow(int followerId, int followeeId) {
        follows[followerId].erase(followeeId);
    }
};

/**
 * Your Twitter object will be instantiated and called as such:
 * Twitter* obj = new Twitter();
 * obj->postTweet(userId,tweetId);
 * vector<int> param_2 = obj->getNewsFeed(userId);
 * obj->follow(followerId,followeeId);
 * obj->unfollow(followerId,followeeId);
 */

在这里插入图片描述

class Solution {
public:
   int countNumbersWithUniqueDigits(int n) {
       if(n == 0) return 1;
       n = min(n, 10);
       vector<int> f(n);
       f[0] = 9;
       for(int i = 1; i < n; i ++) f[i] = f[i - 1] * (10 - i);
       int res = 0;
       for(int i = 0; i < n; i ++) res += f[i];
       return res + 1;
   }
};

在这里插入图片描述

class Solution {
public:
    int maxSumSubmatrix(vector<vector<int>>& matrix, int k) {
        int n = matrix.size(), m = matrix[0].size();
        int res = INT_MIN;
        int s[n + 1][m + 1];
        memset(s, 0, (n + 1) * (m + 1) * sizeof(int)); //大小(内存)
        for(int i = 0; i < n; i ++)
            for(int j = 0; j < m; j ++)
                s[i][j + 1] = s[i][j] + matrix[i][j];
        //枚举矩形左右边界
        for(int i = 0; i < m; i ++)
            for(int j = i; j < m; j ++)
            {
                set<int>myset;
                int A[n], temp = 0;
                myset.insert(0);
                for(int t = 0; t < n; t ++)
                {
                    A[t] = s[t][j + 1] - s[t][i];
                    temp += A[t]; //当前前缀和
                    auto it = myset.lower_bound(temp - k);
                    if(it != myset.end())
                        res = max(res, temp - *it);
                    myset.insert(temp);
                }
            }
        return res;
    }
};

在这里插入图片描述

class Solution {
public:
    bool canMeasureWater(int x, int y, int z) {
        if(!x && !y) return z == 0;
        return z = 0 || (z % gcd(x, y) == 0 && x + y >= z);
    }

    static int gcd(int x, int y)
    {
        if(y == 0) return x;
        int r = x % y;
        return gcd(y, r);
    }
};

在这里插入图片描述

class Solution {
public:
    bool isPerfectSquare(int num) {
        for(long long i = 0; i * i <= num; i++)
            if(i * i == num)
                return true;
        return false;
    }
};
class Solution {
public:
    bool isPerfectSquare(int num) {
        int res = num;
        int next = 1;
        while(res > 0)
        {
            res -= next;
            next += 2;
        }
        return res == 0;
    }
};

在这里插入图片描述

class Solution {
public:
    vector<int> largestDivisibleSubset(vector<int>& nums) {
        int n = nums.size(), max = 0, end = -1;
        vector<int> dp(n, 1), last(n, -1), res;
        sort(nums.begin(), nums.end());
        for(int i = 0; i < n; i ++)
        {
            for(int j = 0; j < i; j ++)
                if(nums[i] % nums[j] == 0 && dp[i] <= dp[j])
                {
                    dp[i] = dp[j] + 1;
                    last[i] = j;
                }
            if(dp[i] > max)
            {
                max = dp[i];
                end = i;
            }
        }
        for(int i = end; i != -1; i = last[i])
            res.push_back(nums[i]);
        return res;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值