diff --git a/solution/2000-2099/2011.Final Value of Variable After Performing Operations/README.md b/solution/2000-2099/2011.Final Value of Variable After Performing Operations/README.md
index d3c8e340c3d81..2393993feeec2 100644
--- a/solution/2000-2099/2011.Final Value of Variable After Performing Operations/README.md
+++ b/solution/2000-2099/2011.Final Value of Variable After Performing Operations/README.md
@@ -50,7 +50,7 @@ X++:X 加 1 ,X = 0 + 1 = 1
输入:operations = ["++X","++X","X++"]
输出:3
-解释:操作按下述步骤执行:
+解释:操作按下述步骤执行:
最初,X = 0
++X:X 加 1 ,X = 0 + 1 = 1
++X:X 加 1 ,X = 1 + 1 = 2
diff --git a/solution/2100-2199/2151.Maximum Good People Based on Statements/README.md b/solution/2100-2199/2151.Maximum Good People Based on Statements/README.md
index 5340b3481f703..272922401b235 100644
--- a/solution/2100-2199/2151.Maximum Good People Based on Statements/README.md
+++ b/solution/2100-2199/2151.Maximum Good People Based on Statements/README.md
@@ -86,7 +86,7 @@ tags:
- 在认为 0 是坏人但说真话的情况下,这组玩家中没有一个好人。
- 说假话。在这种情况下,1 是好人。
- 在认为 0 是坏人且说假话的情况下,这组玩家中只有一个好人。
-在最佳情况下,至多有一个好人,所以返回 1 。
+在最佳情况下,至多有一个好人,所以返回 1 。
注意,能得到此结论的方法不止一种。
diff --git a/solution/2100-2199/2165.Smallest Value of the Rearranged Number/README_EN.md b/solution/2100-2199/2165.Smallest Value of the Rearranged Number/README_EN.md
index 41688c0737c48..007719a68985f 100644
--- a/solution/2100-2199/2165.Smallest Value of the Rearranged Number/README_EN.md
+++ b/solution/2100-2199/2165.Smallest Value of the Rearranged Number/README_EN.md
@@ -31,7 +31,7 @@ tags:
Input: num = 310
Output: 103
-Explanation: The possible arrangements for the digits of 310 are 013, 031, 103, 130, 301, 310.
+Explanation: The possible arrangements for the digits of 310 are 013, 031, 103, 130, 301, 310.
The arrangement with the smallest value that does not contain any leading zeros is 103.
diff --git a/solution/2100-2199/2169.Count Operations to Obtain Zero/README_EN.md b/solution/2100-2199/2169.Count Operations to Obtain Zero/README_EN.md
index a83babb934b00..66746d66e6de6 100644
--- a/solution/2100-2199/2169.Count Operations to Obtain Zero/README_EN.md
+++ b/solution/2100-2199/2169.Count Operations to Obtain Zero/README_EN.md
@@ -35,7 +35,7 @@ tags:
Input: num1 = 2, num2 = 3
Output: 3
-Explanation:
+Explanation:
- Operation 1: num1 = 2, num2 = 3. Since num1 < num2, we subtract num1 from num2 and get num1 = 2, num2 = 3 - 2 = 1.
- Operation 2: num1 = 2, num2 = 1. Since num1 > num2, we subtract num2 from num1.
- Operation 3: num1 = 1, num2 = 1. Since num1 == num2, we subtract num2 from num1.
@@ -48,7 +48,7 @@ So the total number of operations required is 3.
Input: num1 = 10, num2 = 10
Output: 1
-Explanation:
+Explanation:
- Operation 1: num1 = 10, num2 = 10. Since num1 == num2, we subtract num2 from num1 and get num1 = 10 - 10 = 0.
Now num1 = 0 and num2 = 10. Since num1 == 0, we are done.
So the total number of operations required is 1.
diff --git a/solution/2100-2199/2178.Maximum Split of Positive Even Integers/README_EN.md b/solution/2100-2199/2178.Maximum Split of Positive Even Integers/README_EN.md
index d21ee619a8ff1..144ccffc2f0df 100644
--- a/solution/2100-2199/2178.Maximum Split of Positive Even Integers/README_EN.md
+++ b/solution/2100-2199/2178.Maximum Split of Positive Even Integers/README_EN.md
@@ -53,7 +53,7 @@ Thus, we return an empty array.
Input: finalSum = 28
Output: [6,8,2,12]
-Explanation: The following are valid splits: (2 + 26)
, (6 + 8 + 2 + 12)
, and (4 + 24)
.
+Explanation: The following are valid splits: (2 + 26)
, (6 + 8 + 2 + 12)
, and (4 + 24)
.
(6 + 8 + 2 + 12)
has the maximum number of integers, which is 4. Thus, we return [6,8,2,12].
Note that [10,2,4,12], [6,2,4,16], etc. are also accepted.
diff --git a/solution/2200-2299/2206.Divide Array Into Equal Pairs/README_EN.md b/solution/2200-2299/2206.Divide Array Into Equal Pairs/README_EN.md
index 35b9c46b2b4cd..7e4e94cfccf86 100644
--- a/solution/2200-2299/2206.Divide Array Into Equal Pairs/README_EN.md
+++ b/solution/2200-2299/2206.Divide Array Into Equal Pairs/README_EN.md
@@ -38,7 +38,7 @@ tags:
Input: nums = [3,2,3,2,2,2]
Output: true
-Explanation:
+Explanation:
There are 6 elements in nums, so they should be divided into 6 / 2 = 3 pairs.
If nums is divided into the pairs (2, 2), (3, 3), and (2, 2), it will satisfy all the conditions.
@@ -48,7 +48,7 @@ If nums is divided into the pairs (2, 2), (3, 3), and (2, 2), it will satisfy al
Input: nums = [1,2,3,4]
Output: false
-Explanation:
+Explanation:
There is no way to divide nums into 4 / 2 = 2 pairs such that the pairs satisfy every condition.
diff --git a/solution/2200-2299/2229.Check if an Array Is Consecutive/README.md b/solution/2200-2299/2229.Check if an Array Is Consecutive/README.md
index 311cc1ebaf80f..096c376114f24 100644
--- a/solution/2200-2299/2229.Check if an Array Is Consecutive/README.md
+++ b/solution/2200-2299/2229.Check if an Array Is Consecutive/README.md
@@ -41,9 +41,9 @@ tags:
输入:nums = [1,3]
输出:false
解释:
-最小值是 1 ,数组长度为 2 。
-范围 [x, x + n - 1] 中的所有值没有都出现在 nums 中:[1, 1 + 2 - 1] = [1, 2] = (1, 2) 。
-因此,nums 不是一个连贯数组。
+最小值是 1 ,数组长度为 2 。
+范围 [x, x + n - 1] 中的所有值没有都出现在 nums 中:[1, 1 + 2 - 1] = [1, 2] = (1, 2) 。
+因此,nums 不是一个连贯数组。
示例 3:
diff --git a/solution/2200-2299/2243.Calculate Digit Sum of a String/README.md b/solution/2200-2299/2243.Calculate Digit Sum of a String/README.md
index acca2648edfc0..18c281783698b 100644
--- a/solution/2200-2299/2243.Calculate Digit Sum of a String/README.md
+++ b/solution/2200-2299/2243.Calculate Digit Sum of a String/README.md
@@ -39,11 +39,11 @@ tags:
输出:"135"
解释:
- 第一轮,将 s 分成:"111"、"112"、"222" 和 "23" 。
- 接着,计算每一组的数字和:1 + 1 + 1 = 3、1 + 1 + 2 = 4、2 + 2 + 2 = 6 和 2 + 3 = 5 。
+ 接着,计算每一组的数字和:1 + 1 + 1 = 3、1 + 1 + 2 = 4、2 + 2 + 2 = 6 和 2 + 3 = 5 。
这样,s 在第一轮之后变成 "3" + "4" + "6" + "5" = "3465" 。
- 第二轮,将 s 分成:"346" 和 "5" 。
接着,计算每一组的数字和:3 + 4 + 6 = 13 、5 = 5 。
- 这样,s 在第二轮之后变成 "13" + "5" = "135" 。
+ 这样,s 在第二轮之后变成 "13" + "5" = "135" 。
现在,s.length <= k ,所以返回 "135" 作为答案。
@@ -53,7 +53,7 @@ tags:
输出:"000"
解释:
将 "000", "000", and "00".
-接着,计算每一组的数字和:0 + 0 + 0 = 0 、0 + 0 + 0 = 0 和 0 + 0 = 0 。
+接着,计算每一组的数字和:0 + 0 + 0 = 0 、0 + 0 + 0 = 0 和 0 + 0 = 0 。
s 变为 "0" + "0" + "0" = "000" ,其长度等于 k ,所以返回 "000" 。
diff --git a/solution/2200-2299/2243.Calculate Digit Sum of a String/README_EN.md b/solution/2200-2299/2243.Calculate Digit Sum of a String/README_EN.md
index 067f1f646888e..bc65eccc0d046 100644
--- a/solution/2200-2299/2243.Calculate Digit Sum of a String/README_EN.md
+++ b/solution/2200-2299/2243.Calculate Digit Sum of a String/README_EN.md
@@ -37,13 +37,13 @@ tags:
Input: s = "11111222223", k = 3
Output: "135"
-Explanation:
+Explanation:
- For the first round, we divide s into groups of size 3: "111", "112", "222", and "23".
- Then we calculate the digit sum of each group: 1 + 1 + 1 = 3, 1 + 1 + 2 = 4, 2 + 2 + 2 = 6, and 2 + 3 = 5.
+ Then we calculate the digit sum of each group: 1 + 1 + 1 = 3, 1 + 1 + 2 = 4, 2 + 2 + 2 = 6, and 2 + 3 = 5.
So, s becomes "3" + "4" + "6" + "5" = "3465" after the first round.
- For the second round, we divide s into "346" and "5".
- Then we calculate the digit sum of each group: 3 + 4 + 6 = 13, 5 = 5.
- So, s becomes "13" + "5" = "135" after second round.
+ Then we calculate the digit sum of each group: 3 + 4 + 6 = 13, 5 = 5.
+ So, s becomes "13" + "5" = "135" after second round.
Now, s.length <= k, so we return "135" as the answer.
@@ -52,9 +52,9 @@ Now, s.length <= k, so we return "135" as the answer.
Input: s = "00000000", k = 3
Output: "000"
-Explanation:
+Explanation:
We divide s into "000", "000", and "00".
-Then we calculate the digit sum of each group: 0 + 0 + 0 = 0, 0 + 0 + 0 = 0, and 0 + 0 = 0.
+Then we calculate the digit sum of each group: 0 + 0 + 0 = 0, 0 + 0 + 0 = 0, and 0 + 0 = 0.
s becomes "0" + "0" + "0" = "000", whose length is equal to k, so we return "000".
diff --git a/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/README.md b/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/README.md
index 38ace23072d25..1de9e92ed8677 100644
--- a/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/README.md
+++ b/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/README.md
@@ -52,7 +52,7 @@ tags:
输入:nums = [1,2,3,4]
输出:1
解释:
-子数组按位与运算的最大值是 4 。
+子数组按位与运算的最大值是 4 。
能得到此结果的最长子数组是 [4],所以返回 1 。
diff --git a/solution/2900-2999/2920.Maximum Points After Collecting Coins From All Nodes/README.md b/solution/2900-2999/2920.Maximum Points After Collecting Coins From All Nodes/README.md
index 8a088fc38e61f..1d083afb0f134 100644
--- a/solution/2900-2999/2920.Maximum Points After Collecting Coins From All Nodes/README.md
+++ b/solution/2900-2999/2920.Maximum Points After Collecting Coins From All Nodes/README.md
@@ -41,13 +41,13 @@ tags:
输入:edges = [[0,1],[1,2],[2,3]], coins = [10,10,3,3], k = 5
-输出:11
+输出:11
解释:
使用第一种方法收集节点 0 上的所有金币。总积分 = 10 - 5 = 5 。
使用第一种方法收集节点 1 上的所有金币。总积分 = 5 + (10 - 5) = 10 。
使用第二种方法收集节点 2 上的所有金币。所以节点 3 上的金币将会变为 floor(3 / 2) = 1 ,总积分 = 10 + floor(3 / 2) = 11 。
使用第二种方法收集节点 3 上的所有金币。总积分 = 11 + floor(1 / 2) = 11.
-可以证明收集所有节点上的金币能获得的最大积分是 11 。
+可以证明收集所有节点上的金币能获得的最大积分是 11 。
示例 2:
diff --git a/solution/2900-2999/2920.Maximum Points After Collecting Coins From All Nodes/README_EN.md b/solution/2900-2999/2920.Maximum Points After Collecting Coins From All Nodes/README_EN.md
index 74fc4d69e3d84..180b255ead6b9 100644
--- a/solution/2900-2999/2920.Maximum Points After Collecting Coins From All Nodes/README_EN.md
+++ b/solution/2900-2999/2920.Maximum Points After Collecting Coins From All Nodes/README_EN.md
@@ -40,13 +40,13 @@ tags:
Input: edges = [[0,1],[1,2],[2,3]], coins = [10,10,3,3], k = 5
-Output: 11
-Explanation:
+Output: 11
+Explanation:
Collect all the coins from node 0 using the first way. Total points = 10 - 5 = 5.
Collect all the coins from node 1 using the first way. Total points = 5 + (10 - 5) = 10.
Collect all the coins from node 2 using the second way so coins left at node 3 will be floor(3 / 2) = 1. Total points = 10 + floor(3 / 2) = 11.
Collect all the coins from node 3 using the second way. Total points = 11 + floor(1 / 2) = 11.
-It can be shown that the maximum points we can get after collecting coins from all the nodes is 11.
+It can be shown that the maximum points we can get after collecting coins from all the nodes is 11.
Example 2:
@@ -55,7 +55,7 @@ It can be shown that the maximum points we can get after collecting coins from a
Input: edges = [[0,1],[0,2]], coins = [8,4,4], k = 0
Output: 16
-Explanation:
+Explanation:
Coins will be collected from all the nodes using the first way. Therefore, total points = (8 - 0) + (4 - 0) + (4 - 0) = 16.
diff --git a/solution/3400-3499/3431.Minimum Unlocked Indices to Sort Nums/README.md b/solution/3400-3499/3431.Minimum Unlocked Indices to Sort Nums/README.md
new file mode 100644
index 0000000000000..974f22cf35147
--- /dev/null
+++ b/solution/3400-3499/3431.Minimum Unlocked Indices to Sort Nums/README.md
@@ -0,0 +1,268 @@
+---
+comments: true
+difficulty: 中等
+edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3431.Minimum%20Unlocked%20Indices%20to%20Sort%20Nums/README.md
+---
+
+
+
+# [3431. Minimum Unlocked Indices to Sort Nums 🔒](https://leetcode.cn/problems/minimum-unlocked-indices-to-sort-nums)
+
+[English Version](/solution/3400-3499/3431.Minimum%20Unlocked%20Indices%20to%20Sort%20Nums/README_EN.md)
+
+## 题目描述
+
+
+
+You are given an array nums
consisting of integers between 1 and 3, and a binary array locked
of the same size.
+
+We consider nums
sortable if it can be sorted using adjacent swaps, where a swap between two indices i
and i + 1
is allowed if nums[i] - nums[i + 1] == 1
and locked[i] == 0
.
+
+In one operation, you can unlock any index i
by setting locked[i]
to 0.
+
+Return the minimum number of operations needed to make nums
sortable. If it is not possible to make nums
sortable, return -1.
+
+
+Example 1:
+
+
+
Input: nums = [1,2,1,2,3,2], locked = [1,0,1,1,0,1]
+
+
Output: 0
+
+
Explanation:
+
+
We can sort nums
using the following swaps:
+
+
+ - swap indices 1 with 2
+ - swap indices 4 with 5
+
+
+
So, there is no need to unlock any index.
+
+
+Example 2:
+
+
+
Input: nums = [1,2,1,1,3,2,2], locked = [1,0,1,1,0,1,0]
+
+
Output: 2
+
+
Explanation:
+
+
If we unlock indices 2 and 5, we can sort nums
using the following swaps:
+
+
+ - swap indices 1 with 2
+ - swap indices 2 with 3
+ - swap indices 4 with 5
+ - swap indices 5 with 6
+
+
+
+Example 3:
+
+
+
Input: nums = [1,2,1,2,3,2,1], locked = [0,0,0,0,0,0,0]
+
+
Output: -1
+
+
Explanation:
+
+
Even if all indices are unlocked, it can be shown that nums
is not sortable.
+
+
+
+Constraints:
+
+
+ 1 <= nums.length <= 105
+ 1 <= nums[i] <= 3
+ locked.length == nums.length
+ 0 <= locked[i] <= 1
+
+
+
+
+## 解法
+
+
+
+### 方法一:脑筋急转弯
+
+根据题目描述,要使得 $\textit{nums}$ 变成可排序的数组,需要满足数字 $3$ 的位置在数字 $1$ 的位置之后。如果数字 $3$ 的位置在数字 $1$ 的位置之前,那么无论怎么交换,数字 $3$ 都无法到达数字 $1$ 的位置,因此无法使得 $\textit{nums}$ 变成可排序的数组。
+
+我们分别用 $\textit{first2}$ 和 $\textit{first3}$ 表示数字 $2$ 和 $3$ 第一次出现的位置,用 $\textit{last1}$ 和 $\textit{last2}$ 表示数字 $1$ 和 $2$ 最后一次出现的位置。
+
+那么当下标 $i$ 位于 $[\textit{first2}, \textit{last1})$ 或者 $[\textit{first3}, \textit{last2})]$ 时,对应的 $\textit{locked}[i]$ 必须为 $0$,否则我们需要一次操作。因此,我们只需要遍历数组 $\textit{locked}$,统计不满足条件的下标即可。
+
+时间复杂度 $O(n)$,其中 $n$ 为数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。
+
+
+
+#### Python3
+
+```python
+class Solution:
+ def minUnlockedIndices(self, nums: List[int], locked: List[int]) -> int:
+ n = len(nums)
+ first2 = first3 = n
+ last1 = last2 = -1
+ for i, x in enumerate(nums):
+ if x == 1:
+ last1 = i
+ elif x == 2:
+ first2 = min(first2, i)
+ last2 = i
+ else:
+ first3 = min(first3, i)
+ if first3 < last1:
+ return -1
+ return sum(
+ st and (first2 <= i < last1 or first3 <= i < last2)
+ for i, st in enumerate(locked)
+ )
+```
+
+#### Java
+
+```java
+class Solution {
+ public int minUnlockedIndices(int[] nums, int[] locked) {
+ int n = nums.length;
+ int first2 = n, first3 = n;
+ int last1 = -1, last2 = -1;
+ for (int i = 0; i < n; ++i) {
+ if (nums[i] == 1) {
+ last1 = i;
+ } else if (nums[i] == 2) {
+ first2 = Math.min(first2, i);
+ last2 = i;
+ } else {
+ first3 = Math.min(first3, i);
+ }
+ }
+ if (first3 < last1) {
+ return -1;
+ }
+ int ans = 0;
+ for (int i = 0; i < n; ++i) {
+ if (locked[i] == 1 && ((first2 <= i && i < last1) || (first3 <= i && i < last2))) {
+ ++ans;
+ }
+ }
+ return ans;
+ }
+}
+```
+
+#### C++
+
+```cpp
+class Solution {
+public:
+ int minUnlockedIndices(vector& nums, vector& locked) {
+ int n = nums.size();
+ int first2 = n, first3 = n;
+ int last1 = -1, last2 = -1;
+
+ for (int i = 0; i < n; ++i) {
+ if (nums[i] == 1) {
+ last1 = i;
+ } else if (nums[i] == 2) {
+ first2 = min(first2, i);
+ last2 = i;
+ } else {
+ first3 = min(first3, i);
+ }
+ }
+
+ if (first3 < last1) {
+ return -1;
+ }
+
+ int ans = 0;
+ for (int i = 0; i < n; ++i) {
+ if (locked[i] == 1 && ((first2 <= i && i < last1) || (first3 <= i && i < last2))) {
+ ++ans;
+ }
+ }
+
+ return ans;
+ }
+};
+```
+
+#### Go
+
+```go
+func minUnlockedIndices(nums []int, locked []int) (ans int) {
+ n := len(nums)
+ first2, first3 := n, n
+ last1, last2 := -1, -1
+ for i, x := range nums {
+ if x == 1 {
+ last1 = i
+ } else if x == 2 {
+ if i < first2 {
+ first2 = i
+ }
+ last2 = i
+ } else {
+ if i < first3 {
+ first3 = i
+ }
+ }
+ }
+ if first3 < last1 {
+ return -1
+ }
+ for i, st := range locked {
+ if st == 1 && ((first2 <= i && i < last1) || (first3 <= i && i < last2)) {
+ ans++
+ }
+ }
+ return ans
+}
+```
+
+#### TypeScript
+
+```ts
+function minUnlockedIndices(nums: number[], locked: number[]): number {
+ const n = nums.length;
+ let [first2, first3] = [n, n];
+ let [last1, last2] = [-1, -1];
+
+ for (let i = 0; i < n; i++) {
+ if (nums[i] === 1) {
+ last1 = i;
+ } else if (nums[i] === 2) {
+ first2 = Math.min(first2, i);
+ last2 = i;
+ } else {
+ first3 = Math.min(first3, i);
+ }
+ }
+
+ if (first3 < last1) {
+ return -1;
+ }
+
+ let ans = 0;
+ for (let i = 0; i < n; i++) {
+ if (locked[i] === 1 && ((first2 <= i && i < last1) || (first3 <= i && i < last2))) {
+ ans++;
+ }
+ }
+
+ return ans;
+}
+```
+
+
+
+
+
+
diff --git a/solution/3400-3499/3431.Minimum Unlocked Indices to Sort Nums/README_EN.md b/solution/3400-3499/3431.Minimum Unlocked Indices to Sort Nums/README_EN.md
new file mode 100644
index 0000000000000..a012a8ffbac88
--- /dev/null
+++ b/solution/3400-3499/3431.Minimum Unlocked Indices to Sort Nums/README_EN.md
@@ -0,0 +1,268 @@
+---
+comments: true
+difficulty: Medium
+edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3431.Minimum%20Unlocked%20Indices%20to%20Sort%20Nums/README_EN.md
+---
+
+
+
+# [3431. Minimum Unlocked Indices to Sort Nums 🔒](https://leetcode.com/problems/minimum-unlocked-indices-to-sort-nums)
+
+[中文文档](/solution/3400-3499/3431.Minimum%20Unlocked%20Indices%20to%20Sort%20Nums/README.md)
+
+## Description
+
+
+
+You are given an array nums
consisting of integers between 1 and 3, and a binary array locked
of the same size.
+
+We consider nums
sortable if it can be sorted using adjacent swaps, where a swap between two indices i
and i + 1
is allowed if nums[i] - nums[i + 1] == 1
and locked[i] == 0
.
+
+In one operation, you can unlock any index i
by setting locked[i]
to 0.
+
+Return the minimum number of operations needed to make nums
sortable. If it is not possible to make nums
sortable, return -1.
+
+
+Example 1:
+
+
+
Input: nums = [1,2,1,2,3,2], locked = [1,0,1,1,0,1]
+
+
Output: 0
+
+
Explanation:
+
+
We can sort nums
using the following swaps:
+
+
+ - swap indices 1 with 2
+ - swap indices 4 with 5
+
+
+
So, there is no need to unlock any index.
+
+
+Example 2:
+
+
+
Input: nums = [1,2,1,1,3,2,2], locked = [1,0,1,1,0,1,0]
+
+
Output: 2
+
+
Explanation:
+
+
If we unlock indices 2 and 5, we can sort nums
using the following swaps:
+
+
+ - swap indices 1 with 2
+ - swap indices 2 with 3
+ - swap indices 4 with 5
+ - swap indices 5 with 6
+
+
+
+Example 3:
+
+
+
Input: nums = [1,2,1,2,3,2,1], locked = [0,0,0,0,0,0,0]
+
+
Output: -1
+
+
Explanation:
+
+
Even if all indices are unlocked, it can be shown that nums
is not sortable.
+
+
+
+Constraints:
+
+
+ 1 <= nums.length <= 105
+ 1 <= nums[i] <= 3
+ locked.length == nums.length
+ 0 <= locked[i] <= 1
+
+
+
+
+## Solutions
+
+
+
+### Solution 1: Brain Teaser
+
+According to the problem description, to make $\textit{nums}$ a sortable array, the position of the number $3$ must be after the position of the number $1$. If the position of the number $3$ is before the position of the number $1$, no matter how we swap, the number $3$ cannot reach the position of the number $1$, so it is impossible to make $\textit{nums}$ a sortable array.
+
+We use $\textit{first2}$ and $\textit{first3}$ to represent the first occurrence positions of the numbers $2$ and $3$, and $\textit{last1}$ and $\textit{last2}$ to represent the last occurrence positions of the numbers $1$ and $2$.
+
+When the index $i$ is in the range $[\textit{first2}, \textit{last1})$ or $[\textit{first3}, \textit{last2})]$, the corresponding $\textit{locked}[i]$ must be $0$, otherwise, we need one operation. Therefore, we only need to traverse the array $\textit{locked}$ and count the indices that do not meet the condition.
+
+The time complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$. The space complexity is $O(1)$.
+
+
+
+#### Python3
+
+```python
+class Solution:
+ def minUnlockedIndices(self, nums: List[int], locked: List[int]) -> int:
+ n = len(nums)
+ first2 = first3 = n
+ last1 = last2 = -1
+ for i, x in enumerate(nums):
+ if x == 1:
+ last1 = i
+ elif x == 2:
+ first2 = min(first2, i)
+ last2 = i
+ else:
+ first3 = min(first3, i)
+ if first3 < last1:
+ return -1
+ return sum(
+ st and (first2 <= i < last1 or first3 <= i < last2)
+ for i, st in enumerate(locked)
+ )
+```
+
+#### Java
+
+```java
+class Solution {
+ public int minUnlockedIndices(int[] nums, int[] locked) {
+ int n = nums.length;
+ int first2 = n, first3 = n;
+ int last1 = -1, last2 = -1;
+ for (int i = 0; i < n; ++i) {
+ if (nums[i] == 1) {
+ last1 = i;
+ } else if (nums[i] == 2) {
+ first2 = Math.min(first2, i);
+ last2 = i;
+ } else {
+ first3 = Math.min(first3, i);
+ }
+ }
+ if (first3 < last1) {
+ return -1;
+ }
+ int ans = 0;
+ for (int i = 0; i < n; ++i) {
+ if (locked[i] == 1 && ((first2 <= i && i < last1) || (first3 <= i && i < last2))) {
+ ++ans;
+ }
+ }
+ return ans;
+ }
+}
+```
+
+#### C++
+
+```cpp
+class Solution {
+public:
+ int minUnlockedIndices(vector& nums, vector& locked) {
+ int n = nums.size();
+ int first2 = n, first3 = n;
+ int last1 = -1, last2 = -1;
+
+ for (int i = 0; i < n; ++i) {
+ if (nums[i] == 1) {
+ last1 = i;
+ } else if (nums[i] == 2) {
+ first2 = min(first2, i);
+ last2 = i;
+ } else {
+ first3 = min(first3, i);
+ }
+ }
+
+ if (first3 < last1) {
+ return -1;
+ }
+
+ int ans = 0;
+ for (int i = 0; i < n; ++i) {
+ if (locked[i] == 1 && ((first2 <= i && i < last1) || (first3 <= i && i < last2))) {
+ ++ans;
+ }
+ }
+
+ return ans;
+ }
+};
+```
+
+#### Go
+
+```go
+func minUnlockedIndices(nums []int, locked []int) (ans int) {
+ n := len(nums)
+ first2, first3 := n, n
+ last1, last2 := -1, -1
+ for i, x := range nums {
+ if x == 1 {
+ last1 = i
+ } else if x == 2 {
+ if i < first2 {
+ first2 = i
+ }
+ last2 = i
+ } else {
+ if i < first3 {
+ first3 = i
+ }
+ }
+ }
+ if first3 < last1 {
+ return -1
+ }
+ for i, st := range locked {
+ if st == 1 && ((first2 <= i && i < last1) || (first3 <= i && i < last2)) {
+ ans++
+ }
+ }
+ return ans
+}
+```
+
+#### TypeScript
+
+```ts
+function minUnlockedIndices(nums: number[], locked: number[]): number {
+ const n = nums.length;
+ let [first2, first3] = [n, n];
+ let [last1, last2] = [-1, -1];
+
+ for (let i = 0; i < n; i++) {
+ if (nums[i] === 1) {
+ last1 = i;
+ } else if (nums[i] === 2) {
+ first2 = Math.min(first2, i);
+ last2 = i;
+ } else {
+ first3 = Math.min(first3, i);
+ }
+ }
+
+ if (first3 < last1) {
+ return -1;
+ }
+
+ let ans = 0;
+ for (let i = 0; i < n; i++) {
+ if (locked[i] === 1 && ((first2 <= i && i < last1) || (first3 <= i && i < last2))) {
+ ans++;
+ }
+ }
+
+ return ans;
+}
+```
+
+
+
+
+
+
diff --git a/solution/3400-3499/3431.Minimum Unlocked Indices to Sort Nums/Solution.cpp b/solution/3400-3499/3431.Minimum Unlocked Indices to Sort Nums/Solution.cpp
new file mode 100644
index 0000000000000..44565982c40c2
--- /dev/null
+++ b/solution/3400-3499/3431.Minimum Unlocked Indices to Sort Nums/Solution.cpp
@@ -0,0 +1,32 @@
+class Solution {
+public:
+ int minUnlockedIndices(vector& nums, vector& locked) {
+ int n = nums.size();
+ int first2 = n, first3 = n;
+ int last1 = -1, last2 = -1;
+
+ for (int i = 0; i < n; ++i) {
+ if (nums[i] == 1) {
+ last1 = i;
+ } else if (nums[i] == 2) {
+ first2 = min(first2, i);
+ last2 = i;
+ } else {
+ first3 = min(first3, i);
+ }
+ }
+
+ if (first3 < last1) {
+ return -1;
+ }
+
+ int ans = 0;
+ for (int i = 0; i < n; ++i) {
+ if (locked[i] == 1 && ((first2 <= i && i < last1) || (first3 <= i && i < last2))) {
+ ++ans;
+ }
+ }
+
+ return ans;
+ }
+};
diff --git a/solution/3400-3499/3431.Minimum Unlocked Indices to Sort Nums/Solution.go b/solution/3400-3499/3431.Minimum Unlocked Indices to Sort Nums/Solution.go
new file mode 100644
index 0000000000000..bfd9c9c4be86f
--- /dev/null
+++ b/solution/3400-3499/3431.Minimum Unlocked Indices to Sort Nums/Solution.go
@@ -0,0 +1,28 @@
+func minUnlockedIndices(nums []int, locked []int) (ans int) {
+ n := len(nums)
+ first2, first3 := n, n
+ last1, last2 := -1, -1
+ for i, x := range nums {
+ if x == 1 {
+ last1 = i
+ } else if x == 2 {
+ if i < first2 {
+ first2 = i
+ }
+ last2 = i
+ } else {
+ if i < first3 {
+ first3 = i
+ }
+ }
+ }
+ if first3 < last1 {
+ return -1
+ }
+ for i, st := range locked {
+ if st == 1 && ((first2 <= i && i < last1) || (first3 <= i && i < last2)) {
+ ans++
+ }
+ }
+ return ans
+}
diff --git a/solution/3400-3499/3431.Minimum Unlocked Indices to Sort Nums/Solution.java b/solution/3400-3499/3431.Minimum Unlocked Indices to Sort Nums/Solution.java
new file mode 100644
index 0000000000000..a83bba99990db
--- /dev/null
+++ b/solution/3400-3499/3431.Minimum Unlocked Indices to Sort Nums/Solution.java
@@ -0,0 +1,27 @@
+class Solution {
+ public int minUnlockedIndices(int[] nums, int[] locked) {
+ int n = nums.length;
+ int first2 = n, first3 = n;
+ int last1 = -1, last2 = -1;
+ for (int i = 0; i < n; ++i) {
+ if (nums[i] == 1) {
+ last1 = i;
+ } else if (nums[i] == 2) {
+ first2 = Math.min(first2, i);
+ last2 = i;
+ } else {
+ first3 = Math.min(first3, i);
+ }
+ }
+ if (first3 < last1) {
+ return -1;
+ }
+ int ans = 0;
+ for (int i = 0; i < n; ++i) {
+ if (locked[i] == 1 && ((first2 <= i && i < last1) || (first3 <= i && i < last2))) {
+ ++ans;
+ }
+ }
+ return ans;
+ }
+}
diff --git a/solution/3400-3499/3431.Minimum Unlocked Indices to Sort Nums/Solution.py b/solution/3400-3499/3431.Minimum Unlocked Indices to Sort Nums/Solution.py
new file mode 100644
index 0000000000000..44498993d5f5d
--- /dev/null
+++ b/solution/3400-3499/3431.Minimum Unlocked Indices to Sort Nums/Solution.py
@@ -0,0 +1,19 @@
+class Solution:
+ def minUnlockedIndices(self, nums: List[int], locked: List[int]) -> int:
+ n = len(nums)
+ first2 = first3 = n
+ last1 = last2 = -1
+ for i, x in enumerate(nums):
+ if x == 1:
+ last1 = i
+ elif x == 2:
+ first2 = min(first2, i)
+ last2 = i
+ else:
+ first3 = min(first3, i)
+ if first3 < last1:
+ return -1
+ return sum(
+ st and (first2 <= i < last1 or first3 <= i < last2)
+ for i, st in enumerate(locked)
+ )
diff --git a/solution/3400-3499/3431.Minimum Unlocked Indices to Sort Nums/Solution.ts b/solution/3400-3499/3431.Minimum Unlocked Indices to Sort Nums/Solution.ts
new file mode 100644
index 0000000000000..6c02989ed197e
--- /dev/null
+++ b/solution/3400-3499/3431.Minimum Unlocked Indices to Sort Nums/Solution.ts
@@ -0,0 +1,29 @@
+function minUnlockedIndices(nums: number[], locked: number[]): number {
+ const n = nums.length;
+ let [first2, first3] = [n, n];
+ let [last1, last2] = [-1, -1];
+
+ for (let i = 0; i < n; i++) {
+ if (nums[i] === 1) {
+ last1 = i;
+ } else if (nums[i] === 2) {
+ first2 = Math.min(first2, i);
+ last2 = i;
+ } else {
+ first3 = Math.min(first3, i);
+ }
+ }
+
+ if (first3 < last1) {
+ return -1;
+ }
+
+ let ans = 0;
+ for (let i = 0; i < n; i++) {
+ if (locked[i] === 1 && ((first2 <= i && i < last1) || (first3 <= i && i < last2))) {
+ ans++;
+ }
+ }
+
+ return ans;
+}
diff --git a/solution/README.md b/solution/README.md
index 98fd5ade7d201..8ef6296aebd7b 100644
--- a/solution/README.md
+++ b/solution/README.md
@@ -3441,6 +3441,7 @@
| 3428 | [最多 K 个元素的子序列的最值之和](/solution/3400-3499/3428.Maximum%20and%20Minimum%20Sums%20of%20at%20Most%20Size%20K%20Subsequences/README.md) | | 中等 | 第 433 场周赛 |
| 3429 | [粉刷房子 IV](/solution/3400-3499/3429.Paint%20House%20IV/README.md) | | 中等 | 第 433 场周赛 |
| 3430 | [最多 K 个元素的子数组的最值之和](/solution/3400-3499/3430.Maximum%20and%20Minimum%20Sums%20of%20at%20Most%20Size%20K%20Subarrays/README.md) | | 困难 | 第 433 场周赛 |
+| 3431 | [Minimum Unlocked Indices to Sort Nums](/solution/3400-3499/3431.Minimum%20Unlocked%20Indices%20to%20Sort%20Nums/README.md) | | 中等 | 🔒 |
## 版权
diff --git a/solution/README_EN.md b/solution/README_EN.md
index 6185b5d292170..91696b30f7901 100644
--- a/solution/README_EN.md
+++ b/solution/README_EN.md
@@ -3439,6 +3439,7 @@ Press Control + F(or Command + F on
| 3428 | [Maximum and Minimum Sums of at Most Size K Subsequences](/solution/3400-3499/3428.Maximum%20and%20Minimum%20Sums%20of%20at%20Most%20Size%20K%20Subsequences/README_EN.md) | | Medium | Weekly Contest 433 |
| 3429 | [Paint House IV](/solution/3400-3499/3429.Paint%20House%20IV/README_EN.md) | | Medium | Weekly Contest 433 |
| 3430 | [Maximum and Minimum Sums of at Most Size K Subarrays](/solution/3400-3499/3430.Maximum%20and%20Minimum%20Sums%20of%20at%20Most%20Size%20K%20Subarrays/README_EN.md) | | Hard | Weekly Contest 433 |
+| 3431 | [Minimum Unlocked Indices to Sort Nums](/solution/3400-3499/3431.Minimum%20Unlocked%20Indices%20to%20Sort%20Nums/README_EN.md) | | Medium | 🔒 |
## Copyright