Created
October 9, 2023 02:20
-
-
Save unilecs/61bab8a60cdcf478d8e0493ca8121b7c to your computer and use it in GitHub Desktop.
Задача: Цена со скидкой
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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