-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pair Of Shoes
50 lines (36 loc) · 1.16 KB
/
Pair Of Shoes
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
Yesterday you found some shoes in your room. Each shoe is described by two values:
type indicates if it's a left or a right shoe;
size is the size of the shoe.
Your task is to check whether it is possible to pair the shoes you found in such a way that each pair consists of a right and a left shoe of an equal size.
Example
For:
shoes = [[0, 21],
[1, 23],
[1, 21],
[0, 23]]
the output should be true;
For:
shoes = [[0, 21],
[1, 23],
[1, 21],
[1, 23]]
the output should be false.
Input/Output
[input] 2D integer array shoes
Array of shoes. Each shoe is given in the format [type, size], where type is either 0 or 1 for left and right respectively, and size is a positive integer.
Constraints: 2 ≤ shoes.length ≤ 50, 1 ≤ shoes[i][1] ≤ 100.
[output] a boolean value
true if it is possible to pair the shoes, false otherwise.
Solution
function pairOfShoes(shoes) {
const result = shoes.reduce((acc, [side, size]) => {
const action = side ? 1 : -1
acc[size]
? acc[size] += action
: acc[size] = action
return acc;
}, {});
return Object
.values(result)
.every((item) => item === 0);
}