diff --git a/solution/1900-1999/1925.Count Square Sum Triples/README.md b/solution/1900-1999/1925.Count Square Sum Triples/README.md
new file mode 100644
index 0000000000000..698c32367e88f
--- /dev/null
+++ b/solution/1900-1999/1925.Count Square Sum Triples/README.md
@@ -0,0 +1,128 @@
+# [1925. 统计平方和三元组的数目](https://leetcode-cn.com/problems/count-square-sum-triples)
+
+[English Version](/solution/1900-1999/1925.Count%20Square%20Sum%20Triples/README_EN.md)
+
+## 题目描述
+
+
+
+
一个 平方和三元组 (a,b,c)
指的是满足 a2 + b2 = c2
的 整数 三元组 a
,b
和 c
。
+
+给你一个整数 n
,请你返回满足 1 <= a, b, c <= n
的 平方和三元组 的数目。
+
+
+
+示例 1:
+
+输入:n = 5
+输出:2
+解释:平方和三元组为 (3,4,5) 和 (4,3,5) 。
+
+
+示例 2:
+
+输入:n = 10
+输出:4
+解释:平方和三元组为 (3,4,5),(4,3,5),(6,8,10) 和 (8,6,10) 。
+
+
+
+
+提示:
+
+
+
+
+## 解法
+
+
+
+
+
+### **Python3**
+
+
+
+```python
+class Solution:
+ def countTriples(self, n: int) -> int:
+ res = 0
+ for a in range(1, n + 1):
+ for b in range(1, n + 1):
+ t = a ** 2 + b ** 2
+ c = int(sqrt(t))
+ if c <= n and c ** 2 == t:
+ res += 1
+ return res
+```
+
+### **Java**
+
+
+
+```java
+class Solution {
+ public int countTriples(int n) {
+ int res = 0;
+ for (int a = 1; a <= n; ++a) {
+ for (int b = 1; b <= n; ++b) {
+ int t = a * a + b * b;
+ int c = (int) Math.sqrt(t);
+ if (c <= n && c * c == t) {
+ ++res;
+ }
+ }
+ }
+ return res;
+ }
+}
+```
+
+### **C++**
+
+```cpp
+class Solution {
+public:
+ int countTriples(int n) {
+ int res = 0;
+ for (int a = 1; a <= n; ++a) {
+ for (int b = 1; b <= n; ++b) {
+ int t = a * a + b * b;
+ int c = (int) sqrt(t);
+ if (c <= n && c * c == t) {
+ ++res;
+ }
+ }
+ }
+ return res;
+ }
+};
+```
+
+### **Go**
+
+```go
+func countTriples(n int) int {
+ res := 0
+ for a := 1; a <= n; a++ {
+ for b := 1; b <= n; b++ {
+ t := a*a + b*b
+ c := int(math.Sqrt(float64(t)))
+ if c <= n && c*c == t {
+ res++
+ }
+ }
+ }
+ return res
+}
+```
+
+### **...**
+
+```
+
+```
+
+
diff --git a/solution/1900-1999/1925.Count Square Sum Triples/README_EN.md b/solution/1900-1999/1925.Count Square Sum Triples/README_EN.md
new file mode 100644
index 0000000000000..b9e5731991703
--- /dev/null
+++ b/solution/1900-1999/1925.Count Square Sum Triples/README_EN.md
@@ -0,0 +1,120 @@
+# [1925. Count Square Sum Triples](https://leetcode.com/problems/count-square-sum-triples)
+
+[中文文档](/solution/1900-1999/1925.Count%20Square%20Sum%20Triples/README.md)
+
+## Description
+
+A square triple (a,b,c)
is a triple where a
, b
, and c
are integers and a2 + b2 = c2
.
+
+Given an integer n
, return the number of square triples such that 1 <= a, b, c <= n
.
+
+
+Example 1:
+
+
+Input: n = 5
+Output: 2
+Explanation: The square triples are (3,4,5) and (4,3,5).
+
+
+Example 2:
+
+
+Input: n = 10
+Output: 4
+Explanation: The square triples are (3,4,5), (4,3,5), (6,8,10), and (8,6,10).
+
+
+
+Constraints:
+
+
+
+
+## Solutions
+
+
+
+### **Python3**
+
+```python
+class Solution:
+ def countTriples(self, n: int) -> int:
+ res = 0
+ for a in range(1, n + 1):
+ for b in range(1, n + 1):
+ t = a ** 2 + b ** 2
+ c = int(sqrt(t))
+ if c <= n and c ** 2 == t:
+ res += 1
+ return res
+```
+
+### **Java**
+
+```java
+class Solution {
+ public int countTriples(int n) {
+ int res = 0;
+ for (int a = 1; a <= n; ++a) {
+ for (int b = 1; b <= n; ++b) {
+ int t = a * a + b * b;
+ int c = (int) Math.sqrt(t);
+ if (c <= n && c * c == t) {
+ ++res;
+ }
+ }
+ }
+ return res;
+ }
+}
+```
+
+### **C++**
+
+```cpp
+class Solution {
+public:
+ int countTriples(int n) {
+ int res = 0;
+ for (int a = 1; a <= n; ++a) {
+ for (int b = 1; b <= n; ++b) {
+ int t = a * a + b * b;
+ int c = (int) sqrt(t);
+ if (c <= n && c * c == t) {
+ ++res;
+ }
+ }
+ }
+ return res;
+ }
+};
+```
+
+### **Go**
+
+```go
+func countTriples(n int) int {
+ res := 0
+ for a := 1; a <= n; a++ {
+ for b := 1; b <= n; b++ {
+ t := a*a + b*b
+ c := int(math.Sqrt(float64(t)))
+ if c <= n && c*c == t {
+ res++
+ }
+ }
+ }
+ return res
+}
+```
+
+### **...**
+
+```
+
+```
+
+
diff --git a/solution/1900-1999/1925.Count Square Sum Triples/Solution.cpp b/solution/1900-1999/1925.Count Square Sum Triples/Solution.cpp
new file mode 100644
index 0000000000000..89e0a390ffca4
--- /dev/null
+++ b/solution/1900-1999/1925.Count Square Sum Triples/Solution.cpp
@@ -0,0 +1,16 @@
+class Solution {
+public:
+ int countTriples(int n) {
+ int res = 0;
+ for (int a = 1; a <= n; ++a) {
+ for (int b = 1; b <= n; ++b) {
+ int t = a * a + b * b;
+ int c = (int) sqrt(t);
+ if (c <= n && c * c == t) {
+ ++res;
+ }
+ }
+ }
+ return res;
+ }
+};
\ No newline at end of file
diff --git a/solution/1900-1999/1925.Count Square Sum Triples/Solution.go b/solution/1900-1999/1925.Count Square Sum Triples/Solution.go
new file mode 100644
index 0000000000000..855674718cfe2
--- /dev/null
+++ b/solution/1900-1999/1925.Count Square Sum Triples/Solution.go
@@ -0,0 +1,13 @@
+func countTriples(n int) int {
+ res := 0
+ for a := 1; a <= n; a++ {
+ for b := 1; b <= n; b++ {
+ t := a*a + b*b
+ c := int(math.Sqrt(float64(t)))
+ if c <= n && c*c == t {
+ res++
+ }
+ }
+ }
+ return res
+}
\ No newline at end of file
diff --git a/solution/1900-1999/1925.Count Square Sum Triples/Solution.java b/solution/1900-1999/1925.Count Square Sum Triples/Solution.java
new file mode 100644
index 0000000000000..134a540237133
--- /dev/null
+++ b/solution/1900-1999/1925.Count Square Sum Triples/Solution.java
@@ -0,0 +1,15 @@
+class Solution {
+ public int countTriples(int n) {
+ int res = 0;
+ for (int a = 1; a <= n; ++a) {
+ for (int b = 1; b <= n; ++b) {
+ int t = a * a + b * b;
+ int c = (int) Math.sqrt(t);
+ if (c <= n && c * c == t) {
+ ++res;
+ }
+ }
+ }
+ return res;
+ }
+}
\ No newline at end of file
diff --git a/solution/1900-1999/1925.Count Square Sum Triples/Solution.py b/solution/1900-1999/1925.Count Square Sum Triples/Solution.py
new file mode 100644
index 0000000000000..1a62ed08ee8ea
--- /dev/null
+++ b/solution/1900-1999/1925.Count Square Sum Triples/Solution.py
@@ -0,0 +1,10 @@
+class Solution:
+ def countTriples(self, n: int) -> int:
+ res = 0
+ for a in range(1, n + 1):
+ for b in range(1, n + 1):
+ t = a ** 2 + b ** 2
+ c = int(sqrt(t))
+ if c <= n and c ** 2 == t:
+ res += 1
+ return res
diff --git a/solution/1900-1999/1926.Nearest Exit from Entrance in Maze/README.md b/solution/1900-1999/1926.Nearest Exit from Entrance in Maze/README.md
new file mode 100644
index 0000000000000..6951063647918
--- /dev/null
+++ b/solution/1900-1999/1926.Nearest Exit from Entrance in Maze/README.md
@@ -0,0 +1,91 @@
+# [1926. 迷宫中离入口最近的出口](https://leetcode-cn.com/problems/nearest-exit-from-entrance-in-maze)
+
+[English Version](/solution/1900-1999/1926.Nearest%20Exit%20from%20Entrance%20in%20Maze/README_EN.md)
+
+## 题目描述
+
+
+
+给你一个 m x n
的迷宫矩阵 maze
(下标从 0 开始),矩阵中有空格子(用 '.'
表示)和墙(用 '+'
表示)。同时给你迷宫的入口 entrance
,用 entrance = [entrancerow, entrancecol]
表示你一开始所在格子的行和列。
+
+每一步操作,你可以往 上,下,左 或者 右 移动一个格子。你不能进入墙所在的格子,你也不能离开迷宫。你的目标是找到离 entrance
最近 的出口。出口 的含义是 maze
边界 上的 空格子。entrance
格子 不算 出口。
+
+请你返回从 entrance
到最近出口的最短路径的 步数 ,如果不存在这样的路径,请你返回 -1
。
+
+
+
+示例 1:
+
+输入:maze = [["+","+",".","+"],[".",".",".","+"],["+","+","+","."]], entrance = [1,2]
+输出:1
+解释:总共有 3 个出口,分别位于 (1,0),(0,2) 和 (2,3) 。
+一开始,你在入口格子 (1,2) 处。
+- 你可以往左移动 2 步到达 (1,0) 。
+- 你可以往上移动 1 步到达 (0,2) 。
+从入口处没法到达 (2,3) 。
+所以,最近的出口是 (0,2) ,距离为 1 步。
+
+
+示例 2:
+
+输入:maze = [["+","+","+"],[".",".","."],["+","+","+"]], entrance = [1,0]
+输出:2
+解释:迷宫中只有 1 个出口,在 (1,2) 处。
+(1,0) 不算出口,因为它是入口格子。
+初始时,你在入口与格子 (1,0) 处。
+- 你可以往右移动 2 步到达 (1,2) 处。
+所以,最近的出口为 (1,2) ,距离为 2 步。
+
+
+示例 3:
+
+输入:maze = [[".","+"]], entrance = [0,0]
+输出:-1
+解释:这个迷宫中没有出口。
+
+
+
+
+提示:
+
+
+ maze.length == m
+ maze[i].length == n
+ 1 <= m, n <= 100
+ maze[i][j]
要么是 '.'
,要么是 '+'
。
+ entrance.length == 2
+ 0 <= entrancerow < m
+ 0 <= entrancecol < n
+ entrance
一定是空格子。
+
+
+
+## 解法
+
+
+
+
+
+### **Python3**
+
+
+
+```python
+
+```
+
+### **Java**
+
+
+
+```java
+
+```
+
+### **...**
+
+```
+
+```
+
+
diff --git a/solution/1900-1999/1926.Nearest Exit from Entrance in Maze/README_EN.md b/solution/1900-1999/1926.Nearest Exit from Entrance in Maze/README_EN.md
new file mode 100644
index 0000000000000..08fa2c52f26b8
--- /dev/null
+++ b/solution/1900-1999/1926.Nearest Exit from Entrance in Maze/README_EN.md
@@ -0,0 +1,84 @@
+# [1926. Nearest Exit from Entrance in Maze](https://leetcode.com/problems/nearest-exit-from-entrance-in-maze)
+
+[中文文档](/solution/1900-1999/1926.Nearest%20Exit%20from%20Entrance%20in%20Maze/README.md)
+
+## Description
+
+You are given an m x n
matrix maze
(0-indexed) with empty cells (represented as '.'
) and walls (represented as '+'
). You are also given the entrance
of the maze, where entrance = [entrancerow, entrancecol]
denotes the row and column of the cell you are initially standing at.
+
+In one step, you can move one cell up, down, left, or right. You cannot step into a cell with a wall, and you cannot step outside the maze. Your goal is to find the nearest exit from the entrance
. An exit is defined as an empty cell that is at the border of the maze
. The entrance
does not count as an exit.
+
+Return the number of steps in the shortest path from the entrance
to the nearest exit, or -1
if no such path exists.
+
+
+Example 1:
+
+
+Input: maze = [["+","+",".","+"],[".",".",".","+"],["+","+","+","."]], entrance = [1,2]
+Output: 1
+Explanation: There are 3 exits in this maze at [1,0], [0,2], and [2,3].
+Initially, you are at the entrance cell [1,2].
+- You can reach [1,0] by moving 2 steps left.
+- You can reach [0,2] by moving 1 step up.
+It is impossible to reach [2,3] from the entrance.
+Thus, the nearest exit is [0,2], which is 1 step away.
+
+
+Example 2:
+
+
+Input: maze = [["+","+","+"],[".",".","."],["+","+","+"]], entrance = [1,0]
+Output: 2
+Explanation: There is 1 exit in this maze at [1,2].
+[1,0] does not count as an exit since it is the entrance cell.
+Initially, you are at the entrance cell [1,0].
+- You can reach [1,2] by moving 2 steps right.
+Thus, the nearest exit is [1,2], which is 2 steps away.
+
+
+Example 3:
+
+
+Input: maze = [[".","+"]], entrance = [0,0]
+Output: -1
+Explanation: There are no exits in this maze.
+
+
+
+Constraints:
+
+
+ maze.length == m
+ maze[i].length == n
+ 1 <= m, n <= 100
+ maze[i][j]
is either '.'
or '+'
.
+ entrance.length == 2
+ 0 <= entrancerow < m
+ 0 <= entrancecol < n
+ entrance
will always be an empty cell.
+
+
+
+## Solutions
+
+
+
+### **Python3**
+
+```python
+
+```
+
+### **Java**
+
+```java
+
+```
+
+### **...**
+
+```
+
+```
+
+
diff --git a/solution/1900-1999/1926.Nearest Exit from Entrance in Maze/images/nearesr2-grid.jpg b/solution/1900-1999/1926.Nearest Exit from Entrance in Maze/images/nearesr2-grid.jpg
new file mode 100644
index 0000000000000..0e3ef56b9fc21
Binary files /dev/null and b/solution/1900-1999/1926.Nearest Exit from Entrance in Maze/images/nearesr2-grid.jpg differ
diff --git a/solution/1900-1999/1926.Nearest Exit from Entrance in Maze/images/nearest1-grid.jpg b/solution/1900-1999/1926.Nearest Exit from Entrance in Maze/images/nearest1-grid.jpg
new file mode 100644
index 0000000000000..48043bf591d4d
Binary files /dev/null and b/solution/1900-1999/1926.Nearest Exit from Entrance in Maze/images/nearest1-grid.jpg differ
diff --git a/solution/1900-1999/1926.Nearest Exit from Entrance in Maze/images/nearest3-grid.jpg b/solution/1900-1999/1926.Nearest Exit from Entrance in Maze/images/nearest3-grid.jpg
new file mode 100644
index 0000000000000..6761a8125040f
Binary files /dev/null and b/solution/1900-1999/1926.Nearest Exit from Entrance in Maze/images/nearest3-grid.jpg differ
diff --git a/solution/1900-1999/1927.Sum Game/README.md b/solution/1900-1999/1927.Sum Game/README.md
new file mode 100644
index 0000000000000..1e6b14ce2e9c7
--- /dev/null
+++ b/solution/1900-1999/1927.Sum Game/README.md
@@ -0,0 +1,99 @@
+# [1927. 求和游戏](https://leetcode-cn.com/problems/sum-game)
+
+[English Version](/solution/1900-1999/1927.Sum%20Game/README_EN.md)
+
+## 题目描述
+
+
+
+Alice 和 Bob 玩一个游戏,两人轮流行动,Alice 先手 。
+
+给你一个 偶数长度 的字符串 num
,每一个字符为数字字符或者 '?'
。每一次操作中,如果 num
中至少有一个 '?'
,那么玩家可以执行以下操作:
+
+
+ - 选择一个下标
i
满足 num[i] == '?'
。
+ - 将
num[i]
用 '0'
到 '9'
之间的一个数字字符替代。
+
+
+当 num
中没有 '?'
时,游戏结束。
+
+Bob 获胜的条件是 num
中前一半数字的和 等于 后一半数字的和。Alice 获胜的条件是前一半的和与后一半的和 不相等 。
+
+
+ - 比方说,游戏结束时
num = "243801"
,那么 Bob 获胜,因为 2+4+3 = 8+0+1
。如果游戏结束时 num = "243803"
,那么 Alice 获胜,因为 2+4+3 != 8+0+3
。
+
+
+在 Alice 和 Bob 都采取 最优 策略的前提下,如果 Alice 获胜,请返回 true
,如果 Bob 获胜,请返回 false
。
+
+
+
+示例 1:
+
+
+输入:num = "5023"
+输出:false
+解释:num 中没有 '?' ,没法进行任何操作。
+前一半的和等于后一半的和:5 + 0 = 2 + 3 。
+
+
+示例 2:
+
+
+输入:num = "25??"
+输出:true
+解释:Alice 可以将两个 '?' 中的一个替换为 '9' ,Bob 无论如何都无法使前一半的和等于后一半的和。
+
+
+示例 3:
+
+
+输入:num = "?3295???"
+输出:false
+解释:Bob 总是能赢。一种可能的结果是:
+- Alice 将第一个 '?' 用 '9' 替换。num = "93295???" 。
+- Bob 将后面一半中的一个 '?' 替换为 '9' 。num = "932959??" 。
+- Alice 将后面一半中的一个 '?' 替换为 '2' 。num = "9329592?" 。
+- Bob 将后面一半中最后一个 '?' 替换为 '7' 。num = "93295927" 。
+Bob 获胜,因为 9 + 3 + 2 + 9 = 5 + 9 + 2 + 7 。
+
+
+
+
+提示:
+
+
+ 2 <= num.length <= 105
+ num.length
是 偶数 。
+ num
只包含数字字符和 '?'
。
+
+
+
+## 解法
+
+
+
+
+
+### **Python3**
+
+
+
+```python
+
+```
+
+### **Java**
+
+
+
+```java
+
+```
+
+### **...**
+
+```
+
+```
+
+
diff --git a/solution/1900-1999/1927.Sum Game/README_EN.md b/solution/1900-1999/1927.Sum Game/README_EN.md
new file mode 100644
index 0000000000000..9c20bf4c8f1e2
--- /dev/null
+++ b/solution/1900-1999/1927.Sum Game/README_EN.md
@@ -0,0 +1,89 @@
+# [1927. Sum Game](https://leetcode.com/problems/sum-game)
+
+[中文文档](/solution/1900-1999/1927.Sum%20Game/README.md)
+
+## Description
+
+Alice and Bob take turns playing a game, with Alice starting first.
+
+You are given a string num
of even length consisting of digits and '?'
characters. On each turn, a player will do the following if there is still at least one '?'
in num
:
+
+
+ - Choose an index
i
where num[i] == '?'
.
+ - Replace
num[i]
with any digit between '0'
and '9'
.
+
+
+The game ends when there are no more '?'
characters in num
.
+
+For Bob to win, the sum of the digits in the first half of num
must be equal to the sum of the digits in the second half. For Alice to win, the sums must not be equal.
+
+
+ - For example, if the game ended with
num = "243801"
, then Bob wins because 2+4+3 = 8+0+1
. If the game ended with num = "243803"
, then Alice wins because 2+4+3 != 8+0+3
.
+
+
+Assuming Alice and Bob play optimally, return true
if Alice will win and false
if Bob will win.
+
+
+Example 1:
+
+
+Input: num = "5023"
+Output: false
+Explanation: There are no moves to be made.
+The sum of the first half is equal to the sum of the second half: 5 + 0 = 2 + 3.
+
+
+Example 2:
+
+
+Input: num = "25??"
+Output: true
+Explanation: Alice can replace one of the '?'s with '9' and it will be impossible for Bob to make the sums equal.
+
+
+Example 3:
+
+
+Input: num = "?3295???"
+Output: false
+Explanation: It can be proven that Bob will always win. One possible outcome is:
+- Alice replaces the first '?' with '9'. num = "93295???".
+- Bob replaces one of the '?' in the right half with '9'. num = "932959??".
+- Alice replaces one of the '?' in the right half with '2'. num = "9329592?".
+- Bob replaces the last '?' in the right half with '7'. num = "93295927".
+Bob wins because 9 + 3 + 2 + 9 = 5 + 9 + 2 + 7.
+
+
+
+Constraints:
+
+
+ 2 <= num.length <= 105
+ num.length
is even.
+ num
consists of only digits and '?'
.
+
+
+
+## Solutions
+
+
+
+### **Python3**
+
+```python
+
+```
+
+### **Java**
+
+```java
+
+```
+
+### **...**
+
+```
+
+```
+
+
diff --git a/solution/1900-1999/1928.Minimum Cost to Reach Destination in Time/README.md b/solution/1900-1999/1928.Minimum Cost to Reach Destination in Time/README.md
new file mode 100644
index 0000000000000..3222225364cb8
--- /dev/null
+++ b/solution/1900-1999/1928.Minimum Cost to Reach Destination in Time/README.md
@@ -0,0 +1,93 @@
+# [1928. 规定时间内到达终点的最小花费](https://leetcode-cn.com/problems/minimum-cost-to-reach-destination-in-time)
+
+[English Version](/solution/1900-1999/1928.Minimum%20Cost%20to%20Reach%20Destination%20in%20Time/README_EN.md)
+
+## 题目描述
+
+
+
+一个国家有 n
个城市,城市编号为 0
到 n - 1
,题目保证 所有城市 都由双向道路 连接在一起 。道路由二维整数数组 edges
表示,其中 edges[i] = [xi, yi, timei]
表示城市 xi
和 yi
之间有一条双向道路,耗费时间为 timei
分钟。两个城市之间可能会有多条耗费时间不同的道路,但是不会有道路两头连接着同一座城市。
+
+每次经过一个城市时,你需要付通行费。通行费用一个长度为 n
且下标从 0 开始的整数数组 passingFees
表示,其中 passingFees[j]
是你经过城市 j
需要支付的费用。
+
+一开始,你在城市 0
,你想要在 maxTime
分钟以内 (包含 maxTime
分钟)到达城市 n - 1
。旅行的 费用 为你经过的所有城市 通行费之和 (包括 起点和终点城市的通行费)。
+
+给你 maxTime
,edges
和 passingFees
,请你返回完成旅行的 最小费用 ,如果无法在 maxTime
分钟以内完成旅行,请你返回 -1
。
+
+
+
+示例 1:
+
+
+
+
+输入:maxTime = 30, edges = [[0,1,10],[1,2,10],[2,5,10],[0,3,1],[3,4,10],[4,5,15]], passingFees = [5,1,2,20,20,3]
+输出:11
+解释:最优路径为 0 -> 1 -> 2 -> 5 ,总共需要耗费 30 分钟,需要支付 11 的通行费。
+
+
+示例 2:
+
+
+
+
+输入:maxTime = 29, edges = [[0,1,10],[1,2,10],[2,5,10],[0,3,1],[3,4,10],[4,5,15]], passingFees = [5,1,2,20,20,3]
+输出:48
+解释:最优路径为 0 -> 3 -> 4 -> 5 ,总共需要耗费 26 分钟,需要支付 48 的通行费。
+你不能选择路径 0 -> 1 -> 2 -> 5 ,因为这条路径耗费的时间太长。
+
+
+示例 3:
+
+
+输入:maxTime = 25, edges = [[0,1,10],[1,2,10],[2,5,10],[0,3,1],[3,4,10],[4,5,15]], passingFees = [5,1,2,20,20,3]
+输出:-1
+解释:无法在 25 分钟以内从城市 0 到达城市 5 。
+
+
+
+
+提示:
+
+
+ 1 <= maxTime <= 1000
+ n == passingFees.length
+ 2 <= n <= 1000
+ n - 1 <= edges.length <= 1000
+ 0 <= xi, yi <= n - 1
+ 1 <= timei <= 1000
+ 1 <= passingFees[j] <= 1000
+ - 图中两个节点之间可能有多条路径。
+ - 图中不含有自环。
+
+
+
+## 解法
+
+
+
+
+
+### **Python3**
+
+
+
+```python
+
+```
+
+### **Java**
+
+
+
+```java
+
+```
+
+### **...**
+
+```
+
+```
+
+
diff --git a/solution/1900-1999/1928.Minimum Cost to Reach Destination in Time/README_EN.md b/solution/1900-1999/1928.Minimum Cost to Reach Destination in Time/README_EN.md
new file mode 100644
index 0000000000000..2559754e9dfee
--- /dev/null
+++ b/solution/1900-1999/1928.Minimum Cost to Reach Destination in Time/README_EN.md
@@ -0,0 +1,83 @@
+# [1928. Minimum Cost to Reach Destination in Time](https://leetcode.com/problems/minimum-cost-to-reach-destination-in-time)
+
+[中文文档](/solution/1900-1999/1928.Minimum%20Cost%20to%20Reach%20Destination%20in%20Time/README.md)
+
+## Description
+
+There is a country of n
cities numbered from 0
to n - 1
where all the cities are connected by bi-directional roads. The roads are represented as a 2D integer array edges
where edges[i] = [xi, yi, timei]
denotes a road between cities xi
and yi
that takes timei
minutes to travel. There may be multiple roads of differing travel times connecting the same two cities, but no road connects a city to itself.
+
+Each time you pass through a city, you must pay a passing fee. This is represented as a 0-indexed integer array passingFees
of length n
where passingFees[j]
is the amount of dollars you must pay when you pass through city j
.
+
+In the beginning, you are at city 0
and want to reach city n - 1
in maxTime
minutes or less. The cost of your journey is the summation of passing fees for each city that you passed through at some moment of your journey (including the source and destination cities).
+
+Given maxTime
, edges
, and passingFees
, return the minimum cost to complete your journey, or -1
if you cannot complete it within maxTime
minutes.
+
+
+Example 1:
+
+
+
+
+Input: maxTime = 30, edges = [[0,1,10],[1,2,10],[2,5,10],[0,3,1],[3,4,10],[4,5,15]], passingFees = [5,1,2,20,20,3]
+Output: 11
+Explanation: The path to take is 0 -> 1 -> 2 -> 5, which takes 30 minutes and has $11 worth of passing fees.
+
+
+Example 2:
+
+
+
+
+Input: maxTime = 29, edges = [[0,1,10],[1,2,10],[2,5,10],[0,3,1],[3,4,10],[4,5,15]], passingFees = [5,1,2,20,20,3]
+Output: 48
+Explanation: The path to take is 0 -> 3 -> 4 -> 5, which takes 26 minutes and has $48 worth of passing fees.
+You cannot take path 0 -> 1 -> 2 -> 5 since it would take too long.
+
+
+Example 3:
+
+
+Input: maxTime = 25, edges = [[0,1,10],[1,2,10],[2,5,10],[0,3,1],[3,4,10],[4,5,15]], passingFees = [5,1,2,20,20,3]
+Output: -1
+Explanation: There is no way to reach city 5 from city 0 within 25 minutes.
+
+
+
+Constraints:
+
+
+ 1 <= maxTime <= 1000
+ n == passingFees.length
+ 2 <= n <= 1000
+ n - 1 <= edges.length <= 1000
+ 0 <= xi, yi <= n - 1
+ 1 <= timei <= 1000
+ 1 <= passingFees[j] <= 1000
+ - The graph may contain multiple edges between two nodes.
+ - The graph does not contain self loops.
+
+
+
+## Solutions
+
+
+
+### **Python3**
+
+```python
+
+```
+
+### **Java**
+
+```java
+
+```
+
+### **...**
+
+```
+
+```
+
+
diff --git a/solution/1900-1999/1928.Minimum Cost to Reach Destination in Time/images/copy-of-leetgraph1-1.png b/solution/1900-1999/1928.Minimum Cost to Reach Destination in Time/images/copy-of-leetgraph1-1.png
new file mode 100644
index 0000000000000..e642090b52555
Binary files /dev/null and b/solution/1900-1999/1928.Minimum Cost to Reach Destination in Time/images/copy-of-leetgraph1-1.png differ
diff --git a/solution/1900-1999/1928.Minimum Cost to Reach Destination in Time/images/leetgraph1-1.png b/solution/1900-1999/1928.Minimum Cost to Reach Destination in Time/images/leetgraph1-1.png
new file mode 100644
index 0000000000000..b21fec0124531
Binary files /dev/null and b/solution/1900-1999/1928.Minimum Cost to Reach Destination in Time/images/leetgraph1-1.png differ
diff --git a/solution/1900-1999/1929.Concatenation of Array/README.md b/solution/1900-1999/1929.Concatenation of Array/README.md
new file mode 100644
index 0000000000000..9b0feb912bf06
--- /dev/null
+++ b/solution/1900-1999/1929.Concatenation of Array/README.md
@@ -0,0 +1,113 @@
+# [1929. 数组串联](https://leetcode-cn.com/problems/concatenation-of-array)
+
+[English Version](/solution/1900-1999/1929.Concatenation%20of%20Array/README_EN.md)
+
+## 题目描述
+
+
+
+给你一个长度为 n
的整数数组 nums
。请你构建一个长度为 2n
的答案数组 ans
,数组下标 从 0 开始计数 ,对于所有 0 <= i < n
的 i
,满足下述所有要求:
+
+
+ ans[i] == nums[i]
+ ans[i + n] == nums[i]
+
+
+具体而言,ans
由两个 nums
数组 串联 形成。
+
+返回数组 ans
。
+
+
+
+示例 1:
+
+
+输入:nums = [1,2,1]
+输出:[1,2,1,1,2,1]
+解释:数组 ans 按下述方式形成:
+- ans = [nums[0],nums[1],nums[2],nums[0],nums[1],nums[2]]
+- ans = [1,2,1,1,2,1]
+
+示例 2:
+
+
+输入:nums = [1,3,2,1]
+输出:[1,3,2,1,1,3,2,1]
+解释:数组 ans 按下述方式形成:
+- ans = [nums[0],nums[1],nums[2],nums[3],nums[0],nums[1],nums[2],nums[3]]
+- ans = [1,3,2,1,1,3,2,1]
+
+
+
+
+提示:
+
+
+ n == nums.length
+ 1 <= n <= 1000
+ 1 <= nums[i] <= 1000
+
+
+
+## 解法
+
+
+
+
+
+### **Python3**
+
+
+
+```python
+class Solution:
+ def getConcatenation(self, nums: List[int]) -> List[int]:
+ return nums + nums
+```
+
+### **Java**
+
+
+
+```java
+class Solution {
+ public int[] getConcatenation(int[] nums) {
+ int n = nums.length;
+ int[] ans = new int[n << 1];
+ for (int i = 0; i < n << 1; ++i) {
+ ans[i] = nums[i % n];
+ }
+ return ans;
+ }
+}
+```
+
+### **C++**
+
+```cpp
+class Solution {
+public:
+ vector getConcatenation(vector& nums) {
+ for (int i = 0, n = nums.size(); i < n; ++i) {
+ nums.push_back(nums[i]);
+ }
+ return nums;
+ }
+};
+```
+
+### **Go**
+
+```go
+func getConcatenation(nums []int) []int {
+ return append(nums, nums...)
+}
+```
+
+### **...**
+
+```
+
+```
+
+
diff --git a/solution/1900-1999/1929.Concatenation of Array/README_EN.md b/solution/1900-1999/1929.Concatenation of Array/README_EN.md
new file mode 100644
index 0000000000000..606fd5a5df95a
--- /dev/null
+++ b/solution/1900-1999/1929.Concatenation of Array/README_EN.md
@@ -0,0 +1,98 @@
+# [1929. Concatenation of Array](https://leetcode.com/problems/concatenation-of-array)
+
+[中文文档](/solution/1900-1999/1929.Concatenation%20of%20Array/README.md)
+
+## Description
+
+Given an integer array nums
of length n
, you want to create an array ans
of length 2n
where ans[i] == nums[i]
and ans[i + n] == nums[i]
for 0 <= i < n
(0-indexed).
+
+Specifically, ans
is the concatenation of two nums
arrays.
+
+Return the array ans
.
+
+
+Example 1:
+
+
+Input: nums = [1,2,1]
+Output: [1,2,1,1,2,1]
+Explanation: The array ans is formed as follows:
+- ans = [nums[0],nums[1],nums[2],nums[0],nums[1],nums[2]]
+- ans = [1,2,1,1,2,1]
+
+Example 2:
+
+
+Input: nums = [1,3,2,1]
+Output: [1,3,2,1,1,3,2,1]
+Explanation: The array ans is formed as follows:
+- ans = [nums[0],nums[1],nums[2],nums[3],nums[0],nums[1],nums[2],nums[3]]
+- ans = [1,3,2,1,1,3,2,1]
+
+
+
+Constraints:
+
+
+ n == nums.length
+ 1 <= n <= 1000
+ 1 <= nums[i] <= 1000
+
+
+
+## Solutions
+
+
+
+### **Python3**
+
+```python
+class Solution:
+ def getConcatenation(self, nums: List[int]) -> List[int]:
+ return nums + nums
+```
+
+### **Java**
+
+```java
+class Solution {
+ public int[] getConcatenation(int[] nums) {
+ int n = nums.length;
+ int[] ans = new int[n << 1];
+ for (int i = 0; i < n << 1; ++i) {
+ ans[i] = nums[i % n];
+ }
+ return ans;
+ }
+}
+```
+
+### **C++**
+
+```cpp
+class Solution {
+public:
+ vector getConcatenation(vector& nums) {
+ for (int i = 0, n = nums.size(); i < n; ++i) {
+ nums.push_back(nums[i]);
+ }
+ return nums;
+ }
+};
+```
+
+### **Go**
+
+```go
+func getConcatenation(nums []int) []int {
+ return append(nums, nums...)
+}
+```
+
+### **...**
+
+```
+
+```
+
+
diff --git a/solution/1900-1999/1929.Concatenation of Array/Solution.cpp b/solution/1900-1999/1929.Concatenation of Array/Solution.cpp
new file mode 100644
index 0000000000000..a642fe0992616
--- /dev/null
+++ b/solution/1900-1999/1929.Concatenation of Array/Solution.cpp
@@ -0,0 +1,9 @@
+class Solution {
+public:
+ vector getConcatenation(vector& nums) {
+ for (int i = 0, n = nums.size(); i < n; ++i) {
+ nums.push_back(nums[i]);
+ }
+ return nums;
+ }
+};
\ No newline at end of file
diff --git a/solution/1900-1999/1929.Concatenation of Array/Solution.go b/solution/1900-1999/1929.Concatenation of Array/Solution.go
new file mode 100644
index 0000000000000..f719f5368fded
--- /dev/null
+++ b/solution/1900-1999/1929.Concatenation of Array/Solution.go
@@ -0,0 +1,3 @@
+func getConcatenation(nums []int) []int {
+ return append(nums, nums...)
+}
\ No newline at end of file
diff --git a/solution/1900-1999/1929.Concatenation of Array/Solution.java b/solution/1900-1999/1929.Concatenation of Array/Solution.java
new file mode 100644
index 0000000000000..323ad26fe1c59
--- /dev/null
+++ b/solution/1900-1999/1929.Concatenation of Array/Solution.java
@@ -0,0 +1,10 @@
+class Solution {
+ public int[] getConcatenation(int[] nums) {
+ int n = nums.length;
+ int[] ans = new int[n << 1];
+ for (int i = 0; i < n << 1; ++i) {
+ ans[i] = nums[i % n];
+ }
+ return ans;
+ }
+}
\ No newline at end of file
diff --git a/solution/1900-1999/1929.Concatenation of Array/Solution.py b/solution/1900-1999/1929.Concatenation of Array/Solution.py
new file mode 100644
index 0000000000000..d800066d5c16b
--- /dev/null
+++ b/solution/1900-1999/1929.Concatenation of Array/Solution.py
@@ -0,0 +1,3 @@
+class Solution:
+ def getConcatenation(self, nums: List[int]) -> List[int]:
+ return nums + nums
diff --git a/solution/1900-1999/1930.Unique Length-3 Palindromic Subsequences/README.md b/solution/1900-1999/1930.Unique Length-3 Palindromic Subsequences/README.md
new file mode 100644
index 0000000000000..6e43c4f21e889
--- /dev/null
+++ b/solution/1900-1999/1930.Unique Length-3 Palindromic Subsequences/README.md
@@ -0,0 +1,151 @@
+# [1930. 长度为 3 的不同回文子序列](https://leetcode-cn.com/problems/unique-length-3-palindromic-subsequences)
+
+[English Version](/solution/1900-1999/1930.Unique%20Length-3%20Palindromic%20Subsequences/README_EN.md)
+
+## 题目描述
+
+
+
+给你一个字符串 s
,返回 s
中 长度为 3 的不同回文子序列 的个数。
+
+即便存在多种方法来构建相同的子序列,但相同的子序列只计数一次。
+
+回文 是正着读和反着读一样的字符串。
+
+子序列 是由原字符串删除其中部分字符(也可以不删除)且不改变剩余字符之间相对顺序形成的一个新字符串。
+
+
+ - 例如,
"ace"
是 "abcde"
的一个子序列。
+
+
+
+
+示例 1:
+
+
+输入:s = "aabca"
+输出:3
+解释:长度为 3 的 3 个回文子序列分别是:
+- "aba" ("aabca" 的子序列)
+- "aaa" ("aabca" 的子序列)
+- "aca" ("aabca" 的子序列)
+
+
+示例 2:
+
+
+输入:s = "adc"
+输出:0
+解释:"adc" 不存在长度为 3 的回文子序列。
+
+
+示例 3:
+
+
+输入:s = "bbcbaba"
+输出:4
+解释:长度为 3 的 4 个回文子序列分别是:
+- "bbb" ("bbcbaba" 的子序列)
+- "bcb" ("bbcbaba" 的子序列)
+- "bab" ("bbcbaba" 的子序列)
+- "aba" ("bbcbaba" 的子序列)
+
+
+
+
+提示:
+
+
+ 3 <= s.length <= 105
+ s
仅由小写英文字母组成
+
+
+
+## 解法
+
+
+
+
+
+### **Python3**
+
+
+
+```python
+class Solution:
+ def countPalindromicSubsequence(self, s: str) -> int:
+ res = 0
+ for i in range(26):
+ c = chr(ord('a') + i)
+ if c in s:
+ l, r = s.index(c), s.rindex(c)
+ chars = {s[j] for j in range(l + 1, r)}
+ res += len(chars)
+ return res
+```
+
+### **Java**
+
+
+
+```java
+class Solution {
+ public int countPalindromicSubsequence(String s) {
+ int res = 0;
+ for (char c = 'a'; c <= 'z'; ++c) {
+ int l = s.indexOf(c), r = s.lastIndexOf(c);
+ Set chars = new HashSet<>();
+ for (int i = l + 1; i < r; ++i) {
+ chars.add(s.charAt(i));
+ }
+ res += chars.size();
+ }
+ return res;
+ }
+}
+```
+
+### **C++**
+
+```cpp
+class Solution {
+public:
+ int countPalindromicSubsequence(string s) {
+ int res = 0;
+ for (char c = 'a'; c <= 'z'; ++c) {
+ int l = s.find_first_of(c), r = s.find_last_of(c);
+ unordered_set chars;
+ for (int i = l + 1; i < r; ++i) {
+ chars.insert(s[i]);
+ }
+ res += chars.size();
+ }
+ return res;
+ }
+};
+```
+
+### **Go**
+
+```go
+func countPalindromicSubsequence(s string) int {
+ res := 0
+ for c := 'a'; c <= 'z'; c++ {
+ l, r := strings.Index(s, string(c)), strings.LastIndex(s, string(c))
+ chars := make(map[byte]bool)
+ for i := l + 1; i < r; i++ {
+ chars[s[i]] = true
+ }
+ res += len(chars)
+ }
+ return res
+}
+```
+
+### **...**
+
+```
+
+```
+
+
diff --git a/solution/1900-1999/1930.Unique Length-3 Palindromic Subsequences/README_EN.md b/solution/1900-1999/1930.Unique Length-3 Palindromic Subsequences/README_EN.md
new file mode 100644
index 0000000000000..c0a25bae16827
--- /dev/null
+++ b/solution/1900-1999/1930.Unique Length-3 Palindromic Subsequences/README_EN.md
@@ -0,0 +1,141 @@
+# [1930. Unique Length-3 Palindromic Subsequences](https://leetcode.com/problems/unique-length-3-palindromic-subsequences)
+
+[中文文档](/solution/1900-1999/1930.Unique%20Length-3%20Palindromic%20Subsequences/README.md)
+
+## Description
+
+Given a string s
, return the number of unique palindromes of length three that are a subsequence of s
.
+
+Note that even if there are multiple ways to obtain the same subsequence, it is still only counted once.
+
+A palindrome is a string that reads the same forwards and backwards.
+
+A subsequence of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters.
+
+
+ - For example,
"ace"
is a subsequence of "abcde"
.
+
+
+
+Example 1:
+
+
+Input: s = "aabca"
+Output: 3
+Explanation: The 3 palindromic subsequences of length 3 are:
+- "aba" (subsequence of "aabca")
+- "aaa" (subsequence of "aabca")
+- "aca" (subsequence of "aabca")
+
+
+Example 2:
+
+
+Input: s = "adc"
+Output: 0
+Explanation: There are no palindromic subsequences of length 3 in "adc".
+
+
+Example 3:
+
+
+Input: s = "bbcbaba"
+Output: 4
+Explanation: The 4 palindromic subsequences of length 3 are:
+- "bbb" (subsequence of "bbcbaba")
+- "bcb" (subsequence of "bbcbaba")
+- "bab" (subsequence of "bbcbaba")
+- "aba" (subsequence of "bbcbaba")
+
+
+
+Constraints:
+
+
+ 3 <= s.length <= 105
+ s
consists of only lowercase English letters.
+
+
+
+## Solutions
+
+
+
+### **Python3**
+
+```python
+class Solution:
+ def countPalindromicSubsequence(self, s: str) -> int:
+ res = 0
+ for i in range(26):
+ c = chr(ord('a') + i)
+ if c in s:
+ l, r = s.index(c), s.rindex(c)
+ chars = {s[j] for j in range(l + 1, r)}
+ res += len(chars)
+ return res
+```
+
+### **Java**
+
+```java
+class Solution {
+ public int countPalindromicSubsequence(String s) {
+ int res = 0;
+ for (char c = 'a'; c <= 'z'; ++c) {
+ int l = s.indexOf(c), r = s.lastIndexOf(c);
+ Set chars = new HashSet<>();
+ for (int i = l + 1; i < r; ++i) {
+ chars.add(s.charAt(i));
+ }
+ res += chars.size();
+ }
+ return res;
+ }
+}
+```
+
+### **C++**
+
+```cpp
+class Solution {
+public:
+ int countPalindromicSubsequence(string s) {
+ int res = 0;
+ for (char c = 'a'; c <= 'z'; ++c) {
+ int l = s.find_first_of(c), r = s.find_last_of(c);
+ unordered_set chars;
+ for (int i = l + 1; i < r; ++i) {
+ chars.insert(s[i]);
+ }
+ res += chars.size();
+ }
+ return res;
+ }
+};
+```
+
+### **Go**
+
+```go
+func countPalindromicSubsequence(s string) int {
+ res := 0
+ for c := 'a'; c <= 'z'; c++ {
+ l, r := strings.Index(s, string(c)), strings.LastIndex(s, string(c))
+ chars := make(map[byte]bool)
+ for i := l + 1; i < r; i++ {
+ chars[s[i]] = true
+ }
+ res += len(chars)
+ }
+ return res
+}
+```
+
+### **...**
+
+```
+
+```
+
+
diff --git a/solution/1900-1999/1930.Unique Length-3 Palindromic Subsequences/Solution.cpp b/solution/1900-1999/1930.Unique Length-3 Palindromic Subsequences/Solution.cpp
new file mode 100644
index 0000000000000..10cf881d60d2e
--- /dev/null
+++ b/solution/1900-1999/1930.Unique Length-3 Palindromic Subsequences/Solution.cpp
@@ -0,0 +1,15 @@
+class Solution {
+public:
+ int countPalindromicSubsequence(string s) {
+ int res = 0;
+ for (char c = 'a'; c <= 'z'; ++c) {
+ int l = s.find_first_of(c), r = s.find_last_of(c);
+ unordered_set chars;
+ for (int i = l + 1; i < r; ++i) {
+ chars.insert(s[i]);
+ }
+ res += chars.size();
+ }
+ return res;
+ }
+};
\ No newline at end of file
diff --git a/solution/1900-1999/1930.Unique Length-3 Palindromic Subsequences/Solution.go b/solution/1900-1999/1930.Unique Length-3 Palindromic Subsequences/Solution.go
new file mode 100644
index 0000000000000..5d046c486293e
--- /dev/null
+++ b/solution/1900-1999/1930.Unique Length-3 Palindromic Subsequences/Solution.go
@@ -0,0 +1,12 @@
+func countPalindromicSubsequence(s string) int {
+ res := 0
+ for c := 'a'; c <= 'z'; c++ {
+ l, r := strings.Index(s, string(c)), strings.LastIndex(s, string(c))
+ chars := make(map[byte]bool)
+ for i := l + 1; i < r; i++ {
+ chars[s[i]] = true
+ }
+ res += len(chars)
+ }
+ return res
+}
\ No newline at end of file
diff --git a/solution/1900-1999/1930.Unique Length-3 Palindromic Subsequences/Solution.java b/solution/1900-1999/1930.Unique Length-3 Palindromic Subsequences/Solution.java
new file mode 100644
index 0000000000000..5a51a126f2d8d
--- /dev/null
+++ b/solution/1900-1999/1930.Unique Length-3 Palindromic Subsequences/Solution.java
@@ -0,0 +1,14 @@
+class Solution {
+ public int countPalindromicSubsequence(String s) {
+ int res = 0;
+ for (char c = 'a'; c <= 'z'; ++c) {
+ int l = s.indexOf(c), r = s.lastIndexOf(c);
+ Set chars = new HashSet<>();
+ for (int i = l + 1; i < r; ++i) {
+ chars.add(s.charAt(i));
+ }
+ res += chars.size();
+ }
+ return res;
+ }
+}
\ No newline at end of file
diff --git a/solution/1900-1999/1930.Unique Length-3 Palindromic Subsequences/Solution.py b/solution/1900-1999/1930.Unique Length-3 Palindromic Subsequences/Solution.py
new file mode 100644
index 0000000000000..fec7a76c6a453
--- /dev/null
+++ b/solution/1900-1999/1930.Unique Length-3 Palindromic Subsequences/Solution.py
@@ -0,0 +1,10 @@
+class Solution:
+ def countPalindromicSubsequence(self, s: str) -> int:
+ res = 0
+ for i in range(26):
+ c = chr(ord('a') + i)
+ if c in s:
+ l, r = s.index(c), s.rindex(c)
+ chars = {s[j] for j in range(l + 1, r)}
+ res += len(chars)
+ return res
diff --git a/solution/1900-1999/1931.Painting a Grid With Three Different Colors/README.md b/solution/1900-1999/1931.Painting a Grid With Three Different Colors/README.md
new file mode 100644
index 0000000000000..5997adc948692
--- /dev/null
+++ b/solution/1900-1999/1931.Painting a Grid With Three Different Colors/README.md
@@ -0,0 +1,76 @@
+# [1931. 用三种不同颜色为网格涂色](https://leetcode-cn.com/problems/painting-a-grid-with-three-different-colors)
+
+[English Version](/solution/1900-1999/1931.Painting%20a%20Grid%20With%20Three%20Different%20Colors/README_EN.md)
+
+## 题目描述
+
+
+
+给你两个整数 m
和 n
。构造一个 m x n
的网格,其中每个单元格最开始是白色。请你用 红、绿、蓝 三种颜色为每个单元格涂色。所有单元格都需要被涂色。
+
+涂色方案需要满足:不存在相邻两个单元格颜色相同的情况 。返回网格涂色的方法数。因为答案可能非常大, 返回 对 109 + 7
取余 的结果。
+
+
+
+示例 1:
+
+
+输入:m = 1, n = 1
+输出:3
+解释:如上图所示,存在三种可能的涂色方案。
+
+
+示例 2:
+
+
+输入:m = 1, n = 2
+输出:6
+解释:如上图所示,存在六种可能的涂色方案。
+
+
+示例 3:
+
+
+输入:m = 5, n = 5
+输出:580986
+
+
+
+
+提示:
+
+
+ 1 <= m <= 5
+ 1 <= n <= 1000
+
+
+
+## 解法
+
+
+
+
+
+### **Python3**
+
+
+
+```python
+
+```
+
+### **Java**
+
+
+
+```java
+
+```
+
+### **...**
+
+```
+
+```
+
+
diff --git a/solution/1900-1999/1931.Painting a Grid With Three Different Colors/README_EN.md b/solution/1900-1999/1931.Painting a Grid With Three Different Colors/README_EN.md
new file mode 100644
index 0000000000000..0327eb2094e9f
--- /dev/null
+++ b/solution/1900-1999/1931.Painting a Grid With Three Different Colors/README_EN.md
@@ -0,0 +1,66 @@
+# [1931. Painting a Grid With Three Different Colors](https://leetcode.com/problems/painting-a-grid-with-three-different-colors)
+
+[中文文档](/solution/1900-1999/1931.Painting%20a%20Grid%20With%20Three%20Different%20Colors/README.md)
+
+## Description
+
+You are given two integers m
and n
. Consider an m x n
grid where each cell is initially white. You can paint each cell red, green, or blue. All cells must be painted.
+
+Return the number of ways to color the grid with no two adjacent cells having the same color. Since the answer can be very large, return it modulo 109 + 7
.
+
+
+Example 1:
+
+
+Input: m = 1, n = 1
+Output: 3
+Explanation: The three possible colorings are shown in the image above.
+
+
+Example 2:
+
+
+Input: m = 1, n = 2
+Output: 6
+Explanation: The six possible colorings are shown in the image above.
+
+
+Example 3:
+
+
+Input: m = 5, n = 5
+Output: 580986
+
+
+
+Constraints:
+
+
+ 1 <= m <= 5
+ 1 <= n <= 1000
+
+
+
+## Solutions
+
+
+
+### **Python3**
+
+```python
+
+```
+
+### **Java**
+
+```java
+
+```
+
+### **...**
+
+```
+
+```
+
+
diff --git a/solution/1900-1999/1932.Merge BSTs to Create Single BST/README.md b/solution/1900-1999/1932.Merge BSTs to Create Single BST/README.md
new file mode 100644
index 0000000000000..724d606c0220d
--- /dev/null
+++ b/solution/1900-1999/1932.Merge BSTs to Create Single BST/README.md
@@ -0,0 +1,114 @@
+# [1932. 合并多棵二叉搜索树](https://leetcode-cn.com/problems/merge-bsts-to-create-single-bst)
+
+[English Version](/solution/1900-1999/1932.Merge%20BSTs%20to%20Create%20Single%20BST/README_EN.md)
+
+## 题目描述
+
+
+
+给你 n
个 二叉搜索树的根节点 ,存储在数组 trees
中(下标从 0 开始),对应 n
棵不同的二叉搜索树。trees
中的每棵二叉搜索树 最多有 3 个节点 ,且不存在值相同的两个根节点。在一步操作中,将会完成下述步骤:
+
+
+ - 选择两个 不同的 下标
i
和 j
,要求满足在 trees[i]
中的某个 叶节点 的值等于 trees[j]
的 根节点的值 。
+ - 用
trees[j]
替换 trees[i]
中的那个叶节点。
+ - 从
trees
中移除 trees[j]
。
+
+
+如果在执行 n - 1
次操作后,能形成一棵有效的二叉搜索树,则返回结果二叉树的 根节点 ;如果无法构造一棵有效的二叉搜索树,返回 null
。
+
+二叉搜索树是一种二叉树,且树中每个节点均满足下述属性:
+
+
+ - 任意节点的左子树中的值都 严格小于 此节点的值。
+ - 任意节点的右子树中的值都 严格大于 此节点的值。
+
+
+叶节点是不含子节点的节点。
+
+
+
+示例 1:
+
+
+输入:trees = [[2,1],[3,2,5],[5,4]]
+输出:[3,2,5,1,null,4]
+解释:
+第一步操作中,选出 i=1 和 j=0 ,并将 trees[0] 合并到 trees[1] 中。
+删除 trees[0] ,trees = [[3,2,5,1],[5,4]] 。
+
+在第二步操作中,选出 i=0 和 j=1 ,将 trees[1] 合并到 trees[0] 中。
+删除 trees[1] ,trees = [[3,2,5,1,null,4]] 。
+
+结果树如上图所示,为一棵有效的二叉搜索树,所以返回该树的根节点。
+
+示例 2:
+
+
+输入:trees = [[5,3,8],[3,2,6]]
+输出:[]
+解释:
+选出 i=0 和 j=1 ,然后将 trees[1] 合并到 trees[0] 中。
+删除 trees[1] ,trees = [[5,3,8,2,6]] 。
+
+结果树如上图所示。仅能执行一次有效的操作,但结果树不是一棵有效的二叉搜索树,所以返回 null 。
+
+
+示例 3:
+
+
+输入:trees = [[5,4],[3]]
+输出:[]
+解释:无法执行任何操作。
+
+
+示例 4:
+
+
+输入:trees = [[2,1,3]]
+输出:[2,1,3]
+解释:trees 中只有一棵树,且这棵树已经是一棵有效的二叉搜索树,所以返回该树的根节点。
+
+
+
+
+提示:
+
+
+ n == trees.length
+ 1 <= n <= 5 * 104
+ - 每棵树中节点数目在范围
[1, 3]
内。
+ trees
中不存在两棵树根节点值相同的情况。
+ - 输入中的所有树都是 有效的二叉树搜索树 。
+ 1 <= TreeNode.val <= 5 * 104
.
+
+
+
+## 解法
+
+
+
+
+
+### **Python3**
+
+
+
+```python
+
+```
+
+### **Java**
+
+
+
+```java
+
+```
+
+### **...**
+
+```
+
+```
+
+
diff --git a/solution/1900-1999/1932.Merge BSTs to Create Single BST/README_EN.md b/solution/1900-1999/1932.Merge BSTs to Create Single BST/README_EN.md
new file mode 100644
index 0000000000000..92104756c161c
--- /dev/null
+++ b/solution/1900-1999/1932.Merge BSTs to Create Single BST/README_EN.md
@@ -0,0 +1,104 @@
+# [1932. Merge BSTs to Create Single BST](https://leetcode.com/problems/merge-bsts-to-create-single-bst)
+
+[中文文档](/solution/1900-1999/1932.Merge%20BSTs%20to%20Create%20Single%20BST/README.md)
+
+## Description
+
+You are given n
BST (binary search tree) root nodes for n
separate BSTs stored in an array trees
(0-indexed). Each BST in trees
has at most 3 nodes, and no two roots have the same value. In one operation, you can:
+
+
+ - Select two distinct indices
i
and j
such that the value stored at one of the leaves of trees[i]
is equal to the root value of trees[j]
.
+ - Replace the leaf node in
trees[i]
with trees[j]
.
+ - Remove
trees[j]
from trees
.
+
+
+Return the root of the resulting BST if it is possible to form a valid BST after performing n - 1
operations, or null
if it is impossible to create a valid BST.
+
+A BST (binary search tree) is a binary tree where each node satisfies the following property:
+
+
+ - Every node in the node's left subtree has a value strictly less than the node's value.
+ - Every node in the node's right subtree has a value strictly greater than the node's value.
+
+
+A leaf is a node that has no children.
+
+
+Example 1:
+
+
+Input: trees = [[2,1],[3,2,5],[5,4]]
+Output: [3,2,5,1,null,4]
+Explanation:
+In the first operation, pick i=1 and j=0, and merge trees[0] into trees[1].
+Delete trees[0], so trees = [[3,2,5,1],[5,4]].
+
+In the second operation, pick i=0 and j=1, and merge trees[1] into trees[0].
+Delete trees[1], so trees = [[3,2,5,1,null,4]].
+
+The resulting tree, shown above, is a valid BST, so return its root.
+
+Example 2:
+
+
+Input: trees = [[5,3,8],[3,2,6]]
+Output: []
+Explanation:
+Pick i=0 and j=1 and merge trees[1] into trees[0].
+Delete trees[1], so trees = [[5,3,8,2,6]].
+
+The resulting tree is shown above. This is the only valid operation that can be performed, but the resulting tree is not a valid BST, so return null.
+
+
+Example 3:
+
+
+Input: trees = [[5,4],[3]]
+Output: []
+Explanation: It is impossible to perform any operations.
+
+
+Example 4:
+
+
+Input: trees = [[2,1,3]]
+Output: [2,1,3]
+Explanation: There is only one tree, and it is already a valid BST, so return its root.
+
+
+
+Constraints:
+
+
+ n == trees.length
+ 1 <= n <= 5 * 104
+ - The number of nodes in each tree is in the range
[1, 3]
.
+ - No two roots of
trees
have the same value.
+ - All the trees in the input are valid BSTs.
+ 1 <= TreeNode.val <= 5 * 104
.
+
+
+
+## Solutions
+
+
+
+### **Python3**
+
+```python
+
+```
+
+### **Java**
+
+```java
+
+```
+
+### **...**
+
+```
+
+```
+
+
diff --git a/solution/README.md b/solution/README.md
index 3eb249a099d1c..9107066de51fd 100644
--- a/solution/README.md
+++ b/solution/README.md
@@ -716,6 +716,7 @@
| [0703](https://leetcode-cn.com/problems/kth-largest-element-in-a-stream) | [数据流中的第 K 大元素](/solution/0700-0799/0703.Kth%20Largest%20Element%20in%20a%20Stream/README.md) | `树`,`设计`,`二叉搜索树`,`二叉树`,`数据流`,`堆(优先队列)` | 简单 | |
| [0704](https://leetcode-cn.com/problems/binary-search) | [二分查找](/solution/0700-0799/0704.Binary%20Search/README.md) | `数组`,`二分查找` | 简单 | |
| [0705](https://leetcode-cn.com/problems/design-hashset) | [设计哈希集合](/solution/0700-0799/0705.Design%20HashSet/README.md) | `设计`,`数组`,`哈希表`,`链表`,`哈希函数` | 简单 | |
+| [0706](https://leetcode-cn.com/problems/design-hashmap) | [设计哈希映射](/solution/0700-0799/0706.Design%20HashMap/README.md) | `设计`,`数组`,`哈希表`,`链表`,`哈希函数` | 简单 | |
| [0707](https://leetcode-cn.com/problems/design-linked-list) | [设计链表](/solution/0700-0799/0707.Design%20Linked%20List/README.md) | `设计`,`链表` | 中等 | |
| [0708](https://leetcode-cn.com/problems/insert-into-a-sorted-circular-linked-list) | [循环有序列表的插入](/solution/0700-0799/0708.Insert%20into%20a%20Sorted%20Circular%20Linked%20List/README.md) | `链表` | 中等 | 🔒 |
| [0709](https://leetcode-cn.com/problems/to-lower-case) | [转换成小写字母](/solution/0700-0799/0709.To%20Lower%20Case/README.md) | `字符串` | 简单 | |
@@ -872,7 +873,6 @@
| [0860](https://leetcode-cn.com/problems/lemonade-change) | [柠檬水找零](/solution/0800-0899/0860.Lemonade%20Change/README.md) | `贪心`,`数组` | 简单 | |
| [0861](https://leetcode-cn.com/problems/score-after-flipping-matrix) | [翻转矩阵后的得分](/solution/0800-0899/0861.Score%20After%20Flipping%20Matrix/README.md) | `贪心`,`位运算`,`数组`,`矩阵` | 中等 | |
| [0862](https://leetcode-cn.com/problems/shortest-subarray-with-sum-at-least-k) | [和至少为 K 的最短子数组](/solution/0800-0899/0862.Shortest%20Subarray%20with%20Sum%20at%20Least%20K/README.md) | `队列`,`数组`,`二分查找`,`前缀和`,`滑动窗口`,`单调队列`,`堆(优先队列)` | 困难 | |
-| [0863](https://leetcode-cn.com/problems/all-nodes-distance-k-in-binary-tree) | [二叉树中所有距离为 K 的结点](/solution/0800-0899/0863.All%20Nodes%20Distance%20K%20in%20Binary%20Tree/README.md) | `树`,`深度优先搜索`,`广度优先搜索`,`二叉树` | 中等 | |
| [0864](https://leetcode-cn.com/problems/shortest-path-to-get-all-keys) | [获取所有钥匙的最短路径](/solution/0800-0899/0864.Shortest%20Path%20to%20Get%20All%20Keys/README.md) | `位运算`,`广度优先搜索` | 困难 | |
| [0865](https://leetcode-cn.com/problems/smallest-subtree-with-all-the-deepest-nodes) | [具有所有最深节点的最小子树](/solution/0800-0899/0865.Smallest%20Subtree%20with%20all%20the%20Deepest%20Nodes/README.md) | `树`,`深度优先搜索`,`广度优先搜索`,`哈希表`,`二叉树` | 中等 | |
| [0866](https://leetcode-cn.com/problems/prime-palindrome) | [回文素数](/solution/0800-0899/0866.Prime%20Palindrome/README.md) | `数学` | 中等 | |
@@ -1071,7 +1071,6 @@
| [1059](https://leetcode-cn.com/problems/all-paths-from-source-lead-to-destination) | [从始点到终点的所有路径](/solution/1000-1099/1059.All%20Paths%20from%20Source%20Lead%20to%20Destination/README.md) | `深度优先搜索`,`图` | 中等 | 🔒 |
| [1060](https://leetcode-cn.com/problems/missing-element-in-sorted-array) | [有序数组中的缺失元素](/solution/1000-1099/1060.Missing%20Element%20in%20Sorted%20Array/README.md) | `数组`,`二分查找` | 中等 | 🔒 |
| [1061](https://leetcode-cn.com/problems/lexicographically-smallest-equivalent-string) | [按字典序排列最小的等效字符串](/solution/1000-1099/1061.Lexicographically%20Smallest%20Equivalent%20String/README.md) | `并查集`,`字符串` | 中等 | 🔒 |
-| [1062](https://leetcode-cn.com/problems/longest-repeating-substring) | [最长重复子串](/solution/1000-1099/1062.Longest%20Repeating%20Substring/README.md) | `字符串`,`二分查找`,`动态规划`,`后缀数组`,`哈希函数`,`滚动哈希` | 中等 | 🔒 |
| [1063](https://leetcode-cn.com/problems/number-of-valid-subarrays) | [有效子数组的数目](/solution/1000-1099/1063.Number%20of%20Valid%20Subarrays/README.md) | `栈`,`数组`,`单调栈` | 困难 | 🔒 |
| [1064](https://leetcode-cn.com/problems/fixed-point) | [不动点](/solution/1000-1099/1064.Fixed%20Point/README.md) | `数组`,`二分查找` | 简单 | 🔒 |
| [1065](https://leetcode-cn.com/problems/index-pairs-of-a-string) | [字符串的索引对](/solution/1000-1099/1065.Index%20Pairs%20of%20a%20String/README.md) | `字典树`,`数组`,`字符串`,`排序` | 简单 | 🔒 |
@@ -1471,7 +1470,6 @@
| [1459](https://leetcode-cn.com/problems/rectangles-area) | [矩形面积](/solution/1400-1499/1459.Rectangles%20Area/README.md) | `数据库` | 中等 | 🔒 |
| [1460](https://leetcode-cn.com/problems/make-two-arrays-equal-by-reversing-sub-arrays) | [通过翻转子数组使两个数组相等](/solution/1400-1499/1460.Make%20Two%20Arrays%20Equal%20by%20Reversing%20Sub-arrays/README.md) | `数组`,`哈希表`,`排序` | 简单 | |
| [1461](https://leetcode-cn.com/problems/check-if-a-string-contains-all-binary-codes-of-size-k) | [检查一个字符串是否包含所有长度为 K 的二进制子串](/solution/1400-1499/1461.Check%20If%20a%20String%20Contains%20All%20Binary%20Codes%20of%20Size%20K/README.md) | `位运算`,`哈希表`,`字符串`,`哈希函数`,`滚动哈希` | 中等 | |
-| [1462](https://leetcode-cn.com/problems/course-schedule-iv) | [课程表 IV](/solution/1400-1499/1462.Course%20Schedule%20IV/README.md) | `深度优先搜索`,`广度优先搜索`,`图`,`拓扑排序` | 中等 | |
| [1463](https://leetcode-cn.com/problems/cherry-pickup-ii) | [摘樱桃 II](/solution/1400-1499/1463.Cherry%20Pickup%20II/README.md) | `数组`,`动态规划`,`矩阵` | 困难 | |
| [1464](https://leetcode-cn.com/problems/maximum-product-of-two-elements-in-an-array) | [数组中两元素的最大乘积](/solution/1400-1499/1464.Maximum%20Product%20of%20Two%20Elements%20in%20an%20Array/README.md) | `数组`,`排序`,`堆(优先队列)` | 简单 | |
| [1465](https://leetcode-cn.com/problems/maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts) | [切割后面积最大的蛋糕](/solution/1400-1499/1465.Maximum%20Area%20of%20a%20Piece%20of%20Cake%20After%20Horizontal%20and%20Vertical%20Cuts/README.md) | `贪心`,`数组`,`排序` | 中等 | |
@@ -1578,7 +1576,6 @@
| [1566](https://leetcode-cn.com/problems/detect-pattern-of-length-m-repeated-k-or-more-times) | [重复至少 K 次且长度为 M 的模式](/solution/1500-1599/1566.Detect%20Pattern%20of%20Length%20M%20Repeated%20K%20or%20More%20Times/README.md) | `数组`,`枚举` | 简单 | |
| [1567](https://leetcode-cn.com/problems/maximum-length-of-subarray-with-positive-product) | [乘积为正数的最长子数组长度](/solution/1500-1599/1567.Maximum%20Length%20of%20Subarray%20With%20Positive%20Product/README.md) | `贪心`,`数组`,`动态规划` | 中等 | |
| [1568](https://leetcode-cn.com/problems/minimum-number-of-days-to-disconnect-island) | [使陆地分离的最少天数](/solution/1500-1599/1568.Minimum%20Number%20of%20Days%20to%20Disconnect%20Island/README.md) | `深度优先搜索`,`广度优先搜索`,`数组`,`矩阵`,`强连通分量` | 困难 | |
-| [1569](https://leetcode-cn.com/problems/number-of-ways-to-reorder-array-to-get-same-bst) | [将子数组重新排序得到同一个二叉查找树的方案数](/solution/1500-1599/1569.Number%20of%20Ways%20to%20Reorder%20Array%20to%20Get%20Same%20BST/README.md) | `树`,`并查集`,`二叉搜索树`,`记忆化搜索`,`数组`,`数学`,`分治`,`动态规划`,`二叉树`,`组合数学` | 困难 | |
| [1570](https://leetcode-cn.com/problems/dot-product-of-two-sparse-vectors) | [两个稀疏向量的点积](/solution/1500-1599/1570.Dot%20Product%20of%20Two%20Sparse%20Vectors/README.md) | `设计`,`数组`,`哈希表`,`双指针` | 中等 | 🔒 |
| [1571](https://leetcode-cn.com/problems/warehouse-manager) | [仓库经理](/solution/1500-1599/1571.Warehouse%20Manager/README.md) | `数据库` | 简单 | 🔒 |
| [1572](https://leetcode-cn.com/problems/matrix-diagonal-sum) | [矩阵对角线元素的和](/solution/1500-1599/1572.Matrix%20Diagonal%20Sum/README.md) | `数组`,`矩阵` | 简单 | |
@@ -1780,6 +1777,7 @@
| [1768](https://leetcode-cn.com/problems/merge-strings-alternately) | [交替合并字符串](/solution/1700-1799/1768.Merge%20Strings%20Alternately/README.md) | `双指针`,`字符串` | 简单 | |
| [1769](https://leetcode-cn.com/problems/minimum-number-of-operations-to-move-all-balls-to-each-box) | [移动所有球到每个盒子所需的最小操作数](/solution/1700-1799/1769.Minimum%20Number%20of%20Operations%20to%20Move%20All%20Balls%20to%20Each%20Box/README.md) | `数组`,`字符串` | 中等 | |
| [1770](https://leetcode-cn.com/problems/maximum-score-from-performing-multiplication-operations) | [执行乘法运算的最大分数](/solution/1700-1799/1770.Maximum%20Score%20from%20Performing%20Multiplication%20Operations/README.md) | `数组`,`动态规划` | 中等 | |
+| [1771](https://leetcode-cn.com/problems/maximize-palindrome-length-from-subsequences) | [由子序列构造的最长回文串的长度](/solution/1700-1799/1771.Maximize%20Palindrome%20Length%20From%20Subsequences/README.md) | `字符串`,`动态规划` | 困难 | |
| [1772](https://leetcode-cn.com/problems/sort-features-by-popularity) | [按受欢迎程度排列功能](/solution/1700-1799/1772.Sort%20Features%20by%20Popularity/README.md) | `数组`,`哈希表`,`字符串`,`排序` | 中等 | 🔒 |
| [1773](https://leetcode-cn.com/problems/count-items-matching-a-rule) | [统计匹配检索规则的物品数量](/solution/1700-1799/1773.Count%20Items%20Matching%20a%20Rule/README.md) | `数组`,`字符串` | 简单 | |
| [1774](https://leetcode-cn.com/problems/closest-dessert-cost) | [最接近目标价格的甜点成本](/solution/1700-1799/1774.Closest%20Dessert%20Cost/README.md) | `数组`,`动态规划`,`回溯` | 中等 | |
@@ -1788,7 +1786,6 @@
| [1777](https://leetcode-cn.com/problems/products-price-for-each-store) | [每家商店的产品价格](/solution/1700-1799/1777.Product%27s%20Price%20for%20Each%20Store/README.md) | `数据库` | 简单 | 🔒 |
| [1778](https://leetcode-cn.com/problems/shortest-path-in-a-hidden-grid) | [Shortest Path in a Hidden Grid](/solution/1700-1799/1778.Shortest%20Path%20in%20a%20Hidden%20Grid/README.md) | `深度优先搜索`,`广度优先搜索`,`图`,`交互` | 中等 | 🔒 |
| [1779](https://leetcode-cn.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate) | [找到最近的有相同 X 或 Y 坐标的点](/solution/1700-1799/1779.Find%20Nearest%20Point%20That%20Has%20the%20Same%20X%20or%20Y%20Coordinate/README.md) | `数组` | 简单 | |
-| [1780](https://leetcode-cn.com/problems/check-if-number-is-a-sum-of-powers-of-three) | [判断一个数字是否可以表示成三的幂的和](/solution/1700-1799/1780.Check%20if%20Number%20is%20a%20Sum%20of%20Powers%20of%20Three/README.md) | `数学` | 中等 | |
| [1781](https://leetcode-cn.com/problems/sum-of-beauty-of-all-substrings) | [所有子字符串美丽值之和](/solution/1700-1799/1781.Sum%20of%20Beauty%20of%20All%20Substrings/README.md) | `哈希表`,`字符串`,`计数` | 中等 | |
| [1782](https://leetcode-cn.com/problems/count-pairs-of-nodes) | [统计点对的数目](/solution/1700-1799/1782.Count%20Pairs%20Of%20Nodes/README.md) | `图`,`双指针`,`二分查找` | 困难 | |
| [1783](https://leetcode-cn.com/problems/grand-slam-titles) | [大满贯数量](/solution/1700-1799/1783.Grand%20Slam%20Titles/README.md) | `数据库` | 中等 | 🔒 |
@@ -1933,6 +1930,14 @@
| [1922](https://leetcode-cn.com/problems/count-good-numbers) | [统计好数字的数目](/solution/1900-1999/1922.Count%20Good%20Numbers/README.md) | `递归`,`数学` | 中等 | |
| [1923](https://leetcode-cn.com/problems/longest-common-subpath) | [最长公共子路径](/solution/1900-1999/1923.Longest%20Common%20Subpath/README.md) | `数组`,`二分查找`,`后缀数组`,`哈希函数`,`滚动哈希` | 困难 | |
| [1924](https://leetcode-cn.com/problems/erect-the-fence-ii) | [Erect the Fence II](/solution/1900-1999/1924.Erect%20the%20Fence%20II/README.md) | | 困难 | 🔒 |
+| [1925](https://leetcode-cn.com/problems/count-square-sum-triples) | [统计平方和三元组的数目](/solution/1900-1999/1925.Count%20Square%20Sum%20Triples/README.md) | `贪心`,`数组` | 简单 | |
+| [1926](https://leetcode-cn.com/problems/nearest-exit-from-entrance-in-maze) | [迷宫中离入口最近的出口](/solution/1900-1999/1926.Nearest%20Exit%20from%20Entrance%20in%20Maze/README.md) | `深度优先搜索`,`广度优先搜索`,`动态规划` | 中等 | |
+| [1927](https://leetcode-cn.com/problems/sum-game) | [求和游戏](/solution/1900-1999/1927.Sum%20Game/README.md) | `贪心` | 中等 | |
+| [1928](https://leetcode-cn.com/problems/minimum-cost-to-reach-destination-in-time) | [规定时间内到达终点的最小花费](/solution/1900-1999/1928.Minimum%20Cost%20to%20Reach%20Destination%20in%20Time/README.md) | `动态规划` | 困难 | |
+| [1929](https://leetcode-cn.com/problems/concatenation-of-array) | [数组串联](/solution/1900-1999/1929.Concatenation%20of%20Array/README.md) | | 简单 | |
+| [1930](https://leetcode-cn.com/problems/unique-length-3-palindromic-subsequences) | [长度为 3 的不同回文子序列](/solution/1900-1999/1930.Unique%20Length-3%20Palindromic%20Subsequences/README.md) | | 中等 | |
+| [1931](https://leetcode-cn.com/problems/painting-a-grid-with-three-different-colors) | [用三种不同颜色为网格涂色](/solution/1900-1999/1931.Painting%20a%20Grid%20With%20Three%20Different%20Colors/README.md) | | 困难 | |
+| [1932](https://leetcode-cn.com/problems/merge-bsts-to-create-single-bst) | [合并多棵二叉搜索树](/solution/1900-1999/1932.Merge%20BSTs%20to%20Create%20Single%20BST/README.md) | | 困难 | |
## 版权
diff --git a/solution/README_EN.md b/solution/README_EN.md
index 847fa4410d2bd..fb970f2388730 100644
--- a/solution/README_EN.md
+++ b/solution/README_EN.md
@@ -714,6 +714,7 @@ Press Control+F(or Command+F on the
| [0703](https://leetcode.com/problems/kth-largest-element-in-a-stream) | [Kth Largest Element in a Stream](/solution/0700-0799/0703.Kth%20Largest%20Element%20in%20a%20Stream/README_EN.md) | `Tree`,`Design`,`Binary Search Tree`,`Binary Tree`,`Data Stream`,`Heap (Priority Queue)` | Easy | |
| [0704](https://leetcode.com/problems/binary-search) | [Binary Search](/solution/0700-0799/0704.Binary%20Search/README_EN.md) | `Array`,`Binary Search` | Easy | |
| [0705](https://leetcode.com/problems/design-hashset) | [Design HashSet](/solution/0700-0799/0705.Design%20HashSet/README_EN.md) | `Design`,`Array`,`Hash Table`,`Linked List`,`Hash Function` | Easy | |
+| [0706](https://leetcode.com/problems/design-hashmap) | [Design HashMap](/solution/0700-0799/0706.Design%20HashMap/README_EN.md) | `Design`,`Array`,`Hash Table`,`Linked List`,`Hash Function` | Easy | |
| [0707](https://leetcode.com/problems/design-linked-list) | [Design Linked List](/solution/0700-0799/0707.Design%20Linked%20List/README_EN.md) | `Design`,`Linked List` | Medium | |
| [0708](https://leetcode.com/problems/insert-into-a-sorted-circular-linked-list) | [Insert into a Sorted Circular Linked List](/solution/0700-0799/0708.Insert%20into%20a%20Sorted%20Circular%20Linked%20List/README_EN.md) | `Linked List` | Medium | 🔒 |
| [0709](https://leetcode.com/problems/to-lower-case) | [To Lower Case](/solution/0700-0799/0709.To%20Lower%20Case/README_EN.md) | `String` | Easy | |
@@ -870,7 +871,6 @@ Press Control+F(or Command+F on the
| [0860](https://leetcode.com/problems/lemonade-change) | [Lemonade Change](/solution/0800-0899/0860.Lemonade%20Change/README_EN.md) | `Greedy`,`Array` | Easy | |
| [0861](https://leetcode.com/problems/score-after-flipping-matrix) | [Score After Flipping Matrix](/solution/0800-0899/0861.Score%20After%20Flipping%20Matrix/README_EN.md) | `Greedy`,`Bit Manipulation`,`Array`,`Matrix` | Medium | |
| [0862](https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k) | [Shortest Subarray with Sum at Least K](/solution/0800-0899/0862.Shortest%20Subarray%20with%20Sum%20at%20Least%20K/README_EN.md) | `Queue`,`Array`,`Binary Search`,`Prefix Sum`,`Sliding Window`,`Monotonic Queue`,`Heap (Priority Queue)` | Hard | |
-| [0863](https://leetcode.com/problems/all-nodes-distance-k-in-binary-tree) | [All Nodes Distance K in Binary Tree](/solution/0800-0899/0863.All%20Nodes%20Distance%20K%20in%20Binary%20Tree/README_EN.md) | `Tree`,`Depth-First Search`,`Breadth-First Search`,`Binary Tree` | Medium | |
| [0864](https://leetcode.com/problems/shortest-path-to-get-all-keys) | [Shortest Path to Get All Keys](/solution/0800-0899/0864.Shortest%20Path%20to%20Get%20All%20Keys/README_EN.md) | `Bit Manipulation`,`Breadth-First Search` | Hard | |
| [0865](https://leetcode.com/problems/smallest-subtree-with-all-the-deepest-nodes) | [Smallest Subtree with all the Deepest Nodes](/solution/0800-0899/0865.Smallest%20Subtree%20with%20all%20the%20Deepest%20Nodes/README_EN.md) | `Tree`,`Depth-First Search`,`Breadth-First Search`,`Hash Table`,`Binary Tree` | Medium | |
| [0866](https://leetcode.com/problems/prime-palindrome) | [Prime Palindrome](/solution/0800-0899/0866.Prime%20Palindrome/README_EN.md) | `Math` | Medium | |
@@ -1069,7 +1069,6 @@ Press Control+F(or Command+F on the
| [1059](https://leetcode.com/problems/all-paths-from-source-lead-to-destination) | [All Paths from Source Lead to Destination](/solution/1000-1099/1059.All%20Paths%20from%20Source%20Lead%20to%20Destination/README_EN.md) | `Depth-First Search`,`Graph` | Medium | 🔒 |
| [1060](https://leetcode.com/problems/missing-element-in-sorted-array) | [Missing Element in Sorted Array](/solution/1000-1099/1060.Missing%20Element%20in%20Sorted%20Array/README_EN.md) | `Array`,`Binary Search` | Medium | 🔒 |
| [1061](https://leetcode.com/problems/lexicographically-smallest-equivalent-string) | [Lexicographically Smallest Equivalent String](/solution/1000-1099/1061.Lexicographically%20Smallest%20Equivalent%20String/README_EN.md) | `Union Find`,`String` | Medium | 🔒 |
-| [1062](https://leetcode.com/problems/longest-repeating-substring) | [Longest Repeating Substring](/solution/1000-1099/1062.Longest%20Repeating%20Substring/README_EN.md) | `String`,`Binary Search`,`Dynamic Programming`,`Suffix Array`,`Hash Function`,`Rolling Hash` | Medium | 🔒 |
| [1063](https://leetcode.com/problems/number-of-valid-subarrays) | [Number of Valid Subarrays](/solution/1000-1099/1063.Number%20of%20Valid%20Subarrays/README_EN.md) | `Stack`,`Array`,`Monotonic Stack` | Hard | 🔒 |
| [1064](https://leetcode.com/problems/fixed-point) | [Fixed Point](/solution/1000-1099/1064.Fixed%20Point/README_EN.md) | `Array`,`Binary Search` | Easy | 🔒 |
| [1065](https://leetcode.com/problems/index-pairs-of-a-string) | [Index Pairs of a String](/solution/1000-1099/1065.Index%20Pairs%20of%20a%20String/README_EN.md) | `Trie`,`Array`,`String`,`Sorting` | Easy | 🔒 |
@@ -1469,7 +1468,6 @@ Press Control+F(or Command+F on the
| [1459](https://leetcode.com/problems/rectangles-area) | [Rectangles Area](/solution/1400-1499/1459.Rectangles%20Area/README_EN.md) | `Database` | Medium | 🔒 |
| [1460](https://leetcode.com/problems/make-two-arrays-equal-by-reversing-sub-arrays) | [Make Two Arrays Equal by Reversing Sub-arrays](/solution/1400-1499/1460.Make%20Two%20Arrays%20Equal%20by%20Reversing%20Sub-arrays/README_EN.md) | `Array`,`Hash Table`,`Sorting` | Easy | |
| [1461](https://leetcode.com/problems/check-if-a-string-contains-all-binary-codes-of-size-k) | [Check If a String Contains All Binary Codes of Size K](/solution/1400-1499/1461.Check%20If%20a%20String%20Contains%20All%20Binary%20Codes%20of%20Size%20K/README_EN.md) | `Bit Manipulation`,`Hash Table`,`String`,`Hash Function`,`Rolling Hash` | Medium | |
-| [1462](https://leetcode.com/problems/course-schedule-iv) | [Course Schedule IV](/solution/1400-1499/1462.Course%20Schedule%20IV/README_EN.md) | `Depth-First Search`,`Breadth-First Search`,`Graph`,`Topological Sort` | Medium | |
| [1463](https://leetcode.com/problems/cherry-pickup-ii) | [Cherry Pickup II](/solution/1400-1499/1463.Cherry%20Pickup%20II/README_EN.md) | `Array`,`Dynamic Programming`,`Matrix` | Hard | |
| [1464](https://leetcode.com/problems/maximum-product-of-two-elements-in-an-array) | [Maximum Product of Two Elements in an Array](/solution/1400-1499/1464.Maximum%20Product%20of%20Two%20Elements%20in%20an%20Array/README_EN.md) | `Array`,`Sorting`,`Heap (Priority Queue)` | Easy | |
| [1465](https://leetcode.com/problems/maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts) | [Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts](/solution/1400-1499/1465.Maximum%20Area%20of%20a%20Piece%20of%20Cake%20After%20Horizontal%20and%20Vertical%20Cuts/README_EN.md) | `Greedy`,`Array`,`Sorting` | Medium | |
@@ -1576,7 +1574,6 @@ Press Control+F(or Command+F on the
| [1566](https://leetcode.com/problems/detect-pattern-of-length-m-repeated-k-or-more-times) | [Detect Pattern of Length M Repeated K or More Times](/solution/1500-1599/1566.Detect%20Pattern%20of%20Length%20M%20Repeated%20K%20or%20More%20Times/README_EN.md) | `Array`,`Enumeration` | Easy | |
| [1567](https://leetcode.com/problems/maximum-length-of-subarray-with-positive-product) | [Maximum Length of Subarray With Positive Product](/solution/1500-1599/1567.Maximum%20Length%20of%20Subarray%20With%20Positive%20Product/README_EN.md) | `Greedy`,`Array`,`Dynamic Programming` | Medium | |
| [1568](https://leetcode.com/problems/minimum-number-of-days-to-disconnect-island) | [Minimum Number of Days to Disconnect Island](/solution/1500-1599/1568.Minimum%20Number%20of%20Days%20to%20Disconnect%20Island/README_EN.md) | `Depth-First Search`,`Breadth-First Search`,`Array`,`Matrix`,`Strongly Connected Component` | Hard | |
-| [1569](https://leetcode.com/problems/number-of-ways-to-reorder-array-to-get-same-bst) | [Number of Ways to Reorder Array to Get Same BST](/solution/1500-1599/1569.Number%20of%20Ways%20to%20Reorder%20Array%20to%20Get%20Same%20BST/README_EN.md) | `Tree`,`Union Find`,`Binary Search Tree`,`Memoization`,`Array`,`Math`,`Divide and Conquer`,`Dynamic Programming`,`Binary Tree`,`Combinatorics` | Hard | |
| [1570](https://leetcode.com/problems/dot-product-of-two-sparse-vectors) | [Dot Product of Two Sparse Vectors](/solution/1500-1599/1570.Dot%20Product%20of%20Two%20Sparse%20Vectors/README_EN.md) | `Design`,`Array`,`Hash Table`,`Two Pointers` | Medium | 🔒 |
| [1571](https://leetcode.com/problems/warehouse-manager) | [Warehouse Manager](/solution/1500-1599/1571.Warehouse%20Manager/README_EN.md) | `Database` | Easy | 🔒 |
| [1572](https://leetcode.com/problems/matrix-diagonal-sum) | [Matrix Diagonal Sum](/solution/1500-1599/1572.Matrix%20Diagonal%20Sum/README_EN.md) | `Array`,`Matrix` | Easy | |
@@ -1778,6 +1775,7 @@ Press Control+F(or Command+F on the
| [1768](https://leetcode.com/problems/merge-strings-alternately) | [Merge Strings Alternately](/solution/1700-1799/1768.Merge%20Strings%20Alternately/README_EN.md) | `Two Pointers`,`String` | Easy | |
| [1769](https://leetcode.com/problems/minimum-number-of-operations-to-move-all-balls-to-each-box) | [Minimum Number of Operations to Move All Balls to Each Box](/solution/1700-1799/1769.Minimum%20Number%20of%20Operations%20to%20Move%20All%20Balls%20to%20Each%20Box/README_EN.md) | `Array`,`String` | Medium | |
| [1770](https://leetcode.com/problems/maximum-score-from-performing-multiplication-operations) | [Maximum Score from Performing Multiplication Operations](/solution/1700-1799/1770.Maximum%20Score%20from%20Performing%20Multiplication%20Operations/README_EN.md) | `Array`,`Dynamic Programming` | Medium | |
+| [1771](https://leetcode.com/problems/maximize-palindrome-length-from-subsequences) | [Maximize Palindrome Length From Subsequences](/solution/1700-1799/1771.Maximize%20Palindrome%20Length%20From%20Subsequences/README_EN.md) | `String`,`Dynamic Programming` | Hard | |
| [1772](https://leetcode.com/problems/sort-features-by-popularity) | [Sort Features by Popularity](/solution/1700-1799/1772.Sort%20Features%20by%20Popularity/README_EN.md) | `Array`,`Hash Table`,`String`,`Sorting` | Medium | 🔒 |
| [1773](https://leetcode.com/problems/count-items-matching-a-rule) | [Count Items Matching a Rule](/solution/1700-1799/1773.Count%20Items%20Matching%20a%20Rule/README_EN.md) | `Array`,`String` | Easy | |
| [1774](https://leetcode.com/problems/closest-dessert-cost) | [Closest Dessert Cost](/solution/1700-1799/1774.Closest%20Dessert%20Cost/README_EN.md) | `Array`,`Dynamic Programming`,`Backtracking` | Medium | |
@@ -1786,7 +1784,6 @@ Press Control+F(or Command+F on the
| [1777](https://leetcode.com/problems/products-price-for-each-store) | [Product's Price for Each Store](/solution/1700-1799/1777.Product%27s%20Price%20for%20Each%20Store/README_EN.md) | `Database` | Easy | 🔒 |
| [1778](https://leetcode.com/problems/shortest-path-in-a-hidden-grid) | [Shortest Path in a Hidden Grid](/solution/1700-1799/1778.Shortest%20Path%20in%20a%20Hidden%20Grid/README_EN.md) | `Depth-First Search`,`Breadth-First Search`,`Graph`,`Interactive` | Medium | 🔒 |
| [1779](https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate) | [Find Nearest Point That Has the Same X or Y Coordinate](/solution/1700-1799/1779.Find%20Nearest%20Point%20That%20Has%20the%20Same%20X%20or%20Y%20Coordinate/README_EN.md) | `Array` | Easy | |
-| [1780](https://leetcode.com/problems/check-if-number-is-a-sum-of-powers-of-three) | [Check if Number is a Sum of Powers of Three](/solution/1700-1799/1780.Check%20if%20Number%20is%20a%20Sum%20of%20Powers%20of%20Three/README_EN.md) | `Math` | Medium | |
| [1781](https://leetcode.com/problems/sum-of-beauty-of-all-substrings) | [Sum of Beauty of All Substrings](/solution/1700-1799/1781.Sum%20of%20Beauty%20of%20All%20Substrings/README_EN.md) | `Hash Table`,`String`,`Counting` | Medium | |
| [1782](https://leetcode.com/problems/count-pairs-of-nodes) | [Count Pairs Of Nodes](/solution/1700-1799/1782.Count%20Pairs%20Of%20Nodes/README_EN.md) | `Graph`,`Two Pointers`,`Binary Search` | Hard | |
| [1783](https://leetcode.com/problems/grand-slam-titles) | [Grand Slam Titles](/solution/1700-1799/1783.Grand%20Slam%20Titles/README_EN.md) | `Database` | Medium | 🔒 |
@@ -1931,6 +1928,14 @@ Press Control+F(or Command+F on the
| [1922](https://leetcode.com/problems/count-good-numbers) | [Count Good Numbers](/solution/1900-1999/1922.Count%20Good%20Numbers/README_EN.md) | `Recursion`,`Math` | Medium | |
| [1923](https://leetcode.com/problems/longest-common-subpath) | [Longest Common Subpath](/solution/1900-1999/1923.Longest%20Common%20Subpath/README_EN.md) | `Array`,`Binary Search`,`Suffix Array`,`Hash Function`,`Rolling Hash` | Hard | |
| [1924](https://leetcode.com/problems/erect-the-fence-ii) | [Erect the Fence II](/solution/1900-1999/1924.Erect%20the%20Fence%20II/README_EN.md) | | Hard | 🔒 |
+| [1925](https://leetcode.com/problems/count-square-sum-triples) | [Count Square Sum Triples](/solution/1900-1999/1925.Count%20Square%20Sum%20Triples/README_EN.md) | `Greedy`,`Array` | Easy | |
+| [1926](https://leetcode.com/problems/nearest-exit-from-entrance-in-maze) | [Nearest Exit from Entrance in Maze](/solution/1900-1999/1926.Nearest%20Exit%20from%20Entrance%20in%20Maze/README_EN.md) | `Depth-First Search`,`Breadth-First Search`,`Dynamic Programming` | Medium | |
+| [1927](https://leetcode.com/problems/sum-game) | [Sum Game](/solution/1900-1999/1927.Sum%20Game/README_EN.md) | `Greedy` | Medium | |
+| [1928](https://leetcode.com/problems/minimum-cost-to-reach-destination-in-time) | [Minimum Cost to Reach Destination in Time](/solution/1900-1999/1928.Minimum%20Cost%20to%20Reach%20Destination%20in%20Time/README_EN.md) | `Dynamic Programming` | Hard | |
+| [1929](https://leetcode.com/problems/concatenation-of-array) | [Concatenation of Array](/solution/1900-1999/1929.Concatenation%20of%20Array/README_EN.md) | | Easy | |
+| [1930](https://leetcode.com/problems/unique-length-3-palindromic-subsequences) | [Unique Length-3 Palindromic Subsequences](/solution/1900-1999/1930.Unique%20Length-3%20Palindromic%20Subsequences/README_EN.md) | | Medium | |
+| [1931](https://leetcode.com/problems/painting-a-grid-with-three-different-colors) | [Painting a Grid With Three Different Colors](/solution/1900-1999/1931.Painting%20a%20Grid%20With%20Three%20Different%20Colors/README_EN.md) | | Hard | |
+| [1932](https://leetcode.com/problems/merge-bsts-to-create-single-bst) | [Merge BSTs to Create Single BST](/solution/1900-1999/1932.Merge%20BSTs%20to%20Create%20Single%20BST/README_EN.md) | | Hard | |
## Copyright
diff --git a/solution/result.json b/solution/result.json
index 1eb0b46516f79..c4d9b0239feef 100644
--- a/solution/result.json
+++ b/solution/result.json
@@ -1 +1 @@
-[{"question_id": "2074", "frontend_question_id": "1924", "paid_only": true, "paid_only_cn": true, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/erect-the-fence-ii", "url_en": "https://leetcode.com/problems/erect-the-fence-ii", "relative_path_cn": "/solution/1900-1999/1924.Erect%20the%20Fence%20II/README.md", "relative_path_en": "/solution/1900-1999/1924.Erect%20the%20Fence%20II/README_EN.md", "title_cn": "Erect the Fence II", "title_en": "Erect the Fence II", "question_title_slug": "erect-the-fence-ii", "content_en": "You are given a 2D integer array trees
where trees[i] = [xi, yi]
represents the location of the ith
tree in the garden.
\n\nYou are asked to fence the entire garden using the minimum length of rope possible. The garden is well-fenced only if all the trees are enclosed and the rope used forms a perfect circle. A tree is considered enclosed if it is inside or on the border of the circle.
\n\nMore formally, you must form a circle using the rope with a center (x, y)
and radius r
where all trees lie inside or on the circle and r
is minimum.
\n\nReturn the center and radius of the circle as a length 3 array [x, y, r]
. Answers within 10-5
of the actual answer will be accepted.
\n\n
\nExample 1:
\n\n
\n\n\nInput: trees = [[1,1],[2,2],[2,0],[2,4],[3,3],[4,2]]\nOutput: [2.00000,2.00000,2.00000]\nExplanation: The fence will have center = (2, 2) and radius = 2\n
\n\nExample 2:
\n\n
\n\n\nInput: trees = [[1,2],[2,2],[4,2]]\nOutput: [2.50000,2.00000,1.50000]\nExplanation: The fence will have center = (2.5, 2) and radius = 1.5\n
\n\n
\nConstraints:
\n\n\n\t1 <= trees.length <= 3000
\n\ttrees[i].length == 2
\n\t0 <= xi, yi <= 3000
\n
\n", "content_cn": "You are given a 2D integer array trees
where trees[i] = [xi, yi]
represents the location of the ith
tree in the garden.
\n\nYou are asked to fence the entire garden using the minimum length of rope possible. The garden is well-fenced only if all the trees are enclosed and the rope used forms a perfect circle. A tree is considered enclosed if it is inside or on the border of the circle.
\n\nMore formally, you must form a circle using the rope with a center (x, y)
and radius r
where all trees lie inside or on the circle and r
is minimum.
\n\nReturn the center and radius of the circle as a length 3 array [x, y, r]
. Answers within 10-5
of the actual answer will be accepted.
\n\n
\nExample 1:
\n\n
\n\n\nInput: trees = [[1,1],[2,2],[2,0],[2,4],[3,3],[4,2]]\nOutput: [2.00000,2.00000,2.00000]\nExplanation: The fence will have center = (2, 2) and radius = 2\n
\n\nExample 2:
\n\n
\n\n\nInput: trees = [[1,2],[2,2],[4,2]]\nOutput: [2.50000,2.00000,1.50000]\nExplanation: The fence will have center = (2.5, 2) and radius = 1.5\n
\n\n
\nConstraints:
\n\n\n\t1 <= trees.length <= 3000
\n\ttrees[i].length == 2
\n\t0 <= xi, yi <= 3000
\n
\n", "tags_en": [], "tags_cn": [], "difficulty_en": "Hard", "difficulty_cn": "\u56f0\u96be", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n vector outerTrees(vector>& trees) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public double[] outerTrees(int[][] trees) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def outerTrees(self, trees):\n \"\"\"\n :type trees: List[List[int]]\n :rtype: List[float]\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def outerTrees(self, trees: List[List[int]]) -> List[float]:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\n/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\ndouble* outerTrees(int** trees, int treesSize, int* treesColSize, int* returnSize){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public double[] OuterTrees(int[][] trees) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[][]} trees\n * @return {number[]}\n */\nvar outerTrees = function(trees) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer[][]} trees\n# @return {Float[]}\ndef outer_trees(trees)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func outerTrees(_ trees: [[Int]]) -> [Double] {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func outerTrees(trees [][]int) []float64 {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def outerTrees(trees: Array[Array[Int]]): Array[Double] = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun outerTrees(trees: Array): DoubleArray {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn outer_trees(trees: Vec>) -> Vec {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer[][] $trees\n * @return Float[]\n */\n function outerTrees($trees) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function outerTrees(trees: number[][]): number[] {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (outer-trees trees)\n (-> (listof (listof exact-integer?)) (listof flonum?))\n\n )", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1924](https://leetcode-cn.com/problems/erect-the-fence-ii)", "[Erect the Fence II](/solution/1900-1999/1924.Erect%20the%20Fence%20II/README.md)", "", "\u56f0\u96be", "\ud83d\udd12"], "md_table_row_en": ["[1924](https://leetcode.com/problems/erect-the-fence-ii)", "[Erect the Fence II](/solution/1900-1999/1924.Erect%20the%20Fence%20II/README_EN.md)", "", "Hard", "\ud83d\udd12"]}, {"question_id": "2069", "frontend_question_id": "1918", "paid_only": true, "paid_only_cn": true, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/kth-smallest-subarray-sum", "url_en": "https://leetcode.com/problems/kth-smallest-subarray-sum", "relative_path_cn": "/solution/1900-1999/1918.Kth%20Smallest%20Subarray%20Sum/README.md", "relative_path_en": "/solution/1900-1999/1918.Kth%20Smallest%20Subarray%20Sum/README_EN.md", "title_cn": "Kth Smallest Subarray Sum", "title_en": "Kth Smallest Subarray Sum", "question_title_slug": "kth-smallest-subarray-sum", "content_en": "Given an integer array nums
of length n
and an integer k
, return the kth
smallest subarray sum.
\n\nA subarray is defined as a non-empty contiguous sequence of elements in an array. A subarray sum is the sum of all elements in the subarray.
\n\n
\nExample 1:
\n\n\nInput: nums = [2,1,3], k = 4\nOutput: 3\nExplanation: The subarrays of [2,1,3] are:\n- [2] with sum 2\n- [1] with sum 1\n- [3] with sum 3\n- [2,1] with sum 3\n- [1,3] with sum 4\n- [2,1,3] with sum 6 \nOrdering the sums from smallest to largest gives 1, 2, 3, 3, 4, 6. The 4th smallest is 3.\n
\n\nExample 2:
\n\n\nInput: nums = [3,3,5,5], k = 7\nOutput: 10\nExplanation: The subarrays of [3,3,5,5] are:\n- [3] with sum 3\n- [3] with sum 3\n- [5] with sum 5\n- [5] with sum 5\n- [3,3] with sum 6\n- [3,5] with sum 8\n- [5,5] with sum 10\n- [3,3,5], with sum 11\n- [3,5,5] with sum 13\n- [3,3,5,5] with sum 16\nOrdering the sums from smallest to largest gives 3, 3, 5, 5, 6, 8, 10, 11, 13, 16. The 7th smallest is 10.\n
\n\n
\nConstraints:
\n\n\n\tn == nums.length
\n\t1 <= n <= 2 * 104
\n\t1 <= nums[i] <= 5 * 104
\n\t1 <= k <= n * (n + 1) / 2
\n
\n", "content_cn": "Given an integer array nums
of length n
and an integer k
, return the kth
smallest subarray sum.
\n\nA subarray is defined as a non-empty contiguous sequence of elements in an array. A subarray sum is the sum of all elements in the subarray.
\n\n
\nExample 1:
\n\n\nInput: nums = [2,1,3], k = 4\nOutput: 3\nExplanation: The subarrays of [2,1,3] are:\n- [2] with sum 2\n- [1] with sum 1\n- [3] with sum 3\n- [2,1] with sum 3\n- [1,3] with sum 4\n- [2,1,3] with sum 6 \nOrdering the sums from smallest to largest gives 1, 2, 3, 3, 4, 6. The 4th smallest is 3.\n
\n\nExample 2:
\n\n\nInput: nums = [3,3,5,5], k = 7\nOutput: 10\nExplanation: The subarrays of [3,3,5,5] are:\n- [3] with sum 3\n- [3] with sum 3\n- [5] with sum 5\n- [5] with sum 5\n- [3,3] with sum 6\n- [3,5] with sum 8\n- [5,5] with sum 10\n- [3,3,5], with sum 11\n- [3,5,5] with sum 13\n- [3,3,5,5] with sum 16\nOrdering the sums from smallest to largest gives 3, 3, 5, 5, 6, 8, 10, 11, 13, 16. The 7th smallest is 10.\n
\n\n
\nConstraints:
\n\n\n\tn == nums.length
\n\t1 <= n <= 2 * 104
\n\t1 <= nums[i] <= 5 * 104
\n\t1 <= k <= n * (n + 1) / 2
\n
\n", "tags_en": ["Array", "Binary Search", "Sliding Window"], "tags_cn": ["\u6570\u7ec4", "\u4e8c\u5206\u67e5\u627e", "\u6ed1\u52a8\u7a97\u53e3"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n int kthSmallestSubarraySum(vector& nums, int k) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int kthSmallestSubarraySum(int[] nums, int k) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def kthSmallestSubarraySum(self, nums, k):\n \"\"\"\n :type nums: List[int]\n :type k: int\n :rtype: int\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def kthSmallestSubarraySum(self, nums: List[int], k: int) -> int:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nint kthSmallestSubarraySum(int* nums, int numsSize, int k){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int KthSmallestSubarraySum(int[] nums, int k) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[]} nums\n * @param {number} k\n * @return {number}\n */\nvar kthSmallestSubarraySum = function(nums, k) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer[]} nums\n# @param {Integer} k\n# @return {Integer}\ndef kth_smallest_subarray_sum(nums, k)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func kthSmallestSubarraySum(_ nums: [Int], _ k: Int) -> Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func kthSmallestSubarraySum(nums []int, k int) int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def kthSmallestSubarraySum(nums: Array[Int], k: Int): Int = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun kthSmallestSubarraySum(nums: IntArray, k: Int): Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn kth_smallest_subarray_sum(nums: Vec, k: i32) -> i32 {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @param Integer $k\n * @return Integer\n */\n function kthSmallestSubarraySum($nums, $k) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function kthSmallestSubarraySum(nums: number[], k: number): number {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (kth-smallest-subarray-sum nums k)\n (-> (listof exact-integer?) exact-integer? exact-integer?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec kth_smallest_subarray_sum(Nums :: [integer()], K :: integer()) -> integer().\nkth_smallest_subarray_sum(Nums, K) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec kth_smallest_subarray_sum(nums :: [integer], k :: integer) :: integer\n def kth_smallest_subarray_sum(nums, k) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1918](https://leetcode-cn.com/problems/kth-smallest-subarray-sum)", "[Kth Smallest Subarray Sum](/solution/1900-1999/1918.Kth%20Smallest%20Subarray%20Sum/README.md)", "`\u6570\u7ec4`,`\u4e8c\u5206\u67e5\u627e`,`\u6ed1\u52a8\u7a97\u53e3`", "\u4e2d\u7b49", "\ud83d\udd12"], "md_table_row_en": ["[1918](https://leetcode.com/problems/kth-smallest-subarray-sum)", "[Kth Smallest Subarray Sum](/solution/1900-1999/1918.Kth%20Smallest%20Subarray%20Sum/README_EN.md)", "`Array`,`Binary Search`,`Sliding Window`", "Medium", "\ud83d\udd12"]}, {"question_id": "2064", "frontend_question_id": "1919", "paid_only": true, "paid_only_cn": false, "category": "Database", "url_cn": "https://leetcode-cn.com/problems/leetcodify-similar-friends", "url_en": "https://leetcode.com/problems/leetcodify-similar-friends", "relative_path_cn": "/solution/1900-1999/1919.Leetcodify%20Similar%20Friends/README.md", "relative_path_en": "/solution/1900-1999/1919.Leetcodify%20Similar%20Friends/README_EN.md", "title_cn": "Leetcodify Similar Friends", "title_en": "Leetcodify Similar Friends", "question_title_slug": "leetcodify-similar-friends", "content_en": "Table: Listens
\n\n\n+-------------+---------+\n| Column Name | Type |\n+-------------+---------+\n| user_id | int |\n| song_id | int |\n| day | date |\n+-------------+---------+\nThere is no primary key for this table. It may contain duplicates.\nEach row of this table indicates that the user user_id listened to the song song_id on the day day.\n
\n\n
\n\nTable: Friendship
\n\n\n+---------------+---------+\n| Column Name | Type |\n+---------------+---------+\n| user1_id | int |\n| user2_id | int |\n+---------------+---------+\n(user1_id, user2_id) is the primary key for this table.\nEach row of this table indicates that the users user1_id and user2_id are friends.\nNote that user1_id < user2_id.\n
\n\n
\n\nWrite an SQL query to report the similar friends of Leetcodify users. A user x
and user y
are similar friends if:
\n\n\n\t- Users
x
and y
are friends, and \n\t- Users
x
and y
listened to the same three or more different songs on the same day. \n
\n\nReturn the result table in any order. Note that you must return the similar pairs of friends the same way they were represented in the input (i.e., always user1_id < user2_id
).
\n\nThe query result format is in the following example:
\n\n
\n\n\nListens table:\n+---------+---------+------------+\n| user_id | song_id | day |\n+---------+---------+------------+\n| 1 | 10 | 2021-03-15 |\n| 1 | 11 | 2021-03-15 |\n| 1 | 12 | 2021-03-15 |\n| 2 | 10 | 2021-03-15 |\n| 2 | 11 | 2021-03-15 |\n| 2 | 12 | 2021-03-15 |\n| 3 | 10 | 2021-03-15 |\n| 3 | 11 | 2021-03-15 |\n| 3 | 12 | 2021-03-15 |\n| 4 | 10 | 2021-03-15 |\n| 4 | 11 | 2021-03-15 |\n| 4 | 13 | 2021-03-15 |\n| 5 | 10 | 2021-03-16 |\n| 5 | 11 | 2021-03-16 |\n| 5 | 12 | 2021-03-16 |\n+---------+---------+------------+\n\nFriendship table:\n+----------+----------+\n| user1_id | user2_id |\n+----------+----------+\n| 1 | 2 |\n| 2 | 4 |\n| 2 | 5 |\n+----------+----------+\n\nResult table:\n+----------+----------+\n| user1_id | user2_id |\n+----------+----------+\n| 1 | 2 |\n+----------+----------+\n\nUsers 1 and 2 are friends, and they listened to songs 10, 11, and 12 on the same day. They are similar friends.\nUsers 1 and 3 listened to songs 10, 11, and 12 on the same day, but they are not friends.\nUsers 2 and 4 are friends, but they did not listen to the same three different songs.\nUsers 2 and 5 are friends and listened to songs 10, 11, and 12, but they did not listen to them on the same day.\n
\n", "content_cn": "Table: Listens
\n\n\n+-------------+---------+\n| Column Name | Type |\n+-------------+---------+\n| user_id | int |\n| song_id | int |\n| day | date |\n+-------------+---------+\nThere is no primary key for this table. It may contain duplicates.\nEach row of this table indicates that the user user_id listened to the song song_id on the day day.\n
\n\n
\n\nTable: Friendship
\n\n\n+---------------+---------+\n| Column Name | Type |\n+---------------+---------+\n| user1_id | int |\n| user2_id | int |\n+---------------+---------+\n(user1_id, user2_id) is the primary key for this table.\nEach row of this table indicates that the users user1_id and user2_id are friends.\nNote that user1_id < user2_id.\n
\n\n
\n\nWrite an SQL query to report the similar friends of Leetcodify users. A user x
and user y
are similar friends if:
\n\n\n\t- Users
x
and y
are friends, and \n\t- Users
x
and y
listened to the same three or more different songs on the same day. \n
\n\nReturn the result table in any order. Note that you must return the similar pairs of friends the same way they were represented in the input (i.e., always user1_id < user2_id
).
\n\nThe query result format is in the following example:
\n\n
\n\n\nListens table:\n+---------+---------+------------+\n| user_id | song_id | day |\n+---------+---------+------------+\n| 1 | 10 | 2021-03-15 |\n| 1 | 11 | 2021-03-15 |\n| 1 | 12 | 2021-03-15 |\n| 2 | 10 | 2021-03-15 |\n| 2 | 11 | 2021-03-15 |\n| 2 | 12 | 2021-03-15 |\n| 3 | 10 | 2021-03-15 |\n| 3 | 11 | 2021-03-15 |\n| 3 | 12 | 2021-03-15 |\n| 4 | 10 | 2021-03-15 |\n| 4 | 11 | 2021-03-15 |\n| 4 | 13 | 2021-03-15 |\n| 5 | 10 | 2021-03-16 |\n| 5 | 11 | 2021-03-16 |\n| 5 | 12 | 2021-03-16 |\n+---------+---------+------------+\n\nFriendship table:\n+----------+----------+\n| user1_id | user2_id |\n+----------+----------+\n| 1 | 2 |\n| 2 | 4 |\n| 2 | 5 |\n+----------+----------+\n\nResult table:\n+----------+----------+\n| user1_id | user2_id |\n+----------+----------+\n| 1 | 2 |\n+----------+----------+\n\nUsers 1 and 2 are friends, and they listened to songs 10, 11, and 12 on the same day. They are similar friends.\nUsers 1 and 3 listened to songs 10, 11, and 12 on the same day, but they are not friends.\nUsers 2 and 4 are friends, but they did not listen to the same three different songs.\nUsers 2 and 5 are friends and listened to songs 10, 11, and 12, but they did not listen to them on the same day.\n
\n", "tags_en": [], "tags_cn": [], "difficulty_en": "Hard", "difficulty_cn": "\u56f0\u96be", "code_snippets": [{"lang": "MySQL", "langSlug": "mysql", "code": "# Write your MySQL query statement below", "__typename": "CodeSnippetNode"}, {"lang": "MS SQL Server", "langSlug": "mssql", "code": "/* Write your T-SQL query statement below */", "__typename": "CodeSnippetNode"}, {"lang": "Oracle", "langSlug": "oraclesql", "code": "/* Write your PL/SQL query statement below */", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1919](https://leetcode-cn.com/problems/leetcodify-similar-friends)", "[Leetcodify Similar Friends](/solution/1900-1999/1919.Leetcodify%20Similar%20Friends/README.md)", "", "\u56f0\u96be", ""], "md_table_row_en": ["[1919](https://leetcode.com/problems/leetcodify-similar-friends)", "[Leetcodify Similar Friends](/solution/1900-1999/1919.Leetcodify%20Similar%20Friends/README_EN.md)", "", "Hard", "\ud83d\udd12"]}, {"question_id": "2063", "frontend_question_id": "1917", "paid_only": true, "paid_only_cn": true, "category": "Database", "url_cn": "https://leetcode-cn.com/problems/leetcodify-friends-recommendations", "url_en": "https://leetcode.com/problems/leetcodify-friends-recommendations", "relative_path_cn": "/solution/1900-1999/1917.Leetcodify%20Friends%20Recommendations/README.md", "relative_path_en": "/solution/1900-1999/1917.Leetcodify%20Friends%20Recommendations/README_EN.md", "title_cn": "Leetcodify Friends Recommendations", "title_en": "Leetcodify Friends Recommendations", "question_title_slug": "leetcodify-friends-recommendations", "content_en": "Table: Listens
\n\n\n+-------------+---------+\n| Column Name | Type |\n+-------------+---------+\n| user_id | int |\n| song_id | int |\n| day | date |\n+-------------+---------+\nThere is no primary key for this table. It may contain duplicates.\nEach row of this table indicates that the user user_id listened to the song song_id on the day day.\n
\n\n
\n\nTable: Friendship
\n\n\n+---------------+---------+\n| Column Name | Type |\n+---------------+---------+\n| user1_id | int |\n| user2_id | int |\n+---------------+---------+\n(user1_id, user2_id) is the primary key for this table.\nEach row of this table indicates that the users user1_id and user2_id are friends.\nNote that user1_id < user2_id.\n
\n\n
\n\nWrite an SQL query to recommend friends to Leetcodify users. We recommend user x
to user y
if:
\n\n\n\t- Users
x
and y
are not friends, and \n\t- Users
x
and y
listened to the same three or more different songs on the same day. \n
\n\nNote that friend recommendations are unidirectional, meaning if user x
and user y
should be recommended to each other, the result table should have both user x
recommended to user y
and user y
recommended to user x
. Also, note that the result table should not contain duplicates (i.e., user y
should not be recommended to user x
multiple times.).
\n\nReturn the result table in any order.
\n\nThe query result format is in the following example:
\n\n
\n\n\nListens table:\n+---------+---------+------------+\n| user_id | song_id | day |\n+---------+---------+------------+\n| 1 | 10 | 2021-03-15 |\n| 1 | 11 | 2021-03-15 |\n| 1 | 12 | 2021-03-15 |\n| 2 | 10 | 2021-03-15 |\n| 2 | 11 | 2021-03-15 |\n| 2 | 12 | 2021-03-15 |\n| 3 | 10 | 2021-03-15 |\n| 3 | 11 | 2021-03-15 |\n| 3 | 12 | 2021-03-15 |\n| 4 | 10 | 2021-03-15 |\n| 4 | 11 | 2021-03-15 |\n| 4 | 13 | 2021-03-15 |\n| 5 | 10 | 2021-03-16 |\n| 5 | 11 | 2021-03-16 |\n| 5 | 12 | 2021-03-16 |\n+---------+---------+------------+\n\nFriendship table:\n+----------+----------+\n| user1_id | user2_id |\n+----------+----------+\n| 1 | 2 |\n+----------+----------+\n\nResult table:\n+---------+----------------+\n| user_id | recommended_id |\n+---------+----------------+\n| 1 | 3 |\n| 2 | 3 |\n| 3 | 1 |\n| 3 | 2 |\n+---------+----------------+\nUsers 1 and 2 listened to songs 10, 11, and 12 on the same day, but they are already friends.\nUsers 1 and 3 listened to songs 10, 11, and 12 on the same day. Since they are not friends, we recommend them to each other.\nUsers 1 and 4 did not listen to the same three songs.\nUsers 1 and 5 listened to songs 10, 11, and 12, but on different days.\n\nSimilarly, we can see that users 2 and 3 listened to songs 10, 11, and 12 on the same day and are not friends, so we recommend them to each other.\n
\n", "content_cn": "Table: Listens
\n\n\n+-------------+---------+\n| Column Name | Type |\n+-------------+---------+\n| user_id | int |\n| song_id | int |\n| day | date |\n+-------------+---------+\nThere is no primary key for this table. It may contain duplicates.\nEach row of this table indicates that the user user_id listened to the song song_id on the day day.\n
\n\n
\n\nTable: Friendship
\n\n\n+---------------+---------+\n| Column Name | Type |\n+---------------+---------+\n| user1_id | int |\n| user2_id | int |\n+---------------+---------+\n(user1_id, user2_id) is the primary key for this table.\nEach row of this table indicates that the users user1_id and user2_id are friends.\nNote that user1_id < user2_id.\n
\n\n
\n\nWrite an SQL query to recommend friends to Leetcodify users. We recommend user x
to user y
if:
\n\n\n\t- Users
x
and y
are not friends, and \n\t- Users
x
and y
listened to the same three or more different songs on the same day. \n
\n\nNote that friend recommendations are unidirectional, meaning if user x
and user y
should be recommended to each other, the result table should have both user x
recommended to user y
and user y
recommended to user x
. Also, note that the result table should not contain duplicates (i.e., user y
should not be recommended to user x
multiple times.).
\n\nReturn the result table in any order.
\n\nThe query result format is in the following example:
\n\n
\n\n\nListens table:\n+---------+---------+------------+\n| user_id | song_id | day |\n+---------+---------+------------+\n| 1 | 10 | 2021-03-15 |\n| 1 | 11 | 2021-03-15 |\n| 1 | 12 | 2021-03-15 |\n| 2 | 10 | 2021-03-15 |\n| 2 | 11 | 2021-03-15 |\n| 2 | 12 | 2021-03-15 |\n| 3 | 10 | 2021-03-15 |\n| 3 | 11 | 2021-03-15 |\n| 3 | 12 | 2021-03-15 |\n| 4 | 10 | 2021-03-15 |\n| 4 | 11 | 2021-03-15 |\n| 4 | 13 | 2021-03-15 |\n| 5 | 10 | 2021-03-16 |\n| 5 | 11 | 2021-03-16 |\n| 5 | 12 | 2021-03-16 |\n+---------+---------+------------+\n\nFriendship table:\n+----------+----------+\n| user1_id | user2_id |\n+----------+----------+\n| 1 | 2 |\n+----------+----------+\n\nResult table:\n+---------+----------------+\n| user_id | recommended_id |\n+---------+----------------+\n| 1 | 3 |\n| 2 | 3 |\n| 3 | 1 |\n| 3 | 2 |\n+---------+----------------+\nUsers 1 and 2 listened to songs 10, 11, and 12 on the same day, but they are already friends.\nUsers 1 and 3 listened to songs 10, 11, and 12 on the same day. Since they are not friends, we recommend them to each other.\nUsers 1 and 4 did not listen to the same three songs.\nUsers 1 and 5 listened to songs 10, 11, and 12, but on different days.\n\nSimilarly, we can see that users 2 and 3 listened to songs 10, 11, and 12 on the same day and are not friends, so we recommend them to each other.\n
\n", "tags_en": ["Database"], "tags_cn": ["\u6570\u636e\u5e93"], "difficulty_en": "Hard", "difficulty_cn": "\u56f0\u96be", "code_snippets": [{"lang": "MySQL", "langSlug": "mysql", "code": "# Write your MySQL query statement below", "__typename": "CodeSnippetNode"}, {"lang": "MS SQL Server", "langSlug": "mssql", "code": "/* Write your T-SQL query statement below */", "__typename": "CodeSnippetNode"}, {"lang": "Oracle", "langSlug": "oraclesql", "code": "/* Write your PL/SQL query statement below */", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1917](https://leetcode-cn.com/problems/leetcodify-friends-recommendations)", "[Leetcodify Friends Recommendations](/solution/1900-1999/1917.Leetcodify%20Friends%20Recommendations/README.md)", "`\u6570\u636e\u5e93`", "\u56f0\u96be", "\ud83d\udd12"], "md_table_row_en": ["[1917](https://leetcode.com/problems/leetcodify-friends-recommendations)", "[Leetcodify Friends Recommendations](/solution/1900-1999/1917.Leetcodify%20Friends%20Recommendations/README_EN.md)", "`Database`", "Hard", "\ud83d\udd12"]}, {"question_id": "2062", "frontend_question_id": "1908", "paid_only": true, "paid_only_cn": true, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/game-of-nim", "url_en": "https://leetcode.com/problems/game-of-nim", "relative_path_cn": "/solution/1900-1999/1908.Game%20of%20Nim/README.md", "relative_path_en": "/solution/1900-1999/1908.Game%20of%20Nim/README_EN.md", "title_cn": "Game of Nim", "title_en": "Game of Nim", "question_title_slug": "game-of-nim", "content_en": "Alice and Bob take turns playing a game with Alice starting first.
\n\nIn this game, there are n
piles of stones. On each player's turn, the player should remove any positive number of stones from a non-empty pile of his or her choice. The first player who cannot make a move loses, and the other player wins.
\n\nGiven an integer array piles
, where piles[i]
is the number of stones in the ith
pile, return true
if Alice wins, or false
if Bob wins.
\n\nBoth Alice and Bob play optimally.
\n\n
\nExample 1:
\n\n\nInput: piles = [1]\nOutput: true\nExplanation: There is only one possible scenario:\n- On the first turn, Alice removes one stone from the first pile. piles = [0].\n- On the second turn, there are no stones left for Bob to remove. Alice wins.\n
\n\nExample 2:
\n\n\nInput: piles = [1,1]\nOutput: false\nExplanation: It can be proven that Bob will always win. One possible scenario is:\n- On the first turn, Alice removes one stone from the first pile. piles = [0,1].\n- On the second turn, Bob removes one stone from the second pile. piles = [0,0].\n- On the third turn, there are no stones left for Alice to remove. Bob wins.\n
\n\nExample 3:
\n\n\nInput: piles = [1,2,3]\nOutput: false\nExplanation: It can be proven that Bob will always win. One possible scenario is:\n- On the first turn, Alice removes three stones from the third pile. piles = [1,2,0].\n- On the second turn, Bob removes one stone from the second pile. piles = [1,1,0].\n- On the third turn, Alice removes one stone from the first pile. piles = [0,1,0].\n- On the fourth turn, Bob removes one stone from the second pile. piles = [0,0,0].\n- On the fifth turn, there are no stones left for Alice to remove. Bob wins.
\n\n
\nConstraints:
\n\n\n\tn == piles.length
\n\t1 <= n <= 7
\n\t1 <= piles[i] <= 7
\n
\n\n
\nFollow-up: Could you find a linear time solution? Although the linear time solution may be beyond the scope of an interview, it could be interesting to know.
\n", "content_cn": "Alice and Bob take turns playing a game with Alice starting first.
\n\nIn this game, there are n
piles of stones. On each player's turn, the player should remove any positive number of stones from a non-empty pile of his or her choice. The first player who cannot make a move loses, and the other player wins.
\n\nGiven an integer array piles
, where piles[i]
is the number of stones in the ith
pile, return true
if Alice wins, or false
if Bob wins.
\n\nBoth Alice and Bob play optimally.
\n\n
\nExample 1:
\n\n\nInput: piles = [1]\nOutput: true\nExplanation: There is only one possible scenario:\n- On the first turn, Alice removes one stone from the first pile. piles = [0].\n- On the second turn, there are no stones left for Bob to remove. Alice wins.\n
\n\nExample 2:
\n\n\nInput: piles = [1,1]\nOutput: false\nExplanation: It can be proven that Bob will always win. One possible scenario is:\n- On the first turn, Alice removes one stone from the first pile. piles = [0,1].\n- On the second turn, Bob removes one stone from the second pile. piles = [0,0].\n- On the third turn, there are no stones left for Alice to remove. Bob wins.\n
\n\nExample 3:
\n\n\nInput: piles = [1,2,3]\nOutput: false\nExplanation: It can be proven that Bob will always win. One possible scenario is:\n- On the first turn, Alice removes three stones from the third pile. piles = [1,2,0].\n- On the second turn, Bob removes one stone from the second pile. piles = [1,1,0].\n- On the third turn, Alice removes one stone from the first pile. piles = [0,1,0].\n- On the fourth turn, Bob removes one stone from the second pile. piles = [0,0,0].\n- On the fifth turn, there are no stones left for Alice to remove. Bob wins.
\n\n
\nConstraints:
\n\n\n\tn == piles.length
\n\t1 <= n <= 7
\n\t1 <= piles[i] <= 7
\n
\n\n
\nFollow-up: Could you find a linear time solution? Although the linear time solution may be beyond the scope of an interview, it could be interesting to know.
\n", "tags_en": ["Bit Manipulation", "Brainteaser", "Array", "Math", "Dynamic Programming", "Game Theory"], "tags_cn": ["\u4f4d\u8fd0\u7b97", "\u8111\u7b4b\u6025\u8f6c\u5f2f", "\u6570\u7ec4", "\u6570\u5b66", "\u52a8\u6001\u89c4\u5212", "\u535a\u5f08"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n bool nimGame(vector& piles) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public boolean nimGame(int[] piles) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def nimGame(self, piles):\n \"\"\"\n :type piles: List[int]\n :rtype: bool\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def nimGame(self, piles: List[int]) -> bool:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nbool nimGame(int* piles, int pilesSize){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public bool NimGame(int[] piles) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[]} piles\n * @return {boolean}\n */\nvar nimGame = function(piles) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer[]} piles\n# @return {Boolean}\ndef nim_game(piles)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func nimGame(_ piles: [Int]) -> Bool {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func nimGame(piles []int) bool {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def nimGame(piles: Array[Int]): Boolean = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun nimGame(piles: IntArray): Boolean {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn nim_game(piles: Vec) -> bool {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer[] $piles\n * @return Boolean\n */\n function nimGame($piles) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function nimGame(piles: number[]): boolean {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (nim-game piles)\n (-> (listof exact-integer?) boolean?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec nim_game(Piles :: [integer()]) -> boolean().\nnim_game(Piles) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec nim_game(piles :: [integer]) :: boolean\n def nim_game(piles) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1908](https://leetcode-cn.com/problems/game-of-nim)", "[Game of Nim](/solution/1900-1999/1908.Game%20of%20Nim/README.md)", "`\u4f4d\u8fd0\u7b97`,`\u8111\u7b4b\u6025\u8f6c\u5f2f`,`\u6570\u7ec4`,`\u6570\u5b66`,`\u52a8\u6001\u89c4\u5212`,`\u535a\u5f08`", "\u4e2d\u7b49", "\ud83d\udd12"], "md_table_row_en": ["[1908](https://leetcode.com/problems/game-of-nim)", "[Game of Nim](/solution/1900-1999/1908.Game%20of%20Nim/README_EN.md)", "`Bit Manipulation`,`Brainteaser`,`Array`,`Math`,`Dynamic Programming`,`Game Theory`", "Medium", "\ud83d\udd12"]}, {"question_id": "2057", "frontend_question_id": "1907", "paid_only": true, "paid_only_cn": true, "category": "Database", "url_cn": "https://leetcode-cn.com/problems/count-salary-categories", "url_en": "https://leetcode.com/problems/count-salary-categories", "relative_path_cn": "/solution/1900-1999/1907.Count%20Salary%20Categories/README.md", "relative_path_en": "/solution/1900-1999/1907.Count%20Salary%20Categories/README_EN.md", "title_cn": "\u6309\u5206\u7c7b\u7edf\u8ba1\u85aa\u6c34", "title_en": "Count Salary Categories", "question_title_slug": "count-salary-categories", "content_en": "Table: Accounts
\n\n\n+-------------+------+\n| Column Name | Type |\n+-------------+------+\n| account_id | int |\n| income | int |\n+-------------+------+\naccount_id is the primary key for this table.\nEach row contains information about the monthly income for one bank account.\n
\n\n
\n\nWrite an SQL query to report the number of bank accounts of each salary category. The salary categories are:
\n\n\n\t"Low Salary"
: All the salaries strictly less than $20000
. \n\t"Average Salary"
: All the salaries in the inclusive range [$20000, $50000]
. \n\t"High Salary"
: All the salaries strictly greater than $50000
. \n
\n\nThe result table must contain all three categories. If there are no accounts in a category, then report 0
. Return the result table in any order.
\n\nThe query result format is in the following example.
\n\n
\n\n\nAccounts table:\n+------------+--------+\n| account_id | income |\n+------------+--------+\n| 3 | 108939 |\n| 2 | 12747 |\n| 8 | 87709 |\n| 6 | 91796 |\n+------------+--------+\n\nResult table:\n+----------------+----------------+\n| category | accounts_count |\n+----------------+----------------+\n| Low Salary | 1 |\n| Average Salary | 0 |\n| High Salary | 3 |\n+----------------+----------------+\n\nLow Salary: Account 2.\nAverage Salary: No accounts.\nHigh Salary: Accounts 3, 6, and 8.\n
\n", "content_cn": "\u8868: Accounts
\n\n+-------------+------+\n| \u5217\u540d | \u7c7b\u578b |\n+-------------+------+\n| account_id | int |\n| income | int |\n+-------------+------+\naccount_id\u00a0\u662f\u8fd9\u4e2a\u8868\u7684\u4e3b\u952e\u3002\n\u6bcf\u4e00\u884c\u90fd\u5305\u542b\u4e00\u4e2a\u94f6\u884c\u5e10\u6237\u7684\u6708\u6536\u5165\u7684\u4fe1\u606f\u3002\n
\n\n\u5199\u51fa\u4e00\u4e2a\u00a0SQL\u00a0\u67e5\u8be2\uff0c\u6765\u62a5\u544a\u6bcf\u4e2a\u5de5\u8d44\u7c7b\u522b\u7684\u94f6\u884c\u8d26\u6237\u6570\u91cf\u3002\u00a0\u5de5\u8d44\u7c7b\u522b\u5982\u4e0b\uff1a
\n\n\n\n\u7ed3\u679c\u8868\u5fc5\u987b\u5305\u542b\u6240\u6709\u4e09\u4e2a\u7c7b\u522b\u3002\u00a0\u5982\u679c\u67d0\u4e2a\u7c7b\u522b\u4e2d\u6ca1\u6709\u5e10\u6237\uff0c\u5219\u62a5\u544a\u00a00\u3002
\n\n\u6309\u4efb\u610f\u987a\u5e8f\u8fd4\u56de\u7ed3\u679c\u8868\u3002
\n\n\u67e5\u8be2\u7ed3\u679c\u683c\u5f0f\u5982\u4e0b\u793a\u4f8b\uff1a
\n\nAccounts \u8868:\n+------------+--------+\n| account_id | income |\n+------------+--------+\n| 3 | 108939 |\n| 2 | 12747 |\n| 8 | 87709 |\n| 6 | 91796 |\n+------------+--------+\n\nResult \u8868:\n+----------------+----------------+\n| category | accounts_count |\n+----------------+----------------+\n| Low Salary | 1 |\n| Average Salary | 0 |\n| High Salary | 3 |\n+----------------+----------------+\n\n\u4f4e\u85aa: \u6570\u91cf\u4e3a 2.\n\u4e2d\u7b49\u85aa\u6c34: \u6ca1\u6709.\n\u9ad8\u85aa: \u6709\u4e09\u4e2a\u8d26\u6237\uff0c\u4ed6\u4eec\u662f 3, 6\u548c 8.\n
\n", "tags_en": ["Database"], "tags_cn": ["\u6570\u636e\u5e93"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "MySQL", "langSlug": "mysql", "code": "# Write your MySQL query statement below", "__typename": "CodeSnippetNode"}, {"lang": "MS SQL Server", "langSlug": "mssql", "code": "/* Write your T-SQL query statement below */", "__typename": "CodeSnippetNode"}, {"lang": "Oracle", "langSlug": "oraclesql", "code": "/* Write your PL/SQL query statement below */", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1907](https://leetcode-cn.com/problems/count-salary-categories)", "[\u6309\u5206\u7c7b\u7edf\u8ba1\u85aa\u6c34](/solution/1900-1999/1907.Count%20Salary%20Categories/README.md)", "`\u6570\u636e\u5e93`", "\u4e2d\u7b49", "\ud83d\udd12"], "md_table_row_en": ["[1907](https://leetcode.com/problems/count-salary-categories)", "[Count Salary Categories](/solution/1900-1999/1907.Count%20Salary%20Categories/README_EN.md)", "`Database`", "Medium", "\ud83d\udd12"]}, {"question_id": "2052", "frontend_question_id": "1902", "paid_only": true, "paid_only_cn": true, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/depth-of-bst-given-insertion-order", "url_en": "https://leetcode.com/problems/depth-of-bst-given-insertion-order", "relative_path_cn": "/solution/1900-1999/1902.Depth%20of%20BST%20Given%20Insertion%20Order/README.md", "relative_path_en": "/solution/1900-1999/1902.Depth%20of%20BST%20Given%20Insertion%20Order/README_EN.md", "title_cn": "Depth of BST Given Insertion Order", "title_en": "Depth of BST Given Insertion Order", "question_title_slug": "depth-of-bst-given-insertion-order", "content_en": "You are given a 0-indexed integer array order
of length n
, a permutation of integers from 1
to n
representing the order of insertion into a binary search tree.
\r\n\r\nA binary search tree is defined as follows:
\r\n\r\n\r\n\t- The left subtree of a node contains only nodes with keys less than the node's key.
\r\n\t- The right subtree of a node contains only nodes with keys greater than the node's key.
\r\n\t- Both the left and right subtrees must also be binary search trees.
\r\n
\r\n\r\nThe binary search tree is constructed as follows:
\r\n\r\n\r\n\torder[0]
will be the root of the binary search tree. \r\n\t- All subsequent elements are inserted as the child of any existing node such that the binary search tree properties hold.
\r\n
\r\n\r\nReturn the depth of the binary search tree.
\r\n\r\nA binary tree's depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
\r\n\r\n
\r\nExample 1:
\r\n
\r\n\r\nInput: order = [2,1,4,3]\r\nOutput: 3\r\nExplanation: The binary search tree has a depth of 3 with path 2->3->4.\r\n
\r\n\r\nExample 2:
\r\n
\r\n\r\nInput: order = [2,1,3,4]\r\nOutput: 3\r\nExplanation: The binary search tree has a depth of 3 with path 2->3->4.\r\n
\r\n\r\nExample 3:
\r\n
\r\n\r\nInput: order = [1,2,3,4]\r\nOutput: 4\r\nExplanation: The binary search tree has a depth of 4 with path 1->2->3->4.\r\n
\r\n\r\n
\r\nConstraints:
\r\n\r\n\r\n\tn == order.length
\r\n\t1 <= n <= 105
\r\n\torder
is a permutation of integers between 1
and n
. \r\n
", "content_cn": "You are given a 0-indexed integer array order
of length n
, a permutation of integers from 1
to n
representing the order of insertion into a binary search tree.
\r\n\r\nA binary search tree is defined as follows:
\r\n\r\n\r\n\t- The left subtree of a node contains only nodes with keys less than the node's key.
\r\n\t- The right subtree of a node contains only nodes with keys greater than the node's key.
\r\n\t- Both the left and right subtrees must also be binary search trees.
\r\n
\r\n\r\nThe binary search tree is constructed as follows:
\r\n\r\n\r\n\torder[0]
will be the root of the binary search tree. \r\n\t- All subsequent elements are inserted as the child of any existing node such that the binary search tree properties hold.
\r\n
\r\n\r\nReturn the depth of the binary search tree.
\r\n\r\nA binary tree's depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
\r\n\r\n
\r\nExample 1:
\r\n
\r\n\r\nInput: order = [2,1,4,3]\r\nOutput: 3\r\nExplanation: The binary search tree has a depth of 3 with path 2->3->4.\r\n
\r\n\r\nExample 2:
\r\n
\r\n\r\nInput: order = [2,1,3,4]\r\nOutput: 3\r\nExplanation: The binary search tree has a depth of 3 with path 2->3->4.\r\n
\r\n\r\nExample 3:
\r\n
\r\n\r\nInput: order = [1,2,3,4]\r\nOutput: 4\r\nExplanation: The binary search tree has a depth of 4 with path 1->2->3->4.\r\n
\r\n\r\n
\r\nConstraints:
\r\n\r\n\r\n\tn == order.length
\r\n\t1 <= n <= 105
\r\n\torder
is a permutation of integers between 1
and n
. \r\n
", "tags_en": ["Tree", "Binary Search Tree", "Binary Tree", "Ordered Set"], "tags_cn": ["\u6811", "\u4e8c\u53c9\u641c\u7d22\u6811", "\u4e8c\u53c9\u6811", "\u6709\u5e8f\u96c6\u5408"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n int maxDepthBST(vector& order) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int maxDepthBST(int[] order) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def maxDepthBST(self, order):\n \"\"\"\n :type order: List[int]\n :rtype: int\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def maxDepthBST(self, order: List[int]) -> int:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nint maxDepthBST(int* order, int orderSize){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int MaxDepthBST(int[] order) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[]} order\n * @return {number}\n */\nvar maxDepthBST = function(order) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer[]} order\n# @return {Integer}\ndef max_depth_bst(order)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func maxDepthBST(_ order: [Int]) -> Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func maxDepthBST(order []int) int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def maxDepthBST(order: Array[Int]): Int = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun maxDepthBST(order: IntArray): Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn max_depth_bst(order: Vec) -> i32 {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer[] $order\n * @return Integer\n */\n function maxDepthBST($order) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function maxDepthBST(order: number[]): number {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (max-depth-bst order)\n (-> (listof exact-integer?) exact-integer?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec max_depth_bst(Order :: [integer()]) -> integer().\nmax_depth_bst(Order) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec max_depth_bst(order :: [integer]) :: integer\n def max_depth_bst(order) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1902](https://leetcode-cn.com/problems/depth-of-bst-given-insertion-order)", "[Depth of BST Given Insertion Order](/solution/1900-1999/1902.Depth%20of%20BST%20Given%20Insertion%20Order/README.md)", "`\u6811`,`\u4e8c\u53c9\u641c\u7d22\u6811`,`\u4e8c\u53c9\u6811`,`\u6709\u5e8f\u96c6\u5408`", "\u4e2d\u7b49", "\ud83d\udd12"], "md_table_row_en": ["[1902](https://leetcode.com/problems/depth-of-bst-given-insertion-order)", "[Depth of BST Given Insertion Order](/solution/1900-1999/1902.Depth%20of%20BST%20Given%20Insertion%20Order/README_EN.md)", "`Tree`,`Binary Search Tree`,`Binary Tree`,`Ordered Set`", "Medium", "\ud83d\udd12"]}, {"question_id": "2051", "frontend_question_id": "1923", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/longest-common-subpath", "url_en": "https://leetcode.com/problems/longest-common-subpath", "relative_path_cn": "/solution/1900-1999/1923.Longest%20Common%20Subpath/README.md", "relative_path_en": "/solution/1900-1999/1923.Longest%20Common%20Subpath/README_EN.md", "title_cn": "\u6700\u957f\u516c\u5171\u5b50\u8def\u5f84", "title_en": "Longest Common Subpath", "question_title_slug": "longest-common-subpath", "content_en": "There is a country of n
cities numbered from 0
to n - 1
. In this country, there is a road connecting every pair of cities.
\n\nThere are m
friends numbered from 0
to m - 1
who are traveling through the country. Each one of them will take a path consisting of some cities. Each path is represented by an integer array that contains the visited cities in order. The path may contain a city more than once, but the same city will not be listed consecutively.
\n\nGiven an integer n
and a 2D integer array paths
where paths[i]
is an integer array representing the path of the ith
friend, return the length of the longest common subpath that is shared by every friend's path, or 0
if there is no common subpath at all.
\n\nA subpath of a path is a contiguous sequence of cities within that path.
\n\n
\nExample 1:
\n\n\nInput: n = 5, paths = [[0,1,2,3,4],\n [2,3,4],\n [4,0,1,2,3]]\nOutput: 2\nExplanation: The longest common subpath is [2,3].\n
\n\nExample 2:
\n\n\nInput: n = 3, paths = [[0],[1],[2]]\nOutput: 0\nExplanation: There is no common subpath shared by the three paths.\n
\n\nExample 3:
\n\n\nInput: n = 5, paths = [[0,1,2,3,4],\n [4,3,2,1,0]]\nOutput: 1\nExplanation: The possible longest common subpaths are [0], [1], [2], [3], and [4]. All have a length of 1.
\n\n
\nConstraints:
\n\n\n\t1 <= n <= 105
\n\tm == paths.length
\n\t2 <= m <= 105
\n\tsum(paths[i].length) <= 105
\n\t0 <= paths[i][j] < n
\n\t- The same city is not listed multiple times consecutively in
paths[i]
. \n
\n", "content_cn": "\u4e00\u4e2a\u56fd\u5bb6\u7531 n
\u00a0\u4e2a\u7f16\u53f7\u4e3a 0
\u00a0\u5230 n - 1
\u00a0\u7684\u57ce\u5e02\u7ec4\u6210\u3002\u5728\u8fd9\u4e2a\u56fd\u5bb6\u91cc\uff0c\u6bcf\u4e24\u4e2a\u00a0\u57ce\u5e02\u4e4b\u95f4\u90fd\u6709\u4e00\u6761\u9053\u8def\u8fde\u63a5\u3002
\n\n\u603b\u5171\u6709 m
\u00a0\u4e2a\u7f16\u53f7\u4e3a 0
\u00a0\u5230 m - 1
\u00a0\u7684\u670b\u53cb\u60f3\u5728\u8fd9\u4e2a\u56fd\u5bb6\u65c5\u6e38\u3002\u4ed6\u4eec\u6bcf\u4e00\u4e2a\u4eba\u7684\u8def\u5f84\u90fd\u4f1a\u5305\u542b\u4e00\u4e9b\u57ce\u5e02\u3002\u6bcf\u6761\u8def\u5f84\u90fd\u7531\u4e00\u4e2a\u6574\u6570\u6570\u7ec4\u8868\u793a\uff0c\u6bcf\u4e2a\u6574\u6570\u6570\u7ec4\u8868\u793a\u4e00\u4e2a\u670b\u53cb\u6309\u987a\u5e8f\u8bbf\u95ee\u8fc7\u7684\u57ce\u5e02\u5e8f\u5217\u3002\u540c\u4e00\u4e2a\u57ce\u5e02\u5728\u4e00\u6761\u8def\u5f84\u4e2d\u53ef\u80fd \u91cd\u590d \u51fa\u73b0\uff0c\u4f46\u540c\u4e00\u4e2a\u57ce\u5e02\u5728\u4e00\u6761\u8def\u5f84\u4e2d\u4e0d\u4f1a\u8fde\u7eed\u51fa\u73b0\u3002
\n\n\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u00a0n
\u00a0\u548c\u4e8c\u7ef4\u6570\u7ec4\u00a0paths
\u00a0\uff0c\u5176\u4e2d\u00a0paths[i]
\u00a0\u662f\u4e00\u4e2a\u6574\u6570\u6570\u7ec4\uff0c\u8868\u793a\u7b2c i
\u00a0\u4e2a\u670b\u53cb\u8d70\u8fc7\u7684\u8def\u5f84\uff0c\u8bf7\u4f60\u8fd4\u56de \u6bcf\u4e00\u4e2a\u00a0\u670b\u53cb\u90fd\u8d70\u8fc7\u7684 \u6700\u957f\u516c\u5171\u5b50\u8def\u5f84\u00a0\u7684\u957f\u5ea6\uff0c\u5982\u679c\u4e0d\u5b58\u5728\u516c\u5171\u5b50\u8def\u5f84\uff0c\u8bf7\u4f60\u8fd4\u56de 0
\u00a0\u3002
\n\n\u4e00\u4e2a \u5b50\u8def\u5f84 \u6307\u7684\u662f\u4e00\u6761\u8def\u5f84\u4e2d\u8fde\u7eed\u7684\u57ce\u5e02\u5e8f\u5217\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1an = 5, paths = [[0,1,2,3,4],\n [2,3,4],\n [4,0,1,2,3]]\n\u8f93\u51fa\uff1a2\n\u89e3\u91ca\uff1a\u6700\u957f\u516c\u5171\u5b50\u8def\u5f84\u4e3a [2,3] \u3002\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1an = 3, paths = [[0],[1],[2]]\n\u8f93\u51fa\uff1a0\n\u89e3\u91ca\uff1a\u4e09\u6761\u8def\u5f84\u6ca1\u6709\u516c\u5171\u5b50\u8def\u5f84\u3002\n
\n\n\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1an = 5, paths = [[0,1,2,3,4],\n [4,3,2,1,0]]\n\u8f93\u51fa\uff1a1\n\u89e3\u91ca\uff1a\u6700\u957f\u516c\u5171\u5b50\u8def\u5f84\u4e3a [0]\uff0c[1]\uff0c[2]\uff0c[3] \u548c [4] \u3002\u5b83\u4eec\u957f\u5ea6\u90fd\u4e3a 1 \u3002
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\t1 <= n <= 105
\n\tm == paths.length
\n\t2 <= m <= 105
\n\tsum(paths[i].length) <= 105
\n\t0 <= paths[i][j] < n
\n\tpaths[i]
\u00a0\u4e2d\u540c\u4e00\u4e2a\u57ce\u5e02\u4e0d\u4f1a\u8fde\u7eed\u91cd\u590d\u51fa\u73b0\u3002 \n
\n", "tags_en": ["Array", "Binary Search", "Suffix Array", "Hash Function", "Rolling Hash"], "tags_cn": ["\u6570\u7ec4", "\u4e8c\u5206\u67e5\u627e", "\u540e\u7f00\u6570\u7ec4", "\u54c8\u5e0c\u51fd\u6570", "\u6eda\u52a8\u54c8\u5e0c"], "difficulty_en": "Hard", "difficulty_cn": "\u56f0\u96be", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n int longestCommonSubpath(int n, vector>& paths) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int longestCommonSubpath(int n, int[][] paths) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def longestCommonSubpath(self, n, paths):\n \"\"\"\n :type n: int\n :type paths: List[List[int]]\n :rtype: int\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def longestCommonSubpath(self, n: int, paths: List[List[int]]) -> int:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nint longestCommonSubpath(int n, int** paths, int pathsSize, int* pathsColSize){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int LongestCommonSubpath(int n, int[][] paths) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number} n\n * @param {number[][]} paths\n * @return {number}\n */\nvar longestCommonSubpath = function(n, paths) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer} n\n# @param {Integer[][]} paths\n# @return {Integer}\ndef longest_common_subpath(n, paths)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func longestCommonSubpath(_ n: Int, _ paths: [[Int]]) -> Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func longestCommonSubpath(n int, paths [][]int) int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def longestCommonSubpath(n: Int, paths: Array[Array[Int]]): Int = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun longestCommonSubpath(n: Int, paths: Array): Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn longest_common_subpath(n: i32, paths: Vec>) -> i32 {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer $n\n * @param Integer[][] $paths\n * @return Integer\n */\n function longestCommonSubpath($n, $paths) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function longestCommonSubpath(n: number, paths: number[][]): number {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (longest-common-subpath n paths)\n (-> exact-integer? (listof (listof exact-integer?)) exact-integer?)\n\n )", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1923](https://leetcode-cn.com/problems/longest-common-subpath)", "[\u6700\u957f\u516c\u5171\u5b50\u8def\u5f84](/solution/1900-1999/1923.Longest%20Common%20Subpath/README.md)", "`\u6570\u7ec4`,`\u4e8c\u5206\u67e5\u627e`,`\u540e\u7f00\u6570\u7ec4`,`\u54c8\u5e0c\u51fd\u6570`,`\u6eda\u52a8\u54c8\u5e0c`", "\u56f0\u96be", ""], "md_table_row_en": ["[1923](https://leetcode.com/problems/longest-common-subpath)", "[Longest Common Subpath](/solution/1900-1999/1923.Longest%20Common%20Subpath/README_EN.md)", "`Array`,`Binary Search`,`Suffix Array`,`Hash Function`,`Rolling Hash`", "Hard", ""]}, {"question_id": "2050", "frontend_question_id": "1922", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/count-good-numbers", "url_en": "https://leetcode.com/problems/count-good-numbers", "relative_path_cn": "/solution/1900-1999/1922.Count%20Good%20Numbers/README.md", "relative_path_en": "/solution/1900-1999/1922.Count%20Good%20Numbers/README_EN.md", "title_cn": "\u7edf\u8ba1\u597d\u6570\u5b57\u7684\u6570\u76ee", "title_en": "Count Good Numbers", "question_title_slug": "count-good-numbers", "content_en": "A digit string is good if the digits (0-indexed) at even indices are even and the digits at odd indices are prime (2
, 3
, 5
, or 7
).
\n\n\n\t- For example,
"2582"
is good because the digits (2
and 8
) at even positions are even and the digits (5
and 2
) at odd positions are prime. However, "3245"
is not good because 3
is at an even index but is not even. \n
\n\nGiven an integer n
, return the total number of good digit strings of length n
. Since the answer may be large, return it modulo 109 + 7
.
\n\nA digit string is a string consisting of digits 0
through 9
that may contain leading zeros.
\n\n
\nExample 1:
\n\n\nInput: n = 1\nOutput: 5\nExplanation: The good numbers of length 1 are "0", "2", "4", "6", "8".\n
\n\nExample 2:
\n\n\nInput: n = 4\nOutput: 400\n
\n\nExample 3:
\n\n\nInput: n = 50\nOutput: 564908303\n
\n\n
\nConstraints:
\n\n\n", "content_cn": "\u6211\u4eec\u79f0\u4e00\u4e2a\u6570\u5b57\u5b57\u7b26\u4e32\u662f \u597d\u6570\u5b57 \u5f53\u5b83\u6ee1\u8db3\uff08\u4e0b\u6807\u4ece 0\u00a0\u5f00\u59cb\uff09\u5076\u6570 \u4e0b\u6807\u5904\u7684\u6570\u5b57\u4e3a \u5076\u6570\u00a0\u4e14 \u5947\u6570\u00a0\u4e0b\u6807\u5904\u7684\u6570\u5b57\u4e3a \u8d28\u6570\u00a0\uff082
\uff0c3
\uff0c5
\u00a0\u6216\u00a07
\uff09\u3002
\n\n\n\t- \u6bd4\u65b9\u8bf4\uff0c
\"2582\"
\u00a0\u662f\u597d\u6570\u5b57\uff0c\u56e0\u4e3a\u5076\u6570\u4e0b\u6807\u5904\u7684\u6570\u5b57\uff082
\u00a0\u548c\u00a08
\uff09\u662f\u5076\u6570\u4e14\u5947\u6570\u4e0b\u6807\u5904\u7684\u6570\u5b57\uff085
\u548c\u00a02
\uff09\u4e3a\u8d28\u6570\u3002\u4f46\u00a0\"3245\"
\u00a0\u4e0d\u662f \u597d\u6570\u5b57\uff0c\u56e0\u4e3a\u00a03
\u00a0\u5728\u5076\u6570\u4e0b\u6807\u5904\u4f46\u4e0d\u662f\u5076\u6570\u3002 \n
\n\n\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u00a0n
\u00a0\uff0c\u8bf7\u4f60\u8fd4\u56de\u957f\u5ea6\u4e3a\u00a0n
\u00a0\u4e14\u4e3a\u597d\u6570\u5b57\u7684\u6570\u5b57\u5b57\u7b26\u4e32\u00a0\u603b\u6570\u00a0\u3002\u7531\u4e8e\u7b54\u6848\u53ef\u80fd\u4f1a\u5f88\u5927\uff0c\u8bf7\u4f60\u5c06\u5b83\u5bf9\u00a0109 + 7
\u00a0\u53d6\u4f59\u540e\u8fd4\u56de\u00a0\u3002
\n\n\u4e00\u4e2a \u6570\u5b57\u5b57\u7b26\u4e32\u00a0\u662f\u6bcf\u4e00\u4f4d\u90fd\u7531\u00a00
\u00a0\u5230 9
\u00a0\u7ec4\u6210\u7684\u5b57\u7b26\u4e32\uff0c\u4e14\u53ef\u80fd\u5305\u542b\u524d\u5bfc 0 \u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1an = 1\n\u8f93\u51fa\uff1a5\n\u89e3\u91ca\uff1a\u957f\u5ea6\u4e3a 1 \u7684\u597d\u6570\u5b57\u5305\u62ec \"0\"\uff0c\"2\"\uff0c\"4\"\uff0c\"6\"\uff0c\"8\" \u3002\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1an = 4\n\u8f93\u51fa\uff1a400\n
\n\n\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1an = 50\n\u8f93\u51fa\uff1a564908303\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n", "tags_en": ["Recursion", "Math"], "tags_cn": ["\u9012\u5f52", "\u6570\u5b66"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n int countGoodNumbers(long long n) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int countGoodNumbers(long n) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def countGoodNumbers(self, n):\n \"\"\"\n :type n: int\n :rtype: int\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def countGoodNumbers(self, n: int) -> int:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nint countGoodNumbers(long long n){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int CountGoodNumbers(long n) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number} n\n * @return {number}\n */\nvar countGoodNumbers = function(n) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer} n\n# @return {Integer}\ndef count_good_numbers(n)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func countGoodNumbers(_ n: Int) -> Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func countGoodNumbers(n int64) int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def countGoodNumbers(n: Long): Int = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun countGoodNumbers(n: Long): Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn count_good_numbers(n: i64) -> i32 {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer $n\n * @return Integer\n */\n function countGoodNumbers($n) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function countGoodNumbers(n: number): number {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (count-good-numbers n)\n (-> exact-integer? exact-integer?)\n\n )", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1922](https://leetcode-cn.com/problems/count-good-numbers)", "[\u7edf\u8ba1\u597d\u6570\u5b57\u7684\u6570\u76ee](/solution/1900-1999/1922.Count%20Good%20Numbers/README.md)", "`\u9012\u5f52`,`\u6570\u5b66`", "\u4e2d\u7b49", ""], "md_table_row_en": ["[1922](https://leetcode.com/problems/count-good-numbers)", "[Count Good Numbers](/solution/1900-1999/1922.Count%20Good%20Numbers/README_EN.md)", "`Recursion`,`Math`", "Medium", ""]}, {"question_id": "2049", "frontend_question_id": "1921", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/eliminate-maximum-number-of-monsters", "url_en": "https://leetcode.com/problems/eliminate-maximum-number-of-monsters", "relative_path_cn": "/solution/1900-1999/1921.Eliminate%20Maximum%20Number%20of%20Monsters/README.md", "relative_path_en": "/solution/1900-1999/1921.Eliminate%20Maximum%20Number%20of%20Monsters/README_EN.md", "title_cn": "\u6d88\u706d\u602a\u7269\u7684\u6700\u5927\u6570\u91cf", "title_en": "Eliminate Maximum Number of Monsters", "question_title_slug": "eliminate-maximum-number-of-monsters", "content_en": "You are playing a video game where you are defending your city from a group of n
monsters. You are given a 0-indexed integer array dist
of size n
, where dist[i]
is the initial distance in meters of the ith
monster from the city.
\n\nThe monsters walk toward the city at a constant speed. The speed of each monster is given to you in an integer array speed
of size n
, where speed[i]
is the speed of the ith
monster in meters per minute.
\n\nThe monsters start moving at minute 0. You have a weapon that you can choose to use at the start of every minute, including minute 0. You cannot use the weapon in the middle of a minute. The weapon can eliminate any monster that is still alive. You lose when any monster reaches your city. If a monster reaches the city exactly at the start of a minute, it counts as a loss, and the game ends before you can use your weapon in that minute.
\n\nReturn the maximum number of monsters that you can eliminate before you lose, or n
if you can eliminate all the monsters before they reach the city.
\n\n
\nExample 1:
\n\n\nInput: dist = [1,3,4], speed = [1,1,1]\nOutput: 3\nExplanation:\nAt the start of minute 0, the distances of the monsters are [1,3,4], you eliminate the first monster.\nAt the start of minute 1, the distances of the monsters are [X,2,3], you don't do anything.\nAt the start of minute 2, the distances of the monsters are [X,1,2], you eliminate the second monster.\nAt the start of minute 3, the distances of the monsters are [X,X,1], you eliminate the third monster.\nAll 3 monsters can be eliminated.
\n\nExample 2:
\n\n\nInput: dist = [1,1,2,3], speed = [1,1,1,1]\nOutput: 1\nExplanation:\nAt the start of minute 0, the distances of the monsters are [1,1,2,3], you eliminate the first monster.\nAt the start of minute 1, the distances of the monsters are [X,0,1,2], so you lose.\nYou can only eliminate 1 monster.\n
\n\nExample 3:
\n\n\nInput: dist = [3,2,4], speed = [5,3,2]\nOutput: 1\nExplanation:\nAt the start of minute 0, the distances of the monsters are [3,2,4], you eliminate the first monster.\nAt the start of minute 1, the distances of the monsters are [X,0,2], so you lose.\nYou can only eliminate 1 monster.\n
\n\n
\nConstraints:
\n\n\n\tn == dist.length == speed.length
\n\t1 <= n <= 105
\n\t1 <= dist[i], speed[i] <= 105
\n
\n", "content_cn": "\u4f60\u6b63\u5728\u73a9\u4e00\u6b3e\u7535\u5b50\u6e38\u620f\uff0c\u5728\u6e38\u620f\u4e2d\u4f60\u9700\u8981\u4fdd\u62a4\u57ce\u5e02\u514d\u53d7\u602a\u7269\u4fb5\u88ad\u3002\u7ed9\u4f60\u4e00\u4e2a \u4e0b\u6807\u4ece 0 \u5f00\u59cb \u4e14\u957f\u5ea6\u4e3a n
\u7684\u6574\u6570\u6570\u7ec4 dist
\uff0c\u5176\u4e2d dist[i]
\u662f\u7b2c i
\u4e2a\u602a\u7269\u4e0e\u57ce\u5e02\u7684 \u521d\u59cb\u8ddd\u79bb\uff08\u5355\u4f4d\uff1a\u7c73\uff09\u3002
\n\n\u602a\u7269\u4ee5 \u6052\u5b9a \u7684\u901f\u5ea6\u8d70\u5411\u57ce\u5e02\u3002\u7ed9\u4f60\u4e00\u4e2a\u957f\u5ea6\u4e3a n
\u7684\u6574\u6570\u6570\u7ec4 speed
\u8868\u793a\u6bcf\u4e2a\u602a\u7269\u7684\u901f\u5ea6\uff0c\u5176\u4e2d speed[i]
\u662f\u7b2c i
\u4e2a\u602a\u7269\u7684\u901f\u5ea6\uff08\u5355\u4f4d\uff1a\u7c73/\u5206\uff09\u3002
\n\n\u602a\u7269\u4ece \u7b2c 0 \u5206\u949f \u65f6\u5f00\u59cb\u79fb\u52a8\u3002\u4f60\u6709\u4e00\u628a\u6b66\u5668\uff0c\u5e76\u53ef\u4ee5 \u9009\u62e9 \u5728\u6bcf\u4e00\u5206\u949f\u7684\u5f00\u59cb\u65f6\u4f7f\u7528\uff0c\u5305\u62ec\u7b2c 0 \u5206\u949f\u3002\u4f46\u662f\u4f60\u65e0\u6cd5\u5728\u4e00\u5206\u949f\u7684\u4e2d\u95f4\u4f7f\u7528\u6b66\u5668\u3002\u8fd9\u79cd\u6b66\u5668\u5a01\u529b\u60ca\u4eba\uff0c\u4e00\u6b21\u53ef\u4ee5\u6d88\u706d\u4efb\u4e00\u8fd8\u6d3b\u7740\u7684\u602a\u7269\u3002
\n\n\u4e00\u65e6\u4efb\u4e00\u602a\u7269\u5230\u8fbe\u57ce\u5e02\uff0c\u4f60\u5c31\u8f93\u6389\u4e86\u8fd9\u573a\u6e38\u620f\u3002\u5982\u679c\u67d0\u4e2a\u602a\u7269 \u6070 \u5728\u67d0\u4e00\u5206\u949f\u5f00\u59cb\u65f6\u5230\u8fbe\u57ce\u5e02\uff0c\u8fd9\u4f1a\u88ab\u89c6\u4e3a \u8f93\u6389\u00a0\u6e38\u620f\uff0c\u5728\u4f60\u53ef\u4ee5\u4f7f\u7528\u6b66\u5668\u4e4b\u524d\uff0c\u6e38\u620f\u5c31\u4f1a\u7ed3\u675f\u3002
\n\n\u8fd4\u56de\u5728\u4f60\u8f93\u6389\u6e38\u620f\u524d\u53ef\u4ee5\u6d88\u706d\u7684\u602a\u7269\u7684 \u6700\u5927 \u6570\u91cf\u3002\u5982\u679c\u4f60\u53ef\u4ee5\u5728\u6240\u6709\u602a\u7269\u5230\u8fbe\u57ce\u5e02\u524d\u5c06\u5b83\u4eec\u5168\u90e8\u6d88\u706d\uff0c\u8fd4\u56de\u00a0 n
\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1adist = [1,3,4], speed = [1,1,1]\n\u8f93\u51fa\uff1a3\n\u89e3\u91ca\uff1a\n\u7b2c 0 \u5206\u949f\u5f00\u59cb\u65f6\uff0c\u602a\u7269\u7684\u8ddd\u79bb\u662f [1,3,4]\uff0c\u4f60\u6d88\u706d\u4e86\u7b2c\u4e00\u4e2a\u602a\u7269\u3002\n\u7b2c 1 \u5206\u949f\u5f00\u59cb\u65f6\uff0c\u602a\u7269\u7684\u8ddd\u79bb\u662f [X,2,3]\uff0c\u4f60\u6ca1\u6709\u6d88\u706d\u4efb\u4f55\u602a\u7269\u3002\n\u7b2c 2 \u5206\u949f\u5f00\u59cb\u65f6\uff0c\u602a\u7269\u7684\u8ddd\u79bb\u662f [X,1,2]\uff0c\u4f60\u6d88\u706d\u4e86\u7b2c\u4e8c\u4e2a\u602a\u7269\u3002\n\u7b2c 3 \u5206\u949f\u5f00\u59cb\u65f6\uff0c\u602a\u7269\u7684\u8ddd\u79bb\u662f [X,X,1]\uff0c\u4f60\u6d88\u706d\u4e86\u7b2c\u4e09\u4e2a\u602a\u7269\u3002\n\u6240\u6709 3 \u4e2a\u602a\u7269\u90fd\u53ef\u4ee5\u88ab\u6d88\u706d\u3002
\n\n\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1adist = [1,1,2,3], speed = [1,1,1,1]\n\u8f93\u51fa\uff1a1\n\u89e3\u91ca\uff1a\n\u7b2c 0 \u5206\u949f\u5f00\u59cb\u65f6\uff0c\u602a\u7269\u7684\u8ddd\u79bb\u662f [1,1,2,3]\uff0c\u4f60\u6d88\u706d\u4e86\u7b2c\u4e00\u4e2a\u602a\u7269\u3002\n\u7b2c 1 \u5206\u949f\u5f00\u59cb\u65f6\uff0c\u602a\u7269\u7684\u8ddd\u79bb\u662f [X,0,1,2]\uff0c\u4f60\u8f93\u6389\u4e86\u6e38\u620f\u3002\n\u4f60\u53ea\u80fd\u6d88\u706d 1 \u4e2a\u602a\u7269\u3002\n
\n\n\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1adist = [3,2,4], speed = [5,3,2]\n\u8f93\u51fa\uff1a1\n\u89e3\u91ca\uff1a\n\u7b2c 0 \u5206\u949f\u5f00\u59cb\u65f6\uff0c\u602a\u7269\u7684\u8ddd\u79bb\u662f [3,2,4]\uff0c\u4f60\u6d88\u706d\u4e86\u7b2c\u4e00\u4e2a\u602a\u7269\u3002\n\u7b2c 1 \u5206\u949f\u5f00\u59cb\u65f6\uff0c\u602a\u7269\u7684\u8ddd\u79bb\u662f [X,0,2]\uff0c\u4f60\u8f93\u6389\u4e86\u6e38\u620f\u3002 \n\u4f60\u53ea\u80fd\u6d88\u706d 1 \u4e2a\u602a\u7269\u3002\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\tn == dist.length == speed.length
\n\t1 <= n <= 105
\n\t1 <= dist[i], speed[i] <= 105
\n
\n", "tags_en": ["Greedy", "Array", "Sorting"], "tags_cn": ["\u8d2a\u5fc3", "\u6570\u7ec4", "\u6392\u5e8f"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n int eliminateMaximum(vector& dist, vector& speed) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int eliminateMaximum(int[] dist, int[] speed) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def eliminateMaximum(self, dist, speed):\n \"\"\"\n :type dist: List[int]\n :type speed: List[int]\n :rtype: int\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def eliminateMaximum(self, dist: List[int], speed: List[int]) -> int:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nint eliminateMaximum(int* dist, int distSize, int* speed, int speedSize){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int EliminateMaximum(int[] dist, int[] speed) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[]} dist\n * @param {number[]} speed\n * @return {number}\n */\nvar eliminateMaximum = function(dist, speed) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer[]} dist\n# @param {Integer[]} speed\n# @return {Integer}\ndef eliminate_maximum(dist, speed)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func eliminateMaximum(_ dist: [Int], _ speed: [Int]) -> Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func eliminateMaximum(dist []int, speed []int) int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def eliminateMaximum(dist: Array[Int], speed: Array[Int]): Int = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun eliminateMaximum(dist: IntArray, speed: IntArray): Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn eliminate_maximum(dist: Vec, speed: Vec) -> i32 {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer[] $dist\n * @param Integer[] $speed\n * @return Integer\n */\n function eliminateMaximum($dist, $speed) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function eliminateMaximum(dist: number[], speed: number[]): number {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (eliminate-maximum dist speed)\n (-> (listof exact-integer?) (listof exact-integer?) exact-integer?)\n\n )", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1921](https://leetcode-cn.com/problems/eliminate-maximum-number-of-monsters)", "[\u6d88\u706d\u602a\u7269\u7684\u6700\u5927\u6570\u91cf](/solution/1900-1999/1921.Eliminate%20Maximum%20Number%20of%20Monsters/README.md)", "`\u8d2a\u5fc3`,`\u6570\u7ec4`,`\u6392\u5e8f`", "\u4e2d\u7b49", ""], "md_table_row_en": ["[1921](https://leetcode.com/problems/eliminate-maximum-number-of-monsters)", "[Eliminate Maximum Number of Monsters](/solution/1900-1999/1921.Eliminate%20Maximum%20Number%20of%20Monsters/README_EN.md)", "`Greedy`,`Array`,`Sorting`", "Medium", ""]}, {"question_id": "2048", "frontend_question_id": "1920", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/build-array-from-permutation", "url_en": "https://leetcode.com/problems/build-array-from-permutation", "relative_path_cn": "/solution/1900-1999/1920.Build%20Array%20from%20Permutation/README.md", "relative_path_en": "/solution/1900-1999/1920.Build%20Array%20from%20Permutation/README_EN.md", "title_cn": "\u57fa\u4e8e\u6392\u5217\u6784\u5efa\u6570\u7ec4", "title_en": "Build Array from Permutation", "question_title_slug": "build-array-from-permutation", "content_en": "Given a zero-based permutation nums
(0-indexed), build an array ans
of the same length where ans[i] = nums[nums[i]]
for each 0 <= i < nums.length
and return it.
\n\nA zero-based permutation nums
is an array of distinct integers from 0
to nums.length - 1
(inclusive).
\n\n
\nExample 1:
\n\n\nInput: nums = [0,2,1,5,3,4]\nOutput: [0,1,2,4,5,3]\nExplanation: The array ans is built as follows: \nans = [nums[nums[0]], nums[nums[1]], nums[nums[2]], nums[nums[3]], nums[nums[4]], nums[nums[5]]]\n = [nums[0], nums[2], nums[1], nums[5], nums[3], nums[4]]\n = [0,1,2,4,5,3]
\n\nExample 2:
\n\n\nInput: nums = [5,0,1,2,3,4]\nOutput: [4,5,0,1,2,3]\nExplanation: The array ans is built as follows:\nans = [nums[nums[0]], nums[nums[1]], nums[nums[2]], nums[nums[3]], nums[nums[4]], nums[nums[5]]]\n = [nums[5], nums[0], nums[1], nums[2], nums[3], nums[4]]\n = [4,5,0,1,2,3]
\n\n
\nConstraints:
\n\n\n\t1 <= nums.length <= 1000
\n\t0 <= nums[i] < nums.length
\n\t- The elements in
nums
are distinct. \n
\n\n
\nFollow-up: Can you solve it without using an extra space (i.e., O(1)
memory)?
\n", "content_cn": "\u7ed9\u4f60\u4e00\u4e2a \u4ece 0 \u5f00\u59cb\u7684\u6392\u5217 nums
\uff08\u4e0b\u6807\u4e5f\u4ece 0 \u5f00\u59cb\uff09\u3002\u8bf7\u4f60\u6784\u5efa\u4e00\u4e2a \u540c\u6837\u957f\u5ea6 \u7684\u6570\u7ec4 ans
\uff0c\u5176\u4e2d\uff0c\u5bf9\u4e8e\u6bcf\u4e2a i
\uff080 <= i < nums.length
\uff09\uff0c\u90fd\u6ee1\u8db3 ans[i] = nums[nums[i]]
\u3002\u8fd4\u56de\u6784\u5efa\u597d\u7684\u6570\u7ec4 ans
\u3002
\n\n\u4ece 0 \u5f00\u59cb\u7684\u6392\u5217 nums
\u662f\u4e00\u4e2a\u7531 0
\u5230\u00a0nums.length - 1
\uff080
\u548c nums.length - 1
\u4e5f\u5305\u542b\u5728\u5185\uff09\u7684\u4e0d\u540c\u6574\u6570\u7ec4\u6210\u7684\u6570\u7ec4\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1anums = [0,2,1,5,3,4]\n\u8f93\u51fa\uff1a[0,1,2,4,5,3]\n\u89e3\u91ca\uff1a\u6570\u7ec4 ans \u6784\u5efa\u5982\u4e0b\uff1a\nans = [nums[nums[0]], nums[nums[1]], nums[nums[2]], nums[nums[3]], nums[nums[4]], nums[nums[5]]]\n = [nums[0], nums[2], nums[1], nums[5], nums[3], nums[4]]\n = [0,1,2,4,5,3]
\n\n\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1anums = [5,0,1,2,3,4]\n\u8f93\u51fa\uff1a[4,5,0,1,2,3]\n\u89e3\u91ca\uff1a\u6570\u7ec4 ans \u6784\u5efa\u5982\u4e0b\uff1a\nans = [nums[nums[0]], nums[nums[1]], nums[nums[2]], nums[nums[3]], nums[nums[4]], nums[nums[5]]]\n = [nums[5], nums[0], nums[1], nums[2], nums[3], nums[4]]\n = [4,5,0,1,2,3]
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\t1 <= nums.length <= 1000
\n\t0 <= nums[i] < nums.length
\n\tnums
\u4e2d\u7684\u5143\u7d20 \u4e92\u4e0d\u76f8\u540c \n
\n", "tags_en": ["Array", "Simulation"], "tags_cn": ["\u6570\u7ec4", "\u6a21\u62df"], "difficulty_en": "Easy", "difficulty_cn": "\u7b80\u5355", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n vector buildArray(vector& nums) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int[] buildArray(int[] nums) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def buildArray(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: List[int]\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def buildArray(self, nums: List[int]) -> List[int]:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\n/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\nint* buildArray(int* nums, int numsSize, int* returnSize){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int[] BuildArray(int[] nums) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[]} nums\n * @return {number[]}\n */\nvar buildArray = function(nums) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer[]} nums\n# @return {Integer[]}\ndef build_array(nums)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func buildArray(_ nums: [Int]) -> [Int] {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func buildArray(nums []int) []int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def buildArray(nums: Array[Int]): Array[Int] = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun buildArray(nums: IntArray): IntArray {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn build_array(nums: Vec) -> Vec {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @return Integer[]\n */\n function buildArray($nums) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function buildArray(nums: number[]): number[] {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (build-array nums)\n (-> (listof exact-integer?) (listof exact-integer?))\n\n )", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1920](https://leetcode-cn.com/problems/build-array-from-permutation)", "[\u57fa\u4e8e\u6392\u5217\u6784\u5efa\u6570\u7ec4](/solution/1900-1999/1920.Build%20Array%20from%20Permutation/README.md)", "`\u6570\u7ec4`,`\u6a21\u62df`", "\u7b80\u5355", ""], "md_table_row_en": ["[1920](https://leetcode.com/problems/build-array-from-permutation)", "[Build Array from Permutation](/solution/1900-1999/1920.Build%20Array%20from%20Permutation/README_EN.md)", "`Array`,`Simulation`", "Easy", ""]}, {"question_id": "2047", "frontend_question_id": "1901", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/find-a-peak-element-ii", "url_en": "https://leetcode.com/problems/find-a-peak-element-ii", "relative_path_cn": "/solution/1900-1999/1901.Find%20a%20Peak%20Element%20II/README.md", "relative_path_en": "/solution/1900-1999/1901.Find%20a%20Peak%20Element%20II/README_EN.md", "title_cn": "\u627e\u51fa\u9876\u5cf0\u5143\u7d20 II", "title_en": "Find a Peak Element II", "question_title_slug": "find-a-peak-element-ii", "content_en": "A peak element in a 2D grid is an element that is strictly greater than all of its adjacent neighbors to the left, right, top, and bottom.
\n\nGiven a 0-indexed m x n
matrix mat
where no two adjacent cells are equal, find any peak element mat[i][j]
and return the length 2 array [i,j]
.
\n\nYou may assume that the entire matrix is surrounded by an outer perimeter with the value -1
in each cell.
\n\nYou must write an algorithm that runs in O(m log(n))
or O(n log(m))
time.
\n\n
\nExample 1:
\n\n
\n\n\nInput: mat = [[1,4],[3,2]]\nOutput: [0,1]\nExplanation: Both 3 and 4 are peak elements so [1,0] and [0,1] are both acceptable answers.\n
\n\nExample 2:
\n\n
\n\n\nInput: mat = [[10,20,15],[21,30,14],[7,16,32]]\nOutput: [1,1]\nExplanation: Both 30 and 32 are peak elements so [1,1] and [2,2] are both acceptable answers.\n
\n\n
\nConstraints:
\n\n\n\tm == mat.length
\n\tn == mat[i].length
\n\t1 <= m, n <= 500
\n\t1 <= mat[i][j] <= 105
\n\t- No two adjacent cells are equal.
\n
\n", "content_cn": "\u4e00\u4e2a 2D \u7f51\u683c\u4e2d\u7684 \u9876\u5cf0\u5143\u7d20 \u662f\u6307\u90a3\u4e9b \u4e25\u683c\u5927\u4e8e \u5176\u76f8\u90bb\u683c\u5b50(\u4e0a\u3001\u4e0b\u3001\u5de6\u3001\u53f3)\u7684\u5143\u7d20\u3002
\n\n\u7ed9\u4f60\u4e00\u4e2a \u4ece 0 \u5f00\u59cb\u7f16\u53f7 \u7684 m x n
\u77e9\u9635 mat
\uff0c\u5176\u4e2d\u4efb\u610f\u4e24\u4e2a\u76f8\u90bb\u683c\u5b50\u7684\u503c\u90fd \u4e0d\u76f8\u540c \u3002\u627e\u51fa \u4efb\u610f\u4e00\u4e2a \u9876\u5cf0\u5143\u7d20 mat[i][j]
\u5e76 \u8fd4\u56de\u5176\u4f4d\u7f6e [i,j]
\u3002
\n\n\u4f60\u53ef\u4ee5\u5047\u8bbe\u6574\u4e2a\u77e9\u9635\u5468\u8fb9\u73af\u7ed5\u7740\u4e00\u5708\u503c\u4e3a -1
\u7684\u683c\u5b50\u3002
\n\n\u8981\u6c42\u5fc5\u987b\u5199\u51fa\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(m log(n))
\u6216 O(n log(m))
\u7684\u7b97\u6cd5
\n\n\u00a0
\n\n\u00a0
\n\n\u793a\u4f8b 1:
\n\n
\n\n\n\u8f93\u5165: mat = [[1,4],[3,2]]\n\u8f93\u51fa: [0,1]\n\u89e3\u91ca:\u00a03\u548c4\u90fd\u662f\u9876\u5cf0\u5143\u7d20\uff0c\u6240\u4ee5[1,0]\u548c[0,1]\u90fd\u662f\u53ef\u63a5\u53d7\u7684\u7b54\u6848\u3002\n
\n\n\u793a\u4f8b 2:
\n\n
\n\n\n\u8f93\u5165: mat = [[10,20,15],[21,30,14],[7,16,32]]\n\u8f93\u51fa: [1,1]\n\u89e3\u91ca:\u00a030\u548c32\u90fd\u662f\u9876\u5cf0\u5143\u7d20\uff0c\u6240\u4ee5[1,1]\u548c[2,2]\u90fd\u662f\u53ef\u63a5\u53d7\u7684\u7b54\u6848\u3002\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\tm == mat.length
\n\tn == mat[i].length
\n\t1 <= m, n <= 500
\n\t1 <= mat[i][j] <= 105
\n\t- \u4efb\u610f\u4e24\u4e2a\u76f8\u90bb\u5143\u7d20\u5747\u4e0d\u76f8\u7b49.
\n
\n", "tags_en": ["Array", "Binary Search", "Divide and Conquer", "Matrix"], "tags_cn": ["\u6570\u7ec4", "\u4e8c\u5206\u67e5\u627e", "\u5206\u6cbb", "\u77e9\u9635"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n vector findPeakGrid(vector>& mat) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int[] findPeakGrid(int[][] mat) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def findPeakGrid(self, mat):\n \"\"\"\n :type mat: List[List[int]]\n :rtype: List[int]\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def findPeakGrid(self, mat: List[List[int]]) -> List[int]:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\n/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\nint* findPeakGrid(int** mat, int matSize, int* matColSize, int* returnSize){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int[] FindPeakGrid(int[][] mat) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[][]} mat\n * @return {number[]}\n */\nvar findPeakGrid = function(mat) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer[][]} mat\n# @return {Integer[]}\ndef find_peak_grid(mat)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func findPeakGrid(_ mat: [[Int]]) -> [Int] {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func findPeakGrid(mat [][]int) []int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def findPeakGrid(mat: Array[Array[Int]]): Array[Int] = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun findPeakGrid(mat: Array): IntArray {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn find_peak_grid(mat: Vec>) -> Vec {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer[][] $mat\n * @return Integer[]\n */\n function findPeakGrid($mat) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function findPeakGrid(mat: number[][]): number[] {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (find-peak-grid mat)\n (-> (listof (listof exact-integer?)) (listof exact-integer?))\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec find_peak_grid(Mat :: [[integer()]]) -> [integer()].\nfind_peak_grid(Mat) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec find_peak_grid(mat :: [[integer]]) :: [integer]\n def find_peak_grid(mat) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1901](https://leetcode-cn.com/problems/find-a-peak-element-ii)", "[\u627e\u51fa\u9876\u5cf0\u5143\u7d20 II](/solution/1900-1999/1901.Find%20a%20Peak%20Element%20II/README.md)", "`\u6570\u7ec4`,`\u4e8c\u5206\u67e5\u627e`,`\u5206\u6cbb`,`\u77e9\u9635`", "\u4e2d\u7b49", ""], "md_table_row_en": ["[1901](https://leetcode.com/problems/find-a-peak-element-ii)", "[Find a Peak Element II](/solution/1900-1999/1901.Find%20a%20Peak%20Element%20II/README_EN.md)", "`Array`,`Binary Search`,`Divide and Conquer`,`Matrix`", "Medium", ""]}, {"question_id": "2046", "frontend_question_id": "1892", "paid_only": true, "paid_only_cn": true, "category": "Database", "url_cn": "https://leetcode-cn.com/problems/page-recommendations-ii", "url_en": "https://leetcode.com/problems/page-recommendations-ii", "relative_path_cn": "/solution/1800-1899/1892.Page%20Recommendations%20II/README.md", "relative_path_en": "/solution/1800-1899/1892.Page%20Recommendations%20II/README_EN.md", "title_cn": "\u9875\u9762\u63a8\u8350\u2161", "title_en": "Page Recommendations II", "question_title_slug": "page-recommendations-ii", "content_en": "Table: Friendship
\r\n\r\n\r\n+---------------+---------+\r\n| Column Name | Type |\r\n+---------------+---------+\r\n| user1_id | int |\r\n| user2_id | int |\r\n+---------------+---------+\r\n(user1_id, user2_id) is the primary key for this table.\r\nEach row of this table indicates that the users user1_id and user2_id are friends.\r\n
\r\n\r\n
\r\n\r\nTable: Likes
\r\n\r\n\r\n+-------------+---------+\r\n| Column Name | Type |\r\n+-------------+---------+\r\n| user_id | int |\r\n| page_id | int |\r\n+-------------+---------+\r\n(user_id, page_id) is the primary key for this table.\r\nEach row of this table indicates that user_id likes page_id.\r\n
\r\n\r\n
\r\n\r\nYou are implementing a page recommendation system for a social media website. Your system will recommended a page to user_id
if the page is liked by at least one friend of user_id
and is not liked by user_id
.
\r\n\r\nWrite an SQL query to find all the possible page recommendations for every user. Each recommendation should appear as a row in the result table with these columns:
\r\n\r\n\r\n\tuser_id
: The ID of the user that your system is making the recommendation to. \r\n\tpage_id
: The ID of the page that will be recommended to user_id
. \r\n\tfriends_likes
: The number of the friends of user_id
that like page_id
. \r\n
\r\n\r\nReturn result table in any order.
\r\n\r\nThe query result format is in the following example:
\r\n\r\n
\r\n\r\n\r\nFriendship table:\r\n+----------+----------+\r\n| user1_id | user2_id |\r\n+----------+----------+\r\n| 1 | 2 |\r\n| 1 | 3 |\r\n| 1 | 4 |\r\n| 2 | 3 |\r\n| 2 | 4 |\r\n| 2 | 5 |\r\n| 6 | 1 |\r\n+----------+----------+\r\n \r\nLikes table:\r\n+---------+---------+\r\n| user_id | page_id |\r\n+---------+---------+\r\n| 1 | 88 |\r\n| 2 | 23 |\r\n| 3 | 24 |\r\n| 4 | 56 |\r\n| 5 | 11 |\r\n| 6 | 33 |\r\n| 2 | 77 |\r\n| 3 | 77 |\r\n| 6 | 88 |\r\n+---------+---------+\r\n\r\nResult table:\r\n+---------+---------+---------------+\r\n| user_id | page_id | friends_likes |\r\n+---------+---------+---------------+\r\n| 1 | 77 | 2 |\r\n| 1 | 23 | 1 |\r\n| 1 | 24 | 1 |\r\n| 1 | 56 | 1 |\r\n| 1 | 33 | 1 |\r\n| 2 | 24 | 1 |\r\n| 2 | 56 | 1 |\r\n| 2 | 11 | 1 |\r\n| 2 | 88 | 1 |\r\n| 3 | 88 | 1 |\r\n| 3 | 23 | 1 |\r\n| 4 | 88 | 1 |\r\n| 4 | 77 | 1 |\r\n| 4 | 23 | 1 |\r\n| 5 | 77 | 1 |\r\n| 5 | 23 | 1 |\r\n+---------+---------+---------------+\r\nTake user 1 as an example:\r\n - User 1 is friends with users 2, 3, 4, and 6.\r\n - Recommended pages are 23 (user 2 liked it), 24 (user 3 liked it), 56 (user 3 liked it), 33 (user 6 liked it), and 77 (user 2 and user 3 liked it).\r\n - Note that page 88 is not recommended because user 1 already liked it.\r\n\r\nAnother example is user 6:\r\n - User 6 is friends with user 1.\r\n - User 1 only liked page 88, but user 6 already liked it. Hence, user 6 has no recommendations.\r\n\r\nYou can recommend pages for users 2, 3, 4, and 5 using a similar process.\r\n
", "content_cn": "Table: Friendship
\n\n+---------------+---------+\n| Column Name | Type |\n+---------------+---------+\n| user1_id | int |\n| user2_id | int |\n+---------------+---------+\n(user1_id,user2_id)\u662fFriendship\u8868\u7684\u4e3b\u952e\u3002\n\u8be5\u8868\u7684\u6bcf\u4e00\u884c\u8868\u793a\u7528\u6237user1_id\u548cuser2_id\u662f\u597d\u53cb\u3002\n
\n\n\u00a0
\n\nTable: Likes
\n\n+-------------+---------+\n| Column Name | Type |\n+-------------+---------+\n| user_id | int |\n| page_id | int |\n+-------------+---------+\n(user_id,page_id)\u662fLikes\u8868\u7684\u4e3b\u952e\u3002\n(user_id, page_id) is the primary key for this table.\n\u8be5\u8868\u7684\u6bcf\u4e00\u884c\u8868\u793auser_id\u559c\u6b22page_id\u3002\n
\n\n\u00a0
\n\n\u60a8\u6b63\u5728\u4e3a\u4e00\u4e2a\u793e\u4ea4\u5a92\u4f53\u7f51\u7ad9\u5b9e\u65bd\u4e00\u4e2a\u9875\u9762\u63a8\u8350\u7cfb\u7edf\u3002\u5982\u679c\u9875\u9762\u88abuser_id
\u7684\u81f3\u5c11\u4e00\u4e2a\u670b\u53cb\u559c\u6b22\uff0c\u800c\u4e0d\u88abuser_id
\u559c\u6b22\uff0c\u4f60\u7684\u7cfb\u7edf\u5c06\u63a8\u8350\u4e00\u4e2a\u9875\u9762\u5230user_id
\u3002
\n\n\u7f16\u5199\u4e00\u4e2aSQL\u67e5\u8be2\u6765\u67e5\u627e\u9488\u5bf9\u6bcf\u4e2a\u7528\u6237\u7684\u6240\u6709\u53ef\u80fd\u7684\u9875\u9762\u5efa\u8bae\u3002\u6bcf\u4e2a\u5efa\u8bae\u5e94\u8be5\u5728\u7ed3\u679c\u8868\u4e2d\u663e\u793a\u4e3a\u4e00\u884c\uff0c\u5305\u542b\u4ee5\u4e0b\u5217:
\n\n\n\tuser_id
: \u7cfb\u7edf\u5411\u5176\u63d0\u51fa\u5efa\u8bae\u7684\u7528\u6237\u7684ID\u3002 \n\tpage_id
: \u63a8\u8350\u4e3auser_id
\u7684\u9875\u9762ID\u3002. \n\tfriends_likes
: user_id
\u5bf9\u5e94page_id
\u7684\u597d\u53cb\u6570\u3002 \n
\n\n\u4ee5\u4efb\u610f\u987a\u5e8f\u8fd4\u56de\u7ed3\u679c\u8868\u3002
\n\n\u67e5\u8be2\u7ed3\u679c\u683c\u5f0f\u793a\u4f8b\u5982\u4e0b:
\n\n\u00a0
\n\nFriendship table:\n+----------+----------+\n| user1_id | user2_id |\n+----------+----------+\n| 1 | 2 |\n| 1 | 3 |\n| 1 | 4 |\n| 2 | 3 |\n| 2 | 4 |\n| 2 | 5 |\n| 6 | 1 |\n+----------+----------+\n \nLikes table:\n+---------+---------+\n| user_id | page_id |\n+---------+---------+\n| 1 | 88 |\n| 2 | 23 |\n| 3 | 24 |\n| 4 | 56 |\n| 5 | 11 |\n| 6 | 33 |\n| 2 | 77 |\n| 3 | 77 |\n| 6 | 88 |\n+---------+---------+\n\nResult table:\n+---------+---------+---------------+\n| user_id | page_id | friends_likes |\n+---------+---------+---------------+\n| 1 | 77 | 2 |\n| 1 | 23 | 1 |\n| 1 | 24 | 1 |\n| 1 | 56 | 1 |\n| 1 | 33 | 1 |\n| 2 | 24 | 1 |\n| 2 | 56 | 1 |\n| 2 | 11 | 1 |\n| 2 | 88 | 1 |\n| 3 | 88 | 1 |\n| 3 | 23 | 1 |\n| 4 | 88 | 1 |\n| 4 | 77 | 1 |\n| 4 | 23 | 1 |\n| 5 | 77 | 1 |\n| 5 | 23 | 1 |\n+---------+---------+---------------+\n\u4ee5\u7528\u62371\u4e3a\u4f8b:\n\n\u2014\u7528\u62371\u662f\u7528\u62372\u30013\u30014\u30016\u7684\u597d\u53cb\u3002\n\n\u63a8\u8350\u9875\u9762\u670923(\u7528\u62372\u559c\u6b22)\uff0c24(\u7528\u62373\u559c\u6b22)\uff0c56(\u7528\u62373\u559c\u6b22)\uff0c33(\u7528\u62376\u559c\u6b22)\uff0c77(\u7528\u62372\u548c\u7528\u62373\u559c\u6b22)\u3002\n\n-\u8bf7\u6ce8\u610f\uff0c\u7b2c88\u9875\u4e0d\u63a8\u8350\uff0c\u56e0\u4e3a\u7528\u62371\u5df2\u7ecf\u559c\u6b22\u5b83\u3002\n\n\n\u53e6\u4e00\u4e2a\u4f8b\u5b50\u662f\u7528\u62376:\n\n\u2014\u7528\u62376\u662f\u7528\u62371\u7684\u597d\u53cb\u3002\n\n-\u7528\u62371\u53ea\u559c\u6b22\u4e8688\u9875\uff0c\u4f46\u7528\u62376\u5df2\u7ecf\u559c\u6b22\u4e86\u3002\u56e0\u6b64\uff0c\u7528\u62376\u6ca1\u6709\u63a8\u8350\u3002\n\n\n\u60a8\u53ef\u4ee5\u4f7f\u7528\u7c7b\u4f3c\u7684\u8fc7\u7a0b\u4e3a\u7528\u62372\u30013\u30014\u548c5\u63a8\u8350\u9875\u9762\u3002\n
\n", "tags_en": ["Database"], "tags_cn": ["\u6570\u636e\u5e93"], "difficulty_en": "Hard", "difficulty_cn": "\u56f0\u96be", "code_snippets": [{"lang": "MySQL", "langSlug": "mysql", "code": "# Write your MySQL query statement below", "__typename": "CodeSnippetNode"}, {"lang": "MS SQL Server", "langSlug": "mssql", "code": "/* Write your T-SQL query statement below */", "__typename": "CodeSnippetNode"}, {"lang": "Oracle", "langSlug": "oraclesql", "code": "/* Write your PL/SQL query statement below */", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1892](https://leetcode-cn.com/problems/page-recommendations-ii)", "[\u9875\u9762\u63a8\u8350\u2161](/solution/1800-1899/1892.Page%20Recommendations%20II/README.md)", "`\u6570\u636e\u5e93`", "\u56f0\u96be", "\ud83d\udd12"], "md_table_row_en": ["[1892](https://leetcode.com/problems/page-recommendations-ii)", "[Page Recommendations II](/solution/1800-1899/1892.Page%20Recommendations%20II/README_EN.md)", "`Database`", "Hard", "\ud83d\udd12"]}, {"question_id": "2045", "frontend_question_id": "1891", "paid_only": true, "paid_only_cn": true, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/cutting-ribbons", "url_en": "https://leetcode.com/problems/cutting-ribbons", "relative_path_cn": "/solution/1800-1899/1891.Cutting%20Ribbons/README.md", "relative_path_en": "/solution/1800-1899/1891.Cutting%20Ribbons/README_EN.md", "title_cn": "\u5272\u7ef3\u5b50", "title_en": "Cutting Ribbons", "question_title_slug": "cutting-ribbons", "content_en": "You are given an integer array ribbons
, where ribbons[i]
represents the length of the ith
ribbon, and an integer k
. You may cut any of the ribbons into any number of segments of positive integer lengths, or perform no cuts at all.
\n\n\n\t- For example, if you have a ribbon of length
4
, you can:\n\n\t\n\t\t- Keep the ribbon of length
4
, \n\t\t- Cut it into one ribbon of length
3
and one ribbon of length 1
, \n\t\t- Cut it into two ribbons of length
2
, \n\t\t- Cut it into one ribbon of length
2
and two ribbons of length 1
, or \n\t\t- Cut it into four ribbons of length
1
. \n\t
\n\t \n
\n\nYour goal is to obtain k
ribbons of all the same positive integer length. You are allowed to throw away any excess ribbon as a result of cutting.
\n\nReturn the maximum possible positive integer length that you can obtain k
ribbons of, or 0
if you cannot obtain k
ribbons of the same length.
\n\n
\nExample 1:
\n\n\nInput: ribbons = [9,7,5], k = 3\nOutput: 5\nExplanation:\n- Cut the first ribbon to two ribbons, one of length 5 and one of length 4.\n- Cut the second ribbon to two ribbons, one of length 5 and one of length 2.\n- Keep the third ribbon as it is.\nNow you have 3 ribbons of length 5.
\n\nExample 2:
\n\n\nInput: ribbons = [7,5,9], k = 4\nOutput: 4\nExplanation:\n- Cut the first ribbon to two ribbons, one of length 4 and one of length 3.\n- Cut the second ribbon to two ribbons, one of length 4 and one of length 1.\n- Cut the third ribbon to three ribbons, two of length 4 and one of length 1.\nNow you have 4 ribbons of length 4.\n
\n\nExample 3:
\n\n\nInput: ribbons = [5,7,9], k = 22\nOutput: 0\nExplanation: You cannot obtain k ribbons of the same positive integer length.\n
\n\n
\nConstraints:
\n\n\n\t1 <= ribbons.length <= 105
\n\t1 <= ribbons[i] <= 105
\n\t1 <= k <= 109
\n
\n", "content_cn": "\u7ed9\u5b9a\u4e00\u4e2a\u6574\u6570\u6570\u7ec4\u00a0ribbons
\u00a0\u548c\u4e00\u4e2a\u6574\u6570 k
\uff0c\u6570\u7ec4\u6bcf\u9879\u00a0ribbons[i]
\u00a0\u8868\u793a\u7b2c\u00a0i
\u00a0\u6761\u7ef3\u5b50\u7684\u957f\u5ea6\u3002\u5bf9\u4e8e\u6bcf\u6761\u7ef3\u5b50\uff0c\u4f60\u53ef\u4ee5\u5c06\u4efb\u610f\u5207\u5272\u6210\u4e00\u7cfb\u5217\u957f\u5ea6\u4e3a\u6b63\u6574\u6570\u7684\u90e8\u5206\uff0c\u6216\u8005\u9009\u62e9\u4e0d\u8fdb\u884c\u5207\u5272\u3002
\n\n\u4f8b\u5982\uff0c\u5982\u679c\u7ed9\u4f60\u4e00\u6761\u957f\u5ea6\u4e3a 4
\u7684\u7ef3\u5b50\uff0c\u4f60\u53ef\u4ee5\uff1a
\n\n\n\t- \u4fdd\u6301\u7ef3\u5b50\u7684\u957f\u5ea6\u4e3a
4
\u4e0d\u53d8\uff1b \n\t- \u5207\u5272\u6210\u4e00\u6761\u957f\u5ea6\u4e3a
3
\u548c\u4e00\u6761\u957f\u5ea6\u4e3a 1
\u7684\u7ef3\u5b50\uff1b \n\t- \u5207\u5272\u6210\u4e24\u6761\u957f\u5ea6\u4e3a
2
\u00a0\u7684\u7ef3\u5b50\uff1b \n\t- \u5207\u5272\u6210\u4e00\u6761\u957f\u5ea6\u4e3a
2
\u00a0\u548c\u4e24\u6761\u957f\u5ea6\u4e3a 1
\u7684\u7ef3\u5b50\uff1b \n\t- \u5207\u5272\u6210\u56db\u6761\u957f\u5ea6\u4e3a
1
\u00a0\u7684\u7ef3\u5b50\u3002 \n
\n\n\u4f60\u7684\u4efb\u52a1\u662f\u6700\u7ec8\u5f97\u5230 k
\u6761\u5b8c\u5168\u4e00\u6837\u7684\u7ef3\u5b50\uff0c\u4ed6\u4eec\u7684\u957f\u5ea6\u5747\u4e3a\u76f8\u540c\u7684\u6b63\u6574\u6570\u3002\u5982\u679c\u7ef3\u5b50\u5207\u5272\u540e\u6709\u5269\u4f59\uff0c\u4f60\u53ef\u4ee5\u76f4\u63a5\u820d\u5f03\u6389\u591a\u4f59\u7684\u90e8\u5206\u3002
\n\n\u5bf9\u4e8e\u8fd9 k
\u6839\u7ef3\u5b50\uff0c\u8fd4\u56de\u4f60\u80fd\u5f97\u5230\u7684\u7ef3\u5b50\u6700\u5927\u957f\u5ea6\uff1b\u5982\u679c\u4f60\u65e0\u6cd5\u5f97\u5230 k
\u6839\u76f8\u540c\u957f\u5ea6\u7684\u7ef3\u5b50\uff0c\u8fd4\u56de 0
\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1:
\n\n\u8f93\u5165: ribbons = [9,7,5], k = 3\n\u8f93\u51fa: 5\n\u89e3\u91ca:\n- \u628a\u7b2c\u4e00\u6761\u7ef3\u5b50\u5207\u6210\u4e24\u90e8\u5206\uff0c\u4e00\u6761\u957f\u5ea6\u4e3a 5\uff0c\u4e00\u6761\u957f\u5ea6\u4e3a 4\uff1b\n- \u628a\u7b2c\u4e8c\u6761\u7ef3\u5b50\u5207\u6210\u4e24\u90e8\u5206\uff0c\u4e00\u6761\u957f\u5ea6\u4e3a 5\uff0c\u4e00\u6761\u957f\u5ea6\u4e3a 2\uff1b\n- \u7b2c\u4e09\u6761\u7ef3\u5b50\u4e0d\u8fdb\u884c\u5207\u5272\uff1b\n\u73b0\u5728\uff0c\u4f60\u5f97\u5230\u4e86 3 \u6761\u957f\u5ea6\u4e3a 5 \u7684\u7ef3\u5b50\u3002
\n\n\u793a\u4f8b 2:
\n\n\u8f93\u5165: ribbons = [7,5,9], k = 4\n\u8f93\u51fa: 4\n\u89e3\u91ca:\n- \u628a\u7b2c\u4e00\u6761\u7ef3\u5b50\u5207\u6210\u4e24\u90e8\u5206\uff0c\u4e00\u6761\u957f\u5ea6\u4e3a 4\uff0c\u4e00\u6761\u957f\u5ea6\u4e3a 3\uff1b\n- \u628a\u7b2c\u4e8c\u6761\u7ef3\u5b50\u5207\u6210\u4e24\u90e8\u5206\uff0c\u4e00\u6761\u957f\u5ea6\u4e3a 4\uff0c\u4e00\u6761\u957f\u5ea6\u4e3a 1\uff1b\n- \u628a\u7b2c\u4e8c\u6761\u7ef3\u5b50\u5207\u6210\u4e09\u90e8\u5206\uff0c\u4e00\u6761\u957f\u5ea6\u4e3a 4\uff0c\u4e00\u6761\u957f\u5ea6\u4e3a 4\uff0c\u8fd8\u6709\u4e00\u6761\u957f\u5ea6\u4e3a 1\uff1b\n\u73b0\u5728\uff0c\u4f60\u5f97\u5230\u4e86 4 \u6761\u957f\u5ea6\u4e3a 4 \u7684\u7ef3\u5b50\u3002\n
\n\n\u793a\u4f8b 3:
\n\n\u8f93\u5165: ribbons = [5,7,9], k = 22\n\u8f93\u51fa: 0\n\u89e3\u91ca: \u7531\u4e8e\u7ef3\u5b50\u957f\u5ea6\u9700\u8981\u4e3a\u6b63\u6574\u6570\uff0c\u4f60\u65e0\u6cd5\u5f97\u5230 22 \u6761\u957f\u5ea6\u76f8\u540c\u7684\u7ef3\u5b50\u3002\n
\n\n\u00a0
\n\n\u63d0\u793a:
\n\n\n\t1 <= ribbons.length <= 105
\n\t1 <= ribbons[i] <= 105
\n\t1 <= k <= 109
\n
\n", "tags_en": ["Array", "Binary Search"], "tags_cn": ["\u6570\u7ec4", "\u4e8c\u5206\u67e5\u627e"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n int maxLength(vector& ribbons, int k) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int maxLength(int[] ribbons, int k) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def maxLength(self, ribbons, k):\n \"\"\"\n :type ribbons: List[int]\n :type k: int\n :rtype: int\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def maxLength(self, ribbons: List[int], k: int) -> int:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nint maxLength(int* ribbons, int ribbonsSize, int k){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int MaxLength(int[] ribbons, int k) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[]} ribbons\n * @param {number} k\n * @return {number}\n */\nvar maxLength = function(ribbons, k) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer[]} ribbons\n# @param {Integer} k\n# @return {Integer}\ndef max_length(ribbons, k)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func maxLength(_ ribbons: [Int], _ k: Int) -> Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func maxLength(ribbons []int, k int) int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def maxLength(ribbons: Array[Int], k: Int): Int = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun maxLength(ribbons: IntArray, k: Int): Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn max_length(ribbons: Vec, k: i32) -> i32 {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer[] $ribbons\n * @param Integer $k\n * @return Integer\n */\n function maxLength($ribbons, $k) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function maxLength(ribbons: number[], k: number): number {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (max-length ribbons k)\n (-> (listof exact-integer?) exact-integer? exact-integer?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec max_length(Ribbons :: [integer()], K :: integer()) -> integer().\nmax_length(Ribbons, K) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec max_length(ribbons :: [integer], k :: integer) :: integer\n def max_length(ribbons, k) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1891](https://leetcode-cn.com/problems/cutting-ribbons)", "[\u5272\u7ef3\u5b50](/solution/1800-1899/1891.Cutting%20Ribbons/README.md)", "`\u6570\u7ec4`,`\u4e8c\u5206\u67e5\u627e`", "\u4e2d\u7b49", "\ud83d\udd12"], "md_table_row_en": ["[1891](https://leetcode.com/problems/cutting-ribbons)", "[Cutting Ribbons](/solution/1800-1899/1891.Cutting%20Ribbons/README_EN.md)", "`Array`,`Binary Search`", "Medium", "\ud83d\udd12"]}, {"question_id": "2044", "frontend_question_id": "1915", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/number-of-wonderful-substrings", "url_en": "https://leetcode.com/problems/number-of-wonderful-substrings", "relative_path_cn": "/solution/1900-1999/1915.Number%20of%20Wonderful%20Substrings/README.md", "relative_path_en": "/solution/1900-1999/1915.Number%20of%20Wonderful%20Substrings/README_EN.md", "title_cn": "\u6700\u7f8e\u5b50\u5b57\u7b26\u4e32\u7684\u6570\u76ee", "title_en": "Number of Wonderful Substrings", "question_title_slug": "number-of-wonderful-substrings", "content_en": "A wonderful string is a string where at most one letter appears an odd number of times.
\r\n\r\n\r\n\t- For example,
"ccjjc"
and "abab"
are wonderful, but "ab"
is not. \r\n
\r\n\r\nGiven 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.
\r\n\r\nA substring is a contiguous sequence of characters in a string.
\r\n\r\n
\r\nExample 1:
\r\n\r\n\r\nInput: word = "aba"\r\nOutput: 4\r\nExplanation: The four wonderful substrings are underlined below:\r\n- "aba" -> "a"\r\n- "aba" -> "b"\r\n- "aba" -> "a"\r\n- "aba" -> "aba"\r\n
\r\n\r\nExample 2:
\r\n\r\n\r\nInput: word = "aabb"\r\nOutput: 9\r\nExplanation: The nine wonderful substrings are underlined below:\r\n- "aabb" -> "a"\r\n- "aabb" -> "aa"\r\n- "aabb" -> "aab"\r\n- "aabb" -> "aabb"\r\n- "aabb" -> "a"\r\n- "aabb" -> "abb"\r\n- "aabb" -> "b"\r\n- "aabb" -> "bb"\r\n- "aabb" -> "b"\r\n
\r\n\r\nExample 3:
\r\n\r\n\r\nInput: word = "he"\r\nOutput: 2\r\nExplanation: The two wonderful substrings are underlined below:\r\n- "he" -> "h"\r\n- "he" -> "e"\r\n
\r\n\r\n
\r\nConstraints:
\r\n\r\n\r\n\t1 <= word.length <= 105
\r\n\tword
consists of lowercase English letters from 'a'
to 'j'
. \r\n
", "content_cn": "\u5982\u679c\u67d0\u4e2a\u5b57\u7b26\u4e32\u4e2d \u81f3\u591a\u4e00\u4e2a \u5b57\u6bcd\u51fa\u73b0 \u5947\u6570 \u6b21\uff0c\u5219\u79f0\u5176\u4e3a \u6700\u7f8e \u5b57\u7b26\u4e32\u3002
\n\n\n\t- \u4f8b\u5982\uff0c
\"ccjjc\"
\u548c \"abab\"
\u90fd\u662f\u6700\u7f8e\u5b57\u7b26\u4e32\uff0c\u4f46 \"ab\"
\u4e0d\u662f\u3002 \n
\n\n\u7ed9\u4f60\u4e00\u4e2a\u5b57\u7b26\u4e32 word
\uff0c\u8be5\u5b57\u7b26\u4e32\u7531\u524d\u5341\u4e2a\u5c0f\u5199\u82f1\u6587\u5b57\u6bcd\u7ec4\u6210\uff08'a'
\u5230 'j'
\uff09\u3002\u8bf7\u4f60\u8fd4\u56de word
\u4e2d \u6700\u7f8e\u975e\u7a7a\u5b50\u5b57\u7b26\u4e32 \u7684\u6570\u76ee\u3002\u5982\u679c\u540c\u6837\u7684\u5b50\u5b57\u7b26\u4e32\u5728 word
\u4e2d\u51fa\u73b0\u591a\u6b21\uff0c\u90a3\u4e48\u5e94\u5f53\u5bf9 \u6bcf\u6b21\u51fa\u73b0 \u5206\u522b\u8ba1\u6570\u3002
\n\n\u5b50\u5b57\u7b26\u4e32 \u662f\u5b57\u7b26\u4e32\u4e2d\u7684\u4e00\u4e2a\u8fde\u7eed\u5b57\u7b26\u5e8f\u5217\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1aword = \"aba\"\n\u8f93\u51fa\uff1a4\n\u89e3\u91ca\uff1a4 \u4e2a\u6700\u7f8e\u5b50\u5b57\u7b26\u4e32\u5982\u4e0b\u6240\u793a\uff1a\n- \"aba\" -> \"a\"\n- \"aba\" -> \"b\"\n- \"aba\" -> \"a\"\n- \"aba\" -> \"aba\"\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1aword = \"aabb\"\n\u8f93\u51fa\uff1a9\n\u89e3\u91ca\uff1a9 \u4e2a\u6700\u7f8e\u5b50\u5b57\u7b26\u4e32\u5982\u4e0b\u6240\u793a\uff1a\n- \"aabb\" -> \"a\"\n- \"aabb\" -> \"aa\"\n- \"aabb\" -> \"aab\"\n- \"aabb\" -> \"aabb\"\n- \"aabb\" -> \"a\"\n- \"aabb\" -> \"abb\"\n- \"aabb\" -> \"b\"\n- \"aabb\" -> \"bb\"\n- \"aabb\" -> \"b\"\n
\n\n\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1aword = \"he\"\n\u8f93\u51fa\uff1a2\n\u89e3\u91ca\uff1a2 \u4e2a\u6700\u7f8e\u5b50\u5b57\u7b26\u4e32\u5982\u4e0b\u6240\u793a\uff1a\n- \"he\" -> \"h\"\n- \"he\" -> \"e\"\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\t1 <= word.length <= 105
\n\tword
\u7531\u4ece 'a'
\u5230 'j'
\u7684\u5c0f\u5199\u82f1\u6587\u5b57\u6bcd\u7ec4\u6210 \n
\n", "tags_en": ["Bit Manipulation", "Hash Table", "String", "Prefix Sum"], "tags_cn": ["\u4f4d\u8fd0\u7b97", "\u54c8\u5e0c\u8868", "\u5b57\u7b26\u4e32", "\u524d\u7f00\u548c"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n long long wonderfulSubstrings(string word) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public long wonderfulSubstrings(String word) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def wonderfulSubstrings(self, word):\n \"\"\"\n :type word: str\n :rtype: int\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def wonderfulSubstrings(self, word: str) -> int:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "long long wonderfulSubstrings(char * word){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public long WonderfulSubstrings(string word) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {string} word\n * @return {number}\n */\nvar wonderfulSubstrings = function(word) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {String} word\n# @return {Integer}\ndef wonderful_substrings(word)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func wonderfulSubstrings(_ word: String) -> Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func wonderfulSubstrings(word string) int64 {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def wonderfulSubstrings(word: String): Long = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun wonderfulSubstrings(word: String): Long {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn wonderful_substrings(word: String) -> i64 {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param String $word\n * @return Integer\n */\n function wonderfulSubstrings($word) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function wonderfulSubstrings(word: string): number {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (wonderful-substrings word)\n (-> string? exact-integer?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec wonderful_substrings(Word :: unicode:unicode_binary()) -> integer().\nwonderful_substrings(Word) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec wonderful_substrings(word :: String.t) :: integer\n def wonderful_substrings(word) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1915](https://leetcode-cn.com/problems/number-of-wonderful-substrings)", "[\u6700\u7f8e\u5b50\u5b57\u7b26\u4e32\u7684\u6570\u76ee](/solution/1900-1999/1915.Number%20of%20Wonderful%20Substrings/README.md)", "`\u4f4d\u8fd0\u7b97`,`\u54c8\u5e0c\u8868`,`\u5b57\u7b26\u4e32`,`\u524d\u7f00\u548c`", "\u4e2d\u7b49", ""], "md_table_row_en": ["[1915](https://leetcode.com/problems/number-of-wonderful-substrings)", "[Number of Wonderful Substrings](/solution/1900-1999/1915.Number%20of%20Wonderful%20Substrings/README_EN.md)", "`Bit Manipulation`,`Hash Table`,`String`,`Prefix Sum`", "Medium", ""]}, {"question_id": "2043", "frontend_question_id": "1914", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/cyclically-rotating-a-grid", "url_en": "https://leetcode.com/problems/cyclically-rotating-a-grid", "relative_path_cn": "/solution/1900-1999/1914.Cyclically%20Rotating%20a%20Grid/README.md", "relative_path_en": "/solution/1900-1999/1914.Cyclically%20Rotating%20a%20Grid/README_EN.md", "title_cn": "\u5faa\u73af\u8f6e\u8f6c\u77e9\u9635", "title_en": "Cyclically Rotating a Grid", "question_title_slug": "cyclically-rotating-a-grid", "content_en": "You are given an m x n
integer matrix grid
\u200b\u200b\u200b, where m
and n
are both even integers, and an integer k
.
\r\n\r\nThe matrix is composed of several layers, which is shown in the below image, where each color is its own layer:
\r\n\r\n
\r\n\r\nA 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:
\r\n
\r\nReturn the matrix after applying k
cyclic rotations to it.
\r\n\r\n
\r\nExample 1:
\r\n
\r\n\r\nInput: grid = [[40,10],[30,20]], k = 1\r\nOutput: [[10,20],[40,30]]\r\nExplanation: The figures above represent the grid at every state.\r\n
\r\n\r\nExample 2:
\r\n
\r\n\r\n\r\nInput: grid = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]], k = 2\r\nOutput: [[3,4,8,12],[2,11,10,16],[1,7,6,15],[5,9,13,14]]\r\nExplanation: The figures above represent the grid at every state.\r\n
\r\n\r\n
\r\nConstraints:
\r\n\r\n\r\n\tm == grid.length
\r\n\tn == grid[i].length
\r\n\t2 <= m, n <= 50
\r\n\t- Both
m
and n
are even integers. \r\n\t1 <= grid[i][j] <= 5000
\r\n\t1 <= k <= 109
\r\n
", "content_cn": "\u7ed9\u4f60\u4e00\u4e2a\u5927\u5c0f\u4e3a m x n
\u7684\u6574\u6570\u77e9\u9635 grid
\u200b\u200b\u200b \uff0c\u5176\u4e2d m
\u548c n
\u90fd\u662f \u5076\u6570 \uff1b\u53e6\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570 k
\u3002
\n\n\u77e9\u9635\u7531\u82e5\u5e72\u5c42\u7ec4\u6210\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u6bcf\u79cd\u989c\u8272\u4ee3\u8868\u4e00\u5c42\uff1a
\n\n
\n\n\u77e9\u9635\u7684\u5faa\u73af\u8f6e\u8f6c\u662f\u901a\u8fc7\u5206\u522b\u5faa\u73af\u8f6e\u8f6c\u77e9\u9635\u4e2d\u7684\u6bcf\u4e00\u5c42\u5b8c\u6210\u7684\u3002\u5728\u5bf9\u67d0\u4e00\u5c42\u8fdb\u884c\u4e00\u6b21\u5faa\u73af\u65cb\u8f6c\u64cd\u4f5c\u65f6\uff0c\u5c42\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u5c06\u4f1a\u53d6\u4ee3\u5176\u00a0\u9006\u65f6\u9488 \u65b9\u5411\u7684\u76f8\u90bb\u5143\u7d20\u3002\u8f6e\u8f6c\u793a\u4f8b\u5982\u4e0b\uff1a
\n
\n\u8fd4\u56de\u6267\u884c k
\u6b21\u5faa\u73af\u8f6e\u8f6c\u64cd\u4f5c\u540e\u7684\u77e9\u9635\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n
\n\u8f93\u5165\uff1agrid = [[40,10],[30,20]], k = 1\n\u8f93\u51fa\uff1a[[10,20],[40,30]]\n\u89e3\u91ca\uff1a\u4e0a\u56fe\u5c55\u793a\u4e86\u77e9\u9635\u5728\u6267\u884c\u5faa\u73af\u8f6e\u8f6c\u64cd\u4f5c\u65f6\u6bcf\u4e00\u6b65\u7684\u72b6\u6001\u3002
\n\n\u793a\u4f8b 2\uff1a
\n
\n\n\u8f93\u5165\uff1agrid = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]], k = 2\n\u8f93\u51fa\uff1a[[3,4,8,12],[2,11,10,16],[1,7,6,15],[5,9,13,14]]\n\u89e3\u91ca\uff1a\u4e0a\u56fe\u5c55\u793a\u4e86\u77e9\u9635\u5728\u6267\u884c\u5faa\u73af\u8f6e\u8f6c\u64cd\u4f5c\u65f6\u6bcf\u4e00\u6b65\u7684\u72b6\u6001\u3002\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\tm == grid.length
\n\tn == grid[i].length
\n\t2 <= m, n <= 50
\n\tm
\u548c n
\u90fd\u662f \u5076\u6570 \n\t1 <= grid[i][j] <= 5000
\n\t1 <= k <= 109
\n
\n", "tags_en": ["Array", "Matrix", "Simulation"], "tags_cn": ["\u6570\u7ec4", "\u77e9\u9635", "\u6a21\u62df"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n vector> rotateGrid(vector>& grid, int k) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int[][] rotateGrid(int[][] grid, int k) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def rotateGrid(self, grid, k):\n \"\"\"\n :type grid: List[List[int]]\n :type k: int\n :rtype: List[List[int]]\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def rotateGrid(self, grid: List[List[int]], k: int) -> List[List[int]]:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "/**\n * Return an array of arrays of size *returnSize.\n * The sizes of the arrays are returned as *returnColumnSizes array.\n * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().\n */\nint** rotateGrid(int** grid, int gridSize, int* gridColSize, int k, int* returnSize, int** returnColumnSizes){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int[][] RotateGrid(int[][] grid, int k) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[][]} grid\n * @param {number} k\n * @return {number[][]}\n */\nvar rotateGrid = function(grid, k) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer[][]} grid\n# @param {Integer} k\n# @return {Integer[][]}\ndef rotate_grid(grid, k)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func rotateGrid(_ grid: [[Int]], _ k: Int) -> [[Int]] {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func rotateGrid(grid [][]int, k int) [][]int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def rotateGrid(grid: Array[Array[Int]], k: Int): Array[Array[Int]] = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun rotateGrid(grid: Array, k: Int): Array {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn rotate_grid(grid: Vec>, k: i32) -> Vec> {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer[][] $grid\n * @param Integer $k\n * @return Integer[][]\n */\n function rotateGrid($grid, $k) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function rotateGrid(grid: number[][], k: number): number[][] {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (rotate-grid grid k)\n (-> (listof (listof exact-integer?)) exact-integer? (listof (listof exact-integer?)))\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec rotate_grid(Grid :: [[integer()]], K :: integer()) -> [[integer()]].\nrotate_grid(Grid, K) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec rotate_grid(grid :: [[integer]], k :: integer) :: [[integer]]\n def rotate_grid(grid, k) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1914](https://leetcode-cn.com/problems/cyclically-rotating-a-grid)", "[\u5faa\u73af\u8f6e\u8f6c\u77e9\u9635](/solution/1900-1999/1914.Cyclically%20Rotating%20a%20Grid/README.md)", "`\u6570\u7ec4`,`\u77e9\u9635`,`\u6a21\u62df`", "\u4e2d\u7b49", ""], "md_table_row_en": ["[1914](https://leetcode.com/problems/cyclically-rotating-a-grid)", "[Cyclically Rotating a Grid](/solution/1900-1999/1914.Cyclically%20Rotating%20a%20Grid/README_EN.md)", "`Array`,`Matrix`,`Simulation`", "Medium", ""]}, {"question_id": "2042", "frontend_question_id": "1913", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/maximum-product-difference-between-two-pairs", "url_en": "https://leetcode.com/problems/maximum-product-difference-between-two-pairs", "relative_path_cn": "/solution/1900-1999/1913.Maximum%20Product%20Difference%20Between%20Two%20Pairs/README.md", "relative_path_en": "/solution/1900-1999/1913.Maximum%20Product%20Difference%20Between%20Two%20Pairs/README_EN.md", "title_cn": "\u4e24\u4e2a\u6570\u5bf9\u4e4b\u95f4\u7684\u6700\u5927\u4e58\u79ef\u5dee", "title_en": "Maximum Product Difference Between Two Pairs", "question_title_slug": "maximum-product-difference-between-two-pairs", "content_en": "The product difference between two pairs (a, b)
and (c, d)
is defined as (a * b) - (c * d)
.
\r\n\r\n\r\n\t- For example, the product difference between
(5, 6)
and (2, 7)
is (5 * 6) - (2 * 7) = 16
. \r\n
\r\n\r\nGiven 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.
\r\n\r\nReturn the maximum such product difference.
\r\n\r\n
\r\nExample 1:
\r\n\r\n\r\nInput: nums = [5,6,2,7,4]\r\nOutput: 34\r\nExplanation: We can choose indices 1 and 3 for the first pair (6, 7) and indices 2 and 4 for the second pair (2, 4).\r\nThe product difference is (6 * 7) - (2 * 4) = 34.\r\n
\r\n\r\nExample 2:
\r\n\r\n\r\nInput: nums = [4,2,5,9,7,4,8]\r\nOutput: 64\r\nExplanation: We can choose indices 3 and 6 for the first pair (9, 8) and indices 1 and 5 for the second pair (2, 4).\r\nThe product difference is (9 * 8) - (2 * 4) = 64.\r\n
\r\n\r\n
\r\nConstraints:
\r\n\r\n\r\n\t4 <= nums.length <= 104
\r\n\t1 <= nums[i] <= 104
\r\n
", "content_cn": "\u4e24\u4e2a\u6570\u5bf9\u00a0(a, b)
\u548c (c, d)
\u4e4b\u95f4\u7684 \u4e58\u79ef\u5dee \u5b9a\u4e49\u4e3a (a * b) - (c * d)
\u3002
\n\n\n\t- \u4f8b\u5982\uff0c
(5, 6)
\u548c (2, 7)
\u4e4b\u95f4\u7684\u4e58\u79ef\u5dee\u662f (5 * 6) - (2 * 7) = 16
\u3002 \n
\n\n\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 nums
\uff0c\u9009\u51fa\u56db\u4e2a \u4e0d\u540c\u7684 \u4e0b\u6807 w
\u3001x
\u3001y
\u548c z
\uff0c\u4f7f\u6570\u5bf9 (nums[w], nums[x])
\u548c (nums[y], nums[z])
\u4e4b\u95f4\u7684 \u4e58\u79ef\u5dee \u53d6\u5230 \u6700\u5927\u503c \u3002
\n\n\u8fd4\u56de\u4ee5\u8fd9\u79cd\u65b9\u5f0f\u53d6\u5f97\u7684\u4e58\u79ef\u5dee\u4e2d\u7684 \u6700\u5927\u503c \u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1anums = [5,6,2,7,4]\n\u8f93\u51fa\uff1a34\n\u89e3\u91ca\uff1a\u53ef\u4ee5\u9009\u51fa\u4e0b\u6807\u4e3a 1 \u548c 3 \u7684\u5143\u7d20\u6784\u6210\u7b2c\u4e00\u4e2a\u6570\u5bf9 (6, 7) \u4ee5\u53ca\u4e0b\u6807 2 \u548c 4 \u6784\u6210\u7b2c\u4e8c\u4e2a\u6570\u5bf9 (2, 4)\n\u4e58\u79ef\u5dee\u662f (6 * 7) - (2 * 4) = 34\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1anums = [4,2,5,9,7,4,8]\n\u8f93\u51fa\uff1a64\n\u89e3\u91ca\uff1a\u53ef\u4ee5\u9009\u51fa\u4e0b\u6807\u4e3a 3 \u548c 6 \u7684\u5143\u7d20\u6784\u6210\u7b2c\u4e00\u4e2a\u6570\u5bf9 (9, 8) \u4ee5\u53ca\u4e0b\u6807 1 \u548c 5 \u6784\u6210\u7b2c\u4e8c\u4e2a\u6570\u5bf9 (2, 4)\n\u4e58\u79ef\u5dee\u662f (9 * 8) - (2 * 4) = 64\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\t4 <= nums.length <= 104
\n\t1 <= nums[i] <= 104
\n
\n", "tags_en": ["Array", "Sorting"], "tags_cn": ["\u6570\u7ec4", "\u6392\u5e8f"], "difficulty_en": "Easy", "difficulty_cn": "\u7b80\u5355", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n int maxProductDifference(vector& nums) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int maxProductDifference(int[] nums) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def maxProductDifference(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def maxProductDifference(self, nums: List[int]) -> int:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "int maxProductDifference(int* nums, int numsSize){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int MaxProductDifference(int[] nums) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[]} nums\n * @return {number}\n */\nvar maxProductDifference = function(nums) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer[]} nums\n# @return {Integer}\ndef max_product_difference(nums)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func maxProductDifference(_ nums: [Int]) -> Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func maxProductDifference(nums []int) int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def maxProductDifference(nums: Array[Int]): Int = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun maxProductDifference(nums: IntArray): Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn max_product_difference(nums: Vec) -> i32 {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @return Integer\n */\n function maxProductDifference($nums) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function maxProductDifference(nums: number[]): number {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (max-product-difference nums)\n (-> (listof exact-integer?) exact-integer?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec max_product_difference(Nums :: [integer()]) -> integer().\nmax_product_difference(Nums) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec max_product_difference(nums :: [integer]) :: integer\n def max_product_difference(nums) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1913](https://leetcode-cn.com/problems/maximum-product-difference-between-two-pairs)", "[\u4e24\u4e2a\u6570\u5bf9\u4e4b\u95f4\u7684\u6700\u5927\u4e58\u79ef\u5dee](/solution/1900-1999/1913.Maximum%20Product%20Difference%20Between%20Two%20Pairs/README.md)", "`\u6570\u7ec4`,`\u6392\u5e8f`", "\u7b80\u5355", ""], "md_table_row_en": ["[1913](https://leetcode.com/problems/maximum-product-difference-between-two-pairs)", "[Maximum Product Difference Between Two Pairs](/solution/1900-1999/1913.Maximum%20Product%20Difference%20Between%20Two%20Pairs/README_EN.md)", "`Array`,`Sorting`", "Easy", ""]}, {"question_id": "2041", "frontend_question_id": "1890", "paid_only": true, "paid_only_cn": true, "category": "Database", "url_cn": "https://leetcode-cn.com/problems/the-latest-login-in-2020", "url_en": "https://leetcode.com/problems/the-latest-login-in-2020", "relative_path_cn": "/solution/1800-1899/1890.The%20Latest%20Login%20in%202020/README.md", "relative_path_en": "/solution/1800-1899/1890.The%20Latest%20Login%20in%202020/README_EN.md", "title_cn": "2020\u5e74\u6700\u540e\u4e00\u6b21\u767b\u5f55", "title_en": "The Latest Login in 2020", "question_title_slug": "the-latest-login-in-2020", "content_en": "Table: Logins
\r\n\r\n\r\n+----------------+----------+\r\n| Column Name | Type |\r\n+----------------+----------+\r\n| user_id | int |\r\n| time_stamp | datetime |\r\n+----------------+----------+\r\n(user_id, time_stamp) is the primary key for this table.\r\nEach row contains information about the login time for the user with ID user_id.\r\n
\r\n\r\n
\r\n\r\nWrite an SQL query to report the latest login for all users in the year 2020
. Do not include the users who did not login in 2020
.
\r\n\r\nReturn the result table in any order.
\r\n\r\nThe query result format is in the following example:
\r\n\r\n
\r\n\r\n\r\nLogins table:\r\n+---------+---------------------+\r\n| user_id | time_stamp |\r\n+---------+---------------------+\r\n| 6 | 2020-06-30 15:06:07 |\r\n| 6 | 2021-04-21 14:06:06 |\r\n| 6 | 2019-03-07 00:18:15 |\r\n| 8 | 2020-02-01 05:10:53 |\r\n| 8 | 2020-12-30 00:46:50 |\r\n| 2 | 2020-01-16 02:49:50 |\r\n| 2 | 2019-08-25 07:59:08 |\r\n| 14 | 2019-07-14 09:00:00 |\r\n| 14 | 2021-01-06 11:59:59 |\r\n+---------+---------------------+\r\n\r\nResult table:\r\n+---------+---------------------+\r\n| user_id | last_stamp |\r\n+---------+---------------------+\r\n| 6 | 2020-06-30 15:06:07 |\r\n| 8 | 2020-12-30 00:46:50 |\r\n| 2 | 2020-01-16 02:49:50 |\r\n+---------+---------------------+\r\n\r\nUser 6 logged into their account 3 times but only once in 2020, so we include this login in the result table.\r\nUser 8 logged into their account 2 times in 2020, once in February and once in December. We include only the latest one (December) in the result table.\r\nUser 2 logged into their account 2 times but only once in 2020, so we include this login in the result table.\r\nUser 14 did not login in 2020, so we do not include them in the result table.\r\n
", "content_cn": "\u8868: Logins
\n\n+----------------+----------+\n| \u5217\u540d | \u7c7b\u578b |\n+----------------+----------+\n| user_id | int |\n| time_stamp | datetime |\n+----------------+----------+\n(user_id, time_stamp) \u662f\u8fd9\u4e2a\u8868\u7684\u4e3b\u952e\u3002\n\u6bcf\u4e00\u884c\u5305\u542b\u7684\u4fe1\u606f\u662fuser_id \u8fd9\u4e2a\u7528\u6237\u7684\u767b\u5f55\u65f6\u95f4\u3002\n
\n\n\u7f16\u5199\u4e00\u4e2a SQL \u67e5\u8be2\uff0c\u8be5\u67e5\u8be2\u53ef\u4ee5\u83b7\u53d6\u57282020\u5e74\u767b\u5f55\u8fc7\u7684\u6240\u6709\u7528\u6237\u7684\u672c\u5e74\u5ea6\u6700\u540e\u4e00\u6b21\u767b\u5f55\u65f6\u95f4\u3002\u7ed3\u679c\u96c6\u4e0d\u5305\u542b2020\u5e74\u6ca1\u6709\u767b\u5f55\u8fc7\u7684\u7528\u6237\u3002
\n\n\u8fd4\u56de\u7684\u7ed3\u679c\u96c6\u53ef\u4ee5\u6309\u4efb\u610f\u987a\u5e8f\u6392\u5217\u3002
\n\n\u67e5\u8be2\u7ed3\u679c\u683c\u5f0f\u5982\u4e0b\u4f8b\uff1a
\n\nLogins \u8868:\n+---------+---------------------+\n| user_id | time_stamp |\n+---------+---------------------+\n| 6 | 2020-06-30 15:06:07 |\n| 6 | 2021-04-21 14:06:06 |\n| 6 | 2019-03-07 00:18:15 |\n| 8 | 2020-02-01 05:10:53 |\n| 8 | 2020-12-30 00:46:50 |\n| 2 | 2020-01-16 02:49:50 |\n| 2 | 2019-08-25 07:59:08 |\n| 14 | 2019-07-14 09:00:00 |\n| 14 | 2021-01-06 11:59:59 |\n+---------+---------------------+\n\nResult \u8868:\n+---------+---------------------+\n| user_id | last_stamp |\n+---------+---------------------+\n| 6 | 2020-06-30 15:06:07 |\n| 8 | 2020-12-30 00:46:50 |\n| 2 | 2020-01-16 02:49:50 |\n+---------+---------------------+\n6\u53f7\u7528\u6237\u767b\u5f55\u4e863\u6b21\uff0c\u4f46\u662f\u57282020\u5e74\u4ec5\u6709\u4e00\u6b21\uff0c\u6240\u4ee5\u7ed3\u679c\u96c6\u5e94\u5305\u542b\u6b64\u6b21\u767b\u5f55\u3002\n8\u53f7\u7528\u6237\u57282020\u5e74\u767b\u5f55\u4e862\u6b21\uff0c\u4e00\u6b21\u57282\u6708\uff0c\u4e00\u6b21\u572812\u6708\uff0c\u6240\u4ee5\uff0c\u7ed3\u679c\u96c6\u5e94\u8be5\u5305\u542b12\u6708\u7684\u8fd9\u6b21\u767b\u5f55\u3002\n2\u53f7\u7528\u6237\u767b\u5f55\u4e862\u6b21\uff0c\u4f46\u662f\u57282020\u5e74\u4ec5\u6709\u4e00\u6b21\uff0c\u6240\u4ee5\u7ed3\u679c\u96c6\u5e94\u5305\u542b\u6b64\u6b21\u767b\u5f55\u3002\n14\u53f7\u7528\u6237\u57282020\u5e74\u6ca1\u6709\u767b\u5f55\uff0c\u6240\u4ee5\u7ed3\u679c\u96c6\u4e0d\u5e94\u5305\u542b\u3002\n
\n", "tags_en": ["Database"], "tags_cn": ["\u6570\u636e\u5e93"], "difficulty_en": "Easy", "difficulty_cn": "\u7b80\u5355", "code_snippets": [{"lang": "MySQL", "langSlug": "mysql", "code": "# Write your MySQL query statement below", "__typename": "CodeSnippetNode"}, {"lang": "MS SQL Server", "langSlug": "mssql", "code": "/* Write your T-SQL query statement below */", "__typename": "CodeSnippetNode"}, {"lang": "Oracle", "langSlug": "oraclesql", "code": "/* Write your PL/SQL query statement below */", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1890](https://leetcode-cn.com/problems/the-latest-login-in-2020)", "[2020\u5e74\u6700\u540e\u4e00\u6b21\u767b\u5f55](/solution/1800-1899/1890.The%20Latest%20Login%20in%202020/README.md)", "`\u6570\u636e\u5e93`", "\u7b80\u5355", "\ud83d\udd12"], "md_table_row_en": ["[1890](https://leetcode.com/problems/the-latest-login-in-2020)", "[The Latest Login in 2020](/solution/1800-1899/1890.The%20Latest%20Login%20in%202020/README_EN.md)", "`Database`", "Easy", "\ud83d\udd12"]}, {"question_id": "2036", "frontend_question_id": "1885", "paid_only": true, "paid_only_cn": true, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/count-pairs-in-two-arrays", "url_en": "https://leetcode.com/problems/count-pairs-in-two-arrays", "relative_path_cn": "/solution/1800-1899/1885.Count%20Pairs%20in%20Two%20Arrays/README.md", "relative_path_en": "/solution/1800-1899/1885.Count%20Pairs%20in%20Two%20Arrays/README_EN.md", "title_cn": "Count Pairs in Two Arrays", "title_en": "Count Pairs in Two Arrays", "question_title_slug": "count-pairs-in-two-arrays", "content_en": "Given two integer arrays nums1
and nums2
of length n
, count the pairs of indices (i, j)
such that i < j
and nums1[i] + nums1[j] > nums2[i] + nums2[j]
.
\n\nReturn the number of pairs satisfying the condition.
\n\n
\nExample 1:
\n\n\nInput: nums1 = [2,1,2,1], nums2 = [1,2,1,2]\nOutput: 1\nExplanation: The pairs satisfying the condition are:\n- (0, 2) where 2 + 2 > 1 + 1.
\n\nExample 2:
\n\n\nInput: nums1 = [1,10,6,2], nums2 = [1,4,1,5]\nOutput: 5\nExplanation: The pairs satisfying the condition are:\n- (0, 1) where 1 + 10 > 1 + 4.\n- (0, 2) where 1 + 6 > 1 + 1.\n- (1, 2) where 10 + 6 > 4 + 1.\n- (1, 3) where 10 + 2 > 4 + 5.\n- (2, 3) where 6 + 2 > 1 + 5.\n
\n\n
\nConstraints:
\n\n\n\tn == nums1.length == nums2.length
\n\t1 <= n <= 105
\n\t1 <= nums1[i], nums2[i] <= 105
\n
\n", "content_cn": "Given two integer arrays nums1
and nums2
of length n
, count the pairs of indices (i, j)
such that i < j
and nums1[i] + nums1[j] > nums2[i] + nums2[j]
.
\n\nReturn the number of pairs satisfying the condition.
\n\n
\nExample 1:
\n\n\nInput: nums1 = [2,1,2,1], nums2 = [1,2,1,2]\nOutput: 1\nExplanation: The pairs satisfying the condition are:\n- (0, 2) where 2 + 2 > 1 + 1.
\n\nExample 2:
\n\n\nInput: nums1 = [1,10,6,2], nums2 = [1,4,1,5]\nOutput: 5\nExplanation: The pairs satisfying the condition are:\n- (0, 1) where 1 + 10 > 1 + 4.\n- (0, 2) where 1 + 6 > 1 + 1.\n- (1, 2) where 10 + 6 > 4 + 1.\n- (1, 3) where 10 + 2 > 4 + 5.\n- (2, 3) where 6 + 2 > 1 + 5.\n
\n\n
\nConstraints:
\n\n\n\tn == nums1.length == nums2.length
\n\t1 <= n <= 105
\n\t1 <= nums1[i], nums2[i] <= 105
\n
\n", "tags_en": ["Array", "Binary Search", "Sorting"], "tags_cn": ["\u6570\u7ec4", "\u4e8c\u5206\u67e5\u627e", "\u6392\u5e8f"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n long long countPairs(vector& nums1, vector& nums2) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public long countPairs(int[] nums1, int[] nums2) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def countPairs(self, nums1, nums2):\n \"\"\"\n :type nums1: List[int]\n :type nums2: List[int]\n :rtype: int\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def countPairs(self, nums1: List[int], nums2: List[int]) -> int:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nlong long countPairs(int* nums1, int nums1Size, int* nums2, int nums2Size){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public long CountPairs(int[] nums1, int[] nums2) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[]} nums1\n * @param {number[]} nums2\n * @return {number}\n */\nvar countPairs = function(nums1, nums2) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer[]} nums1\n# @param {Integer[]} nums2\n# @return {Integer}\ndef count_pairs(nums1, nums2)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func countPairs(_ nums1: [Int], _ nums2: [Int]) -> Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func countPairs(nums1 []int, nums2 []int) int64 {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def countPairs(nums1: Array[Int], nums2: Array[Int]): Long = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun countPairs(nums1: IntArray, nums2: IntArray): Long {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn count_pairs(nums1: Vec, nums2: Vec) -> i64 {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer[] $nums1\n * @param Integer[] $nums2\n * @return Integer\n */\n function countPairs($nums1, $nums2) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function countPairs(nums1: number[], nums2: number[]): number {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (count-pairs nums1 nums2)\n (-> (listof exact-integer?) (listof exact-integer?) exact-integer?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec count_pairs(Nums1 :: [integer()], Nums2 :: [integer()]) -> integer().\ncount_pairs(Nums1, Nums2) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec count_pairs(nums1 :: [integer], nums2 :: [integer]) :: integer\n def count_pairs(nums1, nums2) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1885](https://leetcode-cn.com/problems/count-pairs-in-two-arrays)", "[Count Pairs in Two Arrays](/solution/1800-1899/1885.Count%20Pairs%20in%20Two%20Arrays/README.md)", "`\u6570\u7ec4`,`\u4e8c\u5206\u67e5\u627e`,`\u6392\u5e8f`", "\u4e2d\u7b49", "\ud83d\udd12"], "md_table_row_en": ["[1885](https://leetcode.com/problems/count-pairs-in-two-arrays)", "[Count Pairs in Two Arrays](/solution/1800-1899/1885.Count%20Pairs%20in%20Two%20Arrays/README_EN.md)", "`Array`,`Binary Search`,`Sorting`", "Medium", "\ud83d\udd12"]}, {"question_id": "2035", "frontend_question_id": "1905", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/count-sub-islands", "url_en": "https://leetcode.com/problems/count-sub-islands", "relative_path_cn": "/solution/1900-1999/1905.Count%20Sub%20Islands/README.md", "relative_path_en": "/solution/1900-1999/1905.Count%20Sub%20Islands/README_EN.md", "title_cn": "\u7edf\u8ba1\u5b50\u5c9b\u5c7f", "title_en": "Count Sub Islands", "question_title_slug": "count-sub-islands", "content_en": "You are given two m x n
binary matrices grid1
and grid2
containing only 0
's (representing water) and 1
's (representing land). An island is a group of 1
's connected 4-directionally (horizontal or vertical). Any cells outside of the grid are considered water cells.
\n\nAn island in grid2
is considered a sub-island if there is an island in grid1
that contains all the cells that make up this island in grid2
.
\n\nReturn the number of islands in grid2
that are considered sub-islands.
\n\n
\nExample 1:
\n
\n\nInput: grid1 = [[1,1,1,0,0],[0,1,1,1,1],[0,0,0,0,0],[1,0,0,0,0],[1,1,0,1,1]], grid2 = [[1,1,1,0,0],[0,0,1,1,1],[0,1,0,0,0],[1,0,1,1,0],[0,1,0,1,0]]\nOutput: 3\nExplanation: In the picture above, the grid on the left is grid1 and the grid on the right is grid2.\nThe 1s colored red in grid2 are those considered to be part of a sub-island. There are three sub-islands.\n
\n\nExample 2:
\n
\n\nInput: grid1 = [[1,0,1,0,1],[1,1,1,1,1],[0,0,0,0,0],[1,1,1,1,1],[1,0,1,0,1]], grid2 = [[0,0,0,0,0],[1,1,1,1,1],[0,1,0,1,0],[0,1,0,1,0],[1,0,0,0,1]]\nOutput: 2 \nExplanation: In the picture above, the grid on the left is grid1 and the grid on the right is grid2.\nThe 1s colored red in grid2 are those considered to be part of a sub-island. There are two sub-islands.\n
\n\n
\nConstraints:
\n\n\n\tm == grid1.length == grid2.length
\n\tn == grid1[i].length == grid2[i].length
\n\t1 <= m, n <= 500
\n\tgrid1[i][j]
and grid2[i][j]
are either 0
or 1
. \n
\n", "content_cn": "\u7ed9\u4f60\u4e24\u4e2a\u00a0m x n
\u00a0\u7684\u4e8c\u8fdb\u5236\u77e9\u9635\u00a0grid1
\u548c\u00a0grid2
\u00a0\uff0c\u5b83\u4eec\u53ea\u5305\u542b\u00a00
\u00a0\uff08\u8868\u793a\u6c34\u57df\uff09\u548c 1
\u00a0\uff08\u8868\u793a\u9646\u5730\uff09\u3002\u4e00\u4e2a \u5c9b\u5c7f\u00a0\u662f\u7531 \u56db\u4e2a\u65b9\u5411\u00a0\uff08\u6c34\u5e73\u6216\u8005\u7ad6\u76f4\uff09\u4e0a\u76f8\u90bb\u7684\u00a01
\u00a0\u7ec4\u6210\u7684\u533a\u57df\u3002\u4efb\u4f55\u77e9\u9635\u4ee5\u5916\u7684\u533a\u57df\u90fd\u89c6\u4e3a\u6c34\u57df\u3002
\n\n\u5982\u679c grid2
\u00a0\u7684\u4e00\u4e2a\u5c9b\u5c7f\uff0c\u88ab grid1
\u00a0\u7684\u4e00\u4e2a\u5c9b\u5c7f\u00a0\u5b8c\u5168 \u5305\u542b\uff0c\u4e5f\u5c31\u662f\u8bf4 grid2
\u00a0\u4e2d\u8be5\u5c9b\u5c7f\u7684\u6bcf\u4e00\u4e2a\u683c\u5b50\u90fd\u88ab grid1
\u00a0\u4e2d\u540c\u4e00\u4e2a\u5c9b\u5c7f\u5b8c\u5168\u5305\u542b\uff0c\u90a3\u4e48\u6211\u4eec\u79f0 grid2
\u00a0\u4e2d\u7684\u8fd9\u4e2a\u5c9b\u5c7f\u4e3a \u5b50\u5c9b\u5c7f\u00a0\u3002
\n\n\u8bf7\u4f60\u8fd4\u56de grid2
\u00a0\u4e2d \u5b50\u5c9b\u5c7f\u00a0\u7684 \u6570\u76ee\u00a0\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n
\n\u8f93\u5165\uff1agrid1 = [[1,1,1,0,0],[0,1,1,1,1],[0,0,0,0,0],[1,0,0,0,0],[1,1,0,1,1]], grid2 = [[1,1,1,0,0],[0,0,1,1,1],[0,1,0,0,0],[1,0,1,1,0],[0,1,0,1,0]]\n\u8f93\u51fa\uff1a3\n\u89e3\u91ca\uff1a\u5982\u4e0a\u56fe\u6240\u793a\uff0c\u5de6\u8fb9\u4e3a grid1 \uff0c\u53f3\u8fb9\u4e3a grid2 \u3002\ngrid2 \u4e2d\u6807\u7ea2\u7684 1 \u533a\u57df\u662f\u5b50\u5c9b\u5c7f\uff0c\u603b\u5171\u6709 3 \u4e2a\u5b50\u5c9b\u5c7f\u3002\n
\n\n\u793a\u4f8b 2\uff1a
\n
\n\u8f93\u5165\uff1agrid1 = [[1,0,1,0,1],[1,1,1,1,1],[0,0,0,0,0],[1,1,1,1,1],[1,0,1,0,1]], grid2 = [[0,0,0,0,0],[1,1,1,1,1],[0,1,0,1,0],[0,1,0,1,0],[1,0,0,0,1]]\n\u8f93\u51fa\uff1a2 \n\u89e3\u91ca\uff1a\u5982\u4e0a\u56fe\u6240\u793a\uff0c\u5de6\u8fb9\u4e3a grid1 \uff0c\u53f3\u8fb9\u4e3a grid2 \u3002\ngrid2 \u4e2d\u6807\u7ea2\u7684 1 \u533a\u57df\u662f\u5b50\u5c9b\u5c7f\uff0c\u603b\u5171\u6709 2 \u4e2a\u5b50\u5c9b\u5c7f\u3002\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\tm == grid1.length == grid2.length
\n\tn == grid1[i].length == grid2[i].length
\n\t1 <= m, n <= 500
\n\tgrid1[i][j]
\u548c\u00a0grid2[i][j]
\u00a0\u90fd\u8981\u4e48\u662f\u00a00
\u00a0\u8981\u4e48\u662f\u00a01
\u00a0\u3002 \n
\n", "tags_en": ["Depth-First Search", "Breadth-First Search", "Union Find", "Array", "Matrix"], "tags_cn": ["\u6df1\u5ea6\u4f18\u5148\u641c\u7d22", "\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22", "\u5e76\u67e5\u96c6", "\u6570\u7ec4", "\u77e9\u9635"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n int countSubIslands(vector>& grid1, vector>& grid2) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int countSubIslands(int[][] grid1, int[][] grid2) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def countSubIslands(self, grid1, grid2):\n \"\"\"\n :type grid1: List[List[int]]\n :type grid2: List[List[int]]\n :rtype: int\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def countSubIslands(self, grid1: List[List[int]], grid2: List[List[int]]) -> int:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nint countSubIslands(int** grid1, int grid1Size, int* grid1ColSize, int** grid2, int grid2Size, int* grid2ColSize){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int CountSubIslands(int[][] grid1, int[][] grid2) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[][]} grid1\n * @param {number[][]} grid2\n * @return {number}\n */\nvar countSubIslands = function(grid1, grid2) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer[][]} grid1\n# @param {Integer[][]} grid2\n# @return {Integer}\ndef count_sub_islands(grid1, grid2)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func countSubIslands(_ grid1: [[Int]], _ grid2: [[Int]]) -> Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func countSubIslands(grid1 [][]int, grid2 [][]int) int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def countSubIslands(grid1: Array[Array[Int]], grid2: Array[Array[Int]]): Int = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun countSubIslands(grid1: Array, grid2: Array): Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn count_sub_islands(grid1: Vec>, grid2: Vec>) -> i32 {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer[][] $grid1\n * @param Integer[][] $grid2\n * @return Integer\n */\n function countSubIslands($grid1, $grid2) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function countSubIslands(grid1: number[][], grid2: number[][]): number {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (count-sub-islands grid1 grid2)\n (-> (listof (listof exact-integer?)) (listof (listof exact-integer?)) exact-integer?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec count_sub_islands(Grid1 :: [[integer()]], Grid2 :: [[integer()]]) -> integer().\ncount_sub_islands(Grid1, Grid2) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec count_sub_islands(grid1 :: [[integer]], grid2 :: [[integer]]) :: integer\n def count_sub_islands(grid1, grid2) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1905](https://leetcode-cn.com/problems/count-sub-islands)", "[\u7edf\u8ba1\u5b50\u5c9b\u5c7f](/solution/1900-1999/1905.Count%20Sub%20Islands/README.md)", "`\u6df1\u5ea6\u4f18\u5148\u641c\u7d22`,`\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22`,`\u5e76\u67e5\u96c6`,`\u6570\u7ec4`,`\u77e9\u9635`", "\u4e2d\u7b49", ""], "md_table_row_en": ["[1905](https://leetcode.com/problems/count-sub-islands)", "[Count Sub Islands](/solution/1900-1999/1905.Count%20Sub%20Islands/README_EN.md)", "`Depth-First Search`,`Breadth-First Search`,`Union Find`,`Array`,`Matrix`", "Medium", ""]}, {"question_id": "2034", "frontend_question_id": "1906", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/minimum-absolute-difference-queries", "url_en": "https://leetcode.com/problems/minimum-absolute-difference-queries", "relative_path_cn": "/solution/1900-1999/1906.Minimum%20Absolute%20Difference%20Queries/README.md", "relative_path_en": "/solution/1900-1999/1906.Minimum%20Absolute%20Difference%20Queries/README_EN.md", "title_cn": "\u67e5\u8be2\u5dee\u7edd\u5bf9\u503c\u7684\u6700\u5c0f\u503c", "title_en": "Minimum Absolute Difference Queries", "question_title_slug": "minimum-absolute-difference-queries", "content_en": "The minimum absolute difference of an array a
is defined as the minimum value of |a[i] - a[j]|
, where 0 <= i < j < a.length
and a[i] != a[j]
. If all elements of a
are the same, the minimum absolute difference is -1
.
\n\n\n\t- For example, the minimum absolute difference of the array
[5,2,3,7,2]
is |2 - 3| = 1
. Note that it is not 0
because a[i]
and a[j]
must be different. \n
\n\nYou are given an integer array nums
and the array queries
where queries[i] = [li, ri]
. For each query i
, compute the minimum absolute difference of the subarray nums[li...ri]
containing the elements of nums
between the 0-based indices li
and ri
(inclusive).
\n\nReturn an array ans
where ans[i]
is the answer to the ith
query.
\n\nA subarray is a contiguous sequence of elements in an array.
\n\nThe value of |x|
is defined as:
\n\n\n\tx
if x >= 0
. \n\t-x
if x < 0
. \n
\n\n
\nExample 1:
\n\n\nInput: nums = [1,3,4,8], queries = [[0,1],[1,2],[2,3],[0,3]]\nOutput: [2,1,4,1]\nExplanation: The queries are processed as follows:\n- queries[0] = [0,1]: The subarray is [1,3] and the minimum absolute difference is |1-3| = 2.\n- queries[1] = [1,2]: The subarray is [3,4] and the minimum absolute difference is |3-4| = 1.\n- queries[2] = [2,3]: The subarray is [4,8] and the minimum absolute difference is |4-8| = 4.\n- queries[3] = [0,3]: The subarray is [1,3,4,8] and the minimum absolute difference is |3-4| = 1.\n
\n\nExample 2:
\n\n\nInput: nums = [4,5,2,2,7,10], queries = [[2,3],[0,2],[0,5],[3,5]]\nOutput: [-1,1,1,3]\nExplanation: The queries are processed as follows:\n- queries[0] = [2,3]: The subarray is [2,2] and the minimum absolute difference is -1 because all the\n elements are the same.\n- queries[1] = [0,2]: The subarray is [4,5,2] and the minimum absolute difference is |4-5| = 1.\n- queries[2] = [0,5]: The subarray is [4,5,2,2,7,10] and the minimum absolute difference is |4-5| = 1.\n- queries[3] = [3,5]: The subarray is [2,7,10] and the minimum absolute difference is |7-10| = 3.\n
\n\n
\nConstraints:
\n\n\n\t2 <= nums.length <= 105
\n\t1 <= nums[i] <= 100
\n\t1 <= queries.length <= 2 * 104
\n\t0 <= li < ri < nums.length
\n
\n", "content_cn": "\u4e00\u4e2a\u6570\u7ec4 a
\u00a0\u7684 \u5dee\u7edd\u5bf9\u503c\u7684\u6700\u5c0f\u503c\u00a0\u5b9a\u4e49\u4e3a\uff1a0 <= i < j < a.length
\u00a0\u4e14 a[i] != a[j]
\u00a0\u7684 |a[i] - a[j]|
\u7684 \u6700\u5c0f\u503c\u3002\u5982\u679c a
\u00a0\u4e2d\u6240\u6709\u5143\u7d20\u90fd \u76f8\u540c\u00a0\uff0c\u90a3\u4e48\u5dee\u7edd\u5bf9\u503c\u7684\u6700\u5c0f\u503c\u4e3a -1
\u00a0\u3002
\n\n\n\t- \u6bd4\u65b9\u8bf4\uff0c\u6570\u7ec4\u00a0
[5,2,3,7,2]
\u00a0\u5dee\u7edd\u5bf9\u503c\u7684\u6700\u5c0f\u503c\u662f\u00a0|2 - 3| = 1
\u00a0\u3002\u6ce8\u610f\u7b54\u6848\u4e0d\u4e3a 0
\u00a0\uff0c\u56e0\u4e3a\u00a0a[i]
\u548c\u00a0a[j]
\u00a0\u5fc5\u987b\u4e0d\u76f8\u7b49\u3002 \n
\n\n\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4\u00a0nums
\u00a0\u548c\u67e5\u8be2\u6570\u7ec4\u00a0queries
\u00a0\uff0c\u5176\u4e2d\u00a0queries[i] = [li, ri]
\u00a0\u3002\u5bf9\u4e8e\u6bcf\u4e2a\u67e5\u8be2\u00a0i
\u00a0\uff0c\u8ba1\u7b97\u00a0\u5b50\u6570\u7ec4\u00a0nums[li...ri]
\u00a0\u4e2d \u5dee\u7edd\u5bf9\u503c\u7684\u6700\u5c0f\u503c \uff0c\u5b50\u6570\u7ec4\u00a0nums[li...ri]
\u5305\u542b nums
\u00a0\u6570\u7ec4\uff08\u4e0b\u6807\u4ece 0\u00a0\u5f00\u59cb\uff09\u4e2d\u4e0b\u6807\u5728 li
\u548c\u00a0ri
\u4e4b\u95f4\u7684\u6240\u6709\u5143\u7d20\uff08\u5305\u542b\u00a0li
\u548c\u00a0ri
\u5728\u5185\uff09\u3002
\n\n\u8bf7\u4f60\u8fd4\u56de\u00a0ans
\u00a0\u6570\u7ec4\uff0c\u5176\u4e2d ans[i]
\u00a0\u662f\u7b2c i
\u00a0\u4e2a\u67e5\u8be2\u7684\u7b54\u6848\u3002
\n\n\u5b50\u6570\u7ec4\u00a0\u662f\u4e00\u4e2a\u6570\u7ec4\u4e2d\u8fde\u7eed\u7684\u4e00\u6bb5\u5143\u7d20\u3002
\n\n|x|
\u00a0\u7684\u503c\u5b9a\u4e49\u4e3a\uff1a
\n\n\n\t- \u5982\u679c\u00a0
x >= 0
\u00a0\uff0c\u90a3\u4e48\u503c\u4e3a\u00a0x
\u00a0\u3002 \n\t- \u5982\u679c\u00a0
x < 0
\u00a0\uff0c\u90a3\u4e48\u503c\u4e3a\u00a0-x
\u00a0\u3002 \n
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1anums = [1,3,4,8], queries = [[0,1],[1,2],[2,3],[0,3]]\n\u8f93\u51fa\uff1a[2,1,4,1]\n\u89e3\u91ca\uff1a\u67e5\u8be2\u7ed3\u679c\u5982\u4e0b\uff1a\n- queries[0] = [0,1]\uff1a\u5b50\u6570\u7ec4\u662f [1,3] \uff0c\u5dee\u7edd\u5bf9\u503c\u7684\u6700\u5c0f\u503c\u4e3a |1-3| = 2 \u3002\n- queries[1] = [1,2]\uff1a\u5b50\u6570\u7ec4\u662f [3,4] \uff0c\u5dee\u7edd\u5bf9\u503c\u7684\u6700\u5c0f\u503c\u4e3a |3-4| = 1 \u3002\n- queries[2] = [2,3]\uff1a\u5b50\u6570\u7ec4\u662f [4,8] \uff0c\u5dee\u7edd\u5bf9\u503c\u7684\u6700\u5c0f\u503c\u4e3a |4-8| = 4 \u3002\n- queries[3] = [0,3]\uff1a\u5b50\u6570\u7ec4\u662f [1,3,4,8] \uff0c\u5dee\u7684\u7edd\u5bf9\u503c\u7684\u6700\u5c0f\u503c\u4e3a |3-4| = 1 \u3002\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1anums = [4,5,2,2,7,10], queries = [[2,3],[0,2],[0,5],[3,5]]\n\u8f93\u51fa\uff1a[-1,1,1,3]\n\u89e3\u91ca\uff1a\u67e5\u8be2\u7ed3\u679c\u5982\u4e0b\uff1a\n- queries[0] = [2,3]\uff1a\u5b50\u6570\u7ec4\u662f [2,2] \uff0c\u5dee\u7edd\u5bf9\u503c\u7684\u6700\u5c0f\u503c\u4e3a -1 \uff0c\u56e0\u4e3a\u6240\u6709\u5143\u7d20\u76f8\u7b49\u3002\n- queries[1] = [0,2]\uff1a\u5b50\u6570\u7ec4\u662f [4,5,2] \uff0c\u5dee\u7edd\u5bf9\u503c\u7684\u6700\u5c0f\u503c\u4e3a |4-5| = 1 \u3002\n- queries[2] = [0,5]\uff1a\u5b50\u6570\u7ec4\u662f [4,5,2,2,7,10] \uff0c\u5dee\u7edd\u5bf9\u503c\u7684\u6700\u5c0f\u503c\u4e3a |4-5| = 1 \u3002\n- queries[3] = [3,5]\uff1a\u5b50\u6570\u7ec4\u662f [2,7,10] \uff0c\u5dee\u7edd\u5bf9\u503c\u7684\u6700\u5c0f\u503c\u4e3a |7-10| = 3 \u3002\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\t2 <= nums.length <= 105
\n\t1 <= nums[i] <= 100
\n\t1 <= queries.length <= 2\u00a0* 104
\n\t0 <= li < ri < nums.length
\n
\n", "tags_en": ["Array", "Hash Table"], "tags_cn": ["\u6570\u7ec4", "\u54c8\u5e0c\u8868"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n vector minDifference(vector& nums, vector>& queries) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int[] minDifference(int[] nums, int[][] queries) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def minDifference(self, nums, queries):\n \"\"\"\n :type nums: List[int]\n :type queries: List[List[int]]\n :rtype: List[int]\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def minDifference(self, nums: List[int], queries: List[List[int]]) -> List[int]:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\n/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\nint* minDifference(int* nums, int numsSize, int** queries, int queriesSize, int* queriesColSize, int* returnSize){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int[] MinDifference(int[] nums, int[][] queries) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[]} nums\n * @param {number[][]} queries\n * @return {number[]}\n */\nvar minDifference = function(nums, queries) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer[]} nums\n# @param {Integer[][]} queries\n# @return {Integer[]}\ndef min_difference(nums, queries)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func minDifference(_ nums: [Int], _ queries: [[Int]]) -> [Int] {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func minDifference(nums []int, queries [][]int) []int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def minDifference(nums: Array[Int], queries: Array[Array[Int]]): Array[Int] = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun minDifference(nums: IntArray, queries: Array): IntArray {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn min_difference(nums: Vec, queries: Vec>) -> Vec {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @param Integer[][] $queries\n * @return Integer[]\n */\n function minDifference($nums, $queries) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function minDifference(nums: number[], queries: number[][]): number[] {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (min-difference nums queries)\n (-> (listof exact-integer?) (listof (listof exact-integer?)) (listof exact-integer?))\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec min_difference(Nums :: [integer()], Queries :: [[integer()]]) -> [integer()].\nmin_difference(Nums, Queries) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec min_difference(nums :: [integer], queries :: [[integer]]) :: [integer]\n def min_difference(nums, queries) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1906](https://leetcode-cn.com/problems/minimum-absolute-difference-queries)", "[\u67e5\u8be2\u5dee\u7edd\u5bf9\u503c\u7684\u6700\u5c0f\u503c](/solution/1900-1999/1906.Minimum%20Absolute%20Difference%20Queries/README.md)", "`\u6570\u7ec4`,`\u54c8\u5e0c\u8868`", "\u4e2d\u7b49", ""], "md_table_row_en": ["[1906](https://leetcode.com/problems/minimum-absolute-difference-queries)", "[Minimum Absolute Difference Queries](/solution/1900-1999/1906.Minimum%20Absolute%20Difference%20Queries/README_EN.md)", "`Array`,`Hash Table`", "Medium", ""]}, {"question_id": "2033", "frontend_question_id": "1904", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/the-number-of-full-rounds-you-have-played", "url_en": "https://leetcode.com/problems/the-number-of-full-rounds-you-have-played", "relative_path_cn": "/solution/1900-1999/1904.The%20Number%20of%20Full%20Rounds%20You%20Have%20Played/README.md", "relative_path_en": "/solution/1900-1999/1904.The%20Number%20of%20Full%20Rounds%20You%20Have%20Played/README_EN.md", "title_cn": "\u4f60\u5b8c\u6210\u7684\u5b8c\u6574\u5bf9\u5c40\u6570", "title_en": "The Number of Full Rounds You Have Played", "question_title_slug": "the-number-of-full-rounds-you-have-played", "content_en": "A new online video game has been released, and in this video game, there are 15-minute rounds scheduled every quarter-hour period. This means that at HH:00
, HH:15
, HH:30
and HH:45
, a new round starts, where HH
represents an integer number from 00
to 23
. A 24-hour clock is used, so the earliest time in the day is 00:00
and the latest is 23:59
.
\n\nGiven two strings startTime
and finishTime
in the format "HH:MM"
representing the exact time you started and finished playing the game, respectively, calculate the number of full rounds that you played during your game session.
\n\n\n\t- For example, if
startTime = "05:20"
and finishTime = "05:59"
this means you played only one full round from 05:30
to 05:45
. You did not play the full round from 05:15
to 05:30
because you started after the round began, and you did not play the full round from 05:45
to 06:00
because you stopped before the round ended. \n
\n\nIf finishTime
is earlier than startTime
, this means you have played overnight (from startTime
to the midnight and from midnight to finishTime
).
\n\nReturn the number of full rounds that you have played if you had started playing at startTime
and finished at finishTime
.
\n\n
\nExample 1:
\n\n\nInput: startTime = "12:01", finishTime = "12:44"\nOutput: 1\nExplanation: You played one full round from 12:15 to 12:30.\nYou did not play the full round from 12:00 to 12:15 because you started playing at 12:01 after it began.\nYou did not play the full round from 12:30 to 12:45 because you stopped playing at 12:44 before it ended.\n
\n\nExample 2:
\n\n\nInput: startTime = "20:00", finishTime = "06:00"\nOutput: 40\nExplanation: You played 16 full rounds from 20:00 to 00:00 and 24 full rounds from 00:00 to 06:00.\n16 + 24 = 40.\n
\n\nExample 3:
\n\n\nInput: startTime = "00:00", finishTime = "23:59"\nOutput: 95\nExplanation: You played 4 full rounds each hour except for the last hour where you played 3 full rounds.\n
\n\n
\nConstraints:
\n\n\n\tstartTime
and finishTime
are in the format HH:MM
. \n\t00 <= HH <= 23
\n\t00 <= MM <= 59
\n\tstartTime
and finishTime
are not equal. \n
\n", "content_cn": "\u4e00\u6b3e\u65b0\u7684\u5728\u7ebf\u7535\u5b50\u6e38\u620f\u5728\u8fd1\u671f\u53d1\u5e03\uff0c\u5728\u8be5\u7535\u5b50\u6e38\u620f\u4e2d\uff0c\u4ee5 \u523b\u949f \u4e3a\u5468\u671f\u89c4\u5212\u82e5\u5e72\u65f6\u957f\u4e3a 15 \u5206\u949f \u7684\u6e38\u620f\u5bf9\u5c40\u3002\u8fd9\u610f\u5473\u7740\uff0c\u5728 HH:00
\u3001HH:15
\u3001HH:30
\u548c HH:45
\uff0c\u5c06\u4f1a\u5f00\u59cb\u4e00\u4e2a\u65b0\u7684\u5bf9\u5c40\uff0c\u5176\u4e2d HH
\u7528\u4e00\u4e2a\u4ece 00
\u5230 23
\u7684\u6574\u6570\u8868\u793a\u3002\u6e38\u620f\u4e2d\u4f7f\u7528 24 \u5c0f\u65f6\u5236\u7684\u65f6\u949f \uff0c\u6240\u4ee5\u4e00\u5929\u4e2d\u6700\u65e9\u7684\u65f6\u95f4\u662f 00:00
\u00a0\uff0c\u6700\u665a\u7684\u65f6\u95f4\u662f 23:59
\u3002
\n\n\u7ed9\u4f60\u4e24\u4e2a\u5b57\u7b26\u4e32 startTime
\u548c finishTime
\uff0c\u5747\u7b26\u5408 \"HH:MM\"
\u683c\u5f0f\uff0c\u5206\u522b\u8868\u793a\u4f60 \u8fdb\u5165 \u548c \u9000\u51fa \u6e38\u620f\u7684\u786e\u5207\u65f6\u95f4\uff0c\u8bf7\u8ba1\u7b97\u5728\u6574\u4e2a\u6e38\u620f\u4f1a\u8bdd\u671f\u95f4\uff0c\u4f60\u5b8c\u6210\u7684 \u5b8c\u6574\u5bf9\u5c40\u7684\u5bf9\u5c40\u6570 \u3002
\n\n\n\t- \u4f8b\u5982\uff0c\u5982\u679c
startTime = \"05:20\"
\u4e14 finishTime = \"05:59\"
\uff0c\u8fd9\u610f\u5473\u7740\u4f60\u4ec5\u4ec5\u5b8c\u6210\u4ece 05:30
\u5230 05:45
\u8fd9\u4e00\u4e2a\u5b8c\u6574\u5bf9\u5c40\u3002\u800c\u4f60\u6ca1\u6709\u5b8c\u6210\u4ece 05:15
\u5230 05:30
\u7684\u5b8c\u6574\u5bf9\u5c40\uff0c\u56e0\u4e3a\u4f60\u662f\u5728\u5bf9\u5c40\u5f00\u59cb\u540e\u8fdb\u5165\u7684\u6e38\u620f\uff1b\u540c\u65f6\uff0c\u4f60\u4e5f\u6ca1\u6709\u5b8c\u6210\u4ece 05:45
\u5230 06:00
\u7684\u5b8c\u6574\u5bf9\u5c40\uff0c\u56e0\u4e3a\u4f60\u662f\u5728\u5bf9\u5c40\u7ed3\u675f\u524d\u9000\u51fa\u7684\u6e38\u620f\u3002 \n
\n\n\u5982\u679c finishTime
\u65e9\u4e8e startTime
\uff0c\u8fd9\u8868\u793a\u4f60\u73a9\u4e86\u4e2a\u901a\u5bb5\uff08\u4e5f\u5c31\u662f\u4ece startTime
\u5230\u5348\u591c\uff0c\u518d\u4ece\u5348\u591c\u5230 finishTime
\uff09\u3002
\n\n\u5047\u8bbe\u4f60\u662f\u4ece\u00a0startTime
\u8fdb\u5165\u6e38\u620f\uff0c\u5e76\u5728\u00a0finishTime
\u9000\u51fa\u6e38\u620f\uff0c\u8bf7\u8ba1\u7b97\u5e76\u8fd4\u56de\u4f60\u5b8c\u6210\u7684 \u5b8c\u6574\u5bf9\u5c40\u7684\u5bf9\u5c40\u6570 \u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1astartTime = \"12:01\", finishTime = \"12:44\"\n\u8f93\u51fa\uff1a1\n\u89e3\u91ca\uff1a\u4f60\u5b8c\u6210\u4e86\u4ece 12:15 \u5230 12:30 \u7684\u4e00\u4e2a\u5b8c\u6574\u5bf9\u5c40\u3002\n\u4f60\u6ca1\u6709\u5b8c\u6210\u4ece 12:00 \u5230 12:15 \u7684\u5b8c\u6574\u5bf9\u5c40\uff0c\u56e0\u4e3a\u4f60\u662f\u5728\u5bf9\u5c40\u5f00\u59cb\u540e\u7684 12:01 \u8fdb\u5165\u7684\u6e38\u620f\u3002\n\u4f60\u6ca1\u6709\u5b8c\u6210\u4ece 12:30 \u5230 12:45 \u7684\u5b8c\u6574\u5bf9\u5c40\uff0c\u56e0\u4e3a\u4f60\u662f\u5728\u5bf9\u5c40\u7ed3\u675f\u524d\u7684 12:44 \u9000\u51fa\u7684\u6e38\u620f\u3002\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1astartTime = \"20:00\", finishTime = \"06:00\"\n\u8f93\u51fa\uff1a40\n\u89e3\u91ca\uff1a\u4f60\u5b8c\u6210\u4e86\u4ece 20:00 \u5230 00:00 \u7684 16 \u4e2a\u5b8c\u6574\u7684\u5bf9\u5c40\uff0c\u4ee5\u53ca\u4ece 00:00 \u5230 06:00 \u7684 24 \u4e2a\u5b8c\u6574\u7684\u5bf9\u5c40\u3002\n16 + 24 = 40\n
\n\n\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1astartTime = \"00:00\", finishTime = \"23:59\"\n\u8f93\u51fa\uff1a95\n\u89e3\u91ca\uff1a\u9664\u6700\u540e\u4e00\u4e2a\u5c0f\u65f6\u4f60\u53ea\u5b8c\u6210\u4e86 3 \u4e2a\u5b8c\u6574\u5bf9\u5c40\u5916\uff0c\u5176\u4f59\u6bcf\u4e2a\u5c0f\u65f6\u5747\u5b8c\u6210\u4e86 4 \u573a\u5b8c\u6574\u5bf9\u5c40\u3002\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\tstartTime
\u548c finishTime
\u7684\u683c\u5f0f\u4e3a HH:MM
\n\t00 <= HH <= 23
\n\t00 <= MM <= 59
\n\tstartTime
\u548c finishTime
\u4e0d\u76f8\u7b49 \n
\n", "tags_en": ["Math", "String"], "tags_cn": ["\u6570\u5b66", "\u5b57\u7b26\u4e32"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n int numberOfRounds(string startTime, string finishTime) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int numberOfRounds(String startTime, String finishTime) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def numberOfRounds(self, startTime, finishTime):\n \"\"\"\n :type startTime: str\n :type finishTime: str\n :rtype: int\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def numberOfRounds(self, startTime: str, finishTime: str) -> int:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nint numberOfRounds(char * startTime, char * finishTime){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int NumberOfRounds(string startTime, string finishTime) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {string} startTime\n * @param {string} finishTime\n * @return {number}\n */\nvar numberOfRounds = function(startTime, finishTime) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {String} start_time\n# @param {String} finish_time\n# @return {Integer}\ndef number_of_rounds(start_time, finish_time)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func numberOfRounds(_ startTime: String, _ finishTime: String) -> Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func numberOfRounds(startTime string, finishTime string) int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def numberOfRounds(startTime: String, finishTime: String): Int = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun numberOfRounds(startTime: String, finishTime: String): Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn number_of_rounds(start_time: String, finish_time: String) -> i32 {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param String $startTime\n * @param String $finishTime\n * @return Integer\n */\n function numberOfRounds($startTime, $finishTime) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function numberOfRounds(startTime: string, finishTime: string): number {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (number-of-rounds startTime finishTime)\n (-> string? string? exact-integer?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec number_of_rounds(StartTime :: unicode:unicode_binary(), FinishTime :: unicode:unicode_binary()) -> integer().\nnumber_of_rounds(StartTime, FinishTime) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec number_of_rounds(start_time :: String.t, finish_time :: String.t) :: integer\n def number_of_rounds(start_time, finish_time) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1904](https://leetcode-cn.com/problems/the-number-of-full-rounds-you-have-played)", "[\u4f60\u5b8c\u6210\u7684\u5b8c\u6574\u5bf9\u5c40\u6570](/solution/1900-1999/1904.The%20Number%20of%20Full%20Rounds%20You%20Have%20Played/README.md)", "`\u6570\u5b66`,`\u5b57\u7b26\u4e32`", "\u4e2d\u7b49", ""], "md_table_row_en": ["[1904](https://leetcode.com/problems/the-number-of-full-rounds-you-have-played)", "[The Number of Full Rounds You Have Played](/solution/1900-1999/1904.The%20Number%20of%20Full%20Rounds%20You%20Have%20Played/README_EN.md)", "`Math`,`String`", "Medium", ""]}, {"question_id": "2032", "frontend_question_id": "1903", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/largest-odd-number-in-string", "url_en": "https://leetcode.com/problems/largest-odd-number-in-string", "relative_path_cn": "/solution/1900-1999/1903.Largest%20Odd%20Number%20in%20String/README.md", "relative_path_en": "/solution/1900-1999/1903.Largest%20Odd%20Number%20in%20String/README_EN.md", "title_cn": "\u5b57\u7b26\u4e32\u4e2d\u7684\u6700\u5927\u5947\u6570", "title_en": "Largest Odd Number in String", "question_title_slug": "largest-odd-number-in-string", "content_en": "You are given a string num
, representing a large integer. Return the largest-valued odd integer (as a string) that is a non-empty substring of num
, or an empty string ""
if no odd integer exists.
\n\nA substring is a contiguous sequence of characters within a string.
\n\n
\nExample 1:
\n\n\nInput: num = "52"\nOutput: "5"\nExplanation: The only non-empty substrings are "5", "2", and "52". "5" is the only odd number.\n
\n\nExample 2:
\n\n\nInput: num = "4206"\nOutput: ""\nExplanation: There are no odd numbers in "4206".\n
\n\nExample 3:
\n\n\nInput: num = "35427"\nOutput: "35427"\nExplanation: "35427" is already an odd number.\n
\n\n
\nConstraints:
\n\n\n\t1 <= num.length <= 105
\n\tnum
only consists of digits and does not contain any leading zeros. \n
\n", "content_cn": "\u7ed9\u4f60\u4e00\u4e2a\u5b57\u7b26\u4e32 num
\uff0c\u8868\u793a\u4e00\u4e2a\u5927\u6574\u6570\u3002\u8bf7\u4f60\u5728\u5b57\u7b26\u4e32 num
\u7684\u6240\u6709\u00a0\u975e\u7a7a\u5b50\u5b57\u7b26\u4e32 \u4e2d\u627e\u51fa \u503c\u6700\u5927\u7684\u5947\u6570 \uff0c\u5e76\u4ee5\u5b57\u7b26\u4e32\u5f62\u5f0f\u8fd4\u56de\u3002\u5982\u679c\u4e0d\u5b58\u5728\u5947\u6570\uff0c\u5219\u8fd4\u56de\u4e00\u4e2a\u7a7a\u5b57\u7b26\u4e32 \"\"
\u3002
\n\n\u5b50\u5b57\u7b26\u4e32 \u662f\u5b57\u7b26\u4e32\u4e2d\u7684\u4e00\u4e2a\u8fde\u7eed\u7684\u5b57\u7b26\u5e8f\u5217\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1anum = \"52\"\n\u8f93\u51fa\uff1a\"5\"\n\u89e3\u91ca\uff1a\u975e\u7a7a\u5b50\u5b57\u7b26\u4e32\u4ec5\u6709 \"5\"\u3001\"2\" \u548c \"52\" \u3002\"5\" \u662f\u5176\u4e2d\u552f\u4e00\u7684\u5947\u6570\u3002\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1anum = \"4206\"\n\u8f93\u51fa\uff1a\"\"\n\u89e3\u91ca\uff1a\u5728 \"4206\" \u4e2d\u4e0d\u5b58\u5728\u5947\u6570\u3002\n
\n\n\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1anum = \"35427\"\n\u8f93\u51fa\uff1a\"35427\"\n\u89e3\u91ca\uff1a\"35427\" \u672c\u8eab\u5c31\u662f\u4e00\u4e2a\u5947\u6570\u3002\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\t1 <= num.length <= 105
\n\tnum
\u4ec5\u7531\u6570\u5b57\u7ec4\u6210\u4e14\u4e0d\u542b\u524d\u5bfc\u96f6 \n
\n", "tags_en": ["Greedy", "Math", "String"], "tags_cn": ["\u8d2a\u5fc3", "\u6570\u5b66", "\u5b57\u7b26\u4e32"], "difficulty_en": "Easy", "difficulty_cn": "\u7b80\u5355", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n string largestOddNumber(string num) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public String largestOddNumber(String num) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def largestOddNumber(self, num):\n \"\"\"\n :type num: str\n :rtype: str\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def largestOddNumber(self, num: str) -> str:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nchar * largestOddNumber(char * num){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public string LargestOddNumber(string num) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {string} num\n * @return {string}\n */\nvar largestOddNumber = function(num) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {String} num\n# @return {String}\ndef largest_odd_number(num)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func largestOddNumber(_ num: String) -> String {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func largestOddNumber(num string) string {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def largestOddNumber(num: String): String = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun largestOddNumber(num: String): String {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn largest_odd_number(num: String) -> String {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param String $num\n * @return String\n */\n function largestOddNumber($num) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function largestOddNumber(num: string): string {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (largest-odd-number num)\n (-> string? string?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec largest_odd_number(Num :: unicode:unicode_binary()) -> unicode:unicode_binary().\nlargest_odd_number(Num) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec largest_odd_number(num :: String.t) :: String.t\n def largest_odd_number(num) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1903](https://leetcode-cn.com/problems/largest-odd-number-in-string)", "[\u5b57\u7b26\u4e32\u4e2d\u7684\u6700\u5927\u5947\u6570](/solution/1900-1999/1903.Largest%20Odd%20Number%20in%20String/README.md)", "`\u8d2a\u5fc3`,`\u6570\u5b66`,`\u5b57\u7b26\u4e32`", "\u7b80\u5355", ""], "md_table_row_en": ["[1903](https://leetcode.com/problems/largest-odd-number-in-string)", "[Largest Odd Number in String](/solution/1900-1999/1903.Largest%20Odd%20Number%20in%20String/README_EN.md)", "`Greedy`,`Math`,`String`", "Easy", ""]}, {"question_id": "2031", "frontend_question_id": "1884", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/egg-drop-with-2-eggs-and-n-floors", "url_en": "https://leetcode.com/problems/egg-drop-with-2-eggs-and-n-floors", "relative_path_cn": "/solution/1800-1899/1884.Egg%20Drop%20With%202%20Eggs%20and%20N%20Floors/README.md", "relative_path_en": "/solution/1800-1899/1884.Egg%20Drop%20With%202%20Eggs%20and%20N%20Floors/README_EN.md", "title_cn": "\u9e21\u86cb\u6389\u843d-\u4e24\u679a\u9e21\u86cb", "title_en": "Egg Drop With 2 Eggs and N Floors", "question_title_slug": "egg-drop-with-2-eggs-and-n-floors", "content_en": "You are given two identical eggs and you have access to a building with n
floors labeled from 1
to n
.
\n\nYou know that there exists a floor f
where 0 <= f <= n
such that any egg dropped at a floor higher than f
will break, and any egg dropped at or below floor f
will not break.
\n\nIn each move, you may take an unbroken egg and drop it from any floor x
(where 1 <= x <= n
). If the egg breaks, you can no longer use it. However, if the egg does not break, you may reuse it in future moves.
\n\nReturn the minimum number of moves that you need to determine with certainty what the value of f
is.
\n\n
\nExample 1:
\n\n\nInput: n = 2\nOutput: 2\nExplanation: We can drop the first egg from floor 1 and the second egg from floor 2.\nIf the first egg breaks, we know that f = 0.\nIf the second egg breaks but the first egg didn't, we know that f = 1.\nOtherwise, if both eggs survive, we know that f = 2.\n
\n\nExample 2:
\n\n\nInput: n = 100\nOutput: 14\nExplanation: One optimal strategy is:\n- Drop the 1st egg at floor 9. If it breaks, we know f is between 0 and 8. Drop the 2nd egg starting\n from floor 1 and going up one at a time to find f within 7 more drops. Total drops is 1 + 7 = 8.\n- If the 1st egg does not break, drop the 1st egg again at floor 22. If it breaks, we know f is between 9\n and 21. Drop the 2nd egg starting from floor 10 and going up one at a time to find f within 12 more\n drops. Total drops is 2 + 12 = 14.\n- If the 1st egg does not break again, follow a similar process dropping the 1st egg from floors 34, 45,\n 55, 64, 72, 79, 85, 90, 94, 97, 99, and 100.\nRegardless of the outcome, it takes at most 14 drops to determine f.\n
\n\n
\nConstraints:
\n\n\n", "content_cn": "\u7ed9\u4f60 2\u00a0\u679a\u76f8\u540c \u7684\u9e21\u86cb\uff0c\u548c\u4e00\u680b\u4ece\u7b2c 1
\u00a0\u5c42\u5230\u7b2c n
\u5c42\u5171\u6709 n
\u5c42\u697c\u7684\u5efa\u7b51\u3002
\n\n\u5df2\u77e5\u5b58\u5728\u697c\u5c42 f
\uff0c\u6ee1\u8db3\u00a00 <= f <= n
\uff0c\u4efb\u4f55\u4ece \u9ad8\u4e8e f
\u7684\u697c\u5c42\u843d\u4e0b\u7684\u9e21\u86cb\u90fd \u4f1a\u788e \uff0c\u4ece f
\u697c\u5c42\u6216\u6bd4\u5b83\u4f4e \u7684\u697c\u5c42\u843d\u4e0b\u7684\u9e21\u86cb\u90fd \u4e0d\u4f1a\u788e \u3002
\n\n\u6bcf\u6b21\u64cd\u4f5c\uff0c\u4f60\u53ef\u4ee5\u53d6\u4e00\u679a \u6ca1\u6709\u788e \u7684\u9e21\u86cb\u5e76\u628a\u5b83\u4ece\u4efb\u4e00\u697c\u5c42 x
\u6254\u4e0b\uff08\u6ee1\u8db3\u00a01 <= x <= n
\uff09\u3002\u5982\u679c\u9e21\u86cb\u788e\u4e86\uff0c\u4f60\u5c31\u4e0d\u80fd\u518d\u6b21\u4f7f\u7528\u5b83\u3002\u5982\u679c\u67d0\u679a\u9e21\u86cb\u6254\u4e0b\u540e\u6ca1\u6709\u6454\u788e\uff0c\u5219\u53ef\u4ee5\u5728\u4e4b\u540e\u7684\u64cd\u4f5c\u4e2d \u91cd\u590d\u4f7f\u7528 \u8fd9\u679a\u9e21\u86cb\u3002
\n\n\u8bf7\u4f60\u8ba1\u7b97\u5e76\u8fd4\u56de\u8981\u786e\u5b9a f
\u786e\u5207\u7684\u503c \u7684 \u6700\u5c0f\u64cd\u4f5c\u6b21\u6570 \u662f\u591a\u5c11\uff1f
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1an = 2\n\u8f93\u51fa\uff1a2\n\u89e3\u91ca\uff1a\u6211\u4eec\u53ef\u4ee5\u5c06\u7b2c\u4e00\u679a\u9e21\u86cb\u4ece 1 \u697c\u6254\u4e0b\uff0c\u7136\u540e\u5c06\u7b2c\u4e8c\u679a\u4ece 2 \u697c\u6254\u4e0b\u3002\n\u5982\u679c\u7b2c\u4e00\u679a\u9e21\u86cb\u788e\u4e86\uff0c\u53ef\u77e5 f = 0\uff1b\n\u5982\u679c\u7b2c\u4e8c\u679a\u9e21\u86cb\u788e\u4e86\uff0c\u4f46\u7b2c\u4e00\u679a\u6ca1\u788e\uff0c\u53ef\u77e5 f = 1\uff1b\n\u5426\u5219\uff0c\u5f53\u4e24\u4e2a\u9e21\u86cb\u90fd\u6ca1\u788e\u65f6\uff0c\u53ef\u77e5 f = 2\u3002\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1an = 100\n\u8f93\u51fa\uff1a14\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n", "tags_en": ["Math", "Dynamic Programming"], "tags_cn": ["\u6570\u5b66", "\u52a8\u6001\u89c4\u5212"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n int twoEggDrop(int n) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int twoEggDrop(int n) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def twoEggDrop(self, n):\n \"\"\"\n :type n: int\n :rtype: int\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def twoEggDrop(self, n: int) -> int:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nint twoEggDrop(int n){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int TwoEggDrop(int n) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number} n\n * @return {number}\n */\nvar twoEggDrop = function(n) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer} n\n# @return {Integer}\ndef two_egg_drop(n)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func twoEggDrop(_ n: Int) -> Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func twoEggDrop(n int) int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def twoEggDrop(n: Int): Int = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun twoEggDrop(n: Int): Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn two_egg_drop(n: i32) -> i32 {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer $n\n * @return Integer\n */\n function twoEggDrop($n) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function twoEggDrop(n: number): number {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (two-egg-drop n)\n (-> exact-integer? exact-integer?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec two_egg_drop(N :: integer()) -> integer().\ntwo_egg_drop(N) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec two_egg_drop(n :: integer) :: integer\n def two_egg_drop(n) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1884](https://leetcode-cn.com/problems/egg-drop-with-2-eggs-and-n-floors)", "[\u9e21\u86cb\u6389\u843d-\u4e24\u679a\u9e21\u86cb](/solution/1800-1899/1884.Egg%20Drop%20With%202%20Eggs%20and%20N%20Floors/README.md)", "`\u6570\u5b66`,`\u52a8\u6001\u89c4\u5212`", "\u4e2d\u7b49", ""], "md_table_row_en": ["[1884](https://leetcode.com/problems/egg-drop-with-2-eggs-and-n-floors)", "[Egg Drop With 2 Eggs and N Floors](/solution/1800-1899/1884.Egg%20Drop%20With%202%20Eggs%20and%20N%20Floors/README_EN.md)", "`Math`,`Dynamic Programming`", "Medium", ""]}, {"question_id": "2030", "frontend_question_id": "1875", "paid_only": true, "paid_only_cn": true, "category": "Database", "url_cn": "https://leetcode-cn.com/problems/group-employees-of-the-same-salary", "url_en": "https://leetcode.com/problems/group-employees-of-the-same-salary", "relative_path_cn": "/solution/1800-1899/1875.Group%20Employees%20of%20the%20Same%20Salary/README.md", "relative_path_en": "/solution/1800-1899/1875.Group%20Employees%20of%20the%20Same%20Salary/README_EN.md", "title_cn": "Group Employees of the Same Salary", "title_en": "Group Employees of the Same Salary", "question_title_slug": "group-employees-of-the-same-salary", "content_en": "Table: Employees
\r\n\r\n\r\n+-------------+---------+\r\n| Column Name | Type |\r\n+-------------+---------+\r\n| employee_id | int |\r\n| name | varchar |\r\n| salary | int |\r\n+-------------+---------+\r\nemployee_id is the primary key for this table.\r\nEach row of this table indicates the employee ID, employee name, and salary.\r\n
\r\n\r\n
\r\n\r\nA company wants to divide the employees into teams such that all the members on each team have the same salary. The teams should follow these criteria:
\r\n\r\n\r\n\t- Each team should consist of at least two employees.
\r\n\t- All the employees on a team should have the same salary.
\r\n\t- All the employees of the same salary should be assigned to the same team.
\r\n\t- If the salary of an employee is unique, we do not assign this employee to any team.
\r\n\t- A team's ID is assigned based on the rank of the team's salary relative to the other teams' salaries, where the team with the lowest salary has
team_id = 1
. Note that the salaries for employees not on a team are not included in this ranking. \r\n
\r\n\r\nWrite an SQL query to get the team_id
of each employee that is in a team.
\r\n\r\nReturn the result table ordered by team_id
in ascending order. In case of a tie, order it by employee_id
in ascending order.
\r\n\r\nThe query result format is in the following example:
\r\n\r\n
\r\n\r\n\r\nEmployees table:\r\n+-------------+---------+--------+\r\n| employee_id | name | salary |\r\n+-------------+---------+--------+\r\n| 2 | Meir | 3000 |\r\n| 3 | Michael | 3000 |\r\n| 7 | Addilyn | 7400 |\r\n| 8 | Juan | 6100 |\r\n| 9 | Kannon | 7400 |\r\n+-------------+---------+--------+\r\n\r\nResult table:\r\n+-------------+---------+--------+---------+\r\n| employee_id | name | salary | team_id |\r\n+-------------+---------+--------+---------+\r\n| 2 | Meir | 3000 | 1 |\r\n| 3 | Michael | 3000 | 1 |\r\n| 7 | Addilyn | 7400 | 2 |\r\n| 9 | Kannon | 7400 | 2 |\r\n+-------------+---------+--------+---------+\r\n\r\nMeir (employee_id=2) and Michael (employee_id=3) are in the same team because they have the same salary of 3000.\r\nAddilyn (employee_id=7) and Kannon (employee_id=9) are in the same team because they have the same salary of 7400.\r\nJuan (employee_id=8) is not included in any team because their salary of 6100 is unique (i.e. no other employee has the same salary).\r\nThe team IDs are assigned as follows (based on salary ranking, lowest first):\r\n- team_id=1: Meir and Michael, salary of 3000\r\n- team_id=2: Addilyn and Kannon, salary of 7400\r\nJuan's salary of 6100 is not included in the ranking because they are not on a team.
", "content_cn": "Table: Employees
\r\n\r\n\r\n+-------------+---------+\r\n| Column Name | Type |\r\n+-------------+---------+\r\n| employee_id | int |\r\n| name | varchar |\r\n| salary | int |\r\n+-------------+---------+\r\nemployee_id is the primary key for this table.\r\nEach row of this table indicates the employee ID, employee name, and salary.\r\n
\r\n\r\n
\r\n\r\nA company wants to divide the employees into teams such that all the members on each team have the same salary. The teams should follow these criteria:
\r\n\r\n\r\n\t- Each team should consist of at least two employees.
\r\n\t- All the employees on a team should have the same salary.
\r\n\t- All the employees of the same salary should be assigned to the same team.
\r\n\t- If the salary of an employee is unique, we do not assign this employee to any team.
\r\n\t- A team's ID is assigned based on the rank of the team's salary relative to the other teams' salaries, where the team with the lowest salary has
team_id = 1
. Note that the salaries for employees not on a team are not included in this ranking. \r\n
\r\n\r\nWrite an SQL query to get the team_id
of each employee that is in a team.
\r\n\r\nReturn the result table ordered by team_id
in ascending order. In case of a tie, order it by employee_id
in ascending order.
\r\n\r\nThe query result format is in the following example:
\r\n\r\n
\r\n\r\n\r\nEmployees table:\r\n+-------------+---------+--------+\r\n| employee_id | name | salary |\r\n+-------------+---------+--------+\r\n| 2 | Meir | 3000 |\r\n| 3 | Michael | 3000 |\r\n| 7 | Addilyn | 7400 |\r\n| 8 | Juan | 6100 |\r\n| 9 | Kannon | 7400 |\r\n+-------------+---------+--------+\r\n\r\nResult table:\r\n+-------------+---------+--------+---------+\r\n| employee_id | name | salary | team_id |\r\n+-------------+---------+--------+---------+\r\n| 2 | Meir | 3000 | 1 |\r\n| 3 | Michael | 3000 | 1 |\r\n| 7 | Addilyn | 7400 | 2 |\r\n| 9 | Kannon | 7400 | 2 |\r\n+-------------+---------+--------+---------+\r\n\r\nMeir (employee_id=2) and Michael (employee_id=3) are in the same team because they have the same salary of 3000.\r\nAddilyn (employee_id=7) and Kannon (employee_id=9) are in the same team because they have the same salary of 7400.\r\nJuan (employee_id=8) is not included in any team because their salary of 6100 is unique (i.e. no other employee has the same salary).\r\nThe team IDs are assigned as follows (based on salary ranking, lowest first):\r\n- team_id=1: Meir and Michael, salary of 3000\r\n- team_id=2: Addilyn and Kannon, salary of 7400\r\nJuan's salary of 6100 is not included in the ranking because they are not on a team.
", "tags_en": ["Database"], "tags_cn": ["\u6570\u636e\u5e93"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "MySQL", "langSlug": "mysql", "code": "# Write your MySQL query statement below", "__typename": "CodeSnippetNode"}, {"lang": "MS SQL Server", "langSlug": "mssql", "code": "/* Write your T-SQL query statement below */", "__typename": "CodeSnippetNode"}, {"lang": "Oracle", "langSlug": "oraclesql", "code": "/* Write your PL/SQL query statement below */", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1875](https://leetcode-cn.com/problems/group-employees-of-the-same-salary)", "[Group Employees of the Same Salary](/solution/1800-1899/1875.Group%20Employees%20of%20the%20Same%20Salary/README.md)", "`\u6570\u636e\u5e93`", "\u4e2d\u7b49", "\ud83d\udd12"], "md_table_row_en": ["[1875](https://leetcode.com/problems/group-employees-of-the-same-salary)", "[Group Employees of the Same Salary](/solution/1800-1899/1875.Group%20Employees%20of%20the%20Same%20Salary/README_EN.md)", "`Database`", "Medium", "\ud83d\udd12"]}, {"question_id": "2029", "frontend_question_id": "1874", "paid_only": true, "paid_only_cn": true, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/minimize-product-sum-of-two-arrays", "url_en": "https://leetcode.com/problems/minimize-product-sum-of-two-arrays", "relative_path_cn": "/solution/1800-1899/1874.Minimize%20Product%20Sum%20of%20Two%20Arrays/README.md", "relative_path_en": "/solution/1800-1899/1874.Minimize%20Product%20Sum%20of%20Two%20Arrays/README_EN.md", "title_cn": "\u4e24\u4e2a\u6570\u7ec4\u7684\u6700\u5c0f\u4e58\u79ef\u548c", "title_en": "Minimize Product Sum of Two Arrays", "question_title_slug": "minimize-product-sum-of-two-arrays", "content_en": "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).
\r\n\r\n\r\n\t- For example, if
a = [1,2,3,4]
and b = [5,2,3,1]
, the product sum would be 1*5 + 2*2 + 3*3 + 4*1 = 22
. \r\n
\r\n\r\nGiven 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
.
\r\n\r\n
\r\nExample 1:
\r\n\r\n\r\nInput: nums1 = [5,3,4,2], nums2 = [4,2,2,5]\r\nOutput: 40\r\nExplanation: 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.\r\n
\r\n\r\nExample 2:
\r\n\r\n\r\nInput: nums1 = [2,1,4,5,7], nums2 = [3,2,4,8,6]\r\nOutput: 65\r\nExplanation: 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.\r\n
\r\n\r\n
\r\nConstraints:
\r\n\r\n\r\n\tn == nums1.length == nums2.length
\r\n\t1 <= n <= 105
\r\n\t1 <= nums1[i], nums2[i] <= 100
\r\n
", "content_cn": "\u7ed9\u5b9a\u4e24\u4e2a\u957f\u5ea6\u76f8\u7b49\u7684\u6570\u7ec4a
\u548cb
\uff0c\u5b83\u4eec\u7684\u4e58\u79ef\u548c\u4e3a\u6570\u7ec4\u4e2d\u6240\u6709\u7684a[i] * b[i]
\u4e4b\u548c\uff0c\u5176\u4e2d0 <= i < a.length
\u3002
\n\n\n\t- \u6bd4\u5982
a = [1,2,3,4]
\uff0cb = [5,2,3,1]
\u65f6\uff0c\u5b83\u4eec\u7684\u4e58\u79ef\u548c\u4e3a1*5 + 2*2 + 3*3 + 4*1 = 22
\n
\n\n\u73b0\u6709\u4e24\u4e2a\u957f\u5ea6\u90fd\u4e3an
\u7684\u6570\u7ec4nums1
\u548cnums2
\uff0c\u4f60\u53ef\u4ee5\u4ee5\u4efb\u610f\u987a\u5e8f\u6392\u5e8fnums1
\uff0c\u8bf7\u8fd4\u56de\u5b83\u4eec\u7684\u6700\u5c0f\u4e58\u79ef\u548c\u3002
\n\n\u793a\u4f8b 1:
\n\n\u8f93\u5165: nums1 = [5,3,4,2], nums2 = [4,2,2,5]\n\u8f93\u51fa: 40\n\u89e3\u91ca: \u5c06 num1 \u91cd\u65b0\u6392\u5217\u4e3a [3,5,4,2] \u540e\uff0c\u53ef\u7531 [3,5,4,2] \u548c [4,2,2,5] \u5f97\u5230\u6700\u5c0f\u4e58\u79ef\u548c 3*4 + 5*2 + 4*2 + 2*5 = 40\u3002\n
\n\n\u793a\u4f8b 2:
\n\n\u8f93\u5165: nums1 = [2,1,4,5,7], nums2 = [3,2,4,8,6]\n\u8f93\u51fa: 65\n\u89e3\u91ca: \u5c06 num1 \u91cd\u65b0\u6392\u5217\u4e3a [5,7,4,1,2] \u540e\uff0c\u53ef\u7531 [5,7,4,1,2] \u548c [3,2,4,8,6] \u5f97\u5230\u6700\u5c0f\u4e58\u79ef\u548c 5*3 + 7*2 + 4*4 + 1*8 + 2*6 = 65\u3002\n
\n\n\u00a0
\n\n\u63d0\u793a:
\n\n\n\tn == nums1.length == nums2.length
\n\t1 <= n <= 105
\n\t1 <= nums1[i], nums2[i] <= 100
\n
\n", "tags_en": ["Greedy", "Array", "Sorting"], "tags_cn": ["\u8d2a\u5fc3", "\u6570\u7ec4", "\u6392\u5e8f"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n int minProductSum(vector& nums1, vector& nums2) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int minProductSum(int[] nums1, int[] nums2) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def minProductSum(self, nums1, nums2):\n \"\"\"\n :type nums1: List[int]\n :type nums2: List[int]\n :rtype: int\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def minProductSum(self, nums1: List[int], nums2: List[int]) -> int:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nint minProductSum(int* nums1, int nums1Size, int* nums2, int nums2Size){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int MinProductSum(int[] nums1, int[] nums2) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[]} nums1\n * @param {number[]} nums2\n * @return {number}\n */\nvar minProductSum = function(nums1, nums2) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer[]} nums1\n# @param {Integer[]} nums2\n# @return {Integer}\ndef min_product_sum(nums1, nums2)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func minProductSum(_ nums1: [Int], _ nums2: [Int]) -> Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func minProductSum(nums1 []int, nums2 []int) int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def minProductSum(nums1: Array[Int], nums2: Array[Int]): Int = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun minProductSum(nums1: IntArray, nums2: IntArray): Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn min_product_sum(nums1: Vec, nums2: Vec) -> i32 {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer[] $nums1\n * @param Integer[] $nums2\n * @return Integer\n */\n function minProductSum($nums1, $nums2) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function minProductSum(nums1: number[], nums2: number[]): number {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (min-product-sum nums1 nums2)\n (-> (listof exact-integer?) (listof exact-integer?) exact-integer?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec min_product_sum(Nums1 :: [integer()], Nums2 :: [integer()]) -> integer().\nmin_product_sum(Nums1, Nums2) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec min_product_sum(nums1 :: [integer], nums2 :: [integer]) :: integer\n def min_product_sum(nums1, nums2) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1874](https://leetcode-cn.com/problems/minimize-product-sum-of-two-arrays)", "[\u4e24\u4e2a\u6570\u7ec4\u7684\u6700\u5c0f\u4e58\u79ef\u548c](/solution/1800-1899/1874.Minimize%20Product%20Sum%20of%20Two%20Arrays/README.md)", "`\u8d2a\u5fc3`,`\u6570\u7ec4`,`\u6392\u5e8f`", "\u4e2d\u7b49", "\ud83d\udd12"], "md_table_row_en": ["[1874](https://leetcode.com/problems/minimize-product-sum-of-two-arrays)", "[Minimize Product Sum of Two Arrays](/solution/1800-1899/1874.Minimize%20Product%20Sum%20of%20Two%20Arrays/README_EN.md)", "`Greedy`,`Array`,`Sorting`", "Medium", "\ud83d\udd12"]}, {"question_id": "2028", "frontend_question_id": "1900", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/the-earliest-and-latest-rounds-where-players-compete", "url_en": "https://leetcode.com/problems/the-earliest-and-latest-rounds-where-players-compete", "relative_path_cn": "/solution/1900-1999/1900.The%20Earliest%20and%20Latest%20Rounds%20Where%20Players%20Compete/README.md", "relative_path_en": "/solution/1900-1999/1900.The%20Earliest%20and%20Latest%20Rounds%20Where%20Players%20Compete/README_EN.md", "title_cn": "\u6700\u4f73\u8fd0\u52a8\u5458\u7684\u6bd4\u62fc\u56de\u5408", "title_en": "The Earliest and Latest Rounds Where Players Compete", "question_title_slug": "the-earliest-and-latest-rounds-where-players-compete", "content_en": "There is a tournament where n
players are participating. The players are standing in a single row and are numbered from 1
to n
based on their initial standing position (player 1
is the first player in the row, player 2
is the second player in the row, etc.).
\n\nThe tournament consists of multiple rounds (starting from round number 1
). In each round, the ith
player from the front of the row competes against the ith
player from the end of the row, and the winner advances to the next round. When the number of players is odd for the current round, the player in the middle automatically advances to the next round.
\n\n\n\t- For example, if the row consists of players
1, 2, 4, 6, 7
\n\n\t\n\t\t- Player
1
competes against player 7
. \n\t\t- Player
2
competes against player 6
. \n\t\t- Player
4
automatically advances to the next round. \n\t
\n\t \n
\n\nAfter each round is over, the winners are lined back up in the row based on the original ordering assigned to them initially (ascending order).
\n\nThe players numbered firstPlayer
and secondPlayer
are the best in the tournament. They can win against any other player before they compete against each other. If any two other players compete against each other, either of them might win, and thus you may choose the outcome of this round.
\n\nGiven the integers n
, firstPlayer
, and secondPlayer
, return an integer array containing two values, the earliest possible round number and the latest possible round number in which these two players will compete against each other, respectively.
\n\n
\nExample 1:
\n\n\nInput: n = 11, firstPlayer = 2, secondPlayer = 4\nOutput: [3,4]\nExplanation:\nOne possible scenario which leads to the earliest round number:\nFirst round: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11\nSecond round: 2, 3, 4, 5, 6, 11\nThird round: 2, 3, 4\nOne possible scenario which leads to the latest round number:\nFirst round: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11\nSecond round: 1, 2, 3, 4, 5, 6\nThird round: 1, 2, 4\nFourth round: 2, 4\n
\n\nExample 2:
\n\n\nInput: n = 5, firstPlayer = 1, secondPlayer = 5\nOutput: [1,1]\nExplanation: The players numbered 1 and 5 compete in the first round.\nThere is no way to make them compete in any other round.\n
\n\n
\nConstraints:
\n\n\n\t2 <= n <= 28
\n\t1 <= firstPlayer < secondPlayer <= n
\n
\n", "content_cn": "n
\u540d\u8fd0\u52a8\u5458\u53c2\u4e0e\u4e00\u573a\u9526\u6807\u8d5b\uff0c\u6240\u6709\u8fd0\u52a8\u5458\u7ad9\u6210\u4e00\u6392\uff0c\u5e76\u6839\u636e \u6700\u5f00\u59cb\u7684 \u7ad9\u4f4d\u4ece 1
\u5230 n
\u7f16\u53f7\uff08\u8fd0\u52a8\u5458 1
\u662f\u8fd9\u4e00\u6392\u4e2d\u7684\u7b2c\u4e00\u4e2a\u8fd0\u52a8\u5458\uff0c\u8fd0\u52a8\u5458 2
\u662f\u7b2c\u4e8c\u4e2a\u8fd0\u52a8\u5458\uff0c\u4f9d\u6b64\u7c7b\u63a8\uff09\u3002
\n\n\u9526\u6807\u8d5b\u7531\u591a\u4e2a\u56de\u5408\u7ec4\u6210\uff08\u4ece\u56de\u5408 1
\u5f00\u59cb\uff09\u3002\u6bcf\u4e00\u56de\u5408\u4e2d\uff0c\u8fd9\u4e00\u6392\u4ece\u524d\u5f80\u540e\u6570\u7684\u7b2c i
\u540d\u8fd0\u52a8\u5458\u9700\u8981\u4e0e\u4ece\u540e\u5f80\u524d\u6570\u7684\u7b2c i
\u540d\u8fd0\u52a8\u5458\u6bd4\u62fc\uff0c\u83b7\u80dc\u8005\u5c06\u4f1a\u8fdb\u5165\u4e0b\u4e00\u56de\u5408\u3002\u5982\u679c\u5f53\u524d\u56de\u5408\u4e2d\u8fd0\u52a8\u5458\u6570\u76ee\u4e3a\u5947\u6570\uff0c\u90a3\u4e48\u4e2d\u95f4\u90a3\u4f4d\u8fd0\u52a8\u5458\u5c06\u8f6e\u7a7a\u664b\u7ea7\u4e0b\u4e00\u56de\u5408\u3002
\n\n\n\t- \u4f8b\u5982\uff0c\u5f53\u524d\u56de\u5408\u4e2d\uff0c\u8fd0\u52a8\u5458
1, 2, 4, 6, 7
\u7ad9\u6210\u4e00\u6392\n\n\t\n\t\t- \u8fd0\u52a8\u5458
1
\u9700\u8981\u548c\u8fd0\u52a8\u5458 7
\u6bd4\u62fc \n\t\t- \u8fd0\u52a8\u5458
2
\u9700\u8981\u548c\u8fd0\u52a8\u5458 6
\u6bd4\u62fc \n\t\t- \u8fd0\u52a8\u5458
4
\u8f6e\u7a7a\u664b\u7ea7\u4e0b\u4e00\u56de\u5408 \n\t
\n\t \n
\n\n\u6bcf\u56de\u5408\u7ed3\u675f\u540e\uff0c\u83b7\u80dc\u8005\u5c06\u4f1a\u57fa\u4e8e\u6700\u5f00\u59cb\u5206\u914d\u7ed9\u4ed6\u4eec\u7684\u539f\u59cb\u987a\u5e8f\uff08\u5347\u5e8f\uff09\u91cd\u65b0\u6392\u6210\u4e00\u6392\u3002
\n\n\u7f16\u53f7\u4e3a firstPlayer
\u548c secondPlayer
\u7684\u8fd0\u52a8\u5458\u662f\u672c\u573a\u9526\u6807\u8d5b\u4e2d\u7684\u6700\u4f73\u8fd0\u52a8\u5458\u3002\u5728\u4ed6\u4eec\u5f00\u59cb\u6bd4\u62fc\u4e4b\u524d\uff0c\u5b8c\u5168\u53ef\u4ee5\u6218\u80dc\u4efb\u4f55\u5176\u4ed6\u8fd0\u52a8\u5458\u3002\u800c\u4efb\u610f\u4e24\u4e2a\u5176\u4ed6\u8fd0\u52a8\u5458\u8fdb\u884c\u6bd4\u62fc\u65f6\uff0c\u5176\u4e2d\u4efb\u610f\u4e00\u4e2a\u90fd\u6709\u83b7\u80dc\u7684\u53ef\u80fd\uff0c\u56e0\u6b64\u4f60\u53ef\u4ee5 \u88c1\u5b9a \u8c01\u662f\u8fd9\u4e00\u56de\u5408\u7684\u83b7\u80dc\u8005\u3002
\n\n\u7ed9\u4f60\u4e09\u4e2a\u6574\u6570 n
\u3001firstPlayer
\u548c secondPlayer
\u3002\u8fd4\u56de\u4e00\u4e2a\u7531\u4e24\u4e2a\u503c\u7ec4\u6210\u7684\u6574\u6570\u6570\u7ec4\uff0c\u5206\u522b\u8868\u793a\u4e24\u4f4d\u6700\u4f73\u8fd0\u52a8\u5458\u5728\u672c\u573a\u9526\u6807\u8d5b\u4e2d\u6bd4\u62fc\u7684 \u6700\u65e9 \u56de\u5408\u6570\u548c \u6700\u665a \u56de\u5408\u6570\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1an = 11, firstPlayer = 2, secondPlayer = 4\n\u8f93\u51fa\uff1a[3,4]\n\u89e3\u91ca\uff1a\n\u4e00\u79cd\u80fd\u591f\u4ea7\u751f\u6700\u65e9\u56de\u5408\u6570\u7684\u60c5\u666f\u662f\uff1a\n\u56de\u5408 1\uff1a1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11\n\u56de\u5408 2\uff1a2, 3, 4, 5, 6, 11\n\u56de\u5408 3\uff1a2, 3, 4\n\u4e00\u79cd\u80fd\u591f\u4ea7\u751f\u6700\u665a\u56de\u5408\u6570\u7684\u60c5\u666f\u662f\uff1a\n\u56de\u5408 1\uff1a1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11\n\u56de\u5408 2\uff1a1, 2, 3, 4, 5, 6\n\u56de\u5408 3\uff1a1, 2, 4\n\u56de\u5408 4\uff1a2, 4\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1an = 5, firstPlayer = 1, secondPlayer = 5\n\u8f93\u51fa\uff1a[1,1]\n\u89e3\u91ca\uff1a\u4e24\u540d\u6700\u4f73\u8fd0\u52a8\u5458 1 \u548c 5 \u5c06\u4f1a\u5728\u56de\u5408 1 \u8fdb\u884c\u6bd4\u62fc\u3002\n\u4e0d\u5b58\u5728\u4f7f\u4ed6\u4eec\u5728\u5176\u4ed6\u56de\u5408\u8fdb\u884c\u6bd4\u62fc\u7684\u53ef\u80fd\u3002\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\t2 <= n <= 28
\n\t1 <= firstPlayer < secondPlayer <= n
\n
\n", "tags_en": ["Memoization", "Dynamic Programming"], "tags_cn": ["\u8bb0\u5fc6\u5316\u641c\u7d22", "\u52a8\u6001\u89c4\u5212"], "difficulty_en": "Hard", "difficulty_cn": "\u56f0\u96be", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n vector earliestAndLatest(int n, int firstPlayer, int secondPlayer) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int[] earliestAndLatest(int n, int firstPlayer, int secondPlayer) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def earliestAndLatest(self, n, firstPlayer, secondPlayer):\n \"\"\"\n :type n: int\n :type firstPlayer: int\n :type secondPlayer: int\n :rtype: List[int]\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def earliestAndLatest(self, n: int, firstPlayer: int, secondPlayer: int) -> List[int]:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\n/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\nint* earliestAndLatest(int n, int firstPlayer, int secondPlayer, int* returnSize){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int[] EarliestAndLatest(int n, int firstPlayer, int secondPlayer) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number} n\n * @param {number} firstPlayer\n * @param {number} secondPlayer\n * @return {number[]}\n */\nvar earliestAndLatest = function(n, firstPlayer, secondPlayer) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer} n\n# @param {Integer} first_player\n# @param {Integer} second_player\n# @return {Integer[]}\ndef earliest_and_latest(n, first_player, second_player)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func earliestAndLatest(_ n: Int, _ firstPlayer: Int, _ secondPlayer: Int) -> [Int] {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func earliestAndLatest(n int, firstPlayer int, secondPlayer int) []int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def earliestAndLatest(n: Int, firstPlayer: Int, secondPlayer: Int): Array[Int] = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun earliestAndLatest(n: Int, firstPlayer: Int, secondPlayer: Int): IntArray {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn earliest_and_latest(n: i32, first_player: i32, second_player: i32) -> Vec {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer $n\n * @param Integer $firstPlayer\n * @param Integer $secondPlayer\n * @return Integer[]\n */\n function earliestAndLatest($n, $firstPlayer, $secondPlayer) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function earliestAndLatest(n: number, firstPlayer: number, secondPlayer: number): number[] {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (earliest-and-latest n firstPlayer secondPlayer)\n (-> exact-integer? exact-integer? exact-integer? (listof exact-integer?))\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec earliest_and_latest(N :: integer(), FirstPlayer :: integer(), SecondPlayer :: integer()) -> [integer()].\nearliest_and_latest(N, FirstPlayer, SecondPlayer) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec earliest_and_latest(n :: integer, first_player :: integer, second_player :: integer) :: [integer]\n def earliest_and_latest(n, first_player, second_player) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1900](https://leetcode-cn.com/problems/the-earliest-and-latest-rounds-where-players-compete)", "[\u6700\u4f73\u8fd0\u52a8\u5458\u7684\u6bd4\u62fc\u56de\u5408](/solution/1900-1999/1900.The%20Earliest%20and%20Latest%20Rounds%20Where%20Players%20Compete/README.md)", "`\u8bb0\u5fc6\u5316\u641c\u7d22`,`\u52a8\u6001\u89c4\u5212`", "\u56f0\u96be", ""], "md_table_row_en": ["[1900](https://leetcode.com/problems/the-earliest-and-latest-rounds-where-players-compete)", "[The Earliest and Latest Rounds Where Players Compete](/solution/1900-1999/1900.The%20Earliest%20and%20Latest%20Rounds%20Where%20Players%20Compete/README_EN.md)", "`Memoization`,`Dynamic Programming`", "Hard", ""]}, {"question_id": "2027", "frontend_question_id": "1898", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/maximum-number-of-removable-characters", "url_en": "https://leetcode.com/problems/maximum-number-of-removable-characters", "relative_path_cn": "/solution/1800-1899/1898.Maximum%20Number%20of%20Removable%20Characters/README.md", "relative_path_en": "/solution/1800-1899/1898.Maximum%20Number%20of%20Removable%20Characters/README_EN.md", "title_cn": "\u53ef\u79fb\u9664\u5b57\u7b26\u7684\u6700\u5927\u6570\u76ee", "title_en": "Maximum Number of Removable Characters", "question_title_slug": "maximum-number-of-removable-characters", "content_en": "You are given two strings s
and p
where p
is a subsequence of s
. You are also given a distinct 0-indexed integer array removable
containing a subset of indices of s
(s
is also 0-indexed).
\n\nYou want to choose an integer k
(0 <= k <= removable.length
) such that, after removing k
characters from s
using the first k
indices in removable
, p
is still a subsequence of s
. More formally, you will mark the character at s[removable[i]]
for each 0 <= i < k
, then remove all marked characters and check if p
is still a subsequence.
\n\nReturn the maximum k
you can choose such that p
is still a subsequence of s
after the removals.
\n\nA subsequence of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters.
\n\n
\nExample 1:
\n\n\nInput: s = "abcacb", p = "ab", removable = [3,1,0]\nOutput: 2\nExplanation: After removing the characters at indices 3 and 1, "abcacb" becomes "accb".\n"ab" is a subsequence of "accb".\nIf we remove the characters at indices 3, 1, and 0, "abcacb" becomes "ccb", and "ab" is no longer a subsequence.\nHence, the maximum k is 2.\n
\n\nExample 2:
\n\n\nInput: s = "abcbddddd", p = "abcd", removable = [3,2,1,4,5,6]\nOutput: 1\nExplanation: After removing the character at index 3, "abcbddddd" becomes "abcddddd".\n"abcd" is a subsequence of "abcddddd".\n
\n\nExample 3:
\n\n\nInput: s = "abcab", p = "abc", removable = [0,1,2,3,4]\nOutput: 0\nExplanation: If you remove the first index in the array removable, "abc" is no longer a subsequence.\n
\n\n
\nConstraints:
\n\n\n\t1 <= p.length <= s.length <= 105
\n\t0 <= removable.length < s.length
\n\t0 <= removable[i] < s.length
\n\tp
is a subsequence of s
. \n\ts
and p
both consist of lowercase English letters. \n\t- The elements in
removable
are distinct. \n
\n", "content_cn": "\u7ed9\u4f60\u4e24\u4e2a\u5b57\u7b26\u4e32 s
\u548c p
\uff0c\u5176\u4e2d p
\u662f s
\u7684\u4e00\u4e2a \u5b50\u5e8f\u5217 \u3002\u540c\u65f6\uff0c\u7ed9\u4f60\u4e00\u4e2a\u5143\u7d20 \u4e92\u4e0d\u76f8\u540c \u4e14\u4e0b\u6807 \u4ece 0 \u5f00\u59cb \u8ba1\u6570\u7684\u6574\u6570\u6570\u7ec4\u00a0removable
\uff0c\u8be5\u6570\u7ec4\u662f s
\u4e2d\u4e0b\u6807\u7684\u4e00\u4e2a\u5b50\u96c6\uff08s
\u7684\u4e0b\u6807\u4e5f \u4ece 0 \u5f00\u59cb \u8ba1\u6570\uff09\u3002
\n\n\u8bf7\u4f60\u627e\u51fa\u4e00\u4e2a\u6574\u6570 k
\uff080 <= k <= removable.length
\uff09\uff0c\u9009\u51fa\u00a0removable
\u4e2d\u7684 \u524d k
\u4e2a\u4e0b\u6807\uff0c\u7136\u540e\u4ece s
\u4e2d\u79fb\u9664\u8fd9\u4e9b\u4e0b\u6807\u5bf9\u5e94\u7684 k
\u4e2a\u5b57\u7b26\u3002\u6574\u6570 k
\u9700\u6ee1\u8db3\uff1a\u5728\u6267\u884c\u5b8c\u4e0a\u8ff0\u6b65\u9aa4\u540e\uff0c p
\u4ecd\u7136\u662f s
\u7684\u4e00\u4e2a \u5b50\u5e8f\u5217 \u3002\u66f4\u6b63\u5f0f\u7684\u89e3\u91ca\u662f\uff0c\u5bf9\u4e8e\u6bcf\u4e2a 0 <= i < k
\uff0c\u5148\u6807\u8bb0\u51fa\u4f4d\u4e8e s[removable[i]]
\u7684\u5b57\u7b26\uff0c\u63a5\u7740\u79fb\u9664\u6240\u6709\u6807\u8bb0\u8fc7\u7684\u5b57\u7b26\uff0c\u7136\u540e\u68c0\u67e5 p
\u662f\u5426\u4ecd\u7136\u662f s
\u7684\u4e00\u4e2a\u5b50\u5e8f\u5217\u3002
\n\n\u8fd4\u56de\u4f60\u53ef\u4ee5\u627e\u51fa\u7684 \u6700\u5927 k
\uff0c\u6ee1\u8db3\u5728\u79fb\u9664\u5b57\u7b26\u540e p
\u4ecd\u7136\u662f s
\u7684\u4e00\u4e2a\u5b50\u5e8f\u5217\u3002
\n\n\u5b57\u7b26\u4e32\u7684\u4e00\u4e2a \u5b50\u5e8f\u5217 \u662f\u4e00\u4e2a\u7531\u539f\u5b57\u7b26\u4e32\u751f\u6210\u7684\u65b0\u5b57\u7b26\u4e32\uff0c\u751f\u6210\u8fc7\u7a0b\u4e2d\u53ef\u80fd\u4f1a\u79fb\u9664\u539f\u5b57\u7b26\u4e32\u4e2d\u7684\u4e00\u4e9b\u5b57\u7b26\uff08\u4e5f\u53ef\u80fd\u4e0d\u79fb\u9664\uff09\u4f46\u4e0d\u6539\u53d8\u5269\u4f59\u5b57\u7b26\u4e4b\u95f4\u7684\u76f8\u5bf9\u987a\u5e8f\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1as = \"abcacb\", p = \"ab\", removable = [3,1,0]\n\u8f93\u51fa\uff1a2\n\u89e3\u91ca\uff1a\u5728\u79fb\u9664\u4e0b\u6807 3 \u548c 1 \u5bf9\u5e94\u7684\u5b57\u7b26\u540e\uff0c\"abcacb\" \u53d8\u6210 \"accb\" \u3002\n\"ab\" \u662f \"accb\" \u7684\u4e00\u4e2a\u5b50\u5e8f\u5217\u3002\n\u5982\u679c\u79fb\u9664\u4e0b\u6807 3\u30011 \u548c 0 \u5bf9\u5e94\u7684\u5b57\u7b26\u540e\uff0c\"abcacb\" \u53d8\u6210 \"ccb\" \uff0c\u90a3\u4e48 \"ab\" \u5c31\u4e0d\u518d\u662f s \u7684\u4e00\u4e2a\u5b50\u5e8f\u5217\u3002\n\u56e0\u6b64\uff0c\u6700\u5927\u7684 k \u662f 2 \u3002\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1as = \"abcbddddd\", p = \"abcd\", removable = [3,2,1,4,5,6]\n\u8f93\u51fa\uff1a1\n\u89e3\u91ca\uff1a\u5728\u79fb\u9664\u4e0b\u6807 3 \u5bf9\u5e94\u7684\u5b57\u7b26\u540e\uff0c\"abcbddddd\" \u53d8\u6210 \"abcddddd\" \u3002\n\"abcd\" \u662f \"abcddddd\" \u7684\u4e00\u4e2a\u5b50\u5e8f\u5217\u3002\n
\n\n\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1as = \"abcab\", p = \"abc\", removable = [0,1,2,3,4]\n\u8f93\u51fa\uff1a0\n\u89e3\u91ca\uff1a\u5982\u679c\u79fb\u9664\u6570\u7ec4 removable \u7684\u7b2c\u4e00\u4e2a\u4e0b\u6807\uff0c\"abc\" \u5c31\u4e0d\u518d\u662f s \u7684\u4e00\u4e2a\u5b50\u5e8f\u5217\u3002\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\t1 <= p.length <= s.length <= 105
\n\t0 <= removable.length < s.length
\n\t0 <= removable[i] < s.length
\n\tp
\u662f s
\u7684\u4e00\u4e2a \u5b50\u5b57\u7b26\u4e32 \n\ts
\u548c p
\u90fd\u7531\u5c0f\u5199\u82f1\u6587\u5b57\u6bcd\u7ec4\u6210 \n\tremovable
\u4e2d\u7684\u5143\u7d20 \u4e92\u4e0d\u76f8\u540c \n
\n", "tags_en": ["Array", "String", "Binary Search"], "tags_cn": ["\u6570\u7ec4", "\u5b57\u7b26\u4e32", "\u4e8c\u5206\u67e5\u627e"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n int maximumRemovals(string s, string p, vector& removable) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int maximumRemovals(String s, String p, int[] removable) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def maximumRemovals(self, s, p, removable):\n \"\"\"\n :type s: str\n :type p: str\n :type removable: List[int]\n :rtype: int\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def maximumRemovals(self, s: str, p: str, removable: List[int]) -> int:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nint maximumRemovals(char * s, char * p, int* removable, int removableSize){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int MaximumRemovals(string s, string p, int[] removable) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {string} s\n * @param {string} p\n * @param {number[]} removable\n * @return {number}\n */\nvar maximumRemovals = function(s, p, removable) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {String} s\n# @param {String} p\n# @param {Integer[]} removable\n# @return {Integer}\ndef maximum_removals(s, p, removable)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func maximumRemovals(_ s: String, _ p: String, _ removable: [Int]) -> Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func maximumRemovals(s string, p string, removable []int) int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def maximumRemovals(s: String, p: String, removable: Array[Int]): Int = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun maximumRemovals(s: String, p: String, removable: IntArray): Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn maximum_removals(s: String, p: String, removable: Vec) -> i32 {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param String $s\n * @param String $p\n * @param Integer[] $removable\n * @return Integer\n */\n function maximumRemovals($s, $p, $removable) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function maximumRemovals(s: string, p: string, removable: number[]): number {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (maximum-removals s p removable)\n (-> string? string? (listof exact-integer?) exact-integer?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec maximum_removals(S :: unicode:unicode_binary(), P :: unicode:unicode_binary(), Removable :: [integer()]) -> integer().\nmaximum_removals(S, P, Removable) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec maximum_removals(s :: String.t, p :: String.t, removable :: [integer]) :: integer\n def maximum_removals(s, p, removable) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1898](https://leetcode-cn.com/problems/maximum-number-of-removable-characters)", "[\u53ef\u79fb\u9664\u5b57\u7b26\u7684\u6700\u5927\u6570\u76ee](/solution/1800-1899/1898.Maximum%20Number%20of%20Removable%20Characters/README.md)", "`\u6570\u7ec4`,`\u5b57\u7b26\u4e32`,`\u4e8c\u5206\u67e5\u627e`", "\u4e2d\u7b49", ""], "md_table_row_en": ["[1898](https://leetcode.com/problems/maximum-number-of-removable-characters)", "[Maximum Number of Removable Characters](/solution/1800-1899/1898.Maximum%20Number%20of%20Removable%20Characters/README_EN.md)", "`Array`,`String`,`Binary Search`", "Medium", ""]}, {"question_id": "2026", "frontend_question_id": "1899", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/merge-triplets-to-form-target-triplet", "url_en": "https://leetcode.com/problems/merge-triplets-to-form-target-triplet", "relative_path_cn": "/solution/1800-1899/1899.Merge%20Triplets%20to%20Form%20Target%20Triplet/README.md", "relative_path_en": "/solution/1800-1899/1899.Merge%20Triplets%20to%20Form%20Target%20Triplet/README_EN.md", "title_cn": "\u5408\u5e76\u82e5\u5e72\u4e09\u5143\u7ec4\u4ee5\u5f62\u6210\u76ee\u6807\u4e09\u5143\u7ec4", "title_en": "Merge Triplets to Form Target Triplet", "question_title_slug": "merge-triplets-to-form-target-triplet", "content_en": "A triplet is an array of three integers. You are given a 2D integer array triplets
, where triplets[i] = [ai, bi, ci]
describes the ith
triplet. You are also given an integer array target = [x, y, z]
that describes the triplet you want to obtain.
\n\nTo obtain target
, you may apply the following operation on triplets
any number of times (possibly zero):
\n\n\n\t- Choose two indices (0-indexed)
i
and j
(i != j
) and update triplets[j]
to become [max(ai, aj), max(bi, bj), max(ci, cj)]
.\n\n\t\n\t\t- For example, if
triplets[i] = [2, 5, 3]
and triplets[j] = [1, 7, 5]
, triplets[j]
will be updated to [max(2, 1), max(5, 7), max(3, 5)] = [2, 7, 5]
. \n\t
\n\t \n
\n\nReturn true
if it is possible to obtain the target
triplet [x, y, z]
as an element of triplets
, or false
otherwise.
\n\n
\nExample 1:
\n\n\nInput: triplets = [[2,5,3],[1,8,4],[1,7,5]], target = [2,7,5]\nOutput: true\nExplanation: Perform the following operations:\n- Choose the first and last triplets [[2,5,3],[1,8,4],[1,7,5]]. Update the last triplet to be [max(2,1), max(5,7), max(3,5)] = [2,7,5]. triplets = [[2,5,3],[1,8,4],[2,7,5]]\nThe target triplet [2,7,5] is now an element of triplets.\n
\n\nExample 2:
\n\n\nInput: triplets = [[1,3,4],[2,5,8]], target = [2,5,8]\nOutput: true\nExplanation: The target triplet [2,5,8] is already an element of triplets.\n
\n\nExample 3:
\n\n\nInput: triplets = [[2,5,3],[2,3,4],[1,2,5],[5,2,3]], target = [5,5,5]\nOutput: true\nExplanation: Perform the following operations:\n- Choose the first and third triplets [[2,5,3],[2,3,4],[1,2,5],[5,2,3]]. Update the third triplet to be [max(2,1), max(5,2), max(3,5)] = [2,5,5]. triplets = [[2,5,3],[2,3,4],[2,5,5],[5,2,3]].\n- Choose the third and fourth triplets [[2,5,3],[2,3,4],[2,5,5],[5,2,3]]. Update the fourth triplet to be [max(2,5), max(5,2), max(5,3)] = [5,5,5]. triplets = [[2,5,3],[2,3,4],[2,5,5],[5,5,5]].\nThe target triplet [5,5,5] is now an element of triplets.\n
\n\nExample 4:
\n\n\nInput: triplets = [[3,4,5],[4,5,6]], target = [3,2,5]\nOutput: false\nExplanation: It is impossible to have [3,2,5] as an element because there is no 2 in any of the triplets.\n
\n\n
\nConstraints:
\n\n\n\t1 <= triplets.length <= 105
\n\ttriplets[i].length == target.length == 3
\n\t1 <= ai, bi, ci, x, y, z <= 1000
\n
\n", "content_cn": "\u4e09\u5143\u7ec4 \u662f\u4e00\u4e2a\u7531\u4e09\u4e2a\u6574\u6570\u7ec4\u6210\u7684\u6570\u7ec4\u3002\u7ed9\u4f60\u4e00\u4e2a\u4e8c\u7ef4\u6574\u6570\u6570\u7ec4 triplets
\uff0c\u5176\u4e2d triplets[i] = [ai, bi, ci]
\u8868\u793a\u7b2c i
\u4e2a \u4e09\u5143\u7ec4 \u3002\u540c\u65f6\uff0c\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 target = [x, y, z]
\uff0c\u8868\u793a\u4f60\u60f3\u8981\u5f97\u5230\u7684 \u4e09\u5143\u7ec4 \u3002
\n\n\u4e3a\u4e86\u5f97\u5230 target
\uff0c\u4f60\u9700\u8981\u5bf9 triplets
\u6267\u884c\u4e0b\u9762\u7684\u64cd\u4f5c \u4efb\u610f\u6b21\uff08\u53ef\u80fd \u96f6 \u6b21\uff09\uff1a
\n\n\n\t- \u9009\u51fa\u4e24\u4e2a\u4e0b\u6807\uff08\u4e0b\u6807 \u4ece 0 \u5f00\u59cb \u8ba1\u6570\uff09
i
\u548c j
\uff08i != j
\uff09\uff0c\u5e76 \u66f4\u65b0 triplets[j]
\u4e3a [max(ai, aj), max(bi, bj), max(ci, cj)]
\u3002\n\n\t\n\t\t- \u4f8b\u5982\uff0c
triplets[i] = [2, 5, 3]
\u4e14 triplets[j] = [1, 7, 5]
\uff0ctriplets[j]
\u5c06\u4f1a\u66f4\u65b0\u4e3a [max(2, 1), max(5, 7), max(3, 5)] = [2, 7, 5]
\u3002 \n\t
\n\t \n
\n\n\u5982\u679c\u901a\u8fc7\u4ee5\u4e0a\u64cd\u4f5c\u6211\u4eec\u53ef\u4ee5\u4f7f\u5f97\u76ee\u6807 \u4e09\u5143\u7ec4\u00a0target
\u00a0\u6210\u4e3a\u00a0triplets
\u7684\u4e00\u4e2a \u5143\u7d20\u00a0\uff0c\u5219\u8fd4\u56de true
\uff1b\u5426\u5219\uff0c\u8fd4\u56de false
\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1atriplets = [[2,5,3],[1,8,4],[1,7,5]], target = [2,7,5]\n\u8f93\u51fa\uff1atrue\n\u89e3\u91ca\uff1a\u6267\u884c\u4e0b\u8ff0\u64cd\u4f5c\uff1a\n- \u9009\u62e9\u7b2c\u4e00\u4e2a\u548c\u6700\u540e\u4e00\u4e2a\u4e09\u5143\u7ec4 [[2,5,3],[1,8,4],[1,7,5]] \u3002\u66f4\u65b0\u6700\u540e\u4e00\u4e2a\u4e09\u5143\u7ec4\u4e3a [max(2,1), max(5,7), max(3,5)] = [2,7,5] \u3002triplets = [[2,5,3],[1,8,4],[2,7,5]]\n\u76ee\u6807\u4e09\u5143\u7ec4 [2,7,5] \u73b0\u5728\u662f triplets \u7684\u4e00\u4e2a\u5143\u7d20\u3002\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1atriplets = [[1,3,4],[2,5,8]], target = [2,5,8]\n\u8f93\u51fa\uff1atrue\n\u89e3\u91ca\uff1a\u76ee\u6807\u4e09\u5143\u7ec4 [2,5,8] \u5df2\u7ecf\u662f triplets \u7684\u4e00\u4e2a\u5143\u7d20\u3002\n
\n\n\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1atriplets = [[2,5,3],[2,3,4],[1,2,5],[5,2,3]], target = [5,5,5]\n\u8f93\u51fa\uff1atrue\n\u89e3\u91ca\uff1a\u6267\u884c\u4e0b\u8ff0\u64cd\u4f5c\uff1a\n- \u9009\u62e9\u7b2c\u4e00\u4e2a\u548c\u7b2c\u4e09\u4e2a\u4e09\u5143\u7ec4 [[2,5,3],[2,3,4],[1,2,5],[5,2,3]] \u3002\u66f4\u65b0\u7b2c\u4e09\u4e2a\u4e09\u5143\u7ec4\u4e3a [max(2,1), max(5,2), max(3,5)] = [2,5,5] \u3002triplets = [[2,5,3],[2,3,4],[2,5,5],[5,2,3]] \u3002\n- \u9009\u62e9\u7b2c\u4e09\u4e2a\u548c\u7b2c\u56db\u4e2a\u4e09\u5143\u7ec4 [[2,5,3],[2,3,4],[2,5,5],[5,2,3]] \u3002\u66f4\u65b0\u7b2c\u56db\u4e2a\u4e09\u5143\u7ec4\u4e3a [max(2,5), max(5,2), max(5,3)] = [5,5,5] \u3002triplets = [[2,5,3],[2,3,4],[2,5,5],[5,5,5]] \u3002\n\u76ee\u6807\u4e09\u5143\u7ec4 [5,5,5] \u73b0\u5728\u662f triplets \u7684\u4e00\u4e2a\u5143\u7d20\u3002\n
\n\n\u793a\u4f8b 4\uff1a
\n\n\n\u8f93\u5165\uff1atriplets = [[3,4,5],[4,5,6]], target = [3,2,5]\n\u8f93\u51fa\uff1afalse\n\u89e3\u91ca\uff1a\u65e0\u6cd5\u5f97\u5230 [3,2,5] \uff0c\u56e0\u4e3a triplets \u4e0d\u542b 2 \u3002\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\t1 <= triplets.length <= 105
\n\ttriplets[i].length == target.length == 3
\n\t1 <= ai, bi, ci, x, y, z <= 1000
\n
\n", "tags_en": ["Greedy", "Array"], "tags_cn": ["\u8d2a\u5fc3", "\u6570\u7ec4"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n bool mergeTriplets(vector>& triplets, vector& target) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public boolean mergeTriplets(int[][] triplets, int[] target) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def mergeTriplets(self, triplets, target):\n \"\"\"\n :type triplets: List[List[int]]\n :type target: List[int]\n :rtype: bool\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nbool mergeTriplets(int** triplets, int tripletsSize, int* tripletsColSize, int* target, int targetSize){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public bool MergeTriplets(int[][] triplets, int[] target) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[][]} triplets\n * @param {number[]} target\n * @return {boolean}\n */\nvar mergeTriplets = function(triplets, target) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer[][]} triplets\n# @param {Integer[]} target\n# @return {Boolean}\ndef merge_triplets(triplets, target)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func mergeTriplets(_ triplets: [[Int]], _ target: [Int]) -> Bool {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func mergeTriplets(triplets [][]int, target []int) bool {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def mergeTriplets(triplets: Array[Array[Int]], target: Array[Int]): Boolean = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun mergeTriplets(triplets: Array, target: IntArray): Boolean {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn merge_triplets(triplets: Vec>, target: Vec) -> bool {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer[][] $triplets\n * @param Integer[] $target\n * @return Boolean\n */\n function mergeTriplets($triplets, $target) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function mergeTriplets(triplets: number[][], target: number[]): boolean {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (merge-triplets triplets target)\n (-> (listof (listof exact-integer?)) (listof exact-integer?) boolean?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec merge_triplets(Triplets :: [[integer()]], Target :: [integer()]) -> boolean().\nmerge_triplets(Triplets, Target) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec merge_triplets(triplets :: [[integer]], target :: [integer]) :: boolean\n def merge_triplets(triplets, target) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1899](https://leetcode-cn.com/problems/merge-triplets-to-form-target-triplet)", "[\u5408\u5e76\u82e5\u5e72\u4e09\u5143\u7ec4\u4ee5\u5f62\u6210\u76ee\u6807\u4e09\u5143\u7ec4](/solution/1800-1899/1899.Merge%20Triplets%20to%20Form%20Target%20Triplet/README.md)", "`\u8d2a\u5fc3`,`\u6570\u7ec4`", "\u4e2d\u7b49", ""], "md_table_row_en": ["[1899](https://leetcode.com/problems/merge-triplets-to-form-target-triplet)", "[Merge Triplets to Form Target Triplet](/solution/1800-1899/1899.Merge%20Triplets%20to%20Form%20Target%20Triplet/README_EN.md)", "`Greedy`,`Array`", "Medium", ""]}, {"question_id": "2025", "frontend_question_id": "1897", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/redistribute-characters-to-make-all-strings-equal", "url_en": "https://leetcode.com/problems/redistribute-characters-to-make-all-strings-equal", "relative_path_cn": "/solution/1800-1899/1897.Redistribute%20Characters%20to%20Make%20All%20Strings%20Equal/README.md", "relative_path_en": "/solution/1800-1899/1897.Redistribute%20Characters%20to%20Make%20All%20Strings%20Equal/README_EN.md", "title_cn": "\u91cd\u65b0\u5206\u914d\u5b57\u7b26\u4f7f\u6240\u6709\u5b57\u7b26\u4e32\u90fd\u76f8\u7b49", "title_en": "Redistribute Characters to Make All Strings Equal", "question_title_slug": "redistribute-characters-to-make-all-strings-equal", "content_en": "You are given an array of strings words
(0-indexed).
\n\nIn one operation, pick two distinct indices i
and j
, where words[i]
is a non-empty string, and move any character from words[i]
to any position in words[j]
.
\n\nReturn true
if you can make every string in words
equal using any number of operations, and false
otherwise.
\n\n
\nExample 1:
\n\n\nInput: words = ["abc","aabc","bc"]\nOutput: true\nExplanation: Move the first 'a' in words[1] to the front of words[2],\nto make
words[1]
= "abc" and words[2] = "abc".\nAll the strings are now equal to "abc", so return true
.\n
\n\nExample 2:
\n\n\nInput: words = ["ab","a"]\nOutput: false\nExplanation: It is impossible to make all the strings equal using the operation.\n
\n\n
\nConstraints:
\n\n\n\t1 <= words.length <= 100
\n\t1 <= words[i].length <= 100
\n\twords[i]
consists of lowercase English letters. \n
\n", "content_cn": "\u7ed9\u4f60\u4e00\u4e2a\u5b57\u7b26\u4e32\u6570\u7ec4 words
\uff08\u4e0b\u6807 \u4ece 0 \u5f00\u59cb \u8ba1\u6570\uff09\u3002
\n\n\u5728\u4e00\u6b65\u64cd\u4f5c\u4e2d\uff0c\u9700\u5148\u9009\u51fa\u4e24\u4e2a \u4e0d\u540c \u4e0b\u6807 i
\u548c j
\uff0c\u5176\u4e2d words[i]
\u662f\u4e00\u4e2a\u975e\u7a7a\u5b57\u7b26\u4e32\uff0c\u63a5\u7740\u5c06 words[i]
\u4e2d\u7684 \u4efb\u4e00 \u5b57\u7b26\u79fb\u52a8\u5230 words[j]
\u4e2d\u7684 \u4efb\u4e00 \u4f4d\u7f6e\u4e0a\u3002
\n\n\u5982\u679c\u6267\u884c\u4efb\u610f\u6b65\u64cd\u4f5c\u53ef\u4ee5\u4f7f words
\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u4e32\u90fd\u76f8\u7b49\uff0c\u8fd4\u56de true
\uff1b\u5426\u5219\uff0c\u8fd4\u56de false
\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1awords = [\"abc\",\"aabc\",\"bc\"]\n\u8f93\u51fa\uff1atrue\n\u89e3\u91ca\uff1a\u5c06 words[1] \u4e2d\u7684\u7b2c\u4e00\u4e2a
'a' \u79fb\u52a8\u5230 words[2] \u7684\u6700\u524d\u9762\u3002\n\u4f7f
words[1]
= \"abc\" \u4e14 words[2] = \"abc\" \u3002\n\u6240\u6709\u5b57\u7b26\u4e32\u90fd\u7b49\u4e8e \"abc\" \uff0c\u6240\u4ee5\u8fd4\u56de true
\u3002\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1awords = [\"ab\",\"a\"]\n\u8f93\u51fa\uff1afalse\n\u89e3\u91ca\uff1a\u6267\u884c\u64cd\u4f5c\u65e0\u6cd5\u4f7f\u6240\u6709\u5b57\u7b26\u4e32\u90fd\u76f8\u7b49\u3002\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\t1 <= words.length <= 100
\n\t1 <= words[i].length <= 100
\n\twords[i]
\u7531\u5c0f\u5199\u82f1\u6587\u5b57\u6bcd\u7ec4\u6210 \n
\n", "tags_en": ["Hash Table", "String", "Counting"], "tags_cn": ["\u54c8\u5e0c\u8868", "\u5b57\u7b26\u4e32", "\u8ba1\u6570"], "difficulty_en": "Easy", "difficulty_cn": "\u7b80\u5355", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n bool makeEqual(vector& words) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public boolean makeEqual(String[] words) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def makeEqual(self, words):\n \"\"\"\n :type words: List[str]\n :rtype: bool\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def makeEqual(self, words: List[str]) -> bool:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nbool makeEqual(char ** words, int wordsSize){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public bool MakeEqual(string[] words) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {string[]} words\n * @return {boolean}\n */\nvar makeEqual = function(words) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {String[]} words\n# @return {Boolean}\ndef make_equal(words)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func makeEqual(_ words: [String]) -> Bool {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func makeEqual(words []string) bool {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def makeEqual(words: Array[String]): Boolean = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun makeEqual(words: Array): Boolean {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn make_equal(words: Vec) -> bool {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param String[] $words\n * @return Boolean\n */\n function makeEqual($words) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function makeEqual(words: string[]): boolean {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (make-equal words)\n (-> (listof string?) boolean?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec make_equal(Words :: [unicode:unicode_binary()]) -> boolean().\nmake_equal(Words) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec make_equal(words :: [String.t]) :: boolean\n def make_equal(words) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1897](https://leetcode-cn.com/problems/redistribute-characters-to-make-all-strings-equal)", "[\u91cd\u65b0\u5206\u914d\u5b57\u7b26\u4f7f\u6240\u6709\u5b57\u7b26\u4e32\u90fd\u76f8\u7b49](/solution/1800-1899/1897.Redistribute%20Characters%20to%20Make%20All%20Strings%20Equal/README.md)", "`\u54c8\u5e0c\u8868`,`\u5b57\u7b26\u4e32`,`\u8ba1\u6570`", "\u7b80\u5355", ""], "md_table_row_en": ["[1897](https://leetcode.com/problems/redistribute-characters-to-make-all-strings-equal)", "[Redistribute Characters to Make All Strings Equal](/solution/1800-1899/1897.Redistribute%20Characters%20to%20Make%20All%20Strings%20Equal/README_EN.md)", "`Hash Table`,`String`,`Counting`", "Easy", ""]}, {"question_id": "2024", "frontend_question_id": "1873", "paid_only": true, "paid_only_cn": true, "category": "Database", "url_cn": "https://leetcode-cn.com/problems/calculate-special-bonus", "url_en": "https://leetcode.com/problems/calculate-special-bonus", "relative_path_cn": "/solution/1800-1899/1873.Calculate%20Special%20Bonus/README.md", "relative_path_en": "/solution/1800-1899/1873.Calculate%20Special%20Bonus/README_EN.md", "title_cn": "\u8ba1\u7b97\u7279\u6b8a\u5956\u91d1", "title_en": "Calculate Special Bonus", "question_title_slug": "calculate-special-bonus", "content_en": "Table: Employees
\r\n\r\n\r\n+-------------+---------+\r\n| Column Name | Type |\r\n+-------------+---------+\r\n| employee_id | int |\r\n| name | varchar |\r\n| salary | int |\r\n+-------------+---------+\r\nemployee_id is the primary key for this table.\r\nEach row of this table indicates the employee ID, employee name, and salary.\r\n
\r\n\r\n
\r\n\r\nWrite an SQL query to calculate the bonus of each employee. The bonus of an employee is 100%
of their salary if the ID of the employee is an odd number and the employee name does not start with the character 'M'
. The bonus of an employee is 0
otherwise.
\r\n\r\nReturn the result table ordered by employee_id
.
\r\n\r\nThe query result format is in the following example:
\r\n\r\n
\r\n\r\n\r\nEmployees table:\r\n+-------------+---------+--------+\r\n| employee_id | name | salary |\r\n+-------------+---------+--------+\r\n| 2 | Meir | 3000 |\r\n| 3 | Michael | 3800 |\r\n| 7 | Addilyn | 7400 |\r\n| 8 | Juan | 6100 |\r\n| 9 | Kannon | 7700 |\r\n+-------------+---------+--------+\r\n\r\nResult table:\r\n+-------------+-------+\r\n| employee_id | bonus |\r\n+-------------+-------+\r\n| 2 | 0 |\r\n| 3 | 0 |\r\n| 7 | 7400 |\r\n| 8 | 0 |\r\n| 9 | 7700 |\r\n+-------------+-------+\r\n\r\nThe employees with IDs 2 and 8 get 0 bonus because they have an even employee_id.\r\nThe employee with ID 3 gets 0 bonus because their name starts with 'M'.\r\nThe rest of the employees get a 100% bonus.\r\n
", "content_cn": "\u8868: Employees
\n\n+-------------+---------+\n| \u5217\u540d | \u7c7b\u578b |\n+-------------+---------+\n| employee_id | int |\n| name | varchar |\n| salary | int |\n+-------------+---------+\nemployee_id \u662f\u8fd9\u4e2a\u8868\u7684\u4e3b\u952e\u3002\n\u6b64\u8868\u7684\u6bcf\u4e00\u884c\u7ed9\u51fa\u4e86\u96c7\u5458id \uff0c\u540d\u5b57\u548c\u85aa\u6c34\u3002\n
\n\n\u00a0
\n\n\u5199\u51fa\u4e00\u4e2aSQL \u67e5\u8be2\u8bed\u53e5\uff0c\u8ba1\u7b97\u6bcf\u4e2a\u96c7\u5458\u7684\u5956\u91d1\u3002\u5982\u679c\u4e00\u4e2a\u96c7\u5458\u7684id\u662f\u5947\u6570\u5e76\u4e14\u4ed6\u7684\u540d\u5b57\u4e0d\u662f\u4ee5'M'\u5f00\u5934\uff0c\u90a3\u4e48\u4ed6\u7684\u5956\u91d1\u662f\u4ed6\u5de5\u8d44\u7684100%\uff0c\u5426\u5219\u5956\u91d1\u4e3a0\u3002
\n\nReturn the result table ordered by employee_id
.
\n\n\u8fd4\u56de\u7684\u7ed3\u679c\u96c6\u8bf7\u6309\u7167employee_id
\u6392\u5e8f\u3002
\n\n\u67e5\u8be2\u7ed3\u679c\u683c\u5f0f\u5982\u4e0b\u9762\u7684\u4f8b\u5b50\u6240\u793a\uff1a
\n\n\u00a0
\n\nEmployees \u8868:\n+-------------+---------+--------+\n| employee_id | name | salary |\n+-------------+---------+--------+\n| 2 | Meir | 3000 |\n| 3 | Michael | 3800 |\n| 7 | Addilyn | 7400 |\n| 8 | Juan | 6100 |\n| 9 | Kannon | 7700 |\n+-------------+---------+--------+\n\n\u7ed3\u679c\u8868:\n+-------------+-------+\n| employee_id | bonus |\n+-------------+-------+\n| 2 | 0 |\n| 3 | 0 |\n| 7 | 7400 |\n| 8 | 0 |\n| 9 | 7700 |\n+-------------+-------+\n\n\u56e0\u4e3a\u96c7\u5458id\u662f\u5076\u6570\uff0c\u6240\u4ee5\u96c7\u5458id \u662f2\u548c8\u7684\u4e24\u4e2a\u96c7\u5458\u5f97\u5230\u7684\u5956\u91d1\u662f0\u3002\n\u96c7\u5458id\u4e3a3\u7684\u56e0\u4e3a\u4ed6\u7684\u540d\u5b57\u4ee5'M'\u5f00\u5934\uff0c\u6240\u4ee5\uff0c\u5956\u91d1\u662f0\u3002\n\u5176\u4ed6\u7684\u96c7\u5458\u5f97\u5230\u4e86\u767e\u5206\u4e4b\u767e\u7684\u5956\u91d1\u3002\n
\n", "tags_en": ["Database"], "tags_cn": ["\u6570\u636e\u5e93"], "difficulty_en": "Easy", "difficulty_cn": "\u7b80\u5355", "code_snippets": [{"lang": "MySQL", "langSlug": "mysql", "code": "# Write your MySQL query statement below", "__typename": "CodeSnippetNode"}, {"lang": "MS SQL Server", "langSlug": "mssql", "code": "/* Write your T-SQL query statement below */", "__typename": "CodeSnippetNode"}, {"lang": "Oracle", "langSlug": "oraclesql", "code": "/* Write your PL/SQL query statement below */", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1873](https://leetcode-cn.com/problems/calculate-special-bonus)", "[\u8ba1\u7b97\u7279\u6b8a\u5956\u91d1](/solution/1800-1899/1873.Calculate%20Special%20Bonus/README.md)", "`\u6570\u636e\u5e93`", "\u7b80\u5355", "\ud83d\udd12"], "md_table_row_en": ["[1873](https://leetcode.com/problems/calculate-special-bonus)", "[Calculate Special Bonus](/solution/1800-1899/1873.Calculate%20Special%20Bonus/README_EN.md)", "`Database`", "Easy", "\ud83d\udd12"]}, {"question_id": "2023", "frontend_question_id": "1912", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/design-movie-rental-system", "url_en": "https://leetcode.com/problems/design-movie-rental-system", "relative_path_cn": "/solution/1900-1999/1912.Design%20Movie%20Rental%20System/README.md", "relative_path_en": "/solution/1900-1999/1912.Design%20Movie%20Rental%20System/README_EN.md", "title_cn": "\u8bbe\u8ba1\u7535\u5f71\u79df\u501f\u7cfb\u7edf", "title_en": "Design Movie Rental System", "question_title_slug": "design-movie-rental-system", "content_en": "You have a movie renting company consisting of n
shops. You want to implement a renting system that supports searching for, booking, and returning movies. The system should also support generating a report of the currently rented movies.
\r\n\r\nEach movie is given as a 2D integer array entries
where entries[i] = [shopi, moviei, pricei]
indicates that there is a copy of movie moviei
at shop shopi
with a rental price of pricei
. Each shop carries at most one copy of a movie moviei
.
\r\n\r\nThe system should support the following functions:
\r\n\r\n\r\n\t- Search: Finds the cheapest 5 shops that have an unrented copy of a given movie. The shops should be sorted by price in ascending order, and in case of a tie, the one with the smaller
shopi
should appear first. If there are less than 5 matching shops, then all of them should be returned. If no shop has an unrented copy, then an empty list should be returned. \r\n\t- Rent: Rents an unrented copy of a given movie from a given shop.
\r\n\t- Drop: Drops off a previously rented copy of a given movie at a given shop.
\r\n\t- Report: Returns the cheapest 5 rented movies (possibly of the same movie ID) as a 2D list
res
where res[j] = [shopj, moviej]
describes that the jth
cheapest rented movie moviej
was rented from the shop shopj
. The movies in res
should be sorted by price in ascending order, and in case of a tie, the one with the smaller shopj
should appear first, and if there is still tie, the one with the smaller moviej
should appear first. If there are fewer than 5 rented movies, then all of them should be returned. If no movies are currently being rented, then an empty list should be returned. \r\n
\r\n\r\nImplement the MovieRentingSystem
class:
\r\n\r\n\r\n\tMovieRentingSystem(int n, int[][] entries)
Initializes the MovieRentingSystem
object with n
shops and the movies in entries
. \r\n\tList<Integer> search(int movie)
Returns a list of shops that have an unrented copy of the given movie
as described above. \r\n\tvoid rent(int shop, int movie)
Rents the given movie
from the given shop
. \r\n\tvoid drop(int shop, int movie)
Drops off a previously rented movie
at the given shop
. \r\n\tList<List<Integer>> report()
Returns a list of cheapest rented movies as described above. \r\n
\r\n\r\nNote: The test cases will be generated such that rent
will only be called if the shop has an unrented copy of the movie, and drop
will only be called if the shop had previously rented out the movie.
\r\n\r\n
\r\nExample 1:
\r\n\r\n\r\nInput\r\n["MovieRentingSystem", "search", "rent", "rent", "report", "drop", "search"]\r\n[[3, [[0, 1, 5], [0, 2, 6], [0, 3, 7], [1, 1, 4], [1, 2, 7], [2, 1, 5]]], [1], [0, 1], [1, 2], [], [1, 2], [2]]\r\nOutput\r\n[null, [1, 0, 2], null, null, [[0, 1], [1, 2]], null, [0, 1]]\r\n\r\nExplanation\r\nMovieRentingSystem movieRentingSystem = new MovieRentingSystem(3, [[0, 1, 5], [0, 2, 6], [0, 3, 7], [1, 1, 4], [1, 2, 7], [2, 1, 5]]);\r\nmovieRentingSystem.search(1); // return [1, 0, 2], Movies of ID 1 are unrented at shops 1, 0, and 2. Shop 1 is cheapest; shop 0 and 2 are the same price, so order by shop number.\r\nmovieRentingSystem.rent(0, 1); // Rent movie 1 from shop 0. Unrented movies at shop 0 are now [2,3].\r\nmovieRentingSystem.rent(1, 2); // Rent movie 2 from shop 1. Unrented movies at shop 1 are now [1].\r\nmovieRentingSystem.report(); // return [[0, 1], [1, 2]]. Movie 1 from shop 0 is cheapest, followed by movie 2 from shop 1.\r\nmovieRentingSystem.drop(1, 2); // Drop off movie 2 at shop 1. Unrented movies at shop 1 are now [1,2].\r\nmovieRentingSystem.search(2); // return [0, 1]. Movies of ID 2 are unrented at shops 0 and 1. Shop 0 is cheapest, followed by shop 1.\r\n
\r\n\r\n
\r\nConstraints:
\r\n\r\n\r\n\t1 <= n <= 3 * 105
\r\n\t1 <= entries.length <= 105
\r\n\t0 <= shopi < n
\r\n\t1 <= moviei, pricei <= 104
\r\n\t- Each shop carries at most one copy of a movie
moviei
. \r\n\t- At most
105
calls in total will be made to search
, rent
, drop
and report
. \r\n
", "content_cn": "\u4f60\u6709\u4e00\u4e2a\u7535\u5f71\u79df\u501f\u516c\u53f8\u548c n
\u00a0\u4e2a\u7535\u5f71\u5546\u5e97\u3002\u4f60\u60f3\u8981\u5b9e\u73b0\u4e00\u4e2a\u7535\u5f71\u79df\u501f\u7cfb\u7edf\uff0c\u5b83\u652f\u6301\u67e5\u8be2\u3001\u9884\u8ba2\u548c\u8fd4\u8fd8\u7535\u5f71\u7684\u64cd\u4f5c\u3002\u540c\u65f6\u7cfb\u7edf\u8fd8\u80fd\u751f\u6210\u4e00\u4efd\u5f53\u524d\u88ab\u501f\u51fa\u7535\u5f71\u7684\u62a5\u544a\u3002
\n\n\u6240\u6709\u7535\u5f71\u7528\u4e8c\u7ef4\u6574\u6570\u6570\u7ec4\u00a0entries
\u00a0\u8868\u793a\uff0c\u5176\u4e2d\u00a0entries[i] = [shopi, moviei, pricei]
\u00a0\u8868\u793a\u5546\u5e97 shopi
\u00a0\u6709\u4e00\u4efd\u7535\u5f71\u00a0moviei
\u00a0\u7684\u62f7\u8d1d\uff0c\u79df\u501f\u4ef7\u683c\u4e3a\u00a0pricei
\u00a0\u3002\u6bcf\u4e2a\u5546\u5e97\u6709\u00a0\u81f3\u591a\u4e00\u4efd\u00a0\u7f16\u53f7\u4e3a\u00a0moviei
\u00a0\u7684\u7535\u5f71\u62f7\u8d1d\u3002
\n\n\u7cfb\u7edf\u9700\u8981\u652f\u6301\u4ee5\u4e0b\u64cd\u4f5c\uff1a
\n\n\n\t- Search\uff1a\u627e\u5230\u62e5\u6709\u6307\u5b9a\u7535\u5f71\u4e14 \u672a\u501f\u51fa\u00a0\u7684\u5546\u5e97\u4e2d\u00a0\u6700\u4fbf\u5b9c\u7684 5 \u4e2a\u00a0\u3002\u5546\u5e97\u9700\u8981\u6309\u7167\u00a0\u4ef7\u683c\u00a0\u5347\u5e8f\u6392\u5e8f\uff0c\u5982\u679c\u4ef7\u683c\u76f8\u540c\uff0c\u5219\u00a0
shopi
\u00a0\u8f83\u5c0f\u00a0\u7684\u5546\u5e97\u6392\u5728\u524d\u9762\u3002\u5982\u679c\u67e5\u8be2\u7ed3\u679c\u5c11\u4e8e 5 \u4e2a\u5546\u5e97\uff0c\u5219\u5c06\u5b83\u4eec\u5168\u90e8\u8fd4\u56de\u3002\u5982\u679c\u67e5\u8be2\u7ed3\u679c\u6ca1\u6709\u4efb\u4f55\u5546\u5e97\uff0c\u5219\u8fd4\u56de\u7a7a\u5217\u8868\u3002 \n\t- Rent\uff1a\u4ece\u6307\u5b9a\u5546\u5e97\u501f\u51fa\u6307\u5b9a\u7535\u5f71\uff0c\u9898\u76ee\u4fdd\u8bc1\u6307\u5b9a\u7535\u5f71\u5728\u6307\u5b9a\u5546\u5e97 \u672a\u501f\u51fa\u00a0\u3002
\n\t- Drop\uff1a\u5728\u6307\u5b9a\u5546\u5e97\u8fd4\u8fd8 \u4e4b\u524d\u5df2\u501f\u51fa\u00a0\u7684\u6307\u5b9a\u7535\u5f71\u3002
\n\t- Report\uff1a\u8fd4\u56de \u6700\u4fbf\u5b9c\u7684 5 \u90e8\u5df2\u501f\u51fa\u7535\u5f71\u00a0\uff08\u53ef\u80fd\u6709\u91cd\u590d\u7684\u7535\u5f71 ID\uff09\uff0c\u5c06\u7ed3\u679c\u7528\u4e8c\u7ef4\u5217\u8868\u00a0
res
\u00a0\u8fd4\u56de\uff0c\u5176\u4e2d res[j] = [shopj, moviej]
\u00a0\u8868\u793a\u7b2c\u00a0j
\u00a0\u4fbf\u5b9c\u7684\u5df2\u501f\u51fa\u7535\u5f71\u662f\u4ece\u5546\u5e97\u00a0shopj
\u00a0\u501f\u51fa\u7684\u7535\u5f71\u00a0moviej
\u00a0\u3002res
\u00a0\u4e2d\u7684\u7535\u5f71\u9700\u8981\u6309 \u4ef7\u683c\u00a0\u5347\u5e8f\u6392\u5e8f\uff1b\u5982\u679c\u4ef7\u683c\u76f8\u540c\uff0c\u5219\u00a0shopj
\u8f83\u5c0f\u00a0\u7684\u6392\u5728\u524d\u9762\uff1b\u5982\u679c\u4ecd\u7136\u76f8\u540c\uff0c\u5219 moviej
\u8f83\u5c0f \u7684\u6392\u5728\u524d\u9762\u3002\u5982\u679c\u5f53\u524d\u501f\u51fa\u7684\u7535\u5f71\u5c0f\u4e8e 5 \u90e8\uff0c\u5219\u5c06\u5b83\u4eec\u5168\u90e8\u8fd4\u56de\u3002\u5982\u679c\u5f53\u524d\u6ca1\u6709\u501f\u51fa\u7535\u5f71\uff0c\u5219\u8fd4\u56de\u4e00\u4e2a\u7a7a\u7684\u5217\u8868\u3002 \n
\n\n\u8bf7\u4f60\u5b9e\u73b0\u00a0MovieRentingSystem
\u00a0\u7c7b\uff1a
\n\n\n\tMovieRentingSystem(int n, int[][] entries)
\u00a0\u5c06\u00a0MovieRentingSystem
\u00a0\u5bf9\u8c61\u7528\u00a0n
\u00a0\u4e2a\u5546\u5e97\u548c\u00a0entries
\u00a0\u8868\u793a\u7684\u7535\u5f71\u5217\u8868\u521d\u59cb\u5316\u3002 \n\tList<Integer> search(int movie)
\u5982\u4e0a\u6240\u8ff0\uff0c\u8fd4\u56de \u672a\u501f\u51fa\u00a0\u6307\u5b9a movie
\u00a0\u7684\u5546\u5e97\u5217\u8868\u3002 \n\tvoid rent(int shop, int movie)
\u00a0\u4ece\u6307\u5b9a\u5546\u5e97 shop
\u00a0\u501f\u51fa\u6307\u5b9a\u7535\u5f71\u00a0movie
\u00a0\u3002 \n\tvoid drop(int shop, int movie)
\u00a0\u5728\u6307\u5b9a\u5546\u5e97 shop
\u00a0\u8fd4\u8fd8\u4e4b\u524d\u501f\u51fa\u7684\u7535\u5f71\u00a0movie
\u00a0\u3002 \n\tList<List<Integer>> report()
\u5982\u4e0a\u6240\u8ff0\uff0c\u8fd4\u56de\u6700\u4fbf\u5b9c\u7684 \u5df2\u501f\u51fa\u00a0\u7535\u5f71\u5217\u8868\u3002 \n
\n\n\u6ce8\u610f\uff1a\u6d4b\u8bd5\u6570\u636e\u4fdd\u8bc1\u00a0rent
\u00a0\u64cd\u4f5c\u4e2d\u6307\u5b9a\u5546\u5e97\u62e5\u6709 \u672a\u501f\u51fa \u7684\u6307\u5b9a\u7535\u5f71\uff0c\u4e14\u00a0drop
\u00a0\u64cd\u4f5c\u6307\u5b9a\u7684\u5546\u5e97 \u4e4b\u524d\u5df2\u501f\u51fa\u00a0\u6307\u5b9a\u7535\u5f71\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1a\n[\"MovieRentingSystem\", \"search\", \"rent\", \"rent\", \"report\", \"drop\", \"search\"]\n[[3, [[0, 1, 5], [0, 2, 6], [0, 3, 7], [1, 1, 4], [1, 2, 7], [2, 1, 5]]], [1], [0, 1], [1, 2], [], [1, 2], [2]]\n\u8f93\u51fa\uff1a\n[null, [1, 0, 2], null, null, [[0, 1], [1, 2]], null, [0, 1]]\n\n\u89e3\u91ca\uff1a\nMovieRentingSystem movieRentingSystem = new MovieRentingSystem(3, [[0, 1, 5], [0, 2, 6], [0, 3, 7], [1, 1, 4], [1, 2, 7], [2, 1, 5]]);\nmovieRentingSystem.search(1); // \u8fd4\u56de [1, 0, 2] \uff0c\u5546\u5e97 1\uff0c0 \u548c 2 \u6709\u672a\u501f\u51fa\u7684 ID \u4e3a 1 \u7684\u7535\u5f71\u3002\u5546\u5e97 1 \u6700\u4fbf\u5b9c\uff0c\u5546\u5e97 0 \u548c 2 \u4ef7\u683c\u76f8\u540c\uff0c\u6240\u4ee5\u6309\u5546\u5e97\u7f16\u53f7\u6392\u5e8f\u3002\nmovieRentingSystem.rent(0, 1); // \u4ece\u5546\u5e97 0 \u501f\u51fa\u7535\u5f71 1 \u3002\u73b0\u5728\u5546\u5e97 0 \u672a\u501f\u51fa\u7535\u5f71\u7f16\u53f7\u4e3a [2,3] \u3002\nmovieRentingSystem.rent(1, 2); // \u4ece\u5546\u5e97 1 \u501f\u51fa\u7535\u5f71 2 \u3002\u73b0\u5728\u5546\u5e97 1 \u672a\u501f\u51fa\u7684\u7535\u5f71\u7f16\u53f7\u4e3a [1] \u3002\nmovieRentingSystem.report(); // \u8fd4\u56de [[0, 1], [1, 2]] \u3002\u5546\u5e97 0 \u501f\u51fa\u7684\u7535\u5f71 1 \u6700\u4fbf\u5b9c\uff0c\u7136\u540e\u662f\u5546\u5e97 1 \u501f\u51fa\u7684\u7535\u5f71 2 \u3002\nmovieRentingSystem.drop(1, 2); // \u5728\u5546\u5e97 1 \u8fd4\u8fd8\u7535\u5f71 2 \u3002\u73b0\u5728\u5546\u5e97 1 \u672a\u501f\u51fa\u7684\u7535\u5f71\u7f16\u53f7\u4e3a [1,2] \u3002\nmovieRentingSystem.search(2); // \u8fd4\u56de [0, 1] \u3002\u5546\u5e97 0 \u548c 1 \u6709\u672a\u501f\u51fa\u7684 ID \u4e3a 2 \u7684\u7535\u5f71\u3002\u5546\u5e97 0 \u6700\u4fbf\u5b9c\uff0c\u7136\u540e\u662f\u5546\u5e97 1 \u3002\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\t1 <= n <= 3 * 105
\n\t1 <= entries.length <= 105
\n\t0 <= shopi < n
\n\t1 <= moviei, pricei <= 104
\n\t- \u6bcf\u4e2a\u5546\u5e97 \u81f3\u591a\u00a0\u6709\u4e00\u4efd\u7535\u5f71\u00a0
moviei
\u00a0\u7684\u62f7\u8d1d\u3002 \n\tsearch
\uff0crent
\uff0cdrop
\u548c\u00a0report
\u00a0\u7684\u8c03\u7528\u00a0\u603b\u5171\u00a0\u4e0d\u8d85\u8fc7\u00a0105
\u00a0\u6b21\u3002 \n
\n", "tags_en": ["Design", "Array", "Hash Table", "Ordered Set", "Heap (Priority Queue)"], "tags_cn": ["\u8bbe\u8ba1", "\u6570\u7ec4", "\u54c8\u5e0c\u8868", "\u6709\u5e8f\u96c6\u5408", "\u5806\uff08\u4f18\u5148\u961f\u5217\uff09"], "difficulty_en": "Hard", "difficulty_cn": "\u56f0\u96be", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class MovieRentingSystem {\npublic:\n MovieRentingSystem(int n, vector>& entries) {\n \n }\n \n vector search(int movie) {\n \n }\n \n void rent(int shop, int movie) {\n \n }\n \n void drop(int shop, int movie) {\n \n }\n \n vector> report() {\n \n }\n};\n\n/**\n * Your MovieRentingSystem object will be instantiated and called as such:\n * MovieRentingSystem* obj = new MovieRentingSystem(n, entries);\n * vector param_1 = obj->search(movie);\n * obj->rent(shop,movie);\n * obj->drop(shop,movie);\n * vector> param_4 = obj->report();\n */", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class MovieRentingSystem {\n\n public MovieRentingSystem(int n, int[][] entries) {\n\n }\n \n public List search(int movie) {\n\n }\n \n public void rent(int shop, int movie) {\n\n }\n \n public void drop(int shop, int movie) {\n\n }\n \n public List> report() {\n\n }\n}\n\n/**\n * Your MovieRentingSystem object will be instantiated and called as such:\n * MovieRentingSystem obj = new MovieRentingSystem(n, entries);\n * List param_1 = obj.search(movie);\n * obj.rent(shop,movie);\n * obj.drop(shop,movie);\n * List> param_4 = obj.report();\n */", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class MovieRentingSystem(object):\n\n def __init__(self, n, entries):\n \"\"\"\n :type n: int\n :type entries: List[List[int]]\n \"\"\"\n\n\n def search(self, movie):\n \"\"\"\n :type movie: int\n :rtype: List[int]\n \"\"\"\n\n\n def rent(self, shop, movie):\n \"\"\"\n :type shop: int\n :type movie: int\n :rtype: None\n \"\"\"\n\n\n def drop(self, shop, movie):\n \"\"\"\n :type shop: int\n :type movie: int\n :rtype: None\n \"\"\"\n\n\n def report(self):\n \"\"\"\n :rtype: List[List[int]]\n \"\"\"\n\n\n\n# Your MovieRentingSystem object will be instantiated and called as such:\n# obj = MovieRentingSystem(n, entries)\n# param_1 = obj.search(movie)\n# obj.rent(shop,movie)\n# obj.drop(shop,movie)\n# param_4 = obj.report()", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class MovieRentingSystem:\n\n def __init__(self, n: int, entries: List[List[int]]):\n\n\n def search(self, movie: int) -> List[int]:\n\n\n def rent(self, shop: int, movie: int) -> None:\n\n\n def drop(self, shop: int, movie: int) -> None:\n\n\n def report(self) -> List[List[int]]:\n\n\n\n# Your MovieRentingSystem object will be instantiated and called as such:\n# obj = MovieRentingSystem(n, entries)\n# param_1 = obj.search(movie)\n# obj.rent(shop,movie)\n# obj.drop(shop,movie)\n# param_4 = obj.report()", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "typedef struct {\n \n} MovieRentingSystem;\n\n\nMovieRentingSystem* movieRentingSystemCreate(int n, int** entries, int entriesSize, int* entriesColSize) {\n \n}\n\nint* movieRentingSystemSearch(MovieRentingSystem* obj, int movie, int* retSize) {\n \n}\n\nvoid movieRentingSystemRent(MovieRentingSystem* obj, int shop, int movie) {\n \n}\n\nvoid movieRentingSystemDrop(MovieRentingSystem* obj, int shop, int movie) {\n \n}\n\nint** movieRentingSystemReport(MovieRentingSystem* obj, int* retSize, int** retColSize) {\n \n}\n\nvoid movieRentingSystemFree(MovieRentingSystem* obj) {\n \n}\n\n/**\n * Your MovieRentingSystem struct will be instantiated and called as such:\n * MovieRentingSystem* obj = movieRentingSystemCreate(n, entries, entriesSize, entriesColSize);\n * int* param_1 = movieRentingSystemSearch(obj, movie, retSize);\n \n * movieRentingSystemRent(obj, shop, movie);\n \n * movieRentingSystemDrop(obj, shop, movie);\n \n * int** param_4 = movieRentingSystemReport(obj, retSize, retColSize);\n \n * movieRentingSystemFree(obj);\n*/", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class MovieRentingSystem {\n\n public MovieRentingSystem(int n, int[][] entries) {\n \n }\n \n public IList Search(int movie) {\n \n }\n \n public void Rent(int shop, int movie) {\n \n }\n \n public void Drop(int shop, int movie) {\n \n }\n \n public IList> Report() {\n \n }\n}\n\n/**\n * Your MovieRentingSystem object will be instantiated and called as such:\n * MovieRentingSystem obj = new MovieRentingSystem(n, entries);\n * IList param_1 = obj.Search(movie);\n * obj.Rent(shop,movie);\n * obj.Drop(shop,movie);\n * IList> param_4 = obj.Report();\n */", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number} n\n * @param {number[][]} entries\n */\nvar MovieRentingSystem = function(n, entries) {\n\n};\n\n/** \n * @param {number} movie\n * @return {number[]}\n */\nMovieRentingSystem.prototype.search = function(movie) {\n\n};\n\n/** \n * @param {number} shop \n * @param {number} movie\n * @return {void}\n */\nMovieRentingSystem.prototype.rent = function(shop, movie) {\n\n};\n\n/** \n * @param {number} shop \n * @param {number} movie\n * @return {void}\n */\nMovieRentingSystem.prototype.drop = function(shop, movie) {\n\n};\n\n/**\n * @return {number[][]}\n */\nMovieRentingSystem.prototype.report = function() {\n\n};\n\n/**\n * Your MovieRentingSystem object will be instantiated and called as such:\n * var obj = new MovieRentingSystem(n, entries)\n * var param_1 = obj.search(movie)\n * obj.rent(shop,movie)\n * obj.drop(shop,movie)\n * var param_4 = obj.report()\n */", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "class MovieRentingSystem\n\n=begin\n :type n: Integer\n :type entries: Integer[][]\n=end\n def initialize(n, entries)\n\n end\n\n\n=begin\n :type movie: Integer\n :rtype: Integer[]\n=end\n def search(movie)\n\n end\n\n\n=begin\n :type shop: Integer\n :type movie: Integer\n :rtype: Void\n=end\n def rent(shop, movie)\n\n end\n\n\n=begin\n :type shop: Integer\n :type movie: Integer\n :rtype: Void\n=end\n def drop(shop, movie)\n\n end\n\n\n=begin\n :rtype: Integer[][]\n=end\n def report()\n\n end\n\n\nend\n\n# Your MovieRentingSystem object will be instantiated and called as such:\n# obj = MovieRentingSystem.new(n, entries)\n# param_1 = obj.search(movie)\n# obj.rent(shop, movie)\n# obj.drop(shop, movie)\n# param_4 = obj.report()", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class MovieRentingSystem {\n\n init(_ n: Int, _ entries: [[Int]]) {\n \n }\n \n func search(_ movie: Int) -> [Int] {\n \n }\n \n func rent(_ shop: Int, _ movie: Int) {\n \n }\n \n func drop(_ shop: Int, _ movie: Int) {\n \n }\n \n func report() -> [[Int]] {\n \n }\n}\n\n/**\n * Your MovieRentingSystem object will be instantiated and called as such:\n * let obj = MovieRentingSystem(n, entries)\n * let ret_1: [Int] = obj.search(movie)\n * obj.rent(shop, movie)\n * obj.drop(shop, movie)\n * let ret_4: [[Int]] = obj.report()\n */", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "type MovieRentingSystem struct {\n\n}\n\n\nfunc Constructor(n int, entries [][]int) MovieRentingSystem {\n\n}\n\n\nfunc (this *MovieRentingSystem) Search(movie int) []int {\n\n}\n\n\nfunc (this *MovieRentingSystem) Rent(shop int, movie int) {\n\n}\n\n\nfunc (this *MovieRentingSystem) Drop(shop int, movie int) {\n\n}\n\n\nfunc (this *MovieRentingSystem) Report() [][]int {\n\n}\n\n\n/**\n * Your MovieRentingSystem object will be instantiated and called as such:\n * obj := Constructor(n, entries);\n * param_1 := obj.Search(movie);\n * obj.Rent(shop,movie);\n * obj.Drop(shop,movie);\n * param_4 := obj.Report();\n */", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "class MovieRentingSystem(_n: Int, _entries: Array[Array[Int]]) {\n\n def search(movie: Int): List[Int] = {\n\n }\n\n def rent(shop: Int, movie: Int) {\n\n }\n\n def drop(shop: Int, movie: Int) {\n\n }\n\n def report(): List[List[Int]] = {\n\n }\n\n}\n\n/**\n * Your MovieRentingSystem object will be instantiated and called as such:\n * var obj = new MovieRentingSystem(n, entries)\n * var param_1 = obj.search(movie)\n * obj.rent(shop,movie)\n * obj.drop(shop,movie)\n * var param_4 = obj.report()\n */", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class MovieRentingSystem(n: Int, entries: Array) {\n\n fun search(movie: Int): List {\n\n }\n\n fun rent(shop: Int, movie: Int) {\n\n }\n\n fun drop(shop: Int, movie: Int) {\n\n }\n\n fun report(): List> {\n\n }\n\n}\n\n/**\n * Your MovieRentingSystem object will be instantiated and called as such:\n * var obj = MovieRentingSystem(n, entries)\n * var param_1 = obj.search(movie)\n * obj.rent(shop,movie)\n * obj.drop(shop,movie)\n * var param_4 = obj.report()\n */", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "struct MovieRentingSystem {\n\n}\n\n\n/**\n * `&self` means the method takes an immutable reference.\n * If you need a mutable reference, change it to `&mut self` instead.\n */\nimpl MovieRentingSystem {\n\n fn new(n: i32, entries: Vec>) -> Self {\n\n }\n \n fn search(&self, movie: i32) -> Vec {\n\n }\n \n fn rent(&self, shop: i32, movie: i32) {\n\n }\n \n fn drop(&self, shop: i32, movie: i32) {\n\n }\n \n fn report(&self) -> Vec> {\n\n }\n}\n\n/**\n * Your MovieRentingSystem object will be instantiated and called as such:\n * let obj = MovieRentingSystem::new(n, entries);\n * let ret_1: Vec = obj.search(movie);\n * obj.rent(shop, movie);\n * obj.drop(shop, movie);\n * let ret_4: Vec> = obj.report();\n */", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class MovieRentingSystem {\n /**\n * @param Integer $n\n * @param Integer[][] $entries\n */\n function __construct($n, $entries) {\n\n }\n\n /**\n * @param Integer $movie\n * @return Integer[]\n */\n function search($movie) {\n\n }\n\n /**\n * @param Integer $shop\n * @param Integer $movie\n * @return NULL\n */\n function rent($shop, $movie) {\n\n }\n\n /**\n * @param Integer $shop\n * @param Integer $movie\n * @return NULL\n */\n function drop($shop, $movie) {\n\n }\n\n /**\n * @return Integer[][]\n */\n function report() {\n\n }\n}\n\n/**\n * Your MovieRentingSystem object will be instantiated and called as such:\n * $obj = MovieRentingSystem($n, $entries);\n * $ret_1 = $obj->search($movie);\n * $obj->rent($shop, $movie);\n * $obj->drop($shop, $movie);\n * $ret_4 = $obj->report();\n */", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "class MovieRentingSystem {\n constructor(n: number, entries: number[][]) {\n\n }\n\n search(movie: number): number[] {\n\n }\n\n rent(shop: number, movie: number): void {\n\n }\n\n drop(shop: number, movie: number): void {\n\n }\n\n report(): number[][] {\n\n }\n}\n\n/**\n * Your MovieRentingSystem object will be instantiated and called as such:\n * var obj = new MovieRentingSystem(n, entries)\n * var param_1 = obj.search(movie)\n * obj.rent(shop,movie)\n * obj.drop(shop,movie)\n * var param_4 = obj.report()\n */", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define movie-renting-system%\n (class object%\n (super-new)\n\n ; n : exact-integer?\n\n ; entries : (listof (listof exact-integer?))\n (init-field\n n\n entries)\n \n ; search : exact-integer? -> (listof exact-integer?)\n (define/public (search movie)\n\n )\n ; rent : exact-integer? exact-integer? -> void?\n (define/public (rent shop movie)\n\n )\n ; drop : exact-integer? exact-integer? -> void?\n (define/public (drop shop movie)\n\n )\n ; report : -> (listof (listof exact-integer?))\n (define/public (report)\n\n )))\n\n;; Your movie-renting-system% object will be instantiated and called as such:\n;; (define obj (new movie-renting-system% [n n] [entries entries]))\n;; (define param_1 (send obj search movie))\n;; (send obj rent shop movie)\n;; (send obj drop shop movie)\n;; (define param_4 (send obj report))", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec movie_renting_system_init_(N :: integer(), Entries :: [[integer()]]) -> any().\nmovie_renting_system_init_(N, Entries) ->\n .\n\n-spec movie_renting_system_search(Movie :: integer()) -> [integer()].\nmovie_renting_system_search(Movie) ->\n .\n\n-spec movie_renting_system_rent(Shop :: integer(), Movie :: integer()) -> any().\nmovie_renting_system_rent(Shop, Movie) ->\n .\n\n-spec movie_renting_system_drop(Shop :: integer(), Movie :: integer()) -> any().\nmovie_renting_system_drop(Shop, Movie) ->\n .\n\n-spec movie_renting_system_report() -> [[integer()]].\nmovie_renting_system_report() ->\n .\n\n\n%% Your functions will be called as such:\n%% movie_renting_system_init_(N, Entries),\n%% Param_1 = movie_renting_system_search(Movie),\n%% movie_renting_system_rent(Shop, Movie),\n%% movie_renting_system_drop(Shop, Movie),\n%% Param_4 = movie_renting_system_report(),\n\n%% movie_renting_system_init_ will be called before every test case, in which you can do some necessary initializations.", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule MovieRentingSystem do\n @spec init_(n :: integer, entries :: [[integer]]) :: any\n def init_(n, entries) do\n\n end\n\n @spec search(movie :: integer) :: [integer]\n def search(movie) do\n\n end\n\n @spec rent(shop :: integer, movie :: integer) :: any\n def rent(shop, movie) do\n\n end\n\n @spec drop(shop :: integer, movie :: integer) :: any\n def drop(shop, movie) do\n\n end\n\n @spec report() :: [[integer]]\n def report() do\n\n end\nend\n\n# Your functions will be called as such:\n# MovieRentingSystem.init_(n, entries)\n# param_1 = MovieRentingSystem.search(movie)\n# MovieRentingSystem.rent(shop, movie)\n# MovieRentingSystem.drop(shop, movie)\n# param_4 = MovieRentingSystem.report()\n\n# MovieRentingSystem.init_ will be called before every test case, in which you can do some necessary initializations.", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1912](https://leetcode-cn.com/problems/design-movie-rental-system)", "[\u8bbe\u8ba1\u7535\u5f71\u79df\u501f\u7cfb\u7edf](/solution/1900-1999/1912.Design%20Movie%20Rental%20System/README.md)", "`\u8bbe\u8ba1`,`\u6570\u7ec4`,`\u54c8\u5e0c\u8868`,`\u6709\u5e8f\u96c6\u5408`,`\u5806\uff08\u4f18\u5148\u961f\u5217\uff09`", "\u56f0\u96be", ""], "md_table_row_en": ["[1912](https://leetcode.com/problems/design-movie-rental-system)", "[Design Movie Rental System](/solution/1900-1999/1912.Design%20Movie%20Rental%20System/README_EN.md)", "`Design`,`Array`,`Hash Table`,`Ordered Set`,`Heap (Priority Queue)`", "Hard", ""]}, {"question_id": "2022", "frontend_question_id": "1911", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/maximum-alternating-subsequence-sum", "url_en": "https://leetcode.com/problems/maximum-alternating-subsequence-sum", "relative_path_cn": "/solution/1900-1999/1911.Maximum%20Alternating%20Subsequence%20Sum/README.md", "relative_path_en": "/solution/1900-1999/1911.Maximum%20Alternating%20Subsequence%20Sum/README_EN.md", "title_cn": "\u6700\u5927\u5b50\u5e8f\u5217\u4ea4\u66ff\u548c", "title_en": "Maximum Alternating Subsequence Sum", "question_title_slug": "maximum-alternating-subsequence-sum", "content_en": "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.
\r\n\r\n\r\n\t- For example, the alternating sum of
[4,2,5,3]
is (4 + 5) - (2 + 3) = 4
. \r\n
\r\n\r\nGiven an array nums
, return the maximum alternating sum of any subsequence of nums
(after reindexing the elements of the subsequence).
\r\n\r\n\r\n\r\nA 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.
\r\n\r\n
\r\nExample 1:
\r\n\r\n\r\nInput: nums = [4,2,5,3]\r\nOutput: 7\r\nExplanation: It is optimal to choose the subsequence [4,2,5] with alternating sum (4 + 5) - 2 = 7.\r\n
\r\n\r\nExample 2:
\r\n\r\n\r\nInput: nums = [5,6,7,8]\r\nOutput: 8\r\nExplanation: It is optimal to choose the subsequence [8] with alternating sum 8.\r\n
\r\n\r\nExample 3:
\r\n\r\n\r\nInput: nums = [6,2,1,2,4,5]\r\nOutput: 10\r\nExplanation: It is optimal to choose the subsequence [6,1,5] with alternating sum (6 + 5) - 1 = 10.\r\n
\r\n\r\n
\r\nConstraints:
\r\n\r\n\r\n\t1 <= nums.length <= 105
\r\n\t1 <= nums[i] <= 105
\r\n
", "content_cn": "\u4e00\u4e2a\u4e0b\u6807\u4ece 0\u00a0\u5f00\u59cb\u7684\u6570\u7ec4\u7684 \u4ea4\u66ff\u548c\u00a0\u5b9a\u4e49\u4e3a \u5076\u6570\u00a0\u4e0b\u6807\u5904\u5143\u7d20\u4e4b \u548c\u00a0\u51cf\u53bb \u5947\u6570\u00a0\u4e0b\u6807\u5904\u5143\u7d20\u4e4b \u548c\u00a0\u3002
\n\n\n\t- \u6bd4\u65b9\u8bf4\uff0c\u6570\u7ec4\u00a0
[4,2,5,3]
\u00a0\u7684\u4ea4\u66ff\u548c\u4e3a\u00a0(4 + 5) - (2 + 3) = 4
\u00a0\u3002 \n
\n\n\u7ed9\u4f60\u4e00\u4e2a\u6570\u7ec4\u00a0nums
\u00a0\uff0c\u8bf7\u4f60\u8fd4\u56de\u00a0nums
\u00a0\u4e2d\u4efb\u610f\u5b50\u5e8f\u5217\u7684\u00a0\u6700\u5927\u4ea4\u66ff\u548c\u00a0\uff08\u5b50\u5e8f\u5217\u7684\u4e0b\u6807 \u91cd\u65b0\u00a0\u4ece 0 \u5f00\u59cb\u7f16\u53f7\uff09\u3002
\n\n\n\n\u4e00\u4e2a\u6570\u7ec4\u7684 \u5b50\u5e8f\u5217\u00a0\u662f\u4ece\u539f\u6570\u7ec4\u4e2d\u5220\u9664\u4e00\u4e9b\u5143\u7d20\u540e\uff08\u4e5f\u53ef\u80fd\u4e00\u4e2a\u4e5f\u4e0d\u5220\u9664\uff09\u5269\u4f59\u5143\u7d20\u4e0d\u6539\u53d8\u987a\u5e8f\u7ec4\u6210\u7684\u6570\u7ec4\u3002\u6bd4\u65b9\u8bf4\uff0c[2,7,4]
\u00a0\u662f\u00a0[4,2,3,7,2,1,4]
\u00a0\u7684\u4e00\u4e2a\u5b50\u5e8f\u5217\uff08\u52a0\u7c97\u5143\u7d20\uff09\uff0c\u4f46\u662f\u00a0[2,4,2]
\u4e0d\u662f\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1anums = [4,2,5,3]\n\u8f93\u51fa\uff1a7\n\u89e3\u91ca\uff1a\u6700\u4f18\u5b50\u5e8f\u5217\u4e3a [4,2,5] \uff0c\u4ea4\u66ff\u548c\u4e3a (4 + 5) - 2 = 7 \u3002\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1anums = [5,6,7,8]\n\u8f93\u51fa\uff1a8\n\u89e3\u91ca\uff1a\u6700\u4f18\u5b50\u5e8f\u5217\u4e3a [8] \uff0c\u4ea4\u66ff\u548c\u4e3a 8 \u3002\n
\n\n\u793a\u4f8b 3\uff1a
\n\n\u8f93\u5165\uff1anums = [6,2,1,2,4,5]\n\u8f93\u51fa\uff1a10\n\u89e3\u91ca\uff1a\u6700\u4f18\u5b50\u5e8f\u5217\u4e3a [6,1,5] \uff0c\u4ea4\u66ff\u548c\u4e3a (6 + 5) - 1 = 10 \u3002\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\t1 <= nums.length <= 105
\n\t1 <= nums[i] <= 105
\n
\n", "tags_en": ["Array", "Dynamic Programming"], "tags_cn": ["\u6570\u7ec4", "\u52a8\u6001\u89c4\u5212"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n long long maxAlternatingSum(vector& nums) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public long maxAlternatingSum(int[] nums) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def maxAlternatingSum(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def maxAlternatingSum(self, nums: List[int]) -> int:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "long long maxAlternatingSum(int* nums, int numsSize){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public long MaxAlternatingSum(int[] nums) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[]} nums\n * @return {number}\n */\nvar maxAlternatingSum = function(nums) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer[]} nums\n# @return {Integer}\ndef max_alternating_sum(nums)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func maxAlternatingSum(_ nums: [Int]) -> Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func maxAlternatingSum(nums []int) int64 {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def maxAlternatingSum(nums: Array[Int]): Long = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun maxAlternatingSum(nums: IntArray): Long {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn max_alternating_sum(nums: Vec) -> i64 {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @return Integer\n */\n function maxAlternatingSum($nums) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function maxAlternatingSum(nums: number[]): number {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (max-alternating-sum nums)\n (-> (listof exact-integer?) exact-integer?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec max_alternating_sum(Nums :: [integer()]) -> integer().\nmax_alternating_sum(Nums) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec max_alternating_sum(nums :: [integer]) :: integer\n def max_alternating_sum(nums) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1911](https://leetcode-cn.com/problems/maximum-alternating-subsequence-sum)", "[\u6700\u5927\u5b50\u5e8f\u5217\u4ea4\u66ff\u548c](/solution/1900-1999/1911.Maximum%20Alternating%20Subsequence%20Sum/README.md)", "`\u6570\u7ec4`,`\u52a8\u6001\u89c4\u5212`", "\u4e2d\u7b49", ""], "md_table_row_en": ["[1911](https://leetcode.com/problems/maximum-alternating-subsequence-sum)", "[Maximum Alternating Subsequence Sum](/solution/1900-1999/1911.Maximum%20Alternating%20Subsequence%20Sum/README_EN.md)", "`Array`,`Dynamic Programming`", "Medium", ""]}, {"question_id": "2021", "frontend_question_id": "1910", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/remove-all-occurrences-of-a-substring", "url_en": "https://leetcode.com/problems/remove-all-occurrences-of-a-substring", "relative_path_cn": "/solution/1900-1999/1910.Remove%20All%20Occurrences%20of%20a%20Substring/README.md", "relative_path_en": "/solution/1900-1999/1910.Remove%20All%20Occurrences%20of%20a%20Substring/README_EN.md", "title_cn": "\u5220\u9664\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u51fa\u73b0\u7684\u7ed9\u5b9a\u5b50\u5b57\u7b26\u4e32", "title_en": "Remove All Occurrences of a Substring", "question_title_slug": "remove-all-occurrences-of-a-substring", "content_en": "Given two strings s
and part
, perform the following operation on s
until all occurrences of the substring part
are removed:
\n\n\n\t- Find the leftmost occurrence of the substring
part
and remove it from s
. \n
\n\nReturn s
after removing all occurrences of part
.
\n\nA substring is a contiguous sequence of characters in a string.
\n\n
\nExample 1:
\n\n\nInput: s = "daabcbaabcbc", part = "abc"\nOutput: "dab"\nExplanation: The following operations are done:\n- s = "daabcbaabcbc", remove "abc" starting at index 2, so s = "dabaabcbc".\n- s = "dabaabcbc", remove "abc" starting at index 4, so s = "dababc".\n- s = "dababc", remove "abc" starting at index 3, so s = "dab".\nNow s has no occurrences of "abc".\n
\n\nExample 2:
\n\n\nInput: s = "axxxxyyyyb", part = "xy"\nOutput: "ab"\nExplanation: The following operations are done:\n- s = "axxxxyyyyb", remove "xy" starting at index 4 so s = "axxxyyyb".\n- s = "axxxyyyb", remove "xy" starting at index 3 so s = "axxyyb".\n- s = "axxyyb", remove "xy" starting at index 2 so s = "axyb".\n- s = "axyb", remove "xy" starting at index 1 so s = "ab".\nNow s has no occurrences of "xy".\n
\n\n
\nConstraints:
\n\n\n\t1 <= s.length <= 1000
\n\t1 <= part.length <= 1000
\n\ts
\u200b\u200b\u200b\u200b\u200b\u200b and part
consists of lowercase English letters. \n
\n", "content_cn": "\u7ed9\u4f60\u4e24\u4e2a\u5b57\u7b26\u4e32\u00a0s
\u00a0\u548c\u00a0part
\u00a0\uff0c\u8bf7\u4f60\u5bf9\u00a0s
\u00a0\u53cd\u590d\u6267\u884c\u4ee5\u4e0b\u64cd\u4f5c\u76f4\u5230 \u6240\u6709\u00a0\u5b50\u5b57\u7b26\u4e32\u00a0part
\u00a0\u90fd\u88ab\u5220\u9664\uff1a
\n\n\n\t- \u627e\u5230
s
\u00a0\u4e2d \u6700\u5de6\u8fb9\u00a0\u7684\u5b50\u5b57\u7b26\u4e32 part
\u00a0\uff0c\u5e76\u5c06\u5b83\u4ece s
\u00a0\u4e2d\u5220\u9664\u3002 \n
\n\n\u8bf7\u4f60\u8fd4\u56de\u4ece s
\u00a0\u4e2d\u5220\u9664\u6240\u6709 part
\u00a0\u5b50\u5b57\u7b26\u4e32\u4ee5\u540e\u5f97\u5230\u7684\u5269\u4f59\u5b57\u7b26\u4e32\u3002
\n\n\u4e00\u4e2a \u5b50\u5b57\u7b26\u4e32\u00a0\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u8fde\u7eed\u7684\u5b57\u7b26\u5e8f\u5217\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1as = \"daabcbaabcbc\", part = \"abc\"\n\u8f93\u51fa\uff1a\"dab\"\n\u89e3\u91ca\uff1a\u4ee5\u4e0b\u64cd\u4f5c\u6309\u987a\u5e8f\u6267\u884c\uff1a\n- s = \"daabcbaabcbc\" \uff0c\u5220\u9664\u4e0b\u6807\u4ece 2 \u5f00\u59cb\u7684 \"abc\" \uff0c\u5f97\u5230 s = \"dabaabcbc\" \u3002\n- s = \"dabaabcbc\" \uff0c\u5220\u9664\u4e0b\u6807\u4ece 4 \u5f00\u59cb\u7684 \"abc\" \uff0c\u5f97\u5230 s = \"dababc\" \u3002\n- s = \"dababc\" \uff0c\u5220\u9664\u4e0b\u6807\u4ece 3 \u5f00\u59cb\u7684 \"abc\" \uff0c\u5f97\u5230 s = \"dab\" \u3002\n\u6b64\u65f6 s \u4e2d\u4e0d\u518d\u542b\u6709\u5b50\u5b57\u7b26\u4e32 \"abc\" \u3002\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1as = \"axxxxyyyyb\", part = \"xy\"\n\u8f93\u51fa\uff1a\"ab\"\n\u89e3\u91ca\uff1a\u4ee5\u4e0b\u64cd\u4f5c\u6309\u987a\u5e8f\u6267\u884c\uff1a\n- s = \"axxxxyyyyb\" \uff0c\u5220\u9664\u4e0b\u6807\u4ece 4 \u5f00\u59cb\u7684 \"xy\" \uff0c\u5f97\u5230 s = \"axxxyyyb\" \u3002\n- s = \"axxxyyyb\" \uff0c\u5220\u9664\u4e0b\u6807\u4ece 3 \u5f00\u59cb\u7684 \"xy\" \uff0c\u5f97\u5230 s = \"axxyyb\" \u3002\n- s = \"axxyyb\" \uff0c\u5220\u9664\u4e0b\u6807\u4ece 2 \u5f00\u59cb\u7684 \"xy\" \uff0c\u5f97\u5230 s = \"axyb\" \u3002\n- s = \"axyb\" \uff0c\u5220\u9664\u4e0b\u6807\u4ece 1 \u5f00\u59cb\u7684 \"xy\" \uff0c\u5f97\u5230 s = \"ab\" \u3002\n\u6b64\u65f6 s \u4e2d\u4e0d\u518d\u542b\u6709\u5b50\u5b57\u7b26\u4e32 \"xy\" \u3002\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\t1 <= s.length <= 1000
\n\t1 <= part.length <= 1000
\n\ts
\u200b\u200b\u200b\u200b\u200b\u200b \u548c\u00a0part
\u00a0\u53ea\u5305\u5c0f\u5199\u82f1\u6587\u5b57\u6bcd\u3002 \n
\n", "tags_en": ["String"], "tags_cn": ["\u5b57\u7b26\u4e32"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n string removeOccurrences(string s, string part) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public String removeOccurrences(String s, String part) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def removeOccurrences(self, s, part):\n \"\"\"\n :type s: str\n :type part: str\n :rtype: str\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def removeOccurrences(self, s: str, part: str) -> str:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nchar * removeOccurrences(char * s, char * part){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public string RemoveOccurrences(string s, string part) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {string} s\n * @param {string} part\n * @return {string}\n */\nvar removeOccurrences = function(s, part) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {String} s\n# @param {String} part\n# @return {String}\ndef remove_occurrences(s, part)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func removeOccurrences(_ s: String, _ part: String) -> String {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func removeOccurrences(s string, part string) string {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def removeOccurrences(s: String, part: String): String = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun removeOccurrences(s: String, part: String): String {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn remove_occurrences(s: String, part: String) -> String {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param String $s\n * @param String $part\n * @return String\n */\n function removeOccurrences($s, $part) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function removeOccurrences(s: string, part: string): string {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (remove-occurrences s part)\n (-> string? string? string?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec remove_occurrences(S :: unicode:unicode_binary(), Part :: unicode:unicode_binary()) -> unicode:unicode_binary().\nremove_occurrences(S, Part) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec remove_occurrences(s :: String.t, part :: String.t) :: String.t\n def remove_occurrences(s, part) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1910](https://leetcode-cn.com/problems/remove-all-occurrences-of-a-substring)", "[\u5220\u9664\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u51fa\u73b0\u7684\u7ed9\u5b9a\u5b50\u5b57\u7b26\u4e32](/solution/1900-1999/1910.Remove%20All%20Occurrences%20of%20a%20Substring/README.md)", "`\u5b57\u7b26\u4e32`", "\u4e2d\u7b49", ""], "md_table_row_en": ["[1910](https://leetcode.com/problems/remove-all-occurrences-of-a-substring)", "[Remove All Occurrences of a Substring](/solution/1900-1999/1910.Remove%20All%20Occurrences%20of%20a%20Substring/README_EN.md)", "`String`", "Medium", ""]}, {"question_id": "2020", "frontend_question_id": "1909", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/remove-one-element-to-make-the-array-strictly-increasing", "url_en": "https://leetcode.com/problems/remove-one-element-to-make-the-array-strictly-increasing", "relative_path_cn": "/solution/1900-1999/1909.Remove%20One%20Element%20to%20Make%20the%20Array%20Strictly%20Increasing/README.md", "relative_path_en": "/solution/1900-1999/1909.Remove%20One%20Element%20to%20Make%20the%20Array%20Strictly%20Increasing/README_EN.md", "title_cn": "\u5220\u9664\u4e00\u4e2a\u5143\u7d20\u4f7f\u6570\u7ec4\u4e25\u683c\u9012\u589e", "title_en": "Remove One Element to Make the Array Strictly Increasing", "question_title_slug": "remove-one-element-to-make-the-array-strictly-increasing", "content_en": "Given a 0-indexed integer array nums
, return true
if it can be made strictly increasing after removing exactly one element, or false
otherwise. If the array is already strictly increasing, return true
.
\n\nThe array nums
is strictly increasing if nums[i - 1] < nums[i]
for each index (1 <= i < nums.length).
\n\n
\nExample 1:
\n\n\nInput: nums = [1,2,10,5,7]\nOutput: true\nExplanation: By removing 10 at index 2 from nums, it becomes [1,2,5,7].\n[1,2,5,7] is strictly increasing, so return true.\n
\n\nExample 2:
\n\n\nInput: nums = [2,3,1,2]\nOutput: false\nExplanation:\n[3,1,2] is the result of removing the element at index 0.\n[2,1,2] is the result of removing the element at index 1.\n[2,3,2] is the result of removing the element at index 2.\n[2,3,1] is the result of removing the element at index 3.\nNo resulting array is strictly increasing, so return false.
\n\nExample 3:
\n\n\nInput: nums = [1,1,1]\nOutput: false\nExplanation: The result of removing any element is [1,1].\n[1,1] is not strictly increasing, so return false.\n
\n\nExample 4:
\n\n\nInput: nums = [1,2,3]\nOutput: true\nExplanation: [1,2,3] is already strictly increasing, so return true.\n
\n\n
\nConstraints:
\n\n\n\t2 <= nums.length <= 1000
\n\t1 <= nums[i] <= 1000
\n
\n", "content_cn": "\u7ed9\u4f60\u4e00\u4e2a\u4e0b\u6807\u4ece 0\u00a0\u5f00\u59cb\u7684\u6574\u6570\u6570\u7ec4\u00a0nums
\u00a0\uff0c\u5982\u679c\u00a0\u6070\u597d\u00a0\u5220\u9664\u00a0\u4e00\u4e2a\u00a0\u5143\u7d20\u540e\uff0c\u6570\u7ec4\u00a0\u4e25\u683c\u9012\u589e\u00a0\uff0c\u90a3\u4e48\u8bf7\u4f60\u8fd4\u56de\u00a0true
\u00a0\uff0c\u5426\u5219\u8fd4\u56de\u00a0false
\u00a0\u3002\u5982\u679c\u6570\u7ec4\u672c\u8eab\u5df2\u7ecf\u662f\u4e25\u683c\u9012\u589e\u7684\uff0c\u8bf7\u4f60\u4e5f\u8fd4\u56de\u00a0true
\u00a0\u3002
\n\n\u6570\u7ec4\u00a0nums
\u00a0\u662f \u4e25\u683c\u9012\u589e\u00a0\u7684\u5b9a\u4e49\u4e3a\uff1a\u5bf9\u4e8e\u4efb\u610f\u4e0b\u6807\u7684\u00a01 <= i < nums.length
\u00a0\u90fd\u6ee1\u8db3\u00a0nums[i - 1] < nums[i]
\u00a0\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1anums = [1,2,10,5,7]\n\u8f93\u51fa\uff1atrue\n\u89e3\u91ca\uff1a\u4ece nums \u4e2d\u5220\u9664\u4e0b\u6807 2 \u5904\u7684 10 \uff0c\u5f97\u5230 [1,2,5,7] \u3002\n[1,2,5,7] \u662f\u4e25\u683c\u9012\u589e\u7684\uff0c\u6240\u4ee5\u8fd4\u56de true \u3002\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1anums = [2,3,1,2]\n\u8f93\u51fa\uff1afalse\n\u89e3\u91ca\uff1a\n[3,1,2] \u662f\u5220\u9664\u4e0b\u6807 0 \u5904\u5143\u7d20\u540e\u5f97\u5230\u7684\u7ed3\u679c\u3002\n[2,1,2] \u662f\u5220\u9664\u4e0b\u6807 1 \u5904\u5143\u7d20\u540e\u5f97\u5230\u7684\u7ed3\u679c\u3002\n[2,3,2] \u662f\u5220\u9664\u4e0b\u6807 2 \u5904\u5143\u7d20\u540e\u5f97\u5230\u7684\u7ed3\u679c\u3002\n[2,3,1] \u662f\u5220\u9664\u4e0b\u6807 3 \u5904\u5143\u7d20\u540e\u5f97\u5230\u7684\u7ed3\u679c\u3002\n\u6ca1\u6709\u4efb\u4f55\u7ed3\u679c\u6570\u7ec4\u662f\u4e25\u683c\u9012\u589e\u7684\uff0c\u6240\u4ee5\u8fd4\u56de false \u3002
\n\n\u793a\u4f8b 3\uff1a
\n\n\u8f93\u5165\uff1anums = [1,1,1]\n\u8f93\u51fa\uff1afalse\n\u89e3\u91ca\uff1a\u5220\u9664\u4efb\u610f\u5143\u7d20\u540e\u7684\u7ed3\u679c\u90fd\u662f [1,1] \u3002\n[1,1] \u4e0d\u662f\u4e25\u683c\u9012\u589e\u7684\uff0c\u6240\u4ee5\u8fd4\u56de false \u3002\n
\n\n\u793a\u4f8b 4\uff1a
\n\n\u8f93\u5165\uff1anums = [1,2,3]\n\u8f93\u51fa\uff1atrue\n\u89e3\u91ca\uff1a[1,2,3] \u5df2\u7ecf\u662f\u4e25\u683c\u9012\u589e\u7684\uff0c\u6240\u4ee5\u8fd4\u56de true \u3002\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\t2 <= nums.length <= 1000
\n\t1 <= nums[i] <= 1000
\n
\n", "tags_en": ["Array"], "tags_cn": ["\u6570\u7ec4"], "difficulty_en": "Easy", "difficulty_cn": "\u7b80\u5355", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n bool canBeIncreasing(vector& nums) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public boolean canBeIncreasing(int[] nums) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def canBeIncreasing(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: bool\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def canBeIncreasing(self, nums: List[int]) -> bool:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nbool canBeIncreasing(int* nums, int numsSize){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public bool CanBeIncreasing(int[] nums) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[]} nums\n * @return {boolean}\n */\nvar canBeIncreasing = function(nums) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer[]} nums\n# @return {Boolean}\ndef can_be_increasing(nums)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func canBeIncreasing(_ nums: [Int]) -> Bool {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func canBeIncreasing(nums []int) bool {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def canBeIncreasing(nums: Array[Int]): Boolean = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun canBeIncreasing(nums: IntArray): Boolean {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn can_be_increasing(nums: Vec) -> bool {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @return Boolean\n */\n function canBeIncreasing($nums) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function canBeIncreasing(nums: number[]): boolean {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (can-be-increasing nums)\n (-> (listof exact-integer?) boolean?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec can_be_increasing(Nums :: [integer()]) -> boolean().\ncan_be_increasing(Nums) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec can_be_increasing(nums :: [integer]) :: boolean\n def can_be_increasing(nums) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1909](https://leetcode-cn.com/problems/remove-one-element-to-make-the-array-strictly-increasing)", "[\u5220\u9664\u4e00\u4e2a\u5143\u7d20\u4f7f\u6570\u7ec4\u4e25\u683c\u9012\u589e](/solution/1900-1999/1909.Remove%20One%20Element%20to%20Make%20the%20Array%20Strictly%20Increasing/README.md)", "`\u6570\u7ec4`", "\u7b80\u5355", ""], "md_table_row_en": ["[1909](https://leetcode.com/problems/remove-one-element-to-make-the-array-strictly-increasing)", "[Remove One Element to Make the Array Strictly Increasing](/solution/1900-1999/1909.Remove%20One%20Element%20to%20Make%20the%20Array%20Strictly%20Increasing/README_EN.md)", "`Array`", "Easy", ""]}, {"question_id": "2019", "frontend_question_id": "1868", "paid_only": true, "paid_only_cn": true, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/product-of-two-run-length-encoded-arrays", "url_en": "https://leetcode.com/problems/product-of-two-run-length-encoded-arrays", "relative_path_cn": "/solution/1800-1899/1868.Product%20of%20Two%20Run-Length%20Encoded%20Arrays/README.md", "relative_path_en": "/solution/1800-1899/1868.Product%20of%20Two%20Run-Length%20Encoded%20Arrays/README_EN.md", "title_cn": "\u4e24\u4e2a\u884c\u7a0b\u7f16\u7801\u6570\u7ec4\u7684\u79ef", "title_en": "Product of Two Run-Length Encoded Arrays", "question_title_slug": "product-of-two-run-length-encoded-arrays", "content_en": "Run-length encoding is a compression algorithm that allows for an integer array nums
with many segments of consecutive repeated numbers to be represented by a (generally smaller) 2D array encoded
. Each encoded[i] = [vali, freqi]
describes the ith
segment of repeated numbers in nums
where vali
is the value that is repeated freqi
times.
\n\n\n\t- For example,
nums = [1,1,1,2,2,2,2,2]
is represented by the run-length encoded array encoded = [[1,3],[2,5]]
. Another way to read this is "three 1
's followed by five 2
's". \n
\n\nThe product of two run-length encoded arrays encoded1
and encoded2
can be calculated using the following steps:
\n\n\n\t- Expand both
encoded1
and encoded2
into the full arrays nums1
and nums2
respectively. \n\t- Create a new array
prodNums
of length nums1.length
and set prodNums[i] = nums1[i] * nums2[i]
. \n\t- Compress
prodNums
into a run-length encoded array and return it. \n
\n\nYou are given two run-length encoded arrays encoded1
and encoded2
representing full arrays nums1
and nums2
respectively. Both nums1
and nums2
have the same length. Each encoded1[i] = [vali, freqi]
describes the ith
segment of nums1
, and each encoded2[j] = [valj, freqj]
describes the jth
segment of nums2
.
\n\nReturn the product of encoded1
and encoded2
.
\n\nNote: Compression should be done such that the run-length encoded array has the minimum possible length.
\n\n
\nExample 1:
\n\n\nInput: encoded1 = [[1,3],[2,3]], encoded2 = [[6,3],[3,3]]\nOutput: [[6,6]]\nExplanation: encoded1 expands to [1,1,1,2,2,2] and encoded2 expands to [6,6,6,3,3,3].\nprodNums = [6,6,6,6,6,6], which is compressed into the run-length encoded array [[6,6]].\n
\n\nExample 2:
\n\n\nInput: encoded1 = [[1,3],[2,1],[3,2]], encoded2 = [[2,3],[3,3]]\nOutput: [[2,3],[6,1],[9,2]]\nExplanation: encoded1 expands to [1,1,1,2,3,3] and encoded2 expands to [2,2,2,3,3,3].\nprodNums = [2,2,2,6,9,9], which is compressed into the run-length encoded array [[2,3],[6,1],[9,2]].\n
\n\n
\nConstraints:
\n\n\n\t1 <= encoded1.length, encoded2.length <= 105
\n\tencoded1[i].length == 2
\n\tencoded2[j].length == 2
\n\t1 <= vali, freqi <= 104
for each encoded1[i]
. \n\t1 <= valj, freqj <= 104
for each encoded2[j]
. \n\t- The full arrays that
encoded1
and encoded2
represent are the same length. \n
\n", "content_cn": "\u884c\u7a0b\u7f16\u7801\uff08Run-length encoding\uff09\u662f\u4e00\u79cd\u538b\u7f29\u7b97\u6cd5\uff0c\u80fd\u8ba9\u4e00\u4e2a\u542b\u6709\u8bb8\u591a\u6bb5\u8fde\u7eed\u91cd\u590d\u6570\u5b57\u7684\u6574\u6570\u7c7b\u578b\u6570\u7ec4\u00a0nums
\u00a0\u4ee5\u4e00\u4e2a\uff08\u901a\u5e38\u66f4\u5c0f\u7684\uff09\u4e8c\u7ef4\u6570\u7ec4\u00a0encoded
\u00a0\u8868\u793a\u3002\u6bcf\u4e2a\u00a0encoded[i] = [vali, freqi]
\u00a0\u8868\u793a nums
\u00a0\u4e2d\u7b2c\u00a0i
\u00a0\u6bb5\u91cd\u590d\u6570\u5b57\uff0c\u5176\u4e2d\u00a0vali
\u00a0\u662f\u8be5\u6bb5\u91cd\u590d\u6570\u5b57\uff0c\u91cd\u590d\u4e86\u00a0freqi
\u6b21\u3002
\n\n\n\t- \u4f8b\u5982\uff0c\u00a0
nums = [1,1,1,2,2,2,2,2]
\u00a0\u53ef\u8868\u793a\u79f0\u884c\u7a0b\u7f16\u7801\u6570\u7ec4\u00a0encoded = [[1,3],[2,5]]
\u00a0\u3002\u5bf9\u6b64\u6570\u7ec4\u7684\u53e6\u4e00\u79cd\u8bfb\u6cd5\u662f\u201c\u4e09\u4e2a\u00a01
\u00a0\uff0c\u540e\u9762\u6709\u4e94\u4e2a\u00a02
\u00a0\u201d\u3002 \n
\n\n\u4e24\u4e2a\u884c\u7a0b\u7f16\u7801\u6570\u7ec4\u00a0encoded1
\u00a0\u548c\u00a0encoded2
\u00a0\u7684\u79ef\u53ef\u4ee5\u6309\u4e0b\u5217\u6b65\u9aa4\u8ba1\u7b97\uff1a
\n\n\n\t- \u5c06\u00a0
encoded1
\u00a0\u548c\u00a0encoded2
\u00a0\u5206\u522b\u6269\u5c55\u6210\u5b8c\u6574\u6570\u7ec4\u00a0nums1
\u00a0\u548c\u00a0nums2
\u3002 \n\t- \u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4\u00a0
prodNums
\u00a0\uff0c\u957f\u5ea6\u4e3a\u00a0nums1.length
\u00a0\u5e76\u8bbe\u00a0prodNums[i] = nums1[i] * nums2[i]
\u00a0\u3002 \n\t- \u5c06\u00a0
prodNums
\u00a0\u538b\u7f29\u6210\u4e00\u4e2a\u884c\u7a0b\u7f16\u7801\u6570\u7ec4\u5e76\u8fd4\u56de\u4e4b\u3002 \n
\n\n\u7ed9\u5b9a\u4e24\u4e2a\u884c\u7a0b\u7f16\u7801\u6570\u7ec4\u00a0encoded1
\u00a0\u548c\u00a0encoded2
\uff0c\u5206\u522b\u8868\u793a\u5b8c\u6574\u6570\u7ec4\u00a0nums1
\u00a0\u548c\u00a0nums2
\u3002nums1
\u00a0\u548c\u00a0nums2
\u00a0\u7684\u957f\u5ea6\u76f8\u540c\u3002\u00a0\u6bcf\u4e00\u4e2a\u00a0encoded1[i] = [vali, freqi]
\u00a0\u8868\u793a nums1
\u00a0\u4e2d\u7684\u7b2c\u00a0i
\u00a0\u6bb5\uff0c\u6bcf\u4e00\u4e2a\u00a0encoded2[j] = [valj, freqj]
\u00a0\u8868\u793a nums2
\u00a0\u4e2d\u7684\u7b2c\u00a0j
\u00a0\u6bb5\u3002
\n\n\u8fd4\u56de\u00a0encoded1
\u00a0\u548c\u00a0encoded2
\u00a0\u7684\u4e58\u79ef\u3002
\n\n\u6ce8\uff1a\u884c\u7a0b\u7f16\u7801\u6570\u7ec4\u9700\u538b\u7f29\u6210\u53ef\u80fd\u7684\u6700\u5c0f\u957f\u5ea6\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1:
\n\n\u8f93\u5165: encoded1 = [[1,3],[2,3]], encoded2 = [[6,3],[3,3]]\n\u8f93\u51fa: [[6,6]]\n\u89e3\u91can: encoded1 \u6269\u5c55\u4e3a [1,1,1,2,2,2] \uff0cencoded2 \u6269\u5c55\u4e3a [6,6,6,3,3,3]\u3002\nprodNums = [6,6,6,6,6,6]\uff0c\u538b\u7f29\u6210\u884c\u7a0b\u7f16\u7801\u6570\u7ec4 [[6,6]]\u3002\n
\n\n\u793a\u4f8b 2:
\n\n\u8f93\u5165: encoded1 = [[1,3],[2,1],[3,2]], encoded2 = [[2,3],[3,3]]\n\u8f93\u51fa: [[2,3],[6,1],[9,2]]\n\u89e3\u91ca: encoded1 \u6269\u5c55\u4e3a [1,1,1,2,3,3] \uff0cencoded2 \u6269\u5c55\u4e3a [2,2,2,3,3,3]\u3002\nprodNums = [2,2,2,6,9,9]\uff0c\u538b\u7f29\u6210\u884c\u7a0b\u7f16\u7801\u6570\u7ec4 [[2,3],[6,1],[9,2]]\u3002\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\t1 <= encoded1.length, encoded2.length <= 105
\n\tencoded1[i].length == 2
\n\tencoded2[j].length == 2
\n\t- \u5bf9\u4e8e\u6bcf\u4e00\u4e2a\u00a0
encoded1[i]
\uff0c\u00a01 <= vali, freqi <= 104
\u00a0\u00a0 \n\t- \u5bf9\u4e8e\u6bcf\u4e00\u4e2a\u00a0
encoded2[j]
\uff0c\u00a01 <= valj, freqj <= 104
\n\tencoded1
\u00a0\u548c\u00a0encoded2
\u00a0\u8868\u793a\u7684\u5b8c\u6574\u6570\u7ec4\u957f\u5ea6\u76f8\u540c\u3002 \n
\n", "tags_en": ["Array", "Two Pointers"], "tags_cn": ["\u6570\u7ec4", "\u53cc\u6307\u9488"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n vector> findRLEArray(vector>& encoded1, vector>& encoded2) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public List> findRLEArray(int[][] encoded1, int[][] encoded2) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def findRLEArray(self, encoded1, encoded2):\n \"\"\"\n :type encoded1: List[List[int]]\n :type encoded2: List[List[int]]\n :rtype: List[List[int]]\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def findRLEArray(self, encoded1: List[List[int]], encoded2: List[List[int]]) -> List[List[int]]:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\n/**\n * Return an array of arrays of size *returnSize.\n * The sizes of the arrays are returned as *returnColumnSizes array.\n * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().\n */\nint** findRLEArray(int** encoded1, int encoded1Size, int* encoded1ColSize, int** encoded2, int encoded2Size, int* encoded2ColSize, int* returnSize, int** returnColumnSizes){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public IList> FindRLEArray(int[][] encoded1, int[][] encoded2) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[][]} encoded1\n * @param {number[][]} encoded2\n * @return {number[][]}\n */\nvar findRLEArray = function(encoded1, encoded2) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer[][]} encoded1\n# @param {Integer[][]} encoded2\n# @return {Integer[][]}\ndef find_rle_array(encoded1, encoded2)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func findRLEArray(_ encoded1: [[Int]], _ encoded2: [[Int]]) -> [[Int]] {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func findRLEArray(encoded1 [][]int, encoded2 [][]int) [][]int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def findRLEArray(encoded1: Array[Array[Int]], encoded2: Array[Array[Int]]): List[List[Int]] = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun findRLEArray(encoded1: Array, encoded2: Array): List> {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn find_rle_array(encoded1: Vec>, encoded2: Vec>) -> Vec> {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer[][] $encoded1\n * @param Integer[][] $encoded2\n * @return Integer[][]\n */\n function findRLEArray($encoded1, $encoded2) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function findRLEArray(encoded1: number[][], encoded2: number[][]): number[][] {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (find-rle-array encoded1 encoded2)\n (-> (listof (listof exact-integer?)) (listof (listof exact-integer?)) (listof (listof exact-integer?)))\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec find_rle_array(Encoded1 :: [[integer()]], Encoded2 :: [[integer()]]) -> [[integer()]].\nfind_rle_array(Encoded1, Encoded2) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec find_rle_array(encoded1 :: [[integer]], encoded2 :: [[integer]]) :: [[integer]]\n def find_rle_array(encoded1, encoded2) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1868](https://leetcode-cn.com/problems/product-of-two-run-length-encoded-arrays)", "[\u4e24\u4e2a\u884c\u7a0b\u7f16\u7801\u6570\u7ec4\u7684\u79ef](/solution/1800-1899/1868.Product%20of%20Two%20Run-Length%20Encoded%20Arrays/README.md)", "`\u6570\u7ec4`,`\u53cc\u6307\u9488`", "\u4e2d\u7b49", "\ud83d\udd12"], "md_table_row_en": ["[1868](https://leetcode.com/problems/product-of-two-run-length-encoded-arrays)", "[Product of Two Run-Length Encoded Arrays](/solution/1800-1899/1868.Product%20of%20Two%20Run-Length%20Encoded%20Arrays/README_EN.md)", "`Array`,`Two Pointers`", "Medium", "\ud83d\udd12"]}, {"question_id": "2018", "frontend_question_id": "1889", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/minimum-space-wasted-from-packaging", "url_en": "https://leetcode.com/problems/minimum-space-wasted-from-packaging", "relative_path_cn": "/solution/1800-1899/1889.Minimum%20Space%20Wasted%20From%20Packaging/README.md", "relative_path_en": "/solution/1800-1899/1889.Minimum%20Space%20Wasted%20From%20Packaging/README_EN.md", "title_cn": "\u88c5\u5305\u88f9\u7684\u6700\u5c0f\u6d6a\u8d39\u7a7a\u95f4", "title_en": "Minimum Space Wasted From Packaging", "question_title_slug": "minimum-space-wasted-from-packaging", "content_en": "You have n
packages that you are trying to place in boxes, one package in each box. There are m
suppliers that each produce boxes of different sizes (with infinite supply). A package can be placed in a box if the size of the package is less than or equal to the size of the box.
\n\nThe package sizes are given as an integer array packages
, where packages[i]
is the size of the ith
package. The suppliers are given as a 2D integer array boxes
, where boxes[j]
is an array of box sizes that the jth
supplier produces.
\n\nYou want to choose a single supplier and use boxes from them such that the total wasted space is minimized. For each package in a box, we define the space wasted to be size of the box - size of the package
. The total wasted space is the sum of the space wasted in all the boxes.
\n\n\n\t- For example, if you have to fit packages with sizes
[2,3,5]
and the supplier offers boxes of sizes [4,8]
, you can fit the packages of size-2
and size-3
into two boxes of size-4
and the package with size-5
into a box of size-8
. This would result in a waste of (4-2) + (4-3) + (8-5) = 6
. \n
\n\nReturn the minimum total wasted space by choosing the box supplier optimally, or -1
if it is impossible to fit all the packages inside boxes. Since the answer may be large, return it modulo 109 + 7
.
\n\n
\nExample 1:
\n\n\nInput: packages = [2,3,5], boxes = [[4,8],[2,8]]\nOutput: 6\nExplanation: It is optimal to choose the first supplier, using two size-4 boxes and one size-8 box.\nThe total waste is (4-2) + (4-3) + (8-5) = 6.\n
\n\nExample 2:
\n\n\nInput: packages = [2,3,5], boxes = [[1,4],[2,3],[3,4]]\nOutput: -1\nExplanation: There is no box that the package of size 5 can fit in.\n
\n\nExample 3:
\n\n\nInput: packages = [3,5,8,10,11,12], boxes = [[12],[11,9],[10,5,14]]\nOutput: 9\nExplanation: It is optimal to choose the third supplier, using two size-5 boxes, two size-10 boxes, and two size-14 boxes.\nThe total waste is (5-3) + (5-5) + (10-8) + (10-10) + (14-11) + (14-12) = 9.\n
\n\n
\nConstraints:
\n\n\n\tn == packages.length
\n\tm == boxes.length
\n\t1 <= n <= 105
\n\t1 <= m <= 105
\n\t1 <= packages[i] <= 105
\n\t1 <= boxes[j].length <= 105
\n\t1 <= boxes[j][k] <= 105
\n\tsum(boxes[j].length) <= 105
\n\t- The elements in
boxes[j]
are distinct. \n
\n", "content_cn": "\u7ed9\u4f60\u00a0n
\u00a0\u4e2a\u5305\u88f9\uff0c\u4f60\u9700\u8981\u628a\u5b83\u4eec\u88c5\u5728\u7bb1\u5b50\u91cc\uff0c\u6bcf\u4e2a\u7bb1\u5b50\u88c5\u4e00\u4e2a\u5305\u88f9\u3002\u603b\u5171\u6709\u00a0m
\u00a0\u4e2a\u4f9b\u5e94\u5546\u63d0\u4f9b \u4e0d\u540c\u5c3a\u5bf8\u00a0\u7684\u7bb1\u5b50\uff08\u6bcf\u4e2a\u89c4\u683c\u90fd\u6709\u65e0\u6570\u4e2a\u7bb1\u5b50\uff09\u3002\u5982\u679c\u4e00\u4e2a\u5305\u88f9\u7684\u5c3a\u5bf8 \u5c0f\u4e8e\u7b49\u4e8e\u00a0\u4e00\u4e2a\u7bb1\u5b50\u7684\u5c3a\u5bf8\uff0c\u90a3\u4e48\u8fd9\u4e2a\u5305\u88f9\u5c31\u53ef\u4ee5\u653e\u5165\u8fd9\u4e2a\u7bb1\u5b50\u4e4b\u4e2d\u3002
\n\n\u5305\u88f9\u7684\u5c3a\u5bf8\u7528\u4e00\u4e2a\u6574\u6570\u6570\u7ec4\u00a0packages
\u00a0\u8868\u793a\uff0c\u5176\u4e2d\u00a0packages[i]
\u00a0\u662f\u7b2c\u00a0i
\u00a0\u4e2a\u5305\u88f9\u7684\u5c3a\u5bf8\u3002\u4f9b\u5e94\u5546\u7528\u4e8c\u7ef4\u6570\u7ec4\u00a0boxes
\u00a0\u8868\u793a\uff0c\u5176\u4e2d\u00a0boxes[j]
\u00a0\u662f\u7b2c j
\u00a0\u4e2a\u4f9b\u5e94\u5546\u63d0\u4f9b\u7684\u6240\u6709\u7bb1\u5b50\u5c3a\u5bf8\u7684\u6570\u7ec4\u3002
\n\n\u4f60\u60f3\u8981\u9009\u62e9 \u4e00\u4e2a\u4f9b\u5e94\u5546\u00a0\u5e76\u53ea\u4f7f\u7528\u8be5\u4f9b\u5e94\u5546\u63d0\u4f9b\u7684\u7bb1\u5b50\uff0c\u4f7f\u5f97 \u603b\u6d6a\u8d39\u7a7a\u95f4\u6700\u5c0f\u00a0\u3002\u5bf9\u4e8e\u6bcf\u4e2a\u88c5\u4e86\u5305\u88f9\u7684\u7bb1\u5b50\uff0c\u6211\u4eec\u5b9a\u4e49 \u6d6a\u8d39\u7684\u00a0\u7a7a\u95f4\u7b49\u4e8e \u7bb1\u5b50\u7684\u5c3a\u5bf8 - \u5305\u88f9\u7684\u5c3a\u5bf8
\u00a0\u3002\u603b\u6d6a\u8d39\u7a7a\u95f4\u00a0\u4e3a\u00a0\u6240\u6709\u00a0\u7bb1\u5b50\u4e2d\u6d6a\u8d39\u7a7a\u95f4\u7684\u603b\u548c\u3002
\n\n\n\t- \u6bd4\u65b9\u8bf4\uff0c\u5982\u679c\u4f60\u60f3\u8981\u7528\u5c3a\u5bf8\u6570\u7ec4\u4e3a\u00a0
[4,8]
\u00a0\u7684\u7bb1\u5b50\u88c5\u4e0b\u5c3a\u5bf8\u4e3a\u00a0[2,3,5]
\u00a0\u7684\u5305\u88f9\uff0c\u4f60\u53ef\u4ee5\u5c06\u5c3a\u5bf8\u4e3a 2
\u00a0\u548c 3
\u00a0\u7684\u4e24\u4e2a\u5305\u88f9\u88c5\u5165\u4e24\u4e2a\u5c3a\u5bf8\u4e3a 4
\u00a0\u7684\u7bb1\u5b50\u4e2d\uff0c\u540c\u65f6\u628a\u5c3a\u5bf8\u4e3a 5
\u00a0\u7684\u5305\u88f9\u88c5\u5165\u5c3a\u5bf8\u4e3a 8
\u00a0\u7684\u7bb1\u5b50\u4e2d\u3002\u603b\u6d6a\u8d39\u7a7a\u95f4\u4e3a\u00a0(4-2) + (4-3) + (8-5) = 6
\u00a0\u3002 \n
\n\n\u8bf7\u4f60\u9009\u62e9 \u6700\u4f18\u00a0\u7bb1\u5b50\u4f9b\u5e94\u5546\uff0c\u4f7f\u5f97 \u603b\u6d6a\u8d39\u7a7a\u95f4\u6700\u5c0f\u00a0\u3002\u5982\u679c \u65e0\u6cd5 \u5c06\u6240\u6709\u5305\u88f9\u653e\u5165\u7bb1\u5b50\u4e2d\uff0c\u8bf7\u4f60\u8fd4\u56de -1
\u00a0\u3002\u7531\u4e8e\u7b54\u6848\u53ef\u80fd\u4f1a \u5f88\u5927\u00a0\uff0c\u8bf7\u8fd4\u56de\u5b83\u5bf9\u00a0109 + 7
\u00a0\u53d6\u4f59\u00a0\u7684\u7ed3\u679c\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1apackages = [2,3,5], boxes = [[4,8],[2,8]]\n\u8f93\u51fa\uff1a6\n\u89e3\u91ca\uff1a\u9009\u62e9\u7b2c\u4e00\u4e2a\u4f9b\u5e94\u5546\u6700\u4f18\uff0c\u7528\u4e24\u4e2a\u5c3a\u5bf8\u4e3a 4 \u7684\u7bb1\u5b50\u548c\u4e00\u4e2a\u5c3a\u5bf8\u4e3a 8 \u7684\u7bb1\u5b50\u3002\n\u603b\u6d6a\u8d39\u7a7a\u95f4\u4e3a (4-2) + (4-3) + (8-5) = 6 \u3002\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1apackages = [2,3,5], boxes = [[1,4],[2,3],[3,4]]\n\u8f93\u51fa\uff1a-1\n\u89e3\u91ca\uff1a\u6ca1\u6709\u7bb1\u5b50\u80fd\u88c5\u4e0b\u5c3a\u5bf8\u4e3a 5 \u7684\u5305\u88f9\u3002\n
\n\n\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1apackages = [3,5,8,10,11,12], boxes = [[12],[11,9],[10,5,14]]\n\u8f93\u51fa\uff1a9\n\u89e3\u91ca\uff1a\u9009\u62e9\u7b2c\u4e09\u4e2a\u4f9b\u5e94\u5546\u6700\u4f18\uff0c\u7528\u4e24\u4e2a\u5c3a\u5bf8\u4e3a 5 \u7684\u7bb1\u5b50\uff0c\u4e24\u4e2a\u5c3a\u5bf8\u4e3a 10 \u7684\u7bb1\u5b50\u548c\u4e24\u4e2a\u5c3a\u5bf8\u4e3a 14 \u7684\u7bb1\u5b50\u3002\n\u603b\u6d6a\u8d39\u7a7a\u95f4\u4e3a (5-3) + (5-5) + (10-8) + (10-10) + (14-11) + (14-12) = 9 \u3002\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\tn == packages.length
\n\tm == boxes.length
\n\t1 <= n <= 105
\n\t1 <= m <= 105
\n\t1 <= packages[i] <= 105
\n\t1 <= boxes[j].length <= 105
\n\t1 <= boxes[j][k] <= 105
\n\tsum(boxes[j].length) <= 105
\n\tboxes[j]
\u00a0\u4e2d\u7684\u5143\u7d20 \u4e92\u4e0d\u76f8\u540c\u00a0\u3002 \n
\n", "tags_en": ["Array", "Binary Search", "Prefix Sum", "Sorting"], "tags_cn": ["\u6570\u7ec4", "\u4e8c\u5206\u67e5\u627e", "\u524d\u7f00\u548c", "\u6392\u5e8f"], "difficulty_en": "Hard", "difficulty_cn": "\u56f0\u96be", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n int minWastedSpace(vector& packages, vector>& boxes) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int minWastedSpace(int[] packages, int[][] boxes) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def minWastedSpace(self, packages, boxes):\n \"\"\"\n :type packages: List[int]\n :type boxes: List[List[int]]\n :rtype: int\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def minWastedSpace(self, packages: List[int], boxes: List[List[int]]) -> int:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nint minWastedSpace(int* packages, int packagesSize, int** boxes, int boxesSize, int* boxesColSize){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int MinWastedSpace(int[] packages, int[][] boxes) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[]} packages\n * @param {number[][]} boxes\n * @return {number}\n */\nvar minWastedSpace = function(packages, boxes) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer[]} packages\n# @param {Integer[][]} boxes\n# @return {Integer}\ndef min_wasted_space(packages, boxes)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func minWastedSpace(_ packages: [Int], _ boxes: [[Int]]) -> Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func minWastedSpace(packages []int, boxes [][]int) int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def minWastedSpace(packages: Array[Int], boxes: Array[Array[Int]]): Int = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun minWastedSpace(packages: IntArray, boxes: Array): Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn min_wasted_space(packages: Vec, boxes: Vec>) -> i32 {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer[] $packages\n * @param Integer[][] $boxes\n * @return Integer\n */\n function minWastedSpace($packages, $boxes) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function minWastedSpace(packages: number[], boxes: number[][]): number {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (min-wasted-space packages boxes)\n (-> (listof exact-integer?) (listof (listof exact-integer?)) exact-integer?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec min_wasted_space(Packages :: [integer()], Boxes :: [[integer()]]) -> integer().\nmin_wasted_space(Packages, Boxes) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec min_wasted_space(packages :: [integer], boxes :: [[integer]]) :: integer\n def min_wasted_space(packages, boxes) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1889](https://leetcode-cn.com/problems/minimum-space-wasted-from-packaging)", "[\u88c5\u5305\u88f9\u7684\u6700\u5c0f\u6d6a\u8d39\u7a7a\u95f4](/solution/1800-1899/1889.Minimum%20Space%20Wasted%20From%20Packaging/README.md)", "`\u6570\u7ec4`,`\u4e8c\u5206\u67e5\u627e`,`\u524d\u7f00\u548c`,`\u6392\u5e8f`", "\u56f0\u96be", ""], "md_table_row_en": ["[1889](https://leetcode.com/problems/minimum-space-wasted-from-packaging)", "[Minimum Space Wasted From Packaging](/solution/1800-1899/1889.Minimum%20Space%20Wasted%20From%20Packaging/README_EN.md)", "`Array`,`Binary Search`,`Prefix Sum`,`Sorting`", "Hard", ""]}, {"question_id": "2017", "frontend_question_id": "1888", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/minimum-number-of-flips-to-make-the-binary-string-alternating", "url_en": "https://leetcode.com/problems/minimum-number-of-flips-to-make-the-binary-string-alternating", "relative_path_cn": "/solution/1800-1899/1888.Minimum%20Number%20of%20Flips%20to%20Make%20the%20Binary%20String%20Alternating/README.md", "relative_path_en": "/solution/1800-1899/1888.Minimum%20Number%20of%20Flips%20to%20Make%20the%20Binary%20String%20Alternating/README_EN.md", "title_cn": "\u4f7f\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u5b57\u7b26\u4ea4\u66ff\u7684\u6700\u5c11\u53cd\u8f6c\u6b21\u6570", "title_en": "Minimum Number of Flips to Make the Binary String Alternating", "question_title_slug": "minimum-number-of-flips-to-make-the-binary-string-alternating", "content_en": "You are given a binary string s
. You are allowed to perform two types of operations on the string in any sequence:
\n\n\n\t- Type-1: Remove the character at the start of the string
s
and append it to the end of the string. \n\t- Type-2: Pick any character in
s
and flip its value, i.e., if its value is '0'
it becomes '1'
and vice-versa. \n
\n\nReturn the minimum number of type-2 operations you need to perform such that s
becomes alternating.
\n\nThe string is called alternating if no two adjacent characters are equal.
\n\n\n\t- For example, the strings
"010"
and "1010"
are alternating, while the string "0100"
is not. \n
\n\n
\nExample 1:
\n\n\nInput: s = "111000"\nOutput: 2\nExplanation: Use the first operation two times to make s = "100011".\nThen, use the second operation on the third and sixth elements to make s = "101010".\n
\n\nExample 2:
\n\n\nInput: s = "010"\nOutput: 0\nExplanation: The string is already alternating.\n
\n\nExample 3:
\n\n\nInput: s = "1110"\nOutput: 1\nExplanation: Use the second operation on the second element to make s = "1010".\n
\n\n
\nConstraints:
\n\n\n\t1 <= s.length <= 105
\n\ts[i]
is either '0'
or '1'
. \n
\n", "content_cn": "\u7ed9\u4f60\u4e00\u4e2a\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u00a0s
\u00a0\u3002\u4f60\u53ef\u4ee5\u6309\u4efb\u610f\u987a\u5e8f\u6267\u884c\u4ee5\u4e0b\u4e24\u79cd\u64cd\u4f5c\u4efb\u610f\u6b21\uff1a
\n\n\n\t- \u7c7b\u578b 1 \uff1a\u5220\u9664 \u5b57\u7b26\u4e32\u00a0
s
\u00a0\u7684\u7b2c\u4e00\u4e2a\u5b57\u7b26\u5e76\u5c06\u5b83 \u6dfb\u52a0\u00a0\u5230\u5b57\u7b26\u4e32\u7ed3\u5c3e\u3002 \n\t- \u7c7b\u578b 2 \uff1a\u9009\u62e9 \u5b57\u7b26\u4e32\u00a0
s
\u00a0\u4e2d\u4efb\u610f\u4e00\u4e2a\u5b57\u7b26\u5e76\u5c06\u8be5\u5b57\u7b26\u00a0\u53cd\u8f6c\u00a0\uff0c\u4e5f\u5c31\u662f\u5982\u679c\u503c\u4e3a\u00a0'0'
\u00a0\uff0c\u5219\u53cd\u8f6c\u5f97\u5230\u00a0'1'
\u00a0\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002 \n
\n\n\u8bf7\u4f60\u8fd4\u56de\u4f7f s
\u00a0\u53d8\u6210 \u4ea4\u66ff \u5b57\u7b26\u4e32\u7684\u524d\u63d0\u4e0b\uff0c\u00a0\u7c7b\u578b 2\u00a0\u7684 \u6700\u5c11\u00a0\u64cd\u4f5c\u6b21\u6570\u00a0\u3002
\n\n\u6211\u4eec\u79f0\u4e00\u4e2a\u5b57\u7b26\u4e32\u662f \u4ea4\u66ff\u00a0\u7684\uff0c\u9700\u8981\u6ee1\u8db3\u4efb\u610f\u76f8\u90bb\u5b57\u7b26\u90fd\u4e0d\u540c\u3002
\n\n\n\t- \u6bd4\u65b9\u8bf4\uff0c\u5b57\u7b26\u4e32\u00a0
\"010\"
\u548c\u00a0\"1010\"
\u00a0\u90fd\u662f\u4ea4\u66ff\u7684\uff0c\u4f46\u662f\u5b57\u7b26\u4e32\u00a0\"0100\"
\u00a0\u4e0d\u662f\u3002 \n
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1as = \"111000\"\n\u8f93\u51fa\uff1a2\n\u89e3\u91ca\uff1a\u6267\u884c\u7b2c\u4e00\u79cd\u64cd\u4f5c\u4e24\u6b21\uff0c\u5f97\u5230 s = \"100011\" \u3002\n\u7136\u540e\u5bf9\u7b2c\u4e09\u4e2a\u548c\u7b2c\u516d\u4e2a\u5b57\u7b26\u6267\u884c\u7b2c\u4e8c\u79cd\u64cd\u4f5c\uff0c\u5f97\u5230 s = \"101010\" \u3002\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1as = \"010\"\n\u8f93\u51fa\uff1a0\n\u89e3\u91ca\uff1a\u5b57\u7b26\u4e32\u5df2\u7ecf\u662f\u4ea4\u66ff\u7684\u3002\n
\n\n\u793a\u4f8b 3\uff1a
\n\n\u8f93\u5165\uff1as = \"1110\"\n\u8f93\u51fa\uff1a1\n\u89e3\u91ca\uff1a\u5bf9\u7b2c\u4e8c\u4e2a\u5b57\u7b26\u6267\u884c\u7b2c\u4e8c\u79cd\u64cd\u4f5c\uff0c\u5f97\u5230 s = \"1010\" \u3002\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\t1 <= s.length <= 105
\n\ts[i]
\u00a0\u8981\u4e48\u662f\u00a0'0'
\u00a0\uff0c\u8981\u4e48\u662f\u00a0'1'
\u00a0\u3002 \n
\n", "tags_en": ["Greedy", "String"], "tags_cn": ["\u8d2a\u5fc3", "\u5b57\u7b26\u4e32"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n int minFlips(string s) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int minFlips(String s) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def minFlips(self, s):\n \"\"\"\n :type s: str\n :rtype: int\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def minFlips(self, s: str) -> int:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nint minFlips(char * s){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int MinFlips(string s) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {string} s\n * @return {number}\n */\nvar minFlips = function(s) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {String} s\n# @return {Integer}\ndef min_flips(s)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func minFlips(_ s: String) -> Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func minFlips(s string) int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def minFlips(s: String): Int = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun minFlips(s: String): Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn min_flips(s: String) -> i32 {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param String $s\n * @return Integer\n */\n function minFlips($s) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function minFlips(s: string): number {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (min-flips s)\n (-> string? exact-integer?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec min_flips(S :: unicode:unicode_binary()) -> integer().\nmin_flips(S) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec min_flips(s :: String.t) :: integer\n def min_flips(s) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1888](https://leetcode-cn.com/problems/minimum-number-of-flips-to-make-the-binary-string-alternating)", "[\u4f7f\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u5b57\u7b26\u4ea4\u66ff\u7684\u6700\u5c11\u53cd\u8f6c\u6b21\u6570](/solution/1800-1899/1888.Minimum%20Number%20of%20Flips%20to%20Make%20the%20Binary%20String%20Alternating/README.md)", "`\u8d2a\u5fc3`,`\u5b57\u7b26\u4e32`", "\u4e2d\u7b49", ""], "md_table_row_en": ["[1888](https://leetcode.com/problems/minimum-number-of-flips-to-make-the-binary-string-alternating)", "[Minimum Number of Flips to Make the Binary String Alternating](/solution/1800-1899/1888.Minimum%20Number%20of%20Flips%20to%20Make%20the%20Binary%20String%20Alternating/README_EN.md)", "`Greedy`,`String`", "Medium", ""]}, {"question_id": "2016", "frontend_question_id": "1887", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/reduction-operations-to-make-the-array-elements-equal", "url_en": "https://leetcode.com/problems/reduction-operations-to-make-the-array-elements-equal", "relative_path_cn": "/solution/1800-1899/1887.Reduction%20Operations%20to%20Make%20the%20Array%20Elements%20Equal/README.md", "relative_path_en": "/solution/1800-1899/1887.Reduction%20Operations%20to%20Make%20the%20Array%20Elements%20Equal/README_EN.md", "title_cn": "\u4f7f\u6570\u7ec4\u5143\u7d20\u76f8\u7b49\u7684\u51cf\u5c11\u64cd\u4f5c\u6b21\u6570", "title_en": "Reduction Operations to Make the Array Elements Equal", "question_title_slug": "reduction-operations-to-make-the-array-elements-equal", "content_en": "Given an integer array nums
, your goal is to make all elements in nums
equal. To complete one operation, follow these steps:
\n\n\n\t- Find the largest value in
nums
. Let its index be i
(0-indexed) and its value be largest
. If there are multiple elements with the largest value, pick the smallest i
. \n\t- Find the next largest value in
nums
strictly smaller than largest
. Let its value be nextLargest
. \n\t- Reduce
nums[i]
to nextLargest
. \n
\n\nReturn the number of operations to make all elements in nums
equal.
\n\n
\nExample 1:
\n\n\nInput: nums = [5,1,3]\nOutput: 3\nExplanation: It takes 3 operations to make all elements in nums equal:\n1. largest = 5 at index 0. nextLargest = 3. Reduce nums[0] to 3. nums = [3,1,3].\n2. largest = 3 at index 0. nextLargest = 1. Reduce nums[0] to 1. nums = [1,1,3].\n3. largest = 3 at index 2. nextLargest = 1. Reduce nums[2] to 1. nums = [1,1,1].\n
\n\nExample 2:
\n\n\nInput: nums = [1,1,1]\nOutput: 0\nExplanation: All elements in nums are already equal.\n
\n\nExample 3:
\n\n\nInput: nums = [1,1,2,2,3]\nOutput: 4\nExplanation: It takes 4 operations to make all elements in nums equal:\n1. largest = 3 at index 4. nextLargest = 2. Reduce nums[4] to 2. nums = [1,1,2,2,2].\n2. largest = 2 at index 2. nextLargest = 1. Reduce nums[2] to 1. nums = [1,1,1,2,2].\n3. largest = 2 at index 3. nextLargest = 1. Reduce nums[3] to 1. nums = [1,1,1,1,2].\n4. largest = 2 at index 4. nextLargest = 1. Reduce nums[4] to 1. nums = [1,1,1,1,1].\n
\n\n
\nConstraints:
\n\n\n\t1 <= nums.length <= 5 * 104
\n\t1 <= nums[i] <= 5 * 104
\n
\n", "content_cn": "\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 nums
\uff0c\u4f60\u7684\u76ee\u6807\u662f\u4ee4 nums
\u4e2d\u7684\u6240\u6709\u5143\u7d20\u76f8\u7b49\u3002\u5b8c\u6210\u4e00\u6b21\u51cf\u5c11\u64cd\u4f5c\u9700\u8981\u9075\u7167\u4e0b\u9762\u7684\u51e0\u4e2a\u6b65\u9aa4\uff1a
\n\n\n\t- \u627e\u51fa
nums
\u4e2d\u7684 \u6700\u5927 \u503c\u3002\u8bb0\u8fd9\u4e2a\u503c\u4e3a largest
\u5e76\u53d6\u5176\u4e0b\u6807 i
\uff08\u4e0b\u6807\u4ece 0 \u5f00\u59cb\u8ba1\u6570\uff09\u3002\u5982\u679c\u6709\u591a\u4e2a\u5143\u7d20\u90fd\u662f\u6700\u5927\u503c\uff0c\u5219\u53d6\u6700\u5c0f\u7684 i
\u3002 \n\t- \u627e\u51fa
nums
\u4e2d\u7684 \u4e0b\u4e00\u4e2a\u6700\u5927 \u503c\uff0c\u8fd9\u4e2a\u503c \u4e25\u683c\u5c0f\u4e8e largest
\uff0c\u8bb0\u4e3a nextLargest
\u3002 \n\t- \u5c06
nums[i]
\u51cf\u5c11\u5230 nextLargest
\u3002 \n
\n\n\u8fd4\u56de\u4f7f nums
\u4e2d\u7684\u6240\u6709\u5143\u7d20\u76f8\u7b49\u7684\u64cd\u4f5c\u6b21\u6570\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1anums = [5,1,3]\n\u8f93\u51fa\uff1a3\n\u89e3\u91ca\uff1a\u9700\u8981 3 \u6b21\u64cd\u4f5c\u4f7f nums \u4e2d\u7684\u6240\u6709\u5143\u7d20\u76f8\u7b49\uff1a\n1. largest = 5 \u4e0b\u6807\u4e3a 0 \u3002nextLargest = 3 \u3002\u5c06 nums[0] \u51cf\u5c11\u5230 3 \u3002nums = [3,1,3] \u3002\n2. largest = 3 \u4e0b\u6807\u4e3a 0 \u3002nextLargest = 1 \u3002\u5c06 nums[0] \u51cf\u5c11\u5230 1 \u3002nums = [1,1,3] \u3002\n3. largest = 3 \u4e0b\u6807\u4e3a 2 \u3002nextLargest = 1 \u3002\u5c06 nums[2] \u51cf\u5c11\u5230 1 \u3002nums = [1,1,1] \u3002\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1anums = [1,1,1]\n\u8f93\u51fa\uff1a0\n\u89e3\u91ca\uff1anums \u4e2d\u7684\u6240\u6709\u5143\u7d20\u5df2\u7ecf\u662f\u76f8\u7b49\u7684\u3002\n
\n\n\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1anums = [1,1,2,2,3]\n\u8f93\u51fa\uff1a4\n\u89e3\u91ca\uff1a\u9700\u8981 4 \u6b21\u64cd\u4f5c\u4f7f nums \u4e2d\u7684\u6240\u6709\u5143\u7d20\u76f8\u7b49\uff1a\n1. largest = 3 \u4e0b\u6807\u4e3a 4 \u3002nextLargest = 2 \u3002\u5c06 nums[4] \u51cf\u5c11\u5230 2 \u3002nums = [1,1,2,2,2] \u3002\n2. largest = 2 \u4e0b\u6807\u4e3a 2 \u3002nextLargest = 1 \u3002\u5c06 nums[2] \u51cf\u5c11\u5230 1 \u3002nums = [1,1,1,2,2] \u3002 \n3. largest = 2 \u4e0b\u6807\u4e3a 3 \u3002nextLargest = 1 \u3002\u5c06 nums[3] \u51cf\u5c11\u5230 1 \u3002nums = [1,1,1,1,2] \u3002 \n4. largest = 2 \u4e0b\u6807\u4e3a 4 \u3002nextLargest = 1 \u3002\u5c06 nums[4] \u51cf\u5c11\u5230 1 \u3002nums = [1,1,1,1,1] \u3002\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\t1 <= nums.length <= 5 * 104
\n\t1 <= nums[i] <= 5 * 104
\n
\n", "tags_en": ["Array", "Sorting"], "tags_cn": ["\u6570\u7ec4", "\u6392\u5e8f"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n int reductionOperations(vector& nums) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int reductionOperations(int[] nums) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def reductionOperations(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def reductionOperations(self, nums: List[int]) -> int:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nint reductionOperations(int* nums, int numsSize){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int ReductionOperations(int[] nums) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[]} nums\n * @return {number}\n */\nvar reductionOperations = function(nums) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer[]} nums\n# @return {Integer}\ndef reduction_operations(nums)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func reductionOperations(_ nums: [Int]) -> Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func reductionOperations(nums []int) int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def reductionOperations(nums: Array[Int]): Int = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun reductionOperations(nums: IntArray): Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn reduction_operations(nums: Vec) -> i32 {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @return Integer\n */\n function reductionOperations($nums) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function reductionOperations(nums: number[]): number {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (reduction-operations nums)\n (-> (listof exact-integer?) exact-integer?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec reduction_operations(Nums :: [integer()]) -> integer().\nreduction_operations(Nums) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec reduction_operations(nums :: [integer]) :: integer\n def reduction_operations(nums) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1887](https://leetcode-cn.com/problems/reduction-operations-to-make-the-array-elements-equal)", "[\u4f7f\u6570\u7ec4\u5143\u7d20\u76f8\u7b49\u7684\u51cf\u5c11\u64cd\u4f5c\u6b21\u6570](/solution/1800-1899/1887.Reduction%20Operations%20to%20Make%20the%20Array%20Elements%20Equal/README.md)", "`\u6570\u7ec4`,`\u6392\u5e8f`", "\u4e2d\u7b49", ""], "md_table_row_en": ["[1887](https://leetcode.com/problems/reduction-operations-to-make-the-array-elements-equal)", "[Reduction Operations to Make the Array Elements Equal](/solution/1800-1899/1887.Reduction%20Operations%20to%20Make%20the%20Array%20Elements%20Equal/README_EN.md)", "`Array`,`Sorting`", "Medium", ""]}, {"question_id": "2015", "frontend_question_id": "1886", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/determine-whether-matrix-can-be-obtained-by-rotation", "url_en": "https://leetcode.com/problems/determine-whether-matrix-can-be-obtained-by-rotation", "relative_path_cn": "/solution/1800-1899/1886.Determine%20Whether%20Matrix%20Can%20Be%20Obtained%20By%20Rotation/README.md", "relative_path_en": "/solution/1800-1899/1886.Determine%20Whether%20Matrix%20Can%20Be%20Obtained%20By%20Rotation/README_EN.md", "title_cn": "\u5224\u65ad\u77e9\u9635\u7ecf\u8f6e\u8f6c\u540e\u662f\u5426\u4e00\u81f4", "title_en": "Determine Whether Matrix Can Be Obtained By Rotation", "question_title_slug": "determine-whether-matrix-can-be-obtained-by-rotation", "content_en": "Given two n x n
binary matrices mat
and target
, return true
if it is possible to make mat
equal to target
by rotating mat
in 90-degree increments, or false
otherwise.
\n\n
\nExample 1:
\n
\n\nInput: mat = [[0,1],[1,0]], target = [[1,0],[0,1]]\nOutput: true\nExplanation: We can rotate mat 90 degrees clockwise to make mat equal target.\n
\n\nExample 2:
\n
\n\nInput: mat = [[0,1],[1,1]], target = [[1,0],[0,1]]\nOutput: false\nExplanation: It is impossible to make mat equal to target by rotating mat.\n
\n\nExample 3:
\n
\n\nInput: mat = [[0,0,0],[0,1,0],[1,1,1]], target = [[1,1,1],[0,1,0],[0,0,0]]\nOutput: true\nExplanation: We can rotate mat 90 degrees clockwise two times to make mat equal target.\n
\n\n
\nConstraints:
\n\n\n\tn == mat.length == target.length
\n\tn == mat[i].length == target[i].length
\n\t1 <= n <= 10
\n\tmat[i][j]
and target[i][j]
are either 0
or 1
. \n
\n", "content_cn": "\u7ed9\u4f60\u4e24\u4e2a\u5927\u5c0f\u4e3a n x n
\u7684\u4e8c\u8fdb\u5236\u77e9\u9635 mat
\u548c target
\u3002\u73b0 \u4ee5 90 \u5ea6\u987a\u65f6\u9488\u8f6e\u8f6c \u77e9\u9635 mat
\u4e2d\u7684\u5143\u7d20 \u82e5\u5e72\u6b21 \uff0c\u5982\u679c\u80fd\u591f\u4f7f mat
\u4e0e\u00a0target
\u4e00\u81f4\uff0c\u8fd4\u56de true
\uff1b\u5426\u5219\uff0c\u8fd4\u56de false
\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n
\n\n\u8f93\u5165\uff1amat = [[0,1],[1,0]], target = [[1,0],[0,1]]\n\u8f93\u51fa\uff1atrue\n\u89e3\u91ca\uff1a\u987a\u65f6\u9488\u8f6e\u8f6c 90 \u5ea6\u4e00\u6b21\u53ef\u4ee5\u4f7f mat \u548c target \u4e00\u81f4\u3002\n
\n\n\u793a\u4f8b 2\uff1a
\n
\n\n\u8f93\u5165\uff1amat = [[0,1],[1,1]], target = [[1,0],[0,1]]\n\u8f93\u51fa\uff1afalse\n\u89e3\u91ca\uff1a\u65e0\u6cd5\u901a\u8fc7\u8f6e\u8f6c\u77e9\u9635\u4e2d\u7684\u5143\u7d20\u4f7f equal \u4e0e target \u4e00\u81f4\u3002\n
\n\n\u793a\u4f8b 3\uff1a
\n
\n\n\u8f93\u5165\uff1amat = [[0,0,0],[0,1,0],[1,1,1]], target = [[1,1,1],[0,1,0],[0,0,0]]\n\u8f93\u51fa\uff1atrue\n\u89e3\u91ca\uff1a\u987a\u65f6\u9488\u8f6e\u8f6c 90 \u5ea6\u4e24\u6b21\u53ef\u4ee5\u4f7f mat \u548c target \u4e00\u81f4\u3002\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\tn == mat.length == target.length
\n\tn == mat[i].length == target[i].length
\n\t1 <= n <= 10
\n\tmat[i][j]
\u548c target[i][j]
\u4e0d\u662f 0
\u5c31\u662f 1
\n
\n", "tags_en": ["Array", "Matrix"], "tags_cn": ["\u6570\u7ec4", "\u77e9\u9635"], "difficulty_en": "Easy", "difficulty_cn": "\u7b80\u5355", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n bool findRotation(vector>& mat, vector>& target) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public boolean findRotation(int[][] mat, int[][] target) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def findRotation(self, mat, target):\n \"\"\"\n :type mat: List[List[int]]\n :type target: List[List[int]]\n :rtype: bool\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nbool findRotation(int** mat, int matSize, int* matColSize, int** target, int targetSize, int* targetColSize){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public bool FindRotation(int[][] mat, int[][] target) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[][]} mat\n * @param {number[][]} target\n * @return {boolean}\n */\nvar findRotation = function(mat, target) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer[][]} mat\n# @param {Integer[][]} target\n# @return {Boolean}\ndef find_rotation(mat, target)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func findRotation(_ mat: [[Int]], _ target: [[Int]]) -> Bool {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func findRotation(mat [][]int, target [][]int) bool {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def findRotation(mat: Array[Array[Int]], target: Array[Array[Int]]): Boolean = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun findRotation(mat: Array, target: Array): Boolean {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn find_rotation(mat: Vec>, target: Vec>) -> bool {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer[][] $mat\n * @param Integer[][] $target\n * @return Boolean\n */\n function findRotation($mat, $target) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function findRotation(mat: number[][], target: number[][]): boolean {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (find-rotation mat target)\n (-> (listof (listof exact-integer?)) (listof (listof exact-integer?)) boolean?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec find_rotation(Mat :: [[integer()]], Target :: [[integer()]]) -> boolean().\nfind_rotation(Mat, Target) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec find_rotation(mat :: [[integer]], target :: [[integer]]) :: boolean\n def find_rotation(mat, target) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1886](https://leetcode-cn.com/problems/determine-whether-matrix-can-be-obtained-by-rotation)", "[\u5224\u65ad\u77e9\u9635\u7ecf\u8f6e\u8f6c\u540e\u662f\u5426\u4e00\u81f4](/solution/1800-1899/1886.Determine%20Whether%20Matrix%20Can%20Be%20Obtained%20By%20Rotation/README.md)", "`\u6570\u7ec4`,`\u77e9\u9635`", "\u7b80\u5355", ""], "md_table_row_en": ["[1886](https://leetcode.com/problems/determine-whether-matrix-can-be-obtained-by-rotation)", "[Determine Whether Matrix Can Be Obtained By Rotation](/solution/1800-1899/1886.Determine%20Whether%20Matrix%20Can%20Be%20Obtained%20By%20Rotation/README_EN.md)", "`Array`,`Matrix`", "Easy", ""]}, {"question_id": "2014", "frontend_question_id": "1867", "paid_only": true, "paid_only_cn": true, "category": "Database", "url_cn": "https://leetcode-cn.com/problems/orders-with-maximum-quantity-above-average", "url_en": "https://leetcode.com/problems/orders-with-maximum-quantity-above-average", "relative_path_cn": "/solution/1800-1899/1867.Orders%20With%20Maximum%20Quantity%20Above%20Average/README.md", "relative_path_en": "/solution/1800-1899/1867.Orders%20With%20Maximum%20Quantity%20Above%20Average/README_EN.md", "title_cn": "Orders With Maximum Quantity Above Average", "title_en": "Orders With Maximum Quantity Above Average", "question_title_slug": "orders-with-maximum-quantity-above-average", "content_en": "Table: OrdersDetails
\n\n\n+-------------+------+\n| Column Name | Type |\n+-------------+------+\n| order_id | int |\n| product_id | int |\n| quantity | int |\n+-------------+------+\n(order_id, product_id) is the primary key for this table.\nA single order is represented as multiple rows, one row for each product in the order.\nEach row of this table contains the quantity ordered of the product product_id in the order order_id.\n
\n\n
\n\nYou are running an ecommerce site that is looking for imbalanced orders. An imbalanced order is one whose maximum quantity is strictly greater than the average quantity of every order (including itself).
\n\nThe average quantity of an order is calculated as (total quantity of all products in the order) / (number of different products in the order)
. The maximum quantity of an order is the highest quantity
of any single product in the order.
\n\nWrite an SQL query to find the order_id
of all imbalanced orders.
\n\nReturn the result table in any order.
\n\nThe query result format is in the following example:
\n\n
\n\n\nOrdersDetails table:\n+----------+------------+----------+\n| order_id | product_id | quantity |\n+----------+------------+----------+\n| 1 | 1 | 12 |\n| 1 | 2 | 10 |\n| 1 | 3 | 15 |\n| 2 | 1 | 8 |\n| 2 | 4 | 4 |\n| 2 | 5 | 6 |\n| 3 | 3 | 5 |\n| 3 | 4 | 18 |\n| 4 | 5 | 2 |\n| 4 | 6 | 8 |\n| 5 | 7 | 9 |\n| 5 | 8 | 9 |\n| 3 | 9 | 20 |\n| 2 | 9 | 4 |\n+----------+------------+----------+\n\nResult table:\n+----------+\n| order_id |\n+----------+\n| 1 |\n| 3 |\n+----------+\n\nThe average quantity of each order is:\n- order_id=1: (12+10+15)/3 = 12.3333333\n- order_id=2: (8+4+6+4)/4 = 5.5\n- order_id=3: (5+18+20)/3 = 14.333333\n- order_id=4: (2+8)/2 = 5\n- order_id=5: (9+9)/2 = 9\n\nThe maximum quantity of each order is:\n- order_id=1: max(12, 10, 15) = 15\n- order_id=2: max(8, 4, 6, 4) = 8\n- order_id=3: max(5, 18, 20) = 20\n- order_id=4: max(2, 8) = 8\n- order_id=5: max(9, 9) = 9\n\nOrders 1 and 3 are imbalanced because they have a maximum quantity that exceeds the average quantity of every order.\n
\n", "content_cn": "Table: OrdersDetails
\n\n\n+-------------+------+\n| Column Name | Type |\n+-------------+------+\n| order_id | int |\n| product_id | int |\n| quantity | int |\n+-------------+------+\n(order_id, product_id) is the primary key for this table.\nA single order is represented as multiple rows, one row for each product in the order.\nEach row of this table contains the quantity ordered of the product product_id in the order order_id.\n
\n\n
\n\nYou are running an ecommerce site that is looking for imbalanced orders. An imbalanced order is one whose maximum quantity is strictly greater than the average quantity of every order (including itself).
\n\nThe average quantity of an order is calculated as (total quantity of all products in the order) / (number of different products in the order)
. The maximum quantity of an order is the highest quantity
of any single product in the order.
\n\nWrite an SQL query to find the order_id
of all imbalanced orders.
\n\nReturn the result table in any order.
\n\nThe query result format is in the following example:
\n\n
\n\n\nOrdersDetails table:\n+----------+------------+----------+\n| order_id | product_id | quantity |\n+----------+------------+----------+\n| 1 | 1 | 12 |\n| 1 | 2 | 10 |\n| 1 | 3 | 15 |\n| 2 | 1 | 8 |\n| 2 | 4 | 4 |\n| 2 | 5 | 6 |\n| 3 | 3 | 5 |\n| 3 | 4 | 18 |\n| 4 | 5 | 2 |\n| 4 | 6 | 8 |\n| 5 | 7 | 9 |\n| 5 | 8 | 9 |\n| 3 | 9 | 20 |\n| 2 | 9 | 4 |\n+----------+------------+----------+\n\nResult table:\n+----------+\n| order_id |\n+----------+\n| 1 |\n| 3 |\n+----------+\n\nThe average quantity of each order is:\n- order_id=1: (12+10+15)/3 = 12.3333333\n- order_id=2: (8+4+6+4)/4 = 5.5\n- order_id=3: (5+18+20)/3 = 14.333333\n- order_id=4: (2+8)/2 = 5\n- order_id=5: (9+9)/2 = 9\n\nThe maximum quantity of each order is:\n- order_id=1: max(12, 10, 15) = 15\n- order_id=2: max(8, 4, 6, 4) = 8\n- order_id=3: max(5, 18, 20) = 20\n- order_id=4: max(2, 8) = 8\n- order_id=5: max(9, 9) = 9\n\nOrders 1 and 3 are imbalanced because they have a maximum quantity that exceeds the average quantity of every order.\n
\n", "tags_en": ["Database"], "tags_cn": ["\u6570\u636e\u5e93"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "MySQL", "langSlug": "mysql", "code": "# Write your MySQL query statement below", "__typename": "CodeSnippetNode"}, {"lang": "MS SQL Server", "langSlug": "mssql", "code": "/* Write your T-SQL query statement below */", "__typename": "CodeSnippetNode"}, {"lang": "Oracle", "langSlug": "oraclesql", "code": "/* Write your PL/SQL query statement below */", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1867](https://leetcode-cn.com/problems/orders-with-maximum-quantity-above-average)", "[Orders With Maximum Quantity Above Average](/solution/1800-1899/1867.Orders%20With%20Maximum%20Quantity%20Above%20Average/README.md)", "`\u6570\u636e\u5e93`", "\u4e2d\u7b49", "\ud83d\udd12"], "md_table_row_en": ["[1867](https://leetcode.com/problems/orders-with-maximum-quantity-above-average)", "[Orders With Maximum Quantity Above Average](/solution/1800-1899/1867.Orders%20With%20Maximum%20Quantity%20Above%20Average/README_EN.md)", "`Database`", "Medium", "\ud83d\udd12"]}, {"question_id": "2013", "frontend_question_id": "1883", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/minimum-skips-to-arrive-at-meeting-on-time", "url_en": "https://leetcode.com/problems/minimum-skips-to-arrive-at-meeting-on-time", "relative_path_cn": "/solution/1800-1899/1883.Minimum%20Skips%20to%20Arrive%20at%20Meeting%20On%20Time/README.md", "relative_path_en": "/solution/1800-1899/1883.Minimum%20Skips%20to%20Arrive%20at%20Meeting%20On%20Time/README_EN.md", "title_cn": "\u51c6\u65f6\u62b5\u8fbe\u4f1a\u8bae\u73b0\u573a\u7684\u6700\u5c0f\u8df3\u8fc7\u4f11\u606f\u6b21\u6570", "title_en": "Minimum Skips to Arrive at Meeting On Time", "question_title_slug": "minimum-skips-to-arrive-at-meeting-on-time", "content_en": "You are given an integer hoursBefore
, the number of hours you have to travel to your meeting. To arrive at your meeting, you have to travel through n
roads. The road lengths are given as an integer array dist
of length n
, where dist[i]
describes the length of the ith
road in kilometers. In addition, you are given an integer speed
, which is the speed (in km/h) you will travel at.
\n\nAfter you travel road i
, you must rest and wait for the next integer hour before you can begin traveling on the next road. Note that you do not have to rest after traveling the last road because you are already at the meeting.
\n\n\n\t- For example, if traveling a road takes
1.4
hours, you must wait until the 2
hour mark before traveling the next road. If traveling a road takes exactly 2
hours, you do not need to wait. \n
\n\nHowever, you are allowed to skip some rests to be able to arrive on time, meaning you do not need to wait for the next integer hour. Note that this means you may finish traveling future roads at different hour marks.
\n\n\n\t- For example, suppose traveling the first road takes
1.4
hours and traveling the second road takes 0.6
hours. Skipping the rest after the first road will mean you finish traveling the second road right at the 2
hour mark, letting you start traveling the third road immediately. \n
\n\nReturn the minimum number of skips required to arrive at the meeting on time, or -1
if it is impossible.
\n\n
\nExample 1:
\n\n\nInput: dist = [1,3,2], speed = 4, hoursBefore = 2\nOutput: 1\nExplanation:\nWithout skipping any rests, you will arrive in (1/4 + 3/4) + (3/4 + 1/4) + (2/4) = 2.5 hours.\nYou can skip the first rest to arrive in ((1/4 + 0) + (3/4 + 0)) + (2/4) = 1.5 hours.\nNote that the second rest is shortened because you finish traveling the second road at an integer hour due to skipping the first rest.\n
\n\nExample 2:
\n\n\nInput: dist = [7,3,5,5], speed = 2, hoursBefore = 10\nOutput: 2\nExplanation:\nWithout skipping any rests, you will arrive in (7/2 + 1/2) + (3/2 + 1/2) + (5/2 + 1/2) + (5/2) = 11.5 hours.\nYou can skip the first and third rest to arrive in ((7/2 + 0) + (3/2 + 0)) + ((5/2 + 0) + (5/2)) = 10 hours.\n
\n\nExample 3:
\n\n\nInput: dist = [7,3,5,5], speed = 1, hoursBefore = 10\nOutput: -1\nExplanation: It is impossible to arrive at the meeting on time even if you skip all the rests.\n
\n\n
\nConstraints:
\n\n\n\tn == dist.length
\n\t1 <= n <= 1000
\n\t1 <= dist[i] <= 105
\n\t1 <= speed <= 106
\n\t1 <= hoursBefore <= 107
\n
\n", "content_cn": "\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570 hoursBefore
\uff0c\u8868\u793a\u4f60\u8981\u524d\u5f80\u4f1a\u8bae\u6240\u5269\u4e0b\u7684\u53ef\u7528\u5c0f\u65f6\u6570\u3002\u8981\u60f3\u6210\u529f\u62b5\u8fbe\u4f1a\u8bae\u73b0\u573a\uff0c\u4f60\u5fc5\u987b\u9014\u7ecf n
\u6761\u9053\u8def\u3002\u9053\u8def\u7684\u957f\u5ea6\u7528\u4e00\u4e2a\u957f\u5ea6\u4e3a n
\u7684\u6574\u6570\u6570\u7ec4 dist
\u8868\u793a\uff0c\u5176\u4e2d dist[i]
\u8868\u793a\u7b2c i
\u6761\u9053\u8def\u7684\u957f\u5ea6\uff08\u5355\u4f4d\uff1a\u5343\u7c73\uff09\u3002\u53e6\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570 speed
\uff0c\u8868\u793a\u4f60\u5728\u9053\u8def\u4e0a\u524d\u8fdb\u7684\u901f\u5ea6\uff08\u5355\u4f4d\uff1a\u5343\u7c73\u6bcf\u5c0f\u65f6\uff09\u3002
\n\n\u5f53\u4f60\u901a\u8fc7\u7b2c i
\u6761\u8def\u4e4b\u540e\uff0c\u5c31\u5fc5\u987b\u4f11\u606f\u5e76\u7b49\u5f85\uff0c\u76f4\u5230 \u4e0b\u4e00\u4e2a\u6574\u6570\u5c0f\u65f6 \u624d\u80fd\u5f00\u59cb\u7ee7\u7eed\u901a\u8fc7\u4e0b\u4e00\u6761\u9053\u8def\u3002\u6ce8\u610f\uff1a\u4f60\u4e0d\u9700\u8981\u5728\u901a\u8fc7\u6700\u540e\u4e00\u6761\u9053\u8def\u540e\u4f11\u606f\uff0c\u56e0\u4e3a\u90a3\u65f6\u4f60\u5df2\u7ecf\u62b5\u8fbe\u4f1a\u8bae\u73b0\u573a\u3002
\n\n\n\t- \u4f8b\u5982\uff0c\u5982\u679c\u4f60\u901a\u8fc7\u4e00\u6761\u9053\u8def\u7528\u53bb
1.4
\u5c0f\u65f6\uff0c\u90a3\u4f60\u5fc5\u987b\u505c\u4e0b\u6765\u7b49\u5f85\uff0c\u5230\u00a02
\u5c0f\u65f6\u624d\u53ef\u4ee5\u7ee7\u7eed\u901a\u8fc7\u4e0b\u4e00\u6761\u9053\u8def\u3002\u5982\u679c\u901a\u8fc7\u4e00\u6761\u9053\u8def\u6070\u597d\u7528\u53bb 2
\u5c0f\u65f6\uff0c\u5c31\u65e0\u9700\u7b49\u5f85\uff0c\u53ef\u4ee5\u76f4\u63a5\u7ee7\u7eed\u3002 \n
\n\n\u7136\u800c\uff0c\u4e3a\u4e86\u80fd\u51c6\u65f6\u5230\u8fbe\uff0c\u4f60\u53ef\u4ee5\u9009\u62e9 \u8df3\u8fc7 \u4e00\u4e9b\u8def\u7684\u4f11\u606f\u65f6\u95f4\uff0c\u8fd9\u610f\u5473\u7740\u4f60\u4e0d\u5fc5\u7b49\u5f85\u4e0b\u4e00\u4e2a\u6574\u6570\u5c0f\u65f6\u3002\u6ce8\u610f\uff0c\u8fd9\u610f\u5473\u7740\u4e0e\u4e0d\u8df3\u8fc7\u4efb\u4f55\u4f11\u606f\u65f6\u95f4\u76f8\u6bd4\uff0c\u4f60\u53ef\u80fd\u5728\u4e0d\u540c\u65f6\u523b\u5230\u8fbe\u63a5\u4e0b\u6765\u7684\u9053\u8def\u3002
\n\n\n\t- \u4f8b\u5982\uff0c\u5047\u8bbe\u901a\u8fc7\u7b2c
1
\u6761\u9053\u8def\u7528\u53bb 1.4
\u5c0f\u65f6\uff0c\u4e14\u901a\u8fc7\u7b2c 2
\u6761\u9053\u8def\u7528\u53bb 0.6
\u5c0f\u65f6\u3002\u8df3\u8fc7\u7b2c 1
\u6761\u9053\u8def\u7684\u4f11\u606f\u65f6\u95f4\u610f\u5473\u7740\u4f60\u5c06\u4f1a\u5728\u6070\u597d\u00a02
\u5c0f\u65f6\u5b8c\u6210\u901a\u8fc7\u7b2c 2
\u6761\u9053\u8def\uff0c\u4e14\u4f60\u80fd\u591f\u7acb\u5373\u5f00\u59cb\u901a\u8fc7\u7b2c 3
\u6761\u9053\u8def\u3002 \n
\n\n\u8fd4\u56de\u51c6\u65f6\u62b5\u8fbe\u4f1a\u8bae\u73b0\u573a\u6240\u9700\u8981\u7684 \u6700\u5c0f\u8df3\u8fc7\u6b21\u6570 \uff0c\u5982\u679c \u65e0\u6cd5\u51c6\u65f6\u53c2\u4f1a \uff0c\u8fd4\u56de -1
\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1adist = [1,3,2], speed = 4, hoursBefore = 2\n\u8f93\u51fa\uff1a1\n\u89e3\u91ca\uff1a\n\u4e0d\u8df3\u8fc7\u4efb\u4f55\u4f11\u606f\u65f6\u95f4\uff0c\u4f60\u5c06\u7528 (1/4 + 3/4) + (3/4 + 1/4) + (2/4) = 2.5 \u5c0f\u65f6\u624d\u80fd\u62b5\u8fbe\u4f1a\u8bae\u73b0\u573a\u3002\n\u53ef\u4ee5\u8df3\u8fc7\u7b2c 1 \u6b21\u4f11\u606f\u65f6\u95f4\uff0c\u5171\u7528 ((1/4 + 0) + (3/4 + 0)) + (2/4) = 1.5 \u5c0f\u65f6\u62b5\u8fbe\u4f1a\u8bae\u73b0\u573a\u3002\n\u6ce8\u610f\uff0c\u7b2c 2 \u6b21\u4f11\u606f\u65f6\u95f4\u7f29\u77ed\u4e3a 0 \uff0c\u7531\u4e8e\u8df3\u8fc7\u7b2c 1 \u6b21\u4f11\u606f\u65f6\u95f4\uff0c\u4f60\u662f\u5728\u6574\u6570\u5c0f\u65f6\u5904\u5b8c\u6210\u901a\u8fc7\u7b2c 2 \u6761\u9053\u8def\u3002\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1adist = [7,3,5,5], speed = 2, hoursBefore = 10\n\u8f93\u51fa\uff1a2\n\u89e3\u91ca\uff1a\n\u4e0d\u8df3\u8fc7\u4efb\u4f55\u4f11\u606f\u65f6\u95f4\uff0c\u4f60\u5c06\u7528 (7/2 + 1/2) + (3/2 + 1/2) + (5/2 + 1/2) + (5/2) = 11.5 \u5c0f\u65f6\u624d\u80fd\u62b5\u8fbe\u4f1a\u8bae\u73b0\u573a\u3002\n\u53ef\u4ee5\u8df3\u8fc7\u7b2c 1 \u6b21\u548c\u7b2c 3 \u6b21\u4f11\u606f\u65f6\u95f4\uff0c\u5171\u7528 ((7/2 + 0) + (3/2 + 0)) + ((5/2 + 0) + (5/2)) = 10 \u5c0f\u65f6\u62b5\u8fbe\u4f1a\u8bae\u73b0\u573a\u3002\n
\n\n\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1adist = [7,3,5,5], speed = 1, hoursBefore = 10\n\u8f93\u51fa\uff1a-1\n\u89e3\u91ca\uff1a\u5373\u4f7f\u8df3\u8fc7\u6240\u6709\u7684\u4f11\u606f\u65f6\u95f4\uff0c\u4e5f\u65e0\u6cd5\u51c6\u65f6\u53c2\u52a0\u4f1a\u8bae\u3002\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\tn == dist.length
\n\t1 <= n <= 1000
\n\t1 <= dist[i] <= 105
\n\t1 <= speed <= 106
\n\t1 <= hoursBefore <= 107
\n
\n", "tags_en": ["Array", "Dynamic Programming"], "tags_cn": ["\u6570\u7ec4", "\u52a8\u6001\u89c4\u5212"], "difficulty_en": "Hard", "difficulty_cn": "\u56f0\u96be", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n int minSkips(vector& dist, int speed, int hoursBefore) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int minSkips(int[] dist, int speed, int hoursBefore) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def minSkips(self, dist, speed, hoursBefore):\n \"\"\"\n :type dist: List[int]\n :type speed: int\n :type hoursBefore: int\n :rtype: int\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def minSkips(self, dist: List[int], speed: int, hoursBefore: int) -> int:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nint minSkips(int* dist, int distSize, int speed, int hoursBefore){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int MinSkips(int[] dist, int speed, int hoursBefore) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[]} dist\n * @param {number} speed\n * @param {number} hoursBefore\n * @return {number}\n */\nvar minSkips = function(dist, speed, hoursBefore) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer[]} dist\n# @param {Integer} speed\n# @param {Integer} hours_before\n# @return {Integer}\ndef min_skips(dist, speed, hours_before)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func minSkips(_ dist: [Int], _ speed: Int, _ hoursBefore: Int) -> Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func minSkips(dist []int, speed int, hoursBefore int) int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def minSkips(dist: Array[Int], speed: Int, hoursBefore: Int): Int = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun minSkips(dist: IntArray, speed: Int, hoursBefore: Int): Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn min_skips(dist: Vec, speed: i32, hours_before: i32) -> i32 {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer[] $dist\n * @param Integer $speed\n * @param Integer $hoursBefore\n * @return Integer\n */\n function minSkips($dist, $speed, $hoursBefore) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function minSkips(dist: number[], speed: number, hoursBefore: number): number {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (min-skips dist speed hoursBefore)\n (-> (listof exact-integer?) exact-integer? exact-integer? exact-integer?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec min_skips(Dist :: [integer()], Speed :: integer(), HoursBefore :: integer()) -> integer().\nmin_skips(Dist, Speed, HoursBefore) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec min_skips(dist :: [integer], speed :: integer, hours_before :: integer) :: integer\n def min_skips(dist, speed, hours_before) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1883](https://leetcode-cn.com/problems/minimum-skips-to-arrive-at-meeting-on-time)", "[\u51c6\u65f6\u62b5\u8fbe\u4f1a\u8bae\u73b0\u573a\u7684\u6700\u5c0f\u8df3\u8fc7\u4f11\u606f\u6b21\u6570](/solution/1800-1899/1883.Minimum%20Skips%20to%20Arrive%20at%20Meeting%20On%20Time/README.md)", "`\u6570\u7ec4`,`\u52a8\u6001\u89c4\u5212`", "\u56f0\u96be", ""], "md_table_row_en": ["[1883](https://leetcode.com/problems/minimum-skips-to-arrive-at-meeting-on-time)", "[Minimum Skips to Arrive at Meeting On Time](/solution/1800-1899/1883.Minimum%20Skips%20to%20Arrive%20at%20Meeting%20On%20Time/README_EN.md)", "`Array`,`Dynamic Programming`", "Hard", ""]}, {"question_id": "2012", "frontend_question_id": "1882", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/process-tasks-using-servers", "url_en": "https://leetcode.com/problems/process-tasks-using-servers", "relative_path_cn": "/solution/1800-1899/1882.Process%20Tasks%20Using%20Servers/README.md", "relative_path_en": "/solution/1800-1899/1882.Process%20Tasks%20Using%20Servers/README_EN.md", "title_cn": "\u4f7f\u7528\u670d\u52a1\u5668\u5904\u7406\u4efb\u52a1", "title_en": "Process Tasks Using Servers", "question_title_slug": "process-tasks-using-servers", "content_en": "You are given two 0-indexed integer arrays servers
and tasks
of lengths n
\u200b\u200b\u200b\u200b\u200b\u200b and m
\u200b\u200b\u200b\u200b\u200b\u200b respectively. servers[i]
is the weight of the i\u200b\u200b\u200b\u200b\u200b\u200bth
\u200b\u200b\u200b\u200b server, and tasks[j]
is the time needed to process the j\u200b\u200b\u200b\u200b\u200b\u200bth
\u200b\u200b\u200b\u200b task in seconds.
\n\nTasks are assigned to the servers using a task queue. Initially, all servers are free, and the queue is empty.
\n\nAt second j
, the jth
task is inserted into the queue (starting with the 0th
task being inserted at second 0
). As long as there are free servers and the queue is not empty, the task in the front of the queue will be assigned to a free server with the smallest weight, and in case of a tie, it is assigned to a free server with the smallest index.
\n\nIf there are no free servers and the queue is not empty, we wait until a server becomes free and immediately assign the next task. If multiple servers become free at the same time, then multiple tasks from the queue will be assigned in order of insertion following the weight and index priorities above.
\n\nA server that is assigned task j
at second t
will be free again at second t + tasks[j]
.
\n\nBuild an array ans
\u200b\u200b\u200b\u200b of length m
, where ans[j]
is the index of the server the j\u200b\u200b\u200b\u200b\u200b\u200bth
task will be assigned to.
\n\nReturn the array ans
\u200b\u200b\u200b\u200b.
\n\n
\nExample 1:
\n\n\nInput: servers = [3,3,2], tasks = [1,2,3,2,1,2]\nOutput: [2,2,0,2,1,2]\nExplanation: Events in chronological order go as follows:\n- At second 0, task 0 is added and processed using server 2 until second 1.\n- At second 1, server 2 becomes free. Task 1 is added and processed using server 2 until second 3.\n- At second 2, task 2 is added and processed using server 0 until second 5.\n- At second 3, server 2 becomes free. Task 3 is added and processed using server 2 until second 5.\n- At second 4, task 4 is added and processed using server 1 until second 5.\n- At second 5, all servers become free. Task 5 is added and processed using server 2 until second 7.
\n\nExample 2:
\n\n\nInput: servers = [5,1,4,3,2], tasks = [2,1,2,4,5,2,1]\nOutput: [1,4,1,4,1,3,2]\nExplanation: Events in chronological order go as follows: \n- At second 0, task 0 is added and processed using server 1 until second 2.\n- At second 1, task 1 is added and processed using server 4 until second 2.\n- At second 2, servers 1 and 4 become free. Task 2 is added and processed using server 1 until second 4. \n- At second 3, task 3 is added and processed using server 4 until second 7.\n- At second 4, server 1 becomes free. Task 4 is added and processed using server 1 until second 9. \n- At second 5, task 5 is added and processed using server 3 until second 7.\n- At second 6, task 6 is added and processed using server 2 until second 7.\n
\n\n
\nConstraints:
\n\n\n\tservers.length == n
\n\ttasks.length == m
\n\t1 <= n, m <= 2 * 105
\n\t1 <= servers[i], tasks[j] <= 2 * 105
\n
\n", "content_cn": "\u7ed9\u4f60\u4e24\u4e2a \u4e0b\u6807\u4ece 0 \u5f00\u59cb \u7684\u6574\u6570\u6570\u7ec4 servers
\u548c tasks
\uff0c\u957f\u5ea6\u5206\u522b\u4e3a n
\u200b\u200b\u200b\u200b\u200b\u200b \u548c m
\u200b\u200b\u200b\u200b\u200b\u200b \u3002servers[i]
\u662f\u7b2c i\u200b\u200b\u200b\u200b\u200b\u200b
\u200b\u200b\u200b\u200b \u53f0\u670d\u52a1\u5668\u7684 \u6743\u91cd \uff0c\u800c tasks[j]
\u662f\u5904\u7406\u7b2c j\u200b\u200b\u200b\u200b\u200b\u200b
\u9879\u4efb\u52a1 \u6240\u9700\u8981\u7684\u65f6\u95f4\uff08\u5355\u4f4d\uff1a\u79d2\uff09\u3002
\n\n\u4f60\u6b63\u5728\u8fd0\u884c\u4e00\u4e2a\u4eff\u771f\u7cfb\u7edf\uff0c\u5728\u5904\u7406\u5b8c\u6240\u6709\u4efb\u52a1\u540e\uff0c\u8be5\u7cfb\u7edf\u5c06\u4f1a\u5173\u95ed\u3002\u6bcf\u53f0\u670d\u52a1\u5668\u53ea\u80fd\u540c\u65f6\u5904\u7406\u4e00\u9879\u4efb\u52a1\u3002\u7b2c 0
\u9879\u4efb\u52a1\u5728\u7b2c 0
\u79d2\u53ef\u4ee5\u5f00\u59cb\u5904\u7406\uff0c\u76f8\u5e94\u5730\uff0c\u7b2c j
\u9879\u4efb\u52a1\u5728\u7b2c j
\u00a0\u79d2\u53ef\u4ee5\u5f00\u59cb\u5904\u7406\u3002\u5904\u7406\u7b2c j
\u9879\u4efb\u52a1\u65f6\uff0c\u4f60\u9700\u8981\u4e3a\u5b83\u5206\u914d\u4e00\u53f0 \u6743\u91cd\u6700\u5c0f \u7684\u7a7a\u95f2\u670d\u52a1\u5668\u3002\u5982\u679c\u5b58\u5728\u591a\u53f0\u76f8\u540c\u6743\u91cd\u7684\u7a7a\u95f2\u670d\u52a1\u5668\uff0c\u8bf7\u9009\u62e9 \u4e0b\u6807\u6700\u5c0f \u7684\u670d\u52a1\u5668\u3002\u5982\u679c\u4e00\u53f0\u7a7a\u95f2\u670d\u52a1\u5668\u5728\u7b2c t
\u79d2\u5206\u914d\u5230\u7b2c j
\u9879\u4efb\u52a1\uff0c\u90a3\u4e48\u5728 t + tasks[j]
\u65f6\u5b83\u5c06\u6062\u590d\u7a7a\u95f2\u72b6\u6001\u3002
\n\n\u5982\u679c\u6ca1\u6709\u7a7a\u95f2\u670d\u52a1\u5668\uff0c\u5219\u5fc5\u987b\u7b49\u5f85\uff0c\u76f4\u5230\u51fa\u73b0\u4e00\u53f0\u7a7a\u95f2\u670d\u52a1\u5668\uff0c\u5e76 \u5c3d\u53ef\u80fd\u65e9\u00a0\u5730\u5904\u7406\u5269\u4f59\u4efb\u52a1\u3002 \u5982\u679c\u6709\u591a\u9879\u4efb\u52a1\u7b49\u5f85\u5206\u914d\uff0c\u5219\u6309\u7167 \u4e0b\u6807\u9012\u589e \u7684\u987a\u5e8f\u5b8c\u6210\u5206\u914d\u3002
\n\n\u5982\u679c\u540c\u4e00\u65f6\u523b\u5b58\u5728\u591a\u53f0\u7a7a\u95f2\u670d\u52a1\u5668\uff0c\u53ef\u4ee5\u540c\u65f6\u5c06\u591a\u9879\u4efb\u52a1\u5206\u522b\u5206\u914d\u7ed9\u5b83\u4eec\u3002
\n\n\u6784\u5efa\u957f\u5ea6\u4e3a\u00a0m
\u7684\u7b54\u6848\u6570\u7ec4 ans
\uff0c\u5176\u4e2d ans[j]
\u662f\u7b2c j
\u9879\u4efb\u52a1\u5206\u914d\u7684\u670d\u52a1\u5668\u7684\u4e0b\u6807\u3002
\n\n\u8fd4\u56de\u7b54\u6848\u6570\u7ec4 ans
\u200b\u200b\u200b\u200b \u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1aservers = [3,3,2], tasks = [1,2,3,2,1,2]\n\u8f93\u51fa\uff1a[2,2,0,2,1,2]\n\u89e3\u91ca\uff1a\u4e8b\u4ef6\u6309\u65f6\u95f4\u987a\u5e8f\u5982\u4e0b\uff1a\n- 0 \u79d2\u65f6\uff0c\u7b2c 0 \u9879\u4efb\u52a1\u52a0\u5165\u5230\u4efb\u52a1\u961f\u5217\uff0c\u4f7f\u7528\u7b2c 2 \u53f0\u670d\u52a1\u5668\u5904\u7406\u5230 1 \u79d2\u3002\n- 1 \u79d2\u65f6\uff0c\u7b2c 2 \u53f0\u670d\u52a1\u5668\u7a7a\u95f2\uff0c\u7b2c 1 \u9879\u4efb\u52a1\u52a0\u5165\u5230\u4efb\u52a1\u961f\u5217\uff0c\u4f7f\u7528\u7b2c 2 \u53f0\u670d\u52a1\u5668\u5904\u7406\u5230 3 \u79d2\u3002\n- 2 \u79d2\u65f6\uff0c\u7b2c 2 \u9879\u4efb\u52a1\u52a0\u5165\u5230\u4efb\u52a1\u961f\u5217\uff0c\u4f7f\u7528\u7b2c 0 \u53f0\u670d\u52a1\u5668\u5904\u7406\u5230 5 \u79d2\u3002\n- 3 \u79d2\u65f6\uff0c\u7b2c 2 \u53f0\u670d\u52a1\u5668\u7a7a\u95f2\uff0c\u7b2c 3 \u9879\u4efb\u52a1\u52a0\u5165\u5230\u4efb\u52a1\u961f\u5217\uff0c\u4f7f\u7528\u7b2c 2 \u53f0\u670d\u52a1\u5668\u5904\u7406\u5230 5 \u79d2\u3002\n- 4 \u79d2\u65f6\uff0c\u7b2c 4 \u9879\u4efb\u52a1\u52a0\u5165\u5230\u4efb\u52a1\u961f\u5217\uff0c\u4f7f\u7528\u7b2c 1 \u53f0\u670d\u52a1\u5668\u5904\u7406\u5230 5 \u79d2\u3002\n- 5 \u79d2\u65f6\uff0c\u6240\u6709\u670d\u52a1\u5668\u90fd\u7a7a\u95f2\uff0c\u7b2c 5 \u9879\u4efb\u52a1\u52a0\u5165\u5230\u4efb\u52a1\u961f\u5217\uff0c\u4f7f\u7528\u7b2c 2 \u53f0\u670d\u52a1\u5668\u5904\u7406\u5230 7 \u79d2\u3002
\n\n\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1aservers = [5,1,4,3,2], tasks = [2,1,2,4,5,2,1]\n\u8f93\u51fa\uff1a[1,4,1,4,1,3,2]\n\u89e3\u91ca\uff1a\u4e8b\u4ef6\u6309\u65f6\u95f4\u987a\u5e8f\u5982\u4e0b\uff1a\n- 0 \u79d2\u65f6\uff0c\u7b2c 0 \u9879\u4efb\u52a1\u52a0\u5165\u5230\u4efb\u52a1\u961f\u5217\uff0c\u4f7f\u7528\u7b2c 1 \u53f0\u670d\u52a1\u5668\u5904\u7406\u5230 2 \u79d2\u3002\n- 1 \u79d2\u65f6\uff0c\u7b2c 1 \u9879\u4efb\u52a1\u52a0\u5165\u5230\u4efb\u52a1\u961f\u5217\uff0c\u4f7f\u7528\u7b2c 4 \u53f0\u670d\u52a1\u5668\u5904\u7406\u5230 2 \u79d2\u3002\n- 2 \u79d2\u65f6\uff0c\u7b2c 1 \u53f0\u548c\u7b2c 4 \u53f0\u670d\u52a1\u5668\u7a7a\u95f2\uff0c\u7b2c 2 \u9879\u4efb\u52a1\u52a0\u5165\u5230\u4efb\u52a1\u961f\u5217\uff0c\u4f7f\u7528\u7b2c 1 \u53f0\u670d\u52a1\u5668\u5904\u7406\u5230 4 \u79d2\u3002\n- 3 \u79d2\u65f6\uff0c\u7b2c 3 \u9879\u4efb\u52a1\u52a0\u5165\u5230\u4efb\u52a1\u961f\u5217\uff0c\u4f7f\u7528\u7b2c 4 \u53f0\u670d\u52a1\u5668\u5904\u7406\u5230 7 \u79d2\u3002\n- 4 \u79d2\u65f6\uff0c\u7b2c 1 \u53f0\u670d\u52a1\u5668\u7a7a\u95f2\uff0c\u7b2c 4 \u9879\u4efb\u52a1\u52a0\u5165\u5230\u4efb\u52a1\u961f\u5217\uff0c\u4f7f\u7528\u7b2c 1 \u53f0\u670d\u52a1\u5668\u5904\u7406\u5230 9 \u79d2\u3002\n- 5 \u79d2\u65f6\uff0c\u7b2c 5 \u9879\u4efb\u52a1\u52a0\u5165\u5230\u4efb\u52a1\u961f\u5217\uff0c\u4f7f\u7528\u7b2c 3 \u53f0\u670d\u52a1\u5668\u5904\u7406\u5230 7 \u79d2\u3002\n- 6 \u79d2\u65f6\uff0c\u7b2c 6 \u9879\u4efb\u52a1\u52a0\u5165\u5230\u4efb\u52a1\u961f\u5217\uff0c\u4f7f\u7528\u7b2c 2 \u53f0\u670d\u52a1\u5668\u5904\u7406\u5230 7 \u79d2\u3002
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\tservers.length == n
\n\ttasks.length == m
\n\t1 <= n, m <= 2 * 105
\n\t1 <= servers[i], tasks[j] <= 2 * 105
\n
\n", "tags_en": ["Array", "Heap (Priority Queue)"], "tags_cn": ["\u6570\u7ec4", "\u5806\uff08\u4f18\u5148\u961f\u5217\uff09"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n vector assignTasks(vector& servers, vector& tasks) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int[] assignTasks(int[] servers, int[] tasks) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def assignTasks(self, servers, tasks):\n \"\"\"\n :type servers: List[int]\n :type tasks: List[int]\n :rtype: List[int]\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def assignTasks(self, servers: List[int], tasks: List[int]) -> List[int]:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\n/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\nint* assignTasks(int* servers, int serversSize, int* tasks, int tasksSize, int* returnSize){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int[] AssignTasks(int[] servers, int[] tasks) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[]} servers\n * @param {number[]} tasks\n * @return {number[]}\n */\nvar assignTasks = function(servers, tasks) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer[]} servers\n# @param {Integer[]} tasks\n# @return {Integer[]}\ndef assign_tasks(servers, tasks)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func assignTasks(_ servers: [Int], _ tasks: [Int]) -> [Int] {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func assignTasks(servers []int, tasks []int) []int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def assignTasks(servers: Array[Int], tasks: Array[Int]): Array[Int] = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun assignTasks(servers: IntArray, tasks: IntArray): IntArray {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn assign_tasks(servers: Vec, tasks: Vec) -> Vec {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer[] $servers\n * @param Integer[] $tasks\n * @return Integer[]\n */\n function assignTasks($servers, $tasks) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function assignTasks(servers: number[], tasks: number[]): number[] {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (assign-tasks servers tasks)\n (-> (listof exact-integer?) (listof exact-integer?) (listof exact-integer?))\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec assign_tasks(Servers :: [integer()], Tasks :: [integer()]) -> [integer()].\nassign_tasks(Servers, Tasks) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec assign_tasks(servers :: [integer], tasks :: [integer]) :: [integer]\n def assign_tasks(servers, tasks) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1882](https://leetcode-cn.com/problems/process-tasks-using-servers)", "[\u4f7f\u7528\u670d\u52a1\u5668\u5904\u7406\u4efb\u52a1](/solution/1800-1899/1882.Process%20Tasks%20Using%20Servers/README.md)", "`\u6570\u7ec4`,`\u5806\uff08\u4f18\u5148\u961f\u5217\uff09`", "\u4e2d\u7b49", ""], "md_table_row_en": ["[1882](https://leetcode.com/problems/process-tasks-using-servers)", "[Process Tasks Using Servers](/solution/1800-1899/1882.Process%20Tasks%20Using%20Servers/README_EN.md)", "`Array`,`Heap (Priority Queue)`", "Medium", ""]}, {"question_id": "2011", "frontend_question_id": "1881", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/maximum-value-after-insertion", "url_en": "https://leetcode.com/problems/maximum-value-after-insertion", "relative_path_cn": "/solution/1800-1899/1881.Maximum%20Value%20after%20Insertion/README.md", "relative_path_en": "/solution/1800-1899/1881.Maximum%20Value%20after%20Insertion/README_EN.md", "title_cn": "\u63d2\u5165\u540e\u7684\u6700\u5927\u503c", "title_en": "Maximum Value after Insertion", "question_title_slug": "maximum-value-after-insertion", "content_en": "You are given a very large integer n
, represented as a string,\u200b\u200b\u200b\u200b\u200b\u200b and an integer digit x
. The digits in n
and the digit x
are in the inclusive range [1, 9]
, and n
may represent a negative number.
\n\nYou want to maximize n
's numerical value by inserting x
anywhere in the decimal representation of n
\u200b\u200b\u200b\u200b\u200b\u200b. You cannot insert x
to the left of the negative sign.
\n\n\n\t- For example, if
n = 73
and x = 6
, it would be best to insert it between 7
and 3
, making n = 763
. \n\t- If
n = -55
and x = 2
, it would be best to insert it before the first 5
, making n = -255
. \n
\n\nReturn a string representing the maximum value of n
\u200b\u200b\u200b\u200b\u200b\u200b after the insertion.
\n\n
\nExample 1:
\n\n\nInput: n = "99", x = 9\nOutput: "999"\nExplanation: The result is the same regardless of where you insert 9.\n
\n\nExample 2:
\n\n\nInput: n = "-13", x = 2\nOutput: "-123"\nExplanation: You can make n one of {-213, -123, -132}, and the largest of those three is -123.\n
\n\n
\nConstraints:
\n\n\n\t1 <= n.length <= 105
\n\t1 <= x <= 9
\n\t- The digits in
n
\u200b\u200b\u200b are in the range [1, 9]
. \n\tn
is a valid representation of an integer. \n\t- In the case of a negative
n
,\u200b\u200b\u200b\u200b\u200b\u200b it will begin with '-'
. \n
\n", "content_cn": "\u7ed9\u4f60\u4e00\u4e2a\u975e\u5e38\u5927\u7684\u6574\u6570 n
\u548c\u4e00\u4e2a\u6574\u6570\u6570\u5b57 x
\uff0c\u5927\u6574\u6570 n
\u00a0\u7528\u4e00\u4e2a\u5b57\u7b26\u4e32\u8868\u793a\u3002n
\u4e2d\u6bcf\u4e00\u4f4d\u6570\u5b57\u548c\u6570\u5b57 x
\u90fd\u5904\u4e8e\u95ed\u533a\u95f4 [1, 9]
\u4e2d\uff0c\u4e14 n
\u53ef\u80fd\u8868\u793a\u4e00\u4e2a \u8d1f\u6570 \u3002
\n\n\u4f60\u6253\u7b97\u901a\u8fc7\u5728 n
\u7684\u5341\u8fdb\u5236\u8868\u793a\u7684\u4efb\u610f\u4f4d\u7f6e\u63d2\u5165 x
\u6765 \u6700\u5927\u5316 n
\u7684 \u6570\u503c \u200b\u200b\u200b\u200b\u200b\u200b\u3002\u4f46 \u4e0d\u80fd \u5728\u8d1f\u53f7\u7684\u5de6\u8fb9\u63d2\u5165 x
\u3002
\n\n\n\t- \u4f8b\u5982\uff0c\u5982\u679c
n = 73
\u4e14 x = 6
\uff0c\u90a3\u4e48\u6700\u4f73\u65b9\u6848\u662f\u5c06 6
\u63d2\u5165 7
\u548c 3
\u4e4b\u95f4\uff0c\u4f7f n = 763
\u3002 \n\t- \u5982\u679c
n = -55
\u4e14 x = 2
\uff0c\u90a3\u4e48\u6700\u4f73\u65b9\u6848\u662f\u5c06 2
\u63d2\u5728\u7b2c\u4e00\u4e2a 5
\u4e4b\u524d\uff0c\u4f7f n = -255
\u3002 \n
\n\n\u8fd4\u56de\u63d2\u5165\u64cd\u4f5c\u540e\uff0c\u7528\u5b57\u7b26\u4e32\u8868\u793a\u7684\u00a0n
\u7684\u6700\u5927\u503c\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1an = \"99\", x = 9\n\u8f93\u51fa\uff1a\"999\"\n\u89e3\u91ca\uff1a\u4e0d\u7ba1\u5728\u54ea\u91cc\u63d2\u5165 9 \uff0c\u7ed3\u679c\u90fd\u662f\u76f8\u540c\u7684\u3002\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1an = \"-13\", x = 2\n\u8f93\u51fa\uff1a\"-123\"\n\u89e3\u91ca\uff1a\u5411 n \u4e2d\u63d2\u5165 x \u53ef\u4ee5\u5f97\u5230 -213\u3001-123 \u6216\u8005 -132 \uff0c\u4e09\u8005\u4e2d\u6700\u5927\u7684\u662f -123 \u3002\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\t1 <= n.length <= 105
\n\t1 <= x <= 9
\n\tn
\u200b\u200b\u200b \u4e2d\u6bcf\u4e00\u4f4d\u7684\u6570\u5b57\u90fd\u5728\u95ed\u533a\u95f4 [1, 9]
\u4e2d\u3002 \n\tn
\u00a0\u4ee3\u8868\u4e00\u4e2a\u6709\u6548\u7684\u6574\u6570\u3002 \n\t- \u5f53
n
\u8868\u793a\u8d1f\u6570\u65f6\uff0c\u5c06\u4f1a\u4ee5\u5b57\u7b26 '-'
\u5f00\u59cb\u3002 \n
\n", "tags_en": ["Greedy", "String"], "tags_cn": ["\u8d2a\u5fc3", "\u5b57\u7b26\u4e32"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n string maxValue(string n, int x) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public String maxValue(String n, int x) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def maxValue(self, n, x):\n \"\"\"\n :type n: str\n :type x: int\n :rtype: str\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def maxValue(self, n: str, x: int) -> str:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nchar * maxValue(char * n, int x){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public string MaxValue(string n, int x) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {string} n\n * @param {number} x\n * @return {string}\n */\nvar maxValue = function(n, x) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {String} n\n# @param {Integer} x\n# @return {String}\ndef max_value(n, x)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func maxValue(_ n: String, _ x: Int) -> String {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func maxValue(n string, x int) string {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def maxValue(n: String, x: Int): String = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun maxValue(n: String, x: Int): String {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn max_value(n: String, x: i32) -> String {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param String $n\n * @param Integer $x\n * @return String\n */\n function maxValue($n, $x) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function maxValue(n: string, x: number): string {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (max-value n x)\n (-> string? exact-integer? string?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec max_value(N :: unicode:unicode_binary(), X :: integer()) -> unicode:unicode_binary().\nmax_value(N, X) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec max_value(n :: String.t, x :: integer) :: String.t\n def max_value(n, x) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1881](https://leetcode-cn.com/problems/maximum-value-after-insertion)", "[\u63d2\u5165\u540e\u7684\u6700\u5927\u503c](/solution/1800-1899/1881.Maximum%20Value%20after%20Insertion/README.md)", "`\u8d2a\u5fc3`,`\u5b57\u7b26\u4e32`", "\u4e2d\u7b49", ""], "md_table_row_en": ["[1881](https://leetcode.com/problems/maximum-value-after-insertion)", "[Maximum Value after Insertion](/solution/1800-1899/1881.Maximum%20Value%20after%20Insertion/README_EN.md)", "`Greedy`,`String`", "Medium", ""]}, {"question_id": "2010", "frontend_question_id": "1880", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/check-if-word-equals-summation-of-two-words", "url_en": "https://leetcode.com/problems/check-if-word-equals-summation-of-two-words", "relative_path_cn": "/solution/1800-1899/1880.Check%20if%20Word%20Equals%20Summation%20of%20Two%20Words/README.md", "relative_path_en": "/solution/1800-1899/1880.Check%20if%20Word%20Equals%20Summation%20of%20Two%20Words/README_EN.md", "title_cn": "\u68c0\u67e5\u67d0\u5355\u8bcd\u662f\u5426\u7b49\u4e8e\u4e24\u5355\u8bcd\u4e4b\u548c", "title_en": "Check if Word Equals Summation of Two Words", "question_title_slug": "check-if-word-equals-summation-of-two-words", "content_en": "The letter value of a letter is its position in the alphabet starting from 0 (i.e. 'a' -> 0
, 'b' -> 1
, 'c' -> 2
, etc.).
\n\nThe numerical value of some string of lowercase English letters s
is the concatenation of the letter values of each letter in s
, which is then converted into an integer.
\n\n\n\t- For example, if
s = "acb"
, we concatenate each letter's letter value, resulting in "021"
. After converting it, we get 21
. \n
\n\nYou are given three strings firstWord
, secondWord
, and targetWord
, each consisting of lowercase English letters 'a'
through 'j'
inclusive.
\n\nReturn true
if the summation of the numerical values of firstWord
and secondWord
equals the numerical value of targetWord
, or false
otherwise.
\n\n
\nExample 1:
\n\n\nInput: firstWord = "acb", secondWord = "cba", targetWord = "cdb"\nOutput: true\nExplanation:\nThe numerical value of firstWord is "acb" -> "021" -> 21.\nThe numerical value of secondWord is "cba" -> "210" -> 210.\nThe numerical value of targetWord is "cdb" -> "231" -> 231.\nWe return true because 21 + 210 == 231.\n
\n\nExample 2:
\n\n\nInput: firstWord = "aaa", secondWord = "a", targetWord = "aab"\nOutput: false\nExplanation: \nThe numerical value of firstWord is "aaa" -> "000" -> 0.\nThe numerical value of secondWord is "a" -> "0" -> 0.\nThe numerical value of targetWord is "aab" -> "001" -> 1.\nWe return false because 0 + 0 != 1.\n
\n\nExample 3:
\n\n\nInput: firstWord = "aaa", secondWord = "a", targetWord = "aaaa"\nOutput: true\nExplanation: \nThe numerical value of firstWord is "aaa" -> "000" -> 0.\nThe numerical value of secondWord is "a" -> "0" -> 0.\nThe numerical value of targetWord is "aaaa" -> "0000" -> 0.\nWe return true because 0 + 0 == 0.\n
\n\n
\nConstraints:
\n\n\n\t1 <= firstWord.length,
secondWord.length,
targetWord.length <= 8
\n\tfirstWord
, secondWord
, and targetWord
consist of lowercase English letters from 'a'
to 'j'
inclusive. \n
\n", "content_cn": "\u5b57\u6bcd\u7684 \u5b57\u6bcd\u503c \u53d6\u51b3\u4e8e\u5b57\u6bcd\u5728\u5b57\u6bcd\u8868\u4e2d\u7684\u4f4d\u7f6e\uff0c\u4ece 0 \u5f00\u59cb \u8ba1\u6570\u3002\u5373\uff0c'a' -> 0
\u3001'b' -> 1
\u3001'c' -> 2
\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002
\n\n\u5bf9\u67d0\u4e2a\u7531\u5c0f\u5199\u5b57\u6bcd\u7ec4\u6210\u7684\u5b57\u7b26\u4e32\u00a0s
\u800c\u8a00\uff0c\u5176 \u6570\u503c \u5c31\u7b49\u4e8e\u5c06 s
\u4e2d\u6bcf\u4e2a\u5b57\u6bcd\u7684 \u5b57\u6bcd\u503c \u6309\u987a\u5e8f \u8fde\u63a5 \u5e76 \u8f6c\u6362 \u6210\u5bf9\u5e94\u6574\u6570\u3002
\n\n\n\t- \u4f8b\u5982\uff0c
s = \"acb\"
\uff0c\u4f9d\u6b21\u8fde\u63a5\u6bcf\u4e2a\u5b57\u6bcd\u7684\u5b57\u6bcd\u503c\u53ef\u4ee5\u5f97\u5230 \"021\"
\uff0c\u8f6c\u6362\u4e3a\u6574\u6570\u5f97\u5230 21
\u3002 \n
\n\n\u7ed9\u4f60\u4e09\u4e2a\u5b57\u7b26\u4e32 firstWord
\u3001secondWord
\u548c targetWord
\uff0c\u6bcf\u4e2a\u5b57\u7b26\u4e32\u90fd\u7531\u4ece 'a'
\u5230 'j'
\uff08\u542b\u00a0'a'
\u548c 'j'
\uff09\u7684\u5c0f\u5199\u82f1\u6587\u5b57\u6bcd\u7ec4\u6210\u3002
\n\n\u5982\u679c\u00a0firstWord
\u548c secondWord
\u7684 \u6570\u503c\u4e4b\u548c \u7b49\u4e8e targetWord
\u7684\u6570\u503c\uff0c\u8fd4\u56de true
\uff1b\u5426\u5219\uff0c\u8fd4\u56de false
\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1afirstWord = \"acb\", secondWord = \"cba\", targetWord = \"cdb\"\n\u8f93\u51fa\uff1atrue\n\u89e3\u91ca\uff1a\nfirstWord \u7684\u6570\u503c\u4e3a \"acb\" -> \"021\" -> 21\nsecondWord \u7684\u6570\u503c\u4e3a \"cba\" -> \"210\" -> 210\ntargetWord \u7684\u6570\u503c\u4e3a \"cdb\" -> \"231\" -> 231\n\u7531\u4e8e 21 + 210 == 231 \uff0c\u8fd4\u56de true\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1afirstWord = \"aaa\", secondWord = \"a\", targetWord = \"aab\"\n\u8f93\u51fa\uff1afalse\n\u89e3\u91ca\uff1a\nfirstWord \u7684\u6570\u503c\u4e3a \"aaa\" -> \"000\" -> 0\nsecondWord \u7684\u6570\u503c\u4e3a \"a\" -> \"0\" -> 0\ntargetWord \u7684\u6570\u503c\u4e3a \"aab\" -> \"001\" -> 1\n\u7531\u4e8e 0 + 0 != 1 \uff0c\u8fd4\u56de false
\n\n\u793a\u4f8b 3\uff1a
\n\n\u8f93\u5165\uff1afirstWord = \"aaa\", secondWord = \"a\", targetWord = \"aaaa\"\n\u8f93\u51fa\uff1atrue\n\u89e3\u91ca\uff1a\nfirstWord \u7684\u6570\u503c\u4e3a \"aaa\" -> \"000\" -> 0\nsecondWord \u7684\u6570\u503c\u4e3a \"a\" -> \"0\" -> 0\ntargetWord \u7684\u6570\u503c\u4e3a \"aaaa\" -> \"0000\" -> 0\n\u7531\u4e8e 0 + 0 == 0 \uff0c\u8fd4\u56de true\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\t1 <= firstWord.length,
secondWord.length,
targetWord.length <= 8
\n\tfirstWord
\u3001secondWord
\u548c targetWord
\u4ec5\u7531\u4ece 'a'
\u5230 'j'
\uff08\u542b\u00a0'a'
\u548c 'j'
\uff09\u7684\u5c0f\u5199\u82f1\u6587\u5b57\u6bcd\u7ec4\u6210\u3002 \n
\n", "tags_en": ["String"], "tags_cn": ["\u5b57\u7b26\u4e32"], "difficulty_en": "Easy", "difficulty_cn": "\u7b80\u5355", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n bool isSumEqual(string firstWord, string secondWord, string targetWord) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public boolean isSumEqual(String firstWord, String secondWord, String targetWord) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def isSumEqual(self, firstWord, secondWord, targetWord):\n \"\"\"\n :type firstWord: str\n :type secondWord: str\n :type targetWord: str\n :rtype: bool\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def isSumEqual(self, firstWord: str, secondWord: str, targetWord: str) -> bool:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nbool isSumEqual(char * firstWord, char * secondWord, char * targetWord){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public bool IsSumEqual(string firstWord, string secondWord, string targetWord) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {string} firstWord\n * @param {string} secondWord\n * @param {string} targetWord\n * @return {boolean}\n */\nvar isSumEqual = function(firstWord, secondWord, targetWord) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {String} first_word\n# @param {String} second_word\n# @param {String} target_word\n# @return {Boolean}\ndef is_sum_equal(first_word, second_word, target_word)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func isSumEqual(_ firstWord: String, _ secondWord: String, _ targetWord: String) -> Bool {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func isSumEqual(firstWord string, secondWord string, targetWord string) bool {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def isSumEqual(firstWord: String, secondWord: String, targetWord: String): Boolean = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun isSumEqual(firstWord: String, secondWord: String, targetWord: String): Boolean {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn is_sum_equal(first_word: String, second_word: String, target_word: String) -> bool {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param String $firstWord\n * @param String $secondWord\n * @param String $targetWord\n * @return Boolean\n */\n function isSumEqual($firstWord, $secondWord, $targetWord) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function isSumEqual(firstWord: string, secondWord: string, targetWord: string): boolean {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (is-sum-equal firstWord secondWord targetWord)\n (-> string? string? string? boolean?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec is_sum_equal(FirstWord :: unicode:unicode_binary(), SecondWord :: unicode:unicode_binary(), TargetWord :: unicode:unicode_binary()) -> boolean().\nis_sum_equal(FirstWord, SecondWord, TargetWord) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec is_sum_equal(first_word :: String.t, second_word :: String.t, target_word :: String.t) :: boolean\n def is_sum_equal(first_word, second_word, target_word) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1880](https://leetcode-cn.com/problems/check-if-word-equals-summation-of-two-words)", "[\u68c0\u67e5\u67d0\u5355\u8bcd\u662f\u5426\u7b49\u4e8e\u4e24\u5355\u8bcd\u4e4b\u548c](/solution/1800-1899/1880.Check%20if%20Word%20Equals%20Summation%20of%20Two%20Words/README.md)", "`\u5b57\u7b26\u4e32`", "\u7b80\u5355", ""], "md_table_row_en": ["[1880](https://leetcode.com/problems/check-if-word-equals-summation-of-two-words)", "[Check if Word Equals Summation of Two Words](/solution/1800-1899/1880.Check%20if%20Word%20Equals%20Summation%20of%20Two%20Words/README_EN.md)", "`String`", "Easy", ""]}, {"question_id": "2009", "frontend_question_id": "1858", "paid_only": true, "paid_only_cn": true, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/longest-word-with-all-prefixes", "url_en": "https://leetcode.com/problems/longest-word-with-all-prefixes", "relative_path_cn": "/solution/1800-1899/1858.Longest%20Word%20With%20All%20Prefixes/README.md", "relative_path_en": "/solution/1800-1899/1858.Longest%20Word%20With%20All%20Prefixes/README_EN.md", "title_cn": "\u5305\u542b\u6240\u6709\u524d\u7f00\u7684\u6700\u957f\u5355\u8bcd", "title_en": "Longest Word With All Prefixes", "question_title_slug": "longest-word-with-all-prefixes", "content_en": "Given an array of strings words
, find the longest string in words
such that every prefix of it is also in words
.
\r\n\r\n\r\n\t- For example, let
words = ["a", "app", "ap"]
. The string "app"
has prefixes "ap"
and "a"
, all of which are in words
. \r\n
\r\n\r\nReturn the string described above. If there is more than one string with the same length, return the lexicographically smallest one, and if no string exists, return ""
.
\r\n\r\n
\r\nExample 1:
\r\n\r\n\r\nInput: words = ["k","ki","kir","kira", "kiran"]\r\nOutput: "kiran"\r\nExplanation: "kiran" has prefixes "kira", "kir", "ki", and "k", and all of them appear in words.\r\n
\r\n\r\nExample 2:
\r\n\r\n\r\nInput: words = ["a", "banana", "app", "appl", "ap", "apply", "apple"]\r\nOutput: "apple"\r\nExplanation: Both "apple" and "apply" have all their prefixes in words.\r\nHowever, "apple" is lexicographically smaller, so we return that.\r\n
\r\n\r\nExample 3:
\r\n\r\n\r\nInput: words = ["abc", "bc", "ab", "qwe"]\r\nOutput: ""\r\n
\r\n\r\n
\r\nConstraints:
\r\n\r\n\r\n\t1 <= words.length <= 105
\r\n\t1 <= words[i].length <= 105
\r\n\t1 <= sum(words[i].length) <= 105
\r\n
", "content_cn": "\u7ed9\u5b9a\u4e00\u4e2a\u5b57\u7b26\u4e32\u6570\u7ec4\u00a0words
\uff0c\u627e\u51fa\u00a0words
\u00a0\u4e2d\u6240\u6709\u7684\u524d\u7f00\u90fd\u5728\u00a0words
\u00a0\u4e2d\u7684\u6700\u957f\u5b57\u7b26\u4e32\u3002
\n\n\n\t- \u4f8b\u5982\uff0c\u4ee4\u00a0
words = [\"a\", \"app\", \"ap\"]
\u3002\u5b57\u7b26\u4e32\u00a0\"app\"
\u00a0\u542b\u524d\u7f00\u00a0\"ap\"
\u00a0\u548c\u00a0\"a\"
\u00a0\uff0c\u90fd\u5728\u00a0words
\u00a0\u4e2d\u3002 \n
\n\n\u8fd4\u56de\u7b26\u5408\u4e0a\u8ff0\u8981\u6c42\u7684\u5b57\u7b26\u4e32\u3002\u5982\u679c\u5b58\u5728\u591a\u4e2a\uff08\u7b26\u5408\u6761\u4ef6\u7684\uff09\u76f8\u540c\u957f\u5ea6\u7684\u5b57\u7b26\u4e32\uff0c\u8fd4\u56de\u5b57\u5178\u5e8f\u4e2d\u6700\u5c0f\u7684\u5b57\u7b26\u4e32\uff0c\u5982\u679c\u8fd9\u6837\u7684\u5b57\u7b26\u4e32\u4e0d\u5b58\u5728\uff0c\u8fd4\u56de\u00a0\"\"
\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1:
\n\n\u8f93\u5165\uff1a words = [\"k\",\"ki\",\"kir\",\"kira\", \"kiran\"]\n\u8f93\u51fa\uff1a \"kiran\"\n\u89e3\u91ca\uff1a \"kiran\" \u542b\u524d\u7f00 \"kira\"\u3001 \"kir\"\u3001 \"ki\"\u3001 \u548c \"k\"\uff0c\u8fd9\u4e9b\u524d\u7f00\u90fd\u51fa\u73b0\u5728 words \u4e2d\u3002\n
\n\n\u793a\u4f8b 2:
\n\n\u8f93\u5165\uff1a words = [\"a\", \"banana\", \"app\", \"appl\", \"ap\", \"apply\", \"apple\"]\n\u8f93\u51fa\uff1a \"apple\"\n\u89e3\u91ca\uff1a \"apple\" \u548c \"apply\" \u90fd\u5728 words \u4e2d\u542b\u6709\u5404\u81ea\u7684\u6240\u6709\u524d\u7f00\u3002\n\u7136\u800c\uff0c\"apple\" \u5728\u5b57\u5178\u5e8f\u4e2d\u66f4\u5c0f\uff0c\u6240\u4ee5\u6211\u4eec\u8fd4\u56de\u4e4b\u3002\n
\n\n\u793a\u4f8b 3:
\n\n\u8f93\u5165\uff1a words = [\"abc\", \"bc\", \"ab\", \"qwe\"]\n\u8f93\u51fa\uff1a \"\"\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\t1 <= words.length <= 105
\n\t1 <= words[i].length <= 105
\n\t1 <= sum(words[i].length) <= 105
\n
\n", "tags_en": ["Depth-First Search", "Trie"], "tags_cn": ["\u6df1\u5ea6\u4f18\u5148\u641c\u7d22", "\u5b57\u5178\u6811"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n string longestWord(vector& words) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public String longestWord(String[] words) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def longestWord(self, words):\n \"\"\"\n :type words: List[str]\n :rtype: str\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def longestWord(self, words: List[str]) -> str:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nchar * longestWord(char ** words, int wordsSize){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public string LongestWord(string[] words) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {string[]} words\n * @return {string}\n */\nvar longestWord = function(words) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {String[]} words\n# @return {String}\ndef longest_word(words)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func longestWord(_ words: [String]) -> String {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func longestWord(words []string) string {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def longestWord(words: Array[String]): String = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun longestWord(words: Array): String {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn longest_word(words: Vec) -> String {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param String[] $words\n * @return String\n */\n function longestWord($words) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function longestWord(words: string[]): string {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (longest-word words)\n (-> (listof string?) string?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec longest_word(Words :: [unicode:unicode_binary()]) -> unicode:unicode_binary().\nlongest_word(Words) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec longest_word(words :: [String.t]) :: String.t\n def longest_word(words) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1858](https://leetcode-cn.com/problems/longest-word-with-all-prefixes)", "[\u5305\u542b\u6240\u6709\u524d\u7f00\u7684\u6700\u957f\u5355\u8bcd](/solution/1800-1899/1858.Longest%20Word%20With%20All%20Prefixes/README.md)", "`\u6df1\u5ea6\u4f18\u5148\u641c\u7d22`,`\u5b57\u5178\u6811`", "\u4e2d\u7b49", "\ud83d\udd12"], "md_table_row_en": ["[1858](https://leetcode.com/problems/longest-word-with-all-prefixes)", "[Longest Word With All Prefixes](/solution/1800-1899/1858.Longest%20Word%20With%20All%20Prefixes/README_EN.md)", "`Depth-First Search`,`Trie`", "Medium", "\ud83d\udd12"]}, {"question_id": "2008", "frontend_question_id": "1896", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/minimum-cost-to-change-the-final-value-of-expression", "url_en": "https://leetcode.com/problems/minimum-cost-to-change-the-final-value-of-expression", "relative_path_cn": "/solution/1800-1899/1896.Minimum%20Cost%20to%20Change%20the%20Final%20Value%20of%20Expression/README.md", "relative_path_en": "/solution/1800-1899/1896.Minimum%20Cost%20to%20Change%20the%20Final%20Value%20of%20Expression/README_EN.md", "title_cn": "\u53cd\u8f6c\u8868\u8fbe\u5f0f\u503c\u7684\u6700\u5c11\u64cd\u4f5c\u6b21\u6570", "title_en": "Minimum Cost to Change the Final Value of Expression", "question_title_slug": "minimum-cost-to-change-the-final-value-of-expression", "content_en": "You are given a valid boolean expression as a string expression
consisting of the characters '1'
,'0'
,'&'
(bitwise AND operator),'|'
(bitwise OR operator),'('
, and ')'
.
\n\n\n\t- For example,
"()1|1"
and "(1)&()"
are not valid while "1"
, "(((1))|(0))"
, and "1|(0&(1))"
are valid expressions. \n
\n\nReturn the minimum cost to change the final value of the expression.
\n\n\n\t- For example, if
expression = "1|1|(0&0)&1"
, its value is 1|1|(0&0)&1 = 1|1|0&1 = 1|0&1 = 1&1 = 1
. We want to apply operations so that the new expression evaluates to 0
. \n
\n\nThe cost of changing the final value of an expression is the number of operations performed on the expression. The types of operations are described as follows:
\n\n\n\t- Turn a
'1'
into a '0'
. \n\t- Turn a
'0'
into a '1'
. \n\t- Turn a
'&'
into a '|'
. \n\t- Turn a
'|'
into a '&'
. \n
\n\nNote: '&'
does not take precedence over '|'
in the order of calculation. Evaluate parentheses first, then in left-to-right order.
\n\n
\nExample 1:
\n\n\nInput: expression = "1&(0|1)"\nOutput: 1\nExplanation: We can turn "1&(0|1)" into "1&(0&1)" by changing the '|' to a '&' using 1 operation.\nThe new expression evaluates to 0. \n
\n\nExample 2:
\n\n\nInput: expression = "(0&0)&(0&0&0)"\nOutput: 3\nExplanation: We can turn "(0&0)&(0&0&0)" into "(0|1)|(0&0&0)" using 3 operations.\nThe new expression evaluates to 1.\n
\n\nExample 3:
\n\n\nInput: expression = "(0|(1|0&1))"\nOutput: 1\nExplanation: We can turn "(0|(1|0&1))" into "(0|(0|0&1))" using 1 operation.\nThe new expression evaluates to 0.
\n\n
\nConstraints:
\n\n\n\t1 <= expression.length <= 105
\n\texpression
only contains '1'
,'0'
,'&'
,'|'
,'('
, and ')'
\n\t- All parentheses are properly matched.
\n\t- There will be no empty parentheses (i.e:
"()"
is not a substring of expression
). \n
\n", "content_cn": "\u7ed9\u4f60\u4e00\u4e2a \u6709\u6548\u7684\u00a0\u5e03\u5c14\u8868\u8fbe\u5f0f\uff0c\u7528\u5b57\u7b26\u4e32\u00a0expression
\u00a0\u8868\u793a\u3002\u8fd9\u4e2a\u5b57\u7b26\u4e32\u5305\u542b\u5b57\u7b26\u00a0'1'
\uff0c'0'
\uff0c'&'
\uff08\u6309\u4f4d \u4e0e\u00a0\u8fd0\u7b97\uff09\uff0c'|'
\uff08\u6309\u4f4d \u6216\u00a0\u8fd0\u7b97\uff09\uff0c'('
\u00a0\u548c\u00a0')'
\u00a0\u3002
\n\n\n\t- \u6bd4\u65b9\u8bf4\uff0c
\"()1|1\"
\u548c\u00a0\"(1)&()\"
\u00a0\u4e0d\u662f\u6709\u6548\u00a0\u5e03\u5c14\u8868\u8fbe\u5f0f\u3002\u800c\u00a0\"1\"
\uff0c\u00a0\"(((1))|(0))\"
\u00a0\u548c\u00a0\"1|(0&(1))\"
\u00a0\u662f \u6709\u6548\u00a0\u5e03\u5c14\u8868\u8fbe\u5f0f\u3002 \n
\n\n\u4f60\u7684\u76ee\u6807\u662f\u5c06\u5e03\u5c14\u8868\u8fbe\u5f0f\u7684 \u503c\u00a0\u53cd\u8f6c \uff08\u4e5f\u5c31\u662f\u5c06 0
\u00a0\u53d8\u4e3a 1
\u00a0\uff0c\u6216\u8005\u5c06 1
\u00a0\u53d8\u4e3a 0
\uff09\uff0c\u8bf7\u4f60\u8fd4\u56de\u8fbe\u6210\u76ee\u6807\u9700\u8981\u7684 \u6700\u5c11\u64cd\u4f5c\u00a0\u6b21\u6570\u3002
\n\n\n\t- \u6bd4\u65b9\u8bf4\uff0c\u5982\u679c\u8868\u8fbe\u5f0f\u00a0
expression = \"1|1|(0&0)&1\"
\u00a0\uff0c\u5b83\u7684\u00a0\u503c\u00a0\u4e3a\u00a01|1|(0&0)&1 = 1|1|0&1 = 1|0&1 = 1&1 = 1
\u00a0\u3002\u6211\u4eec\u60f3\u8981\u6267\u884c\u64cd\u4f5c\u5c06\u00a0\u65b0\u7684\u00a0\u8868\u8fbe\u5f0f\u7684\u503c\u53d8\u6210\u00a00
\u00a0\u3002 \n
\n\n\u53ef\u6267\u884c\u7684 \u64cd\u4f5c\u00a0\u5982\u4e0b\uff1a
\n\n\n\t- \u5c06\u4e00\u4e2a\u00a0
'1'
\u00a0\u53d8\u6210\u4e00\u4e2a\u00a0'0'
\u00a0\u3002 \n\t- \u5c06\u4e00\u4e2a\u00a0
'0'
\u00a0\u53d8\u6210\u4e00\u4e2a\u00a0'1'
\u00a0\u3002 \n\t- \u5c06\u4e00\u4e2a\u00a0
'&'
\u53d8\u6210\u4e00\u4e2a\u00a0'|'
\u00a0\u3002 \n\t- \u5c06\u4e00\u4e2a\u00a0
'|'
\u00a0\u53d8\u6210\u4e00\u4e2a\u00a0'&'
\u00a0\u3002 \n
\n\n\u6ce8\u610f\uff1a'&'
\u00a0\u7684 \u8fd0\u7b97\u4f18\u5148\u7ea7\u00a0\u4e0e\u00a0'|'
\u76f8\u540c\u00a0\u3002\u8ba1\u7b97\u8868\u8fbe\u5f0f\u65f6\uff0c\u62ec\u53f7\u4f18\u5148\u7ea7 \u6700\u9ad8\u00a0\uff0c\u7136\u540e\u6309\u7167 \u4ece\u5de6\u5230\u53f3 \u7684\u987a\u5e8f\u8fd0\u7b97\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1aexpression = \"1&(0|1)\"\n\u8f93\u51fa\uff1a1\n\u89e3\u91ca\uff1a\u6211\u4eec\u53ef\u4ee5\u5c06 \"1&(0|1)\" \u53d8\u6210 \"1&(0&1)\" \uff0c\u6267\u884c\u7684\u64cd\u4f5c\u4e3a\u5c06\u4e00\u4e2a '|' \u53d8\u6210\u4e00\u4e2a '&' \uff0c\u6267\u884c\u4e86 1 \u6b21\u64cd\u4f5c\u3002\n\u65b0\u8868\u8fbe\u5f0f\u7684\u503c\u4e3a 0 \u3002\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1aexpression = \"(0&0)&(0&0&0)\"\n\u8f93\u51fa\uff1a3\n\u89e3\u91ca\uff1a\u6211\u4eec\u53ef\u4ee5\u5c06 \"(0&0)&(0&0&0)\" \u53d8\u6210 \"(0|1)|(0&0&0)\" \uff0c\u6267\u884c\u4e86 3 \u6b21\u64cd\u4f5c\u3002\n\u65b0\u8868\u8fbe\u5f0f\u7684\u503c\u4e3a 1 \u3002\n
\n\n\u793a\u4f8b 3\uff1a
\n\n\u8f93\u5165\uff1aexpression = \"(0|(1|0&1))\"\n\u8f93\u51fa\uff1a1\n\u89e3\u91ca\uff1a\u6211\u4eec\u53ef\u4ee5\u5c06 \"(0|(1|0&1))\" \u53d8\u6210 \"(0|(0|0&1))\" \uff0c\u6267\u884c\u4e86 1 \u6b21\u64cd\u4f5c\u3002\n\u65b0\u8868\u8fbe\u5f0f\u7684\u503c\u4e3a 0 \u3002
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\t1 <= expression.length <= 105
\n\texpression
\u00a0\u53ea\u5305\u542b\u00a0'1'
\uff0c'0'
\uff0c'&'
\uff0c'|'
\uff0c'('
\u00a0\u548c\u00a0')'
\n\t- \u6240\u6709\u62ec\u53f7\u90fd\u6709\u4e0e\u4e4b\u5339\u914d\u7684\u5bf9\u5e94\u62ec\u53f7\u3002
\n\t- \u4e0d\u4f1a\u6709\u7a7a\u7684\u62ec\u53f7\uff08\u4e5f\u5c31\u662f\u8bf4\u00a0
\"()\"
\u00a0\u4e0d\u662f\u00a0expression
\u7684\u5b50\u5b57\u7b26\u4e32\uff09\u3002 \n
\n", "tags_en": ["Stack", "Math", "String", "Dynamic Programming"], "tags_cn": ["\u6808", "\u6570\u5b66", "\u5b57\u7b26\u4e32", "\u52a8\u6001\u89c4\u5212"], "difficulty_en": "Hard", "difficulty_cn": "\u56f0\u96be", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n int minOperationsToFlip(string expression) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int minOperationsToFlip(String expression) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def minOperationsToFlip(self, expression):\n \"\"\"\n :type expression: str\n :rtype: int\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def minOperationsToFlip(self, expression: str) -> int:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nint minOperationsToFlip(char * expression){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int MinOperationsToFlip(string expression) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {string} expression\n * @return {number}\n */\nvar minOperationsToFlip = function(expression) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {String} expression\n# @return {Integer}\ndef min_operations_to_flip(expression)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func minOperationsToFlip(_ expression: String) -> Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func minOperationsToFlip(expression string) int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def minOperationsToFlip(expression: String): Int = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun minOperationsToFlip(expression: String): Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn min_operations_to_flip(expression: String) -> i32 {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param String $expression\n * @return Integer\n */\n function minOperationsToFlip($expression) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function minOperationsToFlip(expression: string): number {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (min-operations-to-flip expression)\n (-> string? exact-integer?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec min_operations_to_flip(Expression :: unicode:unicode_binary()) -> integer().\nmin_operations_to_flip(Expression) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec min_operations_to_flip(expression :: String.t) :: integer\n def min_operations_to_flip(expression) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1896](https://leetcode-cn.com/problems/minimum-cost-to-change-the-final-value-of-expression)", "[\u53cd\u8f6c\u8868\u8fbe\u5f0f\u503c\u7684\u6700\u5c11\u64cd\u4f5c\u6b21\u6570](/solution/1800-1899/1896.Minimum%20Cost%20to%20Change%20the%20Final%20Value%20of%20Expression/README.md)", "`\u6808`,`\u6570\u5b66`,`\u5b57\u7b26\u4e32`,`\u52a8\u6001\u89c4\u5212`", "\u56f0\u96be", ""], "md_table_row_en": ["[1896](https://leetcode.com/problems/minimum-cost-to-change-the-final-value-of-expression)", "[Minimum Cost to Change the Final Value of Expression](/solution/1800-1899/1896.Minimum%20Cost%20to%20Change%20the%20Final%20Value%20of%20Expression/README_EN.md)", "`Stack`,`Math`,`String`,`Dynamic Programming`", "Hard", ""]}, {"question_id": "2006", "frontend_question_id": "1894", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/find-the-student-that-will-replace-the-chalk", "url_en": "https://leetcode.com/problems/find-the-student-that-will-replace-the-chalk", "relative_path_cn": "/solution/1800-1899/1894.Find%20the%20Student%20that%20Will%20Replace%20the%20Chalk/README.md", "relative_path_en": "/solution/1800-1899/1894.Find%20the%20Student%20that%20Will%20Replace%20the%20Chalk/README_EN.md", "title_cn": "\u627e\u5230\u9700\u8981\u8865\u5145\u7c89\u7b14\u7684\u5b66\u751f\u7f16\u53f7", "title_en": "Find the Student that Will Replace the Chalk", "question_title_slug": "find-the-student-that-will-replace-the-chalk", "content_en": "There are n
students in a class numbered from 0
to n - 1
. The teacher will give each student a problem starting with the student number 0
, then the student number 1
, and so on until the teacher reaches the student number n - 1
. After that, the teacher will restart the process, starting with the student number 0
again.
\n\nYou are given a 0-indexed integer array chalk
and an integer k
. There are initially k
pieces of chalk. When the student number i
is given a problem to solve, they will use chalk[i]
pieces of chalk to solve that problem. However, if the current number of chalk pieces is strictly less than chalk[i]
, then the student number i
will be asked to replace the chalk.
\n\nReturn the index of the student that will replace the chalk.
\n\n
\nExample 1:
\n\n\nInput: chalk = [5,1,5], k = 22\nOutput: 0\nExplanation: The students go in turns as follows:\n- Student number 0 uses 5 chalk, so k = 17.\n- Student number 1 uses 1 chalk, so k = 16.\n- Student number 2 uses 5 chalk, so k = 11.\n- Student number 0 uses 5 chalk, so k = 6.\n- Student number 1 uses 1 chalk, so k = 5.\n- Student number 2 uses 5 chalk, so k = 0.\nStudent number 0 does not have enough chalk, so they will have to replace it.
\n\nExample 2:
\n\n\nInput: chalk = [3,4,1,2], k = 25\nOutput: 1\nExplanation: The students go in turns as follows:\n- Student number 0 uses 3 chalk so k = 22.\n- Student number 1 uses 4 chalk so k = 18.\n- Student number 2 uses 1 chalk so k = 17.\n- Student number 3 uses 2 chalk so k = 15.\n- Student number 0 uses 3 chalk so k = 12.\n- Student number 1 uses 4 chalk so k = 8.\n- Student number 2 uses 1 chalk so k = 7.\n- Student number 3 uses 2 chalk so k = 5.\n- Student number 0 uses 3 chalk so k = 2.\nStudent number 1 does not have enough chalk, so they will have to replace it.\n
\n\n
\nConstraints:
\n\n\n\tchalk.length == n
\n\t1 <= n <= 105
\n\t1 <= chalk[i] <= 105
\n\t1 <= k <= 109
\n
\n", "content_cn": "\u4e00\u4e2a\u73ed\u7ea7\u91cc\u6709\u00a0n
\u00a0\u4e2a\u5b66\u751f\uff0c\u7f16\u53f7\u4e3a 0
\u00a0\u5230 n - 1
\u00a0\u3002\u6bcf\u4e2a\u5b66\u751f\u4f1a\u4f9d\u6b21\u56de\u7b54\u95ee\u9898\uff0c\u7f16\u53f7\u4e3a 0
\u00a0\u7684\u5b66\u751f\u5148\u56de\u7b54\uff0c\u7136\u540e\u662f\u7f16\u53f7\u4e3a 1
\u00a0\u7684\u5b66\u751f\uff0c\u4ee5\u6b64\u7c7b\u63a8\uff0c\u76f4\u5230\u7f16\u53f7\u4e3a n - 1
\u00a0\u7684\u5b66\u751f\uff0c\u7136\u540e\u8001\u5e08\u4f1a\u91cd\u590d\u8fd9\u4e2a\u8fc7\u7a0b\uff0c\u91cd\u65b0\u4ece\u7f16\u53f7\u4e3a 0
\u00a0\u7684\u5b66\u751f\u5f00\u59cb\u56de\u7b54\u95ee\u9898\u3002
\n\n\u7ed9\u4f60\u4e00\u4e2a\u957f\u5ea6\u4e3a n
\u00a0\u4e14\u4e0b\u6807\u4ece 0
\u00a0\u5f00\u59cb\u7684\u6574\u6570\u6570\u7ec4\u00a0chalk
\u00a0\u548c\u4e00\u4e2a\u6574\u6570\u00a0k
\u00a0\u3002\u4e00\u5f00\u59cb\u7c89\u7b14\u76d2\u91cc\u603b\u5171\u6709\u00a0k
\u00a0\u652f\u7c89\u7b14\u3002\u5f53\u7f16\u53f7\u4e3a\u00a0i
\u00a0\u7684\u5b66\u751f\u56de\u7b54\u95ee\u9898\u65f6\uff0c\u4ed6\u4f1a\u6d88\u8017 chalk[i]
\u00a0\u652f\u7c89\u7b14\u3002\u5982\u679c\u5269\u4f59\u7c89\u7b14\u6570\u91cf \u4e25\u683c\u5c0f\u4e8e\u00a0chalk[i]
\u00a0\uff0c\u90a3\u4e48\u5b66\u751f i
\u00a0\u9700\u8981 \u8865\u5145\u00a0\u7c89\u7b14\u3002
\n\n\u8bf7\u4f60\u8fd4\u56de\u9700\u8981 \u8865\u5145\u00a0\u7c89\u7b14\u7684\u5b66\u751f \u7f16\u53f7\u00a0\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1achalk = [5,1,5], k = 22\n\u8f93\u51fa\uff1a0\n\u89e3\u91ca\uff1a\u5b66\u751f\u6d88\u8017\u7c89\u7b14\u60c5\u51b5\u5982\u4e0b\uff1a\n- \u7f16\u53f7\u4e3a 0 \u7684\u5b66\u751f\u4f7f\u7528 5 \u652f\u7c89\u7b14\uff0c\u7136\u540e k = 17 \u3002\n- \u7f16\u53f7\u4e3a 1 \u7684\u5b66\u751f\u4f7f\u7528 1 \u652f\u7c89\u7b14\uff0c\u7136\u540e k = 16 \u3002\n- \u7f16\u53f7\u4e3a 2 \u7684\u5b66\u751f\u4f7f\u7528 5 \u652f\u7c89\u7b14\uff0c\u7136\u540e k = 11 \u3002\n- \u7f16\u53f7\u4e3a 0 \u7684\u5b66\u751f\u4f7f\u7528 5 \u652f\u7c89\u7b14\uff0c\u7136\u540e k = 6 \u3002\n- \u7f16\u53f7\u4e3a 1 \u7684\u5b66\u751f\u4f7f\u7528 1 \u652f\u7c89\u7b14\uff0c\u7136\u540e k = 5 \u3002\n- \u7f16\u53f7\u4e3a 2 \u7684\u5b66\u751f\u4f7f\u7528 5 \u652f\u7c89\u7b14\uff0c\u7136\u540e k = 0 \u3002\n\u7f16\u53f7\u4e3a 0 \u7684\u5b66\u751f\u6ca1\u6709\u8db3\u591f\u7684\u7c89\u7b14\uff0c\u6240\u4ee5\u4ed6\u9700\u8981\u8865\u5145\u7c89\u7b14\u3002
\n\n\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1achalk = [3,4,1,2], k = 25\n\u8f93\u51fa\uff1a1\n\u89e3\u91ca\uff1a\u5b66\u751f\u6d88\u8017\u7c89\u7b14\u60c5\u51b5\u5982\u4e0b\uff1a\n- \u7f16\u53f7\u4e3a 0 \u7684\u5b66\u751f\u4f7f\u7528 3 \u652f\u7c89\u7b14\uff0c\u7136\u540e k = 22 \u3002\n- \u7f16\u53f7\u4e3a 1 \u7684\u5b66\u751f\u4f7f\u7528 4 \u652f\u7c89\u7b14\uff0c\u7136\u540e k = 18 \u3002\n- \u7f16\u53f7\u4e3a 2 \u7684\u5b66\u751f\u4f7f\u7528 1 \u652f\u7c89\u7b14\uff0c\u7136\u540e k = 17 \u3002\n- \u7f16\u53f7\u4e3a 3 \u7684\u5b66\u751f\u4f7f\u7528 2 \u652f\u7c89\u7b14\uff0c\u7136\u540e k = 15 \u3002\n- \u7f16\u53f7\u4e3a 0 \u7684\u5b66\u751f\u4f7f\u7528 3 \u652f\u7c89\u7b14\uff0c\u7136\u540e k = 12 \u3002\n- \u7f16\u53f7\u4e3a 1 \u7684\u5b66\u751f\u4f7f\u7528 4 \u652f\u7c89\u7b14\uff0c\u7136\u540e k = 8 \u3002\n- \u7f16\u53f7\u4e3a 2 \u7684\u5b66\u751f\u4f7f\u7528 1 \u652f\u7c89\u7b14\uff0c\u7136\u540e k = 7 \u3002\n- \u7f16\u53f7\u4e3a 3 \u7684\u5b66\u751f\u4f7f\u7528 2 \u652f\u7c89\u7b14\uff0c\u7136\u540e k = 5 \u3002\n- \u7f16\u53f7\u4e3a 0 \u7684\u5b66\u751f\u4f7f\u7528 3 \u652f\u7c89\u7b14\uff0c\u7136\u540e k = 2 \u3002\n\u7f16\u53f7\u4e3a 1 \u7684\u5b66\u751f\u6ca1\u6709\u8db3\u591f\u7684\u7c89\u7b14\uff0c\u6240\u4ee5\u4ed6\u9700\u8981\u8865\u5145\u7c89\u7b14\u3002\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\tchalk.length == n
\n\t1 <= n <= 105
\n\t1 <= chalk[i] <= 105
\n\t1 <= k <= 109
\n
\n", "tags_en": ["Array", "Binary Search", "Prefix Sum", "Simulation"], "tags_cn": ["\u6570\u7ec4", "\u4e8c\u5206\u67e5\u627e", "\u524d\u7f00\u548c", "\u6a21\u62df"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n int chalkReplacer(vector& chalk, int k) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int chalkReplacer(int[] chalk, int k) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def chalkReplacer(self, chalk, k):\n \"\"\"\n :type chalk: List[int]\n :type k: int\n :rtype: int\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def chalkReplacer(self, chalk: List[int], k: int) -> int:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nint chalkReplacer(int* chalk, int chalkSize, int k){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int ChalkReplacer(int[] chalk, int k) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[]} chalk\n * @param {number} k\n * @return {number}\n */\nvar chalkReplacer = function(chalk, k) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer[]} chalk\n# @param {Integer} k\n# @return {Integer}\ndef chalk_replacer(chalk, k)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func chalkReplacer(_ chalk: [Int], _ k: Int) -> Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func chalkReplacer(chalk []int, k int) int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def chalkReplacer(chalk: Array[Int], k: Int): Int = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun chalkReplacer(chalk: IntArray, k: Int): Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn chalk_replacer(chalk: Vec, k: i32) -> i32 {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer[] $chalk\n * @param Integer $k\n * @return Integer\n */\n function chalkReplacer($chalk, $k) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function chalkReplacer(chalk: number[], k: number): number {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (chalk-replacer chalk k)\n (-> (listof exact-integer?) exact-integer? exact-integer?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec chalk_replacer(Chalk :: [integer()], K :: integer()) -> integer().\nchalk_replacer(Chalk, K) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec chalk_replacer(chalk :: [integer], k :: integer) :: integer\n def chalk_replacer(chalk, k) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1894](https://leetcode-cn.com/problems/find-the-student-that-will-replace-the-chalk)", "[\u627e\u5230\u9700\u8981\u8865\u5145\u7c89\u7b14\u7684\u5b66\u751f\u7f16\u53f7](/solution/1800-1899/1894.Find%20the%20Student%20that%20Will%20Replace%20the%20Chalk/README.md)", "`\u6570\u7ec4`,`\u4e8c\u5206\u67e5\u627e`,`\u524d\u7f00\u548c`,`\u6a21\u62df`", "\u4e2d\u7b49", ""], "md_table_row_en": ["[1894](https://leetcode.com/problems/find-the-student-that-will-replace-the-chalk)", "[Find the Student that Will Replace the Chalk](/solution/1800-1899/1894.Find%20the%20Student%20that%20Will%20Replace%20the%20Chalk/README_EN.md)", "`Array`,`Binary Search`,`Prefix Sum`,`Simulation`", "Medium", ""]}, {"question_id": "2005", "frontend_question_id": "1893", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/check-if-all-the-integers-in-a-range-are-covered", "url_en": "https://leetcode.com/problems/check-if-all-the-integers-in-a-range-are-covered", "relative_path_cn": "/solution/1800-1899/1893.Check%20if%20All%20the%20Integers%20in%20a%20Range%20Are%20Covered/README.md", "relative_path_en": "/solution/1800-1899/1893.Check%20if%20All%20the%20Integers%20in%20a%20Range%20Are%20Covered/README_EN.md", "title_cn": "\u68c0\u67e5\u662f\u5426\u533a\u57df\u5185\u6240\u6709\u6574\u6570\u90fd\u88ab\u8986\u76d6", "title_en": "Check if All the Integers in a Range Are Covered", "question_title_slug": "check-if-all-the-integers-in-a-range-are-covered", "content_en": "You are given a 2D integer array ranges
and two integers left
and right
. Each ranges[i] = [starti, endi]
represents an inclusive interval between starti
and endi
.
\n\nReturn true
if each integer in the inclusive range [left, right]
is covered by at least one interval in ranges
. Return false
otherwise.
\n\nAn integer x
is covered by an interval ranges[i] = [starti, endi]
if starti <= x <= endi
.
\n\n
\nExample 1:
\n\n\nInput: ranges = [[1,2],[3,4],[5,6]], left = 2, right = 5\nOutput: true\nExplanation: Every integer between 2 and 5 is covered:\n- 2 is covered by the first range.\n- 3 and 4 are covered by the second range.\n- 5 is covered by the third range.\n
\n\nExample 2:
\n\n\nInput: ranges = [[1,10],[10,20]], left = 21, right = 21\nOutput: false\nExplanation: 21 is not covered by any range.\n
\n\n
\nConstraints:
\n\n\n\t1 <= ranges.length <= 50
\n\t1 <= starti <= endi <= 50
\n\t1 <= left <= right <= 50
\n
\n", "content_cn": "\u7ed9\u4f60\u4e00\u4e2a\u4e8c\u7ef4\u6574\u6570\u6570\u7ec4\u00a0ranges
\u00a0\u548c\u4e24\u4e2a\u6574\u6570\u00a0left
\u00a0\u548c\u00a0right
\u00a0\u3002\u6bcf\u4e2a\u00a0ranges[i] = [starti, endi]
\u00a0\u8868\u793a\u4e00\u4e2a\u4ece\u00a0starti
\u00a0\u5230\u00a0endi
\u00a0\u7684\u00a0\u95ed\u533a\u95f4\u00a0\u3002
\n\n\u5982\u679c\u95ed\u533a\u95f4\u00a0[left, right]
\u00a0\u5185\u6bcf\u4e2a\u6574\u6570\u90fd\u88ab\u00a0ranges
\u00a0\u4e2d\u00a0\u81f3\u5c11\u4e00\u4e2a\u00a0\u533a\u95f4\u8986\u76d6\uff0c\u90a3\u4e48\u8bf7\u4f60\u8fd4\u56de\u00a0true
\u00a0\uff0c\u5426\u5219\u8fd4\u56de\u00a0false
\u00a0\u3002
\n\n\u5df2\u77e5\u533a\u95f4 ranges[i] = [starti, endi]
\uff0c\u5982\u679c\u6574\u6570 x
\u6ee1\u8db3 starti <= x <= endi
\u00a0\uff0c\u90a3\u4e48\u6211\u4eec\u79f0\u6574\u6570x
\u00a0\u88ab\u8986\u76d6\u4e86\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1aranges = [[1,2],[3,4],[5,6]], left = 2, right = 5\n\u8f93\u51fa\uff1atrue\n\u89e3\u91ca\uff1a2 \u5230 5 \u7684\u6bcf\u4e2a\u6574\u6570\u90fd\u88ab\u8986\u76d6\u4e86\uff1a\n- 2 \u88ab\u7b2c\u4e00\u4e2a\u533a\u95f4\u8986\u76d6\u3002\n- 3 \u548c 4 \u88ab\u7b2c\u4e8c\u4e2a\u533a\u95f4\u8986\u76d6\u3002\n- 5 \u88ab\u7b2c\u4e09\u4e2a\u533a\u95f4\u8986\u76d6\u3002\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1aranges = [[1,10],[10,20]], left = 21, right = 21\n\u8f93\u51fa\uff1afalse\n\u89e3\u91ca\uff1a21 \u6ca1\u6709\u88ab\u4efb\u4f55\u4e00\u4e2a\u533a\u95f4\u8986\u76d6\u3002\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\t1 <= ranges.length <= 50
\n\t1 <= starti <= endi <= 50
\n\t1 <= left <= right <= 50
\n
\n", "tags_en": ["Array", "Hash Table", "Prefix Sum"], "tags_cn": ["\u6570\u7ec4", "\u54c8\u5e0c\u8868", "\u524d\u7f00\u548c"], "difficulty_en": "Easy", "difficulty_cn": "\u7b80\u5355", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n bool isCovered(vector>& ranges, int left, int right) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public boolean isCovered(int[][] ranges, int left, int right) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def isCovered(self, ranges, left, right):\n \"\"\"\n :type ranges: List[List[int]]\n :type left: int\n :type right: int\n :rtype: bool\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nbool isCovered(int** ranges, int rangesSize, int* rangesColSize, int left, int right){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public bool IsCovered(int[][] ranges, int left, int right) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[][]} ranges\n * @param {number} left\n * @param {number} right\n * @return {boolean}\n */\nvar isCovered = function(ranges, left, right) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer[][]} ranges\n# @param {Integer} left\n# @param {Integer} right\n# @return {Boolean}\ndef is_covered(ranges, left, right)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func isCovered(_ ranges: [[Int]], _ left: Int, _ right: Int) -> Bool {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func isCovered(ranges [][]int, left int, right int) bool {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def isCovered(ranges: Array[Array[Int]], left: Int, right: Int): Boolean = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun isCovered(ranges: Array, left: Int, right: Int): Boolean {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn is_covered(ranges: Vec>, left: i32, right: i32) -> bool {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer[][] $ranges\n * @param Integer $left\n * @param Integer $right\n * @return Boolean\n */\n function isCovered($ranges, $left, $right) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function isCovered(ranges: number[][], left: number, right: number): boolean {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (is-covered ranges left right)\n (-> (listof (listof exact-integer?)) exact-integer? exact-integer? boolean?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec is_covered(Ranges :: [[integer()]], Left :: integer(), Right :: integer()) -> boolean().\nis_covered(Ranges, Left, Right) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec is_covered(ranges :: [[integer]], left :: integer, right :: integer) :: boolean\n def is_covered(ranges, left, right) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1893](https://leetcode-cn.com/problems/check-if-all-the-integers-in-a-range-are-covered)", "[\u68c0\u67e5\u662f\u5426\u533a\u57df\u5185\u6240\u6709\u6574\u6570\u90fd\u88ab\u8986\u76d6](/solution/1800-1899/1893.Check%20if%20All%20the%20Integers%20in%20a%20Range%20Are%20Covered/README.md)", "`\u6570\u7ec4`,`\u54c8\u5e0c\u8868`,`\u524d\u7f00\u548c`", "\u7b80\u5355", ""], "md_table_row_en": ["[1893](https://leetcode.com/problems/check-if-all-the-integers-in-a-range-are-covered)", "[Check if All the Integers in a Range Are Covered](/solution/1800-1899/1893.Check%20if%20All%20the%20Integers%20in%20a%20Range%20Are%20Covered/README_EN.md)", "`Array`,`Hash Table`,`Prefix Sum`", "Easy", ""]}, {"question_id": "2004", "frontend_question_id": "1853", "paid_only": true, "paid_only_cn": true, "category": "Database", "url_cn": "https://leetcode-cn.com/problems/convert-date-format", "url_en": "https://leetcode.com/problems/convert-date-format", "relative_path_cn": "/solution/1800-1899/1853.Convert%20Date%20Format/README.md", "relative_path_en": "/solution/1800-1899/1853.Convert%20Date%20Format/README_EN.md", "title_cn": "\u8f6c\u6362\u65e5\u671f\u683c\u5f0f", "title_en": "Convert Date Format", "question_title_slug": "convert-date-format", "content_en": "Table: Days
\n\n\n+-------------+------+\n| Column Name | Type |\n+-------------+------+\n| day | date |\n+-------------+------+\nday is the primary key for this table.\n
\n\n
\n\nWrite an SQL query to convert each date in Days
into a string formatted as "day_name, month_name day, year"
.
\n\nReturn the result table in any order.
\n\nThe query result format is in the following example:
\n\n
\n\n\nDays table:\n+------------+\n| day |\n+------------+\n| 2022-04-12 |\n| 2021-08-09 |\n| 2020-06-26 |\n+------------+\n\nResult table:\n+-------------------------+\n| day |\n+-------------------------+\n| Tuesday, April 12, 2022 |\n| Monday, August 9, 2021 |\n| Friday, June 26, 2020 |\n+-------------------------+\nPlease note that the output is case-sensitive.\n
\n", "content_cn": "\u8868: Days
\n\n+-------------+------+\n| Column Name | Type |\n+-------------+------+\n| day | date |\n+-------------+------+\nday \u662f\u8fd9\u4e2a\u8868\u7684\u4e3b\u952e\u3002\n
\n\n\u00a0
\n\n\u7ed9\u5b9a\u4e00\u4e2aDays
\u8868\uff0c\u8bf7\u4f60\u7f16\u5199SQL\u67e5\u8be2\u8bed\u53e5\uff0c\u5c06Days
\u8868\u4e2d\u7684\u6bcf\u4e00\u4e2a\u65e5\u671f\u8f6c\u5316\u4e3a\"day_name, month_name day, year\"
\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u3002
\n\n\u8fd4\u56de\u7684\u7ed3\u679c\u8868\u4e0d\u8ba1\u987a\u5e8f\u3002
\n\n\u00a0
\n\n\u4f8b\u5982\uff1a
\n\nDays table:\n+------------+\n| day |\n+------------+\n| 2022-04-12 |\n| 2021-08-09 |\n| 2020-06-26 |\n+------------+\n\nResult table:\n+-------------------------+\n| day |\n+-------------------------+\n| Tuesday, April 12, 2022 |\n| Monday, August 9, 2021 |\n| Friday, June 26, 2020 |\n+-------------------------+\n\u8bf7\u6ce8\u610f\uff0c\u8f93\u51fa\u5bf9\u5927\u5c0f\u5199\u654f\u611f\u3002\n
\n", "tags_en": ["Database"], "tags_cn": ["\u6570\u636e\u5e93"], "difficulty_en": "Easy", "difficulty_cn": "\u7b80\u5355", "code_snippets": [{"lang": "MySQL", "langSlug": "mysql", "code": "# Write your MySQL query statement below", "__typename": "CodeSnippetNode"}, {"lang": "MS SQL Server", "langSlug": "mssql", "code": "/* Write your T-SQL query statement below */", "__typename": "CodeSnippetNode"}, {"lang": "Oracle", "langSlug": "oraclesql", "code": "/* Write your PL/SQL query statement below */", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1853](https://leetcode-cn.com/problems/convert-date-format)", "[\u8f6c\u6362\u65e5\u671f\u683c\u5f0f](/solution/1800-1899/1853.Convert%20Date%20Format/README.md)", "`\u6570\u636e\u5e93`", "\u7b80\u5355", "\ud83d\udd12"], "md_table_row_en": ["[1853](https://leetcode.com/problems/convert-date-format)", "[Convert Date Format](/solution/1800-1899/1853.Convert%20Date%20Format/README_EN.md)", "`Database`", "Easy", "\ud83d\udd12"]}, {"question_id": "2003", "frontend_question_id": "1852", "paid_only": true, "paid_only_cn": true, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/distinct-numbers-in-each-subarray", "url_en": "https://leetcode.com/problems/distinct-numbers-in-each-subarray", "relative_path_cn": "/solution/1800-1899/1852.Distinct%20Numbers%20in%20Each%20Subarray/README.md", "relative_path_en": "/solution/1800-1899/1852.Distinct%20Numbers%20in%20Each%20Subarray/README_EN.md", "title_cn": "\u6bcf\u4e2a\u5b50\u6570\u7ec4\u7684\u6570\u5b57\u79cd\u7c7b\u6570", "title_en": "Distinct Numbers in Each Subarray", "question_title_slug": "distinct-numbers-in-each-subarray", "content_en": "Given an integer array nums
and an integer k
, you are asked to construct the array ans
of size n-k+1
where ans[i]
is the number of distinct numbers in the subarray nums[i:i+k-1] = [nums[i], nums[i+1], ..., nums[i+k-1]]
.
\r\n\r\nReturn the array ans
.
\r\n\r\n
\r\nExample 1:
\r\n\r\n\r\nInput: nums = [1,2,3,2,2,1,3], k = 3\r\nOutput: [3,2,2,2,3]\r\nExplanation: The number of distinct elements in each subarray goes as follows:\r\n- nums[0:2] = [1,2,3] so ans[0] = 3\r\n- nums[1:3] = [2,3,2] so ans[1] = 2\r\n- nums[2:4] = [3,2,2] so ans[2] = 2\r\n- nums[3:5] = [2,2,1] so ans[3] = 2\r\n- nums[4:6] = [2,1,3] so ans[4] = 3\r\n
\r\n\r\nExample 2:
\r\n\r\n\r\nInput: nums = [1,1,1,1,2,3,4], k = 4\r\nOutput: [1,2,3,4]\r\nExplanation: The number of distinct elements in each subarray goes as follows:\r\n- nums[0:3] = [1,1,1,1] so ans[0] = 1\r\n- nums[1:4] = [1,1,1,2] so ans[1] = 2\r\n- nums[2:5] = [1,1,2,3] so ans[2] = 3\r\n- nums[3:6] = [1,2,3,4] so ans[3] = 4\r\n
\r\n\r\n
\r\nConstraints:
\r\n\r\n\r\n\t1 <= k <= nums.length <= 105
\r\n\t1 <= nums[i] <= 105
\r\n
", "content_cn": "\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4\u00a0nums
\u4e0e\u4e00\u4e2a\u6574\u6570 k
\uff0c\u8bf7\u4f60\u6784\u9020\u4e00\u4e2a\u957f\u5ea6 n-k+1
\u7684\u6570\u7ec4 ans
\uff0c\u8fd9\u4e2a\u6570\u7ec4\u7b2ci
\u4e2a\u5143\u7d20 ans[i]
\u662f\u6bcf\u4e2a\u957f\u5ea6\u4e3ak\u7684\u5b50\u6570\u7ec4 nums[i:i+k-1] = [nums[i], nums[i+1], ..., nums[i+k-1]]
\u4e2d\u6570\u5b57\u7684\u79cd\u7c7b\u6570\u3002
\n\n\u8fd4\u56de\u8fd9\u4e2a\u6570\u7ec4 ans
\u3002
\n\n\u793a\u4f8b 1:
\n\n\u8f93\u5165: nums = [1,2,3,2,2,1,3], k = 3\n\u8f93\u51fa: [3,2,2,2,3]\n\u89e3\u91ca\uff1a\u6bcf\u4e2a\u5b50\u6570\u7ec4\u7684\u6570\u5b57\u79cd\u7c7b\u8ba1\u7b97\u65b9\u6cd5\u5982\u4e0b\uff1a\n- nums[0:2] = [1,2,3] \u6709'1','2','3'\u4e09\u79cd\u6570\u5b57\u6240\u4ee5 ans[0] = 3\n- nums[1:3] = [2,3,2] \u6709'2','3'\u4e24\u79cd\u6570\u5b57\u6240\u4ee5 ans[1] = 2\n- nums[2:4] = [3,2,2] \u6709'2','3'\u4e24\u79cd\u6570\u5b57\u6240\u4ee5 ans[2] = 2\n- nums[3:5] = [2,2,1] \u6709'1','2'\u4e24\u79cd\u6570\u5b57\u6240\u4ee5 ans[3] = 2\n- nums[4:6] = [2,1,3] \u6709'1','2','3'\u4e09\u79cd\u6570\u5b57\u6240\u4ee5 ans[4] = 3\n
\n\n\u793a\u4f8b\u00a02:
\n\n\u8f93\u5165: nums = [1,1,1,1,2,3,4], k = 4\n\u8f93\u51fa: [1,2,3,4]\n\u89e3\u91ca: \u6bcf\u4e2a\u5b50\u6570\u7ec4\u7684\u6570\u5b57\u79cd\u7c7b\u8ba1\u7b97\u65b9\u6cd5\u5982\u4e0b\uff1a\n- nums[0:3] = [1,1,1,1] \u53ea\u6709'1'\u8fd9\u4e00\u79cd\u6570\u5b57\u6240\u4ee5 ans[0] = 1\n- nums[1:4] = [1,1,1,2] \u6709'1','2'\u4e24\u79cd\u6570\u5b57\u6240\u4ee5 ans[1] = 2\n- nums[2:5] = [1,1,2,3] \u6709'1','2','3'\u4e09\u79cd\u6570\u5b57\u6240\u4ee5 ans[2] = 3\n- nums[3:6] = [1,2,3,4] \u6709'1','2','3','4'\u56db\u79cd\u6570\u5b57\u6240\u4ee5 ans[3] = 4\n
\n\n\u00a0
\n\n\u63d0\u793a:
\n\n\n\t1 <= k <= nums.length <= 105
\n\t1 <= nums[i] <= 105
\n
\n", "tags_en": ["Array", "Hash Table", "Sliding Window"], "tags_cn": ["\u6570\u7ec4", "\u54c8\u5e0c\u8868", "\u6ed1\u52a8\u7a97\u53e3"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n vector distinctNumbers(vector& nums, int k) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int[] distinctNumbers(int[] nums, int k) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def distinctNumbers(self, nums, k):\n \"\"\"\n :type nums: List[int]\n :type k: int\n :rtype: List[int]\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def distinctNumbers(self, nums: List[int], k: int) -> List[int]:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\n/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\nint* distinctNumbers(int* nums, int numsSize, int k, int* returnSize){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int[] DistinctNumbers(int[] nums, int k) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[]} nums\n * @param {number} k\n * @return {number[]}\n */\nvar distinctNumbers = function(nums, k) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer[]} nums\n# @param {Integer} k\n# @return {Integer[]}\ndef distinct_numbers(nums, k)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func distinctNumbers(_ nums: [Int], _ k: Int) -> [Int] {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func distinctNumbers(nums []int, k int) []int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def distinctNumbers(nums: Array[Int], k: Int): Array[Int] = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun distinctNumbers(nums: IntArray, k: Int): IntArray {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn distinct_numbers(nums: Vec, k: i32) -> Vec {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @param Integer $k\n * @return Integer[]\n */\n function distinctNumbers($nums, $k) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function distinctNumbers(nums: number[], k: number): number[] {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (distinct-numbers nums k)\n (-> (listof exact-integer?) exact-integer? (listof exact-integer?))\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec distinct_numbers(Nums :: [integer()], K :: integer()) -> [integer()].\ndistinct_numbers(Nums, K) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec distinct_numbers(nums :: [integer], k :: integer) :: [integer]\n def distinct_numbers(nums, k) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1852](https://leetcode-cn.com/problems/distinct-numbers-in-each-subarray)", "[\u6bcf\u4e2a\u5b50\u6570\u7ec4\u7684\u6570\u5b57\u79cd\u7c7b\u6570](/solution/1800-1899/1852.Distinct%20Numbers%20in%20Each%20Subarray/README.md)", "`\u6570\u7ec4`,`\u54c8\u5e0c\u8868`,`\u6ed1\u52a8\u7a97\u53e3`", "\u4e2d\u7b49", "\ud83d\udd12"], "md_table_row_en": ["[1852](https://leetcode.com/problems/distinct-numbers-in-each-subarray)", "[Distinct Numbers in Each Subarray](/solution/1800-1899/1852.Distinct%20Numbers%20in%20Each%20Subarray/README_EN.md)", "`Array`,`Hash Table`,`Sliding Window`", "Medium", "\ud83d\udd12"]}, {"question_id": "2002", "frontend_question_id": "1872", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/stone-game-viii", "url_en": "https://leetcode.com/problems/stone-game-viii", "relative_path_cn": "/solution/1800-1899/1872.Stone%20Game%20VIII/README.md", "relative_path_en": "/solution/1800-1899/1872.Stone%20Game%20VIII/README_EN.md", "title_cn": "\u77f3\u5b50\u6e38\u620f VIII", "title_en": "Stone Game VIII", "question_title_slug": "stone-game-viii", "content_en": "Alice and Bob take turns playing a game, with Alice starting first.
\r\n\r\nThere 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:
\r\n\r\n\r\n\t- Choose an integer
x > 1
, and remove the leftmost x
stones from the row. \r\n\t- Add the sum of the removed stones' values to the player's score.
\r\n\t- Place a new stone, whose value is equal to that sum, on the left side of the row.
\r\n
\r\n\r\nThe game stops when only one stone is left in the row.
\r\n\r\nThe score difference between Alice and Bob is (Alice's score - Bob's score)
. Alice's goal is to maximize the score difference, and Bob's goal is the minimize the score difference.
\r\n\r\nGiven 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.
\r\n\r\n
\r\nExample 1:
\r\n\r\n\r\nInput: stones = [-1,2,-3,4,-5]\r\nOutput: 5\r\nExplanation:\r\n- Alice removes the first 4 stones, adds (-1) + 2 + (-3) + 4 = 2 to her score, and places a stone of\r\n value 2 on the left. stones = [2,-5].\r\n- Bob removes the first 2 stones, adds 2 + (-5) = -3 to his score, and places a stone of value -3 on\r\n the left. stones = [-3].\r\nThe difference between their scores is 2 - (-3) = 5.\r\n
\r\n\r\nExample 2:
\r\n\r\n\r\nInput: stones = [7,-6,5,10,5,-2,-6]\r\nOutput: 13\r\nExplanation:\r\n- Alice removes all stones, adds 7 + (-6) + 5 + 10 + 5 + (-2) + (-6) = 13 to her score, and places a\r\n stone of value 13 on the left. stones = [13].\r\nThe difference between their scores is 13 - 0 = 13.\r\n
\r\n\r\nExample 3:
\r\n\r\n\r\nInput: stones = [-10,-12]\r\nOutput: -22\r\nExplanation:\r\n- Alice can only make one move, which is to remove both stones. She adds (-10) + (-12) = -22 to her\r\n score and places a stone of value -22 on the left. stones = [-22].\r\nThe difference between their scores is (-22) - 0 = -22.\r\n
\r\n\r\n
\r\nConstraints:
\r\n\r\n\r\n\tn == stones.length
\r\n\t2 <= n <= 105
\r\n\t-104 <= stones[i] <= 104
\r\n
", "content_cn": "Alice \u548c Bob \u73a9\u4e00\u4e2a\u6e38\u620f\uff0c\u4e24\u4eba\u8f6e\u6d41\u64cd\u4f5c\uff0c Alice \u5148\u624b\u00a0\u3002
\n\n\u603b\u5171\u6709\u00a0n
\u00a0\u4e2a\u77f3\u5b50\u6392\u6210\u4e00\u884c\u3002\u8f6e\u5230\u67d0\u4e2a\u73a9\u5bb6\u7684\u56de\u5408\u65f6\uff0c\u5982\u679c\u77f3\u5b50\u7684\u6570\u76ee \u5927\u4e8e 1\u00a0\uff0c\u4ed6\u5c06\u6267\u884c\u4ee5\u4e0b\u64cd\u4f5c\uff1a
\n\n\n\t- \u9009\u62e9\u4e00\u4e2a\u6574\u6570\u00a0
x > 1
\u00a0\uff0c\u5e76\u4e14 \u79fb\u9664\u00a0\u6700\u5de6\u8fb9\u7684\u00a0x
\u00a0\u4e2a\u77f3\u5b50\u3002 \n\t- \u5c06\u00a0\u79fb\u9664\u00a0\u7684\u77f3\u5b50\u4ef7\u503c\u4e4b \u548c\u00a0\u7d2f\u52a0\u5230\u8be5\u73a9\u5bb6\u7684\u5206\u6570\u4e2d\u3002
\n\t- \u5c06\u4e00\u4e2a \u65b0\u7684\u77f3\u5b50\u00a0\u653e\u5728\u6700\u5de6\u8fb9\uff0c\u4e14\u65b0\u77f3\u5b50\u7684\u503c\u4e3a\u88ab\u79fb\u9664\u77f3\u5b50\u503c\u4e4b\u548c\u3002
\n
\n\n\u5f53\u53ea\u5269\u4e0b \u4e00\u4e2a\u00a0\u77f3\u5b50\u65f6\uff0c\u6e38\u620f\u7ed3\u675f\u3002
\n\nAlice \u548c Bob \u7684 \u5206\u6570\u4e4b\u5dee\u00a0\u4e3a\u00a0(Alice \u7684\u5206\u6570\u00a0- Bob \u7684\u5206\u6570)
\u00a0\u3002\u00a0Alice \u7684\u76ee\u6807\u662f\u00a0\u6700\u5927\u5316\u00a0\u5206\u6570\u5dee\uff0cBob \u7684\u76ee\u6807\u662f \u6700\u5c0f\u5316\u00a0\u5206\u6570\u5dee\u3002
\n\n\u7ed9\u4f60\u4e00\u4e2a\u957f\u5ea6\u4e3a n
\u00a0\u7684\u6574\u6570\u6570\u7ec4\u00a0stones
\u00a0\uff0c\u5176\u4e2d\u00a0stones[i]
\u00a0\u662f \u4ece\u5de6\u8fb9\u8d77\u00a0\u7b2c\u00a0i
\u00a0\u4e2a\u77f3\u5b50\u7684\u4ef7\u503c\u3002\u8bf7\u4f60\u8fd4\u56de\u5728\u53cc\u65b9\u90fd\u91c7\u7528 \u6700\u4f18 \u7b56\u7565\u7684\u60c5\u51b5\u4e0b\uff0cAlice \u548c Bob \u7684 \u5206\u6570\u4e4b\u5dee \u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1astones = [-1,2,-3,4,-5]\n\u8f93\u51fa\uff1a5\n\u89e3\u91ca\uff1a\n- Alice \u79fb\u9664\u6700\u5de6\u8fb9\u7684 4 \u4e2a\u77f3\u5b50\uff0c\u5f97\u5206\u589e\u52a0 (-1) + 2 + (-3) + 4 = 2 \uff0c\u5e76\u4e14\u5c06\u4e00\u4e2a\u4ef7\u503c\u4e3a 2 \u7684\u77f3\u5b50\u653e\u5728\u6700\u5de6\u8fb9\u3002stones = [2,-5] \u3002\n- Bob \u79fb\u9664\u6700\u5de6\u8fb9\u7684 2 \u4e2a\u77f3\u5b50\uff0c\u5f97\u5206\u589e\u52a0 2 + (-5) = -3 \uff0c\u5e76\u4e14\u5c06\u4e00\u4e2a\u4ef7\u503c\u4e3a -3 \u7684\u77f3\u5b50\u653e\u5728\u6700\u5de6\u8fb9\u3002stones = [-3] \u3002\n\u4e24\u8005\u5206\u6570\u4e4b\u5dee\u4e3a 2 - (-3) = 5 \u3002\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1astones = [7,-6,5,10,5,-2,-6]\n\u8f93\u51fa\uff1a13\n\u89e3\u91ca\uff1a\n- Alice \u79fb\u9664\u6240\u6709\u77f3\u5b50\uff0c\u5f97\u5206\u589e\u52a0 7 + (-6) + 5 + 10 + 5 + (-2) + (-6) = 13 \uff0c\u5e76\u4e14\u5c06\u4e00\u4e2a\u4ef7\u503c\u4e3a 13 \u7684\u77f3\u5b50\u653e\u5728\u6700\u5de6\u8fb9\u3002stones = [13] \u3002\n\u4e24\u8005\u5206\u6570\u4e4b\u5dee\u4e3a 13 - 0 = 13 \u3002\n
\n\n\u793a\u4f8b 3\uff1a
\n\n\u8f93\u5165\uff1astones = [-10,-12]\n\u8f93\u51fa\uff1a-22\n\u89e3\u91ca\uff1a\n- Alice \u53ea\u6709\u4e00\u79cd\u64cd\u4f5c\uff0c\u5c31\u662f\u79fb\u9664\u6240\u6709\u77f3\u5b50\u3002\u5f97\u5206\u589e\u52a0 (-10) + (-12) = -22 \uff0c\u5e76\u4e14\u5c06\u4e00\u4e2a\u4ef7\u503c\u4e3a -22 \u7684\u77f3\u5b50\u653e\u5728\u6700\u5de6\u8fb9\u3002stones = [-22] \u3002\n\u4e24\u8005\u5206\u6570\u4e4b\u5dee\u4e3a (-22) - 0 = -22 \u3002\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\tn == stones.length
\n\t2 <= n <= 105
\n\t-104 <= stones[i] <= 104
\n
\n", "tags_en": ["Array", "Math", "Dynamic Programming", "Game Theory", "Prefix Sum"], "tags_cn": ["\u6570\u7ec4", "\u6570\u5b66", "\u52a8\u6001\u89c4\u5212", "\u535a\u5f08", "\u524d\u7f00\u548c"], "difficulty_en": "Hard", "difficulty_cn": "\u56f0\u96be", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n int stoneGameVIII(vector& stones) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int stoneGameVIII(int[] stones) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def stoneGameVIII(self, stones):\n \"\"\"\n :type stones: List[int]\n :rtype: int\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def stoneGameVIII(self, stones: List[int]) -> int:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nint stoneGameVIII(int* stones, int stonesSize){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int StoneGameVIII(int[] stones) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[]} stones\n * @return {number}\n */\nvar stoneGameVIII = function(stones) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer[]} stones\n# @return {Integer}\ndef stone_game_viii(stones)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func stoneGameVIII(_ stones: [Int]) -> Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func stoneGameVIII(stones []int) int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def stoneGameVIII(stones: Array[Int]): Int = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun stoneGameVIII(stones: IntArray): Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn stone_game_viii(stones: Vec) -> i32 {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer[] $stones\n * @return Integer\n */\n function stoneGameVIII($stones) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function stoneGameVIII(stones: number[]): number {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (stone-game-viii stones)\n (-> (listof exact-integer?) exact-integer?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec stone_game_viii(Stones :: [integer()]) -> integer().\nstone_game_viii(Stones) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec stone_game_viii(stones :: [integer]) :: integer\n def stone_game_viii(stones) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1872](https://leetcode-cn.com/problems/stone-game-viii)", "[\u77f3\u5b50\u6e38\u620f VIII](/solution/1800-1899/1872.Stone%20Game%20VIII/README.md)", "`\u6570\u7ec4`,`\u6570\u5b66`,`\u52a8\u6001\u89c4\u5212`,`\u535a\u5f08`,`\u524d\u7f00\u548c`", "\u56f0\u96be", ""], "md_table_row_en": ["[1872](https://leetcode.com/problems/stone-game-viii)", "[Stone Game VIII](/solution/1800-1899/1872.Stone%20Game%20VIII/README_EN.md)", "`Array`,`Math`,`Dynamic Programming`,`Game Theory`,`Prefix Sum`", "Hard", ""]}, {"question_id": "2001", "frontend_question_id": "1871", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/jump-game-vii", "url_en": "https://leetcode.com/problems/jump-game-vii", "relative_path_cn": "/solution/1800-1899/1871.Jump%20Game%20VII/README.md", "relative_path_en": "/solution/1800-1899/1871.Jump%20Game%20VII/README_EN.md", "title_cn": "\u8df3\u8dc3\u6e38\u620f VII", "title_en": "Jump Game VII", "question_title_slug": "jump-game-vii", "content_en": "You are given a 0-indexed binary string s
and two integers minJump
and maxJump
. In the beginning, you are standing at index 0
, which is equal to '0'
. You can move from index i
to index j
if the following conditions are fulfilled:
\n\n\n\ti + minJump <= j <= min(i + maxJump, s.length - 1)
, and \n\ts[j] == '0'
. \n
\n\nReturn true
if you can reach index s.length - 1
in s
, or false
otherwise.
\n\n
\nExample 1:
\n\n\nInput: s = "011010", minJump = 2, maxJump = 3\nOutput: true\nExplanation:\nIn the first step, move from index 0 to index 3. \nIn the second step, move from index 3 to index 5.\n
\n\nExample 2:
\n\n\nInput: s = "01101110", minJump = 2, maxJump = 3\nOutput: false\n
\n\n
\nConstraints:
\n\n\n\t2 <= s.length <= 105
\n\ts[i]
is either '0'
or '1'
. \n\ts[0] == '0'
\n\t1 <= minJump <= maxJump < s.length
\n
\n", "content_cn": "\u7ed9\u4f60\u4e00\u4e2a\u4e0b\u6807\u4ece 0 \u5f00\u59cb\u7684\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u00a0s
\u00a0\u548c\u4e24\u4e2a\u6574\u6570\u00a0minJump
\u548c\u00a0maxJump
\u00a0\u3002\u4e00\u5f00\u59cb\uff0c\u4f60\u5728\u4e0b\u6807\u00a00
\u00a0\u5904\uff0c\u4e14\u8be5\u4f4d\u7f6e\u7684\u503c\u4e00\u5b9a\u4e3a\u00a0'0'
\u00a0\u3002\u5f53\u540c\u65f6\u6ee1\u8db3\u5982\u4e0b\u6761\u4ef6\u65f6\uff0c\u4f60\u53ef\u4ee5\u4ece\u4e0b\u6807\u00a0i
\u00a0\u79fb\u52a8\u5230\u4e0b\u6807\u00a0j
\u00a0\u5904\uff1a
\n\n\n\ti + minJump <= j <= min(i + maxJump, s.length - 1)
\u00a0\u4e14 \n\ts[j] == '0'
. \n
\n\n\u5982\u679c\u4f60\u53ef\u4ee5\u5230\u8fbe s
\u00a0\u7684\u4e0b\u6807\u00a0s.length - 1
\u00a0\u5904\uff0c\u8bf7\u4f60\u8fd4\u56de\u00a0true
\u00a0\uff0c\u5426\u5219\u8fd4\u56de\u00a0false
\u00a0\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1as = \"011010\", minJump = 2, maxJump = 3\n\u8f93\u51fa\uff1atrue\n\u89e3\u91ca\uff1a\n\u7b2c\u4e00\u6b65\uff0c\u4ece\u4e0b\u6807 0 \u79fb\u52a8\u5230\u4e0b\u6807 3 \u3002\n\u7b2c\u4e8c\u6b65\uff0c\u4ece\u4e0b\u6807 3 \u79fb\u52a8\u5230\u4e0b\u6807 5 \u3002\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1as = \"01101110\", minJump = 2, maxJump = 3\n\u8f93\u51fa\uff1afalse\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\t2 <= s.length <= 105
\n\ts[i]
\u00a0\u8981\u4e48\u662f\u00a0'0'
\u00a0\uff0c\u8981\u4e48\u662f\u00a0'1'
\n\ts[0] == '0'
\n\t1 <= minJump <= maxJump < s.length
\n
\n", "tags_en": ["Two Pointers", "String", "Prefix Sum"], "tags_cn": ["\u53cc\u6307\u9488", "\u5b57\u7b26\u4e32", "\u524d\u7f00\u548c"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n bool canReach(string s, int minJump, int maxJump) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public boolean canReach(String s, int minJump, int maxJump) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def canReach(self, s, minJump, maxJump):\n \"\"\"\n :type s: str\n :type minJump: int\n :type maxJump: int\n :rtype: bool\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def canReach(self, s: str, minJump: int, maxJump: int) -> bool:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nbool canReach(char * s, int minJump, int maxJump){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public bool CanReach(string s, int minJump, int maxJump) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {string} s\n * @param {number} minJump\n * @param {number} maxJump\n * @return {boolean}\n */\nvar canReach = function(s, minJump, maxJump) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {String} s\n# @param {Integer} min_jump\n# @param {Integer} max_jump\n# @return {Boolean}\ndef can_reach(s, min_jump, max_jump)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func canReach(_ s: String, _ minJump: Int, _ maxJump: Int) -> Bool {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func canReach(s string, minJump int, maxJump int) bool {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def canReach(s: String, minJump: Int, maxJump: Int): Boolean = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun canReach(s: String, minJump: Int, maxJump: Int): Boolean {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn can_reach(s: String, min_jump: i32, max_jump: i32) -> bool {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param String $s\n * @param Integer $minJump\n * @param Integer $maxJump\n * @return Boolean\n */\n function canReach($s, $minJump, $maxJump) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function canReach(s: string, minJump: number, maxJump: number): boolean {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (can-reach s minJump maxJump)\n (-> string? exact-integer? exact-integer? boolean?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec can_reach(S :: unicode:unicode_binary(), MinJump :: integer(), MaxJump :: integer()) -> boolean().\ncan_reach(S, MinJump, MaxJump) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec can_reach(s :: String.t, min_jump :: integer, max_jump :: integer) :: boolean\n def can_reach(s, min_jump, max_jump) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1871](https://leetcode-cn.com/problems/jump-game-vii)", "[\u8df3\u8dc3\u6e38\u620f VII](/solution/1800-1899/1871.Jump%20Game%20VII/README.md)", "`\u53cc\u6307\u9488`,`\u5b57\u7b26\u4e32`,`\u524d\u7f00\u548c`", "\u4e2d\u7b49", ""], "md_table_row_en": ["[1871](https://leetcode.com/problems/jump-game-vii)", "[Jump Game VII](/solution/1800-1899/1871.Jump%20Game%20VII/README_EN.md)", "`Two Pointers`,`String`,`Prefix Sum`", "Medium", ""]}, {"question_id": "2000", "frontend_question_id": "1870", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/minimum-speed-to-arrive-on-time", "url_en": "https://leetcode.com/problems/minimum-speed-to-arrive-on-time", "relative_path_cn": "/solution/1800-1899/1870.Minimum%20Speed%20to%20Arrive%20on%20Time/README.md", "relative_path_en": "/solution/1800-1899/1870.Minimum%20Speed%20to%20Arrive%20on%20Time/README_EN.md", "title_cn": "\u51c6\u65f6\u5230\u8fbe\u7684\u5217\u8f66\u6700\u5c0f\u65f6\u901f", "title_en": "Minimum Speed to Arrive on Time", "question_title_slug": "minimum-speed-to-arrive-on-time", "content_en": "You are given a floating-point number hour
, representing the amount of time you have to reach the office. To commute to the office, you must take n
trains in sequential order. You are also given an integer array dist
of length n
, where dist[i]
describes the distance (in kilometers) of the ith
train ride.
\n\nEach train can only depart at an integer hour, so you may need to wait in between each train ride.
\n\n\n\t- For example, if the
1st
train ride takes 1.5
hours, you must wait for an additional 0.5
hours before you can depart on the 2nd
train ride at the 2 hour mark. \n
\n\nReturn the minimum positive integer speed (in kilometers per hour) that all the trains must travel at for you to reach the office on time, or -1
if it is impossible to be on time.
\n\nTests are generated such that the answer will not exceed 107
and hour
will have at most two digits after the decimal point.
\n\n
\nExample 1:
\n\n\nInput: dist = [1,3,2], hour = 6\nOutput: 1\nExplanation: At speed 1:\n- The first train ride takes 1/1 = 1 hour.\n- Since we are already at an integer hour, we depart immediately at the 1 hour mark. The second train takes 3/1 = 3 hours.\n- Since we are already at an integer hour, we depart immediately at the 4 hour mark. The third train takes 2/1 = 2 hours.\n- You will arrive at exactly the 6 hour mark.\n
\n\nExample 2:
\n\n\nInput: dist = [1,3,2], hour = 2.7\nOutput: 3\nExplanation: At speed 3:\n- The first train ride takes 1/3 = 0.33333 hours.\n- Since we are not at an integer hour, we wait until the 1 hour mark to depart. The second train ride takes 3/3 = 1 hour.\n- Since we are already at an integer hour, we depart immediately at the 2 hour mark. The third train takes 2/3 = 0.66667 hours.\n- You will arrive at the 2.66667 hour mark.\n
\n\nExample 3:
\n\n\nInput: dist = [1,3,2], hour = 1.9\nOutput: -1\nExplanation: It is impossible because the earliest the third train can depart is at the 2 hour mark.\n
\n\n
\nConstraints:
\n\n\n\tn == dist.length
\n\t1 <= n <= 105
\n\t1 <= dist[i] <= 105
\n\t1 <= hour <= 109
\n\t- There will be at most two digits after the decimal point in
hour
. \n
\n", "content_cn": "\u7ed9\u4f60\u4e00\u4e2a\u6d6e\u70b9\u6570 hour
\uff0c\u8868\u793a\u4f60\u5230\u8fbe\u529e\u516c\u5ba4\u53ef\u7528\u7684\u603b\u901a\u52e4\u65f6\u95f4\u3002\u8981\u5230\u8fbe\u529e\u516c\u5ba4\uff0c\u4f60\u5fc5\u987b\u6309\u7ed9\u5b9a\u6b21\u5e8f\u4e58\u5750 n
\u8d9f\u5217\u8f66\u3002\u53e6\u7ed9\u4f60\u4e00\u4e2a\u957f\u5ea6\u4e3a n
\u7684\u6574\u6570\u6570\u7ec4 dist
\uff0c\u5176\u4e2d dist[i]
\u8868\u793a\u7b2c i
\u8d9f\u5217\u8f66\u7684\u884c\u9a76\u8ddd\u79bb\uff08\u5355\u4f4d\u662f\u5343\u7c73\uff09\u3002
\n\n\u6bcf\u8d9f\u5217\u8f66\u5747\u53ea\u80fd\u5728\u6574\u70b9\u53d1\u8f66\uff0c\u6240\u4ee5\u4f60\u53ef\u80fd\u9700\u8981\u5728\u4e24\u8d9f\u5217\u8f66\u4e4b\u95f4\u7b49\u5f85\u4e00\u6bb5\u65f6\u95f4\u3002
\n\n\n\t- \u4f8b\u5982\uff0c\u7b2c
1
\u8d9f\u5217\u8f66\u9700\u8981 1.5
\u5c0f\u65f6\uff0c\u90a3\u4f60\u5fc5\u987b\u518d\u7b49\u5f85 0.5
\u5c0f\u65f6\uff0c\u642d\u4e58\u5728\u7b2c 2 \u5c0f\u65f6\u53d1\u8f66\u7684\u7b2c 2
\u8d9f\u5217\u8f66\u3002 \n
\n\n\u8fd4\u56de\u80fd\u6ee1\u8db3\u4f60\u51c6\u65f6\u5230\u8fbe\u529e\u516c\u5ba4\u6240\u8981\u6c42\u5168\u90e8\u5217\u8f66\u7684 \u6700\u5c0f\u6b63\u6574\u6570 \u65f6\u901f\uff08\u5355\u4f4d\uff1a\u5343\u7c73\u6bcf\u5c0f\u65f6\uff09\uff0c\u5982\u679c\u65e0\u6cd5\u51c6\u65f6\u5230\u8fbe\uff0c\u5219\u8fd4\u56de -1
\u3002
\n\n\u751f\u6210\u7684\u6d4b\u8bd5\u7528\u4f8b\u4fdd\u8bc1\u7b54\u6848\u4e0d\u8d85\u8fc7 107
\uff0c\u4e14 hour
\u7684 \u5c0f\u6570\u70b9\u540e\u6700\u591a\u5b58\u5728\u4e24\u4f4d\u6570\u5b57 \u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1adist = [1,3,2], hour = 6\n\u8f93\u51fa\uff1a1\n\u89e3\u91ca\uff1a\u901f\u5ea6\u4e3a 1 \u65f6\uff1a\n- \u7b2c 1 \u8d9f\u5217\u8f66\u8fd0\u884c\u9700\u8981 1/1 = 1 \u5c0f\u65f6\u3002\n- \u7531\u4e8e\u662f\u5728\u6574\u6570\u65f6\u95f4\u5230\u8fbe\uff0c\u53ef\u4ee5\u7acb\u5373\u6362\u4e58\u5728\u7b2c 1 \u5c0f\u65f6\u53d1\u8f66\u7684\u5217\u8f66\u3002\u7b2c 2 \u8d9f\u5217\u8f66\u8fd0\u884c\u9700\u8981 3/1 = 3 \u5c0f\u65f6\u3002\n- \u7531\u4e8e\u662f\u5728\u6574\u6570\u65f6\u95f4\u5230\u8fbe\uff0c\u53ef\u4ee5\u7acb\u5373\u6362\u4e58\u5728\u7b2c 4 \u5c0f\u65f6\u53d1\u8f66\u7684\u5217\u8f66\u3002\u7b2c 3 \u8d9f\u5217\u8f66\u8fd0\u884c\u9700\u8981 2/1 = 2 \u5c0f\u65f6\u3002\n- \u4f60\u5c06\u4f1a\u6070\u597d\u5728\u7b2c 6 \u5c0f\u65f6\u5230\u8fbe\u3002\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1adist = [1,3,2], hour = 2.7\n\u8f93\u51fa\uff1a3\n\u89e3\u91ca\uff1a\u901f\u5ea6\u4e3a 3 \u65f6\uff1a\n- \u7b2c 1 \u8d9f\u5217\u8f66\u8fd0\u884c\u9700\u8981 1/3 = 0.33333 \u5c0f\u65f6\u3002\n- \u7531\u4e8e\u4e0d\u662f\u5728\u6574\u6570\u65f6\u95f4\u5230\u8fbe\uff0c\u6545\u9700\u8981\u7b49\u5f85\u81f3\u7b2c 1 \u5c0f\u65f6\u624d\u80fd\u642d\u4e58\u5217\u8f66\u3002\u7b2c 2 \u8d9f\u5217\u8f66\u8fd0\u884c\u9700\u8981 3/3 = 1 \u5c0f\u65f6\u3002\n- \u7531\u4e8e\u662f\u5728\u6574\u6570\u65f6\u95f4\u5230\u8fbe\uff0c\u53ef\u4ee5\u7acb\u5373\u6362\u4e58\u5728\u7b2c 2 \u5c0f\u65f6\u53d1\u8f66\u7684\u5217\u8f66\u3002\u7b2c 3 \u8d9f\u5217\u8f66\u8fd0\u884c\u9700\u8981 2/3 = 0.66667 \u5c0f\u65f6\u3002\n- \u4f60\u5c06\u4f1a\u5728\u7b2c 2.66667 \u5c0f\u65f6\u5230\u8fbe\u3002
\n\n\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1adist = [1,3,2], hour = 1.9\n\u8f93\u51fa\uff1a-1\n\u89e3\u91ca\uff1a\u4e0d\u53ef\u80fd\u51c6\u65f6\u5230\u8fbe\uff0c\u56e0\u4e3a\u7b2c 3 \u8d9f\u5217\u8f66\u6700\u65e9\u662f\u5728\u7b2c 2 \u5c0f\u65f6\u53d1\u8f66\u3002
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\tn == dist.length
\n\t1 <= n <= 105
\n\t1 <= dist[i] <= 105
\n\t1 <= hour <= 109
\n\thours
\u4e2d\uff0c\u5c0f\u6570\u70b9\u540e\u6700\u591a\u5b58\u5728\u4e24\u4f4d\u6570\u5b57 \n
\n", "tags_en": ["Array", "Binary Search"], "tags_cn": ["\u6570\u7ec4", "\u4e8c\u5206\u67e5\u627e"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n int minSpeedOnTime(vector& dist, double hour) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int minSpeedOnTime(int[] dist, double hour) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def minSpeedOnTime(self, dist, hour):\n \"\"\"\n :type dist: List[int]\n :type hour: float\n :rtype: int\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def minSpeedOnTime(self, dist: List[int], hour: float) -> int:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nint minSpeedOnTime(int* dist, int distSize, double hour){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int MinSpeedOnTime(int[] dist, double hour) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[]} dist\n * @param {number} hour\n * @return {number}\n */\nvar minSpeedOnTime = function(dist, hour) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer[]} dist\n# @param {Float} hour\n# @return {Integer}\ndef min_speed_on_time(dist, hour)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func minSpeedOnTime(_ dist: [Int], _ hour: Double) -> Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func minSpeedOnTime(dist []int, hour float64) int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def minSpeedOnTime(dist: Array[Int], hour: Double): Int = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun minSpeedOnTime(dist: IntArray, hour: Double): Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn min_speed_on_time(dist: Vec, hour: f64) -> i32 {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer[] $dist\n * @param Float $hour\n * @return Integer\n */\n function minSpeedOnTime($dist, $hour) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function minSpeedOnTime(dist: number[], hour: number): number {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (min-speed-on-time dist hour)\n (-> (listof exact-integer?) flonum? exact-integer?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec min_speed_on_time(Dist :: [integer()], Hour :: float()) -> integer().\nmin_speed_on_time(Dist, Hour) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec min_speed_on_time(dist :: [integer], hour :: float) :: integer\n def min_speed_on_time(dist, hour) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1870](https://leetcode-cn.com/problems/minimum-speed-to-arrive-on-time)", "[\u51c6\u65f6\u5230\u8fbe\u7684\u5217\u8f66\u6700\u5c0f\u65f6\u901f](/solution/1800-1899/1870.Minimum%20Speed%20to%20Arrive%20on%20Time/README.md)", "`\u6570\u7ec4`,`\u4e8c\u5206\u67e5\u627e`", "\u4e2d\u7b49", ""], "md_table_row_en": ["[1870](https://leetcode.com/problems/minimum-speed-to-arrive-on-time)", "[Minimum Speed to Arrive on Time](/solution/1800-1899/1870.Minimum%20Speed%20to%20Arrive%20on%20Time/README_EN.md)", "`Array`,`Binary Search`", "Medium", ""]}, {"question_id": "1999", "frontend_question_id": "1869", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/longer-contiguous-segments-of-ones-than-zeros", "url_en": "https://leetcode.com/problems/longer-contiguous-segments-of-ones-than-zeros", "relative_path_cn": "/solution/1800-1899/1869.Longer%20Contiguous%20Segments%20of%20Ones%20than%20Zeros/README.md", "relative_path_en": "/solution/1800-1899/1869.Longer%20Contiguous%20Segments%20of%20Ones%20than%20Zeros/README_EN.md", "title_cn": "\u54ea\u79cd\u8fde\u7eed\u5b50\u5b57\u7b26\u4e32\u66f4\u957f", "title_en": "Longer Contiguous Segments of Ones than Zeros", "question_title_slug": "longer-contiguous-segments-of-ones-than-zeros", "content_en": "Given a binary string s
, return true
if the longest contiguous segment of 1
s is strictly longer than the longest contiguous segment of 0
s in s
. Return false
otherwise.
\n\n\n\t- For example, in
s = "110100010"
the longest contiguous segment of 1
s has length 2
, and the longest contiguous segment of 0
s has length 3
. \n
\n\nNote that if there are no 0
s, then the longest contiguous segment of 0
s is considered to have length 0
. The same applies if there are no 1
s.
\n\n
\nExample 1:
\n\n\nInput: s = "1101"\nOutput: true\nExplanation:\nThe longest contiguous segment of 1s has length 2: "1101"\nThe longest contiguous segment of 0s has length 1: "1101"\nThe segment of 1s is longer, so return true.\n
\n\nExample 2:
\n\n\nInput: s = "111000"\nOutput: false\nExplanation:\nThe longest contiguous segment of 1s has length 3: "111000"\nThe longest contiguous segment of 0s has length 3: "111000"\nThe segment of 1s is not longer, so return false.\n
\n\nExample 3:
\n\n\nInput: s = "110100010"\nOutput: false\nExplanation:\nThe longest contiguous segment of 1s has length 2: "110100010"\nThe longest contiguous segment of 0s has length 3: "110100010"\nThe segment of 1s is not longer, so return false.\n
\n\n
\nConstraints:
\n\n\n\t1 <= s.length <= 100
\n\ts[i]
is either '0'
or '1'
. \n
\n", "content_cn": "\u7ed9\u4f60\u4e00\u4e2a\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32 s
\u3002\u5982\u679c\u5b57\u7b26\u4e32\u4e2d\u7531 1
\u7ec4\u6210\u7684 \u6700\u957f \u8fde\u7eed\u5b50\u5b57\u7b26\u4e32 \u4e25\u683c\u957f\u4e8e \u7531 0
\u7ec4\u6210\u7684 \u6700\u957f \u8fde\u7eed\u5b50\u5b57\u7b26\u4e32\uff0c\u8fd4\u56de true
\uff1b\u5426\u5219\uff0c\u8fd4\u56de false
\u3002
\n\n\n\t- \u4f8b\u5982\uff0c
s = \"110100010\"
\u4e2d\uff0c\u7531 1
\u7ec4\u6210\u7684\u6700\u957f\u8fde\u7eed\u5b50\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u662f 2
\uff0c\u7531 0
\u7ec4\u6210\u7684\u6700\u957f\u8fde\u7eed\u5b50\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u662f 3
\u3002 \n
\n\n\u6ce8\u610f\uff0c\u5982\u679c\u5b57\u7b26\u4e32\u4e2d\u4e0d\u5b58\u5728 0
\uff0c\u6b64\u65f6\u8ba4\u4e3a\u7531 0
\u7ec4\u6210\u7684\u6700\u957f\u8fde\u7eed\u5b50\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u662f 0
\u3002\u5b57\u7b26\u4e32\u4e2d\u4e0d\u5b58\u5728 1
\u7684\u60c5\u51b5\u4e5f\u9002\u7528\u6b64\u89c4\u5219\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1as = \"1101\"\n\u8f93\u51fa\uff1atrue\n\u89e3\u91ca\uff1a\n\u7531 1
\u7ec4\u6210\u7684\u6700\u957f\u8fde\u7eed\u5b50\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u662f 2\uff1a\"1101\"\n\u7531 0
\u7ec4\u6210\u7684\u6700\u957f\u8fde\u7eed\u5b50\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u662f 1\uff1a\"1101\"\n\u7531 1 \u7ec4\u6210\u7684\u5b50\u5b57\u7b26\u4e32\u66f4\u957f\uff0c\u6545\u8fd4\u56de true \u3002\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1as = \"111000\"\n\u8f93\u51fa\uff1afalse\n\u89e3\u91ca\uff1a\n\u7531 1
\u7ec4\u6210\u7684\u6700\u957f\u8fde\u7eed\u5b50\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u662f 3\uff1a\"111000\"\n\u7531 0
\u7ec4\u6210\u7684\u6700\u957f\u8fde\u7eed\u5b50\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u662f 3\uff1a\"111000\"\n\u7531 1 \u7ec4\u6210\u7684\u5b50\u5b57\u7b26\u4e32\u4e0d\u6bd4\u7531 0 \u7ec4\u6210\u7684\u5b50\u5b57\u7b26\u4e32\u957f\uff0c\u6545\u8fd4\u56de false \u3002\n
\n\n\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1as = \"110100010\"\n\u8f93\u51fa\uff1afalse\n\u89e3\u91ca\uff1a\n\u7531 1
\u7ec4\u6210\u7684\u6700\u957f\u8fde\u7eed\u5b50\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u662f 2\uff1a\"110100010\"\n\u7531 0
\u7ec4\u6210\u7684\u6700\u957f\u8fde\u7eed\u5b50\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u662f 3\uff1a\"110100010\"\n\u7531 1 \u7ec4\u6210\u7684\u5b50\u5b57\u7b26\u4e32\u4e0d\u6bd4\u7531 0 \u7ec4\u6210\u7684\u5b50\u5b57\u7b26\u4e32\u957f\uff0c\u6545\u8fd4\u56de false \u3002\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\t1 <= s.length <= 100
\n\ts[i]
\u4e0d\u662f '0'
\u5c31\u662f '1'
\n
\n", "tags_en": ["String"], "tags_cn": ["\u5b57\u7b26\u4e32"], "difficulty_en": "Easy", "difficulty_cn": "\u7b80\u5355", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n bool checkZeroOnes(string s) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public boolean checkZeroOnes(String s) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def checkZeroOnes(self, s):\n \"\"\"\n :type s: str\n :rtype: bool\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def checkZeroOnes(self, s: str) -> bool:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nbool checkZeroOnes(char * s){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public bool CheckZeroOnes(string s) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {string} s\n * @return {boolean}\n */\nvar checkZeroOnes = function(s) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {String} s\n# @return {Boolean}\ndef check_zero_ones(s)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func checkZeroOnes(_ s: String) -> Bool {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func checkZeroOnes(s string) bool {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def checkZeroOnes(s: String): Boolean = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun checkZeroOnes(s: String): Boolean {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn check_zero_ones(s: String) -> bool {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param String $s\n * @return Boolean\n */\n function checkZeroOnes($s) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function checkZeroOnes(s: string): boolean {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (check-zero-ones s)\n (-> string? boolean?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec check_zero_ones(S :: unicode:unicode_binary()) -> boolean().\ncheck_zero_ones(S) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec check_zero_ones(s :: String.t) :: boolean\n def check_zero_ones(s) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1869](https://leetcode-cn.com/problems/longer-contiguous-segments-of-ones-than-zeros)", "[\u54ea\u79cd\u8fde\u7eed\u5b50\u5b57\u7b26\u4e32\u66f4\u957f](/solution/1800-1899/1869.Longer%20Contiguous%20Segments%20of%20Ones%20than%20Zeros/README.md)", "`\u5b57\u7b26\u4e32`", "\u7b80\u5355", ""], "md_table_row_en": ["[1869](https://leetcode.com/problems/longer-contiguous-segments-of-ones-than-zeros)", "[Longer Contiguous Segments of Ones than Zeros](/solution/1800-1899/1869.Longer%20Contiguous%20Segments%20of%20Ones%20than%20Zeros/README_EN.md)", "`String`", "Easy", ""]}, {"question_id": "1998", "frontend_question_id": "1843", "paid_only": true, "paid_only_cn": true, "category": "Database", "url_cn": "https://leetcode-cn.com/problems/suspicious-bank-accounts", "url_en": "https://leetcode.com/problems/suspicious-bank-accounts", "relative_path_cn": "/solution/1800-1899/1843.Suspicious%20Bank%20Accounts/README.md", "relative_path_en": "/solution/1800-1899/1843.Suspicious%20Bank%20Accounts/README_EN.md", "title_cn": "Suspicious Bank Accounts", "title_en": "Suspicious Bank Accounts", "question_title_slug": "suspicious-bank-accounts", "content_en": "Table: Accounts
\r\n\r\n\r\n+----------------+------+\r\n| Column Name | Type |\r\n+----------------+------+\r\n| account_id | int |\r\n| max_income | int |\r\n+----------------+------+\r\naccount_id is the primary key for this table.\r\nEach row contains information about the maximum monthly income for one bank account.\r\n
\r\n\r\n
\r\n\r\nTable: Transactions
\r\n\r\n\r\n+----------------+----------+\r\n| Column Name | Type |\r\n+----------------+----------+\r\n| transaction_id | int |\r\n| account_id | int |\r\n| type | ENUM |\r\n| amount | int |\r\n| day | datetime |\r\n+----------------+----------+\r\ntransaction_id is the primary key for this table.\r\nEach row contains information about one transaction.\r\ntype is ENUM ('Creditor','Debtor') where 'Creditor' means the user deposited money into their account and 'Debtor' means the user withdrew money from their account.\r\namount is the amount of money depositied/withdrawn during the transaction.\r\n
\r\n\r\n
\r\n\r\nWrite an SQL query to report the IDs of all suspicious bank accounts.
\r\n\r\nA bank account is suspicious if the total income exceeds the max_income
for this account for two or more consecutive months. The total income of an account in some month is the sum of all its deposits in that month (i.e., transactions of the type 'Creditor'
).
\r\n\r\nReturn the result table in ascending order by transaction_id
.
\r\n\r\nThe query result format is in the following example:
\r\n\r\n
\r\n\r\n\r\nAccounts table:\r\n+------------+------------+\r\n| account_id | max_income |\r\n+------------+------------+\r\n| 3 | 21000 |\r\n| 4 | 10400 |\r\n+------------+------------+\r\n\r\nTransactions table:\r\n+----------------+------------+----------+--------+---------------------+\r\n| transaction_id | account_id | type | amount | day |\r\n+----------------+------------+----------+--------+---------------------+\r\n| 2 | 3 | Creditor | 107100 | 2021-06-02 11:38:14 |\r\n| 4 | 4 | Creditor | 10400 | 2021-06-20 12:39:18 |\r\n| 11 | 4 | Debtor | 58800 | 2021-07-23 12:41:55 |\r\n| 1 | 4 | Creditor | 49300 | 2021-05-03 16:11:04 |\r\n| 15 | 3 | Debtor | 75500 | 2021-05-23 14:40:20 |\r\n| 10 | 3 | Creditor | 102100 | 2021-06-15 10:37:16 |\r\n| 14 | 4 | Creditor | 56300 | 2021-07-21 12:12:25 |\r\n| 19 | 4 | Debtor | 101100 | 2021-05-09 15:21:49 |\r\n| 8 | 3 | Creditor | 64900 | 2021-07-26 15:09:56 |\r\n| 7 | 3 | Creditor | 90900 | 2021-06-14 11:23:07 |\r\n+----------------+------------+----------+--------+---------------------+\r\n\r\nResult table:\r\n+------------+\r\n| account_id |\r\n+------------+\r\n| 3 |\r\n+------------+\r\n\r\nFor account 3:\r\n- In 6-2021, the user had an income of 107100 + 102100 + 90900 = 300100.\r\n- In 7-2021, the user had an income of 64900.\r\nWe can see that the income exceeded the max income of 21000 for two consecutive months, so we include 3 in the result table.\r\n\r\nFor account 4:\r\n- In 5-2021, the user had an income of 49300.\r\n- In 6-2021, the user had an income of 10400.\r\n- In 7-2021, the user had an income of 56300.\r\nWe can see that the income exceeded the max income in May and July, but not in June. Since the account did not exceed the max income for two consecutive months, we do not include it in the result table.\r\n
", "content_cn": "Table: Accounts
\r\n\r\n\r\n+----------------+------+\r\n| Column Name | Type |\r\n+----------------+------+\r\n| account_id | int |\r\n| max_income | int |\r\n+----------------+------+\r\naccount_id is the primary key for this table.\r\nEach row contains information about the maximum monthly income for one bank account.\r\n
\r\n\r\n
\r\n\r\nTable: Transactions
\r\n\r\n\r\n+----------------+----------+\r\n| Column Name | Type |\r\n+----------------+----------+\r\n| transaction_id | int |\r\n| account_id | int |\r\n| type | ENUM |\r\n| amount | int |\r\n| day | datetime |\r\n+----------------+----------+\r\ntransaction_id is the primary key for this table.\r\nEach row contains information about one transaction.\r\ntype is ENUM ('Creditor','Debtor') where 'Creditor' means the user deposited money into their account and 'Debtor' means the user withdrew money from their account.\r\namount is the amount of money depositied/withdrawn during the transaction.\r\n
\r\n\r\n
\r\n\r\nWrite an SQL query to report the IDs of all suspicious bank accounts.
\r\n\r\nA bank account is suspicious if the total income exceeds the max_income
for this account for two or more consecutive months. The total income of an account in some month is the sum of all its deposits in that month (i.e., transactions of the type 'Creditor'
).
\r\n\r\nReturn the result table in ascending order by transaction_id
.
\r\n\r\nThe query result format is in the following example:
\r\n\r\n
\r\n\r\n\r\nAccounts table:\r\n+------------+------------+\r\n| account_id | max_income |\r\n+------------+------------+\r\n| 3 | 21000 |\r\n| 4 | 10400 |\r\n+------------+------------+\r\n\r\nTransactions table:\r\n+----------------+------------+----------+--------+---------------------+\r\n| transaction_id | account_id | type | amount | day |\r\n+----------------+------------+----------+--------+---------------------+\r\n| 2 | 3 | Creditor | 107100 | 2021-06-02 11:38:14 |\r\n| 4 | 4 | Creditor | 10400 | 2021-06-20 12:39:18 |\r\n| 11 | 4 | Debtor | 58800 | 2021-07-23 12:41:55 |\r\n| 1 | 4 | Creditor | 49300 | 2021-05-03 16:11:04 |\r\n| 15 | 3 | Debtor | 75500 | 2021-05-23 14:40:20 |\r\n| 10 | 3 | Creditor | 102100 | 2021-06-15 10:37:16 |\r\n| 14 | 4 | Creditor | 56300 | 2021-07-21 12:12:25 |\r\n| 19 | 4 | Debtor | 101100 | 2021-05-09 15:21:49 |\r\n| 8 | 3 | Creditor | 64900 | 2021-07-26 15:09:56 |\r\n| 7 | 3 | Creditor | 90900 | 2021-06-14 11:23:07 |\r\n+----------------+------------+----------+--------+---------------------+\r\n\r\nResult table:\r\n+------------+\r\n| account_id |\r\n+------------+\r\n| 3 |\r\n+------------+\r\n\r\nFor account 3:\r\n- In 6-2021, the user had an income of 107100 + 102100 + 90900 = 300100.\r\n- In 7-2021, the user had an income of 64900.\r\nWe can see that the income exceeded the max income of 21000 for two consecutive months, so we include 3 in the result table.\r\n\r\nFor account 4:\r\n- In 5-2021, the user had an income of 49300.\r\n- In 6-2021, the user had an income of 10400.\r\n- In 7-2021, the user had an income of 56300.\r\nWe can see that the income exceeded the max income in May and July, but not in June. Since the account did not exceed the max income for two consecutive months, we do not include it in the result table.\r\n
", "tags_en": ["Database"], "tags_cn": ["\u6570\u636e\u5e93"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "MySQL", "langSlug": "mysql", "code": "# Write your MySQL query statement below", "__typename": "CodeSnippetNode"}, {"lang": "MS SQL Server", "langSlug": "mssql", "code": "/* Write your T-SQL query statement below */", "__typename": "CodeSnippetNode"}, {"lang": "Oracle", "langSlug": "oraclesql", "code": "/* Write your PL/SQL query statement below */", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1843](https://leetcode-cn.com/problems/suspicious-bank-accounts)", "[Suspicious Bank Accounts](/solution/1800-1899/1843.Suspicious%20Bank%20Accounts/README.md)", "`\u6570\u636e\u5e93`", "\u4e2d\u7b49", "\ud83d\udd12"], "md_table_row_en": ["[1843](https://leetcode.com/problems/suspicious-bank-accounts)", "[Suspicious Bank Accounts](/solution/1800-1899/1843.Suspicious%20Bank%20Accounts/README_EN.md)", "`Database`", "Medium", "\ud83d\udd12"]}, {"question_id": "1997", "frontend_question_id": "1842", "paid_only": true, "paid_only_cn": true, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/next-palindrome-using-same-digits", "url_en": "https://leetcode.com/problems/next-palindrome-using-same-digits", "relative_path_cn": "/solution/1800-1899/1842.Next%20Palindrome%20Using%20Same%20Digits/README.md", "relative_path_en": "/solution/1800-1899/1842.Next%20Palindrome%20Using%20Same%20Digits/README_EN.md", "title_cn": "\u4e0b\u4e2a\u7531\u76f8\u540c\u6570\u5b57\u6784\u6210\u7684\u56de\u6587\u4e32", "title_en": "Next Palindrome Using Same Digits", "question_title_slug": "next-palindrome-using-same-digits", "content_en": "You are given a numeric string num
, representing a very large palindrome.
\r\n\r\nReturn the smallest palindrome larger than num
that can be created by rearranging its digits. If no such palindrome exists, return an empty string ""
.
\r\n\r\nA palindrome is a number that reads the same backward as forward.
\r\n\r\n
\r\nExample 1:
\r\n\r\n\r\nInput: num = "1221"\r\nOutput: "2112"\r\nExplanation: The next palindrome larger than "1221" is "2112".\r\n
\r\n\r\nExample 2:
\r\n\r\n\r\nInput: num = "32123"\r\nOutput: ""\r\nExplanation: No palindromes larger than "32123" can be made by rearranging the digits.\r\n
\r\n\r\nExample 3:
\r\n\r\n\r\nInput: num = "45544554"\r\nOutput: "54455445"\r\nExplanation: The next palindrome larger than "45544554" is "54455445".\r\n
\r\n\r\n
\r\nConstraints:
\r\n\r\n\r\n\t1 <= num.length <= 105
\r\n\tnum
is a palindrome. \r\n
", "content_cn": "\u7ed9\u4f60\u4e00\u4e2a\u5f88\u957f\u7684\u6570\u5b57\u56de\u6587\u4e32 num
\uff0c\u8fd4\u56de \u5927\u4e8e num
\u3001\u7531\u76f8\u540c\u6570\u5b57\u91cd\u65b0\u7ec4\u5408\u800c\u6210\u7684\u6700\u5c0f \u56de\u6587\u4e32\u3002
\n\n\u5982\u679c\u4e0d\u5b58\u5728\u8fd9\u6837\u7684\u56de\u6587\u4e32\uff0c\u5219\u8fd4\u56de\u7a7a\u4e32 \"\"
\u3002
\n\n\u56de\u6587\u4e32 \u662f\u6b63\u8bfb\u548c\u53cd\u8bfb\u90fd\u4e00\u6837\u7684\u5b57\u7b26\u4e32\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1anum = \"1221\"\n\u8f93\u51fa\uff1a\"2112\"\n\u89e3\u91ca\uff1a\u4e0b\u4e2a\u6bd4 \"1221\" \u5927\u7684\u56de\u6587\u4e32\u662f \"2112\"\u3002\n
\n\n\u793a\u4f8b\u00a02\uff1a
\n\n\n\u8f93\u5165\uff1anum = \"32123\"\n\u8f93\u51fa\uff1a\"\"\n\u89e3\u91ca\uff1a\u4e0d\u5b58\u5728\u901a\u8fc7\u91cd\u7ec4 \"32123\" \u7684\u6570\u5b57\u53ef\u5f97\u3001\u6bd4 \"32123\" \u8fd8\u5927\u7684\u56de\u6587\u4e32\u3002\n
\n\n\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1anum = \"45544554\"\n\u8f93\u51fa\uff1a\"54455445\"\n\u89e3\u91ca\uff1a\u4e0b\u4e2a\u6bd4 \"45544554\" \u8fd8\u8981\u5927\u7684\u56de\u6587\u4e32\u662f \"54455445\"\u3002\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\t1 <= num.length <= 105
\n\tnum
\u00a0\u662f\u56de\u6587\u4e32\u3002 \n
\n", "tags_en": ["Two Pointers", "String"], "tags_cn": ["\u53cc\u6307\u9488", "\u5b57\u7b26\u4e32"], "difficulty_en": "Hard", "difficulty_cn": "\u56f0\u96be", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n string nextPalindrome(string num) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public String nextPalindrome(String num) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def nextPalindrome(self, num):\n \"\"\"\n :type num: str\n :rtype: str\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def nextPalindrome(self, num: str) -> str:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nchar * nextPalindrome(char * num){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public string NextPalindrome(string num) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {string} num\n * @return {string}\n */\nvar nextPalindrome = function(num) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {String} num\n# @return {String}\ndef next_palindrome(num)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func nextPalindrome(_ num: String) -> String {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func nextPalindrome(num string) string {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def nextPalindrome(num: String): String = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun nextPalindrome(num: String): String {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn next_palindrome(num: String) -> String {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param String $num\n * @return String\n */\n function nextPalindrome($num) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function nextPalindrome(num: string): string {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (next-palindrome num)\n (-> string? string?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec next_palindrome(Num :: unicode:unicode_binary()) -> unicode:unicode_binary().\nnext_palindrome(Num) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec next_palindrome(num :: String.t) :: String.t\n def next_palindrome(num) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1842](https://leetcode-cn.com/problems/next-palindrome-using-same-digits)", "[\u4e0b\u4e2a\u7531\u76f8\u540c\u6570\u5b57\u6784\u6210\u7684\u56de\u6587\u4e32](/solution/1800-1899/1842.Next%20Palindrome%20Using%20Same%20Digits/README.md)", "`\u53cc\u6307\u9488`,`\u5b57\u7b26\u4e32`", "\u56f0\u96be", "\ud83d\udd12"], "md_table_row_en": ["[1842](https://leetcode.com/problems/next-palindrome-using-same-digits)", "[Next Palindrome Using Same Digits](/solution/1800-1899/1842.Next%20Palindrome%20Using%20Same%20Digits/README_EN.md)", "`Two Pointers`,`String`", "Hard", "\ud83d\udd12"]}, {"question_id": "1996", "frontend_question_id": "1866", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/number-of-ways-to-rearrange-sticks-with-k-sticks-visible", "url_en": "https://leetcode.com/problems/number-of-ways-to-rearrange-sticks-with-k-sticks-visible", "relative_path_cn": "/solution/1800-1899/1866.Number%20of%20Ways%20to%20Rearrange%20Sticks%20With%20K%20Sticks%20Visible/README.md", "relative_path_en": "/solution/1800-1899/1866.Number%20of%20Ways%20to%20Rearrange%20Sticks%20With%20K%20Sticks%20Visible/README_EN.md", "title_cn": "\u6070\u6709 K \u6839\u6728\u68cd\u53ef\u4ee5\u770b\u5230\u7684\u6392\u5217\u6570\u76ee", "title_en": "Number of Ways to Rearrange Sticks With K Sticks Visible", "question_title_slug": "number-of-ways-to-rearrange-sticks-with-k-sticks-visible", "content_en": "There are n
uniquely-sized sticks whose lengths are integers from 1
to n
. You want to arrange the sticks such that exactly k
sticks are visible from the left. A stick is visible from the left if there are no longer sticks to the left of it.
\n\n\n\t- For example, if the sticks are arranged
[1,3,2,5,4]
, then the sticks with lengths 1
, 3
, and 5
are visible from the left. \n
\n\nGiven n
and k
, return the number of such arrangements. Since the answer may be large, return it modulo 109 + 7
.
\n\n
\nExample 1:
\n\n\nInput: n = 3, k = 2\nOutput: 3\nExplanation: [1,3,2], [2,3,1], and [2,1,3] are the only arrangements such that exactly 2 sticks are visible.\nThe visible sticks are underlined.\n
\n\nExample 2:
\n\n\nInput: n = 5, k = 5\nOutput: 1\nExplanation: [1,2,3,4,5] is the only arrangement such that all 5 sticks are visible.\nThe visible sticks are underlined.\n
\n\nExample 3:
\n\n\nInput: n = 20, k = 11\nOutput: 647427950\nExplanation: There are 647427950 (mod 109 + 7) ways to rearrange the sticks such that exactly 11 sticks are visible.\n
\n\n
\nConstraints:
\n\n\n\t1 <= n <= 1000
\n\t1 <= k <= n
\n
\n", "content_cn": "\u6709 n
\u6839\u957f\u5ea6\u4e92\u4e0d\u76f8\u540c\u7684\u6728\u68cd\uff0c\u957f\u5ea6\u4e3a\u4ece 1
\u5230 n
\u7684\u6574\u6570\u3002\u8bf7\u4f60\u5c06\u8fd9\u4e9b\u6728\u68cd\u6392\u6210\u4e00\u6392\uff0c\u5e76\u6ee1\u8db3\u4ece\u5de6\u4fa7 \u53ef\u4ee5\u770b\u5230\u00a0\u6070\u597d k
\u6839\u6728\u68cd\u3002\u4ece\u5de6\u4fa7 \u53ef\u4ee5\u770b\u5230 \u6728\u68cd\u7684\u524d\u63d0\u662f\u8fd9\u4e2a\u6728\u68cd\u7684 \u5de6\u4fa7 \u4e0d\u5b58\u5728\u6bd4\u5b83 \u66f4\u957f\u7684 \u6728\u68cd\u3002
\n\n\n\t- \u4f8b\u5982\uff0c\u5982\u679c\u6728\u68cd\u6392\u5217\u4e3a
[1,3,2,5,4]
\uff0c\u90a3\u4e48\u4ece\u5de6\u4fa7\u53ef\u4ee5\u770b\u5230\u7684\u5c31\u662f\u957f\u5ea6\u5206\u522b\u4e3a 1
\u30013
\u30015
\u7684\u6728\u68cd\u3002 \n
\n\n\u7ed9\u4f60 n
\u548c k
\uff0c\u8fd4\u56de\u7b26\u5408\u9898\u76ee\u8981\u6c42\u7684\u6392\u5217 \u6570\u76ee \u3002\u7531\u4e8e\u7b54\u6848\u53ef\u80fd\u5f88\u5927\uff0c\u8bf7\u8fd4\u56de\u5bf9 109 + 7
\u53d6\u4f59 \u7684\u7ed3\u679c\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1an = 3, k = 2\n\u8f93\u51fa\uff1a3\n\u89e3\u91ca\uff1a[1,3,2], [2,3,1] \u548c [2,1,3] \u662f\u4ec5\u6709\u7684\u80fd\u6ee1\u8db3\u6070\u597d 2 \u6839\u6728\u68cd\u53ef\u4ee5\u770b\u5230\u7684\u6392\u5217\u3002\n\u53ef\u4ee5\u770b\u5230\u7684\u6728\u68cd\u5df2\u7ecf\u7528\u7c97\u4f53+\u659c\u4f53\u6807\u8bc6\u3002\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1an = 5, k = 5\n\u8f93\u51fa\uff1a1\n\u89e3\u91ca\uff1a[1,2,3,4,5] \u662f\u552f\u4e00\u4e00\u79cd\u80fd\u6ee1\u8db3\u5168\u90e8 5 \u6839\u6728\u68cd\u53ef\u4ee5\u770b\u5230\u7684\u6392\u5217\u3002\n\u53ef\u4ee5\u770b\u5230\u7684\u6728\u68cd\u5df2\u7ecf\u7528\u7c97\u4f53+\u659c\u4f53\u6807\u8bc6\u3002\n
\n\n\u793a\u4f8b 3\uff1a
\n\n\u8f93\u5165\uff1an = 20, k = 11\n\u8f93\u51fa\uff1a647427950\n\u89e3\u91ca\uff1a\u603b\u5171\u6709 647427950 (mod 109 + 7) \u79cd\u80fd\u6ee1\u8db3\u6070\u597d\u6709 11 \u6839\u6728\u68cd\u53ef\u4ee5\u770b\u5230\u7684\u6392\u5217\u3002\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\t1 <= n <= 1000
\n\t1 <= k <= n
\n
\n", "tags_en": ["Math", "Dynamic Programming", "Combinatorics"], "tags_cn": ["\u6570\u5b66", "\u52a8\u6001\u89c4\u5212", "\u7ec4\u5408\u6570\u5b66"], "difficulty_en": "Hard", "difficulty_cn": "\u56f0\u96be", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n int rearrangeSticks(int n, int k) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int rearrangeSticks(int n, int k) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def rearrangeSticks(self, n, k):\n \"\"\"\n :type n: int\n :type k: int\n :rtype: int\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def rearrangeSticks(self, n: int, k: int) -> int:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nint rearrangeSticks(int n, int k){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int RearrangeSticks(int n, int k) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number} n\n * @param {number} k\n * @return {number}\n */\nvar rearrangeSticks = function(n, k) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer} n\n# @param {Integer} k\n# @return {Integer}\ndef rearrange_sticks(n, k)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func rearrangeSticks(_ n: Int, _ k: Int) -> Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func rearrangeSticks(n int, k int) int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def rearrangeSticks(n: Int, k: Int): Int = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun rearrangeSticks(n: Int, k: Int): Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn rearrange_sticks(n: i32, k: i32) -> i32 {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer $n\n * @param Integer $k\n * @return Integer\n */\n function rearrangeSticks($n, $k) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function rearrangeSticks(n: number, k: number): number {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (rearrange-sticks n k)\n (-> exact-integer? exact-integer? exact-integer?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec rearrange_sticks(N :: integer(), K :: integer()) -> integer().\nrearrange_sticks(N, K) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec rearrange_sticks(n :: integer, k :: integer) :: integer\n def rearrange_sticks(n, k) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1866](https://leetcode-cn.com/problems/number-of-ways-to-rearrange-sticks-with-k-sticks-visible)", "[\u6070\u6709 K \u6839\u6728\u68cd\u53ef\u4ee5\u770b\u5230\u7684\u6392\u5217\u6570\u76ee](/solution/1800-1899/1866.Number%20of%20Ways%20to%20Rearrange%20Sticks%20With%20K%20Sticks%20Visible/README.md)", "`\u6570\u5b66`,`\u52a8\u6001\u89c4\u5212`,`\u7ec4\u5408\u6570\u5b66`", "\u56f0\u96be", ""], "md_table_row_en": ["[1866](https://leetcode.com/problems/number-of-ways-to-rearrange-sticks-with-k-sticks-visible)", "[Number of Ways to Rearrange Sticks With K Sticks Visible](/solution/1800-1899/1866.Number%20of%20Ways%20to%20Rearrange%20Sticks%20With%20K%20Sticks%20Visible/README_EN.md)", "`Math`,`Dynamic Programming`,`Combinatorics`", "Hard", ""]}, {"question_id": "1995", "frontend_question_id": "1865", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/finding-pairs-with-a-certain-sum", "url_en": "https://leetcode.com/problems/finding-pairs-with-a-certain-sum", "relative_path_cn": "/solution/1800-1899/1865.Finding%20Pairs%20With%20a%20Certain%20Sum/README.md", "relative_path_en": "/solution/1800-1899/1865.Finding%20Pairs%20With%20a%20Certain%20Sum/README_EN.md", "title_cn": "\u627e\u51fa\u548c\u4e3a\u6307\u5b9a\u503c\u7684\u4e0b\u6807\u5bf9", "title_en": "Finding Pairs With a Certain Sum", "question_title_slug": "finding-pairs-with-a-certain-sum", "content_en": "You are given two integer arrays nums1
and nums2
. You are tasked to implement a data structure that supports queries of two types:
\n\n\n\t- Add a positive integer to an element of a given index in the array
nums2
. \n\t- Count the number of pairs
(i, j)
such that nums1[i] + nums2[j]
equals a given value (0 <= i < nums1.length
and 0 <= j < nums2.length
). \n
\n\nImplement the FindSumPairs
class:
\n\n\n\tFindSumPairs(int[] nums1, int[] nums2)
Initializes the FindSumPairs
object with two integer arrays nums1
and nums2
. \n\tvoid add(int index, int val)
Adds val
to nums2[index]
, i.e., apply nums2[index] += val
. \n\tint count(int tot)
Returns the number of pairs (i, j)
such that nums1[i] + nums2[j] == tot
. \n
\n\n
\nExample 1:
\n\n\nInput\n["FindSumPairs", "count", "add", "count", "count", "add", "add", "count"]\n[[[1, 1, 2, 2, 2, 3], [1, 4, 5, 2, 5, 4]], [7], [3, 2], [8], [4], [0, 1], [1, 1], [7]]\nOutput\n[null, 8, null, 2, 1, null, null, 11]\n\nExplanation\nFindSumPairs findSumPairs = new FindSumPairs([1, 1, 2, 2, 2, 3], [1, 4, 5, 2, 5, 4]);\nfindSumPairs.count(7); // return 8; pairs (2,2), (3,2), (4,2), (2,4), (3,4), (4,4) make 2 + 5 and pairs (5,1), (5,5) make 3 + 4\nfindSumPairs.add(3, 2); // now nums2 = [1,4,5,4,5,4
]\nfindSumPairs.count(8); // return 2; pairs (5,2), (5,4) make 3 + 5\nfindSumPairs.count(4); // return 1; pair (5,0) makes 3 + 1\nfindSumPairs.add(0, 1); // now nums2 = [2
,4,5,4,5,4
]\nfindSumPairs.add(1, 1); // now nums2 = [2
,5,5,4,5,4
]\nfindSumPairs.count(7); // return 11; pairs (2,1), (2,2), (2,4), (3,1), (3,2), (3,4), (4,1), (4,2), (4,4) make 2 + 5 and pairs (5,3), (5,5) make 3 + 4\n
\n\n
\nConstraints:
\n\n\n\t1 <= nums1.length <= 1000
\n\t1 <= nums2.length <= 105
\n\t1 <= nums1[i] <= 109
\n\t1 <= nums2[i] <= 105
\n\t0 <= index < nums2.length
\n\t1 <= val <= 105
\n\t1 <= tot <= 109
\n\t- At most
1000
calls are made to add
and count
each. \n
\n", "content_cn": "\u7ed9\u4f60\u4e24\u4e2a\u6574\u6570\u6570\u7ec4 nums1
\u548c nums2
\uff0c\u8bf7\u4f60\u5b9e\u73b0\u4e00\u4e2a\u652f\u6301\u4e0b\u8ff0\u4e24\u7c7b\u67e5\u8be2\u7684\u6570\u636e\u7ed3\u6784\uff1a
\n\n\n\t- \u7d2f\u52a0 \uff0c\u5c06\u4e00\u4e2a\u6b63\u6574\u6570\u52a0\u5230
nums2
\u4e2d\u6307\u5b9a\u4e0b\u6807\u5bf9\u5e94\u5143\u7d20\u4e0a\u3002 \n\t- \u8ba1\u6570 \uff0c\u7edf\u8ba1\u6ee1\u8db3
nums1[i] + nums2[j]
\u7b49\u4e8e\u6307\u5b9a\u503c\u7684\u4e0b\u6807\u5bf9 (i, j)
\u6570\u76ee\uff080 <= i < nums1.length
\u4e14 0 <= j < nums2.length
\uff09\u3002 \n
\n\n\u5b9e\u73b0 FindSumPairs
\u7c7b\uff1a
\n\n\n\tFindSumPairs(int[] nums1, int[] nums2)
\u4f7f\u7528\u6574\u6570\u6570\u7ec4\u00a0nums1
\u548c nums2
\u521d\u59cb\u5316 FindSumPairs
\u5bf9\u8c61\u3002 \n\tvoid add(int index, int val)
\u5c06 val
\u52a0\u5230 nums2[index]
\u4e0a\uff0c\u5373\uff0c\u6267\u884c nums2[index] += val
\u3002 \n\tint count(int tot)
\u8fd4\u56de\u6ee1\u8db3\u00a0nums1[i] + nums2[j] == tot
\u7684\u4e0b\u6807\u5bf9 (i, j)
\u6570\u76ee\u3002 \n
\n\n\u00a0
\n\n\u793a\u4f8b\uff1a
\n\n\n\u8f93\u5165\uff1a\n[\"FindSumPairs\", \"count\", \"add\", \"count\", \"count\", \"add\", \"add\", \"count\"]\n[[[1, 1, 2, 2, 2, 3], [1, 4, 5, 2, 5, 4]], [7], [3, 2], [8], [4], [0, 1], [1, 1], [7]]\n\u8f93\u51fa\uff1a\n[null, 8, null, 2, 1, null, null, 11]\n\n\u89e3\u91ca\uff1a\nFindSumPairs findSumPairs = new FindSumPairs([1, 1, 2, 2, 2, 3], [1, 4, 5, 2, 5, 4]);\nfindSumPairs.count(7); // \u8fd4\u56de 8 ; \u4e0b\u6807\u5bf9 (2,2), (3,2), (4,2), (2,4), (3,4), (4,4) \u6ee1\u8db3 2 + 5 = 7 \uff0c\u4e0b\u6807\u5bf9 (5,1), (5,5) \u6ee1\u8db3 3 + 4 = 7\nfindSumPairs.add(3, 2); // \u6b64\u65f6 nums2 = [1,4,5,4,5,4
]\nfindSumPairs.count(8); // \u8fd4\u56de 2 \uff1b\u4e0b\u6807\u5bf9 (5,2), (5,4) \u6ee1\u8db3 3 + 5 = 8\nfindSumPairs.count(4); // \u8fd4\u56de 1 \uff1b\u4e0b\u6807\u5bf9 (5,0) \u6ee1\u8db3 3 + 1 = 4\nfindSumPairs.add(0, 1); // \u6b64\u65f6 nums2 = [2
,4,5,4,5,4
]\nfindSumPairs.add(1, 1); // \u6b64\u65f6 nums2 = [2
,5,5,4,5,4
]\nfindSumPairs.count(7); // \u8fd4\u56de 11 \uff1b\u4e0b\u6807\u5bf9 (2,1), (2,2), (2,4), (3,1), (3,2), (3,4), (4,1), (4,2), (4,4) \u6ee1\u8db3 2 + 5 = 7 \uff0c\u4e0b\u6807\u5bf9 (5,3), (5,5) \u6ee1\u8db3 3 + 4 = 7\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\t1 <= nums1.length <= 1000
\n\t1 <= nums2.length <= 105
\n\t1 <= nums1[i] <= 109
\n\t1 <= nums2[i] <= 105
\n\t0 <= index < nums2.length
\n\t1 <= val <= 105
\n\t1 <= tot <= 109
\n\t- \u6700\u591a\u8c03\u7528\u00a0
add
\u548c count
\u51fd\u6570\u5404 1000
\u6b21 \n
\n", "tags_en": ["Design", "Array", "Hash Table"], "tags_cn": ["\u8bbe\u8ba1", "\u6570\u7ec4", "\u54c8\u5e0c\u8868"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class FindSumPairs {\npublic:\n FindSumPairs(vector& nums1, vector& nums2) {\n\n }\n \n void add(int index, int val) {\n\n }\n \n int count(int tot) {\n\n }\n};\n\n/**\n * Your FindSumPairs object will be instantiated and called as such:\n * FindSumPairs* obj = new FindSumPairs(nums1, nums2);\n * obj->add(index,val);\n * int param_2 = obj->count(tot);\n */", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class FindSumPairs {\n\n public FindSumPairs(int[] nums1, int[] nums2) {\n\n }\n \n public void add(int index, int val) {\n\n }\n \n public int count(int tot) {\n\n }\n}\n\n/**\n * Your FindSumPairs object will be instantiated and called as such:\n * FindSumPairs obj = new FindSumPairs(nums1, nums2);\n * obj.add(index,val);\n * int param_2 = obj.count(tot);\n */", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class FindSumPairs(object):\n\n def __init__(self, nums1, nums2):\n \"\"\"\n :type nums1: List[int]\n :type nums2: List[int]\n \"\"\"\n\n\n def add(self, index, val):\n \"\"\"\n :type index: int\n :type val: int\n :rtype: None\n \"\"\"\n\n\n def count(self, tot):\n \"\"\"\n :type tot: int\n :rtype: int\n \"\"\"\n\n\n\n# Your FindSumPairs object will be instantiated and called as such:\n# obj = FindSumPairs(nums1, nums2)\n# obj.add(index,val)\n# param_2 = obj.count(tot)", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class FindSumPairs:\n\n def __init__(self, nums1: List[int], nums2: List[int]):\n\n\n def add(self, index: int, val: int) -> None:\n\n\n def count(self, tot: int) -> int:\n\n\n\n# Your FindSumPairs object will be instantiated and called as such:\n# obj = FindSumPairs(nums1, nums2)\n# obj.add(index,val)\n# param_2 = obj.count(tot)", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\n\ntypedef struct {\n\n} FindSumPairs;\n\n\nFindSumPairs* findSumPairsCreate(int* nums1, int nums1Size, int* nums2, int nums2Size) {\n\n}\n\nvoid findSumPairsAdd(FindSumPairs* obj, int index, int val) {\n\n}\n\nint findSumPairsCount(FindSumPairs* obj, int tot) {\n\n}\n\nvoid findSumPairsFree(FindSumPairs* obj) {\n\n}\n\n/**\n * Your FindSumPairs struct will be instantiated and called as such:\n * FindSumPairs* obj = findSumPairsCreate(nums1, nums1Size, nums2, nums2Size);\n * findSumPairsAdd(obj, index, val);\n \n * int param_2 = findSumPairsCount(obj, tot);\n \n * findSumPairsFree(obj);\n*/", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class FindSumPairs {\n\n public FindSumPairs(int[] nums1, int[] nums2) {\n\n }\n \n public void Add(int index, int val) {\n\n }\n \n public int Count(int tot) {\n\n }\n}\n\n/**\n * Your FindSumPairs object will be instantiated and called as such:\n * FindSumPairs obj = new FindSumPairs(nums1, nums2);\n * obj.Add(index,val);\n * int param_2 = obj.Count(tot);\n */", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[]} nums1\n * @param {number[]} nums2\n */\nvar FindSumPairs = function(nums1, nums2) {\n\n};\n\n/** \n * @param {number} index \n * @param {number} val\n * @return {void}\n */\nFindSumPairs.prototype.add = function(index, val) {\n\n};\n\n/** \n * @param {number} tot\n * @return {number}\n */\nFindSumPairs.prototype.count = function(tot) {\n\n};\n\n/**\n * Your FindSumPairs object will be instantiated and called as such:\n * var obj = new FindSumPairs(nums1, nums2)\n * obj.add(index,val)\n * var param_2 = obj.count(tot)\n */", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "class FindSumPairs\n\n=begin\n :type nums1: Integer[]\n :type nums2: Integer[]\n=end\n def initialize(nums1, nums2)\n\n end\n\n\n=begin\n :type index: Integer\n :type val: Integer\n :rtype: Void\n=end\n def add(index, val)\n\n end\n\n\n=begin\n :type tot: Integer\n :rtype: Integer\n=end\n def count(tot)\n\n end\n\n\nend\n\n# Your FindSumPairs object will be instantiated and called as such:\n# obj = FindSumPairs.new(nums1, nums2)\n# obj.add(index, val)\n# param_2 = obj.count(tot)", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "\nclass FindSumPairs {\n\n init(_ nums1: [Int], _ nums2: [Int]) {\n\n }\n \n func add(_ index: Int, _ val: Int) {\n\n }\n \n func count(_ tot: Int) -> Int {\n\n }\n}\n\n/**\n * Your FindSumPairs object will be instantiated and called as such:\n * let obj = FindSumPairs(nums1, nums2)\n * obj.add(index, val)\n * let ret_2: Int = obj.count(tot)\n */", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "type FindSumPairs struct {\n\n}\n\n\nfunc Constructor(nums1 []int, nums2 []int) FindSumPairs {\n\n}\n\n\nfunc (this *FindSumPairs) Add(index int, val int) {\n\n}\n\n\nfunc (this *FindSumPairs) Count(tot int) int {\n\n}\n\n\n/**\n * Your FindSumPairs object will be instantiated and called as such:\n * obj := Constructor(nums1, nums2);\n * obj.Add(index,val);\n * param_2 := obj.Count(tot);\n */", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "class FindSumPairs(_nums1: Array[Int], _nums2: Array[Int]) {\n\n def add(index: Int, `val`: Int) {\n \n }\n\n def count(tot: Int): Int = {\n \n }\n\n}\n\n/**\n * Your FindSumPairs object will be instantiated and called as such:\n * var obj = new FindSumPairs(nums1, nums2)\n * obj.add(index,`val`)\n * var param_2 = obj.count(tot)\n */", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class FindSumPairs(nums1: IntArray, nums2: IntArray) {\n\n fun add(index: Int, `val`: Int) {\n\n }\n\n fun count(tot: Int): Int {\n\n }\n\n}\n\n/**\n * Your FindSumPairs object will be instantiated and called as such:\n * var obj = FindSumPairs(nums1, nums2)\n * obj.add(index,`val`)\n * var param_2 = obj.count(tot)\n */", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "struct FindSumPairs {\n\n}\n\n\n/**\n * `&self` means the method takes an immutable reference.\n * If you need a mutable reference, change it to `&mut self` instead.\n */\nimpl FindSumPairs {\n\n fn new(nums1: Vec, nums2: Vec) -> Self {\n\n }\n \n fn add(&self, index: i32, val: i32) {\n\n }\n \n fn count(&self, tot: i32) -> i32 {\n\n }\n}\n\n/**\n * Your FindSumPairs object will be instantiated and called as such:\n * let obj = FindSumPairs::new(nums1, nums2);\n * obj.add(index, val);\n * let ret_2: i32 = obj.count(tot);\n */", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class FindSumPairs {\n /**\n * @param Integer[] $nums1\n * @param Integer[] $nums2\n */\n function __construct($nums1, $nums2) {\n\n }\n\n /**\n * @param Integer $index\n * @param Integer $val\n * @return NULL\n */\n function add($index, $val) {\n\n }\n\n /**\n * @param Integer $tot\n * @return Integer\n */\n function count($tot) {\n\n }\n}\n\n/**\n * Your FindSumPairs object will be instantiated and called as such:\n * $obj = FindSumPairs($nums1, $nums2);\n * $obj->add($index, $val);\n * $ret_2 = $obj->count($tot);\n */", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "class FindSumPairs {\n constructor(nums1: number[], nums2: number[]) {\n\n }\n\n add(index: number, val: number): void {\n\n }\n\n count(tot: number): number {\n\n }\n}\n\n/**\n * Your FindSumPairs object will be instantiated and called as such:\n * var obj = new FindSumPairs(nums1, nums2)\n * obj.add(index,val)\n * var param_2 = obj.count(tot)\n */", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define find-sum-pairs%\n (class object%\n (super-new)\n\n ; nums1 : (listof exact-integer?)\n\n ; nums2 : (listof exact-integer?)\n (init-field\n nums1\n nums2)\n \n ; add : exact-integer? exact-integer? -> void?\n (define/public (add index val)\n\n )\n ; count : exact-integer? -> exact-integer?\n (define/public (count tot)\n\n )))\n\n;; Your find-sum-pairs% object will be instantiated and called as such:\n;; (define obj (new find-sum-pairs% [nums1 nums1] [nums2 nums2]))\n;; (send obj add index val)\n;; (define param_2 (send obj count tot))", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec find_sum_pairs_init_(Nums1 :: [integer()], Nums2 :: [integer()]) -> any().\nfind_sum_pairs_init_(Nums1, Nums2) ->\n .\n\n-spec find_sum_pairs_add(Index :: integer(), Val :: integer()) -> any().\nfind_sum_pairs_add(Index, Val) ->\n .\n\n-spec find_sum_pairs_count(Tot :: integer()) -> integer().\nfind_sum_pairs_count(Tot) ->\n .\n\n\n%% Your functions will be called as such:\n%% find_sum_pairs_init_(Nums1, Nums2),\n%% find_sum_pairs_add(Index, Val),\n%% Param_2 = find_sum_pairs_count(Tot),\n\n%% find_sum_pairs_init_ will be called before every test case, in which you can do some necessary initializations.", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule FindSumPairs do\n @spec init_(nums1 :: [integer], nums2 :: [integer]) :: any\n def init_(nums1, nums2) do\n\n end\n\n @spec add(index :: integer, val :: integer) :: any\n def add(index, val) do\n\n end\n\n @spec count(tot :: integer) :: integer\n def count(tot) do\n\n end\nend\n\n# Your functions will be called as such:\n# FindSumPairs.init_(nums1, nums2)\n# FindSumPairs.add(index, val)\n# param_2 = FindSumPairs.count(tot)\n\n# FindSumPairs.init_ will be called before every test case, in which you can do some necessary initializations.", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1865](https://leetcode-cn.com/problems/finding-pairs-with-a-certain-sum)", "[\u627e\u51fa\u548c\u4e3a\u6307\u5b9a\u503c\u7684\u4e0b\u6807\u5bf9](/solution/1800-1899/1865.Finding%20Pairs%20With%20a%20Certain%20Sum/README.md)", "`\u8bbe\u8ba1`,`\u6570\u7ec4`,`\u54c8\u5e0c\u8868`", "\u4e2d\u7b49", ""], "md_table_row_en": ["[1865](https://leetcode.com/problems/finding-pairs-with-a-certain-sum)", "[Finding Pairs With a Certain Sum](/solution/1800-1899/1865.Finding%20Pairs%20With%20a%20Certain%20Sum/README_EN.md)", "`Design`,`Array`,`Hash Table`", "Medium", ""]}, {"question_id": "1994", "frontend_question_id": "1864", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/minimum-number-of-swaps-to-make-the-binary-string-alternating", "url_en": "https://leetcode.com/problems/minimum-number-of-swaps-to-make-the-binary-string-alternating", "relative_path_cn": "/solution/1800-1899/1864.Minimum%20Number%20of%20Swaps%20to%20Make%20the%20Binary%20String%20Alternating/README.md", "relative_path_en": "/solution/1800-1899/1864.Minimum%20Number%20of%20Swaps%20to%20Make%20the%20Binary%20String%20Alternating/README_EN.md", "title_cn": "\u6784\u6210\u4ea4\u66ff\u5b57\u7b26\u4e32\u9700\u8981\u7684\u6700\u5c0f\u4ea4\u6362\u6b21\u6570", "title_en": "Minimum Number of Swaps to Make the Binary String Alternating", "question_title_slug": "minimum-number-of-swaps-to-make-the-binary-string-alternating", "content_en": "Given a binary string s
, return the minimum number of character swaps to make it alternating, or -1
if it is impossible.
\n\nThe string is called alternating if no two adjacent characters are equal. For example, the strings "010"
and "1010"
are alternating, while the string "0100"
is not.
\n\nAny two characters may be swapped, even if they are not adjacent.
\n\n
\nExample 1:
\n\n\nInput: s = "111000"\nOutput: 1\nExplanation: Swap positions 1 and 4: "111000" -> "101010"\nThe string is now alternating.\n
\n\nExample 2:
\n\n\nInput: s = "010"\nOutput: 0\nExplanation: The string is already alternating, no swaps are needed.\n
\n\nExample 3:
\n\n\nInput: s = "1110"\nOutput: -1\n
\n\n
\nConstraints:
\n\n\n\t1 <= s.length <= 1000
\n\ts[i]
is either '0'
or '1'
. \n
\n", "content_cn": "\u7ed9\u4f60\u4e00\u4e2a\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32 s
\uff0c\u73b0\u9700\u8981\u5c06\u5176\u8f6c\u5316\u4e3a\u4e00\u4e2a \u4ea4\u66ff\u5b57\u7b26\u4e32 \u3002\u8bf7\u4f60\u8ba1\u7b97\u5e76\u8fd4\u56de\u8f6c\u5316\u6240\u9700\u7684 \u6700\u5c0f \u5b57\u7b26\u4ea4\u6362\u6b21\u6570\uff0c\u5982\u679c\u65e0\u6cd5\u5b8c\u6210\u8f6c\u5316\uff0c\u8fd4\u56de -1
\u3002
\n\n\u4ea4\u66ff\u5b57\u7b26\u4e32 \u662f\u6307\uff1a\u76f8\u90bb\u5b57\u7b26\u4e4b\u95f4\u4e0d\u5b58\u5728\u76f8\u7b49\u60c5\u51b5\u7684\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c\u5b57\u7b26\u4e32 \"010\"
\u548c \"1010\"
\u5c5e\u4e8e\u4ea4\u66ff\u5b57\u7b26\u4e32\uff0c\u4f46 \"0100\"
\u4e0d\u662f\u3002
\n\n\u4efb\u610f\u4e24\u4e2a\u5b57\u7b26\u90fd\u53ef\u4ee5\u8fdb\u884c\u4ea4\u6362\uff0c\u4e0d\u5fc5\u76f8\u90bb \u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1as = \"111000\"\n\u8f93\u51fa\uff1a1\n\u89e3\u91ca\uff1a\u4ea4\u6362\u4f4d\u7f6e 1 \u548c 4\uff1a\"111000\" -> \"101010\" \uff0c\u5b57\u7b26\u4e32\u53d8\u4e3a\u4ea4\u66ff\u5b57\u7b26\u4e32\u3002\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1as = \"010\"\n\u8f93\u51fa\uff1a0\n\u89e3\u91ca\uff1a\u5b57\u7b26\u4e32\u5df2\u7ecf\u662f\u4ea4\u66ff\u5b57\u7b26\u4e32\u4e86\uff0c\u4e0d\u9700\u8981\u4ea4\u6362\u3002\n
\n\n\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1as = \"1110\"\n\u8f93\u51fa\uff1a-1\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\t1 <= s.length <= 1000
\n\ts[i]
\u7684\u503c\u4e3a '0'
\u6216 '1'
\n
\n", "tags_en": ["Greedy", "String"], "tags_cn": ["\u8d2a\u5fc3", "\u5b57\u7b26\u4e32"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n int minSwaps(string s) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int minSwaps(String s) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def minSwaps(self, s):\n \"\"\"\n :type s: str\n :rtype: int\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def minSwaps(self, s: str) -> int:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nint minSwaps(char * s){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int MinSwaps(string s) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {string} s\n * @return {number}\n */\nvar minSwaps = function(s) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {String} s\n# @return {Integer}\ndef min_swaps(s)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func minSwaps(_ s: String) -> Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func minSwaps(s string) int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def minSwaps(s: String): Int = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun minSwaps(s: String): Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn min_swaps(s: String) -> i32 {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param String $s\n * @return Integer\n */\n function minSwaps($s) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function minSwaps(s: string): number {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (min-swaps s)\n (-> string? exact-integer?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec min_swaps(S :: unicode:unicode_binary()) -> integer().\nmin_swaps(S) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec min_swaps(s :: String.t) :: integer\n def min_swaps(s) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1864](https://leetcode-cn.com/problems/minimum-number-of-swaps-to-make-the-binary-string-alternating)", "[\u6784\u6210\u4ea4\u66ff\u5b57\u7b26\u4e32\u9700\u8981\u7684\u6700\u5c0f\u4ea4\u6362\u6b21\u6570](/solution/1800-1899/1864.Minimum%20Number%20of%20Swaps%20to%20Make%20the%20Binary%20String%20Alternating/README.md)", "`\u8d2a\u5fc3`,`\u5b57\u7b26\u4e32`", "\u4e2d\u7b49", ""], "md_table_row_en": ["[1864](https://leetcode.com/problems/minimum-number-of-swaps-to-make-the-binary-string-alternating)", "[Minimum Number of Swaps to Make the Binary String Alternating](/solution/1800-1899/1864.Minimum%20Number%20of%20Swaps%20to%20Make%20the%20Binary%20String%20Alternating/README_EN.md)", "`Greedy`,`String`", "Medium", ""]}, {"question_id": "1993", "frontend_question_id": "1863", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/sum-of-all-subset-xor-totals", "url_en": "https://leetcode.com/problems/sum-of-all-subset-xor-totals", "relative_path_cn": "/solution/1800-1899/1863.Sum%20of%20All%20Subset%20XOR%20Totals/README.md", "relative_path_en": "/solution/1800-1899/1863.Sum%20of%20All%20Subset%20XOR%20Totals/README_EN.md", "title_cn": "\u627e\u51fa\u6240\u6709\u5b50\u96c6\u7684\u5f02\u6216\u603b\u548c\u518d\u6c42\u548c", "title_en": "Sum of All Subset XOR Totals", "question_title_slug": "sum-of-all-subset-xor-totals", "content_en": "The XOR total of an array is defined as the bitwise XOR
of all its elements, or 0
if the array is empty.
\n\n\n\t- For example, the XOR total of the array
[2,5,6]
is 2 XOR 5 XOR 6 = 1
. \n
\n\nGiven an array nums
, return the sum of all XOR totals for every subset of nums
.
\n\nNote: Subsets with the same elements should be counted multiple times.
\n\nAn array a
is a subset of an array b
if a
can be obtained from b
by deleting some (possibly zero) elements of b
.
\n\n
\nExample 1:
\n\n\nInput: nums = [1,3]\nOutput: 6\nExplanation: The 4 subsets of [1,3] are:\n- The empty subset has an XOR total of 0.\n- [1] has an XOR total of 1.\n- [3] has an XOR total of 3.\n- [1,3] has an XOR total of 1 XOR 3 = 2.\n0 + 1 + 3 + 2 = 6\n
\n\nExample 2:
\n\n\nInput: nums = [5,1,6]\nOutput: 28\nExplanation: The 8 subsets of [5,1,6] are:\n- The empty subset has an XOR total of 0.\n- [5] has an XOR total of 5.\n- [1] has an XOR total of 1.\n- [6] has an XOR total of 6.\n- [5,1] has an XOR total of 5 XOR 1 = 4.\n- [5,6] has an XOR total of 5 XOR 6 = 3.\n- [1,6] has an XOR total of 1 XOR 6 = 7.\n- [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2.\n0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28\n
\n\nExample 3:
\n\n\nInput: nums = [3,4,5,6,7,8]\nOutput: 480\nExplanation: The sum of all XOR totals for every subset is 480.\n
\n\n
\nConstraints:
\n\n\n\t1 <= nums.length <= 12
\n\t1 <= nums[i] <= 20
\n
\n", "content_cn": "\u4e00\u4e2a\u6570\u7ec4\u7684 \u5f02\u6216\u603b\u548c \u5b9a\u4e49\u4e3a\u6570\u7ec4\u4e2d\u6240\u6709\u5143\u7d20\u6309\u4f4d XOR
\u7684\u7ed3\u679c\uff1b\u5982\u679c\u6570\u7ec4\u4e3a \u7a7a \uff0c\u5219\u5f02\u6216\u603b\u548c\u4e3a 0
\u3002
\n\n\n\t- \u4f8b\u5982\uff0c\u6570\u7ec4\u00a0
[2,5,6]
\u7684 \u5f02\u6216\u603b\u548c \u4e3a 2 XOR 5 XOR 6 = 1
\u3002 \n
\n\n\u7ed9\u4f60\u4e00\u4e2a\u6570\u7ec4 nums
\uff0c\u8bf7\u4f60\u6c42\u51fa nums
\u4e2d\u6bcf\u4e2a \u5b50\u96c6 \u7684 \u5f02\u6216\u603b\u548c \uff0c\u8ba1\u7b97\u5e76\u8fd4\u56de\u8fd9\u4e9b\u503c\u76f8\u52a0\u4e4b \u548c \u3002
\n\n\u6ce8\u610f\uff1a\u5728\u672c\u9898\u4e2d\uff0c\u5143\u7d20 \u76f8\u540c \u7684\u4e0d\u540c\u5b50\u96c6\u5e94 \u591a\u6b21 \u8ba1\u6570\u3002
\n\n\u6570\u7ec4 a
\u662f\u6570\u7ec4 b
\u7684\u4e00\u4e2a \u5b50\u96c6 \u7684\u524d\u63d0\u6761\u4ef6\u662f\uff1a\u4ece b
\u5220\u9664\u51e0\u4e2a\uff08\u4e5f\u53ef\u80fd\u4e0d\u5220\u9664\uff09\u5143\u7d20\u80fd\u591f\u5f97\u5230 a
\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1anums = [1,3]\n\u8f93\u51fa\uff1a6\n\u89e3\u91ca\uff1a[1,3] \u5171\u6709 4 \u4e2a\u5b50\u96c6\uff1a\n- \u7a7a\u5b50\u96c6\u7684\u5f02\u6216\u603b\u548c\u662f 0 \u3002\n- [1] \u7684\u5f02\u6216\u603b\u548c\u4e3a 1 \u3002\n- [3] \u7684\u5f02\u6216\u603b\u548c\u4e3a 3 \u3002\n- [1,3] \u7684\u5f02\u6216\u603b\u548c\u4e3a 1 XOR 3 = 2 \u3002\n0 + 1 + 3 + 2 = 6\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1anums = [5,1,6]\n\u8f93\u51fa\uff1a28\n\u89e3\u91ca\uff1a[5,1,6] \u5171\u6709 8 \u4e2a\u5b50\u96c6\uff1a\n- \u7a7a\u5b50\u96c6\u7684\u5f02\u6216\u603b\u548c\u662f 0 \u3002\n- [5] \u7684\u5f02\u6216\u603b\u548c\u4e3a 5 \u3002\n- [1] \u7684\u5f02\u6216\u603b\u548c\u4e3a 1 \u3002\n- [6] \u7684\u5f02\u6216\u603b\u548c\u4e3a 6 \u3002\n- [5,1] \u7684\u5f02\u6216\u603b\u548c\u4e3a 5 XOR 1 = 4 \u3002\n- [5,6] \u7684\u5f02\u6216\u603b\u548c\u4e3a 5 XOR 6 = 3 \u3002\n- [1,6] \u7684\u5f02\u6216\u603b\u548c\u4e3a 1 XOR 6 = 7 \u3002\n- [5,1,6] \u7684\u5f02\u6216\u603b\u548c\u4e3a 5 XOR 1 XOR 6 = 2 \u3002\n0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28\n
\n\n\u793a\u4f8b 3\uff1a
\n\n\u8f93\u5165\uff1anums = [3,4,5,6,7,8]\n\u8f93\u51fa\uff1a480\n\u89e3\u91ca\uff1a\u6bcf\u4e2a\u5b50\u96c6\u7684\u5168\u90e8\u5f02\u6216\u603b\u548c\u503c\u4e4b\u548c\u4e3a 480 \u3002\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\t1 <= nums.length <= 12
\n\t1 <= nums[i] <= 20
\n
\n", "tags_en": ["Bit Manipulation", "Array", "Backtracking"], "tags_cn": ["\u4f4d\u8fd0\u7b97", "\u6570\u7ec4", "\u56de\u6eaf"], "difficulty_en": "Easy", "difficulty_cn": "\u7b80\u5355", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n int subsetXORSum(vector& nums) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int subsetXORSum(int[] nums) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def subsetXORSum(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def subsetXORSum(self, nums: List[int]) -> int:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nint subsetXORSum(int* nums, int numsSize){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int SubsetXORSum(int[] nums) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[]} nums\n * @return {number}\n */\nvar subsetXORSum = function(nums) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer[]} nums\n# @return {Integer}\ndef subset_xor_sum(nums)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func subsetXORSum(_ nums: [Int]) -> Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func subsetXORSum(nums []int) int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def subsetXORSum(nums: Array[Int]): Int = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun subsetXORSum(nums: IntArray): Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn subset_xor_sum(nums: Vec) -> i32 {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @return Integer\n */\n function subsetXORSum($nums) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function subsetXORSum(nums: number[]): number {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (subset-xor-sum nums)\n (-> (listof exact-integer?) exact-integer?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec subset_xor_sum(Nums :: [integer()]) -> integer().\nsubset_xor_sum(Nums) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec subset_xor_sum(nums :: [integer]) :: integer\n def subset_xor_sum(nums) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1863](https://leetcode-cn.com/problems/sum-of-all-subset-xor-totals)", "[\u627e\u51fa\u6240\u6709\u5b50\u96c6\u7684\u5f02\u6216\u603b\u548c\u518d\u6c42\u548c](/solution/1800-1899/1863.Sum%20of%20All%20Subset%20XOR%20Totals/README.md)", "`\u4f4d\u8fd0\u7b97`,`\u6570\u7ec4`,`\u56de\u6eaf`", "\u7b80\u5355", ""], "md_table_row_en": ["[1863](https://leetcode.com/problems/sum-of-all-subset-xor-totals)", "[Sum of All Subset XOR Totals](/solution/1800-1899/1863.Sum%20of%20All%20Subset%20XOR%20Totals/README_EN.md)", "`Bit Manipulation`,`Array`,`Backtracking`", "Easy", ""]}, {"question_id": "1991", "frontend_question_id": "1841", "paid_only": true, "paid_only_cn": true, "category": "Database", "url_cn": "https://leetcode-cn.com/problems/league-statistics", "url_en": "https://leetcode.com/problems/league-statistics", "relative_path_cn": "/solution/1800-1899/1841.League%20Statistics/README.md", "relative_path_en": "/solution/1800-1899/1841.League%20Statistics/README_EN.md", "title_cn": "League Statistics", "title_en": "League Statistics", "question_title_slug": "league-statistics", "content_en": "Table: Teams
\r\n\r\n\r\n+----------------+---------+\r\n| Column Name | Type |\r\n+----------------+---------+\r\n| team_id | int |\r\n| team_name | varchar |\r\n+----------------+---------+\r\nteam_id is the primary key for this table.\r\nEach row contains information about one team in the league.\r\n
\r\n\r\n
\r\n\r\nTable: Matches
\r\n\r\n\r\n+-----------------+---------+\r\n| Column Name | Type |\r\n+-----------------+---------+\r\n| home_team_id | int |\r\n| away_team_id | int |\r\n| home_team_goals | int |\r\n| away_team_goals | int |\r\n+-----------------+---------+\r\n(home_team_id, away_team_id) is the primary key for this table.\r\nEach row contains information about one match.\r\nhome_team_goals is the number of goals scored by the home team.\r\naway_team_goals is the number of goals scored by the away team.\r\nThe winner of the match is the team with the higher number of goals.\r\n
\r\n\r\n
\r\n\r\nWrite an SQL query to report the statistics of the league. The statistics should be built using the played matches where the winning team gets three points and the losing team gets no points. If a match ends with a draw, both teams get one point.
\r\n\r\nEach row of the result table should contain:
\r\n\r\n\r\n\tteam_name
- The name of the team in the Teams
table. \r\n\tmatches_played
- The number of matches played as either a home or away team. \r\n\tpoints
- The total points the team has so far. \r\n\tgoal_for
- The total number of goals scored by the team across all matches. \r\n\tgoal_against
- The total number of goals scored by opponent teams against this team across all matches. \r\n\tgoal_diff
- The result of goal_for - goal_against
. \r\n
\r\n\r\nReturn the result table in descending order by points
. If two or more teams have the same points
, order them in descending order by goal_diff
. If there is still a tie, order them by team_name
in lexicographical order.
\r\n\r\nThe query result format is in the following example:
\r\n\r\n
\r\n\r\n\r\nTeams table:\r\n+---------+-----------+\r\n| team_id | team_name |\r\n+---------+-----------+\r\n| 1 | Ajax |\r\n| 4 | Dortmund |\r\n| 6 | Arsenal |\r\n+---------+-----------+\r\n\r\nMatches table:\r\n+--------------+--------------+-----------------+-----------------+\r\n| home_team_id | away_team_id | home_team_goals | away_team_goals |\r\n+--------------+--------------+-----------------+-----------------+\r\n| 1 | 4 | 0 | 1 |\r\n| 1 | 6 | 3 | 3 |\r\n| 4 | 1 | 5 | 2 |\r\n| 6 | 1 | 0 | 0 |\r\n+--------------+--------------+-----------------+-----------------+\r\n\r\n\r\nResult table:\r\n+-----------+----------------+--------+----------+--------------+-----------+\r\n| team_name | matches_played | points | goal_for | goal_against | goal_diff |\r\n+-----------+----------------+--------+----------+--------------+-----------+\r\n| Dortmund | 2 | 6 | 6 | 2 | 4 |\r\n| Arsenal | 2 | 2 | 3 | 3 | 0 |\r\n| Ajax | 4 | 2 | 5 | 9 | -4 |\r\n+-----------+----------------+--------+----------+--------------+-----------+\r\n\r\nAjax (team_id=1) played 4 matches: 2 losses and 2 draws. Total points = 0 + 0 + 1 + 1 = 2.\r\nDortmund (team_id=4) played 2 matches: 2 wins. Total points = 3 + 3 = 6.\r\nArsenal (team_id=6) played 2 matches: 2 draws. Total points = 1 + 1 = 2.\r\nDortmund is the first team in the table. Ajax and Arsenal have the same points, but since Arsenal has a higher goal_diff than Ajax, Arsenal comes before Ajax in the table.\r\n
", "content_cn": "Table: Teams
\r\n\r\n\r\n+----------------+---------+\r\n| Column Name | Type |\r\n+----------------+---------+\r\n| team_id | int |\r\n| team_name | varchar |\r\n+----------------+---------+\r\nteam_id is the primary key for this table.\r\nEach row contains information about one team in the league.\r\n
\r\n\r\n
\r\n\r\nTable: Matches
\r\n\r\n\r\n+-----------------+---------+\r\n| Column Name | Type |\r\n+-----------------+---------+\r\n| home_team_id | int |\r\n| away_team_id | int |\r\n| home_team_goals | int |\r\n| away_team_goals | int |\r\n+-----------------+---------+\r\n(home_team_id, away_team_id) is the primary key for this table.\r\nEach row contains information about one match.\r\nhome_team_goals is the number of goals scored by the home team.\r\naway_team_goals is the number of goals scored by the away team.\r\nThe winner of the match is the team with the higher number of goals.\r\n
\r\n\r\n
\r\n\r\nWrite an SQL query to report the statistics of the league. The statistics should be built using the played matches where the winning team gets three points and the losing team gets no points. If a match ends with a draw, both teams get one point.
\r\n\r\nEach row of the result table should contain:
\r\n\r\n\r\n\tteam_name
- The name of the team in the Teams
table. \r\n\tmatches_played
- The number of matches played as either a home or away team. \r\n\tpoints
- The total points the team has so far. \r\n\tgoal_for
- The total number of goals scored by the team across all matches. \r\n\tgoal_against
- The total number of goals scored by opponent teams against this team across all matches. \r\n\tgoal_diff
- The result of goal_for - goal_against
. \r\n
\r\n\r\nReturn the result table in descending order by points
. If two or more teams have the same points
, order them in descending order by goal_diff
. If there is still a tie, order them by team_name
in lexicographical order.
\r\n\r\nThe query result format is in the following example:
\r\n\r\n
\r\n\r\n\r\nTeams table:\r\n+---------+-----------+\r\n| team_id | team_name |\r\n+---------+-----------+\r\n| 1 | Ajax |\r\n| 4 | Dortmund |\r\n| 6 | Arsenal |\r\n+---------+-----------+\r\n\r\nMatches table:\r\n+--------------+--------------+-----------------+-----------------+\r\n| home_team_id | away_team_id | home_team_goals | away_team_goals |\r\n+--------------+--------------+-----------------+-----------------+\r\n| 1 | 4 | 0 | 1 |\r\n| 1 | 6 | 3 | 3 |\r\n| 4 | 1 | 5 | 2 |\r\n| 6 | 1 | 0 | 0 |\r\n+--------------+--------------+-----------------+-----------------+\r\n\r\n\r\nResult table:\r\n+-----------+----------------+--------+----------+--------------+-----------+\r\n| team_name | matches_played | points | goal_for | goal_against | goal_diff |\r\n+-----------+----------------+--------+----------+--------------+-----------+\r\n| Dortmund | 2 | 6 | 6 | 2 | 4 |\r\n| Arsenal | 2 | 2 | 3 | 3 | 0 |\r\n| Ajax | 4 | 2 | 5 | 9 | -4 |\r\n+-----------+----------------+--------+----------+--------------+-----------+\r\n\r\nAjax (team_id=1) played 4 matches: 2 losses and 2 draws. Total points = 0 + 0 + 1 + 1 = 2.\r\nDortmund (team_id=4) played 2 matches: 2 wins. Total points = 3 + 3 = 6.\r\nArsenal (team_id=6) played 2 matches: 2 draws. Total points = 1 + 1 = 2.\r\nDortmund is the first team in the table. Ajax and Arsenal have the same points, but since Arsenal has a higher goal_diff than Ajax, Arsenal comes before Ajax in the table.\r\n
", "tags_en": ["Database"], "tags_cn": ["\u6570\u636e\u5e93"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "MySQL", "langSlug": "mysql", "code": "# Write your MySQL query statement below", "__typename": "CodeSnippetNode"}, {"lang": "MS SQL Server", "langSlug": "mssql", "code": "/* Write your T-SQL query statement below */", "__typename": "CodeSnippetNode"}, {"lang": "Oracle", "langSlug": "oraclesql", "code": "/* Write your PL/SQL query statement below */", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1841](https://leetcode-cn.com/problems/league-statistics)", "[League Statistics](/solution/1800-1899/1841.League%20Statistics/README.md)", "`\u6570\u636e\u5e93`", "\u4e2d\u7b49", "\ud83d\udd12"], "md_table_row_en": ["[1841](https://leetcode.com/problems/league-statistics)", "[League Statistics](/solution/1800-1899/1841.League%20Statistics/README_EN.md)", "`Database`", "Medium", "\ud83d\udd12"]}, {"question_id": "1990", "frontend_question_id": "1878", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/get-biggest-three-rhombus-sums-in-a-grid", "url_en": "https://leetcode.com/problems/get-biggest-three-rhombus-sums-in-a-grid", "relative_path_cn": "/solution/1800-1899/1878.Get%20Biggest%20Three%20Rhombus%20Sums%20in%20a%20Grid/README.md", "relative_path_en": "/solution/1800-1899/1878.Get%20Biggest%20Three%20Rhombus%20Sums%20in%20a%20Grid/README_EN.md", "title_cn": "\u77e9\u9635\u4e2d\u6700\u5927\u7684\u4e09\u4e2a\u83f1\u5f62\u548c", "title_en": "Get Biggest Three Rhombus Sums in a Grid", "question_title_slug": "get-biggest-three-rhombus-sums-in-a-grid", "content_en": "You are given an m x n
integer matrix grid
\u200b\u200b\u200b.
\n\nA rhombus sum is the sum of the elements that form the border of a regular rhombus shape in grid
\u200b\u200b\u200b. The rhombus must have the shape of a square rotated 45 degrees with each of the corners centered in a grid cell. Below is an image of four valid rhombus shapes with the corresponding colored cells that should be included in each rhombus sum:
\n
\nNote that the rhombus can have an area of 0, which is depicted by the purple rhombus in the bottom right corner.
\n\nReturn the biggest three distinct rhombus sums in the grid
in descending order. If there are less than three distinct values, return all of them.
\n\n
\nExample 1:
\n
\n\nInput: grid = [[3,4,5,1,3],[3,3,4,2,3],[20,30,200,40,10],[1,5,5,4,1],[4,3,2,2,5]]\nOutput: [228,216,211]\nExplanation: The rhombus shapes for the three biggest distinct rhombus sums are depicted above.\n- Blue: 20 + 3 + 200 + 5 = 228\n- Red: 200 + 2 + 10 + 4 = 216\n- Green: 5 + 200 + 4 + 2 = 211\n
\n\nExample 2:
\n
\n\nInput: grid = [[1,2,3],[4,5,6],[7,8,9]]\nOutput: [20,9,8]\nExplanation: The rhombus shapes for the three biggest distinct rhombus sums are depicted above.\n- Blue: 4 + 2 + 6 + 8 = 20\n- Red: 9 (area 0 rhombus in the bottom right corner)\n- Green: 8 (area 0 rhombus in the bottom middle)\n
\n\nExample 3:
\n\n\nInput: grid = [[7,7,7]]\nOutput: [7]\nExplanation: All three possible rhombus sums are the same, so return [7].\n
\n\n
\nConstraints:
\n\n\n\tm == grid.length
\n\tn == grid[i].length
\n\t1 <= m, n <= 50
\n\t1 <= grid[i][j] <= 105
\n
\n", "content_cn": "\u7ed9\u4f60\u4e00\u4e2a\u00a0m x n
\u00a0\u7684\u6574\u6570\u77e9\u9635\u00a0grid
\u00a0\u3002
\n\n\u83f1\u5f62\u548c \u6307\u7684\u662f grid
\u00a0\u4e2d\u4e00\u4e2a\u6b63\u83f1\u5f62 \u8fb9\u754c\u00a0\u4e0a\u7684\u5143\u7d20\u4e4b\u548c\u3002\u672c\u9898\u4e2d\u7684\u83f1\u5f62\u5fc5\u987b\u4e3a\u6b63\u65b9\u5f62\u65cb\u8f6c45\u5ea6\uff0c\u4e14\u56db\u4e2a\u89d2\u90fd\u5728\u4e00\u4e2a\u683c\u5b50\u5f53\u4e2d\u3002\u4e0b\u56fe\u662f\u56db\u4e2a\u53ef\u884c\u7684\u83f1\u5f62\uff0c\u6bcf\u4e2a\u83f1\u5f62\u548c\u5e94\u8be5\u5305\u542b\u7684\u683c\u5b50\u90fd\u7528\u4e86\u76f8\u5e94\u989c\u8272\u6807\u6ce8\u5728\u56fe\u4e2d\u3002
\n
\n\u00a0
\n\n\u6ce8\u610f\uff0c\u83f1\u5f62\u53ef\u4ee5\u662f\u4e00\u4e2a\u9762\u79ef\u4e3a 0 \u7684\u533a\u57df\uff0c\u5982\u4e0a\u56fe\u4e2d\u53f3\u4e0b\u89d2\u7684\u7d2b\u8272\u83f1\u5f62\u6240\u793a\u3002
\n\n\u8bf7\u4f60\u6309\u7167 \u964d\u5e8f\u00a0\u8fd4\u56de grid
\u00a0\u4e2d\u4e09\u4e2a\u6700\u5927\u7684\u00a0\u4e92\u4e0d\u76f8\u540c\u7684\u83f1\u5f62\u548c\u00a0\u3002\u5982\u679c\u4e0d\u540c\u7684\u548c\u5c11\u4e8e\u4e09\u4e2a\uff0c\u5219\u5c06\u5b83\u4eec\u5168\u90e8\u8fd4\u56de\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n
\n\n\u8f93\u5165\uff1agrid = [[3,4,5,1,3],[3,3,4,2,3],[20,30,200,40,10],[1,5,5,4,1],[4,3,2,2,5]]\n\u8f93\u51fa\uff1a[228,216,211]\n\u89e3\u91ca\uff1a\u6700\u5927\u7684\u4e09\u4e2a\u83f1\u5f62\u548c\u5982\u4e0a\u56fe\u6240\u793a\u3002\n- \u84dd\u8272\uff1a20 + 3 + 200 + 5 = 228\n- \u7ea2\u8272\uff1a200 + 2 + 10 + 4 = 216\n- \u7eff\u8272\uff1a5 + 200 + 4 + 2 = 211\n
\n\n\u793a\u4f8b 2\uff1a
\n
\n\n\u8f93\u5165\uff1agrid = [[1,2,3],[4,5,6],[7,8,9]]\n\u8f93\u51fa\uff1a[20,9,8]\n\u89e3\u91ca\uff1a\u6700\u5927\u7684\u4e09\u4e2a\u83f1\u5f62\u548c\u5982\u4e0a\u56fe\u6240\u793a\u3002\n- \u84dd\u8272\uff1a4 + 2 + 6 + 8 = 20\n- \u7ea2\u8272\uff1a9 \uff08\u53f3\u4e0b\u89d2\u7ea2\u8272\u7684\u9762\u79ef\u4e3a 0 \u7684\u83f1\u5f62\uff09\n- \u7eff\u8272\uff1a8 \uff08\u4e0b\u65b9\u4e2d\u592e\u9762\u79ef\u4e3a 0 \u7684\u83f1\u5f62\uff09\n
\n\n\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1agrid = [[7,7,7]]\n\u8f93\u51fa\uff1a[7]\n\u89e3\u91ca\uff1a\u6240\u6709\u4e09\u4e2a\u53ef\u80fd\u7684\u83f1\u5f62\u548c\u90fd\u76f8\u540c\uff0c\u6240\u4ee5\u8fd4\u56de [7] \u3002\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\tm == grid.length
\n\tn == grid[i].length
\n\t1 <= m, n <= 100
\n\t1 <= grid[i][j] <= 105
\n
\n", "tags_en": ["Array", "Math", "Matrix", "Prefix Sum", "Sorting", "Heap (Priority Queue)"], "tags_cn": ["\u6570\u7ec4", "\u6570\u5b66", "\u77e9\u9635", "\u524d\u7f00\u548c", "\u6392\u5e8f", "\u5806\uff08\u4f18\u5148\u961f\u5217\uff09"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n vector getBiggestThree(vector>& grid) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int[] getBiggestThree(int[][] grid) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def getBiggestThree(self, grid):\n \"\"\"\n :type grid: List[List[int]]\n :rtype: List[int]\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def getBiggestThree(self, grid: List[List[int]]) -> List[int]:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\n/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\nint* getBiggestThree(int** grid, int gridSize, int* gridColSize, int* returnSize){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int[] GetBiggestThree(int[][] grid) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[][]} grid\n * @return {number[]}\n */\nvar getBiggestThree = function(grid) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer[][]} grid\n# @return {Integer[]}\ndef get_biggest_three(grid)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func getBiggestThree(_ grid: [[Int]]) -> [Int] {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func getBiggestThree(grid [][]int) []int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def getBiggestThree(grid: Array[Array[Int]]): Array[Int] = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun getBiggestThree(grid: Array): IntArray {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn get_biggest_three(grid: Vec>) -> Vec {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer[][] $grid\n * @return Integer[]\n */\n function getBiggestThree($grid) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function getBiggestThree(grid: number[][]): number[] {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (get-biggest-three grid)\n (-> (listof (listof exact-integer?)) (listof exact-integer?))\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec get_biggest_three(Grid :: [[integer()]]) -> [integer()].\nget_biggest_three(Grid) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec get_biggest_three(grid :: [[integer]]) :: [integer]\n def get_biggest_three(grid) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1878](https://leetcode-cn.com/problems/get-biggest-three-rhombus-sums-in-a-grid)", "[\u77e9\u9635\u4e2d\u6700\u5927\u7684\u4e09\u4e2a\u83f1\u5f62\u548c](/solution/1800-1899/1878.Get%20Biggest%20Three%20Rhombus%20Sums%20in%20a%20Grid/README.md)", "`\u6570\u7ec4`,`\u6570\u5b66`,`\u77e9\u9635`,`\u524d\u7f00\u548c`,`\u6392\u5e8f`,`\u5806\uff08\u4f18\u5148\u961f\u5217\uff09`", "\u4e2d\u7b49", ""], "md_table_row_en": ["[1878](https://leetcode.com/problems/get-biggest-three-rhombus-sums-in-a-grid)", "[Get Biggest Three Rhombus Sums in a Grid](/solution/1800-1899/1878.Get%20Biggest%20Three%20Rhombus%20Sums%20in%20a%20Grid/README_EN.md)", "`Array`,`Math`,`Matrix`,`Prefix Sum`,`Sorting`,`Heap (Priority Queue)`", "Medium", ""]}, {"question_id": "1989", "frontend_question_id": "1879", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/minimum-xor-sum-of-two-arrays", "url_en": "https://leetcode.com/problems/minimum-xor-sum-of-two-arrays", "relative_path_cn": "/solution/1800-1899/1879.Minimum%20XOR%20Sum%20of%20Two%20Arrays/README.md", "relative_path_en": "/solution/1800-1899/1879.Minimum%20XOR%20Sum%20of%20Two%20Arrays/README_EN.md", "title_cn": "\u4e24\u4e2a\u6570\u7ec4\u6700\u5c0f\u7684\u5f02\u6216\u503c\u4e4b\u548c", "title_en": "Minimum XOR Sum of Two Arrays", "question_title_slug": "minimum-xor-sum-of-two-arrays", "content_en": "You are given two integer arrays nums1
and nums2
of length n
.
\n\nThe XOR sum of the two integer arrays is (nums1[0] XOR nums2[0]) + (nums1[1] XOR nums2[1]) + ... + (nums1[n - 1] XOR nums2[n - 1])
(0-indexed).
\n\n\n\t- For example, the XOR sum of
[1,2,3]
and [3,2,1]
is equal to (1 XOR 3) + (2 XOR 2) + (3 XOR 1) = 2 + 0 + 2 = 4
. \n
\n\nRearrange the elements of nums2
such that the resulting XOR sum is minimized.
\n\nReturn the XOR sum after the rearrangement.
\n\n
\nExample 1:
\n\n\nInput: nums1 = [1,2], nums2 = [2,3]\nOutput: 2\nExplanation: Rearrange nums2
so that it becomes [3,2]
.\nThe XOR sum is (1 XOR 3) + (2 XOR 2) = 2 + 0 = 2.
\n\nExample 2:
\n\n\nInput: nums1 = [1,0,3], nums2 = [5,3,4]\nOutput: 8\nExplanation: Rearrange nums2
so that it becomes [5,4,3]
. \nThe XOR sum is (1 XOR 5) + (0 XOR 4) + (3 XOR 3) = 4 + 4 + 0 = 8.\n
\n\n
\nConstraints:
\n\n\n\tn == nums1.length
\n\tn == nums2.length
\n\t1 <= n <= 14
\n\t0 <= nums1[i], nums2[i] <= 107
\n
\n", "content_cn": "\u7ed9\u4f60\u4e24\u4e2a\u6574\u6570\u6570\u7ec4\u00a0nums1
\u548c\u00a0nums2
\u00a0\uff0c\u5b83\u4eec\u957f\u5ea6\u90fd\u4e3a\u00a0n
\u00a0\u3002
\n\n\u4e24\u4e2a\u6570\u7ec4\u7684 \u5f02\u6216\u503c\u4e4b\u548c\u00a0\u4e3a\u00a0(nums1[0] XOR nums2[0]) + (nums1[1] XOR nums2[1]) + ... + (nums1[n - 1] XOR nums2[n - 1])
\u00a0\uff08\u4e0b\u6807\u4ece 0 \u5f00\u59cb\uff09\u3002
\n\n\n\t- \u6bd4\u65b9\u8bf4\uff0c
[1,2,3]
\u548c\u00a0[3,2,1]
\u00a0\u7684 \u5f02\u6216\u503c\u4e4b\u548c\u00a0\u7b49\u4e8e\u00a0(1 XOR 3) + (2 XOR 2) + (3 XOR 1) = 2 + 0 + 2 = 4
\u00a0\u3002 \n
\n\n\u8bf7\u4f60\u5c06\u00a0nums2
\u00a0\u4e2d\u7684\u5143\u7d20\u91cd\u65b0\u6392\u5217\uff0c\u4f7f\u5f97 \u5f02\u6216\u503c\u4e4b\u548c\u00a0\u6700\u5c0f\u00a0\u3002
\n\n\u8bf7\u4f60\u8fd4\u56de\u91cd\u65b0\u6392\u5217\u4e4b\u540e\u7684 \u5f02\u6216\u503c\u4e4b\u548c\u00a0\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1anums1 = [1,2], nums2 = [2,3]\n\u8f93\u51fa\uff1a2\n\u89e3\u91ca\uff1a\u5c06 nums2
\u91cd\u65b0\u6392\u5217\u5f97\u5230 [3,2] \u3002
\n\u5f02\u6216\u503c\u4e4b\u548c\u4e3a (1 XOR 3) + (2 XOR 2) = 2 + 0 = 2 \u3002
\n\n\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1anums1 = [1,0,3], nums2 = [5,3,4]\n\u8f93\u51fa\uff1a8\n\u89e3\u91ca\uff1a\u5c06 nums2 \u91cd\u65b0\u6392\u5217\u5f97\u5230
[5,4,3] \u3002
\n\u5f02\u6216\u503c\u4e4b\u548c\u4e3a (1 XOR 5) + (0 XOR 4) + (3 XOR 3) = 4 + 4 + 0 = 8 \u3002\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\tn == nums1.length
\n\tn == nums2.length
\n\t1 <= n <= 14
\n\t0 <= nums1[i], nums2[i] <= 107
\n
\n", "tags_en": ["Bit Manipulation", "Array", "Dynamic Programming", "Bitmask"], "tags_cn": ["\u4f4d\u8fd0\u7b97", "\u6570\u7ec4", "\u52a8\u6001\u89c4\u5212", "\u72b6\u6001\u538b\u7f29"], "difficulty_en": "Hard", "difficulty_cn": "\u56f0\u96be", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n int minimumXORSum(vector& nums1, vector& nums2) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int minimumXORSum(int[] nums1, int[] nums2) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def minimumXORSum(self, nums1, nums2):\n \"\"\"\n :type nums1: List[int]\n :type nums2: List[int]\n :rtype: int\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def minimumXORSum(self, nums1: List[int], nums2: List[int]) -> int:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nint minimumXORSum(int* nums1, int nums1Size, int* nums2, int nums2Size){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int MinimumXORSum(int[] nums1, int[] nums2) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[]} nums1\n * @param {number[]} nums2\n * @return {number}\n */\nvar minimumXORSum = function(nums1, nums2) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer[]} nums1\n# @param {Integer[]} nums2\n# @return {Integer}\ndef minimum_xor_sum(nums1, nums2)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func minimumXORSum(_ nums1: [Int], _ nums2: [Int]) -> Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func minimumXORSum(nums1 []int, nums2 []int) int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def minimumXORSum(nums1: Array[Int], nums2: Array[Int]): Int = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun minimumXORSum(nums1: IntArray, nums2: IntArray): Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn minimum_xor_sum(nums1: Vec, nums2: Vec) -> i32 {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer[] $nums1\n * @param Integer[] $nums2\n * @return Integer\n */\n function minimumXORSum($nums1, $nums2) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function minimumXORSum(nums1: number[], nums2: number[]): number {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (minimum-xor-sum nums1 nums2)\n (-> (listof exact-integer?) (listof exact-integer?) exact-integer?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec minimum_xor_sum(Nums1 :: [integer()], Nums2 :: [integer()]) -> integer().\nminimum_xor_sum(Nums1, Nums2) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec minimum_xor_sum(nums1 :: [integer], nums2 :: [integer]) :: integer\n def minimum_xor_sum(nums1, nums2) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1879](https://leetcode-cn.com/problems/minimum-xor-sum-of-two-arrays)", "[\u4e24\u4e2a\u6570\u7ec4\u6700\u5c0f\u7684\u5f02\u6216\u503c\u4e4b\u548c](/solution/1800-1899/1879.Minimum%20XOR%20Sum%20of%20Two%20Arrays/README.md)", "`\u4f4d\u8fd0\u7b97`,`\u6570\u7ec4`,`\u52a8\u6001\u89c4\u5212`,`\u72b6\u6001\u538b\u7f29`", "\u56f0\u96be", ""], "md_table_row_en": ["[1879](https://leetcode.com/problems/minimum-xor-sum-of-two-arrays)", "[Minimum XOR Sum of Two Arrays](/solution/1800-1899/1879.Minimum%20XOR%20Sum%20of%20Two%20Arrays/README_EN.md)", "`Bit Manipulation`,`Array`,`Dynamic Programming`,`Bitmask`", "Hard", ""]}, {"question_id": "1988", "frontend_question_id": "1877", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/minimize-maximum-pair-sum-in-array", "url_en": "https://leetcode.com/problems/minimize-maximum-pair-sum-in-array", "relative_path_cn": "/solution/1800-1899/1877.Minimize%20Maximum%20Pair%20Sum%20in%20Array/README.md", "relative_path_en": "/solution/1800-1899/1877.Minimize%20Maximum%20Pair%20Sum%20in%20Array/README_EN.md", "title_cn": "\u6570\u7ec4\u4e2d\u6700\u5927\u6570\u5bf9\u548c\u7684\u6700\u5c0f\u503c", "title_en": "Minimize Maximum Pair Sum in Array", "question_title_slug": "minimize-maximum-pair-sum-in-array", "content_en": "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.
\r\n\r\n\r\n\t- For example, if we have pairs
(1,5)
, (2,3)
, and (4,4)
, the maximum pair sum would be max(1+5, 2+3, 4+4) = max(6, 5, 8) = 8
. \r\n
\r\n\r\nGiven an array nums
of even length n
, pair up the elements of nums
into n / 2
pairs such that:
\r\n\r\n\r\n\t- Each element of
nums
is in exactly one pair, and \r\n\t- The maximum pair sum is minimized.
\r\n
\r\n\r\nReturn the minimized maximum pair sum after optimally pairing up the elements.
\r\n\r\n
\r\nExample 1:
\r\n\r\n\r\nInput: nums = [3,5,2,3]\r\nOutput: 7\r\nExplanation: The elements can be paired up into pairs (3,3) and (5,2).\r\nThe maximum pair sum is max(3+3, 5+2) = max(6, 7) = 7.\r\n
\r\n\r\nExample 2:
\r\n\r\n\r\nInput: nums = [3,5,4,2,4,6]\r\nOutput: 8\r\nExplanation: The elements can be paired up into pairs (3,5), (4,4), and (6,2).\r\nThe maximum pair sum is max(3+5, 4+4, 6+2) = max(8, 8, 8) = 8.\r\n
\r\n\r\n
\r\nConstraints:
\r\n\r\n\r\n\tn == nums.length
\r\n\t2 <= n <= 105
\r\n\tn
is even. \r\n\t1 <= nums[i] <= 105
\r\n
", "content_cn": "\u4e00\u4e2a\u6570\u5bf9\u00a0(a,b)
\u00a0\u7684 \u6570\u5bf9\u548c\u00a0\u7b49\u4e8e\u00a0a + b
\u00a0\u3002\u6700\u5927\u6570\u5bf9\u548c\u00a0\u662f\u4e00\u4e2a\u6570\u5bf9\u6570\u7ec4\u4e2d\u6700\u5927\u7684\u00a0\u6570\u5bf9\u548c\u00a0\u3002
\n\n\n\t- \u6bd4\u65b9\u8bf4\uff0c\u5982\u679c\u6211\u4eec\u6709\u6570\u5bf9\u00a0
(1,5)
\u00a0\uff0c(2,3)
\u00a0\u548c\u00a0(4,4)
\uff0c\u6700\u5927\u6570\u5bf9\u548c\u00a0\u4e3a\u00a0max(1+5, 2+3, 4+4) = max(6, 5, 8) = 8
\u00a0\u3002 \n
\n\n\u7ed9\u4f60\u4e00\u4e2a\u957f\u5ea6\u4e3a \u5076\u6570\u00a0n
\u00a0\u7684\u6570\u7ec4\u00a0nums
\u00a0\uff0c\u8bf7\u4f60\u5c06 nums
\u00a0\u4e2d\u7684\u5143\u7d20\u5206\u6210 n / 2
\u00a0\u4e2a\u6570\u5bf9\uff0c\u4f7f\u5f97\uff1a
\n\n\n\tnums
\u00a0\u4e2d\u6bcf\u4e2a\u5143\u7d20\u00a0\u6070\u597d\u00a0\u5728 \u4e00\u4e2a\u00a0\u6570\u5bf9\u4e2d\uff0c\u4e14 \n\t- \u6700\u5927\u6570\u5bf9\u548c\u00a0\u7684\u503c \u6700\u5c0f\u00a0\u3002
\n
\n\n\u8bf7\u4f60\u5728\u6700\u4f18\u6570\u5bf9\u5212\u5206\u7684\u65b9\u6848\u4e0b\uff0c\u8fd4\u56de\u6700\u5c0f\u7684 \u6700\u5927\u6570\u5bf9\u548c\u00a0\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1anums = [3,5,2,3]\n\u8f93\u51fa\uff1a7\n\u89e3\u91ca\uff1a\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u53ef\u4ee5\u5206\u4e3a\u6570\u5bf9 (3,3) \u548c (5,2) \u3002\n\u6700\u5927\u6570\u5bf9\u548c\u4e3a max(3+3, 5+2) = max(6, 7) = 7 \u3002\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1anums = [3,5,4,2,4,6]\n\u8f93\u51fa\uff1a8\n\u89e3\u91ca\uff1a\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u53ef\u4ee5\u5206\u4e3a\u6570\u5bf9 (3,5)\uff0c(4,4) \u548c (6,2) \u3002\n\u6700\u5927\u6570\u5bf9\u548c\u4e3a max(3+5, 4+4, 6+2) = max(8, 8, 8) = 8 \u3002\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\tn == nums.length
\n\t2 <= n <= 105
\n\tn
\u00a0\u662f \u5076\u6570\u00a0\u3002 \n\t1 <= nums[i] <= 105
\n
\n", "tags_en": ["Greedy", "Array", "Two Pointers", "Sorting"], "tags_cn": ["\u8d2a\u5fc3", "\u6570\u7ec4", "\u53cc\u6307\u9488", "\u6392\u5e8f"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n int minPairSum(vector& nums) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int minPairSum(int[] nums) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def minPairSum(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def minPairSum(self, nums: List[int]) -> int:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nint minPairSum(int* nums, int numsSize){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int MinPairSum(int[] nums) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[]} nums\n * @return {number}\n */\nvar minPairSum = function(nums) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer[]} nums\n# @return {Integer}\ndef min_pair_sum(nums)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func minPairSum(_ nums: [Int]) -> Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func minPairSum(nums []int) int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def minPairSum(nums: Array[Int]): Int = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun minPairSum(nums: IntArray): Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn min_pair_sum(nums: Vec) -> i32 {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @return Integer\n */\n function minPairSum($nums) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function minPairSum(nums: number[]): number {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (min-pair-sum nums)\n (-> (listof exact-integer?) exact-integer?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec min_pair_sum(Nums :: [integer()]) -> integer().\nmin_pair_sum(Nums) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec min_pair_sum(nums :: [integer]) :: integer\n def min_pair_sum(nums) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1877](https://leetcode-cn.com/problems/minimize-maximum-pair-sum-in-array)", "[\u6570\u7ec4\u4e2d\u6700\u5927\u6570\u5bf9\u548c\u7684\u6700\u5c0f\u503c](/solution/1800-1899/1877.Minimize%20Maximum%20Pair%20Sum%20in%20Array/README.md)", "`\u8d2a\u5fc3`,`\u6570\u7ec4`,`\u53cc\u6307\u9488`,`\u6392\u5e8f`", "\u4e2d\u7b49", ""], "md_table_row_en": ["[1877](https://leetcode.com/problems/minimize-maximum-pair-sum-in-array)", "[Minimize Maximum Pair Sum in Array](/solution/1800-1899/1877.Minimize%20Maximum%20Pair%20Sum%20in%20Array/README_EN.md)", "`Greedy`,`Array`,`Two Pointers`,`Sorting`", "Medium", ""]}, {"question_id": "1987", "frontend_question_id": "1876", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/substrings-of-size-three-with-distinct-characters", "url_en": "https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters", "relative_path_cn": "/solution/1800-1899/1876.Substrings%20of%20Size%20Three%20with%20Distinct%20Characters/README.md", "relative_path_en": "/solution/1800-1899/1876.Substrings%20of%20Size%20Three%20with%20Distinct%20Characters/README_EN.md", "title_cn": "\u957f\u5ea6\u4e3a\u4e09\u4e14\u5404\u5b57\u7b26\u4e0d\u540c\u7684\u5b50\u5b57\u7b26\u4e32", "title_en": "Substrings of Size Three with Distinct Characters", "question_title_slug": "substrings-of-size-three-with-distinct-characters", "content_en": "A string is good if there are no repeated characters.
\n\nGiven a string s
\u200b\u200b\u200b\u200b\u200b, return the number of good substrings of length three in s
\u200b\u200b\u200b\u200b\u200b\u200b.
\n\nNote that if there are multiple occurrences of the same substring, every occurrence should be counted.
\n\nA substring is a contiguous sequence of characters in a string.
\n\n
\nExample 1:
\n\n\nInput: s = "xyzzaz"\nOutput: 1\nExplanation: There are 4 substrings of size 3: "xyz", "yzz", "zza", and "zaz". \nThe only good substring of length 3 is "xyz".\n
\n\nExample 2:
\n\n\nInput: s = "aababcabc"\nOutput: 4\nExplanation: There are 7 substrings of size 3: "aab", "aba", "bab", "abc", "bca", "cab", and "abc".\nThe good substrings are "abc", "bca", "cab", and "abc".\n
\n\n
\nConstraints:
\n\n\n\t1 <= s.length <= 100
\n\ts
\u200b\u200b\u200b\u200b\u200b\u200b consists of lowercase English letters. \n
\n", "content_cn": "\u5982\u679c\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e0d\u542b\u6709\u4efb\u4f55\u91cd\u590d\u5b57\u7b26\uff0c\u6211\u4eec\u79f0\u8fd9\u4e2a\u5b57\u7b26\u4e32\u4e3a \u597d\u00a0\u5b57\u7b26\u4e32\u3002
\n\n\u7ed9\u4f60\u4e00\u4e2a\u5b57\u7b26\u4e32 s
\u00a0\uff0c\u8bf7\u4f60\u8fd4\u56de s
\u00a0\u4e2d\u957f\u5ea6\u4e3a 3\u00a0\u7684 \u597d\u5b50\u5b57\u7b26\u4e32 \u7684\u6570\u91cf\u3002
\n\n\u6ce8\u610f\uff0c\u5982\u679c\u76f8\u540c\u7684\u597d\u5b50\u5b57\u7b26\u4e32\u51fa\u73b0\u591a\u6b21\uff0c\u6bcf\u4e00\u6b21\u90fd\u5e94\u8be5\u88ab\u8bb0\u5165\u7b54\u6848\u4e4b\u4e2d\u3002
\n\n\u5b50\u5b57\u7b26\u4e32 \u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u8fde\u7eed\u7684\u5b57\u7b26\u5e8f\u5217\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1as = \"xyzzaz\"\n\u8f93\u51fa\uff1a1\n\u89e3\u91ca\uff1a\u603b\u5171\u6709 4 \u4e2a\u957f\u5ea6\u4e3a 3 \u7684\u5b50\u5b57\u7b26\u4e32\uff1a\"xyz\"\uff0c\"yzz\"\uff0c\"zza\" \u548c \"zaz\" \u3002\n\u552f\u4e00\u7684\u957f\u5ea6\u4e3a 3 \u7684\u597d\u5b50\u5b57\u7b26\u4e32\u662f \"xyz\" \u3002\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1as = \"aababcabc\"\n\u8f93\u51fa\uff1a4\n\u89e3\u91ca\uff1a\u603b\u5171\u6709 7 \u4e2a\u957f\u5ea6\u4e3a 3 \u7684\u5b50\u5b57\u7b26\u4e32\uff1a\"aab\"\uff0c\"aba\"\uff0c\"bab\"\uff0c\"abc\"\uff0c\"bca\"\uff0c\"cab\" \u548c \"abc\" \u3002\n\u597d\u5b50\u5b57\u7b26\u4e32\u5305\u62ec \"abc\"\uff0c\"bca\"\uff0c\"cab\" \u548c \"abc\" \u3002\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\t1 <= s.length <= 100
\n\ts
\u200b\u200b\u200b\u200b\u200b\u200b \u53ea\u5305\u542b\u5c0f\u5199\u82f1\u6587\u5b57\u6bcd\u3002 \n
\n", "tags_en": ["Hash Table", "String", "Counting", "Sliding Window"], "tags_cn": ["\u54c8\u5e0c\u8868", "\u5b57\u7b26\u4e32", "\u8ba1\u6570", "\u6ed1\u52a8\u7a97\u53e3"], "difficulty_en": "Easy", "difficulty_cn": "\u7b80\u5355", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n int countGoodSubstrings(string s) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int countGoodSubstrings(String s) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def countGoodSubstrings(self, s):\n \"\"\"\n :type s: str\n :rtype: int\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def countGoodSubstrings(self, s: str) -> int:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nint countGoodSubstrings(char * s){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int CountGoodSubstrings(string s) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {string} s\n * @return {number}\n */\nvar countGoodSubstrings = function(s) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {String} s\n# @return {Integer}\ndef count_good_substrings(s)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func countGoodSubstrings(_ s: String) -> Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func countGoodSubstrings(s string) int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def countGoodSubstrings(s: String): Int = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun countGoodSubstrings(s: String): Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn count_good_substrings(s: String) -> i32 {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param String $s\n * @return Integer\n */\n function countGoodSubstrings($s) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function countGoodSubstrings(s: string): number {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (count-good-substrings s)\n (-> string? exact-integer?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec count_good_substrings(S :: unicode:unicode_binary()) -> integer().\ncount_good_substrings(S) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec count_good_substrings(s :: String.t) :: integer\n def count_good_substrings(s) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1876](https://leetcode-cn.com/problems/substrings-of-size-three-with-distinct-characters)", "[\u957f\u5ea6\u4e3a\u4e09\u4e14\u5404\u5b57\u7b26\u4e0d\u540c\u7684\u5b50\u5b57\u7b26\u4e32](/solution/1800-1899/1876.Substrings%20of%20Size%20Three%20with%20Distinct%20Characters/README.md)", "`\u54c8\u5e0c\u8868`,`\u5b57\u7b26\u4e32`,`\u8ba1\u6570`,`\u6ed1\u52a8\u7a97\u53e3`", "\u7b80\u5355", ""], "md_table_row_en": ["[1876](https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters)", "[Substrings of Size Three with Distinct Characters](/solution/1800-1899/1876.Substrings%20of%20Size%20Three%20with%20Distinct%20Characters/README_EN.md)", "`Hash Table`,`String`,`Counting`,`Sliding Window`", "Easy", ""]}, {"question_id": "1986", "frontend_question_id": "1857", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/largest-color-value-in-a-directed-graph", "url_en": "https://leetcode.com/problems/largest-color-value-in-a-directed-graph", "relative_path_cn": "/solution/1800-1899/1857.Largest%20Color%20Value%20in%20a%20Directed%20Graph/README.md", "relative_path_en": "/solution/1800-1899/1857.Largest%20Color%20Value%20in%20a%20Directed%20Graph/README_EN.md", "title_cn": "\u6709\u5411\u56fe\u4e2d\u6700\u5927\u989c\u8272\u503c", "title_en": "Largest Color Value in a Directed Graph", "question_title_slug": "largest-color-value-in-a-directed-graph", "content_en": "There is a directed graph of n
colored nodes and m
edges. The nodes are numbered from 0
to n - 1
.
\r\n\r\nYou are given a string colors
where colors[i]
is a lowercase English letter representing the color of the ith
node in this graph (0-indexed). You are also given a 2D array edges
where edges[j] = [aj, bj]
indicates that there is a directed edge from node aj
to node bj
.
\r\n\r\nA valid path in the graph is a sequence of nodes x1 -> x2 -> x3 -> ... -> xk
such that there is a directed edge from xi
to xi+1
for every 1 <= i < k
. The color value of the path is the number of nodes that are colored the most frequently occurring color along that path.
\r\n\r\nReturn the largest color value of any valid path in the given graph, or -1
if the graph contains a cycle.
\r\n\r\n
\r\nExample 1:
\r\n\r\n
\r\n\r\n\r\nInput: colors = "abaca", edges = [[0,1],[0,2],[2,3],[3,4]]\r\nOutput: 3\r\nExplanation: The path 0 -> 2 -> 3 -> 4 contains 3 nodes that are colored "a" (red in the above image)
.\r\n
\r\n\r\nExample 2:
\r\n\r\n
\r\n\r\n\r\nInput: colors = "a", edges = [[0,0]]\r\nOutput: -1\r\nExplanation: There is a cycle from 0 to 0.\r\n
\r\n\r\n
\r\nConstraints:
\r\n\r\n\r\n\tn == colors.length
\r\n\tm == edges.length
\r\n\t1 <= n <= 105
\r\n\t0 <= m <= 105
\r\n\tcolors
consists of lowercase English letters. \r\n\t0 <= aj, bj < n
\r\n
", "content_cn": "\u7ed9\u4f60\u4e00\u4e2a\u00a0\u6709\u5411\u56fe\u00a0\uff0c\u5b83\u542b\u6709\u00a0n
\u00a0\u4e2a\u8282\u70b9\u548c m
\u00a0\u6761\u8fb9\u3002\u8282\u70b9\u7f16\u53f7\u4ece\u00a00
\u5230\u00a0n - 1
\u00a0\u3002
\n\n\u7ed9\u4f60\u4e00\u4e2a\u5b57\u7b26\u4e32\u00a0colors
\uff0c\u5176\u4e2d\u00a0colors[i]
\u00a0\u662f\u5c0f\u5199\u82f1\u6587\u5b57\u6bcd\uff0c\u8868\u793a\u56fe\u4e2d\u7b2c i
\u00a0\u4e2a\u8282\u70b9\u7684 \u989c\u8272\u00a0\uff08\u4e0b\u6807\u4ece 0\u00a0\u5f00\u59cb\uff09\u3002\u540c\u65f6\u7ed9\u4f60\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\u00a0edges
\u00a0\uff0c\u5176\u4e2d\u00a0edges[j] = [aj, bj]
\u00a0\u8868\u793a\u4ece\u8282\u70b9\u00a0aj
\u00a0\u5230\u8282\u70b9\u00a0bj
\u00a0\u6709\u4e00\u6761\u00a0\u6709\u5411\u8fb9\u00a0\u3002
\n\n\u56fe\u4e2d\u4e00\u6761\u6709\u6548 \u8def\u5f84\u00a0\u662f\u4e00\u4e2a\u70b9\u5e8f\u5217\u00a0x1 -> x2 -> x3 -> ... -> xk
\u00a0\uff0c\u5bf9\u4e8e\u6240\u6709\u00a01 <= i < k
\u00a0\uff0c\u4ece\u00a0xi
\u5230\u00a0xi+1
\u00a0\u5728\u56fe\u4e2d\u6709\u4e00\u6761\u6709\u5411\u8fb9\u3002\u8def\u5f84\u7684 \u989c\u8272\u503c\u00a0\u662f\u8def\u5f84\u4e2d \u51fa\u73b0\u6b21\u6570\u6700\u591a \u989c\u8272\u7684\u8282\u70b9\u6570\u76ee\u3002
\n\n\u8bf7\u4f60\u8fd4\u56de\u7ed9\u5b9a\u56fe\u4e2d\u6709\u6548\u8def\u5f84\u91cc\u9762\u7684\u00a0\u6700\u5927\u989c\u8272\u503c\u00a0\u3002\u5982\u679c\u56fe\u4e2d\u542b\u6709\u73af\uff0c\u8bf7\u8fd4\u56de -1
\u00a0\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n
\n\n\u8f93\u5165\uff1acolors = \"abaca\", edges = [[0,1],[0,2],[2,3],[3,4]]\n\u8f93\u51fa\uff1a3\n\u89e3\u91ca\uff1a\u8def\u5f84 0 -> 2 -> 3 -> 4 \u542b\u6709 3 \u4e2a\u989c\u8272\u4e3a \"a\" \u7684\u8282\u70b9\uff08\u4e0a\u56fe\u4e2d\u7684\u7ea2\u8272\u8282\u70b9\uff09\u3002
\n
\n\n\u793a\u4f8b 2\uff1a
\n\n
\n\n\u8f93\u5165\uff1acolors = \"a\", edges = [[0,0]]\n\u8f93\u51fa\uff1a-1\n\u89e3\u91ca\uff1a\u4ece 0 \u5230 0 \u6709\u4e00\u4e2a\u73af\u3002\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\tn == colors.length
\n\tm == edges.length
\n\t1 <= n <= 105
\n\t0 <= m <= 105
\n\tcolors
\u00a0\u53ea\u542b\u6709\u5c0f\u5199\u82f1\u6587\u5b57\u6bcd\u3002 \n\t0 <= aj, bj\u00a0< n
\n
\n", "tags_en": ["Graph", "Topological Sort", "Memoization", "Hash Table", "Dynamic Programming", "Counting"], "tags_cn": ["\u56fe", "\u62d3\u6251\u6392\u5e8f", "\u8bb0\u5fc6\u5316\u641c\u7d22", "\u54c8\u5e0c\u8868", "\u52a8\u6001\u89c4\u5212", "\u8ba1\u6570"], "difficulty_en": "Hard", "difficulty_cn": "\u56f0\u96be", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n int largestPathValue(string colors, vector>& edges) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int largestPathValue(String colors, int[][] edges) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def largestPathValue(self, colors, edges):\n \"\"\"\n :type colors: str\n :type edges: List[List[int]]\n :rtype: int\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def largestPathValue(self, colors: str, edges: List[List[int]]) -> int:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "int largestPathValue(char * colors, int** edges, int edgesSize, int* edgesColSize){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int LargestPathValue(string colors, int[][] edges) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {string} colors\n * @param {number[][]} edges\n * @return {number}\n */\nvar largestPathValue = function(colors, edges) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {String} colors\n# @param {Integer[][]} edges\n# @return {Integer}\ndef largest_path_value(colors, edges)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func largestPathValue(_ colors: String, _ edges: [[Int]]) -> Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func largestPathValue(colors string, edges [][]int) int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def largestPathValue(colors: String, edges: Array[Array[Int]]): Int = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun largestPathValue(colors: String, edges: Array): Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn largest_path_value(colors: String, edges: Vec>) -> i32 {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param String $colors\n * @param Integer[][] $edges\n * @return Integer\n */\n function largestPathValue($colors, $edges) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function largestPathValue(colors: string, edges: number[][]): number {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (largest-path-value colors edges)\n (-> string? (listof (listof exact-integer?)) exact-integer?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec largest_path_value(Colors :: unicode:unicode_binary(), Edges :: [[integer()]]) -> integer().\nlargest_path_value(Colors, Edges) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec largest_path_value(colors :: String.t, edges :: [[integer]]) :: integer\n def largest_path_value(colors, edges) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1857](https://leetcode-cn.com/problems/largest-color-value-in-a-directed-graph)", "[\u6709\u5411\u56fe\u4e2d\u6700\u5927\u989c\u8272\u503c](/solution/1800-1899/1857.Largest%20Color%20Value%20in%20a%20Directed%20Graph/README.md)", "`\u56fe`,`\u62d3\u6251\u6392\u5e8f`,`\u8bb0\u5fc6\u5316\u641c\u7d22`,`\u54c8\u5e0c\u8868`,`\u52a8\u6001\u89c4\u5212`,`\u8ba1\u6570`", "\u56f0\u96be", ""], "md_table_row_en": ["[1857](https://leetcode.com/problems/largest-color-value-in-a-directed-graph)", "[Largest Color Value in a Directed Graph](/solution/1800-1899/1857.Largest%20Color%20Value%20in%20a%20Directed%20Graph/README_EN.md)", "`Graph`,`Topological Sort`,`Memoization`,`Hash Table`,`Dynamic Programming`,`Counting`", "Hard", ""]}, {"question_id": "1985", "frontend_question_id": "1856", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/maximum-subarray-min-product", "url_en": "https://leetcode.com/problems/maximum-subarray-min-product", "relative_path_cn": "/solution/1800-1899/1856.Maximum%20Subarray%20Min-Product/README.md", "relative_path_en": "/solution/1800-1899/1856.Maximum%20Subarray%20Min-Product/README_EN.md", "title_cn": "\u5b50\u6570\u7ec4\u6700\u5c0f\u4e58\u79ef\u7684\u6700\u5927\u503c", "title_en": "Maximum Subarray Min-Product", "question_title_slug": "maximum-subarray-min-product", "content_en": "The min-product of an array is equal to the minimum value in the array multiplied by the array's sum.
\n\n\n\t- For example, the array
[3,2,5]
(minimum value is 2
) has a min-product of 2 * (3+2+5) = 2 * 10 = 20
. \n
\n\nGiven an array of integers nums
, return the maximum min-product of any non-empty subarray of nums
. Since the answer may be large, return it modulo 109 + 7
.
\n\nNote that the min-product should be maximized before performing the modulo operation. Testcases are generated such that the maximum min-product without modulo will fit in a 64-bit signed integer.
\n\nA subarray is a contiguous part of an array.
\n\n
\nExample 1:
\n\n\nInput: nums = [1,2,3,2]\nOutput: 14\nExplanation: The maximum min-product is achieved with the subarray [2,3,2] (minimum value is 2).\n2 * (2+3+2) = 2 * 7 = 14.\n
\n\nExample 2:
\n\n\nInput: nums = [2,3,3,1,2]\nOutput: 18\nExplanation: The maximum min-product is achieved with the subarray [3,3] (minimum value is 3).\n3 * (3+3) = 3 * 6 = 18.\n
\n\nExample 3:
\n\n\nInput: nums = [3,1,5,6,4,2]\nOutput: 60\nExplanation: The maximum min-product is achieved with the subarray [5,6,4] (minimum value is 4).\n4 * (5+6+4) = 4 * 15 = 60.\n
\n\n
\nConstraints:
\n\n\n\t1 <= nums.length <= 105
\n\t1 <= nums[i] <= 107
\n
\n", "content_cn": "\u4e00\u4e2a\u6570\u7ec4\u7684 \u6700\u5c0f\u4e58\u79ef\u00a0\u5b9a\u4e49\u4e3a\u8fd9\u4e2a\u6570\u7ec4\u4e2d \u6700\u5c0f\u503c\u00a0\u4e58\u4ee5\u00a0\u6570\u7ec4\u7684 \u548c\u00a0\u3002
\n\n\n\t- \u6bd4\u65b9\u8bf4\uff0c\u6570\u7ec4\u00a0
[3,2,5]
\u00a0\uff08\u6700\u5c0f\u503c\u662f\u00a02
\uff09\u7684\u6700\u5c0f\u4e58\u79ef\u4e3a\u00a02 * (3+2+5) = 2 * 10 = 20
\u00a0\u3002 \n
\n\n\u7ed9\u4f60\u4e00\u4e2a\u6b63\u6574\u6570\u6570\u7ec4\u00a0nums
\u00a0\uff0c\u8bf7\u4f60\u8fd4\u56de\u00a0nums
\u00a0\u4efb\u610f\u00a0\u975e\u7a7a\u5b50\u6570\u7ec4\u00a0\u7684\u6700\u5c0f\u4e58\u79ef\u00a0\u7684\u00a0\u6700\u5927\u503c\u00a0\u3002\u7531\u4e8e\u7b54\u6848\u53ef\u80fd\u5f88\u5927\uff0c\u8bf7\u4f60\u8fd4\u56de\u7b54\u6848\u5bf9\u00a0\u00a0109 + 7
\u00a0\u53d6\u4f59\u00a0\u7684\u7ed3\u679c\u3002
\n\n\u8bf7\u6ce8\u610f\uff0c\u6700\u5c0f\u4e58\u79ef\u7684\u6700\u5927\u503c\u8003\u8651\u7684\u662f\u53d6\u4f59\u64cd\u4f5c \u4e4b\u524d\u00a0\u7684\u7ed3\u679c\u3002\u9898\u76ee\u4fdd\u8bc1\u6700\u5c0f\u4e58\u79ef\u7684\u6700\u5927\u503c\u5728 \u4e0d\u53d6\u4f59 \u7684\u60c5\u51b5\u4e0b\u53ef\u4ee5\u7528 64 \u4f4d\u6709\u7b26\u53f7\u6574\u6570\u00a0\u4fdd\u5b58\u3002
\n\n\u5b50\u6570\u7ec4\u00a0\u5b9a\u4e49\u4e3a\u4e00\u4e2a\u6570\u7ec4\u7684 \u8fde\u7eed\u00a0\u90e8\u5206\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1anums = [1,2,3,2]\n\u8f93\u51fa\uff1a14\n\u89e3\u91ca\uff1a\u6700\u5c0f\u4e58\u79ef\u7684\u6700\u5927\u503c\u7531\u5b50\u6570\u7ec4 [2,3,2] \uff08\u6700\u5c0f\u503c\u662f 2\uff09\u5f97\u5230\u3002\n2 * (2+3+2) = 2 * 7 = 14 \u3002\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1anums = [2,3,3,1,2]\n\u8f93\u51fa\uff1a18\n\u89e3\u91ca\uff1a\u6700\u5c0f\u4e58\u79ef\u7684\u6700\u5927\u503c\u7531\u5b50\u6570\u7ec4 [3,3] \uff08\u6700\u5c0f\u503c\u662f 3\uff09\u5f97\u5230\u3002\n3 * (3+3) = 3 * 6 = 18 \u3002\n
\n\n\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1anums = [3,1,5,6,4,2]\n\u8f93\u51fa\uff1a60\n\u89e3\u91ca\uff1a\u6700\u5c0f\u4e58\u79ef\u7684\u6700\u5927\u503c\u7531\u5b50\u6570\u7ec4 [5,6,4] \uff08\u6700\u5c0f\u503c\u662f 4\uff09\u5f97\u5230\u3002\n4 * (5+6+4) = 4 * 15 = 60 \u3002\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\t1 <= nums.length <= 105
\n\t1 <= nums[i] <= 107
\n
\n", "tags_en": ["Stack", "Array", "Prefix Sum", "Monotonic Stack"], "tags_cn": ["\u6808", "\u6570\u7ec4", "\u524d\u7f00\u548c", "\u5355\u8c03\u6808"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n int maxSumMinProduct(vector& nums) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int maxSumMinProduct(int[] nums) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def maxSumMinProduct(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def maxSumMinProduct(self, nums: List[int]) -> int:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nint maxSumMinProduct(int* nums, int numsSize){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int MaxSumMinProduct(int[] nums) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[]} nums\n * @return {number}\n */\nvar maxSumMinProduct = function(nums) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer[]} nums\n# @return {Integer}\ndef max_sum_min_product(nums)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func maxSumMinProduct(_ nums: [Int]) -> Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func maxSumMinProduct(nums []int) int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def maxSumMinProduct(nums: Array[Int]): Int = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun maxSumMinProduct(nums: IntArray): Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn max_sum_min_product(nums: Vec) -> i32 {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @return Integer\n */\n function maxSumMinProduct($nums) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function maxSumMinProduct(nums: number[]): number {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (max-sum-min-product nums)\n (-> (listof exact-integer?) exact-integer?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec max_sum_min_product(Nums :: [integer()]) -> integer().\nmax_sum_min_product(Nums) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec max_sum_min_product(nums :: [integer]) :: integer\n def max_sum_min_product(nums) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1856](https://leetcode-cn.com/problems/maximum-subarray-min-product)", "[\u5b50\u6570\u7ec4\u6700\u5c0f\u4e58\u79ef\u7684\u6700\u5927\u503c](/solution/1800-1899/1856.Maximum%20Subarray%20Min-Product/README.md)", "`\u6808`,`\u6570\u7ec4`,`\u524d\u7f00\u548c`,`\u5355\u8c03\u6808`", "\u4e2d\u7b49", ""], "md_table_row_en": ["[1856](https://leetcode.com/problems/maximum-subarray-min-product)", "[Maximum Subarray Min-Product](/solution/1800-1899/1856.Maximum%20Subarray%20Min-Product/README_EN.md)", "`Stack`,`Array`,`Prefix Sum`,`Monotonic Stack`", "Medium", ""]}, {"question_id": "1984", "frontend_question_id": "1855", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/maximum-distance-between-a-pair-of-values", "url_en": "https://leetcode.com/problems/maximum-distance-between-a-pair-of-values", "relative_path_cn": "/solution/1800-1899/1855.Maximum%20Distance%20Between%20a%20Pair%20of%20Values/README.md", "relative_path_en": "/solution/1800-1899/1855.Maximum%20Distance%20Between%20a%20Pair%20of%20Values/README_EN.md", "title_cn": "\u4e0b\u6807\u5bf9\u4e2d\u7684\u6700\u5927\u8ddd\u79bb", "title_en": "Maximum Distance Between a Pair of Values", "question_title_slug": "maximum-distance-between-a-pair-of-values", "content_en": "You are given two non-increasing 0-indexed integer arrays nums1
\u200b\u200b\u200b\u200b\u200b\u200b and nums2
\u200b\u200b\u200b\u200b\u200b\u200b.
\n\nA pair of indices (i, j)
, where 0 <= i < nums1.length
and 0 <= j < nums2.length
, is valid if both i <= j
and nums1[i] <= nums2[j]
. The distance of the pair is j - i
\u200b\u200b\u200b\u200b.
\n\nReturn the maximum distance of any valid pair (i, j)
. If there are no valid pairs, return 0
.
\n\nAn array arr
is non-increasing if arr[i-1] >= arr[i]
for every 1 <= i < arr.length
.
\n\n
\nExample 1:
\n\n\nInput: nums1 = [55,30,5,4,2], nums2 = [100,20,10,10,5]\nOutput: 2\nExplanation: The valid pairs are (0,0), (2,2), (2,3), (2,4), (3,3), (3,4), and (4,4).\nThe maximum distance is 2 with pair (2,4).\n
\n\nExample 2:
\n\n\nInput: nums1 = [2,2,2], nums2 = [10,10,1]\nOutput: 1\nExplanation: The valid pairs are (0,0), (0,1), and (1,1).\nThe maximum distance is 1 with pair (0,1).\n
\n\nExample 3:
\n\n\nInput: nums1 = [30,29,19,5], nums2 = [25,25,25,25,25]\nOutput: 2\nExplanation: The valid pairs are (2,2), (2,3), (2,4), (3,3), and (3,4).\nThe maximum distance is 2 with pair (2,4).\n
\n\nExample 4:
\n\n\nInput: nums1 = [5,4], nums2 = [3,2]\nOutput: 0\nExplanation: There are no valid pairs, so return 0.\n
\n\n
\nConstraints:
\n\n\n\t1 <= nums1.length <= 105
\n\t1 <= nums2.length <= 105
\n\t1 <= nums1[i], nums2[j] <= 105
\n\t- Both
nums1
and nums2
are non-increasing. \n
\n", "content_cn": "\u7ed9\u4f60\u4e24\u4e2a \u975e\u9012\u589e \u7684\u6574\u6570\u6570\u7ec4 nums1
\u200b\u200b\u200b\u200b\u200b\u200b \u548c nums2
\u200b\u200b\u200b\u200b\u200b\u200b \uff0c\u6570\u7ec4\u4e0b\u6807\u5747 \u4ece 0 \u5f00\u59cb \u8ba1\u6570\u3002
\n\n\u4e0b\u6807\u5bf9 (i, j)
\u4e2d 0 <= i < nums1.length
\u4e14 0 <= j < nums2.length
\u3002\u5982\u679c\u8be5\u4e0b\u6807\u5bf9\u540c\u65f6\u6ee1\u8db3 i <= j
\u4e14 nums1[i] <= nums2[j]
\uff0c\u5219\u79f0\u4e4b\u4e3a \u6709\u6548 \u4e0b\u6807\u5bf9\uff0c\u8be5\u4e0b\u6807\u5bf9\u7684 \u8ddd\u79bb \u4e3a j - i
\u200b\u200b \u3002\u200b\u200b
\n\n\u8fd4\u56de\u6240\u6709 \u6709\u6548 \u4e0b\u6807\u5bf9 (i, j)
\u4e2d\u7684 \u6700\u5927\u8ddd\u79bb \u3002\u5982\u679c\u4e0d\u5b58\u5728\u6709\u6548\u4e0b\u6807\u5bf9\uff0c\u8fd4\u56de 0
\u3002
\n\n\u4e00\u4e2a\u6570\u7ec4 arr
\uff0c\u5982\u679c\u6bcf\u4e2a 1 <= i < arr.length
\u5747\u6709 arr[i-1] >= arr[i]
\u6210\u7acb\uff0c\u90a3\u4e48\u8be5\u6570\u7ec4\u662f\u4e00\u4e2a \u975e\u9012\u589e \u6570\u7ec4\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1anums1 = [55,30,5,4,2], nums2 = [100,20,10,10,5]\n\u8f93\u51fa\uff1a2\n\u89e3\u91ca\uff1a\u6709\u6548\u4e0b\u6807\u5bf9\u662f (0,0), (2,2), (2,3), (2,4), (3,3), (3,4) \u548c (4,4) \u3002\n\u6700\u5927\u8ddd\u79bb\u662f 2 \uff0c\u5bf9\u5e94\u4e0b\u6807\u5bf9 (2,4) \u3002\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1anums1 = [2,2,2], nums2 = [10,10,1]\n\u8f93\u51fa\uff1a1\n\u89e3\u91ca\uff1a\u6709\u6548\u4e0b\u6807\u5bf9\u662f (0,0), (0,1) \u548c (1,1) \u3002\n\u6700\u5927\u8ddd\u79bb\u662f 1 \uff0c\u5bf9\u5e94\u4e0b\u6807\u5bf9 (0,1) \u3002
\n\n\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1anums1 = [30,29,19,5], nums2 = [25,25,25,25,25]\n\u8f93\u51fa\uff1a2\n\u89e3\u91ca\uff1a\u6709\u6548\u4e0b\u6807\u5bf9\u662f (2,2), (2,3), (2,4), (3,3) \u548c (3,4) \u3002\n\u6700\u5927\u8ddd\u79bb\u662f 2 \uff0c\u5bf9\u5e94\u4e0b\u6807\u5bf9 (2,4) \u3002\n
\n\n\u793a\u4f8b 4\uff1a
\n\n\n\u8f93\u5165\uff1anums1 = [5,4], nums2 = [3,2]\n\u8f93\u51fa\uff1a0\n\u89e3\u91ca\uff1a\u4e0d\u5b58\u5728\u6709\u6548\u4e0b\u6807\u5bf9\uff0c\u6240\u4ee5\u8fd4\u56de 0 \u3002\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\t1 <= nums1.length <= 105
\n\t1 <= nums2.length <= 105
\n\t1 <= nums1[i], nums2[j] <= 105
\n\tnums1
\u548c nums2
\u90fd\u662f \u975e\u9012\u589e \u6570\u7ec4 \n
\n", "tags_en": ["Greedy", "Array", "Two Pointers", "Binary Search"], "tags_cn": ["\u8d2a\u5fc3", "\u6570\u7ec4", "\u53cc\u6307\u9488", "\u4e8c\u5206\u67e5\u627e"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n int maxDistance(vector& nums1, vector& nums2) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int maxDistance(int[] nums1, int[] nums2) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def maxDistance(self, nums1, nums2):\n \"\"\"\n :type nums1: List[int]\n :type nums2: List[int]\n :rtype: int\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def maxDistance(self, nums1: List[int], nums2: List[int]) -> int:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nint maxDistance(int* nums1, int nums1Size, int* nums2, int nums2Size){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int MaxDistance(int[] nums1, int[] nums2) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[]} nums1\n * @param {number[]} nums2\n * @return {number}\n */\nvar maxDistance = function(nums1, nums2) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer[]} nums1\n# @param {Integer[]} nums2\n# @return {Integer}\ndef max_distance(nums1, nums2)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func maxDistance(_ nums1: [Int], _ nums2: [Int]) -> Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func maxDistance(nums1 []int, nums2 []int) int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def maxDistance(nums1: Array[Int], nums2: Array[Int]): Int = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun maxDistance(nums1: IntArray, nums2: IntArray): Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn max_distance(nums1: Vec, nums2: Vec) -> i32 {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer[] $nums1\n * @param Integer[] $nums2\n * @return Integer\n */\n function maxDistance($nums1, $nums2) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function maxDistance(nums1: number[], nums2: number[]): number {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (max-distance nums1 nums2)\n (-> (listof exact-integer?) (listof exact-integer?) exact-integer?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec max_distance(Nums1 :: [integer()], Nums2 :: [integer()]) -> integer().\nmax_distance(Nums1, Nums2) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec max_distance(nums1 :: [integer], nums2 :: [integer]) :: integer\n def max_distance(nums1, nums2) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1855](https://leetcode-cn.com/problems/maximum-distance-between-a-pair-of-values)", "[\u4e0b\u6807\u5bf9\u4e2d\u7684\u6700\u5927\u8ddd\u79bb](/solution/1800-1899/1855.Maximum%20Distance%20Between%20a%20Pair%20of%20Values/README.md)", "`\u8d2a\u5fc3`,`\u6570\u7ec4`,`\u53cc\u6307\u9488`,`\u4e8c\u5206\u67e5\u627e`", "\u4e2d\u7b49", ""], "md_table_row_en": ["[1855](https://leetcode.com/problems/maximum-distance-between-a-pair-of-values)", "[Maximum Distance Between a Pair of Values](/solution/1800-1899/1855.Maximum%20Distance%20Between%20a%20Pair%20of%20Values/README_EN.md)", "`Greedy`,`Array`,`Two Pointers`,`Binary Search`", "Medium", ""]}, {"question_id": "1983", "frontend_question_id": "1854", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/maximum-population-year", "url_en": "https://leetcode.com/problems/maximum-population-year", "relative_path_cn": "/solution/1800-1899/1854.Maximum%20Population%20Year/README.md", "relative_path_en": "/solution/1800-1899/1854.Maximum%20Population%20Year/README_EN.md", "title_cn": "\u4eba\u53e3\u6700\u591a\u7684\u5e74\u4efd", "title_en": "Maximum Population Year", "question_title_slug": "maximum-population-year", "content_en": "You are given a 2D integer array logs
where each logs[i] = [birthi, deathi]
indicates the birth and death years of the ith
person.
\n\nThe population of some year x
is the number of people alive during that year. The ith
person is counted in year x
's population if x
is in the inclusive range [birthi, deathi - 1]
. Note that the person is not counted in the year that they die.
\n\nReturn the earliest year with the maximum population.
\n\n
\nExample 1:
\n\n\nInput: logs = [[1993,1999],[2000,2010]]\nOutput: 1993\nExplanation: The maximum population is 1, and 1993 is the earliest year with this population.\n
\n\nExample 2:
\n\n\nInput: logs = [[1950,1961],[1960,1971],[1970,1981]]\nOutput: 1960\nExplanation: \nThe maximum population is 2, and it had happened in years 1960 and 1970.\nThe earlier year between them is 1960.
\n\n
\nConstraints:
\n\n\n\t1 <= logs.length <= 100
\n\t1950 <= birthi < deathi <= 2050
\n
\n", "content_cn": "\u7ed9\u4f60\u4e00\u4e2a\u4e8c\u7ef4\u6574\u6570\u6570\u7ec4 logs
\uff0c\u5176\u4e2d\u6bcf\u4e2a logs[i] = [birthi, deathi]
\u8868\u793a\u7b2c i
\u4e2a\u4eba\u7684\u51fa\u751f\u548c\u6b7b\u4ea1\u5e74\u4efd\u3002
\n\n\u5e74\u4efd x
\u7684 \u4eba\u53e3 \u5b9a\u4e49\u4e3a\u8fd9\u4e00\u5e74\u671f\u95f4\u6d3b\u7740\u7684\u4eba\u7684\u6570\u76ee\u3002\u7b2c i
\u4e2a\u4eba\u88ab\u8ba1\u5165\u5e74\u4efd x
\u7684\u4eba\u53e3\u9700\u8981\u6ee1\u8db3\uff1ax
\u5728\u95ed\u533a\u95f4 [birthi, deathi - 1]
\u5185\u3002\u6ce8\u610f\uff0c\u4eba\u4e0d\u5e94\u5f53\u8ba1\u5165\u4ed6\u4eec\u6b7b\u4ea1\u5f53\u5e74\u7684\u4eba\u53e3\u4e2d\u3002
\n\n\u8fd4\u56de \u4eba\u53e3\u6700\u591a \u4e14 \u6700\u65e9 \u7684\u5e74\u4efd\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1alogs = [[1993,1999],[2000,2010]]\n\u8f93\u51fa\uff1a1993\n\u89e3\u91ca\uff1a\u4eba\u53e3\u6700\u591a\u4e3a 1 \uff0c\u800c 1993 \u662f\u4eba\u53e3\u4e3a 1 \u7684\u6700\u65e9\u5e74\u4efd\u3002\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1alogs = [[1950,1961],[1960,1971],[1970,1981]]\n\u8f93\u51fa\uff1a1960\n\u89e3\u91ca\uff1a \n\u4eba\u53e3\u6700\u591a\u4e3a 2 \uff0c\u5206\u522b\u51fa\u73b0\u5728 1960 \u548c 1970 \u3002\n\u5176\u4e2d\u6700\u65e9\u5e74\u4efd\u662f 1960 \u3002
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\t1 <= logs.length <= 100
\n\t1950 <= birthi < deathi <= 2050
\n
\n", "tags_en": ["Array", "Counting"], "tags_cn": ["\u6570\u7ec4", "\u8ba1\u6570"], "difficulty_en": "Easy", "difficulty_cn": "\u7b80\u5355", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n int maximumPopulation(vector>& logs) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int maximumPopulation(int[][] logs) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def maximumPopulation(self, logs):\n \"\"\"\n :type logs: List[List[int]]\n :rtype: int\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def maximumPopulation(self, logs: List[List[int]]) -> int:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nint maximumPopulation(int** logs, int logsSize, int* logsColSize){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int MaximumPopulation(int[][] logs) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[][]} logs\n * @return {number}\n */\nvar maximumPopulation = function(logs) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer[][]} logs\n# @return {Integer}\ndef maximum_population(logs)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func maximumPopulation(_ logs: [[Int]]) -> Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func maximumPopulation(logs [][]int) int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def maximumPopulation(logs: Array[Array[Int]]): Int = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun maximumPopulation(logs: Array): Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn maximum_population(logs: Vec>) -> i32 {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer[][] $logs\n * @return Integer\n */\n function maximumPopulation($logs) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function maximumPopulation(logs: number[][]): number {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (maximum-population logs)\n (-> (listof (listof exact-integer?)) exact-integer?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec maximum_population(Logs :: [[integer()]]) -> integer().\nmaximum_population(Logs) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec maximum_population(logs :: [[integer]]) :: integer\n def maximum_population(logs) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1854](https://leetcode-cn.com/problems/maximum-population-year)", "[\u4eba\u53e3\u6700\u591a\u7684\u5e74\u4efd](/solution/1800-1899/1854.Maximum%20Population%20Year/README.md)", "`\u6570\u7ec4`,`\u8ba1\u6570`", "\u7b80\u5355", ""], "md_table_row_en": ["[1854](https://leetcode.com/problems/maximum-population-year)", "[Maximum Population Year](/solution/1800-1899/1854.Maximum%20Population%20Year/README_EN.md)", "`Array`,`Counting`", "Easy", ""]}, {"question_id": "1982", "frontend_question_id": "1836", "paid_only": true, "paid_only_cn": true, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/remove-duplicates-from-an-unsorted-linked-list", "url_en": "https://leetcode.com/problems/remove-duplicates-from-an-unsorted-linked-list", "relative_path_cn": "/solution/1800-1899/1836.Remove%20Duplicates%20From%20an%20Unsorted%20Linked%20List/README.md", "relative_path_en": "/solution/1800-1899/1836.Remove%20Duplicates%20From%20an%20Unsorted%20Linked%20List/README_EN.md", "title_cn": "\u4ece\u672a\u6392\u5e8f\u7684\u94fe\u8868\u4e2d\u79fb\u9664\u91cd\u590d\u5143\u7d20", "title_en": "Remove Duplicates From an Unsorted Linked List", "question_title_slug": "remove-duplicates-from-an-unsorted-linked-list", "content_en": "Given the head
of a linked list, find all the values that appear more than once in the list and delete the nodes that have any of those values.
\r\n\r\nReturn the linked list after the deletions.
\r\n\r\n
\r\nExample 1:
\r\n
\r\n\r\nInput: head = [1,2,3,2]\r\nOutput: [1,3]\r\nExplanation: 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].\r\n
\r\n\r\nExample 2:
\r\n
\r\n\r\nInput: head = [2,1,1,2]\r\nOutput: []\r\nExplanation: 2 and 1 both appear twice. All the elements should be deleted.\r\n
\r\n\r\nExample 3:
\r\n
\r\n\r\nInput: head = [3,2,2,1,3,2,4]\r\nOutput: [1,4]\r\nExplanation: 3 appears twice and 2 appears three times. After deleting all 3's and 2's, we are left with [1,4].\r\n
\r\n\r\n
\r\nConstraints:
\r\n\r\n\r\n\t- The number of nodes in the list is in the range
[1, 105]
\r\n\t1 <= Node.val <= 105
\r\n
", "content_cn": "\u7ed9\u5b9a\u4e00\u4e2a\u94fe\u8868\u7684\u7b2c\u4e00\u4e2a\u8282\u70b9\u00a0head
\u00a0\uff0c\u627e\u5230\u94fe\u8868\u4e2d\u6240\u6709\u51fa\u73b0\u591a\u4e8e\u4e00\u6b21\u7684\u5143\u7d20\uff0c\u5e76\u5220\u9664\u8fd9\u4e9b\u5143\u7d20\u6240\u5728\u7684\u8282\u70b9\u3002
\n\n\u8fd4\u56de\u5220\u9664\u540e\u7684\u94fe\u8868\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1:
\n
\n\u8f93\u5165: head = [1,2,3,2]\n\u8f93\u51fa: [1,3]\n\u89e3\u91ca: 2 \u5728\u94fe\u8868\u4e2d\u51fa\u73b0\u4e86\u4e24\u6b21\uff0c\u6240\u4ee5\u6240\u6709\u7684 2 \u90fd\u9700\u8981\u88ab\u5220\u9664\u3002\u5220\u9664\u4e86\u6240\u6709\u7684 2 \u4e4b\u540e\uff0c\u6211\u4eec\u8fd8\u5269\u4e0b [1,3] \u3002\n
\n\n\u793a\u4f8b 2:
\n
\n\u8f93\u5165: head = [2,1,1,2]\n\u8f93\u51fa: []\n\u89e3\u91ca: 2 \u548c 1 \u90fd\u51fa\u73b0\u4e86\u4e24\u6b21\u3002\u6240\u6709\u5143\u7d20\u90fd\u9700\u8981\u88ab\u5220\u9664\u3002\n
\n\n\u793a\u4f8b 3:
\n
\n\u8f93\u5165: head = [3,2,2,1,3,2,4]\n\u8f93\u51fa: [1,4]\n\u89e3\u91ca: 3 \u51fa\u73b0\u4e86\u4e24\u6b21\uff0c\u4e14 2 \u51fa\u73b0\u4e86\u4e09\u6b21\u3002\u79fb\u9664\u4e86\u6240\u6709\u7684 3 \u548c 2 \u540e\uff0c\u6211\u4eec\u8fd8\u5269\u4e0b [1,4] \u3002\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\t- \u94fe\u8868\u4e2d\u8282\u70b9\u4e2a\u6570\u7684\u8303\u56f4\u662f\u00a0
[1, 105]
\n\t1 <= Node.val <= 105
\n
\n", "tags_en": ["Hash Table", "Linked List"], "tags_cn": ["\u54c8\u5e0c\u8868", "\u94fe\u8868"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* deleteDuplicatesUnsorted(ListNode* head) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "/**\n * Definition for singly-linked list.\n * public class ListNode {\n * int val;\n * ListNode next;\n * ListNode() {}\n * ListNode(int val) { this.val = val; }\n * ListNode(int val, ListNode next) { this.val = val; this.next = next; }\n * }\n */\nclass Solution {\n public ListNode deleteDuplicatesUnsorted(ListNode head) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "# Definition for singly-linked list.\n# class ListNode(object):\n# def __init__(self, val=0, next=None):\n# self.val = val\n# self.next = next\nclass Solution(object):\n def deleteDuplicatesUnsorted(self, head):\n \"\"\"\n :type head: ListNode\n :rtype: ListNode\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, val=0, next=None):\n# self.val = val\n# self.next = next\nclass Solution:\n def deleteDuplicatesUnsorted(self, head: ListNode) -> ListNode:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * struct ListNode *next;\n * };\n */\n\n\nstruct ListNode* deleteDuplicatesUnsorted(struct ListNode* head){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "/**\n * Definition for singly-linked list.\n * public class ListNode {\n * public int val;\n * public ListNode next;\n * public ListNode(int val=0, ListNode next=null) {\n * this.val = val;\n * this.next = next;\n * }\n * }\n */\npublic class Solution {\n public ListNode DeleteDuplicatesUnsorted(ListNode head) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * Definition for singly-linked list.\n * function ListNode(val, next) {\n * this.val = (val===undefined ? 0 : val)\n * this.next = (next===undefined ? null : next)\n * }\n */\n/**\n * @param {ListNode} head\n * @return {ListNode}\n */\nvar deleteDuplicatesUnsorted = function(head) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# Definition for singly-linked list.\n# class ListNode\n# attr_accessor :val, :next\n# def initialize(val = 0, _next = nil)\n# @val = val\n# @next = _next\n# end\n# end\n# @param {ListNode} head\n# @return {ListNode}\ndef delete_duplicates_unsorted(head)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "/**\n * Definition for singly-linked list.\n * public class ListNode {\n * public var val: Int\n * public var next: ListNode?\n * public init() { self.val = 0; self.next = nil; }\n * public init(_ val: Int) { self.val = val; self.next = nil; }\n * public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }\n * }\n */\nclass Solution {\n func deleteDuplicatesUnsorted(_ head: ListNode?) -> ListNode? {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "/**\n * Definition for singly-linked list.\n * type ListNode struct {\n * Val int\n * Next *ListNode\n * }\n */\nfunc deleteDuplicatesUnsorted(head *ListNode) *ListNode {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "/**\n * Definition for singly-linked list.\n * class ListNode(_x: Int = 0, _next: ListNode = null) {\n * var next: ListNode = _next\n * var x: Int = _x\n * }\n */\nobject Solution {\n def deleteDuplicatesUnsorted(head: ListNode): ListNode = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "/**\n * Example:\n * var li = ListNode(5)\n * var v = li.`val`\n * Definition for singly-linked list.\n * class ListNode(var `val`: Int) {\n * var next: ListNode? = null\n * }\n */\nclass Solution {\n fun deleteDuplicatesUnsorted(head: ListNode?): ListNode? {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "// Definition for singly-linked list.\n// #[derive(PartialEq, Eq, Clone, Debug)]\n// pub struct ListNode {\n// pub val: i32,\n// pub next: Option>\n// }\n//\n// impl ListNode {\n// #[inline]\n// fn new(val: i32) -> Self {\n// ListNode {\n// next: None,\n// val\n// }\n// }\n// }\nimpl Solution {\n pub fn delete_duplicates_unsorted(head: Option>) -> Option> {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "/**\n * Definition for a singly-linked list.\n * class ListNode {\n * public $val = 0;\n * public $next = null;\n * function __construct($val = 0, $next = null) {\n * $this->val = $val;\n * $this->next = $next;\n * }\n * }\n */\nclass Solution {\n\n /**\n * @param ListNode $head\n * @return ListNode\n */\n function deleteDuplicatesUnsorted($head) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "/**\n * Definition for singly-linked list.\n * class ListNode {\n * val: number\n * next: ListNode | null\n * constructor(val?: number, next?: ListNode | null) {\n * this.val = (val===undefined ? 0 : val)\n * this.next = (next===undefined ? null : next)\n * }\n * }\n */\n\nfunction deleteDuplicatesUnsorted(head: ListNode | null): ListNode | null {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "; Definition for singly-linked list:\n#|\n\n; val : integer?\n; next : (or/c list-node? #f)\n(struct list-node\n (val next) #:mutable #:transparent)\n\n; constructor\n(define (make-list-node [val 0])\n (list-node val #f))\n\n|#\n\n(define/contract (delete-duplicates-unsorted head)\n (-> (or/c list-node? #f) (or/c list-node? #f))\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "%% Definition for singly-linked list.\n%%\n%% -record(list_node, {val = 0 :: integer(),\n%% next = null :: 'null' | #list_node{}}).\n\n-spec delete_duplicates_unsorted(Head :: #list_node{} | null) -> #list_node{} | null.\ndelete_duplicates_unsorted(Head) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "# Definition for singly-linked list.\n#\n# defmodule ListNode do\n# @type t :: %__MODULE__{\n# val: integer,\n# next: ListNode.t() | nil\n# }\n# defstruct val: 0, next: nil\n# end\n\ndefmodule Solution do\n @spec delete_duplicates_unsorted(head :: ListNode.t | nil) :: ListNode.t | nil\n def delete_duplicates_unsorted(head) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1836](https://leetcode-cn.com/problems/remove-duplicates-from-an-unsorted-linked-list)", "[\u4ece\u672a\u6392\u5e8f\u7684\u94fe\u8868\u4e2d\u79fb\u9664\u91cd\u590d\u5143\u7d20](/solution/1800-1899/1836.Remove%20Duplicates%20From%20an%20Unsorted%20Linked%20List/README.md)", "`\u54c8\u5e0c\u8868`,`\u94fe\u8868`", "\u4e2d\u7b49", "\ud83d\udd12"], "md_table_row_en": ["[1836](https://leetcode.com/problems/remove-duplicates-from-an-unsorted-linked-list)", "[Remove Duplicates From an Unsorted Linked List](/solution/1800-1899/1836.Remove%20Duplicates%20From%20an%20Unsorted%20Linked%20List/README_EN.md)", "`Hash Table`,`Linked List`", "Medium", "\ud83d\udd12"]}, {"question_id": "1981", "frontend_question_id": "1831", "paid_only": true, "paid_only_cn": true, "category": "Database", "url_cn": "https://leetcode-cn.com/problems/maximum-transaction-each-day", "url_en": "https://leetcode.com/problems/maximum-transaction-each-day", "relative_path_cn": "/solution/1800-1899/1831.Maximum%20Transaction%20Each%20Day/README.md", "relative_path_en": "/solution/1800-1899/1831.Maximum%20Transaction%20Each%20Day/README_EN.md", "title_cn": "\u6bcf\u5929\u7684\u6700\u5927\u4ea4\u6613", "title_en": "Maximum Transaction Each Day", "question_title_slug": "maximum-transaction-each-day", "content_en": "Table: Transactions
\r\n\r\n\r\n+----------------+----------+\r\n| Column Name | Type |\r\n+----------------+----------+\r\n| transaction_id | int |\r\n| day | datetime |\r\n| amount | int |\r\n+----------------+----------+\r\ntransaction_id is the primary key for this table.\r\nEach row contains information about one transaction.\r\n
\r\n\r\n
\r\n\r\nWrite an SQL query to report the IDs of the transactions with the maximum amount
on their respective day. If in one day there are multiple such transactions, return all of them.
\r\n\r\nReturn the result table in ascending order by transaction_id
.
\r\n\r\nThe query result format is in the following example:
\r\n\r\n
\r\n\r\n\r\nTransactions table:\r\n+----------------+--------------------+--------+\r\n| transaction_id | day | amount |\r\n+----------------+--------------------+--------+\r\n| 8 | 2021-4-3 15:57:28 | 57 |\r\n| 9 | 2021-4-28 08:47:25 | 21 |\r\n| 1 | 2021-4-29 13:28:30 | 58 |\r\n| 5 | 2021-4-28 16:39:59 | 40 |\r\n| 6 | 2021-4-29 23:39:28 | 58 |\r\n+----------------+--------------------+--------+\r\n\r\nResult table:\r\n+----------------+\r\n| transaction_id |\r\n+----------------+\r\n| 1 |\r\n| 5 |\r\n| 6 |\r\n| 8 |\r\n+----------------+\r\n"2021-4-3" --> We have one transaction with ID 8, so we add 8 to the result table.\r\n"2021-4-28" --> We have two transactions with IDs 5 and 9. The transaction with ID 5 has an amount of 40, while the transaction with ID 9 has an amount of 21. We only include the transaction with ID 5 as it has the maximum amount this day.\r\n"2021-4-29" --> We have two transactions with IDs 1 and 6. Both transactions have the same amount of 58, so we include both in the result table.\r\nWe order the result table by transaction_id after collecting these IDs.
\r\n\r\n
\r\nFollow up: Could you solve it without using the MAX()
function?
", "content_cn": "\u8868: Transactions
\n\n\n+----------------+----------+\n| Column Name | Type |\n+----------------+----------+\n| transaction_id | int |\n| day | datetime |\n| amount | int |\n+----------------+----------+\ntransaction_id \u662f\u6b64\u8868\u7684\u4e3b\u952e\u3002\n\u6bcf\u884c\u5305\u62ec\u4e86\u8be5\u6b21\u4ea4\u6613\u7684\u4fe1\u606f\u3002\n
\n\n\u00a0
\n\n\u5199\u4e00\u6761 SQL\u00a0\u8fd4\u56de\u6bcf\u5929\u4ea4\u6613\u91d1\u989d amount
\u6700\u5927\u7684\u4ea4\u6613 ID \u3002\u5982\u679c\u67d0\u5929\u6709\u591a\u4e2a\u8fd9\u6837\u7684\u4ea4\u6613\uff0c\u8fd4\u56de\u8fd9\u4e9b\u4ea4\u6613\u7684 ID \u3002
\n\n\u8fd4\u56de\u7ed3\u679c\u6839\u636e transaction_id
\u00a0\u5347\u5e8f\u6392\u5217\u3002
\n\n\u67e5\u8be2\u7ed3\u679c\u6837\u4f8b\u5982\u4e0b\uff1a
\n\n\u00a0
\n\n\nTransactions table:\n+----------------+--------------------+--------+\n| transaction_id | day | amount |\n+----------------+--------------------+--------+\n| 8 | 2021-4-3 15:57:28 | 57 |\n| 9 | 2021-4-28 08:47:25 | 21 |\n| 1 | 2021-4-29 13:28:30 | 58 |\n| 5 | 2021-4-28 16:39:59 | 40 |\n| 6 | 2021-4-29 23:39:28 | 58 |\n+----------------+--------------------+--------+\n\nResult table:\n+----------------+\n| transaction_id |\n+----------------+\n| 1 |\n| 5 |\n| 6 |\n| 8 |\n+----------------+\n\"2021-4-3\" --> \u6709\u4e00\u4e2a id \u662f 8 \u7684\u4ea4\u6613\uff0c\u56e0\u6b64\uff0c\u628a\u5b83\u52a0\u5165\u7ed3\u679c\u8868\u3002 \n\"2021-4-28\" --> \u6709\u4e24\u4e2a\u4ea4\u6613\uff0cid \u662f 5 \u548c 9 \uff0c\u4ea4\u6613 5 \u7684\u91d1\u989d\u662f 40 \uff0c\u800c\u4ea4\u6613 9 \u7684\u6570\u91cf\u662f 21 \u3002\u53ea\u9700\u8981\u5c06\u4ea4\u6613 5 \u52a0\u5165\u7ed3\u679c\u8868\uff0c\u56e0\u4e3a\u5b83\u662f\u5f53\u5929\u91d1\u989d\u6700\u5927\u7684\u4ea4\u6613\u3002\n\"2021-4-29\" --> \u6709\u4e24\u4e2a\u4ea4\u6613\uff0cid \u662f 1 \u548c 6 \uff0c\u8fd9\u4e24\u4e2a\u4ea4\u6613\u7684\u91d1\u989d\u90fd\u662f 58 \uff0c\u56e0\u6b64\u9700\u8981\u628a\u5b83\u4eec\u90fd\u5199\u5165\u7ed3\u679c\u8868\u3002\n\u6700\u540e\uff0c\u628a\u4ea4\u6613 id \u6309\u7167\u5347\u5e8f\u6392\u5217\u3002
\n\n\u00a0
\n\n\u8fdb\u9636\uff1a\u4f60\u53ef\u4ee5\u4e0d\u4f7f\u7528\u00a0MAX()
\u00a0\u51fd\u6570\u89e3\u51b3\u8fd9\u9053\u9898\u76ee\u5417?
\n", "tags_en": ["Database"], "tags_cn": ["\u6570\u636e\u5e93"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "MySQL", "langSlug": "mysql", "code": "# Write your MySQL query statement below", "__typename": "CodeSnippetNode"}, {"lang": "MS SQL Server", "langSlug": "mssql", "code": "/* Write your T-SQL query statement below */", "__typename": "CodeSnippetNode"}, {"lang": "Oracle", "langSlug": "oraclesql", "code": "/* Write your PL/SQL query statement below */", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1831](https://leetcode-cn.com/problems/maximum-transaction-each-day)", "[\u6bcf\u5929\u7684\u6700\u5927\u4ea4\u6613](/solution/1800-1899/1831.Maximum%20Transaction%20Each%20Day/README.md)", "`\u6570\u636e\u5e93`", "\u4e2d\u7b49", "\ud83d\udd12"], "md_table_row_en": ["[1831](https://leetcode.com/problems/maximum-transaction-each-day)", "[Maximum Transaction Each Day](/solution/1800-1899/1831.Maximum%20Transaction%20Each%20Day/README_EN.md)", "`Database`", "Medium", "\ud83d\udd12"]}, {"question_id": "1980", "frontend_question_id": "1826", "paid_only": true, "paid_only_cn": true, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/faulty-sensor", "url_en": "https://leetcode.com/problems/faulty-sensor", "relative_path_cn": "/solution/1800-1899/1826.Faulty%20Sensor/README.md", "relative_path_en": "/solution/1800-1899/1826.Faulty%20Sensor/README_EN.md", "title_cn": "\u6709\u7f3a\u9677\u7684\u4f20\u611f\u5668", "title_en": "Faulty Sensor", "question_title_slug": "faulty-sensor", "content_en": "An experiment is being conducted in a lab. To ensure accuracy, there are two sensors collecting data simultaneously. You are given two arrays sensor1
and sensor2
, where sensor1[i]
and sensor2[i]
are the ith
data points collected by the two sensors.
\n\nHowever, this type of sensor has a chance of being defective, which causes exactly one data point to be dropped. After the data is dropped, all the data points to the right of the dropped data are shifted one place to the left, and the last data point is replaced with some random value. It is guaranteed that this random value will not be equal to the dropped value.
\n\n\n\t- For example, if the correct data is
[1,2,3,4,5]
and 3
is dropped, the sensor could return [1,2,4,5,7]
(the last position can be any value, not just 7
). \n
\n\nWe know that there is a defect in at most one of the sensors. Return the sensor number (1
or 2
) with the defect. If there is no defect in either sensor or if it is impossible to determine the defective sensor, return -1
.
\n\n
\nExample 1:
\n\n\nInput: sensor1 = [2,3,4,5], sensor2 = [2,1,3,4]\nOutput: 1\nExplanation: Sensor 2 has the correct values.\nThe second data point from sensor 2 is dropped, and the last value of sensor 1 is replaced by a 5.\n
\n\nExample 2:
\n\n\nInput: sensor1 = [2,2,2,2,2], sensor2 = [2,2,2,2,5]\nOutput: -1\nExplanation: It is impossible to determine which sensor has a defect.\nDropping the last value for either sensor could produce the output for the other sensor.\n
\n\nExample 3:
\n\n\nInput: sensor1 = [2,3,2,2,3,2], sensor2 = [2,3,2,3,2,7]\nOutput: 2\nExplanation: Sensor 1 has the correct values.\nThe fourth data point from sensor 1 is dropped, and the last value of sensor 1 is replaced by a 7.\n
\n\n
\nConstraints:
\n\n\n\tsensor1.length == sensor2.length
\n\t1 <= sensor1.length <= 100
\n\t1 <= sensor1[i], sensor2[i] <= 100
\n
\n", "content_cn": "\u5b9e\u9a8c\u5ba4\u91cc\u6b63\u5728\u8fdb\u884c\u4e00\u9879\u5b9e\u9a8c\u3002\u4e3a\u4e86\u786e\u4fdd\u6570\u636e\u7684\u51c6\u786e\u6027\uff0c\u540c\u65f6\u4f7f\u7528 \u4e24\u4e2a \u4f20\u611f\u5668\u6765\u91c7\u96c6\u6570\u636e\u3002\u60a8\u5c06\u83b7\u5f972\u4e2a\u6570\u7ec4 sensor1
and sensor2
\uff0c\u5176\u4e2d sensor1[i]
\u00a0\u548c\u00a0sensor2[i]
\u00a0\u5206\u522b\u662f\u4e24\u4e2a\u4f20\u611f\u5668\u5bf9\u7b2c i
\u4e2a\u6570\u636e\u70b9\u91c7\u96c6\u5230\u7684\u6570\u636e\u3002
\n\n\u4f46\u662f\uff0c\u8fd9\u79cd\u7c7b\u578b\u7684\u4f20\u611f\u5668\u6709\u53ef\u80fd\u5b58\u5728\u7f3a\u9677\uff0c\u5b83\u4f1a\u5bfc\u81f4 \u67d0\u4e00\u4e2a \u6570\u636e\u70b9\u91c7\u96c6\u7684\u6570\u636e\uff08\u6389\u843d\u503c\uff09\u88ab\u4e22\u5f03\u3002
\n\n\u6570\u636e\u88ab\u4e22\u5f03\u540e\uff0c\u6240\u6709\u5728\u5176\u53f3\u4fa7\u7684\u6570\u636e\u70b9\u91c7\u96c6\u7684\u6570\u636e\uff0c\u90fd\u4f1a\u88ab\u5411\u5de6\u79fb\u52a8\u4e00\u4e2a\u4f4d\u7f6e\uff0c\u6700\u540e\u4e00\u4e2a\u6570\u636e\u70b9\u91c7\u96c6\u7684\u6570\u636e\u4f1a\u88ab\u4e00\u4e9b\u968f\u673a\u503c\u66ff\u6362\u3002\u53ef\u4ee5\u4fdd\u8bc1\u6b64\u968f\u673a\u503c\u4e0d\u7b49\u4e8e\u6389\u843d\u503c\u3002
\n\n\n\t- \u4e3e\u4e2a\u4f8b\u5b50, \u5982\u679c\u6b63\u786e\u7684\u6570\u636e\u662f\u00a0
[1,2,3,4,5]
\u00a0\uff0c\u00a0\u6b64\u65f6 3
\u88ab\u4e22\u5f03\u4e86, \u4f20\u611f\u5668\u4f1a\u8fd4\u56de\u00a0[1,2,4,5,7]
(\u6700\u540e\u7684\u4f4d\u7f6e\u53ef\u4ee5\u662f\u4efb\u4f55\u503c, \u4e0d\u4ec5\u4ec5\u662f\u00a07
). \n
\n\n\u53ef\u4ee5\u786e\u5b9a\u7684\u662f\uff0c\u6700\u591a\u6709\u4e00\u4e2a \u4f20\u611f\u5668\u6709\u7f3a\u9677\u3002\u8bf7\u8fd4\u56de\u8fd9\u4e2a\u6709\u7f3a\u9677\u7684\u4f20\u611f\u5668\u7684\u7f16\u53f7 \uff081
\u6216 2
\uff09\u3002\u5982\u679c\u4efb\u4e00\u4f20\u611f\u5668 \u6ca1\u6709\u7f3a\u9677 \uff0c\u6216\u8005 \u65e0\u6cd5 \u786e\u5b9a\u6709\u7f3a\u9677\u7684\u4f20\u611f\u5668\uff0c\u5219\u8fd4\u56de -1
\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1asensor1 = [2,3,4,5], sensor2 = [2,1,3,4]\n\u8f93\u51fa\uff1a1\n\u89e3\u91ca\uff1a\u4f20\u611f\u5668 2 \u8fd4\u56de\u4e86\u6240\u6709\u6b63\u786e\u7684\u6570\u636e.\n\u4f20\u611f\u56682\u5bf9\u7b2c\u4e8c\u4e2a\u6570\u636e\u70b9\u91c7\u96c6\u7684\u6570\u636e\uff0c\u88ab\u4f20\u611f\u56681\u4e22\u5f03\u4e86\uff0c\u4f20\u611f\u56681\u8fd4\u56de\u7684\u6700\u540e\u4e00\u4e2a\u6570\u636e\u88ab\u66ff\u6362\u4e3a 5 \u3002
\n\n\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1asensor1 = [2,2,2,2,2], sensor2 = [2,2,2,2,5]\n\u8f93\u51fa\uff1a-1\n\u89e3\u91ca\uff1a\u65e0\u6cd5\u5224\u5b9a\u62ff\u4e2a\u4f20\u611f\u5668\u662f\u6709\u7f3a\u9677\u7684\u3002\n\u5047\u8bbe\u4efb\u4e00\u4f20\u611f\u5668\u4e22\u5f03\u7684\u6570\u636e\u662f\u6700\u540e\u4e00\u4f4d\uff0c\u90a3\u4e48\uff0c\u53e6\u4e00\u4e2a\u4f20\u611f\u5668\u5c31\u80fd\u7ed9\u51fa\u4e0e\u4e4b\u5bf9\u5e94\u7684\u8f93\u51fa\u3002\n
\n\n\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1asensor1 = [2,3,2,2,3,2], sensor2 = [2,3,2,3,2,7]\n\u8f93\u51fa\uff1a2\n\u89e3\u91ca\uff1a\u4f20\u611f\u5668 1 \u8fd4\u56de\u4e86\u6240\u6709\u6b63\u786e\u7684\u6570\u636e.\n\u4f20\u611f\u5668 1 \u5bf9\u7b2c\u56db\u4e2a\u6570\u636e\u70b9\u7684\u91c7\u96c6\u6570\u636e\uff0c\u88ab\u4f20\u611f\u56682\u4e22\u5931\u4e86, \u4f20\u611f\u5668 2 \u8fd4\u56de\u7684\u6700\u540e\u4e00\u4e2a\u6570\u636e\u88ab\u66ff\u6362\u4e3a 7 \u3002\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\tsensor1.length == sensor2.length
\n\t1 <= sensor1.length <= 100
\n\t1 <= sensor1[i], sensor2[i] <= 100
\n
\n", "tags_en": ["Array", "Two Pointers"], "tags_cn": ["\u6570\u7ec4", "\u53cc\u6307\u9488"], "difficulty_en": "Easy", "difficulty_cn": "\u7b80\u5355", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n int badSensor(vector& sensor1, vector& sensor2) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int badSensor(int[] sensor1, int[] sensor2) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def badSensor(self, sensor1, sensor2):\n \"\"\"\n :type sensor1: List[int]\n :type sensor2: List[int]\n :rtype: int\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def badSensor(self, sensor1: List[int], sensor2: List[int]) -> int:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nint badSensor(int* sensor1, int sensor1Size, int* sensor2, int sensor2Size){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int BadSensor(int[] sensor1, int[] sensor2) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[]} sensor1\n * @param {number[]} sensor2\n * @return {number}\n */\nvar badSensor = function(sensor1, sensor2) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer[]} sensor1\n# @param {Integer[]} sensor2\n# @return {Integer}\ndef bad_sensor(sensor1, sensor2)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func badSensor(_ sensor1: [Int], _ sensor2: [Int]) -> Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func badSensor(sensor1 []int, sensor2 []int) int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def badSensor(sensor1: Array[Int], sensor2: Array[Int]): Int = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun badSensor(sensor1: IntArray, sensor2: IntArray): Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn bad_sensor(sensor1: Vec, sensor2: Vec) -> i32 {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer[] $sensor1\n * @param Integer[] $sensor2\n * @return Integer\n */\n function badSensor($sensor1, $sensor2) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function badSensor(sensor1: number[], sensor2: number[]): number {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (bad-sensor sensor1 sensor2)\n (-> (listof exact-integer?) (listof exact-integer?) exact-integer?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec bad_sensor(Sensor1 :: [integer()], Sensor2 :: [integer()]) -> integer().\nbad_sensor(Sensor1, Sensor2) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec bad_sensor(sensor1 :: [integer], sensor2 :: [integer]) :: integer\n def bad_sensor(sensor1, sensor2) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1826](https://leetcode-cn.com/problems/faulty-sensor)", "[\u6709\u7f3a\u9677\u7684\u4f20\u611f\u5668](/solution/1800-1899/1826.Faulty%20Sensor/README.md)", "`\u6570\u7ec4`,`\u53cc\u6307\u9488`", "\u7b80\u5355", "\ud83d\udd12"], "md_table_row_en": ["[1826](https://leetcode.com/problems/faulty-sensor)", "[Faulty Sensor](/solution/1800-1899/1826.Faulty%20Sensor/README_EN.md)", "`Array`,`Two Pointers`", "Easy", "\ud83d\udd12"]}, {"question_id": "1978", "frontend_question_id": "1850", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/minimum-adjacent-swaps-to-reach-the-kth-smallest-number", "url_en": "https://leetcode.com/problems/minimum-adjacent-swaps-to-reach-the-kth-smallest-number", "relative_path_cn": "/solution/1800-1899/1850.Minimum%20Adjacent%20Swaps%20to%20Reach%20the%20Kth%20Smallest%20Number/README.md", "relative_path_en": "/solution/1800-1899/1850.Minimum%20Adjacent%20Swaps%20to%20Reach%20the%20Kth%20Smallest%20Number/README_EN.md", "title_cn": "\u90bb\u4f4d\u4ea4\u6362\u7684\u6700\u5c0f\u6b21\u6570", "title_en": "Minimum Adjacent Swaps to Reach the Kth Smallest Number", "question_title_slug": "minimum-adjacent-swaps-to-reach-the-kth-smallest-number", "content_en": "You are given a string num
, representing a large integer, and an integer k
.
\n\nWe call some integer wonderful if it is a permutation of the digits in num
and is greater in value than num
. There can be many wonderful integers. However, we only care about the smallest-valued ones.
\n\n\n\t- For example, when
num = "5489355142"
:\n\n\t\n\t\t- The 1st smallest wonderful integer is
"5489355214"
. \n\t\t- The 2nd smallest wonderful integer is
"5489355241"
. \n\t\t- The 3rd smallest wonderful integer is
"5489355412"
. \n\t\t- The 4th smallest wonderful integer is
"5489355421"
. \n\t
\n\t \n
\n\nReturn the minimum number of adjacent digit swaps that needs to be applied to num
to reach the kth
smallest wonderful integer.
\n\nThe tests are generated in such a way that kth
smallest wonderful integer exists.
\n\n
\nExample 1:
\n\n\nInput: num = "5489355142", k = 4\nOutput: 2\nExplanation: The 4th smallest wonderful number is "5489355421". To get this number:\n- Swap index 7 with index 8: "5489355142" -> "5489355412"\n- Swap index 8 with index 9: "5489355412" -> "5489355421"\n
\n\nExample 2:
\n\n\nInput: num = "11112", k = 4\nOutput: 4\nExplanation: The 4th smallest wonderful number is "21111". To get this number:\n- Swap index 3 with index 4: "11112" -> "11121"\n- Swap index 2 with index 3: "11121" -> "11211"\n- Swap index 1 with index 2: "11211" -> "12111"\n- Swap index 0 with index 1: "12111" -> "21111"\n
\n\nExample 3:
\n\n\nInput: num = "00123", k = 1\nOutput: 1\nExplanation: The 1st smallest wonderful number is "00132". To get this number:\n- Swap index 3 with index 4: "00123" -> "00132"\n
\n\n
\nConstraints:
\n\n\n\t2 <= num.length <= 1000
\n\t1 <= k <= 1000
\n\tnum
only consists of digits. \n
\n", "content_cn": "\u7ed9\u4f60\u4e00\u4e2a\u8868\u793a\u5927\u6574\u6570\u7684\u5b57\u7b26\u4e32 num
\uff0c\u548c\u4e00\u4e2a\u6574\u6570 k
\u3002
\n\n\u5982\u679c\u67d0\u4e2a\u6574\u6570\u662f num
\u4e2d\u5404\u4f4d\u6570\u5b57\u7684\u4e00\u4e2a \u6392\u5217 \u4e14\u5b83\u7684 \u503c\u5927\u4e8e num
\uff0c\u5219\u79f0\u8fd9\u4e2a\u6574\u6570\u4e3a \u5999\u6570 \u3002\u53ef\u80fd\u5b58\u5728\u5f88\u591a\u5999\u6570\uff0c\u4f46\u662f\u53ea\u9700\u8981\u5173\u6ce8 \u503c\u6700\u5c0f \u7684\u90a3\u4e9b\u3002
\n\n\n\t- \u4f8b\u5982\uff0c
num = \"5489355142\"
\uff1a\n\n\t\n\t\t- \u7b2c 1 \u4e2a\u6700\u5c0f\u5999\u6570\u662f
\"5489355214\"
\n\t\t- \u7b2c 2 \u4e2a\u6700\u5c0f\u5999\u6570\u662f
\"5489355241\"
\n\t\t- \u7b2c 3 \u4e2a\u6700\u5c0f\u5999\u6570\u662f
\"5489355412\"
\n\t\t- \u7b2c 4 \u4e2a\u6700\u5c0f\u5999\u6570\u662f
\"5489355421\"
\n\t
\n\t \n
\n\n\u8fd4\u56de\u8981\u5f97\u5230\u7b2c k
\u4e2a \u6700\u5c0f\u5999\u6570 \u9700\u8981\u5bf9 num
\u6267\u884c\u7684 \u76f8\u90bb\u4f4d\u6570\u5b57\u4ea4\u6362\u7684\u6700\u5c0f\u6b21\u6570 \u3002
\n\n\u6d4b\u8bd5\u7528\u4f8b\u662f\u6309\u5b58\u5728\u7b2c k
\u4e2a\u6700\u5c0f\u5999\u6570\u800c\u751f\u6210\u7684\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1anum = \"5489355142\", k = 4\n\u8f93\u51fa\uff1a2\n\u89e3\u91ca\uff1a\u7b2c 4 \u4e2a\u6700\u5c0f\u5999\u6570\u662f \"5489355421\" \uff0c\u8981\u60f3\u5f97\u5230\u8fd9\u4e2a\u6570\u5b57\uff1a\n- \u4ea4\u6362\u4e0b\u6807 7 \u548c\u4e0b\u6807 8 \u5bf9\u5e94\u7684\u4f4d\uff1a\"5489355142\" -> \"5489355412\"\n- \u4ea4\u6362\u4e0b\u6807 8 \u548c\u4e0b\u6807 9 \u5bf9\u5e94\u7684\u4f4d\uff1a\"5489355412\" -> \"5489355421\"\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1anum = \"11112\", k = 4\n\u8f93\u51fa\uff1a4\n\u89e3\u91ca\uff1a\u7b2c 4 \u4e2a\u6700\u5c0f\u5999\u6570\u662f \"21111\" \uff0c\u8981\u60f3\u5f97\u5230\u8fd9\u4e2a\u6570\u5b57\uff1a\n- \u4ea4\u6362\u4e0b\u6807 3 \u548c\u4e0b\u6807 4 \u5bf9\u5e94\u7684\u4f4d\uff1a\"11112\" -> \"11121\"\n- \u4ea4\u6362\u4e0b\u6807 2 \u548c\u4e0b\u6807 3 \u5bf9\u5e94\u7684\u4f4d\uff1a\"11121\" -> \"11211\"\n- \u4ea4\u6362\u4e0b\u6807 1 \u548c\u4e0b\u6807 2 \u5bf9\u5e94\u7684\u4f4d\uff1a\"11211\" -> \"12111\"\n- \u4ea4\u6362\u4e0b\u6807 0 \u548c\u4e0b\u6807 1 \u5bf9\u5e94\u7684\u4f4d\uff1a\"12111\" -> \"21111\"\n
\n\n\u793a\u4f8b 3\uff1a
\n\n\u8f93\u5165\uff1anum = \"00123\", k = 1\n\u8f93\u51fa\uff1a1\n\u89e3\u91ca\uff1a\u7b2c 1 \u4e2a\u6700\u5c0f\u5999\u6570\u662f \"00132\" \uff0c\u8981\u60f3\u5f97\u5230\u8fd9\u4e2a\u6570\u5b57\uff1a\n- \u4ea4\u6362\u4e0b\u6807 3 \u548c\u4e0b\u6807 4 \u5bf9\u5e94\u7684\u4f4d\uff1a\"00123\" -> \"00132\"\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\t2 <= num.length <= 1000
\n\t1 <= k <= 1000
\n\tnum
\u4ec5\u7531\u6570\u5b57\u7ec4\u6210 \n
\n", "tags_en": ["Greedy", "Two Pointers", "String"], "tags_cn": ["\u8d2a\u5fc3", "\u53cc\u6307\u9488", "\u5b57\u7b26\u4e32"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n int getMinSwaps(string num, int k) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int getMinSwaps(String num, int k) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def getMinSwaps(self, num, k):\n \"\"\"\n :type num: str\n :type k: int\n :rtype: int\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def getMinSwaps(self, num: str, k: int) -> int:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nint getMinSwaps(char * num, int k){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int GetMinSwaps(string num, int k) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {string} num\n * @param {number} k\n * @return {number}\n */\nvar getMinSwaps = function(num, k) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {String} num\n# @param {Integer} k\n# @return {Integer}\ndef get_min_swaps(num, k)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func getMinSwaps(_ num: String, _ k: Int) -> Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func getMinSwaps(num string, k int) int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def getMinSwaps(num: String, k: Int): Int = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun getMinSwaps(num: String, k: Int): Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn get_min_swaps(num: String, k: i32) -> i32 {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param String $num\n * @param Integer $k\n * @return Integer\n */\n function getMinSwaps($num, $k) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function getMinSwaps(num: string, k: number): number {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (get-min-swaps num k)\n (-> string? exact-integer? exact-integer?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec get_min_swaps(Num :: unicode:unicode_binary(), K :: integer()) -> integer().\nget_min_swaps(Num, K) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec get_min_swaps(num :: String.t, k :: integer) :: integer\n def get_min_swaps(num, k) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1850](https://leetcode-cn.com/problems/minimum-adjacent-swaps-to-reach-the-kth-smallest-number)", "[\u90bb\u4f4d\u4ea4\u6362\u7684\u6700\u5c0f\u6b21\u6570](/solution/1800-1899/1850.Minimum%20Adjacent%20Swaps%20to%20Reach%20the%20Kth%20Smallest%20Number/README.md)", "`\u8d2a\u5fc3`,`\u53cc\u6307\u9488`,`\u5b57\u7b26\u4e32`", "\u4e2d\u7b49", ""], "md_table_row_en": ["[1850](https://leetcode.com/problems/minimum-adjacent-swaps-to-reach-the-kth-smallest-number)", "[Minimum Adjacent Swaps to Reach the Kth Smallest Number](/solution/1800-1899/1850.Minimum%20Adjacent%20Swaps%20to%20Reach%20the%20Kth%20Smallest%20Number/README_EN.md)", "`Greedy`,`Two Pointers`,`String`", "Medium", ""]}, {"question_id": "1977", "frontend_question_id": "1851", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/minimum-interval-to-include-each-query", "url_en": "https://leetcode.com/problems/minimum-interval-to-include-each-query", "relative_path_cn": "/solution/1800-1899/1851.Minimum%20Interval%20to%20Include%20Each%20Query/README.md", "relative_path_en": "/solution/1800-1899/1851.Minimum%20Interval%20to%20Include%20Each%20Query/README_EN.md", "title_cn": "\u5305\u542b\u6bcf\u4e2a\u67e5\u8be2\u7684\u6700\u5c0f\u533a\u95f4", "title_en": "Minimum Interval to Include Each Query", "question_title_slug": "minimum-interval-to-include-each-query", "content_en": "You are given a 2D integer array intervals
, where intervals[i] = [lefti, righti]
describes the ith
interval starting at lefti
and ending at righti
(inclusive). The size of an interval is defined as the number of integers it contains, or more formally righti - lefti + 1
.
\n\nYou are also given an integer array queries
. The answer to the jth
query is the size of the smallest interval i
such that lefti <= queries[j] <= righti
. If no such interval exists, the answer is -1
.
\n\nReturn an array containing the answers to the queries.
\n\n
\nExample 1:
\n\n\nInput: intervals = [[1,4],[2,4],[3,6],[4,4]], queries = [2,3,4,5]\nOutput: [3,3,1,4]\nExplanation: The queries are processed as follows:\n- Query = 2: The interval [2,4] is the smallest interval containing 2. The answer is 4 - 2 + 1 = 3.\n- Query = 3: The interval [2,4] is the smallest interval containing 3. The answer is 4 - 2 + 1 = 3.\n- Query = 4: The interval [4,4] is the smallest interval containing 4. The answer is 4 - 4 + 1 = 1.\n- Query = 5: The interval [3,6] is the smallest interval containing 5. The answer is 6 - 3 + 1 = 4.\n
\n\nExample 2:
\n\n\nInput: intervals = [[2,3],[2,5],[1,8],[20,25]], queries = [2,19,5,22]\nOutput: [2,-1,4,6]\nExplanation: The queries are processed as follows:\n- Query = 2: The interval [2,3] is the smallest interval containing 2. The answer is 3 - 2 + 1 = 2.\n- Query = 19: None of the intervals contain 19. The answer is -1.\n- Query = 5: The interval [2,5] is the smallest interval containing 5. The answer is 5 - 2 + 1 = 4.\n- Query = 22: The interval [20,25] is the smallest interval containing 22. The answer is 25 - 20 + 1 = 6.\n
\n\n
\nConstraints:
\n\n\n\t1 <= intervals.length <= 105
\n\t1 <= queries.length <= 105
\n\tintervals[i].length == 2
\n\t1 <= lefti <= righti <= 107
\n\t1 <= queries[j] <= 107
\n
\n", "content_cn": "\u7ed9\u4f60\u4e00\u4e2a\u4e8c\u7ef4\u6574\u6570\u6570\u7ec4 intervals
\uff0c\u5176\u4e2d intervals[i] = [lefti, righti]
\u8868\u793a\u7b2c i
\u4e2a\u533a\u95f4\u5f00\u59cb\u4e8e lefti
\u3001\u7ed3\u675f\u4e8e righti
\uff08\u5305\u542b\u4e24\u4fa7\u53d6\u503c\uff0c\u95ed\u533a\u95f4\uff09\u3002\u533a\u95f4\u7684 \u957f\u5ea6 \u5b9a\u4e49\u4e3a\u533a\u95f4\u4e2d\u5305\u542b\u7684\u6574\u6570\u6570\u76ee\uff0c\u66f4\u6b63\u5f0f\u5730\u8868\u8fbe\u662f righti - lefti + 1
\u3002
\n\n\u518d\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 queries
\u3002\u7b2c j
\u4e2a\u67e5\u8be2\u7684\u7b54\u6848\u662f\u6ee1\u8db3\u00a0lefti <= queries[j] <= righti
\u7684 \u957f\u5ea6\u6700\u5c0f\u533a\u95f4 i
\u7684\u957f\u5ea6 \u3002\u5982\u679c\u4e0d\u5b58\u5728\u8fd9\u6837\u7684\u533a\u95f4\uff0c\u90a3\u4e48\u7b54\u6848\u662f -1
\u3002
\n\n\u4ee5\u6570\u7ec4\u5f62\u5f0f\u8fd4\u56de\u5bf9\u5e94\u67e5\u8be2\u7684\u6240\u6709\u7b54\u6848\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1aintervals = [[1,4],[2,4],[3,6],[4,4]], queries = [2,3,4,5]\n\u8f93\u51fa\uff1a[3,3,1,4]\n\u89e3\u91ca\uff1a\u67e5\u8be2\u5904\u7406\u5982\u4e0b\uff1a\n- Query = 2 \uff1a\u533a\u95f4 [2,4] \u662f\u5305\u542b 2 \u7684\u6700\u5c0f\u533a\u95f4\uff0c\u7b54\u6848\u4e3a 4 - 2 + 1 = 3 \u3002\n- Query = 3 \uff1a\u533a\u95f4 [2,4] \u662f\u5305\u542b 3 \u7684\u6700\u5c0f\u533a\u95f4\uff0c\u7b54\u6848\u4e3a 4 - 2 + 1 = 3 \u3002\n- Query = 4 \uff1a\u533a\u95f4 [4,4] \u662f\u5305\u542b 4 \u7684\u6700\u5c0f\u533a\u95f4\uff0c\u7b54\u6848\u4e3a 4 - 4 + 1 = 1 \u3002\n- Query = 5 \uff1a\u533a\u95f4 [3,6] \u662f\u5305\u542b 5 \u7684\u6700\u5c0f\u533a\u95f4\uff0c\u7b54\u6848\u4e3a 6 - 3 + 1 = 4 \u3002\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1aintervals = [[2,3],[2,5],[1,8],[20,25]], queries = [2,19,5,22]\n\u8f93\u51fa\uff1a[2,-1,4,6]\n\u89e3\u91ca\uff1a\u67e5\u8be2\u5904\u7406\u5982\u4e0b\uff1a\n- Query = 2 \uff1a\u533a\u95f4 [2,3] \u662f\u5305\u542b 2 \u7684\u6700\u5c0f\u533a\u95f4\uff0c\u7b54\u6848\u4e3a 3 - 2 + 1 = 2 \u3002\n- Query = 19\uff1a\u4e0d\u5b58\u5728\u5305\u542b 19 \u7684\u533a\u95f4\uff0c\u7b54\u6848\u4e3a -1 \u3002\n- Query = 5 \uff1a\u533a\u95f4 [2,5] \u662f\u5305\u542b 5 \u7684\u6700\u5c0f\u533a\u95f4\uff0c\u7b54\u6848\u4e3a 5 - 2 + 1 = 4 \u3002\n- Query = 22\uff1a\u533a\u95f4 [20,25] \u662f\u5305\u542b 22 \u7684\u6700\u5c0f\u533a\u95f4\uff0c\u7b54\u6848\u4e3a 25 - 20 + 1 = 6 \u3002\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\t1 <= intervals.length <= 105
\n\t1 <= queries.length <= 105
\n\tqueries[i].length == 2
\n\t1 <= lefti <= righti <= 107
\n\t1 <= queries[j] <= 107
\n
\n", "tags_en": ["Array", "Binary Search", "Sorting", "Line Sweep", "Heap (Priority Queue)"], "tags_cn": ["\u6570\u7ec4", "\u4e8c\u5206\u67e5\u627e", "\u6392\u5e8f", "\u626b\u63cf\u7ebf", "\u5806\uff08\u4f18\u5148\u961f\u5217\uff09"], "difficulty_en": "Hard", "difficulty_cn": "\u56f0\u96be", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n vector minInterval(vector>& intervals, vector& queries) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int[] minInterval(int[][] intervals, int[] queries) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def minInterval(self, intervals, queries):\n \"\"\"\n :type intervals: List[List[int]]\n :type queries: List[int]\n :rtype: List[int]\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def minInterval(self, intervals: List[List[int]], queries: List[int]) -> List[int]:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\n/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\nint* minInterval(int** intervals, int intervalsSize, int* intervalsColSize, int* queries, int queriesSize, int* returnSize){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int[] MinInterval(int[][] intervals, int[] queries) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[][]} intervals\n * @param {number[]} queries\n * @return {number[]}\n */\nvar minInterval = function(intervals, queries) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer[][]} intervals\n# @param {Integer[]} queries\n# @return {Integer[]}\ndef min_interval(intervals, queries)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func minInterval(_ intervals: [[Int]], _ queries: [Int]) -> [Int] {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func minInterval(intervals [][]int, queries []int) []int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def minInterval(intervals: Array[Array[Int]], queries: Array[Int]): Array[Int] = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun minInterval(intervals: Array, queries: IntArray): IntArray {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn min_interval(intervals: Vec>, queries: Vec) -> Vec {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer[][] $intervals\n * @param Integer[] $queries\n * @return Integer[]\n */\n function minInterval($intervals, $queries) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function minInterval(intervals: number[][], queries: number[]): number[] {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (min-interval intervals queries)\n (-> (listof (listof exact-integer?)) (listof exact-integer?) (listof exact-integer?))\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec min_interval(Intervals :: [[integer()]], Queries :: [integer()]) -> [integer()].\nmin_interval(Intervals, Queries) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec min_interval(intervals :: [[integer]], queries :: [integer]) :: [integer]\n def min_interval(intervals, queries) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1851](https://leetcode-cn.com/problems/minimum-interval-to-include-each-query)", "[\u5305\u542b\u6bcf\u4e2a\u67e5\u8be2\u7684\u6700\u5c0f\u533a\u95f4](/solution/1800-1899/1851.Minimum%20Interval%20to%20Include%20Each%20Query/README.md)", "`\u6570\u7ec4`,`\u4e8c\u5206\u67e5\u627e`,`\u6392\u5e8f`,`\u626b\u63cf\u7ebf`,`\u5806\uff08\u4f18\u5148\u961f\u5217\uff09`", "\u56f0\u96be", ""], "md_table_row_en": ["[1851](https://leetcode.com/problems/minimum-interval-to-include-each-query)", "[Minimum Interval to Include Each Query](/solution/1800-1899/1851.Minimum%20Interval%20to%20Include%20Each%20Query/README_EN.md)", "`Array`,`Binary Search`,`Sorting`,`Line Sweep`,`Heap (Priority Queue)`", "Hard", ""]}, {"question_id": "1976", "frontend_question_id": "1849", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/splitting-a-string-into-descending-consecutive-values", "url_en": "https://leetcode.com/problems/splitting-a-string-into-descending-consecutive-values", "relative_path_cn": "/solution/1800-1899/1849.Splitting%20a%20String%20Into%20Descending%20Consecutive%20Values/README.md", "relative_path_en": "/solution/1800-1899/1849.Splitting%20a%20String%20Into%20Descending%20Consecutive%20Values/README_EN.md", "title_cn": "\u5c06\u5b57\u7b26\u4e32\u62c6\u5206\u4e3a\u9012\u51cf\u7684\u8fde\u7eed\u503c", "title_en": "Splitting a String Into Descending Consecutive Values", "question_title_slug": "splitting-a-string-into-descending-consecutive-values", "content_en": "You are given a string s
that consists of only digits.
\n\nCheck if we can split s
into two or more non-empty substrings such that the numerical values of the substrings are in descending order and the difference between numerical values of every two adjacent substrings is equal to 1
.
\n\n\n\t- For example, the string
s = "0090089"
can be split into ["0090", "089"]
with numerical values [90,89]
. The values are in descending order and adjacent values differ by 1
, so this way is valid. \n\t- Another example, the string
s = "001"
can be split into ["0", "01"]
, ["00", "1"]
, or ["0", "0", "1"]
. However all the ways are invalid because they have numerical values [0,1]
, [0,1]
, and [0,0,1]
respectively, all of which are not in descending order. \n
\n\nReturn true
if it is possible to split s
\u200b\u200b\u200b\u200b\u200b\u200b as described above, or false
otherwise.
\n\nA substring is a contiguous sequence of characters in a string.
\n\n
\nExample 1:
\n\n\nInput: s = "1234"\nOutput: false\nExplanation: There is no valid way to split s.\n
\n\nExample 2:
\n\n\nInput: s = "050043"\nOutput: true\nExplanation: s can be split into ["05", "004", "3"] with numerical values [5,4,3].\nThe values are in descending order with adjacent values differing by 1.\n
\n\nExample 3:
\n\n\nInput: s = "9080701"\nOutput: false\nExplanation: There is no valid way to split s.\n
\n\nExample 4:
\n\n\nInput: s = "10009998"\nOutput: true\nExplanation: s can be split into ["100", "099", "98"] with numerical values [100,99,98].\nThe values are in descending order with adjacent values differing by 1.\n
\n\n
\nConstraints:
\n\n\n\t1 <= s.length <= 20
\n\ts
only consists of digits. \n
\n", "content_cn": "\u7ed9\u4f60\u4e00\u4e2a\u4ec5\u7531\u6570\u5b57\u7ec4\u6210\u7684\u5b57\u7b26\u4e32 s
\u3002
\n\n\u8bf7\u4f60\u5224\u65ad\u80fd\u5426\u5c06 s
\u62c6\u5206\u6210\u4e24\u4e2a\u6216\u8005\u591a\u4e2a \u975e\u7a7a\u5b50\u5b57\u7b26\u4e32 \uff0c\u4f7f\u5b50\u5b57\u7b26\u4e32\u7684 \u6570\u503c \u6309 \u964d\u5e8f \u6392\u5217\uff0c\u4e14\u6bcf\u4e24\u4e2a \u76f8\u90bb\u5b50\u5b57\u7b26\u4e32 \u7684\u6570\u503c\u4e4b \u5dee \u7b49\u4e8e 1
\u3002
\n\n\n\t- \u4f8b\u5982\uff0c\u5b57\u7b26\u4e32
s = \"0090089\"
\u53ef\u4ee5\u62c6\u5206\u6210 [\"0090\", \"089\"]
\uff0c\u6570\u503c\u4e3a [90,89]
\u3002\u8fd9\u4e9b\u6570\u503c\u6ee1\u8db3\u6309\u964d\u5e8f\u6392\u5217\uff0c\u4e14\u76f8\u90bb\u503c\u76f8\u5dee 1
\uff0c\u8fd9\u79cd\u62c6\u5206\u65b9\u6cd5\u53ef\u884c\u3002 \n\t- \u53e6\u4e00\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5b57\u7b26\u4e32
s = \"001\"
\u53ef\u4ee5\u62c6\u5206\u6210 [\"0\", \"01\"]
\u3001[\"00\", \"1\"]
\u6216 [\"0\", \"0\", \"1\"]
\u3002\u7136\u800c\uff0c\u6240\u6709\u8fd9\u4e9b\u62c6\u5206\u65b9\u6cd5\u90fd\u4e0d\u53ef\u884c\uff0c\u56e0\u4e3a\u5bf9\u5e94\u6570\u503c\u5206\u522b\u662f [0,1]
\u3001[0,1]
\u548c [0,0,1]
\uff0c\u90fd\u4e0d\u6ee1\u8db3\u6309\u964d\u5e8f\u6392\u5217\u7684\u8981\u6c42\u3002 \n
\n\n\u5982\u679c\u53ef\u4ee5\u6309\u8981\u6c42\u62c6\u5206 s
\uff0c\u8fd4\u56de true
\uff1b\u5426\u5219\uff0c\u8fd4\u56de false
\u3002
\n\n\u5b50\u5b57\u7b26\u4e32 \u662f\u5b57\u7b26\u4e32\u4e2d\u7684\u4e00\u4e2a\u8fde\u7eed\u5b57\u7b26\u5e8f\u5217\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1as = \"1234\"\n\u8f93\u51fa\uff1afalse\n\u89e3\u91ca\uff1a\u4e0d\u5b58\u5728\u62c6\u5206 s \u7684\u53ef\u884c\u65b9\u6cd5\u3002\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1as = \"050043\"\n\u8f93\u51fa\uff1atrue\n\u89e3\u91ca\uff1as \u53ef\u4ee5\u62c6\u5206\u4e3a [\"05\", \"004\", \"3\"] \uff0c\u5bf9\u5e94\u6570\u503c\u4e3a [5,4,3] \u3002\n\u6ee1\u8db3\u6309\u964d\u5e8f\u6392\u5217\uff0c\u4e14\u76f8\u90bb\u503c\u76f8\u5dee 1
\u3002\n
\n\n\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1as = \"9080701\"\n\u8f93\u51fa\uff1afalse\n\u89e3\u91ca\uff1a\u4e0d\u5b58\u5728\u62c6\u5206 s \u7684\u53ef\u884c\u65b9\u6cd5\u3002\n
\n\n\u793a\u4f8b 4\uff1a
\n\n\n\u8f93\u5165\uff1as = \"10009998\"\n\u8f93\u51fa\uff1atrue\n\u89e3\u91ca\uff1as \u53ef\u4ee5\u62c6\u5206\u4e3a [\"100\", \"099\", \"98\"] \uff0c\u5bf9\u5e94\u6570\u503c\u4e3a [100,99,98] \u3002\n\u6ee1\u8db3\u6309\u964d\u5e8f\u6392\u5217\uff0c\u4e14\u76f8\u90bb\u503c\u76f8\u5dee 1
\u3002
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\t1 <= s.length <= 20
\n\ts
\u4ec5\u7531\u6570\u5b57\u7ec4\u6210 \n
\n", "tags_en": ["String", "Backtracking"], "tags_cn": ["\u5b57\u7b26\u4e32", "\u56de\u6eaf"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n bool splitString(string s) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public boolean splitString(String s) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def splitString(self, s):\n \"\"\"\n :type s: str\n :rtype: bool\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def splitString(self, s: str) -> bool:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nbool splitString(char * s){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public bool SplitString(string s) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {string} s\n * @return {boolean}\n */\nvar splitString = function(s) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {String} s\n# @return {Boolean}\ndef split_string(s)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func splitString(_ s: String) -> Bool {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func splitString(s string) bool {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def splitString(s: String): Boolean = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun splitString(s: String): Boolean {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn split_string(s: String) -> bool {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param String $s\n * @return Boolean\n */\n function splitString($s) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function splitString(s: string): boolean {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (split-string s)\n (-> string? boolean?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec split_string(S :: unicode:unicode_binary()) -> boolean().\nsplit_string(S) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec split_string(s :: String.t) :: boolean\n def split_string(s) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1849](https://leetcode-cn.com/problems/splitting-a-string-into-descending-consecutive-values)", "[\u5c06\u5b57\u7b26\u4e32\u62c6\u5206\u4e3a\u9012\u51cf\u7684\u8fde\u7eed\u503c](/solution/1800-1899/1849.Splitting%20a%20String%20Into%20Descending%20Consecutive%20Values/README.md)", "`\u5b57\u7b26\u4e32`,`\u56de\u6eaf`", "\u4e2d\u7b49", ""], "md_table_row_en": ["[1849](https://leetcode.com/problems/splitting-a-string-into-descending-consecutive-values)", "[Splitting a String Into Descending Consecutive Values](/solution/1800-1899/1849.Splitting%20a%20String%20Into%20Descending%20Consecutive%20Values/README_EN.md)", "`String`,`Backtracking`", "Medium", ""]}, {"question_id": "1975", "frontend_question_id": "1848", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/minimum-distance-to-the-target-element", "url_en": "https://leetcode.com/problems/minimum-distance-to-the-target-element", "relative_path_cn": "/solution/1800-1899/1848.Minimum%20Distance%20to%20the%20Target%20Element/README.md", "relative_path_en": "/solution/1800-1899/1848.Minimum%20Distance%20to%20the%20Target%20Element/README_EN.md", "title_cn": "\u5230\u76ee\u6807\u5143\u7d20\u7684\u6700\u5c0f\u8ddd\u79bb", "title_en": "Minimum Distance to the Target Element", "question_title_slug": "minimum-distance-to-the-target-element", "content_en": "Given an integer array nums
(0-indexed) and two integers target
and start
, find an index i
such that nums[i] == target
and abs(i - start)
is minimized. Note that abs(x)
is the absolute value of x
.
\n\nReturn abs(i - start)
.
\n\nIt is guaranteed that target
exists in nums
.
\n\n
\nExample 1:
\n\n\nInput: nums = [1,2,3,4,5], target = 5, start = 3\nOutput: 1\nExplanation: nums[4] = 5 is the only value equal to target, so the answer is abs(4 - 3) = 1.\n
\n\nExample 2:
\n\n\nInput: nums = [1], target = 1, start = 0\nOutput: 0\nExplanation: nums[0] = 1 is the only value equal to target, so the answer is abs(0 - 0) = 0.\n
\n\nExample 3:
\n\n\nInput: nums = [1,1,1,1,1,1,1,1,1,1], target = 1, start = 0\nOutput: 0\nExplanation: Every value of nums is 1, but nums[0] minimizes abs(i - start), which is abs(0 - 0) = 0.\n
\n\n
\nConstraints:
\n\n\n\t1 <= nums.length <= 1000
\n\t1 <= nums[i] <= 104
\n\t0 <= start < nums.length
\n\ttarget
is in nums
. \n
\n", "content_cn": "\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 nums
\uff08\u4e0b\u6807 \u4ece 0 \u5f00\u59cb \u8ba1\u6570\uff09\u4ee5\u53ca\u4e24\u4e2a\u6574\u6570 target
\u548c start
\uff0c\u8bf7\u4f60\u627e\u51fa\u4e00\u4e2a\u4e0b\u6807 i
\uff0c\u6ee1\u8db3 nums[i] == target
\u4e14 abs(i - start)
\u6700\u5c0f\u5316 \u3002\u6ce8\u610f\uff1aabs(x)
\u8868\u793a x
\u7684\u7edd\u5bf9\u503c\u3002
\n\n\u8fd4\u56de abs(i - start)
\u3002
\n\n\u9898\u76ee\u6570\u636e\u4fdd\u8bc1 target
\u5b58\u5728\u4e8e nums
\u4e2d\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1anums = [1,2,3,4,5], target = 5, start = 3\n\u8f93\u51fa\uff1a1\n\u89e3\u91ca\uff1anums[4] = 5 \u662f\u552f\u4e00\u4e00\u4e2a\u7b49\u4e8e target \u7684\u503c\uff0c\u6240\u4ee5\u7b54\u6848\u662f abs(4 - 3) = 1 \u3002\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1anums = [1], target = 1, start = 0\n\u8f93\u51fa\uff1a0\n\u89e3\u91ca\uff1anums[0] = 1 \u662f\u552f\u4e00\u4e00\u4e2a\u7b49\u4e8e target \u7684\u503c\uff0c\u6240\u4ee5\u7b54\u6848\u662f abs(0 - 0) = 0 \u3002\n
\n\n\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1anums = [1,1,1,1,1,1,1,1,1,1], target = 1, start = 0\n\u8f93\u51fa\uff1a0\n\u89e3\u91ca\uff1anums \u4e2d\u7684\u6bcf\u4e2a\u503c\u90fd\u662f 1 \uff0c\u4f46 nums[0] \u4f7f abs(i - start) \u7684\u7ed3\u679c\u5f97\u4ee5\u6700\u5c0f\u5316\uff0c\u6240\u4ee5\u7b54\u6848\u662f abs(0 - 0) = 0 \u3002\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\t1 <= nums.length <= 1000
\n\t1 <= nums[i] <= 104
\n\t0 <= start < nums.length
\n\ttarget
\u5b58\u5728\u4e8e nums
\u4e2d \n
\n", "tags_en": ["Array"], "tags_cn": ["\u6570\u7ec4"], "difficulty_en": "Easy", "difficulty_cn": "\u7b80\u5355", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n int getMinDistance(vector& nums, int target, int start) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int getMinDistance(int[] nums, int target, int start) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def getMinDistance(self, nums, target, start):\n \"\"\"\n :type nums: List[int]\n :type target: int\n :type start: int\n :rtype: int\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def getMinDistance(self, nums: List[int], target: int, start: int) -> int:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nint getMinDistance(int* nums, int numsSize, int target, int start){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int GetMinDistance(int[] nums, int target, int start) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[]} nums\n * @param {number} target\n * @param {number} start\n * @return {number}\n */\nvar getMinDistance = function(nums, target, start) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer[]} nums\n# @param {Integer} target\n# @param {Integer} start\n# @return {Integer}\ndef get_min_distance(nums, target, start)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func getMinDistance(_ nums: [Int], _ target: Int, _ start: Int) -> Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func getMinDistance(nums []int, target int, start int) int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def getMinDistance(nums: Array[Int], target: Int, start: Int): Int = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun getMinDistance(nums: IntArray, target: Int, start: Int): Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn get_min_distance(nums: Vec, target: i32, start: i32) -> i32 {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @param Integer $target\n * @param Integer $start\n * @return Integer\n */\n function getMinDistance($nums, $target, $start) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function getMinDistance(nums: number[], target: number, start: number): number {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (get-min-distance nums target start)\n (-> (listof exact-integer?) exact-integer? exact-integer? exact-integer?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec get_min_distance(Nums :: [integer()], Target :: integer(), Start :: integer()) -> integer().\nget_min_distance(Nums, Target, Start) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec get_min_distance(nums :: [integer], target :: integer, start :: integer) :: integer\n def get_min_distance(nums, target, start) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1848](https://leetcode-cn.com/problems/minimum-distance-to-the-target-element)", "[\u5230\u76ee\u6807\u5143\u7d20\u7684\u6700\u5c0f\u8ddd\u79bb](/solution/1800-1899/1848.Minimum%20Distance%20to%20the%20Target%20Element/README.md)", "`\u6570\u7ec4`", "\u7b80\u5355", ""], "md_table_row_en": ["[1848](https://leetcode.com/problems/minimum-distance-to-the-target-element)", "[Minimum Distance to the Target Element](/solution/1800-1899/1848.Minimum%20Distance%20to%20the%20Target%20Element/README_EN.md)", "`Array`", "Easy", ""]}, {"question_id": "1974", "frontend_question_id": "1821", "paid_only": true, "paid_only_cn": true, "category": "Database", "url_cn": "https://leetcode-cn.com/problems/find-customers-with-positive-revenue-this-year", "url_en": "https://leetcode.com/problems/find-customers-with-positive-revenue-this-year", "relative_path_cn": "/solution/1800-1899/1821.Find%20Customers%20With%20Positive%20Revenue%20this%20Year/README.md", "relative_path_en": "/solution/1800-1899/1821.Find%20Customers%20With%20Positive%20Revenue%20this%20Year/README_EN.md", "title_cn": "\u5bfb\u627e\u4eca\u5e74\u5177\u6709\u6b63\u6536\u5165\u7684\u5ba2\u6237", "title_en": "Find Customers With Positive Revenue this Year", "question_title_slug": "find-customers-with-positive-revenue-this-year", "content_en": "Table: Customers
\n\n\n+--------------+------+\n| Column Name | Type |\n+--------------+------+\n| customer_id | int |\n| year | int |\n| revenue | int |\n+--------------+------+\n(customer_id, year) is the primary key for this table.\nThis table contains the customer ID and the revenue of customers in different years.\nNote that this revenue can be negative.\n
\n\n
\n\nWrite an SQL query to report the customers with postive revenue in the year 2021.
\n\nReturn the result table in any order.
\n\nThe query result format is in the following example:
\n\n
\n\n\nCustomers\n+-------------+------+---------+\n| customer_id | year | revenue |\n+-------------+------+---------+\n| 1 | 2018 | 50 |\n| 1 | 2021 | 30 |\n| 1 | 2020 | 70 |\n| 2 | 2021 | -50 |\n| 3 | 2018 | 10 |\n| 3 | 2016 | 50 |\n| 4 | 2021 | 20 |\n+-------------+------+---------+\n\nResult table:\n+-------------+\n| customer_id |\n+-------------+\n| 1 |\n| 4 |\n+-------------+\n\nCustomer 1 has revenue equal to 30 in year 2021.\nCustomer 2 has revenue equal to -50 in year 2021.\nCustomer 3 has no revenue in year 2021.\nCustomer 4 has revenue equal to 20 in year 2021.\nThus only customers 1 and 4 have postive revenue in year 2021.
\n", "content_cn": "\u8868\uff1aCustomers
\n\n\n+--------------+------+\n| Column Name | Type |\n+--------------+------+\n| customer_id | int |\n| year | int |\n| revenue | int |\n+--------------+------+\n(customer_id, year) \u662f\u8fd9\u4e2a\u8868\u7684\u4e3b\u952e\u3002\n\u8fd9\u4e2a\u8868\u5305\u542b\u5ba2\u6237 ID \u548c\u4e0d\u540c\u5e74\u4efd\u7684\u5ba2\u6237\u6536\u5165\u3002\n\u6ce8\u610f\uff0c\u8fd9\u4e2a\u6536\u5165\u53ef\u80fd\u662f\u8d1f\u6570\u3002\n
\n\n\u00a0
\n\n\u5199\u4e00\u4e2a SQL \u67e5\u8be2\u6765\u67e5\u8be2 2021 \u5e74\u5177\u6709 \u6b63\u6536\u5165 \u7684\u5ba2\u6237\u3002
\n\n\u53ef\u4ee5\u6309 \u4efb\u610f\u987a\u5e8f \u8fd4\u56de\u7ed3\u679c\u8868\u3002
\n\n\u67e5\u8be2\u7ed3\u679c\u683c\u5f0f\u5982\u4e0b\u4f8b\u3002
\n\n\u00a0
\n\n\nCustomers\n+-------------+------+---------+\n| customer_id | year | revenue |\n+-------------+------+---------+\n| 1 | 2018 | 50 |\n| 1 | 2021 | 30 |\n| 1 | 2020 | 70 |\n| 2 | 2021 | -50 |\n| 3 | 2018 | 10 |\n| 3 | 2016 | 50 |\n| 4 | 2021 | 20 |\n+-------------+------+---------+\n\nResult table:\n+-------------+\n| customer_id |\n+-------------+\n| 1 |\n| 4 |\n+-------------+\n\u5ba2\u6237 1 \u5728 2021 \u5e74\u7684\u6536\u5165\u7b49\u4e8e 30 \u3002\n\u5ba2\u6237 2 \u5728 2021 \u5e74\u7684\u6536\u5165\u7b49\u4e8e -50 \u3002\n\u5ba2\u6237 3 \u5728 2021 \u5e74\u6ca1\u6709\u6536\u5165\u3002\n\u5ba2\u6237 4 \u5728 2021 \u5e74\u7684\u6536\u5165\u7b49\u4e8e 20 \u3002\n\u56e0\u6b64\uff0c\u53ea\u6709\u5ba2\u6237 1 \u548c 4 \u5728 2021 \u5e74\u6709\u6b63\u6536\u5165\u3002
\n", "tags_en": ["Database"], "tags_cn": ["\u6570\u636e\u5e93"], "difficulty_en": "Easy", "difficulty_cn": "\u7b80\u5355", "code_snippets": [{"lang": "MySQL", "langSlug": "mysql", "code": "# Write your MySQL query statement below", "__typename": "CodeSnippetNode"}, {"lang": "MS SQL Server", "langSlug": "mssql", "code": "/* Write your T-SQL query statement below */", "__typename": "CodeSnippetNode"}, {"lang": "Oracle", "langSlug": "oraclesql", "code": "/* Write your PL/SQL query statement below */", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1821](https://leetcode-cn.com/problems/find-customers-with-positive-revenue-this-year)", "[\u5bfb\u627e\u4eca\u5e74\u5177\u6709\u6b63\u6536\u5165\u7684\u5ba2\u6237](/solution/1800-1899/1821.Find%20Customers%20With%20Positive%20Revenue%20this%20Year/README.md)", "`\u6570\u636e\u5e93`", "\u7b80\u5355", "\ud83d\udd12"], "md_table_row_en": ["[1821](https://leetcode.com/problems/find-customers-with-positive-revenue-this-year)", "[Find Customers With Positive Revenue this Year](/solution/1800-1899/1821.Find%20Customers%20With%20Positive%20Revenue%20this%20Year/README_EN.md)", "`Database`", "Easy", "\ud83d\udd12"]}, {"question_id": "1972", "frontend_question_id": "1861", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/rotating-the-box", "url_en": "https://leetcode.com/problems/rotating-the-box", "relative_path_cn": "/solution/1800-1899/1861.Rotating%20the%20Box/README.md", "relative_path_en": "/solution/1800-1899/1861.Rotating%20the%20Box/README_EN.md", "title_cn": "\u65cb\u8f6c\u76d2\u5b50", "title_en": "Rotating the Box", "question_title_slug": "rotating-the-box", "content_en": "You are given an m x n
matrix of characters box
representing a side-view of a box. Each cell of the box is one of the following:
\r\n\r\n\r\n\t- A stone
'#'
\r\n\t- A stationary obstacle
'*'
\r\n\t- Empty
'.'
\r\n
\r\n\r\nThe box is rotated 90 degrees clockwise, causing some of the stones to fall due to gravity. Each stone falls down until it lands on an obstacle, another stone, or the bottom of the box. Gravity does not affect the obstacles' positions, and the inertia from the box's rotation does not affect the stones' horizontal positions.
\r\n\r\nIt is guaranteed that each stone in box
rests on an obstacle, another stone, or the bottom of the box.
\r\n\r\nReturn an n x m
matrix representing the box after the rotation described above.
\r\n\r\n
\r\nExample 1:
\r\n\r\n
\r\n\r\n\r\nInput: box = [["#",".","#"]]\r\nOutput: [["."],\r\n ["#"],\r\n ["#"]]\r\n
\r\n\r\nExample 2:
\r\n\r\n
\r\n\r\n\r\nInput: box = [["#",".","*","."],\r\n ["#","#","*","."]]\r\nOutput: [["#","."],\r\n ["#","#"],\r\n ["*","*"],\r\n [".","."]]\r\n
\r\n\r\nExample 3:
\r\n\r\n
\r\n\r\n\r\nInput: box = [["#","#","*",".","*","."],\r\n ["#","#","#","*",".","."],\r\n ["#","#","#",".","#","."]]\r\nOutput: [[".","#","#"],\r\n [".","#","#"],\r\n ["#","#","*"],\r\n ["#","*","."],\r\n ["#",".","*"],\r\n ["#",".","."]]\r\n
\r\n\r\n
\r\nConstraints:
\r\n\r\n\r\n\tm == box.length
\r\n\tn == box[i].length
\r\n\t1 <= m, n <= 500
\r\n\tbox[i][j]
is either '#'
, '*'
, or '.'
. \r\n
", "content_cn": "\u7ed9\u4f60\u4e00\u4e2a\u00a0m x n
\u00a0\u7684\u5b57\u7b26\u77e9\u9635\u00a0box
\u00a0\uff0c\u5b83\u8868\u793a\u4e00\u4e2a\u7bb1\u5b50\u7684\u4fa7\u89c6\u56fe\u3002\u7bb1\u5b50\u7684\u6bcf\u4e00\u4e2a\u683c\u5b50\u53ef\u80fd\u4e3a\uff1a
\n\n\n\t'#'
\u00a0\u8868\u793a\u77f3\u5934 \n\t'*'
\u00a0\u8868\u793a\u56fa\u5b9a\u7684\u969c\u788d\u7269 \n\t'.'
\u00a0\u8868\u793a\u7a7a\u4f4d\u7f6e \n
\n\n\u8fd9\u4e2a\u7bb1\u5b50\u88ab \u987a\u65f6\u9488\u65cb\u8f6c 90 \u5ea6\u00a0\uff0c\u7531\u4e8e\u91cd\u529b\u539f\u56e0\uff0c\u90e8\u5206\u77f3\u5934\u7684\u4f4d\u7f6e\u4f1a\u53d1\u751f\u6539\u53d8\u3002\u6bcf\u4e2a\u77f3\u5934\u4f1a\u5782\u76f4\u6389\u843d\uff0c\u76f4\u5230\u5b83\u9047\u5230\u969c\u788d\u7269\uff0c\u53e6\u4e00\u4e2a\u77f3\u5934\u6216\u8005\u7bb1\u5b50\u7684\u5e95\u90e8\u3002\u91cd\u529b \u4e0d\u4f1a\u00a0\u5f71\u54cd\u969c\u788d\u7269\u7684\u4f4d\u7f6e\uff0c\u540c\u65f6\u7bb1\u5b50\u65cb\u8f6c\u4e0d\u4f1a\u4ea7\u751f\u60ef\u6027\u00a0\uff0c\u4e5f\u5c31\u662f\u8bf4\u77f3\u5934\u7684\u6c34\u5e73\u4f4d\u7f6e\u4e0d\u4f1a\u53d1\u751f\u6539\u53d8\u3002
\n\n\u9898\u76ee\u4fdd\u8bc1\u521d\u59cb\u65f6\u00a0box
\u00a0\u4e2d\u7684\u77f3\u5934\u8981\u4e48\u5728\u4e00\u4e2a\u969c\u788d\u7269\u4e0a\uff0c\u8981\u4e48\u5728\u53e6\u4e00\u4e2a\u77f3\u5934\u4e0a\uff0c\u8981\u4e48\u5728\u7bb1\u5b50\u7684\u5e95\u90e8\u3002
\n\n\u8bf7\u4f60\u8fd4\u56de\u4e00\u4e2a\u00a0n x m
\u7684\u77e9\u9635\uff0c\u8868\u793a\u6309\u7167\u4e0a\u8ff0\u65cb\u8f6c\u540e\uff0c\u7bb1\u5b50\u5185\u7684\u7ed3\u679c\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n
\n\n\u8f93\u5165\uff1abox = [[\"#\",\".\",\"#\"]]\n\u8f93\u51fa\uff1a[[\".\"],\n\u00a0 [\"#\"],\n\u00a0 [\"#\"]]\n
\n\n\u793a\u4f8b 2\uff1a
\n\n
\n\n\u8f93\u5165\uff1abox = [[\"#\",\".\",\"*\",\".\"],\n\u00a0 [\"#\",\"#\",\"*\",\".\"]]\n\u8f93\u51fa\uff1a[[\"#\",\".\"],\n\u00a0 [\"#\",\"#\"],\n\u00a0 [\"*\",\"*\"],\n\u00a0 [\".\",\".\"]]\n
\n\n\u793a\u4f8b 3\uff1a
\n\n
\n\n\u8f93\u5165\uff1abox = [[\"#\",\"#\",\"*\",\".\",\"*\",\".\"],\n\u00a0 [\"#\",\"#\",\"#\",\"*\",\".\",\".\"],\n\u00a0 [\"#\",\"#\",\"#\",\".\",\"#\",\".\"]]\n\u8f93\u51fa\uff1a[[\".\",\"#\",\"#\"],\n\u00a0 [\".\",\"#\",\"#\"],\n\u00a0 [\"#\",\"#\",\"*\"],\n\u00a0 [\"#\",\"*\",\".\"],\n\u00a0 [\"#\",\".\",\"*\"],\n\u00a0 [\"#\",\".\",\".\"]]\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\tm == box.length
\n\tn == box[i].length
\n\t1 <= m, n <= 500
\n\tbox[i][j]
\u00a0\u53ea\u53ef\u80fd\u662f\u00a0'#'
\u00a0\uff0c'*'
\u00a0\u6216\u8005\u00a0'.'
\u00a0\u3002 \n
\n", "tags_en": ["Array", "Two Pointers", "Matrix"], "tags_cn": ["\u6570\u7ec4", "\u53cc\u6307\u9488", "\u77e9\u9635"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n vector> rotateTheBox(vector>& box) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public char[][] rotateTheBox(char[][] box) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def rotateTheBox(self, box):\n \"\"\"\n :type box: List[List[str]]\n :rtype: List[List[str]]\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def rotateTheBox(self, box: List[List[str]]) -> List[List[str]]:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\n/**\n * Return an array of arrays of size *returnSize.\n * The sizes of the arrays are returned as *returnColumnSizes array.\n * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().\n */\nchar** rotateTheBox(char** box, int boxSize, int* boxColSize, int* returnSize, int** returnColumnSizes){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public char[][] RotateTheBox(char[][] box) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {character[][]} box\n * @return {character[][]}\n */\nvar rotateTheBox = function(box) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Character[][]} box\n# @return {Character[][]}\ndef rotate_the_box(box)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func rotateTheBox(_ box: [[Character]]) -> [[Character]] {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func rotateTheBox(box [][]byte) [][]byte {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def rotateTheBox(box: Array[Array[Char]]): Array[Array[Char]] = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun rotateTheBox(box: Array): Array {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn rotate_the_box(box: Vec>) -> Vec> {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param String[][] $box\n * @return String[][]\n */\n function rotateTheBox($box) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function rotateTheBox(box: string[][]): string[][] {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (rotate-the-box box)\n (-> (listof (listof char?)) (listof (listof char?)))\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec rotate_the_box(Box :: [[char()]]) -> [[char()]].\nrotate_the_box(Box) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec rotate_the_box(box :: [[char]]) :: [[char]]\n def rotate_the_box(box) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1861](https://leetcode-cn.com/problems/rotating-the-box)", "[\u65cb\u8f6c\u76d2\u5b50](/solution/1800-1899/1861.Rotating%20the%20Box/README.md)", "`\u6570\u7ec4`,`\u53cc\u6307\u9488`,`\u77e9\u9635`", "\u4e2d\u7b49", ""], "md_table_row_en": ["[1861](https://leetcode.com/problems/rotating-the-box)", "[Rotating the Box](/solution/1800-1899/1861.Rotating%20the%20Box/README_EN.md)", "`Array`,`Two Pointers`,`Matrix`", "Medium", ""]}, {"question_id": "1971", "frontend_question_id": "1860", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/incremental-memory-leak", "url_en": "https://leetcode.com/problems/incremental-memory-leak", "relative_path_cn": "/solution/1800-1899/1860.Incremental%20Memory%20Leak/README.md", "relative_path_en": "/solution/1800-1899/1860.Incremental%20Memory%20Leak/README_EN.md", "title_cn": "\u589e\u957f\u7684\u5185\u5b58\u6cc4\u9732", "title_en": "Incremental Memory Leak", "question_title_slug": "incremental-memory-leak", "content_en": "You are given two integers memory1
and memory2
representing the available memory in bits on two memory sticks. There is currently a faulty program running that consumes an increasing amount of memory every second.
\n\nAt the ith
second (starting from 1), i
bits of memory are allocated to the stick with more available memory (or from the first memory stick if both have the same available memory). If neither stick has at least i
bits of available memory, the program crashes.
\n\nReturn an array containing [crashTime, memory1crash, memory2crash]
, where crashTime
is the time (in seconds) when the program crashed and memory1crash
and memory2crash
are the available bits of memory in the first and second sticks respectively.
\n\n
\nExample 1:
\n\n\nInput: memory1 = 2, memory2 = 2\nOutput: [3,1,0]\nExplanation: The memory is allocated as follows:\n- At the 1st second, 1 bit of memory is allocated to stick 1. The first stick now has 1 bit of available memory.\n- At the 2nd second, 2 bits of memory are allocated to stick 2. The second stick now has 0 bits of available memory.\n- At the 3rd second, the program crashes. The sticks have 1 and 0 bits available respectively.\n
\n\nExample 2:
\n\n\nInput: memory1 = 8, memory2 = 11\nOutput: [6,0,4]\nExplanation: The memory is allocated as follows:\n- At the 1st second, 1 bit of memory is allocated to stick 2. The second stick now has 10 bit of available memory.\n- At the 2nd second, 2 bits of memory are allocated to stick 2. The second stick now has 8 bits of available memory.\n- At the 3rd second, 3 bits of memory are allocated to stick 1. The first stick now has 5 bits of available memory.\n- At the 4th second, 4 bits of memory are allocated to stick 2. The second stick now has 4 bits of available memory.\n- At the 5th second, 5 bits of memory are allocated to stick 1. The first stick now has 0 bits of available memory.\n- At the 6th second, the program crashes. The sticks have 0 and 4 bits available respectively.\n
\n\n
\nConstraints:
\n\n\n\t0 <= memory1, memory2 <= 231 - 1
\n
\n", "content_cn": "\u7ed9\u4f60\u4e24\u4e2a\u6574\u6570\u00a0memory1
\u548c\u00a0memory2
\u00a0\u5206\u522b\u8868\u793a\u4e24\u4e2a\u5185\u5b58\u6761\u5269\u4f59\u53ef\u7528\u5185\u5b58\u7684\u4f4d\u6570\u3002\u73b0\u5728\u6709\u4e00\u4e2a\u7a0b\u5e8f\u6bcf\u79d2\u9012\u589e\u7684\u901f\u5ea6\u6d88\u8017\u7740\u5185\u5b58\u3002
\n\n\u5728\u7b2c\u00a0i
\u00a0\u79d2\uff08\u79d2\u6570\u4ece 1 \u5f00\u59cb\uff09\uff0c\u6709 i
\u00a0\u4f4d\u5185\u5b58\u88ab\u5206\u914d\u5230\u00a0\u5269\u4f59\u5185\u5b58\u8f83\u591a\u00a0\u7684\u5185\u5b58\u6761\uff08\u5982\u679c\u4e24\u8005\u4e00\u6837\u591a\uff0c\u5219\u5206\u914d\u5230\u7b2c\u4e00\u4e2a\u5185\u5b58\u6761\uff09\u3002\u5982\u679c\u4e24\u8005\u5269\u4f59\u5185\u5b58\u90fd\u4e0d\u8db3 i
\u00a0\u4f4d\uff0c\u90a3\u4e48\u7a0b\u5e8f\u5c06 \u610f\u5916\u9000\u51fa\u00a0\u3002
\n\n\u8bf7\u4f60\u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4\uff0c\u5305\u542b [crashTime, memory1crash, memory2crash]
\u00a0\uff0c\u5176\u4e2d\u00a0crashTime
\u662f\u7a0b\u5e8f\u610f\u5916\u9000\u51fa\u7684\u65f6\u95f4\uff08\u5355\u4f4d\u4e3a\u79d2\uff09\uff0c\u00a0memory1crash
\u548c\u00a0memory2crash
\u00a0\u5206\u522b\u662f\u4e24\u4e2a\u5185\u5b58\u6761\u6700\u540e\u5269\u4f59\u5185\u5b58\u7684\u4f4d\u6570\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1amemory1 = 2, memory2 = 2\n\u8f93\u51fa\uff1a[3,1,0]\n\u89e3\u91ca\uff1a\u5185\u5b58\u5206\u914d\u5982\u4e0b\uff1a\n- \u7b2c 1 \u79d2\uff0c\u5185\u5b58\u6761 1 \u88ab\u5360\u7528 1 \u4f4d\u5185\u5b58\u3002\u5185\u5b58\u6761 1 \u73b0\u5728\u6709 1 \u4f4d\u5269\u4f59\u53ef\u7528\u5185\u5b58\u3002\n- \u7b2c 2 \u79d2\uff0c\u5185\u5b58\u6761 2 \u88ab\u5360\u7528 2 \u4f4d\u5185\u5b58\u3002\u5185\u5b58\u6761 2 \u73b0\u5728\u6709 0 \u4f4d\u5269\u4f59\u53ef\u7528\u5185\u5b58\u3002\n- \u7b2c 3 \u79d2\uff0c\u7a0b\u5e8f\u610f\u5916\u9000\u51fa\uff0c\u4e24\u4e2a\u5185\u5b58\u6761\u5206\u522b\u6709 1 \u4f4d\u548c 0 \u4f4d\u5269\u4f59\u53ef\u7528\u5185\u5b58\u3002\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1amemory1 = 8, memory2 = 11\n\u8f93\u51fa\uff1a[6,0,4]\n\u89e3\u91ca\uff1a\u5185\u5b58\u5206\u914d\u5982\u4e0b\uff1a\n- \u7b2c 1 \u79d2\uff0c\u5185\u5b58\u6761 2 \u88ab\u5360\u7528 1 \u4f4d\u5185\u5b58\uff0c\u5185\u5b58\u6761 2 \u73b0\u5728\u6709 10 \u4f4d\u5269\u4f59\u53ef\u7528\u5185\u5b58\u3002\n- \u7b2c 2 \u79d2\uff0c\u5185\u5b58\u6761 2 \u88ab\u5360\u7528 2 \u4f4d\u5185\u5b58\uff0c\u5185\u5b58\u6761 2 \u73b0\u5728\u6709 8 \u4f4d\u5269\u4f59\u53ef\u7528\u5185\u5b58\u3002\n- \u7b2c 3 \u79d2\uff0c\u5185\u5b58\u6761 1 \u88ab\u5360\u7528 3 \u4f4d\u5185\u5b58\uff0c\u5185\u5b58\u6761 1 \u73b0\u5728\u6709 5 \u4f4d\u5269\u4f59\u53ef\u7528\u5185\u5b58\u3002\n- \u7b2c 4 \u79d2\uff0c\u5185\u5b58\u6761 2 \u88ab\u5360\u7528 4 \u4f4d\u5185\u5b58\uff0c\u5185\u5b58\u6761 2 \u73b0\u5728\u6709 4 \u4f4d\u5269\u4f59\u53ef\u7528\u5185\u5b58\u3002\n- \u7b2c 5 \u79d2\uff0c\u5185\u5b58\u6761 1 \u88ab\u5360\u7528 5 \u4f4d\u5185\u5b58\uff0c\u5185\u5b58\u6761 1 \u73b0\u5728\u6709 0 \u4f4d\u5269\u4f59\u53ef\u7528\u5185\u5b58\u3002\n- \u7b2c 6 \u79d2\uff0c\u7a0b\u5e8f\u610f\u5916\u9000\u51fa\uff0c\u4e24\u4e2a\u5185\u5b58\u6761\u5206\u522b\u6709 0 \u4f4d\u548c 4 \u4f4d\u5269\u4f59\u53ef\u7528\u5185\u5b58\u3002\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\t0 <= memory1, memory2 <= 231 - 1
\n
\n", "tags_en": ["Simulation"], "tags_cn": ["\u6a21\u62df"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n vector memLeak(int memory1, int memory2) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int[] memLeak(int memory1, int memory2) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def memLeak(self, memory1, memory2):\n \"\"\"\n :type memory1: int\n :type memory2: int\n :rtype: List[int]\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def memLeak(self, memory1: int, memory2: int) -> List[int]:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\n/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\nint* memLeak(int memory1, int memory2, int* returnSize){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int[] MemLeak(int memory1, int memory2) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number} memory1\n * @param {number} memory2\n * @return {number[]}\n */\nvar memLeak = function(memory1, memory2) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer} memory1\n# @param {Integer} memory2\n# @return {Integer[]}\ndef mem_leak(memory1, memory2)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func memLeak(_ memory1: Int, _ memory2: Int) -> [Int] {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func memLeak(memory1 int, memory2 int) []int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def memLeak(memory1: Int, memory2: Int): Array[Int] = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun memLeak(memory1: Int, memory2: Int): IntArray {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn mem_leak(memory1: i32, memory2: i32) -> Vec {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer $memory1\n * @param Integer $memory2\n * @return Integer[]\n */\n function memLeak($memory1, $memory2) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function memLeak(memory1: number, memory2: number): number[] {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (mem-leak memory1 memory2)\n (-> exact-integer? exact-integer? (listof exact-integer?))\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec mem_leak(Memory1 :: integer(), Memory2 :: integer()) -> [integer()].\nmem_leak(Memory1, Memory2) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec mem_leak(memory1 :: integer, memory2 :: integer) :: [integer]\n def mem_leak(memory1, memory2) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1860](https://leetcode-cn.com/problems/incremental-memory-leak)", "[\u589e\u957f\u7684\u5185\u5b58\u6cc4\u9732](/solution/1800-1899/1860.Incremental%20Memory%20Leak/README.md)", "`\u6a21\u62df`", "\u4e2d\u7b49", ""], "md_table_row_en": ["[1860](https://leetcode.com/problems/incremental-memory-leak)", "[Incremental Memory Leak](/solution/1800-1899/1860.Incremental%20Memory%20Leak/README_EN.md)", "`Simulation`", "Medium", ""]}, {"question_id": "1970", "frontend_question_id": "1859", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/sorting-the-sentence", "url_en": "https://leetcode.com/problems/sorting-the-sentence", "relative_path_cn": "/solution/1800-1899/1859.Sorting%20the%20Sentence/README.md", "relative_path_en": "/solution/1800-1899/1859.Sorting%20the%20Sentence/README_EN.md", "title_cn": "\u5c06\u53e5\u5b50\u6392\u5e8f", "title_en": "Sorting the Sentence", "question_title_slug": "sorting-the-sentence", "content_en": "A sentence is a list of words that are separated by a single space with no leading or trailing spaces. Each word consists of lowercase and uppercase English letters.
\r\n\r\nA sentence can be shuffled by appending the 1-indexed word position to each word then rearranging the words in the sentence.
\r\n\r\n\r\n\t- For example, the sentence
"This is a sentence"
can be shuffled as "sentence4 a3 is2 This1"
or "is2 sentence4 This1 a3"
. \r\n
\r\n\r\nGiven a shuffled sentence s
containing no more than 9
words, reconstruct and return the original sentence.
\r\n\r\n
\r\nExample 1:
\r\n\r\n\r\nInput: s = "is2 sentence4 This1 a3"\r\nOutput: "This is a sentence"\r\nExplanation: Sort the words in s to their original positions "This1 is2 a3 sentence4", then remove the numbers.\r\n
\r\n\r\nExample 2:
\r\n\r\n\r\nInput: s = "Myself2 Me1 I4 and3"\r\nOutput: "Me Myself and I"\r\nExplanation: Sort the words in s to their original positions "Me1 Myself2 and3 I4", then remove the numbers.\r\n
\r\n\r\n
\r\nConstraints:
\r\n\r\n\r\n\t2 <= s.length <= 200
\r\n\ts
consists of lowercase and uppercase English letters, spaces, and digits from 1
to 9
. \r\n\t- The number of words in
s
is between 1
and 9
. \r\n\t- The words in
s
are separated by a single space. \r\n\ts
contains no leading or trailing spaces. \r\n
", "content_cn": "\u4e00\u4e2a \u53e5\u5b50\u00a0\u6307\u7684\u662f\u4e00\u4e2a\u5e8f\u5217\u7684\u5355\u8bcd\u7528\u5355\u4e2a\u7a7a\u683c\u8fde\u63a5\u8d77\u6765\uff0c\u4e14\u5f00\u5934\u548c\u7ed3\u5c3e\u6ca1\u6709\u4efb\u4f55\u7a7a\u683c\u3002\u6bcf\u4e2a\u5355\u8bcd\u90fd\u53ea\u5305\u542b\u5c0f\u5199\u6216\u5927\u5199\u82f1\u6587\u5b57\u6bcd\u3002
\n\n\u6211\u4eec\u53ef\u4ee5\u7ed9\u4e00\u4e2a\u53e5\u5b50\u6dfb\u52a0 \u4ece 1 \u5f00\u59cb\u7684\u5355\u8bcd\u4f4d\u7f6e\u7d22\u5f15 \uff0c\u5e76\u4e14\u5c06\u53e5\u5b50\u4e2d\u6240\u6709\u5355\u8bcd\u00a0\u6253\u4e71\u987a\u5e8f\u00a0\u3002
\n\n\n\t- \u6bd4\u65b9\u8bf4\uff0c\u53e5\u5b50\u00a0
\"This is a sentence\"
\u00a0\u53ef\u4ee5\u88ab\u6253\u4e71\u987a\u5e8f\u5f97\u5230\u00a0\"sentence4 a3 is2 This1\"
\u00a0\u6216\u8005\u00a0\"is2 sentence4 This1 a3\"
\u00a0\u3002 \n
\n\n\u7ed9\u4f60\u4e00\u4e2a \u6253\u4e71\u987a\u5e8f\u00a0\u7684\u53e5\u5b50\u00a0s
\u00a0\uff0c\u5b83\u5305\u542b\u7684\u5355\u8bcd\u4e0d\u8d85\u8fc7\u00a09
\u00a0\u4e2a\uff0c\u8bf7\u4f60\u91cd\u65b0\u6784\u9020\u5e76\u5f97\u5230\u539f\u672c\u987a\u5e8f\u7684\u53e5\u5b50\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1as = \"is2 sentence4 This1 a3\"\n\u8f93\u51fa\uff1a\"This is a sentence\"\n\u89e3\u91ca\uff1a\u5c06 s \u4e2d\u7684\u5355\u8bcd\u6309\u7167\u521d\u59cb\u4f4d\u7f6e\u6392\u5e8f\uff0c\u5f97\u5230 \"This1 is2 a3 sentence4\" \uff0c\u7136\u540e\u5220\u9664\u6570\u5b57\u3002\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1as = \"Myself2 Me1 I4 and3\"\n\u8f93\u51fa\uff1a\"Me Myself and I\"\n\u89e3\u91ca\uff1a\u5c06 s \u4e2d\u7684\u5355\u8bcd\u6309\u7167\u521d\u59cb\u4f4d\u7f6e\u6392\u5e8f\uff0c\u5f97\u5230 \"Me1 Myself2 and3 I4\" \uff0c\u7136\u540e\u5220\u9664\u6570\u5b57\u3002
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\t2 <= s.length <= 200
\n\ts
\u00a0\u53ea\u5305\u542b\u5c0f\u5199\u548c\u5927\u5199\u82f1\u6587\u5b57\u6bcd\u3001\u7a7a\u683c\u4ee5\u53ca\u4ece\u00a01
\u00a0\u5230\u00a09
\u00a0\u7684\u6570\u5b57\u3002 \n\ts
\u00a0\u4e2d\u5355\u8bcd\u6570\u76ee\u4e3a\u00a01
\u00a0\u5230\u00a09
\u00a0\u4e2a\u3002 \n\ts
\u00a0\u4e2d\u7684\u5355\u8bcd\u7531\u5355\u4e2a\u7a7a\u683c\u5206\u9694\u3002 \n\ts
\u00a0\u4e0d\u5305\u542b\u4efb\u4f55\u524d\u5bfc\u6216\u8005\u540e\u7f00\u7a7a\u683c\u3002 \n
\n", "tags_en": ["String", "Sorting"], "tags_cn": ["\u5b57\u7b26\u4e32", "\u6392\u5e8f"], "difficulty_en": "Easy", "difficulty_cn": "\u7b80\u5355", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n string sortSentence(string s) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public String sortSentence(String s) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def sortSentence(self, s):\n \"\"\"\n :type s: str\n :rtype: str\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def sortSentence(self, s: str) -> str:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nchar * sortSentence(char * s){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public string SortSentence(string s) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {string} s\n * @return {string}\n */\nvar sortSentence = function(s) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {String} s\n# @return {String}\ndef sort_sentence(s)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func sortSentence(_ s: String) -> String {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func sortSentence(s string) string {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def sortSentence(s: String): String = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun sortSentence(s: String): String {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn sort_sentence(s: String) -> String {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param String $s\n * @return String\n */\n function sortSentence($s) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function sortSentence(s: string): string {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (sort-sentence s)\n (-> string? string?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec sort_sentence(S :: unicode:unicode_binary()) -> unicode:unicode_binary().\nsort_sentence(S) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec sort_sentence(s :: String.t) :: String.t\n def sort_sentence(s) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1859](https://leetcode-cn.com/problems/sorting-the-sentence)", "[\u5c06\u53e5\u5b50\u6392\u5e8f](/solution/1800-1899/1859.Sorting%20the%20Sentence/README.md)", "`\u5b57\u7b26\u4e32`,`\u6392\u5e8f`", "\u7b80\u5355", ""], "md_table_row_en": ["[1859](https://leetcode.com/problems/sorting-the-sentence)", "[Sorting the Sentence](/solution/1800-1899/1859.Sorting%20the%20Sentence/README_EN.md)", "`String`,`Sorting`", "Easy", ""]}, {"question_id": "1969", "frontend_question_id": "1820", "paid_only": true, "paid_only_cn": true, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/maximum-number-of-accepted-invitations", "url_en": "https://leetcode.com/problems/maximum-number-of-accepted-invitations", "relative_path_cn": "/solution/1800-1899/1820.Maximum%20Number%20of%20Accepted%20Invitations/README.md", "relative_path_en": "/solution/1800-1899/1820.Maximum%20Number%20of%20Accepted%20Invitations/README_EN.md", "title_cn": "\u6700\u591a\u9080\u8bf7\u7684\u4e2a\u6570", "title_en": "Maximum Number of Accepted Invitations", "question_title_slug": "maximum-number-of-accepted-invitations", "content_en": "There are m
boys and n
girls in a class attending an upcoming party.
\n\nYou are given an m x n
integer matrix grid
, where grid[i][j]
equals 0
or 1
. If grid[i][j] == 1
, then that means the ith
boy can invite the jth
girl to the party. A boy can invite at most one girl, and a girl can accept at most one invitation from a boy.
\n\nReturn the maximum possible number of accepted invitations.
\n\n
\nExample 1:
\n\n\nInput: grid = [[1,1,1],\n [1,0,1],\n [0,0,1]]\nOutput: 3\nExplanation: The invitations are sent as follows:\n- The 1st boy invites the 2nd girl.\n- The 2nd boy invites the 1st girl.\n- The 3rd boy invites the 3rd girl.
\n\nExample 2:
\n\n\nInput: grid = [[1,0,1,0],\n [1,0,0,0],\n [0,0,1,0],\n [1,1,1,0]]\nOutput: 3\nExplanation: The invitations are sent as follows:\n-The 1st boy invites the 3rd girl.\n-The 2nd boy invites the 1st girl.\n-The 3rd boy invites no one.\n-The 4th boy invites the 2nd girl.
\n\n
\nConstraints:
\n\n\n\tgrid.length == m
\n\tgrid[i].length == n
\n\t1 <= m, n <= 200
\n\tgrid[i][j]
is either 0
or 1
. \n
\n", "content_cn": "\u67d0\u4e00\u4e2a\u73ed\u7ea7\u6709\u00a0m
\u00a0\u4e2a\u7537\u5b69\u548c\u00a0n
\u00a0\u4e2a\u5973\u5b69\uff0c\u5373\u5c06\u4e3e\u884c\u4e00\u4e2a\u6d3e\u5bf9\u3002
\n\n\u7ed9\u5b9a\u4e00\u4e2a\u00a0m x n
\u00a0\u7684\u6574\u6570\u77e9\u9635\u00a0grid
\u00a0\uff0c\u5176\u4e2d\u00a0grid[i][j]
\u00a0\u7b49\u4e8e\u00a00
\u00a0\u6216\u00a01
\u00a0\u3002 \u82e5\u00a0grid[i][j] == 1
\u00a0\uff0c\u5219\u8868\u793a\u7b2c\u00a0i
\u00a0\u4e2a\u7537\u5b69\u53ef\u4ee5\u9080\u8bf7\u7b2c\u00a0j
\u00a0\u4e2a\u5973\u5b69\u53c2\u52a0\u6d3e\u5bf9\u3002\u00a0\u4e00\u4e2a\u7537\u5b69\u6700\u591a\u53ef\u4ee5\u9080\u8bf7\u4e00\u4e2a\u5973\u5b69\uff0c\u4e00\u4e2a\u5973\u5b69\u6700\u591a\u53ef\u4ee5\u63a5\u53d7\u4e00\u4e2a\u7537\u5b69\u7684\u4e00\u4e2a\u9080\u8bf7\u3002
\n\n\u8fd4\u56de\u53ef\u80fd\u7684\u6700\u591a\u9080\u8bf7\u7684\u4e2a\u6570\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1:
\n\n\u8f93\u5165: grid = [[1,1,1],\n [1,0,1],\n [0,0,1]]\n\u8f93\u51fa: 3\n\u89e3\u91ca: \u6309\u4e0b\u5217\u65b9\u5f0f\u9080\u8bf7\uff1a\n- \u7b2c 1 \u4e2a\u7537\u5b69\u9080\u8bf7\u7b2c 2 \u4e2a\u5973\u5b69\u3002\n- \u7b2c 2 \u4e2a\u7537\u5b69\u9080\u8bf7\u7b2c 1 \u4e2a\u5973\u5b69\u3002\n- \u7b2c 3 \u4e2a\u7537\u5b69\u9080\u8bf7\u7b2c 3 \u4e2a\u5973\u5b69\u3002
\n\n\u793a\u4f8b 2:
\n\n\u8f93\u5165: grid = [[1,0,1,0],\n [1,0,0,0],\n [0,0,1,0],\n [1,1,1,0]]\n\u8f93\u51fa: 3\n\u89e3\u91ca: \u6309\u4e0b\u5217\u65b9\u5f0f\u9080\u8bf7\uff1a\n- \u7b2c 1 \u4e2a\u7537\u5b69\u9080\u8bf7\u7b2c 3 \u4e2a\u5973\u5b69\u3002\n- \u7b2c 2 \u4e2a\u7537\u5b69\u9080\u8bf7\u7b2c 1 \u4e2a\u5973\u5b69\u3002\n- \u7b2c 3 \u4e2a\u7537\u5b69\u672a\u9080\u8bf7\u4efb\u4f55\u4eba\u3002\n- \u7b2c 4 \u4e2a\u7537\u5b69\u9080\u8bf7\u7b2c 2 \u4e2a\u5973\u5b69\u3002
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\tgrid.length == m
\n\tgrid[i].length == n
\n\t1 <= m, n <= 200
\n\tgrid[i][j]
\u00a0\u662f\u00a00
\u00a0\u6216\u00a01
\u00a0\u4e4b\u4e00\u3002 \n
\n", "tags_en": ["Array", "Backtracking", "Matrix"], "tags_cn": ["\u6570\u7ec4", "\u56de\u6eaf", "\u77e9\u9635"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n int maximumInvitations(vector>& grid) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int maximumInvitations(int[][] grid) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def maximumInvitations(self, grid):\n \"\"\"\n :type grid: List[List[int]]\n :rtype: int\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def maximumInvitations(self, grid: List[List[int]]) -> int:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nint maximumInvitations(int** grid, int gridSize, int* gridColSize){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int MaximumInvitations(int[][] grid) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[][]} grid\n * @return {number}\n */\nvar maximumInvitations = function(grid) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer[][]} grid\n# @return {Integer}\ndef maximum_invitations(grid)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func maximumInvitations(_ grid: [[Int]]) -> Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func maximumInvitations(grid [][]int) int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def maximumInvitations(grid: Array[Array[Int]]): Int = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun maximumInvitations(grid: Array): Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn maximum_invitations(grid: Vec>) -> i32 {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer[][] $grid\n * @return Integer\n */\n function maximumInvitations($grid) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function maximumInvitations(grid: number[][]): number {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (maximum-invitations grid)\n (-> (listof (listof exact-integer?)) exact-integer?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec maximum_invitations(Grid :: [[integer()]]) -> integer().\nmaximum_invitations(Grid) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec maximum_invitations(grid :: [[integer]]) :: integer\n def maximum_invitations(grid) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1820](https://leetcode-cn.com/problems/maximum-number-of-accepted-invitations)", "[\u6700\u591a\u9080\u8bf7\u7684\u4e2a\u6570](/solution/1800-1899/1820.Maximum%20Number%20of%20Accepted%20Invitations/README.md)", "`\u6570\u7ec4`,`\u56de\u6eaf`,`\u77e9\u9635`", "\u4e2d\u7b49", "\ud83d\udd12"], "md_table_row_en": ["[1820](https://leetcode.com/problems/maximum-number-of-accepted-invitations)", "[Maximum Number of Accepted Invitations](/solution/1800-1899/1820.Maximum%20Number%20of%20Accepted%20Invitations/README_EN.md)", "`Array`,`Backtracking`,`Matrix`", "Medium", "\ud83d\udd12"]}, {"question_id": "1968", "frontend_question_id": "1840", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/maximum-building-height", "url_en": "https://leetcode.com/problems/maximum-building-height", "relative_path_cn": "/solution/1800-1899/1840.Maximum%20Building%20Height/README.md", "relative_path_en": "/solution/1800-1899/1840.Maximum%20Building%20Height/README_EN.md", "title_cn": "\u6700\u9ad8\u5efa\u7b51\u9ad8\u5ea6", "title_en": "Maximum Building Height", "question_title_slug": "maximum-building-height", "content_en": "You want to build n
new buildings in a city. The new buildings will be built in a line and are labeled from 1
to n
.
\n\nHowever, there are city restrictions on the heights of the new buildings:
\n\n\n\t- The height of each building must be a non-negative integer.
\n\t- The height of the first building must be
0
. \n\t- The height difference between any two adjacent buildings cannot exceed
1
. \n
\n\nAdditionally, there are city restrictions on the maximum height of specific buildings. These restrictions are given as a 2D integer array restrictions
where restrictions[i] = [idi, maxHeighti]
indicates that building idi
must have a height less than or equal to maxHeighti
.
\n\nIt is guaranteed that each building will appear at most once in restrictions
, and building 1
will not be in restrictions
.
\n\nReturn the maximum possible height of the tallest building.
\n\n
\nExample 1:
\n
\n\nInput: n = 5, restrictions = [[2,1],[4,1]]\nOutput: 2\nExplanation: The green area in the image indicates the maximum allowed height for each building.\nWe can build the buildings with heights [0,1,2,1,2], and the tallest building has a height of 2.
\n\nExample 2:
\n
\n\nInput: n = 6, restrictions = []\nOutput: 5\nExplanation: The green area in the image indicates the maximum allowed height for each building.\nWe can build the buildings with heights [0,1,2,3,4,5], and the tallest building has a height of 5.\n
\n\nExample 3:
\n
\n\nInput: n = 10, restrictions = [[5,3],[2,5],[7,4],[10,3]]\nOutput: 5\nExplanation: The green area in the image indicates the maximum allowed height for each building.\nWe can build the buildings with heights [0,1,2,3,3,4,4,5,4,3], and the tallest building has a height of 5.\n
\n\n
\nConstraints:
\n\n\n\t2 <= n <= 109
\n\t0 <= restrictions.length <= min(n - 1, 105)
\n\t2 <= idi <= n
\n\tidi
is unique. \n\t0 <= maxHeighti <= 109
\n
\n", "content_cn": "\u5728\u4e00\u5ea7\u57ce\u5e02\u91cc\uff0c\u4f60\u9700\u8981\u5efa\u00a0n
\u00a0\u680b\u65b0\u7684\u5efa\u7b51\u3002\u8fd9\u4e9b\u65b0\u7684\u5efa\u7b51\u4f1a\u4ece 1
\u00a0\u5230 n
\u00a0\u7f16\u53f7\u6392\u6210\u4e00\u5217\u3002
\n\n\u8fd9\u5ea7\u57ce\u5e02\u5bf9\u8fd9\u4e9b\u65b0\u5efa\u7b51\u6709\u4e00\u4e9b\u89c4\u5b9a\uff1a
\n\n\n\t- \u6bcf\u680b\u5efa\u7b51\u7684\u9ad8\u5ea6\u5fc5\u987b\u662f\u4e00\u4e2a\u975e\u8d1f\u6574\u6570\u3002
\n\t- \u7b2c\u4e00\u680b\u5efa\u7b51\u7684\u9ad8\u5ea6 \u5fc5\u987b\u00a0\u662f\u00a0
0
\u00a0\u3002 \n\t- \u4efb\u610f\u4e24\u680b\u76f8\u90bb\u5efa\u7b51\u7684\u9ad8\u5ea6\u5dee \u4e0d\u80fd\u8d85\u8fc7\u00a0\u00a0
1
\u00a0\u3002 \n
\n\n\u9664\u6b64\u4ee5\u5916\uff0c\u67d0\u4e9b\u5efa\u7b51\u8fd8\u6709\u989d\u5916\u7684\u6700\u9ad8\u9ad8\u5ea6\u9650\u5236\u3002\u8fd9\u4e9b\u9650\u5236\u4f1a\u4ee5\u4e8c\u7ef4\u6574\u6570\u6570\u7ec4\u00a0restrictions
\u00a0\u7684\u5f62\u5f0f\u7ed9\u51fa\uff0c\u5176\u4e2d\u00a0restrictions[i] = [idi, maxHeighti]
\u00a0\uff0c\u8868\u793a\u5efa\u7b51\u00a0idi
\u00a0\u7684\u9ad8\u5ea6 \u4e0d\u80fd\u8d85\u8fc7\u00a0maxHeighti
\u00a0\u3002
\n\n\u9898\u76ee\u4fdd\u8bc1\u6bcf\u680b\u5efa\u7b51\u5728 restrictions
\u00a0\u4e2d\u00a0\u81f3\u591a\u51fa\u73b0\u4e00\u6b21\u00a0\uff0c\u540c\u65f6\u5efa\u7b51 1
\u00a0\u4e0d\u4f1a\u00a0\u51fa\u73b0\u5728\u00a0restrictions
\u00a0\u4e2d\u3002
\n\n\u8bf7\u4f60\u8fd4\u56de \u6700\u9ad8\u00a0\u5efa\u7b51\u80fd\u8fbe\u5230\u7684 \u6700\u9ad8\u9ad8\u5ea6\u00a0\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n
\n\n\u8f93\u5165\uff1an = 5, restrictions = [[2,1],[4,1]]\n\u8f93\u51fa\uff1a2\n\u89e3\u91ca\uff1a\u4e0a\u56fe\u4e2d\u7684\u7eff\u8272\u533a\u57df\u4e3a\u6bcf\u680b\u5efa\u7b51\u88ab\u5141\u8bb8\u7684\u6700\u9ad8\u9ad8\u5ea6\u3002\n\u6211\u4eec\u53ef\u4ee5\u4f7f\u5efa\u7b51\u9ad8\u5ea6\u5206\u522b\u4e3a [0,1,2,1,2] \uff0c\u6700\u9ad8\u5efa\u7b51\u7684\u9ad8\u5ea6\u4e3a 2 \u3002
\n\n\u793a\u4f8b 2\uff1a
\n
\n\n\u8f93\u5165\uff1an = 6, restrictions = []\n\u8f93\u51fa\uff1a5\n\u89e3\u91ca\uff1a\u4e0a\u56fe\u4e2d\u7684\u7eff\u8272\u533a\u57df\u4e3a\u6bcf\u680b\u5efa\u7b51\u88ab\u5141\u8bb8\u7684\u6700\u9ad8\u9ad8\u5ea6\u3002\n\u6211\u4eec\u53ef\u4ee5\u4f7f\u5efa\u7b51\u9ad8\u5ea6\u5206\u522b\u4e3a [0,1,2,3,4,5] \uff0c\u6700\u9ad8\u5efa\u7b51\u7684\u9ad8\u5ea6\u4e3a 5 \u3002\n
\n\n\u793a\u4f8b 3\uff1a
\n
\n\n\u8f93\u5165\uff1an = 10, restrictions = [[5,3],[2,5],[7,4],[10,3]]\n\u8f93\u51fa\uff1a5\n\u89e3\u91ca\uff1a\u4e0a\u56fe\u4e2d\u7684\u7eff\u8272\u533a\u57df\u4e3a\u6bcf\u680b\u5efa\u7b51\u88ab\u5141\u8bb8\u7684\u6700\u9ad8\u9ad8\u5ea6\u3002\n\u6211\u4eec\u53ef\u4ee5\u4f7f\u5efa\u7b51\u9ad8\u5ea6\u5206\u522b\u4e3a [0,1,2,3,3,4,4,5,4,3] \uff0c\u6700\u9ad8\u5efa\u7b51\u7684\u9ad8\u5ea6\u4e3a 5 \u3002\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\t2 <= n <= 109
\n\t0 <= restrictions.length <= min(n - 1, 105)
\n\t2 <= idi <= n
\n\tidi
\u00a0\u662f \u552f\u4e00\u7684\u00a0\u3002 \n\t0 <= maxHeighti <= 109
\n
\n", "tags_en": ["Array", "Math"], "tags_cn": ["\u6570\u7ec4", "\u6570\u5b66"], "difficulty_en": "Hard", "difficulty_cn": "\u56f0\u96be", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n int maxBuilding(int n, vector>& restrictions) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int maxBuilding(int n, int[][] restrictions) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def maxBuilding(self, n, restrictions):\n \"\"\"\n :type n: int\n :type restrictions: List[List[int]]\n :rtype: int\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def maxBuilding(self, n: int, restrictions: List[List[int]]) -> int:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nint maxBuilding(int n, int** restrictions, int restrictionsSize, int* restrictionsColSize){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int MaxBuilding(int n, int[][] restrictions) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number} n\n * @param {number[][]} restrictions\n * @return {number}\n */\nvar maxBuilding = function(n, restrictions) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer} n\n# @param {Integer[][]} restrictions\n# @return {Integer}\ndef max_building(n, restrictions)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func maxBuilding(_ n: Int, _ restrictions: [[Int]]) -> Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func maxBuilding(n int, restrictions [][]int) int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def maxBuilding(n: Int, restrictions: Array[Array[Int]]): Int = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun maxBuilding(n: Int, restrictions: Array): Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn max_building(n: i32, restrictions: Vec>) -> i32 {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer $n\n * @param Integer[][] $restrictions\n * @return Integer\n */\n function maxBuilding($n, $restrictions) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function maxBuilding(n: number, restrictions: number[][]): number {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (max-building n restrictions)\n (-> exact-integer? (listof (listof exact-integer?)) exact-integer?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec max_building(N :: integer(), Restrictions :: [[integer()]]) -> integer().\nmax_building(N, Restrictions) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec max_building(n :: integer, restrictions :: [[integer]]) :: integer\n def max_building(n, restrictions) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1840](https://leetcode-cn.com/problems/maximum-building-height)", "[\u6700\u9ad8\u5efa\u7b51\u9ad8\u5ea6](/solution/1800-1899/1840.Maximum%20Building%20Height/README.md)", "`\u6570\u7ec4`,`\u6570\u5b66`", "\u56f0\u96be", ""], "md_table_row_en": ["[1840](https://leetcode.com/problems/maximum-building-height)", "[Maximum Building Height](/solution/1800-1899/1840.Maximum%20Building%20Height/README_EN.md)", "`Array`,`Math`", "Hard", ""]}, {"question_id": "1967", "frontend_question_id": "1839", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/longest-substring-of-all-vowels-in-order", "url_en": "https://leetcode.com/problems/longest-substring-of-all-vowels-in-order", "relative_path_cn": "/solution/1800-1899/1839.Longest%20Substring%20Of%20All%20Vowels%20in%20Order/README.md", "relative_path_en": "/solution/1800-1899/1839.Longest%20Substring%20Of%20All%20Vowels%20in%20Order/README_EN.md", "title_cn": "\u6240\u6709\u5143\u97f3\u6309\u987a\u5e8f\u6392\u5e03\u7684\u6700\u957f\u5b50\u5b57\u7b26\u4e32", "title_en": "Longest Substring Of All Vowels in Order", "question_title_slug": "longest-substring-of-all-vowels-in-order", "content_en": "A string is considered beautiful if it satisfies the following conditions:
\n\n\n\t- Each of the 5 English vowels (
'a'
, 'e'
, 'i'
, 'o'
, 'u'
) must appear at least once in it. \n\t- The letters must be sorted in alphabetical order (i.e. all
'a'
s before 'e'
s, all 'e'
s before 'i'
s, etc.). \n
\n\nFor example, strings "aeiou"
and "aaaaaaeiiiioou"
are considered beautiful, but "uaeio"
, "aeoiu"
, and "aaaeeeooo"
are not beautiful.
\n\nGiven a string word
consisting of English vowels, return the length of the longest beautiful substring of word
. If no such substring exists, return 0
.
\n\nA substring is a contiguous sequence of characters in a string.
\n\n
\nExample 1:
\n\n\nInput: word = "aeiaaioaaaaeiiiiouuuooaauuaeiu"\nOutput: 13\nExplanation: The longest beautiful substring in word is "aaaaeiiiiouuu" of length 13.
\n\nExample 2:
\n\n\nInput: word = "aeeeiiiioooauuuaeiou"\nOutput: 5\nExplanation: The longest beautiful substring in word is "aeiou" of length 5.\n
\n\nExample 3:
\n\n\nInput: word = "a"\nOutput: 0\nExplanation: There is no beautiful substring, so return 0.\n
\n\n
\nConstraints:
\n\n\n\t1 <= word.length <= 5 * 105
\n\tword
consists of characters 'a'
, 'e'
, 'i'
, 'o'
, and 'u'
. \n
\n", "content_cn": "\u5f53\u4e00\u4e2a\u5b57\u7b26\u4e32\u6ee1\u8db3\u5982\u4e0b\u6761\u4ef6\u65f6\uff0c\u6211\u4eec\u79f0\u5b83\u662f \u7f8e\u4e3d\u7684\u00a0\uff1a
\n\n\n\t- \u6240\u6709 5 \u4e2a\u82f1\u6587\u5143\u97f3\u5b57\u6bcd\uff08
'a'
\u00a0\uff0c'e'
\u00a0\uff0c'i'
\u00a0\uff0c'o'
\u00a0\uff0c'u'
\uff09\u90fd\u5fc5\u987b\u00a0\u81f3\u5c11\u00a0\u51fa\u73b0\u4e00\u6b21\u3002 \n\t- \u8fd9\u4e9b\u5143\u97f3\u5b57\u6bcd\u7684\u987a\u5e8f\u90fd\u5fc5\u987b\u6309\u7167 \u5b57\u5178\u5e8f\u00a0\u5347\u5e8f\u6392\u5e03\uff08\u4e5f\u5c31\u662f\u8bf4\u6240\u6709\u7684
'a'
\u00a0\u90fd\u5728 'e'
\u00a0\u524d\u9762\uff0c\u6240\u6709\u7684 'e'
\u00a0\u90fd\u5728 'i'
\u00a0\u524d\u9762\uff0c\u4ee5\u6b64\u7c7b\u63a8\uff09 \n
\n\n\u6bd4\u65b9\u8bf4\uff0c\u5b57\u7b26\u4e32\u00a0\"aeiou\"
\u548c\u00a0\"aaaaaaeiiiioou\"
\u00a0\u90fd\u662f \u7f8e\u4e3d\u7684\u00a0\uff0c\u4f46\u662f\u00a0\"uaeio\"
\u00a0\uff0c\"aeoiu\"
\u00a0\u548c\u00a0\"aaaeeeooo\"
\u00a0\u4e0d\u662f\u7f8e\u4e3d\u7684\u00a0\u3002
\n\n\u7ed9\u4f60\u4e00\u4e2a\u53ea\u5305\u542b\u82f1\u6587\u5143\u97f3\u5b57\u6bcd\u7684\u5b57\u7b26\u4e32\u00a0word
\u00a0\uff0c\u8bf7\u4f60\u8fd4\u56de\u00a0word
\u4e2d \u6700\u957f\u7f8e\u4e3d\u5b50\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u00a0\u3002\u5982\u679c\u4e0d\u5b58\u5728\u8fd9\u6837\u7684\u5b50\u5b57\u7b26\u4e32\uff0c\u8bf7\u8fd4\u56de 0
\u00a0\u3002
\n\n\u5b50\u5b57\u7b26\u4e32 \u662f\u5b57\u7b26\u4e32\u4e2d\u4e00\u4e2a\u8fde\u7eed\u7684\u5b57\u7b26\u5e8f\u5217\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1aword = \"aeiaaioaaaaeiiiiouuuooaauuaeiu\"\n\u8f93\u51fa\uff1a13\n\u89e3\u91ca\uff1a\u6700\u957f\u5b50\u5b57\u7b26\u4e32\u662f \"aaaaeiiiiouuu\" \uff0c\u957f\u5ea6\u4e3a 13 \u3002
\n\n\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1aword = \"aeeeiiiioooauuuaeiou\"\n\u8f93\u51fa\uff1a5\n\u89e3\u91ca\uff1a\u6700\u957f\u5b50\u5b57\u7b26\u4e32\u662f \"aeiou\" \uff0c\u957f\u5ea6\u4e3a 5 \u3002\n
\n\n\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1aword = \"a\"\n\u8f93\u51fa\uff1a0\n\u89e3\u91ca\uff1a\u6ca1\u6709\u7f8e\u4e3d\u5b50\u5b57\u7b26\u4e32\uff0c\u6240\u4ee5\u8fd4\u56de 0 \u3002\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\t1 <= word.length <= 5 * 105
\n\tword
\u00a0\u53ea\u5305\u542b\u5b57\u7b26\u00a0'a'
\uff0c'e'
\uff0c'i'
\uff0c'o'
\u00a0\u548c\u00a0'u'
\u00a0\u3002 \n
\n", "tags_en": ["String", "Sliding Window"], "tags_cn": ["\u5b57\u7b26\u4e32", "\u6ed1\u52a8\u7a97\u53e3"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n int longestBeautifulSubstring(string word) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int longestBeautifulSubstring(String word) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def longestBeautifulSubstring(self, word):\n \"\"\"\n :type word: str\n :rtype: int\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def longestBeautifulSubstring(self, word: str) -> int:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nint longestBeautifulSubstring(char * word){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int LongestBeautifulSubstring(string word) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {string} word\n * @return {number}\n */\nvar longestBeautifulSubstring = function(word) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {String} word\n# @return {Integer}\ndef longest_beautiful_substring(word)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func longestBeautifulSubstring(_ word: String) -> Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func longestBeautifulSubstring(word string) int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def longestBeautifulSubstring(word: String): Int = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun longestBeautifulSubstring(word: String): Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn longest_beautiful_substring(word: String) -> i32 {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param String $word\n * @return Integer\n */\n function longestBeautifulSubstring($word) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function longestBeautifulSubstring(word: string): number {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (longest-beautiful-substring word)\n (-> string? exact-integer?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec longest_beautiful_substring(Word :: unicode:unicode_binary()) -> integer().\nlongest_beautiful_substring(Word) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec longest_beautiful_substring(word :: String.t) :: integer\n def longest_beautiful_substring(word) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1839](https://leetcode-cn.com/problems/longest-substring-of-all-vowels-in-order)", "[\u6240\u6709\u5143\u97f3\u6309\u987a\u5e8f\u6392\u5e03\u7684\u6700\u957f\u5b50\u5b57\u7b26\u4e32](/solution/1800-1899/1839.Longest%20Substring%20Of%20All%20Vowels%20in%20Order/README.md)", "`\u5b57\u7b26\u4e32`,`\u6ed1\u52a8\u7a97\u53e3`", "\u4e2d\u7b49", ""], "md_table_row_en": ["[1839](https://leetcode.com/problems/longest-substring-of-all-vowels-in-order)", "[Longest Substring Of All Vowels in Order](/solution/1800-1899/1839.Longest%20Substring%20Of%20All%20Vowels%20in%20Order/README_EN.md)", "`String`,`Sliding Window`", "Medium", ""]}, {"question_id": "1966", "frontend_question_id": "1838", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/frequency-of-the-most-frequent-element", "url_en": "https://leetcode.com/problems/frequency-of-the-most-frequent-element", "relative_path_cn": "/solution/1800-1899/1838.Frequency%20of%20the%20Most%20Frequent%20Element/README.md", "relative_path_en": "/solution/1800-1899/1838.Frequency%20of%20the%20Most%20Frequent%20Element/README_EN.md", "title_cn": "\u6700\u9ad8\u9891\u5143\u7d20\u7684\u9891\u6570", "title_en": "Frequency of the Most Frequent Element", "question_title_slug": "frequency-of-the-most-frequent-element", "content_en": "The frequency of an element is the number of times it occurs in an array.
\n\nYou are given an integer array nums
and an integer k
. In one operation, you can choose an index of nums
and increment the element at that index by 1
.
\n\nReturn the maximum possible frequency of an element after performing at most k
operations.
\n\n
\nExample 1:
\n\n\nInput: nums = [1,2,4], k = 5\nOutput: 3\nExplanation: Increment the first element three times and the second element two times to make nums = [4,4,4].\n4 has a frequency of 3.
\n\nExample 2:
\n\n\nInput: nums = [1,4,8,13], k = 5\nOutput: 2\nExplanation: There are multiple optimal solutions:\n- Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2.\n- Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2.\n- Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2.\n
\n\nExample 3:
\n\n\nInput: nums = [3,9,6], k = 2\nOutput: 1\n
\n\n
\nConstraints:
\n\n\n\t1 <= nums.length <= 105
\n\t1 <= nums[i] <= 105
\n\t1 <= k <= 105
\n
\n", "content_cn": "\u5143\u7d20\u7684 \u9891\u6570 \u662f\u8be5\u5143\u7d20\u5728\u4e00\u4e2a\u6570\u7ec4\u4e2d\u51fa\u73b0\u7684\u6b21\u6570\u3002
\n\n\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 nums
\u548c\u4e00\u4e2a\u6574\u6570 k
\u3002\u5728\u4e00\u6b65\u64cd\u4f5c\u4e2d\uff0c\u4f60\u53ef\u4ee5\u9009\u62e9 nums
\u7684\u4e00\u4e2a\u4e0b\u6807\uff0c\u5e76\u5c06\u8be5\u4e0b\u6807\u5bf9\u5e94\u5143\u7d20\u7684\u503c\u589e\u52a0 1
\u3002
\n\n\u6267\u884c\u6700\u591a k
\u6b21\u64cd\u4f5c\u540e\uff0c\u8fd4\u56de\u6570\u7ec4\u4e2d\u6700\u9ad8\u9891\u5143\u7d20\u7684 \u6700\u5927\u53ef\u80fd\u9891\u6570 \u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1anums = [1,2,4], k = 5\n\u8f93\u51fa\uff1a3\n\u89e3\u91ca\uff1a\u5bf9\u7b2c\u4e00\u4e2a\u5143\u7d20\u6267\u884c 3 \u6b21\u9012\u589e\u64cd\u4f5c\uff0c\u5bf9\u7b2c\u4e8c\u4e2a\u5143\u7d20\u6267 2 \u6b21\u9012\u589e\u64cd\u4f5c\uff0c\u6b64\u65f6 nums = [4,4,4] \u3002\n4 \u662f\u6570\u7ec4\u4e2d\u6700\u9ad8\u9891\u5143\u7d20\uff0c\u9891\u6570\u662f 3 \u3002
\n\n\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1anums = [1,4,8,13], k = 5\n\u8f93\u51fa\uff1a2\n\u89e3\u91ca\uff1a\u5b58\u5728\u591a\u79cd\u6700\u4f18\u89e3\u51b3\u65b9\u6848\uff1a\n- \u5bf9\u7b2c\u4e00\u4e2a\u5143\u7d20\u6267\u884c 3 \u6b21\u9012\u589e\u64cd\u4f5c\uff0c\u6b64\u65f6 nums = [4,4,8,13] \u30024 \u662f\u6570\u7ec4\u4e2d\u6700\u9ad8\u9891\u5143\u7d20\uff0c\u9891\u6570\u662f 2 \u3002\n- \u5bf9\u7b2c\u4e8c\u4e2a\u5143\u7d20\u6267\u884c 4 \u6b21\u9012\u589e\u64cd\u4f5c\uff0c\u6b64\u65f6 nums = [1,8,8,13] \u30028 \u662f\u6570\u7ec4\u4e2d\u6700\u9ad8\u9891\u5143\u7d20\uff0c\u9891\u6570\u662f 2 \u3002\n- \u5bf9\u7b2c\u4e09\u4e2a\u5143\u7d20\u6267\u884c 5 \u6b21\u9012\u589e\u64cd\u4f5c\uff0c\u6b64\u65f6 nums = [1,4,13,13] \u300213 \u662f\u6570\u7ec4\u4e2d\u6700\u9ad8\u9891\u5143\u7d20\uff0c\u9891\u6570\u662f 2 \u3002\n
\n\n\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1anums = [3,9,6], k = 2\n\u8f93\u51fa\uff1a1\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\t1 <= nums.length <= 105
\n\t1 <= nums[i] <= 105
\n\t1 <= k <= 105
\n
\n", "tags_en": ["Array", "Binary Search", "Prefix Sum", "Sliding Window"], "tags_cn": ["\u6570\u7ec4", "\u4e8c\u5206\u67e5\u627e", "\u524d\u7f00\u548c", "\u6ed1\u52a8\u7a97\u53e3"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n int maxFrequency(vector& nums, int k) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int maxFrequency(int[] nums, int k) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def maxFrequency(self, nums, k):\n \"\"\"\n :type nums: List[int]\n :type k: int\n :rtype: int\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def maxFrequency(self, nums: List[int], k: int) -> int:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nint maxFrequency(int* nums, int numsSize, int k){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int MaxFrequency(int[] nums, int k) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[]} nums\n * @param {number} k\n * @return {number}\n */\nvar maxFrequency = function(nums, k) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer[]} nums\n# @param {Integer} k\n# @return {Integer}\ndef max_frequency(nums, k)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func maxFrequency(_ nums: [Int], _ k: Int) -> Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func maxFrequency(nums []int, k int) int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def maxFrequency(nums: Array[Int], k: Int): Int = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun maxFrequency(nums: IntArray, k: Int): Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn max_frequency(nums: Vec, k: i32) -> i32 {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @param Integer $k\n * @return Integer\n */\n function maxFrequency($nums, $k) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function maxFrequency(nums: number[], k: number): number {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (max-frequency nums k)\n (-> (listof exact-integer?) exact-integer? exact-integer?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec max_frequency(Nums :: [integer()], K :: integer()) -> integer().\nmax_frequency(Nums, K) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec max_frequency(nums :: [integer], k :: integer) :: integer\n def max_frequency(nums, k) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1838](https://leetcode-cn.com/problems/frequency-of-the-most-frequent-element)", "[\u6700\u9ad8\u9891\u5143\u7d20\u7684\u9891\u6570](/solution/1800-1899/1838.Frequency%20of%20the%20Most%20Frequent%20Element/README.md)", "`\u6570\u7ec4`,`\u4e8c\u5206\u67e5\u627e`,`\u524d\u7f00\u548c`,`\u6ed1\u52a8\u7a97\u53e3`", "\u4e2d\u7b49", ""], "md_table_row_en": ["[1838](https://leetcode.com/problems/frequency-of-the-most-frequent-element)", "[Frequency of the Most Frequent Element](/solution/1800-1899/1838.Frequency%20of%20the%20Most%20Frequent%20Element/README_EN.md)", "`Array`,`Binary Search`,`Prefix Sum`,`Sliding Window`", "Medium", ""]}, {"question_id": "1965", "frontend_question_id": "1837", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/sum-of-digits-in-base-k", "url_en": "https://leetcode.com/problems/sum-of-digits-in-base-k", "relative_path_cn": "/solution/1800-1899/1837.Sum%20of%20Digits%20in%20Base%20K/README.md", "relative_path_en": "/solution/1800-1899/1837.Sum%20of%20Digits%20in%20Base%20K/README_EN.md", "title_cn": "K \u8fdb\u5236\u8868\u793a\u4e0b\u7684\u5404\u4f4d\u6570\u5b57\u603b\u548c", "title_en": "Sum of Digits in Base K", "question_title_slug": "sum-of-digits-in-base-k", "content_en": "Given an integer n
(in base 10
) and a base k
, return the sum of the digits of n
after converting n
from base 10
to base k
.
\n\nAfter converting, each digit should be interpreted as a base 10
number, and the sum should be returned in base 10
.
\n\n
\nExample 1:
\n\n\nInput: n = 34, k = 6\nOutput: 9\nExplanation: 34 (base 10) expressed in base 6 is 54. 5 + 4 = 9.\n
\n\nExample 2:
\n\n\nInput: n = 10, k = 10\nOutput: 1\nExplanation: n is already in base 10. 1 + 0 = 1.\n
\n\n
\nConstraints:
\n\n\n\t1 <= n <= 100
\n\t2 <= k <= 10
\n
\n", "content_cn": "\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570 n
\uff0810
\u8fdb\u5236\uff09\u548c\u4e00\u4e2a\u57fa\u6570 k
\uff0c\u8bf7\u4f60\u5c06 n
\u4ece 10
\u8fdb\u5236\u8868\u793a\u8f6c\u6362\u4e3a k
\u8fdb\u5236\u8868\u793a\uff0c\u8ba1\u7b97\u5e76\u8fd4\u56de\u8f6c\u6362\u540e\u5404\u4f4d\u6570\u5b57\u7684 \u603b\u548c \u3002
\n\n\u8f6c\u6362\u540e\uff0c\u5404\u4f4d\u6570\u5b57\u5e94\u5f53\u89c6\u4f5c\u662f 10
\u8fdb\u5236\u6570\u5b57\uff0c\u4e14\u5b83\u4eec\u7684\u603b\u548c\u4e5f\u5e94\u5f53\u6309 10
\u8fdb\u5236\u8868\u793a\u8fd4\u56de\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1an = 34, k = 6\n\u8f93\u51fa\uff1a9\n\u89e3\u91ca\uff1a34 (10 \u8fdb\u5236) \u5728 6 \u8fdb\u5236\u4e0b\u8868\u793a\u4e3a 54 \u30025 + 4 = 9 \u3002\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1an = 10, k = 10\n\u8f93\u51fa\uff1a1\n\u89e3\u91ca\uff1an \u672c\u8eab\u5c31\u662f 10 \u8fdb\u5236\u3002 1 + 0 = 1 \u3002\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\t1 <= n <= 100
\n\t2 <= k <= 10
\n
\n", "tags_en": ["Math"], "tags_cn": ["\u6570\u5b66"], "difficulty_en": "Easy", "difficulty_cn": "\u7b80\u5355", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n int sumBase(int n, int k) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int sumBase(int n, int k) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def sumBase(self, n, k):\n \"\"\"\n :type n: int\n :type k: int\n :rtype: int\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def sumBase(self, n: int, k: int) -> int:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nint sumBase(int n, int k){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int SumBase(int n, int k) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number} n\n * @param {number} k\n * @return {number}\n */\nvar sumBase = function(n, k) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer} n\n# @param {Integer} k\n# @return {Integer}\ndef sum_base(n, k)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func sumBase(_ n: Int, _ k: Int) -> Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func sumBase(n int, k int) int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def sumBase(n: Int, k: Int): Int = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun sumBase(n: Int, k: Int): Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn sum_base(n: i32, k: i32) -> i32 {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer $n\n * @param Integer $k\n * @return Integer\n */\n function sumBase($n, $k) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function sumBase(n: number, k: number): number {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (sum-base n k)\n (-> exact-integer? exact-integer? exact-integer?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec sum_base(N :: integer(), K :: integer()) -> integer().\nsum_base(N, K) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec sum_base(n :: integer, k :: integer) :: integer\n def sum_base(n, k) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1837](https://leetcode-cn.com/problems/sum-of-digits-in-base-k)", "[K \u8fdb\u5236\u8868\u793a\u4e0b\u7684\u5404\u4f4d\u6570\u5b57\u603b\u548c](/solution/1800-1899/1837.Sum%20of%20Digits%20in%20Base%20K/README.md)", "`\u6570\u5b66`", "\u7b80\u5355", ""], "md_table_row_en": ["[1837](https://leetcode.com/problems/sum-of-digits-in-base-k)", "[Sum of Digits in Base K](/solution/1800-1899/1837.Sum%20of%20Digits%20in%20Base%20K/README_EN.md)", "`Math`", "Easy", ""]}, {"question_id": "1964", "frontend_question_id": "1811", "paid_only": true, "paid_only_cn": true, "category": "Database", "url_cn": "https://leetcode-cn.com/problems/find-interview-candidates", "url_en": "https://leetcode.com/problems/find-interview-candidates", "relative_path_cn": "/solution/1800-1899/1811.Find%20Interview%20Candidates/README.md", "relative_path_en": "/solution/1800-1899/1811.Find%20Interview%20Candidates/README_EN.md", "title_cn": "\u5bfb\u627e\u9762\u8bd5\u5019\u9009\u4eba", "title_en": "Find Interview Candidates", "question_title_slug": "find-interview-candidates", "content_en": "Table: Contests
\n\n\n+--------------+------+\n| Column Name | Type |\n+--------------+------+\n| contest_id | int |\n| gold_medal | int |\n| silver_medal | int |\n| bronze_medal | int |\n+--------------+------+\ncontest_id is the primary key for this table.\nThis table contains the LeetCode contest ID and the user IDs of the gold, silver, and bronze medalists.\nIt is guaranteed that any consecutive contests have consecutive IDs and that no ID is skipped.
\n\n
\n\nTable: Users
\n\n\n+-------------+---------+\n| Column Name | Type |\n+-------------+---------+\n| user_id | int |\n| mail | varchar |\n| name | varchar |\n+-------------+---------+\nuser_id is the primary key for this table.\nThis table contains information about the users.\n
\n\n
\n\nWrite an SQL query to report the name
and the mail
of all interview candidates. A user is an interview candidate if at least one of these two conditions is true:
\n\n\n\t- The user won any medal in three or more consecutive contests.
\n\t- The user won the gold medal in three or more different contests (not necessarily consecutive).
\n
\n\nReturn the result table in any order.
\n\nThe query result format is in the following example:
\n\n
\n\n\nContests table:\n+------------+------------+--------------+--------------+\n| contest_id | gold_medal | silver_medal | bronze_medal |\n+------------+------------+--------------+--------------+\n| 190 | 1 | 5 | 2 |\n| 191 | 2 | 3 | 5 |\n| 192 | 5 | 2 | 3 |\n| 193 | 1 | 3 | 5 |\n| 194 | 4 | 5 | 2 |\n| 195 | 4 | 2 | 1 |\n| 196 | 1 | 5 | 2 |\n+------------+------------+--------------+--------------+\n\nUsers table:\n+---------+--------------------+-------+\n| user_id | mail | name |\n+---------+--------------------+-------+\n| 1 | sarah@leetcode.com | Sarah |\n| 2 | bob@leetcode.com | Bob |\n| 3 | alice@leetcode.com | Alice |\n| 4 | hercy@leetcode.com | Hercy |\n| 5 | quarz@leetcode.com | Quarz |\n+---------+--------------------+-------+\n\nResult table:\n+-------+--------------------+\n| name | mail |\n+-------+--------------------+\n| Sarah | sarah@leetcode.com |\n| Bob | bob@leetcode.com |\n| Alice | alice@leetcode.com |\n| Quarz | quarz@leetcode.com |\n+-------+--------------------+\n\nSarah won 3 gold medals (190, 193, and 196), so we include her in the result table.\nBob won a medal in 3 consecutive contests (190, 191, and 192), so we include him in the result table.\n - Note that he also won a medal in 3 other consecutive contests (194, 195, and 196).\nAlice won a medal in 3 consecutive contests (191, 192, and 193), so we include her in the result table.\nQuarz won a medal in 5 consecutive contests (190, 191, 192, 193, and 194), so we include them in the result table.\n
\n\n
\nFollow up:
\n\n\n\t- What if the first condition changed to be "any medal in
n
or more consecutive contests"? How would you change your solution to get the interview candidates? Imagine that n
is the parameter of a stored procedure. \n\t- Some users may not participate in every contest but still perform well in the ones they do. How would you change your solution to only consider contests where the user was a participant? Suppose the registered users for each contest are given in another table.
\n
\n", "content_cn": "\u8868: Contests
\n\n\n+--------------+------+\n| Column Name | Type |\n+--------------+------+\n| contest_id | int |\n| gold_medal | int |\n| silver_medal | int |\n| bronze_medal | int |\n+--------------+------+\ncontest_id \u662f\u8be5\u8868\u7684\u4e3b\u952e.\n\u8be5\u8868\u5305\u542bLeetCode\u7ade\u8d5b\u7684ID\u548c\u8be5\u573a\u6bd4\u8d5b\u4e2d\u91d1\u724c\u3001\u94f6\u724c\u3001\u94dc\u724c\u7684\u7528\u6237id\u3002\n\u53ef\u4ee5\u4fdd\u8bc1\uff0c\u6240\u6709\u8fde\u7eed\u7684\u6bd4\u8d5b\u90fd\u6709\u8fde\u7eed\u7684ID\uff0c\u6ca1\u6709ID\u88ab\u8df3\u8fc7\u3002\n\n
\n\nTable: Users
\n\n\n+-------------+---------+\n| Column Name | Type |\n+-------------+---------+\n| user_id | int |\n| mail | varchar |\n| name | varchar |\n+-------------+---------+\nuser_id \u662f\u8be5\u8868\u7684\u4e3b\u952e.\n\u8be5\u8868\u5305\u542b\u7528\u6237\u4fe1\u606f\u3002
\n\n\u00a0
\n\n\u7f16\u5199 SQL \u8bed\u53e5\u6765\u8fd4\u56de\u9762\u8bd5\u5019\u9009\u4eba\u7684 \u59d3\u540d
\u548c\u00a0\u90ae\u4ef6
.\u5f53\u7528\u6237\u6ee1\u8db3\u4ee5\u4e0b\u4e24\u4e2a\u8981\u6c42\u4e2d\u7684\u4efb\u610f\u4e00\u6761\uff0c\u5176\u6210\u4e3a\u9762\u8bd5\u5019\u9009\u4eba:
\n\n\n\t- \u8be5\u7528\u6237\u5728\u8fde\u7eed\u4e09\u573a\u53ca\u66f4\u591a\u6bd4\u8d5b\u4e2d\u8d62\u5f97\u5956\u724c\u3002
\n\t- \u8be5\u7528\u6237\u5728\u4e09\u573a\u53ca\u66f4\u591a\u4e0d\u540c\u7684\u6bd4\u8d5b\u4e2d\u8d62\u5f97\u91d1\u724c\uff08\u8fd9\u4e9b\u6bd4\u8d5b\u53ef\u4ee5\u4e0d\u662f\u8fde\u7eed\u7684\uff09
\n
\n\n\u53ef\u4ee5\u4ee5\u4efb\u4f55\u987a\u5e8f\u8fd4\u56de\u7ed3\u679c\u3002
\n\n\u67e5\u8be2\u7ed3\u679c\u683c\u5f0f\u5982\u4e0b\u4f8b\u6240\u793a\uff1a
\n\n\nContests\u8868:\n+------------+------------+--------------+--------------+\n| contest_id | gold_medal | silver_medal | bronze_medal |\n+------------+------------+--------------+--------------+\n| 190 | 1 | 5 | 2 |\n| 191 | 2 | 3 | 5 |\n| 192 | 5 | 2 | 3 |\n| 193 | 1 | 3 | 5 |\n| 194 | 4 | 5 | 2 |\n| 195 | 4 | 2 | 1 |\n| 196 | 1 | 5 | 2 |\n+------------+------------+--------------+--------------+\n\nUsers\u8868:\n+---------+--------------------+-------+\n| user_id | mail | name |\n+---------+--------------------+-------+\n| 1 | sarah@leetcode.com | Sarah |\n| 2 | bob@leetcode.com | Bob |\n| 3 | alice@leetcode.com | Alice |\n| 4 | hercy@leetcode.com | Hercy |\n| 5 | quarz@leetcode.com | Quarz |\n+---------+--------------------+-------+\n\n\u7ed3\u679c\u8868:\n+-------+--------------------+\n| name | mail |\n+-------+--------------------+\n| Sarah | sarah@leetcode.com |\n| Bob | bob@leetcode.com |\n| Alice | alice@leetcode.com |\n| Quarz | quarz@leetcode.com |\n+-------+--------------------+\n\nSarah \u8d62\u5f97\u4e863\u5757\u91d1\u724c (190, 193, and 196),\u6240\u4ee5\u6211\u4eec\u5c06\u5979\u5217\u5165\u7ed3\u679c\u8868\u3002\nBob\u5728\u8fde\u7eed3\u573a\u7ade\u8d5b\u4e2d\u8d62\u5f97\u4e86\u5956\u724c(190, 191, and 192), \u6240\u4ee5\u6211\u4eec\u5c06\u4ed6\u5217\u5165\u7ed3\u679c\u8868\u3002\n - \u6ce8\u610f\u4ed6\u5728\u53e6\u5916\u7684\u8fde\u7eed3\u573a\u7ade\u8d5b\u4e2d\u4e5f\u8d62\u5f97\u4e86\u5956\u724c(194, 195, and 196).\nAlice\u5728\u8fde\u7eed3\u573a\u7ade\u8d5b\u4e2d\u8d62\u5f97\u4e86\u5956\u724c (191, 192, and 193), \u6240\u4ee5\u6211\u4eec\u5c06\u5979\u5217\u5165\u7ed3\u679c\u8868\u3002\nQuarz\u5728\u8fde\u7eed5\u573a\u7ade\u8d5b\u4e2d\u8d62\u5f97\u4e86\u5956\u724c(190, 191, 192, 193, and 194), \u6240\u4ee5\u6211\u4eec\u5c06\u4ed6\u5217\u5165\u7ed3\u679c\u8868\u3002\n
\n\n\u00a0
\n\n\u8fdb\u9636\uff1a
\n\n\n\t- \u5982\u679c\u7b2c\u4e00\u4e2a\u6761\u4ef6\u53d8\u6210\u201c\u8be5\u7528\u6237\u5728\u8fde\u7eedn\u573a\u53ca\u6bd4\u8d5b\u4e2d\u8d62\u5f97\u4efb\u610f\u5956\u724c\u3002\u201d\u5462\uff1f\u4f60\u5982\u4f55\u66f4\u6539\u4f60\u7684\u89e3\u6cd5\uff0c\u6765\u9009\u51fa\u9762\u8bd5\u5019\u9009\u4eba\uff1f\u53ef\u4ee5\u628an\u60f3\u8c61\u6210\u5b58\u50a8\u8fc7\u7a0b\u4e2d\u7684\u53c2\u6570\u3002
\n\t- \u6709\u7684\u7528\u6237\u53ef\u80fd\u6ca1\u6709\u53c2\u52a0\u6bcf\u4e00\u573a\u7ade\u8d5b\uff0c\u4f46\u662f\u5728\u53c2\u52a0\u7684\u6bcf\u4e00\u573a\u7ade\u8d5b\u4e2d\u90fd\u8868\u73b0\u5f97\u4e0d\u9519\u3002\u4f60\u5982\u4f55\u66f4\u6539\u4f60\u7684\u89e3\u6cd5\uff0c\u4ee5\u8fbe\u5230\u53ea\u8003\u8651\u90a3\u4e9b\u7528\u6237\u53c2\u4e0e\u4e86\u7684\u6bd4\u8d5b\uff1f\u53ef\u5047\u8bbe\u53e6\u4e00\u5f20\u8868\u7ed9\u51fa\u4e86\u6bcf\u573a\u6bd4\u8d5b\u7684\u6ce8\u518c\u7528\u6237\u4fe1\u606f\u3002
\n
\n", "tags_en": ["Database"], "tags_cn": ["\u6570\u636e\u5e93"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "MySQL", "langSlug": "mysql", "code": "# Write your MySQL query statement below", "__typename": "CodeSnippetNode"}, {"lang": "MS SQL Server", "langSlug": "mssql", "code": "/* Write your T-SQL query statement below */", "__typename": "CodeSnippetNode"}, {"lang": "Oracle", "langSlug": "oraclesql", "code": "/* Write your PL/SQL query statement below */", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1811](https://leetcode-cn.com/problems/find-interview-candidates)", "[\u5bfb\u627e\u9762\u8bd5\u5019\u9009\u4eba](/solution/1800-1899/1811.Find%20Interview%20Candidates/README.md)", "`\u6570\u636e\u5e93`", "\u4e2d\u7b49", "\ud83d\udd12"], "md_table_row_en": ["[1811](https://leetcode.com/problems/find-interview-candidates)", "[Find Interview Candidates](/solution/1800-1899/1811.Find%20Interview%20Candidates/README_EN.md)", "`Database`", "Medium", "\ud83d\udd12"]}, {"question_id": "1963", "frontend_question_id": "1835", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/find-xor-sum-of-all-pairs-bitwise-and", "url_en": "https://leetcode.com/problems/find-xor-sum-of-all-pairs-bitwise-and", "relative_path_cn": "/solution/1800-1899/1835.Find%20XOR%20Sum%20of%20All%20Pairs%20Bitwise%20AND/README.md", "relative_path_en": "/solution/1800-1899/1835.Find%20XOR%20Sum%20of%20All%20Pairs%20Bitwise%20AND/README_EN.md", "title_cn": "\u6240\u6709\u6570\u5bf9\u6309\u4f4d\u4e0e\u7ed3\u679c\u7684\u5f02\u6216\u548c", "title_en": "Find XOR Sum of All Pairs Bitwise AND", "question_title_slug": "find-xor-sum-of-all-pairs-bitwise-and", "content_en": "The XOR sum of a list is the bitwise XOR
of all its elements. If the list only contains one element, then its XOR sum will be equal to this element.
\n\n\n\t- For example, the XOR sum of
[1,2,3,4]
is equal to 1 XOR 2 XOR 3 XOR 4 = 4
, and the XOR sum of [3]
is equal to 3
. \n
\n\nYou are given two 0-indexed arrays arr1
and arr2
that consist only of non-negative integers.
\n\nConsider the list containing the result of arr1[i] AND arr2[j]
(bitwise AND
) for every (i, j)
pair where 0 <= i < arr1.length
and 0 <= j < arr2.length
.
\n\nReturn the XOR sum of the aforementioned list.
\n\n
\nExample 1:
\n\n\nInput: arr1 = [1,2,3], arr2 = [6,5]\nOutput: 0\nExplanation: The list = [1 AND 6, 1 AND 5, 2 AND 6, 2 AND 5, 3 AND 6, 3 AND 5] = [0,1,2,0,2,1].\nThe XOR sum = 0 XOR 1 XOR 2 XOR 0 XOR 2 XOR 1 = 0.\n
\n\nExample 2:
\n\n\nInput: arr1 = [12], arr2 = [4]\nOutput: 4\nExplanation: The list = [12 AND 4] = [4]. The XOR sum = 4.\n
\n\n
\nConstraints:
\n\n\n\t1 <= arr1.length, arr2.length <= 105
\n\t0 <= arr1[i], arr2[j] <= 109
\n
\n", "content_cn": "\u5217\u8868\u7684 \u5f02\u6216\u548c\uff08XOR sum\uff09\u6307\u5bf9\u6240\u6709\u5143\u7d20\u8fdb\u884c\u6309\u4f4d XOR
\u8fd0\u7b97\u7684\u7ed3\u679c\u3002\u5982\u679c\u5217\u8868\u4e2d\u4ec5\u6709\u4e00\u4e2a\u5143\u7d20\uff0c\u90a3\u4e48\u5176 \u5f02\u6216\u548c \u5c31\u7b49\u4e8e\u8be5\u5143\u7d20\u3002
\n\n\n\t- \u4f8b\u5982\uff0c
[1,2,3,4]
\u7684 \u5f02\u6216\u548c \u7b49\u4e8e 1 XOR 2 XOR 3 XOR 4 = 4
\uff0c\u800c [3]
\u7684 \u5f02\u6216\u548c \u7b49\u4e8e 3
\u3002 \n
\n\n\u7ed9\u4f60\u4e24\u4e2a\u4e0b\u6807 \u4ece 0 \u5f00\u59cb \u8ba1\u6570\u7684\u6570\u7ec4 arr1
\u548c arr2
\uff0c\u4e24\u6570\u7ec4\u5747\u7531\u975e\u8d1f\u6574\u6570\u7ec4\u6210\u3002
\n\n\u6839\u636e\u6bcf\u4e2a\u00a0(i, j)
\u6570\u5bf9\uff0c\u6784\u9020\u4e00\u4e2a\u7531 arr1[i] AND arr2[j]
\uff08\u6309\u4f4d AND
\u8fd0\u7b97\uff09\u7ed3\u679c\u7ec4\u6210\u7684\u5217\u8868\u3002\u5176\u4e2d 0 <= i < arr1.length
\u4e14 0 <= j < arr2.length
\u3002
\n\n\u8fd4\u56de\u4e0a\u8ff0\u5217\u8868\u7684 \u5f02\u6216\u548c \u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1aarr1 = [1,2,3], arr2 = [6,5]\n\u8f93\u51fa\uff1a0\n\u89e3\u91ca\uff1a\u5217\u8868 = [1 AND 6, 1 AND 5, 2 AND 6, 2 AND 5, 3 AND 6, 3 AND 5] = [0,1,2,0,2,1] \uff0c\n\u5f02\u6216\u548c = 0 XOR 1 XOR 2 XOR 0 XOR 2 XOR 1 = 0 \u3002
\n\n\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1aarr1 = [12], arr2 = [4]\n\u8f93\u51fa\uff1a4\n\u89e3\u91ca\uff1a\u5217\u8868 = [12 AND 4] = [4] \uff0c\u5f02\u6216\u548c = 4 \u3002\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\t1 <= arr1.length, arr2.length <= 105
\n\t0 <= arr1[i], arr2[j] <= 109
\n
\n", "tags_en": ["Bit Manipulation", "Array", "Math"], "tags_cn": ["\u4f4d\u8fd0\u7b97", "\u6570\u7ec4", "\u6570\u5b66"], "difficulty_en": "Hard", "difficulty_cn": "\u56f0\u96be", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n int getXORSum(vector& arr1, vector& arr2) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int getXORSum(int[] arr1, int[] arr2) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def getXORSum(self, arr1, arr2):\n \"\"\"\n :type arr1: List[int]\n :type arr2: List[int]\n :rtype: int\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def getXORSum(self, arr1: List[int], arr2: List[int]) -> int:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nint getXORSum(int* arr1, int arr1Size, int* arr2, int arr2Size){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int GetXORSum(int[] arr1, int[] arr2) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[]} arr1\n * @param {number[]} arr2\n * @return {number}\n */\nvar getXORSum = function(arr1, arr2) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer[]} arr1\n# @param {Integer[]} arr2\n# @return {Integer}\ndef get_xor_sum(arr1, arr2)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func getXORSum(_ arr1: [Int], _ arr2: [Int]) -> Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func getXORSum(arr1 []int, arr2 []int) int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def getXORSum(arr1: Array[Int], arr2: Array[Int]): Int = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun getXORSum(arr1: IntArray, arr2: IntArray): Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn get_xor_sum(arr1: Vec, arr2: Vec) -> i32 {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer[] $arr1\n * @param Integer[] $arr2\n * @return Integer\n */\n function getXORSum($arr1, $arr2) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function getXORSum(arr1: number[], arr2: number[]): number {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (get-xor-sum arr1 arr2)\n (-> (listof exact-integer?) (listof exact-integer?) exact-integer?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec get_xor_sum(Arr1 :: [integer()], Arr2 :: [integer()]) -> integer().\nget_xor_sum(Arr1, Arr2) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec get_xor_sum(arr1 :: [integer], arr2 :: [integer]) :: integer\n def get_xor_sum(arr1, arr2) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1835](https://leetcode-cn.com/problems/find-xor-sum-of-all-pairs-bitwise-and)", "[\u6240\u6709\u6570\u5bf9\u6309\u4f4d\u4e0e\u7ed3\u679c\u7684\u5f02\u6216\u548c](/solution/1800-1899/1835.Find%20XOR%20Sum%20of%20All%20Pairs%20Bitwise%20AND/README.md)", "`\u4f4d\u8fd0\u7b97`,`\u6570\u7ec4`,`\u6570\u5b66`", "\u56f0\u96be", ""], "md_table_row_en": ["[1835](https://leetcode.com/problems/find-xor-sum-of-all-pairs-bitwise-and)", "[Find XOR Sum of All Pairs Bitwise AND](/solution/1800-1899/1835.Find%20XOR%20Sum%20of%20All%20Pairs%20Bitwise%20AND/README_EN.md)", "`Bit Manipulation`,`Array`,`Math`", "Hard", ""]}, {"question_id": "1962", "frontend_question_id": "1834", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/single-threaded-cpu", "url_en": "https://leetcode.com/problems/single-threaded-cpu", "relative_path_cn": "/solution/1800-1899/1834.Single-Threaded%20CPU/README.md", "relative_path_en": "/solution/1800-1899/1834.Single-Threaded%20CPU/README_EN.md", "title_cn": "\u5355\u7ebf\u7a0b CPU", "title_en": "Single-Threaded CPU", "question_title_slug": "single-threaded-cpu", "content_en": "You are given n
\u200b\u200b\u200b\u200b\u200b\u200b tasks labeled from 0
to n - 1
represented by a 2D integer array tasks
, where tasks[i] = [enqueueTimei, processingTimei]
means that the i\u200b\u200b\u200b\u200b\u200b\u200bth
\u200b\u200b\u200b\u200b task will be available to process at enqueueTimei
and will take processingTimei
to finish processing.
\n\nYou have a single-threaded CPU that can process at most one task at a time and will act in the following way:
\n\n\n\t- If the CPU is idle and there are no available tasks to process, the CPU remains idle.
\n\t- If the CPU is idle and there are available tasks, the CPU will choose the one with the shortest processing time. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.
\n\t- Once a task is started, the CPU will process the entire task without stopping.
\n\t- The CPU can finish a task then start a new one instantly.
\n
\n\nReturn the order in which the CPU will process the tasks.
\n\n
\nExample 1:
\n\n\nInput: tasks = [[1,2],[2,4],[3,2],[4,1]]\nOutput: [0,2,3,1]\nExplanation: The events go as follows: \n- At time = 1, task 0 is available to process. Available tasks = {0}.\n- Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}.\n- At time = 2, task 1 is available to process. Available tasks = {1}.\n- At time = 3, task 2 is available to process. Available tasks = {1, 2}.\n- Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}.\n- At time = 4, task 3 is available to process. Available tasks = {1, 3}.\n- At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}.\n- At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}.\n- At time = 10, the CPU finishes task 1 and becomes idle.\n
\n\nExample 2:
\n\n\nInput: tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]]\nOutput: [4,3,2,0,1]\nExplanation: The events go as follows:\n- At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}.\n- Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}.\n- At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}.\n- At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}.\n- At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}.\n- At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}.\n- At time = 40, the CPU finishes task 1 and becomes idle.\n
\n\n
\nConstraints:
\n\n\n\ttasks.length == n
\n\t1 <= n <= 105
\n\t1 <= enqueueTimei, processingTimei <= 109
\n
\n", "content_cn": "\u7ed9\u4f60\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4 tasks
\uff0c\u7528\u4e8e\u8868\u793a n
\u200b\u200b\u200b\u200b\u200b\u200b \u9879\u4ece 0
\u5230 n - 1
\u7f16\u53f7\u7684\u4efb\u52a1\u3002\u5176\u4e2d tasks[i] = [enqueueTimei, processingTimei]
\u610f\u5473\u7740\u7b2c i\u200b\u200b\u200b\u200b\u200b\u200b
\u200b\u200b\u200b\u200b \u9879\u4efb\u52a1\u5c06\u4f1a\u4e8e enqueueTimei
\u65f6\u8fdb\u5165\u4efb\u52a1\u961f\u5217\uff0c\u9700\u8981 processingTimei
\u7684\u65f6\u957f\u5b8c\u6210\u6267\u884c\u3002
\n\n\u73b0\u6709\u4e00\u4e2a\u5355\u7ebf\u7a0b CPU \uff0c\u540c\u4e00\u65f6\u95f4\u53ea\u80fd\u6267\u884c \u6700\u591a\u4e00\u9879 \u4efb\u52a1\uff0c\u8be5 CPU \u5c06\u4f1a\u6309\u7167\u4e0b\u8ff0\u65b9\u5f0f\u8fd0\u884c\uff1a
\n\n\n\t- \u5982\u679c CPU \u7a7a\u95f2\uff0c\u4e14\u4efb\u52a1\u961f\u5217\u4e2d\u6ca1\u6709\u9700\u8981\u6267\u884c\u7684\u4efb\u52a1\uff0c\u5219 CPU \u4fdd\u6301\u7a7a\u95f2\u72b6\u6001\u3002
\n\t- \u5982\u679c CPU \u7a7a\u95f2\uff0c\u4f46\u4efb\u52a1\u961f\u5217\u4e2d\u6709\u9700\u8981\u6267\u884c\u7684\u4efb\u52a1\uff0c\u5219 CPU \u5c06\u4f1a\u9009\u62e9 \u6267\u884c\u65f6\u95f4\u6700\u77ed \u7684\u4efb\u52a1\u5f00\u59cb\u6267\u884c\u3002\u5982\u679c\u591a\u4e2a\u4efb\u52a1\u5177\u6709\u540c\u6837\u7684\u6700\u77ed\u6267\u884c\u65f6\u95f4\uff0c\u5219\u9009\u62e9\u4e0b\u6807\u6700\u5c0f\u7684\u4efb\u52a1\u5f00\u59cb\u6267\u884c\u3002
\n\t- \u4e00\u65e6\u67d0\u9879\u4efb\u52a1\u5f00\u59cb\u6267\u884c\uff0cCPU \u5728 \u6267\u884c\u5b8c\u6574\u4e2a\u4efb\u52a1 \u524d\u90fd\u4e0d\u4f1a\u505c\u6b62\u3002
\n\t- CPU \u53ef\u4ee5\u5728\u5b8c\u6210\u4e00\u9879\u4efb\u52a1\u540e\uff0c\u7acb\u5373\u5f00\u59cb\u6267\u884c\u4e00\u9879\u65b0\u4efb\u52a1\u3002
\n
\n\n\u8fd4\u56de CPU \u5904\u7406\u4efb\u52a1\u7684\u987a\u5e8f\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1atasks = [[1,2],[2,4],[3,2],[4,1]]\n\u8f93\u51fa\uff1a[0,2,3,1]\n\u89e3\u91ca\uff1a\u4e8b\u4ef6\u6309\u4e0b\u8ff0\u6d41\u7a0b\u8fd0\u884c\uff1a \n- time = 1 \uff0c\u4efb\u52a1 0 \u8fdb\u5165\u4efb\u52a1\u961f\u5217\uff0c\u53ef\u6267\u884c\u4efb\u52a1\u9879 = {0}\n- \u540c\u6837\u5728 time = 1 \uff0c\u7a7a\u95f2\u72b6\u6001\u7684 CPU \u5f00\u59cb\u6267\u884c\u4efb\u52a1 0 \uff0c\u53ef\u6267\u884c\u4efb\u52a1\u9879 = {}\n- time = 2 \uff0c\u4efb\u52a1 1 \u8fdb\u5165\u4efb\u52a1\u961f\u5217\uff0c\u53ef\u6267\u884c\u4efb\u52a1\u9879 = {1}\n- time = 3 \uff0c\u4efb\u52a1 2 \u8fdb\u5165\u4efb\u52a1\u961f\u5217\uff0c\u53ef\u6267\u884c\u4efb\u52a1\u9879 = {1, 2}\n- \u540c\u6837\u5728 time = 3 \uff0cCPU \u5b8c\u6210\u4efb\u52a1 0 \u5e76\u5f00\u59cb\u6267\u884c\u961f\u5217\u4e2d\u7528\u65f6\u6700\u77ed\u7684\u4efb\u52a1 2 \uff0c\u53ef\u6267\u884c\u4efb\u52a1\u9879 = {1}\n- time = 4 \uff0c\u4efb\u52a1 3 \u8fdb\u5165\u4efb\u52a1\u961f\u5217\uff0c\u53ef\u6267\u884c\u4efb\u52a1\u9879 = {1, 3}\n- time = 5 \uff0cCPU \u5b8c\u6210\u4efb\u52a1 2 \u5e76\u5f00\u59cb\u6267\u884c\u961f\u5217\u4e2d\u7528\u65f6\u6700\u77ed\u7684\u4efb\u52a1 3 \uff0c\u53ef\u6267\u884c\u4efb\u52a1\u9879 = {1}\n- time = 6 \uff0cCPU \u5b8c\u6210\u4efb\u52a1 3 \u5e76\u5f00\u59cb\u6267\u884c\u4efb\u52a1 1 \uff0c\u53ef\u6267\u884c\u4efb\u52a1\u9879 = {}\n- time = 10 \uff0cCPU \u5b8c\u6210\u4efb\u52a1 1 \u5e76\u8fdb\u5165\u7a7a\u95f2\u72b6\u6001\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1atasks = [[7,10],[7,12],[7,5],[7,4],[7,2]]\n\u8f93\u51fa\uff1a[4,3,2,0,1]\n\u89e3\u91ca\uff1a\u4e8b\u4ef6\u6309\u4e0b\u8ff0\u6d41\u7a0b\u8fd0\u884c\uff1a \n- time = 7 \uff0c\u6240\u6709\u4efb\u52a1\u540c\u65f6\u8fdb\u5165\u4efb\u52a1\u961f\u5217\uff0c\u53ef\u6267\u884c\u4efb\u52a1\u9879 = {0,1,2,3,4}\n- \u540c\u6837\u5728 time = 7 \uff0c\u7a7a\u95f2\u72b6\u6001\u7684 CPU \u5f00\u59cb\u6267\u884c\u4efb\u52a1 4 \uff0c\u53ef\u6267\u884c\u4efb\u52a1\u9879 = {0,1,2,3}\n- time = 9 \uff0cCPU \u5b8c\u6210\u4efb\u52a1 4 \u5e76\u5f00\u59cb\u6267\u884c\u4efb\u52a1 3 \uff0c\u53ef\u6267\u884c\u4efb\u52a1\u9879 = {0,1,2}\n- time = 13 \uff0cCPU \u5b8c\u6210\u4efb\u52a1 3 \u5e76\u5f00\u59cb\u6267\u884c\u4efb\u52a1 2 \uff0c\u53ef\u6267\u884c\u4efb\u52a1\u9879 = {0,1}\n- time = 18 \uff0cCPU \u5b8c\u6210\u4efb\u52a1 2 \u5e76\u5f00\u59cb\u6267\u884c\u4efb\u52a1 0 \uff0c\u53ef\u6267\u884c\u4efb\u52a1\u9879 = {1}\n- time = 28 \uff0cCPU \u5b8c\u6210\u4efb\u52a1 0 \u5e76\u5f00\u59cb\u6267\u884c\u4efb\u52a1 1 \uff0c\u53ef\u6267\u884c\u4efb\u52a1\u9879 = {}\n- time = 40 \uff0cCPU \u5b8c\u6210\u4efb\u52a1 1 \u5e76\u8fdb\u5165\u7a7a\u95f2\u72b6\u6001
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\ttasks.length == n
\n\t1 <= n <= 105
\n\t1 <= enqueueTimei, processingTimei <= 109
\n
\n", "tags_en": ["Array", "Sorting", "Heap (Priority Queue)"], "tags_cn": ["\u6570\u7ec4", "\u6392\u5e8f", "\u5806\uff08\u4f18\u5148\u961f\u5217\uff09"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n vector getOrder(vector>& tasks) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int[] getOrder(int[][] tasks) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def getOrder(self, tasks):\n \"\"\"\n :type tasks: List[List[int]]\n :rtype: List[int]\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def getOrder(self, tasks: List[List[int]]) -> List[int]:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\n/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\nint* getOrder(int** tasks, int tasksSize, int* tasksColSize, int* returnSize){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int[] GetOrder(int[][] tasks) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[][]} tasks\n * @return {number[]}\n */\nvar getOrder = function(tasks) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer[][]} tasks\n# @return {Integer[]}\ndef get_order(tasks)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func getOrder(_ tasks: [[Int]]) -> [Int] {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func getOrder(tasks [][]int) []int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def getOrder(tasks: Array[Array[Int]]): Array[Int] = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun getOrder(tasks: Array): IntArray {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn get_order(tasks: Vec>) -> Vec {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer[][] $tasks\n * @return Integer[]\n */\n function getOrder($tasks) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function getOrder(tasks: number[][]): number[] {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (get-order tasks)\n (-> (listof (listof exact-integer?)) (listof exact-integer?))\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec get_order(Tasks :: [[integer()]]) -> [integer()].\nget_order(Tasks) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec get_order(tasks :: [[integer]]) :: [integer]\n def get_order(tasks) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1834](https://leetcode-cn.com/problems/single-threaded-cpu)", "[\u5355\u7ebf\u7a0b CPU](/solution/1800-1899/1834.Single-Threaded%20CPU/README.md)", "`\u6570\u7ec4`,`\u6392\u5e8f`,`\u5806\uff08\u4f18\u5148\u961f\u5217\uff09`", "\u4e2d\u7b49", ""], "md_table_row_en": ["[1834](https://leetcode.com/problems/single-threaded-cpu)", "[Single-Threaded CPU](/solution/1800-1899/1834.Single-Threaded%20CPU/README_EN.md)", "`Array`,`Sorting`,`Heap (Priority Queue)`", "Medium", ""]}, {"question_id": "1961", "frontend_question_id": "1833", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/maximum-ice-cream-bars", "url_en": "https://leetcode.com/problems/maximum-ice-cream-bars", "relative_path_cn": "/solution/1800-1899/1833.Maximum%20Ice%20Cream%20Bars/README.md", "relative_path_en": "/solution/1800-1899/1833.Maximum%20Ice%20Cream%20Bars/README_EN.md", "title_cn": "\u96ea\u7cd5\u7684\u6700\u5927\u6570\u91cf", "title_en": "Maximum Ice Cream Bars", "question_title_slug": "maximum-ice-cream-bars", "content_en": "It is a sweltering summer day, and a boy wants to buy some ice cream bars.
\r\n\r\nAt the store, there are n
ice cream bars. You are given an array costs
of length n
, where costs[i]
is the price of the ith
ice cream bar in coins. The boy initially has coins
coins to spend, and he wants to buy as many ice cream bars as possible.
\r\n\r\nReturn the maximum number of ice cream bars the boy can buy with coins
coins.
\r\n\r\nNote: The boy can buy the ice cream bars in any order.
\r\n\r\n
\r\nExample 1:
\r\n\r\n\r\nInput: costs = [1,3,2,4,1], coins = 7\r\nOutput: 4\r\nExplanation: The boy can buy ice cream bars at indices 0,1,2,4 for a total price of 1 + 3 + 2 + 1 = 7.\r\n
\r\n\r\nExample 2:
\r\n\r\n\r\nInput: costs = [10,6,8,7,7,8], coins = 5\r\nOutput: 0\r\nExplanation: The boy cannot afford any of the ice cream bars.\r\n
\r\n\r\nExample 3:
\r\n\r\n\r\nInput: costs = [1,6,3,1,2,5], coins = 20\r\nOutput: 6\r\nExplanation: The boy can buy all the ice cream bars for a total price of 1 + 6 + 3 + 1 + 2 + 5 = 18.\r\n
\r\n\r\n
\r\nConstraints:
\r\n\r\n\r\n\tcosts.length == n
\r\n\t1 <= n <= 105
\r\n\t1 <= costs[i] <= 105
\r\n\t1 <= coins <= 108
\r\n
", "content_cn": "\u590f\u65e5\u708e\u708e\uff0c\u5c0f\u7537\u5b69 Tony \u60f3\u4e70\u4e00\u4e9b\u96ea\u7cd5\u6d88\u6d88\u6691\u3002
\n\n\u5546\u5e97\u4e2d\u65b0\u5230 n
\u652f\u96ea\u7cd5\uff0c\u7528\u957f\u5ea6\u4e3a n
\u7684\u6570\u7ec4 costs
\u8868\u793a\u96ea\u7cd5\u7684\u5b9a\u4ef7\uff0c\u5176\u4e2d costs[i]
\u8868\u793a\u7b2c i
\u652f\u96ea\u7cd5\u7684\u73b0\u91d1\u4ef7\u683c\u3002Tony \u4e00\u5171\u6709 coins
\u73b0\u91d1\u53ef\u4ee5\u7528\u4e8e\u6d88\u8d39\uff0c\u4ed6\u60f3\u8981\u4e70\u5c3d\u53ef\u80fd\u591a\u7684\u96ea\u7cd5\u3002
\n\n\u7ed9\u4f60\u4ef7\u683c\u6570\u7ec4 costs
\u548c\u73b0\u91d1\u91cf coins
\uff0c\u8bf7\u4f60\u8ba1\u7b97\u5e76\u8fd4\u56de Tony \u7528 coins
\u73b0\u91d1\u80fd\u591f\u4e70\u5230\u7684\u96ea\u7cd5\u7684 \u6700\u5927\u6570\u91cf \u3002
\n\n\u6ce8\u610f\uff1aTony \u53ef\u4ee5\u6309\u4efb\u610f\u987a\u5e8f\u8d2d\u4e70\u96ea\u7cd5\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1acosts = [1,3,2,4,1], coins = 7\n\u8f93\u51fa\uff1a4\n\u89e3\u91ca\uff1aTony \u53ef\u4ee5\u4e70\u4e0b\u6807\u4e3a 0\u30011\u30012\u30014 \u7684\u96ea\u7cd5\uff0c\u603b\u4ef7\u4e3a 1 + 3 + 2 + 1 = 7\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1acosts = [10,6,8,7,7,8], coins = 5\n\u8f93\u51fa\uff1a0\n\u89e3\u91ca\uff1aTony \u6ca1\u6709\u8db3\u591f\u7684\u94b1\u4e70\u4efb\u4f55\u4e00\u652f\u96ea\u7cd5\u3002\n
\n\n\u793a\u4f8b 3\uff1a
\n\n\u8f93\u5165\uff1acosts = [1,6,3,1,2,5], coins = 20\n\u8f93\u51fa\uff1a6\n\u89e3\u91ca\uff1aTony \u53ef\u4ee5\u4e70\u4e0b\u6240\u6709\u7684\u96ea\u7cd5\uff0c\u603b\u4ef7\u4e3a 1 + 6 + 3 + 1 + 2 + 5 = 18 \u3002\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\tcosts.length == n
\n\t1 <= n <= 105
\n\t1 <= costs[i] <= 105
\n\t1 <= coins <= 108
\n
\n", "tags_en": ["Greedy", "Array", "Sorting"], "tags_cn": ["\u8d2a\u5fc3", "\u6570\u7ec4", "\u6392\u5e8f"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n int maxIceCream(vector& costs, int coins) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int maxIceCream(int[] costs, int coins) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def maxIceCream(self, costs, coins):\n \"\"\"\n :type costs: List[int]\n :type coins: int\n :rtype: int\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def maxIceCream(self, costs: List[int], coins: int) -> int:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nint maxIceCream(int* costs, int costsSize, int coins){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int MaxIceCream(int[] costs, int coins) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[]} costs\n * @param {number} coins\n * @return {number}\n */\nvar maxIceCream = function(costs, coins) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer[]} costs\n# @param {Integer} coins\n# @return {Integer}\ndef max_ice_cream(costs, coins)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func maxIceCream(_ costs: [Int], _ coins: Int) -> Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func maxIceCream(costs []int, coins int) int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def maxIceCream(costs: Array[Int], coins: Int): Int = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun maxIceCream(costs: IntArray, coins: Int): Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn max_ice_cream(costs: Vec, coins: i32) -> i32 {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer[] $costs\n * @param Integer $coins\n * @return Integer\n */\n function maxIceCream($costs, $coins) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function maxIceCream(costs: number[], coins: number): number {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (max-ice-cream costs coins)\n (-> (listof exact-integer?) exact-integer? exact-integer?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec max_ice_cream(Costs :: [integer()], Coins :: integer()) -> integer().\nmax_ice_cream(Costs, Coins) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec max_ice_cream(costs :: [integer], coins :: integer) :: integer\n def max_ice_cream(costs, coins) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1833](https://leetcode-cn.com/problems/maximum-ice-cream-bars)", "[\u96ea\u7cd5\u7684\u6700\u5927\u6570\u91cf](/solution/1800-1899/1833.Maximum%20Ice%20Cream%20Bars/README.md)", "`\u8d2a\u5fc3`,`\u6570\u7ec4`,`\u6392\u5e8f`", "\u4e2d\u7b49", ""], "md_table_row_en": ["[1833](https://leetcode.com/problems/maximum-ice-cream-bars)", "[Maximum Ice Cream Bars](/solution/1800-1899/1833.Maximum%20Ice%20Cream%20Bars/README_EN.md)", "`Greedy`,`Array`,`Sorting`", "Medium", ""]}, {"question_id": "1960", "frontend_question_id": "1832", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/check-if-the-sentence-is-pangram", "url_en": "https://leetcode.com/problems/check-if-the-sentence-is-pangram", "relative_path_cn": "/solution/1800-1899/1832.Check%20if%20the%20Sentence%20Is%20Pangram/README.md", "relative_path_en": "/solution/1800-1899/1832.Check%20if%20the%20Sentence%20Is%20Pangram/README_EN.md", "title_cn": "\u5224\u65ad\u53e5\u5b50\u662f\u5426\u4e3a\u5168\u5b57\u6bcd\u53e5", "title_en": "Check if the Sentence Is Pangram", "question_title_slug": "check-if-the-sentence-is-pangram", "content_en": "A pangram is a sentence where every letter of the English alphabet appears at least once.
\n\nGiven a string sentence
containing only lowercase English letters, return true
if sentence
is a pangram, or false
otherwise.
\n\n
\nExample 1:
\n\n\nInput: sentence = "thequickbrownfoxjumpsoverthelazydog"\nOutput: true\nExplanation: sentence contains at least one of every letter of the English alphabet.\n
\n\nExample 2:
\n\n\nInput: sentence = "leetcode"\nOutput: false\n
\n\n
\nConstraints:
\n\n\n\t1 <= sentence.length <= 1000
\n\tsentence
consists of lowercase English letters. \n
\n", "content_cn": "\u5168\u5b57\u6bcd\u53e5 \u6307\u5305\u542b\u82f1\u8bed\u5b57\u6bcd\u8868\u4e2d\u6bcf\u4e2a\u5b57\u6bcd\u81f3\u5c11\u4e00\u6b21\u7684\u53e5\u5b50\u3002
\n\n\u7ed9\u4f60\u4e00\u4e2a\u4ec5\u7531\u5c0f\u5199\u82f1\u6587\u5b57\u6bcd\u7ec4\u6210\u7684\u5b57\u7b26\u4e32 sentence
\uff0c\u8bf7\u4f60\u5224\u65ad\u00a0sentence
\u662f\u5426\u4e3a \u5168\u5b57\u6bcd\u53e5 \u3002
\n\n\u5982\u679c\u662f\uff0c\u8fd4\u56de true
\uff1b\u5426\u5219\uff0c\u8fd4\u56de false
\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1asentence = \"thequickbrownfoxjumpsoverthelazydog\"\n\u8f93\u51fa\uff1atrue\n\u89e3\u91ca\uff1asentence
\u5305\u542b\u82f1\u8bed\u5b57\u6bcd\u8868\u4e2d\u6bcf\u4e2a\u5b57\u6bcd\u81f3\u5c11\u4e00\u6b21\u3002\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1asentence = \"leetcode\"\n\u8f93\u51fa\uff1afalse\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\t1 <= sentence.length <= 1000
\n\tsentence
\u7531\u5c0f\u5199\u82f1\u8bed\u5b57\u6bcd\u7ec4\u6210 \n
\n", "tags_en": ["Hash Table", "String"], "tags_cn": ["\u54c8\u5e0c\u8868", "\u5b57\u7b26\u4e32"], "difficulty_en": "Easy", "difficulty_cn": "\u7b80\u5355", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n bool checkIfPangram(string sentence) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public boolean checkIfPangram(String sentence) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def checkIfPangram(self, sentence):\n \"\"\"\n :type sentence: str\n :rtype: bool\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def checkIfPangram(self, sentence: str) -> bool:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nbool checkIfPangram(char * sentence){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public bool CheckIfPangram(string sentence) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {string} sentence\n * @return {boolean}\n */\nvar checkIfPangram = function(sentence) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {String} sentence\n# @return {Boolean}\ndef check_if_pangram(sentence)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func checkIfPangram(_ sentence: String) -> Bool {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func checkIfPangram(sentence string) bool {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def checkIfPangram(sentence: String): Boolean = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun checkIfPangram(sentence: String): Boolean {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn check_if_pangram(sentence: String) -> bool {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param String $sentence\n * @return Boolean\n */\n function checkIfPangram($sentence) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function checkIfPangram(sentence: string): boolean {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (check-if-pangram sentence)\n (-> string? boolean?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec check_if_pangram(Sentence :: unicode:unicode_binary()) -> boolean().\ncheck_if_pangram(Sentence) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec check_if_pangram(sentence :: String.t) :: boolean\n def check_if_pangram(sentence) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1832](https://leetcode-cn.com/problems/check-if-the-sentence-is-pangram)", "[\u5224\u65ad\u53e5\u5b50\u662f\u5426\u4e3a\u5168\u5b57\u6bcd\u53e5](/solution/1800-1899/1832.Check%20if%20the%20Sentence%20Is%20Pangram/README.md)", "`\u54c8\u5e0c\u8868`,`\u5b57\u7b26\u4e32`", "\u7b80\u5355", ""], "md_table_row_en": ["[1832](https://leetcode.com/problems/check-if-the-sentence-is-pangram)", "[Check if the Sentence Is Pangram](/solution/1800-1899/1832.Check%20if%20the%20Sentence%20Is%20Pangram/README_EN.md)", "`Hash Table`,`String`", "Easy", ""]}, {"question_id": "1959", "frontend_question_id": "1810", "paid_only": true, "paid_only_cn": true, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/minimum-path-cost-in-a-hidden-grid", "url_en": "https://leetcode.com/problems/minimum-path-cost-in-a-hidden-grid", "relative_path_cn": "/solution/1800-1899/1810.Minimum%20Path%20Cost%20in%20a%20Hidden%20Grid/README.md", "relative_path_en": "/solution/1800-1899/1810.Minimum%20Path%20Cost%20in%20a%20Hidden%20Grid/README_EN.md", "title_cn": "Minimum Path Cost in a Hidden Grid", "title_en": "Minimum Path Cost in a Hidden Grid", "question_title_slug": "minimum-path-cost-in-a-hidden-grid", "content_en": "This is an interactive problem.
\n\nThere is a robot in a hidden grid, and you are trying to get it from its starting cell to the target cell in this grid. The grid is of size m x n
, and each cell in the grid is either empty or blocked. It is guaranteed that the starting cell and the target cell are different, and neither of them is blocked.
\n\nEach cell has a cost that you need to pay each time you move to the cell. The starting cell's cost is not applied before the robot moves.
\n\nYou want to find the minimum total cost to move the robot to the target cell. However, you do not know the grid's dimensions, the starting cell, nor the target cell. You are only allowed to ask queries to the GridMaster
object.
\n\nThe GridMaster
class has the following functions:
\n\n\n\tboolean canMove(char direction)
Returns true
if the robot can move in that direction. Otherwise, it returns false
. \n\tint move(char direction)
Moves the robot in that direction and returns the cost of moving to that cell. If this move would move the robot to a blocked cell or off the grid, the move will be ignored, the robot will remain in the same position, and the function will return -1
. \n\tboolean isTarget()
Returns true
if the robot is currently on the target cell. Otherwise, it returns false
. \n
\n\nNote that direction
in the above functions should be a character from {'U','D','L','R'}
, representing the directions up, down, left, and right, respectively.
\n\nReturn the minimum total cost to get the robot from its initial starting cell to the target cell. If there is no valid path between the cells, return -1
.
\n\nCustom testing:
\n\nThe test input is read as a 2D matrix grid
of size m x n
and four integers r1
, c1
, r2
, and c2
where:
\n\n\n\tgrid[i][j] == 0
indicates that the cell (i, j)
is blocked. \n\tgrid[i][j] >= 1
indicates that the cell (i, j)
is empty and grid[i][j]
is the cost to move to that cell. \n\t(r1, c1)
is the starting cell of the robot. \n\t(r2, c2)
is the target cell of the robot. \n
\n\nRemember that you will not have this information in your code.
\n\n
\nExample 1:
\n\n\nInput: grid = [[2,3],[1,1]], r1 = 0, c1 = 1, r2 = 1, c2 = 0\nOutput: 2\nExplanation: One possible interaction is described below:\nThe robot is initially standing on cell (0, 1), denoted by the 3.\n- master.canMove('U') returns false.\n- master.canMove('D') returns true.\n- master.canMove('L') returns true.\n- master.canMove('R') returns false.\n- master.move('L') moves the robot to the cell (0, 0) and returns 2.\n- master.isTarget() returns false.\n- master.canMove('U') returns false.\n- master.canMove('D') returns true.\n- master.canMove('L') returns false.\n- master.canMove('R') returns true.\n- master.move('D') moves the robot to the cell (1, 0) and returns 1.\n- master.isTarget() returns true.\n- master.move('L') doesn't move the robot and returns -1.\n- master.move('R') moves the robot to the cell (1, 1) and returns 1.\nWe now know that the target is the cell (1, 0), and the minimum total cost to reach it is 2.
\n\nExample 2:
\n\n\nInput: grid = [[0,3,1],[3,4,2],[1,2,0]], r1 = 2, c1 = 0, r2 = 0, c2 = 2\nOutput: 9\nExplanation: The minimum cost path is (2,0) -> (2,1) -> (1,1) -> (1,2) -> (0,2).\n
\n\nExample 3:
\n\n\nInput: grid = [[1,0],[0,1]], r1 = 0, c1 = 0, r2 = 1, c2 = 1\nOutput: -1\nExplanation: There is no path from the robot to the target cell.\n
\n\n
\nConstraints:
\n\n\n\t1 <= n, m <= 100
\n\tm == grid.length
\n\tn == grid[i].length
\n\t0 <= grid[i][j] <= 100
\n
\n", "content_cn": "This is an interactive problem.
\n\nThere is a robot in a hidden grid, and you are trying to get it from its starting cell to the target cell in this grid. The grid is of size m x n
, and each cell in the grid is either empty or blocked. It is guaranteed that the starting cell and the target cell are different, and neither of them is blocked.
\n\nEach cell has a cost that you need to pay each time you move to the cell. The starting cell's cost is not applied before the robot moves.
\n\nYou want to find the minimum total cost to move the robot to the target cell. However, you do not know the grid's dimensions, the starting cell, nor the target cell. You are only allowed to ask queries to the GridMaster
object.
\n\nThe GridMaster
class has the following functions:
\n\n\n\tboolean canMove(char direction)
Returns true
if the robot can move in that direction. Otherwise, it returns false
. \n\tint move(char direction)
Moves the robot in that direction and returns the cost of moving to that cell. If this move would move the robot to a blocked cell or off the grid, the move will be ignored, the robot will remain in the same position, and the function will return -1
. \n\tboolean isTarget()
Returns true
if the robot is currently on the target cell. Otherwise, it returns false
. \n
\n\nNote that direction
in the above functions should be a character from {'U','D','L','R'}
, representing the directions up, down, left, and right, respectively.
\n\nReturn the minimum total cost to get the robot from its initial starting cell to the target cell. If there is no valid path between the cells, return -1
.
\n\nCustom testing:
\n\nThe test input is read as a 2D matrix grid
of size m x n
and four integers r1
, c1
, r2
, and c2
where:
\n\n\n\tgrid[i][j] == 0
indicates that the cell (i, j)
is blocked. \n\tgrid[i][j] >= 1
indicates that the cell (i, j)
is empty and grid[i][j]
is the cost to move to that cell. \n\t(r1, c1)
is the starting cell of the robot. \n\t(r2, c2)
is the target cell of the robot. \n
\n\nRemember that you will not have this information in your code.
\n\n
\nExample 1:
\n\n\nInput: grid = [[2,3],[1,1]], r1 = 0, c1 = 1, r2 = 1, c2 = 0\nOutput: 2\nExplanation: One possible interaction is described below:\nThe robot is initially standing on cell (0, 1), denoted by the 3.\n- master.canMove('U') returns false.\n- master.canMove('D') returns true.\n- master.canMove('L') returns true.\n- master.canMove('R') returns false.\n- master.move('L') moves the robot to the cell (0, 0) and returns 2.\n- master.isTarget() returns false.\n- master.canMove('U') returns false.\n- master.canMove('D') returns true.\n- master.canMove('L') returns false.\n- master.canMove('R') returns true.\n- master.move('D') moves the robot to the cell (1, 0) and returns 1.\n- master.isTarget() returns true.\n- master.move('L') doesn't move the robot and returns -1.\n- master.move('R') moves the robot to the cell (1, 1) and returns 1.\nWe now know that the target is the cell (1, 0), and the minimum total cost to reach it is 2.
\n\nExample 2:
\n\n\nInput: grid = [[0,3,1],[3,4,2],[1,2,0]], r1 = 2, c1 = 0, r2 = 0, c2 = 2\nOutput: 9\nExplanation: The minimum cost path is (2,0) -> (2,1) -> (1,1) -> (1,2) -> (0,2).\n
\n\nExample 3:
\n\n\nInput: grid = [[1,0],[0,1]], r1 = 0, c1 = 0, r2 = 1, c2 = 1\nOutput: -1\nExplanation: There is no path from the robot to the target cell.\n
\n\n
\nConstraints:
\n\n\n\t1 <= n, m <= 100
\n\tm == grid.length
\n\tn == grid[i].length
\n\t0 <= grid[i][j] <= 100
\n
\n", "tags_en": ["Depth-First Search", "Breadth-First Search", "Graph", "Interactive", "Heap (Priority Queue)"], "tags_cn": ["\u6df1\u5ea6\u4f18\u5148\u641c\u7d22", "\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22", "\u56fe", "\u4ea4\u4e92", "\u5806\uff08\u4f18\u5148\u961f\u5217\uff09"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "/**\n * // This is the GridMaster's API interface.\n * // You should not implement it, or speculate about its implementation\n * class GridMaster {\n * public:\n * bool canMove(char direction);\n * int move(char direction);\n * boolean isTarget();\n * };\n */\n\nclass Solution {\npublic:\n int findShortestPath(GridMaster &master) {\n \n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "/**\n * // This is the GridMaster's API interface.\n * // You should not implement it, or speculate about its implementation\n * class GridMaster {\n * boolean canMove(char direction);\n * int move(char direction);\n * boolean isTarget();\n * }\n */\n\nclass Solution {\n public int findShortestPath(GridMaster master) {\n \n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "# \"\"\"\n# This is GridMaster's API interface.\n# You should not implement it, or speculate about it's implementation\n# \"\"\"\n#class GridMaster(object):\n# def canMove(self, direction):\n# \"\"\"\n# :type direction: str\n# :rtype bool\n# \"\"\"\n#\n# def move(self, direction):\n# \"\"\"\n# :type direction: str\n#. :rtype int\n# \"\"\"\n#\n# def isTarget(self):\n# \"\"\"\n# :rtype bool\n# \"\"\"\n#\n\nclass Solution(object):\n def findShortestPath(self, master):\n \"\"\"\n :type master: GridMaster\n :rtype: int\n \"\"\"\n ", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "# \"\"\"\n# This is GridMaster's API interface.\n# You should not implement it, or speculate about its implementation\n# \"\"\"\n#class GridMaster(object):\n# def canMove(self, direction: str) -> bool:\n# \n#\n# def move(self, direction: str) -> int:\n# \n#\n# def isTarget(self) -> None:\n# \n#\n\nclass Solution(object):\n def findShortestPath(self, master: 'GridMaster') -> int:\n ", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "/**\n * // This is the GridMaster's API interface.\n * // You should not implement it, or speculate about its implementation\n * class GridMaster {\n * bool canMove(char direction);\n * int move(char direction);\n * bool isTarget();\n * };\n */\n\nclass Solution {\n public int FindShortestPath(GridMaster master) {\n \n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * // This is the GridMaster's API interface.\n * // You should not implement it, or speculate about its implementation\n * function GridMaster() {\n *\n * @param {character} direction\n * @return {boolean}\n * this.canMove = function(direction) {\n * ...\n * };\n * @param {character} direction\n * @return {integer}\n * this.move = function(direction) {\n * ...\n * };\n * @return {boolean}\n * this.isTarget = function() {\n * ...\n * };\n * };\n */\n\n/**\n * @param {GridMaster} master\n * @return {integer}\n */\nvar findShortestPath = function(master) {\n \n};", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1810](https://leetcode-cn.com/problems/minimum-path-cost-in-a-hidden-grid)", "[Minimum Path Cost in a Hidden Grid](/solution/1800-1899/1810.Minimum%20Path%20Cost%20in%20a%20Hidden%20Grid/README.md)", "`\u6df1\u5ea6\u4f18\u5148\u641c\u7d22`,`\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22`,`\u56fe`,`\u4ea4\u4e92`,`\u5806\uff08\u4f18\u5148\u961f\u5217\uff09`", "\u4e2d\u7b49", "\ud83d\udd12"], "md_table_row_en": ["[1810](https://leetcode.com/problems/minimum-path-cost-in-a-hidden-grid)", "[Minimum Path Cost in a Hidden Grid](/solution/1800-1899/1810.Minimum%20Path%20Cost%20in%20a%20Hidden%20Grid/README_EN.md)", "`Depth-First Search`,`Breadth-First Search`,`Graph`,`Interactive`,`Heap (Priority Queue)`", "Medium", "\ud83d\udd12"]}, {"question_id": "1958", "frontend_question_id": "1809", "paid_only": true, "paid_only_cn": true, "category": "Database", "url_cn": "https://leetcode-cn.com/problems/ad-free-sessions", "url_en": "https://leetcode.com/problems/ad-free-sessions", "relative_path_cn": "/solution/1800-1899/1809.Ad-Free%20Sessions/README.md", "relative_path_en": "/solution/1800-1899/1809.Ad-Free%20Sessions/README_EN.md", "title_cn": "\u6ca1\u6709\u5e7f\u544a\u7684\u5267\u96c6", "title_en": "Ad-Free Sessions", "question_title_slug": "ad-free-sessions", "content_en": "Table: Playback
\n\n\n+-------------+------+\n| Column Name | Type |\n+-------------+------+\n| session_id | int |\n| customer_id | int |\n| start_time | int |\n| end_time | int |\n+-------------+------+\nsession_id is the primary key for this table.\ncustomer_id is the ID of the customer watching this session.\nThe session runs during the inclusive interval between start_time and end_time.\nIt is guaranteed that start_time <= end_time and that two sessions for the same customer do not intersect.
\n\n
\n\nTable: Ads
\n\n\n+-------------+------+\n| Column Name | Type |\n+-------------+------+\n| ad_id | int |\n| customer_id | int |\n| timestamp | int |\n+-------------+------+\nad_id is the primary key for this table.\ncustomer_id is the ID of the customer viewing this ad.\ntimestamp is the moment of time at which the ad was shown.\n
\n\n
\n\nWrite an SQL query to report all the sessions that did not get shown any ads.
\n\nReturn the result table in any order.
\n\nThe query result format is in the following example:
\n\n
\n\n\nPlayback table:\n+------------+-------------+------------+----------+\n| session_id | customer_id | start_time | end_time |\n+------------+-------------+------------+----------+\n| 1 | 1 | 1 | 5 |\n| 2 | 1 | 15 | 23 |\n| 3 | 2 | 10 | 12 |\n| 4 | 2 | 17 | 28 |\n| 5 | 2 | 2 | 8 |\n+------------+-------------+------------+----------+\n\nAds table:\n+-------+-------------+-----------+\n| ad_id | customer_id | timestamp |\n+-------+-------------+-----------+\n| 1 | 1 | 5 |\n| 2 | 2 | 17 |\n| 3 | 2 | 20 |\n+-------+-------------+-----------+\n\nResult table:\n+------------+\n| session_id |\n+------------+\n| 2 |\n| 3 |\n| 5 |\n+------------+\nThe ad with ID 1 was shown to user 1 at time 5 while they were in session 1.\nThe ad with ID 2 was shown to user 2 at time 17 while they were in session 4.\nThe ad with ID 3 was shown to user 2 at time 20 while they were in session 4.\nWe can see that sessions 1 and 4 had at least one ad. Sessions 2, 3, and 5 did not have any ads, so we return them.
\n", "content_cn": "Table: Playback
\n\n+-------------+------+\n| Column Name | Type |\n+-------------+------+\n| session_id | int |\n| customer_id | int |\n| start_time | int |\n| end_time | int |\n+-------------+------+\n\u8be5\u8868\u4e3b\u952e\u4e3a\uff1asession_id \uff08\u5267\u96c6id\uff09\ncustomer_id \u662f\u89c2\u770b\u8be5\u5267\u96c6\u7684\u89c2\u4f17id\n\u5267\u96c6\u64ad\u653e\u65f6\u95f4\u5305\u542bstart_time\uff08\u5f00\u59cb\u65f6\u95f4\uff09 \u53ca end_time\uff08\u7ed3\u675f\u65f6\u95f4\uff09\n\u53ef\u4ee5\u4fdd\u8bc1\u7684\u662f\uff0cstart_time\uff08\u5f00\u59cb\u65f6\u95f4\uff09<= end_time\uff08\u7ed3\u675f\u65f6\u95f4\uff09\uff0c\u4e00\u4e2a\u89c2\u4f17\u89c2\u770b\u7684\u4e24\u4e2a\u5267\u96c6\u7684\u65f6\u95f4\u4e0d\u4f1a\u51fa\u73b0\u91cd\u53e0\u3002
\n\n\u00a0
\n\nTable: Ads
\n\n+-------------+------+\n| Column Name | Type |\n+-------------+------+\n| ad_id | int |\n| customer_id | int |\n| timestamp | int |\n+-------------+------+\n\u8be5\u8868\u7684\u4e3b\u952e\u4e3a\uff1aad_id\uff08\u5e7f\u544aid\uff09\ncustomer_id \u4e3a \u89c2\u770b\u5e7f\u544a\u7684\u7528\u6237id\ntimestamp \u8868\u793a\u5e7f\u544a\u51fa\u73b0\u7684\u65f6\u95f4\u70b9\n
\n\n\u00a0
\n\n\u8bf7\u67e5\u51fa\uff0c\u6240\u6709\u6ca1\u6709\u5e7f\u544a\u51fa\u73b0\u8fc7\u7684\u5267\u96c6\u3002
\n\n\u5982\u679c\u89c2\u4f17\u89c2\u770b\u4e86\u5267\u96c6\uff0c\u5e76\u4e14\u5267\u96c6\u91cc\u51fa\u73b0\u4e86\u5e7f\u544a\uff0c\u5c31\u4e00\u5b9a\u4f1a\u6709\u89c2\u4f17\u89c2\u770b\u5e7f\u544a\u7684\u8bb0\u5f55\u3002
\n\n\u8fd4\u56de\u7ed3\u679c\u6ca1\u6709\u987a\u5e8f\u8981\u6c42\u3002
\n\n\u00a0
\n\n\u793a\u4f8b\uff1a
\n\nPlayback table:\n+------------+-------------+------------+----------+\n| session_id | customer_id | start_time | end_time |\n+------------+-------------+------------+----------+\n| 1 | 1 | 1 | 5 |\n| 2 | 1 | 15 | 23 |\n| 3 | 2 | 10 | 12 |\n| 4 | 2 | 17 | 28 |\n| 5 | 2 | 2 | 8 |\n+------------+-------------+------------+----------+\n\nAds table:\n+-------+-------------+-----------+\n| ad_id | customer_id | timestamp |\n+-------+-------------+-----------+\n| 1 | 1 | 5 |\n| 2 | 2 | 17 |\n| 3 | 2 | 20 |\n+-------+-------------+-----------+\n\nResult table:\n+------------+\n| session_id |\n+------------+\n| 2 |\n| 3 |\n| 5 |\n+------------+\n\u5e7f\u544a1\u51fa\u73b0\u5728\u4e86\u5267\u96c61\u7684\u65f6\u95f4\u6bb5\uff0c\u88ab\u89c2\u4f171\u770b\u5230\u4e86\u3002\n\u5e7f\u544a2\u51fa\u73b0\u5728\u4e86\u5267\u96c64\u7684\u65f6\u95f4\u6bb5\uff0c\u88ab\u89c2\u4f172\u770b\u5230\u4e86\u3002\n\u5e7f\u544a3\u51fa\u73b0\u5728\u4e86\u5267\u96c64\u7684\u65f6\u95f4\u6bb5\uff0c\u88ab\u89c2\u4f172\u770b\u5230\u4e86\u3002\n\u6211\u4eec\u53ef\u4ee5\u5f97\u51fa\u7ed3\u8bba\uff0c\u5267\u96c61 \u30014 \u5185\uff0c\u8d77\u7801\u67091\u5904\u5e7f\u544a\u3002 \u5267\u96c62 \u30013 \u30015 \u6ca1\u6709\u5e7f\u544a\u3002
\n", "tags_en": ["Database"], "tags_cn": ["\u6570\u636e\u5e93"], "difficulty_en": "Easy", "difficulty_cn": "\u7b80\u5355", "code_snippets": [{"lang": "MySQL", "langSlug": "mysql", "code": "# Write your MySQL query statement below", "__typename": "CodeSnippetNode"}, {"lang": "MS SQL Server", "langSlug": "mssql", "code": "/* Write your T-SQL query statement below */", "__typename": "CodeSnippetNode"}, {"lang": "Oracle", "langSlug": "oraclesql", "code": "/* Write your PL/SQL query statement below */", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1809](https://leetcode-cn.com/problems/ad-free-sessions)", "[\u6ca1\u6709\u5e7f\u544a\u7684\u5267\u96c6](/solution/1800-1899/1809.Ad-Free%20Sessions/README.md)", "`\u6570\u636e\u5e93`", "\u7b80\u5355", "\ud83d\udd12"], "md_table_row_en": ["[1809](https://leetcode.com/problems/ad-free-sessions)", "[Ad-Free Sessions](/solution/1800-1899/1809.Ad-Free%20Sessions/README_EN.md)", "`Database`", "Easy", "\ud83d\udd12"]}, {"question_id": "1957", "frontend_question_id": "1847", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/closest-room", "url_en": "https://leetcode.com/problems/closest-room", "relative_path_cn": "/solution/1800-1899/1847.Closest%20Room/README.md", "relative_path_en": "/solution/1800-1899/1847.Closest%20Room/README_EN.md", "title_cn": "\u6700\u8fd1\u7684\u623f\u95f4", "title_en": "Closest Room", "question_title_slug": "closest-room", "content_en": "There is a hotel with n
rooms. The rooms are represented by a 2D integer array rooms
where rooms[i] = [roomIdi, sizei]
denotes that there is a room with room number roomIdi
and size equal to sizei
. Each roomIdi
is guaranteed to be unique.
\n\nYou are also given k
queries in a 2D array queries
where queries[j] = [preferredj, minSizej]
. The answer to the jth
query is the room number id
of a room such that:
\n\n\n\t- The room has a size of at least
minSizej
, and \n\tabs(id - preferredj)
is minimized, where abs(x)
is the absolute value of x
. \n
\n\nIf there is a tie in the absolute difference, then use the room with the smallest such id
. If there is no such room, the answer is -1
.
\n\nReturn an array answer
of length k
where answer[j]
contains the answer to the jth
query.
\n\n
\nExample 1:
\n\n\nInput: rooms = [[2,2],[1,2],[3,2]], queries = [[3,1],[3,3],[5,2]]\nOutput: [3,-1,3]\nExplanation: The answers to the queries are as follows:\nQuery = [3,1]: Room number 3 is the closest as abs(3 - 3) = 0, and its size of 2 is at least 1. The answer is 3.\nQuery = [3,3]: There are no rooms with a size of at least 3, so the answer is -1.\nQuery = [5,2]: Room number 3 is the closest as abs(3 - 5) = 2, and its size of 2 is at least 2. The answer is 3.
\n\nExample 2:
\n\n\nInput: rooms = [[1,4],[2,3],[3,5],[4,1],[5,2]], queries = [[2,3],[2,4],[2,5]]\nOutput: [2,1,3]\nExplanation: The answers to the queries are as follows:\nQuery = [2,3]: Room number 2 is the closest as abs(2 - 2) = 0, and its size of 3 is at least 3. The answer is 2.\nQuery = [2,4]: Room numbers 1 and 3 both have sizes of at least 4. The answer is 1 since it is smaller.\nQuery = [2,5]: Room number 3 is the only room with a size of at least 5. The answer is 3.
\n\n
\nConstraints:
\n\n\n\tn == rooms.length
\n\t1 <= n <= 105
\n\tk == queries.length
\n\t1 <= k <= 104
\n\t1 <= roomIdi, preferredj <= 107
\n\t1 <= sizei, minSizej <= 107
\n\t-
\n
\n", "content_cn": "\u4e00\u4e2a\u9152\u5e97\u91cc\u6709\u00a0n
\u00a0\u4e2a\u623f\u95f4\uff0c\u8fd9\u4e9b\u623f\u95f4\u7528\u4e8c\u7ef4\u6574\u6570\u6570\u7ec4\u00a0rooms
\u00a0\u8868\u793a\uff0c\u5176\u4e2d\u00a0rooms[i] = [roomIdi, sizei]
\u00a0\u8868\u793a\u6709\u4e00\u4e2a\u623f\u95f4\u53f7\u4e3a\u00a0roomIdi
\u00a0\u7684\u623f\u95f4\u4e14\u5b83\u7684\u9762\u79ef\u4e3a\u00a0sizei
\u00a0\u3002\u6bcf\u4e00\u4e2a\u623f\u95f4\u53f7\u00a0roomIdi
\u00a0\u4fdd\u8bc1\u662f \u72ec\u4e00\u65e0\u4e8c\u00a0\u7684\u3002
\n\n\u540c\u65f6\u7ed9\u4f60 k
\u00a0\u4e2a\u67e5\u8be2\uff0c\u7528\u4e8c\u7ef4\u6570\u7ec4\u00a0queries
\u00a0\u8868\u793a\uff0c\u5176\u4e2d\u00a0queries[j] = [preferredj, minSizej]
\u00a0\u3002\u7b2c\u00a0j
\u00a0\u4e2a\u67e5\u8be2\u7684\u7b54\u6848\u662f\u6ee1\u8db3\u5982\u4e0b\u6761\u4ef6\u7684\u623f\u95f4\u00a0id
\u00a0\uff1a
\n\n\n\t- \u623f\u95f4\u7684\u9762\u79ef\u00a0\u81f3\u5c11\u00a0\u4e3a\u00a0
minSizej
\u00a0\uff0c\u4e14 \n\tabs(id - preferredj)
\u00a0\u7684\u503c \u6700\u5c0f\u00a0\uff0c\u5176\u4e2d\u00a0abs(x)
\u00a0\u662f\u00a0x
\u00a0\u7684\u7edd\u5bf9\u503c\u3002 \n
\n\n\u5982\u679c\u5dee\u7684\u7edd\u5bf9\u503c\u6709 \u76f8\u7b49\u00a0\u7684\uff0c\u9009\u62e9 \u6700\u5c0f\u00a0\u7684\u00a0id
\u00a0\u3002\u5982\u679c \u6ca1\u6709\u6ee1\u8db3\u6761\u4ef6\u7684\u623f\u95f4\u00a0\uff0c\u7b54\u6848\u4e3a -1
\u00a0\u3002
\n\n\u8bf7\u4f60\u8fd4\u56de\u957f\u5ea6\u4e3a k
\u00a0\u7684\u6570\u7ec4\u00a0answer
\u00a0\uff0c\u5176\u4e2d\u00a0answer[j]
\u00a0\u4e3a\u7b2c j
\u00a0\u4e2a\u67e5\u8be2\u7684\u7ed3\u679c\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1arooms = [[2,2],[1,2],[3,2]], queries = [[3,1],[3,3],[5,2]]\n\u8f93\u51fa\uff1a[3,-1,3]\n\u89e3\u91ca\uff1a\u67e5\u8be2\u7684\u7b54\u6848\u5982\u4e0b\uff1a\n\u67e5\u8be2 [3,1] \uff1a\u623f\u95f4 3 \u7684\u9762\u79ef\u4e3a 2 \uff0c\u5927\u4e8e\u7b49\u4e8e 1 \uff0c\u4e14\u53f7\u7801\u662f\u6700\u63a5\u8fd1 3 \u7684\uff0c\u4e3a abs(3 - 3) = 0 \uff0c\u6240\u4ee5\u7b54\u6848\u4e3a 3 \u3002\n\u67e5\u8be2 [3,3] \uff1a\u6ca1\u6709\u623f\u95f4\u7684\u9762\u79ef\u81f3\u5c11\u4e3a 3 \uff0c\u6240\u4ee5\u7b54\u6848\u4e3a -1 \u3002\n\u67e5\u8be2 [5,2] \uff1a\u623f\u95f4 3 \u7684\u9762\u79ef\u4e3a 2 \uff0c\u5927\u4e8e\u7b49\u4e8e 2 \uff0c\u4e14\u53f7\u7801\u662f\u6700\u63a5\u8fd1 5 \u7684\uff0c\u4e3a abs(3 - 5) = 2 \uff0c\u6240\u4ee5\u7b54\u6848\u4e3a 3 \u3002
\n\n\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1arooms = [[1,4],[2,3],[3,5],[4,1],[5,2]], queries = [[2,3],[2,4],[2,5]]\n\u8f93\u51fa\uff1a[2,1,3]\n\u89e3\u91ca\uff1a\u67e5\u8be2\u7684\u7b54\u6848\u5982\u4e0b\uff1a\n\u67e5\u8be2 [2,3] \uff1a\u623f\u95f4 2 \u7684\u9762\u79ef\u4e3a 3 \uff0c\u5927\u4e8e\u7b49\u4e8e 3 \uff0c\u4e14\u53f7\u7801\u662f\u6700\u63a5\u8fd1\u7684\uff0c\u4e3a abs(2 - 2) = 0 \uff0c\u6240\u4ee5\u7b54\u6848\u4e3a 2 \u3002\n\u67e5\u8be2 [2,4] \uff1a\u623f\u95f4 1 \u548c 3 \u7684\u9762\u79ef\u90fd\u81f3\u5c11\u4e3a 4 \uff0c\u7b54\u6848\u4e3a 1 \u56e0\u4e3a\u5b83\u623f\u95f4\u7f16\u53f7\u66f4\u5c0f\u3002\n\u67e5\u8be2 [2,5] \uff1a\u623f\u95f4 3 \u662f\u552f\u4e00\u9762\u79ef\u5927\u4e8e\u7b49\u4e8e 5 \u7684\uff0c\u6240\u4ee5\u7b54\u6848\u4e3a 3 \u3002
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\tn == rooms.length
\n\t1 <= n <= 105
\n\tk == queries.length
\n\t1 <= k <= 104
\n\t1 <= roomIdi, preferredj <= 107
\n\t1 <= sizei, minSizej <= 107
\n
\n", "tags_en": ["Array", "Binary Search", "Sorting"], "tags_cn": ["\u6570\u7ec4", "\u4e8c\u5206\u67e5\u627e", "\u6392\u5e8f"], "difficulty_en": "Hard", "difficulty_cn": "\u56f0\u96be", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n vector closestRoom(vector>& rooms, vector>& queries) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int[] closestRoom(int[][] rooms, int[][] queries) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def closestRoom(self, rooms, queries):\n \"\"\"\n :type rooms: List[List[int]]\n :type queries: List[List[int]]\n :rtype: List[int]\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def closestRoom(self, rooms: List[List[int]], queries: List[List[int]]) -> List[int]:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\n/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\nint* closestRoom(int** rooms, int roomsSize, int* roomsColSize, int** queries, int queriesSize, int* queriesColSize, int* returnSize){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int[] ClosestRoom(int[][] rooms, int[][] queries) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[][]} rooms\n * @param {number[][]} queries\n * @return {number[]}\n */\nvar closestRoom = function(rooms, queries) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer[][]} rooms\n# @param {Integer[][]} queries\n# @return {Integer[]}\ndef closest_room(rooms, queries)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func closestRoom(_ rooms: [[Int]], _ queries: [[Int]]) -> [Int] {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func closestRoom(rooms [][]int, queries [][]int) []int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def closestRoom(rooms: Array[Array[Int]], queries: Array[Array[Int]]): Array[Int] = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun closestRoom(rooms: Array, queries: Array): IntArray {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn closest_room(rooms: Vec>, queries: Vec>) -> Vec {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer[][] $rooms\n * @param Integer[][] $queries\n * @return Integer[]\n */\n function closestRoom($rooms, $queries) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function closestRoom(rooms: number[][], queries: number[][]): number[] {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (closest-room rooms queries)\n (-> (listof (listof exact-integer?)) (listof (listof exact-integer?)) (listof exact-integer?))\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec closest_room(Rooms :: [[integer()]], Queries :: [[integer()]]) -> [integer()].\nclosest_room(Rooms, Queries) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec closest_room(rooms :: [[integer]], queries :: [[integer]]) :: [integer]\n def closest_room(rooms, queries) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1847](https://leetcode-cn.com/problems/closest-room)", "[\u6700\u8fd1\u7684\u623f\u95f4](/solution/1800-1899/1847.Closest%20Room/README.md)", "`\u6570\u7ec4`,`\u4e8c\u5206\u67e5\u627e`,`\u6392\u5e8f`", "\u56f0\u96be", ""], "md_table_row_en": ["[1847](https://leetcode.com/problems/closest-room)", "[Closest Room](/solution/1800-1899/1847.Closest%20Room/README_EN.md)", "`Array`,`Binary Search`,`Sorting`", "Hard", ""]}, {"question_id": "1956", "frontend_question_id": "1846", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/maximum-element-after-decreasing-and-rearranging", "url_en": "https://leetcode.com/problems/maximum-element-after-decreasing-and-rearranging", "relative_path_cn": "/solution/1800-1899/1846.Maximum%20Element%20After%20Decreasing%20and%20Rearranging/README.md", "relative_path_en": "/solution/1800-1899/1846.Maximum%20Element%20After%20Decreasing%20and%20Rearranging/README_EN.md", "title_cn": "\u51cf\u5c0f\u548c\u91cd\u65b0\u6392\u5217\u6570\u7ec4\u540e\u7684\u6700\u5927\u5143\u7d20", "title_en": "Maximum Element After Decreasing and Rearranging", "question_title_slug": "maximum-element-after-decreasing-and-rearranging", "content_en": "You are given an array of positive integers arr
. Perform some operations (possibly none) on arr
so that it satisfies these conditions:
\n\n\n\t- The value of the first element in
arr
must be 1
. \n\t- The absolute difference between any 2 adjacent elements must be less than or equal to
1
. In other words, abs(arr[i] - arr[i - 1]) <= 1
for each i
where 1 <= i < arr.length
(0-indexed). abs(x)
is the absolute value of x
. \n
\n\nThere are 2 types of operations that you can perform any number of times:
\n\n\n\t- Decrease the value of any element of
arr
to a smaller positive integer. \n\t- Rearrange the elements of
arr
to be in any order. \n
\n\nReturn the maximum possible value of an element in arr
after performing the operations to satisfy the conditions.
\n\n
\nExample 1:
\n\n\nInput: arr = [2,2,1,2,1]\nOutput: 2\nExplanation: \nWe can satisfy the conditions by rearranging arr
so it becomes [1,2,2,2,1]
.\nThe largest element in arr
is 2.\n
\n\nExample 2:
\n\n\nInput: arr = [100,1,1000]\nOutput: 3\nExplanation: \nOne possible way to satisfy the conditions is by doing the following:\n1. Rearrange arr
so it becomes [1,100,1000]
.\n2. Decrease the value of the second element to 2.\n3. Decrease the value of the third element to 3.\nNow arr = [1,2,3], which
satisfies the conditions.\nThe largest element in arr is 3.
\n
\n\nExample 3:
\n\n\nInput: arr = [1,2,3,4,5]\nOutput: 5\nExplanation: The array already satisfies the conditions, and the largest element is 5.\n
\n\n
\nConstraints:
\n\n\n\t1 <= arr.length <= 105
\n\t1 <= arr[i] <= 109
\n
\n", "content_cn": "\u7ed9\u4f60\u4e00\u4e2a\u6b63\u6574\u6570\u6570\u7ec4\u00a0arr
\u00a0\u3002\u8bf7\u4f60\u5bf9 arr
\u00a0\u6267\u884c\u4e00\u4e9b\u64cd\u4f5c\uff08\u4e5f\u53ef\u4ee5\u4e0d\u8fdb\u884c\u4efb\u4f55\u64cd\u4f5c\uff09\uff0c\u4f7f\u5f97\u6570\u7ec4\u6ee1\u8db3\u4ee5\u4e0b\u6761\u4ef6\uff1a
\n\n\n\tarr
\u00a0\u4e2d \u7b2c\u4e00\u4e2a\u00a0\u5143\u7d20\u5fc5\u987b\u4e3a\u00a01
\u00a0\u3002 \n\t- \u4efb\u610f\u76f8\u90bb\u4e24\u4e2a\u5143\u7d20\u7684\u5dee\u7684\u7edd\u5bf9\u503c \u5c0f\u4e8e\u7b49\u4e8e\u00a0
1
\u00a0\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5bf9\u4e8e\u4efb\u610f\u7684 1 <= i < arr.length
\u00a0\uff08\u6570\u7ec4\u4e0b\u6807\u4ece 0 \u5f00\u59cb\uff09\uff0c\u90fd\u6ee1\u8db3\u00a0abs(arr[i] - arr[i - 1]) <= 1
\u00a0\u3002abs(x)
\u00a0\u4e3a\u00a0x
\u00a0\u7684\u7edd\u5bf9\u503c\u3002 \n
\n\n\u4f60\u53ef\u4ee5\u6267\u884c\u4ee5\u4e0b 2 \u79cd\u64cd\u4f5c\u4efb\u610f\u6b21\uff1a
\n\n\n\t- \u51cf\u5c0f
arr
\u00a0\u4e2d\u4efb\u610f\u5143\u7d20\u7684\u503c\uff0c\u4f7f\u5176\u53d8\u4e3a\u4e00\u4e2a \u66f4\u5c0f\u7684\u6b63\u6574\u6570\u00a0\u3002 \n\t- \u91cd\u65b0\u6392\u5217\u00a0
arr
\u00a0\u4e2d\u7684\u5143\u7d20\uff0c\u4f60\u53ef\u4ee5\u4ee5\u4efb\u610f\u987a\u5e8f\u91cd\u65b0\u6392\u5217\u3002 \n
\n\n\u8bf7\u4f60\u8fd4\u56de\u6267\u884c\u4ee5\u4e0a\u64cd\u4f5c\u540e\uff0c\u5728\u6ee1\u8db3\u524d\u6587\u6240\u8ff0\u7684\u6761\u4ef6\u4e0b\uff0carr
\u00a0\u4e2d\u53ef\u80fd\u7684 \u6700\u5927\u503c\u00a0\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1aarr = [2,2,1,2,1]\n\u8f93\u51fa\uff1a2\n\u89e3\u91ca\uff1a\n\u6211\u4eec\u53ef\u4ee5\u91cd\u65b0\u6392\u5217 arr \u5f97\u5230 [1,2,2,2,1] \uff0c\u8be5\u6570\u7ec4\u6ee1\u8db3\u6240\u6709\u6761\u4ef6\u3002
\narr \u4e2d\u6700\u5927\u5143\u7d20\u4e3a 2 \u3002\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1aarr = [100,1,1000]\n\u8f93\u51fa\uff1a3\n\u89e3\u91ca\uff1a\n\u4e00\u4e2a\u53ef\u884c\u7684\u65b9\u6848\u5982\u4e0b\uff1a\n1. \u91cd\u65b0\u6392\u5217 arr
\u5f97\u5230 [1,100,1000] \u3002
\n2. \u5c06\u7b2c\u4e8c\u4e2a\u5143\u7d20\u51cf\u5c0f\u4e3a 2 \u3002\n3. \u5c06\u7b2c\u4e09\u4e2a\u5143\u7d20\u51cf\u5c0f\u4e3a 3 \u3002\n\u73b0\u5728 arr = [1,2,3] \uff0c\u6ee1\u8db3\u6240\u6709\u6761\u4ef6\u3002
\narr \u4e2d\u6700\u5927\u5143\u7d20\u4e3a 3 \u3002\n
\n\n\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1aarr = [1,2,3,4,5]\n\u8f93\u51fa\uff1a5\n\u89e3\u91ca\uff1a\u6570\u7ec4\u5df2\u7ecf\u6ee1\u8db3\u6240\u6709\u6761\u4ef6\uff0c\u6700\u5927\u5143\u7d20\u4e3a 5 \u3002\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\t1 <= arr.length <= 105
\n\t1 <= arr[i] <= 109
\n
\n", "tags_en": ["Greedy", "Array", "Sorting"], "tags_cn": ["\u8d2a\u5fc3", "\u6570\u7ec4", "\u6392\u5e8f"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n int maximumElementAfterDecrementingAndRearranging(vector& arr) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int maximumElementAfterDecrementingAndRearranging(int[] arr) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def maximumElementAfterDecrementingAndRearranging(self, arr):\n \"\"\"\n :type arr: List[int]\n :rtype: int\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def maximumElementAfterDecrementingAndRearranging(self, arr: List[int]) -> int:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nint maximumElementAfterDecrementingAndRearranging(int* arr, int arrSize){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int MaximumElementAfterDecrementingAndRearranging(int[] arr) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[]} arr\n * @return {number}\n */\nvar maximumElementAfterDecrementingAndRearranging = function(arr) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer[]} arr\n# @return {Integer}\ndef maximum_element_after_decrementing_and_rearranging(arr)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func maximumElementAfterDecrementingAndRearranging(_ arr: [Int]) -> Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func maximumElementAfterDecrementingAndRearranging(arr []int) int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def maximumElementAfterDecrementingAndRearranging(arr: Array[Int]): Int = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun maximumElementAfterDecrementingAndRearranging(arr: IntArray): Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn maximum_element_after_decrementing_and_rearranging(arr: Vec) -> i32 {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer[] $arr\n * @return Integer\n */\n function maximumElementAfterDecrementingAndRearranging($arr) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function maximumElementAfterDecrementingAndRearranging(arr: number[]): number {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (maximum-element-after-decrementing-and-rearranging arr)\n (-> (listof exact-integer?) exact-integer?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec maximum_element_after_decrementing_and_rearranging(Arr :: [integer()]) -> integer().\nmaximum_element_after_decrementing_and_rearranging(Arr) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec maximum_element_after_decrementing_and_rearranging(arr :: [integer]) :: integer\n def maximum_element_after_decrementing_and_rearranging(arr) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1846](https://leetcode-cn.com/problems/maximum-element-after-decreasing-and-rearranging)", "[\u51cf\u5c0f\u548c\u91cd\u65b0\u6392\u5217\u6570\u7ec4\u540e\u7684\u6700\u5927\u5143\u7d20](/solution/1800-1899/1846.Maximum%20Element%20After%20Decreasing%20and%20Rearranging/README.md)", "`\u8d2a\u5fc3`,`\u6570\u7ec4`,`\u6392\u5e8f`", "\u4e2d\u7b49", ""], "md_table_row_en": ["[1846](https://leetcode.com/problems/maximum-element-after-decreasing-and-rearranging)", "[Maximum Element After Decreasing and Rearranging](/solution/1800-1899/1846.Maximum%20Element%20After%20Decreasing%20and%20Rearranging/README_EN.md)", "`Greedy`,`Array`,`Sorting`", "Medium", ""]}, {"question_id": "1955", "frontend_question_id": "1845", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/seat-reservation-manager", "url_en": "https://leetcode.com/problems/seat-reservation-manager", "relative_path_cn": "/solution/1800-1899/1845.Seat%20Reservation%20Manager/README.md", "relative_path_en": "/solution/1800-1899/1845.Seat%20Reservation%20Manager/README_EN.md", "title_cn": "\u5ea7\u4f4d\u9884\u7ea6\u7ba1\u7406\u7cfb\u7edf", "title_en": "Seat Reservation Manager", "question_title_slug": "seat-reservation-manager", "content_en": "Design a system that manages the reservation state of n
seats that are numbered from 1
to n
.
\n\nImplement the SeatManager
class:
\n\n\n\tSeatManager(int n)
Initializes a SeatManager
object that will manage n
seats numbered from 1
to n
. All seats are initially available. \n\tint reserve()
Fetches the smallest-numbered unreserved seat, reserves it, and returns its number. \n\tvoid unreserve(int seatNumber)
Unreserves the seat with the given seatNumber
. \n
\n\n
\nExample 1:
\n\n\nInput\n["SeatManager", "reserve", "reserve", "unreserve", "reserve", "reserve", "reserve", "reserve", "unreserve"]\n[[5], [], [], [2], [], [], [], [], [5]]\nOutput\n[null, 1, 2, null, 2, 3, 4, 5, null]\n\nExplanation\nSeatManager seatManager = new SeatManager(5); // Initializes a SeatManager with 5 seats.\nseatManager.reserve(); // All seats are available, so return the lowest numbered seat, which is 1.\nseatManager.reserve(); // The available seats are [2,3,4,5], so return the lowest of them, which is 2.\nseatManager.unreserve(2); // Unreserve seat 2, so now the available seats are [2,3,4,5].\nseatManager.reserve(); // The available seats are [2,3,4,5], so return the lowest of them, which is 2.\nseatManager.reserve(); // The available seats are [3,4,5], so return the lowest of them, which is 3.\nseatManager.reserve(); // The available seats are [4,5], so return the lowest of them, which is 4.\nseatManager.reserve(); // The only available seat is seat 5, so return 5.\nseatManager.unreserve(5); // Unreserve seat 5, so now the available seats are [5].\n
\n\n
\nConstraints:
\n\n\n\t1 <= n <= 105
\n\t1 <= seatNumber <= n
\n\t- For each call to
reserve
, it is guaranteed that there will be at least one unreserved seat. \n\t- For each call to
unreserve
, it is guaranteed that seatNumber
will be reserved. \n\t- At most
105
calls in total will be made to reserve
and unreserve
. \n
\n", "content_cn": "\u8bf7\u4f60\u8bbe\u8ba1\u4e00\u4e2a\u7ba1\u7406 n
\u00a0\u4e2a\u5ea7\u4f4d\u9884\u7ea6\u7684\u7cfb\u7edf\uff0c\u5ea7\u4f4d\u7f16\u53f7\u4ece\u00a01
\u00a0\u5230\u00a0n
\u00a0\u3002
\n\n\u8bf7\u4f60\u5b9e\u73b0\u00a0SeatManager
\u00a0\u7c7b\uff1a
\n\n\n\tSeatManager(int n)
\u00a0\u521d\u59cb\u5316\u4e00\u4e2a\u00a0SeatManager
\u00a0\u5bf9\u8c61\uff0c\u5b83\u7ba1\u7406\u4ece 1
\u00a0\u5230 n
\u00a0\u7f16\u53f7\u7684\u00a0n
\u00a0\u4e2a\u5ea7\u4f4d\u3002\u6240\u6709\u5ea7\u4f4d\u521d\u59cb\u90fd\u662f\u53ef\u9884\u7ea6\u7684\u3002 \n\tint reserve()
\u00a0\u8fd4\u56de\u53ef\u4ee5\u9884\u7ea6\u5ea7\u4f4d\u7684\u00a0\u6700\u5c0f\u7f16\u53f7\u00a0\uff0c\u6b64\u5ea7\u4f4d\u53d8\u4e3a\u4e0d\u53ef\u9884\u7ea6\u3002 \n\tvoid unreserve(int seatNumber)
\u00a0\u5c06\u7ed9\u5b9a\u7f16\u53f7\u00a0seatNumber
\u00a0\u5bf9\u5e94\u7684\u5ea7\u4f4d\u53d8\u6210\u53ef\u4ee5\u9884\u7ea6\u3002 \n
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1a\n[\"SeatManager\", \"reserve\", \"reserve\", \"unreserve\", \"reserve\", \"reserve\", \"reserve\", \"reserve\", \"unreserve\"]\n[[5], [], [], [2], [], [], [], [], [5]]\n\u8f93\u51fa\uff1a\n[null, 1, 2, null, 2, 3, 4, 5, null]\n\n\u89e3\u91ca\uff1a\nSeatManager seatManager = new SeatManager(5); // \u521d\u59cb\u5316 SeatManager \uff0c\u6709 5 \u4e2a\u5ea7\u4f4d\u3002\nseatManager.reserve(); // \u6240\u6709\u5ea7\u4f4d\u90fd\u53ef\u4ee5\u9884\u7ea6\uff0c\u6240\u4ee5\u8fd4\u56de\u6700\u5c0f\u7f16\u53f7\u7684\u5ea7\u4f4d\uff0c\u4e5f\u5c31\u662f 1 \u3002\nseatManager.reserve(); // \u53ef\u4ee5\u9884\u7ea6\u7684\u5ea7\u4f4d\u4e3a [2,3,4,5] \uff0c\u8fd4\u56de\u6700\u5c0f\u7f16\u53f7\u7684\u5ea7\u4f4d\uff0c\u4e5f\u5c31\u662f 2 \u3002\nseatManager.unreserve(2); // \u5c06\u5ea7\u4f4d 2 \u53d8\u4e3a\u53ef\u4ee5\u9884\u7ea6\uff0c\u73b0\u5728\u53ef\u9884\u7ea6\u7684\u5ea7\u4f4d\u4e3a [2,3,4,5] \u3002\nseatManager.reserve(); // \u53ef\u4ee5\u9884\u7ea6\u7684\u5ea7\u4f4d\u4e3a [2,3,4,5] \uff0c\u8fd4\u56de\u6700\u5c0f\u7f16\u53f7\u7684\u5ea7\u4f4d\uff0c\u4e5f\u5c31\u662f 2 \u3002\nseatManager.reserve(); // \u53ef\u4ee5\u9884\u7ea6\u7684\u5ea7\u4f4d\u4e3a [3,4,5] \uff0c\u8fd4\u56de\u6700\u5c0f\u7f16\u53f7\u7684\u5ea7\u4f4d\uff0c\u4e5f\u5c31\u662f 3 \u3002\nseatManager.reserve(); // \u53ef\u4ee5\u9884\u7ea6\u7684\u5ea7\u4f4d\u4e3a [4,5] \uff0c\u8fd4\u56de\u6700\u5c0f\u7f16\u53f7\u7684\u5ea7\u4f4d\uff0c\u4e5f\u5c31\u662f 4 \u3002\nseatManager.reserve(); // \u552f\u4e00\u53ef\u4ee5\u9884\u7ea6\u7684\u662f\u5ea7\u4f4d 5 \uff0c\u6240\u4ee5\u8fd4\u56de 5 \u3002\nseatManager.unreserve(5); // \u5c06\u5ea7\u4f4d 5 \u53d8\u4e3a\u53ef\u4ee5\u9884\u7ea6\uff0c\u73b0\u5728\u53ef\u9884\u7ea6\u7684\u5ea7\u4f4d\u4e3a [5] \u3002\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\t1 <= n <= 105
\n\t1 <= seatNumber <= n
\n\t- \u6bcf\u4e00\u6b21\u5bf9\u00a0
reserve
\u00a0\u7684\u8c03\u7528\uff0c\u9898\u76ee\u4fdd\u8bc1\u81f3\u5c11\u5b58\u5728\u4e00\u4e2a\u53ef\u4ee5\u9884\u7ea6\u7684\u5ea7\u4f4d\u3002 \n\t- \u6bcf\u4e00\u6b21\u5bf9\u00a0
unreserve
\u00a0\u7684\u8c03\u7528\uff0c\u9898\u76ee\u4fdd\u8bc1\u00a0seatNumber
\u00a0\u5728\u8c03\u7528\u51fd\u6570\u524d\u90fd\u662f\u88ab\u9884\u7ea6\u72b6\u6001\u3002 \n\t- \u5bf9\u00a0
reserve
\u548c\u00a0unreserve
\u00a0\u7684\u8c03\u7528\u00a0\u603b\u5171\u00a0\u4e0d\u8d85\u8fc7\u00a0105
\u00a0\u6b21\u3002 \n
\n", "tags_en": ["Design", "Heap (Priority Queue)"], "tags_cn": ["\u8bbe\u8ba1", "\u5806\uff08\u4f18\u5148\u961f\u5217\uff09"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class SeatManager {\npublic:\n SeatManager(int n) {\n\n }\n \n int reserve() {\n\n }\n \n void unreserve(int seatNumber) {\n\n }\n};\n\n/**\n * Your SeatManager object will be instantiated and called as such:\n * SeatManager* obj = new SeatManager(n);\n * int param_1 = obj->reserve();\n * obj->unreserve(seatNumber);\n */", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class SeatManager {\n\n public SeatManager(int n) {\n\n }\n \n public int reserve() {\n\n }\n \n public void unreserve(int seatNumber) {\n\n }\n}\n\n/**\n * Your SeatManager object will be instantiated and called as such:\n * SeatManager obj = new SeatManager(n);\n * int param_1 = obj.reserve();\n * obj.unreserve(seatNumber);\n */", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class SeatManager(object):\n\n def __init__(self, n):\n \"\"\"\n :type n: int\n \"\"\"\n\n\n def reserve(self):\n \"\"\"\n :rtype: int\n \"\"\"\n\n\n def unreserve(self, seatNumber):\n \"\"\"\n :type seatNumber: int\n :rtype: None\n \"\"\"\n\n\n\n# Your SeatManager object will be instantiated and called as such:\n# obj = SeatManager(n)\n# param_1 = obj.reserve()\n# obj.unreserve(seatNumber)", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class SeatManager:\n\n def __init__(self, n: int):\n\n\n def reserve(self) -> int:\n\n\n def unreserve(self, seatNumber: int) -> None:\n\n\n\n# Your SeatManager object will be instantiated and called as such:\n# obj = SeatManager(n)\n# param_1 = obj.reserve()\n# obj.unreserve(seatNumber)", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\n\ntypedef struct {\n\n} SeatManager;\n\n\nSeatManager* seatManagerCreate(int n) {\n\n}\n\nint seatManagerReserve(SeatManager* obj) {\n\n}\n\nvoid seatManagerUnreserve(SeatManager* obj, int seatNumber) {\n\n}\n\nvoid seatManagerFree(SeatManager* obj) {\n\n}\n\n/**\n * Your SeatManager struct will be instantiated and called as such:\n * SeatManager* obj = seatManagerCreate(n);\n * int param_1 = seatManagerReserve(obj);\n \n * seatManagerUnreserve(obj, seatNumber);\n \n * seatManagerFree(obj);\n*/", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class SeatManager {\n\n public SeatManager(int n) {\n\n }\n \n public int Reserve() {\n\n }\n \n public void Unreserve(int seatNumber) {\n\n }\n}\n\n/**\n * Your SeatManager object will be instantiated and called as such:\n * SeatManager obj = new SeatManager(n);\n * int param_1 = obj.Reserve();\n * obj.Unreserve(seatNumber);\n */", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number} n\n */\nvar SeatManager = function(n) {\n\n};\n\n/**\n * @return {number}\n */\nSeatManager.prototype.reserve = function() {\n\n};\n\n/** \n * @param {number} seatNumber\n * @return {void}\n */\nSeatManager.prototype.unreserve = function(seatNumber) {\n\n};\n\n/**\n * Your SeatManager object will be instantiated and called as such:\n * var obj = new SeatManager(n)\n * var param_1 = obj.reserve()\n * obj.unreserve(seatNumber)\n */", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "class SeatManager\n\n=begin\n :type n: Integer\n=end\n def initialize(n)\n\n end\n\n\n=begin\n :rtype: Integer\n=end\n def reserve()\n\n end\n\n\n=begin\n :type seat_number: Integer\n :rtype: Void\n=end\n def unreserve(seat_number)\n\n end\n\n\nend\n\n# Your SeatManager object will be instantiated and called as such:\n# obj = SeatManager.new(n)\n# param_1 = obj.reserve()\n# obj.unreserve(seat_number)", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "\nclass SeatManager {\n\n init(_ n: Int) {\n\n }\n \n func reserve() -> Int {\n\n }\n \n func unreserve(_ seatNumber: Int) {\n\n }\n}\n\n/**\n * Your SeatManager object will be instantiated and called as such:\n * let obj = SeatManager(n)\n * let ret_1: Int = obj.reserve()\n * obj.unreserve(seatNumber)\n */", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "type SeatManager struct {\n\n}\n\n\nfunc Constructor(n int) SeatManager {\n\n}\n\n\nfunc (this *SeatManager) Reserve() int {\n\n}\n\n\nfunc (this *SeatManager) Unreserve(seatNumber int) {\n\n}\n\n\n/**\n * Your SeatManager object will be instantiated and called as such:\n * obj := Constructor(n);\n * param_1 := obj.Reserve();\n * obj.Unreserve(seatNumber);\n */", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "class SeatManager(_n: Int) {\n\n def reserve(): Int = {\n\n }\n\n def unreserve(seatNumber: Int) {\n\n }\n\n}\n\n/**\n * Your SeatManager object will be instantiated and called as such:\n * var obj = new SeatManager(n)\n * var param_1 = obj.reserve()\n * obj.unreserve(seatNumber)\n */", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class SeatManager(n: Int) {\n\n fun reserve(): Int {\n\n }\n\n fun unreserve(seatNumber: Int) {\n\n }\n\n}\n\n/**\n * Your SeatManager object will be instantiated and called as such:\n * var obj = SeatManager(n)\n * var param_1 = obj.reserve()\n * obj.unreserve(seatNumber)\n */", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "struct SeatManager {\n\n}\n\n\n/**\n * `&self` means the method takes an immutable reference.\n * If you need a mutable reference, change it to `&mut self` instead.\n */\nimpl SeatManager {\n\n fn new(n: i32) -> Self {\n\n }\n \n fn reserve(&self) -> i32 {\n\n }\n \n fn unreserve(&self, seat_number: i32) {\n\n }\n}\n\n/**\n * Your SeatManager object will be instantiated and called as such:\n * let obj = SeatManager::new(n);\n * let ret_1: i32 = obj.reserve();\n * obj.unreserve(seatNumber);\n */", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class SeatManager {\n /**\n * @param Integer $n\n */\n function __construct($n) {\n\n }\n\n /**\n * @return Integer\n */\n function reserve() {\n\n }\n\n /**\n * @param Integer $seatNumber\n * @return NULL\n */\n function unreserve($seatNumber) {\n\n }\n}\n\n/**\n * Your SeatManager object will be instantiated and called as such:\n * $obj = SeatManager($n);\n * $ret_1 = $obj->reserve();\n * $obj->unreserve($seatNumber);\n */", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "class SeatManager {\n constructor(n: number) {\n\n }\n\n reserve(): number {\n\n }\n\n unreserve(seatNumber: number): void {\n\n }\n}\n\n/**\n * Your SeatManager object will be instantiated and called as such:\n * var obj = new SeatManager(n)\n * var param_1 = obj.reserve()\n * obj.unreserve(seatNumber)\n */", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define seat-manager%\n (class object%\n (super-new)\n\n ; n : exact-integer?\n (init-field\n n)\n \n ; reserve : -> exact-integer?\n (define/public (reserve)\n\n )\n ; unreserve : exact-integer? -> void?\n (define/public (unreserve seatNumber)\n\n )))\n\n;; Your seat-manager% object will be instantiated and called as such:\n;; (define obj (new seat-manager% [n n]))\n;; (define param_1 (send obj reserve))\n;; (send obj unreserve seat-number)", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec seat_manager_init_(N :: integer()) -> any().\nseat_manager_init_(N) ->\n .\n\n-spec seat_manager_reserve() -> integer().\nseat_manager_reserve() ->\n .\n\n-spec seat_manager_unreserve(SeatNumber :: integer()) -> any().\nseat_manager_unreserve(SeatNumber) ->\n .\n\n\n%% Your functions will be called as such:\n%% seat_manager_init_(N),\n%% Param_1 = seat_manager_reserve(),\n%% seat_manager_unreserve(SeatNumber),\n\n%% seat_manager_init_ will be called before every test case, in which you can do some necessary initializations.", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule SeatManager do\n @spec init_(n :: integer) :: any\n def init_(n) do\n\n end\n\n @spec reserve() :: integer\n def reserve() do\n\n end\n\n @spec unreserve(seat_number :: integer) :: any\n def unreserve(seat_number) do\n\n end\nend\n\n# Your functions will be called as such:\n# SeatManager.init_(n)\n# param_1 = SeatManager.reserve()\n# SeatManager.unreserve(seat_number)\n\n# SeatManager.init_ will be called before every test case, in which you can do some necessary initializations.", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1845](https://leetcode-cn.com/problems/seat-reservation-manager)", "[\u5ea7\u4f4d\u9884\u7ea6\u7ba1\u7406\u7cfb\u7edf](/solution/1800-1899/1845.Seat%20Reservation%20Manager/README.md)", "`\u8bbe\u8ba1`,`\u5806\uff08\u4f18\u5148\u961f\u5217\uff09`", "\u4e2d\u7b49", ""], "md_table_row_en": ["[1845](https://leetcode.com/problems/seat-reservation-manager)", "[Seat Reservation Manager](/solution/1800-1899/1845.Seat%20Reservation%20Manager/README_EN.md)", "`Design`,`Heap (Priority Queue)`", "Medium", ""]}, {"question_id": "1954", "frontend_question_id": "1844", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/replace-all-digits-with-characters", "url_en": "https://leetcode.com/problems/replace-all-digits-with-characters", "relative_path_cn": "/solution/1800-1899/1844.Replace%20All%20Digits%20with%20Characters/README.md", "relative_path_en": "/solution/1800-1899/1844.Replace%20All%20Digits%20with%20Characters/README_EN.md", "title_cn": "\u5c06\u6240\u6709\u6570\u5b57\u7528\u5b57\u7b26\u66ff\u6362", "title_en": "Replace All Digits with Characters", "question_title_slug": "replace-all-digits-with-characters", "content_en": "You are given a 0-indexed string s
that has lowercase English letters in its even indices and digits in its odd indices.
\n\nThere is a function shift(c, x)
, where c
is a character and x
is a digit, that returns the xth
character after c
.
\n\n\n\t- For example,
shift('a', 5) = 'f'
and shift('x', 0) = 'x'
. \n
\n\nFor every odd index i
, you want to replace the digit s[i]
with shift(s[i-1], s[i])
.
\n\nReturn s
after replacing all digits. It is guaranteed that shift(s[i-1], s[i])
will never exceed 'z'
.
\n\n
\nExample 1:
\n\n\nInput: s = "a1c1e1"\nOutput: "abcdef"\nExplanation: The digits are replaced as follows:\n- s[1] -> shift('a',1) = 'b'\n- s[3] -> shift('c',1) = 'd'\n- s[5] -> shift('e',1) = 'f'
\n\nExample 2:
\n\n\nInput: s = "a1b2c3d4e"\nOutput: "abbdcfdhe"\nExplanation: The digits are replaced as follows:\n- s[1] -> shift('a',1) = 'b'\n- s[3] -> shift('b',2) = 'd'\n- s[5] -> shift('c',3) = 'f'\n- s[7] -> shift('d',4) = 'h'
\n\n
\nConstraints:
\n\n\n\t1 <= s.length <= 100
\n\ts
consists only of lowercase English letters and digits. \n\tshift(s[i-1], s[i]) <= 'z'
for all odd indices i
. \n
\n", "content_cn": "\u7ed9\u4f60\u4e00\u4e2a\u4e0b\u6807\u4ece 0\u00a0\u5f00\u59cb\u7684\u5b57\u7b26\u4e32 s
\u00a0\uff0c\u5b83\u7684 \u5076\u6570 \u4e0b\u6807\u5904\u4e3a\u5c0f\u5199\u82f1\u6587\u5b57\u6bcd\uff0c\u5947\u6570\u00a0\u4e0b\u6807\u5904\u4e3a\u6570\u5b57\u3002
\n\n\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u00a0shift(c, x)
\u00a0\uff0c\u5176\u4e2d\u00a0c
\u00a0\u662f\u4e00\u4e2a\u5b57\u7b26\u4e14\u00a0x
\u00a0\u662f\u4e00\u4e2a\u6570\u5b57\uff0c\u51fd\u6570\u8fd4\u56de\u5b57\u6bcd\u8868\u4e2d\u00a0c
\u00a0\u540e\u9762\u7b2c x
\u00a0\u4e2a\u5b57\u7b26\u3002
\n\n\n\t- \u6bd4\u65b9\u8bf4\uff0c
shift('a', 5) = 'f'
\u00a0\u548c\u00a0shift('x', 0) = 'x'
\u00a0\u3002 \n
\n\n\u5bf9\u4e8e\u6bcf\u4e2a \u5947\u6570\u00a0\u4e0b\u6807\u00a0i
\u00a0\uff0c\u4f60\u9700\u8981\u5c06\u6570\u5b57\u00a0s[i]
\u7528\u00a0shift(s[i-1], s[i])
\u00a0\u66ff\u6362\u3002
\n\n\u8bf7\u4f60\u66ff\u6362\u6240\u6709\u6570\u5b57\u4ee5\u540e\uff0c\u5c06\u5b57\u7b26\u4e32 s
\u00a0\u8fd4\u56de\u3002\u9898\u76ee \u4fdd\u8bc1\u00a0shift(s[i-1], s[i])
\u00a0\u4e0d\u4f1a\u8d85\u8fc7 'z'
\u00a0\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1as = \"a1c1e1\"\n\u8f93\u51fa\uff1a\"abcdef\"\n\u89e3\u91ca\uff1a\u6570\u5b57\u88ab\u66ff\u6362\u7ed3\u679c\u5982\u4e0b\uff1a\n- s[1] -> shift('a',1) = 'b'\n- s[3] -> shift('c',1) = 'd'\n- s[5] -> shift('e',1) = 'f'
\n\n\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1as = \"a1b2c3d4e\"\n\u8f93\u51fa\uff1a\"abbdcfdhe\"\n\u89e3\u91ca\uff1a\u6570\u5b57\u88ab\u66ff\u6362\u7ed3\u679c\u5982\u4e0b\uff1a\n- s[1] -> shift('a',1) = 'b'\n- s[3] -> shift('b',2) = 'd'\n- s[5] -> shift('c',3) = 'f'\n- s[7] -> shift('d',4) = 'h'
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\t1 <= s.length <= 100
\n\ts
\u00a0\u53ea\u5305\u542b\u5c0f\u5199\u82f1\u6587\u5b57\u6bcd\u548c\u6570\u5b57\u3002 \n\t- \u5bf9\u6240\u6709 \u5947\u6570 \u4e0b\u6807\u5904\u7684\u00a0
i
\u00a0\uff0c\u6ee1\u8db3\u00a0shift(s[i-1], s[i]) <= 'z'
\u00a0\u3002 \n
\n", "tags_en": ["String"], "tags_cn": ["\u5b57\u7b26\u4e32"], "difficulty_en": "Easy", "difficulty_cn": "\u7b80\u5355", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n string replaceDigits(string s) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public String replaceDigits(String s) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def replaceDigits(self, s):\n \"\"\"\n :type s: str\n :rtype: str\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def replaceDigits(self, s: str) -> str:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nchar * replaceDigits(char * s){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public string ReplaceDigits(string s) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {string} s\n * @return {string}\n */\nvar replaceDigits = function(s) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {String} s\n# @return {String}\ndef replace_digits(s)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func replaceDigits(_ s: String) -> String {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func replaceDigits(s string) string {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def replaceDigits(s: String): String = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun replaceDigits(s: String): String {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn replace_digits(s: String) -> String {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param String $s\n * @return String\n */\n function replaceDigits($s) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function replaceDigits(s: string): string {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (replace-digits s)\n (-> string? string?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec replace_digits(S :: unicode:unicode_binary()) -> unicode:unicode_binary().\nreplace_digits(S) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec replace_digits(s :: String.t) :: String.t\n def replace_digits(s) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1844](https://leetcode-cn.com/problems/replace-all-digits-with-characters)", "[\u5c06\u6240\u6709\u6570\u5b57\u7528\u5b57\u7b26\u66ff\u6362](/solution/1800-1899/1844.Replace%20All%20Digits%20with%20Characters/README.md)", "`\u5b57\u7b26\u4e32`", "\u7b80\u5355", ""], "md_table_row_en": ["[1844](https://leetcode.com/problems/replace-all-digits-with-characters)", "[Replace All Digits with Characters](/solution/1800-1899/1844.Replace%20All%20Digits%20with%20Characters/README_EN.md)", "`String`", "Easy", ""]}, {"question_id": "1953", "frontend_question_id": "1825", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/finding-mk-average", "url_en": "https://leetcode.com/problems/finding-mk-average", "relative_path_cn": "/solution/1800-1899/1825.Finding%20MK%20Average/README.md", "relative_path_en": "/solution/1800-1899/1825.Finding%20MK%20Average/README_EN.md", "title_cn": "\u6c42\u51fa MK \u5e73\u5747\u503c", "title_en": "Finding MK Average", "question_title_slug": "finding-mk-average", "content_en": "You are given two integers, m
and k
, and a stream of integers. You are tasked to implement a data structure that calculates the MKAverage for the stream.
\n\nThe MKAverage can be calculated using these steps:
\n\n\n\t- If the number of the elements in the stream is less than
m
you should consider the MKAverage to be -1
. Otherwise, copy the last m
elements of the stream to a separate container. \n\t- Remove the smallest
k
elements and the largest k
elements from the container. \n\t- Calculate the average value for the rest of the elements rounded down to the nearest integer.
\n
\n\nImplement the MKAverage
class:
\n\n\n\tMKAverage(int m, int k)
Initializes the MKAverage object with an empty stream and the two integers m
and k
. \n\tvoid addElement(int num)
Inserts a new element num
into the stream. \n\tint calculateMKAverage()
Calculates and returns the MKAverage for the current stream rounded down to the nearest integer. \n
\n\n
\nExample 1:
\n\n\nInput\n["MKAverage", "addElement", "addElement", "calculateMKAverage", "addElement", "calculateMKAverage", "addElement", "addElement", "addElement", "calculateMKAverage"]\n[[3, 1], [3], [1], [], [10], [], [5], [5], [5], []]\nOutput\n[null, null, null, -1, null, 3, null, null, null, 5]\n\nExplanation\nMKAverage obj = new MKAverage(3, 1); \nobj.addElement(3); // current elements are [3]\nobj.addElement(1); // current elements are [3,1]\nobj.calculateMKAverage(); // return -1, because m = 3 and only 2 elements exist.\nobj.addElement(10); // current elements are [3,1,10]\nobj.calculateMKAverage(); // The last 3 elements are [3,1,10].\n // After removing smallest and largest 1 element the container will be [3].\n // The average of [3] equals 3/1 = 3, return 3\nobj.addElement(5); // current elements are [3,1,10,5]\nobj.addElement(5); // current elements are [3,1,10,5,5]\nobj.addElement(5); // current elements are [3,1,10,5,5,5]\nobj.calculateMKAverage(); // The last 3 elements are [5,5,5].\n // After removing smallest and largest 1 element the container will be [5].\n // The average of [5] equals 5/1 = 5, return 5\n
\n\n
\nConstraints:
\n\n\n\t3 <= m <= 105
\n\t1 <= k*2 < m
\n\t1 <= num <= 105
\n\t- At most
105
calls will be made to addElement
and calculateMKAverage
. \n
\n", "content_cn": "\u7ed9\u4f60\u4e24\u4e2a\u6574\u6570\u00a0m
\u00a0\u548c\u00a0k
\u00a0\uff0c\u4ee5\u53ca\u6570\u636e\u6d41\u5f62\u5f0f\u7684\u82e5\u5e72\u6574\u6570\u3002\u4f60\u9700\u8981\u5b9e\u73b0\u4e00\u4e2a\u6570\u636e\u7ed3\u6784\uff0c\u8ba1\u7b97\u8fd9\u4e2a\u6570\u636e\u6d41\u7684 MK \u5e73\u5747\u503c\u00a0\u3002
\n\nMK \u5e73\u5747\u503c\u00a0\u6309\u7167\u5982\u4e0b\u6b65\u9aa4\u8ba1\u7b97\uff1a
\n\n\n\t- \u5982\u679c\u6570\u636e\u6d41\u4e2d\u7684\u6574\u6570\u5c11\u4e8e
m
\u00a0\u4e2a\uff0cMK \u5e73\u5747\u503c\u00a0\u4e3a -1
\u00a0\uff0c\u5426\u5219\u5c06\u6570\u636e\u6d41\u4e2d\u6700\u540e m
\u00a0\u4e2a\u5143\u7d20\u62f7\u8d1d\u5230\u4e00\u4e2a\u72ec\u7acb\u7684\u5bb9\u5668\u4e2d\u3002 \n\t- \u4ece\u8fd9\u4e2a\u5bb9\u5668\u4e2d\u5220\u9664\u6700\u5c0f\u7684
k
\u00a0\u4e2a\u6570\u548c\u6700\u5927\u7684 k
\u00a0\u4e2a\u6570\u3002 \n\t- \u8ba1\u7b97\u5269\u4f59\u5143\u7d20\u7684\u5e73\u5747\u503c\uff0c\u5e76 \u5411\u4e0b\u53d6\u6574\u5230\u6700\u8fd1\u7684\u6574\u6570\u00a0\u3002
\n
\n\n\u8bf7\u4f60\u5b9e\u73b0\u00a0MKAverage
\u00a0\u7c7b\uff1a
\n\n\n\tMKAverage(int m, int k)
\u00a0\u7528\u4e00\u4e2a\u7a7a\u7684\u6570\u636e\u6d41\u548c\u4e24\u4e2a\u6574\u6570 m
\u00a0\u548c k
\u00a0\u521d\u59cb\u5316\u00a0MKAverage\u00a0\u5bf9\u8c61\u3002 \n\tvoid addElement(int num)
\u00a0\u5f80\u6570\u636e\u6d41\u4e2d\u63d2\u5165\u4e00\u4e2a\u65b0\u7684\u5143\u7d20\u00a0num
\u00a0\u3002 \n\tint calculateMKAverage()
\u00a0\u5bf9\u5f53\u524d\u7684\u6570\u636e\u6d41\u8ba1\u7b97\u5e76\u8fd4\u56de MK \u5e73\u5747\u6570\u00a0\uff0c\u7ed3\u679c\u9700 \u5411\u4e0b\u53d6\u6574\u5230\u6700\u8fd1\u7684\u6574\u6570 \u3002 \n
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1a\n[\"MKAverage\", \"addElement\", \"addElement\", \"calculateMKAverage\", \"addElement\", \"calculateMKAverage\", \"addElement\", \"addElement\", \"addElement\", \"calculateMKAverage\"]\n[[3, 1], [3], [1], [], [10], [], [5], [5], [5], []]\n\u8f93\u51fa\uff1a\n[null, null, null, -1, null, 3, null, null, null, 5]\n\n\u89e3\u91ca\uff1a\nMKAverage obj = new MKAverage(3, 1); \nobj.addElement(3); // \u5f53\u524d\u5143\u7d20\u4e3a [3]\nobj.addElement(1); // \u5f53\u524d\u5143\u7d20\u4e3a [3,1]\nobj.calculateMKAverage(); // \u8fd4\u56de -1 \uff0c\u56e0\u4e3a m = 3 \uff0c\u4f46\u6570\u636e\u6d41\u4e2d\u53ea\u6709 2 \u4e2a\u5143\u7d20\nobj.addElement(10); // \u5f53\u524d\u5143\u7d20\u4e3a [3,1,10]\nobj.calculateMKAverage(); // \u6700\u540e 3 \u4e2a\u5143\u7d20\u4e3a [3,1,10]\n // \u5220\u9664\u6700\u5c0f\u4ee5\u53ca\u6700\u5927\u7684 1 \u4e2a\u5143\u7d20\u540e\uff0c\u5bb9\u5668\u4e3a [3]\n // [3] \u7684\u5e73\u5747\u503c\u7b49\u4e8e 3/1 = 3 \uff0c\u6545\u8fd4\u56de 3\nobj.addElement(5); // \u5f53\u524d\u5143\u7d20\u4e3a [3,1,10,5]\nobj.addElement(5); // \u5f53\u524d\u5143\u7d20\u4e3a [3,1,10,5,5]\nobj.addElement(5); // \u5f53\u524d\u5143\u7d20\u4e3a [3,1,10,5,5,5]\nobj.calculateMKAverage(); // \u6700\u540e 3 \u4e2a\u5143\u7d20\u4e3a [5,5,5]\n //
\u5220\u9664\u6700\u5c0f\u4ee5\u53ca\u6700\u5927\u7684 1 \u4e2a\u5143\u7d20\u540e\uff0c\u5bb9\u5668\u4e3a [5]\n //
[5] \u7684\u5e73\u5747\u503c\u7b49\u4e8e 5/1 = 5 \uff0c\u6545\u8fd4\u56de 5\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\t3 <= m <= 105
\n\t1 <= k*2 < m
\n\t1 <= num <= 105
\n\taddElement
\u4e0e\u00a0calculateMKAverage
\u00a0\u603b\u64cd\u4f5c\u6b21\u6570\u4e0d\u8d85\u8fc7 105
\u6b21\u3002 \n
\n", "tags_en": ["Design", "Queue", "Ordered Set", "Heap (Priority Queue)"], "tags_cn": ["\u8bbe\u8ba1", "\u961f\u5217", "\u6709\u5e8f\u96c6\u5408", "\u5806\uff08\u4f18\u5148\u961f\u5217\uff09"], "difficulty_en": "Hard", "difficulty_cn": "\u56f0\u96be", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class MKAverage {\npublic:\n MKAverage(int m, int k) {\n\n }\n \n void addElement(int num) {\n\n }\n \n int calculateMKAverage() {\n\n }\n};\n\n/**\n * Your MKAverage object will be instantiated and called as such:\n * MKAverage* obj = new MKAverage(m, k);\n * obj->addElement(num);\n * int param_2 = obj->calculateMKAverage();\n */", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class MKAverage {\n\n public MKAverage(int m, int k) {\n\n }\n \n public void addElement(int num) {\n\n }\n \n public int calculateMKAverage() {\n\n }\n}\n\n/**\n * Your MKAverage object will be instantiated and called as such:\n * MKAverage obj = new MKAverage(m, k);\n * obj.addElement(num);\n * int param_2 = obj.calculateMKAverage();\n */", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class MKAverage(object):\n\n def __init__(self, m, k):\n \"\"\"\n :type m: int\n :type k: int\n \"\"\"\n\n\n def addElement(self, num):\n \"\"\"\n :type num: int\n :rtype: None\n \"\"\"\n\n\n def calculateMKAverage(self):\n \"\"\"\n :rtype: int\n \"\"\"\n\n\n\n# Your MKAverage object will be instantiated and called as such:\n# obj = MKAverage(m, k)\n# obj.addElement(num)\n# param_2 = obj.calculateMKAverage()", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class MKAverage:\n\n def __init__(self, m: int, k: int):\n\n\n def addElement(self, num: int) -> None:\n\n\n def calculateMKAverage(self) -> int:\n\n\n\n# Your MKAverage object will be instantiated and called as such:\n# obj = MKAverage(m, k)\n# obj.addElement(num)\n# param_2 = obj.calculateMKAverage()", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\n\ntypedef struct {\n\n} MKAverage;\n\n\nMKAverage* mKAverageCreate(int m, int k) {\n\n}\n\nvoid mKAverageAddElement(MKAverage* obj, int num) {\n\n}\n\nint mKAverageCalculateMKAverage(MKAverage* obj) {\n\n}\n\nvoid mKAverageFree(MKAverage* obj) {\n\n}\n\n/**\n * Your MKAverage struct will be instantiated and called as such:\n * MKAverage* obj = mKAverageCreate(m, k);\n * mKAverageAddElement(obj, num);\n \n * int param_2 = mKAverageCalculateMKAverage(obj);\n \n * mKAverageFree(obj);\n*/", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class MKAverage {\n\n public MKAverage(int m, int k) {\n\n }\n \n public void AddElement(int num) {\n\n }\n \n public int CalculateMKAverage() {\n\n }\n}\n\n/**\n * Your MKAverage object will be instantiated and called as such:\n * MKAverage obj = new MKAverage(m, k);\n * obj.AddElement(num);\n * int param_2 = obj.CalculateMKAverage();\n */", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number} m\n * @param {number} k\n */\nvar MKAverage = function(m, k) {\n\n};\n\n/** \n * @param {number} num\n * @return {void}\n */\nMKAverage.prototype.addElement = function(num) {\n\n};\n\n/**\n * @return {number}\n */\nMKAverage.prototype.calculateMKAverage = function() {\n\n};\n\n/**\n * Your MKAverage object will be instantiated and called as such:\n * var obj = new MKAverage(m, k)\n * obj.addElement(num)\n * var param_2 = obj.calculateMKAverage()\n */", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "class MKAverage\n\n=begin\n :type m: Integer\n :type k: Integer\n=end\n def initialize(m, k)\n\n end\n\n\n=begin\n :type num: Integer\n :rtype: Void\n=end\n def add_element(num)\n\n end\n\n\n=begin\n :rtype: Integer\n=end\n def calculate_mk_average()\n\n end\n\n\nend\n\n# Your MKAverage object will be instantiated and called as such:\n# obj = MKAverage.new(m, k)\n# obj.add_element(num)\n# param_2 = obj.calculate_mk_average()", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "\nclass MKAverage {\n\n init(_ m: Int, _ k: Int) {\n\n }\n \n func addElement(_ num: Int) {\n\n }\n \n func calculateMKAverage() -> Int {\n\n }\n}\n\n/**\n * Your MKAverage object will be instantiated and called as such:\n * let obj = MKAverage(m, k)\n * obj.addElement(num)\n * let ret_2: Int = obj.calculateMKAverage()\n */", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "type MKAverage struct {\n\n}\n\n\nfunc Constructor(m int, k int) MKAverage {\n\n}\n\n\nfunc (this *MKAverage) AddElement(num int) {\n\n}\n\n\nfunc (this *MKAverage) CalculateMKAverage() int {\n\n}\n\n\n/**\n * Your MKAverage object will be instantiated and called as such:\n * obj := Constructor(m, k);\n * obj.AddElement(num);\n * param_2 := obj.CalculateMKAverage();\n */", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "class MKAverage(_m: Int, _k: Int) {\n\n def addElement(num: Int) {\n \n }\n\n def calculateMKAverage(): Int = {\n \n }\n\n}\n\n/**\n * Your MKAverage object will be instantiated and called as such:\n * var obj = new MKAverage(m, k)\n * obj.addElement(num)\n * var param_2 = obj.calculateMKAverage()\n */", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class MKAverage(m: Int, k: Int) {\n\n fun addElement(num: Int) {\n\n }\n\n fun calculateMKAverage(): Int {\n\n }\n\n}\n\n/**\n * Your MKAverage object will be instantiated and called as such:\n * var obj = MKAverage(m, k)\n * obj.addElement(num)\n * var param_2 = obj.calculateMKAverage()\n */", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "struct MKAverage {\n\n}\n\n\n/**\n * `&self` means the method takes an immutable reference.\n * If you need a mutable reference, change it to `&mut self` instead.\n */\nimpl MKAverage {\n\n fn new(m: i32, k: i32) -> Self {\n\n }\n \n fn add_element(&self, num: i32) {\n\n }\n \n fn calculate_mk_average(&self) -> i32 {\n\n }\n}\n\n/**\n * Your MKAverage object will be instantiated and called as such:\n * let obj = MKAverage::new(m, k);\n * obj.add_element(num);\n * let ret_2: i32 = obj.calculate_mk_average();\n */", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class MKAverage {\n /**\n * @param Integer $m\n * @param Integer $k\n */\n function __construct($m, $k) {\n\n }\n\n /**\n * @param Integer $num\n * @return NULL\n */\n function addElement($num) {\n\n }\n\n /**\n * @return Integer\n */\n function calculateMKAverage() {\n\n }\n}\n\n/**\n * Your MKAverage object will be instantiated and called as such:\n * $obj = MKAverage($m, $k);\n * $obj->addElement($num);\n * $ret_2 = $obj->calculateMKAverage();\n */", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "class MKAverage {\n constructor(m: number, k: number) {\n\n }\n\n addElement(num: number): void {\n\n }\n\n calculateMKAverage(): number {\n\n }\n}\n\n/**\n * Your MKAverage object will be instantiated and called as such:\n * var obj = new MKAverage(m, k)\n * obj.addElement(num)\n * var param_2 = obj.calculateMKAverage()\n */", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define mk-average%\n (class object%\n (super-new)\n\n ; m : exact-integer?\n\n ; k : exact-integer?\n (init-field\n m\n k)\n \n ; add-element : exact-integer? -> void?\n (define/public (add-element num)\n\n )\n ; calculate-mk-average : -> exact-integer?\n (define/public (calculate-mk-average)\n\n )))\n\n;; Your mk-average% object will be instantiated and called as such:\n;; (define obj (new mk-average% [m m] [k k]))\n;; (send obj add-element num)\n;; (define param_2 (send obj calculate-mk-average))", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec mk_average_init_(M :: integer(), K :: integer()) -> any().\nmk_average_init_(M, K) ->\n .\n\n-spec mk_average_add_element(Num :: integer()) -> any().\nmk_average_add_element(Num) ->\n .\n\n-spec mk_average_calculate_mk_average() -> integer().\nmk_average_calculate_mk_average() ->\n .\n\n\n%% Your functions will be called as such:\n%% mk_average_init_(M, K),\n%% mk_average_add_element(Num),\n%% Param_2 = mk_average_calculate_mk_average(),\n\n%% mk_average_init_ will be called before every test case, in which you can do some necessary initializations.", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule MKAverage do\n @spec init_(m :: integer, k :: integer) :: any\n def init_(m, k) do\n\n end\n\n @spec add_element(num :: integer) :: any\n def add_element(num) do\n\n end\n\n @spec calculate_mk_average() :: integer\n def calculate_mk_average() do\n\n end\nend\n\n# Your functions will be called as such:\n# MKAverage.init_(m, k)\n# MKAverage.add_element(num)\n# param_2 = MKAverage.calculate_mk_average()\n\n# MKAverage.init_ will be called before every test case, in which you can do some necessary initializations.", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1825](https://leetcode-cn.com/problems/finding-mk-average)", "[\u6c42\u51fa MK \u5e73\u5747\u503c](/solution/1800-1899/1825.Finding%20MK%20Average/README.md)", "`\u8bbe\u8ba1`,`\u961f\u5217`,`\u6709\u5e8f\u96c6\u5408`,`\u5806\uff08\u4f18\u5148\u961f\u5217\uff09`", "\u56f0\u96be", ""], "md_table_row_en": ["[1825](https://leetcode.com/problems/finding-mk-average)", "[Finding MK Average](/solution/1800-1899/1825.Finding%20MK%20Average/README_EN.md)", "`Design`,`Queue`,`Ordered Set`,`Heap (Priority Queue)`", "Hard", ""]}, {"question_id": "1952", "frontend_question_id": "1824", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/minimum-sideway-jumps", "url_en": "https://leetcode.com/problems/minimum-sideway-jumps", "relative_path_cn": "/solution/1800-1899/1824.Minimum%20Sideway%20Jumps/README.md", "relative_path_en": "/solution/1800-1899/1824.Minimum%20Sideway%20Jumps/README_EN.md", "title_cn": "\u6700\u5c11\u4fa7\u8df3\u6b21\u6570", "title_en": "Minimum Sideway Jumps", "question_title_slug": "minimum-sideway-jumps", "content_en": "There is a 3 lane road of length n
that consists of n + 1
points labeled from 0
to n
. A frog starts at point 0
in the second lane and wants to jump to point n
. However, there could be obstacles along the way.
\n\nYou are given an array obstacles
of length n + 1
where each obstacles[i]
(ranging from 0 to 3) describes an obstacle on the lane obstacles[i]
at point i
. If obstacles[i] == 0
, there are no obstacles at point i
. There will be at most one obstacle in the 3 lanes at each point.
\n\n\n\t- For example, if
obstacles[2] == 1
, then there is an obstacle on lane 1 at point 2. \n
\n\nThe frog can only travel from point i
to point i + 1
on the same lane if there is not an obstacle on the lane at point i + 1
. To avoid obstacles, the frog can also perform a side jump to jump to another lane (even if they are not adjacent) at the same point if there is no obstacle on the new lane.
\n\n\n\t- For example, the frog can jump from lane 3 at point 3 to lane 1 at point 3.
\n
\n\nReturn the minimum number of side jumps the frog needs to reach any lane at point n starting from lane 2
at point 0.
\n\nNote: There will be no obstacles on points 0
and n
.
\n\n
\nExample 1:
\n
\n\nInput: obstacles = [0,1,2,3,0]\nOutput: 2 \nExplanation: The optimal solution is shown by the arrows above. There are 2 side jumps (red arrows).\nNote that the frog can jump over obstacles only when making side jumps (as shown at point 2).\n
\n\nExample 2:
\n
\n\nInput: obstacles = [0,1,1,3,3,0]\nOutput: 0\nExplanation: There are no obstacles on lane 2. No side jumps are required.\n
\n\nExample 3:
\n
\n\nInput: obstacles = [0,2,1,0,3,0]\nOutput: 2\nExplanation: The optimal solution is shown by the arrows above. There are 2 side jumps.\n
\n\n
\nConstraints:
\n\n\n\tobstacles.length == n + 1
\n\t1 <= n <= 5 * 105
\n\t0 <= obstacles[i] <= 3
\n\tobstacles[0] == obstacles[n] == 0
\n
\n", "content_cn": "\u7ed9\u4f60\u4e00\u4e2a\u957f\u5ea6\u4e3a\u00a0n
\u00a0\u7684\u00a03 \u8dd1\u9053\u9053\u8def\u00a0\uff0c\u5b83\u603b\u5171\u5305\u542b\u00a0n + 1
\u00a0\u4e2a\u00a0\u70b9\u00a0\uff0c\u7f16\u53f7\u4e3a\u00a00
\u00a0\u5230\u00a0n
\u00a0\u3002\u4e00\u53ea\u9752\u86d9\u4ece\u00a00
\u00a0\u53f7\u70b9\u7b2c\u4e8c\u6761\u8dd1\u9053\u00a0\u51fa\u53d1\u00a0\uff0c\u5b83\u60f3\u8981\u8df3\u5230\u70b9\u00a0n
\u00a0\u5904\u3002\u7136\u800c\u9053\u8def\u4e0a\u53ef\u80fd\u6709\u4e00\u4e9b\u969c\u788d\u3002
\n\n\u7ed9\u4f60\u4e00\u4e2a\u957f\u5ea6\u4e3a n + 1
\u00a0\u7684\u6570\u7ec4\u00a0obstacles
\u00a0\uff0c\u5176\u4e2d\u00a0obstacles[i]
\u00a0\uff08\u53d6\u503c\u8303\u56f4\u4ece 0 \u5230 3\uff09\u8868\u793a\u5728\u70b9 i
\u00a0\u5904\u7684\u00a0obstacles[i]
\u00a0\u8dd1\u9053\u4e0a\u6709\u4e00\u4e2a\u969c\u788d\u3002\u5982\u679c\u00a0obstacles[i] == 0
\u00a0\uff0c\u90a3\u4e48\u70b9\u00a0i
\u00a0\u5904\u6ca1\u6709\u969c\u788d\u3002\u4efb\u4f55\u4e00\u4e2a\u70b9\u7684\u4e09\u6761\u8dd1\u9053\u4e2d\u00a0\u6700\u591a\u6709\u4e00\u4e2a\u00a0\u969c\u788d\u3002
\n\n\n\t- \u6bd4\u65b9\u8bf4\uff0c\u5982\u679c\u00a0
obstacles[2] == 1
\u00a0\uff0c\u90a3\u4e48\u8bf4\u660e\u5728\u70b9 2 \u5904\u8dd1\u9053 1 \u6709\u969c\u788d\u3002 \n
\n\n\u8fd9\u53ea\u9752\u86d9\u4ece\u70b9 i
\u00a0\u8df3\u5230\u70b9 i + 1
\u00a0\u4e14\u8dd1\u9053\u4e0d\u53d8\u7684\u524d\u63d0\u662f\u70b9 i + 1
\u00a0\u7684\u540c\u4e00\u8dd1\u9053\u4e0a\u6ca1\u6709\u969c\u788d\u3002\u4e3a\u4e86\u8eb2\u907f\u969c\u788d\uff0c\u8fd9\u53ea\u9752\u86d9\u4e5f\u53ef\u4ee5\u5728\u00a0\u540c\u4e00\u4e2a\u00a0\u70b9\u5904\u00a0\u4fa7\u8df3\u00a0\u5230 \u53e6\u5916\u4e00\u6761\u00a0\u8dd1\u9053\uff08\u8fd9\u4e24\u6761\u8dd1\u9053\u53ef\u4ee5\u4e0d\u76f8\u90bb\uff09\uff0c\u4f46\u524d\u63d0\u662f\u8df3\u8fc7\u53bb\u7684\u8dd1\u9053\u8be5\u70b9\u5904\u6ca1\u6709\u969c\u788d\u3002
\n\n\n\t- \u6bd4\u65b9\u8bf4\uff0c\u8fd9\u53ea\u9752\u86d9\u53ef\u4ee5\u4ece\u70b9 3 \u5904\u7684\u8dd1\u9053 3 \u8df3\u5230\u70b9 3 \u5904\u7684\u8dd1\u9053 1 \u3002
\n
\n\n\u8fd9\u53ea\u9752\u86d9\u4ece\u70b9 0 \u5904\u8dd1\u9053 2
\u00a0\u51fa\u53d1\uff0c\u5e76\u60f3\u5230\u8fbe\u70b9 n
\u00a0\u5904\u7684 \u4efb\u4e00\u8dd1\u9053 \uff0c\u8bf7\u4f60\u8fd4\u56de \u6700\u5c11\u4fa7\u8df3\u6b21\u6570\u00a0\u3002
\n\n\u6ce8\u610f\uff1a\u70b9 0
\u00a0\u5904\u548c\u70b9 n
\u00a0\u5904\u7684\u4efb\u4e00\u8dd1\u9053\u90fd\u4e0d\u4f1a\u6709\u969c\u788d\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n
\n\n\u8f93\u5165\uff1aobstacles = [0,1,2,3,0]\n\u8f93\u51fa\uff1a2 \n\u89e3\u91ca\uff1a\u6700\u4f18\u65b9\u6848\u5982\u4e0a\u56fe\u7bad\u5934\u6240\u793a\u3002\u603b\u5171\u6709 2 \u6b21\u4fa7\u8df3\uff08\u7ea2\u8272\u7bad\u5934\uff09\u3002\n\u6ce8\u610f\uff0c\u8fd9\u53ea\u9752\u86d9\u53ea\u6709\u5f53\u4fa7\u8df3\u65f6\u624d\u53ef\u4ee5\u8df3\u8fc7\u969c\u788d\uff08\u5982\u4e0a\u56fe\u70b9 2 \u5904\u6240\u793a\uff09\u3002\n
\n\n\u793a\u4f8b 2\uff1a
\n
\n\n\u8f93\u5165\uff1aobstacles = [0,1,1,3,3,0]\n\u8f93\u51fa\uff1a0\n\u89e3\u91ca\uff1a\u8dd1\u9053 2 \u6ca1\u6709\u4efb\u4f55\u969c\u788d\uff0c\u6240\u4ee5\u4e0d\u9700\u8981\u4efb\u4f55\u4fa7\u8df3\u3002\n
\n\n\u793a\u4f8b 3\uff1a
\n
\n\n\u8f93\u5165\uff1aobstacles = [0,2,1,0,3,0]\n\u8f93\u51fa\uff1a2\n\u89e3\u91ca\uff1a\u6700\u4f18\u65b9\u6848\u5982\u4e0a\u56fe\u6240\u793a\u3002\u603b\u5171\u6709 2 \u6b21\u4fa7\u8df3\u3002\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\tobstacles.length == n + 1
\n\t1 <= n <= 5 * 105
\n\t0 <= obstacles[i] <= 3
\n\tobstacles[0] == obstacles[n] == 0
\n
\n", "tags_en": ["Greedy", "Array", "Dynamic Programming"], "tags_cn": ["\u8d2a\u5fc3", "\u6570\u7ec4", "\u52a8\u6001\u89c4\u5212"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n int minSideJumps(vector& obstacles) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int minSideJumps(int[] obstacles) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def minSideJumps(self, obstacles):\n \"\"\"\n :type obstacles: List[int]\n :rtype: int\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def minSideJumps(self, obstacles: List[int]) -> int:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nint minSideJumps(int* obstacles, int obstaclesSize){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int MinSideJumps(int[] obstacles) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[]} obstacles\n * @return {number}\n */\nvar minSideJumps = function(obstacles) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer[]} obstacles\n# @return {Integer}\ndef min_side_jumps(obstacles)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func minSideJumps(_ obstacles: [Int]) -> Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func minSideJumps(obstacles []int) int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def minSideJumps(obstacles: Array[Int]): Int = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun minSideJumps(obstacles: IntArray): Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn min_side_jumps(obstacles: Vec) -> i32 {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer[] $obstacles\n * @return Integer\n */\n function minSideJumps($obstacles) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function minSideJumps(obstacles: number[]): number {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (min-side-jumps obstacles)\n (-> (listof exact-integer?) exact-integer?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec min_side_jumps(Obstacles :: [integer()]) -> integer().\nmin_side_jumps(Obstacles) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec min_side_jumps(obstacles :: [integer]) :: integer\n def min_side_jumps(obstacles) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1824](https://leetcode-cn.com/problems/minimum-sideway-jumps)", "[\u6700\u5c11\u4fa7\u8df3\u6b21\u6570](/solution/1800-1899/1824.Minimum%20Sideway%20Jumps/README.md)", "`\u8d2a\u5fc3`,`\u6570\u7ec4`,`\u52a8\u6001\u89c4\u5212`", "\u4e2d\u7b49", ""], "md_table_row_en": ["[1824](https://leetcode.com/problems/minimum-sideway-jumps)", "[Minimum Sideway Jumps](/solution/1800-1899/1824.Minimum%20Sideway%20Jumps/README_EN.md)", "`Greedy`,`Array`,`Dynamic Programming`", "Medium", ""]}, {"question_id": "1951", "frontend_question_id": "1823", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/find-the-winner-of-the-circular-game", "url_en": "https://leetcode.com/problems/find-the-winner-of-the-circular-game", "relative_path_cn": "/solution/1800-1899/1823.Find%20the%20Winner%20of%20the%20Circular%20Game/README.md", "relative_path_en": "/solution/1800-1899/1823.Find%20the%20Winner%20of%20the%20Circular%20Game/README_EN.md", "title_cn": "\u627e\u51fa\u6e38\u620f\u7684\u83b7\u80dc\u8005", "title_en": "Find the Winner of the Circular Game", "question_title_slug": "find-the-winner-of-the-circular-game", "content_en": "There are n
friends that are playing a game. The friends are sitting in a circle and are numbered from 1
to n
in clockwise order. More formally, moving clockwise from the ith
friend brings you to the (i+1)th
friend for 1 <= i < n
, and moving clockwise from the nth
friend brings you to the 1st
friend.
\r\n\r\nThe rules of the game are as follows:
\r\n\r\n\r\n\t- Start at the
1st
friend. \r\n\t- Count the next
k
friends in the clockwise direction including the friend you started at. The counting wraps around the circle and may count some friends more than once. \r\n\t- The last friend you counted leaves the circle and loses the game.
\r\n\t- If there is still more than one friend in the circle, go back to step
2
starting from the friend immediately clockwise of the friend who just lost and repeat. \r\n\t- Else, the last friend in the circle wins the game.
\r\n
\r\n\r\nGiven the number of friends, n
, and an integer k
, return the winner of the game.
\r\n\r\n
\r\nExample 1:
\r\n
\r\n\r\nInput: n = 5, k = 2\r\nOutput: 3\r\nExplanation: Here are the steps of the game:\r\n1) Start at friend 1.\r\n2) Count 2 friends clockwise, which are friends 1 and 2.\r\n3) Friend 2 leaves the circle. Next start is friend 3.\r\n4) Count 2 friends clockwise, which are friends 3 and 4.\r\n5) Friend 4 leaves the circle. Next start is friend 5.\r\n6) Count 2 friends clockwise, which are friends 5 and 1.\r\n7) Friend 1 leaves the circle. Next start is friend 3.\r\n8) Count 2 friends clockwise, which are friends 3 and 5.\r\n9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.
\r\n\r\nExample 2:
\r\n\r\n\r\nInput: n = 6, k = 5\r\nOutput: 1\r\nExplanation: The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.\r\n
\r\n\r\n
\r\nConstraints:
\r\n\r\n\r\n\t1 <= k <= n <= 500
\r\n
", "content_cn": "\u5171\u6709 n
\u540d\u5c0f\u4f19\u4f34\u4e00\u8d77\u505a\u6e38\u620f\u3002\u5c0f\u4f19\u4f34\u4eec\u56f4\u6210\u4e00\u5708\uff0c\u6309 \u987a\u65f6\u9488\u987a\u5e8f \u4ece 1
\u5230 n
\u7f16\u53f7\u3002\u786e\u5207\u5730\u8bf4\uff0c\u4ece\u7b2c i
\u540d\u5c0f\u4f19\u4f34\u987a\u65f6\u9488\u79fb\u52a8\u4e00\u4f4d\u4f1a\u5230\u8fbe\u7b2c (i+1)
\u540d\u5c0f\u4f19\u4f34\u7684\u4f4d\u7f6e\uff0c\u5176\u4e2d 1 <= i < n
\uff0c\u4ece\u7b2c n
\u540d\u5c0f\u4f19\u4f34\u987a\u65f6\u9488\u79fb\u52a8\u4e00\u4f4d\u4f1a\u56de\u5230\u7b2c 1
\u540d\u5c0f\u4f19\u4f34\u7684\u4f4d\u7f6e\u3002
\n\n\u6e38\u620f\u9075\u5faa\u5982\u4e0b\u89c4\u5219\uff1a
\n\n\n\t- \u4ece\u7b2c
1
\u540d\u5c0f\u4f19\u4f34\u6240\u5728\u4f4d\u7f6e \u5f00\u59cb \u3002 \n\t- \u6cbf\u7740\u987a\u65f6\u9488\u65b9\u5411\u6570
k
\u540d\u5c0f\u4f19\u4f34\uff0c\u8ba1\u6570\u65f6\u9700\u8981 \u5305\u542b \u8d77\u59cb\u65f6\u7684\u90a3\u4f4d\u5c0f\u4f19\u4f34\u3002\u9010\u4e2a\u7ed5\u5708\u8fdb\u884c\u8ba1\u6570\uff0c\u4e00\u4e9b\u5c0f\u4f19\u4f34\u53ef\u80fd\u4f1a\u88ab\u6570\u8fc7\u4e0d\u6b62\u4e00\u6b21\u3002 \n\t- \u4f60\u6570\u5230\u7684\u6700\u540e\u4e00\u540d\u5c0f\u4f19\u4f34\u9700\u8981\u79bb\u5f00\u5708\u5b50\uff0c\u5e76\u89c6\u4f5c\u8f93\u6389\u6e38\u620f\u3002
\n\t- \u5982\u679c\u5708\u5b50\u4e2d\u4ecd\u7136\u6709\u4e0d\u6b62\u4e00\u540d\u5c0f\u4f19\u4f34\uff0c\u4ece\u521a\u521a\u8f93\u6389\u7684\u5c0f\u4f19\u4f34\u7684 \u987a\u65f6\u9488\u4e0b\u4e00\u4f4d \u5c0f\u4f19\u4f34 \u5f00\u59cb\uff0c\u56de\u5230\u6b65\u9aa4
2
\u7ee7\u7eed\u6267\u884c\u3002 \n\t- \u5426\u5219\uff0c\u5708\u5b50\u4e2d\u6700\u540e\u4e00\u540d\u5c0f\u4f19\u4f34\u8d62\u5f97\u6e38\u620f\u3002
\n
\n\n\u7ed9\u4f60\u53c2\u4e0e\u6e38\u620f\u7684\u5c0f\u4f19\u4f34\u603b\u6570 n
\uff0c\u548c\u4e00\u4e2a\u6574\u6570 k
\uff0c\u8fd4\u56de\u6e38\u620f\u7684\u83b7\u80dc\u8005\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n
\n\n\u8f93\u5165\uff1an = 5, k = 2\n\u8f93\u51fa\uff1a3\n\u89e3\u91ca\uff1a\u6e38\u620f\u8fd0\u884c\u6b65\u9aa4\u5982\u4e0b\uff1a\n1) \u4ece\u5c0f\u4f19\u4f34 1 \u5f00\u59cb\u3002\n2) \u987a\u65f6\u9488\u6570 2 \u540d\u5c0f\u4f19\u4f34\uff0c\u4e5f\u5c31\u662f\u5c0f\u4f19\u4f34 1 \u548c 2 \u3002\n3) \u5c0f\u4f19\u4f34 2 \u79bb\u5f00\u5708\u5b50\u3002\u4e0b\u4e00\u6b21\u4ece\u5c0f\u4f19\u4f34 3 \u5f00\u59cb\u3002\n4) \u987a\u65f6\u9488\u6570 2 \u540d\u5c0f\u4f19\u4f34\uff0c\u4e5f\u5c31\u662f\u5c0f\u4f19\u4f34 3 \u548c 4 \u3002\n5) \u5c0f\u4f19\u4f34 4 \u79bb\u5f00\u5708\u5b50\u3002\u4e0b\u4e00\u6b21\u4ece\u5c0f\u4f19\u4f34 5 \u5f00\u59cb\u3002\n6) \u987a\u65f6\u9488\u6570 2 \u540d\u5c0f\u4f19\u4f34\uff0c\u4e5f\u5c31\u662f\u5c0f\u4f19\u4f34 5 \u548c 1 \u3002\n7) \u5c0f\u4f19\u4f34 1 \u79bb\u5f00\u5708\u5b50\u3002\u4e0b\u4e00\u6b21\u4ece\u5c0f\u4f19\u4f34 3 \u5f00\u59cb\u3002\n8) \u987a\u65f6\u9488\u6570 2 \u540d\u5c0f\u4f19\u4f34\uff0c\u4e5f\u5c31\u662f\u5c0f\u4f19\u4f34 3 \u548c 5 \u3002\n9) \u5c0f\u4f19\u4f34 5 \u79bb\u5f00\u5708\u5b50\u3002\u53ea\u5269\u4e0b\u5c0f\u4f19\u4f34 3 \u3002\u6240\u4ee5\u5c0f\u4f19\u4f34 3 \u662f\u6e38\u620f\u7684\u83b7\u80dc\u8005\u3002
\n\n\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1an = 6, k = 5\n\u8f93\u51fa\uff1a1\n\u89e3\u91ca\uff1a\u5c0f\u4f19\u4f34\u79bb\u5f00\u5708\u5b50\u7684\u987a\u5e8f\uff1a5\u30014\u30016\u30012\u30013 \u3002\u5c0f\u4f19\u4f34 1 \u662f\u6e38\u620f\u7684\u83b7\u80dc\u8005\u3002\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n", "tags_en": ["Recursion", "Array", "Math", "Simulation"], "tags_cn": ["\u9012\u5f52", "\u6570\u7ec4", "\u6570\u5b66", "\u6a21\u62df"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n int findTheWinner(int n, int k) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int findTheWinner(int n, int k) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def findTheWinner(self, n, k):\n \"\"\"\n :type n: int\n :type k: int\n :rtype: int\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def findTheWinner(self, n: int, k: int) -> int:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nint findTheWinner(int n, int k){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int FindTheWinner(int n, int k) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number} n\n * @param {number} k\n * @return {number}\n */\nvar findTheWinner = function(n, k) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer} n\n# @param {Integer} k\n# @return {Integer}\ndef find_the_winner(n, k)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func findTheWinner(_ n: Int, _ k: Int) -> Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func findTheWinner(n int, k int) int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def findTheWinner(n: Int, k: Int): Int = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun findTheWinner(n: Int, k: Int): Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn find_the_winner(n: i32, k: i32) -> i32 {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer $n\n * @param Integer $k\n * @return Integer\n */\n function findTheWinner($n, $k) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function findTheWinner(n: number, k: number): number {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (find-the-winner n k)\n (-> exact-integer? exact-integer? exact-integer?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec find_the_winner(N :: integer(), K :: integer()) -> integer().\nfind_the_winner(N, K) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec find_the_winner(n :: integer, k :: integer) :: integer\n def find_the_winner(n, k) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1823](https://leetcode-cn.com/problems/find-the-winner-of-the-circular-game)", "[\u627e\u51fa\u6e38\u620f\u7684\u83b7\u80dc\u8005](/solution/1800-1899/1823.Find%20the%20Winner%20of%20the%20Circular%20Game/README.md)", "`\u9012\u5f52`,`\u6570\u7ec4`,`\u6570\u5b66`,`\u6a21\u62df`", "\u4e2d\u7b49", ""], "md_table_row_en": ["[1823](https://leetcode.com/problems/find-the-winner-of-the-circular-game)", "[Find the Winner of the Circular Game](/solution/1800-1899/1823.Find%20the%20Winner%20of%20the%20Circular%20Game/README_EN.md)", "`Recursion`,`Array`,`Math`,`Simulation`", "Medium", ""]}, {"question_id": "1950", "frontend_question_id": "1822", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/sign-of-the-product-of-an-array", "url_en": "https://leetcode.com/problems/sign-of-the-product-of-an-array", "relative_path_cn": "/solution/1800-1899/1822.Sign%20of%20the%20Product%20of%20an%20Array/README.md", "relative_path_en": "/solution/1800-1899/1822.Sign%20of%20the%20Product%20of%20an%20Array/README_EN.md", "title_cn": "\u6570\u7ec4\u5143\u7d20\u79ef\u7684\u7b26\u53f7", "title_en": "Sign of the Product of an Array", "question_title_slug": "sign-of-the-product-of-an-array", "content_en": "There is a function signFunc(x)
that returns:
\n\n\n\t1
if x
is positive. \n\t-1
if x
is negative. \n\t0
if x
is equal to 0
. \n
\n\nYou are given an integer array nums
. Let product
be the product of all values in the array nums
.
\n\nReturn signFunc(product)
.
\n\n
\nExample 1:
\n\n\nInput: nums = [-1,-2,-3,-4,3,2,1]\nOutput: 1\nExplanation: The product of all values in the array is 144, and signFunc(144) = 1\n
\n\nExample 2:
\n\n\nInput: nums = [1,5,0,2,-3]\nOutput: 0\nExplanation: The product of all values in the array is 0, and signFunc(0) = 0\n
\n\nExample 3:
\n\n\nInput: nums = [-1,1,-1,1,-1]\nOutput: -1\nExplanation: The product of all values in the array is -1, and signFunc(-1) = -1\n
\n\n
\nConstraints:
\n\n\n\t1 <= nums.length <= 1000
\n\t-100 <= nums[i] <= 100
\n
\n", "content_cn": "\u5df2\u77e5\u51fd\u6570\u00a0signFunc(x)
\u5c06\u4f1a\u6839\u636e x
\u7684\u6b63\u8d1f\u8fd4\u56de\u7279\u5b9a\u503c\uff1a
\n\n\n\t- \u5982\u679c
x
\u662f\u6b63\u6570\uff0c\u8fd4\u56de 1
\u3002 \n\t- \u5982\u679c
x
\u662f\u8d1f\u6570\uff0c\u8fd4\u56de -1
\u3002 \n\t- \u5982\u679c
x
\u662f\u7b49\u4e8e 0
\uff0c\u8fd4\u56de 0
\u3002 \n
\n\n\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 nums
\u3002\u4ee4 product
\u4e3a\u6570\u7ec4 nums
\u4e2d\u6240\u6709\u5143\u7d20\u503c\u7684\u4e58\u79ef\u3002
\n\n\u8fd4\u56de signFunc(product)
\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1anums = [-1,-2,-3,-4,3,2,1]\n\u8f93\u51fa\uff1a1\n\u89e3\u91ca\uff1a\u6570\u7ec4\u4e2d\u6240\u6709\u503c\u7684\u4e58\u79ef\u662f 144 \uff0c\u4e14 signFunc(144) = 1\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1anums = [1,5,0,2,-3]\n\u8f93\u51fa\uff1a0\n\u89e3\u91ca\uff1a\u6570\u7ec4\u4e2d\u6240\u6709\u503c\u7684\u4e58\u79ef\u662f 0 \uff0c\u4e14 signFunc(0) = 0\n
\n\n\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1anums = [-1,1,-1,1,-1]\n\u8f93\u51fa\uff1a-1\n\u89e3\u91ca\uff1a\u6570\u7ec4\u4e2d\u6240\u6709\u503c\u7684\u4e58\u79ef\u662f -1 \uff0c\u4e14 signFunc(-1) = -1\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\t1 <= nums.length <= 1000
\n\t-100 <= nums[i] <= 100
\n
\n", "tags_en": ["Array", "Math"], "tags_cn": ["\u6570\u7ec4", "\u6570\u5b66"], "difficulty_en": "Easy", "difficulty_cn": "\u7b80\u5355", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n int arraySign(vector& nums) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int arraySign(int[] nums) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def arraySign(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def arraySign(self, nums: List[int]) -> int:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nint arraySign(int* nums, int numsSize){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int ArraySign(int[] nums) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[]} nums\n * @return {number}\n */\nvar arraySign = function(nums) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer[]} nums\n# @return {Integer}\ndef array_sign(nums)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func arraySign(_ nums: [Int]) -> Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func arraySign(nums []int) int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def arraySign(nums: Array[Int]): Int = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun arraySign(nums: IntArray): Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn array_sign(nums: Vec) -> i32 {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @return Integer\n */\n function arraySign($nums) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function arraySign(nums: number[]): number {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (array-sign nums)\n (-> (listof exact-integer?) exact-integer?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec array_sign(Nums :: [integer()]) -> integer().\narray_sign(Nums) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec array_sign(nums :: [integer]) :: integer\n def array_sign(nums) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1822](https://leetcode-cn.com/problems/sign-of-the-product-of-an-array)", "[\u6570\u7ec4\u5143\u7d20\u79ef\u7684\u7b26\u53f7](/solution/1800-1899/1822.Sign%20of%20the%20Product%20of%20an%20Array/README.md)", "`\u6570\u7ec4`,`\u6570\u5b66`", "\u7b80\u5355", ""], "md_table_row_en": ["[1822](https://leetcode.com/problems/sign-of-the-product-of-an-array)", "[Sign of the Product of an Array](/solution/1800-1899/1822.Sign%20of%20the%20Product%20of%20an%20Array/README_EN.md)", "`Array`,`Math`", "Easy", ""]}, {"question_id": "1949", "frontend_question_id": "1804", "paid_only": true, "paid_only_cn": true, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/implement-trie-ii-prefix-tree", "url_en": "https://leetcode.com/problems/implement-trie-ii-prefix-tree", "relative_path_cn": "/solution/1800-1899/1804.Implement%20Trie%20II%20%28Prefix%20Tree%29/README.md", "relative_path_en": "/solution/1800-1899/1804.Implement%20Trie%20II%20%28Prefix%20Tree%29/README_EN.md", "title_cn": "\u5b9e\u73b0 Trie \uff08\u524d\u7f00\u6811\uff09 II", "title_en": "Implement Trie II (Prefix Tree)", "question_title_slug": "implement-trie-ii-prefix-tree", "content_en": "A trie (pronounced as "try") or prefix tree is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There are various applications of this data structure, such as autocomplete and spellchecker.
\n\nImplement the Trie class:
\n\n\n\tTrie()
Initializes the trie object. \n\tvoid insert(String word)
Inserts the string word
into the trie. \n\tint countWordsEqualTo(String word)
Returns the number of instances of the string word
in the trie. \n\tint countWordsStartingWith(String prefix)
Returns the number of strings in the trie that have the string prefix
as a prefix. \n\tvoid erase(String word)
Erases the string word
from the trie. \n
\n\n
\nExample 1:
\n\n\nInput\n["Trie", "insert", "insert", "countWordsEqualTo", "countWordsStartingWith", "erase", "countWordsEqualTo", "countWordsStartingWith", "erase", "countWordsStartingWith"]\n[[], ["apple"], ["apple"], ["apple"], ["app"], ["apple"], ["apple"], ["app"], ["apple"], ["app"]]\nOutput\n[null, null, null, 2, 2, null, 1, 1, null, 0]\n\nExplanation\nTrie trie = new Trie();\ntrie.insert("apple"); // Inserts "apple".\ntrie.insert("apple"); // Inserts another "apple".\ntrie.countWordsEqualTo("apple"); // There are two instances of "apple" so return 2.\ntrie.countWordsStartingWith("app"); // "app" is a prefix of "apple" so return 2.\ntrie.erase("apple"); // Erases one "apple".\ntrie.countWordsEqualTo("apple"); // Now there is only one instance of "apple" so return 1.\ntrie.countWordsStartingWith("app"); // return 1\ntrie.erase("apple"); // Erases "apple". Now the trie is empty.\ntrie.countWordsStartingWith("app"); // return 0\n
\n\n
\nConstraints:
\n\n\n\t1 <= word.length, prefix.length <= 2000
\n\tword
and prefix
consist only of lowercase English letters. \n\t- At most
3 * 104
calls in total will be made to insert
, countWordsEqualTo
, countWordsStartingWith
, and erase
. \n\t- It is guaranteed that for any function call to
erase
, the string word
will exist in the trie. \n
\n", "content_cn": "\u524d\u7f00\u6811\uff08trie\u00a0\uff0c\u53d1\u97f3\u4e3a \"try\"\uff09\u662f\u4e00\u4e2a\u6811\u72b6\u7684\u6570\u636e\u7ed3\u6784\uff0c\u7528\u4e8e\u9ad8\u6548\u5730\u5b58\u50a8\u548c\u68c0\u7d22\u4e00\u7cfb\u5217\u5b57\u7b26\u4e32\u7684\u524d\u7f00\u3002\u524d\u7f00\u6811\u6709\u8bb8\u591a\u5e94\u7528\uff0c\u5982\u81ea\u52a8\u8865\u5168\u548c\u62fc\u5199\u68c0\u67e5\u3002
\n\n\u5b9e\u73b0\u524d\u7f00\u6811 Trie \u7c7b\uff1a
\n\n\n\tTrie()
\u00a0\u521d\u59cb\u5316\u524d\u7f00\u6811\u5bf9\u8c61\u3002 \n\tvoid insert(String word)
\u00a0\u5c06\u5b57\u7b26\u4e32\u00a0word
\u00a0\u63d2\u5165\u524d\u7f00\u6811\u4e2d\u3002 \n\tint countWordsEqualTo(String word)
\u00a0\u8fd4\u56de\u524d\u7f00\u6811\u4e2d\u5b57\u7b26\u4e32\u00a0word
\u00a0\u7684\u5b9e\u4f8b\u4e2a\u6570\u3002 \n\tint countWordsStartingWith(String prefix)
\u00a0\u8fd4\u56de\u524d\u7f00\u6811\u4e2d\u4ee5\u00a0prefix
\u00a0\u4e3a\u524d\u7f00\u7684\u5b57\u7b26\u4e32\u4e2a\u6570\u3002 \n\tvoid erase(String word)
\u00a0\u4ece\u524d\u7f00\u6811\u4e2d\u79fb\u9664\u5b57\u7b26\u4e32\u00a0word
\u3002 \n
\n\n\u00a0
\n\n\u793a\u4f8b 1:
\n\n\u8f93\u5165\n[\"Trie\", \"insert\", \"insert\", \"countWordsEqualTo\", \"countWordsStartingWith\", \"erase\", \"countWordsEqualTo\", \"countWordsStartingWith\", \"erase\", \"countWordsStartingWith\"]\n[[], [\"apple\"], [\"apple\"], [\"apple\"], [\"app\"], [\"apple\"], [\"apple\"], [\"app\"], [\"apple\"], [\"app\"]]\n\u8f93\u51fa\n[null, null, null, 2, 2, null, 1, 1, null, 0]\n\n\u89e3\u91ca\nTrie trie = new Trie();\ntrie.insert(\"apple\"); // \u63d2\u5165 \"apple\"\u3002\ntrie.insert(\"apple\"); // \u63d2\u5165\u53e6\u4e00\u4e2a \"apple\"\u3002\ntrie.countWordsEqualTo(\"apple\"); // \u6709\u4e24\u4e2a \"apple\" \u5b9e\u4f8b\uff0c\u6240\u4ee5\u8fd4\u56de 2\u3002\ntrie.countWordsStartingWith(\"app\"); // \"app\" \u662f \"apple\" \u7684\u524d\u7f00\uff0c\u6240\u4ee5\u8fd4\u56de 2\u3002\ntrie.erase(\"apple\"); // \u79fb\u9664\u4e00\u4e2a \"apple\"\u3002\ntrie.countWordsEqualTo(\"apple\"); // \u73b0\u5728\u53ea\u6709\u4e00\u4e2a \"apple\" \u5b9e\u4f8b\uff0c\u6240\u4ee5\u8fd4\u56de 1\u3002\ntrie.countWordsStartingWith(\"app\"); // \u8fd4\u56de 1\ntrie.erase(\"apple\"); // \u79fb\u9664 \"apple\"\u3002\u73b0\u5728\u524d\u7f00\u6811\u662f\u7a7a\u7684\u3002\ntrie.countWordsStartingWith(\"app\"); // \u8fd4\u56de 0\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\t1 <= word.length, prefix.length <= 2000
\n\tword
\u00a0\u548c\u00a0prefix
\u00a0\u53ea\u5305\u542b\u5c0f\u5199\u82f1\u6587\u5b57\u6bcd\u3002 \n\tinsert
\u3001\u00a0countWordsEqualTo
\u3001\u00a0countWordsStartingWith
\u00a0\u548c\u00a0erase
\u00a0\u603b\u5171\u8c03\u7528\u6700\u591a\u00a03 * 104
\u00a0\u6b21\u3002 \n\t- \u4fdd\u8bc1\u6bcf\u6b21\u8c03\u7528\u00a0
erase
\u00a0\u65f6\uff0c\u5b57\u7b26\u4e32\u00a0word
\u00a0\u603b\u662f\u5b58\u5728\u4e8e\u524d\u7f00\u6811\u4e2d\u3002 \n
\n", "tags_en": ["Design", "Trie", "Hash Table", "String"], "tags_cn": ["\u8bbe\u8ba1", "\u5b57\u5178\u6811", "\u54c8\u5e0c\u8868", "\u5b57\u7b26\u4e32"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Trie {\npublic:\n Trie() {\n\n }\n \n void insert(string word) {\n\n }\n \n int countWordsEqualTo(string word) {\n\n }\n \n int countWordsStartingWith(string prefix) {\n\n }\n \n void erase(string word) {\n\n }\n};\n\n/**\n * Your Trie object will be instantiated and called as such:\n * Trie* obj = new Trie();\n * obj->insert(word);\n * int param_2 = obj->countWordsEqualTo(word);\n * int param_3 = obj->countWordsStartingWith(prefix);\n * obj->erase(word);\n */", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Trie {\n\n public Trie() {\n\n }\n \n public void insert(String word) {\n\n }\n \n public int countWordsEqualTo(String word) {\n\n }\n \n public int countWordsStartingWith(String prefix) {\n\n }\n \n public void erase(String word) {\n\n }\n}\n\n/**\n * Your Trie object will be instantiated and called as such:\n * Trie obj = new Trie();\n * obj.insert(word);\n * int param_2 = obj.countWordsEqualTo(word);\n * int param_3 = obj.countWordsStartingWith(prefix);\n * obj.erase(word);\n */", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Trie(object):\n\n def __init__(self):\n\n\n def insert(self, word):\n \"\"\"\n :type word: str\n :rtype: None\n \"\"\"\n\n\n def countWordsEqualTo(self, word):\n \"\"\"\n :type word: str\n :rtype: int\n \"\"\"\n\n\n def countWordsStartingWith(self, prefix):\n \"\"\"\n :type prefix: str\n :rtype: int\n \"\"\"\n\n\n def erase(self, word):\n \"\"\"\n :type word: str\n :rtype: None\n \"\"\"\n\n\n\n# Your Trie object will be instantiated and called as such:\n# obj = Trie()\n# obj.insert(word)\n# param_2 = obj.countWordsEqualTo(word)\n# param_3 = obj.countWordsStartingWith(prefix)\n# obj.erase(word)", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Trie:\n\n def __init__(self):\n\n\n def insert(self, word: str) -> None:\n\n\n def countWordsEqualTo(self, word: str) -> int:\n\n\n def countWordsStartingWith(self, prefix: str) -> int:\n\n\n def erase(self, word: str) -> None:\n\n\n\n# Your Trie object will be instantiated and called as such:\n# obj = Trie()\n# obj.insert(word)\n# param_2 = obj.countWordsEqualTo(word)\n# param_3 = obj.countWordsStartingWith(prefix)\n# obj.erase(word)", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\n\ntypedef struct {\n \n} Trie;\n\n\nTrie* trieCreate() {\n \n}\n\nvoid trieInsert(Trie* obj, char * word) {\n \n}\n\nint trieCountWordsEqualTo(Trie* obj, char * word) {\n \n}\n\nint trieCountWordsStartingWith(Trie* obj, char * prefix) {\n \n}\n\nvoid trieErase(Trie* obj, char * word) {\n \n}\n\nvoid trieFree(Trie* obj) {\n \n}\n\n/**\n * Your Trie struct will be instantiated and called as such:\n * Trie* obj = trieCreate();\n * trieInsert(obj, word);\n \n * int param_2 = trieCountWordsEqualTo(obj, word);\n \n * int param_3 = trieCountWordsStartingWith(obj, prefix);\n \n * trieErase(obj, word);\n \n * trieFree(obj);\n*/", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Trie {\n\n public Trie() {\n\n }\n \n public void Insert(string word) {\n\n }\n \n public int CountWordsEqualTo(string word) {\n\n }\n \n public int CountWordsStartingWith(string prefix) {\n\n }\n \n public void Erase(string word) {\n\n }\n}\n\n/**\n * Your Trie object will be instantiated and called as such:\n * Trie obj = new Trie();\n * obj.Insert(word);\n * int param_2 = obj.CountWordsEqualTo(word);\n * int param_3 = obj.CountWordsStartingWith(prefix);\n * obj.Erase(word);\n */", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "\nvar Trie = function() {\n\n};\n\n/** \n * @param {string} word\n * @return {void}\n */\nTrie.prototype.insert = function(word) {\n\n};\n\n/** \n * @param {string} word\n * @return {number}\n */\nTrie.prototype.countWordsEqualTo = function(word) {\n\n};\n\n/** \n * @param {string} prefix\n * @return {number}\n */\nTrie.prototype.countWordsStartingWith = function(prefix) {\n\n};\n\n/** \n * @param {string} word\n * @return {void}\n */\nTrie.prototype.erase = function(word) {\n\n};\n\n/**\n * Your Trie object will be instantiated and called as such:\n * var obj = new Trie()\n * obj.insert(word)\n * var param_2 = obj.countWordsEqualTo(word)\n * var param_3 = obj.countWordsStartingWith(prefix)\n * obj.erase(word)\n */", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "class Trie\n def initialize()\n\n end\n\n\n=begin\n :type word: String\n :rtype: Void\n=end\n def insert(word)\n\n end\n\n\n=begin\n :type word: String\n :rtype: Integer\n=end\n def count_words_equal_to(word)\n\n end\n\n\n=begin\n :type prefix: String\n :rtype: Integer\n=end\n def count_words_starting_with(prefix)\n\n end\n\n\n=begin\n :type word: String\n :rtype: Void\n=end\n def erase(word)\n\n end\n\n\nend\n\n# Your Trie object will be instantiated and called as such:\n# obj = Trie.new()\n# obj.insert(word)\n# param_2 = obj.count_words_equal_to(word)\n# param_3 = obj.count_words_starting_with(prefix)\n# obj.erase(word)", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "\nclass Trie {\n\n init() {\n\n }\n \n func insert(_ word: String) {\n\n }\n \n func countWordsEqualTo(_ word: String) -> Int {\n\n }\n \n func countWordsStartingWith(_ prefix: String) -> Int {\n\n }\n \n func erase(_ word: String) {\n\n }\n}\n\n/**\n * Your Trie object will be instantiated and called as such:\n * let obj = Trie()\n * obj.insert(word)\n * let ret_2: Int = obj.countWordsEqualTo(word)\n * let ret_3: Int = obj.countWordsStartingWith(prefix)\n * obj.erase(word)\n */", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "type Trie struct {\n\n}\n\n\nfunc Constructor() Trie {\n\n}\n\n\nfunc (this *Trie) Insert(word string) {\n\n}\n\n\nfunc (this *Trie) CountWordsEqualTo(word string) int {\n\n}\n\n\nfunc (this *Trie) CountWordsStartingWith(prefix string) int {\n\n}\n\n\nfunc (this *Trie) Erase(word string) {\n\n}\n\n\n/**\n * Your Trie object will be instantiated and called as such:\n * obj := Constructor();\n * obj.Insert(word);\n * param_2 := obj.CountWordsEqualTo(word);\n * param_3 := obj.CountWordsStartingWith(prefix);\n * obj.Erase(word);\n */", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "class Trie() {\n\n def insert(word: String) {\n \n }\n\n def countWordsEqualTo(word: String): Int = {\n \n }\n\n def countWordsStartingWith(prefix: String): Int = {\n \n }\n\n def erase(word: String) {\n \n }\n\n}\n\n/**\n * Your Trie object will be instantiated and called as such:\n * var obj = new Trie()\n * obj.insert(word)\n * var param_2 = obj.countWordsEqualTo(word)\n * var param_3 = obj.countWordsStartingWith(prefix)\n * obj.erase(word)\n */", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Trie() {\n\n fun insert(word: String) {\n\n }\n\n fun countWordsEqualTo(word: String): Int {\n\n }\n\n fun countWordsStartingWith(prefix: String): Int {\n\n }\n\n fun erase(word: String) {\n\n }\n\n}\n\n/**\n * Your Trie object will be instantiated and called as such:\n * var obj = Trie()\n * obj.insert(word)\n * var param_2 = obj.countWordsEqualTo(word)\n * var param_3 = obj.countWordsStartingWith(prefix)\n * obj.erase(word)\n */", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "struct Trie {\n\n}\n\n\n/**\n * `&self` means the method takes an immutable reference.\n * If you need a mutable reference, change it to `&mut self` instead.\n */\nimpl Trie {\n\n fn new() -> Self {\n\n }\n \n fn insert(&self, word: String) {\n\n }\n \n fn count_words_equal_to(&self, word: String) -> i32 {\n\n }\n \n fn count_words_starting_with(&self, prefix: String) -> i32 {\n\n }\n \n fn erase(&self, word: String) {\n\n }\n}\n\n/**\n * Your Trie object will be instantiated and called as such:\n * let obj = Trie::new();\n * obj.insert(word);\n * let ret_2: i32 = obj.count_words_equal_to(word);\n * let ret_3: i32 = obj.count_words_starting_with(prefix);\n * obj.erase(word);\n */", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Trie {\n /**\n */\n function __construct() {\n\n }\n\n /**\n * @param String $word\n * @return NULL\n */\n function insert($word) {\n\n }\n\n /**\n * @param String $word\n * @return Integer\n */\n function countWordsEqualTo($word) {\n\n }\n\n /**\n * @param String $prefix\n * @return Integer\n */\n function countWordsStartingWith($prefix) {\n\n }\n\n /**\n * @param String $word\n * @return NULL\n */\n function erase($word) {\n\n }\n}\n\n/**\n * Your Trie object will be instantiated and called as such:\n * $obj = Trie();\n * $obj->insert($word);\n * $ret_2 = $obj->countWordsEqualTo($word);\n * $ret_3 = $obj->countWordsStartingWith($prefix);\n * $obj->erase($word);\n */", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "class Trie {\n constructor() {\n\n }\n\n insert(word: string): void {\n\n }\n\n countWordsEqualTo(word: string): number {\n\n }\n\n countWordsStartingWith(prefix: string): number {\n\n }\n\n erase(word: string): void {\n\n }\n}\n\n/**\n * Your Trie object will be instantiated and called as such:\n * var obj = new Trie()\n * obj.insert(word)\n * var param_2 = obj.countWordsEqualTo(word)\n * var param_3 = obj.countWordsStartingWith(prefix)\n * obj.erase(word)\n */", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define trie%\n (class object%\n (super-new)\n (init-field)\n \n ; insert : string? -> void?\n (define/public (insert word)\n\n )\n ; count-words-equal-to : string? -> exact-integer?\n (define/public (count-words-equal-to word)\n\n )\n ; count-words-starting-with : string? -> exact-integer?\n (define/public (count-words-starting-with prefix)\n\n )\n ; erase : string? -> void?\n (define/public (erase word)\n\n )))\n\n;; Your trie% object will be instantiated and called as such:\n;; (define obj (new trie%))\n;; (send obj insert word)\n;; (define param_2 (send obj count-words-equal-to word))\n;; (define param_3 (send obj count-words-starting-with prefix))\n;; (send obj erase word)", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec trie_init_() -> any().\ntrie_init_() ->\n .\n\n-spec trie_insert(Word :: unicode:unicode_binary()) -> any().\ntrie_insert(Word) ->\n .\n\n-spec trie_count_words_equal_to(Word :: unicode:unicode_binary()) -> integer().\ntrie_count_words_equal_to(Word) ->\n .\n\n-spec trie_count_words_starting_with(Prefix :: unicode:unicode_binary()) -> integer().\ntrie_count_words_starting_with(Prefix) ->\n .\n\n-spec trie_erase(Word :: unicode:unicode_binary()) -> any().\ntrie_erase(Word) ->\n .\n\n\n%% Your functions will be called as such:\n%% trie_init_(),\n%% trie_insert(Word),\n%% Param_2 = trie_count_words_equal_to(Word),\n%% Param_3 = trie_count_words_starting_with(Prefix),\n%% trie_erase(Word),\n\n%% trie_init_ will be called before every test case, in which you can do some necessary initializations.", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Trie do\n @spec init_() :: any\n def init_() do\n\n end\n\n @spec insert(word :: String.t) :: any\n def insert(word) do\n\n end\n\n @spec count_words_equal_to(word :: String.t) :: integer\n def count_words_equal_to(word) do\n\n end\n\n @spec count_words_starting_with(prefix :: String.t) :: integer\n def count_words_starting_with(prefix) do\n\n end\n\n @spec erase(word :: String.t) :: any\n def erase(word) do\n\n end\nend\n\n# Your functions will be called as such:\n# Trie.init_()\n# Trie.insert(word)\n# param_2 = Trie.count_words_equal_to(word)\n# param_3 = Trie.count_words_starting_with(prefix)\n# Trie.erase(word)\n\n# Trie.init_ will be called before every test case, in which you can do some necessary initializations.", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1804](https://leetcode-cn.com/problems/implement-trie-ii-prefix-tree)", "[\u5b9e\u73b0 Trie \uff08\u524d\u7f00\u6811\uff09 II](/solution/1800-1899/1804.Implement%20Trie%20II%20%28Prefix%20Tree%29/README.md)", "`\u8bbe\u8ba1`,`\u5b57\u5178\u6811`,`\u54c8\u5e0c\u8868`,`\u5b57\u7b26\u4e32`", "\u4e2d\u7b49", "\ud83d\udd12"], "md_table_row_en": ["[1804](https://leetcode.com/problems/implement-trie-ii-prefix-tree)", "[Implement Trie II (Prefix Tree)](/solution/1800-1899/1804.Implement%20Trie%20II%20%28Prefix%20Tree%29/README_EN.md)", "`Design`,`Trie`,`Hash Table`,`String`", "Medium", "\ud83d\udd12"]}, {"question_id": "1948", "frontend_question_id": "1795", "paid_only": true, "paid_only_cn": true, "category": "Database", "url_cn": "https://leetcode-cn.com/problems/rearrange-products-table", "url_en": "https://leetcode.com/problems/rearrange-products-table", "relative_path_cn": "/solution/1700-1799/1795.Rearrange%20Products%20Table/README.md", "relative_path_en": "/solution/1700-1799/1795.Rearrange%20Products%20Table/README_EN.md", "title_cn": "\u6bcf\u4e2a\u4ea7\u54c1\u5728\u4e0d\u540c\u5546\u5e97\u7684\u4ef7\u683c", "title_en": "Rearrange Products Table", "question_title_slug": "rearrange-products-table", "content_en": "Table: Products
\r\n\r\n\r\n+-------------+---------+\r\n| Column Name | Type |\r\n+-------------+---------+\r\n| product_id | int |\r\n| store1 | int |\r\n| store2 | int |\r\n| store3 | int |\r\n+-------------+---------+\r\nproduct_id is the primary key for this table.\r\nEach row in this table indicates the product's price in 3 different stores: store1, store2, and store3.\r\nIf the product is not available in a store, the price will be null in that store's column.\r\n
\r\n\r\n
\r\n\r\nWrite an SQL query to rearrange the Products
table so that each row has (product_id, store, price)
. If a product is not available in a store, do not include a row with that product_id
and store
combination in the result table.
\r\n\r\nReturn the result table in any order.
\r\n\r\nThe query result format is in the following example:
\r\n\r\n
\r\n\r\n\r\nProducts table:\r\n+------------+--------+--------+--------+\r\n| product_id | store1 | store2 | store3 |\r\n+------------+--------+--------+--------+\r\n| 0 | 95 | 100 | 105 |\r\n| 1 | 70 | null | 80 |\r\n+------------+--------+--------+--------+\r\n\r\nResult table:\r\n+------------+--------+-------+\r\n| product_id | store | price |\r\n+------------+--------+-------+\r\n| 0 | store1 | 95 |\r\n| 0 | store2 | 100 |\r\n| 0 | store3 | 105 |\r\n| 1 | store1 | 70 |\r\n| 1 | store3 | 80 |\r\n+------------+--------+-------+\r\n\r\nProduct 0 is available in all three stores with prices 95, 100, and 105 respectively.\r\nProduct 1 is available in store1 with price 70 and store3 with price 80. The product is not available in store2.\r\n
", "content_cn": "\u8868\uff1aProducts
\n\n\n+-------------+---------+\n| Column Name | Type |\n+-------------+---------+\n| product_id | int |\n| store1 | int |\n| store2 | int |\n| store3 | int |\n+-------------+---------+\n\u8fd9\u5f20\u8868\u7684\u4e3b\u952e\u662fproduct_id\uff08\u4ea7\u54c1Id\uff09\u3002\n\u6bcf\u884c\u5b58\u50a8\u4e86\u8fd9\u4e00\u4ea7\u54c1\u5728\u4e0d\u540c\u5546\u5e97store1, store2, store3\u7684\u4ef7\u683c\u3002\n\u5982\u679c\u8fd9\u4e00\u4ea7\u54c1\u5728\u5546\u5e97\u91cc\u6ca1\u6709\u51fa\u552e\uff0c\u5219\u503c\u5c06\u4e3anull\u3002\n
\n\n\u00a0
\n\n\u8bf7\u4f60\u91cd\u6784 Products
\u8868\uff0c\u67e5\u8be2\u6bcf\u4e2a\u4ea7\u54c1\u5728\u4e0d\u540c\u5546\u5e97\u7684\u4ef7\u683c\uff0c\u4f7f\u5f97\u8f93\u51fa\u7684\u683c\u5f0f\u53d8\u4e3a(product_id, store, price)
\u3002\u5982\u679c\u8fd9\u4e00\u4ea7\u54c1\u5728\u5546\u5e97\u91cc\u6ca1\u6709\u51fa\u552e\uff0c\u5219\u4e0d\u8f93\u51fa\u8fd9\u4e00\u884c\u3002
\n\n\u8f93\u51fa\u7ed3\u679c\u8868\u4e2d\u7684\u987a\u5e8f\u4e0d\u4f5c\u8981\u6c42\u3002
\n\n\u67e5\u8be2\u8f93\u51fa\u683c\u5f0f\u8bf7\u53c2\u8003\u4e0b\u9762\u793a\u4f8b\u3002
\n\n\u00a0
\n\n\nProducts table:\n+------------+--------+--------+--------+\n| product_id | store1 | store2 | store3 |\n+------------+--------+--------+--------+\n| 0 | 95 | 100 | 105 |\n| 1 | 70 | null | 80 |\n+------------+--------+--------+--------+\n\nResult table:\n+------------+--------+-------+\n| product_id | store | price |\n+------------+--------+-------+\n| 0 | store1 | 95 |\n| 0 | store2 | 100 |\n| 0 | store3 | 105 |\n| 1 | store1 | 70 |\n| 1 | store3 | 80 |\n+------------+--------+-------+\n\n\u4ea7\u54c10\u5728store1\uff0cstore2,store3\u7684\u4ef7\u683c\u5206\u522b\u4e3a95,100,105\u3002\n\u4ea7\u54c11\u5728store1\uff0cstore3\u7684\u4ef7\u683c\u5206\u522b\u4e3a70,80\u3002\u5728store2\u65e0\u6cd5\u4e70\u5230\u3002\n
\n", "tags_en": ["Database"], "tags_cn": ["\u6570\u636e\u5e93"], "difficulty_en": "Easy", "difficulty_cn": "\u7b80\u5355", "code_snippets": [{"lang": "MySQL", "langSlug": "mysql", "code": "# Write your MySQL query statement below", "__typename": "CodeSnippetNode"}, {"lang": "MS SQL Server", "langSlug": "mssql", "code": "/* Write your T-SQL query statement below */", "__typename": "CodeSnippetNode"}, {"lang": "Oracle", "langSlug": "oraclesql", "code": "/* Write your PL/SQL query statement below */", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1795](https://leetcode-cn.com/problems/rearrange-products-table)", "[\u6bcf\u4e2a\u4ea7\u54c1\u5728\u4e0d\u540c\u5546\u5e97\u7684\u4ef7\u683c](/solution/1700-1799/1795.Rearrange%20Products%20Table/README.md)", "`\u6570\u636e\u5e93`", "\u7b80\u5355", "\ud83d\udd12"], "md_table_row_en": ["[1795](https://leetcode.com/problems/rearrange-products-table)", "[Rearrange Products Table](/solution/1700-1799/1795.Rearrange%20Products%20Table/README_EN.md)", "`Database`", "Easy", "\ud83d\udd12"]}, {"question_id": "1947", "frontend_question_id": "1819", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/number-of-different-subsequences-gcds", "url_en": "https://leetcode.com/problems/number-of-different-subsequences-gcds", "relative_path_cn": "/solution/1800-1899/1819.Number%20of%20Different%20Subsequences%20GCDs/README.md", "relative_path_en": "/solution/1800-1899/1819.Number%20of%20Different%20Subsequences%20GCDs/README_EN.md", "title_cn": "\u5e8f\u5217\u4e2d\u4e0d\u540c\u6700\u5927\u516c\u7ea6\u6570\u7684\u6570\u76ee", "title_en": "Number of Different Subsequences GCDs", "question_title_slug": "number-of-different-subsequences-gcds", "content_en": "You are given an array nums
that consists of positive integers.
\n\nThe GCD of a sequence of numbers is defined as the greatest integer that divides all the numbers in the sequence evenly.
\n\n\n\t- For example, the GCD of the sequence
[4,6,16]
is 2
. \n
\n\nA subsequence of an array is a sequence that can be formed by removing some elements (possibly none) of the array.
\n\n\n\t- For example,
[2,5,10]
is a subsequence of [1,2,1,2,4,1,5,10]
. \n
\n\nReturn the number of different GCDs among all non-empty subsequences of nums
.
\n\n
\nExample 1:
\n
\n\nInput: nums = [6,10,3]\nOutput: 5\nExplanation: The figure shows all the non-empty subsequences and their GCDs.\nThe different GCDs are 6, 10, 3, 2, and 1.\n
\n\nExample 2:
\n\n\nInput: nums = [5,15,40,5,6]\nOutput: 7\n
\n\n
\nConstraints:
\n\n\n\t1 <= nums.length <= 105
\n\t1 <= nums[i] <= 2 * 105
\n
\n", "content_cn": "\u7ed9\u4f60\u4e00\u4e2a\u7531\u6b63\u6574\u6570\u7ec4\u6210\u7684\u6570\u7ec4 nums
\u3002
\n\n\u6570\u5b57\u5e8f\u5217\u7684 \u6700\u5927\u516c\u7ea6\u6570 \u5b9a\u4e49\u4e3a\u5e8f\u5217\u4e2d\u6240\u6709\u6574\u6570\u7684\u5171\u6709\u7ea6\u6570\u4e2d\u7684\u6700\u5927\u6574\u6570\u3002
\n\n\n\t- \u4f8b\u5982\uff0c\u5e8f\u5217
[4,6,16]
\u7684\u6700\u5927\u516c\u7ea6\u6570\u662f 2
\u3002 \n
\n\n\u6570\u7ec4\u7684\u4e00\u4e2a \u5b50\u5e8f\u5217 \u672c\u8d28\u662f\u4e00\u4e2a\u5e8f\u5217\uff0c\u53ef\u4ee5\u901a\u8fc7\u5220\u9664\u6570\u7ec4\u4e2d\u7684\u67d0\u4e9b\u5143\u7d20\uff08\u6216\u8005\u4e0d\u5220\u9664\uff09\u5f97\u5230\u3002
\n\n\n\t- \u4f8b\u5982\uff0c
[2,5,10]
\u662f [1,2,1,2,4,1,5,10]
\u7684\u4e00\u4e2a\u5b50\u5e8f\u5217\u3002 \n
\n\n\u8ba1\u7b97\u5e76\u8fd4\u56de nums
\u7684\u6240\u6709 \u975e\u7a7a \u5b50\u5e8f\u5217\u4e2d \u4e0d\u540c \u6700\u5927\u516c\u7ea6\u6570\u7684 \u6570\u76ee \u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n
\n\n\u8f93\u5165\uff1anums = [6,10,3]\n\u8f93\u51fa\uff1a5\n\u89e3\u91ca\uff1a\u4e0a\u56fe\u663e\u793a\u4e86\u6240\u6709\u7684\u975e\u7a7a\u5b50\u5e8f\u5217\u4e0e\u5404\u81ea\u7684\u6700\u5927\u516c\u7ea6\u6570\u3002\n\u4e0d\u540c\u7684\u6700\u5927\u516c\u7ea6\u6570\u4e3a 6 \u300110 \u30013 \u30012 \u548c 1 \u3002\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1anums = [5,15,40,5,6]\n\u8f93\u51fa\uff1a7\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\t1 <= nums.length <= 105
\n\t1 <= nums[i] <= 2 * 105
\n
\n", "tags_en": ["Array", "Math", "Counting", "Number Theory"], "tags_cn": ["\u6570\u7ec4", "\u6570\u5b66", "\u8ba1\u6570", "\u6570\u8bba"], "difficulty_en": "Hard", "difficulty_cn": "\u56f0\u96be", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n int countDifferentSubsequenceGCDs(vector& nums) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int countDifferentSubsequenceGCDs(int[] nums) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def countDifferentSubsequenceGCDs(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def countDifferentSubsequenceGCDs(self, nums: List[int]) -> int:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nint countDifferentSubsequenceGCDs(int* nums, int numsSize){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int CountDifferentSubsequenceGCDs(int[] nums) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[]} nums\n * @return {number}\n */\nvar countDifferentSubsequenceGCDs = function(nums) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer[]} nums\n# @return {Integer}\ndef count_different_subsequence_gc_ds(nums)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func countDifferentSubsequenceGCDs(_ nums: [Int]) -> Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func countDifferentSubsequenceGCDs(nums []int) int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def countDifferentSubsequenceGCDs(nums: Array[Int]): Int = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun countDifferentSubsequenceGCDs(nums: IntArray): Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn count_different_subsequence_gc_ds(nums: Vec) -> i32 {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @return Integer\n */\n function countDifferentSubsequenceGCDs($nums) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function countDifferentSubsequenceGCDs(nums: number[]): number {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (count-different-subsequence-gc-ds nums)\n (-> (listof exact-integer?) exact-integer?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec count_different_subsequence_gc_ds(Nums :: [integer()]) -> integer().\ncount_different_subsequence_gc_ds(Nums) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec count_different_subsequence_gc_ds(nums :: [integer]) :: integer\n def count_different_subsequence_gc_ds(nums) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1819](https://leetcode-cn.com/problems/number-of-different-subsequences-gcds)", "[\u5e8f\u5217\u4e2d\u4e0d\u540c\u6700\u5927\u516c\u7ea6\u6570\u7684\u6570\u76ee](/solution/1800-1899/1819.Number%20of%20Different%20Subsequences%20GCDs/README.md)", "`\u6570\u7ec4`,`\u6570\u5b66`,`\u8ba1\u6570`,`\u6570\u8bba`", "\u56f0\u96be", ""], "md_table_row_en": ["[1819](https://leetcode.com/problems/number-of-different-subsequences-gcds)", "[Number of Different Subsequences GCDs](/solution/1800-1899/1819.Number%20of%20Different%20Subsequences%20GCDs/README_EN.md)", "`Array`,`Math`,`Counting`,`Number Theory`", "Hard", ""]}, {"question_id": "1946", "frontend_question_id": "1818", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/minimum-absolute-sum-difference", "url_en": "https://leetcode.com/problems/minimum-absolute-sum-difference", "relative_path_cn": "/solution/1800-1899/1818.Minimum%20Absolute%20Sum%20Difference/README.md", "relative_path_en": "/solution/1800-1899/1818.Minimum%20Absolute%20Sum%20Difference/README_EN.md", "title_cn": "\u7edd\u5bf9\u5dee\u503c\u548c", "title_en": "Minimum Absolute Sum Difference", "question_title_slug": "minimum-absolute-sum-difference", "content_en": "You are given two positive integer arrays nums1
and nums2
, both of length n
.
\n\nThe absolute sum difference of arrays nums1
and nums2
is defined as the sum of |nums1[i] - nums2[i]|
for each 0 <= i < n
(0-indexed).
\n\nYou can replace at most one element of nums1
with any other element in nums1
to minimize the absolute sum difference.
\n\nReturn the minimum absolute sum difference after replacing at most one element in the array nums1
. Since the answer may be large, return it modulo 109 + 7
.
\n\n|x|
is defined as:
\n\n\n\tx
if x >= 0
, or \n\t-x
if x < 0
. \n
\n\n
\nExample 1:
\n\n\nInput: nums1 = [1,7,5], nums2 = [2,3,5]\nOutput: 3\nExplanation: There are two possible optimal solutions:\n- Replace the second element with the first: [1,7,5] => [1,1,5], or\n- Replace the second element with the third: [1,7,5] => [1,5,5].\nBoth will yield an absolute sum difference of |1-2| + (|1-3| or |5-3|) + |5-5| =
3.\n
\n\nExample 2:
\n\n\nInput: nums1 = [2,4,6,8,10], nums2 = [2,4,6,8,10]\nOutput: 0\nExplanation: nums1 is equal to nums2 so no replacement is needed. This will result in an \nabsolute sum difference of 0.\n
\n\nExample 3:
\n\n\nInput: nums1 = [1,10,4,4,2,7], nums2 = [9,3,5,1,7,4]\nOutput: 20\nExplanation: Replace the first element with the second: [1,10,4,4,2,7] => [10,10,4,4,2,7].\nThis yields an absolute sum difference of |10-9| + |10-3| + |4-5| + |4-1| + |2-7| + |7-4| = 20
\n
\n\n
\nConstraints:
\n\n\n\tn == nums1.length
\n\tn == nums2.length
\n\t1 <= n <= 105
\n\t1 <= nums1[i], nums2[i] <= 105
\n
\n", "content_cn": "\u7ed9\u4f60\u4e24\u4e2a\u6b63\u6574\u6570\u6570\u7ec4 nums1
\u548c nums2
\uff0c\u6570\u7ec4\u7684\u957f\u5ea6\u90fd\u662f n
\u3002
\n\n\u6570\u7ec4 nums1
\u548c nums2
\u7684 \u7edd\u5bf9\u5dee\u503c\u548c \u5b9a\u4e49\u4e3a\u6240\u6709 |nums1[i] - nums2[i]|
\uff080 <= i < n
\uff09\u7684 \u603b\u548c\uff08\u4e0b\u6807\u4ece 0 \u5f00\u59cb\uff09\u3002
\n\n\u4f60\u53ef\u4ee5\u9009\u7528 nums1
\u4e2d\u7684 \u4efb\u610f\u4e00\u4e2a \u5143\u7d20\u6765\u66ff\u6362 nums1
\u4e2d\u7684 \u81f3\u591a \u4e00\u4e2a\u5143\u7d20\uff0c\u4ee5 \u6700\u5c0f\u5316 \u7edd\u5bf9\u5dee\u503c\u548c\u3002
\n\n\u5728\u66ff\u6362\u6570\u7ec4 nums1
\u4e2d\u6700\u591a\u4e00\u4e2a\u5143\u7d20 \u4e4b\u540e \uff0c\u8fd4\u56de\u6700\u5c0f\u7edd\u5bf9\u5dee\u503c\u548c\u3002\u56e0\u4e3a\u7b54\u6848\u53ef\u80fd\u5f88\u5927\uff0c\u6240\u4ee5\u9700\u8981\u5bf9 109 + 7
\u53d6\u4f59 \u540e\u8fd4\u56de\u3002
\n\n|x|
\u5b9a\u4e49\u4e3a\uff1a
\n\n\n\t- \u5982\u679c
x >= 0
\uff0c\u503c\u4e3a x
\uff0c\u6216\u8005 \n\t- \u5982\u679c
x <= 0
\uff0c\u503c\u4e3a -x
\n
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1anums1 = [1,7,5], nums2 = [2,3,5]\n\u8f93\u51fa\uff1a3\n\u89e3\u91ca\uff1a\u6709\u4e24\u79cd\u53ef\u80fd\u7684\u6700\u4f18\u65b9\u6848\uff1a\n- \u5c06\u7b2c\u4e8c\u4e2a\u5143\u7d20\u66ff\u6362\u4e3a\u7b2c\u4e00\u4e2a\u5143\u7d20\uff1a[1,7,5] => [1,1,5] \uff0c\u6216\u8005\n- \u5c06\u7b2c\u4e8c\u4e2a\u5143\u7d20\u66ff\u6362\u4e3a\u7b2c\u4e09\u4e2a\u5143\u7d20\uff1a[1,7,5] => [1,5,5]\n\u4e24\u79cd\u65b9\u6848\u7684\u7edd\u5bf9\u5dee\u503c\u548c\u90fd\u662f |1-2| + (|1-3| \u6216\u8005 |5-3|) + |5-5| =
3\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1anums1 = [2,4,6,8,10], nums2 = [2,4,6,8,10]\n\u8f93\u51fa\uff1a0\n\u89e3\u91ca\uff1anums1 \u548c nums2 \u76f8\u7b49\uff0c\u6240\u4ee5\u4e0d\u7528\u66ff\u6362\u5143\u7d20\u3002\u7edd\u5bf9\u5dee\u503c\u548c\u4e3a 0\n
\n\n\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1anums1 = [1,10,4,4,2,7], nums2 = [9,3,5,1,7,4]\n\u8f93\u51fa\uff1a20\n\u89e3\u91ca\uff1a\u5c06\u7b2c\u4e00\u4e2a\u5143\u7d20\u66ff\u6362\u4e3a\u7b2c\u4e8c\u4e2a\u5143\u7d20\uff1a[1,10,4,4,2,7] => [10,10,4,4,2,7]\n\u7edd\u5bf9\u5dee\u503c\u548c\u4e3a |10-9| + |10-3| + |4-5| + |4-1| + |2-7| + |7-4| = 20
\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\tn == nums1.length
\n\tn == nums2.length
\n\t1 <= n <= 105
\n\t1 <= nums1[i], nums2[i] <= 105
\n
\n", "tags_en": ["Greedy", "Array", "Binary Search", "Ordered Set"], "tags_cn": ["\u8d2a\u5fc3", "\u6570\u7ec4", "\u4e8c\u5206\u67e5\u627e", "\u6709\u5e8f\u96c6\u5408"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n int minAbsoluteSumDiff(vector& nums1, vector& nums2) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int minAbsoluteSumDiff(int[] nums1, int[] nums2) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def minAbsoluteSumDiff(self, nums1, nums2):\n \"\"\"\n :type nums1: List[int]\n :type nums2: List[int]\n :rtype: int\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def minAbsoluteSumDiff(self, nums1: List[int], nums2: List[int]) -> int:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\nint minAbsoluteSumDiff(int* nums1, int nums1Size, int* nums2, int nums2Size){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int MinAbsoluteSumDiff(int[] nums1, int[] nums2) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[]} nums1\n * @param {number[]} nums2\n * @return {number}\n */\nvar minAbsoluteSumDiff = function(nums1, nums2) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer[]} nums1\n# @param {Integer[]} nums2\n# @return {Integer}\ndef min_absolute_sum_diff(nums1, nums2)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func minAbsoluteSumDiff(_ nums1: [Int], _ nums2: [Int]) -> Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func minAbsoluteSumDiff(nums1 []int, nums2 []int) int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def minAbsoluteSumDiff(nums1: Array[Int], nums2: Array[Int]): Int = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun minAbsoluteSumDiff(nums1: IntArray, nums2: IntArray): Int {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn min_absolute_sum_diff(nums1: Vec, nums2: Vec) -> i32 {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer[] $nums1\n * @param Integer[] $nums2\n * @return Integer\n */\n function minAbsoluteSumDiff($nums1, $nums2) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function minAbsoluteSumDiff(nums1: number[], nums2: number[]): number {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (min-absolute-sum-diff nums1 nums2)\n (-> (listof exact-integer?) (listof exact-integer?) exact-integer?)\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec min_absolute_sum_diff(Nums1 :: [integer()], Nums2 :: [integer()]) -> integer().\nmin_absolute_sum_diff(Nums1, Nums2) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec min_absolute_sum_diff(nums1 :: [integer], nums2 :: [integer]) :: integer\n def min_absolute_sum_diff(nums1, nums2) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1818](https://leetcode-cn.com/problems/minimum-absolute-sum-difference)", "[\u7edd\u5bf9\u5dee\u503c\u548c](/solution/1800-1899/1818.Minimum%20Absolute%20Sum%20Difference/README.md)", "`\u8d2a\u5fc3`,`\u6570\u7ec4`,`\u4e8c\u5206\u67e5\u627e`,`\u6709\u5e8f\u96c6\u5408`", "\u4e2d\u7b49", ""], "md_table_row_en": ["[1818](https://leetcode.com/problems/minimum-absolute-sum-difference)", "[Minimum Absolute Sum Difference](/solution/1800-1899/1818.Minimum%20Absolute%20Sum%20Difference/README_EN.md)", "`Greedy`,`Array`,`Binary Search`,`Ordered Set`", "Medium", ""]}, {"question_id": "1945", "frontend_question_id": "1817", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/finding-the-users-active-minutes", "url_en": "https://leetcode.com/problems/finding-the-users-active-minutes", "relative_path_cn": "/solution/1800-1899/1817.Finding%20the%20Users%20Active%20Minutes/README.md", "relative_path_en": "/solution/1800-1899/1817.Finding%20the%20Users%20Active%20Minutes/README_EN.md", "title_cn": "\u67e5\u627e\u7528\u6237\u6d3b\u8dc3\u5206\u949f\u6570", "title_en": "Finding the Users Active Minutes", "question_title_slug": "finding-the-users-active-minutes", "content_en": "You are given the logs for users' actions on LeetCode, and an integer k
. The logs are represented by a 2D integer array logs
where each logs[i] = [IDi, timei]
indicates that the user with IDi
performed an action at the minute timei
.
\n\nMultiple users can perform actions simultaneously, and a single user can perform multiple actions in the same minute.
\n\nThe user active minutes (UAM) for a given user is defined as the number of unique minutes in which the user performed an action on LeetCode. A minute can only be counted once, even if multiple actions occur during it.
\n\nYou are to calculate a 1-indexed array answer
of size k
such that, for each j
(1 <= j <= k
), answer[j]
is the number of users whose UAM equals j
.
\n\nReturn the array answer
as described above.
\n\n
\nExample 1:
\n\n\nInput: logs = [[0,5],[1,2],[0,2],[0,5],[1,3]], k = 5\nOutput: [0,2,0,0,0]\nExplanation:\nThe user with ID=0 performed actions at minutes 5, 2, and 5 again. Hence, they have a UAM of 2 (minute 5 is only counted once).\nThe user with ID=1 performed actions at minutes 2 and 3. Hence, they have a UAM of 2.\nSince both users have a UAM of 2, answer[2] is 2, and the remaining answer[j] values are 0.\n
\n\nExample 2:
\n\n\nInput: logs = [[1,1],[2,2],[2,3]], k = 4\nOutput: [1,1,0,0]\nExplanation:\nThe user with ID=1 performed a single action at minute 1. Hence, they have a UAM of 1.\nThe user with ID=2 performed actions at minutes 2 and 3. Hence, they have a UAM of 2.\nThere is one user with a UAM of 1 and one with a UAM of 2.\nHence, answer[1] = 1, answer[2] = 1, and the remaining values are 0.\n
\n\n
\nConstraints:
\n\n\n\t1 <= logs.length <= 104
\n\t0 <= IDi <= 109
\n\t1 <= timei <= 105
\n\tk
is in the range [The maximum UAM for a user, 105]
. \n
\n", "content_cn": "\u7ed9\u4f60\u7528\u6237\u5728 LeetCode \u7684\u64cd\u4f5c\u65e5\u5fd7\uff0c\u548c\u4e00\u4e2a\u6574\u6570 k
\u3002\u65e5\u5fd7\u7528\u4e00\u4e2a\u4e8c\u7ef4\u6574\u6570\u6570\u7ec4 logs
\u8868\u793a\uff0c\u5176\u4e2d\u6bcf\u4e2a logs[i] = [IDi, timei]
\u8868\u793a ID \u4e3a IDi
\u7684\u7528\u6237\u5728 timei
\u5206\u949f\u65f6\u6267\u884c\u4e86\u67d0\u4e2a\u64cd\u4f5c\u3002
\n\n\u591a\u4e2a\u7528\u6237 \u53ef\u4ee5\u540c\u65f6\u6267\u884c\u64cd\u4f5c\uff0c\u5355\u4e2a\u7528\u6237\u53ef\u4ee5\u5728\u540c\u4e00\u5206\u949f\u5185\u6267\u884c \u591a\u4e2a\u64cd\u4f5c \u3002
\n\n\u6307\u5b9a\u7528\u6237\u7684 \u7528\u6237\u6d3b\u8dc3\u5206\u949f\u6570\uff08user active minutes\uff0cUAM\uff09 \u5b9a\u4e49\u4e3a\u7528\u6237\u5bf9 LeetCode \u6267\u884c\u64cd\u4f5c\u7684 \u552f\u4e00\u5206\u949f\u6570 \u3002 \u5373\u4f7f\u4e00\u5206\u949f\u5185\u6267\u884c\u591a\u4e2a\u64cd\u4f5c\uff0c\u4e5f\u53ea\u80fd\u6309\u4e00\u5206\u949f\u8ba1\u6570\u3002
\n\n\u8bf7\u4f60\u7edf\u8ba1\u7528\u6237\u6d3b\u8dc3\u5206\u949f\u6570\u7684\u5206\u5e03\u60c5\u51b5\uff0c\u7edf\u8ba1\u7ed3\u679c\u662f\u4e00\u4e2a\u957f\u5ea6\u4e3a k
\u4e14 \u4e0b\u6807\u4ece 1 \u5f00\u59cb\u8ba1\u6570 \u7684\u6570\u7ec4 answer
\uff0c\u5bf9\u4e8e\u6bcf\u4e2a j
\uff081 <= j <= k
\uff09\uff0canswer[j]
\u8868\u793a \u7528\u6237\u6d3b\u8dc3\u5206\u949f\u6570 \u7b49\u4e8e j
\u7684\u7528\u6237\u6570\u3002
\n\n\u8fd4\u56de\u4e0a\u9762\u63cf\u8ff0\u7684\u7b54\u6848\u6570\u7ec4 answer
\u3002
\n\n\u00a0
\n\n\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1alogs = [[0,5],[1,2],[0,2],[0,5],[1,3]], k = 5\n\u8f93\u51fa\uff1a[0,2,0,0,0]\n\u89e3\u91ca\uff1a\nID=0 \u7684\u7528\u6237\u6267\u884c\u64cd\u4f5c\u7684\u5206\u949f\u5206\u522b\u662f\uff1a5 \u30012 \u548c 5 \u3002\u56e0\u6b64\uff0c\u8be5\u7528\u6237\u7684\u7528\u6237\u6d3b\u8dc3\u5206\u949f\u6570\u4e3a 2\uff08\u5206\u949f 5 \u53ea\u8ba1\u6570\u4e00\u6b21\uff09\nID=1 \u7684\u7528\u6237\u6267\u884c\u64cd\u4f5c\u7684\u5206\u949f\u5206\u522b\u662f\uff1a2 \u548c 3 \u3002\u56e0\u6b64\uff0c\u8be5\u7528\u6237\u7684\u7528\u6237\u6d3b\u8dc3\u5206\u949f\u6570\u4e3a 2\n2 \u4e2a\u7528\u6237\u7684\u7528\u6237\u6d3b\u8dc3\u5206\u949f\u6570\u90fd\u662f 2 \uff0canswer[2] \u4e3a 2 \uff0c\u5176\u4f59 answer[j] \u7684\u503c\u90fd\u662f 0\n
\n\n\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1alogs = [[1,1],[2,2],[2,3]], k = 4\n\u8f93\u51fa\uff1a[1,1,0,0]\n\u89e3\u91ca\uff1a\nID=1 \u7684\u7528\u6237\u4ec5\u5728\u5206\u949f 1 \u6267\u884c\u5355\u4e2a\u64cd\u4f5c\u3002\u56e0\u6b64\uff0c\u8be5\u7528\u6237\u7684\u7528\u6237\u6d3b\u8dc3\u5206\u949f\u6570\u4e3a 1\nID=2 \u7684\u7528\u6237\u6267\u884c\u64cd\u4f5c\u7684\u5206\u949f\u5206\u522b\u662f\uff1a2 \u548c 3 \u3002\u56e0\u6b64\uff0c\u8be5\u7528\u6237\u7684\u7528\u6237\u6d3b\u8dc3\u5206\u949f\u6570\u4e3a 2\n1 \u4e2a\u7528\u6237\u7684\u7528\u6237\u6d3b\u8dc3\u5206\u949f\u6570\u662f 1 \uff0c1 \u4e2a\u7528\u6237\u7684\u7528\u6237\u6d3b\u8dc3\u5206\u949f\u6570\u662f 2 \n\u56e0\u6b64\uff0canswer[1] = 1 \uff0canswer[2] = 1 \uff0c\u5176\u4f59\u7684\u503c\u90fd\u662f 0\n
\n\n\u00a0
\n\n\u63d0\u793a\uff1a
\n\n\n\t1 <= logs.length <= 104
\n\t0 <= IDi <= 109
\n\t1 <= timei <= 105
\n\tk
\u7684\u53d6\u503c\u8303\u56f4\u662f [\u7528\u6237\u7684\u6700\u5927\u7528\u6237\u6d3b\u8dc3\u5206\u949f\u6570, 105]
\n
\n", "tags_en": ["Array", "Hash Table"], "tags_cn": ["\u6570\u7ec4", "\u54c8\u5e0c\u8868"], "difficulty_en": "Medium", "difficulty_cn": "\u4e2d\u7b49", "code_snippets": [{"lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n vector findingUsersActiveMinutes(vector>& logs, int k) {\n\n }\n};", "__typename": "CodeSnippetNode"}, {"lang": "Java", "langSlug": "java", "code": "class Solution {\n public int[] findingUsersActiveMinutes(int[][] logs, int k) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def findingUsersActiveMinutes(self, logs, k):\n \"\"\"\n :type logs: List[List[int]]\n :type k: int\n :rtype: List[int]\n \"\"\"", "__typename": "CodeSnippetNode"}, {"lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:", "__typename": "CodeSnippetNode"}, {"lang": "C", "langSlug": "c", "code": "\n\n/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\nint* findingUsersActiveMinutes(int** logs, int logsSize, int* logsColSize, int k, int* returnSize){\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int[] FindingUsersActiveMinutes(int[][] logs, int k) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[][]} logs\n * @param {number} k\n * @return {number[]}\n */\nvar findingUsersActiveMinutes = function(logs, k) {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer[][]} logs\n# @param {Integer} k\n# @return {Integer[]}\ndef finding_users_active_minutes(logs, k)\n\nend", "__typename": "CodeSnippetNode"}, {"lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func findingUsersActiveMinutes(_ logs: [[Int]], _ k: Int) -> [Int] {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Go", "langSlug": "golang", "code": "func findingUsersActiveMinutes(logs [][]int, k int) []int {\n\n}", "__typename": "CodeSnippetNode"}, {"lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def findingUsersActiveMinutes(logs: Array[Array[Int]], k: Int): Array[Int] = {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun findingUsersActiveMinutes(logs: Array, k: Int): IntArray {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn finding_users_active_minutes(logs: Vec>, k: i32) -> Vec {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer[][] $logs\n * @param Integer $k\n * @return Integer[]\n */\n function findingUsersActiveMinutes($logs, $k) {\n\n }\n}", "__typename": "CodeSnippetNode"}, {"lang": "TypeScript", "langSlug": "typescript", "code": "function findingUsersActiveMinutes(logs: number[][], k: number): number[] {\n\n};", "__typename": "CodeSnippetNode"}, {"lang": "Racket", "langSlug": "racket", "code": "(define/contract (finding-users-active-minutes logs k)\n (-> (listof (listof exact-integer?)) exact-integer? (listof exact-integer?))\n\n )", "__typename": "CodeSnippetNode"}, {"lang": "Erlang", "langSlug": "erlang", "code": "-spec finding_users_active_minutes(Logs :: [[integer()]], K :: integer()) -> [integer()].\nfinding_users_active_minutes(Logs, K) ->\n .", "__typename": "CodeSnippetNode"}, {"lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec finding_users_active_minutes(logs :: [[integer]], k :: integer) :: [integer]\n def finding_users_active_minutes(logs, k) do\n\n end\nend", "__typename": "CodeSnippetNode"}], "md_table_row_cn": ["[1817](https://leetcode-cn.com/problems/finding-the-users-active-minutes)", "[\u67e5\u627e\u7528\u6237\u6d3b\u8dc3\u5206\u949f\u6570](/solution/1800-1899/1817.Finding%20the%20Users%20Active%20Minutes/README.md)", "`\u6570\u7ec4`,`\u54c8\u5e0c\u8868`", "\u4e2d\u7b49", ""], "md_table_row_en": ["[1817](https://leetcode.com/problems/finding-the-users-active-minutes)", "[Finding the Users Active Minutes](/solution/1800-1899/1817.Finding%20the%20Users%20Active%20Minutes/README_EN.md)", "`Array`,`Hash Table`", "Medium", ""]}, {"question_id": "1944", "frontend_question_id": "1816", "paid_only": false, "paid_only_cn": false, "category": "Algorithms", "url_cn": "https://leetcode-cn.com/problems/truncate-sentence", "url_en": "https://leetcode.com/problems/truncate-sentence", "relative_path_cn": "/solution/1800-1899/1816.Truncate%20Sentence/README.md", "relative_path_en": "/solution/1800-1899/1816.Truncate%20Sentence/README_EN.md", "title_cn": "\u622a\u65ad\u53e5\u5b50", "title_en": "Truncate Sentence", "question_title_slug": "truncate-sentence", "content_en": "A