FastPrepShopkeeper Final Price Summary

Shopkeeper Final Price Summary

Microsoft logoMicrosoft● MediumNEW GRADOA
Learn

Problem statement

A shopkeeper arranges items in a list for a sale. For each item, find the first item to its right whose price is less than or equal to the current price.

If such an item exists, subtract its price from the current item's price. Otherwise, the current item is sold at full price.

Return a two-element String[] representing the required output lines:

  • The first string is the sum of the final costs of all items.
  • The second string contains the 0-based indices of every item sold at full price, separated by single spaces and listed in ascending order.

Function

finalPrice(prices: int[]) → String[]

Examples

Example 1

prices = [2,3,1,2,4,2]return = ["8","2 5"]

The final costs are [1,2,1,0,2,2], whose sum is 8. Items at indices 2 and 5 have no equal-or-lower-priced item to their right, so they are sold at full price.

Example 2

prices = [1,2,3,4]return = ["10","0 1 2 3"]

No item has an equal-or-lower-priced item to its right. Every item is sold at full price, the total is 10, and all indices appear on the second output line.

Constraints

  • 1 <= prices.length <= 10^5
  • 1 <= prices[i] <= 10^6

More Microsoft problems

See Microsoft hiring insights
public String[] finalPrice(int[] prices) {
  // write your code here
}
prices[2,3,1,2,4,2]
expected["8", "2 5"]
Checking account…