Skip to content

Commit 1d64e81

Browse files
author
Shuo
committed
Add: new
1 parent 6d11c3a commit 1d64e81

File tree

16 files changed

+348
-3
lines changed

16 files changed

+348
-3
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ LeetCode Problems' Solutions
6262

6363
| # | Title | Solution | Difficulty |
6464
| :-: | - | - | :-: |
65+
| <span id="1354">1354</span> | [Construct Target Array With Multiple Sums](https://leetcode.com/problems/construct-target-array-with-multiple-sums "多次求和构造目标数组") | [Go](problems/construct-target-array-with-multiple-sums) | Hard |
66+
| <span id="1353">1353</span> | [Maximum Number of Events That Can Be Attended](https://leetcode.com/problems/maximum-number-of-events-that-can-be-attended "最多可以参加的会议数目") | [Go](problems/maximum-number-of-events-that-can-be-attended) | Medium |
67+
| <span id="1352">1352</span> | [Product of the Last K Numbers](https://leetcode.com/problems/product-of-the-last-k-numbers "最后 K 个数的乘积") | [Go](problems/product-of-the-last-k-numbers) | Medium |
68+
| <span id="1351">1351</span> | [Count Negative Numbers in a Sorted Matrix](https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix "统计有序矩阵中的负数") | [Go](problems/count-negative-numbers-in-a-sorted-matrix) | Easy |
69+
| <span id="1350">1350</span> | [Students With Invalid Departments](https://leetcode.com/problems/students-with-invalid-departments) 🔒 | [MySQL](problems/students-with-invalid-departments) | Easy |
6570
| <span id="1349">1349</span> | [Maximum Students Taking Exam](https://leetcode.com/problems/maximum-students-taking-exam "参加考试的最大学生数") | [Go](problems/maximum-students-taking-exam) | Hard |
6671
| <span id="1348">1348</span> | [Tweet Counts Per Frequency](https://leetcode.com/problems/tweet-counts-per-frequency "推文计数") | [Go](problems/tweet-counts-per-frequency) | Medium |
6772
| <span id="1347">1347</span> | [Minimum Number of Steps to Make Two Strings Anagram](https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram "制造字母异位词的最小步骤数") | [Go](problems/minimum-number-of-steps-to-make-two-strings-anagram) | Medium |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<!--|This file generated by command(leetcode description); DO NOT EDIT. |-->
2+
<!--+----------------------------------------------------------------------+-->
3+
<!--|@author openset <[email protected]> |-->
4+
<!--|@link https://github.com/openset |-->
5+
<!--|@home https://github.com/openset/leetcode |-->
6+
<!--+----------------------------------------------------------------------+-->
7+
8+
[< Previous](../maximum-number-of-events-that-can-be-attended "Maximum Number of Events That Can Be Attended")
9+
                
10+
Next >
11+
12+
## [1354. Construct Target Array With Multiple Sums (Hard)](https://leetcode.com/problems/construct-target-array-with-multiple-sums "多次求和构造目标数组")
13+
14+
<p>Given an array of integers&nbsp;<code>target</code>. From a starting array, <code>A</code>&nbsp;consisting of all 1&#39;s, you may perform the following procedure :</p>
15+
16+
<ul>
17+
<li>let <code>x</code> be the sum of all elements currently in your array.</li>
18+
<li>choose index <code>i</code>, such that&nbsp;<code>0 &lt;= i &lt; target.size</code> and set the value of <code>A</code> at index <code>i</code> to <code>x</code>.</li>
19+
<li>You may repeat this procedure&nbsp;as many times as needed.</li>
20+
</ul>
21+
22+
<p>Return True if it is possible to construct the <code>target</code> array from <code>A</code> otherwise&nbsp;return False.</p>
23+
24+
<p>&nbsp;</p>
25+
<p><strong>Example 1:</strong></p>
26+
27+
<pre>
28+
<strong>Input:</strong> target = [9,3,5]
29+
<strong>Output:</strong> true
30+
<strong>Explanation:</strong> Start with [1, 1, 1]
31+
[1, 1, 1], sum = 3 choose index 1
32+
[1, 3, 1], sum = 5 choose index 2
33+
[1, 3, 5], sum = 9 choose index 0
34+
[9, 3, 5] Done
35+
</pre>
36+
37+
<p><strong>Example 2:</strong></p>
38+
39+
<pre>
40+
<strong>Input:</strong> target = [1,1,1,2]
41+
<strong>Output:</strong> false
42+
<strong>Explanation:</strong> Impossible to create target array from [1,1,1,1].
43+
</pre>
44+
45+
<p><strong>Example 3:</strong></p>
46+
47+
<pre>
48+
<strong>Input:</strong> target = [8,5]
49+
<strong>Output:</strong> true
50+
</pre>
51+
52+
<p>&nbsp;</p>
53+
<p><strong>Constraints:</strong></p>
54+
55+
<ul>
56+
<li><code>N == target.length</code></li>
57+
<li><code>1 &lt;= target.length&nbsp;&lt;= 5 * 10^4</code></li>
58+
<li><code>1 &lt;= target[i] &lt;= 10^9</code></li>
59+
</ul>
60+
61+
### Related Topics
62+
[[Greedy](../../tag/greedy/README.md)]
63+
64+
### Hints
65+
<details>
66+
<summary>Hint 1</summary>
67+
Given that the sum is strictly increasing, the largest element in the target must be formed in the last step by adding the total sum in the previous step. Thus, we can simulate the process in a reversed way.
68+
</details>
69+
70+
<details>
71+
<summary>Hint 2</summary>
72+
Subtract the largest with the rest of the array, and put the new element into the array. Repeat until all elements become one
73+
</details>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<!--|This file generated by command(leetcode description); DO NOT EDIT. |-->
2+
<!--+----------------------------------------------------------------------+-->
3+
<!--|@author openset <[email protected]> |-->
4+
<!--|@link https://github.com/openset |-->
5+
<!--|@home https://github.com/openset/leetcode |-->
6+
<!--+----------------------------------------------------------------------+-->
7+
8+
[< Previous](../students-with-invalid-departments "Students With Invalid Departments")
9+
                
10+
[Next >](../product-of-the-last-k-numbers "Product of the Last K Numbers")
11+
12+
## [1351. Count Negative Numbers in a Sorted Matrix (Easy)](https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix "统计有序矩阵中的负数")
13+
14+
<p>Given a <code>m&nbsp;* n</code>&nbsp;matrix <code>grid</code>&nbsp;which is sorted in non-increasing order both row-wise and column-wise.&nbsp;</p>
15+
16+
<p>Return the number of <strong>negative</strong> numbers in&nbsp;<code>grid</code>.</p>
17+
18+
<p>&nbsp;</p>
19+
<p><strong>Example 1:</strong></p>
20+
21+
<pre>
22+
<strong>Input:</strong> grid = [[4,3,2,-1],[3,2,1,-1],[1,1,-1,-2],[-1,-1,-2,-3]]
23+
<strong>Output:</strong> 8
24+
<strong>Explanation:</strong> There are 8 negatives number in the matrix.
25+
</pre>
26+
27+
<p><strong>Example 2:</strong></p>
28+
29+
<pre>
30+
<strong>Input:</strong> grid = [[3,2],[1,0]]
31+
<strong>Output:</strong> 0
32+
</pre>
33+
34+
<p><strong>Example 3:</strong></p>
35+
36+
<pre>
37+
<strong>Input:</strong> grid = [[1,-1],[-1,-1]]
38+
<strong>Output:</strong> 3
39+
</pre>
40+
41+
<p><strong>Example 4:</strong></p>
42+
43+
<pre>
44+
<strong>Input:</strong> grid = [[-1]]
45+
<strong>Output:</strong> 1
46+
</pre>
47+
48+
<p>&nbsp;</p>
49+
<p><strong>Constraints:</strong></p>
50+
51+
<ul>
52+
<li><code>m == grid.length</code></li>
53+
<li><code>n == grid[i].length</code></li>
54+
<li><code>1 &lt;= m, n &lt;= 100</code></li>
55+
<li><code>-100 &lt;= grid[i][j] &lt;= 100</code></li>
56+
</ul>
57+
58+
### Related Topics
59+
[[Array](../../tag/array/README.md)]
60+
[[Binary Search](../../tag/binary-search/README.md)]
61+
62+
### Hints
63+
<details>
64+
<summary>Hint 1</summary>
65+
Use binary search for optimization or simply brute force.
66+
</details>

problems/counting-bits/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
</ul>
3636

3737
### Related Topics
38-
[[Dynamic Programming](../../tag/dynamic-programming/README.md)]
3938
[[Bit Manipulation](../../tag/bit-manipulation/README.md)]
39+
[[Dynamic Programming](../../tag/dynamic-programming/README.md)]
4040

4141
### Similar Questions
4242
1. [Number of 1 Bits](../number-of-1-bits) (Easy)

problems/intersection-of-two-arrays/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@
3838
<p>&nbsp;</p>
3939

4040
### Related Topics
41+
[[Sort](../../tag/sort/README.md)]
4142
[[Hash Table](../../tag/hash-table/README.md)]
4243
[[Two Pointers](../../tag/two-pointers/README.md)]
4344
[[Binary Search](../../tag/binary-search/README.md)]
44-
[[Sort](../../tag/sort/README.md)]
4545

4646
### Similar Questions
4747
1. [Intersection of Two Arrays II](../intersection-of-two-arrays-ii) (Easy)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<!--|This file generated by command(leetcode description); DO NOT EDIT. |-->
2+
<!--+----------------------------------------------------------------------+-->
3+
<!--|@author openset <[email protected]> |-->
4+
<!--|@link https://github.com/openset |-->
5+
<!--|@home https://github.com/openset/leetcode |-->
6+
<!--+----------------------------------------------------------------------+-->
7+
8+
[< Previous](../product-of-the-last-k-numbers "Product of the Last K Numbers")
9+
                
10+
[Next >](../construct-target-array-with-multiple-sums "Construct Target Array With Multiple Sums")
11+
12+
## [1353. Maximum Number of Events That Can Be Attended (Medium)](https://leetcode.com/problems/maximum-number-of-events-that-can-be-attended "最多可以参加的会议数目")
13+
14+
<p>Given an array of <code>events</code> where <code>events[i] = [startDay<sub>i</sub>, endDay<sub>i</sub>]</code>. Every event <code>i</code> starts at&nbsp;<code>startDay<sub>i</sub></code><sub>&nbsp;</sub>and ends at&nbsp;<code>endDay<sub>i</sub></code>.</p>
15+
16+
<p>You can attend an event <code>i</code>&nbsp;at any day&nbsp;<code>d</code> where&nbsp;<code>startTime<sub>i</sub>&nbsp;&lt;= d &lt;= endTime<sub>i</sub></code>. Notice that you can only attend one event at any time <code>d</code>.</p>
17+
18+
<p>Return <em>the maximum number of events&nbsp;</em>you can attend.</p>
19+
20+
<p>&nbsp;</p>
21+
<p><strong>Example 1:</strong></p>
22+
<img alt="" src="https://assets.leetcode.com/uploads/2020/02/05/e1.png" style="width: 660px; height: 440px;" />
23+
<pre>
24+
<strong>Input:</strong> events = [[1,2],[2,3],[3,4]]
25+
<strong>Output:</strong> 3
26+
<strong>Explanation:</strong> You can attend all the three events.
27+
One way to attend them all is as shown.
28+
Attend the first event on day 1.
29+
Attend the second event on day 2.
30+
Attend the third event on day 3.
31+
</pre>
32+
33+
<p><strong>Example 2:</strong></p>
34+
35+
<pre>
36+
<strong>Input:</strong> events= [[1,2],[2,3],[3,4],[1,2]]
37+
<strong>Output:</strong> 4
38+
</pre>
39+
40+
<p><strong>Example 3:</strong></p>
41+
42+
<pre>
43+
<strong>Input:</strong> events = [[1,4],[4,4],[2,2],[3,4],[1,1]]
44+
<strong>Output:</strong> 4
45+
</pre>
46+
47+
<p><strong>Example 4:</strong></p>
48+
49+
<pre>
50+
<strong>Input:</strong> events = [[1,100000]]
51+
<strong>Output:</strong> 1
52+
</pre>
53+
54+
<p><strong>Example 5:</strong></p>
55+
56+
<pre>
57+
<strong>Input:</strong> events = [[1,1],[1,2],[1,3],[1,4],[1,5],[1,6],[1,7]]
58+
<strong>Output:</strong> 7
59+
</pre>
60+
61+
<p>&nbsp;</p>
62+
<p><strong>Constraints:</strong></p>
63+
64+
<ul>
65+
<li><code>1 &lt;= events.length &lt;= 10^5</code></li>
66+
<li><code>events[i].length == 2</code></li>
67+
<li><code>1 &lt;= events[i][0] &lt;= events[i][1] &lt;= 10^5</code></li>
68+
</ul>
69+
70+
### Related Topics
71+
[[Greedy](../../tag/greedy/README.md)]
72+
[[Sort](../../tag/sort/README.md)]
73+
[[Segment Tree](../../tag/segment-tree/README.md)]
74+
75+
### Hints
76+
<details>
77+
<summary>Hint 1</summary>
78+
Sort the events by the start time and in case of tie by the end time in ascending order.
79+
</details>
80+
81+
<details>
82+
<summary>Hint 2</summary>
83+
Loop over the sorted events. Attend as much as you can and keep the last day occupied. When you try to attend new event keep in mind the first day you can attend a new event in.
84+
</details>

problems/maximum-students-taking-exam/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
[< Previous](../tweet-counts-per-frequency "Tweet Counts Per Frequency")
99

10-
Next >
10+
[Next >](../students-with-invalid-departments "Students With Invalid Departments")
1111

1212
## [1349. Maximum Students Taking Exam (Hard)](https://leetcode.com/problems/maximum-students-taking-exam "参加考试的最大学生数")
1313

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<!--|This file generated by command(leetcode description); DO NOT EDIT. |-->
2+
<!--+----------------------------------------------------------------------+-->
3+
<!--|@author openset <[email protected]> |-->
4+
<!--|@link https://github.com/openset |-->
5+
<!--|@home https://github.com/openset/leetcode |-->
6+
<!--+----------------------------------------------------------------------+-->
7+
8+
[< Previous](../count-negative-numbers-in-a-sorted-matrix "Count Negative Numbers in a Sorted Matrix")
9+
                
10+
[Next >](../maximum-number-of-events-that-can-be-attended "Maximum Number of Events That Can Be Attended")
11+
12+
## [1352. Product of the Last K Numbers (Medium)](https://leetcode.com/problems/product-of-the-last-k-numbers "最后 K 个数的乘积")
13+
14+
<p>Implement the class <code>ProductOfNumbers</code>&nbsp;that supports two methods:</p>
15+
16+
<p>1.<code>&nbsp;add(int num)</code></p>
17+
18+
<ul>
19+
<li>Adds the number <code>num</code> to the back of the current list of numbers.</li>
20+
</ul>
21+
22+
<p>2.<code> getProduct(int k)</code></p>
23+
24+
<ul>
25+
<li>Returns the product of the last <code>k</code> numbers in the current list.</li>
26+
<li>You can assume that always the current list has <strong>at least</strong> <code>k</code> numbers.</li>
27+
</ul>
28+
29+
<p>At any time, the product of any contiguous sequence of numbers will fit into a single 32-bit integer without overflowing.</p>
30+
31+
<p>&nbsp;</p>
32+
<p><strong>Example:</strong></p>
33+
34+
<pre>
35+
<strong>Input</strong>
36+
[&quot;ProductOfNumbers&quot;,&quot;add&quot;,&quot;add&quot;,&quot;add&quot;,&quot;add&quot;,&quot;add&quot;,&quot;getProduct&quot;,&quot;getProduct&quot;,&quot;getProduct&quot;,&quot;add&quot;,&quot;getProduct&quot;]
37+
[[],[3],[0],[2],[5],[4],[2],[3],[4],[8],[2]]
38+
39+
<strong>Output</strong>
40+
[null,null,null,null,null,null,20,40,0,null,32]
41+
42+
<strong>Explanation</strong>
43+
ProductOfNumbers productOfNumbers = new ProductOfNumbers();
44+
productOfNumbers.add(3); // [3]
45+
productOfNumbers.add(0); // [3,0]
46+
productOfNumbers.add(2); // [3,0,2]
47+
productOfNumbers.add(5); // [3,0,2,5]
48+
productOfNumbers.add(4); // [3,0,2,5,4]
49+
productOfNumbers.getProduct(2); // return 20. The product of the last 2 numbers is 5 * 4 = 20
50+
productOfNumbers.getProduct(3); // return 40. The product of the last 3 numbers is 2 * 5 * 4 = 40
51+
productOfNumbers.getProduct(4); // return 0. The product of the last 4 numbers is 0 * 2 * 5 * 4 = 0
52+
productOfNumbers.add(8); // [3,0,2,5,4,8]
53+
productOfNumbers.getProduct(2); // return 32. The product of the last 2 numbers is 4 * 8 = 32
54+
</pre>
55+
56+
<p>&nbsp;</p>
57+
<p><strong>Constraints:</strong></p>
58+
59+
<ul>
60+
<li>There will be at most <code>40000</code>&nbsp;operations considering both <code>add</code> and <code>getProduct</code>.</li>
61+
<li><code>0 &lt;= num&nbsp;&lt;=&nbsp;100</code></li>
62+
<li><code>1 &lt;= k &lt;= 40000</code></li>
63+
</ul>
64+
65+
### Related Topics
66+
[[Design](../../tag/design/README.md)]
67+
[[Array](../../tag/array/README.md)]
68+
69+
### Hints
70+
<details>
71+
<summary>Hint 1</summary>
72+
Keep all prefix products of numbers in an array, then calculate the product of last K elements in O(1) complexity.
73+
</details>
74+
75+
<details>
76+
<summary>Hint 2</summary>
77+
When a zero number is added, clean the array of prefix products.
78+
</details>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!--|This file generated by command(leetcode description); DO NOT EDIT. |-->
2+
<!--+----------------------------------------------------------------------+-->
3+
<!--|@author openset <[email protected]> |-->
4+
<!--|@link https://github.com/openset |-->
5+
<!--|@home https://github.com/openset/leetcode |-->
6+
<!--+----------------------------------------------------------------------+-->
7+
8+
[< Previous](../maximum-students-taking-exam "Maximum Students Taking Exam")
9+
                
10+
[Next >](../count-negative-numbers-in-a-sorted-matrix "Count Negative Numbers in a Sorted Matrix")
11+
12+
## [1350. Students With Invalid Departments (Easy)](https://leetcode.com/problems/students-with-invalid-departments "")
13+
14+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Create table If Not Exists Departments (id int, name varchar(30));
2+
Create table If Not Exists Students (id int, name varchar(30), department_id int);
3+
Truncate table Departments;
4+
insert into Departments (id, name) values ('1', 'Electrical Engineering');
5+
insert into Departments (id, name) values ('7', 'Computer Engineering');
6+
insert into Departments (id, name) values ('13', 'Bussiness Administration');
7+
Truncate table Students;
8+
insert into Students (id, name, department_id) values ('23', 'Alice', '1');
9+
insert into Students (id, name, department_id) values ('1', 'Bob', '7');
10+
insert into Students (id, name, department_id) values ('5', 'Jennifer', '13');
11+
insert into Students (id, name, department_id) values ('2', 'John', '14');
12+
insert into Students (id, name, department_id) values ('4', 'Jasmine', '77');
13+
insert into Students (id, name, department_id) values ('3', 'Steve', '74');
14+
insert into Students (id, name, department_id) values ('6', 'Luis', '1');
15+
insert into Students (id, name, department_id) values ('8', 'Jonathan', '7');
16+
insert into Students (id, name, department_id) values ('7', 'Daiana', '33');
17+
insert into Students (id, name, department_id) values ('11', 'Madelynn', '1');

tag/array/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
| # | 题目 | 标签 | 难度 |
1111
| :-: | - | - | :-: |
12+
| 1352 | [最后 K 个数的乘积](../../problems/product-of-the-last-k-numbers) | [[设计](../design/README.md)] [[数组](../array/README.md)] | Medium |
13+
| 1351 | [统计有序矩阵中的负数](../../problems/count-negative-numbers-in-a-sorted-matrix) | [[数组](../array/README.md)] [[二分查找](../binary-search/README.md)] | Easy |
1214
| 1346 | [检查整数及其两倍数是否存在](../../problems/check-if-n-and-its-double-exist) | [[数组](../array/README.md)] | Easy |
1315
| 1343 | [大小为 K 且平均值大于等于阈值的子数组数目](../../problems/number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold) | [[数组](../array/README.md)] | Medium |
1416
| 1338 | [数组大小减半](../../problems/reduce-array-size-to-the-half) | [[贪心算法](../greedy/README.md)] [[数组](../array/README.md)] | Medium |

tag/binary-search/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
| # | 题目 | 标签 | 难度 |
1111
| :-: | - | - | :-: |
12+
| 1351 | [统计有序矩阵中的负数](../../problems/count-negative-numbers-in-a-sorted-matrix) | [[数组](../array/README.md)] [[二分查找](../binary-search/README.md)] | Easy |
1213
| 1337 | [方阵中战斗力最弱的 K 行](../../problems/the-k-weakest-rows-in-a-matrix) | [[数组](../array/README.md)] [[二分查找](../binary-search/README.md)] | Easy |
1314
| 1300 | [转变数组后最接近目标值的数组和](../../problems/sum-of-mutated-array-closest-to-target) | [[数组](../array/README.md)] [[二分查找](../binary-search/README.md)] | Medium |
1415
| 1292 | [元素和小于等于阈值的正方形的最大边长](../../problems/maximum-side-length-of-a-square-with-sum-less-than-or-equal-to-threshold) | [[数组](../array/README.md)] [[二分查找](../binary-search/README.md)] | Medium |

0 commit comments

Comments
 (0)