-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.cs
More file actions
157 lines (129 loc) · 4.97 KB
/
Solution.cs
File metadata and controls
157 lines (129 loc) · 4.97 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System;
using System.Linq;
namespace BiValuedArray
{
public class Solution
{
public int ObtainLargerValue(int current, int maximum) =>
current > maximum? current : maximum;
public int solution(int[] A)
{
var maximumLength = 0;
var currentLength = 2;
var repeatedValue = 0;
var repeatedValueCount = 0;
if (A.Length >= 2)
{
var values = (A[0], A[1]);
for (var index = 2; index < A.Length; index++)
{
if (values.Item1 == A[index] || values.Item2 == A[index])
{
currentLength++;
if (A[index] == A[index - 1])
{
repeatedValue = A[index];
repeatedValueCount++;
}
}
else
{
if (values.Item1 == values.Item2)
{
values = (values.Item2, A[index]);
currentLength++;
if (A[index] == A[index - 1])
{
repeatedValue = A[index];
repeatedValueCount++;
}
}
else
{
maximumLength = ObtainLargerValue(currentLength, maximumLength);
values = (A[index - 1], A[index]);
currentLength = 2 + (values.Item1 == repeatedValue? repeatedValue - 1 : 0);
repeatedValue = 0;
repeatedValueCount = 0;
}
}
}
maximumLength = ObtainLargerValue(currentLength, maximumLength);
}
return maximumLength;
}
}
public class SolutionAsObjectOriented
{
private readonly int[] _array;
private int _maximumLength;
private (int, int) _currentLength;
private (int, int) _pair;
private int _index;
public int Result => _maximumLength;
public SolutionAsObjectOriented(int[] array)
{
_array = array;
_maximumLength = 0;
InitializeCurrentLengthOfAPair();
InitializeIndexToStartWithFirstElementOfPair();
}
private void InitializeCurrentLengthOfAPair() => _currentLength = (1, 1);
private void InitializeIndexToStartWithFirstElementOfPair() => _index = 1;
public void Calculate()
{
if (! InputArrayHasMinimumLength())
{
return;
}
ExtractFirstPairOfValues();
while (ThereAreValuesToProcess())
{
if (CurrentValueBelongsToPair())
{
AddOneToCurrentLength();
}
else
{
if (PairMembersAreIdentical())
{
AddCurrentValueToPair();
AccumulateIntoSinglePair();
ShiftCurrentLengthOfAPair();
}
else
{
UpdateMaximumValueWhenNecessary();
AddCurrentValueShiftingValuesInPair();
ShiftCurrentLengthOfAPair();
}
}
}
UpdateMaximumValueWhenNecessary();
}
private bool InputArrayHasMinimumLength() => _array.Length >= 2;
private void ExtractFirstPairOfValues() => _pair = (_array[0], _array[1]);
private bool ThereAreValuesToProcess() => ++_index < _array.Length;
private bool CurrentValueBelongsToPair() => _pair.Item1 == _array[_index] || _pair.Item2 == _array[_index];
private void AddOneToCurrentLength()
{
if (_array[_index] == _pair.Item1)
{
_currentLength.Item1++;
}
else
{
_currentLength.Item2++;
}
}
private bool PairMembersAreIdentical() => _pair.Item1 == _pair.Item2;
private void AddCurrentValueToPair() => _pair = (_pair.Item2, _array[_index]);
private void AccumulateIntoSinglePair() => _currentLength.Item1 += _currentLength.Item2;
public int UpdateMaximumValueWhenNecessary() =>
_maximumLength = _currentLength.Item1 + _currentLength.Item2 > _maximumLength
? _currentLength.Item1 + _currentLength.Item2
: _maximumLength;
private void AddCurrentValueShiftingValuesInPair() => _pair = (_array[_index - 1], _array[_index]);
private void ShiftCurrentLengthOfAPair() => _currentLength = (_currentLength.Item2, 1);
}
}