Skip to content

Commit e6cdf2a

Browse files
update more solutions
1 parent c1ad1a6 commit e6cdf2a

File tree

6 files changed

+170
-0
lines changed

6 files changed

+170
-0
lines changed

Eazy/1603_DesignParkingSystem.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* @param {number} big
3+
* @param {number} medium
4+
* @param {number} small
5+
*/
6+
var ParkingSystem = function(big, medium, small) {
7+
this.big = big;
8+
this.medium = medium;
9+
this.small = small;
10+
this.space = {
11+
big: 0,
12+
medium: 0,
13+
small: 0,
14+
}
15+
};
16+
17+
/**
18+
* @param {number} carType
19+
* @return {boolean}
20+
*/
21+
ParkingSystem.prototype.addCar = function(carType) {
22+
switch(carType) {
23+
case 1:
24+
if (this.space.big === this.big) {
25+
return false;
26+
}
27+
this.space.big = this.space.big+1;
28+
return true;
29+
case 2:
30+
if (this.space.medium === this.medium) {
31+
return false;
32+
}
33+
this.space.medium = this.space.medium+1;
34+
return true;
35+
case 3:
36+
if (this.space.small === this.small) {
37+
return false;
38+
}
39+
this.space.small = this.space.small+1;
40+
return true;
41+
default: return false;
42+
}
43+
};
44+
45+
/**
46+
* Your ParkingSystem object will be instantiated and called as such:
47+
* var obj = new ParkingSystem(big, medium, small)
48+
* var param_1 = obj.addCar(carType)
49+
*/

Eazy/2695_ArrayWrapper.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @param {number[]} nums
3+
*/
4+
var ArrayWrapper = function(nums) {
5+
this.innerArray = nums
6+
};
7+
8+
ArrayWrapper.prototype.valueOf = function() {
9+
return this.innerArray.reduce((acc, val) => acc + val, 0)
10+
}
11+
12+
ArrayWrapper.prototype.toString = function() {
13+
return "[" + this.innerArray.join() + "]"
14+
}
15+
16+
/**
17+
* const obj1 = new ArrayWrapper([1,2]);
18+
* const obj2 = new ArrayWrapper([3,4]);
19+
* obj1 + obj2; // 10
20+
* String(obj1); // "[1,2]"
21+
* String(obj2); // "[3,4]"
22+
*/
23+
24+
export default ArrayWrapper;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {any[]} arr
3+
* @param {number} depth
4+
* @return {any[]}
5+
*/
6+
var flat = function (arr, n) {
7+
if(n === 0) {
8+
return arr;
9+
}
10+
11+
let re = []
12+
for(let el of arr) {
13+
if(Array.isArray(el)) {
14+
re.push(...flat(el, n-1)) ;
15+
}else {
16+
re.push(el)
17+
}
18+
}
19+
20+
return re
21+
};
22+
23+
export default flat;

Medium/2631_GroupBy.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param {Function} fn
3+
* @return {Array}
4+
*/
5+
Array.prototype.groupBy = function(fn) {
6+
const groupedData = {};
7+
8+
for(const el of this) {
9+
const key = fn(el);
10+
11+
if(!groupedData[key]) {
12+
groupedData[key] = [el];
13+
}else {
14+
let currentData = groupedData[key];
15+
currentData.push(el)
16+
groupedData[key] = currentData;
17+
}
18+
}
19+
20+
return groupedData;
21+
};
22+
23+
/**
24+
* [1,2,3].groupBy(String) // {"1":[1],"2":[2],"3":[3]}
25+
*/
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* @param {Object} context
3+
* @param {any[]} args
4+
* @return {any}
5+
*/
6+
Function.prototype.callPolyfill = function(context, ...args) {
7+
return this.call(context, ...args);
8+
}
9+
10+
/**
11+
* function increment() { this.count++; return this.count; }
12+
* increment.callPolyfill({count: 1}); // 2
13+
*/

Medium/2694_EventEmitter.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class EventEmitter {
2+
eventOrder = 0;
3+
eventList = [];
4+
5+
subscribe(event, cb) {
6+
const eventId = ++this.eventOrder;
7+
8+
this.eventList.push({eid: eventId, eventItem: event, callback: cb});
9+
return {
10+
unsubscribe: () => {
11+
return this.eventList.splice(this.eventList.indexOf(el => el.eid === eventId) - 1, 1);
12+
}
13+
};
14+
}
15+
emit(event, args = []) {
16+
const currentEventList = this.eventList.filter(evt => evt.eventItem === event);
17+
18+
const resultList = currentEventList.map(evt => evt.callback(...args));
19+
20+
return resultList;
21+
}
22+
}
23+
24+
/**
25+
* const emitter = new EventEmitter();
26+
*
27+
* // Subscribe to the onClick event with onClickCallback
28+
* function onClickCallback() { return 99 }
29+
* const sub = emitter.subscribe('onClick', onClickCallback);
30+
*
31+
* emitter.emit('onClick'); // [99]
32+
* sub.unsubscribe(); // undefined
33+
* emitter.emit('onClick'); // []
34+
*/
35+
36+
export default EventEmitter;

0 commit comments

Comments
 (0)