public static int Answer(string[,] machineToBeFixed, int numOfConsecutiveMachines) { int m = machineToBeFixed.GetLength(1); if (m < numOfConsecutiveMachines) { return(0); } else { int result = int.MaxValue; int n = machineToBeFixed.GetLength(0); for (int i = 0; i < n; i++) { int lastX = -1; int GODNESS = 0; bool pred = false; for (int j = 0; j < m; j++) { if (machineToBeFixed[i, j][0] == 'X') { lastX = j; GODNESS = 0; pred = false; if (m - j - 1 < numOfConsecutiveMachines) { break; } } else { if (j - lastX == numOfConsecutiveMachines) { if (pred) { GODNESS += Question4.Parse(machineToBeFixed[i, j]); } else { for (int kk = 0; kk < numOfConsecutiveMachines; ++kk) { GODNESS += Question4.Parse(machineToBeFixed[i, j - kk]); } pred = true; } if (GODNESS < result) { result = GODNESS; } lastX += 1; GODNESS -= Question4.Parse(machineToBeFixed[i, lastX]); } } } } return((result == int.MaxValue) ? 0 : result); } }
public static int Answer(string[,] machineToBeFixed, int numOfConsecutiveMachines) { //TODO: Please work out the solution; int nRows = machineToBeFixed.GetLength(0); int lenRow = machineToBeFixed.GetLength(1); int answer = 1000000; if (numOfConsecutiveMachines > lenRow) { return(0); } for (int i = 0; i < nRows; i++) { int counter = 0; int sumTmp = 0; for (int j = 0; j < lenRow; j++) { if (machineToBeFixed[i, j] == "X") { counter = 0; sumTmp = 0; if (numOfConsecutiveMachines >= lenRow - j) { break; } } else { int elt = Question4.Parse(machineToBeFixed[i, j]); counter += 1; sumTmp += elt; if (counter == numOfConsecutiveMachines + 1) { sumTmp -= Question4.Parse(machineToBeFixed[i, j - numOfConsecutiveMachines]); counter -= 1; } if (counter == numOfConsecutiveMachines) { if (sumTmp < answer) { answer = sumTmp; } } } } } if (answer == 1000000) { return(0); } return(answer); }