Created
November 11, 2015 13:39
-
-
Save sholfen/7c8ce5b39bd2f9bb78c7 to your computer and use it in GitHub Desktop.
for Cordiality MaxCounters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace MaxCounters | |
{ | |
class MainClass | |
{ | |
public static void Main (string[] args) | |
{ | |
int[] A = { 3, 4, 4, 6, 1, 4, 4 }; | |
int n = 5; | |
Solution sol = new Solution (); | |
foreach (var item in sol.solution(n,A)) { | |
Console.WriteLine (item); | |
} | |
} | |
} | |
class Solution { | |
public int[] solution(int n, int[] input) { | |
int[] result = new int[n]; | |
int max_num = 0; | |
int lower_bound = 0; | |
for (int i = 0; i < input.Length; i++) { | |
int index = input [i]; | |
if (1 <= index && index <= n) { | |
if (result [index - 1] < lower_bound) { | |
result [index - 1] = lower_bound + 1; | |
} else { | |
result [index - 1] += 1; | |
} | |
if (result [index - 1] > max_num) { | |
max_num = result [index - 1]; | |
} | |
} else if (input [i] == n + 1) { | |
lower_bound = max_num; | |
} | |
} | |
for (int i = 0; i < result.Length; i++) { | |
if (result [i] < lower_bound) { | |
result [i] = lower_bound; | |
} | |
} | |
return result; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment