Skip to content

Commit 4f1ca66

Browse files
committed
feat: add new lc problems
1 parent 5e84dae commit 4f1ca66

File tree

14 files changed

+642
-32
lines changed

14 files changed

+642
-32
lines changed

solution/0300-0399/0336.Palindrome Pairs/README.md

-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@
3232
<strong>输出:</strong>[[0,1],[1,0]]
3333
</pre>
3434

35-
36-
3735
<p><strong>提示:</strong></p>
3836

3937
<ul>

solution/0500-0599/0554.Brick Wall/README.md

-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
<strong>输出:</strong>3
2929
</pre>
3030

31-
32-
3331
<p><strong>提示:</strong></p>
3432

3533
<ul>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
# [2675. Array of Objects to Matrix](https://leetcode.cn/problems/array-of-objects-to-matrix)
2+
3+
[English Version](/solution/2600-2699/2675.Array%20of%20Objects%20to%20Matrix/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>Write a function that converts an array of objects&nbsp;<code>arr</code> into a matrix <code>m</code>.</p>
10+
11+
<p><code>arr</code>&nbsp;is an array of objects or arrays. Each item in the array can be deeply nested with child arrays and child objects. It can also contain numbers, strings, booleans, and&nbsp;null values.</p>
12+
13+
<p>The first row <code>m</code>&nbsp;should be the column names. If there is no nesting, the column names are the unique keys within the objects. If there is nesting, the column names&nbsp;are the respective paths in the object separated by <code>&quot;.&quot;</code>.</p>
14+
15+
<p>Each of the remaining rows corresponds to an object in&nbsp;<code>arr</code>. Each value in the matrix corresponds to a value in an object. If a given object doesn&#39;t contain a value for a given column, the cell should contain an empty string&nbsp;<code>&quot;&quot;</code>.</p>
16+
17+
<p>The colums in the matrix should be in <strong>lexographically ascending</strong> order.</p>
18+
19+
<p>&nbsp;</p>
20+
<p><strong class="example">Example 1:</strong></p>
21+
22+
<pre>
23+
<strong>Input:</strong>
24+
arr = [
25+
&nbsp; {&quot;b&quot;: 1, &quot;a&quot;: 2},
26+
&nbsp; {&quot;b&quot;: 3, &quot;a&quot;: 4}
27+
]
28+
<strong>Output:</strong>
29+
[
30+
&nbsp; [&quot;a&quot;, &quot;b&quot;],
31+
&nbsp; [2, 1],
32+
&nbsp; [4, 3]
33+
]
34+
35+
<strong>Explanation:</strong>
36+
There are two unique column names in the two objects: &quot;a&quot; and &quot;b&quot;.
37+
&quot;a&quot; corresponds with [2, 4].
38+
&quot;b&quot; coresponds with [1, 3].
39+
</pre>
40+
41+
<p><strong class="example">Example 2:</strong></p>
42+
43+
<pre>
44+
<strong>Input:</strong>
45+
arr = [
46+
&nbsp; {&quot;a&quot;: 1, &quot;b&quot;: 2},
47+
&nbsp; {&quot;c&quot;: 3, &quot;d&quot;: 4},
48+
&nbsp; {}
49+
]
50+
<strong>Output:</strong>
51+
[
52+
&nbsp; [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;],
53+
&nbsp; [1, 2, &quot;&quot;, &quot;&quot;],
54+
&nbsp; [&quot;&quot;, &quot;&quot;, 3, 4],
55+
&nbsp; [&quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;]
56+
]
57+
58+
<strong>Explanation:</strong>
59+
There are 4 unique column names: &quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;.
60+
The first object has values associated with &quot;a&quot; and &quot;b&quot;.
61+
The second object has values associated with &quot;c&quot; and &quot;d&quot;.
62+
The third object has no keys, so it is just a row of empty strings.
63+
</pre>
64+
65+
<p><strong class="example">Example 3:</strong></p>
66+
67+
<pre>
68+
<strong>Input:</strong>
69+
arr = [
70+
&nbsp; {&quot;a&quot;: {&quot;b&quot;: 1, &quot;c&quot;: 2}},
71+
&nbsp; {&quot;a&quot;: {&quot;b&quot;: 3, &quot;d&quot;: 4}}
72+
]
73+
<strong>Output:</strong>
74+
[
75+
&nbsp; [&quot;a.b&quot;, &quot;a.c&quot;, &quot;a.d&quot;],
76+
&nbsp; [1, 2, &quot;&quot;],
77+
&nbsp; [3, &quot;&quot;, 4]
78+
]
79+
80+
<strong>Explanation:</strong>
81+
In this example, the objects are nested. The keys represent the full path to each value separated by periods.
82+
There are three paths: &quot;a.b&quot;, &quot;a.c&quot;, &quot;a.d&quot;.
83+
</pre>
84+
85+
<p><strong class="example">Example 4:</strong></p>
86+
87+
<pre>
88+
<strong>Input:</strong>
89+
arr = [
90+
&nbsp; [{&quot;a&quot;: null}],
91+
&nbsp; [{&quot;b&quot;: true}],
92+
&nbsp; [{&quot;c&quot;: &quot;x&quot;}]
93+
]
94+
<strong>Output:</strong>
95+
[
96+
&nbsp; [&quot;0.a&quot;, &quot;0.b&quot;, &quot;0.c&quot;],
97+
&nbsp; [null, &quot;&quot;, &quot;&quot;],
98+
&nbsp; [&quot;&quot;, true, &quot;&quot;],
99+
&nbsp; [&quot;&quot;, &quot;&quot;, &quot;x&quot;]
100+
]
101+
102+
<strong>Explanation:</strong>
103+
Arrays are also considered objects with their keys being their indices.
104+
Each array has one element so the keys are &quot;0.a&quot;, &quot;0.b&quot;, and &quot;0.c&quot;.
105+
</pre>
106+
107+
<p><strong class="example">Example 5:</strong></p>
108+
109+
<pre>
110+
<strong>Input:</strong>
111+
arr = [
112+
{},
113+
&nbsp; {},
114+
&nbsp; {},
115+
]
116+
<strong>Output:</strong>
117+
[
118+
&nbsp; [],
119+
&nbsp; [],
120+
&nbsp; [],
121+
&nbsp; []
122+
]
123+
124+
<strong>Explanation:</strong>
125+
There are no keys so every row is an empty array.</pre>
126+
127+
<p>&nbsp;</p>
128+
<p><strong>Constraints:</strong></p>
129+
130+
<ul>
131+
<li><code>1 &lt;= arr.length &lt;= 1000</code></li>
132+
<li><code>unique keys &lt;= 1000</code></li>
133+
</ul>
134+
135+
## 解法
136+
137+
<!-- 这里可写通用的实现逻辑 -->
138+
139+
<!-- tabs:start -->
140+
141+
### **TypeScript**
142+
143+
<!-- 这里可写当前语言的特殊实现逻辑 -->
144+
145+
```ts
146+
147+
```
148+
149+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
# [2675. Array of Objects to Matrix](https://leetcode.com/problems/array-of-objects-to-matrix)
2+
3+
[中文文档](/solution/2600-2699/2675.Array%20of%20Objects%20to%20Matrix/README.md)
4+
5+
## Description
6+
7+
<p>Write a function that converts an array of objects&nbsp;<code>arr</code> into a matrix <code>m</code>.</p>
8+
9+
<p><code>arr</code>&nbsp;is an array of objects or arrays. Each item in the array can be deeply nested with child arrays and child objects. It can also contain numbers, strings, booleans, and&nbsp;null values.</p>
10+
11+
<p>The first row <code>m</code>&nbsp;should be the column names. If there is no nesting, the column names are the unique keys within the objects. If there is nesting, the column names&nbsp;are the respective paths in the object separated by <code>&quot;.&quot;</code>.</p>
12+
13+
<p>Each of the remaining rows corresponds to an object in&nbsp;<code>arr</code>. Each value in the matrix corresponds to a value in an object. If a given object doesn&#39;t contain a value for a given column, the cell should contain an empty string&nbsp;<code>&quot;&quot;</code>.</p>
14+
15+
<p>The colums in the matrix should be in <strong>lexographically ascending</strong> order.</p>
16+
17+
<p>&nbsp;</p>
18+
<p><strong class="example">Example 1:</strong></p>
19+
20+
<pre>
21+
<strong>Input:</strong>
22+
arr = [
23+
&nbsp; {&quot;b&quot;: 1, &quot;a&quot;: 2},
24+
&nbsp; {&quot;b&quot;: 3, &quot;a&quot;: 4}
25+
]
26+
<strong>Output:</strong>
27+
[
28+
&nbsp; [&quot;a&quot;, &quot;b&quot;],
29+
&nbsp; [2, 1],
30+
&nbsp; [4, 3]
31+
]
32+
33+
<strong>Explanation:</strong>
34+
There are two unique column names in the two objects: &quot;a&quot; and &quot;b&quot;.
35+
&quot;a&quot; corresponds with [2, 4].
36+
&quot;b&quot; coresponds with [1, 3].
37+
</pre>
38+
39+
<p><strong class="example">Example 2:</strong></p>
40+
41+
<pre>
42+
<strong>Input:</strong>
43+
arr = [
44+
&nbsp; {&quot;a&quot;: 1, &quot;b&quot;: 2},
45+
&nbsp; {&quot;c&quot;: 3, &quot;d&quot;: 4},
46+
&nbsp; {}
47+
]
48+
<strong>Output:</strong>
49+
[
50+
&nbsp; [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;],
51+
&nbsp; [1, 2, &quot;&quot;, &quot;&quot;],
52+
&nbsp; [&quot;&quot;, &quot;&quot;, 3, 4],
53+
&nbsp; [&quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;]
54+
]
55+
56+
<strong>Explanation:</strong>
57+
There are 4 unique column names: &quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;.
58+
The first object has values associated with &quot;a&quot; and &quot;b&quot;.
59+
The second object has values associated with &quot;c&quot; and &quot;d&quot;.
60+
The third object has no keys, so it is just a row of empty strings.
61+
</pre>
62+
63+
<p><strong class="example">Example 3:</strong></p>
64+
65+
<pre>
66+
<strong>Input:</strong>
67+
arr = [
68+
&nbsp; {&quot;a&quot;: {&quot;b&quot;: 1, &quot;c&quot;: 2}},
69+
&nbsp; {&quot;a&quot;: {&quot;b&quot;: 3, &quot;d&quot;: 4}}
70+
]
71+
<strong>Output:</strong>
72+
[
73+
&nbsp; [&quot;a.b&quot;, &quot;a.c&quot;, &quot;a.d&quot;],
74+
&nbsp; [1, 2, &quot;&quot;],
75+
&nbsp; [3, &quot;&quot;, 4]
76+
]
77+
78+
<strong>Explanation:</strong>
79+
In this example, the objects are nested. The keys represent the full path to each value separated by periods.
80+
There are three paths: &quot;a.b&quot;, &quot;a.c&quot;, &quot;a.d&quot;.
81+
</pre>
82+
83+
<p><strong class="example">Example 4:</strong></p>
84+
85+
<pre>
86+
<strong>Input:</strong>
87+
arr = [
88+
&nbsp; [{&quot;a&quot;: null}],
89+
&nbsp; [{&quot;b&quot;: true}],
90+
&nbsp; [{&quot;c&quot;: &quot;x&quot;}]
91+
]
92+
<strong>Output:</strong>
93+
[
94+
&nbsp; [&quot;0.a&quot;, &quot;0.b&quot;, &quot;0.c&quot;],
95+
&nbsp; [null, &quot;&quot;, &quot;&quot;],
96+
&nbsp; [&quot;&quot;, true, &quot;&quot;],
97+
&nbsp; [&quot;&quot;, &quot;&quot;, &quot;x&quot;]
98+
]
99+
100+
<strong>Explanation:</strong>
101+
Arrays are also considered objects with their keys being their indices.
102+
Each array has one element so the keys are &quot;0.a&quot;, &quot;0.b&quot;, and &quot;0.c&quot;.
103+
</pre>
104+
105+
<p><strong class="example">Example 5:</strong></p>
106+
107+
<pre>
108+
<strong>Input:</strong>
109+
arr = [
110+
{},
111+
&nbsp; {},
112+
&nbsp; {},
113+
]
114+
<strong>Output:</strong>
115+
[
116+
&nbsp; [],
117+
&nbsp; [],
118+
&nbsp; [],
119+
&nbsp; []
120+
]
121+
122+
<strong>Explanation:</strong>
123+
There are no keys so every row is an empty array.</pre>
124+
125+
<p>&nbsp;</p>
126+
<p><strong>Constraints:</strong></p>
127+
128+
<ul>
129+
<li><code>1 &lt;= arr.length &lt;= 1000</code></li>
130+
<li><code>unique keys &lt;= 1000</code></li>
131+
</ul>
132+
133+
## Solutions
134+
135+
<!-- tabs:start -->
136+
137+
### **TypeScript**
138+
139+
```ts
140+
141+
```
142+
143+
<!-- tabs:end -->
+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# [2676. Throttle](https://leetcode.cn/problems/throttle)
2+
3+
[English Version](/solution/2600-2699/2676.Throttle/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>Given a function <code>fn</code> and&nbsp;a time in milliseconds <code>t</code>, return&nbsp;a <strong>throttled</strong> version of that function.</p>
10+
11+
<p>A <strong>throttled</strong> function is first called without delay and then, for a time interval of <code>t</code> milliseconds, can&#39;t be executed but should store the latest function arguments provided to call <code>fn</code> with them after the end of the delay.</p>
12+
13+
<p>For instance, <code>t = 50ms</code>, and the function was called at <code>30ms</code>, <code>40ms</code>, and <code>60ms</code>. The first function call would block calling functions for the following <code>t</code> milliseconds. The second function call would save arguments, and the third call arguments should overwrite currently stored arguments from the second call because the second and third calls are called before <code>80ms</code>. Once the delay has passed, the throttled function should be called with the latest arguments provided during the delay period, and it should also create another delay period of <code>80ms + t</code>.</p>
14+
15+
<p><img alt="Throttle Diagram" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2600-2699/2676.Throttle/images/screen-shot-2023-04-08-at-120313-pm.png" style="width: 1156px; height: 372px;" />The above diagram&nbsp;shows how throttle&nbsp;will transform&nbsp;events. Each rectangle represents 100ms and the throttle&nbsp;time is 400ms. Each color represents a different set of inputs.</p>
16+
17+
<p>&nbsp;</p>
18+
<p><strong class="example">Example 1:</strong></p>
19+
20+
<pre>
21+
<strong>Input:</strong> t = 100, calls = [{&quot;t&quot;:20,&quot;inputs&quot;:[1]}]
22+
<strong>Output:</strong> [{&quot;t&quot;:20,&quot;inputs&quot;:[1]}]
23+
<strong>Explanation:</strong> The 1st call is always called without delay
24+
</pre>
25+
26+
<p><strong class="example">Example 2:</strong></p>
27+
28+
<pre>
29+
<strong>Input:</strong> t = 50, calls = [{&quot;t&quot;:50,&quot;inputs&quot;:[1]},{&quot;t&quot;:75,&quot;inputs&quot;:[2]}]
30+
<strong>Output:</strong> [{&quot;t&quot;:50,&quot;inputs&quot;:[1]},{&quot;t&quot;:100,&quot;inputs&quot;:[2]}]
31+
<strong>Explanation:</strong>
32+
The 1st is called a function with arguments (1) without delay.
33+
The 2nd is called at 75ms, within the delay period because 50ms + 50ms = 100ms, so the next call can be reached at 100ms. Therefore, we save arguments from the 2nd call to use them at the callback of the 1st call.
34+
</pre>
35+
36+
<p><strong class="example">Example 3:</strong></p>
37+
38+
<pre>
39+
<strong>Input:</strong> t = 70, calls = [{&quot;t&quot;:50,&quot;inputs&quot;:[1]},{&quot;t&quot;:75,&quot;inputs&quot;:[2]},{&quot;t&quot;:90,&quot;inputs&quot;:[8]},{&quot;t&quot;: 140, &quot;inputs&quot;:[5,7]},{&quot;t&quot;: 300, &quot;inputs&quot;: [9,4]}]
40+
<strong>Output:</strong> [{&quot;t&quot;:50,&quot;inputs&quot;:[1]},{&quot;t&quot;:120,&quot;inputs&quot;:[8]},{&quot;t&quot;:190,&quot;inputs&quot;:[5,7]},{&quot;t&quot;:300,&quot;inputs&quot;:[9,4]}]
41+
<strong>Explanation:</strong>
42+
The 1st is called a function with arguments (1) without delay.
43+
The 2nd is called at 75ms within the delay period because 50ms + 70ms = 120ms, so it should only save arguments.&nbsp;
44+
The 3rd is also called within the delay period, and because we need just the latest function arguments, we overwrite previous ones. After the delay period, we do a callback at 120ms with saved arguments. That callback makes another delay period of 120ms + 70ms = 190ms so that the next function can be called at 190ms.
45+
The 4th is called at 140ms in the delay period, so it should be called as a callback at 190ms. That will create another delay period of 190ms + 70ms = 260ms.
46+
The 5th is called at 300ms, but it is after 260ms, so it should be called immediately and should create another delay period of 300ms + 70ms = 370ms.</pre>
47+
48+
<p>&nbsp;</p>
49+
<p><strong>Constraints:</strong></p>
50+
51+
<ul>
52+
<li><code>0 &lt;= t &lt;= 1000</code></li>
53+
<li><code>1 &lt;= calls.length &lt;= 10</code></li>
54+
<li><code>0 &lt;= calls[i].t &lt;= 1000</code></li>
55+
<li><code>0 &lt;= calls[i].inputs[i], calls[i].inputs.length &lt;= 10</code></li>
56+
</ul>
57+
58+
## 解法
59+
60+
<!-- 这里可写通用的实现逻辑 -->
61+
62+
<!-- tabs:start -->
63+
64+
### **TypeScript**
65+
66+
<!-- 这里可写当前语言的特殊实现逻辑 -->
67+
68+
```ts
69+
70+
```
71+
72+
<!-- tabs:end -->

0 commit comments

Comments
 (0)