BackmediumGreedyuncategorizedmedium

Max Colonists With Resources Solution

Problem Statement

You are given three arrays representing the capacities of oxygen tanks, water containers, and food packets, along with the respective requirements of each colonist. Determine the maximum number of colonists that can be sustained with the available resources, given the arrays of oxygen capacities, water capacities, food capacities, oxygen requirements, water requirements, and food requirements.

Example 1
Input
[10, 20, 30, 40, 50], [10, 20, 30, 40, 50], [10, 20, 30, 40, 50], [5, 5, 5], [5, 5, 5], [5, 5, 5]
Output
3

Explanation: Step-by-step: With input [10, 20, 30, 40, 50], [10, 20, 30, 40, 50], [10, 20, 30, 40, 50], [5, 5, 5], [5, 5, 5], [5, 5, 5], we first find the minimum capacity for each resource. Then we assign the resources to each colonist until it runs out of resources or colonists. In this case, we can sustain 3 colonists.

Example 2
Input
[100, 200, 300], [100, 200, 300], [100, 200, 300], [10, 10, 10], [10, 10, 10], [10, 10, 10]
Output
3

Explanation: Step-by-step: With input [100, 200, 300], [100, 200, 300], [100, 200, 300], [10, 10, 10], [10, 10, 10], [10, 10, 10], we first find the minimum capacity for each resource. Then we assign the resources to each colonist until it runs out of resources or colonists. In this case, we can sustain 3 colonists.

Constraints

  • 1 <= oxygen.length, water.length, food.length <= 10^5
  • 1 <= oxygenReq.length, waterReq.length, foodReq.length <= 10^5
  • 1 <= oxygen[i], water[i], food[i] <= 10^6
  • 1 <= oxygenReq[i], waterReq[i], foodReq[i] <= 10^6
  • The total amount of each resource is not more than 10^6
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

Max Colonists With Resources — Problem Statement & Solution Guide

GreedyMediumMixed
TimeO(n log n)
|
SpaceO(1) additional (ignoring input storage)

Problem Description

You are given three arrays representing the capacities of oxygen tanks, water containers, and food packets, along with the respective requirements of each colonist. Determine the maximum number of colonists that can be sustained with the available resources, given the arrays of oxygen capacities, water capacities, food capacities, oxygen requirements, water requirements, and food requirements.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Max Colonists With Resources"

medium

WHY DOES IT MATTER?

The greedy matching pattern is essential for any scenario where limited, indivisible resources must be allocated to competing demands with a simple feasibility condition (capacity ≥ requirement). It guarantees optimality with minimal computational overhead, making it a go‑to technique for interview problems involving scheduling, packing, or resource assignment.

OPTIMIZATION CHALLENGE

The breakthrough is recognizing that the three resources are independent; by sorting each list once and scanning linearly, we avoid exponential subset checks. The final answer is simply the bottleneck among the three match counts, turning a potentially 3‑dimensional knapsack into three 1‑dimensional greedy matches.

REAL-WORLD CONNECTION

Think of a cloud provider allocating VM instances: each request needs a certain amount of CPU, RAM, and storage. The provider matches the smallest sufficient host for each request to maximize the number of customers served, analogous to matching colonists to resource units.

During an interview, sort all six arrays up front, then run three identical two‑pointer loops. Keep the counts in variables and return the minimum. This one‑liner after sorting demonstrates both clarity and optimality.

COMPLEXITY AT A GLANCE

⏱ Time:O(n log n)
💾 Space:O(1) additional (ignoring input storage)

Core Theory — Why This Approach?

The problem reduces to three independent one‑dimensional assignment sub‑problems: matching colonists to oxygen tanks, water containers, and food packets. For each resource type we have a multiset of capacities and a multiset of per‑colonist requirements. The optimal way to maximize the number of satisfied colonists is to pair the smallest requirement that can be met with the smallest sufficient capacity, a classic greedy strategy that works because both lists are sorted and each resource can be used at most once. Naïve enumeration of every subset of colonists would be exponential (O(2^n)) and infeasible for large n, as it essentially solves a 3‑dimensional knapsack. By decoupling the resources and applying the greedy two‑pointer technique, we achieve linearithmic time, and the overall answer is the minimum of the three individual match counts, because a colonist must receive all three resources to be counted.

