-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_150364.java
More file actions
114 lines (100 loc) · 3.46 KB
/
_150364.java
File metadata and controls
114 lines (100 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import java.util.*;
class Solution {
public class Node{
ArrayList<Integer> Route = new ArrayList<>();
int nextIndex = 0;
Queue<Integer> Balls = new ArrayDeque<>();
int score = 0;
public void addRoute(int n){
Route.add(n);
}
public void sortRoute(){
Collections.sort(Route);
}
public void addBall(int i){
Balls.add(i);
}
public void pass(){
nextIndex++;
if(nextIndex==Route.size()) nextIndex=0;
}
public int getNext(){
return Route.get(nextIndex);
}
public boolean hasRoute(){
return Route.size()>0;
}
}
Node[] Nodes;
int ScoreLeftNodeCnt = 0;
public int[] solution(int[][] edges, int[] target) {
int N = target.length;
// 트리 초기화
Nodes = new Node[N];
for(int i=0; i<N; i++){
Nodes[i] = new Node();
}
// 트리 입력
for(int[] e : edges){
Nodes[e[0]-1].addRoute(e[1]-1);
}
for(int i=0; i<N; i++){
Nodes[i].sortRoute();
}
// 점수가 필요한 노드 수
for(int e : target){
if(e>0){
ScoreLeftNodeCnt++;
}
}
// 공 넣기
int ballCnt=0;
while(ScoreLeftNodeCnt>0){
// 공이 도착할 인덱스 탐색
int index = 0;
while(Nodes[index].hasRoute()){
int nextIndex = Nodes[index].getNext();
Nodes[index].pass();
index = nextIndex;
}
// 목표 점수가 0인데 공이 들어갈 경우
if(target[index]==0) return new int[]{-1};
// 넣었을 때 목표 점수보다 최솟값이 높아질 경우
else if(target[index] < Nodes[index].Balls.size()+1) return new int[]{-1};
// 넣었을 때 목표 점수를 만들 수 있도록 된 경우
else if( target[index] > Nodes[index].Balls.size()*3 && target[index] <= (Nodes[index].Balls.size()+1)*3 ){
ScoreLeftNodeCnt--;
}
Nodes[index].addBall(ballCnt);
ballCnt++;
}
// // 각 노드에 들어간 공 인덱스
// for(int i=0; i<N; i++){
// System.out.print((i+1)+": ");
// for(int ball : Nodes[i].Balls){
// System.out.print((ball+1)+",");
// }
// System.out.println();
// }
// System.out.println();
// 공 값 구하기
int[] result = new int[ballCnt];
for(int i=0; i<N; i++){
Queue<Integer> Balls = Nodes[i].Balls;
int targetValue = target[i];
for(int ballIndex : Balls){
int value = 1;
// 사전 순으로 가장 빠른 경우를 구하기 위한 연산
if(targetValue == Balls.size()*3) value = 3;
else if(targetValue == ((Balls.size()-1)*3) +2) value = 2;
else if(targetValue == ((Balls.size()-1)*3) +1) value = 1;
result[Balls.poll()] = value;
targetValue -= value;
}
}
// for(int i=0; i<ballCnt; i++){
// System.out.println(result[i]);
// }
return result;
}
}