#2659

Make Array Empty

hard · verified · 26.7% accepted · 571 likes · top 5%

array · binary search · greedy · binary indexed tree · segment tree · sorting · ordered set

Description

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

Example 2:

Input: nums = [1,2,4,3]
Output: 5

Example 3:

Input: nums = [1,2,3]
Output: 3

Solution