All problems
0218EasyArrayStackMonotonic Stack

Job Hour Credits

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1475Final Prices With a Special Discount in a Shop

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A workshop keeps its jobs in one queue and bills them in queue order. hours holds the hours booked against each job, position by position.

One settlement rule applies to every job. Read further along the queue from that job and stop at the first job whose booked hours are no more than this job's booked hours; that many hours come off this job's bill. If no later job qualifies, the job is billed exactly as booked.

The rule is worked out for each job from the booked figures alone, so a credit given to one job never changes what any other job is credited, and a job is never credited against itself.

Return the bill for every job, in queue order.

Examples

Example 1

Input
hours = [9, 5, 5, 12, 4]
Output
[4, 0, 1, 8, 4]

Job 0 stops at the 5 in position 1, so it is billed 9 - 5. Job 1 stops at the equal 5 in position 2 and is billed 5 - 5. Job 2 stops at the 4 in position 4 and is billed 5 - 4. Job 3 stops at that same 4 and is billed 12 - 4. Job 4 has nothing after it, so its 4 hours stand.

Example 2

Input
hours = [7, 8, 10]
Output
[7, 8, 10]

Every job further along the queue is booked for more hours than the one before it, so no job finds a qualifying job and each is billed as booked.

Example 3

Input
hours = [12, 3, 12, 3]
Output
[9, 0, 9, 3]

Both 12-hour jobs stop at a 3 that follows them and are billed 9. The 3 in position 1 stops at the equal 3 in position 3 and is billed 0, while the last job keeps its 3 hours.

Constraints

  • 1 <= hours.length <= 500
  • 1 <= hours[i] <= 1000

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def job_hour_credits(hours: list[int]) -> list[int]:
Java
public int[] jobHourCredits(int[] hours)
September 7
Apply