Skip to content

Instantly share code, notes, and snippets.

@midorikocak
Created November 17, 2017 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save midorikocak/76f6dddca41c85d4b0fb62a6c4839813 to your computer and use it in GitHub Desktop.
Save midorikocak/76f6dddca41c85d4b0fb62a6c4839813 to your computer and use it in GitHub Desktop.
Amazon Practice 1
// 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
}
@ashu-22
Copy link

ashu-22 commented Jan 28, 2019

Is the following code is being run at the running or compiling mode!

@rafael-as-martins
Copy link

rafael-as-martins commented Jan 12, 2020

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);

}

}

@oneclose-ankitabiswas
Copy link

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