You are viewing a single comment's thread. Return to all comments →
public static String isSwapPossible(int[][] matrix) { int rows = matrix.length; int cols = matrix[0].length; Map<Integer, BigDecimal> rowMap = new HashMap<>(); Map<Integer, BigDecimal> colMap = new HashMap<>(); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { BigDecimal currentRowSum = rowMap.get(i); if (currentRowSum != null) { rowMap.put(i, currentRowSum.add(new BigDecimal(matrix[i][j]))); } else { rowMap.put(i, new BigDecimal(matrix[i][j])); } } } for (int j = 0; j < cols; j++) { for (int i = 0; i < rows; i++) { BigDecimal currentColSum = colMap.get(j); if (currentColSum != null) { colMap.put(j, currentColSum.add(new BigDecimal(matrix[i][j]))); } else { colMap.put(j, new BigDecimal(matrix[i][j])); } } } List<BigDecimal> rowList = new ArrayList<>(rowMap.values()); List<BigDecimal> colList = new ArrayList<>(colMap.values()); Collections.sort(rowList); Collections.sort(colList); if (!rowList.equals(colList)) { return "Impossible"; } return "Possible"; }
Seems like cookies are disabled on this browser, please enable them to open this website
Organizing Containers of Balls
You are viewing a single comment's thread. Return to all comments →