Skip to main content

Leetcode 1603

· 2 min read
Junhe Chen

Easy question , you can use three int variable or array is faster and easier.

class ParkingSystem {
int[] lot;
public ParkingSystem(int big, int medium, int small) {
lot = new int[]{big,medium,small};
}

public boolean addCar(int carType) {
if(lot[carType - 1] == 0) return false;
lot[carType - 1] --;
return true;
}
}

348

class TicTacToe {
int[] rows,cols;
int diagonal,antiDiagonal;

public TicTacToe(int n) {
rows = new int[n];
cols = new int[n];
diagonal = 0;
antiDiagonal = 0;
}

public int move(int row, int col, int player) {
int n = rows.length;
// determine the mark by player
int mark = (player == 1) ? 1 : -1;
// mark the row and col
rows[row] += mark;
cols[col] += mark;
// mark the diagonal or antiDiagonal if applies
if(row == col) diagonal += mark;
if(col == (n - row - 1)) antiDiagonal += mark;
// if any row or col scores n that means our new mark connected a row col diagonal or antiDiagonal;
if(Math.abs(rows[row]) == n|| Math.abs(cols[col]) == n || Math.abs(diagonal) == n || Math.abs(antiDiagonal) == n) return player;
// no winer
return 0;
}
}

/**
* Your TicTacToe object will be instantiated and called as such:
* TicTacToe obj = new TicTacToe(n);
* int param_1 = obj.move(row,col,player);
*/