暑期算法打卡-Day6

1.807.保持城市天际线

暑期算法打卡-Day6_第1张图片

暑期算法打卡-Day6_第2张图片

暑期算法打卡-Day6_第3张图片

class Solution {
public:
    int maxIncreaseKeepingSkyline(vector>& grid) {
        int n = grid.size();
        vector rowMax(n);
        vector colMax(n);
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                rowMax[i] = max(rowMax[i], grid[i][j]);
                colMax[j] = max(colMax[j], grid[i][j]);
            }
        }
        int ans = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                ans += min(rowMax[i], colMax[j]) - grid[i][j];
            }
        }
        return ans;
    }
};

2.LCP 40. 心算挑战 

暑期算法打卡-Day6_第4张图片

const int N=1e5+10;
bool cmp(int a,int b){ return a>b; }
class Solution {
public:
    int maxmiumScore(vector& nums, int cnt) {
        vector shuzu_1,shuzu_2;
        for(auto &x:nums){
            if(x&1) shuzu_1.push_back(x); //奇数数组
            else shuzu_2.push_back(x); //偶数数组
        }
        sort(shuzu_1.begin(),shuzu_1.end(),cmp); //从大到小排序
        sort(shuzu_2.begin(),shuzu_2.end(),cmp); //从大到小排序
        int sum_1[N],sum_2[N];
        memset(sum_1,0,sizeof sum_1);
        memset(sum_2,0,sizeof sum_2);
        for(int i=0;i

3.455. 分发饼干 

 暑期算法打卡-Day6_第5张图片

class Solution {
public:
    int findContentChildren(vector& g, vector& s) {
        sort(g.begin(), g.end());
        sort(s.begin(), s.end());
        int numOfChildren = g.size(), numOfCookies = s.size();
        int count = 0;
        for (int i = 0, j = 0; i < numOfChildren && j < numOfCookies; i++, j++) {
            while (j < numOfCookies && g[i] > s[j]) {
                j++;
            }
            if (j < numOfCookies) {
                count++;
            }
        }
        return count;
    }
};

你可能感兴趣的:(日常练习,C++,算法,java,服务器,前端,c++,算法)