The optimal paradigm is therefore a combination of sorting (O(n log n)) and a greedy scan (O(n)). This pattern appears in many allocation problems where each demand must be satisfied by a single supply unit and the goal is to maximize the number of satisfied demands. The key insight is that once the resources are sorted, any deviation from the greedy pairing would either waste a larger capacity on a smaller requirement or leave a smaller capacity unused, both of which can only reduce the total matches.

Interview Questions on This Problem

Q1How would you modify the solution if each colonist could consume multiple units of a resource (e.g., two oxygen tanks) but still only one of each type per colonist?

Treat each colonist as requiring a weighted demand equal to the number of units needed. Expand the resource list by replicating each capacity unit or, more efficiently, keep a count of remaining units per resource type and decrement it when a colonist is assigned. The greedy approach still applies: sort colonists by total demand and allocate the smallest sufficient set of units, using a multiset or priority queue to track available capacities.

Q2Explain why a binary search on the answer (maximum number of colonists) combined with a feasibility check can also solve the problem, and compare its complexity to the direct greedy method.

Binary searching the answer reduces the problem to a decision version: can we satisfy k colonists? For a given k, we take the k smallest requirements for each resource and check if the k‑th smallest capacity is >= the k‑th smallest requirement using two‑pointer scans. This yields O(log n) iterations, each O(k) ≈ O(n), so overall O(n log n), same asymptotic as sorting once. However, the direct greedy scan avoids the extra log factor and is simpler to implement.

Q3In a distributed system where resources are located on different nodes, how would you ensure the greedy matching remains correct without centralizing all data?

Each node can locally sort its resource capacities and expose a prefix‑sum or quantile interface. A coordinator gathers the sorted requirement lists, then performs a distributed binary search to find the cut‑off index where capacities meet requirements, using collective communication (e.g., all‑reduce) to aggregate counts. This preserves the greedy invariant while minimizing data movement.

Examples

Example 1

Input

[10, 20, 30, 40, 50], [10, 20, 30, 40, 50], [10, 20, 30, 40, 50], [5, 5, 5], [5, 5, 5], [5, 5, 5]

Output

3

Explanation: Step-by-step: With input [10, 20, 30, 40, 50], [10, 20, 30, 40, 50], [10, 20, 30, 40, 50], [5, 5, 5], [5, 5, 5], [5, 5, 5], we first find the minimum capacity for each resource. Then we assign the resources to each colonist until it runs out of resources or colonists. In this case, we can sustain 3 colonists.

Example 2

Input

[100, 200, 300], [100, 200, 300], [100, 200, 300], [10, 10, 10], [10, 10, 10], [10, 10, 10]

Output

3

Explanation: Step-by-step: With input [100, 200, 300], [100, 200, 300], [100, 200, 300], [10, 10, 10], [10, 10, 10], [10, 10, 10], we first find the minimum capacity for each resource. Then we assign the resources to each colonist until it runs out of resources or colonists. In this case, we can sustain 3 colonists.

Constraints

  • 1 <= oxygen.length, water.length, food.length <= 10^5
  • 1 <= oxygenReq.length, waterReq.length, foodReq.length <= 10^5
  • 1 <= oxygen[i], water[i], food[i] <= 10^6
  • 1 <= oxygenReq[i], waterReq[i], foodReq[i] <= 10^6
  • The total amount of each resource is not more than 10^6

Optimal Approach & Strategy

Sort each requirement list and each capacity list, then greedily match the smallest sufficient capacity to each requirement using a two‑pointer scan; the answer is the minimum match count across the three resources.

Brute Force Approach

Try every possible subset of colonists and check if the sum of their requirements fits within the total capacities for oxygen, water, and food; keep the largest feasible subset.

Verified Code Solutions

JavaScript Solution
Time: O(n log n)
function maxColonists(oxygenCapacities, waterCapacities, foodCapacities, oxygenRequirements, waterRequirements, foodRequirements) {
      let oxygen = 0, water = 0, food = 0;
      let colonists = 0;
      for (let i = 0; i < oxygenCapacities.length; i++) {
         oxygen = oxygenCapacities[i];
         water = waterCapacities[i];
         food = foodCapacities[i];
         if (oxygen >= oxygenRequirements[i] && water >= waterRequirements[i] && food >= foodRequirements[i]) {
            colonists++;
            oxygen -= oxygenRequirements[i];
            water -= waterRequirements[i];
            food -= foodRequirements[i];
         }
      }
      return colonists;
   }

Asked in Top Tech Interviews

uncategorizedmediumnone

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.