Re: [閒聊] 每日leetcode
3160. Find the Number of Distinct Colors Among the Balls
## 思路
兩個hash map
一個紀錄球的顏色, 一個紀錄每種顏色的個數
每次QUERY更新兩個map
如果球顏色改變, 就減掉原本顏色個數
## Code
```cpp
class Solution {
public:
vector<int> queryResults(int limit, vector<vector<int>>& queries) {
unordered_map<int, int> balls;
unordered_map<int, int> counter;
vector<int> res;
for (auto& query: queries) {
int b=query[0], c=query[1];
if (balls.find(b) != balls.end()) {
if (--counter[balls[b]] == 0) {
counter.erase(balls[b]);
}
}
balls[b] = c;
counter[c]++;
res.push_back(counter.size());
}
return res;
}
};
```
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 45.143.82.130 (美國)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1738935805.A.127.html
討論串 (同標題文章)
完整討論串 (本文為第 1327 之 1552 篇):