# # Leetcode 第36场双周赛题解

## # Problem A - 设计停车系统 (opens new window)

class ParkingSystem {
vector<int> rem;
public:
ParkingSystem(int big, int medium, int small) {
rem = vector<int>{big, medium, small};
}

return (rem[--carType]-- > 0);
}
};

1
2
3
4
5
6
7
8
9
10
11

## # Problem B - 警告一小时内使用相同员工卡大于等于三次的人 (opens new window)

class Solution {
int to_int(string s) {
int hh = stoi(s.substr(0, 2));
int mm = stoi(s.substr(3, 2));
return hh * 60 + mm;
}
public:
vector<string> alertNames(vector<string>& keyName, vector<string>& keyTime) {
set<string> invalid;
unordered_map<string, vector<int>> mp;
for (int i = 0; i < keyName.size(); ++i)
mp[keyName[i]].emplace_back(to_int(keyTime[i]));
for (auto p : mp) {
auto &v = p.second;
sort(v.begin(), v.end());
for (int i = 0; i + 2 < v.size(); ++i) {
if (v[i + 2] - v[i] <= 60) {
invalid.insert(p.first);
break;
}
}
}
return vector<string>(invalid.begin(), invalid.end());
}
};

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

## # Problem C - 给定行和列的和求可行矩阵 (opens new window)

class Solution {
public:
vector<vector<int>> restoreMatrix(vector<int>& rowSum, vector<int>& colSum) {
int n = rowSum.size(), m = colSum.size();
vector<vector<int>> ans(n, vector<int>(m));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
ans[i][j] = min(rowSum[i], colSum[j]);
rowSum[i] -= ans[i][j];
colSum[j] -= ans[i][j];
}
}
return ans;
}
};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

## # Problem D - 找到处理最多请求的服务器 (opens new window)

class Solution {
public:
vector<int> busiestServers(int k, vector<int>& arrival, vector<int>& load) {
int n = arrival.size();
vector<int> cnt(k);
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq;
set<int> vac;
for (int i = 0; i < k; ++i)
vac.insert(i);
for (int i = 0; i < n; ++i) {
while (!pq.empty() && pq.top().first <= arrival[i]) {
int pos = pq.top().second;
pq.pop();
vac.insert(pos);
}
int pos = -1;
auto it = vac.lower_bound(i % k);
if (it == vac.end())
it = vac.begin();
if (it != vac.end()) {
pos = *it;
vac.erase(it);
}
if (pos != -1) {
cnt[pos]++;
}
}

int hi = 0;
vector<int> ans;
for (int i = 0; i < k; ++i) {
if (cnt[i] > hi) {
ans.clear();
hi = cnt[i];
}
if (cnt[i] == hi)
ans.emplace_back(i);
}

return ans;
}
};

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44