From 7af862d7daf848b4cbe0aca37fa1a8ddb918f225 Mon Sep 17 00:00:00 2001 From: Shuo Date: Mon, 6 Apr 2020 16:50:29 +0800 Subject: [PATCH] A: new --- README.md | 11 ++- problems/add-binary/README.md | 9 ++ problems/capital-gainloss/README.md | 2 +- .../README.md | 73 +++++++++++++++ .../construct-k-palindrome-strings/README.md | 86 ++++++++++++++++++ problems/count-largest-group/README.md | 64 ++++++++++++++ .../README.md | 14 +++ .../mysql_schemas.sql | 17 ++++ problems/design-underground-system/README.md | 36 ++++++-- problems/find-all-good-strings/README.md | 2 +- problems/find-in-mountain-array/README.md | 4 +- problems/integer-replacement/README.md | 2 +- problems/longest-happy-string/README.md | 71 +++++++++++++++ .../README.md | 60 +++++++++++++ .../README.md | 80 +++++++++++++++++ problems/reducing-dishes/README.md | 74 ++++++++++++++++ problems/stone-game-iii/README.md | 88 +++++++++++++++++++ tag/README.md | 6 +- tag/array/README.md | 1 + tag/bit-manipulation/README.md | 1 + tag/dynamic-programming/README.md | 3 + tag/geometry/README.md | 1 + tag/greedy/README.md | 3 + tag/sort/README.md | 1 + tag/string/README.md | 1 + tag/tags.json | 10 +-- 26 files changed, 700 insertions(+), 20 deletions(-) create mode 100644 problems/circle-and-rectangle-overlapping/README.md create mode 100644 problems/construct-k-palindrome-strings/README.md create mode 100644 problems/count-largest-group/README.md create mode 100644 problems/customers-who-bought-products-a-and-b-but-not-c/README.md create mode 100644 problems/customers-who-bought-products-a-and-b-but-not-c/mysql_schemas.sql create mode 100644 problems/longest-happy-string/README.md create mode 100644 problems/minimum-subsequence-in-non-increasing-order/README.md create mode 100644 problems/number-of-steps-to-reduce-a-number-in-binary-representation-to-one/README.md create mode 100644 problems/reducing-dishes/README.md create mode 100644 problems/stone-game-iii/README.md diff --git a/README.md b/README.md index 19f651c0e..e863a47f5 100644 --- a/README.md +++ b/README.md @@ -62,11 +62,20 @@ LeetCode Problems' Solutions | # | Title | Solution | Difficulty | | :-: | - | - | :-: | +| 1406 | [Stone Game III](https://leetcode.com/problems/stone-game-iii "石子游戏 III") | [Go](problems/stone-game-iii) | Hard | +| 1405 | [Longest Happy String](https://leetcode.com/problems/longest-happy-string "最长快乐字符串") | [Go](problems/longest-happy-string) | Medium | +| 1404 | [Number of Steps to Reduce a Number in Binary Representation to One](https://leetcode.com/problems/number-of-steps-to-reduce-a-number-in-binary-representation-to-one "将二进制表示减到 1 的步骤数") | [Go](problems/number-of-steps-to-reduce-a-number-in-binary-representation-to-one) | Medium | +| 1403 | [Minimum Subsequence in Non-Increasing Order](https://leetcode.com/problems/minimum-subsequence-in-non-increasing-order "非递增顺序的最小子序列") | [Go](problems/minimum-subsequence-in-non-increasing-order) | Easy | +| 1402 | [Reducing Dishes](https://leetcode.com/problems/reducing-dishes "做菜顺序") | [Go](problems/reducing-dishes) | Hard | +| 1401 | [Circle and Rectangle Overlapping](https://leetcode.com/problems/circle-and-rectangle-overlapping "圆和矩形是否有重叠") | [Go](problems/circle-and-rectangle-overlapping) | Medium | +| 1400 | [Construct K Palindrome Strings](https://leetcode.com/problems/construct-k-palindrome-strings "构造 K 个回文字符串") | [Go](problems/construct-k-palindrome-strings) | Medium | +| 1399 | [Count Largest Group](https://leetcode.com/problems/count-largest-group "统计最大组的数目") | [Go](problems/count-largest-group) | Easy | +| 1398 | [Customers Who Bought Products A and B but Not C](https://leetcode.com/problems/customers-who-bought-products-a-and-b-but-not-c) 🔒 | [MySQL](problems/customers-who-bought-products-a-and-b-but-not-c) | Medium | | 1397 | [Find All Good Strings](https://leetcode.com/problems/find-all-good-strings "找到所有好字符串") | [Go](problems/find-all-good-strings) | Hard | | 1396 | [Design Underground System](https://leetcode.com/problems/design-underground-system "设计地铁系统") | [Go](problems/design-underground-system) | Medium | | 1395 | [Count Number of Teams](https://leetcode.com/problems/count-number-of-teams "统计作战单位数") | [Go](problems/count-number-of-teams) | Medium | | 1394 | [Find Lucky Integer in an Array](https://leetcode.com/problems/find-lucky-integer-in-an-array "找出数组中的幸运数") | [Go](problems/find-lucky-integer-in-an-array) | Easy | -| 1393 | [Capital Gain/Loss](https://leetcode.com/problems/capital-gainloss) 🔒 | [MySQL](problems/capital-gainloss) | Medium | +| 1393 | [Capital Gain/Loss](https://leetcode.com/problems/capital-gainloss "股票的资本损益") 🔒 | [MySQL](problems/capital-gainloss) | Medium | | 1392 | [Longest Happy Prefix](https://leetcode.com/problems/longest-happy-prefix "最长快乐前缀") | [Go](problems/longest-happy-prefix) | Hard | | 1391 | [Check if There is a Valid Path in a Grid](https://leetcode.com/problems/check-if-there-is-a-valid-path-in-a-grid "检查网格中是否存在有效路径") | [Go](problems/check-if-there-is-a-valid-path-in-a-grid) | Medium | | 1390 | [Four Divisors](https://leetcode.com/problems/four-divisors "四因数") | [Go](problems/four-divisors) | Medium | diff --git a/problems/add-binary/README.md b/problems/add-binary/README.md index 98b349191..072f50da0 100644 --- a/problems/add-binary/README.md +++ b/problems/add-binary/README.md @@ -27,6 +27,15 @@ Input: a = "1010", b = "1011" Output: "10101" +

 

