Skip to content

Instantly share code, notes, and snippets.

@unilecs
Created October 9, 2023 02:20
Show Gist options
  • Save unilecs/61bab8a60cdcf478d8e0493ca8121b7c to your computer and use it in GitHub Desktop.
Save unilecs/61bab8a60cdcf478d8e0493ca8121b7c to your computer and use it in GitHub Desktop.
Задача: Цена со скидкой
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public static int[] ApplyDiscounts(int[] prices)
{
int len = prices.Length;
var stack = new Stack<int>();
for (int i = 0; i < len; i++) {
while(stack.Any() && prices[stack.Peek()] >= prices[i])
{
prices[stack.Pop()] -= prices[i];
}
stack.Push(i);
}
return prices;
}
public static void Main()
{
Console.WriteLine("UniLecs");
// tests
Console.WriteLine(string.Join(", ", ApplyDiscounts(new int[] { 8,4,6,2,3 }))); // 4,2,4,2,3
Console.WriteLine(string.Join(", ", ApplyDiscounts(new int[] { 1,2,3,4,5 }))); // 1,2,3,4,5
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment