From 5cc11e4c1879d67ea582cd46f33d8eca0999da97 Mon Sep 17 00:00:00 2001 From: ivan Date: Mon, 10 Aug 2026 05:08:39 -0600 Subject: [PATCH] adding updates --- .../minimum_swaps_to_group_all_ones.py | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/my_project/interviews/amazon_high_frequency_23/round_7/minimum_swaps_to_group_all_ones.py diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_7/minimum_swaps_to_group_all_ones.py b/src/my_project/interviews/amazon_high_frequency_23/round_7/minimum_swaps_to_group_all_ones.py new file mode 100644 index 00000000..90895c3b --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/round_7/minimum_swaps_to_group_all_ones.py @@ -0,0 +1,32 @@ +from typing import List, Union, Collection, Mapping, Optional + + +class Solution: + def minSwaps(self, data: List[int]) -> int: + + # Set window size + k = sum(data) + + val = answer = 0 + + for i, v in enumerate(data): + + val += v + + if i >= k: + val -= data[i - k] + + if i >= k - 1: + answer = max(answer, val) + + return k - answer + +''' +Window size (k): Count total number of 1s in the array. This is the size of the window needed to fit all 1s. + +Sliding window: Move a window of size k across the array and count how many 1s are already in each window position. + +Find maximum 1s: Track the maximum number of 1s found in any window (ans). + +Calculate swaps: The minimum swaps needed = k - ans (total 1s minus the maximum 1s already grouped in any window). +'''