Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #732 from openset/develop
Add: new
- Loading branch information
Showing
13 changed files
with
403 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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 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
65 changes: 65 additions & 0 deletions
65
problems/find-the-smallest-divisor-given-a-threshold/README.md
This file contains 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,65 @@ | ||
<!--|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](https://github.com/openset/leetcode/tree/master/problems/group-the-people-given-the-group-size-they-belong-to "Group the People Given the Group Size They Belong To") | ||
|
||
[Next >](https://github.com/openset/leetcode/tree/master/problems/minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix "Minimum Number of Flips to Convert Binary Matrix to Zero Matrix") | ||
|
||
## [1283. Find the Smallest Divisor Given a Threshold (Medium)](https://leetcode.com/problems/find-the-smallest-divisor-given-a-threshold "使结果不超过阈值的最小除数") | ||
|
||
<p>Given an array of integers <code>nums</code> and an integer <code>threshold</code>, we will choose a positive integer divisor and divide all the array by it and sum the result of the division. Find the <strong>smallest</strong> divisor such that the result mentioned above is less than or equal to <code>threshold</code>.</p> | ||
|
||
<p>Each result of division is rounded to the nearest integer greater than or equal to that element. (For example: 7/3 = 3 and 10/2 = 5).</p> | ||
|
||
<p>It is guaranteed that there will be an answer.</p> | ||
|
||
<p> </p> | ||
<p><strong>Example 1:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> nums = [1,2,5,9], threshold = 6 | ||
<strong>Output:</strong> 5 | ||
<strong>Explanation:</strong> We can get a sum to 17 (1+2+5+9) if the divisor is 1. | ||
If the divisor is 4 we can get a sum to 7 (1+1+2+3) and if the divisor is 5 the sum will be 5 (1+1+1+2). | ||
</pre> | ||
|
||
<p><strong>Example 2:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> nums = [2,3,5,7,11], threshold = 11 | ||
<strong>Output:</strong> 3 | ||
</pre> | ||
|
||
<p><strong>Example 3:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> nums = [19], threshold = 5 | ||
<strong>Output:</strong> 4 | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>1 <= nums.length <= 5 * 10^4</code></li> | ||
<li><code>1 <= nums[i] <= 10^6</code></li> | ||
<li><code>nums.length <= threshold <= 10^6</code></li> | ||
</ul> | ||
|
||
### Related Topics | ||
[[Binary Search](https://github.com/openset/leetcode/tree/master/tag/binary-search/README.md)] | ||
|
||
### Hints | ||
<details> | ||
<summary>Hint 1</summary> | ||
Examine every possible number for solution. Choose the largest of them. | ||
</details> | ||
|
||
<details> | ||
<summary>Hint 2</summary> | ||
Use binary search to reduce the time complexity. | ||
</details> |
56 changes: 56 additions & 0 deletions
56
problems/group-the-people-given-the-group-size-they-belong-to/README.md
This file contains 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,56 @@ | ||
<!--|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](https://github.com/openset/leetcode/tree/master/problems/subtract-the-product-and-sum-of-digits-of-an-integer "Subtract the Product and Sum of Digits of an Integer") | ||
|
||
[Next >](https://github.com/openset/leetcode/tree/master/problems/find-the-smallest-divisor-given-a-threshold "Find the Smallest Divisor Given a Threshold") | ||
|
||
## [1282. Group the People Given the Group Size They Belong To (Medium)](https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to "用户分组") | ||
|
||
<p>There are <code>n</code> people whose <strong>IDs</strong> go from <code>0</code> to <code>n - 1</code> and each person belongs <strong>exactly</strong> to one group. Given the array <code>groupSizes</code> of length <code>n</code> telling the group size each person belongs to, return the groups there are and the people's <strong>IDs</strong> each group includes.</p> | ||
|
||
<p>You can return any solution in any order and the same applies for IDs. Also, it is guaranteed that there exists at least one solution. </p> | ||
|
||
<p> </p> | ||
<p><strong>Example 1:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> groupSizes = [3,3,3,3,3,1,3] | ||
<strong>Output:</strong> [[5],[0,1,2],[3,4,6]] | ||
<b>Explanation:</b> | ||
Other possible solutions are [[2,1,6],[5],[0,4,3]] and [[5],[0,6,2],[4,3,1]]. | ||
</pre> | ||
|
||
<p><strong>Example 2:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> groupSizes = [2,1,3,3,3,2] | ||
<strong>Output:</strong> [[1],[0,5],[2,3,4]] | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>groupSizes.length == n</code></li> | ||
<li><code>1 <= n <= 500</code></li> | ||
<li><code>1 <= groupSizes[i] <= n</code></li> | ||
</ul> | ||
|
||
### Related Topics | ||
[[Greedy](https://github.com/openset/leetcode/tree/master/tag/greedy/README.md)] | ||
|
||
### Hints | ||
<details> | ||
<summary>Hint 1</summary> | ||
Put people's IDs with same groupSize into buckets, then split each bucket into groups. | ||
</details> | ||
|
||
<details> | ||
<summary>Hint 2</summary> | ||
Greedy fill until you need a new group. | ||
</details> |
72 changes: 72 additions & 0 deletions
72
problems/minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix/README.md
This file contains 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,72 @@ | ||
<!--|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](https://github.com/openset/leetcode/tree/master/problems/find-the-smallest-divisor-given-a-threshold "Find the Smallest Divisor Given a Threshold") | ||
|
||
Next > | ||
|
||
## [1284. Minimum Number of Flips to Convert Binary Matrix to Zero Matrix (Hard)](https://leetcode.com/problems/minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix "转化为全零矩阵的最少反转次数") | ||
|
||
<p>Given a <code>m x n</code> binary matrix <code>mat</code>. In one step, you can choose one cell and flip it and all the four neighbours of it if they exist (Flip is changing 1 to 0 and 0 to 1). A pair of cells are called neighboors if they share one edge.</p> | ||
|
||
<p>Return the <em>minimum number of steps</em> required to convert <code>mat</code> to a zero matrix or <strong>-1</strong> if you cannot.</p> | ||
|
||
<p>Binary matrix is a matrix with all cells equal to 0 or 1 only.</p> | ||
|
||
<p>Zero matrix is a matrix with all cells equal to 0.</p> | ||
|
||
<p> </p> | ||
<p><strong>Example 1:</strong></p> | ||
<img alt="" src="https://assets.leetcode.com/uploads/2019/11/28/matrix.png" style="width: 409px; height: 86px;" /> | ||
<pre> | ||
<strong>Input:</strong> mat = [[0,0],[0,1]] | ||
<strong>Output:</strong> 3 | ||
<strong>Explanation:</strong> One possible solution is to flip (1, 0) then (0, 1) and finally (1, 1) as shown. | ||
</pre> | ||
|
||
<p><strong>Example 2:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> mat = [[0]] | ||
<strong>Output:</strong> 0 | ||
<strong>Explanation:</strong> Given matrix is a zero matrix. We don't need to change it. | ||
</pre> | ||
|
||
<p><strong>Example 3:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> mat = [[1,1,1],[1,0,1],[0,0,0]] | ||
<strong>Output:</strong> 6 | ||
</pre> | ||
|
||
<p><strong>Example 4:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> mat = [[1,0,0],[1,0,0]] | ||
<strong>Output:</strong> -1 | ||
<strong>Explanation:</strong> Given matrix can't be a zero matrix | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>m == mat.length</code></li> | ||
<li><code>n == mat[0].length</code></li> | ||
<li><code>1 <= m <= 3</code></li> | ||
<li><code>1 <= n <= 3</code></li> | ||
<li><code>mat[i][j]</code> is 0 or 1.</li> | ||
</ul> | ||
|
||
### Related Topics | ||
[[Breadth-first Search](https://github.com/openset/leetcode/tree/master/tag/breadth-first-search/README.md)] | ||
|
||
### Hints | ||
<details> | ||
<summary>Hint 1</summary> | ||
Flipping same index two times is like not flipping it at all. Each index can be flipped one time. Try all possible combinations. O(2^(n*m)). | ||
</details> |
This file contains 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,111 @@ | ||
<!--|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](https://github.com/openset/leetcode/tree/master/problems/traffic-light-controlled-intersection "Traffic Light Controlled Intersection") | ||
|
||
[Next >](https://github.com/openset/leetcode/tree/master/problems/subtract-the-product-and-sum-of-digits-of-an-integer "Subtract the Product and Sum of Digits of an Integer") | ||
|
||
## [1280. Students and Examinations (Easy)](https://leetcode.com/problems/students-and-examinations "") | ||
|
||
<p>Table: <code>Students</code></p> | ||
<pre> | ||
+---------------+---------+ | ||
| Column Name | Type | | ||
+---------------+---------+ | ||
| student_id | int | | ||
| student_name | varchar | | ||
+---------------+---------+ | ||
student_id is the primary key for this table. | ||
Each row of this table contains the ID and the name of one student in the school. | ||
</pre> | ||
|
||
<p>Table: <code>Subjects</code></p> | ||
<pre> | ||
+--------------+---------+ | ||
| Column Name | Type | | ||
+--------------+---------+ | ||
| subject_name | varchar | | ||
+--------------+---------+ | ||
subject_name is the primary key for this table. | ||
Each row of this table contains a name of one subject in the school. | ||
</pre> | ||
|
||
<p>Table: <code>Examinations</code></p> | ||
<pre> | ||
+--------------+---------+ | ||
| Column Name | Type | | ||
+--------------+---------+ | ||
| student_id | int | | ||
| subject_name | varchar | | ||
+--------------+---------+ | ||
There is no primary key for this table. It may contain duplicates. | ||
Each student from Students table takes every course from Subjects table. | ||
Each row of this table indicates that a student with ID student_id attended the exam of subject_name. | ||
</pre> | ||
|
||
Write an SQL query to find the number of times each student attended each exam. | ||
|
||
Order the result table by student_id and subject_name. | ||
|
||
The query result format is in the following example: | ||
<pre> | ||
Students table: | ||
+------------+--------------+ | ||
| student_id | student_name | | ||
+------------+--------------+ | ||
| 1 | Alice | | ||
| 2 | Bob | | ||
| 13 | John | | ||
| 6 | Alex | | ||
+------------+--------------+ | ||
Subjects table: | ||
+--------------+ | ||
| subject_name | | ||
+--------------+ | ||
| Math | | ||
| Physics | | ||
| Programming | | ||
+--------------+ | ||
Examinations table: | ||
+------------+--------------+ | ||
| student_id | subject_name | | ||
+------------+--------------+ | ||
| 1 | Math | | ||
| 1 | Physics | | ||
| 1 | Programming | | ||
| 2 | Programming | | ||
| 1 | Physics | | ||
| 1 | Math | | ||
| 13 | Math | | ||
| 13 | Programming | | ||
| 13 | Physics | | ||
| 2 | Math | | ||
| 1 | Math | | ||
+------------+--------------+ | ||
Result table: | ||
+------------+--------------+--------------+----------------+ | ||
| student_id | student_name | subject_name | attended_exams | | ||
+------------+--------------+--------------+----------------+ | ||
| 1 | Alice | Math | 3 | | ||
| 1 | Alice | Physics | 2 | | ||
| 1 | Alice | Programming | 1 | | ||
| 2 | Bob | Math | 1 | | ||
| 2 | Bob | Physics | 0 | | ||
| 2 | Bob | Programming | 1 | | ||
| 6 | Alex | Math | 0 | | ||
| 6 | Alex | Physics | 0 | | ||
| 6 | Alex | Programming | 0 | | ||
| 13 | John | Math | 1 | | ||
| 13 | John | Physics | 1 | | ||
| 13 | John | Programming | 1 | | ||
+------------+--------------+--------------+----------------+ | ||
The result table should contain all students and all subjects. | ||
Alice attended Math exam 3 times, Physics exam 2 times and Programming exam 1 time. | ||
Bob attended Math exam 1 time, Programming exam 1 time and didn't attend the Physics exam. | ||
Alex didn't attend any exam. | ||
John attended Math exam 1 time, Physics exam 1 time and Programming exam 1 time. | ||
</pre> |
This file contains 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,24 @@ | ||
Create table If Not Exists Students (student_id int, student_name varchar(20)); | ||
Create table If Not Exists Subjects (subject_name varchar(20)); | ||
Create table If Not Exists Examinations (student_id int, subject_name varchar(20)); | ||
Truncate table Students; | ||
insert into Students (student_id, student_name) values ('1', 'Alice'); | ||
insert into Students (student_id, student_name) values ('2', 'Bob'); | ||
insert into Students (student_id, student_name) values ('13', 'John'); | ||
insert into Students (student_id, student_name) values ('6', 'Alex'); | ||
Truncate table Subjects; | ||
insert into Subjects (subject_name) values ('Math'); | ||
insert into Subjects (subject_name) values ('Physics'); | ||
insert into Subjects (subject_name) values ('Programming'); | ||
Truncate table Examinations; | ||
insert into Examinations (student_id, subject_name) values ('1', 'Math'); | ||
insert into Examinations (student_id, subject_name) values ('1', 'Physics'); | ||
insert into Examinations (student_id, subject_name) values ('1', 'Programming'); | ||
insert into Examinations (student_id, subject_name) values ('2', 'Programming'); | ||
insert into Examinations (student_id, subject_name) values ('1', 'Physics'); | ||
insert into Examinations (student_id, subject_name) values ('1', 'Math'); | ||
insert into Examinations (student_id, subject_name) values ('13', 'Math'); | ||
insert into Examinations (student_id, subject_name) values ('13', 'Programming'); | ||
insert into Examinations (student_id, subject_name) values ('13', 'Physics'); | ||
insert into Examinations (student_id, subject_name) values ('2', 'Math'); | ||
insert into Examinations (student_id, subject_name) values ('1', 'Math'); |
Oops, something went wrong.