inimum Swaps to Group All s Together II
2134. Minimum Swaps to Group All 1's Together II
Medium
A swap is defined as taking two distinct positions in an array and swapping the values in them.
A circular array is defined as an array where we consider the first element and the last element to be adjacent.
Given a binary circular array nums, return the minimum number of swaps required to group all 1's present in the array together at any location.
Example 1:
- Input: nums = [0,1,0,1,1,0,0]
- Output: 1
-
Explanation: Here are a few of the ways to group all the 1's together:
- [0,0,1,1,1,0,0] using 1 swap.
- [0,1,1,1,0,0,0] using 1 swap.
- [1,1,0,0,0,0,1] using 2 swaps (using the circular property of the array).
- There is no way to group all 1's together with 0 swaps.
- Thus, the minimum number of swaps required is 1.
Example 2:
- Input: nums = [0,1,1,1,0,0,1,1,0]
- Output: 2
-
Explanation: Here are a few of the ways to group all the 1's together:
- [1,1,1,0,0,0,0,1,1] using 2 swaps (using the circular property of the array).
- [1,1,1,1,1,0,0,0,0] using 2 swaps.
- There is no way to group all 1's together with 0 or 1 swaps.
- Thus, the minimum number of swaps required is 2.
Example 3:
- Input: nums = [1,1,0,0,1]
- Output: 0
-
Explanation: All the 1's are already grouped together due to the circular property of the array.
- Thus, the minimum number of swaps required is 0.
Constraints:
- 1 <= nums.length <= 105
- nums[i] is either 0 or 1.
Hint:
- Notice that the number of 1’s to be grouped together is fixed. It is the number of 1's the whole array has.
- Call this number total. We should then check for every subarray of size total (possibly wrapped around), how many swaps are required to have the subarray be all 1’s.
- The number of swaps required is the number of 0’s in the subarray.
- To eliminate the circular property of the array, we can append the original array to itself. Then, we check each subarray of length total.
- How do we avoid recounting the number of 0’s in the subarray each time? The Sliding Window technique can help.
Solution:
To solve this problem, we can follow these steps:
- Count the Total Number of 1s: This will be the number of 1s we need to group together.
- Extend the Array: To handle the circular nature, append the array to itself.
- Use Sliding Window Technique: Apply the sliding window technique on the extended array to find the minimum number of swaps required.
Let's implement this solution in PHP: 2134. Minimum Swaps to Group All 1's Together II
Explanation:
- Count the Total Number of 1s: Calculate the total number of 1s in the original array.
- Extend the Array: Concatenate the original array to itself to handle the circular nature.
- Initial Window: Count the number of 0s in the initial window of size equal to the total number of 1s.
- Sliding Window: Slide the window across the extended array. For each new position, update the count of 0s based on the elements entering and leaving the window.
- Find Minimum: Keep track of the minimum number of 0s encountered, which corresponds to the minimum number of swaps needed.
This solution efficiently handles the circular array by transforming it into a linear problem and uses the sliding window technique to maintain a running count of 0s in each window of size equal to the total number of 1s.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks ?. Your support would mean a lot to me!
If you want more helpful content like this, feel free to follow me:
- GitHub
The above is the detailed content of inimum Swaps to Group All s Together II. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...
