문제
https://school.programmers.co.kr/learn/courses/30/lessons/42890
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
코드
import java.util.*;
class Solution {
String table [][];
List<List<Integer>> keyList = new ArrayList<>();
public int solution(String[][] relation) {
table = relation;
if(relation[0].length == 1){
if(unique(List.of(0)))
return 1;
else return 0;
}
for(int i=1;i<=relation[0].length;i++){
back(new ArrayList<>(), i, 0);
}
return keyList.size();
}
public void back(ArrayList<Integer> list, int cnt, int idx){
if(cnt == 0){
if (unique(list) && minimum(list)){
keyList.add(list);
}
return;
}
for(int i=idx;i<table[0].length;i++){
ArrayList<Integer> newList = new ArrayList<>(list);
newList.add(i);
back(newList, cnt-1, idx+1);
}
}
public boolean unique(List<Integer> key){
List<String> list = new ArrayList<>();
for(String [] row : table){
String str = "";
for(int idx : key){
str += row[idx];
}
if(list.contains(str)) return false;
else list.add(str);
}
return true;
}
public boolean minimum(List<Integer> newKeys){
for(List<Integer> keys : keyList){
if(newKeys.containsAll(keys)) return false;
}
return true;
}
}
문제
https://school.programmers.co.kr/learn/courses/30/lessons/42890
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
코드
import java.util.*; class Solution { String table [][]; List<List<Integer>> keyList = new ArrayList<>(); public int solution(String[][] relation) { table = relation; if(relation[0].length == 1){ if(unique(List.of(0))) return 1; else return 0; } for(int i=1;i<=relation[0].length;i++){ back(new ArrayList<>(), i, 0); } return keyList.size(); } public void back(ArrayList<Integer> list, int cnt, int idx){ if(cnt == 0){ if (unique(list) && minimum(list)){ keyList.add(list); } return; } for(int i=idx;i<table[0].length;i++){ ArrayList<Integer> newList = new ArrayList<>(list); newList.add(i); back(newList, cnt-1, idx+1); } } public boolean unique(List<Integer> key){ List<String> list = new ArrayList<>(); for(String [] row : table){ String str = ""; for(int idx : key){ str += row[idx]; } if(list.contains(str)) return false; else list.add(str); } return true; } public boolean minimum(List<Integer> newKeys){ for(List<Integer> keys : keyList){ if(newKeys.containsAll(keys)) return false; } return true; } }