Drawer grids: First qualifying slip
Inputs
drawers is a list of rows. Each row is a list of numbers and may be empty.
Your tasks
- Create a list
rowTotals.drawersis a list of rows. Add the numbers in each row. Put one total per row inrowTotals, in row order. An empty row has a total of 0. - Read
rowTotalsfrom left to right. Find the first number that is at leastkand store that number, not its position, infirstMatch. - Complete
drawerGridsFirstQualifyingSlipso it returnsfirstMatch. Return it, do not print it.
Example
If drawers is [[2, 0], [], [3, 1], [3, 1], [7, 5]] and k is 1, then rowTotals is [2, 0, 4, 4, 12] and drawerGridsFirstQualifyingSlip returns 2.
Rules
- If no number is at least
k, return -1. k(a whole number from 1 to 8) is the threshold in task 2: compare each number with it.
Test data
TEST DATA 1: drawers is [[8, 6], [], [0, -2], [7, 5], [7, 5]] and k is 6.
TEST DATA 2: drawers is [[-2, -4], [], [7, 5], [7, 5]] and k is 8.
Examples
Input: drawers = [[8,6],[],[0,-2],[7,5],[7,5]] k = 6 Output: 14
rowTotals is [14, 0, -2, 12, 12]. Here k is 6. The first number at least 6 is 14. Return that value.
Input: drawers = [[-2,-4],[],[7,5],[7,5]] k = 8 Output: 12
rowTotals is [-6, 0, 12, 12]. Here k is 8. The first number at least 8 is 12. Return that value.
Input: drawers = [] k = 6 Output: -1
rowTotals is []. Here k is 6. No number is at least 6, so return -1.
Input limits
- 0 <= input size <= 16 per row.
- k is a whole number from 1 to 8. 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.
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 input length. These labels describe how work and memory grow with the input.
Practice focus
Build rowTotals 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.