알고리즘 공부/프로그래머스
(Java)프로그래머스 코딩테스트 연습 - 해시 - 위장
HRuler
2020. 10. 28. 14:05
1. 문제
https://programmers.co.kr/learn/courses/30/lessons/42578
코딩테스트 연습 - 위장
programmers.co.kr
2. 나의 풀이
class Solution {
public int solution(String[][] clothes) {
int answer = 0;
boolean [] visited = new boolean [clothes.length];
int imsi = 1;
while(check_boolean(visited)) {
String check = "";
int su = 0;
for(int i = 0; i < clothes.length; i = i + 1) {
if(visited[i] == true) {
continue;
}else if(check.equals("")) {
check = clothes[i][1];
}
if(check.equals(clothes[i][1])) {
su += 1;
visited[i] = true;
}else if(!check.equals(clothes[i][1])) {
continue;
}
System.out.println("su : " + su);
}
imsi *= (su + 1);
}
answer = answer + imsi - 1;
return answer;
}
boolean check_boolean(boolean [] visited) {
for(int i = 0; i < visited.length; i = i + 1) {
if(visited[i] == false) {
return true;
}
}
return false;
}
}
3. 다른 사람 풀이
import java.util.*;
import static java.util.stream.Collectors.*;
class Solution {
public int solution(String[][] clothes) {
return Arrays.stream(clothes)
.collect(groupingBy(p -> p[1], mapping(p -> p[0], counting())))
.values()
.stream()
.collect(reducing(1L, (x, y) -> x * (y + 1))).intValue() - 1;
}
}