aboutsummaryrefslogtreecommitdiff
path: root/challenge-343/deadmarshal/java/Ch2.java
blob: fe4b609d79f013313a923016cccb646c256ada45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.util.Map;
import java.util.HashMap;

public class Ch2 {
  public void main(String[] args) {
    System.out.println(
        champion_team(new int[][] { { 0, 1, 1 }, { 0, 0, 1 }, { 0, 0, 0 } }));
    System.out.println(champion_team(new int[][] { { 0, 1, 0, 0 },
        { 0, 0, 0, 0 }, { 1, 1, 0, 0 }, { 1, 1, 1, 0 } }));
    System.out.println(champion_team(new int[][] { { 0, 1, 0, 1 },
        { 0, 0, 1, 1 }, { 1, 0, 0, 0 }, { 0, 0, 1, 0 } }));
    System.out.println(
        champion_team(new int[][] { { 0, 1, 1 }, { 0, 0, 0 }, { 0, 1, 0 } }));
    System.out.println(
        champion_team(new int[][] { { 0, 0, 0, 0, 0 }, { 1, 0, 0, 0, 0 },
            { 1, 1, 0, 1, 1 }, { 1, 1, 0, 0, 0 }, { 1, 1, 0, 1, 0 } }));
  }

  private static int champion_team(int[][] grid) {
    int m = 0;
    Map<Integer,Integer> h = new HashMap<>();
    for(int y = 0; y < grid.length; ++y) {
      for(int x = 0; x < grid[y].length; ++x) {
	int w = grid[y][x] == 1 ? y : x;
	h.put(w,h.getOrDefault(w,0) + 1);
	if(h.get(w) > h.getOrDefault(m,0)) m = w;
      }
    }
    return m;
  }
}