Exam

 avatar
unknown
java
4 years ago
997 B
6
Indexable
class Solution {
    public int solution(int N, String S) {
        // write your code in Java SE 8
        if(S.length() == 0){
            return 2*N;
        }

        int numFamily = 0;
        int[][] seatArray = new int[N][10];

        for(int[] arr : seatArray){
            Arrays.fill(arr, 0);
        }

        String[] seats = S.split(" ");
        
        for(String str : seats){
            int row = str.charAt(0) - '1';
            int col = str.charAt(1) - 'A';
            seatArray[row][col] = 1;
        }

        for(int i = 0; i < N; i++){
            int counter = 0;
            for(int j = 1; j < 9; j++){
               	if (seatArray[i][j] == 1) {
					counter = 0;
				}else{
                    counter++;
                }
				
				//family have to sit together
				if (j == 2 && counter == 0) {
					continue;
				}
				
				if (counter == 4) {
					numFamily++;
                    counter = 0;
				}
            }
        }

        return numFamily;

    }
}
Editor is loading...