Prefix cabinet: Peak ticket
Stuck or curious how this works? Visualize Binary Heap Operations ↗
Inputs
words is a list of words.
Your tasks
- Create a list
prefixTotals. A prefix is the start of a word. For example, "c", "ca", and "cat" are the prefixes with at least one letter of "cat". For each word inwords, look at all its prefixes with at least one letter. Count how many inputwordsstart with each prefix, then add those counts. Count repeated inputwordsseparately, and include the word itself. Put one total per word inprefixTotals, in input order. - Find the largest number in
prefixTotals. Store it inlargest. - Complete
prefixCabinetPeakTicketso it returnslargest. Return it, do not print it.
Example
If words is ["key", "key", "keh", "ledger"], then prefixTotals is [8, 8, 7, 6] and prefixCabinetPeakTicket returns 8.
Rules
- If
prefixTotalsis empty, return 0.
Test data
TEST DATA 1: words is ["margin", "marer", "malk", "ink"].
TEST DATA 2: words is ["folio", "folio", "folip"].
Examples
Input: words = ["margin","marer","malk","ink"] Output: 11
prefixTotals is [11, 10, 8, 3]. The largest number is 11. Return 11.
Input: words = ["folio","folio","folip"] Output: 14
prefixTotals is [14, 14, 13]. The largest number is 14. Return 14.
Input: words = [] Output: 0
prefixTotals is []. The list is empty, so return 0.
Input limits
- 0 <= input size <= 64.
- Answers fit in a signed 32-bit integer: -2,147,483,648 through 2,147,483,647.
- There are at most 64 lowercase English words, each with 1 to 12 letters.
Efficiency goal
Big-O describes how the work or memory needed grows as the input gets larger.
Efficiency goal: time O(total characters); extra memory, including the helper list, O(n). n is the input length; total characters means all word lengths added together. These labels describe how work and memory grow with the input.
Practice focus
Build prefixTotals 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.