Maximum Weight in Two Bags
Updated
Problem statement
You have items with weights weights and two bags with capacities w1 and w2. Each item may go into the first bag, into the second bag, or remain unpacked.
An item cannot be split or used in both bags. Neither bag may exceed its capacity. Return the greatest combined weight of the packed items.
Function
maxWeight(weights: int[], w1: int, w2: int) → intExamples
Example 1
weights = [1,4,3,2]w1 = 5w2 = 4return = 9Put weights 3 and 2 in the first bag and weight 4 in the second.
Example 2
weights = [3,6,4,8]w1 = 9w2 = 7return = 15Pack 8 in the first bag and 3 + 4 in the second.
Example 3
weights = [5,7]w1 = 2w2 = 3return = 0Neither item fits in either bag.
Constraints
1 <= weights.length <= 100.1 <= weights[i] <= 100.1 <= w1, w2 <= 300.
More Apple interview questions
- Merge IntervalsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Merge Two Strings by Maximum Boundary OverlapOA · Seen Sep 2026
- Collect Letters Along a Unique Grid PathOA · Seen Aug 2025
- Ignore SectionsOA · Seen Mar 2024
- Gas Station Circular TourOA · Seen Nov 2021
- House Robber IIOA · Seen Nov 2021
- Clone an Undirected Graph StateOA · Seen Mar 2021
- Longest Increasing Subsequence LengthOA · Seen Feb 2021
- Maximum Later DifferenceOA · Seen Feb 2021
- Topological OrderingPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Card Game: Best First-Player ScoreShopify · OAShopify interview questions
- Maximum Game Score with Fixed Forward JumpsStripe · OAStripe interview questions
- 0/1 KnapsackConfluent · OAConfluent interview questions
- Minimum Cost to Tile a Strip with Widths 1, 2, and 3Infosys · OAInfosys interview questions
- Maximum Escape Game ScoreMicrosoft · OAMicrosoft interview questions
- Maximum Subarray SumStraive · OA
- Longest Increasing Price SubsequenceOpenAI · OAOpenAI interview questions
- Lowest-Cost Shirt Color ScheduleElevenLabs · OAElevenLabs interview questions
- Maximum Game Board ScoreJP Morgan Chase · OAJP Morgan Chase interview questions
- API Bandwidth AllocationArista Networks · OAArista Networks interview questions