2134. Minimum Swaps to Group All 1's Together II
2022. 1. 9. 17:08ㆍAlgorithm/Leetcode, Lintcode, HackerRank, etc.
- 목차
반응형
class Solution:
def minSwaps(self, nums: List[int]) -> int:
n = len(nums)
ones = nums.count(1)
zeros = nums[:ones].count(0)
mn_zeros = zeros
for i in range(n - 1):
if nums[i] == 0:
zeros -= 1
if nums[(i + ones)%n] == 0:
zeros += 1
mn_zeros = min(mn_zeros, zeros)
return mn_zeros
반응형
'Algorithm > Leetcode, Lintcode, HackerRank, etc.' 카테고리의 다른 글
2244. Minimum Rounds to Complete All Tasks (0) | 2022.05.01 |
---|---|
2243. Calculate Digit Sum of a String (0) | 2022.04.29 |
2248. Intersection of Multiple Arrays (0) | 2022.04.29 |
2138. Divide a String Into Groups of Size k (0) | 2022.01.22 |
2133. Check if Every Row and Column Contains All Numbers (0) | 2022.01.09 |