| | 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.DefuseTheBomb; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class DefuseTheBombSlidingWindow : IDefuseTheBomb |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(n) |
| | 19 | | /// Space complexity - O(n) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="code"></param> |
| | 22 | | /// <param name="k"></param> |
| | 23 | | /// <returns></returns> |
| | 24 | | public int[] Decrypt(int[] code, int k) |
| 3 | 25 | | { |
| 3 | 26 | | var result = new int[code.Length]; |
| | 27 | |
|
| 3 | 28 | | if (k == 0) |
| 1 | 29 | | { |
| 1 | 30 | | return result; |
| | 31 | | } |
| | 32 | |
|
| 2 | 33 | | var sum = 0; |
| 2 | 34 | | var start = k > 0 ? 1 : code.Length + k; |
| 2 | 35 | | var end = k > 0 ? k : code.Length - 1; |
| | 36 | |
|
| 14 | 37 | | for (var i = start; i <= end; i++) |
| 5 | 38 | | { |
| 5 | 39 | | sum += code[i % code.Length]; |
| 5 | 40 | | } |
| | 41 | |
|
| 20 | 42 | | for (var i = 0; i < code.Length; i++) |
| 8 | 43 | | { |
| 8 | 44 | | result[i] = sum; |
| | 45 | |
|
| 8 | 46 | | sum -= code[start % code.Length]; |
| | 47 | |
|
| 8 | 48 | | start++; |
| 8 | 49 | | end++; |
| | 50 | |
|
| 8 | 51 | | sum += code[end % code.Length]; |
| 8 | 52 | | } |
| | 53 | |
|
| 2 | 54 | | return result; |
| 3 | 55 | | } |
| | 56 | | } |