일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 프로그래머스
- EOI
- LAA
- 코딩테스트
- Deep learning
- omscs
- 개발자
- 조지아텍
- zeros
- cpp
- LV1
- machine learning
- C++
- 알고리즘
- 캐나다 영주권
- 머신러닝
- 매트랩
- 선형대수
- 위니펙
- mpnp
- leetcode
- 딥러닝
- 온라인석사
- Plotting
- 기본
- MATLAB
- 방송통신대학교
- 컴퓨터과학과
- 방통대
- 주정부이민
Archives
- Today
- Total
Byte by Byte
1207. Unique Number of Occurrences.cpp 본문
🔑 arr[i]을 key로 하는 map을 만들고, count for every occurence of such key. 그리고, occurence, 즉 map의 value 값이 동일한지 확인하기 위해, set에 value를 넣고, set의 size와 map의 사이즈가 동일하면 true, 아니면 false를 return 한다.
unique는 set을 떠올릴 것.
class Solution {
public:
bool uniqueOccurrences(vector<int>& arr) {
// vector<int>check;
// sort(arr.begin(),arr.end());
// int tmp = arr[0];
// int cnt = 1;
// for(int i =1; i<arr.size(); i++){
// if(arr[i] == tmp) cnt++;
// else{
// check.push_back(cnt);
// tmp = arr[i];
// cnt = 1;
// }
// if(i == arr.size()-1) check.push_back(cnt);
// }
// sort(check.begin(), check.end());
// if(check.size()==1)return true;
// for(int i =0; i<check.size()-1; i++){
// if(check[i] == check[i+1]) return false;
// }
map<int,int>m;
set<int>s;
for(int i =0; i<arr.size(); i++){
m[arr[i]]++;
}
for(auto it: m){
s.insert(it.second);
}
return m.size() == s.size() ? true : false;
}
};
'개발 로그 > 알고리즘' 카테고리의 다른 글
1337. The K Weakest Rows in a Matrix.cpp (0) | 2021.10.30 |
---|---|
1935. Maximum Number of Words You Can Type.cpp (0) | 2021.10.27 |
1380. Lucky Numbers in a Matrix.cpp (0) | 2021.10.21 |
811. Subdomain Visit Count.cpp (0) | 2021.10.20 |
1448. Count Good Nodes in Binary Tree.cpp (0) | 2021.10.19 |