728x90

정사각형 모양의 틀에 여러 종류의 인형이 있고 1~5번 라인 중 몇번으로 이동해서 인형을 가져오는지 moves를 배열로 주어주면 인형을 가져와서 바구니에 담는다. 

이때 바구니의 맨 윗 인형과 내가 가져온 인형이 같은 종류라면 인형들을 없애고 없어진 인형의 수만큼 점수로 획득한다. 

public int solution(int[][] board, int[] moves) {

int answer = 0;

Map<Integer, ArrayList<Integer>> map = new HashMap<Integer, ArrayList<Integer>>();

for (int i = board.length - 1; i >= 0; i--) {

for (int j = 0; j < board[i].length; j++) {

if (board[i][j] == 0)

continue;

ArrayList<Integer> temp = map.getOrDefault(j, new ArrayList<Integer>());

temp.add(board[i][j]);

map.put(j, temp);

}

}

 

ArrayList<Integer> stack = new ArrayList<>();

for (int i = 0; i < moves.length; i++) {

if (map.get(moves[i] - 1).size() != 0) {

ArrayList<Integer> temp = map.get(moves[i] - 1);

if (stack.size() > 0 && stack.get(stack.size() - 1) == temp.get(temp.size() - 1)) {

answer += 2;

stack.remove(stack.size() - 1);

} else {

stack.add(temp.get(temp.size() - 1));

}

temp.remove(temp.size() - 1);

map.put(moves[i] - 1, temp);

}

}

return answer;

}

1~5열까지 ArrayList를 각각 가지고 ArrayList에는 각 열에 담긴 인형들의 종류를 int로 가지고 있다. 

stack은 뽑은 인형을 담는 바구니이다.

인형을 뽑아 stack의 윗 부분을 확인해서 같으면 stack의 윗부분을 없애고 점수를 2점 얻는다

그리고 인형을 뽑을 때마다 map에서 ArrayList를 수정해준다. 

'Java > Coding Test' 카테고리의 다른 글

카펫 문제 풀이  (0) 2023.10.06
올바른 괄호 문제 풀이  (1) 2023.10.05
로또의 최고 순위와 최저 순위 문제 풀이  (0) 2023.10.04
다트 게임 문제 풀이  (0) 2023.10.04
실패율 문제 풀이  (0) 2023.10.01
728x90

알아볼 수 없는 로또 번호는 0으로 하여 입력한 로또 번호 배열과 실제 로또 우승 번호를 비교하여 

가능한 최저 순위와 최고 순위를 반환

public int[] solution(int[] lottos, int[] win_nums) {

int[] answer = new int[2];

int zeroCount = 0;

Set<Integer> set = new HashSet<>();

for (int i = 0; i < lottos.length; i++) {

if(lottos[i]==0)zeroCount++;

else {

set.add(lottos[i]);

}

}

int nonZeroCount = 6-zeroCount;

for (int i = 0; i < win_nums.length; i++) {

set.add(win_nums[i]);

}

answer[1] = 7-(6+nonZeroCount-set.size());

answer[0] = 7-(6+nonZeroCount-set.size() + zeroCount);

if(answer[1]>=7)answer[1] = 6;

if(answer[0]>=7)answer[0] = 6;

return answer;

}

0이 아닌 수 중에서 win_nums와 일치하는 개수가 최저 일치 개수이고 

최저 일치 개수에 0이 모두 win_nums와 일치한다고 가정했을 때가 최고 일치 개수이다. 

 

일치 개수는 set에 win_nums와 lottos중 0이 아닌 수를 집어넣었을 때 중복을 허용하지 않는 set의 size()를 이용해 구하였다. 

 

구한 수는 맞힌 개수이지 순위가 아니기 때문에 7에서 빼주어 순위를 구하고 

만약 맞힌개 0 이라면 7에서 뺐을때 7이 나오지만 순위로는 1과 같은 6위 이므로 6으로 변경해준다

'Java > Coding Test' 카테고리의 다른 글

올바른 괄호 문제 풀이  (1) 2023.10.05
크레인 인형뽑기 게임 문제 풀이  (0) 2023.10.04
다트 게임 문제 풀이  (0) 2023.10.04
실패율 문제 풀이  (0) 2023.10.01
문자열 나누기 문제 풀이  (1) 2023.10.01
728x90

S, D(점수 2제곱),T(점수 3제곱)

*(앞점수와 해당 점수 2배), #(해당 점수를 음수로)

3번의 다트 점수가 주어질때 S,D,T, * , # 를 계산한 총점을 반환 

