Trader Joe Trades
Updated
Problem statement
Joe the trader works with 26 types of trade operations, labeled with the uppercase letters 'A' to 'Z'. He is given a list of operations in a fixed order. Operation i has type tasks[i] (the i-th character of the string tasks) and earns rewards[i].
Joe goes through the operations in order and either executes or skips each one. The executed operations, kept in their original order, must never contain more than k operations of the same type in a row. For example, if k = 2, the executed sequence "BCCC" is invalid because 'C' appears 3 times in a row, while "BCBCC" is valid.
Skipping an operation does not reset the count of identical operations in a row: only the executed operations form the sequence that is checked.
Return the maximum total reward of the executed operations.
Function
traderTrades(tasks: String, rewards: int[], k: int) → intExamples
Example 1
tasks = "BAAAB"rewards = [1, 4, 2, 10, 3]k = 2return = 18The best strategy is to skip the task at index 2 (tasks[2] = 'A'). The sequence then becomes "BAAB", leading to a total gain of 1 + 4 + 10 + 3 = 18.
Constraints
1 ≤ tasks.length ≤ 10^5rewards.length = tasks.lengthtaskscontains only uppercase English letters'A'to'Z'.1 ≤ rewards[i] ≤ 10^41 ≤ k ≤ tasks.length
More Amazon interview questions
- LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Maximal SquareONSITE INTERVIEW · Seen Nov 2026
- Serialize and Deserialize a Binary TreeONSITE INTERVIEW · Seen Nov 2026
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Course Schedule IIPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Diagonal Traversal of a Binary TreeONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Spread Through a GridPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Consistent-Hash Packet RouterPHONE SCREEN · Seen Nov 2026
- Count Connected ComponentsPHONE SCREEN · Seen Nov 2026
- Maximize K-Element AND with an Increment BudgetOA · Seen Nov 2026
Similar questions at other companies
- Split Array to Minimize the Largest SumInfosys · OAInfosys interview questions
- Split Array Largest SumAuxoAI · OA
- Get Minimum IncrementIBM · OAIBM interview questions
- Minimum Satellite Data Transfer IterationsHSBC · OAHSBC interview questions
- Minimum Number of Taps to Water a GardenGoldman Sachs · OAGoldman Sachs interview questions
- Minimum Cost to Select People for Skill QuotasVisa · OAVisa interview questions
- Find Minimum Cost to Shift MachinesCisco · OACisco interview questions
- Maximize Sum of Processed TimesSalesforce · OASalesforce interview questions
- Get Process TimeLinkedIn · OALinkedIn interview questions
- Maximize Wall HitsOpenAI · OAOpenAI interview questions