| | 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.MaximumScoreAfterSplittingString; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class MaximumScoreAfterSplittingStringIterative : IMaximumScoreAfterSplittingString |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(n) |
| | 19 | | /// Space complexity - O(1) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="s"></param> |
| | 22 | | /// <returns></returns> |
| | 23 | | public int MaxScore(string s) |
| 3 | 24 | | { |
| 3 | 25 | | var zeros = 0; |
| | 26 | |
|
| 3 | 27 | | if (s[0] == '0') |
| 2 | 28 | | { |
| 2 | 29 | | zeros++; |
| 2 | 30 | | } |
| | 31 | |
|
| 3 | 32 | | var ones = 0; |
| | 33 | |
|
| 30 | 34 | | for (var i = 1; i < s.Length; i++) |
| 12 | 35 | | { |
| 12 | 36 | | if (s[i] == '1') |
| 10 | 37 | | { |
| 10 | 38 | | ones++; |
| 10 | 39 | | } |
| 12 | 40 | | } |
| | 41 | |
|
| 3 | 42 | | var maxScore = zeros + ones; |
| | 43 | |
|
| 30 | 44 | | for (var i = 1; i < s.Length; i++) |
| 12 | 45 | | { |
| 12 | 46 | | maxScore = Math.Max(maxScore, zeros + ones); |
| | 47 | |
|
| 12 | 48 | | if (s[i] == '0') |
| 2 | 49 | | { |
| 2 | 50 | | zeros++; |
| 2 | 51 | | } |
| | 52 | | else |
| 10 | 53 | | { |
| 10 | 54 | | ones--; |
| 10 | 55 | | } |
| 12 | 56 | | } |
| | 57 | |
|
| 3 | 58 | | return maxScore; |
| 3 | 59 | | } |
| | 60 | | } |