From ddfb07378b0c288861b5d243fc4289e156892644 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 25 Mar 2025 10:02:48 +0000 Subject: [PATCH] feat: update lc problems --- package-lock.json | 3 +- package.json | 2 +- .../0100-0199/0195.Tenth Line/README_EN.md | 15 -- .../0799.Champagne Tower/README_EN.md | 20 +- .../1108.Defanging an IP Address/README_EN.md | 13 +- .../README_EN.md | 20 +- .../1138.Alphabet Board Path/README_EN.md | 32 +-- .../README_EN.md | 18 +- .../README_EN.md | 26 +-- .../README_EN.md | 28 +-- .../1200-1299/1256.Encode Number/README_EN.md | 12 +- .../README_EN.md | 16 +- .../README_EN.md | 22 +- .../1324.Print Words Vertically/README_EN.md | 31 +-- .../README_EN.md | 39 +--- .../README_EN.md | 22 +- .../1470.Shuffle the Array/README_EN.md | 22 +- .../README_EN.md | 20 +- .../README_EN.md | 12 +- .../1534.Count Good Triplets/README_EN.md | 33 +-- .../README_EN.md | 31 +-- .../README_EN.md | 49 +---- .../README_EN.md | 40 +--- .../README_EN.md | 49 +---- .../1660.Correct a Binary Tree/README_EN.md | 42 +--- .../README_EN.md | 23 +- .../README_EN.md | 15 +- .../README_EN.md | 30 +-- .../README_EN.md | 30 +-- .../README_EN.md | 51 +---- .../README_EN.md | 33 +-- .../README_EN.md | 18 +- .../README_EN.md | 26 +-- .../README_EN.md | 27 +-- .../README_EN.md | 29 +-- .../1872.Stone Game VIII/README_EN.md | 45 +--- .../README_EN.md | 24 +-- .../README_EN.md | 36 +--- .../README_EN.md | 26 +-- .../README_EN.md | 23 +- .../README_EN.md | 34 +-- .../README_EN.md | 40 +--- .../README_EN.md | 36 +--- .../README.md | 3 +- .../README_EN.md | 3 +- .../README.md | 1 + .../README_EN.md | 1 + .../README.md | 201 ++++++++++++++++++ .../README_EN.md | 199 +++++++++++++++++ .../Solution.cpp | 20 ++ .../Solution.go | 16 ++ .../Solution.java | 19 ++ .../Solution.py | 6 + .../Solution.ts | 13 ++ solution/README.md | 5 +- solution/README_EN.md | 5 +- 56 files changed, 655 insertions(+), 1000 deletions(-) create mode 100644 solution/3400-3499/3496.Maximize Score After Pair Deletions/README.md create mode 100644 solution/3400-3499/3496.Maximize Score After Pair Deletions/README_EN.md create mode 100644 solution/3400-3499/3496.Maximize Score After Pair Deletions/Solution.cpp create mode 100644 solution/3400-3499/3496.Maximize Score After Pair Deletions/Solution.go create mode 100644 solution/3400-3499/3496.Maximize Score After Pair Deletions/Solution.java create mode 100644 solution/3400-3499/3496.Maximize Score After Pair Deletions/Solution.py create mode 100644 solution/3400-3499/3496.Maximize Score After Pair Deletions/Solution.ts diff --git a/package-lock.json b/package-lock.json index 93e256af88f15..734a405a7a3b2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,7 +8,7 @@ "@commitlint/cli": "^19.3.0", "@commitlint/config-conventional": "^19.2.2", "@prettier/plugin-php": "^0.22.2", - "clang-format": "1.8.0", + "clang-format": "^1.8.0", "husky": "^9.0.1", "lint-staged": "^15.2.7", "prettier": "^3.3.2", @@ -474,6 +474,7 @@ "resolved": "https://registry.npmjs.org/clang-format/-/clang-format-1.8.0.tgz", "integrity": "sha512-pK8gzfu55/lHzIpQ1givIbWfn3eXnU7SfxqIwVgnn5jEM6j4ZJYjpFqFs4iSBPNedzRMmfjYjuQhu657WAXHXw==", "dev": true, + "license": "Apache-2.0", "dependencies": { "async": "^3.2.3", "glob": "^7.0.0", diff --git a/package.json b/package.json index 3040f2fc33aac..a4f2c373c3278 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "@commitlint/cli": "^19.3.0", "@commitlint/config-conventional": "^19.2.2", "@prettier/plugin-php": "^0.22.2", - "clang-format": "1.8.0", + "clang-format": "^1.8.0", "husky": "^9.0.1", "lint-staged": "^15.2.7", "prettier": "^3.3.2", diff --git a/solution/0100-0199/0195.Tenth Line/README_EN.md b/solution/0100-0199/0195.Tenth Line/README_EN.md index 69bdfb2395242..7ca9f7187d210 100644 --- a/solution/0100-0199/0195.Tenth Line/README_EN.md +++ b/solution/0100-0199/0195.Tenth Line/README_EN.md @@ -23,41 +23,26 @@ tags:

Assume that file.txt has the following content:

-
 Line 1
-
 Line 2
-
 Line 3
-
 Line 4
-
 Line 5
-
 Line 6
-
 Line 7
-
 Line 8
-
 Line 9
-
 Line 10
-
 

Your script should output the tenth line, which is:

-
 Line 10
-
 
Note:
- 1. If the file contains less than 10 lines, what should you output?
- 2. There's at least three different solutions. Try to explore all possibilities.
diff --git a/solution/0700-0799/0799.Champagne Tower/README_EN.md b/solution/0700-0799/0799.Champagne Tower/README_EN.md index c04fa21334900..7c1df1e554754 100644 --- a/solution/0700-0799/0799.Champagne Tower/README_EN.md +++ b/solution/0700-0799/0799.Champagne Tower/README_EN.md @@ -27,51 +27,35 @@ tags:

Now after pouring some non-negative integer cups of champagne, return how full the jth glass in the ith row is (both i and j are 0-indexed.)

 

-

Example 1:

-
 Input: poured = 1, query_row = 1, query_glass = 1
-
 Output: 0.00000
-
 Explanation: We poured 1 cup of champange to the top glass of the tower (which is indexed as (0, 0)). There will be no excess liquid so all the glasses under the top glass will remain empty.
-
 

Example 2:

-
 Input: poured = 2, query_row = 1, query_glass = 1
-
 Output: 0.50000
-
 Explanation: We poured 2 cups of champange to the top glass of the tower (which is indexed as (0, 0)). There is one cup of excess liquid. The glass indexed as (1, 0) and the glass indexed as (1, 1) will share the excess liquid equally, and each will get half cup of champange.
-
 

Example 3:

-
 Input: poured = 100000009, query_row = 33, query_glass = 17
-
 Output: 1.00000
-
 

 

-

Constraints:

diff --git a/solution/1100-1199/1108.Defanging an IP Address/README_EN.md b/solution/1100-1199/1108.Defanging an IP Address/README_EN.md index 537e7be2aa54c..d141c92ba6e45 100644 --- a/solution/1100-1199/1108.Defanging an IP Address/README_EN.md +++ b/solution/1100-1199/1108.Defanging an IP Address/README_EN.md @@ -23,29 +23,18 @@ tags:

A defanged IP address replaces every period "." with "[.]".

 

-

Example 1:

-
Input: address = "1.1.1.1"
-
 Output: "1[.]1[.]1[.]1"
-
 

Example 2:

-
Input: address = "255.100.50.0"
-
 Output: "255[.]100[.]50[.]0"
-
 
-

 

-

Constraints:

diff --git a/solution/1100-1199/1111.Maximum Nesting Depth of Two Valid Parentheses Strings/README_EN.md b/solution/1100-1199/1111.Maximum Nesting Depth of Two Valid Parentheses Strings/README_EN.md index f4d4f5e49c794..c9697163bc163 100644 --- a/solution/1100-1199/1111.Maximum Nesting Depth of Two Valid Parentheses Strings/README_EN.md +++ b/solution/1100-1199/1111.Maximum Nesting Depth of Two Valid Parentheses Strings/README_EN.md @@ -22,25 +22,17 @@ tags:

A string is a valid parentheses string (denoted VPS) if and only if it consists of "(" and ")" characters only, and:

We can similarly define the nesting depth depth(S) of any VPS S as follows:

For example,  """()()", and "()(()())" are VPS's (with nesting depths 0, 1, and 2), and ")(" and "(()" are not VPS's.

diff --git a/solution/1100-1199/1138.Alphabet Board Path/README_EN.md b/solution/1100-1199/1138.Alphabet Board Path/README_EN.md index 27f26e711961c..985ac691f6ce8 100644 --- a/solution/1100-1199/1138.Alphabet Board Path/README_EN.md +++ b/solution/1100-1199/1138.Alphabet Board Path/README_EN.md @@ -28,17 +28,11 @@ tags:

We may make the following moves:

(Here, the only positions that exist on the board are positions with letters on them.)

@@ -46,31 +40,19 @@ tags:

Return a sequence of moves that makes our answer equal to target in the minimum number of moves.  You may return any path that does so.

 

-

Example 1:

-
Input: target = "leet"
-
 Output: "DDR!UURRR!!DDD!"
-
 

Example 2:

-
Input: target = "code"
-
 Output: "RR!DDRR!UUL!R!"
-
 
-

 

-

Constraints:

diff --git a/solution/1100-1199/1139.Largest 1-Bordered Square/README_EN.md b/solution/1100-1199/1139.Largest 1-Bordered Square/README_EN.md index f88d6f213fbf6..0e417fbc50b55 100644 --- a/solution/1100-1199/1139.Largest 1-Bordered Square/README_EN.md +++ b/solution/1100-1199/1139.Largest 1-Bordered Square/README_EN.md @@ -23,39 +23,27 @@ tags:

Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid that has all 1s on its border, or 0 if such a subgrid doesn't exist in the grid.

 

-

Example 1:

-
 Input: grid = [[1,1,1],[1,0,1],[1,1,1]]
-
 Output: 9
-
 

Example 2:

-
 Input: grid = [[1,1,0,0]]
-
 Output: 1
-
 

 

-

Constraints:

diff --git a/solution/1100-1199/1184.Distance Between Bus Stops/README_EN.md b/solution/1100-1199/1184.Distance Between Bus Stops/README_EN.md index b4e5e94f2f0f6..d947861120f5a 100644 --- a/solution/1100-1199/1184.Distance Between Bus Stops/README_EN.md +++ b/solution/1100-1199/1184.Distance Between Bus Stops/README_EN.md @@ -25,17 +25,13 @@ tags:

Return the shortest distance between the given start and destination stops.

 

-

Example 1:

-
 Input: distance = [1,2,3,4], start = 0, destination = 1
-
 Output: 1
-
 Explanation: Distance between 0 and 1 is 1 or 9, minimum is 1.

 

@@ -45,13 +41,9 @@ tags:

-
 Input: distance = [1,2,3,4], start = 0, destination = 2
-
 Output: 3
-
 Explanation: Distance between 0 and 2 is 3 or 7, minimum is 3.
-
 

 

@@ -61,29 +53,19 @@ tags:

-
 Input: distance = [1,2,3,4], start = 0, destination = 3
-
 Output: 4
-
 Explanation: Distance between 0 and 3 is 6 or 4, minimum is 4.
-
 

 

-

Constraints:

diff --git a/solution/1200-1299/1238.Circular Permutation in Binary Representation/README_EN.md b/solution/1200-1299/1238.Circular Permutation in Binary Representation/README_EN.md index f3d3a209aa212..57a0b48ed9325 100644 --- a/solution/1200-1299/1238.Circular Permutation in Binary Representation/README_EN.md +++ b/solution/1200-1299/1238.Circular Permutation in Binary Representation/README_EN.md @@ -23,53 +23,35 @@ tags:

Given 2 integers n and start. Your task is return any permutation p of (0,1,2.....,2^n -1) such that :

 

-

Example 1:

-
 Input: n = 2, start = 3
-
 Output: [3,2,0,1]
-
 Explanation: The binary representation of the permutation is (11,10,00,01). 
-
 All the adjacent element differ by one bit. Another valid permutation is [3,1,0,2]
-
 

Example 2:

-
 Input: n = 3, start = 2
-
 Output: [2,6,7,5,4,0,1,3]
-
 Explanation: The binary representation of the permutation is (010,110,111,101,100,000,001,011).
-
 

 

-

Constraints:

diff --git a/solution/1200-1299/1256.Encode Number/README_EN.md b/solution/1200-1299/1256.Encode Number/README_EN.md index 43b8d3ed61ded..8793661958481 100644 --- a/solution/1200-1299/1256.Encode Number/README_EN.md +++ b/solution/1200-1299/1256.Encode Number/README_EN.md @@ -27,35 +27,25 @@ tags:

 

-

Example 1:

-
 Input: num = 23
-
 Output: "1000"
-
 

Example 2:

-
 Input: num = 107
-
 Output: "101100"
-
 

 

-

Constraints:

diff --git a/solution/1300-1399/1301.Number of Paths with Max Score/README_EN.md b/solution/1300-1399/1301.Number of Paths with Max Score/README_EN.md index 42f8aab8492d8..e9ae14167981a 100644 --- a/solution/1300-1399/1301.Number of Paths with Max Score/README_EN.md +++ b/solution/1300-1399/1301.Number of Paths with Max Score/README_EN.md @@ -29,35 +29,21 @@ tags:

In case there is no path, return [0, 0].

 

-

Example 1:

-
Input: board = ["E23","2X2","12S"]
-
 Output: [7,1]
-
 

Example 2:

-
Input: board = ["E12","1X1","21S"]
-
 Output: [4,2]
-
 

Example 3:

-
Input: board = ["E11","XXX","11S"]
-
 Output: [0,0]
-
 
-

 

-

Constraints:

diff --git a/solution/1300-1399/1318.Minimum Flips to Make a OR b Equal to c/README_EN.md b/solution/1300-1399/1318.Minimum Flips to Make a OR b Equal to c/README_EN.md index 5d94e91fb76df..25cf3e136deec 100644 --- a/solution/1300-1399/1318.Minimum Flips to Make a OR b Equal to c/README_EN.md +++ b/solution/1300-1399/1318.Minimum Flips to Make a OR b Equal to c/README_EN.md @@ -19,55 +19,39 @@ tags:

Given 3 positives numbers a, b and c. Return the minimum flips required in some bits of a and b to make ( a OR b == c ). (bitwise OR operation).
- Flip operation consists of change any single bit 1 to 0 or change the bit 0 to 1 in their binary representation.

 

-

Example 1:

-
 Input: a = 2, b = 6, c = 5
-
 Output: 3
-
 Explanation: After flips a = 1 , b = 4 , c = 5 such that (a OR b == c)

Example 2:

-
 Input: a = 4, b = 2, c = 7
-
 Output: 1
-
 

Example 3:

-
 Input: a = 1, b = 2, c = 3
-
 Output: 0
-
 

 

-

Constraints:

diff --git a/solution/1300-1399/1324.Print Words Vertically/README_EN.md b/solution/1300-1399/1324.Print Words Vertically/README_EN.md index c86ec25b2c2cb..e1542d061c72f 100644 --- a/solution/1300-1399/1324.Print Words Vertically/README_EN.md +++ b/solution/1300-1399/1324.Print Words Vertically/README_EN.md @@ -21,71 +21,46 @@ tags:

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:

diff --git a/solution/1400-1499/1418.Display Table of Food Orders in a Restaurant/README_EN.md b/solution/1400-1499/1418.Display Table of Food Orders in a Restaurant/README_EN.md index dd6bc86d1a53a..809ec164c6da3 100644 --- a/solution/1400-1499/1418.Display Table of Food Orders in a Restaurant/README_EN.md +++ b/solution/1400-1499/1418.Display Table of Food Orders in a Restaurant/README_EN.md @@ -27,77 +27,48 @@ tags:

Return the restaurant's “display table. The “display table” is a table whose row entries denote how many of each food item each table ordered. The first column is the table number and the remaining columns correspond to each food item in alphabetical order. The first row should be a header whose first column is “Table”, followed by the names of the food items. Note that the customer names are not part of the table. Additionally, the rows should be sorted in numerically increasing order.

 

-

Example 1:

-
 Input: orders = [["David","3","Ceviche"],["Corina","10","Beef Burrito"],["David","3","Fried Chicken"],["Carla","5","Water"],["Carla","5","Ceviche"],["Rous","3","Ceviche"]]
-
 Output: [["Table","Beef Burrito","Ceviche","Fried Chicken","Water"],["3","0","2","1","0"],["5","0","1","0","1"],["10","1","0","0","0"]] 
-
 Explanation:
-
 The displaying table looks like:
-
 Table,Beef Burrito,Ceviche,Fried Chicken,Water
-
 3    ,0           ,2      ,1            ,0
-
 5    ,0           ,1      ,0            ,1
-
 10   ,1           ,0      ,0            ,0
-
 For the table 3: David orders "Ceviche" and "Fried Chicken", and Rous orders "Ceviche".
-
 For the table 5: Carla orders "Water" and "Ceviche".
-
 For the table 10: Corina orders "Beef Burrito". 
-
 

Example 2:

-
 Input: orders = [["James","12","Fried Chicken"],["Ratesh","12","Fried Chicken"],["Amadeus","12","Fried Chicken"],["Adam","1","Canadian Waffles"],["Brianna","1","Canadian Waffles"]]
-
 Output: [["Table","Canadian Waffles","Fried Chicken"],["1","2","0"],["12","0","3"]] 
-
 Explanation: 
-
 For the table 1: Adam and Brianna order "Canadian Waffles".
-
 For the table 12: James, Ratesh and Amadeus order "Fried Chicken".
-
 

Example 3:

-
 Input: orders = [["Laura","2","Bean Burrito"],["Jhon","2","Beef Burrito"],["Melissa","2","Soda"]]
-
 Output: [["Table","Bean Burrito","Beef Burrito","Soda"],["2","1","1","1"]]
-
 

 

-

Constraints:

diff --git a/solution/1400-1499/1448.Count Good Nodes in Binary Tree/README_EN.md b/solution/1400-1499/1448.Count Good Nodes in Binary Tree/README_EN.md index 4a07c72b0b946..75f9d52c74990 100644 --- a/solution/1400-1499/1448.Count Good Nodes in Binary Tree/README_EN.md +++ b/solution/1400-1499/1448.Count Good Nodes in Binary Tree/README_EN.md @@ -26,25 +26,17 @@ tags:

Return the number of good nodes in the binary tree.

 

-

Example 1:

-
 Input: root = [3,1,4,3,null,1,5]
-
 Output: 4
-
 Explanation: Nodes in blue are good.
-
 Root Node (3) is always a good node.
-
 Node 4 -> (3,4) is the maximum value in the path starting from the root.
-
 Node 5 -> (3,4,5) is the maximum value in the path
-
 Node 3 -> (3,1,3) is the maximum value in the path.

Example 2:

@@ -52,33 +44,23 @@ Node 3 -> (3,1,3) is the maximum value in the path.

-
 Input: root = [3,3,null,4,2]
-
 Output: 3
-
 Explanation: Node 2 -> (3, 3, 2) is not good, because "3" is higher than it.

Example 3:

-
 Input: root = [1]
-
 Output: 1
-
 Explanation: Root is considered as good.

 

-

Constraints:

diff --git a/solution/1400-1499/1470.Shuffle the Array/README_EN.md b/solution/1400-1499/1470.Shuffle the Array/README_EN.md index 8a806cd3fd73d..9246c35a92d80 100644 --- a/solution/1400-1499/1470.Shuffle the Array/README_EN.md +++ b/solution/1400-1499/1470.Shuffle the Array/README_EN.md @@ -23,51 +23,35 @@ tags:

Return the array in the form [x1,y1,x2,y2,...,xn,yn].

 

-

Example 1:

-
 Input: nums = [2,5,1,3,4,7], n = 3
-
 Output: [2,3,5,4,1,7] 
-
 Explanation: Since x1=2, x2=5, x3=1, y1=3, y2=4, y3=7 then the answer is [2,3,5,4,1,7].
-
 

Example 2:

-
 Input: nums = [1,2,3,4,4,3,2,1], n = 4
-
 Output: [1,4,2,3,3,2,4,1]
-
 

Example 3:

-
 Input: nums = [1,1,2,2], n = 2
-
 Output: [1,2,1,2]
-
 

 

-

Constraints:

diff --git a/solution/1400-1499/1481.Least Number of Unique Integers after K Removals/README_EN.md b/solution/1400-1499/1481.Least Number of Unique Integers after K Removals/README_EN.md index d367cf0458c18..00dee15fada33 100644 --- a/solution/1400-1499/1481.Least Number of Unique Integers after K Removals/README_EN.md +++ b/solution/1400-1499/1481.Least Number of Unique Integers after K Removals/README_EN.md @@ -25,45 +25,31 @@ tags:

Given an array of integers arr and an integer k. Find the least number of unique integers after removing exactly k elements.

    -

 

-

Example 1:

-
 Input: arr = [5,5,4], k = 1
-
 Output: 1
-
 Explanation: Remove the single 4, only 5 is left.
-
 
Example 2:
-
 Input: arr = [4,3,1,1,3,3,2], k = 3
-
 Output: 2
-
 Explanation: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.

 

-

Constraints:

diff --git a/solution/1500-1599/1523.Count Odd Numbers in an Interval Range/README_EN.md b/solution/1500-1599/1523.Count Odd Numbers in an Interval Range/README_EN.md index 03cb439cf8dbc..d1eea923bf055 100644 --- a/solution/1500-1599/1523.Count Odd Numbers in an Interval Range/README_EN.md +++ b/solution/1500-1599/1523.Count Odd Numbers in an Interval Range/README_EN.md @@ -21,35 +21,25 @@ tags:

Given two non-negative integers low and high. Return the count of odd numbers between low and high (inclusive).

 

-

Example 1:

-
 Input: low = 3, high = 7
-
 Output: 3
-
 Explanation: The odd numbers between 3 and 7 are [3,5,7].

Example 2:

-
 Input: low = 8, high = 10
-
 Output: 1
-
 Explanation: The odd numbers between 8 and 10 are [9].

 

-

Constraints:

diff --git a/solution/1500-1599/1534.Count Good Triplets/README_EN.md b/solution/1500-1599/1534.Count Good Triplets/README_EN.md index 1a417e60574bf..cdec96dfb8cd3 100644 --- a/solution/1500-1599/1534.Count Good Triplets/README_EN.md +++ b/solution/1500-1599/1534.Count Good Triplets/README_EN.md @@ -24,15 +24,10 @@ tags:

A triplet (arr[i], arr[j], arr[k]) is good if the following conditions are true:

Where |x| denotes the absolute value of x.

@@ -40,43 +35,29 @@ tags:

Return the number of good triplets.

 

-

Example 1:

-
 Input: arr = [3,0,1,1,9,7], a = 7, b = 2, c = 3
-
 Output: 4
-
 Explanation: There are 4 good triplets: [(3,0,1), (3,0,1), (3,1,1), (0,1,1)].
-
 

Example 2:

-
 Input: arr = [1,1,2,2,3], a = 0, b = 0, c = 1
-
 Output: 0
-
 Explanation: No triplet satisfies all conditions.
-
 

 

-

Constraints:

diff --git a/solution/1600-1699/1617.Count Subtrees With Max Distance Between Cities/README_EN.md b/solution/1600-1699/1617.Count Subtrees With Max Distance Between Cities/README_EN.md index a39d7dd0a4861..58a5aa233655e 100644 --- a/solution/1600-1699/1617.Count Subtrees With Max Distance Between Cities/README_EN.md +++ b/solution/1600-1699/1617.Count Subtrees With Max Distance Between Cities/README_EN.md @@ -33,63 +33,42 @@ tags:

Notice that the distance between the two cities is the number of edges in the path between them.

 

-

Example 1:

-
 Input: n = 4, edges = [[1,2],[2,3],[2,4]]
-
 Output: [3,4,0]
-
 Explanation:
-
 The subtrees with subsets {1,2}, {2,3} and {2,4} have a max distance of 1.
-
 The subtrees with subsets {1,2,3}, {1,2,4}, {2,3,4} and {1,2,3,4} have a max distance of 2.
-
 No subtree has two nodes where the max distance between them is 3.
-
 

Example 2:

-
 Input: n = 2, edges = [[1,2]]
-
 Output: [1]
-
 

Example 3:

-
 Input: n = 3, edges = [[1,2],[2,3]]
-
 Output: [2,1]
-
 

 

-

Constraints:

diff --git a/solution/1600-1699/1618.Maximum Font to Fit a Sentence in a Screen/README_EN.md b/solution/1600-1699/1618.Maximum Font to Fit a Sentence in a Screen/README_EN.md index 86b831b8cd79f..637b3531cdded 100644 --- a/solution/1600-1699/1618.Maximum Font to Fit a Sentence in a Screen/README_EN.md +++ b/solution/1600-1699/1618.Maximum Font to Fit a Sentence in a Screen/README_EN.md @@ -26,23 +26,14 @@ tags:

The FontInfo interface is defined as such:

-
 interface FontInfo {
-
   // Returns the width of character ch on the screen using font size fontSize.
-
   // O(1) per call
-
   public int getWidth(int fontSize, char ch);
 
-
-
   // Returns the height of any character on the screen using font size fontSize.
-
   // O(1) per call
-
   public int getHeight(int fontSize);
-
 }

The calculated width of text for some fontSize is the sum of every getWidth(fontSize, text[i]) call for each 0 <= i < text.length (0-indexed). The calculated height of text for some fontSize is getHeight(fontSize). Note that text is displayed on a single line.

@@ -52,67 +43,45 @@ interface FontInfo {

It is also guaranteed that for any font size fontSize and any character ch:

Return the maximum font size you can use to display text on the screen. If text cannot fit on the display with any font size, return -1.

 

-

Example 1:

-
 Input: text = "helloworld", w = 80, h = 20, fonts = [6,8,10,12,14,16,18,24,36]
-
 Output: 6
-
 

Example 2:

-
 Input: text = "leetcode", w = 1000, h = 50, fonts = [1,2,4]
-
 Output: 4
-
 

Example 3:

-
 Input: text = "easyquestion", w = 100, h = 100, fonts = [10,15,20,25]
-
 Output: -1
-
 

 

-

Constraints:

diff --git a/solution/1600-1699/1634.Add Two Polynomials Represented as Linked Lists/README_EN.md b/solution/1600-1699/1634.Add Two Polynomials Represented as Linked Lists/README_EN.md index 107929af544fa..b5a4c41d13436 100644 --- a/solution/1600-1699/1634.Add Two Polynomials Represented as Linked Lists/README_EN.md +++ b/solution/1600-1699/1634.Add Two Polynomials Represented as Linked Lists/README_EN.md @@ -23,13 +23,9 @@ tags:

Each node has three attributes:

For example, the polynomial 5x3 + 4x - 7 is represented by the polynomial linked list illustrated below:

@@ -45,61 +41,41 @@ tags:

The input/output format is as a list of n nodes, where each node is represented as its [coefficient, power]. For example, the polynomial 5x3 + 4x - 7 would be represented as: [[5,3],[4,1],[-7,0]].

 

-

Example 1:

-
 Input: poly1 = [[1,1]], poly2 = [[1,0]]
-
 Output: [[1,1],[1,0]]
-
 Explanation: poly1 = x. poly2 = 1. The sum is x + 1.
-
 

Example 2:

-
 Input: poly1 = [[2,2],[4,1],[3,0]], poly2 = [[3,2],[-4,1],[-1,0]]
-
 Output: [[5,2],[2,0]]
-
 Explanation: poly1 = 2x2 + 4x + 3. poly2 = 3x2 - 4x - 1. The sum is 5x2 + 2. Notice that we omit the "0x" term.
-
 

Example 3:

-
 Input: poly1 = [[1,2]], poly2 = [[-1,2]]
-
 Output: []
-
 Explanation: The sum is 0. We return an empty list.
-
 

 

-

Constraints:

diff --git a/solution/1600-1699/1649.Create Sorted Array through Instructions/README_EN.md b/solution/1600-1699/1649.Create Sorted Array through Instructions/README_EN.md index d7708d65d82ee..429cfe2142d4e 100644 --- a/solution/1600-1699/1649.Create Sorted Array through Instructions/README_EN.md +++ b/solution/1600-1699/1649.Create Sorted Array through Instructions/README_EN.md @@ -27,11 +27,8 @@ tags:

Given an integer array instructions, you are asked to create a sorted array from the elements in instructions. You start with an empty container nums. For each element from left to right in instructions, insert it into nums. The cost of each insertion is the minimum of the following:

For example, if inserting element 3 into nums = [1,2,3,5], the cost of insertion is min(2, 1) (elements 1 and 2 are less than 3, element 5 is greater than 3) and nums will become [1,2,3,3,5].

@@ -39,95 +36,57 @@ tags:

Return the total cost to insert all elements from instructions into nums. Since the answer may be large, return it modulo 109 + 7

 

-

Example 1:

-
 Input: instructions = [1,5,6,2]
-
 Output: 1
-
 Explanation: Begin with nums = [].
-
 Insert 1 with cost min(0, 0) = 0, now nums = [1].
-
 Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
-
 Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
-
 Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
-
 The total cost is 0 + 0 + 0 + 1 = 1.

Example 2:

-
 Input: instructions = [1,2,3,6,5,4]
-
 Output: 3
-
 Explanation: Begin with nums = [].
-
 Insert 1 with cost min(0, 0) = 0, now nums = [1].
-
 Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
-
 Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
-
 Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
-
 Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
-
 Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
-
 The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
-
 

Example 3:

-
 Input: instructions = [1,3,3,3,2,4,2,1,2]
-
 Output: 4
-
 Explanation: Begin with nums = [].
-
 Insert 1 with cost min(0, 0) = 0, now nums = [1].
-
 Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
-
 Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
-
 Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
-
 Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
-
 Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
-
 ​​​​​​​Insert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
-
 ​​​​​​​Insert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
-
 ​​​​​​​Insert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
-
 The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
-
 

 

-

Constraints:

diff --git a/solution/1600-1699/1660.Correct a Binary Tree/README_EN.md b/solution/1600-1699/1660.Correct a Binary Tree/README_EN.md index 4c4926392dd0d..4a5e058ed554d 100644 --- a/solution/1600-1699/1660.Correct a Binary Tree/README_EN.md +++ b/solution/1600-1699/1660.Correct a Binary Tree/README_EN.md @@ -29,31 +29,22 @@ tags:

The test input is read as 3 lines:

After the binary tree rooted at root is parsed, the TreeNode with value of fromNode will have its right child pointer pointing to the TreeNode with a value of toNode. Then, root is passed to correctBinaryTree.

 

-

Example 1:

-
 Input: root = [1,2,3], fromNode = 2, toNode = 3
-
 Output: [1,null,3]
-
 Explanation: The node with value 2 is invalid, so remove it.
-
 

Example 2:

@@ -61,35 +52,22 @@ tags:

-
 Input: root = [8,3,1,7,null,9,4,2,null,null,null,5,6], fromNode = 7, toNode = 4
-
 Output: [8,3,1,null,null,9,4,null,null,5,6]
-
 Explanation: The node with value 7 is invalid, so remove it and the node underneath it, node 2.
-
 

 

-

Constraints:

diff --git a/solution/1700-1799/1725.Number Of Rectangles That Can Form The Largest Square/README_EN.md b/solution/1700-1799/1725.Number Of Rectangles That Can Form The Largest Square/README_EN.md index 34f00129f2a96..cbb623295007f 100644 --- a/solution/1700-1799/1725.Number Of Rectangles That Can Form The Largest Square/README_EN.md +++ b/solution/1700-1799/1725.Number Of Rectangles That Can Form The Largest Square/README_EN.md @@ -27,45 +27,30 @@ tags:

Return the number of rectangles that can make a square with a side length of maxLen.

 

-

Example 1:

-
 Input: rectangles = [[5,8],[3,9],[5,12],[16,5]]
-
 Output: 3
-
 Explanation: The largest squares you can get from each rectangle are of lengths [5,3,5,5].
-
 The largest possible square is of length 5, and you can get it out of 3 rectangles.
-
 

Example 2:

-
 Input: rectangles = [[2,3],[3,7],[4,3],[3,7]]
-
 Output: 3
-
 

 

-

Constraints:

diff --git a/solution/1700-1799/1746.Maximum Subarray Sum After One Operation/README_EN.md b/solution/1700-1799/1746.Maximum Subarray Sum After One Operation/README_EN.md index 2ae33ffa3a49c..a96cde7d700f3 100644 --- a/solution/1700-1799/1746.Maximum Subarray Sum After One Operation/README_EN.md +++ b/solution/1700-1799/1746.Maximum Subarray Sum After One Operation/README_EN.md @@ -22,37 +22,26 @@ tags:

Return the maximum possible subarray sum after exactly one operation. The subarray must be non-empty.

 

-

Example 1:

-
 Input: nums = [2,-1,-4,-3]
-
 Output: 17
-
 Explanation: You can perform the operation on index 2 (0-indexed) to make nums = [2,-1,16,-3]. Now, the maximum subarray sum is 2 + -1 + 16 = 17.

Example 2:

-
 Input: nums = [1,-1,1,1,-1,-1,1]
-
 Output: 4
-
 Explanation: You can perform the operation on index 1 (0-indexed) to make nums = [1,1,1,1,-1,-1,1]. Now, the maximum subarray sum is 1 + 1 + 1 + 1 = 4.

 

-

Constraints:

diff --git a/solution/1700-1799/1768.Merge Strings Alternately/README_EN.md b/solution/1700-1799/1768.Merge Strings Alternately/README_EN.md index 23f19bc99a475..28ec43946435a 100644 --- a/solution/1700-1799/1768.Merge Strings Alternately/README_EN.md +++ b/solution/1700-1799/1768.Merge Strings Alternately/README_EN.md @@ -24,71 +24,45 @@ tags:

Return the merged string.

 

-

Example 1:

-
 Input: word1 = "abc", word2 = "pqr"
-
 Output: "apbqcr"
-
 Explanation: The merged string will be merged as so:
-
 word1:  a   b   c
-
 word2:    p   q   r
-
 merged: a p b q c r
-
 

Example 2:

-
 Input: word1 = "ab", word2 = "pqrs"
-
 Output: "apbqrs"
-
 Explanation: Notice that as word2 is longer, "rs" is appended to the end.
-
 word1:  a   b 
-
 word2:    p   q   r   s
-
 merged: a p b q   r   s
-
 

Example 3:

-
 Input: word1 = "abcd", word2 = "pq"
-
 Output: "apbqcd"
-
 Explanation: Notice that as word1 is longer, "cd" is appended to the end.
-
 word1:  a   b   c   d
-
 word2:    p   q 
-
 merged: a p b q c   d
-
 

 

-

Constraints:

diff --git a/solution/1700-1799/1788.Maximize the Beauty of the Garden/README_EN.md b/solution/1700-1799/1788.Maximize the Beauty of the Garden/README_EN.md index 08179c3e044b3..8060248067a33 100644 --- a/solution/1700-1799/1788.Maximize the Beauty of the Garden/README_EN.md +++ b/solution/1700-1799/1788.Maximize the Beauty of the Garden/README_EN.md @@ -24,11 +24,8 @@ tags:

A garden is valid if it meets these conditions:

As the appointed gardener, you have the ability to remove any (possibly none) flowers from the garden. You want to remove flowers in a way that makes the remaining garden valid. The beauty of the garden is the sum of the beauty of all the remaining flowers.

@@ -36,53 +33,36 @@ tags:

Return the maximum possible beauty of some valid garden after you have removed any (possibly none) flowers.

 

-

Example 1:

-
 Input: flowers = [1,2,3,1,2]
-
 Output: 8
-
 Explanation: You can produce the valid garden [2,3,1,2] to have a total beauty of 2 + 3 + 1 + 2 = 8.

Example 2:

-
 Input: flowers = [100,1,1,-3,1]
-
 Output: 3
-
 Explanation: You can produce the valid garden [1,1,1] to have a total beauty of 1 + 1 + 1 = 3.
-
 

Example 3:

-
 Input: flowers = [-1,-2,0,-1]
-
 Output: -2
-
 Explanation: You can produce the valid garden [-1,-1] to have a total beauty of -1 + -1 = -2.
-
 

 

-

Constraints:

diff --git a/solution/1800-1899/1801.Number of Orders in the Backlog/README_EN.md b/solution/1800-1899/1801.Number of Orders in the Backlog/README_EN.md index fb8b1f0df8967..858710a6203f7 100644 --- a/solution/1800-1899/1801.Number of Orders in the Backlog/README_EN.md +++ b/solution/1800-1899/1801.Number of Orders in the Backlog/README_EN.md @@ -23,11 +23,8 @@ tags:

You are given a 2D integer array orders, where each orders[i] = [pricei, amounti, orderTypei] denotes that amounti orders have been placed of type orderTypei at the price pricei. The orderTypei is:

Note that orders[i] represents a batch of amounti independent orders with the same price and order type. All orders represented by orders[i] will be placed before all orders represented by orders[i+1] for all valid i.

@@ -35,79 +32,47 @@ tags:

There is a backlog that consists of orders that have not been executed. The backlog is initially empty. When an order is placed, the following happens:

Return the total amount of orders in the backlog after placing all the orders from the input. Since this number can be large, return it modulo 109 + 7.

 

-

Example 1:

- -
-
 Input: orders = [[10,5,0],[15,2,1],[25,1,1],[30,4,0]]
-
 Output: 6
-
 Explanation: Here is what happens with the orders:
-
 - 5 orders of type buy with price 10 are placed. There are no sell orders, so the 5 orders are added to the backlog.
-
 - 2 orders of type sell with price 15 are placed. There are no buy orders with prices larger than or equal to 15, so the 2 orders are added to the backlog.
-
 - 1 order of type sell with price 25 is placed. There are no buy orders with prices larger than or equal to 25 in the backlog, so this order is added to the backlog.
-
 - 4 orders of type buy with price 30 are placed. The first 2 orders are matched with the 2 sell orders of the least price, which is 15 and these 2 sell orders are removed from the backlog. The 3rd order is matched with the sell order of the least price, which is 25 and this sell order is removed from the backlog. Then, there are no more sell orders in the backlog, so the 4th order is added to the backlog.
-
 Finally, the backlog has 5 buy orders with price 10, and 1 buy order with price 30. So the total number of orders in the backlog is 6.
-
 

Example 2:

- -
-
 Input: orders = [[7,1000000000,1],[15,3,0],[5,999999995,0],[5,1,1]]
-
 Output: 999999984
-
 Explanation: Here is what happens with the orders:
-
 - 109 orders of type sell with price 7 are placed. There are no buy orders, so the 109 orders are added to the backlog.
-
 - 3 orders of type buy with price 15 are placed. They are matched with the 3 sell orders with the least price which is 7, and those 3 sell orders are removed from the backlog.
-
 - 999999995 orders of type buy with price 5 are placed. The least price of a sell order is 7, so the 999999995 orders are added to the backlog.
-
 - 1 order of type sell with price 5 is placed. It is matched with the buy order of the highest price, which is 5, and that buy order is removed from the backlog.
-
 Finally, the backlog has (1000000000-3) sell orders with price 7, and (999999995-1) buy orders with price 5. So the total number of orders = 1999999991, which is equal to 999999984 % (109 + 7).
-
 

 

-

Constraints:

diff --git a/solution/1800-1899/1803.Count Pairs With XOR in a Range/README_EN.md b/solution/1800-1899/1803.Count Pairs With XOR in a Range/README_EN.md index b2f0cba2a3268..71d734e8f7114 100644 --- a/solution/1800-1899/1803.Count Pairs With XOR in a Range/README_EN.md +++ b/solution/1800-1899/1803.Count Pairs With XOR in a Range/README_EN.md @@ -25,69 +25,42 @@ tags:

A nice pair is a pair (i, j) where 0 <= i < j < nums.length and low <= (nums[i] XOR nums[j]) <= high.

 

-

Example 1:

-
 Input: nums = [1,4,2,7], low = 2, high = 6
-
 Output: 6
-
 Explanation: All nice pairs (i, j) are as follows:
-
     - (0, 1): nums[0] XOR nums[1] = 5 
-
     - (0, 2): nums[0] XOR nums[2] = 3
-
     - (0, 3): nums[0] XOR nums[3] = 6
-
     - (1, 2): nums[1] XOR nums[2] = 6
-
     - (1, 3): nums[1] XOR nums[3] = 3
-
     - (2, 3): nums[2] XOR nums[3] = 5
-
 

Example 2:

-
 Input: nums = [9,8,4,2,1], low = 5, high = 14
-
 Output: 8
-
 Explanation: All nice pairs (i, j) are as follows:
-
 ​​​​​    - (0, 2): nums[0] XOR nums[2] = 13
-
     - (0, 3): nums[0] XOR nums[3] = 11
-
     - (0, 4): nums[0] XOR nums[4] = 8
-
     - (1, 2): nums[1] XOR nums[2] = 12
-
     - (1, 3): nums[1] XOR nums[3] = 10
-
     - (1, 4): nums[1] XOR nums[4] = 9
-
     - (2, 3): nums[2] XOR nums[3] = 6
-
     - (2, 4): nums[2] XOR nums[4] = 5

 

-

Constraints:

diff --git a/solution/1800-1899/1808.Maximize Number of Nice Divisors/README_EN.md b/solution/1800-1899/1808.Maximize Number of Nice Divisors/README_EN.md index d114a92da494e..043890fcacd1a 100644 --- a/solution/1800-1899/1808.Maximize Number of Nice Divisors/README_EN.md +++ b/solution/1800-1899/1808.Maximize Number of Nice Divisors/README_EN.md @@ -23,11 +23,8 @@ tags:

You are given a positive integer primeFactors. You are asked to construct a positive integer n that satisfies the following conditions:

Return the number of nice divisors of n. Since that number can be too large, return it modulo 109 + 7.

@@ -35,41 +32,28 @@ tags:

Note that a prime number is a natural number greater than 1 that is not a product of two smaller natural numbers. The prime factors of a number n is a list of prime numbers such that their product equals n.

 

-

Example 1:

-
 Input: primeFactors = 5
-
 Output: 6
-
 Explanation: 200 is a valid value of n.
-
 It has 5 prime factors: [2,2,2,5,5], and it has 6 nice divisors: [10,20,40,50,100,200].
-
 There is not other value of n that has at most 5 prime factors and more nice divisors.
-
 

Example 2:

-
 Input: primeFactors = 8
-
 Output: 18
-
 

 

-

Constraints:

diff --git a/solution/1800-1899/1827.Minimum Operations to Make the Array Increasing/README_EN.md b/solution/1800-1899/1827.Minimum Operations to Make the Array Increasing/README_EN.md index 979e584e8350d..af4a7cf20dd88 100644 --- a/solution/1800-1899/1827.Minimum Operations to Make the Array Increasing/README_EN.md +++ b/solution/1800-1899/1827.Minimum Operations to Make the Array Increasing/README_EN.md @@ -22,9 +22,7 @@ tags:

You are given an integer array nums (0-indexed). In one operation, you can choose an element of the array and increment it by 1.

Return the minimum number of operations needed to make nums strictly increasing.

@@ -32,55 +30,37 @@ tags:

An array nums is strictly increasing if nums[i] < nums[i+1] for all 0 <= i < nums.length - 1. An array of length 1 is trivially strictly increasing.

 

-

Example 1:

-
 Input: nums = [1,1,1]
-
 Output: 3
-
 Explanation: You can do the following operations:
-
 1) Increment nums[2], so nums becomes [1,1,2].
-
 2) Increment nums[1], so nums becomes [1,2,2].
-
 3) Increment nums[2], so nums becomes [1,2,3].
-
 

Example 2:

-
 Input: nums = [1,5,2,4,1]
-
 Output: 14
-
 

Example 3:

-
 Input: nums = [8]
-
 Output: 0
-
 

 

-

Constraints:

diff --git a/solution/1800-1899/1836.Remove Duplicates From an Unsorted Linked List/README_EN.md b/solution/1800-1899/1836.Remove Duplicates From an Unsorted Linked List/README_EN.md index e1c623dee2b31..22fb099044ee4 100644 --- a/solution/1800-1899/1836.Remove Duplicates From an Unsorted Linked List/README_EN.md +++ b/solution/1800-1899/1836.Remove Duplicates From an Unsorted Linked List/README_EN.md @@ -22,59 +22,36 @@ tags:

Return the linked list after the deletions.

 

-

Example 1:

- -
-
 Input: head = [1,2,3,2]
-
 Output: [1,3]
-
 Explanation: 2 appears twice in the linked list, so all 2's should be deleted. After deleting all 2's, we are left with [1,3].
-
 

Example 2:

- -
-
 Input: head = [2,1,1,2]
-
 Output: []
-
 Explanation: 2 and 1 both appear twice. All the elements should be deleted.
-
 

Example 3:

- -
-
 Input: head = [3,2,2,1,3,2,4]
-
 Output: [1,4]
-
 Explanation: 3 appears twice and 2 appears three times. After deleting all 3's and 2's, we are left with [1,4].
-
 

 

-

Constraints:

diff --git a/solution/1800-1899/1857.Largest Color Value in a Directed Graph/README_EN.md b/solution/1800-1899/1857.Largest Color Value in a Directed Graph/README_EN.md index d90b69df177ce..0aba7a5ebe9a4 100644 --- a/solution/1800-1899/1857.Largest Color Value in a Directed Graph/README_EN.md +++ b/solution/1800-1899/1857.Largest Color Value in a Directed Graph/README_EN.md @@ -32,19 +32,14 @@ tags:

Return the largest color value of any valid path in the given graph, or -1 if the graph contains a cycle.

 

-

Example 1:

-
 Input: colors = "abaca", edges = [[0,1],[0,2],[2,3],[3,4]]
-
 Output: 3
-
 Explanation: The path 0 -> 2 -> 3 -> 4 contains 3 nodes that are colored "a" (red in the above image).
-
 

Example 2:

@@ -52,33 +47,21 @@ tags:

-
 Input: colors = "a", edges = [[0,0]]
-
 Output: -1
-
 Explanation: There is a cycle from 0 to 0.
-
 

 

-

Constraints:

diff --git a/solution/1800-1899/1872.Stone Game VIII/README_EN.md b/solution/1800-1899/1872.Stone Game VIII/README_EN.md index db273764cb33b..fc84b32817c0f 100644 --- a/solution/1800-1899/1872.Stone Game VIII/README_EN.md +++ b/solution/1800-1899/1872.Stone Game VIII/README_EN.md @@ -27,13 +27,9 @@ tags:

There are n stones arranged in a row. On each player's turn, while the number of stones is more than one, they will do the following:

    - -
  1. Choose an integer x > 1, and remove the leftmost x stones from the row.
  2. - -
  3. Add the sum of the removed stones' values to the player's score.
  4. - -
  5. Place a new stone, whose value is equal to that sum, on the left side of the row.
  6. - +
  7. Choose an integer x > 1, and remove the leftmost x stones from the row.
  8. +
  9. Add the sum of the removed stones' values to the player's score.
  10. +
  11. Place a new stone, whose value is equal to that sum, on the left side of the row.

The game stops when only one stone is left in the row.

@@ -43,77 +39,48 @@ tags:

Given an integer array stones of length n where stones[i] represents the value of the ith stone from the left, return the score difference between Alice and Bob if they both play optimally.

 

-

Example 1:

-
 Input: stones = [-1,2,-3,4,-5]
-
 Output: 5
-
 Explanation:
-
 - Alice removes the first 4 stones, adds (-1) + 2 + (-3) + 4 = 2 to her score, and places a stone of
-
   value 2 on the left. stones = [2,-5].
-
 - Bob removes the first 2 stones, adds 2 + (-5) = -3 to his score, and places a stone of value -3 on
-
   the left. stones = [-3].
-
 The difference between their scores is 2 - (-3) = 5.
-
 

Example 2:

-
 Input: stones = [7,-6,5,10,5,-2,-6]
-
 Output: 13
-
 Explanation:
-
 - Alice removes all stones, adds 7 + (-6) + 5 + 10 + 5 + (-2) + (-6) = 13 to her score, and places a
-
   stone of value 13 on the left. stones = [13].
-
 The difference between their scores is 13 - 0 = 13.
-
 

Example 3:

-
 Input: stones = [-10,-12]
-
 Output: -22
-
 Explanation:
-
 - Alice can only make one move, which is to remove both stones. She adds (-10) + (-12) = -22 to her
-
   score and places a stone of value -22 on the left. stones = [-22].
-
 The difference between their scores is (-22) - 0 = -22.
-
 

 

-

Constraints:

diff --git a/solution/1800-1899/1874.Minimize Product Sum of Two Arrays/README_EN.md b/solution/1800-1899/1874.Minimize Product Sum of Two Arrays/README_EN.md index f4db46805a1ca..a072d1144acd3 100644 --- a/solution/1800-1899/1874.Minimize Product Sum of Two Arrays/README_EN.md +++ b/solution/1800-1899/1874.Minimize Product Sum of Two Arrays/README_EN.md @@ -21,51 +21,35 @@ tags:

The product sum of two equal-length arrays a and b is equal to the sum of a[i] * b[i] for all 0 <= i < a.length (0-indexed).

Given two arrays nums1 and nums2 of length n, return the minimum product sum if you are allowed to rearrange the order of the elements in nums1

 

-

Example 1:

-
 Input: nums1 = [5,3,4,2], nums2 = [4,2,2,5]
-
 Output: 40
-
 Explanation: We can rearrange nums1 to become [3,5,4,2]. The product sum of [3,5,4,2] and [4,2,2,5] is 3*4 + 5*2 + 4*2 + 2*5 = 40.
-
 

Example 2:

-
 Input: nums1 = [2,1,4,5,7], nums2 = [3,2,4,8,6]
-
 Output: 65
-
 Explanation: We can rearrange nums1 to become [5,7,4,1,2]. The product sum of [5,7,4,1,2] and [3,2,4,8,6] is 5*3 + 7*2 + 4*4 + 1*8 + 2*6 = 65.
-
 

 

-

Constraints:

diff --git a/solution/1800-1899/1877.Minimize Maximum Pair Sum in Array/README_EN.md b/solution/1800-1899/1877.Minimize Maximum Pair Sum in Array/README_EN.md index 4e50827246f87..929c483956728 100644 --- a/solution/1800-1899/1877.Minimize Maximum Pair Sum in Array/README_EN.md +++ b/solution/1800-1899/1877.Minimize Maximum Pair Sum in Array/README_EN.md @@ -24,67 +24,45 @@ tags:

The pair sum of a pair (a,b) is equal to a + b. The maximum pair sum is the largest pair sum in a list of pairs.

Given an array nums of even length n, pair up the elements of nums into n / 2 pairs such that:

Return the minimized maximum pair sum after optimally pairing up the elements.

 

-

Example 1:

-
 Input: nums = [3,5,2,3]
-
 Output: 7
-
 Explanation: The elements can be paired up into pairs (3,3) and (5,2).
-
 The maximum pair sum is max(3+3, 5+2) = max(6, 7) = 7.
-
 

Example 2:

-
 Input: nums = [3,5,4,2,4,6]
-
 Output: 8
-
 Explanation: The elements can be paired up into pairs (3,5), (4,4), and (6,2).
-
 The maximum pair sum is max(3+5, 4+4, 6+2) = max(8, 8, 8) = 8.
-
 

 

-

Constraints:

diff --git a/solution/1900-1999/1911.Maximum Alternating Subsequence Sum/README_EN.md b/solution/1900-1999/1911.Maximum Alternating Subsequence Sum/README_EN.md index 76cda62341188..ecc7532e55269 100644 --- a/solution/1900-1999/1911.Maximum Alternating Subsequence Sum/README_EN.md +++ b/solution/1900-1999/1911.Maximum Alternating Subsequence Sum/README_EN.md @@ -22,67 +22,47 @@ tags:

The alternating sum of a 0-indexed array is defined as the sum of the elements at even indices minus the sum of the elements at odd indices.

Given an array nums, return the maximum alternating sum of any subsequence of nums (after reindexing the elements of the subsequence).

A subsequence of an array is a new array generated from the original array by deleting some elements (possibly none) without changing the remaining elements' relative order. For example, [2,7,4] is a subsequence of [4,2,3,7,2,1,4] (the underlined elements), while [2,4,2] is not.

 

-

Example 1:

-
 Input: nums = [4,2,5,3]
-
 Output: 7
-
 Explanation: It is optimal to choose the subsequence [4,2,5] with alternating sum (4 + 5) - 2 = 7.
-
 

Example 2:

-
 Input: nums = [5,6,7,8]
-
 Output: 8
-
 Explanation: It is optimal to choose the subsequence [8] with alternating sum 8.
-
 

Example 3:

-
 Input: nums = [6,2,1,2,4,5]
-
 Output: 10
-
 Explanation: It is optimal to choose the subsequence [6,1,5] with alternating sum (6 + 5) - 1 = 10.
-
 

 

-

Constraints:

diff --git a/solution/1900-1999/1913.Maximum Product Difference Between Two Pairs/README_EN.md b/solution/1900-1999/1913.Maximum Product Difference Between Two Pairs/README_EN.md index 806f7cb640d4f..de2987a86b5c6 100644 --- a/solution/1900-1999/1913.Maximum Product Difference Between Two Pairs/README_EN.md +++ b/solution/1900-1999/1913.Maximum Product Difference Between Two Pairs/README_EN.md @@ -22,9 +22,7 @@ tags:

The product difference between two pairs (a, b) and (c, d) is defined as (a * b) - (c * d).

Given an integer array nums, choose four distinct indices w, x, y, and z such that the product difference between pairs (nums[w], nums[x]) and (nums[y], nums[z]) is maximized.

@@ -32,45 +30,30 @@ tags:

Return the maximum such product difference.

 

-

Example 1:

-
 Input: nums = [5,6,2,7,4]
-
 Output: 34
-
 Explanation: We can choose indices 1 and 3 for the first pair (6, 7) and indices 2 and 4 for the second pair (2, 4).
-
 The product difference is (6 * 7) - (2 * 4) = 34.
-
 

Example 2:

-
 Input: nums = [4,2,5,9,7,4,8]
-
 Output: 64
-
 Explanation: We can choose indices 3 and 6 for the first pair (9, 8) and indices 1 and 5 for the second pair (2, 4).
-
 The product difference is (9 * 8) - (2 * 4) = 64.
-
 

 

-

Constraints:

diff --git a/solution/1900-1999/1914.Cyclically Rotating a Grid/README_EN.md b/solution/1900-1999/1914.Cyclically Rotating a Grid/README_EN.md index 2b5a5d14a34c7..fae78135d2d47 100644 --- a/solution/1900-1999/1914.Cyclically Rotating a Grid/README_EN.md +++ b/solution/1900-1999/1914.Cyclically Rotating a Grid/README_EN.md @@ -27,59 +27,37 @@ tags:

A cyclic rotation of the matrix is done by cyclically rotating each layer in the matrix. To cyclically rotate a layer once, each element in the layer will take the place of the adjacent element in the counter-clockwise direction. An example rotation is shown below:

- -

Return the matrix after applying k cyclic rotations to it.

 

-

Example 1:

- -
-
 Input: grid = [[40,10],[30,20]], k = 1
-
 Output: [[10,20],[40,30]]
-
 Explanation: The figures above represent the grid at every state.
-
 

Example 2:

-
-
 Input: grid = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]], k = 2
-
 Output: [[3,4,8,12],[2,11,10,16],[1,7,6,15],[5,9,13,14]]
-
 Explanation: The figures above represent the grid at every state.
-
 

 

-

Constraints:

diff --git a/solution/1900-1999/1915.Number of Wonderful Substrings/README_EN.md b/solution/1900-1999/1915.Number of Wonderful Substrings/README_EN.md index e6048268f5cc4..1db1ec7914388 100644 --- a/solution/1900-1999/1915.Number of Wonderful Substrings/README_EN.md +++ b/solution/1900-1999/1915.Number of Wonderful Substrings/README_EN.md @@ -24,9 +24,7 @@ tags:

A wonderful string is a string where at most one letter appears an odd number of times.

Given a string word that consists of the first ten lowercase English letters ('a' through 'j'), return the number of wonderful non-empty substrings in word. If the same substring appears multiple times in word, then count each occurrence separately.

@@ -34,83 +32,51 @@ tags:

A substring is a contiguous sequence of characters in a string.

 

-

Example 1:

-
 Input: word = "aba"
-
 Output: 4
-
 Explanation: The four wonderful substrings are underlined below:
-
 - "aba" -> "a"
-
 - "aba" -> "b"
-
 - "aba" -> "a"
-
 - "aba" -> "aba"
-
 

Example 2:

-
 Input: word = "aabb"
-
 Output: 9
-
 Explanation: The nine wonderful substrings are underlined below:
-
 - "aabb" -> "a"
-
 - "aabb" -> "aa"
-
 - "aabb" -> "aab"
-
 - "aabb" -> "aabb"
-
 - "aabb" -> "a"
-
 - "aabb" -> "abb"
-
 - "aabb" -> "b"
-
 - "aabb" -> "bb"
-
 - "aabb" -> "b"
-
 

Example 3:

-
 Input: word = "he"
-
 Output: 2
-
 Explanation: The two wonderful substrings are underlined below:
-
 - "he" -> "h"
-
 - "he" -> "e"
-
 

 

-

Constraints:

diff --git a/solution/1900-1999/1916.Count Ways to Build Rooms in an Ant Colony/README_EN.md b/solution/1900-1999/1916.Count Ways to Build Rooms in an Ant Colony/README_EN.md index f306ec994eb05..08e8d9fb13bfc 100644 --- a/solution/1900-1999/1916.Count Ways to Build Rooms in an Ant Colony/README_EN.md +++ b/solution/1900-1999/1916.Count Ways to Build Rooms in an Ant Colony/README_EN.md @@ -30,65 +30,39 @@ tags:

Return the number of different orders you can build all the rooms in. Since the answer may be large, return it modulo 109 + 7.

 

-

Example 1:

- -
-
 Input: prevRoom = [-1,0,1]
-
 Output: 1
-
 Explanation: There is only one way to build the additional rooms: 0 → 1 → 2
-
 

Example 2:

-
-
 Input: prevRoom = [-1,0,0,1,2]
-
 Output: 6
-
 Explanation:
-
 The 6 ways are:
-
 0 → 1 → 3 → 2 → 4
-
 0 → 2 → 4 → 1 → 3
-
 0 → 1 → 2 → 3 → 4
-
 0 → 1 → 2 → 4 → 3
-
 0 → 2 → 1 → 3 → 4
-
 0 → 2 → 1 → 4 → 3
-
 

 

-

Constraints:

diff --git a/solution/2000-2099/2067.Number of Equal Count Substrings/README.md b/solution/2000-2099/2067.Number of Equal Count Substrings/README.md index 6e645f6e5a575..c791886cf86cb 100644 --- a/solution/2000-2099/2067.Number of Equal Count Substrings/README.md +++ b/solution/2000-2099/2067.Number of Equal Count Substrings/README.md @@ -3,9 +3,10 @@ comments: true difficulty: 中等 edit_url: https://github.com/doocs/leetcode/edit/main/solution/2000-2099/2067.Number%20of%20Equal%20Count%20Substrings/README.md tags: + - 哈希表 - 字符串 - 计数 - - 前缀和 + - 滑动窗口 --- diff --git a/solution/2000-2099/2067.Number of Equal Count Substrings/README_EN.md b/solution/2000-2099/2067.Number of Equal Count Substrings/README_EN.md index 2abb7b46e1175..15f56dea32ab1 100644 --- a/solution/2000-2099/2067.Number of Equal Count Substrings/README_EN.md +++ b/solution/2000-2099/2067.Number of Equal Count Substrings/README_EN.md @@ -3,9 +3,10 @@ comments: true difficulty: Medium edit_url: https://github.com/doocs/leetcode/edit/main/solution/2000-2099/2067.Number%20of%20Equal%20Count%20Substrings/README_EN.md tags: + - Hash Table - String - Counting - - Prefix Sum + - Sliding Window --- diff --git a/solution/3400-3499/3434.Maximum Frequency After Subarray Operation/README.md b/solution/3400-3499/3434.Maximum Frequency After Subarray Operation/README.md index 6c882c3b5fe00..315e4e0c1d1af 100644 --- a/solution/3400-3499/3434.Maximum Frequency After Subarray Operation/README.md +++ b/solution/3400-3499/3434.Maximum Frequency After Subarray Operation/README.md @@ -9,6 +9,7 @@ tags: - 数组 - 哈希表 - 动态规划 + - 枚举 - 前缀和 --- diff --git a/solution/3400-3499/3434.Maximum Frequency After Subarray Operation/README_EN.md b/solution/3400-3499/3434.Maximum Frequency After Subarray Operation/README_EN.md index 7ace03e03f209..ce9706fef6c1b 100644 --- a/solution/3400-3499/3434.Maximum Frequency After Subarray Operation/README_EN.md +++ b/solution/3400-3499/3434.Maximum Frequency After Subarray Operation/README_EN.md @@ -9,6 +9,7 @@ tags: - Array - Hash Table - Dynamic Programming + - Enumeration - Prefix Sum --- diff --git a/solution/3400-3499/3496.Maximize Score After Pair Deletions/README.md b/solution/3400-3499/3496.Maximize Score After Pair Deletions/README.md new file mode 100644 index 0000000000000..2d28374ba4b3b --- /dev/null +++ b/solution/3400-3499/3496.Maximize Score After Pair Deletions/README.md @@ -0,0 +1,201 @@ +--- +comments: true +difficulty: 中等 +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3496.Maximize%20Score%20After%20Pair%20Deletions/README.md +--- + + + +# [3496. 最大化配对删除后的得分 🔒](https://leetcode.cn/problems/maximize-score-after-pair-deletions) + +[English Version](/solution/3400-3499/3496.Maximize%20Score%20After%20Pair%20Deletions/README_EN.md) + +## 题目描述 + + + +

给定一个整数数组 nums。当数组中元素超过两个时,你 必须 重复执行以下操作中的一个:

+ + + +

对于每次操作,将移除的元素之和加到你的总分上。

+ +

返回你可以达到的 最高 分数。

+ +

 

+ +

示例 1:

+ +
+

输入:nums = [2,4,1]

+ +

输出:6

+ +

解释:

+ +

可能的操作有:

+ + + +

通过删除最前面的两个元素可以得到最高分,因此最终分数是 6。

+
+ +

示例 2:

+ +
+

输入:nums = [5,-1,4,2]

+ +

输出:7

+ +

解释:

+ +

可能的操作是:

+ + + +

通过删除第一个和最后一个元素可以得到最高分,因此最终分数是 7。

+
+ +

 

+ +

提示:

+ + + + + +## 解法 + + + +### 方法一:逆向思维 + +根据题目描述,每次操作会移除掉端点的两个元素。因此,当元素个数为奇数时,最终会剩下 1 个元素;当元素个数为偶数时,最终会剩下数组中的连续两个元素。 + +为了使得删除后的得分最大化,我们应该使得剩下的元素最小。 + +因此,如果数组 $\textit{nums}$ 元素个数为奇数,那么答案就是数组 $\textit{nums}$ 所有元素的总和 $s$,减去数组 $\textit{nums}$ 中的最小值 $\textit{mi}$;如果数组 $\textit{nums}$ 元素个数为偶数,那么答案就是数组 $\textit{nums}$ 所有元素的总和 $s$,减去数组连续两个元素之和的最小值。 + +时间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。 + + + +#### Python3 + +```python +class Solution: + def maxScore(self, nums: List[int]) -> int: + s = sum(nums) + if len(nums) & 1: + return s - min(nums) + return s - min(a + b for a, b in pairwise(nums)) +``` + +#### Java + +```java +class Solution { + public int maxScore(int[] nums) { + final int inf = 1 << 30; + int n = nums.length; + int s = 0, mi = inf; + int t = inf; + for (int i = 0; i < n; ++i) { + s += nums[i]; + mi = Math.min(mi, nums[i]); + if (i + 1 < n) { + t = Math.min(t, nums[i] + nums[i + 1]); + } + } + if (n % 2 == 1) { + return s - mi; + } + return s - t; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + int maxScore(vector& nums) { + const int inf = 1 << 30; + int n = nums.size(); + int s = 0, mi = inf; + int t = inf; + for (int i = 0; i < n; ++i) { + s += nums[i]; + mi = min(mi, nums[i]); + if (i + 1 < n) { + t = min(t, nums[i] + nums[i + 1]); + } + } + if (n % 2 == 1) { + return s - mi; + } + return s - t; + } +}; +``` + +#### Go + +```go +func maxScore(nums []int) int { + const inf = 1 << 30 + n := len(nums) + s, mi, t := 0, inf, inf + for i, x := range nums { + s += x + mi = min(mi, x) + if i+1 < n { + t = min(t, x+nums[i+1]) + } + } + if n%2 == 1 { + return s - mi + } + return s - t +} +``` + +#### TypeScript + +```ts +function maxScore(nums: number[]): number { + const inf = Infinity; + const n = nums.length; + let [s, mi, t] = [0, inf, inf]; + for (let i = 0; i < n; ++i) { + s += nums[i]; + mi = Math.min(mi, nums[i]); + if (i + 1 < n) { + t = Math.min(t, nums[i] + nums[i + 1]); + } + } + return n % 2 ? s - mi : s - t; +} +``` + + + + + + diff --git a/solution/3400-3499/3496.Maximize Score After Pair Deletions/README_EN.md b/solution/3400-3499/3496.Maximize Score After Pair Deletions/README_EN.md new file mode 100644 index 0000000000000..16f79e3644d4e --- /dev/null +++ b/solution/3400-3499/3496.Maximize Score After Pair Deletions/README_EN.md @@ -0,0 +1,199 @@ +--- +comments: true +difficulty: Medium +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3496.Maximize%20Score%20After%20Pair%20Deletions/README_EN.md +--- + + + +# [3496. Maximize Score After Pair Deletions 🔒](https://leetcode.com/problems/maximize-score-after-pair-deletions) + +[中文文档](/solution/3400-3499/3496.Maximize%20Score%20After%20Pair%20Deletions/README.md) + +## Description + + + +

You are given an array of integers nums. You must repeatedly perform one of the following operations while the array has more than two elements:

+ + + +

For each operation, add the sum of the removed elements to your total score.

+ +

Return the maximum possible score you can achieve.

+ +

 

+

Example 1:

+ +
+

Input: nums = [2,4,1]

+ +

Output: 6

+ +

Explanation:

+ +

The possible operations are:

+ +
    +
  • Remove the first two elements (2 + 4) = 6. The remaining array is [1].
  • +
  • Remove the last two elements (4 + 1) = 5. The remaining array is [2].
  • +
  • Remove the first and last elements (2 + 1) = 3. The remaining array is [4].
  • +
+ +

The maximum score is obtained by removing the first two elements, resulting in a final score of 6.

+
+ +

Example 2:

+ +
+

Input: nums = [5,-1,4,2]

+ +

Output: 7

+ +

Explanation:

+ +

The possible operations are:

+ +
    +
  • Remove the first and last elements (5 + 2) = 7. The remaining array is [-1, 4].
  • +
  • Remove the first two elements (5 + -1) = 4. The remaining array is [4, 2].
  • +
  • Remove the last two elements (4 + 2) = 6. The remaining array is [5, -1].
  • +
+ +

The maximum score is obtained by removing the first and last elements, resulting in a total score of 7.

+
+ +

 

+

Constraints:

+ + + + + +## Solutions + + + +### Solution 1: Reverse Thinking + +According to the problem description, each operation removes the two elements at the endpoints. Therefore, when the number of elements is odd, one element will eventually remain; when the number of elements is even, two consecutive elements in the array will eventually remain. + +To maximize the score after deletions, we should minimize the remaining elements. + +Thus, if the array $\textit{nums}$ has an odd number of elements, the answer is the sum of all elements $s$ in the array $\textit{nums}$ minus the minimum value $\textit{mi}$ in $\textit{nums}$; if the array $\textit{nums}$ has an even number of elements, the answer is the sum of all elements $s$ in the array $\textit{nums}$ minus the minimum sum of any two consecutive elements. + +The time complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$. The space complexity is $O(1)$. + + + +#### Python3 + +```python +class Solution: + def maxScore(self, nums: List[int]) -> int: + s = sum(nums) + if len(nums) & 1: + return s - min(nums) + return s - min(a + b for a, b in pairwise(nums)) +``` + +#### Java + +```java +class Solution { + public int maxScore(int[] nums) { + final int inf = 1 << 30; + int n = nums.length; + int s = 0, mi = inf; + int t = inf; + for (int i = 0; i < n; ++i) { + s += nums[i]; + mi = Math.min(mi, nums[i]); + if (i + 1 < n) { + t = Math.min(t, nums[i] + nums[i + 1]); + } + } + if (n % 2 == 1) { + return s - mi; + } + return s - t; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + int maxScore(vector& nums) { + const int inf = 1 << 30; + int n = nums.size(); + int s = 0, mi = inf; + int t = inf; + for (int i = 0; i < n; ++i) { + s += nums[i]; + mi = min(mi, nums[i]); + if (i + 1 < n) { + t = min(t, nums[i] + nums[i + 1]); + } + } + if (n % 2 == 1) { + return s - mi; + } + return s - t; + } +}; +``` + +#### Go + +```go +func maxScore(nums []int) int { + const inf = 1 << 30 + n := len(nums) + s, mi, t := 0, inf, inf + for i, x := range nums { + s += x + mi = min(mi, x) + if i+1 < n { + t = min(t, x+nums[i+1]) + } + } + if n%2 == 1 { + return s - mi + } + return s - t +} +``` + +#### TypeScript + +```ts +function maxScore(nums: number[]): number { + const inf = Infinity; + const n = nums.length; + let [s, mi, t] = [0, inf, inf]; + for (let i = 0; i < n; ++i) { + s += nums[i]; + mi = Math.min(mi, nums[i]); + if (i + 1 < n) { + t = Math.min(t, nums[i] + nums[i + 1]); + } + } + return n % 2 ? s - mi : s - t; +} +``` + + + + + + diff --git a/solution/3400-3499/3496.Maximize Score After Pair Deletions/Solution.cpp b/solution/3400-3499/3496.Maximize Score After Pair Deletions/Solution.cpp new file mode 100644 index 0000000000000..8924180d576da --- /dev/null +++ b/solution/3400-3499/3496.Maximize Score After Pair Deletions/Solution.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + int maxScore(vector& nums) { + const int inf = 1 << 30; + int n = nums.size(); + int s = 0, mi = inf; + int t = inf; + for (int i = 0; i < n; ++i) { + s += nums[i]; + mi = min(mi, nums[i]); + if (i + 1 < n) { + t = min(t, nums[i] + nums[i + 1]); + } + } + if (n % 2 == 1) { + return s - mi; + } + return s - t; + } +}; \ No newline at end of file diff --git a/solution/3400-3499/3496.Maximize Score After Pair Deletions/Solution.go b/solution/3400-3499/3496.Maximize Score After Pair Deletions/Solution.go new file mode 100644 index 0000000000000..bd2a58441b914 --- /dev/null +++ b/solution/3400-3499/3496.Maximize Score After Pair Deletions/Solution.go @@ -0,0 +1,16 @@ +func maxScore(nums []int) int { + const inf = 1 << 30 + n := len(nums) + s, mi, t := 0, inf, inf + for i, x := range nums { + s += x + mi = min(mi, x) + if i+1 < n { + t = min(t, x+nums[i+1]) + } + } + if n%2 == 1 { + return s - mi + } + return s - t +} \ No newline at end of file diff --git a/solution/3400-3499/3496.Maximize Score After Pair Deletions/Solution.java b/solution/3400-3499/3496.Maximize Score After Pair Deletions/Solution.java new file mode 100644 index 0000000000000..07925d2017a55 --- /dev/null +++ b/solution/3400-3499/3496.Maximize Score After Pair Deletions/Solution.java @@ -0,0 +1,19 @@ +class Solution { + public int maxScore(int[] nums) { + final int inf = 1 << 30; + int n = nums.length; + int s = 0, mi = inf; + int t = inf; + for (int i = 0; i < n; ++i) { + s += nums[i]; + mi = Math.min(mi, nums[i]); + if (i + 1 < n) { + t = Math.min(t, nums[i] + nums[i + 1]); + } + } + if (n % 2 == 1) { + return s - mi; + } + return s - t; + } +} \ No newline at end of file diff --git a/solution/3400-3499/3496.Maximize Score After Pair Deletions/Solution.py b/solution/3400-3499/3496.Maximize Score After Pair Deletions/Solution.py new file mode 100644 index 0000000000000..606f35b87bf5d --- /dev/null +++ b/solution/3400-3499/3496.Maximize Score After Pair Deletions/Solution.py @@ -0,0 +1,6 @@ +class Solution: + def maxScore(self, nums: List[int]) -> int: + s = sum(nums) + if len(nums) & 1: + return s - min(nums) + return s - min(a + b for a, b in pairwise(nums)) diff --git a/solution/3400-3499/3496.Maximize Score After Pair Deletions/Solution.ts b/solution/3400-3499/3496.Maximize Score After Pair Deletions/Solution.ts new file mode 100644 index 0000000000000..d7ea0f7de56d5 --- /dev/null +++ b/solution/3400-3499/3496.Maximize Score After Pair Deletions/Solution.ts @@ -0,0 +1,13 @@ +function maxScore(nums: number[]): number { + const inf = Infinity; + const n = nums.length; + let [s, mi, t] = [0, inf, inf]; + for (let i = 0; i < n; ++i) { + s += nums[i]; + mi = Math.min(mi, nums[i]); + if (i + 1 < n) { + t = Math.min(t, nums[i] + nums[i + 1]); + } + } + return n % 2 ? s - mi : s - t; +} diff --git a/solution/README.md b/solution/README.md index deabb57a91228..23f5850dc2508 100644 --- a/solution/README.md +++ b/solution/README.md @@ -2077,7 +2077,7 @@ | 2064 | [分配给商店的最多商品的最小值](/solution/2000-2099/2064.Minimized%20Maximum%20of%20Products%20Distributed%20to%20Any%20Store/README.md) | `贪心`,`数组`,`二分查找` | 中等 | 第 266 场周赛 | | 2065 | [最大化一张图中的路径价值](/solution/2000-2099/2065.Maximum%20Path%20Quality%20of%20a%20Graph/README.md) | `图`,`数组`,`回溯` | 困难 | 第 266 场周赛 | | 2066 | [账户余额](/solution/2000-2099/2066.Account%20Balance/README.md) | `数据库` | 中等 | 🔒 | -| 2067 | [等计数子串的数量](/solution/2000-2099/2067.Number%20of%20Equal%20Count%20Substrings/README.md) | `字符串`,`计数`,`前缀和` | 中等 | 🔒 | +| 2067 | [等计数子串的数量](/solution/2000-2099/2067.Number%20of%20Equal%20Count%20Substrings/README.md) | `哈希表`,`字符串`,`计数`,`滑动窗口` | 中等 | 🔒 | | 2068 | [检查两个字符串是否几乎相等](/solution/2000-2099/2068.Check%20Whether%20Two%20Strings%20are%20Almost%20Equivalent/README.md) | `哈希表`,`字符串`,`计数` | 简单 | 第 65 场双周赛 | | 2069 | [模拟行走机器人 II](/solution/2000-2099/2069.Walking%20Robot%20Simulation%20II/README.md) | `设计`,`模拟` | 中等 | 第 65 场双周赛 | | 2070 | [每一个查询的最大美丽值](/solution/2000-2099/2070.Most%20Beautiful%20Item%20for%20Each%20Query/README.md) | `数组`,`二分查找`,`排序` | 中等 | 第 65 场双周赛 | @@ -3444,7 +3444,7 @@ | 3431 | [对数字排序的最小解锁下标](/solution/3400-3499/3431.Minimum%20Unlocked%20Indices%20to%20Sort%20Nums/README.md) | `数组`,`哈希表` | 中等 | 🔒 | | 3432 | [统计元素和差值为偶数的分区方案](/solution/3400-3499/3432.Count%20Partitions%20with%20Even%20Sum%20Difference/README.md) | `数组`,`数学`,`前缀和` | 简单 | 第 434 场周赛 | | 3433 | [统计用户被提及情况](/solution/3400-3499/3433.Count%20Mentions%20Per%20User/README.md) | `数组`,`数学`,`排序`,`模拟` | 中等 | 第 434 场周赛 | -| 3434 | [子数组操作后的最大频率](/solution/3400-3499/3434.Maximum%20Frequency%20After%20Subarray%20Operation/README.md) | `贪心`,`数组`,`哈希表`,`动态规划`,`前缀和` | 中等 | 第 434 场周赛 | +| 3434 | [子数组操作后的最大频率](/solution/3400-3499/3434.Maximum%20Frequency%20After%20Subarray%20Operation/README.md) | `贪心`,`数组`,`哈希表`,`动态规划`,`枚举`,`前缀和` | 中等 | 第 434 场周赛 | | 3435 | [最短公共超序列的字母出现频率](/solution/3400-3499/3435.Frequencies%20of%20Shortest%20Supersequences/README.md) | `位运算`,`图`,`拓扑排序`,`数组`,`字符串`,`枚举` | 困难 | 第 434 场周赛 | | 3436 | [查找合法邮箱](/solution/3400-3499/3436.Find%20Valid%20Emails/README.md) | `数据库` | 简单 | | | 3437 | [全排列 III](/solution/3400-3499/3437.Permutations%20III/README.md) | `数组`,`回溯` | 中等 | 🔒 | @@ -3506,6 +3506,7 @@ | 3493 | [属性图](/solution/3400-3499/3493.Properties%20Graph/README.md) | `深度优先搜索`,`广度优先搜索`,`并查集`,`图`,`数组`,`哈希表` | 中等 | 第 442 场周赛 | | 3494 | [酿造药水需要的最少总时间](/solution/3400-3499/3494.Find%20the%20Minimum%20Amount%20of%20Time%20to%20Brew%20Potions/README.md) | `数组`,`前缀和`,`模拟` | 中等 | 第 442 场周赛 | | 3495 | [使数组元素都变为零的最少操作次数](/solution/3400-3499/3495.Minimum%20Operations%20to%20Make%20Array%20Elements%20Zero/README.md) | `位运算`,`数组`,`数学` | 困难 | 第 442 场周赛 | +| 3496 | [最大化配对删除后的得分](/solution/3400-3499/3496.Maximize%20Score%20After%20Pair%20Deletions/README.md) | | 中等 | 🔒 | ## 版权 diff --git a/solution/README_EN.md b/solution/README_EN.md index 2904bc8fc4c9d..1eb98bfc53173 100644 --- a/solution/README_EN.md +++ b/solution/README_EN.md @@ -2075,7 +2075,7 @@ Press Control + F(or Command + F on | 2064 | [Minimized Maximum of Products Distributed to Any Store](/solution/2000-2099/2064.Minimized%20Maximum%20of%20Products%20Distributed%20to%20Any%20Store/README_EN.md) | `Greedy`,`Array`,`Binary Search` | Medium | Weekly Contest 266 | | 2065 | [Maximum Path Quality of a Graph](/solution/2000-2099/2065.Maximum%20Path%20Quality%20of%20a%20Graph/README_EN.md) | `Graph`,`Array`,`Backtracking` | Hard | Weekly Contest 266 | | 2066 | [Account Balance](/solution/2000-2099/2066.Account%20Balance/README_EN.md) | `Database` | Medium | 🔒 | -| 2067 | [Number of Equal Count Substrings](/solution/2000-2099/2067.Number%20of%20Equal%20Count%20Substrings/README_EN.md) | `String`,`Counting`,`Prefix Sum` | Medium | 🔒 | +| 2067 | [Number of Equal Count Substrings](/solution/2000-2099/2067.Number%20of%20Equal%20Count%20Substrings/README_EN.md) | `Hash Table`,`String`,`Counting`,`Sliding Window` | Medium | 🔒 | | 2068 | [Check Whether Two Strings are Almost Equivalent](/solution/2000-2099/2068.Check%20Whether%20Two%20Strings%20are%20Almost%20Equivalent/README_EN.md) | `Hash Table`,`String`,`Counting` | Easy | Biweekly Contest 65 | | 2069 | [Walking Robot Simulation II](/solution/2000-2099/2069.Walking%20Robot%20Simulation%20II/README_EN.md) | `Design`,`Simulation` | Medium | Biweekly Contest 65 | | 2070 | [Most Beautiful Item for Each Query](/solution/2000-2099/2070.Most%20Beautiful%20Item%20for%20Each%20Query/README_EN.md) | `Array`,`Binary Search`,`Sorting` | Medium | Biweekly Contest 65 | @@ -3442,7 +3442,7 @@ Press Control + F(or Command + F on | 3431 | [Minimum Unlocked Indices to Sort Nums](/solution/3400-3499/3431.Minimum%20Unlocked%20Indices%20to%20Sort%20Nums/README_EN.md) | `Array`,`Hash Table` | Medium | 🔒 | | 3432 | [Count Partitions with Even Sum Difference](/solution/3400-3499/3432.Count%20Partitions%20with%20Even%20Sum%20Difference/README_EN.md) | `Array`,`Math`,`Prefix Sum` | Easy | Weekly Contest 434 | | 3433 | [Count Mentions Per User](/solution/3400-3499/3433.Count%20Mentions%20Per%20User/README_EN.md) | `Array`,`Math`,`Sorting`,`Simulation` | Medium | Weekly Contest 434 | -| 3434 | [Maximum Frequency After Subarray Operation](/solution/3400-3499/3434.Maximum%20Frequency%20After%20Subarray%20Operation/README_EN.md) | `Greedy`,`Array`,`Hash Table`,`Dynamic Programming`,`Prefix Sum` | Medium | Weekly Contest 434 | +| 3434 | [Maximum Frequency After Subarray Operation](/solution/3400-3499/3434.Maximum%20Frequency%20After%20Subarray%20Operation/README_EN.md) | `Greedy`,`Array`,`Hash Table`,`Dynamic Programming`,`Enumeration`,`Prefix Sum` | Medium | Weekly Contest 434 | | 3435 | [Frequencies of Shortest Supersequences](/solution/3400-3499/3435.Frequencies%20of%20Shortest%20Supersequences/README_EN.md) | `Bit Manipulation`,`Graph`,`Topological Sort`,`Array`,`String`,`Enumeration` | Hard | Weekly Contest 434 | | 3436 | [Find Valid Emails](/solution/3400-3499/3436.Find%20Valid%20Emails/README_EN.md) | `Database` | Easy | | | 3437 | [Permutations III](/solution/3400-3499/3437.Permutations%20III/README_EN.md) | `Array`,`Backtracking` | Medium | 🔒 | @@ -3504,6 +3504,7 @@ Press Control + F(or Command + F on | 3493 | [Properties Graph](/solution/3400-3499/3493.Properties%20Graph/README_EN.md) | `Depth-First Search`,`Breadth-First Search`,`Union Find`,`Graph`,`Array`,`Hash Table` | Medium | Weekly Contest 442 | | 3494 | [Find the Minimum Amount of Time to Brew Potions](/solution/3400-3499/3494.Find%20the%20Minimum%20Amount%20of%20Time%20to%20Brew%20Potions/README_EN.md) | `Array`,`Prefix Sum`,`Simulation` | Medium | Weekly Contest 442 | | 3495 | [Minimum Operations to Make Array Elements Zero](/solution/3400-3499/3495.Minimum%20Operations%20to%20Make%20Array%20Elements%20Zero/README_EN.md) | `Bit Manipulation`,`Array`,`Math` | Hard | Weekly Contest 442 | +| 3496 | [Maximize Score After Pair Deletions](/solution/3400-3499/3496.Maximize%20Score%20After%20Pair%20Deletions/README_EN.md) | | Medium | 🔒 | ## Copyright