Paired drawers: Boss receipt
Stuck or curious how this works? Visualize Two Pointers ↗
Inputs
drawers contains exactly two lists of numbers. The first list is drawers[0]; the second is drawers[1].
Your tasks
- Create a list
merged.drawerscontains two lists, each already sorted from smallest to largest. Combine their numbers into one sortedmerged. Keep all repeated numbers, including copies from both input lists. - Multiply each number in
mergedby its position, counting from 1: the first number by 1, the second by 2, and so on. - Add those products. Store the sum in
weightedTotal. - Complete
pairedDrawersBossReceiptso it returnsweightedTotal. Return it, do not print it.
Example
If drawers is [[-6, 6, 6, 6, 10], [-6, 6, 6, 6, 10]], then merged is [-6, -6, 6, 6, 6, 6, 6, 6, 10, 10] and pairedDrawersBossReceipt returns 370.
Rules
- If
mergedis empty, return 0.
Test data
TEST DATA 1: drawers is [[-3, 0, 9, 9, 13], []].
TEST DATA 2: drawers is [[-4, 9, 9, 9], [19]].
Examples
Input: drawers = [[-3,0,9,9,13],[]] Output: 125
merged is [-3, 0, 9, 9, 13]. Multiply by positions that start at 1: 1 * (-3) + 2 * (0) + 3 * (9) + 4 * (9) + 5 * (13) = 125. Return 125.
Input: drawers = [[-4,9,9,9],[19]] Output: 172
merged is [-4, 9, 9, 9, 19]. Multiply by positions that start at 1: 1 * (-4) + 2 * (9) + 3 * (9) + 4 * (9) + 5 * (19) = 172. Return 172.
Input: drawers = [[],[]] Output: 0
merged is []. There are no numbers to multiply or add, so return 0.
Input limits
- 0 <= input size <= 64 per row.
- Answers fit in a signed 32-bit integer: -2,147,483,648 through 2,147,483,647.
- Input numbers have no decimal part. They range from -1,000,000 to 1,000,000. Follow any extra input rules in task 1.
- The input has exactly two rows, each containing at most 64 integers.
Efficiency goal
Big-O describes how the work or memory needed grows as the input gets larger.
Efficiency goal: time O(n); extra memory, including the helper list, O(n). n is the combined length of the two lists. These labels describe how work and memory grow with the input.
Practice focus
Build merged first, then return exactly what the tasks ask for. Follow the order, keep repeated values when the rule says so, and check the empty case.