mediumStackPattern: Mixed

Warehouse Inventory Validation Solution

Problem Statement

Determine if a given warehouse inventory can be transformed into a desired inventory by removing items from the front of the inventory using a stack, where each item can only be removed once and the remaining items must be in the same order as they appear in the original inventory.

Examples

Example 1:
Input:{"inventory":[17,23,35,42,11],"target":[23,35,11]}
Output:true
Explanation: The inventory can be transformed into the target by removing 17 and 42 from the front.
Example 2:
Input:{"inventory":[51,73,28,41],"target":[73,41,28]}
Output:false
Explanation: The inventory cannot be transformed into the target because 28 must be after 41 in the original inventory.

Constraints

  • 1 <= inventory.length <= 1000
  • 1 <= target.length <= 1000
  • 0 <= inventory[i] <= 10000
  • 0 <= target[i] <= 10000
Time: O(n) Space: O(1)
A more efficient approach involves using two pointers to track the current position in the inventory and target sequences, and efficiently skipping over items in the inventory that do not match the target sequence, resulting in a time complexity of O(n).

Run, Test & Submit Code

Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.

Solve on Interactive Workspace

Tested Solutions

No solution code is currently loaded.
Complete this code in the workspace editor.