public int solution(String dartResult) {

int answer = 0;

char[] arr = dartResult.toCharArray();

ArrayList<Integer> scoreList = new ArrayList<>();

for (int i=-0; i<arr.length;i++) {

if(Character.isDigit(arr[i])&&!Character.isDigit(arr[i+1])) {

scoreList.add(Integer.parseInt(arr[i]+""));

}else if(Character.isDigit(arr[i])) {

scoreList.add(Integer.parseInt(arr[i]+""+arr[i+1]));

i++;

}else if(arr[i]=='D') {

int score =scoreList.get(scoreList.size()-1);

score = score*score;

scoreList.remove(scoreList.size()-1);

scoreList.add(score);

}else if(arr[i]=='T') {

int score = scoreList.get(scoreList.size()-1);

score = score*score*score;

scoreList.remove(scoreList.size()-1);

scoreList.add(score);

}else if(arr[i]=='*') {

int score = scoreList.get(scoreList.size()-1);

score = 2*score;

scoreList.remove(scoreList.size()-1);

scoreList.add(score);

if(scoreList.size()>=2) {

int pscore = scoreList.get(scoreList.size()-2);

pscore = 2*pscore;

scoreList.remove(scoreList.size()-2);

scoreList.add(scoreList.size()-1, pscore);

}

}else if(arr[i]=='#') {

int score = scoreList.get(scoreList.size()-1);

score = -1*score;

scoreList.remove(scoreList.size()-1);

scoreList.add(score);

}

}

for (Integer integer : scoreList) {

answer+=integer;

}

return answer;

}

점수리스트를 만들어 

int score = scoreList.get(scoreList.size()-1);

를 통해 해당 점수를 얻어 S,D,T, *, # 연산을 해준다. 

만약 *의 경우 앞에 점수가 있다면 

 

if(scoreList.size()>=2) {

int pscore = scoreList.get(scoreList.size()-2);

pscore = 2*pscore;

scoreList.remove(scoreList.size()-2);

scoreList.add(scoreList.size()-1, pscore);

}

이전 점수까지 계산해주고 본래 위치로 다시 삽입한다. 

 

for (Integer integer : scoreList) {

answer+=integer;

}

이후 answer에 리스트를 순회하면서 점수를 각각 더해준다.

728x90

실패율 = 도달했으나 클리어하지 못한 유저 수/ 도달한 유저 수

아무도 도달하지 못한 경우 실패율 = 0

전체 스테이지 개수 N 

현재 유저들이 멈춰있는 스테이지 번호가 담긴 배열 stages

실패율이 높은 스테이지부터 내림차순으로 정렬하고 실패율이 같은 경우 스테이지 번호의 오름차순 정렬하여 반환

 

int[] current = new int[N+1];

for (int i = 0; i < stages.length; i++) {

for (int j = 0; j < stages[i]; j++) {

current[j]++;

}

}

각 스테이지별 현재까지 유저들이 지나가거나 멈춰있는 횟수를 담은 배열 current 

 

double[] failure = new double[N];

for (int i = 0; i < failure.length; i++) {

if(current[i]==0)failure[i]=0.0;

else failure[i] = (0.0+current[i]-current[i+1])/current[i];

}

실패율을 double[]로 저장 

만약 도달한 사람이 없다면 (current[i]==0) 실패율은 0.0 

 

Map<Double,ArrayList<Integer>> failureMapR = new HashMap<Double, ArrayList<Integer>>();

for (int i = 0; i < failure.length; i++) {

ArrayList<Integer> list = failureMapR.getOrDefault(failure[i], new ArrayList<Integer>());

list.add(i+1);

failureMapR.put(failure[i], list);

}

 

실패율을 map에 저장 

실패율을 key로 

해당 실패율에 대한 층 수는 중복될 수 있으므로 ArrayList<Integer>를 value로 갖는다. 

 

Arrays.sort(failure);

for (int i = failure.length-1; i >=0; i--) {

ArrayList<Integer> temp = failureMapR.get(failure[i]);

for (int j = 0; j < temp.size(); j++) {

answer.add(temp.get(j));

}

temp.removeAll(temp);

}

failure를 정렬하고 정렬된 배열의 뒷원소부터 map에서 찾아서 층 수를 answer에 저장해준다.

'Java > Coding Test' 카테고리의 다른 글

로또의 최고 순위와 최저 순위 문제 풀이  (0) 2023.10.04
다트 게임 문제 풀이  (0) 2023.10.04
문자열 나누기 문제 풀이  (1) 2023.10.01
대충 만든 자판 문제 풀이  (0) 2023.10.01
모의고사 문제 풀이  (0) 2023.09.30

+ Recent posts