-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Zanger67/leetcode
authored and
Zanger67/leetcode
committed
Aug 2, 2024
1 parent
1063e46
commit 348b52f
Showing
9 changed files
with
66 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
markdowns/_2134. Minimum Swaps to Group All 1's Together II.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# 2134. [Minimum Swaps to Group All 1's Together II](<https://leetcode.com/problems/minimum-swaps-to-group-all-1s-together-ii>) | ||
|
||
*All prompts are owned by LeetCode. To view the prompt, click the title link above.* | ||
|
||
*[Back to top](<../README.md>)* | ||
|
||
------ | ||
|
||
> *First completed : August 02, 2024* | ||
> | ||
> *Last updated : August 02, 2024* | ||
------ | ||
|
||
> **Related Topics** : **[Array](<by_topic/Array.md>), [Sliding Window](<by_topic/Sliding Window.md>)** | ||
> | ||
> **Acceptance Rate** : **52.72 %** | ||
------ | ||
|
||
## Solutions | ||
|
||
- [m2134.py](<../my-submissions/m2134.py>) | ||
### Python | ||
#### [m2134.py](<../my-submissions/m2134.py>) | ||
```Python | ||
class Solution: | ||
def minSwaps(self, nums: List[int]) -> int: | ||
totalOnes = nums.count(1) | ||
if not totalOnes : | ||
return 0 | ||
|
||
oneCount = nums[:totalOnes].count(1) | ||
zeroCount = totalOnes - oneCount | ||
minMoves = zeroCount | ||
|
||
for i, nxtVal in enumerate(nums[totalOnes:] + nums[:totalOnes], totalOnes) : | ||
if nxtVal : | ||
oneCount += 1 | ||
else : | ||
zeroCount += 1 | ||
|
||
if nums[i - totalOnes] : | ||
oneCount -= 1 | ||
else : | ||
zeroCount -= 1 | ||
|
||
minMoves = min(minMoves, zeroCount) | ||
|
||
return minMoves | ||
git p | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.