Skip to content

Commit e5ca5ef

Browse files
committed
Add solution #3431
1 parent 9452047 commit e5ca5ef

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2692,6 +2692,7 @@
26922692
3416|[Subsequences with a Unique Middle Mode II](./solutions/3416-subsequences-with-a-unique-middle-mode-ii.js)|Hard|
26932693
3422|[Minimum Operations to Make Subarray Elements Equal](./solutions/3422-minimum-operations-to-make-subarray-elements-equal.js)|Medium|
26942694
3423|[Maximum Difference Between Adjacent Elements in a Circular Array](./solutions/3423-maximum-difference-between-adjacent-elements-in-a-circular-array.js)|Easy|
2695+
3431|[Minimum Unlocked Indices to Sort Nums](./solutions/3431-minimum-unlocked-indices-to-sort-nums.js)|Medium|
26952696
3439|[Reschedule Meetings for Maximum Free Time I](./solutions/3439-reschedule-meetings-for-maximum-free-time-i.js)|Medium|
26962697
3440|[Reschedule Meetings for Maximum Free Time II](./solutions/3440-reschedule-meetings-for-maximum-free-time-ii.js)|Medium|
26972698
3442|[Maximum Difference Between Even and Odd Frequency I](./solutions/3442-maximum-difference-between-even-and-odd-frequency-i.js)|Easy|
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* 3431. Minimum Unlocked Indices to Sort Nums
3+
* https://leetcode.com/problems/minimum-unlocked-indices-to-sort-nums/
4+
* Difficulty: Medium
5+
*
6+
* You are given an array nums consisting of integers between 1 and 3, and a binary array
7+
* locked of the same size.
8+
*
9+
* We consider nums sortable if it can be sorted using adjacent swaps, where a swap between
10+
* two indices i and i + 1 is allowed if nums[i] - nums[i + 1] == 1 and locked[i] == 0.
11+
*
12+
* In one operation, you can unlock any index i by setting locked[i] to 0.
13+
*
14+
* Return the minimum number of operations needed to make nums sortable. If it is not possible
15+
* to make nums sortable, return -1.
16+
*/
17+
18+
/**
19+
* @param {number[]} nums
20+
* @param {number[]} locked
21+
* @return {number}
22+
*/
23+
var minUnlockedIndices = function(nums, locked) {
24+
let currentMax = 1;
25+
let locks = 0;
26+
let result = 0;
27+
28+
for (let i = 0; i < nums.length; i++) {
29+
if (currentMax < nums[i]) {
30+
currentMax = nums[i];
31+
locks = 0;
32+
}
33+
if (nums[i] < currentMax) {
34+
if (nums[i] + 1 < currentMax) {
35+
return -1;
36+
}
37+
result += locks;
38+
locks = 0;
39+
}
40+
locks += locked[i];
41+
}
42+
43+
return result;
44+
};

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy