forked from RyanFehr/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.cs
62 lines (55 loc) · 2.2 KB
/
Solution.cs
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
/*
Problem: https://www.hackerrank.com/challenges/breaking-best-and-worst-records/problem
C# Language Version: 6.0
.Net Framework Version: 4.7
Tool Version : Visual Studio Community 2017
Thoughts :
1. Let the count of minimum and maximum records broken be cmin and cmax. Initially, set cmin and cmax to 0.
2. If total number of games is 1 then jump to step 4.
3. If total number of games is greater than 1 then a processing is reequired as below:
3.1 Let the mininum and maximum score record be gameMin and gameMax. Set gameMin and gameMax to score of first game.
3.2 Start iterating the scores starting from second game onwards:
3.2.1 If score of current game is less than gameMin then increment cmin by 1 and set gameMin to current game score.
3.2.2 If score of current game is greater than gameMax then increment cmax by 1 and set gameMax to current game score.
3.3.3 Process all the game scores by following steps 3.2.1. through 3.2.2.
4. Print cmin and cmax on same line separated by a space.
Time Complexity: O(n)
Space Complexity: O(1)
*/
using System;
class Solution
{
static int[] getRecord(int[] gameScores)
{
var minRecordBroken = 0;
var maxRecordBroken = 0;
if (gameScores.Length > 1)
{
var minRecord = gameScores[0];
var maxRecord = gameScores[0];
for (int i = 1; i < gameScores.Length; i++)
{
if (gameScores[i] < minRecord)
{
minRecord = gameScores[i];
minRecordBroken++;
}
if (gameScores[i] > maxRecord)
{
maxRecord = gameScores[i];
maxRecordBroken++;
}
}
}
return new int[] { maxRecordBroken, minRecordBroken };
}
static void Main(String[] args)
{
//No need to capture number of games as I use array's length property to get it.
Console.ReadLine();
var s_temp = Console.ReadLine().Split(' ');
var gameScores = Array.ConvertAll(s_temp, Int32.Parse);
var result = getRecord(gameScores);
Console.WriteLine(String.Join(" ", result));
}
}