-
Notifications
You must be signed in to change notification settings - Fork 130
Add: new #765
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
Add: new #765
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
problems/construct-target-array-with-multiple-sums/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<!--|This file generated by command(leetcode description); DO NOT EDIT. |--> | ||
<!--+----------------------------------------------------------------------+--> | ||
<!--|@author openset <openset.wang@gmail.com> |--> | ||
<!--|@link https://github.com/openset |--> | ||
<!--|@home https://github.com/openset/leetcode |--> | ||
<!--+----------------------------------------------------------------------+--> | ||
|
||
[< Previous](../maximum-number-of-events-that-can-be-attended "Maximum Number of Events That Can Be Attended") | ||
|
||
Next > | ||
|
||
## [1354. Construct Target Array With Multiple Sums (Hard)](https://leetcode.com/problems/construct-target-array-with-multiple-sums "多次求和构造目标数组") | ||
|
||
<p>Given an array of integers <code>target</code>. From a starting array, <code>A</code> consisting of all 1's, you may perform the following procedure :</p> | ||
|
||
<ul> | ||
<li>let <code>x</code> be the sum of all elements currently in your array.</li> | ||
<li>choose index <code>i</code>, such that <code>0 <= i < target.size</code> and set the value of <code>A</code> at index <code>i</code> to <code>x</code>.</li> | ||
<li>You may repeat this procedure as many times as needed.</li> | ||
</ul> | ||
|
||
<p>Return True if it is possible to construct the <code>target</code> array from <code>A</code> otherwise return False.</p> | ||
|
||
<p> </p> | ||
<p><strong>Example 1:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> target = [9,3,5] | ||
<strong>Output:</strong> true | ||
<strong>Explanation:</strong> Start with [1, 1, 1] | ||
[1, 1, 1], sum = 3 choose index 1 | ||
[1, 3, 1], sum = 5 choose index 2 | ||
[1, 3, 5], sum = 9 choose index 0 | ||
[9, 3, 5] Done | ||
</pre> | ||
|
||
<p><strong>Example 2:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> target = [1,1,1,2] | ||
<strong>Output:</strong> false | ||
<strong>Explanation:</strong> Impossible to create target array from [1,1,1,1]. | ||
</pre> | ||
|
||
<p><strong>Example 3:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> target = [8,5] | ||
<strong>Output:</strong> true | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>N == target.length</code></li> | ||
<li><code>1 <= target.length <= 5 * 10^4</code></li> | ||
<li><code>1 <= target[i] <= 10^9</code></li> | ||
</ul> | ||
|
||
### Related Topics | ||
[[Greedy](../../tag/greedy/README.md)] | ||
|
||
### Hints | ||
<details> | ||
<summary>Hint 1</summary> | ||
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. | ||
</details> | ||
|
||
<details> | ||
<summary>Hint 2</summary> | ||
Subtract the largest with the rest of the array, and put the new element into the array. Repeat until all elements become one | ||
</details> |
66 changes: 66 additions & 0 deletions
problems/count-negative-numbers-in-a-sorted-matrix/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<!--|This file generated by command(leetcode description); DO NOT EDIT. |--> | ||
<!--+----------------------------------------------------------------------+--> | ||
<!--|@author openset <openset.wang@gmail.com> |--> | ||
<!--|@link https://github.com/openset |--> | ||
<!--|@home https://github.com/openset/leetcode |--> | ||
<!--+----------------------------------------------------------------------+--> | ||
|
||
[< Previous](../students-with-invalid-departments "Students With Invalid Departments") | ||
|
||
[Next >](../product-of-the-last-k-numbers "Product of the Last K Numbers") | ||
|
||
## [1351. Count Negative Numbers in a Sorted Matrix (Easy)](https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix "统计有序矩阵中的负数") | ||
|
||
<p>Given a <code>m * n</code> matrix <code>grid</code> which is sorted in non-increasing order both row-wise and column-wise. </p> | ||
|
||
<p>Return the number of <strong>negative</strong> numbers in <code>grid</code>.</p> | ||
|
||
<p> </p> | ||
<p><strong>Example 1:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> grid = [[4,3,2,-1],[3,2,1,-1],[1,1,-1,-2],[-1,-1,-2,-3]] | ||
<strong>Output:</strong> 8 | ||
<strong>Explanation:</strong> There are 8 negatives number in the matrix. | ||
</pre> | ||
|
||
<p><strong>Example 2:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> grid = [[3,2],[1,0]] | ||
<strong>Output:</strong> 0 | ||
</pre> | ||
|
||
<p><strong>Example 3:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> grid = [[1,-1],[-1,-1]] | ||
<strong>Output:</strong> 3 | ||
</pre> | ||
|
||
<p><strong>Example 4:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> grid = [[-1]] | ||
<strong>Output:</strong> 1 | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>m == grid.length</code></li> | ||
<li><code>n == grid[i].length</code></li> | ||
<li><code>1 <= m, n <= 100</code></li> | ||
<li><code>-100 <= grid[i][j] <= 100</code></li> | ||
</ul> | ||
|
||
### Related Topics | ||
[[Array](../../tag/array/README.md)] | ||
[[Binary Search](../../tag/binary-search/README.md)] | ||
|
||
### Hints | ||
<details> | ||
<summary>Hint 1</summary> | ||
Use binary search for optimization or simply brute force. | ||
</details> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
problems/maximum-number-of-events-that-can-be-attended/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<!--|This file generated by command(leetcode description); DO NOT EDIT. |--> | ||
<!--+----------------------------------------------------------------------+--> | ||
<!--|@author openset <openset.wang@gmail.com> |--> | ||
<!--|@link https://github.com/openset |--> | ||
<!--|@home https://github.com/openset/leetcode |--> | ||
<!--+----------------------------------------------------------------------+--> | ||
|
||
[< Previous](../product-of-the-last-k-numbers "Product of the Last K Numbers") | ||
|
||
[Next >](../construct-target-array-with-multiple-sums "Construct Target Array With Multiple Sums") | ||
|
||
## [1353. Maximum Number of Events That Can Be Attended (Medium)](https://leetcode.com/problems/maximum-number-of-events-that-can-be-attended "最多可以参加的会议数目") | ||
|
||
<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 <code>startDay<sub>i</sub></code><sub> </sub>and ends at <code>endDay<sub>i</sub></code>.</p> | ||
|
||
<p>You can attend an event <code>i</code> at any day <code>d</code> where <code>startTime<sub>i</sub> <= d <= endTime<sub>i</sub></code>. Notice that you can only attend one event at any time <code>d</code>.</p> | ||
|
||
<p>Return <em>the maximum number of events </em>you can attend.</p> | ||
|
||
<p> </p> | ||
<p><strong>Example 1:</strong></p> | ||
<img alt="" src="https://assets.leetcode.com/uploads/2020/02/05/e1.png" style="width: 660px; height: 440px;" /> | ||
<pre> | ||
<strong>Input:</strong> events = [[1,2],[2,3],[3,4]] | ||
<strong>Output:</strong> 3 | ||
<strong>Explanation:</strong> You can attend all the three events. | ||
One way to attend them all is as shown. | ||
Attend the first event on day 1. | ||
Attend the second event on day 2. | ||
Attend the third event on day 3. | ||
</pre> | ||
|
||
<p><strong>Example 2:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> events= [[1,2],[2,3],[3,4],[1,2]] | ||
<strong>Output:</strong> 4 | ||
</pre> | ||
|
||
<p><strong>Example 3:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> events = [[1,4],[4,4],[2,2],[3,4],[1,1]] | ||
<strong>Output:</strong> 4 | ||
</pre> | ||
|
||
<p><strong>Example 4:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> events = [[1,100000]] | ||
<strong>Output:</strong> 1 | ||
</pre> | ||
|
||
<p><strong>Example 5:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> events = [[1,1],[1,2],[1,3],[1,4],[1,5],[1,6],[1,7]] | ||
<strong>Output:</strong> 7 | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>1 <= events.length <= 10^5</code></li> | ||
<li><code>events[i].length == 2</code></li> | ||
<li><code>1 <= events[i][0] <= events[i][1] <= 10^5</code></li> | ||
</ul> | ||
|
||
### Related Topics | ||
[[Greedy](../../tag/greedy/README.md)] | ||
[[Sort](../../tag/sort/README.md)] | ||
[[Segment Tree](../../tag/segment-tree/README.md)] | ||
|
||
### Hints | ||
<details> | ||
<summary>Hint 1</summary> | ||
Sort the events by the start time and in case of tie by the end time in ascending order. | ||
</details> | ||
|
||
<details> | ||
<summary>Hint 2</summary> | ||
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. | ||
</details> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
problems/product-of-the-last-k-numbers/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<!--|This file generated by command(leetcode description); DO NOT EDIT. |--> | ||
<!--+----------------------------------------------------------------------+--> | ||
<!--|@author openset <openset.wang@gmail.com> |--> | ||
<!--|@link https://github.com/openset |--> | ||
<!--|@home https://github.com/openset/leetcode |--> | ||
<!--+----------------------------------------------------------------------+--> | ||
|
||
[< Previous](../count-negative-numbers-in-a-sorted-matrix "Count Negative Numbers in a Sorted Matrix") | ||
|
||
[Next >](../maximum-number-of-events-that-can-be-attended "Maximum Number of Events That Can Be Attended") | ||
|
||
## [1352. Product of the Last K Numbers (Medium)](https://leetcode.com/problems/product-of-the-last-k-numbers "最后 K 个数的乘积") | ||
|
||
<p>Implement the class <code>ProductOfNumbers</code> that supports two methods:</p> | ||
|
||
<p>1.<code> add(int num)</code></p> | ||
|
||
<ul> | ||
<li>Adds the number <code>num</code> to the back of the current list of numbers.</li> | ||
</ul> | ||
|
||
<p>2.<code> getProduct(int k)</code></p> | ||
|
||
<ul> | ||
<li>Returns the product of the last <code>k</code> numbers in the current list.</li> | ||
<li>You can assume that always the current list has <strong>at least</strong> <code>k</code> numbers.</li> | ||
</ul> | ||
|
||
<p>At any time, the product of any contiguous sequence of numbers will fit into a single 32-bit integer without overflowing.</p> | ||
|
||
<p> </p> | ||
<p><strong>Example:</strong></p> | ||
|
||
<pre> | ||
<strong>Input</strong> | ||
["ProductOfNumbers","add","add","add","add","add","getProduct","getProduct","getProduct","add","getProduct"] | ||
[[],[3],[0],[2],[5],[4],[2],[3],[4],[8],[2]] | ||
|
||
<strong>Output</strong> | ||
[null,null,null,null,null,null,20,40,0,null,32] | ||
|
||
<strong>Explanation</strong> | ||
ProductOfNumbers productOfNumbers = new ProductOfNumbers(); | ||
productOfNumbers.add(3); // [3] | ||
productOfNumbers.add(0); // [3,0] | ||
productOfNumbers.add(2); // [3,0,2] | ||
productOfNumbers.add(5); // [3,0,2,5] | ||
productOfNumbers.add(4); // [3,0,2,5,4] | ||
productOfNumbers.getProduct(2); // return 20. The product of the last 2 numbers is 5 * 4 = 20 | ||
productOfNumbers.getProduct(3); // return 40. The product of the last 3 numbers is 2 * 5 * 4 = 40 | ||
productOfNumbers.getProduct(4); // return 0. The product of the last 4 numbers is 0 * 2 * 5 * 4 = 0 | ||
productOfNumbers.add(8); // [3,0,2,5,4,8] | ||
productOfNumbers.getProduct(2); // return 32. The product of the last 2 numbers is 4 * 8 = 32 | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li>There will be at most <code>40000</code> operations considering both <code>add</code> and <code>getProduct</code>.</li> | ||
<li><code>0 <= num <= 100</code></li> | ||
<li><code>1 <= k <= 40000</code></li> | ||
</ul> | ||
|
||
### Related Topics | ||
[[Design](../../tag/design/README.md)] | ||
[[Array](../../tag/array/README.md)] | ||
|
||
### Hints | ||
<details> | ||
<summary>Hint 1</summary> | ||
Keep all prefix products of numbers in an array, then calculate the product of last K elements in O(1) complexity. | ||
</details> | ||
|
||
<details> | ||
<summary>Hint 2</summary> | ||
When a zero number is added, clean the array of prefix products. | ||
</details> |
14 changes: 14 additions & 0 deletions
problems/students-with-invalid-departments/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<!--|This file generated by command(leetcode description); DO NOT EDIT. |--> | ||
<!--+----------------------------------------------------------------------+--> | ||
<!--|@author openset <openset.wang@gmail.com> |--> | ||
<!--|@link https://github.com/openset |--> | ||
<!--|@home https://github.com/openset/leetcode |--> | ||
<!--+----------------------------------------------------------------------+--> | ||
|
||
[< Previous](../maximum-students-taking-exam "Maximum Students Taking Exam") | ||
|
||
[Next >](../count-negative-numbers-in-a-sorted-matrix "Count Negative Numbers in a Sorted Matrix") | ||
|
||
## [1350. Students With Invalid Departments (Easy)](https://leetcode.com/problems/students-with-invalid-departments "") | ||
|
||
|
17 changes: 17 additions & 0 deletions
problems/students-with-invalid-departments/mysql_schemas.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
Create table If Not Exists Departments (id int, name varchar(30)); | ||
Create table If Not Exists Students (id int, name varchar(30), department_id int); | ||
Truncate table Departments; | ||
insert into Departments (id, name) values ('1', 'Electrical Engineering'); | ||
insert into Departments (id, name) values ('7', 'Computer Engineering'); | ||
insert into Departments (id, name) values ('13', 'Bussiness Administration'); | ||
Truncate table Students; | ||
insert into Students (id, name, department_id) values ('23', 'Alice', '1'); | ||
insert into Students (id, name, department_id) values ('1', 'Bob', '7'); | ||
insert into Students (id, name, department_id) values ('5', 'Jennifer', '13'); | ||
insert into Students (id, name, department_id) values ('2', 'John', '14'); | ||
insert into Students (id, name, department_id) values ('4', 'Jasmine', '77'); | ||
insert into Students (id, name, department_id) values ('3', 'Steve', '74'); | ||
insert into Students (id, name, department_id) values ('6', 'Luis', '1'); | ||
insert into Students (id, name, department_id) values ('8', 'Jonathan', '7'); | ||
insert into Students (id, name, department_id) values ('7', 'Daiana', '33'); | ||
insert into Students (id, name, department_id) values ('11', 'Madelynn', '1'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.