Skip to content

Commit 9fee723

Browse files
committed
feat : 인프런 3주차 38 크레인 인형뽑기
1 parent 66ef343 commit 9fee723

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package sgyj.inflearn.yeji.week4;
2+
3+
import java.util.Arrays;
4+
import java.util.Scanner;
5+
import java.util.Stack;
6+
7+
class Solution38{
8+
// 크레인 인형뽑기
9+
public static int solution(int n,int m,int[][] inputArray,int[] inputM){
10+
int answer = 0;
11+
Stack<Integer> stack = new Stack<>();
12+
for(int i = 0; i<inputM.length; i++){
13+
int targetIndex=inputM[i]-1;
14+
int j = 0;
15+
while(j < n){
16+
int compare = inputArray[j][targetIndex];
17+
if(compare != 0){
18+
if(!stack.isEmpty() && stack.peek() == compare){
19+
answer+=2;
20+
stack.pop();
21+
}else{
22+
stack.push( compare );
23+
}
24+
inputArray[j][targetIndex] = 0;
25+
break;
26+
}else{
27+
j++;
28+
}
29+
}
30+
}
31+
return answer;
32+
}
33+
34+
public static void main(String[] args){
35+
Scanner sc = new Scanner(System.in);
36+
int n = sc.nextInt();
37+
sc.nextLine();
38+
int[][] inputArray = new int[n][n];
39+
for(int i = 0; i<n; i++){
40+
inputArray[i] = Arrays.stream( sc.nextLine().split( " ")).mapToInt( Integer::parseInt).toArray();
41+
}
42+
int m = sc.nextInt();
43+
sc.nextLine();
44+
int[] inputM = Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
45+
System.out.println(solution(n,m,inputArray,inputM));
46+
}
47+
}

0 commit comments

Comments
 (0)