From 1d64e8139b2ba528a2f9c31faefb3b1f192a057c Mon Sep 17 00:00:00 2001 From: Shuo Date: 2020年2月17日 10:23:08 +0800 Subject: [PATCH] Add: new --- README.md | 5 ++ .../README.md | 73 ++++++++++++++++ .../README.md | 66 +++++++++++++++ problems/counting-bits/README.md | 2 +- problems/intersection-of-two-arrays/README.md | 2 +- .../README.md | 84 +++++++++++++++++++ .../maximum-students-taking-exam/README.md | 2 +- .../product-of-the-last-k-numbers/README.md | 78 +++++++++++++++++ .../README.md | 14 ++++ .../mysql_schemas.sql | 17 ++++ tag/array/README.md | 2 + tag/binary-search/README.md | 1 + tag/design/README.md | 1 + tag/greedy/README.md | 2 + tag/segment-tree/README.md | 1 + tag/sort/README.md | 1 + 16 files changed, 348 insertions(+), 3 deletions(-) create mode 100644 problems/construct-target-array-with-multiple-sums/README.md create mode 100644 problems/count-negative-numbers-in-a-sorted-matrix/README.md create mode 100644 problems/maximum-number-of-events-that-can-be-attended/README.md create mode 100644 problems/product-of-the-last-k-numbers/README.md create mode 100644 problems/students-with-invalid-departments/README.md create mode 100644 problems/students-with-invalid-departments/mysql_schemas.sql diff --git a/README.md b/README.md index 5f49225c2..c849b280b 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,11 @@ LeetCode Problems' Solutions | # | Title | Solution | Difficulty | | :-: | - | - | :-: | +| 1354 | [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 | +| 1353 | [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 | +| 1352 | [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 | +| 1351 | [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 | +| 1350 | [Students With Invalid Departments](https://leetcode.com/problems/students-with-invalid-departments) 🔒 | [MySQL](problems/students-with-invalid-departments) | Easy | | 1349 | [Maximum Students Taking Exam](https://leetcode.com/problems/maximum-students-taking-exam "参加考试的最大学生数") | [Go](problems/maximum-students-taking-exam) | Hard | | 1348 | [Tweet Counts Per Frequency](https://leetcode.com/problems/tweet-counts-per-frequency "推文计数") | [Go](problems/tweet-counts-per-frequency) | Medium | | 1347 | [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 | diff --git a/problems/construct-target-array-with-multiple-sums/README.md b/problems/construct-target-array-with-multiple-sums/README.md new file mode 100644 index 000000000..2367ddbea --- /dev/null +++ b/problems/construct-target-array-with-multiple-sums/README.md @@ -0,0 +1,73 @@ + + + + + + + +[< 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 "多次求和构造目标数组") + +

Given an array of integers target. From a starting array, A consisting of all 1's, you may perform the following procedure :

+ + + +

Return True if it is possible to construct the target array from A otherwise return False.

+ + +

Example 1:

+ +
+Input: target = [9,3,5]
+Output: true
+Explanation: 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
+
+ +

Example 2:

+ +
+Input: target = [1,1,1,2]
+Output: false
+Explanation: Impossible to create target array from [1,1,1,1].
+
+ +

Example 3:

+ +
+Input: target = [8,5]
+Output: true
+
+ + +

Constraints:

+ + + +### Related Topics + [[Greedy](../../tag/greedy/README.md)] + +### Hints +
+Hint 1 +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. +
+ +
+Hint 2 +Subtract the largest with the rest of the array, and put the new element into the array. Repeat until all elements become one +
diff --git a/problems/count-negative-numbers-in-a-sorted-matrix/README.md b/problems/count-negative-numbers-in-a-sorted-matrix/README.md new file mode 100644 index 000000000..075589a10 --- /dev/null +++ b/problems/count-negative-numbers-in-a-sorted-matrix/README.md @@ -0,0 +1,66 @@ + + + + + + + +[< 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 "统计有序矩阵中的负数") + +

Given a m * n matrix grid which is sorted in non-increasing order both row-wise and column-wise.

+ +

Return the number of negative numbers in grid.

+ + +

Example 1:

+ +
+Input: grid = [[4,3,2,-1],[3,2,1,-1],[1,1,-1,-2],[-1,-1,-2,-3]]
+Output: 8
+Explanation: There are 8 negatives number in the matrix.
+
+ +

Example 2:

+ +
+Input: grid = [[3,2],[1,0]]
+Output: 0
+
+ +

Example 3:

+ +
+Input: grid = [[1,-1],[-1,-1]]
+Output: 3
+
+ +

Example 4:

+ +
+Input: grid = [[-1]]
+Output: 1
+
+ + +

Constraints:

+ + + +### Related Topics + [[Array](../../tag/array/README.md)] + [[Binary Search](../../tag/binary-search/README.md)] + +### Hints +
+Hint 1 +Use binary search for optimization or simply brute force. +
diff --git a/problems/counting-bits/README.md b/problems/counting-bits/README.md index 9169d1a68..e8d6a286b 100644 --- a/problems/counting-bits/README.md +++ b/problems/counting-bits/README.md @@ -35,8 +35,8 @@ ### Related Topics - [[Dynamic Programming](../../tag/dynamic-programming/README.md)] [[Bit Manipulation](../../tag/bit-manipulation/README.md)] + [[Dynamic Programming](../../tag/dynamic-programming/README.md)] ### Similar Questions 1. [Number of 1 Bits](../number-of-1-bits) (Easy) diff --git a/problems/intersection-of-two-arrays/README.md b/problems/intersection-of-two-arrays/README.md index 26f036111..09355375f 100644 --- a/problems/intersection-of-two-arrays/README.md +++ b/problems/intersection-of-two-arrays/README.md @@ -38,10 +38,10 @@ ### Related Topics + [[Sort](../../tag/sort/README.md)] [[Hash Table](../../tag/hash-table/README.md)] [[Two Pointers](../../tag/two-pointers/README.md)] [[Binary Search](../../tag/binary-search/README.md)] - [[Sort](../../tag/sort/README.md)] ### Similar Questions 1. [Intersection of Two Arrays II](../intersection-of-two-arrays-ii) (Easy) diff --git a/problems/maximum-number-of-events-that-can-be-attended/README.md b/problems/maximum-number-of-events-that-can-be-attended/README.md new file mode 100644 index 000000000..abbb0b36a --- /dev/null +++ b/problems/maximum-number-of-events-that-can-be-attended/README.md @@ -0,0 +1,84 @@ + + + + + + + +[< 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 "最多可以参加的会议数目") + +

Given an array of events where events[i] = [startDayi, endDayi]. Every event i starts at startDayiand ends at endDayi.

+ +

You can attend an event i at any day d where startTimei <= d <= endTimei. Notice that you can only attend one event at any time d.

+ +

Return the maximum number of events you can attend.

+ + +

Example 1:

+ +
+Input: events = [[1,2],[2,3],[3,4]]
+Output: 3
+Explanation: 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.
+
+ +

Example 2:

+ +
+Input: events= [[1,2],[2,3],[3,4],[1,2]]
+Output: 4
+
+ +

Example 3:

+ +
+Input: events = [[1,4],[4,4],[2,2],[3,4],[1,1]]
+Output: 4
+
+ +

Example 4:

+ +
+Input: events = [[1,100000]]
+Output: 1
+
+ +

Example 5:

+ +
+Input: events = [[1,1],[1,2],[1,3],[1,4],[1,5],[1,6],[1,7]]
+Output: 7
+
+ + +

Constraints:

+ + + +### Related Topics + [[Greedy](../../tag/greedy/README.md)] + [[Sort](../../tag/sort/README.md)] + [[Segment Tree](../../tag/segment-tree/README.md)] + +### Hints +
+Hint 1 +Sort the events by the start time and in case of tie by the end time in ascending order. +
+ +
+Hint 2 +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. +
diff --git a/problems/maximum-students-taking-exam/README.md b/problems/maximum-students-taking-exam/README.md index 42528f4fb..6776e572c 100644 --- a/problems/maximum-students-taking-exam/README.md +++ b/problems/maximum-students-taking-exam/README.md @@ -7,7 +7,7 @@ [< Previous](../tweet-counts-per-frequency "Tweet Counts Per Frequency") -Next> +[Next>](../students-with-invalid-departments "Students With Invalid Departments") ## [1349. Maximum Students Taking Exam (Hard)](https://leetcode.com/problems/maximum-students-taking-exam "参加考试的最大学生数") diff --git a/problems/product-of-the-last-k-numbers/README.md b/problems/product-of-the-last-k-numbers/README.md new file mode 100644 index 000000000..799a75cc8 --- /dev/null +++ b/problems/product-of-the-last-k-numbers/README.md @@ -0,0 +1,78 @@ + + + + + + + +[< 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 个数的乘积") + +

