// TopCoder TCO 2013 Round 1A Div 1, L1 HouseBuilding
import java.util.*;
import java.math.*;
class TopcoderSolution {
public static void main(String[] args) {
HouseBuilding obj = new HouseBuilding();
System.out.println(
// obj.numPairs(numbers)
obj.getMinimum(new String[]
{ "10",
"31" })
);
}
}
// change to public before submit
public class HouseBuilding {
public int getMinimum(String[] area) {
int minEffort = Integer.MAX_VALUE;
for (int lev = 0; lev <= 9; lev++) {
int effort = 0;
for (int i = 0; i < area.length; i++) {
for (int j = 0; j < area[i].length(); j++) {
int curlev = area[i].charAt(j) - '0';
int diff = Math.min(Math.abs(curlev - lev),
Math.abs(curlev - (lev + 1)));
if (diff > 0) {
int add = Math.min(Math.abs(curlev - lev),
Math.abs(curlev - (lev + 1)));
effort += add;
}
}
}
minEffort = Math.min(minEffort, effort);
}
return minEffort;
}
}
No comments:
Post a Comment