AMAZON Coding Question – Solved

11 Live
Amazon Prime Video is developing a new feature called "Segmentify." This feature applies to a video with n (even) visual frames, where each frame is represented by a binary character in the array `frames`. In this format, a "0" represents a black pixel, and a "1" represents a white pixel. Due to factors like lighting and camera angles, some frames may need horizontal or vertical flips (changing "0"s to "1"s and vice versa) to create consistent visuals. The objective is to divide the video into subsegments so that all frames in a subsegment are visually identical (i.e., the frames in a subsegment are either all "0"s or all "1"s). Additionally, each subsegment should have an even length. The goal is to accomplish this segmentation with two criteria in mind: 1. Minimize the number of flips required to form valid segments — let this be denoted by B. 2. Among all configurations requiring B flips, minimize the total number of subsegments. Given the binary string `frames`, determine the minimum number of such subsegments that meet the criteria. Note: A subsegment is a segment that can be derived from another segment by deleting some elements without changing the order of the remaining elements. Example: `frames = "1110011000"` One set of optimal moves is as follows: - Flip the first 0 to 1 → "1111011000" - Flip the next 0 to 1 → "1111111000" - Flip the final 0 to 1 → "1111111100" All resulting subsegments (11111111 and 00) are of even length. So, the minimum number of subsegments is 2 with a total of 3 flips. Hence, the answer is 2. Another example: `frames = "110011"` In this case, the string already consists of groups of "0"s and "1"s that have even lengths. Therefore, no flipping is necessary, and the string can be divided into 3 even-length subsegments without modification. Hence, the answer is 3. Function Description: Complete the function `getminSubsegments` in the editor below. `getminSubsegments` has the following parameter: - string `frames`: the frames of the video as a binary string. Returns: - int: the minimum number of subsegments. Constraints: - 2 ≤ n ≤ 10^5, where n is even. - It is guaranteed that the string `frames` only consists of '0's and '1's. Sample Input: `100110` Sample Output: 2 Explanation: Flip the first 0 to 1, resulting in "110110". All segments can then be grouped into even-length parts.

Asked in: AMAZON

Image of the Question

Question Image Question Image Question Image

All Testcases Passed ✔



Passcode Image

Solution


from functools import lru_cache
from sys import setrecursionlimit
setrecursionlimit(10**6)
def getminSubsegments(frames):
// ... rest of solution available after purchase

🔒 Please login to view the solution

Explanation


```
To solve this problem effectively, it's important to deeply understand what constitutes a valid solution. The core idea is to divide a binary string (representing visual video frames) into the minimum number of subsegments such that:

1. All characters in each subsegment are the same (either all '0's or all '1's).
2. The length of each subsegment is even.
3. The number of bit flips (i.e., changing '0' to '1' or vice versa) across the entire string is minimized.
4. Among all segmentations that use the minimum number of flips, the one with the fewest subsegments should be chosen.

The strategy revolves around balancing two intertwined goals: reduce the number of bit flips, and reduce the number of resulting segments, while maintaining the requirement that every segment has an even length and uniform values.

### Step 1: Understand Runs and Groupings

The first observation is that the string consists of alternating runs of '0's and '1's. For example, in the string "110011", the runs are:
- Run 1: "11" (length 2, all '1's)
- Run 2: "00" (length 2, all '0's)
- Run 3: "11" (length 2, all '1's)

These runs already satisfy the uniformity condition. If each run is of even length, then we can consider it as a valid subsegment, and no flips are needed. If the length is odd, then we need to fix that, either by flipping one character to merge it with an adjacent segment or by adjusting the boundaries of the segments to form valid even-length groups.

### Step 2: Flipping to Make Runs Even

Whenever we have a run of odd length, a flip will be necessary. This is because there's no way to split an odd-length sequence into smaller even-length sequences without modifying the characters. The minimal cost to fix an odd run is typically to flip one bit to balance it into an even grouping with its neighboring run.

For instance, consider a scenario like: "1110001". The runs are:
- Run 1: "111" (odd)
- Run 2: "000" (odd)
- Run 3: "1" (odd)

Each odd run contributes to imbalance. Since we need segments of even length, we must modify some runs — perhaps by flipping a bit in a neighboring run to merge them or adjusting run boundaries by one character via a flip.

The important insight is that **odd-length runs are the root cause of imbalance**, and each one typically requires **one flip** to become part of a valid even-length segment. However, there’s an optimization layer: if two adjacent odd-length runs exist, flipping the boundary character between them can make both even in a single move. For instance, changing the last bit of one run and the first of the next may allow combining them into a longer, even-length segment.

### Step 3: Greedy and Two-Pointer Strategy

The optimal strategy involves scanning the string and identifying these runs. For each run:
- If the run has even length: accept it as a segment.
- If the run has odd length: attempt to pair it with the next run if it's also odd.

This leads to a greedy algorithm where:
- You traverse the string and track the length of current runs.
- If you find two adjacent odd runs, one flip at the boundary can resolve both.
- If you have a single odd run with no odd neighbor, then a flip is required just for that one.

This strategy minimizes the number of flips, as each flip contributes toward balancing one or two runs.

### Step 4: Tracking Subsegments

Once the string has been transformed such that all runs have even lengths, you count how many such segments exist. This corresponds to how many contiguous runs you can form without further modification.

Among all configurations that use the minimum number of flips (as achieved by the greedy pairing of odd runs), you must select the one with the least number of resulting segments. That’s why pairing two odd runs into one even-length run not only saves flips but also reduces the number of subsegments.

### Step 5: Edge Cases

Some edge scenarios to consider:
- A string entirely of '1's or '0's, but of odd length.
- Alternating single-character sequences like "101010..."
- Long sequences of uniform values.

In each of these, applying the above logic of pairing odd-length runs and carefully tracking segments will yield the optimal result.

### Step 6: Efficiency

The final algorithm will scan the string once to identify runs (O(n)), and make decisions during the scan to pair odd runs and count segments. This ensures the solution works efficiently for large strings (up to 100,000 characters) as required by the problem constraints.

To conclude, the problem revolves around:
- Recognizing runs of same characters.
- Pairing odd-length runs to minimize flips.
- Counting the resulting even-length uniform segments.
By handling these systematically, we achieve the goal of minimal flips and minimal segments.
```


Related Questions