| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | 3 | | // All Rights Reserved. |
| | 4 | | // -------------------------------------------------------------------------------- |
| | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | 9 | | // known as Yevhenii Yeriemeieiv). |
| | 10 | | // -------------------------------------------------------------------------------- |
| | 11 | |
|
| | 12 | | namespace LeetCode.Algorithms.PushDominoes; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class PushDominoesForceArray : IPushDominoes |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(n) |
| | 19 | | /// Space complexity - O(n) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="dominoes"></param> |
| | 22 | | /// <returns></returns> |
| | 23 | | public string PushDominoes(string dominoes) |
| 2 | 24 | | { |
| 2 | 25 | | var forces = new int[dominoes.Length]; |
| | 26 | |
|
| 2 | 27 | | var leftForce = 0; |
| | 28 | |
|
| 40 | 29 | | for (var left = 0; left < dominoes.Length; left++) |
| 18 | 30 | | { |
| 18 | 31 | | leftForce = dominoes[left] switch |
| 18 | 32 | | { |
| 4 | 33 | | 'R' => dominoes.Length, |
| 4 | 34 | | 'L' => 0, |
| 10 | 35 | | _ => Math.Max(0, leftForce - 1) |
| 18 | 36 | | }; |
| | 37 | |
|
| 18 | 38 | | forces[left] += leftForce; |
| 18 | 39 | | } |
| | 40 | |
|
| 2 | 41 | | var rightForce = 0; |
| | 42 | |
|
| 40 | 43 | | for (var right = dominoes.Length - 1; right >= 0; right--) |
| 18 | 44 | | { |
| 18 | 45 | | rightForce = dominoes[right] switch |
| 18 | 46 | | { |
| 4 | 47 | | 'R' => 0, |
| 4 | 48 | | 'L' => dominoes.Length, |
| 10 | 49 | | _ => Math.Max(0, rightForce - 1) |
| 18 | 50 | | }; |
| | 51 | |
|
| 18 | 52 | | forces[right] -= rightForce; |
| 18 | 53 | | } |
| | 54 | |
|
| 2 | 55 | | var result = new char[dominoes.Length]; |
| | 56 | |
|
| 40 | 57 | | for (var i = 0; i < forces.Length; i++) |
| 18 | 58 | | { |
| 18 | 59 | | result[i] = forces[i] switch |
| 18 | 60 | | { |
| 6 | 61 | | > 0 => 'R', |
| 7 | 62 | | < 0 => 'L', |
| 5 | 63 | | _ => '.' |
| 18 | 64 | | }; |
| 18 | 65 | | } |
| | 66 | |
|
| 2 | 67 | | return new string(result); |
| 2 | 68 | | } |
| | 69 | | } |