+

Constraints:

+ + + ### Related Topics [[Math](../../tag/math/README.md)] [[String](../../tag/string/README.md)] diff --git a/problems/capital-gainloss/README.md b/problems/capital-gainloss/README.md index bffd10d17..cd4960a2b 100644 --- a/problems/capital-gainloss/README.md +++ b/problems/capital-gainloss/README.md @@ -9,6 +9,6 @@                  [Next >](../find-lucky-integer-in-an-array "Find Lucky Integer in an Array") -## [1393. Capital Gain/Loss (Medium)](https://leetcode.com/problems/capital-gainloss "") +## [1393. Capital Gain/Loss (Medium)](https://leetcode.com/problems/capital-gainloss "股票的资本损益") diff --git a/problems/circle-and-rectangle-overlapping/README.md b/problems/circle-and-rectangle-overlapping/README.md new file mode 100644 index 000000000..405cf2a7e --- /dev/null +++ b/problems/circle-and-rectangle-overlapping/README.md @@ -0,0 +1,73 @@ + + + + + + + +[< Previous](../construct-k-palindrome-strings "Construct K Palindrome Strings") +                 +[Next >](../reducing-dishes "Reducing Dishes") + +## [1401. Circle and Rectangle Overlapping (Medium)](https://leetcode.com/problems/circle-and-rectangle-overlapping "圆和矩形是否有重叠") + +

Given a circle represented as (radius, x_center, y_center) and an axis-aligned rectangle represented as (x1, y1, x2, y2), where (x1, y1) are the coordinates of the bottom-left corner, and (x2, y2) are the coordinates of the top-right corner of the rectangle.

+ +

Return True if the circle and rectangle are overlapped otherwise return False.

+ +

In other words, check if there are any point (xi, yi) such that belongs to the circle and the rectangle at the same time.

+ +

 

+

Example 1:

+ +

+ +
+Input: radius = 1, x_center = 0, y_center = 0, x1 = 1, y1 = -1, x2 = 3, y2 = 1
+Output: true
+Explanation: Circle and rectangle share the point (1,0) 
+
+ +

Example 2:

+ +

+ +
+Input: radius = 1, x_center = 0, y_center = 0, x1 = -1, y1 = 0, x2 = 0, y2 = 1
+Output: true
+
+ +

Example 3:

+ +

+ +
+Input: radius = 1, x_center = 1, y_center = 1, x1 = -3, y1 = -3, x2 = 3, y2 = 3
+Output: true
+
+ +

Example 4:

+ +
+Input: radius = 1, x_center = 1, y_center = 1, x1 = 1, y1 = -3, x2 = 2, y2 = -1
+Output: false
+
+ +

 

+

Constraints:

+ + + +### Related Topics + [[Geometry](../../tag/geometry/README.md)] + +### Hints +
+Hint 1 +Locate the closest point of the square to the circle, you can then find the distance from this point to the center of the circle and check if this is less than or equal to the radius. +
diff --git a/problems/construct-k-palindrome-strings/README.md b/problems/construct-k-palindrome-strings/README.md new file mode 100644 index 000000000..da1e88f7c --- /dev/null +++ b/problems/construct-k-palindrome-strings/README.md @@ -0,0 +1,86 @@ + + + + + + + +[< Previous](../count-largest-group "Count Largest Group") +                 +[Next >](../circle-and-rectangle-overlapping "Circle and Rectangle Overlapping") + +## [1400. Construct K Palindrome Strings (Medium)](https://leetcode.com/problems/construct-k-palindrome-strings "构造 K 个回文字符串") + +

Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s.

+ +

Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.

+ +

 

+

Example 1:

+ +
+Input: s = "annabelle", k = 2
+Output: true
+Explanation: You can construct two palindromes using all characters in s.
+Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b"
+
+ +

Example 2:

+ +
+Input: s = "leetcode", k = 3
+Output: false
+Explanation: It is impossible to construct 3 palindromes using all the characters of s.
+
+ +

Example 3:

+ +
+Input: s = "true", k = 4
+Output: true
+Explanation: The only possible solution is to put each character in a separate string.
+
+ +

Example 4:

+ +
+Input: s = "yzyzyzyzyzyzyzy", k = 2
+Output: true
+Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome.
+
+ +

Example 5:

