第78场双周赛(Game 78 biweekly)

只会写前两题,太菜了,第三题改bug改不出来,第四题属于看都没看了

第一题

题目链接:https://leetcode.cn/problems/find-the-k-beauty-of-a-number/

个人题解:

  • 首先把整数转化为字符串
  • 遍历字符串,每次取出长度为 \(k\) 的子字符串
  • 子字符串转化为整形,判断不为 \(0\) 并且可以整除,则计算。

代码:

class Solution {
public:
    int divisorSubstrings(int num, int k) {
        string res=to_string(num);
        int cnt=0;
        for(int i=0;i+k<=res.size();i++){
            string t=res.substr(i,k);
            if(stoi(t)!=0 && num%stoi(t)==0) cnt++;
        }
        return cnt;
    }
};

第二题

题目链接:https://leetcode.cn/problems/number-of-ways-to-split-array/

个人题解:

  • 前缀和处理出数组
  • 遍历,左边是 \(presum[i+1]\),右边是 \(presum[n]-presum[i+1]\),比较即可

代码:

typedef long long LL;
class Solution {
public:
    int waysToSplitArray(vector<int>& nums) {
        int n=nums.size();
        LL pre[n+1];
        pre[0]=0LL;

        for(int i=0;i<n;i++) pre[i+1]=pre[i]+nums[i];

        int cnt=0;
        for(int i=0;i<n-1;i++){
            LL le=pre[i+1],ri=pre[n]-pre[i+1];
            if(le>=ri) cnt++;
        }
        return cnt;
    }
};

第三题

写不出来第三题,知道是贪心+双指针,但是不知道怎么贪心。。。(纯纯的废物)
题目链接:https://leetcode.cn/problems/maximum-white-tiles-covered-by-a-carpet/

个人题解:

  • 首先按照左端点排序
  • 用双指针遍历,双指针的差来维护答案

代码:

class Solution {
public:
    int maximumWhiteTiles(vector<vector<int>>& tiles, int carpetLen) {
        int n=tiles.size();
        sort(tiles.begin(),tiles.end());

        int j=0,cur=0,res=0;
        for(int i=0;i<n;i++){
            while(j<n && tiles[j][1]-tiles[i][0]+1<=carpetLen){
                cur+=tiles[j][1]-tiles[j][0]+1;
                j++;
            }

            if(j<n) res=max(res,cur+max(0,tiles[i][0]+carpetLen-tiles[j][0]));
            else res=max(res,cur);
            
            cur-=(tiles[i][1]-tiles[i][0]+1);
        }
        return res;
    }
};

第四题没写,刚看到的时候满脑子都是暴力,大佬题解还没看懂,就先不写了

————————

I can only write the first two questions. It’s too delicious. I can’t change the bug in the third question, and I haven’t seen the fourth question

First question

题目链接:https://leetcode.cn/problems/find-the-k-beauty-of-a-number/

Personal questions:

  • First, convert an integer to a string
  • Traverse the string and take out the substring with length \ (K \) each time
  • The substring is converted into an integer. If it is judged that it is not \ (0 \) and can be divided, it is calculated.

code:

class Solution {
public:
    int divisorSubstrings(int num, int k) {
        string res=to_string(num);
        int cnt=0;
        for(int i=0;i+k<=res.size();i++){
            string t=res.substr(i,k);
            if(stoi(t)!=0 && num%stoi(t)==0) cnt++;
        }
        return cnt;
    }
};

Second question

题目链接:https://leetcode.cn/problems/number-of-ways-to-split-array/

Personal questions:

  • Prefixes and processed arrays
  • Traversal. The left side is \ (presum [i + 1] \) and the right side is \ (presum [n] – presum [i + 1] \), which can be compared

code:

typedef long long LL;
class Solution {
public:
    int waysToSplitArray(vector<int>& nums) {
        int n=nums.size();
        LL pre[n+1];
        pre[0]=0LL;

        for(int i=0;i<n;i++) pre[i+1]=pre[i]+nums[i];

        int cnt=0;
        for(int i=0;i<n-1;i++){
            LL le=pre[i+1],ri=pre[n]-pre[i+1];
            if(le>=ri) cnt++;
        }
        return cnt;
    }
};

Question 3

I can’t write the third question. I know it’s greed + double pointer, but I don’t know how to be greedy… (pure waste)
Title Link: https://leetcode.cn/problems/maximum-white-tiles-covered-by-a-carpet/

Personal questions:

  • Sort by left endpoint first
  • Double pointer traversal is used, and the difference of double pointer is used to maintain the answer

code:

class Solution {
public:
    int maximumWhiteTiles(vector<vector<int>>& tiles, int carpetLen) {
        int n=tiles.size();
        sort(tiles.begin(),tiles.end());

        int j=0,cur=0,res=0;
        for(int i=0;i<n;i++){
            while(j<n && tiles[j][1]-tiles[i][0]+1<=carpetLen){
                cur+=tiles[j][1]-tiles[j][0]+1;
                j++;
            }

            if(j<n) res=max(res,cur+max(0,tiles[i][0]+carpetLen-tiles[j][0]));
            else res=max(res,cur);
            
            cur-=(tiles[i][1]-tiles[i][0]+1);
        }
        return res;
    }
};

The fourth question was not written. When I first saw it, my mind was full of violence. I didn’t write it until I understood the boss’s solution