From 1537f52008067415132538698a5fe4a234220fbe Mon Sep 17 00:00:00 2001 From: Yang Libin Date: Fri, 4 Aug 2023 01:15:19 +0000 Subject: [PATCH 1/2] feat: add new lc problems --- .../2803.Factorial Generator/README.md | 72 +++++++++++++++ .../2803.Factorial Generator/README_EN.md | 66 ++++++++++++++ .../2804.Array Prototype ForEach/README.md | 88 +++++++++++++++++++ .../2804.Array Prototype ForEach/README_EN.md | 82 +++++++++++++++++ .../2800-2899/2805.Custom Interval/README.md | 84 ++++++++++++++++++ .../2805.Custom Interval/README_EN.md | 78 ++++++++++++++++ solution/README.md | 3 + solution/README_EN.md | 3 + solution/summary.md | 3 + solution/summary_en.md | 3 + 10 files changed, 482 insertions(+) create mode 100644 solution/2800-2899/2803.Factorial Generator/README.md create mode 100644 solution/2800-2899/2803.Factorial Generator/README_EN.md create mode 100644 solution/2800-2899/2804.Array Prototype ForEach/README.md create mode 100644 solution/2800-2899/2804.Array Prototype ForEach/README_EN.md create mode 100644 solution/2800-2899/2805.Custom Interval/README.md create mode 100644 solution/2800-2899/2805.Custom Interval/README_EN.md diff --git a/solution/2800-2899/2803.Factorial Generator/README.md b/solution/2800-2899/2803.Factorial Generator/README.md new file mode 100644 index 0000000000000..6e53eee9938c8 --- /dev/null +++ b/solution/2800-2899/2803.Factorial Generator/README.md @@ -0,0 +1,72 @@ +# [2803. Factorial Generator](https://leetcode.cn/problems/factorial-generator) + +[English Version](/solution/2800-2899/2803.Factorial%20Generator/README_EN.md) + +## 题目描述 + + + +

Write a generator function that takes an integer n as an argument and returns a generator object which yields the factorial sequence.

+ +

The factorial sequence is defined by the relation n! = n * (n-1) * (n-2) * ... * 2 * 1​​​.

+ +

The factorial of 0 is defined as 1.

+ +

 

+

Example 1:

+ +
+Input: n = 5
+Output: [1,2,6,24,120]
+Explanation: 
+const gen = factorial(5)
+gen.next().value // 1
+gen.next().value // 2
+gen.next().value // 6
+gen.next().value // 24
+gen.next().value // 120
+
+ +

Example 2:

+ +
+Input: n = 2
+Output: [1,2]
+Explanation: 
+const gen = factorial(2) 
+gen.next().value // 1 
+gen.next().value // 2 
+
+ +

Example 3:

+ +
+Input: n = 0
+Output: [1]
+Explanation: 
+const gen = factorial(0) 
+gen.next().value // 1 
+
+ +

 

+

Constraints:

+ + + +## 解法 + + + + + +### **TypeScript** + + + +```ts + +``` + + diff --git a/solution/2800-2899/2803.Factorial Generator/README_EN.md b/solution/2800-2899/2803.Factorial Generator/README_EN.md new file mode 100644 index 0000000000000..3c860dab97e3e --- /dev/null +++ b/solution/2800-2899/2803.Factorial Generator/README_EN.md @@ -0,0 +1,66 @@ +# [2803. Factorial Generator](https://leetcode.com/problems/factorial-generator) + +[中文文档](/solution/2800-2899/2803.Factorial%20Generator/README.md) + +## Description + +

Write a generator function that takes an integer n as an argument and returns a generator object which yields the factorial sequence.

+ +

The factorial sequence is defined by the relation n! = n * (n-1) * (n-2) * ... * 2 * 1​​​.

+ +

The factorial of 0 is defined as 1.

+ +

 

+

Example 1:

+ +
+Input: n = 5
+Output: [1,2,6,24,120]
+Explanation: 
+const gen = factorial(5)
+gen.next().value // 1
+gen.next().value // 2
+gen.next().value // 6
+gen.next().value // 24
+gen.next().value // 120
+
+ +

Example 2:

+ +
+Input: n = 2
+Output: [1,2]
+Explanation: 
+const gen = factorial(2) 
+gen.next().value // 1 
+gen.next().value // 2 
+
+ +

Example 3:

+ +
+Input: n = 0
+Output: [1]
+Explanation: 
+const gen = factorial(0) 
+gen.next().value // 1 
+
+ +

 

+

Constraints:

+ + + +## Solutions + + + +### **TypeScript** + +```ts + +``` + + diff --git a/solution/2800-2899/2804.Array Prototype ForEach/README.md b/solution/2800-2899/2804.Array Prototype ForEach/README.md new file mode 100644 index 0000000000000..dab44d7f3c1c4 --- /dev/null +++ b/solution/2800-2899/2804.Array Prototype ForEach/README.md @@ -0,0 +1,88 @@ +# [2804. Array Prototype ForEach](https://leetcode.cn/problems/array-prototype-foreach) + +[English Version](/solution/2800-2899/2804.Array%20Prototype%20ForEach/README_EN.md) + +## 题目描述 + + + +

Write your version of method forEach that enhances all arrays such that you can call the array.forEach(callback, context) method on any array and it will execute callback on each element of the array. Method forEach should not return anything.

+ +

callback accepts the following arguments:

+ + + +

The context is the object that should be passed as the function context parameter to the callback function, ensuring that the this keyword within the callback function refers to this context object.

+ +

Try to implement it without using the built-in array methods.

+ +

 

+

Example 1:

+ +
+Input: 
+arr = [1,2,3], 
+callback = (val, i, arr) => arr[i] = val * 2, 
+context = {"context":true}
+Output: [2,4,6]
+Explanation: 
+arr.forEach(callback, context)  
+console.log(arr) // [2,4,6]
+
+The callback is executed on each element of the array.
+
+ +

Example 2:

+ +
+Input: 
+arr = [true, true, false, false], 
+callback = (val, i, arr) => arr[i] = this, 
+context = {"context": false}
+Output: [{"context":false},{"context":false},{"context":false},{"context":false}]
+Explanation: 
+arr.forEach(callback, context) 
+console.log(arr) // [{"context":false},{"context":false},{"context":false},{"context":false}]
+
+The callback is executed on each element of the array with the right context.
+
+ +

Example 3:

+ +
+Input: 
+arr = [true, true, false, false], 
+callback = (val, i, arr) => arr[i] = !val, 
+context = {"context": 5}
+Output: [false,false,true,true]
+
+ +

 

+

Constraints:

+ + + +## 解法 + + + + + +### **TypeScript** + + + +```ts + +``` + + diff --git a/solution/2800-2899/2804.Array Prototype ForEach/README_EN.md b/solution/2800-2899/2804.Array Prototype ForEach/README_EN.md new file mode 100644 index 0000000000000..e4777ccadc01b --- /dev/null +++ b/solution/2800-2899/2804.Array Prototype ForEach/README_EN.md @@ -0,0 +1,82 @@ +# [2804. Array Prototype ForEach](https://leetcode.com/problems/array-prototype-foreach) + +[中文文档](/solution/2800-2899/2804.Array%20Prototype%20ForEach/README.md) + +## Description + +

Write your version of method forEach that enhances all arrays such that you can call the array.forEach(callback, context) method on any array and it will execute callback on each element of the array. Method forEach should not return anything.

+ +

callback accepts the following arguments:

+ + + +

The context is the object that should be passed as the function context parameter to the callback function, ensuring that the this keyword within the callback function refers to this context object.

+ +

Try to implement it without using the built-in array methods.

+ +

 

+

Example 1:

+ +
+Input: 
+arr = [1,2,3], 
+callback = (val, i, arr) => arr[i] = val * 2, 
+context = {"context":true}
+Output: [2,4,6]
+Explanation: 
+arr.forEach(callback, context)  
+console.log(arr) // [2,4,6]
+
+The callback is executed on each element of the array.
+
+ +

Example 2:

+ +
+Input: 
+arr = [true, true, false, false], 
+callback = (val, i, arr) => arr[i] = this, 
+context = {"context": false}
+Output: [{"context":false},{"context":false},{"context":false},{"context":false}]
+Explanation: 
+arr.forEach(callback, context) 
+console.log(arr) // [{"context":false},{"context":false},{"context":false},{"context":false}]
+
+The callback is executed on each element of the array with the right context.
+
+ +

Example 3:

+ +
+Input: 
+arr = [true, true, false, false], 
+callback = (val, i, arr) => arr[i] = !val, 
+context = {"context": 5}
+Output: [false,false,true,true]
+
+ +

 

+

Constraints:

+ + + +## Solutions + + + +### **TypeScript** + +```ts + +``` + + diff --git a/solution/2800-2899/2805.Custom Interval/README.md b/solution/2800-2899/2805.Custom Interval/README.md new file mode 100644 index 0000000000000..6f807ba470235 --- /dev/null +++ b/solution/2800-2899/2805.Custom Interval/README.md @@ -0,0 +1,84 @@ +# [2805. Custom Interval](https://leetcode.cn/problems/custom-interval) + +[English Version](/solution/2800-2899/2805.Custom%20Interval/README_EN.md) + +## 题目描述 + + + +

Function customInterval

+ +

Given a function fn, a number delay and a number period, return a number id. customInterval is a function that should execute the provided function fn at intervals based on a linear pattern defined by the formula delay + period * count. The count in the formula represents the number of times the interval has been executed starting from an initial value of 0.

+ +

Function customClearInterval 

+ +

Given the id. id is the returned value from the function customInterval. customClearInterval should stop executing provided function fn at intervals.

+ +

 

+

Example 1:

+ +
+Input: delay = 50, period = 20, stopTime = 225
+Output: [50,120,210]
+Explanation: 
+const t = performance.now()  
+const result = []
+        
+const fn = () => {
+    result.push(Math.floor(performance.now() - t))
+}
+const id = customInterval(fn, delay, period)
+        
+setTimeout(() => {
+    customClearInterval(id)
+}, 225)
+
+50 + 20 * 0 = 50 // 50ms - 1st function call
+50 + 20 * 1 = 70 // 50ms + 70ms = 120ms - 2nd function call
+50 + 20 * 2 = 90 // 50ms + 70ms + 90ms = 210ms - 3rd function call
+
+ +

Example 2:

+ +
+Input: delay = 20, period = 20, stopTime = 150
+Output: [20,60,120]
+Explanation: 
+20 + 20 * 0 = 20 // 20ms - 1st function call
+20 + 20 * 1 = 40 // 20ms + 40ms = 60ms - 2nd function call
+20 + 20 * 2 = 60 // 20ms + 40ms + 60ms = 120ms - 3rd function call
+
+ +

Example 3:

+ +
+Input: delay = 100, period = 200, stopTime = 500
+Output: [100,400]
+Explanation: 
+100 + 200 * 0 = 100 // 100ms - 1st function call
+100 + 200 * 1 = 300 // 100ms + 300ms = 400ms - 2nd function call
+
+ +

 

+

Constraints:

+ + + +## 解法 + + + + + +### **TypeScript** + + + +```ts + +``` + + diff --git a/solution/2800-2899/2805.Custom Interval/README_EN.md b/solution/2800-2899/2805.Custom Interval/README_EN.md new file mode 100644 index 0000000000000..56dc6794ccaa0 --- /dev/null +++ b/solution/2800-2899/2805.Custom Interval/README_EN.md @@ -0,0 +1,78 @@ +# [2805. Custom Interval](https://leetcode.com/problems/custom-interval) + +[中文文档](/solution/2800-2899/2805.Custom%20Interval/README.md) + +## Description + +

Function customInterval

+ +

Given a function fn, a number delay and a number period, return a number id. customInterval is a function that should execute the provided function fn at intervals based on a linear pattern defined by the formula delay + period * count. The count in the formula represents the number of times the interval has been executed starting from an initial value of 0.

+ +

Function customClearInterval 

+ +

Given the id. id is the returned value from the function customInterval. customClearInterval should stop executing provided function fn at intervals.

+ +

 

+

Example 1:

+ +
+Input: delay = 50, period = 20, stopTime = 225
+Output: [50,120,210]
+Explanation: 
+const t = performance.now()  
+const result = []
+        
+const fn = () => {
+    result.push(Math.floor(performance.now() - t))
+}
+const id = customInterval(fn, delay, period)
+        
+setTimeout(() => {
+    customClearInterval(id)
+}, 225)
+
+50 + 20 * 0 = 50 // 50ms - 1st function call
+50 + 20 * 1 = 70 // 50ms + 70ms = 120ms - 2nd function call
+50 + 20 * 2 = 90 // 50ms + 70ms + 90ms = 210ms - 3rd function call
+
+ +

Example 2:

+ +
+Input: delay = 20, period = 20, stopTime = 150
+Output: [20,60,120]
+Explanation: 
+20 + 20 * 0 = 20 // 20ms - 1st function call
+20 + 20 * 1 = 40 // 20ms + 40ms = 60ms - 2nd function call
+20 + 20 * 2 = 60 // 20ms + 40ms + 60ms = 120ms - 3rd function call
+
+ +

Example 3:

+ +
+Input: delay = 100, period = 200, stopTime = 500
+Output: [100,400]
+Explanation: 
+100 + 200 * 0 = 100 // 100ms - 1st function call
+100 + 200 * 1 = 300 // 100ms + 300ms = 400ms - 2nd function call
+
+ +

 

+

Constraints:

