Re: [閒聊] 每日leetcode

看板Marginalman作者 (dont)時間10月前 (2025/02/07 21:43), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1327/1552 (看更多)
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
文章代碼(AID): #1dfWtz4d (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1dfWtz4d (Marginalman)