Advertisement

Make Array Empty - LeetCode 2659 Solution

Make Array Empty - Complete Solution Guide

Make Array Empty is LeetCode problem 2659, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are given an integer array nums containing distinct numbers, and you can perform the following operations until the array is empty : If the first element has the smallest value, remove it Otherwise, put the first element at the end of the array. Return an integer denoting the number of operations it takes to make nums empty. Example 1: Input: nums = [3,4,-1] Output: 5 Operation Array 1 [4, -1, 3] 2 [-1, 3, 4] 3 [3, 4] 4 [4] 5 [] Example 2: Input: nums = [1,2,4,3] Output: 5 Operation Array 1

Detailed Explanation

The problem presents a scenario where you're given an array of distinct integers, and you need to determine the number of operations to empty the array following a specific rule. If the first element of the array is the smallest, you remove it. Otherwise, you move it to the end of the array. The objective is to count the total operations required to completely empty the array.

Solution Approach

The solution leverages a Binary Indexed Tree (BIT) to keep track of the elements that have been removed from the array. First, we sort the array to determine the order in which the elements will be removed. Then, for each element in the sorted array, we determine its original position in the input array. We then calculate the number of moves it takes to bring that element to the front. The BIT is used to track which elements have been removed from the array. When considering a new position, we can use the BIT to calculate the number of already removed elements that are before the new position, thus helping to calculate the number of moves required.

Step-by-Step Algorithm

  1. Step 1: Create a map to store the original index (position) of each number in the input array 'nums'.
  2. Step 2: Sort the input array 'nums' and store it as `sorted_nums` to determine the order of removal.
  3. Step 3: Initialize a Binary Indexed Tree (BIT) of the same size as the input array to keep track of which elements have been removed.
  4. Step 4: Iterate through the `sorted_nums`. For each number, retrieve its original position (curr_pos) from the position map.
  5. Step 5: Determine the number of moves required to bring the element at `curr_pos` to the front. If `curr_pos > last_pos`, the target is to remove all elements between `last_pos` and `curr_pos`. Otherwise, all elements after last_pos and before curr_pos must be moved before the element at curr_pos can be moved.
  6. Step 6: Use BIT queries to determine how many of the elements in those ranges were already removed, and calculate the number of moves accordingly.
  7. Step 7: Add 1 to the total number of moves for removing the element at the front.
  8. Step 8: Update the BIT to mark the current element's position as removed.
  9. Step 9: Update `last_pos` with the current element's position `curr_pos` for the next iteration.
  10. Step 10: Return the total number of operations.

Key Insights

  • Insight 1: The core of the problem lies in efficiently tracking the positions of the elements as they are being removed or shifted within the array.
  • Insight 2: Using sorting to determine the order in which elements are removed is crucial. However, directly simulating array rotations is inefficient.
  • Insight 3: A Binary Indexed Tree (BIT), also known as a Fenwick Tree, is an ideal data structure to track removed elements and efficiently query the number of elements removed before a given position.

Complexity Analysis

Time Complexity: O(n log n)

Space Complexity: O(n)

Topics

This problem involves: Array, Binary Search, Greedy, Binary Indexed Tree, Segment Tree, Sorting, Ordered Set.

Companies

Asked at: Zepto.