@Test public void onePointIsOneLine() throws Exception { Solution solution = new Solution(); int maxPoints = solution.maxPoints(new Point[]{ new Point(5, 5) }); assertThat(maxPoints, is(1)); }
public int maxPoints(Point[] points) { if (points.length == 0) { return 0; } int max = 1; for (int i = 0; i < points.length; i++) { Point a = points[i]; List<Segment> segmentsStartWithA = new ArrayList<>(); for (int j = i + 1; j < points.length; j++) { Point b = points[j]; Segment seg = new Segment(a, b); segmentsStartWithA.add(seg); } Map<Angle, List<Segment>> sameAngleSegGroups = segmentsStartWithA.stream() .collect(groupingBy(Segment::getAngle)); final int samePointSegCount; if (sameAngleSegGroups.containsKey(Same.instance())) { samePointSegCount = sameAngleSegGroups.get(Same.instance()).size(); } else { samePointSegCount = 0; } Optional<Integer> maxNumOptional = sameAngleSegGroups.values().stream().map(group -> { if (group.get(0).getAngle() instanceof Same) { return samePointSegCount + 1; } return group.size() + 1 + samePointSegCount; }).max((i1, i2) -> i1 - i2); if (maxNumOptional.isPresent()) { int maxNum = maxNumOptional.get(); if (maxNum > max) { max = maxNum; } } } return max; }
Solution { public int maxPoints(Point[] points) { if (points.length == 0) { return 0; } int max = 1; for (int i = 0; i < points.length; i++) { Point a = points[i]; List<Segment> segmentsStartWithA = new ArrayList<>(); for (int j = i + 1; j < points.length; j++) { Point b = points[j]; Segment seg = new Segment(a, b); segmentsStartWithA.add(seg); } Map<Angle, List<Segment>> sameAngleSegGroups = segmentsStartWithA.stream() .collect(groupingBy(Segment::getAngle)); final int samePointSegCount; if (sameAngleSegGroups.containsKey(Same.instance())) { samePointSegCount = sameAngleSegGroups.get(Same.instance()).size(); } else { samePointSegCount = 0; } Optional<Integer> maxNumOptional = sameAngleSegGroups.values().stream().map(group -> { if (group.get(0).getAngle() instanceof Same) { return samePointSegCount + 1; } return group.size() + 1 + samePointSegCount; }).max((i1, i2) -> i1 - i2); if (maxNumOptional.isPresent()) { int maxNum = maxNumOptional.get(); if (maxNum > max) { max = maxNum; } } } return max; } }
Solution { public int maxPoints(Point[] points) { if (points.length == 0) { return 0; } int max = 1; for (int i = 0; i < points.length; i++) { Point a = points[i]; List<Segment> segmentsStartWithA = new ArrayList<>(); for (int j = i + 1; j < points.length; j++) { Point b = points[j]; Segment seg = new Segment(a, b); segmentsStartWithA.add(seg); } Map<Angle, List<Segment>> sameAngleSegGroups = segmentsStartWithA.stream() .collect(groupingBy(Segment::getAngle)); final int samePointSegCount; if (sameAngleSegGroups.containsKey(Same.instance())) { samePointSegCount = sameAngleSegGroups.get(Same.instance()).size(); } else { samePointSegCount = 0; } Optional<Integer> maxNumOptional = sameAngleSegGroups.values().stream().map(group -> { if (group.get(0).getAngle() instanceof Same) { return samePointSegCount + 1; } return group.size() + 1 + samePointSegCount; }).max((i1, i2) -> i1 - i2); if (maxNumOptional.isPresent()) { int maxNum = maxNumOptional.get(); if (maxNum > max) { max = maxNum; } } } return max; }  }
Solution { public int maxPoints(Point[] points) { if (points.length == 0) { return 0; } int max = 1; for (int i = 0; i < points.length; i++) { Point a = points[i]; List<Segment> segmentsStartWithA = new ArrayList<>(); for (int j = i + 1; j < points.length; j++) { Point b = points[j]; Segment seg = new Segment(a, b); segmentsStartWithA.add(seg); } Map<Angle, List<Segment>> sameAngleSegGroups = segmentsStartWithA.stream() .collect(groupingBy(Segment::getAngle)); final int samePointSegCount; if (sameAngleSegGroups.containsKey(Same.instance())) { samePointSegCount = sameAngleSegGroups.get(Same.instance()).size(); } else { samePointSegCount = 0; } Optional<Integer> maxNumOptional = sameAngleSegGroups.values().stream().map(group -> { if (group.get(0).getAngle() instanceof Same) { return samePointSegCount + 1; } return group.size() + 1 + samePointSegCount; }).max((i1, i2) -> i1 - i2); if (maxNumOptional.isPresent()) { int maxNum = maxNumOptional.get(); if (maxNum > max) { max = maxNum; } } } return max; }  int maxPoints(Point[] points); }
Solution { public int maxPoints(Point[] points) { if (points.length == 0) { return 0; } int max = 1; for (int i = 0; i < points.length; i++) { Point a = points[i]; List<Segment> segmentsStartWithA = new ArrayList<>(); for (int j = i + 1; j < points.length; j++) { Point b = points[j]; Segment seg = new Segment(a, b); segmentsStartWithA.add(seg); } Map<Angle, List<Segment>> sameAngleSegGroups = segmentsStartWithA.stream() .collect(groupingBy(Segment::getAngle)); final int samePointSegCount; if (sameAngleSegGroups.containsKey(Same.instance())) { samePointSegCount = sameAngleSegGroups.get(Same.instance()).size(); } else { samePointSegCount = 0; } Optional<Integer> maxNumOptional = sameAngleSegGroups.values().stream().map(group -> { if (group.get(0).getAngle() instanceof Same) { return samePointSegCount + 1; } return group.size() + 1 + samePointSegCount; }).max((i1, i2) -> i1 - i2); if (maxNumOptional.isPresent()) { int maxNum = maxNumOptional.get(); if (maxNum > max) { max = maxNum; } } } return max; }  int maxPoints(Point[] points);  }