Schedule Invoice Emails and Delinquencies
Problem statement
You are given a configurable invoice-email schedule, one invoice per customer, and an unsorted list of customer payments.
- Each schedule row is
offset,title. The offset is relative to the invoice time. - Each invoice row is
invoiceTime,name,amount. - Each payment row is
paymentTime,name,amount.
For every scheduled email time, first apply all payments made at or before that time. Send the email only when the customer still owes a positive amount. Format it as time: [title] Invoice for name for amount dollars.
The greatest schedule offset is the due-date offset. After all email lines, append Delinquent customers:, followed by one line name owes amount dollars for every invoice with a positive balance at its due time.
Sort email events by time, then invoice input position, then schedule input position. List delinquent customers in invoice input order.
Function
scheduleInvoiceEmails(schedule: String[], invoices: String[], payments: String[]) → String[]Examples
Example 1
schedule = ["-10,Upcoming","0,New","20,Reminder","30,Due"]invoices = ["0,Alice,200","1,Bob,100"]payments = ["-9,Alice,100","1,Alice,50","0,Bob,100"]return = ["-10: [Upcoming] Invoice for Alice for 200 dollars","-9: [Upcoming] Invoice for Bob for 100 dollars","0: [New] Invoice for Alice for 100 dollars","20: [Reminder] Invoice for Alice for 50 dollars","30: [Due] Invoice for Alice for 50 dollars","Delinquent customers:","Alice owes 50 dollars"]Payments reduce the amount shown by later emails. Bob pays in full before his new-invoice email, so all later Bob emails are skipped. Alice still owes 50 at the due time.
Example 2
schedule = ["-5,Upcoming","0,Due"]invoices = ["10,Cara,50"]payments = ["5,Cara,50"]return = ["Delinquent customers:"]The payment occurs at the first scheduled email time and is applied first, so no email is sent and Cara is not delinquent.
Example 3
schedule = ["0,New","10,Due"]invoices = ["0,Ada,40","0,Bea,30"]payments = []return = ["0: [New] Invoice for Ada for 40 dollars","0: [New] Invoice for Bea for 30 dollars","10: [Due] Invoice for Ada for 40 dollars","10: [Due] Invoice for Bea for 30 dollars","Delinquent customers:","Ada owes 40 dollars","Bea owes 30 dollars"]Equal-time emails preserve invoice input order. Neither customer pays, so both appear in the delinquency section.
Constraints
1 <= schedule.length <= 20; offsets are unique integers in strictly increasing order.1 <= invoices.length <= 1000; customer names are unique.0 <= payments.length <= 10000; every payment names a known customer.- Invoice amounts and payment amounts are positive integers at most
10^9. - Times and offsets fit in signed
32-bit integers, and their sums fit in signed64-bit integers. - Payments at an email timestamp are applied before that email. Payments beyond the due time do not affect delinquency.