From a69f341ffc71d8f9b65acc4f7a7476fc4e63693a Mon Sep 17 00:00:00 2001 From: Shuo Date: Tue, 4 Feb 2020 17:15:17 +0800 Subject: [PATCH] Add: new --- README.md | 19 ++++ problems/ads-performance/README.md | 14 +++ problems/ads-performance/mysql_schemas.sql | 12 +++ problems/break-a-palindrome/README.md | 63 +++++++++++++ .../README.md | 79 ++++++++++++++++ .../README.md | 71 ++++++++++++++ .../README.md | 78 ++++++++++++++++ .../README.md | 2 +- problems/jump-game-v/README.md | 89 ++++++++++++++++++ .../README.md | 14 +++ .../mysql_schemas.sql | 21 +++++ problems/maximum-69-number/README.md | 66 +++++++++++++ .../README.md | 69 ++++++++++++++ .../README.md | 84 +++++++++++++++++ .../README.md | 93 +++++++++++++++++++ .../README.md | 14 +++ .../mysql_schemas.sql | 22 +++++ problems/print-words-vertically/README.md | 64 +++++++++++++ problems/rank-transform-of-an-array/README.md | 67 +++++++++++++ .../reduce-array-size-to-the-half/README.md | 85 +++++++++++++++++ .../remove-palindromic-subsequences/README.md | 76 +++++++++++++++ problems/restaurant-growth/README.md | 2 +- .../README.md | 72 ++++++++++++++ problems/sliding-window-median/README.md | 13 ++- problems/sort-the-matrix-diagonally/README.md | 52 +++++++++++ problems/spiral-matrix/README.md | 17 ++++ .../the-k-weakest-rows-in-a-matrix/README.md | 78 ++++++++++++++++ problems/zuma-game/README.md | 76 ++++++++------- tag/array/README.md | 6 ++ tag/binary-search/README.md | 1 + tag/dynamic-programming/README.md | 4 + tag/graph/README.md | 1 + tag/greedy/README.md | 2 + tag/math/README.md | 2 + tag/sort/README.md | 2 + tag/string/README.md | 3 + tag/tree/README.md | 2 + 37 files changed, 1397 insertions(+), 38 deletions(-) create mode 100644 problems/ads-performance/README.md create mode 100644 problems/ads-performance/mysql_schemas.sql create mode 100644 problems/break-a-palindrome/README.md create mode 100644 problems/delete-leaves-with-a-given-value/README.md create mode 100644 problems/filter-restaurants-by-vegan-friendly-price-and-distance/README.md create mode 100644 problems/find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/README.md create mode 100644 problems/jump-game-v/README.md create mode 100644 problems/list-the-products-ordered-in-a-period/README.md create mode 100644 problems/list-the-products-ordered-in-a-period/mysql_schemas.sql create mode 100644 problems/maximum-69-number/README.md create mode 100644 problems/maximum-product-of-splitted-binary-tree/README.md create mode 100644 problems/minimum-difficulty-of-a-job-schedule/README.md create mode 100644 problems/minimum-number-of-taps-to-open-to-water-a-garden/README.md create mode 100644 problems/number-of-transactions-per-visit/README.md create mode 100644 problems/number-of-transactions-per-visit/mysql_schemas.sql create mode 100644 problems/print-words-vertically/README.md create mode 100644 problems/rank-transform-of-an-array/README.md create mode 100644 problems/reduce-array-size-to-the-half/README.md create mode 100644 problems/remove-palindromic-subsequences/README.md create mode 100644 problems/reverse-subarray-to-maximize-array-value/README.md create mode 100644 problems/sort-the-matrix-diagonally/README.md create mode 100644 problems/the-k-weakest-rows-in-a-matrix/README.md diff --git a/README.md b/README.md index 9b15a1ee4..02d19b0a3 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,25 @@ LeetCode Problems' Solutions | # | Title | Solution | Difficulty | | :-: | - | - | :-: | +| 1340 | [Jump Game V](https://leetcode.com/problems/jump-game-v "跳跃游戏 V") | [Go](problems/jump-game-v) | Hard | +| 1339 | [Maximum Product of Splitted Binary Tree](https://leetcode.com/problems/maximum-product-of-splitted-binary-tree "分裂二叉树的最大乘积") | [Go](problems/maximum-product-of-splitted-binary-tree) | Medium | +| 1338 | [Reduce Array Size to The Half](https://leetcode.com/problems/reduce-array-size-to-the-half "数组大小减半") | [Go](problems/reduce-array-size-to-the-half) | Medium | +| 1337 | [The K Weakest Rows in a Matrix](https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix "方阵中战斗力最弱的 K 行") | [Go](problems/the-k-weakest-rows-in-a-matrix) | Easy | +| 1336 | [Number of Transactions per Visit](https://leetcode.com/problems/number-of-transactions-per-visit) 🔒 | [MySQL](problems/number-of-transactions-per-visit) | Medium | +| 1335 | [Minimum Difficulty of a Job Schedule](https://leetcode.com/problems/minimum-difficulty-of-a-job-schedule "工作计划的最低难度") | [Go](problems/minimum-difficulty-of-a-job-schedule) | Hard | +| 1334 | [Find the City With the Smallest Number of Neighbors at a Threshold Distance](https://leetcode.com/problems/find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance "阈值距离内邻居最少的城市") | [Go](problems/find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance) | Medium | +| 1333 | [Filter Restaurants by Vegan-Friendly, Price and Distance](https://leetcode.com/problems/filter-restaurants-by-vegan-friendly-price-and-distance "餐厅过滤器") | [Go](problems/filter-restaurants-by-vegan-friendly-price-and-distance) | Medium | +| 1332 | [Remove Palindromic Subsequences](https://leetcode.com/problems/remove-palindromic-subsequences "删除回文子序列") | [Go](problems/remove-palindromic-subsequences) | Easy | +| 1331 | [Rank Transform of an Array](https://leetcode.com/problems/rank-transform-of-an-array "数组序号转换") | [Go](problems/rank-transform-of-an-array) | Easy | +| 1330 | [Reverse Subarray To Maximize Array Value](https://leetcode.com/problems/reverse-subarray-to-maximize-array-value "翻转子数组得到最大的数组值") | [Go](problems/reverse-subarray-to-maximize-array-value) | Hard | +| 1329 | [Sort the Matrix Diagonally](https://leetcode.com/problems/sort-the-matrix-diagonally "将矩阵按对角线排序") | [Go](problems/sort-the-matrix-diagonally) | Medium | +| 1328 | [Break a Palindrome](https://leetcode.com/problems/break-a-palindrome "破坏回文串") | [Go](problems/break-a-palindrome) | Medium | +| 1327 | [List the Products Ordered in a Period](https://leetcode.com/problems/list-the-products-ordered-in-a-period) 🔒 | [MySQL](problems/list-the-products-ordered-in-a-period) | Easy | +| 1326 | [Minimum Number of Taps to Open to Water a Garden](https://leetcode.com/problems/minimum-number-of-taps-to-open-to-water-a-garden "灌溉花园的最少水龙头数目") | [Go](problems/minimum-number-of-taps-to-open-to-water-a-garden) | Hard | +| 1325 | [Delete Leaves With a Given Value](https://leetcode.com/problems/delete-leaves-with-a-given-value "删除给定值的叶子节点") | [Go](problems/delete-leaves-with-a-given-value) | Medium | +| 1324 | [Print Words Vertically](https://leetcode.com/problems/print-words-vertically "竖直打印单词") | [Go](problems/print-words-vertically) | Medium | +| 1323 | [Maximum 69 Number](https://leetcode.com/problems/maximum-69-number "6 和 9 组成的最大数字") | [Go](problems/maximum-69-number) | Easy | +| 1322 | [Ads Performance](https://leetcode.com/problems/ads-performance) 🔒 | [MySQL](problems/ads-performance) | Easy | | 1321 | [Restaurant Growth](https://leetcode.com/problems/restaurant-growth) 🔒 | [MySQL](problems/restaurant-growth) | Medium | | 1320 | [Minimum Distance to Type a Word Using Two Fingers](https://leetcode.com/problems/minimum-distance-to-type-a-word-using-two-fingers "二指输入的的最小距离") | [Go](problems/minimum-distance-to-type-a-word-using-two-fingers) | Hard | | 1319 | [Number of Operations to Make Network Connected](https://leetcode.com/problems/number-of-operations-to-make-network-connected "连通网络的操作次数") | [Go](problems/number-of-operations-to-make-network-connected) | Medium | diff --git a/problems/ads-performance/README.md b/problems/ads-performance/README.md new file mode 100644 index 000000000..17c8661d2 --- /dev/null +++ b/problems/ads-performance/README.md @@ -0,0 +1,14 @@ + + + + + + + +[< Previous](../restaurant-growth "Restaurant Growth") +                 +[Next >](../maximum-69-number "Maximum 69 Number") + +## [1322. Ads Performance (Easy)](https://leetcode.com/problems/ads-performance "") + + diff --git a/problems/ads-performance/mysql_schemas.sql b/problems/ads-performance/mysql_schemas.sql new file mode 100644 index 000000000..0ccce8bb5 --- /dev/null +++ b/problems/ads-performance/mysql_schemas.sql @@ -0,0 +1,12 @@ +Create table If Not Exists Ads (ad_id int, user_id int, action ENUM('Clicked', 'Viewed', 'Ignored')); +Truncate table Ads; +insert into Ads (ad_id, user_id, action) values ('1', '1', 'Clicked'); +insert into Ads (ad_id, user_id, action) values ('2', '2', 'Clicked'); +insert into Ads (ad_id, user_id, action) values ('3', '3', 'Viewed'); +insert into Ads (ad_id, user_id, action) values ('5', '5', 'Ignored'); +insert into Ads (ad_id, user_id, action) values ('1', '7', 'Ignored'); +insert into Ads (ad_id, user_id, action) values ('2', '7', 'Viewed'); +insert into Ads (ad_id, user_id, action) values ('3', '5', 'Clicked'); +insert into Ads (ad_id, user_id, action) values ('1', '4', 'Viewed'); +insert into Ads (ad_id, user_id, action) values ('2', '11', 'Viewed'); +insert into Ads (ad_id, user_id, action) values ('1', '2', 'Clicked'); diff --git a/problems/break-a-palindrome/README.md b/problems/break-a-palindrome/README.md new file mode 100644 index 000000000..b64bba2c8 --- /dev/null +++ b/problems/break-a-palindrome/README.md @@ -0,0 +1,63 @@ + + + + + + + +[< Previous](../list-the-products-ordered-in-a-period "List the Products Ordered in a Period") +                 +[Next >](../sort-the-matrix-diagonally "Sort the Matrix Diagonally") + +## [1328. Break a Palindrome (Medium)](https://leetcode.com/problems/break-a-palindrome "破坏回文串") + +