Implement the class ProductOfNumbers that supports two methods:

+ +

1. add(int num)

+ + + +

2. getProduct(int k)

+ + + +

At any time, the product of any contiguous sequence of numbers will fit into a single 32-bit integer without overflowing.

+ + +

Example:

+ +
+Input
+["ProductOfNumbers","add","add","add","add","add","getProduct","getProduct","getProduct","add","getProduct"]
+[[],[3],[0],[2],[5],[4],[2],[3],[4],[8],[2]]
+
+Output
+[null,null,null,null,null,null,20,40,0,null,32]
+
+Explanation
+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 
+
+ + +

Constraints:

+ + + +### Related Topics + [[Design](../../tag/design/README.md)] + [[Array](../../tag/array/README.md)] + +### Hints +
+Hint 1 +Keep all prefix products of numbers in an array, then calculate the product of last K elements in O(1) complexity. +
+ +
+Hint 2 +When a zero number is added, clean the array of prefix products. +
diff --git a/problems/students-with-invalid-departments/README.md b/problems/students-with-invalid-departments/README.md new file mode 100644 index 000000000..2a1b9c1ec --- /dev/null +++ b/problems/students-with-invalid-departments/README.md @@ -0,0 +1,14 @@ + + + + + + + +[< 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 "") + + diff --git a/problems/students-with-invalid-departments/mysql_schemas.sql b/problems/students-with-invalid-departments/mysql_schemas.sql new file mode 100644 index 000000000..3f3e4af12 --- /dev/null +++ b/problems/students-with-invalid-departments/mysql_schemas.sql @@ -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'); diff --git a/tag/array/README.md b/tag/array/README.md index 9db12f938..ba5742da3 100644 --- a/tag/array/README.md +++ b/tag/array/README.md @@ -9,6 +9,8 @@ | # | 题目 | 标签 | 难度 | | :-: | - | - | :-: | +| 1352 | [最后 K 个数的乘积](../../problems/product-of-the-last-k-numbers) | [[设计](../design/README.md)] [[数组](../array/README.md)] | Medium | +| 1351 | [统计有序矩阵中的负数](../../problems/count-negative-numbers-in-a-sorted-matrix) | [[数组](../array/README.md)] [[二分查找](../binary-search/README.md)] | Easy | | 1346 | [检查整数及其两倍数是否存在](../../problems/check-if-n-and-its-double-exist) | [[数组](../array/README.md)] | Easy | | 1343 | [大小为 K 且平均值大于等于阈值的子数组数目](../../problems/number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold) | [[数组](../array/README.md)] | Medium | | 1338 | [数组大小减半](../../problems/reduce-array-size-to-the-half) | [[贪心算法](../greedy/README.md)] [[数组](../array/README.md)] | Medium | diff --git a/tag/binary-search/README.md b/tag/binary-search/README.md index f2d86062a..fd809ab4a 100644 --- a/tag/binary-search/README.md +++ b/tag/binary-search/README.md @@ -9,6 +9,7 @@ | # | 题目 | 标签 | 难度 | | :-: | - | - | :-: | +| 1351 | [统计有序矩阵中的负数](../../problems/count-negative-numbers-in-a-sorted-matrix) | [[数组](../array/README.md)] [[二分查找](../binary-search/README.md)] | Easy | | 1337 | [方阵中战斗力最弱的 K 行](../../problems/the-k-weakest-rows-in-a-matrix) | [[数组](../array/README.md)] [[二分查找](../binary-search/README.md)] | Easy | | 1300 | [转变数组后最接近目标值的数组和](../../problems/sum-of-mutated-array-closest-to-target) | [[数组](../array/README.md)] [[二分查找](../binary-search/README.md)] | Medium | | 1292 | [元素和小于等于阈值的正方形的最大边长](../../problems/maximum-side-length-of-a-square-with-sum-less-than-or-equal-to-threshold) | [[数组](../array/README.md)] [[二分查找](../binary-search/README.md)] | Medium | diff --git a/tag/design/README.md b/tag/design/README.md index 05840b149..91b34835c 100644 --- a/tag/design/README.md +++ b/tag/design/README.md @@ -9,6 +9,7 @@ | # | 题目 | 标签 | 难度 | | :-: | - | - | :-: | +| 1352 | [最后 K 个数的乘积](../../problems/product-of-the-last-k-numbers) | [[设计](../design/README.md)] [[数组](../array/README.md)] | Medium | | 1348 | [推文计数](../../problems/tweet-counts-per-frequency) | [[设计](../design/README.md)] | Medium | | 1286 | [字母组合迭代器](../../problems/iterator-for-combination) | [[设计](../design/README.md)] [[回溯算法](../backtracking/README.md)] | Medium | | 1244 | [力扣排行榜](../../problems/design-a-leaderboard) 🔒 | [[排序](../sort/README.md)] [[设计](../design/README.md)] [[哈希表](../hash-table/README.md)] | Medium | diff --git a/tag/greedy/README.md b/tag/greedy/README.md index 2e8dc11ce..ca7efdb77 100644 --- a/tag/greedy/README.md +++ b/tag/greedy/README.md @@ -9,6 +9,8 @@ | # | 题目 | 标签 | 难度 | | :-: | - | - | :-: | +| 1354 | [多次求和构造目标数组](../../problems/construct-target-array-with-multiple-sums) | [[贪心算法](../greedy/README.md)] | Hard | +| 1353 | [最多可以参加的会议数目](../../problems/maximum-number-of-events-that-can-be-attended) | [[贪心算法](../greedy/README.md)] [[排序](../sort/README.md)] [[线段树](../segment-tree/README.md)] | Medium | | 1338 | [数组大小减半](../../problems/reduce-array-size-to-the-half) | [[贪心算法](../greedy/README.md)] [[数组](../array/README.md)] | Medium | | 1326 | [灌溉花园的最少水龙头数目](../../problems/minimum-number-of-taps-to-open-to-water-a-garden) | [[贪心算法](../greedy/README.md)] [[动态规划](../dynamic-programming/README.md)] | Hard | | 1296 | [划分数组为连续数字的集合](../../problems/divide-array-in-sets-of-k-consecutive-numbers) | [[贪心算法](../greedy/README.md)] [[数组](../array/README.md)] | Medium | diff --git a/tag/segment-tree/README.md b/tag/segment-tree/README.md index 4fb79b397..d17f4d302 100644 --- a/tag/segment-tree/README.md +++ b/tag/segment-tree/README.md @@ -9,6 +9,7 @@ | # | 题目 | 标签 | 难度 | | :-: | - | - | :-: | +| 1353 | [最多可以参加的会议数目](../../problems/maximum-number-of-events-that-can-be-attended) | [[贪心算法](../greedy/README.md)] [[排序](../sort/README.md)] [[线段树](../segment-tree/README.md)] | Medium | | 1157 | [子数组中占绝大多数的元素](../../problems/online-majority-element-in-subarray) | [[线段树](../segment-tree/README.md)] [[数组](../array/README.md)] [[二分查找](../binary-search/README.md)] | Hard | | 850 | [矩形面积 II](../../problems/rectangle-area-ii) | [[线段树](../segment-tree/README.md)] [[Line Sweep](../line-sweep/README.md)] | Hard | | 732 | [我的日程安排表 III](../../problems/my-calendar-iii) | [[线段树](../segment-tree/README.md)] [[Ordered Map](../ordered-map/README.md)] | Hard | diff --git a/tag/sort/README.md b/tag/sort/README.md index 8bd489283..1948cedb1 100644 --- a/tag/sort/README.md +++ b/tag/sort/README.md @@ -9,6 +9,7 @@ | # | 题目 | 标签 | 难度 | | :-: | - | - | :-: | +| 1353 | [最多可以参加的会议数目](../../problems/maximum-number-of-events-that-can-be-attended) | [[贪心算法](../greedy/README.md)] [[排序](../sort/README.md)] [[线段树](../segment-tree/README.md)] | Medium | | 1333 | [餐厅过滤器](../../problems/filter-restaurants-by-vegan-friendly-price-and-distance) | [[排序](../sort/README.md)] [[数组](../array/README.md)] | Medium | | 1329 | [将矩阵按对角线排序](../../problems/sort-the-matrix-diagonally) | [[排序](../sort/README.md)] [[数组](../array/README.md)] | Medium | | 1305 | [两棵二叉搜索树中的所有元素](../../problems/all-elements-in-two-binary-search-trees) | [[排序](../sort/README.md)] [[树](../tree/README.md)] | Medium |

AltStyle によって変換されたページ (->オリジナル) /