+ +
+Input: s = "cr", k = 7
+Output: false
+Explanation: We don't have enough characters in s to construct 7 palindromes.
+
+ +

 

+

Constraints:

+ + + +### Related Topics + [[Greedy](../../tag/greedy/README.md)] + +### Hints +
+Hint 1 +If the s.length < k we cannot construct k strings from s and answer is false. +
+ +
+Hint 2 +If the number of characters that have odd counts is > k then the minimum number of palindrome strings we can construct is > k and answer is false. +
+ +
+Hint 3 +Otherwise you can construct exactly k palindrome strings and answer is true (why ?). +
diff --git a/problems/count-largest-group/README.md b/problems/count-largest-group/README.md new file mode 100644 index 000000000..9035e595d --- /dev/null +++ b/problems/count-largest-group/README.md @@ -0,0 +1,64 @@ + + + + + + + +[< Previous](../customers-who-bought-products-a-and-b-but-not-c "Customers Who Bought Products A and B but Not C") +                 +[Next >](../construct-k-palindrome-strings "Construct K Palindrome Strings") + +## [1399. Count Largest Group (Easy)](https://leetcode.com/problems/count-largest-group "统计最大组的数目") + +

Given an integer n. Each number from 1 to n is grouped according to the sum of its digits. 

+ +

Return how many groups have the largest size.

+ +

 

+

Example 1:

+ +
+Input: n = 13
+Output: 4
+Explanation: There are 9 groups in total, they are grouped according sum of its digits of numbers from 1 to 13:
+[1,10], [2,11], [3,12], [4,13], [5], [6], [7], [8], [9]. There are 4 groups with largest size.
+
+ +

Example 2:

+ +
+Input: n = 2
+Output: 2
+Explanation: There are 2 groups [1], [2] of size 1.
+
+ +

Example 3:

+ +
+Input: n = 15
+Output: 6
+
+ +

Example 4:

+ +
+Input: n = 24
+Output: 5
+
+ +

 

+

Constraints:

+ + + +### Related Topics + [[Array](../../tag/array/README.md)] + +### Hints +
+Hint 1 +Count the digit sum for each integer in the range and find out the largest groups. +
diff --git a/problems/customers-who-bought-products-a-and-b-but-not-c/README.md b/problems/customers-who-bought-products-a-and-b-but-not-c/README.md new file mode 100644 index 000000000..e434966cd --- /dev/null +++ b/problems/customers-who-bought-products-a-and-b-but-not-c/README.md @@ -0,0 +1,14 @@ + + + + + + + +[< Previous](../find-all-good-strings "Find All Good Strings") +                 +[Next >](../count-largest-group "Count Largest Group") + +## [1398. Customers Who Bought Products A and B but Not C (Medium)](https://leetcode.com/problems/customers-who-bought-products-a-and-b-but-not-c "") + + diff --git a/problems/customers-who-bought-products-a-and-b-but-not-c/mysql_schemas.sql b/problems/customers-who-bought-products-a-and-b-but-not-c/mysql_schemas.sql new file mode 100644 index 000000000..a69fad8cb --- /dev/null +++ b/problems/customers-who-bought-products-a-and-b-but-not-c/mysql_schemas.sql @@ -0,0 +1,17 @@ +Create table If Not Exists Customers (customer_id int, customer_name varchar(30)); +Create table If Not Exists Orders (order_id int, customer_id int, product_name varchar(30)); +Truncate table Customers; +insert into Customers (customer_id, customer_name) values ('1', 'Daniel'); +insert into Customers (customer_id, customer_name) values ('2', 'Diana'); +insert into Customers (customer_id, customer_name) values ('3', 'Elizabeth'); +insert into Customers (customer_id, customer_name) values ('4', 'Jhon'); +Truncate table Orders; +insert into Orders (order_id, customer_id, product_name) values ('10', '1', 'A'); +insert into Orders (order_id, customer_id, product_name) values ('20', '1', 'B'); +insert into Orders (order_id, customer_id, product_name) values ('30', '1', 'D'); +insert into Orders (order_id, customer_id, product_name) values ('40', '1', 'C'); +insert into Orders (order_id, customer_id, product_name) values ('50', '2', 'A'); +insert into Orders (order_id, customer_id, product_name) values ('60', '3', 'A'); +insert into Orders (order_id, customer_id, product_name) values ('70', '3', 'B'); +insert into Orders (order_id, customer_id, product_name) values ('80', '3', 'D'); +insert into Orders (order_id, customer_id, product_name) values ('90', '4', 'C'); diff --git a/problems/design-underground-system/README.md b/problems/design-underground-system/README.md index e95e2a2d0..f834f1cad 100644 --- a/problems/design-underground-system/README.md +++ b/problems/design-underground-system/README.md @@ -38,14 +38,14 @@

 

Example 1:

+Input
-Input
 ["UndergroundSystem","checkIn","checkIn","checkIn","checkOut","checkOut","checkOut","getAverageTime","getAverageTime","checkIn","getAverageTime","checkOut","getAverageTime"]
 [[],[45,"Leyton",3],[32,"Paradise",8],[27,"Leyton",10],[45,"Waterloo",15],[27,"Waterloo",20],[32,"Cambridge",22],["Paradise","Cambridge"],["Leyton","Waterloo"],[10,"Leyton",24],["Leyton","Waterloo"],[10,"Waterloo",38],["Leyton","Waterloo"]]
 
 Output
-[null,null,null,null,null,null,null,14.0,11.0,null,11.0,null,12.0]
+[null,null,null,null,null,null,null,14.00000,11.00000,null,11.00000,null,12.00000]
 
 Explanation
 UndergroundSystem undergroundSystem = new UndergroundSystem();
@@ -55,12 +55,36 @@ undergroundSystem.checkIn(27, "Leyton", 10);
 undergroundSystem.checkOut(45, "Waterloo", 15);
 undergroundSystem.checkOut(27, "Waterloo", 20);
 undergroundSystem.checkOut(32, "Cambridge", 22);
-undergroundSystem.getAverageTime("Paradise", "Cambridge");       // return 14.0. There was only one travel from "Paradise" (at time 8) to "Cambridge" (at time 22)
-undergroundSystem.getAverageTime("Leyton", "Waterloo");          // return 11.0. There were two travels from "Leyton" to "Waterloo", a customer with id=45 from time=3 to time=15 and a customer with id=27 from time=10 to time=20. So the average time is ( (15-3) + (20-10) ) / 2 = 11.0
+undergroundSystem.getAverageTime("Paradise", "Cambridge");       // return 14.00000. There was only one travel from "Paradise" (at time 8) to "Cambridge" (at time 22)
+undergroundSystem.getAverageTime("Leyton", "Waterloo");          // return 11.00000. There were two travels from "Leyton" to "Waterloo", a customer with id=45 from time=3 to time=15 and a customer with id=27 from time=10 to time=20. So the average time is ( (15-3) + (20-10) ) / 2 = 11.00000
 undergroundSystem.checkIn(10, "Leyton", 24);
-undergroundSystem.getAverageTime("Leyton", "Waterloo");          // return 11.0
+undergroundSystem.getAverageTime("Leyton", "Waterloo");          // return 11.00000
 undergroundSystem.checkOut(10, "Waterloo", 38);
-undergroundSystem.getAverageTime("Leyton", "Waterloo");          // return 12.0
+undergroundSystem.getAverageTime("Leyton", "Waterloo");          // return 12.00000 + + +

Example 2:

+Input + +
+["UndergroundSystem","checkIn","checkOut","getAverageTime","checkIn","checkOut","getAverageTime","checkIn","checkOut","getAverageTime"]
+[[],[10,"Leyton",3],[10,"Paradise",8],["Leyton","Paradise"],[5,"Leyton",10],[5,"Paradise",16],["Leyton","Paradise"],[2,"Leyton",21],[2,"Paradise",30],["Leyton","Paradise"]]
+
+Output
+[null,null,null,5.00000,null,null,5.50000,null,null,6.66667]
+
+Explanation
+UndergroundSystem undergroundSystem = new UndergroundSystem();
+undergroundSystem.checkIn(10, "Leyton", 3);
+undergroundSystem.checkOut(10, "Paradise", 8);
+undergroundSystem.getAverageTime("Leyton", "Paradise"); // return 5.00000
+undergroundSystem.checkIn(5, "Leyton", 10);
+undergroundSystem.checkOut(5, "Paradise", 16);
+undergroundSystem.getAverageTime("Leyton", "Paradise"); // return 5.50000
+undergroundSystem.checkIn(2, "Leyton", 21);
+undergroundSystem.checkOut(2, "Paradise", 30);
+undergroundSystem.getAverageTime("Leyton", "Paradise"); // return 6.66667
+

 

Constraints:

diff --git a/problems/find-all-good-strings/README.md b/problems/find-all-good-strings/README.md index a81a4cfc5..d1f1b7e4b 100644 --- a/problems/find-all-good-strings/README.md +++ b/problems/find-all-good-strings/README.md @@ -7,7 +7,7 @@ [< Previous](../design-underground-system "Design Underground System")                  -Next > +[Next >](../customers-who-bought-products-a-and-b-but-not-c "Customers Who Bought Products A and B but Not C") ## [1397. Find All Good Strings (Hard)](https://leetcode.com/problems/find-all-good-strings "找到所有好字符串") diff --git a/problems/find-in-mountain-array/README.md b/problems/find-in-mountain-array/README.md index beb2a9ff2..9d531ae4d 100644 --- a/problems/find-in-mountain-array/README.md +++ b/problems/find-in-mountain-array/README.md @@ -58,11 +58,11 @@

 

Constraints:

-
    +
+ ### Related Topics [[Binary Search](../../tag/binary-search/README.md)] diff --git a/problems/integer-replacement/README.md b/problems/integer-replacement/README.md index db19d0243..a34ff2c94 100644 --- a/problems/integer-replacement/README.md +++ b/problems/integer-replacement/README.md @@ -57,5 +57,5 @@ or

### Related Topics - [[Math](../../tag/math/README.md)] [[Bit Manipulation](../../tag/bit-manipulation/README.md)] + [[Math](../../tag/math/README.md)] diff --git a/problems/longest-happy-string/README.md b/problems/longest-happy-string/README.md new file mode 100644 index 000000000..6d94a5f68 --- /dev/null +++ b/problems/longest-happy-string/README.md @@ -0,0 +1,71 @@ + + + + + + + +[< Previous](../number-of-steps-to-reduce-a-number-in-binary-representation-to-one "Number of Steps to Reduce a Number in Binary Representation to One") +                 +[Next >](../stone-game-iii "Stone Game III") + +## [1405. Longest Happy String (Medium)](https://leetcode.com/problems/longest-happy-string "最长快乐字符串") + +

