Sign in with Google

Google will share your name, email, and profile picture with DevExCode. See our privacy policy.

LeetCode 3293: Calculate Product Final Price

Database

LeetCode 3293 Solution Explanation

Explanation:

To solve this problem, we iterate through the array from left to right. For each element, we iterate through the subsequent elements to find the first element that is smaller than the current element. If found, we update the current element by subtracting the found element from it. If no smaller element is found, the current element remains unchanged. :

LeetCode 3293 Solutions in Java, C++, Python

class Solution {
    public int[] finalPrices(int[] prices) {
        for (int i = 0; i < prices.length; i++) {
            int discount = 0;
            for (int j = i + 1; j < prices.length; j++) {
                if (prices[j] <= prices[i]) {
                    discount = prices[j];
                    break;
                }
            }
            prices[i] -= discount;
        }
        return prices;
    }
}

Interactive Code Editor for LeetCode 3293

Improve Your LeetCode 3293 Solution

Use the editor below to refine the provided solution for LeetCode 3293. Select a programming language and try the following:

  • Add import statements if required.
  • Optimize the code for better time or space complexity.
  • Add test cases to validate edge cases and common scenarios.
  • Handle error conditions or invalid inputs gracefully.
  • Experiment with alternative approaches to deepen your understanding.

Click "Run Code" to execute your solution and view the output. If errors occur, check the line numbers and debug accordingly. Resize the editor by dragging its bottom edge.

Loading editor...

Related LeetCode Problems