diff --git a/README.md b/README.md index 55bf53546..fe3f1906c 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,11 @@ LeetCode Problems' Solutions | # | Title | Solution | Difficulty | | :-: | - | - | :-: | +| 1284 | [Minimum Number of Flips to Convert Binary Matrix to Zero Matrix](https://leetcode.com/problems/minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix "转化为全零矩阵的最少反转次数") | [Go](https://github.com/openset/leetcode/tree/master/problems/minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix) | Hard | +| 1283 | [Find the Smallest Divisor Given a Threshold](https://leetcode.com/problems/find-the-smallest-divisor-given-a-threshold "使结果不超过阈值的最小除数") | [Go](https://github.com/openset/leetcode/tree/master/problems/find-the-smallest-divisor-given-a-threshold) | Medium | +| 1282 | [Group the People Given the Group Size They Belong To](https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to "用户分组") | [Go](https://github.com/openset/leetcode/tree/master/problems/group-the-people-given-the-group-size-they-belong-to) | Medium | +| 1281 | [Subtract the Product and Sum of Digits of an Integer](https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer "整数的各位积和之差") | [Go](https://github.com/openset/leetcode/tree/master/problems/subtract-the-product-and-sum-of-digits-of-an-integer) | Easy | +| 1280 | [Students and Examinations](https://leetcode.com/problems/students-and-examinations) 🔒 | [MySQL](https://github.com/openset/leetcode/tree/master/problems/students-and-examinations) | Easy | | 1279 | [Traffic Light Controlled Intersection](https://leetcode.com/problems/traffic-light-controlled-intersection) 🔒 | [Go](https://github.com/openset/leetcode/tree/master/problems/traffic-light-controlled-intersection) | Easy | | 1278 | [Palindrome Partitioning III](https://leetcode.com/problems/palindrome-partitioning-iii "分割回文串 III") | [Go](https://github.com/openset/leetcode/tree/master/problems/palindrome-partitioning-iii) | Hard | | 1277 | [Count Square Submatrices with All Ones](https://leetcode.com/problems/count-square-submatrices-with-all-ones "统计全为 1 的正方形子矩阵") | [Go](https://github.com/openset/leetcode/tree/master/problems/count-square-submatrices-with-all-ones) | Medium | diff --git a/internal/leetcode/question_translation.go b/internal/leetcode/question_translation.go index e9fd5a3cf..b403de568 100644 --- a/internal/leetcode/question_translation.go +++ b/internal/leetcode/question_translation.go @@ -17,12 +17,7 @@ type qtDataType struct { } type translationsType struct { - Title string `json:"title"` - Question questionIDType `json:"question"` - TypeName string `json:"__typename"` -} - -type questionIDType struct { + Title string `json:"title"` QuestionID string `json:"questionId"` TypeName string `json:"__typename"` } @@ -32,7 +27,7 @@ func GetQuestionTranslation() (qt QuestionTranslationType) { jsonStr := `{ "operationName": "getQuestionTranslation", "variables": {}, - "query": "query getQuestionTranslation($lang: String) {\n translations: allAppliedQuestionTranslations(lang: $lang) {\n title\n question {\n questionId\n __typename\n }\n __typename\n }\n}\n" + "query": "query getQuestionTranslation($lang: String) {\n translations: allAppliedQuestionTranslations(lang: $lang) {\n title\n questionId\n __typename\n }\n}\n" }` graphQLRequest(graphQLCnURL, jsonStr, questionTranslationFile, 2, &qt) if qt.Data.Translations == nil { @@ -47,7 +42,7 @@ func GetQuestionTranslation() (qt QuestionTranslationType) { func init() { translation := GetQuestionTranslation() for _, item := range translation.Data.Translations { - id := item.Question.QuestionID + id := item.QuestionID if id, err := strconv.Atoi(id); err == nil { translationSet[id] = item.Title } diff --git a/problems/find-the-smallest-divisor-given-a-threshold/README.md b/problems/find-the-smallest-divisor-given-a-threshold/README.md new file mode 100644 index 000000000..af86b2db7 --- /dev/null +++ b/problems/find-the-smallest-divisor-given-a-threshold/README.md @@ -0,0 +1,65 @@ + + + + + + + +[< 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 "使结果不超过阈值的最小除数") + +
Given an array of integers nums
and an integer threshold
, we will choose a positive integer divisor and divide all the array by it and sum the result of the division. Find the smallest divisor such that the result mentioned above is less than or equal to threshold
.
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).
+ +It is guaranteed that there will be an answer.
+ ++
Example 1:
+ ++Input: nums = [1,2,5,9], threshold = 6 +Output: 5 +Explanation: 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). ++ +
Example 2:
+ ++Input: nums = [2,3,5,7,11], threshold = 11 +Output: 3 ++ +
Example 3:
+ ++Input: nums = [19], threshold = 5 +Output: 4 ++ +
+
Constraints:
+ +1 <= nums.length <= 5 * 10^4
1 <= nums[i] <= 10^6
nums.length <= threshold <= 10^6
There are n
people whose IDs go from 0
to n - 1
and each person belongs exactly to one group. Given the array groupSizes
of length n
telling the group size each person belongs to, return the groups there are and the people's IDs each group includes.
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.
+ ++
Example 1:
+ ++Input: groupSizes = [3,3,3,3,3,1,3] +Output: [[5],[0,1,2],[3,4,6]] +Explanation: +Other possible solutions are [[2,1,6],[5],[0,4,3]] and [[5],[0,6,2],[4,3,1]]. ++ +
Example 2:
+ ++Input: groupSizes = [2,1,3,3,3,2] +Output: [[1],[0,5],[2,3,4]] ++ +
+
Constraints:
+ +groupSizes.length == n
1 <= n <= 500
1 <= groupSizes[i] <= n
Given a m x n
binary matrix mat
. 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.
Return the minimum number of steps required to convert mat
to a zero matrix or -1 if you cannot.
Binary matrix is a matrix with all cells equal to 0 or 1 only.
+ +Zero matrix is a matrix with all cells equal to 0.
+ ++
Example 1:
++Input: mat = [[0,0],[0,1]] +Output: 3 +Explanation: One possible solution is to flip (1, 0) then (0, 1) and finally (1, 1) as shown. ++ +
Example 2:
+ ++Input: mat = [[0]] +Output: 0 +Explanation: Given matrix is a zero matrix. We don't need to change it. ++ +
Example 3:
+ ++Input: mat = [[1,1,1],[1,0,1],[0,0,0]] +Output: 6 ++ +
Example 4:
+ ++Input: mat = [[1,0,0],[1,0,0]] +Output: -1 +Explanation: Given matrix can't be a zero matrix ++ +
+
Constraints:
+ +m == mat.length
n == mat[0].length
1 <= m <= 3
1 <= n <= 3
mat[i][j]
is 0 or 1.Table: Students
++---------------+---------+ +| 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. ++ +
Table: Subjects
++--------------+---------+ +| 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. ++ +
Table: Examinations
++--------------+---------+ +| 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. ++ +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: +
+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. +diff --git a/problems/students-and-examinations/mysql_schemas.sql b/problems/students-and-examinations/mysql_schemas.sql new file mode 100644 index 000000000..4664a44c1 --- /dev/null +++ b/problems/students-and-examinations/mysql_schemas.sql @@ -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'); diff --git a/problems/subtract-the-product-and-sum-of-digits-of-an-integer/README.md b/problems/subtract-the-product-and-sum-of-digits-of-an-integer/README.md new file mode 100644 index 000000000..d39edd388 --- /dev/null +++ b/problems/subtract-the-product-and-sum-of-digits-of-an-integer/README.md @@ -0,0 +1,62 @@ + + + + + + + +[< Previous](https://github.com/openset/leetcode/tree/master/problems/students-and-examinations "Students and Examinations") + +[Next >](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") + +## [1281. Subtract the Product and Sum of Digits of an Integer (Easy)](https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer "整数的各位积和之差") + +Given an integer number
n
, return the difference between the product of its digits and the sum of its digits.
++
Example 1:
+ ++Input: n = 234 +Output: 15 +Explanation: +Product of digits = 2 * 3 * 4 = 24 +Sum of digits = 2 + 3 + 4 = 9 +Result = 24 - 9 = 15 ++ +
Example 2:
+ ++Input: n = 4421 +Output: 21 +Explanation: +Product of digits = 4 * 4 * 2 * 1 = 32 +Sum of digits = 4 + 4 + 2 + 1 = 11 +Result = 32 - 11 = 21 ++ +
+
Constraints:
+ +1 <= n <= 10^5