A string is called happy if it does not have any of the strings 'aaa', 'bbb' or 'ccc' as a substring.

+ +

Given three integers a, b and c, return any string s, which satisfies following conditions:

+ + + +

If there is no such string s return the empty string "".

+ +

 

+

Example 1:

+ +
+Input: a = 1, b = 1, c = 7
+Output: "ccaccbcc"
+Explanation: "ccbccacc" would also be a correct answer.
+
+ +

Example 2:

+ +
+Input: a = 2, b = 2, c = 1
+Output: "aabbc"
+
+ +

Example 3:

+ +
+Input: a = 7, b = 1, c = 0
+Output: "aabaa"
+Explanation: It's the only correct answer in this case.
+
+ +

 

+

Constraints:

+ + + +### Related Topics + [[Greedy](../../tag/greedy/README.md)] + [[Dynamic Programming](../../tag/dynamic-programming/README.md)] + +### Hints +
+Hint 1 +Use a greedy approach. +
+ +
+Hint 2 +Use the letter with the maximum current limit that can be added without breaking the condition. +
diff --git a/problems/minimum-subsequence-in-non-increasing-order/README.md b/problems/minimum-subsequence-in-non-increasing-order/README.md new file mode 100644 index 000000000..93bfcbbba --- /dev/null +++ b/problems/minimum-subsequence-in-non-increasing-order/README.md @@ -0,0 +1,60 @@ + + + + + + + +[< Previous](../reducing-dishes "Reducing Dishes") +                 +[Next >](../number-of-steps-to-reduce-a-number-in-binary-representation-to-one "Number of Steps to Reduce a Number in Binary Representation to One") + +## [1403. Minimum Subsequence in Non-Increasing Order (Easy)](https://leetcode.com/problems/minimum-subsequence-in-non-increasing-order "非递增顺序的最小子序列") + +

Given the array nums, obtain a subsequence of the array whose sum of elements is strictly greater than the sum of the non included elements in such subsequence. 

+ +

If there are multiple solutions, return the subsequence with minimum size and if there still exist multiple solutions, return the subsequence with the maximum total sum of all its elements. A subsequence of an array can be obtained by erasing some (possibly zero) elements from the array. 

+ +

Note that the solution with the given constraints is guaranteed to be unique. Also return the answer sorted in non-increasing order.

+ +

 

+

Example 1:

+ +
+Input: nums = [4,3,10,9,8]
+Output: [10,9] 
+Explanation: The subsequences [10,9] and [10,8] are minimal such that the sum of their elements is strictly greater than the sum of elements not included, however, the subsequence [10,9] has the maximum total sum of its elements. 
+
+ +

Example 2:

+ +
+Input: nums = [4,4,7,6,7]
+Output: [7,7,6] 
+Explanation: The subsequence [7,7] has the sum of its elements equal to 14 which is not strictly greater than the sum of elements not included (14 = 4 + 4 + 6). Therefore, the subsequence [7,6,7] is the minimal satisfying the conditions. Note the subsequence has to returned in non-decreasing order.  
+
+ +

Example 3:

+ +
+Input: nums = [6]
+Output: [6]
+
+ +

 

+

Constraints:

+ + + +### Related Topics + [[Greedy](../../tag/greedy/README.md)] + [[Sort](../../tag/sort/README.md)] + +### Hints +
+Hint 1 +Sort elements and take each element from the largest until accomplish the conditions. +
diff --git a/problems/number-of-steps-to-reduce-a-number-in-binary-representation-to-one/README.md b/problems/number-of-steps-to-reduce-a-number-in-binary-representation-to-one/README.md new file mode 100644 index 000000000..287f5d23f --- /dev/null +++ b/problems/number-of-steps-to-reduce-a-number-in-binary-representation-to-one/README.md @@ -0,0 +1,80 @@ + + + + + + + +[< Previous](../minimum-subsequence-in-non-increasing-order "Minimum Subsequence in Non-Increasing Order") +                 +[Next >](../longest-happy-string "Longest Happy String") + +## [1404. Number of Steps to Reduce a Number in Binary Representation to One (Medium)](https://leetcode.com/problems/number-of-steps-to-reduce-a-number-in-binary-representation-to-one "将二进制表示减到 1 的步骤数") + +

Given a number s in their binary representation. Return the number of steps to reduce it to 1 under the following rules:

+ + + +

It's guaranteed that you can always reach to one for all testcases.

+ +

 

+

Example 1:

+ +
+Input: s = "1101"
+Output: 6
+Explanation: "1101" corressponds to number 13 in their decimal representation.
+Step 1) 13 is odd, add 1 and obtain 14. 
+Step 2) 14 is even, divide by 2 and obtain 7.
+Step 3) 7 is odd, add 1 and obtain 8.
+Step 4) 8 is even, divide by 2 and obtain 4.  
+Step 5) 4 is even, divide by 2 and obtain 2. 
+Step 6) 2 is even, divide by 2 and obtain 1.  
+
+ +

Example 2:

+ +
+Input: s = "10"
+Output: 1
+Explanation: "10" corressponds to number 2 in their decimal representation.
+Step 1) 2 is even, divide by 2 and obtain 1.  
+
+ +

Example 3:

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

 

+

Constraints:

