biweekly contest 36

写在前面

一共四题,1题easy,2题medium,1题hard
第一道,简单的系统设计题,要清楚object function;
第二道,同样应用题,但是通过率不高;
第三道,给出制定行和&列和的矩阵;
第四道,

  • Design Parking System
  • Alert Using Same Key-Card Three or More Times in a One Hour Period
  • Find Valid Matrix Given Row and Column Sums
  • Find Servers That Handled Most Number of Requests

    Design Parking System

    原题链接

解法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class ParkingSystem {
public:
vector<int>ps;
ParkingSystem(int big, int medium, int small) {
ps.push_back(big);
ps.push_back(medium);
ps.push_back(small);
}

bool addCar(int carType) {
if(ps[carType-1]>0){
ps[carType-1]--;
return true;
}
else return false;
}
};

Alert Using Same Key-Card Three or More Times in a One Hour Period

原题链接
解法:

Find Valid Matrix Given Row and Column Sums

原题链接
解法:Greedy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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;
}
};

Time: O(mn)
Space: O(mn)

Find Servers That Handled Most Number of Requests

原题链接