question




Comment hidden because of low score. Click to expand.
0
of 0 vote

This is how I would do it :

Look at it like a tree. Iterate through each element as if it is the one being chosen. Keep a boolean array to check if it is visited or not. If an element is chosen and both it's neighbors have been visited, just iterate to find either the closest left or right one (Go in Order). In case one is, visit the other. Simply do this till you hit a node whose neighbors are visited and there are no elements left [I do this with a loop every time, but you can simply keep a count of the elements too].

public class Ruffians {

	public static void main(String[] args) {
		System.out.println(houseArray(new int[]{20, 50, 50, 1, 5})); 
	}
	
	private static int houseArray(int[] houseArray) {
		return calculateBestSteal(houseArray);
	}
	
	private static int calculateBestSteal(int[] houseArray) {
		int bestSteal = 0;
		for(int startHouse = 0; startHouse < houseArray.length; startHouse++) { 
			int stealWithThisStart = stealWithStart(houseArray, startHouse); 
			bestSteal = (bestSteal < stealWithThisStart) ? stealWithThisStart : bestSteal; 
			if( stealWithThisStart > bestSteal) {
				bestSteal = stealWithThisStart;
			}
		}
        return bestSteal;		
	}
	
	private static int stealWithStart(int[] houseArray, int currentHouse) {
		boolean[] visited = new boolean[houseArray.length];
		for(int i = 0; i < visited.length; i++) {
			visited[i] = true;
		}
		boolean anyHouseLeft = true;
		int thisSteal = 0;
		while(anyHouseLeft) { 
			visited[currentHouse] = false;
			thisSteal += houseArray[currentHouse];
			if(currentHouse == 0) {
				visited[currentHouse + 1] = false;
			} else if(currentHouse == houseArray.length - 1) {
				visited[currentHouse - 1] = false; 
			} else {
				visited[currentHouse - 1] = false;
				visited[currentHouse + 1] = false;
			}			
			int nextHouse = nextHouseToChoose(currentHouse, visited);
			anyHouseLeft = (nextHouse == -1) ? false : true;
			currentHouse = nextHouse;
		}
		
		return thisSteal;
	}
	
	private static int nextHouseToChoose(int currentHouse, boolean[] visited) { 
		if(currentHouse == 0) {
			int firstTrue = -1;
			for(int visVal = 0; visVal < visited.length; visVal++) {
				if(visited[visVal]) {
					firstTrue = visVal;
				}
			}
			return firstTrue;			
		} else if(currentHouse == visited.length - 1) {
			int lastTrue = -1;
			for(int visVal = visited.length - 1; visVal >= 0; visVal--) {
				if(visited[visVal]) {
					lastTrue = visVal;
				}
			}
			return lastTrue;
		} else { 
			if(visited[currentHouse - 1]) {
				return currentHouse - 1;
			} else if(visited[currentHouse + 1]) {
				return currentHouse + 1;
			} else { 
				for(int valOnLeft = currentHouse; valOnLeft >= 0; valOnLeft--) {
					if(visited[valOnLeft]) {
						return valOnLeft;
					} 
				}
				
				for(int valOnRight = currentHouse; valOnRight < visited.length; valOnRight++) {
					if(visited[valOnRight]) {
						return valOnRight;
					} 
				}
				
				return -1;
			}
		}
	}
}

- Aditya Tirodkar January 26, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Wrong answer. Try this array and you would know its wrong:
{1,50,20,10,28,27,37,8}

- Parikksit October 21, 2014 | Flag




Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More