Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created March 6, 2016 05:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/6c0ee8af6e88964410ab to your computer and use it in GitHub Desktop.
Save jianminchen/6c0ee8af6e88964410ab to your computer and use it in GitHub Desktop.
Bear And Steady Gene - algorithm, two pointers, linear time solution, sliding window
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
public class Solver
{
/*
* Julia worked through the code execution, then, wrote down the steps using test case:
*
* GAAATAAA
*
* var a = [3, 0,0,0,2, 0, 0, 0]
* Target[] = [4, 0, 0, 0]
*
* If all element in Target == 0, then return 0
*
* Left, right two pointer
Start a loop,
Add a[r]’s char into Sum array,
r++, r moves to next one
inside the first loop, another loop for r, continue to move forward if sums not reaches target. – 4 of them >= target
*
inside the first loop, another loop for left pointer – l
condition: l < r
if sums array is bigger than target array, then remove left pointer char count, then, move left pointer to next one
then, the substring is the reaching the target, and compare to existing smallest length
*
Julia's comment: the code is more readable and quick to write
*/
public void Solve()
{
int n = ReadInt();
var a = ReadToken().Select(ch => "ACTG".IndexOf(ch)).ToArray(); // var a = [3, 0,0,0,2, 0, 0, 0]
var target = new int[4];
for (int i = 0; i < 4; i++)
target[i] = Math.Max(0, a.Count(aa => aa == i) - n / 4);
if (target.All(t => t == 0))
{
Write(0);
return;
}
int ans = n;
int l = 0, r = 0;
var sums = new int[4];
while (r < n)
{
sums[a[r]]++;
r++;
while (r < n && !(sums[0] >= target[0] && sums[1] >= target[1] && sums[2] >= target[2] && sums[3] >= target[3]))
{
sums[a[r]]++;
r++;
}
while (l < r)
{
if (!(sums[0] >= target[0] && sums[1] >= target[1] && sums[2] >= target[2] && sums[3] >= target[3]))
break;
sums[a[l]]--;
l++;
}
ans = Math.Min(ans, r - l + 1);
}
Write(ans);
}
#region Main
protected static TextReader reader;
protected static TextWriter writer;
static void Main()
{
reader = new StreamReader(Console.OpenStandardInput());
writer = new StreamWriter(Console.OpenStandardOutput());
//reader = new StreamReader("input.txt");
//writer = new StreamWriter("output.txt");
try
{
//var thread = new Thread(new Solver().Solve, 1024 * 1024 * 128);
//thread.Start();
//thread.Join();
new Solver().Solve();
}
catch (Exception ex)
{
Console.WriteLine(ex);
#if DEBUG
#else
throw;
#endif
}
reader.Close();
writer.Close();
}
#endregion
#region Read / Write
private static Queue<string> currentLineTokens = new Queue<string>();
private static string[] ReadAndSplitLine() {
return reader.ReadLine().Split(new[] { ' ', '\t', },
StringSplitOptions.RemoveEmptyEntries);
}
public static string ReadToken() {
while (currentLineTokens.Count == 0)
currentLineTokens = new Queue<string>(ReadAndSplitLine());
return currentLineTokens.Dequeue();
}
public static int ReadInt() {
return int.Parse(ReadToken());
}
public static long ReadLong() { return long.Parse(ReadToken()); }
public static double ReadDouble() { return double.Parse(ReadToken(), CultureInfo.InvariantCulture); }
public static int[] ReadIntArray() { return ReadAndSplitLine().Select(int.Parse).ToArray(); }
public static long[] ReadLongArray() { return ReadAndSplitLine().Select(long.Parse).ToArray(); }
public static double[] ReadDoubleArray() { return ReadAndSplitLine().Select(s => double.Parse(s, CultureInfo.InvariantCulture)).ToArray(); }
public static int[][] ReadIntMatrix(int numberOfRows) { int[][] matrix = new int[numberOfRows][]; for (int i = 0; i < numberOfRows; i++)matrix[i] = ReadIntArray(); return matrix; }
public static int[][] ReadAndTransposeIntMatrix(int numberOfRows)
{
int[][] matrix = ReadIntMatrix(numberOfRows); int[][] ret = new int[matrix[0].Length][];
for (int i = 0; i < ret.Length; i++) { ret[i] = new int[numberOfRows]; for (int j = 0; j < numberOfRows; j++)ret[i][j] = matrix[j][i]; } return ret;
}
public static string[] ReadLines(int quantity) { string[] lines = new string[quantity]; for (int i = 0; i < quantity; i++)lines[i] = reader.ReadLine().Trim(); return lines; }
public static void WriteArray<T>(IEnumerable<T> array) { writer.WriteLine(string.Join(" ", array)); }
public static void Write(params object[] array) { WriteArray(array); }
public static void WriteLines<T>(IEnumerable<T> array) { foreach (var a in array)writer.WriteLine(a); }
private class SDictionary<TKey, TValue> : Dictionary<TKey, TValue>
{
public new TValue this[TKey key]
{
get { return ContainsKey(key) ? base[key] : default(TValue); }
set { base[key] = value; }
}
}
private static T[] Init<T>(int size) where T : new() { var ret = new T[size]; for (int i = 0; i < size; i++)ret[i] = new T(); return ret; }
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment