Skip to content

feat: add solutions to lc problems: No.2803,2804 #1385

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: add new lc problems
  • Loading branch information
yanglbme committed Aug 4, 2023
commit 1537f52008067415132538698a5fe4a234220fbe
72 changes: 72 additions & 0 deletions solution/2800-2899/2803.Factorial Generator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# [2803. Factorial Generator](https://leetcode.cn/problems/factorial-generator)

[English Version](/solution/2800-2899/2803.Factorial%20Generator/README_EN.md)

## 题目描述

<!-- 这里写题目描述 -->

<p>Write a generator function that takes an integer <code>n</code> as an argument and returns a generator object which yields the <strong>factorial sequence</strong>.</p>

<p>The&nbsp;<strong>factorial sequence</strong>&nbsp;is defined by the relation <code>n!&nbsp;= n *&nbsp;<span style="font-size: 13px;">(</span>n-1)&nbsp;* (n-2)&nbsp;*&nbsp;...&nbsp;* 2 * 1​​​.</code></p>

<p>The factorial of 0 is defined as 1.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

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

<p><strong class="example">Example 2:</strong></p>

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

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> n = 0
<strong>Output:</strong> [1]
<strong>Explanation:</strong>
const gen = factorial(0)
gen.next().value // 1
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>0 &lt;= n &lt;= 18</code></li>
</ul>

## 解法

<!-- 这里可写通用的实现逻辑 -->

<!-- tabs:start -->

### **TypeScript**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```ts

```

<!-- tabs:end -->
66 changes: 66 additions & 0 deletions solution/2800-2899/2803.Factorial Generator/README_EN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# [2803. Factorial Generator](https://leetcode.com/problems/factorial-generator)

[中文文档](/solution/2800-2899/2803.Factorial%20Generator/README.md)

## Description

<p>Write a generator function that takes an integer <code>n</code> as an argument and returns a generator object which yields the <strong>factorial sequence</strong>.</p>

<p>The&nbsp;<strong>factorial sequence</strong>&nbsp;is defined by the relation <code>n!&nbsp;= n *&nbsp;<span style="font-size: 13px;">(</span>n-1)&nbsp;* (n-2)&nbsp;*&nbsp;...&nbsp;* 2 * 1​​​.</code></p>

<p>The factorial of 0 is defined as 1.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

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

<p><strong class="example">Example 2:</strong></p>

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

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> n = 0
<strong>Output:</strong> [1]
<strong>Explanation:</strong>
const gen = factorial(0)
gen.next().value // 1
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>0 &lt;= n &lt;= 18</code></li>
</ul>

## Solutions

<!-- tabs:start -->

### **TypeScript**

```ts

```

<!-- tabs:end -->
88 changes: 88 additions & 0 deletions solution/2800-2899/2804.Array Prototype ForEach/README.md
Original file line number Diff line number Diff line change
@@ -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)

## 题目描述

<!-- 这里写题目描述 -->

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

<p><code>callback</code> accepts the following arguments:</p>

<ul>
<li><code>value</code> -&nbsp;represents the current element being processed in the array. It is the value of the element in the current iteration.</li>
<li><code>index</code> -&nbsp;represents the index of the current element being processed in the array.</li>
<li><code>array</code> -&nbsp;represents the array itself, allowing access to the entire array within the callback function.</li>
</ul>

<p>The <code>context</code> is the object that should be passed as the function context parameter to the <code>callback</code> function, ensuring that the <code>this</code>&nbsp;keyword within the <code>callback</code> function refers to this <code>context</code> object.</p>

<p>Try to implement it without using the built-in array methods.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong>
arr = [1,2,3],
callback = (val, i, arr) =&gt; arr[i] = val * 2,
context = {&quot;context&quot;:true}
<strong>Output:</strong> [2,4,6]
<strong>Explanation:</strong>
arr.forEach(callback, context)&nbsp;
console.log(arr) // [2,4,6]

The callback is executed on each element of the array.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong>
arr = [true, true, false, false],
callback = (val, i, arr) =&gt; arr[i] = this,
context = {&quot;context&quot;: false}
<strong>Output:</strong> [{&quot;context&quot;:false},{&quot;context&quot;:false},{&quot;context&quot;:false},{&quot;context&quot;:false}]
<strong>Explanation:</strong>
arr.forEach(callback, context)&nbsp;
console.log(arr) // [{&quot;context&quot;:false},{&quot;context&quot;:false},{&quot;context&quot;:false},{&quot;context&quot;:false}]

The callback is executed on each element of the array with the right context.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong>
arr = [true, true, false, false],
callback = (val, i, arr) =&gt; arr[i] = !val,
context = {&quot;context&quot;: 5}
<strong>Output:</strong> [false,false,true,true]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>arr</code> is a valid JSON array</li>
<li><code>context</code> is a valid JSON object</li>
<li><code>fn</code> is a function</li>
<li><code>0 &lt;= arr.length &lt;= 10<sup>5</sup></code></li>
</ul>

## 解法

<!-- 这里可写通用的实现逻辑 -->

<!-- tabs:start -->

### **TypeScript**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```ts

```

<!-- tabs:end -->
82 changes: 82 additions & 0 deletions solution/2800-2899/2804.Array Prototype ForEach/README_EN.md
Original file line number Diff line number Diff line change
@@ -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

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

<p><code>callback</code> accepts the following arguments:</p>

<ul>
<li><code>value</code> -&nbsp;represents the current element being processed in the array. It is the value of the element in the current iteration.</li>
<li><code>index</code> -&nbsp;represents the index of the current element being processed in the array.</li>
<li><code>array</code> -&nbsp;represents the array itself, allowing access to the entire array within the callback function.</li>
</ul>

<p>The <code>context</code> is the object that should be passed as the function context parameter to the <code>callback</code> function, ensuring that the <code>this</code>&nbsp;keyword within the <code>callback</code> function refers to this <code>context</code> object.</p>

<p>Try to implement it without using the built-in array methods.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong>
arr = [1,2,3],
callback = (val, i, arr) =&gt; arr[i] = val * 2,
context = {&quot;context&quot;:true}
<strong>Output:</strong> [2,4,6]
<strong>Explanation:</strong>
arr.forEach(callback, context)&nbsp;
console.log(arr) // [2,4,6]

The callback is executed on each element of the array.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong>
arr = [true, true, false, false],
callback = (val, i, arr) =&gt; arr[i] = this,
context = {&quot;context&quot;: false}
<strong>Output:</strong> [{&quot;context&quot;:false},{&quot;context&quot;:false},{&quot;context&quot;:false},{&quot;context&quot;:false}]
<strong>Explanation:</strong>
arr.forEach(callback, context)&nbsp;
console.log(arr) // [{&quot;context&quot;:false},{&quot;context&quot;:false},{&quot;context&quot;:false},{&quot;context&quot;:false}]

The callback is executed on each element of the array with the right context.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong>
arr = [true, true, false, false],
callback = (val, i, arr) =&gt; arr[i] = !val,
context = {&quot;context&quot;: 5}
<strong>Output:</strong> [false,false,true,true]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>arr</code> is a valid JSON array</li>
<li><code>context</code> is a valid JSON object</li>
<li><code>fn</code> is a function</li>
<li><code>0 &lt;= arr.length &lt;= 10<sup>5</sup></code></li>
</ul>

## Solutions

<!-- tabs:start -->

### **TypeScript**

```ts

```

<!-- tabs:end -->
84 changes: 84 additions & 0 deletions solution/2800-2899/2805.Custom Interval/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# [2805. Custom Interval](https://leetcode.cn/problems/custom-interval)

[English Version](/solution/2800-2899/2805.Custom%20Interval/README_EN.md)

## 题目描述

<!-- 这里写题目描述 -->

<p><strong>Function&nbsp;</strong><code>customInterval</code></p>

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

<p><strong>Function </strong><code>customClearInterval</code>&nbsp;</p>

<p>Given the&nbsp;<code>id</code>. <code>id</code>&nbsp;is the&nbsp;returned value from&nbsp;the function&nbsp;<code>customInterval</code>. <code>customClearInterval</code>&nbsp;should stop executing&nbsp;provided function <code>fn</code> at intervals.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> delay = 50, period = 20, stopTime = 225
<strong>Output:</strong> [50,120,210]
<strong>Explanation:</strong>
const t = performance.now()&nbsp;&nbsp;
const result = []
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
const fn = () =&gt; {
result.push(Math.floor(performance.now() - t))
}
const id = customInterval(fn, delay, period)

setTimeout(() =&gt; {
customClearInterval(id)
}, 225)

50 + 20 * 0 = 50 // 50ms - 1st function call
50 + 20&nbsp;* 1 = 70 // 50ms + 70ms = 120ms - 2nd function call
50 + 20 * 2 = 90 // 50ms + 70ms + 90ms = 210ms - 3rd function call
</pre>

<p><strong class="example">Example 2:</strong></p>

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

<p><strong class="example">Example 3:</strong></p>

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

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>20 &lt;= delay, period &lt;= 250</code></li>
<li><code>20 &lt;= stopTime &lt;= 1000</code></li>
</ul>

## 解法

<!-- 这里可写通用的实现逻辑 -->

<!-- tabs:start -->

### **TypeScript**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```ts

```

<!-- tabs:end -->
Loading