diff --git a/src/main/kotlin/g2601_2700/s2619_array_prototype_last/solution.ts b/src/main/kotlin/g2601_2700/s2619_array_prototype_last/solution.ts index c79440611..9f5e789cc 100644 --- a/src/main/kotlin/g2601_2700/s2619_array_prototype_last/solution.ts +++ b/src/main/kotlin/g2601_2700/s2619_array_prototype_last/solution.ts @@ -7,7 +7,7 @@ declare global { } Array.prototype.last = function () { //NOSONAR - return this.length !== 0 ? this[this.length - 1] : -1 + return this.length !== 0 ? this[this.length - 1] : -1 //NOSONAR } /* @@ -15,4 +15,4 @@ Array.prototype.last = function () { //NOSONAR * arr.last(); // 3 */ -export {} +export {} //NOSONAR diff --git a/src/main/kotlin/g2601_2700/s2624_snail_traversal/solution.ts b/src/main/kotlin/g2601_2700/s2624_snail_traversal/solution.ts index d51e4f83e..4c29f5af8 100644 --- a/src/main/kotlin/g2601_2700/s2624_snail_traversal/solution.ts +++ b/src/main/kotlin/g2601_2700/s2624_snail_traversal/solution.ts @@ -24,4 +24,4 @@ Array.prototype.snail = function (rowsCount: number, colsCount: number): number[ * arr.snail(1,4); // [[1,2,3,4]] */ -export {} +export {} //NOSONAR diff --git a/src/main/kotlin/g2601_2700/s2626_array_reduce_transformation/solution.ts b/src/main/kotlin/g2601_2700/s2626_array_reduce_transformation/solution.ts index 14d4c31f3..db8eb1a7c 100644 --- a/src/main/kotlin/g2601_2700/s2626_array_reduce_transformation/solution.ts +++ b/src/main/kotlin/g2601_2700/s2626_array_reduce_transformation/solution.ts @@ -4,7 +4,7 @@ type Fn = (accum: number, curr: number) => number function reduce(nums: number[], fn: Fn, init: number): number { let accumulator = init - nums.forEach((num) => { + nums.forEach((num) => { //NOSONAR accumulator = fn(accumulator, num) }) return accumulator diff --git a/src/main/kotlin/g2601_2700/s2630_memoize_ii/solution.ts b/src/main/kotlin/g2601_2700/s2630_memoize_ii/solution.ts index ad10c8dc2..edec30866 100644 --- a/src/main/kotlin/g2601_2700/s2630_memoize_ii/solution.ts +++ b/src/main/kotlin/g2601_2700/s2630_memoize_ii/solution.ts @@ -36,7 +36,7 @@ function memoize(fn: Fn): Fn { let value = fn(...args) - currentCache.set(args[args.length - 1], value) + currentCache.set(args[args.length - 1], value) //NOSONAR return value } } diff --git a/src/main/kotlin/g2601_2700/s2631_group_by/solution.ts b/src/main/kotlin/g2601_2700/s2631_group_by/solution.ts index d0d798e13..fac77e3ae 100644 --- a/src/main/kotlin/g2601_2700/s2631_group_by/solution.ts +++ b/src/main/kotlin/g2601_2700/s2631_group_by/solution.ts @@ -23,4 +23,4 @@ Array.prototype.groupBy = function (fn: (item: T) => string) { //NOSONAR * [1,2,3].groupBy(String) // {"1":[1],"2":[2],"3":[3]} */ -export {} +export {} //NOSONAR diff --git a/src/main/kotlin/g2601_2700/s2693_call_function_with_custom_context/solution.ts b/src/main/kotlin/g2601_2700/s2693_call_function_with_custom_context/solution.ts index f78ad1bb3..37e8fb02c 100644 --- a/src/main/kotlin/g2601_2700/s2693_call_function_with_custom_context/solution.ts +++ b/src/main/kotlin/g2601_2700/s2693_call_function_with_custom_context/solution.ts @@ -16,4 +16,4 @@ Function.prototype.callPolyfill = function (context, ...args): any { //NOSONAR * increment.callPolyfill({count: 1}); // 2 */ -export {} +export {} //NOSONAR diff --git a/src/main/kotlin/g2601_2700/s2694_event_emitter/solution.ts b/src/main/kotlin/g2601_2700/s2694_event_emitter/solution.ts index 7bbcce03b..5881f9b8a 100644 --- a/src/main/kotlin/g2601_2700/s2694_event_emitter/solution.ts +++ b/src/main/kotlin/g2601_2700/s2694_event_emitter/solution.ts @@ -1,4 +1,4 @@ -// #Medium #2023_07_29_Time_45_ms_(99.58%)_Space_44.6_MB_(72.08%) +// #Medium #2023_09_13_Time_50_ms_(90.72%)_Space_45.2_MB_(5.06%) type Callback = (...args: any[]) => any type Subscription = { @@ -6,18 +6,34 @@ type Subscription = { } class EventEmitter { - subs: Record = {} + eventMap: Map> + + constructor() { + this.eventMap = new Map() + } subscribe(eventName: string, callback: Callback): Subscription { - if (!this.subs[eventName]) this.subs[eventName] = [] - const idx = this.subs[eventName].push(callback) - 1 + if (this.eventMap.has(eventName)) { + const set = this.eventMap.get(eventName)! + set.add(callback) + this.eventMap.set(eventName, set) + } else { + const set = new Set() + set.add(callback) + this.eventMap.set(eventName, set) + } + return { - unsubscribe: () => this.subs[eventName].splice(idx, 1), + unsubscribe: () => { + this.eventMap.get(eventName).delete(callback) + }, } } emit(eventName: string, args: any[] = []): any[] { - return this.subs[eventName]?.map((callback) => callback(...args)) || [] + const res = [] + this.eventMap.get(eventName)?.forEach((cb) => res.push(cb(...args))) //NOSONAR + return res } } diff --git a/src/main/kotlin/g2701_2800/s2705_compact_object/solution.ts b/src/main/kotlin/g2701_2800/s2705_compact_object/solution.ts index 9950df2e1..c57b14bd7 100644 --- a/src/main/kotlin/g2701_2800/s2705_compact_object/solution.ts +++ b/src/main/kotlin/g2701_2800/s2705_compact_object/solution.ts @@ -1,11 +1,11 @@ -// #Medium #2023_07_29_Time_78_ms_(99.38%)_Space_53.4_MB_(71.88%) +// #Medium #2023_09_14_Time_80_ms_(88.30%)_Space_53.2_MB_(70.41%) type Obj = Record function compactObject(obj: Obj): Obj { if (Array.isArray(obj)) { let retArr = [] - obj.forEach((e, idx) => { + obj.forEach((e, idx) => { //NOSONAR if (e) { retArr.push(compactObject(e)) } diff --git a/src/main/kotlin/g2701_2800/s2726_calculator_with_method_chaining/solution.ts b/src/main/kotlin/g2701_2800/s2726_calculator_with_method_chaining/solution.ts index 73590aece..a387ead7d 100644 --- a/src/main/kotlin/g2701_2800/s2726_calculator_with_method_chaining/solution.ts +++ b/src/main/kotlin/g2701_2800/s2726_calculator_with_method_chaining/solution.ts @@ -1,4 +1,4 @@ -// #Easy #2023_08_03_Time_43_ms_(99.15%)_Space_43.4_MB_(9.15%) +// #Easy #2023_09_19_Time_39_ms_(99.67%)_Space_42.1_MB_(95.49%) class Calculator { init: number @@ -23,7 +23,7 @@ class Calculator { } divide(value: number): Calculator { //NOSONAR - if (value === 0) throw Error('Division by zero is not allowed') + if (value === 0) throw new Error('Division by zero is not allowed') this.init /= value return this }