+ + + +### Related Topics + [[Bit Manipulation](../../tag/bit-manipulation/README.md)] + [[String](../../tag/string/README.md)] + +### Hints +
+Hint 1 +Read the string from right to left, if the string ends in '0' then the number is even otherwise it is odd. +
+ +
+Hint 2 +Simulate the steps described in the binary string. +
diff --git a/problems/reducing-dishes/README.md b/problems/reducing-dishes/README.md new file mode 100644 index 000000000..318b14ce1 --- /dev/null +++ b/problems/reducing-dishes/README.md @@ -0,0 +1,74 @@ + + + + + + + +[< Previous](../circle-and-rectangle-overlapping "Circle and Rectangle Overlapping") +                 +[Next >](../minimum-subsequence-in-non-increasing-order "Minimum Subsequence in Non-Increasing Order") + +## [1402. Reducing Dishes (Hard)](https://leetcode.com/problems/reducing-dishes "做菜顺序") + +

A chef has collected data on the satisfaction level of his n dishes. Chef can cook any dish in 1 unit of time.

+ +

Like-time coefficient of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level  i.e.  time[i]*satisfaction[i]

+ +

Return the maximum sum of Like-time coefficient that the chef can obtain after dishes preparation.

+ +

Dishes can be prepared in any order and the chef can discard some dishes to get this maximum value.

+ +

 

+

Example 1:

+ +
+Input: satisfaction = [-1,-8,0,5,-9]
+Output: 14
+Explanation: After Removing the second and last dish, the maximum total Like-time coefficient will be equal to (-1*1 + 0*2 + 5*3 = 14). Each dish is prepared in one unit of time.
+ +

Example 2:

+ +
+Input: satisfaction = [4,3,2]
+Output: 20
+Explanation: Dishes can be prepared in any order, (2*1 + 3*2 + 4*3 = 20)
+
+ +

Example 3:

+ +
+Input: satisfaction = [-1,-4,-5]
+Output: 0
+Explanation: People don't like the dishes. No dish is prepared.
+
+ +

Example 4:

+ +
+Input: satisfaction = [-2,5,-1,0,3,-3]
+Output: 35
+
+ +

 

+

Constraints:

+ + + +### Related Topics + [[Dynamic Programming](../../tag/dynamic-programming/README.md)] + +### Hints +
+Hint 1 +Use dynamic programming to find the optimal solution by saving the previous best like-time coefficient and its corresponding element sum. +
+ +
+Hint 2 +If adding the current element to the previous best like-time coefficient and its corresponding element sum would increase the best like-time coefficient, then go ahead and add it. Otherwise, keep the previous best like-time coefficient. +
diff --git a/problems/stone-game-iii/README.md b/problems/stone-game-iii/README.md new file mode 100644 index 000000000..fcb7afd2a --- /dev/null +++ b/problems/stone-game-iii/README.md @@ -0,0 +1,88 @@ + + + + + + + +[< Previous](../longest-happy-string "Longest Happy String") +                 +Next > + +## [1406. Stone Game III (Hard)](https://leetcode.com/problems/stone-game-iii "石子游戏 III") + +

Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.

+ +

Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.

+ +

The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.

+ +

The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.

+ +

Assume Alice and Bob play optimally.

+ +

Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.

+ +

 

+

Example 1:

+ +
+Input: values = [1,2,3,7]
+Output: "Bob"
+Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
+
+ +

Example 2:

+ +
+Input: values = [1,2,3,-9]
+Output: "Alice"
+Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
+If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
+If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
+Remember that both play optimally so here Alice will choose the scenario that makes her win.
+
+ +

Example 3:

+ +
+Input: values = [1,2,3,6]
+Output: "Tie"
+Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
+
+ +

Example 4:

+ +
+Input: values = [1,2,3,-1,-2,-3,7]
+Output: "Alice"
+
+ +

Example 5:

+ +
+Input: values = [-1,-2,-3]
+Output: "Tie"
+
+ +

 

+

Constraints:

