From 9f958322b28881fc24caf62af65265bd78c6a93f Mon Sep 17 00:00:00 2001 From: Lam Haoyin Date: Tue, 22 Feb 2022 00:12:59 +0800 Subject: [PATCH] add: 220221-CN [csharp] --- csharp/Feb22/cn-0221/Program.cs | 96 ++++++++++++++++++++++++++++++--- 1 file changed, 88 insertions(+), 8 deletions(-) diff --git a/csharp/Feb22/cn-0221/Program.cs b/csharp/Feb22/cn-0221/Program.cs index 2de9a41..3e2c0e9 100644 --- a/csharp/Feb22/cn-0221/Program.cs +++ b/csharp/Feb22/cn-0221/Program.cs @@ -1,29 +1,109 @@ namespace cn_0221; public class Solution { - public static string PushDominoes(string dominoes) { + public string PushDominoes(string dominoes) { var q = new Queue(); var n = dominoes.Length; + var result = new int[n]; + var lastModified = new int[n]; + for (int i = 0; i < n; ++i) { if ('.' != dominoes[i]) { q.Enqueue(i); - q.Enqueue(0); + q.Enqueue(1); + + lastModified[i] = 1; + + if (dominoes[i] == 'L') { + result[i] = -1; + } + else { // == 'R' + result[i] = 1; + } } } - var result = new int[n]; - var lastModified = new int[n]; + int maxSize = 0, cnt = 0; while (q.Count > 0) { + ++cnt; + if (q.Count > maxSize) { + maxSize = q.Count; + } + // every point in queue must be either be altered to L or R + // Take one point out of the queue + // alter all its affected points + // And insert back all affected into queue. var position = q.Dequeue(); var timePoint = q.Dequeue(); - + + if (position > 0 && (lastModified[position - 1] == timePoint + 1 || lastModified[position - 1] == 0) && result[position] < 0) { + // L element not affected + + if (lastModified[position - 1] == 0) { + q.Enqueue(position - 1); + q.Enqueue(timePoint + 1); + } + + --result[position - 1]; + lastModified[position - 1] = timePoint + 1; + } else if (position < n - 1 && (lastModified[position + 1] == timePoint + 1 || lastModified[position + 1] == 0) && result[position] > 0) { + + if (lastModified[position + 1] == 0) { + q.Enqueue(position + 1); + q.Enqueue(timePoint + 1); + } + + ++result[position + 1]; + lastModified[position + 1] = timePoint + 1; + } + } + + string ret = ""; + + foreach (int i in result) { + if (i < 0) + ret += "L"; + else if (i > 0) + ret += "R"; + else + ret += "."; } - return "test"; + Console.WriteLine(maxSize); + Console.WriteLine(cnt); + + return ret; } +} +public class Runner { public static void Main() { - // empty + + var r = new Random(); + + string args = ""; + const int SIZE = 100000; + for (int i = 0; i < SIZE; ++i) { + switch (r.Next() % 3) { + case 0: + args += "L"; + break; + case 1: + args += "R"; + break; + case 2: + args += "."; + break; + } + } + + var s = new Solution(); + + long start = DateTimeOffset.Now.ToUnixTimeMilliseconds(); + var ret = s.PushDominoes(args); + long end = DateTimeOffset.Now.ToUnixTimeMilliseconds(); + + Console.WriteLine("" + (end - start) + " ms"); } -} \ No newline at end of file +}