weekly contest 209

写在前面

主要四道题目,1道easy,2道medium,1道hard
主要题目如下:

  • Special Array With X Elements Greater Than or Equal X
  • Even Odd Tree
  • Find Valid Matrix Given Row and Column Sums
  • Minimum One Bit Operations to Make Integers Zero

Special Array With X Elements Greater Than or Equal X

原题链接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public:
int specialArray(vector<int>& nums) {
int x=0,count=0;
sort(nums.begin(),nums.end());
while(x<=nums[nums.size()-1]){
for(int i=0;i<nums.size();i++){
if(nums[i]>=x)count++;
}
if(x!=count){x++;count=0;}
else break;
}
return x==count? x:-1;
}
};

Even Odd Tree

原题链接

Find Valid Matrix Given Row and Column Sums

原题链接

算法思想:greedy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public:
vector<vector<int>> restoreMatrix(vector<int>& row, vector<int>& col) {
int m = row.size(), n = col.size();
vector<vector<int>> A(m, vector<int>(n, 0));
for (int i = 0; i < m; ++i) {
for (int j = 0 ; j < n; ++j) {
A[i][j] = min(row[i], col[j]);
row[i] -= A[i][j];
col[j] -= A[i][j];
}
}
return A;
}
};

Minimum One Bit Operations to Make Integers Zero

原题链接