mediumArraysPattern: Dynamic Programming

Maximum Tomes On Shelf Solution

Problem Statement

You are given an array of integers `weights` representing the weights of tomes and an integer `limit` representing the weight limit of a shelf. Write a function that returns the maximum number of tomes that can be placed on the shelf without exceeding the weight limit.

Examples

Example 1:
Input:[1, 2, 3, 4, 5], 10
Output:3
Explanation: The maximum number of tomes that can be placed on the shelf is 3 (1+2+3 = 6, adding the next tome would exceed the limit)
Example 2:
Input:[5, 5, 5], 10
Output:2
Explanation: The maximum number of tomes that can be placed on the shelf is 2 (5+5 = 10, adding the next tome would exceed the limit)

Constraints

  • The sales figures are integers.
  • The window size is a positive integer.
  • The sales figures are subject to sudden spikes or crashes.
Time: O(n*windowSize) Space: O(1)
The optimized approach involves using dynamic programming with a time complexity of O(n*limit) to find the maximum number of tomes that can be placed on the shelf.

Run, Test & Submit Code

Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.

Solve on Interactive Workspace

Tested Solutions

No solution code is currently loaded.
Complete this code in the workspace editor.