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;
}
}
'알고리즘 공부 > 프로그래머스' 카테고리의 다른 글
(Java)프로그래머스 코딩테스트 연습 - 깊이/너비 우선 탐색(DFS/BFS) - 타겟 넘버 (0) | 2020.10.30 |
---|---|
(Java)프로그래머스 코딩테스트 연습 - 정렬 - H-Index (0) | 2020.10.28 |
(Java)프로그래머스 코딩테스트 연습 - 탐욕법(Greedy) - 구명보트 (0) | 2020.10.24 |
(Java)프로그래머스 코딩테스트 연습 - 해시 - 전화번호 목록 (0) | 2020.10.21 |
(Java)프로그래머스 코딩테스트 연습 - 완전탐색 - 소수 찾기 (0) | 2020.10.20 |