+ + + +## Solutions + + + +### **TypeScript** + +```ts + +``` + + diff --git a/solution/README.md b/solution/README.md index f129500045c0e..cf609f7530c7a 100644 --- a/solution/README.md +++ b/solution/README.md @@ -2813,6 +2813,9 @@ | 2800 | [包含三个字符串的最短字符串](/solution/2800-2899/2800.Shortest%20String%20That%20Contains%20Three%20Strings/README.md) | `贪心`,`字符串`,`枚举` | 中等 | 第 356 场周赛 | | 2801 | [统计范围内的步进数字数目](/solution/2800-2899/2801.Count%20Stepping%20Numbers%20in%20Range/README.md) | `字符串`,`动态规划` | 困难 | 第 356 场周赛 | | 2802 | [Find The K-th Lucky Number](/solution/2800-2899/2802.Find%20The%20K-th%20Lucky%20Number/README.md) | | 中等 | 🔒 | +| 2803 | [Factorial Generator](/solution/2800-2899/2803.Factorial%20Generator/README.md) | | 简单 | 🔒 | +| 2804 | [Array Prototype ForEach](/solution/2800-2899/2804.Array%20Prototype%20ForEach/README.md) | | 简单 | 🔒 | +| 2805 | [Custom Interval](/solution/2800-2899/2805.Custom%20Interval/README.md) | | 中等 | 🔒 | ## 版权 diff --git a/solution/README_EN.md b/solution/README_EN.md index 9f73573ab2e7e..771d9e01f45de 100644 --- a/solution/README_EN.md +++ b/solution/README_EN.md @@ -2811,6 +2811,9 @@ Press Control+F(or Command+F on the | 2800 | [Shortest String That Contains Three Strings](/solution/2800-2899/2800.Shortest%20String%20That%20Contains%20Three%20Strings/README_EN.md) | `Greedy`,`String`,`Enumeration` | Medium | Weekly Contest 356 | | 2801 | [Count Stepping Numbers in Range](/solution/2800-2899/2801.Count%20Stepping%20Numbers%20in%20Range/README_EN.md) | `String`,`Dynamic Programming` | Hard | Weekly Contest 356 | | 2802 | [Find The K-th Lucky Number](/solution/2800-2899/2802.Find%20The%20K-th%20Lucky%20Number/README_EN.md) | | Medium | 🔒 | +| 2803 | [Factorial Generator](/solution/2800-2899/2803.Factorial%20Generator/README_EN.md) | | Easy | 🔒 | +| 2804 | [Array Prototype ForEach](/solution/2800-2899/2804.Array%20Prototype%20ForEach/README_EN.md) | | Easy | 🔒 | +| 2805 | [Custom Interval](/solution/2800-2899/2805.Custom%20Interval/README_EN.md) | | Medium | 🔒 | ## Copyright diff --git a/solution/summary.md b/solution/summary.md index bb4c00f0b3e4b..0b7f44e0b3a19 100644 --- a/solution/summary.md +++ b/solution/summary.md @@ -2858,3 +2858,6 @@ - [2800.包含三个字符串的最短字符串](/solution/2800-2899/2800.Shortest%20String%20That%20Contains%20Three%20Strings/README.md) - [2801.统计范围内的步进数字数目](/solution/2800-2899/2801.Count%20Stepping%20Numbers%20in%20Range/README.md) - [2802.Find The K-th Lucky Number](/solution/2800-2899/2802.Find%20The%20K-th%20Lucky%20Number/README.md) + - [2803.Factorial Generator](/solution/2800-2899/2803.Factorial%20Generator/README.md) + - [2804.Array Prototype ForEach](/solution/2800-2899/2804.Array%20Prototype%20ForEach/README.md) + - [2805.Custom Interval](/solution/2800-2899/2805.Custom%20Interval/README.md) diff --git a/solution/summary_en.md b/solution/summary_en.md index c027f187ca48c..3f1c49a4b6054 100644 --- a/solution/summary_en.md +++ b/solution/summary_en.md @@ -2858,3 +2858,6 @@ - [2800.Shortest String That Contains Three Strings](/solution/2800-2899/2800.Shortest%20String%20That%20Contains%20Three%20Strings/README_EN.md) - [2801.Count Stepping Numbers in Range](/solution/2800-2899/2801.Count%20Stepping%20Numbers%20in%20Range/README_EN.md) - [2802.Find The K-th Lucky Number](/solution/2800-2899/2802.Find%20The%20K-th%20Lucky%20Number/README_EN.md) + - [2803.Factorial Generator](/solution/2800-2899/2803.Factorial%20Generator/README_EN.md) + - [2804.Array Prototype ForEach](/solution/2800-2899/2804.Array%20Prototype%20ForEach/README_EN.md) + - [2805.Custom Interval](/solution/2800-2899/2805.Custom%20Interval/README_EN.md) From 4e6477f03eb1bf2328ad781edc9f48d52f6acc86 Mon Sep 17 00:00:00 2001 From: Yang Libin Date: Fri, 4 Aug 2023 01:38:06 +0000 Subject: [PATCH 2/2] feat: add solutions to lc problems: No.2803,2804 * No.2803.Factorial Generator * No.2804.Array Prototype ForEach --- .../2803.Factorial Generator/README.md | 17 ++++++++++++++++- .../2803.Factorial Generator/README_EN.md | 17 ++++++++++++++++- .../2803.Factorial Generator/Solution.ts | 16 ++++++++++++++++ .../2804.Array Prototype ForEach/README.md | 16 +++++++++++++++- .../2804.Array Prototype ForEach/README_EN.md | 16 +++++++++++++++- .../2804.Array Prototype ForEach/Solution.ts | 15 +++++++++++++++ 6 files changed, 93 insertions(+), 4 deletions(-) create mode 100644 solution/2800-2899/2803.Factorial Generator/Solution.ts create mode 100644 solution/2800-2899/2804.Array Prototype ForEach/Solution.ts diff --git a/solution/2800-2899/2803.Factorial Generator/README.md b/solution/2800-2899/2803.Factorial Generator/README.md index 6e53eee9938c8..683d08d87ccff 100644 --- a/solution/2800-2899/2803.Factorial Generator/README.md +++ b/solution/2800-2899/2803.Factorial Generator/README.md @@ -66,7 +66,22 @@ gen.next().value // 1 ```ts - +function* factorial(n: number): Generator { + if (n === 0) { + yield 1; + } + let ans = 1; + for (let i = 1; i <= n; ++i) { + ans *= i; + yield ans; + } +} + +/** + * const gen = factorial(2); + * gen.next().value; // 1 + * gen.next().value; // 2 + */ ``` diff --git a/solution/2800-2899/2803.Factorial Generator/README_EN.md b/solution/2800-2899/2803.Factorial Generator/README_EN.md index 3c860dab97e3e..af14e586fc465 100644 --- a/solution/2800-2899/2803.Factorial Generator/README_EN.md +++ b/solution/2800-2899/2803.Factorial Generator/README_EN.md @@ -60,7 +60,22 @@ gen.next().value // 1 ### **TypeScript** ```ts - +function* factorial(n: number): Generator { + if (n === 0) { + yield 1; + } + let ans = 1; + for (let i = 1; i <= n; ++i) { + ans *= i; + yield ans; + } +} + +/** + * const gen = factorial(2); + * gen.next().value; // 1 + * gen.next().value; // 2 + */ ``` diff --git a/solution/2800-2899/2803.Factorial Generator/Solution.ts b/solution/2800-2899/2803.Factorial Generator/Solution.ts new file mode 100644 index 0000000000000..739e63183f250 --- /dev/null +++ b/solution/2800-2899/2803.Factorial Generator/Solution.ts @@ -0,0 +1,16 @@ +function* factorial(n: number): Generator { + if (n === 0) { + yield 1; + } + let ans = 1; + for (let i = 1; i <= n; ++i) { + ans *= i; + yield ans; + } +} + +/** + * const gen = factorial(2); + * gen.next().value; // 1 + * gen.next().value; // 2 + */ diff --git a/solution/2800-2899/2804.Array Prototype ForEach/README.md b/solution/2800-2899/2804.Array Prototype ForEach/README.md index dab44d7f3c1c4..763e05f08e1aa 100644 --- a/solution/2800-2899/2804.Array Prototype ForEach/README.md +++ b/solution/2800-2899/2804.Array Prototype ForEach/README.md @@ -82,7 +82,21 @@ context = {"context": 5} ```ts - +Array.prototype.forEach = function (callback: Function, context: any): void { + for (let i = 0; i < this.length; ++i) { + callback.call(context, this[i], i, this); + } +}; + +/** + * const arr = [1,2,3]; + * const callback = (val, i, arr) => arr[i] = val * 2; + * const context = {"context":true}; + * + * arr.forEach(callback, context) + * + * console.log(arr) // [2,4,6] + */ ``` diff --git a/solution/2800-2899/2804.Array Prototype ForEach/README_EN.md b/solution/2800-2899/2804.Array Prototype ForEach/README_EN.md index e4777ccadc01b..ebb6566ac1b53 100644 --- a/solution/2800-2899/2804.Array Prototype ForEach/README_EN.md +++ b/solution/2800-2899/2804.Array Prototype ForEach/README_EN.md @@ -76,7 +76,21 @@ context = {"context": 5} ### **TypeScript** ```ts - +Array.prototype.forEach = function (callback: Function, context: any): void { + for (let i = 0; i < this.length; ++i) { + callback.call(context, this[i], i, this); + } +}; + +/** + * const arr = [1,2,3]; + * const callback = (val, i, arr) => arr[i] = val * 2; + * const context = {"context":true}; + * + * arr.forEach(callback, context) + * + * console.log(arr) // [2,4,6] + */ ``` diff --git a/solution/2800-2899/2804.Array Prototype ForEach/Solution.ts b/solution/2800-2899/2804.Array Prototype ForEach/Solution.ts new file mode 100644 index 0000000000000..0a926f59a61cb --- /dev/null +++ b/solution/2800-2899/2804.Array Prototype ForEach/Solution.ts @@ -0,0 +1,15 @@ +Array.prototype.forEach = function (callback: Function, context: any): void { + for (let i = 0; i < this.length; ++i) { + callback.call(context, this[i], i, this); + } +}; + +/** + * const arr = [1,2,3]; + * const callback = (val, i, arr) => arr[i] = val * 2; + * const context = {"context":true}; + * + * arr.forEach(callback, context) + * + * console.log(arr) // [2,4,6] + */