Given a palindromic string palindrome, replace exactly one character by any lowercase English letter so that the string becomes the lexicographically smallest possible string that isn't a palindrome.

+ +

After doing so, return the final string.  If there is no way to do so, return the empty string.

+ +

 

+

Example 1:

+ +
+Input: palindrome = "abccba"
+Output: "aaccba"
+
+ +

Example 2:

+ +
+Input: palindrome = "a"
+Output: ""
+
+ +

 

+

Constraints:

+ + + +### Related Topics + [[String](../../tag/string/README.md)] + +### Hints +
+Hint 1 +How to detect if there is impossible to perform the replacement? Only when the length = 1. +
+ +
+Hint 2 +Change the first non 'a' character to 'a'. +
+ +
+Hint 3 +What if the string has only 'a'? +
+ +
+Hint 4 +Change the last character to 'b'. +
diff --git a/problems/delete-leaves-with-a-given-value/README.md b/problems/delete-leaves-with-a-given-value/README.md new file mode 100644 index 000000000..65abf060f --- /dev/null +++ b/problems/delete-leaves-with-a-given-value/README.md @@ -0,0 +1,79 @@ + + + + + + + +[< Previous](../print-words-vertically "Print Words Vertically") +                 +[Next >](../minimum-number-of-taps-to-open-to-water-a-garden "Minimum Number of Taps to Open to Water a Garden") + +## [1325. Delete Leaves With a Given Value (Medium)](https://leetcode.com/problems/delete-leaves-with-a-given-value "删除给定值的叶子节点") + +

Given a binary tree root and an integer target, delete all the leaf nodes with value target.

+ +

Note that once you delete a leaf node with value targetif it's parent node becomes a leaf node and has the value target, it should also be deleted (you need to continue doing that until you can't).

+ +

 

+

Example 1:

+ +

+ +
+Input: root = [1,2,3,2,null,2,4], target = 2
+Output: [1,null,3,null,4]
+Explanation: Leaf nodes in green with value (target = 2) are removed (Picture in left). 
+After removing, new nodes become leaf nodes with value (target = 2) (Picture in center).
+
+ +

Example 2:

+ +

+ +
+Input: root = [1,3,3,3,2], target = 3
+Output: [1,3,null,null,2]
+
+ +

Example 3:

+ +

+ +
+Input: root = [1,2,null,2,null,2], target = 2
+Output: [1]
+Explanation: Leaf nodes in green with value (target = 2) are removed at each step.
+
+ +

Example 4:

+ +
+Input: root = [1,1,1], target = 1
+Output: []
+
+ +

Example 5:

+ +
+Input: root = [1,2,3], target = 1
+Output: [1,2,3]
+
+ +

 

+

Constraints:

+ + + +### Related Topics + [[Tree](../../tag/tree/README.md)] + +### Hints +
+Hint 1 +Use the DFS to reconstruct the tree such that no leaf node is equal to the target. If the leaf node is equal to the target, return an empty object instead. +
diff --git a/problems/filter-restaurants-by-vegan-friendly-price-and-distance/README.md b/problems/filter-restaurants-by-vegan-friendly-price-and-distance/README.md new file mode 100644 index 000000000..9ae2a5248 --- /dev/null +++ b/problems/filter-restaurants-by-vegan-friendly-price-and-distance/README.md @@ -0,0 +1,71 @@ + + + + + + + +[< Previous](../remove-palindromic-subsequences "Remove Palindromic Subsequences") +                 +[Next >](../find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance "Find the City With the Smallest Number of Neighbors at a Threshold Distance") + +## [1333. Filter Restaurants by Vegan-Friendly, Price and Distance (Medium)](https://leetcode.com/problems/filter-restaurants-by-vegan-friendly-price-and-distance "餐厅过滤器") + +

Given the array restaurants where  restaurants[i] = [idi, ratingi, veganFriendlyi, pricei, distancei]. You have to filter the restaurants using three filters.

+ +

The veganFriendly filter will be either true (meaning you should only include restaurants with veganFriendlyi set to true) or false (meaning you can include any restaurant). In addition, you have the filters maxPrice and maxDistance which are the maximum value for price and distance of restaurants you should consider respectively.

+ +

Return the array of restaurant IDs after filtering, ordered by rating from highest to lowest. For restaurants with the same rating, order them by id from highest to lowest. For simplicity veganFriendlyi and veganFriendly take value 1 when it is true, and 0 when it is false.

+ +

 

+

Example 1:

+ +
+Input: restaurants = [[1,4,1,40,10],[2,8,0,50,5],[3,8,1,30,4],[4,10,0,10,3],[5,1,1,15,1]], veganFriendly = 1, maxPrice = 50, maxDistance = 10
+Output: [3,1,5] 
+Explanation: 
+The restaurants are:
+Restaurant 1 [id=1, rating=4, veganFriendly=1, price=40, distance=10]
+Restaurant 2 [id=2, rating=8, veganFriendly=0, price=50, distance=5]
+Restaurant 3 [id=3, rating=8, veganFriendly=1, price=30, distance=4]
+Restaurant 4 [id=4, rating=10, veganFriendly=0, price=10, distance=3]
+Restaurant 5 [id=5, rating=1, veganFriendly=1, price=15, distance=1] 
+After filter restaurants with veganFriendly = 1, maxPrice = 50 and maxDistance = 10 we have restaurant 3, restaurant 1 and restaurant 5 (ordered by rating from highest to lowest). 
+
+ +

Example 2:

+ +
+Input: restaurants = [[1,4,1,40,10],[2,8,0,50,5],[3,8,1,30,4],[4,10,0,10,3],[5,1,1,15,1]], veganFriendly = 0, maxPrice = 50, maxDistance = 10
+Output: [4,3,2,1,5]
+Explanation: The restaurants are the same as in example 1, but in this case the filter veganFriendly = 0, therefore all restaurants are considered.
+
+ +

