-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
vasya-clerk.js
206 lines (197 loc) · 4.86 KB
/
vasya-clerk.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
function tickets(peopleInLine){
// need a cash register (place to keep track of bills we have)
const register = {
25: 0,
50: 0,
100: 0
};
// iterate over the line
for (let i = 0; i < peopleInLine.length; i++) {
const payment = peopleInLine[i];
// check if cash register has enough change
if (payment === 25) {
register[payment]++;
} else if (payment === 50 && register[25] > 0) {
register[25]--;
register[payment]++;
} else if (payment === 100) {
if (register[25] > 0 && register[50] > 0) {
register[25]--;
register[50]--;
register[payment]++;
} else if (register[25] > 2) {
register[25] -= 3;
register[payment]++;
} else {
return 'NO';
}
} else {
return 'NO';
}
}
return 'YES';
}
function tickets(peopleInLine){
// need a cash register (place to keep track of bills we have)
const register = {
25: 0,
50: 0,
100: 0
};
return peopleInLine.every(payment => {
if (payment === 25) {
register[payment]++;
} else if (payment === 50 && register[25] > 0) {
register[25]--;
register[payment]++;
} else if (payment === 100) {
if (register[25] > 0 && register[50] > 0) {
register[25]--;
register[50]--;
register[payment]++;
} else if (register[25] > 2) {
register[25] -= 3;
register[payment]++;
} else {
return false;
}
} else {
return false;
}
return true;
}) ? 'YES' : 'NO';
}
function tickets(peopleInLine){
// need a cash register (place to keep track of bills we have)
const register = {
25: 0,
50: 0,
100: 0
};
return peopleInLine.reduce((valid, payment) => {
if (payment === 25) {
register[payment]++;
} else if (payment === 50 && register[25] > 0) {
register[25]--;
register[payment]++;
} else if (payment === 100) {
if (register[25] > 0 && register[50] > 0) {
register[25]--;
register[50]--;
register[payment]++;
} else if (register[25] > 2) {
register[25] -= 3;
register[payment]++;
} else {
return 'NO';
}
} else {
return 'NO';
}
return valid != 'NO' ? 'YES' : 'NO';
}, '');
}
function tickets(peopleInLine){
const register = {
25: [],
50: [],
100: []
};
for (let i = 0; i < peopleInLine.length; i++) {
const payment = peopleInLine[i];
let canMakeChange = true;
if (payment === 50 && register[25].length > 0) {
register[25].pop();
} else if (payment === 100) {
if (register[25].length > 0 && register[50].length > 0) {
register[25].pop();
register[50].pop();
} else if (register[25].length > 2) {
register[25].pop();
register[25].pop();
register[25].pop();
} else {
return 'NO';
}
} else if (payment != 25) {
return 'NO';
}
if (canMakeChange) {
register[payment].push(0);
}
}
return 'YES';
}
function tickets(peopleInLine){
// need a cash register (place to keep track of bills we have)
const register = {
25: 0,
50: 0,
100: 0
};
// iterate over the line
for (let i = 0; i < peopleInLine.length; i++) {
const payment = peopleInLine[i];
// check if cash register has enough change
const result = {
25: () => {},
50: () => {
if (register[25] > 0) {
register[25]--;
} else {
return 'NO';
}
},
100: () => {
if (register[25] > 0 && register[50] > 0) {
register[25]--;
register[50]--;
} else if (register[25] > 2) {
register[25] -= 3;
} else {
return 'NO';
}
}
}[payment]();
if (result === 'NO') {
return 'NO';
} else {
register[payment]++;
}
}
return 'YES';
}
function tickets(peopleInLine, register = {
25: 0,
50: 0,
100: 0
}){
const checkPayment = {
25: () => true,
50: () => {
if (register[25] > 0) {
register[25]--;
return true;
} else {
return false;
}
},
100: () => {
if (register[25] > 0 && register[50] > 0) {
register[25]--;
register[50]--;
} else if (register[25] > 2) {
register[25] -= 3;
} else {
return false;
}
return true;
}
};
return peopleInLine.every(payment => (checkPayment[payment]() && (register[payment]++, true))) ? 'YES' : 'NO';
}
// const tickets = (peopleInLine) => peopleInLine.reduce(({ v, s, m }, c) => ({ v: v && (c === 25 ? ++s : (((s -= (c === 50 ? (m++, 1) : m ? (m--, 1) : 3)) < 0) ? 0 : 1)), s, m}), { v: 1, s: 0, m: 0 }).v ? "YES" : "NO";
console.log(tickets([50]), 'NO');
console.log(tickets([25, 25, 50, 50]), 'YES');
console.log(tickets([25, 100]), 'NO');
console.log(tickets([25, 25, 50, 50, 100]), 'NO');