Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- unity
- Camera Zoom
- Unity Editor
- SDK upgrade
- Digital Ocean
- java
- nodejs
- screencapture
- OverTheWire
- linux
- Camera Movement
- MySQL
- server
- docker
- critical rendering path
- rpg server
- springboot
- Git
- spread 연산자
- mongoDB
- Unity IAP
- Google Refund
- Spring Boot
- draganddrop
- css framework
- react
- Google Developer API
- Packet Network
- --watch
- express
Archives
- Today
- Total
우당탕탕 개발일지
[프로그래머스] Level 2. 후보키 본문
https://school.programmers.co.kr/learn/courses/30/lessons/42890
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
너가어떻게 level 2문제니? 산넘어산이다
1. 모든 가능한 조합 구하기
2. 이 가능한 조합중에서 유일성을 만족하는 키만 추려내기
3. 이 추려낸 키 중에서, 최소성을 만족하는 키 찾기.
가능한 조합은 combination함수이다. pick.size() == count일 때 원하는 수만큼 조합이 완료된거고, 거기에서 2번을 바로 진행하여 유일성을 만족할 경우에 keys에 넣어준다.
void combination(vector<int>& pick, int index, int count){
if(pick.size() == count){
if(IsUniqe(pick)) keys.push_back(pick); //조합이 완성되었을때, 유일성을 만족할 경우 keys에 넣음.
return;
}
for(;index<column; index++){
pick.push_back(index);
combination(pick,index+1,count);
pick.pop_back();
}
}
int solution(vector<vector<string>> relation) {
column = relation[0].size();
::relation = relation;
int answer = 0;
vector<int> picks;
//유일성을 만족하는 조합을 찾음.
for(int i=1; i<=column;i++){
combination(picks,0,i); //1개조합부터 8개조합까지 모두구함
}
//...
return answer;
}
2. 유일성을 만족하는지 어떻게 아는가. 해당 컬럼의 값들을 모두 a,b,c 형식으로 string화 한다음, set에 넣어줌으로써 확인이 가능하다. set은 동일한 값은 중복해서 넣지 않으므로, set의 개수와 데이터의 개수가 동일해야한다.
bool IsUniqe(vector<int>& pick){
set<string> s;
for(int i=0; i<relation.size(); i++){
string str = "";
for(int j=0; j<pick.size(); j++){
str += relation[i][pick[j]]+",";
}
s.insert(str);
if(s.size() != i+1) return false;
}
return true;
}
3. 최소성을 만족하는지 검사하기. 내 pick의 개수보다 적은 수의 key를 검사한다. 만약 key의 모든 요소가 pick에 있을 경우, pick은 최소성을 만족하지않는다.
bool IsMinimal(vector<int>& pick){
for(int i=0; i<keys.size(); i++){
if(keys[i].size() >= pick.size()) continue;
int match = 0;
for(int j=0; j<keys[i].size(); j++){
if(find(pick.begin(), pick.end(), keys[i][j]) != pick.end()) match++;
}
if(match == keys[i].size()) return false;
}
return true;
}
최종코드
#include <string>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
int column = 0;
vector<vector<int>> keys;
vector<vector<string>> relation;
bool IsMinimal(vector<int>& pick){
for(int i=0; i<keys.size(); i++){
if(keys[i].size() >= pick.size()) continue;
int match = 0;
for(int j=0; j<keys[i].size(); j++){
if(find(pick.begin(), pick.end(), keys[i][j]) != pick.end()) match++;
}
if(match == keys[i].size()) return false;
}
return true;
}
bool IsUniqe(vector<int>& pick){
set<string> s;
for(int i=0; i<relation.size(); i++){
string str = "";
for(int j=0; j<pick.size(); j++){
str += relation[i][pick[j]]+",";
}
s.insert(str);
if(s.size() != i+1) return false;
}
return true;
}
void combination(vector<int>& pick, int index, int count){
if(pick.size() == count){
if(IsUniqe(pick)) keys.push_back(pick);
return;
}
for(;index<column; index++){
pick.push_back(index);
combination(pick,index+1,count);
pick.pop_back();
}
}
int solution(vector<vector<string>> relation) {
column = relation[0].size();
::relation = relation;
int answer = 0;
vector<int> picks;
//유일성을 만족하는 조합을 찾음.
for(int i=1; i<=column;i++){
combination(picks,0,i);
}
for(int i=0; i<keys.size(); i++){
if(IsMinimal(keys[i])) answer++;
}
return answer;
}
카카오싫어
'Algorithm(c++) > Level 2' 카테고리의 다른 글
[프로그래머스] Level 2. 숫자 블록 (0) | 2025.03.20 |
---|---|
[프로그래머스] Level 2 . 이모티콘 할인행사 (0) | 2025.03.18 |
[프로그래머스] Level 2. 거리두기 확인하기 (0) | 2025.03.07 |
[프로그래머스] Level 2. 2개 이하로 다른 비트 (0) | 2025.02.26 |
[프로그래머스] Level 2. 2 x n 타일링 (0) | 2025.02.25 |