-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
41 lines (40 loc) · 1.15 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using System;
using System.Linq;
namespace MinimumDeletionCost
{
class Program
{
static void Main(string[] args)
{
var s = "abc";
int[] cost = { 1, 2, 3};
Console.WriteLine(MinCost(s, cost));
}
static int MinCost(string s, int[] cost)
{
int maxTotalCost = 0, maxCost = cost[0], totalDeleteCost = 0;
int countSame = 1;
for (int i = 1; i < s.Length; i++)
{
if (s[i] == s[i - 1])
{
countSame++;
maxCost = Math.Max(maxCost, cost[i]);
totalDeleteCost += cost[i];
if (countSame == 2)
totalDeleteCost += cost[i - 1];
}
else
{
if (countSame > 1)
maxTotalCost += maxCost;
maxCost = cost[i];
countSame = 1;
}
}
if (countSame > 1)
maxTotalCost += maxCost;
return totalDeleteCost - maxTotalCost;
}
}
}