BackmediumStackAdobe

Valid Crate Stacking Solution

Problem Statement

Given two arrays of integers crateWeights1 and crateWeights2, determine if it is possible to stack crates from both arrays into a single stack following the last-in-first-out principle, such that the total weight of crates in the stack never decreases.

Example 1
Input
[3, 5, 4, 2, 1, 5, 4, 2, 1], [1, 2, 3, 4, 5]
Output
true

Explanation: Step-by-step: We can stack crates from both arrays in the order [3, 5, 4, 2, 1, 5, 4, 2, 1] following the last-in-first-out principle. First, we take the first crate from the first array (3), then the first crate from the second array (1), then the second crate from the first array (5), and so on. The total weight of crates in the stack never decreases.

Example 2
Input
[1, 2, 3, 4, 5, 3, 5, 4, 2, 1], [1, 2, 3, 4, 5]
Output
true

Explanation: Step-by-step: We can stack crates from both arrays in the order [1, 2, 3, 4, 5, 3, 5, 4, 2, 1] following the last-in-first-out principle. First, we take the first crate from the first array (1), then the first crate from the second array (1), then the second crate from the first array (2), and so on. The total weight of crates in the stack never decreases.

Constraints

  • 1 <= array lengths <= 100
  • 1 <= crate weights <= 1000
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Valid Crate Stacking — Problem Statement & Solution Guide

StackMediumMixed
TimeO(n)
|
SpaceO(1)

Problem Description

Given two arrays of integers crateWeights1 and crateWeights2, determine if it is possible to stack crates from both arrays into a single stack following the last-in-first-out principle, such that the total weight of crates in the stack never decreases.

Examples

Example 1

Input

[3, 5, 4, 2, 1, 5, 4, 2, 1], [1, 2, 3, 4, 5]

Output

true

Explanation: Step-by-step: We can stack crates from both arrays in the order [3, 5, 4, 2, 1, 5, 4, 2, 1] following the last-in-first-out principle. First, we take the first crate from the first array (3), then the first crate from the second array (1), then the second crate from the first array (5), and so on. The total weight of crates in the stack never decreases.

Example 2

Input

[1, 2, 3, 4, 5, 3, 5, 4, 2, 1], [1, 2, 3, 4, 5]

Output

true

Explanation: Step-by-step: We can stack crates from both arrays in the order [1, 2, 3, 4, 5, 3, 5, 4, 2, 1] following the last-in-first-out principle. First, we take the first crate from the first array (1), then the first crate from the second array (1), then the second crate from the first array (2), and so on. The total weight of crates in the stack never decreases.

Constraints

  • 1 <= array lengths <= 100
  • 1 <= crate weights <= 1000

Optimal Approach & Strategy

An optimal approach involves using two pointers, one for each array, to track the current element being considered for loading. By comparing the weights of the current elements and determining whether loading one would increase or maintain the total weight, we can efficiently decide the order of loading.

Brute Force Approach

One naive approach would involve generating all possible sequences of loading and unloading crates and checking each one to see if it meets the condition. This would have a high time complexity due to the large number of sequences. Another approach could involve using nested loops to compare each element from one array with every element from the other array.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function validCrateStacking(crateWeights1, crateWeights2) {
  if (crateWeights1.length === 0 && crateWeights2.length === 0) return true;
  if (crateWeights1.length === 0 || crateWeights2.length === 0) return false;
  let sum1 = 0, sum2 = 0;
  let i = crateWeights1.length - 1, j = crateWeights2.length - 1;
  while (i >= 0 && j >= 0) {
    if (crateWeights1[i] > crateWeights2[j]) {
      sum1 += crateWeights1[i--];
    } else {
      sum2 += crateWeights2[j--];
    }
  }
  return sum1 >= sum2;
}

Asked in Top Tech Interviews

Adobe

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.