Group Transactions by Hour
Problem statement
A payments service keeps a log of transactions. You are given transactions, where transactions[i] = [id, time] gives a transaction's unique ID and the time it happened, in seconds. You are also given the current time now, in seconds.
Group the transactions from the past 24 hours by hour. The age of a transaction is now - time. Hour k, for 0 <= k <= 23, holds every transaction that is k full hours old, meaning k * 3600 <= age < (k + 1) * 3600. So hours are counted back from now, not by the clock, and hour 0 is the most recent hour.
A transaction with a negative age (it is in the future) or with age >= 86400 (24 hours or older) is in no group.
Return an array of exactly 24 groups, where group k lists the IDs of the transactions in hour k. Inside a group, order the IDs by time, earliest first, and order transactions with the same time by ID, smallest first. A group with no transactions is an empty array.
Function
groupByHour(transactions: int[][], now: int) → int[][]Examples
Example 1
transactions = [[101,10800],[102,9000],[103,7200],[104,7300],[105,12000],[106,9000]]now = 10800return = [[104,102,106,101],[103],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]]now - timeis0for 101,1800for 102 and 106,3500for 104, and3600for 103. Transaction 105 hastime = 12000 > now, so it is in the future and in no group.- 101, 102, 104 and 106 are less than
3600seconds old, so they are in hour 0. In time order that is 104 (7300), then 102 and 106 (both 9000, smaller ID first), then 101 (10800). - 103 is exactly
3600seconds old, so it is in hour 1, not hour 0. - Hours 2 to 23 are empty.
Example 2
transactions = [[1,13600],[2,13601],[3,82000],[4,82001]]now = 100000return = [[],[],[],[],[4],[3],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[2]]- Transaction 1 is
100000 - 13600 = 86400seconds old, a full 24 hours, so it is in no group. - Transaction 2 is
86399seconds old, so it is in hour86399 / 3600 = 23(rounded down). - Transaction 3 is
18000seconds old, exactly 5 hours, so it is in hour 5. Transaction 4 is17999seconds old, so it is in hour 4. - Every other hour is empty.
Example 3
transactions = [[8,5],[0,0]]now = 0return = [[0],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]]With now = 0, transaction 0 happened right now, so it is in hour 0. Transaction 8 has time = 5 > now, so it is in the future and in no group. The other 23 hours are empty.
Constraints
1 <= transactions.length <= 10^4transactions[i].length == 2- Every ID and every time is an integer from
0to10^9. - All transaction IDs are distinct.
0 <= now <= 10^9