content: package codingquestions.leetcode;
  
  import java.util.Stack;
  
  /**
   * 1402. Reducing Dishes
   * Solved
   * Hard
   * Topics
   * Companies
   * Hint
   * A chef has collected data on the satisfaction level of his n dishes. Chef can cook any dish in 1 unit of time.
   * <p>
   * Like-time coefficient of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. time[i] * satisfaction[i].
   * <p>
   * Return the maximum sum of like-time coefficient that the chef can obtain after preparing some amount of dishes.
   * <p>
   * Dishes can be prepared in any order and the chef can discard some dishes to get this maximum value.
   */
  
  public class MaxWidthramp {
      public int maxWidthRamp(int[] nums) {
          int n = nums.length;
          Stack<Integer> stack = new Stack<>();
  
          // Step 1: Build a decreasing stack of indices
          for (int i = 0; i < n; ++i) {
              if (stack.isEmpty() || nums[stack.peek()] > nums[i]) {
                  System.out.println(i);
                  stack.push(i);
              }
          }
  
          int maxWidth = 0;
  
          // Step 2: Traverse from the end and find maximum width ramp
          for (int j = n - 1; j >= 0; --j) {
              while (!stack.isEmpty() && nums[stack.peek()] <= nums[j]) {
                  maxWidth = Math.max(maxWidth, j - stack.pop());
              }
          }
  
          return maxWidth;
      }
  }