第 176 场周赛

5340. 统计有序矩阵中的负数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
int countNegatives(vector<vector<int>>& grid) {
int count = 0;
for(int i=0; i<grid.size(); i++){
int cur = 0;
for(; cur<grid[0].size(); cur++){
if(grid[i][cur] < 0) break;
}
count += grid[0].size()-cur;
}
return count;
}
};

5341. 最后 K 个数的乘积

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class ProductOfNumbers {
public:
vector<int> cot;

ProductOfNumbers() {}

void add(int num) {
cot.push_back(num);
}

int getProduct(int k) {
int ret = 1;
int count = 0;
for(int i=cot.size()-1; i>=0; i--){
ret *= cot[i];
count++;
if(count == k) break;
}
return ret;
}
};

5342. 最多可以参加的会议数目

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public:
static bool cmp(vector<int>& a, vector<int>& b){
if(a[1] == b[1]) return a[0] < b[0];
return a[1] < b[1];
}
int maxEvents(vector<vector<int>>& events) {
sort(events.begin(), events.end(), cmp);
vector<int> visited(100005);
int ret = 0;
for(int i=0; i<events.size(); i++){
for(int j=events[i][0]; j<=events[i][1]; j++){
if(!visited[j]){
visited[j] = 1;
ret ++;
break;
}
}
}
return ret;
}
};

5343. 多次求和构造目标数组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Solution {
public:
bool isPossible(vector<int>& target) {
priority_queue<int> q;
long long s = 0;
for(int i=0; i<target.size(); i++){
q.push(target[i]);
s += target[i];
}
while(true){
if(q.top() == 1) return true;
long long p = q.top();
q.pop();
if(q.top() == 1){
if((p-1) % (s-p) == 0) return true;
else return false;
}else{
long long n = (p-q.top())/(s-p)+1;
long long x = p-(s-p)*n;
s = p-(s-p)*(n-1);
if(x <= 0) return false;
q.push(x);
}
}
return false;
}
};
Donate? comment?