Created
November 17, 2017 10:10
-
-
Save midorikocak/76f6dddca41c85d4b0fb62a6c4839813 to your computer and use it in GitHub Desktop.
Amazon Practice 1
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
// IMPORT LIBRARY PACKAGES NEEDED BY YOUR PROGRAM | |
import java.util.*; | |
import java.util.Arrays; | |
import java.util.List; | |
// SOME CLASSES WITHIN A PACKAGE MAY BE RESTRICTED | |
// DEFINE ANY CLASS AND METHOD NEEDED | |
// CLASS BEGINS, THIS CLASS IS REQUIRED | |
public class Solution | |
{ | |
// METHOD SIGNATURE BEGINS, THIS METHOD IS REQUIRED | |
public List<Integer> cellCompete(int[] states, int days) | |
{ | |
for(int j=0; j<days; j++){ | |
int[] nextDay = new int[states.length]; | |
for(int i=0; i<states.length; i++){ | |
int next; | |
int previous; | |
if(i==0){ | |
previous = 0; | |
} | |
else{ | |
previous = states[i-1]; | |
} | |
if(i==states.length-1){ | |
next = 0; | |
} | |
else{ | |
next = states[i+1]; | |
} | |
if(previous==next){ | |
nextDay[i] = 0; | |
} | |
else{ | |
nextDay[i] = 1; | |
} | |
} | |
states = nextDay; | |
} | |
return this.toList(states); | |
} | |
public List<Integer> toList(int[] ints) | |
{ | |
List<Integer> intList = new ArrayList<Integer>(); | |
for (int index = 0; index < ints.length; index++) | |
{ | |
intList.add(ints[index]); | |
} | |
return intList; | |
} | |
// METHOD SIGNATURE ENDS | |
} |
I made the following implementation while performing the demo test:
package main.java.example;
import java.util.ArrayList;
import java.util.List;
public class Example {
public static List<Integer> cellCompete(int[] states, int days) {
List<Integer> competition = new ArrayList<>();
int next;
int previous;
for (int i = 0; i < days; i++) {
previous = 0;
next = 0;
competition.clear();
for (int j = 0; j < states.length; j++) {
next = (states.length - 1 == j) ? 0 : states[j + 1];
if (previous != next) {
competition.add(1);
} else {
competition.add(0);
}
previous = states[j];
}
states = competition.stream().mapToInt(x -> x).toArray();
}
return competition;
}
public static void main(String[] args) {
int[] input = { 1, 1, 1, 0, 1, 1, 1, 1 };
cellCompete(input, 2);
}
}
Is the following code is being run at the running or compiling mode!
yes. You have to importthe class though
import java.util.ArrayList;
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is the following code is being run at the running or compiling mode!