Skip to content

Commit 9c7ee88

Browse files
committed
Feb-13
1 parent be2ec19 commit 9c7ee88

12 files changed

+680
-13
lines changed

README.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
3636

3737
| Status | List | Progress | Notes |
3838
| ----- | ----- | ----- | ----- |
39-
| [15%] | [灵茶山艾府.md](./list/灵茶山艾府.md) | 345/2207 | 4 vips |
39+
| [15%] | [灵茶山艾府.md](./list/灵茶山艾府.md) | 349/2207 | 5 vips |
4040
| [18%] | [leetcode-discuss.md](./list/leetcode-discuss.md) | 10/54 | - |
4141
| [47%] | [leetcode-google.md](./list/leetcode-google.md) | 223/471 | 2 vips |
4242
| [48%] | [leetcode-75.md](./list/leetcode-75.md) | 36/75 | - |
@@ -53,9 +53,9 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
5353
| [] | [leetcode-top-100-liked.md](./list/leetcode-top-100-liked.md) | 100/100 | - |
5454
| [] | [leetcode101.md](./list/leetcode101.md) | 183/184 | 1 vip |
5555
| [🔲] | [9c-basic.md](./list/9c-basic.md) | 14/129 | 3 vips |
56-
| [🔲] | [9c-top.md](./list/9c-top.md) | 37/56 | 1 vip |
56+
| [🔲] | [9c-top.md](./list/9c-top.md) | 38/56 | 1 vip |
5757

58-
**Solved**: 519 problems
58+
**Solved**: 523 problems
5959

6060
## 类型/Category
6161

@@ -178,13 +178,14 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
178178

179179
## Simulation
180180

181-
| Link | Problem(21) | Solution | Tag | Time | Space | Ref |
181+
| Link | Problem(22) | Solution | Tag | Time | Space | Ref |
182182
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
183183
| [Leetcode-67](https://leetcode.com/problems/add-binary/) | Add Binary | [c++](./leetcode/67.add-binary.cpp), [python3](./leetcode/67.add-binary.py) | Simulation | O\(M\+N\) | O\(1\) | - |
184184
| [Leetcode-415](https://leetcode.com/problems/add-strings/) | Add Strings | [c++](./leetcode/415.add-strings.cpp), [python3](./leetcode/415.add-strings.py) | Simulation | O\(N\) | O\(1\) | - |
185185
| [Leetcode-735](https://leetcode.com/problems/asteroid-collision/) | Asteroid Collision | [c++](./leetcode/735.asteroid-collision.cpp), [python3](./leetcode/735.asteroid-collision.py) | Simulation | O\(N\) | O\(N\) | - |
186186
| [Leetcode-3174](https://leetcode.com/problems/clear-digits/) | Clear Digits | [c++](./leetcode/3174.clear-digits.cpp), [python3](./leetcode/3174.clear-digits.py) | Simulation | O\(N\) | O\(N\) | - |
187187
| [Leetcode-3160](https://leetcode.com/problems/find-the-number-of-distinct-colors-among-the-balls/) | Find The Number Of Distinct Colors Among The Balls | [c++](./leetcode/3160.find-the-number-of-distinct-colors-among-the-balls.cpp), [python3](./leetcode/3160.find-the-number-of-distinct-colors-among-the-balls.py) | Simulation | O\(N\) | O\(N\) | - |
188+
| [Leetcode-3066](https://leetcode.com/problems/minimum-operations-to-exceed-threshold-value-ii/) | Minimum Operations To Exceed Threshold Value II | [c++](./leetcode/3066.minimum-operations-to-exceed-threshold-value-ii.cpp), [python3](./leetcode/3066.minimum-operations-to-exceed-threshold-value-ii.py) | Simulation | O\(NlogN\) | O\(N\) | - |
188189
| [Leetcode-1910](https://leetcode.com/problems/remove-all-occurrences-of-a-substring/) | Remove All Occurrences Of A Substring | [c++](./leetcode/1910.remove-all-occurrences-of-a-substring.cpp), [python3](./leetcode/1910.remove-all-occurrences-of-a-substring.py) | Simulation | O\(NM\) | O\(1\) | - |
189190
| [Leetcode-566](https://leetcode.com/problems/reshape-the-matrix/) | Reshape The Matrix | [c++](./leetcode/566.reshape-the-matrix.cpp), [python3](./leetcode/566.reshape-the-matrix.py) | Simulation | O\(MN\) | O\(1\) | - |
190191
| [Leetcode-874](https://leetcode.com/problems/walking-robot-simulation/) | Walking Robot Simulation | [c++](./leetcode/874.walking-robot-simulation.cpp), [python3](./leetcode/874.walking-robot-simulation.py) | Simulation | O\(N\) | O\(K\) | - |
@@ -428,7 +429,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
428429

429430
## Hash Table
430431

431-
| Link | Problem(69) | Solution | Tag | Time | Space | Ref |
432+
| Link | Problem(70) | Solution | Tag | Time | Space | Ref |
432433
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
433434
| [Leetcode-721](https://leetcode.com/problems/accounts-merge/) | Accounts Merge | [c++](./leetcode/721.accounts-merge.cpp), [python3](./leetcode/721.accounts-merge.py) | Hash Table | O\(MlogM\) | O\(N\) | - |
434435
| [Leetcode-863](https://leetcode.com/problems/all-nodes-distance-k-in-binary-tree/) | All Nodes Distance K In Binary Tree | [c++](./leetcode/863.all-nodes-distance-k-in-binary-tree.cpp), [python3](./leetcode/863.all-nodes-distance-k-in-binary-tree.py) | Hash Table | O\(N\) | O\(N\) | - |
@@ -495,6 +496,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
495496
| [Leetcode-874](https://leetcode.com/problems/walking-robot-simulation/) | Walking Robot Simulation | [c++](./leetcode/874.walking-robot-simulation.cpp), [python3](./leetcode/874.walking-robot-simulation.py) | Hash Table | O\(N\) | O\(K\) | - |
496497
| [Leetcode-139](https://leetcode.com/problems/word-break/) | Word Break | [c++](./leetcode/139.word-break.cpp), [python3](./leetcode/139.word-break.py) | Hash Table | O\(MN\) | O\(N\) | - |
497498
| [Leetcode-126](https://leetcode.com/problems/word-ladder-ii/) | Word Ladder II | [c++](./leetcode/126.word-ladder-ii.cpp), [python3](./leetcode/126.word-ladder-ii.py) | Hash Table | O\(\(N\*L\)^C\) | O\(N\) | - |
499+
| [Lintcode-3756](https://www.lintcode.com/problem/number-of-unique-flavors-remaining-after-sharing-k-candies/) | Number Of Unique Flavors Remaining After Sharing K Candies | [c++](./lintcode/3756.number-of-unique-flavors-remaining-after-sharing-k-candies.cpp), [python3](./lintcode/3756.number-of-unique-flavors-remaining-after-sharing-k-candies.py) | Hash Table | O\(N\) | O\(N\) | Leetcode-2107 |
498500
| [Lintcode-644](https://www.lintcode.com/problem/strobogrammatic-number/) | Strobogrammatic Number | [c++](./lintcode/644.strobogrammatic-number.cpp), [python3](./lintcode/644.strobogrammatic-number.py) | Hash Table | O\(N\) | O\(1\) | Leetcode-246 |
499501
| [Lintcode-138](https://www.lintcode.com/problem/subarray-sum/) | Subarray Sum | [c++](./lintcode/138.subarray-sum.cpp), [python3](./lintcode/138.subarray-sum.py) | Hash Table | O\(N\) | O\(N\) | - |
500502
| [Lintcode-648](https://www.lintcode.com/problem/unique-word-abbreviation/) | Unique Word Abbreviation | [c++](./lintcode/648.unique-word-abbreviation.cpp), [python3](./lintcode/648.unique-word-abbreviation.py) | Hash Table | O\(1\) | O\(N\) | Leetcode-288 |
@@ -560,7 +562,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
560562

561563
## Heap
562564

563-
| Link | Problem(30) | Solution | Tag | Time | Space | Ref |
565+
| Link | Problem(31) | Solution | Tag | Time | Space | Ref |
564566
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
565567
| [Leetcode-787](https://leetcode.com/problems/cheapest-flights-within-k-stops/) | Cheapest Flights Within K Stops | [c++](./leetcode/787.cheapest-flights-within-k-stops.cpp), [python3](./leetcode/787.cheapest-flights-within-k-stops.py) | Heap | O\(ElogV\) | O\(E\+V\) | - |
566568
| [Leetcode-2349](https://leetcode.com/problems/design-a-number-container-system/) | Design A Number Container System | [c++](./leetcode/2349.design-a-number-container-system.cpp), [python3](./leetcode/2349.design-a-number-container-system.py) | Heap | O\(LogN\) | O\(N\) | - |
@@ -577,6 +579,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
577579
| [Leetcode-1046](https://leetcode.com/problems/last-stone-weight/) | Last Stone Weight | [c++](./leetcode/1046.last-stone-weight.cpp), [python3](./leetcode/1046.last-stone-weight.py) | Heap | O\(NlogN\) | O\(N\) | - |
578580
| [Leetcode-2342](https://leetcode.com/problems/max-sum-of-a-pair-with-equal-sum-of-digits/) | Max Sum Of A Pair With Equal Sum Of Digits | [c++](./leetcode/2342.max-sum-of-a-pair-with-equal-sum-of-digits.cpp), [python3](./leetcode/2342.max-sum-of-a-pair-with-equal-sum-of-digits.py) | Heap | O\(N\) | O\(N\) | - |
579581
| [Leetcode-23](https://leetcode.com/problems/merge-k-sorted-lists/) | Merge K Sorted Lists | [c++](./leetcode/23.merge-k-sorted-lists.cpp), [python3](./leetcode/23.merge-k-sorted-lists.py) | Heap | O\(NlogK\) | O\(K\) | - |
582+
| [Leetcode-3066](https://leetcode.com/problems/minimum-operations-to-exceed-threshold-value-ii/) | Minimum Operations To Exceed Threshold Value II | [c++](./leetcode/3066.minimum-operations-to-exceed-threshold-value-ii.cpp), [python3](./leetcode/3066.minimum-operations-to-exceed-threshold-value-ii.py) | Heap | O\(NlogN\) | O\(N\) | - |
580583
| [Leetcode-743](https://leetcode.com/problems/network-delay-time/) | Network Delay Time | [c++](./leetcode/743.network-delay-time.cpp), [python3](./leetcode/743.network-delay-time.py) | Heap | O\(ElogE\) | O\(E\+V\) | - |
581584
| [Leetcode-1514](https://leetcode.com/problems/path-with-maximum-probability/) | Path With Maximum Probability | [c++](./leetcode/1514.path-with-maximum-probability.cpp), [python3](./leetcode/1514.path-with-maximum-probability.py) | Heap | O\(VlogE\) | O\(V\+E\) | - |
582585
| [Leetcode-1631](https://leetcode.com/problems/path-with-minimum-effort/) | Path With Minimum Effort | [c++](./leetcode/1631.path-with-minimum-effort.cpp), [python3](./leetcode/1631.path-with-minimum-effort.py) | Heap | O\(NMlogNM\) | O\(NM\) | - |
@@ -630,7 +633,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
630633

631634
## Sliding Window
632635

633-
| Link | Problem(23) | Solution | Tag | Time | Space | Ref |
636+
| Link | Problem(26) | Solution | Tag | Time | Space | Ref |
634637
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
635638
| [Leetcode-1652](https://leetcode.com/problems/defuse-the-bomb/) | Defuse The Bomb | [c++](./leetcode/1652.defuse-the-bomb.cpp), [python3](./leetcode/1652.defuse-the-bomb.py) | Sliding Window | O\(N\) | O\(1\) | - |
636639
| [Leetcode-438](https://leetcode.com/problems/find-all-anagrams-in-a-string/) | Find All Anagrams In A String | [c++](./leetcode/438.find-all-anagrams-in-a-string.cpp), [python3](./leetcode/438.find-all-anagrams-in-a-string.py) | Sliding Window | O\(N\) | O\(1\) | - |
@@ -654,7 +657,10 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
654657
| [Leetcode-239](https://leetcode.com/problems/sliding-window-maximum/) | Sliding Window Maximum | [c++](./leetcode/239.sliding-window-maximum.cpp), [python3](./leetcode/239.sliding-window-maximum.py) | Sliding Window | O\(N\) | O\(K\) | - |
655658
| [Leetcode-480](https://leetcode.com/problems/sliding-window-median/) | Sliding Window Median | [c++](./leetcode/480.sliding-window-median.cpp), [python3](./leetcode/480.sliding-window-median.py) | Sliding Window | O\(NlogK\) | O\(K\) | - |
656659
| [Leetcode-632](https://leetcode.com/problems/smallest-range-covering-elements-from-k-lists/) | Smallest Range Covering Elements From K Lists | [c++](./leetcode/632.smallest-range-covering-elements-from-k-lists.cpp), [python3](./leetcode/632.smallest-range-covering-elements-from-k-lists.py) | Sliding Window | O\(NlogN\) | O\(N\) | - |
660+
| [Lintcode-3724](https://www.lintcode.com/problem/distinct-numbers-in-each-subarray/) | Distinct Numbers In Each Subarray | [c++](./lintcode/3724.distinct-numbers-in-each-subarray.cpp), [python3](./lintcode/3724.distinct-numbers-in-each-subarray.py) | Sliding Window | O\(N\) | O\(K\) | Leetcode-1852 |
661+
| [Lintcode-3675](https://www.lintcode.com/problem/minimum-swaps-to-group-all-1s-together/) | Minimum Swaps To Group All 1S Together | [c++](./lintcode/3675.minimum-swaps-to-group-all-1s-together.cpp), [python3](./lintcode/3675.minimum-swaps-to-group-all-1s-together.py) | Sliding Window | O\(N\) | O\(1\) | Leetcode-1151 |
657662
| [Lintcode-642](https://www.lintcode.com/problem/moving-average-from-data-stream/) | Moving Average From Data Stream | [c++](./lintcode/642.moving-average-from-data-stream.cpp), [python3](./lintcode/642.moving-average-from-data-stream.py) | Sliding Window | O\(1\) | O\(K\) | Leetcode-346 |
663+
| [Lintcode-3756](https://www.lintcode.com/problem/number-of-unique-flavors-remaining-after-sharing-k-candies/) | Number Of Unique Flavors Remaining After Sharing K Candies | [c++](./lintcode/3756.number-of-unique-flavors-remaining-after-sharing-k-candies.cpp), [python3](./lintcode/3756.number-of-unique-flavors-remaining-after-sharing-k-candies.py) | Sliding Window | O\(N\) | O\(N\) | Leetcode-2107 |
658664

659665
## Sorting
660666

leetcode/1100.find-k-length-substrings-with-no-repeated-characters.vip

Whitespace-only changes.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Tag: Array, Heap (Priority Queue), Simulation
2+
// Time: O(NlogN)
3+
// Space: O(N)
4+
// Ref: -
5+
// Note: -
6+
// Video: https://youtu.be/u6LhqSUykLs
7+
8+
// You are given a 0-indexed integer array nums, and an integer k.
9+
// In one operation, you will:
10+
//
11+
// Take the two smallest integers x and y in nums.
12+
// Remove x and y from nums.
13+
// Add min(x, y) * 2 + max(x, y) anywhere in the array.
14+
//
15+
// Note that you can only apply the described operation if nums contains at least two elements.
16+
// Return the minimum number of operations needed so that all elements of the array are greater than or equal to k.
17+
//  
18+
// Example 1:
19+
//
20+
// Input: nums = [2,11,10,1,3], k = 10
21+
// Output: 2
22+
// Explanation: In the first operation, we remove elements 1 and 2, then add 1 * 2 + 2 to nums. nums becomes equal to [4, 11, 10, 3].
23+
// In the second operation, we remove elements 3 and 4, then add 3 * 2 + 4 to nums. nums becomes equal to [10, 11, 10].
24+
// At this stage, all the elements of nums are greater than or equal to 10 so we can stop.
25+
// It can be shown that 2 is the minimum number of operations needed so that all elements of the array are greater than or equal to 10.
26+
//
27+
// Example 2:
28+
//
29+
// Input: nums = [1,1,2,4,9], k = 20
30+
// Output: 4
31+
// Explanation: After one operation, nums becomes equal to [2, 4, 9, 3].
32+
// After two operations, nums becomes equal to [7, 4, 9].
33+
// After three operations, nums becomes equal to [15, 9].
34+
// After four operations, nums becomes equal to [33].
35+
// At this stage, all the elements of nums are greater than 20 so we can stop.
36+
// It can be shown that 4 is the minimum number of operations needed so that all elements of the array are greater than or equal to 20.
37+
//  
38+
// Constraints:
39+
//
40+
// 2 <= nums.length <= 2 * 105
41+
// 1 <= nums[i] <= 109
42+
// 1 <= k <= 109
43+
// The input is generated such that an answer always exists. That is, there exists some sequence of operations after which all elements of the array are greater than or equal to k.
44+
//
45+
//
46+
47+
class Solution {
48+
public:
49+
int minOperations(vector<int>& nums, int k) {
50+
int n = nums.size();
51+
priority_queue<long long, vector<long long>, greater<long long>> q;
52+
for (int i = 0; i < n; i++) {
53+
q.push(nums[i]);
54+
}
55+
56+
int count = 0;
57+
while (q.size() >= 2) {
58+
long long x = q.top();
59+
q.pop();
60+
long long y = q.top();
61+
q.pop();
62+
if (x >= k && y >= k) {
63+
break;
64+
}
65+
count += 1;
66+
long long res = min(x, y) * 2LL + max(x, y);
67+
q.push(res);
68+
}
69+
return count;
70+
}
71+
};
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Tag: Array, Heap (Priority Queue), Simulation
2+
# Time: O(NlogN)
3+
# Space: O(N)
4+
# Ref: -
5+
# Note: -
6+
# Video: https://youtu.be/u6LhqSUykLs
7+
8+
# You are given a 0-indexed integer array nums, and an integer k.
9+
# In one operation, you will:
10+
#
11+
# Take the two smallest integers x and y in nums.
12+
# Remove x and y from nums.
13+
# Add min(x, y) * 2 + max(x, y) anywhere in the array.
14+
#
15+
# Note that you can only apply the described operation if nums contains at least two elements.
16+
# Return the minimum number of operations needed so that all elements of the array are greater than or equal to k.
17+
#  
18+
# Example 1:
19+
#
20+
# Input: nums = [2,11,10,1,3], k = 10
21+
# Output: 2
22+
# Explanation: In the first operation, we remove elements 1 and 2, then add 1 * 2 + 2 to nums. nums becomes equal to [4, 11, 10, 3].
23+
# In the second operation, we remove elements 3 and 4, then add 3 * 2 + 4 to nums. nums becomes equal to [10, 11, 10].
24+
# At this stage, all the elements of nums are greater than or equal to 10 so we can stop.
25+
# It can be shown that 2 is the minimum number of operations needed so that all elements of the array are greater than or equal to 10.
26+
#
27+
# Example 2:
28+
#
29+
# Input: nums = [1,1,2,4,9], k = 20
30+
# Output: 4
31+
# Explanation: After one operation, nums becomes equal to [2, 4, 9, 3].
32+
# After two operations, nums becomes equal to [7, 4, 9].
33+
# After three operations, nums becomes equal to [15, 9].
34+
# After four operations, nums becomes equal to [33].
35+
# At this stage, all the elements of nums are greater than 20 so we can stop.
36+
# It can be shown that 4 is the minimum number of operations needed so that all elements of the array are greater than or equal to 20.
37+
#  
38+
# Constraints:
39+
#
40+
# 2 <= nums.length <= 2 * 105
41+
# 1 <= nums[i] <= 109
42+
# 1 <= k <= 109
43+
# The input is generated such that an answer always exists. That is, there exists some sequence of operations after which all elements of the array are greater than or equal to k.
44+
#
45+
#
46+
47+
import heapq
48+
class Solution:
49+
def minOperations(self, nums: List[int], k: int) -> int:
50+
heapq.heapify(nums)
51+
count = 0
52+
while len(nums) >= 2:
53+
first = heapq.heappop(nums)
54+
second = heapq.heappop(nums)
55+
if first >= k and second >= k:
56+
break
57+
58+
count += 1
59+
res = min(first, second) * 2 + max(first, second)
60+
heapq.heappush(nums, res)
61+
62+
return count
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Tag: Sliding Window, Array
2+
// Time: O(N)
3+
// Space: O(1)
4+
// Ref: Leetcode-1151
5+
// Note: -
6+
7+
// In this problem there is a binary array (contains only 0 and 1).
8+
// You can swap arbitrary elements of the array.
9+
//
10+
// Now you need to combine all 1s in the array.
11+
// Returns the minimum number of swaps required for a combination.
12+
//
13+
// **Example 1**
14+
//
15+
// Input:
16+
//
17+
// ```plaintext
18+
// [1,0,1,0,1]
19+
// ```
20+
//
21+
// Explanation:
22+
//
23+
// `[1,1,1,0,0]` or `[0,0,1,1,1]` only need one swap.
24+
//
25+
// Output:
26+
//
27+
// ```plaintext
28+
// 1
29+
// ```
30+
//
31+
// **Example 2**
32+
//
33+
// Input:
34+
//
35+
// ```plaintext
36+
// [1,0,1,0,1,0,1,1,0,1]
37+
// ```
38+
//
39+
// Output:
40+
//
41+
// ```plaintext
42+
// 2
43+
// ```
44+
//
45+
// Explanation:
46+
//
47+
// `[0,0,0,0,1,1,1,1,1,1]` requires only two swaps.
48+
//
49+
//
50+
51+
class Solution {
52+
public:
53+
/**
54+
* @param arr: the binary array
55+
* @return: the minimal number of swaps
56+
*/
57+
int minSwaps(vector<int> &arr) {
58+
// write your code here
59+
int n = arr.size();
60+
int k = 0;
61+
for (int x: arr) {
62+
k += x;
63+
}
64+
65+
int count = 0;
66+
int ones = 0;
67+
int i = 0;
68+
for (int j = 0; j < n; j++) {
69+
if (arr[j] == 1) {
70+
count += 1;
71+
}
72+
73+
if (j - i + 1 == k) {
74+
ones = max(ones, count);
75+
if (arr[i] == 1) {
76+
count -= 1;
77+
}
78+
i += 1;
79+
}
80+
81+
}
82+
83+
return k - ones;
84+
}
85+
};
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Tag: Sliding Window, Array
2+
# Time: O(N)
3+
# Space: O(1)
4+
# Ref: Leetcode-1151
5+
# Note: -
6+
7+
# In this problem there is a binary array (contains only 0 and 1).
8+
# You can swap arbitrary elements of the array.
9+
#
10+
# Now you need to combine all 1s in the array.
11+
# Returns the minimum number of swaps required for a combination.
12+
#
13+
# **Example 1**
14+
#
15+
# Input:
16+
#
17+
# ```plaintext
18+
# [1,0,1,0,1]
19+
# ```
20+
#
21+
# Explanation:
22+
#
23+
# `[1,1,1,0,0]` or `[0,0,1,1,1]` only need one swap.
24+
#
25+
# Output:
26+
#
27+
# ```plaintext
28+
# 1
29+
# ```
30+
#
31+
# **Example 2**
32+
#
33+
# Input:
34+
#
35+
# ```plaintext
36+
# [1,0,1,0,1,0,1,1,0,1]
37+
# ```
38+
#
39+
# Output:
40+
#
41+
# ```plaintext
42+
# 2
43+
# ```
44+
#
45+
# Explanation:
46+
#
47+
# `[0,0,0,0,1,1,1,1,1,1]` requires only two swaps.
48+
#
49+
#
50+
51+
from typing import (
52+
List,
53+
)
54+
55+
class Solution:
56+
"""
57+
@param arr: the binary array
58+
@return: the minimal number of swaps
59+
"""
60+
def min_swaps(self, arr: List[int]) -> int:
61+
# write your code here
62+
n = len(arr)
63+
k = arr.count(1)
64+
ones = 0
65+
count = 0
66+
i = 0
67+
for j in range(n):
68+
if arr[j] == 1:
69+
count += 1
70+
if j - i + 1 == k:
71+
ones = max(ones, count)
72+
if arr[i] == 1:
73+
count -= 1
74+
i += 1
75+
76+
return k - ones

0 commit comments

Comments
 (0)