-
Notifications
You must be signed in to change notification settings - Fork 2
/
organizing-containers-of-balls.js
112 lines (99 loc) · 3.19 KB
/
organizing-containers-of-balls.js
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
process.stdin.resume();
process.stdin.setEncoding('ascii');
var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;
process.stdin.on('data', function (data) {
input_stdin += data;
});
process.stdin.on('end', function () {
input_stdin_array = input_stdin.split("\n");
main();
});
function readLine() {
return input_stdin_array[input_currentline++];
}
/////////////// ignore above this line ////////////////////
class Organize {
constructor(props) {
this.possible = true;
this.n = props.length;
let count = new Array(this.n).fill(0);
let containers = [];
props.forEach((types) => {
let balls = 0;
types.forEach((type, index) => {
balls += type;
count[index] += type;
});
containers.push({
types,
balls,
target: -1,
});
})
this.containers = containers.map((container) => {
let target = 0;
while (target < count.length) {
if (count[target] == container.balls) {
container.target = target;
count[target] = -1;
break;
}
target++;
}
if (target == count.length) {
this.possible = false;
}
return container;
})
}
swap(container1, type1, container2, type2) {
this.containers[container1].types[type2]++;
this.containers[container1].types[type1]--;
this.containers[container2].types[type1]++;
this.containers[container2].types[type2]--;
}
swapAll(container1, container2) {
const type2 = this.containers[container1].target;
while (this.containers[container2].types[type2] > 0) {
let type1 = this.containers[container2].target;
if (this.containers[container1].types[type1] > 0) {
this.swap(container1, type1, container2, type2);
} else {
type1 = 0;
while (this.containers[container1].types[type1] == 0 && type1 < this.n) {
type1++;
}
this.swap(container1, type1, container2, type2);
}
}
}
check() {
for (let i = 0; i < this.n; i++) {
for (let j = i + 1; j < this.n; j++) {
this.swapAll(i, j);
}
}
}
}
function organizingContainers(container) {
// Complete this function
let organize = new Organize(container);
return organize.possible ? 'Possible' : 'Impossible';
//organize.check();
//console.log(organize.containers);
}
function main() {
var q = parseInt(readLine());
for(var a0 = 0; a0 < q; a0++){
var n = parseInt(readLine());
var container = [];
for(container_i = 0; container_i < n; container_i++){
container[container_i] = readLine().split(' ');
container[container_i] = container[container_i].map(Number);
}
var result = organizingContainers(container);
process.stdout.write("" + result + "\n");
}
}