코드스테이츠 Day28
<알고리즘 with Math>
<중복 순열 템플릿>
public class Solution {
public ArrayList<String[]> rockPaperScissors(int rounds) {
ArrayList<String[]> outcomes = new ArrayList<>();
return permutation(rounds, new String[]{}, outcomes);
}
public ArrayList<String[]> permutation(int roundsToGo, String[] playedSoFar, ArrayList<String[]> outcomes) {
if(roundsToGo == 0) {
outcomes.add(playedSoFar);
return outcomes;
}
String[] rps = new String[]{"rock", "paper", "scissors"};
for(int i = 0; i < rps.length; i++) {
String currentPlay = rps[i];
String[] concatArray = Arrays.copyOf(playedSoFar, playedSoFar.length + 1);
concatArray[concatArray.length - 1] = currentPlay;
outcomes = permutation(roundsToGo - 1, concatArray, outcomes);
}
return outcomes;
}
}
<순열 템플릿>
public class Solution {
public ArrayList<Integer[]> newChickenRecipe(int[] stuffArr, int choiceNum) {
ArrayList<Integer> freshArr = new ArrayList<>();
for(int i = 0; i < stuffArr.length; i++) {
String str = String.valueOf(stuffArr[i]);
int[] element = str.chars().filter(c -> c == '0').toArray();
if(element.length <= 2) {
freshArr.add(stuffArr[i]);
}
}
Collections.sort(freshArr);
if (freshArr.size() == 0 || freshArr.size() < choiceNum) return null;
ArrayList<Integer[]> result = new ArrayList<>();
boolean[] visited = new boolean[freshArr.size()]; // (f,f,f)
return permutation(choiceNum, new Integer[]{}, result, freshArr, visited);
}
public ArrayList<Integer[]> permutation(int choiceNum, Integer[] bucket,
ArrayList<Integer[]> result, ArrayList<Integer> freshArr, boolean[] visited) {
if(choiceNum == 0) {
result.add(bucket);
return result;
}
for(int i = 0; i < freshArr.size(); i++) {
if(!visited[i]) {
visited[i] = true;
Integer[] concatArray = Arrays.copyOf(bucket, bucket.length + 1);
concatArray[concatArray.length - 1] = freshArr.get(i);
result = permutation(choiceNum-1, concatArray, result, freshArr, visited);
visited[i] = false;
}
}
return result;
}
}
<조합 템플릿>
public class Solution {
public int boringBlackjack(int[] cards) {
// 3장을 순서와 중복 없이 고르고 더한 후, 합이 소수이면, 카운트++
int count =0;
for(int i=0; i<cards.length; i++){ // 1
for(int j=i+1; j<cards.length; j++){ // 2
for(int k=j+1; k<cards.length; k++){ // 3 4
if(isPrime(cards[i]+cards[j]+cards[k])) count++;
}
}
}
return count;
}
public boolean isPrime(int num){
for(int i=2; i<=Math.sqrt(num); i++){
if(num%i==0) return false;
}
return true;
}
}
<최대 공약수 템플릿>
public class Solution {
public ArrayList<Integer[]> divideChocolateStick(int M, int N) {
int GCD = gcd(M, N);
ArrayList<Integer[]> output = new ArrayList<>();
for(int i=1; i<=GCD; i++){
if(GCD%i==0) output.add(new Integer[]{i, M/i, N/i});
}
return output;
}
public int gcd(int a, int b){
if(a%b==0) return b;
return gcd(b, a%b);
}
}
<느낀 점>
알고리즘의 가장 큰 벽은 재귀함수가 아닐까 생각이 든다... ㅋㅋㅋ
오늘도 재귀함수의 늪에 빠져 허우적거림.
레퍼런스와 풀이 영상을 보며 이해하는 것만으로도 시간이 빠듯했다.
재귀함수 과정 하나 하나를 다 써보기도 하고 이해하기위해 갖은 노력을,,ㅋㅋ
멱집합이랑 정규표현식은 내일 공부 해야지,,
내일 순열 풀이 영상도 한 번 더 봐야겠다.