Skip to content

Commit ae2cd23

Browse files
committed
algo
1 parent 76c1bb7 commit ae2cd23

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed

SandBox.cs

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
using System.Text;
2+
3+
namespace CSharpLibraries
4+
{
5+
public class SandBox
6+
{
7+
public void run()
8+
{
9+
Console.WriteLine("hello");
10+
var str = "Ala ma kota. Sentence with a-dash";
11+
var reversed = Reverse(str);
12+
Console.WriteLine(reversed);
13+
14+
var A = new[] { 0, 5, 5, 5, -22 };
15+
Console.WriteLine(Solve(A));
16+
17+
var input1 = new List<DateTime>() {
18+
new DateTime(2016, 6,20),
19+
new DateTime(2016, 6,21),
20+
new DateTime(2016, 6,22),
21+
new DateTime(2016, 6,25),
22+
new DateTime(2016, 6,26),
23+
};
24+
var res = Dates(input1);
25+
foreach (var d in res)
26+
{
27+
Console.WriteLine(d.Item1);
28+
Console.WriteLine(d.Item2);
29+
Console.WriteLine("sep");
30+
}
31+
32+
var input2 = new[] { 1, 1, 0, 1, 0, 0, 1, 1 };
33+
var res2 = Coins(input2);
34+
Console.WriteLine(res2);
35+
}
36+
37+
public string Reverse(string str)
38+
{
39+
var result = new StringBuilder();
40+
var sentences = str.Split('.');
41+
foreach (var sentence in sentences)
42+
{
43+
var words = sentence.Split(' ');
44+
var reversedWords = new string[words.Length];
45+
for (var i = 0; i < words.Length; i++)
46+
{
47+
var li = words.Length - i - 1;
48+
reversedWords[li] = words[i];
49+
}
50+
var reversedSentence = string.Join(' ', reversedWords);
51+
result.Append(reversedSentence + ". ");
52+
}
53+
return result.ToString();
54+
}
55+
56+
public int Solve(int[] array)
57+
{
58+
var unique = new HashSet<int>();
59+
foreach (var val in array)
60+
{
61+
unique.Add(val);
62+
}
63+
return unique.Count;
64+
}
65+
66+
public List<Tuple<DateTime, DateTime>> Dates(List<DateTime> array)
67+
{
68+
array.Sort();
69+
List<Tuple<DateTime, DateTime>> result = new();
70+
for (var i = 0; i < array.Count; i += 2)
71+
{
72+
var secondIndex = i + 1;
73+
if (secondIndex >= array.Count)
74+
{
75+
secondIndex = i;
76+
}
77+
result.Add(Tuple.Create(array[i], array[secondIndex]));
78+
}
79+
return result;
80+
}
81+
82+
public int Coins(int[] array)
83+
{
84+
List<int> lookUp = new();
85+
86+
int currentUp = -1;
87+
foreach (var val in array)
88+
{
89+
if (val != currentUp)
90+
{
91+
lookUp.Add(1);
92+
}
93+
else if (val == currentUp)
94+
{
95+
++lookUp[lookUp.Count - 1];
96+
}
97+
currentUp = val;
98+
}
99+
if (lookUp.Count == 1)
100+
{
101+
return lookUp[0];
102+
}
103+
if (lookUp.Count == 2)
104+
{
105+
return lookUp[0] > lookUp[1] ? lookUp[0]++ : lookUp[1]++;
106+
}
107+
var max = 0;
108+
var maxAlone = 0;
109+
for (int i = 1; i < lookUp.Count - 1; i++)
110+
{
111+
var prev = lookUp[i - 1];
112+
var curr = lookUp[i];
113+
var next = lookUp[i + 1];
114+
if (curr == 1)
115+
{
116+
var currentValue = prev + curr + next;
117+
if (currentValue > max)
118+
{
119+
max = currentValue;
120+
}
121+
}
122+
if (prev > maxAlone) maxAlone = prev;
123+
if (curr > maxAlone) maxAlone = curr;
124+
if (next > maxAlone) maxAlone = next;
125+
}
126+
if (max == 0)
127+
{
128+
return maxAlone++;
129+
}
130+
return max;
131+
}
132+
}
133+
}

0 commit comments

Comments
 (0)