Untitled
unknown
plain_text
15 days ago
1.4 kB
3
Indexable
Never
int[] solution(char[][] field) { int rows = field.length; int cols = field[0].length; int minMoves = 0; int maxMoves = 0; boolean[] columnFilled = new boolean[rows]; for (int r = 0; r < rows; r++) { for (int c = 0; c < cols; c++) { if (field[r][c] == '#') { fillFirstColumn(field, columnFilled, r); minMoves++; break; } } } for (int r = 0; r < rows; r++) { if (!columnFilled[r]) { int remainingSpots = 1; while (remainingSpots > 0) { fillFirstColumn(field, new boolean[rows], r); maxMoves++; remainingSpots--; } } } return new int[]{minMoves, maxMoves}; } public static void fillFirstColumn(char[][] field, boolean[] filled, int startRow) { int rows = field.length; for (int r = startRow; r >= 0 && !filled[r]; r--) { if (field[r][0] == '.') { filled[r] = true; break; } } for (int r = startRow; r < rows && !filled[r]; r++) { if (field[r][0] == '.') { filled[r] = true; break; } } }
Leave a Comment