Example 3:

+ +
+Input: restaurants = [[1,4,1,40,10],[2,8,0,50,5],[3,8,1,30,4],[4,10,0,10,3],[5,1,1,15,1]], veganFriendly = 0, maxPrice = 30, maxDistance = 3
+Output: [4,5]
+
+ +

 

+

Constraints:

+ + + +### Related Topics + [[Sort](../../tag/sort/README.md)] + [[Array](../../tag/array/README.md)] + +### Hints +
+Hint 1 +Do the filtering and sort as said. Note that the id may not be the index in the array. +
diff --git a/problems/find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/README.md b/problems/find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/README.md new file mode 100644 index 000000000..d0ddb9996 --- /dev/null +++ b/problems/find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/README.md @@ -0,0 +1,78 @@ + + + + + + + +[< Previous](../filter-restaurants-by-vegan-friendly-price-and-distance "Filter Restaurants by Vegan-Friendly, Price and Distance") +                 +[Next >](../minimum-difficulty-of-a-job-schedule "Minimum Difficulty of a Job Schedule") + +## [1334. Find the City With the Smallest Number of Neighbors at a Threshold Distance (Medium)](https://leetcode.com/problems/find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance "阈值距离内邻居最少的城市") + +

There are n cities numbered from 0 to n-1. Given the array edges where edges[i] = [fromi, toi, weighti] represents a bidirectional and weighted edge between cities fromi and toi, and given the integer distanceThreshold.

+ +

Return the city with the smallest number of cities that are reachable through some path and whose distance is at most distanceThreshold, If there are multiple such cities, return the city with the greatest number.

+ +

Notice that the distance of a path connecting cities i and j is equal to the sum of the edges' weights along that path.

+ +

 

+

Example 1:

+ +

+ +
+Input: n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4
+Output: 3
+Explanation: The figure above describes the graph. 
+The neighboring cities at a distanceThreshold = 4 for each city are:
+City 0 -> [City 1, City 2] 
+City 1 -> [City 0, City 2, City 3] 
+City 2 -> [City 0, City 1, City 3] 
+City 3 -> [City 1, City 2] 
+Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number.
+
+ +

Example 2:

+ +

+ +
+Input: n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2
+Output: 0
+Explanation: The figure above describes the graph. 
+The neighboring cities at a distanceThreshold = 2 for each city are:
+City 0 -> [City 1] 
+City 1 -> [City 0, City 4] 
+City 2 -> [City 3, City 4] 
+City 3 -> [City 2, City 4]
+City 4 -> [City 1, City 2, City 3] 
+The city 0 has 1 neighboring city at a distanceThreshold = 2.
+
+ +

 

+

Constraints:

+ + + +### Related Topics + [[Graph](../../tag/graph/README.md)] + +### Hints +
+Hint 1 +Use Floyd-Warshall's algorithm to compute any-point to any-point distances. (Or can also do Dijkstra from every node due to the weights are non-negative). +
+ +
+Hint 2 +For each city calculate the number of reachable cities within the threshold, then search for the optimal city. +
diff --git a/problems/get-watched-videos-by-your-friends/README.md b/problems/get-watched-videos-by-your-friends/README.md index d3e1a3f97..fbaddab8c 100644 --- a/problems/get-watched-videos-by-your-friends/README.md +++ b/problems/get-watched-videos-by-your-friends/README.md @@ -13,7 +13,7 @@

There are n people, each person has a unique id between 0 and n-1. Given the arrays watchedVideos and friends, where watchedVideos[i] and friends[i] contain the list of watched videos and the list of friends respectively for the person with id = i.

-

Level 1 of videos are all watched videos by your friends, level 2 of videos are all watched videos by the friends of your friends and so on. In general, the level k of videos are all watched videos by people with the shortest path equal to k with you. Given your id and the level of videos, return the list of videos ordered by their frequencies (increasing). For videos with the same frequency order them alphabetically from least to greatest. 

+

Level 1 of videos are all watched videos by your friends, level 2 of videos are all watched videos by the friends of your friends and so on. In general, the level k of videos are all watched videos by people with the shortest path exactly equal to k with you. Given your id and the level of videos, return the list of videos ordered by their frequencies (increasing). For videos with the same frequency order them alphabetically from least to greatest. 

 

Example 1:

diff --git a/problems/jump-game-v/README.md b/problems/jump-game-v/README.md new file mode 100644 index 000000000..b30bfe79c --- /dev/null +++ b/problems/jump-game-v/README.md @@ -0,0 +1,89 @@ + + + + + + + +[< Previous](../maximum-product-of-splitted-binary-tree "Maximum Product of Splitted Binary Tree") +                 +Next > + +## [1340. Jump Game V (Hard)](https://leetcode.com/problems/jump-game-v "跳跃游戏 V") + +

Given an array of integers arr and an integer d. In one step you can jump from index i to index:

+ + + +

In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)).

+ +

You can choose any index of the array and start jumping. Return the maximum number of indices you can visit.

+ +

Notice that you can not jump outside of the array at any time.

+ +

 

+

Example 1:

+ +
+Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2
+Output: 4
+Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown.
+Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9.
+Similarly You cannot jump from index 3 to index 2 or index 1.
+
+ +

Example 2:

+ +
+Input: arr = [3,3,3,3,3], d = 3
+Output: 1
+Explanation: You can start at any index. You always cannot jump to any index.
+
+ +

Example 3:

+ +
+Input: arr = [7,6,5,4,3,2,1], d = 1
+Output: 7
+Explanation: Start at index 0. You can visit all the indicies. 
+
+ +

Example 4:

+ +
+Input: arr = [7,1,7,1,7,1], d = 2
+Output: 2
+
+ +

Example 5:

+ +
+Input: arr = [66], d = 1
+Output: 1
+
+ +

 

+

Constraints:

+ + + +### Related Topics + [[Dynamic Programming](../../tag/dynamic-programming/README.md)] + +### Hints +
+Hint 1 +Use dynamic programming. dp[i] is max jumps you can do starting from index i. Answer is max(dp[i]). +
+ +
+Hint 2 +dp[i] = 1 + max (dp[j]) where j is all indices you can reach from i. +
diff --git a/problems/list-the-products-ordered-in-a-period/README.md b/problems/list-the-products-ordered-in-a-period/README.md new file mode 100644 index 000000000..e33dfda4b --- /dev/null +++ b/problems/list-the-products-ordered-in-a-period/README.md @@ -0,0 +1,14 @@ + + + + + + + +[< Previous](../minimum-number-of-taps-to-open-to-water-a-garden "Minimum Number of Taps to Open to Water a Garden") +                 +[Next >](../break-a-palindrome "Break a Palindrome") + +## [1327. List the Products Ordered in a Period (Easy)](https://leetcode.com/problems/list-the-products-ordered-in-a-period "") + + diff --git a/problems/list-the-products-ordered-in-a-period/mysql_schemas.sql b/problems/list-the-products-ordered-in-a-period/mysql_schemas.sql new file mode 100644 index 000000000..a69778f8f --- /dev/null +++ b/problems/list-the-products-ordered-in-a-period/mysql_schemas.sql @@ -0,0 +1,21 @@ +Create table If Not Exists Products (product_id int, product_name varchar(40), product_category varchar(40)); +Create table If Not Exists Orders (product_id int, order_date date, unit int); +Truncate table Products; +insert into Products (product_id, product_name, product_category) values ('1', 'Leetcode Solutions', 'Book'); +insert into Products (product_id, product_name, product_category) values ('2', 'Jewels of Stringology', 'Book'); +insert into Products (product_id, product_name, product_category) values ('3', 'HP', 'Laptop'); +insert into Products (product_id, product_name, product_category) values ('4', 'Lenovo', 'Laptop'); +insert into Products (product_id, product_name, product_category) values ('5', 'Leetcode Kit', 'T-shirt'); +Truncate table Orders; +insert into Orders (product_id, order_date, unit) values ('1', '2020-02-05', '60'); +insert into Orders (product_id, order_date, unit) values ('1', '2020-02-10', '70'); +insert into Orders (product_id, order_date, unit) values ('2', '2020-01-18', '30'); +insert into Orders (product_id, order_date, unit) values ('2', '2020-02-11', '80'); +insert into Orders (product_id, order_date, unit) values ('3', '2020-02-17', '2'); +insert into Orders (product_id, order_date, unit) values ('3', '2020-02-24', '3'); +insert into Orders (product_id, order_date, unit) values ('4', '2020-03-01', '20'); +insert into Orders (product_id, order_date, unit) values ('4', '2020-03-04', '30'); +insert into Orders (product_id, order_date, unit) values ('4', '2020-03-04', '60'); +insert into Orders (product_id, order_date, unit) values ('5', '2020-02-25', '50'); +insert into Orders (product_id, order_date, unit) values ('5', '2020-02-27', '50'); +insert into Orders (product_id, order_date, unit) values ('5', '2020-03-01', '50'); diff --git a/problems/maximum-69-number/README.md b/problems/maximum-69-number/README.md new file mode 100644 index 000000000..002a3340a --- /dev/null +++ b/problems/maximum-69-number/README.md @@ -0,0 +1,66 @@ + + + + + + + +[< Previous](../ads-performance "Ads Performance") +                 +[Next >](../print-words-vertically "Print Words Vertically") + +## [1323. Maximum 69 Number (Easy)](https://leetcode.com/problems/maximum-69-number "6 和 9 组成的最大数字") + +

Given a positive integer num consisting only of digits 6 and 9.

+ +

Return the maximum number you can get by changing at most one digit (6 becomes 9, and 9 becomes 6).

+ +

 

+

Example 1:

+ +
+Input: num = 9669
+Output: 9969
+Explanation: 
+Changing the first digit results in 6669.
+Changing the second digit results in 9969.
+Changing the third digit results in 9699.
+Changing the fourth digit results in 9666. 
+The maximum number is 9969.
+
+ +

Example 2:

+ +
+Input: num = 9996
+Output: 9999
+Explanation: Changing the last digit 6 to 9 results in the maximum number.
+ +

Example 3:

+ +
+Input: num = 9999
+Output: 9999
+Explanation: It is better not to apply any change.
+ +

 

+

Constraints:

+ + + +### Related Topics + [[Math](../../tag/math/README.md)] + +### Hints +
+Hint 1 +Convert the number in an array of its digits. +
+ +
+Hint 2 +Brute force on every digit to get the maximum number. +
diff --git a/problems/maximum-product-of-splitted-binary-tree/README.md b/problems/maximum-product-of-splitted-binary-tree/README.md new file mode 100644 index 000000000..fbe9b425e --- /dev/null +++ b/problems/maximum-product-of-splitted-binary-tree/README.md @@ -0,0 +1,69 @@ + + + + + + + +[< Previous](../reduce-array-size-to-the-half "Reduce Array Size to The Half") +                 +[Next >](../jump-game-v "Jump Game V") + +## [1339. Maximum Product of Splitted Binary Tree (Medium)](https://leetcode.com/problems/maximum-product-of-splitted-binary-tree "分裂二叉树的最大乘积") + +

Given a binary tree root. Split the binary tree into two subtrees by removing 1 edge such that the product of the sums of the subtrees are maximized.

+ +

Since the answer may be too large, return it modulo 10^9 + 7.

+ +

 

+

Example 1:

+ +

+ +
+Input: root = [1,2,3,4,5,6]
+Output: 110
+Explanation: Remove the red edge and get 2 binary trees with sum 11 and 10. Their product is 110 (11*10)
+
+ +

Example 2:

+ +

+ +
+Input: root = [1,null,2,3,4,null,null,5,6]
+Output: 90
+Explanation:  Remove the red edge and get 2 binary trees with sum 15 and 6.Their product is 90 (15*6)
+
+ +

Example 3:

+ +
+Input: root = [2,3,9,10,7,8,6,5,4,11,1]
+Output: 1025
+
+ +

Example 4:

+ +
+Input: root = [1,1]
+Output: 1
+
+ +

 

+

Constraints:

+ + + +### Related Topics + [[Tree](../../tag/tree/README.md)] + [[Dynamic Programming](../../tag/dynamic-programming/README.md)] + +### Hints +
+Hint 1 +If we know the sum of a subtree, the answer is max( (total_sum - subtree_sum) * subtree_sum) in each node. +
diff --git a/problems/minimum-difficulty-of-a-job-schedule/README.md b/problems/minimum-difficulty-of-a-job-schedule/README.md new file mode 100644 index 000000000..d45e64349 --- /dev/null +++ b/problems/minimum-difficulty-of-a-job-schedule/README.md @@ -0,0 +1,84 @@ + + + + + + + +[< Previous](../find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance "Find the City With the Smallest Number of Neighbors at a Threshold Distance") +                 +[Next >](../number-of-transactions-per-visit "Number of Transactions per Visit") + +## [1335. Minimum Difficulty of a Job Schedule (Hard)](https://leetcode.com/problems/minimum-difficulty-of-a-job-schedule "工作计划的最低难度") + +

You want to schedule a list of jobs in d days. Jobs are dependent (i.e To work on the i-th job, you have to finish all the jobs j where 0 <= j < i).

+ +

You have to finish at least one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the d days. The difficulty of a day is the maximum difficulty of a job done in that day.

+ +

Given an array of integers jobDifficulty and an integer d. The difficulty of the i-th job is jobDifficulty[i].

+ +

Return the minimum difficulty of a job schedule. If you cannot find a schedule for the jobs return -1.

+ +

 

+

Example 1:

+ +
+Input: jobDifficulty = [6,5,4,3,2,1], d = 2
+Output: 7
+Explanation: First day you can finish the first 5 jobs, total difficulty = 6.
+Second day you can finish the last job, total difficulty = 1.
+The difficulty of the schedule = 6 + 1 = 7 
+
+ +

Example 2:

+ +
+Input: jobDifficulty = [9,9,9], d = 4
+Output: -1
+Explanation: If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs.
+
+ +

Example 3:

+ +
+Input: jobDifficulty = [1,1,1], d = 3
+Output: 3
+Explanation: The schedule is one job per day. total difficulty will be 3.
+
+ +

Example 4:

+ +
+Input: jobDifficulty = [7,1,7,1,7,1], d = 3
+Output: 15
+
+ +

Example 5:

+ +
+Input: jobDifficulty = [11,111,22,222,33,333,44,444], d = 6
+Output: 843
+
+ +

 

+

Constraints:

+ + + +### Related Topics + [[Dynamic Programming](../../tag/dynamic-programming/README.md)] + +### Hints +
+Hint 1 +Use DP. Try to cut the array into d non-empty sub-arrays. Try all possible cuts for the array. +
+ +
+Hint 2 +Use dp[i][j] where DP states are i the index of the last cut and j the number of remaining cuts. Complexity is O(n * n * d). +
diff --git a/problems/minimum-number-of-taps-to-open-to-water-a-garden/README.md b/problems/minimum-number-of-taps-to-open-to-water-a-garden/README.md new file mode 100644 index 000000000..b3fbccb97 --- /dev/null +++ b/problems/minimum-number-of-taps-to-open-to-water-a-garden/README.md @@ -0,0 +1,93 @@ + + + + + + + +[< Previous](../delete-leaves-with-a-given-value "Delete Leaves With a Given Value") +                 +[Next >](../list-the-products-ordered-in-a-period "List the Products Ordered in a Period") + +## [1326. Minimum Number of Taps to Open to Water a Garden (Hard)](https://leetcode.com/problems/minimum-number-of-taps-to-open-to-water-a-garden "灌溉花园的最少水龙头数目") + +

There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n).

+ +

There are n + 1 taps located at points [0, 1, ..., n] in the garden.

+ +

Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open.

+ +

Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.

+ +

 

+

Example 1:

+ +
+Input: n = 5, ranges = [3,4,1,1,0,0]
+Output: 1
+Explanation: The tap at point 0 can cover the interval [-3,3]
+The tap at point 1 can cover the interval [-3,5]
+The tap at point 2 can cover the interval [1,3]
+The tap at point 3 can cover the interval [2,4]
+The tap at point 4 can cover the interval [4,4]
+The tap at point 5 can cover the interval [5,5]
+Opening Only the second tap will water the whole garden [0,5]
+
+ +

Example 2:

+ +
+Input: n = 3, ranges = [0,0,0,0]
+Output: -1
+Explanation: Even if you activate all the four taps you cannot water the whole garden.
+
+ +

Example 3:

+ +
+Input: n = 7, ranges = [1,2,1,0,2,1,0,1]
+Output: 3
+
+ +

Example 4:

+ +
+Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4]
+Output: 2
+
+ +

Example 5:

+ +
+Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4]
+Output: 1
+
+ +

 

+

Constraints:

+ + + +### Related Topics + [[Greedy](../../tag/greedy/README.md)] + [[Dynamic Programming](../../tag/dynamic-programming/README.md)] + +### Hints +
+Hint 1 +Create intervals of the area covered by each tap, sort intervals by the left end. +
+ +
+Hint 2 +We need to cover the interval [0, n]. we can start with the first interval and out of all intervals that intersect with it we choose the one that covers the farthest point to the right. +
+ +
+Hint 3 +What if there is a gap between intervals that is not covered ? we should stop and return -1 as there is some interval that cannot be covered. +
diff --git a/problems/number-of-transactions-per-visit/README.md b/problems/number-of-transactions-per-visit/README.md new file mode 100644 index 000000000..21309d506 --- /dev/null +++ b/problems/number-of-transactions-per-visit/README.md @@ -0,0 +1,14 @@ + + + + + + + +[< Previous](../minimum-difficulty-of-a-job-schedule "Minimum Difficulty of a Job Schedule") +                 +[Next >](../the-k-weakest-rows-in-a-matrix "The K Weakest Rows in a Matrix") + +## [1336. Number of Transactions per Visit (Medium)](https://leetcode.com/problems/number-of-transactions-per-visit "") + + diff --git a/problems/number-of-transactions-per-visit/mysql_schemas.sql b/problems/number-of-transactions-per-visit/mysql_schemas.sql new file mode 100644 index 000000000..0c16a9c57 --- /dev/null +++ b/problems/number-of-transactions-per-visit/mysql_schemas.sql @@ -0,0 +1,22 @@ +Create table If Not Exists Visits (user_id int, visit_date date); +Create table If Not Exists Transactions (user_id int, transaction_date date, amount int); +Truncate table Visits; +insert into Visits (user_id, visit_date) values ('1', '2020-01-01'); +insert into Visits (user_id, visit_date) values ('2', '2020-01-02'); +insert into Visits (user_id, visit_date) values ('12', '2020-01-01'); +insert into Visits (user_id, visit_date) values ('19', '2020-01-03'); +insert into Visits (user_id, visit_date) values ('1', '2020-01-02'); +insert into Visits (user_id, visit_date) values ('2', '2020-01-03'); +insert into Visits (user_id, visit_date) values ('1', '2020-01-04'); +insert into Visits (user_id, visit_date) values ('7', '2020-01-11'); +insert into Visits (user_id, visit_date) values ('9', '2020-01-25'); +insert into Visits (user_id, visit_date) values ('8', '2020-01-28'); +Truncate table Transactions; +insert into Transactions (user_id, transaction_date, amount) values ('1', '2020-01-02', '120'); +insert into Transactions (user_id, transaction_date, amount) values ('2', '2020-01-03', '22'); +insert into Transactions (user_id, transaction_date, amount) values ('7', '2020-01-11', '232'); +insert into Transactions (user_id, transaction_date, amount) values ('1', '2020-01-04', '7'); +insert into Transactions (user_id, transaction_date, amount) values ('9', '2020-01-25', '33'); +insert into Transactions (user_id, transaction_date, amount) values ('9', '2020-01-25', '66'); +insert into Transactions (user_id, transaction_date, amount) values ('8', '2020-01-28', '1'); +insert into Transactions (user_id, transaction_date, amount) values ('9', '2020-01-25', '99'); diff --git a/problems/print-words-vertically/README.md b/problems/print-words-vertically/README.md new file mode 100644 index 000000000..ca42eb973 --- /dev/null +++ b/problems/print-words-vertically/README.md @@ -0,0 +1,64 @@ + + + + + + + +[< Previous](../maximum-69-number "Maximum 69 Number") +                 +[Next >](../delete-leaves-with-a-given-value "Delete Leaves With a Given Value") + +## [1324. Print Words Vertically (Medium)](https://leetcode.com/problems/print-words-vertically "竖直打印单词") + +

Given a string s. Return all the words vertically in the same order in which they appear in s.
+Words are returned as a list of strings, complete with spaces when is necessary. (Trailing spaces are not allowed).
+Each word would be put on only one column and that in one column there will be only one word.

+ +

 

+

Example 1:

+ +
+Input: s = "HOW ARE YOU"
+Output: ["HAY","ORO","WEU"]
+Explanation: Each word is printed vertically. 
+ "HAY"
+ "ORO"
+ "WEU"
+
+ +

Example 2:

+ +
+Input: s = "TO BE OR NOT TO BE"
+Output: ["TBONTB","OEROOE","   T"]
+Explanation: Trailing spaces is not allowed. 
+"TBONTB"
+"OEROOE"
+"   T"
+
+ +

Example 3:

+ +
+Input: s = "CONTEST IS COMING"
+Output: ["CIC","OSO","N M","T I","E N","S G","T"]
+
+ +

 

+

Constraints:

+ + + +### Related Topics + [[String](../../tag/string/README.md)] + +### Hints +
+Hint 1 +Use the maximum length of words to determine the length of the returned answer. However, don't forget to remove trailing spaces. +
diff --git a/problems/rank-transform-of-an-array/README.md b/problems/rank-transform-of-an-array/README.md new file mode 100644 index 000000000..0d8e92a8f --- /dev/null +++ b/problems/rank-transform-of-an-array/README.md @@ -0,0 +1,67 @@ + + + + + + + +[< Previous](../reverse-subarray-to-maximize-array-value "Reverse Subarray To Maximize Array Value") +                 +[Next >](../remove-palindromic-subsequences "Remove Palindromic Subsequences") + +## [1331. Rank Transform of an Array (Easy)](https://leetcode.com/problems/rank-transform-of-an-array "数组序号转换") + +

Given an array of integers arr, replace each element with its rank.

+ +

The rank represents how large the element is. The rank has the following rules:

+ + + +

 

+

Example 1:

+ +
+Input: arr = [40,10,20,30]
+Output: [4,1,2,3]
+Explanation: 40 is the largest element. 10 is the smallest. 20 is the second smallest. 30 is the third smallest.
+ +

Example 2:

+ +
+Input: arr = [100,100,100]
+Output: [1,1,1]
+Explanation: Same elements share the same rank.
+
+ +

Example 3:

+ +
+Input: arr = [37,12,28,9,100,56,80,5,12]
+Output: [5,3,4,2,8,6,7,1,3]
+
+ +

 

+

Constraints:

+ + + +### Related Topics + [[Array](../../tag/array/README.md)] + +### Hints +
+Hint 1 +Use a temporary array to copy the array and sort it. +
+ +
+Hint 2 +The rank of each element is the number of elements smaller than it in the sorted array plus one. +
diff --git a/problems/reduce-array-size-to-the-half/README.md b/problems/reduce-array-size-to-the-half/README.md new file mode 100644 index 000000000..954a94437 --- /dev/null +++ b/problems/reduce-array-size-to-the-half/README.md @@ -0,0 +1,85 @@ + + + + + + + +[< Previous](../the-k-weakest-rows-in-a-matrix "The K Weakest Rows in a Matrix") +                 +[Next >](../maximum-product-of-splitted-binary-tree "Maximum Product of Splitted Binary Tree") + +## [1338. Reduce Array Size to The Half (Medium)](https://leetcode.com/problems/reduce-array-size-to-the-half "数组大小减半") + +

Given an array arr.  You can choose a set of integers and remove all the occurrences of these integers in the array.

+ +

Return the minimum size of the set so that at least half of the integers of the array are removed.

+ +

 

+

Example 1:

+ +
+Input: arr = [3,3,3,3,5,5,5,2,2,7]
+Output: 2
+Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
+Possible sets of size 2 are {3,5},{3,2},{5,2}.
+Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
+
+ +

Example 2:

+ +
+Input: arr = [7,7,7,7,7,7]
+Output: 1
+Explanation: The only possible set you can choose is {7}. This will make the new array empty.
+
+ +

Example 3:

+ +
+Input: arr = [1,9]
+Output: 1
+
+ +

Example 4:

+ +
+Input: arr = [1000,1000,3,7]
+Output: 1
+
+ +

Example 5:

+ +
+Input: arr = [1,2,3,4,5,6,7,8,9,10]
+Output: 5
+
+ +

 

+

Constraints:

+ + + +### Related Topics + [[Greedy](../../tag/greedy/README.md)] + [[Array](../../tag/array/README.md)] + +### Hints +
+Hint 1 +Count the frequency of each integer in the array. +
+ +
+Hint 2 +Start with an empty set, add to the set the integer with the maximum frequency. +
+ +
+Hint 3 +Keep Adding the integer with the max frequency until you remove at least half of the integers. +
diff --git a/problems/remove-palindromic-subsequences/README.md b/problems/remove-palindromic-subsequences/README.md new file mode 100644 index 000000000..4380f54d4 --- /dev/null +++ b/problems/remove-palindromic-subsequences/README.md @@ -0,0 +1,76 @@ + + + + + + + +[< Previous](../rank-transform-of-an-array "Rank Transform of an Array") +                 +[Next >](../filter-restaurants-by-vegan-friendly-price-and-distance "Filter Restaurants by Vegan-Friendly, Price and Distance") + +## [1332. Remove Palindromic Subsequences (Easy)](https://leetcode.com/problems/remove-palindromic-subsequences "删除回文子序列") + +

Given a string s consisting only of letters 'a' and 'b'. In a single step you can remove one palindromic subsequence from s.

+ +

Return the minimum number of steps to make the given string empty.

+ +

A string is a subsequence of a given string, if it is generated by deleting some characters of a given string without changing its order.

+ +

A string is called palindrome if is one that reads the same backward as well as forward.

+ +

 

+

Example 1:

+ +
+Input: s = "ababa"
+Output: 1
+Explanation: String is already palindrome
+
+ +

Example 2:

+ +
+Input: s = "abb"
+Output: 2
+Explanation: "abb" -> "bb" -> "". 
+Remove palindromic subsequence "a" then "bb".
+
+ +

Example 3:

+ +
+Input: s = "baabb"
+Output: 2
+Explanation: "baabb" -> "b" -> "". 
+Remove palindromic subsequence "baab" then "b".
+
+ +

Example 4:

+ +
+Input: s = ""
+Output: 0
+
+ +

 

+

Constraints:

+ + + +### Related Topics + [[String](../../tag/string/README.md)] + +### Hints +
+Hint 1 +Use the fact that string contains only 2 characters. +
+ +
+Hint 2 +Are subsequences composed of only one type of letter always palindrome strings ? +
diff --git a/problems/restaurant-growth/README.md b/problems/restaurant-growth/README.md index a13c18fff..71b2ba3fb 100644 --- a/problems/restaurant-growth/README.md +++ b/problems/restaurant-growth/README.md @@ -7,7 +7,7 @@ [< Previous](../minimum-distance-to-type-a-word-using-two-fingers "Minimum Distance to Type a Word Using Two Fingers")                  -Next > +[Next >](../ads-performance "Ads Performance") ## [1321. Restaurant Growth (Medium)](https://leetcode.com/problems/restaurant-growth "") diff --git a/problems/reverse-subarray-to-maximize-array-value/README.md b/problems/reverse-subarray-to-maximize-array-value/README.md new file mode 100644 index 000000000..697eb0f3c --- /dev/null +++ b/problems/reverse-subarray-to-maximize-array-value/README.md @@ -0,0 +1,72 @@ + + + + + + + +[< Previous](../sort-the-matrix-diagonally "Sort the Matrix Diagonally") +                 +[Next >](../rank-transform-of-an-array "Rank Transform of an Array") + +## [1330. Reverse Subarray To Maximize Array Value (Hard)](https://leetcode.com/problems/reverse-subarray-to-maximize-array-value "翻转子数组得到最大的数组值") + +

You are given an integer array nums. The value of this array is defined as the sum of |nums[i]-nums[i+1]| for all 0 <= i < nums.length-1.

+ +

You are allowed to select any subarray of the given array and reverse it. You can perform this operation only once.

+ +

Find maximum possible value of the final array.

+ +

 

+

Example 1:

+ +
+Input: nums = [2,3,1,5,4]
+Output: 10
+Explanation: By reversing the subarray [3,1,5] the array becomes [2,5,1,3,4] whose value is 10.
+
+ +

Example 2:

+ +
+Input: nums = [2,4,9,24,2,1,10]
+Output: 68
+
+ +

 

+

Constraints:

+ + + +### Related Topics + [[Array](../../tag/array/README.md)] + [[Math](../../tag/math/README.md)] + +### Hints +
+Hint 1 +What's the score after reversing a sub-array [L, R] ? +
+ +
+Hint 2 +It's the score without reversing it + abs(a[R] - a[L-1]) + abs(a[L] - a[R+1]) - abs(a[L] - a[L-1]) - abs(a[R] - a[R+1]) +
+ +
+Hint 3 +How to maximize that formula given that abs(x - y) = max(x - y, y - x) ? +
+ +
+Hint 4 +This can be written as max(max(a[R] - a[L - 1], a[L - 1] - a[R]) + max(a[R + 1] - a[L], a[L] - a[R + 1]) - value(L) - value(R + 1)) over all L < R where value(i) = abs(a[i] - a[i-1]) +
+ +
+Hint 5 +This can be divided into 4 cases. +
diff --git a/problems/sliding-window-median/README.md b/problems/sliding-window-median/README.md index 6f6b6e780..86deb00fb 100644 --- a/problems/sliding-window-median/README.md +++ b/problems/sliding-window-median/README.md @@ -12,13 +12,15 @@ ## [480. Sliding Window Median (Hard)](https://leetcode.com/problems/sliding-window-median "滑动窗口中位数")

Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value.

-Examples:
+Examples: +

[2,3,4] , the median is 3

-

[2,3], the median is (2 + 3) / 2 = 2.5

+ +

[2,3], the median is (2 + 3) / 2 = 2.5

Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position. Your job is to output the median array for each window in the original array.

-

For example,
+

For example,
Given nums = [1,3,-1,-3,5,3,6,7], and k = 3.

@@ -34,8 +36,9 @@ Window position                Median
 
 

Therefore, return the median sliding window as [1,-1,-1,3,5,6].

-

Note:
-You may assume k is always valid, ie: k is always smaller than input array's size for non-empty array.

+

Note:
+You may assume k is always valid, ie: k is always smaller than input array's size for non-empty array.
+Answers within 10^-5 of the actual value will be accepted as correct.

### Related Topics [[Sliding Window](../../tag/sliding-window/README.md)] diff --git a/problems/sort-the-matrix-diagonally/README.md b/problems/sort-the-matrix-diagonally/README.md new file mode 100644 index 000000000..d99b12887 --- /dev/null +++ b/problems/sort-the-matrix-diagonally/README.md @@ -0,0 +1,52 @@ + + + + + + + +[< Previous](../break-a-palindrome "Break a Palindrome") +                 +[Next >](../reverse-subarray-to-maximize-array-value "Reverse Subarray To Maximize Array Value") + +## [1329. Sort the Matrix Diagonally (Medium)](https://leetcode.com/problems/sort-the-matrix-diagonally "将矩阵按对角线排序") + +

Given a m * n matrix mat of integers, sort it diagonally in ascending order from the top-left to the bottom-right then return the sorted array.

+ +

 

+

Example 1:

+ +
+Input: mat = [[3,3,1,1],[2,2,1,2],[1,1,1,2]]
+Output: [[1,1,1,1],[1,2,2,2],[1,2,3,3]]
+
+ +

 

+

Constraints:

+ + + +### Related Topics + [[Sort](../../tag/sort/README.md)] + [[Array](../../tag/array/README.md)] + +### Hints +
+Hint 1 +Use a data structure to store all values of each diagonal. +
+ +
+Hint 2 +How to index the data structure with the id of the diagonal? +
+ +
+Hint 3 +All cells in the same diagonal (i,j) have the same difference so we can get the diagonal of a cell using the difference i-j. +
diff --git a/problems/spiral-matrix/README.md b/problems/spiral-matrix/README.md index f0139fd00..428ad14cb 100644 --- a/problems/spiral-matrix/README.md +++ b/problems/spiral-matrix/README.md @@ -41,3 +41,20 @@ ### Similar Questions 1. [Spiral Matrix II](../spiral-matrix-ii) (Medium) + +### Hints +
+Hint 1 +Well for some problems, the best way really is to come up with some algorithms for simulation. Basically, you need to simulate what the problem asks us to do. +
+ +
+Hint 2 +We go boundary by boundary and move inwards. That is the essential operation. First row, last column, last row, first column and then we move inwards by 1 and then repeat. That's all, that is all the simulation that we need. +
+ +
+Hint 3 +Think about when you want to switch the progress on one of the indexes. If you progress on
i
out of
[i, j]
, you'd be shifting in the same column. Similarly, by changing values for
j
, you'd be shifting in the same row. +Also, keep track of the end of a boundary so that you can move inwards and then keep repeating. It's always best to run the simulation on edge cases like a single column or a single row to see if anything breaks or not. +
diff --git a/problems/the-k-weakest-rows-in-a-matrix/README.md b/problems/the-k-weakest-rows-in-a-matrix/README.md new file mode 100644 index 000000000..411515b6c --- /dev/null +++ b/problems/the-k-weakest-rows-in-a-matrix/README.md @@ -0,0 +1,78 @@ + + + + + + + +[< Previous](../number-of-transactions-per-visit "Number of Transactions per Visit") +                 +[Next >](../reduce-array-size-to-the-half "Reduce Array Size to The Half") + +## [1337. The K Weakest Rows in a Matrix (Easy)](https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix "方阵中战斗力最弱的 K 行") + +

Given a m * n matrix mat of ones (representing soldiers) and zeros (representing civilians), return the indexes of the k weakest rows in the matrix ordered from the weakest to the strongest.

+ +

A row i is weaker than row j, if the number of soldiers in row i is less than the number of soldiers in row j, or they have the same number of soldiers but i is less than j. Soldiers are always stand in the frontier of a row, that is, always ones may appear first and then zeros.

+ +

 

+

Example 1:

+ +
+Input: mat = 
+[[1,1,0,0,0],
+ [1,1,1,1,0],
+ [1,0,0,0,0],
+ [1,1,0,0,0],
+ [1,1,1,1,1]], 
+k = 3
+Output: [2,0,3]
+Explanation: 
+The number of soldiers for each row is: 
+row 0 -> 2 
+row 1 -> 4 
+row 2 -> 1 
+row 3 -> 2 
+row 4 -> 5 
+Rows ordered from the weakest to the strongest are [2,0,3,1,4]
+
+ +

Example 2:

+ +
+Input: mat = 
+[[1,0,0,0],
+ [1,1,1,1],
+ [1,0,0,0],
+ [1,0,0,0]], 
+k = 2
+Output: [0,2]
+Explanation: 
+The number of soldiers for each row is: 
+row 0 -> 1 
+row 1 -> 4 
+row 2 -> 1 
+row 3 -> 1 
+Rows ordered from the weakest to the strongest are [0,2,3,1]
+
+ +

 

+

Constraints:

+ + + +### Related Topics + [[Array](../../tag/array/README.md)] + [[Binary Search](../../tag/binary-search/README.md)] + +### Hints +
+Hint 1 +Sort the matrix row indexes by the number of soldiers and then row indexes. +
diff --git a/problems/zuma-game/README.md b/problems/zuma-game/README.md index a8d9b5b65..03e93ae42 100644 --- a/problems/zuma-game/README.md +++ b/problems/zuma-game/README.md @@ -12,39 +12,53 @@ ## [488. Zuma Game (Hard)](https://leetcode.com/problems/zuma-game "祖玛游戏")

Think about Zuma Game. You have a row of balls on the table, colored red(R), yellow(Y), blue(B), green(G), and white(W). You also have several balls in your hand.

-

-Each time, you may choose a ball in your hand, and insert it into the row (including the leftmost place and rightmost place). Then, if there is a group of 3 or more balls in the same color touching, remove these balls. Keep doing this until no more balls can be removed.

-

-Find the minimal balls you have to insert to remove all the balls on the table. If you cannot remove all the balls, output -1. -

+ +

Each time, you may choose a ball in your hand, and insert it into the row (including the leftmost place and rightmost place). Then, if there is a group of 3 or more balls in the same color touching, remove these balls. Keep doing this until no more balls can be removed.

+ +

Find the minimal balls you have to insert to remove all the balls on the table. If you cannot remove all the balls, output -1.

+ +

 

+

Example 1:

+ +
+Input: board = "WRRBBW", hand = "RB"
+Output: -1
+Explanation: WRRBBW -> WRR[R]BBW -> WBBW -> WBB[B]W -> WW
+
+ +

Example 2:

+ +
+Input: board = "WWRRBBWW", hand = "WRBRW"
+Output: 2
+Explanation: WWRRBBWW -> WWRR[R]BBWW -> WWBBWW -> WWBB[B]WW -> WWWW -> empty
+
+ +

Example 3:

+
-

Examples:
-Input: "WRRBBW", "RB" -Output: -1 -Explanation: WRRBBW -> WRR[R]BBW -> WBBW -> WBB[B]W -> WW - -Input: "WWRRBBWW", "WRBRW" -Output: 2 -Explanation: WWRRBBWW -> WWRR[R]BBWW -> WWBBWW -> WWBB[B]WW -> WWWW -> empty - -Input:"G", "GGGGG" -Output: 2 -Explanation: G -> G[G] -> GG[G] -> empty - -Input: "RBYYBBRRB", "YRBGB" -Output: 3 -Explanation: RBYYBBRRB -> RBYY[Y]BBRRB -> RBBBRRB -> RRRB -> B -> B[B] -> BB[B] -> empty +Input: board = "G", hand = "GGGGG" +Output: 2 +Explanation: G -> G[G] -> GG[G] -> empty

-

- -

Note:
-

    -
  1. You may assume that the initial row of balls on the table won’t have any 3 or more consecutive balls with the same color.
  2. -
  3. The number of balls on the table won't exceed 20, and the string represents these balls is called "board" in the input.
  4. -
  5. The number of balls in your hand won't exceed 5, and the string represents these balls is called "hand" in the input.
  6. -
  7. Both input strings will be non-empty and only contain characters 'R','Y','B','G','W'.
  8. -
-

+ +

Example 4:

+ +
+Input: board = "RBYYBBRRB", hand = "YRBGB"
+Output: 3
+Explanation: RBYYBBRRB -> RBYY[Y]BBRRB -> RBBBRRB -> RRRB -> B -> B[B] -> BB[B] -> empty 
+
+ +

 

+

Constraints:

