# 第78场双周赛(Game 78 biweekly)-其他

## 第78场双周赛(Game 78 biweekly)

### 第一题

• 首先把整数转化为字符串
• 遍历字符串，每次取出长度为 \(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;
}
};
``````

### 第二题

• 前缀和处理出数组
• 遍历，左边是 \(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;
}
};
``````

### 第三题

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

``````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

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

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)

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