I wanted to ask an idea to solve the question. But couldn't find the right place so posting it here, In a hope to get an answer from someone.
Find longest valid subarray in the orders array.
- Each order can be delivered or picked once.
- Delivery can only happen after pick up.
Ex 1: orders = ['P1', 'P1', 'D1'], return ['P1', 'D1'] Ex 2: orders = ['P1', 'P1', 'D1', 'D1'], return ['P1', 'D1'] Ex 3: orders = ['P1', 'P2' , 'P3', 'D2', 'D3'] return ['P2', 'P3', 'D2', 'D3']
How can we solve this? I could not think of a better solution than brute force which is O(n^3).