+ + ### Related Topics [[Depth-first Search](../../tag/depth-first-search/README.md)] diff --git a/tag/array/README.md b/tag/array/README.md index 5e864adc6..a5f55a21f 100644 --- a/tag/array/README.md +++ b/tag/array/README.md @@ -9,6 +9,12 @@ | # | 题目 | 标签 | 难度 | | :-: | - | - | :-: | +| 1338 | [数组大小减半](../../problems/reduce-array-size-to-the-half) | [[贪心算法](../greedy/README.md)] [[数组](../array/README.md)] | Medium | +| 1337 | [方阵中战斗力最弱的 K 行](../../problems/the-k-weakest-rows-in-a-matrix) | [[数组](../array/README.md)] [[二分查找](../binary-search/README.md)] | Easy | +| 1333 | [餐厅过滤器](../../problems/filter-restaurants-by-vegan-friendly-price-and-distance) | [[排序](../sort/README.md)] [[数组](../array/README.md)] | Medium | +| 1331 | [数组序号转换](../../problems/rank-transform-of-an-array) | [[数组](../array/README.md)] | Easy | +| 1330 | [翻转子数组得到最大的数组值](../../problems/reverse-subarray-to-maximize-array-value) | [[数组](../array/README.md)] [[数学](../math/README.md)] | Hard | +| 1329 | [将矩阵按对角线排序](../../problems/sort-the-matrix-diagonally) | [[排序](../sort/README.md)] [[数组](../array/README.md)] | Medium | | 1313 | [解压缩编码列表](../../problems/decompress-run-length-encoded-list) | [[数组](../array/README.md)] | Easy | | 1304 | [和为零的N个唯一整数](../../problems/find-n-unique-integers-sum-up-to-zero) | [[数组](../array/README.md)] | Easy | | 1300 | [转变数组后最接近目标值的数组和](../../problems/sum-of-mutated-array-closest-to-target) | [[数组](../array/README.md)] [[二分查找](../binary-search/README.md)] | Medium | diff --git a/tag/binary-search/README.md b/tag/binary-search/README.md index 13f7da3be..f2d86062a 100644 --- a/tag/binary-search/README.md +++ b/tag/binary-search/README.md @@ -9,6 +9,7 @@ | # | 题目 | 标签 | 难度 | | :-: | - | - | :-: | +| 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 | | 1283 | [使结果不超过阈值的最小除数](../../problems/find-the-smallest-divisor-given-a-threshold) | [[二分查找](../binary-search/README.md)] | Medium | diff --git a/tag/dynamic-programming/README.md b/tag/dynamic-programming/README.md index 84453c782..ef7346fa4 100644 --- a/tag/dynamic-programming/README.md +++ b/tag/dynamic-programming/README.md @@ -9,6 +9,10 @@ | # | 题目 | 标签 | 难度 | | :-: | - | - | :-: | +| 1340 | [跳跃游戏 V](../../problems/jump-game-v) | [[动态规划](../dynamic-programming/README.md)] | Hard | +| 1339 | [分裂二叉树的最大乘积](../../problems/maximum-product-of-splitted-binary-tree) | [[树](../tree/README.md)] [[动态规划](../dynamic-programming/README.md)] | Medium | +| 1335 | [工作计划的最低难度](../../problems/minimum-difficulty-of-a-job-schedule) | [[动态规划](../dynamic-programming/README.md)] | Hard | +| 1326 | [灌溉花园的最少水龙头数目](../../problems/minimum-number-of-taps-to-open-to-water-a-garden) | [[贪心算法](../greedy/README.md)] [[动态规划](../dynamic-programming/README.md)] | Hard | | 1320 | [二指输入的的最小距离](../../problems/minimum-distance-to-type-a-word-using-two-fingers) | [[动态规划](../dynamic-programming/README.md)] | Hard | | 1314 | [矩阵区域和](../../problems/matrix-block-sum) | [[动态规划](../dynamic-programming/README.md)] | Medium | | 1312 | [让字符串成为回文串的最少插入次数](../../problems/minimum-insertion-steps-to-make-a-string-palindrome) | [[动态规划](../dynamic-programming/README.md)] | Hard | diff --git a/tag/graph/README.md b/tag/graph/README.md index 29f1f46a0..46ae8c5ba 100644 --- a/tag/graph/README.md +++ b/tag/graph/README.md @@ -9,6 +9,7 @@ | # | 题目 | 标签 | 难度 | | :-: | - | - | :-: | +| 1334 | [阈值距离内邻居最少的城市](../../problems/find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance) | [[图](../graph/README.md)] | Medium | | 1306 | [跳跃游戏 III](../../problems/jump-game-iii) | [[广度优先搜索](../breadth-first-search/README.md)] [[图](../graph/README.md)] | Medium | | 1267 | [统计参与通信的服务器](../../problems/count-servers-that-communicate) | [[图](../graph/README.md)] [[数组](../array/README.md)] | Medium | | 1203 | [项目管理](../../problems/sort-items-by-groups-respecting-dependencies) | [[深度优先搜索](../depth-first-search/README.md)] [[图](../graph/README.md)] [[拓扑排序](../topological-sort/README.md)] | Hard | diff --git a/tag/greedy/README.md b/tag/greedy/README.md index 1874d4171..2e8dc11ce 100644 --- a/tag/greedy/README.md +++ b/tag/greedy/README.md @@ -9,6 +9,8 @@ | # | 题目 | 标签 | 难度 | | :-: | - | - | :-: | +| 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 | | 1282 | [用户分组](../../problems/group-the-people-given-the-group-size-they-belong-to) | [[贪心算法](../greedy/README.md)] | Medium | | 1276 | [不浪费原料的汉堡制作方案](../../problems/number-of-burgers-with-no-waste-of-ingredients) | [[贪心算法](../greedy/README.md)] [[数学](../math/README.md)] | Medium | diff --git a/tag/math/README.md b/tag/math/README.md index 7cc0c2a34..bf444c7fc 100644 --- a/tag/math/README.md +++ b/tag/math/README.md @@ -9,6 +9,8 @@ | # | 题目 | 标签 | 难度 | | :-: | - | - | :-: | +| 1330 | [翻转子数组得到最大的数组值](../../problems/reverse-subarray-to-maximize-array-value) | [[数组](../array/README.md)] [[数学](../math/README.md)] | Hard | +| 1323 | [6 和 9 组成的最大数字](../../problems/maximum-69-number) | [[数学](../math/README.md)] | Easy | | 1317 | [将整数转换为两个无零整数的和](../../problems/convert-integer-to-the-sum-of-two-no-zero-integers) | [[数学](../math/README.md)] | Easy | | 1307 | [口算难题](../../problems/verbal-arithmetic-puzzle) | [[数学](../math/README.md)] [[回溯算法](../backtracking/README.md)] | Hard | | 1281 | [整数的各位积和之差](../../problems/subtract-the-product-and-sum-of-digits-of-an-integer) | [[数学](../math/README.md)] | Easy | diff --git a/tag/sort/README.md b/tag/sort/README.md index 66714adb4..8bd489283 100644 --- a/tag/sort/README.md +++ b/tag/sort/README.md @@ -9,6 +9,8 @@ | # | 题目 | 标签 | 难度 | | :-: | - | - | :-: | +| 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 | | 1244 | [力扣排行榜](../../problems/design-a-leaderboard) 🔒 | [[排序](../sort/README.md)] [[设计](../design/README.md)] [[哈希表](../hash-table/README.md)] | Medium | | 1235 | [规划兼职工作](../../problems/maximum-profit-in-job-scheduling) | [[排序](../sort/README.md)] [[二分查找](../binary-search/README.md)] [[动态规划](../dynamic-programming/README.md)] | Hard | diff --git a/tag/string/README.md b/tag/string/README.md index fb2e0f732..56d5bf36b 100644 --- a/tag/string/README.md +++ b/tag/string/README.md @@ -9,6 +9,9 @@ | # | 题目 | 标签 | 难度 | | :-: | - | - | :-: | +| 1332 | [删除回文子序列](../../problems/remove-palindromic-subsequences) | [[字符串](../string/README.md)] | Easy | +| 1328 | [破坏回文串](../../problems/break-a-palindrome) | [[字符串](../string/README.md)] | Medium | +| 1324 | [竖直打印单词](../../problems/print-words-vertically) | [[字符串](../string/README.md)] | Medium | | 1316 | [不同的循环子字符串](../../problems/distinct-echo-substrings) | [[字符串](../string/README.md)] | Hard | | 1311 | [获取你好友已观看的视频](../../problems/get-watched-videos-by-your-friends) | [[广度优先搜索](../breadth-first-search/README.md)] [[哈希表](../hash-table/README.md)] [[字符串](../string/README.md)] | Medium | | 1309 | [解码字母到整数映射](../../problems/decrypt-string-from-alphabet-to-integer-mapping) | [[字符串](../string/README.md)] | Easy | diff --git a/tag/tree/README.md b/tag/tree/README.md index 8a85b87d4..78d48cd39 100644 --- a/tag/tree/README.md +++ b/tag/tree/README.md @@ -9,6 +9,8 @@ | # | 题目 | 标签 | 难度 | | :-: | - | - | :-: | +| 1339 | [分裂二叉树的最大乘积](../../problems/maximum-product-of-splitted-binary-tree) | [[树](../tree/README.md)] [[动态规划](../dynamic-programming/README.md)] | Medium | +| 1325 | [删除给定值的叶子节点](../../problems/delete-leaves-with-a-given-value) | [[树](../tree/README.md)] | Medium | | 1315 | [祖父节点值为偶数的节点和](../../problems/sum-of-nodes-with-even-valued-grandparent) | [[树](../tree/README.md)] [[深度优先搜索](../depth-first-search/README.md)] | Medium | | 1305 | [两棵二叉搜索树中的所有元素](../../problems/all-elements-in-two-binary-search-trees) | [[排序](../sort/README.md)] [[树](../tree/README.md)] | Medium | | 1302 | [层数最深叶子节点的和](../../problems/deepest-leaves-sum) | [[树](../tree/README.md)] [[深度优先搜索](../depth-first-search/README.md)] | Medium |