Sha256: 8eed74126fb45cada186775e3a121820db4744f98f59bff6bc0683999ab1486f
Contents?: true
Size: 1.09 KB
Versions: 213
Compression:
Stored size: 1.09 KB
Contents
import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; final class Matrix { private final List<List<Integer>> values; Matrix(final List<List<Integer>> values) { this.values = values; } Set<MatrixCoordinate> getSaddlePoints() { final Set<MatrixCoordinate> result = new HashSet<>(); if (values.isEmpty()) { return result; } for (int row = 0; row < values.size(); row++) { for (int column = 0; column < values.get(0).size(); column++) { final int coordinateValue = values.get(row).get(column); if (coordinateValue == getRowMax(row) && coordinateValue == getColumnMin(column)) { result.add(new MatrixCoordinate(row, column)); } } } return result; } private int getRowMax(final int row) { return Collections.max(values.get(row)); } private int getColumnMin(final int column) { return values.stream() .map(row -> row.get(column)) .min(Integer::compareTo) .orElseThrow(() -> new IllegalArgumentException("Column cannot be empty")); } }
Version data entries
213 entries across 213 versions & 1 rubygems