Tower

You are given an integer array nums representing the sizes of cubes that are handed to you one by one, in order.

Your goal is to build towers out of these cubes following one strict rule: a cube can only be placed on top of an existing tower if its size is strictly smaller than the cube currently at the top of that tower. If you cannot place the current cube on any existing tower, you must start a brand new tower with it. You must process the cubes in the exact order they appear in the array.

What is the minimum possible number of towers you can build?

Example 1:

Input: nums: [3, 8, 2, 1, 5]

Output: 2

Explanation:

  • Cube 3: Starts Tower 1 (Tops: [3])
  • Cube 8: Starts Tower 2 (Tops: [3, 8])
  • Cube 2: Placed on Tower 1 (Tops: [2, 8])
  • Cube 1: Placed on Tower 1 (Tops: [1, 8])
  • Cube 5: Placed on Tower 2 (Tops: [1, 5])

Total towers built: 2.

Example 2:

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

Output: 1

Explanation:

Since every incoming cube is strictly smaller than the previous one, they can all be stacked perfectly on top of each other to form a single tower.

Now Your Turn!

Pick the correct output for the given input

Input: nums: [10, 10, 10]

(What is the minimum number of towers needed for this array?)

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= nums.length <= 2 x 105
  • 1 <= nums[i] <= 109

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
int minTowers(vector<int>& nums) {
}
};
Test Case

Input:

Nums