LC 3843 - First Element with Unique Frequency

I count the freuqency for each number, and do reverse for the hashmap to map frequency to numbers, and stored the numbers that occured only once, and search the first occurence of this number. Actually,we can have a more simple version (in Code-2), to further store the count for each frequency in freqCnt, and we only need to do dobule hashmap when searching again in nums. Code-1: class Solution { public: int firstUniqueFreq(vector<int>& nums) { unordered_map<int, int> counts; for(const auto& num: nums) { ++counts[num]; } unordered_map<int, vector<int>> numOfCnt; for(auto [num, cnt]: counts) { if(numOfCnt.count(cnt)) { numOfCnt[cnt].push_back(num); } else { numOfCnt[cnt] = {num}; } } unordered_set<int> uniq; for(auto [cnt, v]: numOfCnt) { if(v.size() > 1) continue; uniq.insert(v[0]); } if(uniq.size() == 0) return -1; for(const auto& num: nums) { if(uniq.count(num)) return num; } return -1; } }; Code-2: Simplified by ChatGPT class Solution { public: int firstUniqueFreq(vector<int>& nums) { unordered_map<int,int> cnt; for (int x : nums) ++cnt[x]; unordered_map<int,int> freqCnt; // freq -> how many numbers have this freq for (auto &p : cnt) ++freqCnt[p.second]; for (int x : nums) { if (freqCnt[cnt[x]] == 1) return x; } return -1; } };

February 21, 2026 · 1 min

LC 3815 - Design Auction System

Approach This problem is simple, maintain unordered_map for the itemID, for each itermID maintain userId mapping to the bid, and also store this pair inside a unordered_set for fast query. Need to chekc if hte itemId and userId is still inside the hashmap (get 1 WA on this … ) . Code class AuctionSystem { public: unordered_map<int, set<pair<int,int>, greater<pair<int,int>>> > mp; // itemId -> unordered_map<int, unordered_map<int, int> > mp2; // itemId -> AuctionSystem() { mp.clear(); mp2.clear(); } void addBid(int userId, int itemId, int bidAmount) { auto& st = mp[itemId]; auto& st2 = mp2[itemId]; if(st2.count(userId)) { int bid = st2[userId]; st.erase({bid, userId}); st.insert({bidAmount, userId}); st2[userId] = bidAmount; } else { st.insert({bidAmount, userId}); st2[userId] = bidAmount; } } void updateBid(int userId, int itemId, int newAmount) { auto& st = mp[itemId]; auto& st2 = mp2[itemId]; int bid = st2[userId]; st.erase({bid, userId}); st.insert({newAmount, userId}); st2[userId] = newAmount; } void removeBid(int userId, int itemId) { auto& st = mp[itemId]; auto& st2 = mp2[itemId]; int bid = st2[userId]; st.erase({bid, userId}); st2.erase(userId); } int getHighestBidder(int itemId) { return mp.count(itemId) && mp[itemId].size() != 0 ? mp[itemId].begin()->second : -1; } };

January 18, 2026 · 1 min

LC 2657 - Find the Prefix Common Array of Two Arrays

[translate by ChatGPT] Just maintain two caches, count if the character B[i] is in cache of A, and do the same thing reversely. Code class Solution { public: vector<int> findThePrefixCommonArray(vector<int>& A, vector<int>& B) { vector<bool> cacheA(50, false); vector<bool> cacheB(50, false); int ans = 0; vector<int> ansAry; for(int i = 0 ; i < A.size() ; ++i) { if(cacheA[B[i]]) ++ans; if(cacheB[A[i]]) ++ans; if(A[i] == B[i]) ++ans; cacheA[A[i]] = true; cacheB[B[i]] = true; ansAry.push_back(ans); } return ansAry; } };

January 15, 2025 · 1 min