public static void main
(String[] args
) {
System.
out.
println(cellCompete2
(new int[]{1,
1,
1,
0,
1,
1,
1,
1},
2));
System.
out.
println(cellCompete
(new int[]{1,
1,
1,
0,
1,
1,
1,
1},
2));
}
public static List<Integer> cellCompete(int[] states, int days) {
int edge = 0;
int counter = 1;
int[] temp = new int[states.length];
while (counter <= days) {
for (int i = 0; i <= states.length - 1; i++) {
if (i == 0) {
temp[i] = states[i + 1] == edge ? 0 : 1;
} else if (i == states.length - 1) {
temp[i] = states[i - 1] == edge ? 0 : 1;
} else {
temp[i] = states[i - 1] == states[i + 1] ? 0 : 1;
}
}
counter++;
System.
arraycopy(temp,
0, states,
0, states.
length);
}
List
<Integer
> list
= Arrays.
stream(states
).
boxed().
collect(Collectors.
toList());
return list;
}
public static List<Integer> cellCompete2(int[] states, int days) {
int temp = 0;
int[] newStates = new int[states.length];
while (days > 0) {
for (int i = 1; i <= states.length - 1; i++) {
if (temp == i) {
newStates[i-1] = states[i - 1] == 0 ? 1 : 0;
}
temp = states[i];
}
days--;
System.
arraycopy(newStates,
0, states,
0, states.
length);
}
List
<Integer
> list
= Arrays.
stream(states
).
boxed().
collect(Collectors.
toList());
return list;
}
{"html5":"htmlmixed","css":"css","javascript":"javascript","php":"php","python":"python","ruby":"ruby","lua":"text\/x-lua","bash":"text\/x-sh","go":"go","c":"text\/x-csrc","cpp":"text\/x-c++src","diff":"diff","latex":"stex","sql":"sql","xml":"xml","apl":"apl","asterisk":"asterisk","c_loadrunner":"text\/x-csrc","c_mac":"text\/x-csrc","coffeescript":"text\/x-coffeescript","csharp":"text\/x-csharp","d":"d","ecmascript":"javascript","erlang":"erlang","groovy":"text\/x-groovy","haskell":"text\/x-haskell","haxe":"text\/x-haxe","html4strict":"htmlmixed","java":"text\/x-java","java5":"text\/x-java","jquery":"javascript","mirc":"mirc","mysql":"sql","ocaml":"text\/x-ocaml","pascal":"text\/x-pascal","perl":"perl","perl6":"perl","plsql":"sql","properties":"text\/x-properties","q":"text\/x-q","scala":"scala","scheme":"text\/x-scheme","tcl":"text\/x-tcl","vb":"text\/x-vb","verilog":"text\/x-verilog","yaml":"text\/x-yaml","z80":"text\/x-z80"}