-
1207. Unique Number of Occurrences릿코드(LEETCODE) 2020. 3. 3. 11:39반응형
https://leetcode.com/problems/unique-number-of-occurrences/
각 숫자의 요소의 갯수가 유일한지 판단하는 문제
class Solution { public: unordered_map<int, int> m; bool check(int count) { return m.count(count); } bool uniqueOccurrences(vector<int>& arr) { sort(arr.begin(), arr.end()); int prev = arr[0]; int prev_count = 0; for (int i = 0; i < arr.size(); i++) { int current = arr[i]; if (prev == current) { prev_count++; } else { if (check(prev_count)) { return false; } m[prev_count] = prev; prev = current; prev_count = 1; // check last if (i + 1 == arr.size()) { if (check(prev_count)) { return false; } } } } return true; } };
반응형'릿코드(LEETCODE)' 카테고리의 다른 글
804. Unique Morse Code Words (0) 2020.03.05 1252. Cells with Odd Values in a Matrix (0) 2020.03.03 1365. How Many Numbers Are Smaller Than the Current Number (0) 2020.03.03 1366. Rank Teams by Votes (0) 2020.03.03 733. Flood Fill (0) 2020.03.03