Minimum Swaps to Group All 1's Together II | 2 Ways | Dry Runs | Leetcode 2134 | codestorywithMIK

Subscribers:
92,300
Published on ● Video Link: https://www.youtube.com/watch?v=t82KJaI1kNQ



Duration: 25:57
10,513 views
513


Whatsapp Community Link : https://www.whatsapp.com/channel/0029...
This is the 25th Video of our Playlist "Sliding Window : Popular Interview Problems" by codestorywithMIK

In this video we will try to solve a good and standard Sliding Window Problem : Minimum Swaps to Group All 1's Together II | 2 Ways | Dry Runs | Leetcode 2134 | codestorywithMIK

I will explain the intuition so easily that you will never forget and start seeing this as cakewalk EASYYY.
We will do live coding after explanation and see if we are able to pass all the test cases.
Also, please note that my Github solution link below contains both C++ as well as JAVA code.

Problem Name : Minimum Swaps to Group All 1's Together II | 2 Ways | Dry Runs | Leetcode 2134 | codestorywithMIK
Company Tags : Microsoft
My solutions on Github(C++ & JAVA) : https://github.com/MAZHARMIK/Intervie...
Leetcode Link : https://leetcode.com/problems/minimum...


My DP Concepts Playlist :    • Roadmap for DP | How to Start DP ? | ...  
My Graph Concepts Playlist :    • Graph Concepts & Qns - 1 : Graph will...  
My Recursion Concepts Playlist :    • Introduction | Recursion Concepts And...  
My GitHub Repo for interview preparation : https://github.com/MAZHARMIK/Intervie...
Instagram :   / codestorywithmik  
Facebook :   / 100090524295846  
Twitter :   / cswithmik  
Subscribe to my channel :    / @codestorywithmik  

╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝

Summary :
Approach 1: Using Extra Space and Sliding Window

Time Complexity: O(n)
Space Complexity: O(2n) ~ O(n)
Explanation:

Duplicating the Array: A temporary array temp of size 2*n is created by concatenating the original array with itself. This allows for a circular array approach using a linear array.
Count of 1s: The total number of 1s in the original array is counted using accumulate.
Sliding Window: A sliding window of size equal to the number of 1s is used to find the maximum number of 1s in any subarray of this size. This is done by:
Initializing pointers i and j to the start of the array.
Moving j to expand the window and counting 1s.
If the window size exceeds the number of 1s, moving i to shrink the window and adjusting the count of 1s.
Tracking the maximum number of 1s found in any window of the required size.
Result Calculation: The minimum swaps needed is calculated as the total number of 1s minus the maximum number of 1s found in any window.
Approach 2: Sliding Window without Extra Space

Time Complexity: O(n)
Space Complexity: O(1)
Explanation:

Count of 1s: Similar to Approach 1, the total number of 1s in the original array is counted.
Sliding Window with Modulo Operation: Instead of duplicating the array, the modulo operation (j % n and i % n) is used to handle the circular nature of the array within the same array space.
Sliding Window: The logic for the sliding window remains the same as in Approach 1:
Using pointers i and j to define the window.
Adjusting the count of 1s when expanding or shrinking the window.
Tracking the maximum number of 1s found.
Result Calculation: The result is calculated as the total number of 1s minus the maximum number of 1s found in any window.
Comparison:

Both approaches have the same time complexity of O(n).
Approach 1 uses additional space to create a duplicated array, resulting in O(n) space complexity.
Approach 2 optimizes space usage by leveraging the modulo operation, resulting in O(1) space complexity.

✨ Timelines✨
00:00 - Introduction

#coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge#leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview#interviewtips #interviewpreparation #interview_ds_algo #hinglish #github #design #data #google #video #instagram #facebook #leetcode #computerscience #leetcodesolutions #leetcodequestionandanswers #code #learning #dsalgo #dsa #newyear2024




Other Videos By codestorywithMIK


2024-08-11Combination Sum II | Simple Khandani Template | Leetcode 40 | codestorywithMIK
2024-08-09Minimum Number of Days to Disconnect Island | Studied Concept | Leetcode 1568 | codestorywithMIK
2024-08-09Regions Cut By Slashes | Already Studied Concept | Easy Code | Leetcode 959 | codestorywithMIK
2024-08-08Magic Squares In Grid | Simple Approach | Clean Code | Leetcode 840 | codestorywithMIK
2024-08-08Thank you 60,000 Subscribers ❤️
2024-08-06Spiral Matrix III | Simple Simulation | Made Easy | Leetcode 885 | codestorywithMIK
2024-08-05Integer to English Words | Story To Code | Leetcode 273 | codestorywithMIK
2024-08-03Happy Friendship Day ❤️
2024-08-03Range Sum of Sorted Subarray Sums | Learning Something New | Leetcode 1508 | codestorywithMIK
2024-08-01Make Two Arrays Equal by Reversing Subarrays | Easy | Leetcode 1460 | codestorywithMIK
2024-07-31Minimum Swaps to Group All 1's Together II | 2 Ways | Dry Runs | Leetcode 2134 | codestorywithMIK
2024-07-29Filling Bookcase Shelves | Recursion Memoization | Why not Greedy | Leetcode 1105 | codestorywithMIK
2024-07-28Minimum Deletions to Make String Balanced | Multiple Approaches | Leetcode 1653 | codestorywithMIK
2024-07-27Count Number of Teams | Simple thought process | Intuition | Leetcode 1395
2024-07-26Second Minimum Time to Reach Destination | 2 Approaches | FULL DRY RUN | Leetcode 2045
2024-07-24Find the City With the Smallest Number of Neighbors at a Threshold Distance | 3 Ways | Leetcode 1334
2024-07-22Sort the Jumbled Numbers | 2 Approaches | Leetcode 2191 | codestorywithMIK
2024-07-20Minimum Operations to Make Array Equal to Target | Minimum Number of Increments|Leetcode 3229 & 1526
2024-07-19Build a Matrix With Conditions | BFS | DFS | Detailed Explanation | Leetcode 2392 | codestorywithMIK
2024-07-19Palindrome Partitioning | Using Blue Print | DP On Strings | Leetcode 131 | DP Concepts & Qns-27
2024-07-18Find Valid Matrix Given Row and Column Sums | Simple Approach | Leetcode 1605 | codestorywithMIK