Created
November 28, 2016 20:20
-
-
Save jianminchen/c4f1c84c984e58fdcdc467e6f28d84e3 to your computer and use it in GitHub Desktop.
Bear and Steady Gene - study code - HackerRank - Nov. 28, 2016 - the code is very simple, try to figure out the idea.
This file contains 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; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace BearAndSteadyGene2 | |
{ | |
class BearAndSteadyGene2 | |
{ | |
/* | |
* Nov. 28, 2016 | |
* | |
*/ | |
static void Main(string[] args) | |
{ | |
int number = practice(8, "GAAATAAA"); | |
} | |
/* | |
* Nov. 28, 2016 | |
* | |
*/ | |
private static int practice(int n, string input) | |
{ | |
int[] a = new int[1007]; | |
for (int i = 0; i < n; i++) | |
{ | |
a[input[i]]++; | |
} | |
int ans = Int32.MaxValue; | |
int index = 0; | |
for (int i = 0; i < n; i++) | |
{ | |
a[input[i]]--; | |
while (valid(n, a) && index <= i) | |
{ | |
ans = Math.Min(ans, i - index + 1); | |
a[input[index]]++; | |
index++; | |
} | |
} | |
return ans; | |
} | |
private static bool valid(int n, int[] a) | |
{ | |
int A, G, T, C; | |
A = a['A']; | |
G = a['G']; | |
T = a['T']; | |
C = a['C']; | |
if (A <= n / 4 && G <= n / 4 && T <= n / 4 && C <= n / 4) return true; | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment