Paginate Retained Conversation History
Problem statement
You are given an ordered conversation history messages, a positive retention limit historyLimit, and a positive page size pageSize.
First compact the history by discarding messages from the beginning until at most the newest historyLimit messages remain. Then split that retained suffix into consecutive pages of at most pageSize messages.
Return the pages in chronological order. Every retained message must appear exactly once, and no discarded message may appear.
Function
paginateConversationHistory(messages: String[], historyLimit: int, pageSize: int) → String[][]Examples
Example 1
messages = ["m1","m2","m3","m4","m5"]historyLimit = 4pageSize = 2return = [["m2","m3"],["m4","m5"]]The newest four messages are ["m2","m3","m4","m5"]. Splitting that retained suffix into pages of size 2 yields the two returned pages.
Example 2
messages = ["a","b","c"]historyLimit = 10pageSize = 2return = [["a","b"],["c"]]All three messages fit within historyLimit. The final page contains the one remaining message.
Example 3
messages = []historyLimit = 3pageSize = 2return = []An empty history has no retained messages and therefore no pages.
Constraints
0 <= messages.length <= 10^5- Every entry in
messagesis a non-empty ASCII string. 1 <= historyLimit <= 10^51 <= pageSize <= 10^5