Tap here to switch tabs
Problem
Submissions

Problem: Final Prices With a Special Discount in a Shop

Final Prices With a Special Discount in a Shop gives a price array. Return final prices in order, subtracting the first later price no greater than each item's price when one exists.

Statement

You are given an integer array prices where prices[i] is the price of the ith item in a shop.

For each item i, find the smallest index j such that j>ij > i and prices[j] <= prices[i]. If such an index j exists, the discount for item i is prices[j], and the final price becomes prices[i] - prices[j]. If no such j exists, the final price for item i remains prices[i].

Return an array of the final prices for all items in the same order as prices.

Constraints:

  • 1≤1 \leq prices.length ≤500\leq 500

  • 1≤1 \leq prices[i] ≤1000\leq 1000

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Final Prices With a Special Discount in a Shop

Final Prices With a Special Discount in a Shop gives a price array. Return final prices in order, subtracting the first later price no greater than each item's price when one exists.

Statement

You are given an integer array prices where prices[i] is the price of the ith item in a shop.

For each item i, find the smallest index j such that j>ij > i and prices[j] <= prices[i]. If such an index j exists, the discount for item i is prices[j], and the final price becomes prices[i] - prices[j]. If no such j exists, the final price for item i remains prices[i].

Return an array of the final prices for all items in the same order as prices.

Constraints:

  • 1≤1 \leq prices.length ≤500\leq 500

  • 1≤1 \leq prices[i] ≤1000\leq 1000