+ + + +### Related Topics + [[Dynamic Programming](../../tag/dynamic-programming/README.md)] + +### Hints +
+Hint 1 +The game can be mapped to minmax game. Alex tries to maximize the total score and Lee tries to minimize it. +
+ +
+Hint 2 +Use dynamic programming to simulate the game. If the total score was 0 the game is "Tie", and if it has positive value then "Alex" wins, otherwise "Lee" wins. +
diff --git a/tag/README.md b/tag/README.md index 61fdd8818..854b8235f 100644 --- a/tag/README.md +++ b/tag/README.md @@ -23,9 +23,9 @@ | 23 | [Trie](trie/README.md) | [字典树](https://openset.github.io/tags/trie/) | | 24 | [Recursion](recursion/README.md) | [递归](https://openset.github.io/tags/recursion/) | | 25 | [Segment Tree](segment-tree/README.md) | [线段树](https://openset.github.io/tags/segment-tree/) | | 26 | [Ordered Map](ordered-map/README.md) | [Ordered Map](https://openset.github.io/tags/ordered-map/) | | 27 | [Queue](queue/README.md) | [队列](https://openset.github.io/tags/queue/) | | 28 | [Minimax](minimax/README.md) | [极小化极大](https://openset.github.io/tags/minimax/) | -| 29 | [Binary Indexed Tree](binary-indexed-tree/README.md) | [树状数组](https://openset.github.io/tags/binary-indexed-tree/) | | 30 | [Line Sweep](line-sweep/README.md) | [Line Sweep](https://openset.github.io/tags/line-sweep/) | -| 31 | [Random](random/README.md) | [Random](https://openset.github.io/tags/random/) | | 32 | [Topological Sort](topological-sort/README.md) | [拓扑排序](https://openset.github.io/tags/topological-sort/) | -| 33 | [Brainteaser](brainteaser/README.md) | [脑筋急转弯](https://openset.github.io/tags/brainteaser/) | | 34 | [Geometry](geometry/README.md) | [几何](https://openset.github.io/tags/geometry/) | +| 29 | [Binary Indexed Tree](binary-indexed-tree/README.md) | [树状数组](https://openset.github.io/tags/binary-indexed-tree/) | | 30 | [Geometry](geometry/README.md) | [几何](https://openset.github.io/tags/geometry/) | +| 31 | [Line Sweep](line-sweep/README.md) | [Line Sweep](https://openset.github.io/tags/line-sweep/) | | 32 | [Random](random/README.md) | [Random](https://openset.github.io/tags/random/) | +| 33 | [Topological Sort](topological-sort/README.md) | [拓扑排序](https://openset.github.io/tags/topological-sort/) | | 34 | [Brainteaser](brainteaser/README.md) | [脑筋急转弯](https://openset.github.io/tags/brainteaser/) | | 35 | [Binary Search Tree](binary-search-tree/README.md) | [二叉搜索树](https://openset.github.io/tags/binary-search-tree/) | | 36 | [Rejection Sampling](rejection-sampling/README.md) | [Rejection Sampling](https://openset.github.io/tags/rejection-sampling/) | | 37 | [Reservoir Sampling](reservoir-sampling/README.md) | [蓄水池抽样](https://openset.github.io/tags/reservoir-sampling/) | | 38 | [Rolling Hash](rolling-hash/README.md) | [Rolling Hash](https://openset.github.io/tags/rolling-hash/) | | 39 | [Memoization](memoization/README.md) | [记忆化](https://openset.github.io/tags/memoization/) | | 40 | [Suffix Array](suffix-array/README.md) | [Suffix Array](https://openset.github.io/tags/suffix-array/) | diff --git a/tag/array/README.md b/tag/array/README.md index 5023dc46d..6a3ad09e4 100644 --- a/tag/array/README.md +++ b/tag/array/README.md @@ -9,6 +9,7 @@ | # | 题目 | 标签 | 难度 | | :-: | - | - | :-: | +| 1399 | [统计最大组的数目](../../problems/count-largest-group) | [[数组](../array/README.md)] | Easy | | 1395 | [统计作战单位数](../../problems/count-number-of-teams) | [[数组](../array/README.md)] | Medium | | 1394 | [找出数组中的幸运数](../../problems/find-lucky-integer-in-an-array) | [[数组](../array/README.md)] | Easy | | 1389 | [按既定顺序创建目标数组](../../problems/create-target-array-in-the-given-order) | [[数组](../array/README.md)] | Easy | diff --git a/tag/bit-manipulation/README.md b/tag/bit-manipulation/README.md index 1c8caef90..666decd3d 100644 --- a/tag/bit-manipulation/README.md +++ b/tag/bit-manipulation/README.md @@ -9,6 +9,7 @@ | # | 题目 | 标签 | 难度 | | :-: | - | - | :-: | +| 1404 | [将二进制表示减到 1 的步骤数](../../problems/number-of-steps-to-reduce-a-number-in-binary-representation-to-one) | [[位运算](../bit-manipulation/README.md)] [[字符串](../string/README.md)] | Medium | | 1356 | [根据数字二进制下 1 的数目排序](../../problems/sort-integers-by-the-number-of-1-bits) | [[排序](../sort/README.md)] [[位运算](../bit-manipulation/README.md)] | Easy | | 1342 | [将数字变成 0 的操作次数](../../problems/number-of-steps-to-reduce-a-number-to-zero) | [[位运算](../bit-manipulation/README.md)] | Easy | | 1318 | [或运算的最小翻转次数](../../problems/minimum-flips-to-make-a-or-b-equal-to-c) | [[位运算](../bit-manipulation/README.md)] | Medium | diff --git a/tag/dynamic-programming/README.md b/tag/dynamic-programming/README.md index 5827ea901..4ea035f8e 100644 --- a/tag/dynamic-programming/README.md +++ b/tag/dynamic-programming/README.md @@ -9,6 +9,9 @@ | # | 题目 | 标签 | 难度 | | :-: | - | - | :-: | +| 1406 | [石子游戏 III](../../problems/stone-game-iii) | [[动态规划](../dynamic-programming/README.md)] | Hard | +| 1405 | [最长快乐字符串](../../problems/longest-happy-string) | [[贪心算法](../greedy/README.md)] [[动态规划](../dynamic-programming/README.md)] | Medium | +| 1402 | [做菜顺序](../../problems/reducing-dishes) | [[动态规划](../dynamic-programming/README.md)] | Hard | | 1397 | [找到所有好字符串](../../problems/find-all-good-strings) | [[动态规划](../dynamic-programming/README.md)] | Hard | | 1388 | [3n 块披萨](../../problems/pizza-with-3n-slices) | [[动态规划](../dynamic-programming/README.md)] | Hard | | 1373 | [二叉搜索子树的最大键值和](../../problems/maximum-sum-bst-in-binary-tree) | [[二叉搜索树](../binary-search-tree/README.md)] [[动态规划](../dynamic-programming/README.md)] | Hard | diff --git a/tag/geometry/README.md b/tag/geometry/README.md index 444a2be36..1a0038d3e 100644 --- a/tag/geometry/README.md +++ b/tag/geometry/README.md @@ -9,6 +9,7 @@ | # | 题目 | 标签 | 难度 | | :-: | - | - | :-: | +| 1401 | [圆和矩形是否有重叠](../../problems/circle-and-rectangle-overlapping) | [[几何](../geometry/README.md)] | Medium | | 1266 | [访问所有点的最小时间](../../problems/minimum-time-visiting-all-points) | [[几何](../geometry/README.md)] [[数组](../array/README.md)] | Easy | | 1232 | [缀点成线](../../problems/check-if-it-is-a-straight-line) | [[几何](../geometry/README.md)] [[数组](../array/README.md)] [[数学](../math/README.md)] | Easy | | 963 | [最小面积矩形 II](../../problems/minimum-area-rectangle-ii) | [[几何](../geometry/README.md)] [[数学](../math/README.md)] | Medium | diff --git a/tag/greedy/README.md b/tag/greedy/README.md index 9f10119ea..4e436672e 100644 --- a/tag/greedy/README.md +++ b/tag/greedy/README.md @@ -9,6 +9,9 @@ | # | 题目 | 标签 | 难度 | | :-: | - | - | :-: | +| 1405 | [最长快乐字符串](../../problems/longest-happy-string) | [[贪心算法](../greedy/README.md)] [[动态规划](../dynamic-programming/README.md)] | Medium | +| 1403 | [非递增顺序的最小子序列](../../problems/minimum-subsequence-in-non-increasing-order) | [[贪心算法](../greedy/README.md)] [[排序](../sort/README.md)] | Easy | +| 1400 | [构造 K 个回文字符串](../../problems/construct-k-palindrome-strings) | [[贪心算法](../greedy/README.md)] | Medium | | 1386 | [安排电影院座位](../../problems/cinema-seat-allocation) | [[贪心算法](../greedy/README.md)] [[数组](../array/README.md)] | Medium | | 1383 | [最大的团队表现值](../../problems/maximum-performance-of-a-team) | [[贪心算法](../greedy/README.md)] [[排序](../sort/README.md)] | Hard | | 1354 | [多次求和构造目标数组](../../problems/construct-target-array-with-multiple-sums) | [[贪心算法](../greedy/README.md)] | Hard | diff --git a/tag/sort/README.md b/tag/sort/README.md index 02d67afd8..5b346163f 100644 --- a/tag/sort/README.md +++ b/tag/sort/README.md @@ -9,6 +9,7 @@ | # | 题目 | 标签 | 难度 | | :-: | - | - | :-: | +| 1403 | [非递增顺序的最小子序列](../../problems/minimum-subsequence-in-non-increasing-order) | [[贪心算法](../greedy/README.md)] [[排序](../sort/README.md)] | Easy | | 1387 | [将整数按权重排序](../../problems/sort-integers-by-the-power-value) | [[排序](../sort/README.md)] [[图](../graph/README.md)] | Medium | | 1383 | [最大的团队表现值](../../problems/maximum-performance-of-a-team) | [[贪心算法](../greedy/README.md)] [[排序](../sort/README.md)] | Hard | | 1370 | [上升下降字符串](../../problems/increasing-decreasing-string) | [[排序](../sort/README.md)] [[字符串](../string/README.md)] | Easy | diff --git a/tag/string/README.md b/tag/string/README.md index 200174778..c03bb0b56 100644 --- a/tag/string/README.md +++ b/tag/string/README.md @@ -9,6 +9,7 @@ | # | 题目 | 标签 | 难度 | | :-: | - | - | :-: | +| 1404 | [将二进制表示减到 1 的步骤数](../../problems/number-of-steps-to-reduce-a-number-in-binary-representation-to-one) | [[位运算](../bit-manipulation/README.md)] [[字符串](../string/README.md)] | Medium | | 1392 | [最长快乐前缀](../../problems/longest-happy-prefix) | [[字符串](../string/README.md)] | Hard | | 1374 | [生成每种字符都是奇数个的字符串](../../problems/generate-a-string-with-characters-that-have-odd-counts) | [[字符串](../string/README.md)] | Easy | | 1371 | [每个元音包含偶数次的最长子字符串](../../problems/find-the-longest-substring-containing-vowels-in-even-counts) | [[字符串](../string/README.md)] | Medium | diff --git a/tag/tags.json b/tag/tags.json index a6f7edc10..1f0b44a86 100644 --- a/tag/tags.json +++ b/tag/tags.json @@ -144,6 +144,11 @@ "Slug": "binary-indexed-tree", "TranslatedName": "树状数组" }, + { + "Name": "Geometry", + "Slug": "geometry", + "TranslatedName": "几何" + }, { "Name": "Line Sweep", "Slug": "line-sweep", @@ -164,11 +169,6 @@ "Slug": "brainteaser", "TranslatedName": "脑筋急转弯" }, - { - "Name": "Geometry", - "Slug": "geometry", - "TranslatedName": "几何" - }, { "Name": "Binary Search Tree", "Slug": "binary-search-tree",