-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution664.cs
42 lines (35 loc) · 969 Bytes
/
Solution664.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
42
using LeetCode.DataStructures;
namespace LeetCode.Solutions;
public class Solution664
{
/// <summary>
/// 664. Strange Printer - Hard
/// <a href="https://leetcode.com/problems/strange-printer">See the problem</a>
/// </summary>
public int StrangePrinter(string s)
{
var n = s.Length;
var dp = new int[n, n];
for (var i = n - 1; i >= 0; i--)
{
dp[i, i] = 1;
for (var j = i + 1; j < n; j++)
{
if (s[i] == s[j])
{
dp[i, j] = dp[i, j - 1];
}
else
{
var min = int.MaxValue;
for (var k = i; k < j; k++)
{
min = Math.Min(min, dp[i, k] + dp[k + 1, j]);
}
dp[i, j] = min;
}
}
}
return dp[0, n - 1];
}
}