aboutsummaryrefslogtreecommitdiff
path: root/making_large_island.c
blob: 49e6e60d43c2be8c2eb9d842df1e1461f60b3fe5 (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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// 827. Making A Large Island
#include "leetcode.h"

/*
 * you are given an 'm x n' binary matrix 'grid'. you are allowed to change at
 * most one 0 to be 1. return size of the largest island in 'grid' after
 * applying this operation. an island is a 4-directionally connected group of
 * 1's.
 */

enum state { water, land };

struct point {
  int r;
  int c;
};

int explor_island(int r, int c, int id, int **grid, int nrow, int ncol) {
  int land_size = 0, dir[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
  struct point queue[nrow * ncol];
  int front = 0, rear = 0;
  queue[rear++] = (struct point){r, c};
  grid[r][c] = id;
  while (front < rear) {
    struct point p = queue[front++];
    ++land_size;
    for (int i = 0; i < 4; ++i) {
      int dr = p.r + dir[i][0];
      int dc = p.c + dir[i][1];
      if (dr < 0 || dr >= nrow || dc < 0 || dc >= ncol || grid[dr][dc] != land)
        continue;
      grid[dr][dc] = id;
      queue[rear++] = (struct point){dr, dc};
    }
  }
  return land_size;
}

int largestIsland(int **grid, int gridSize, int *gridColSize) {
  int max_island = gridSize * (*gridColSize), id = 2;
  int *islands = (int *)calloc(max_island + 2, sizeof(int));
  for (int i = 0; i < gridSize; ++i)
    for (int j = 0; j < (*gridColSize); ++j)
      if (grid[i][j] == land) {
        islands[id] = explor_island(i, j, id, grid, gridSize, *gridColSize);
        ++id;
      }
  if (id == 2) {
    free(islands);
    return 1;
  }
  if (id == 3) {
    int total = gridSize * (*gridColSize), land_size = islands[2] + 1;
    int num_land = fmin(total, land_size);
    free(islands);
    return num_land;
  }
  int max_size = 1, dir[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
  for (int i = 0; i < gridSize; ++i)
    for (int j = 0; j < (*gridColSize); ++j) {
      if (grid[i][j] != water)
        continue;
      bool vis[id];
      int curr_size = 1;
      memset(vis, 0, sizeof(vis));
      for (int k = 0; k < 4; ++k) {
        int dr = i + dir[k][0];
        int dc = j + dir[k][1];
        if (dr < 0 || dr >= gridSize || dc < 0 || dc >= (*gridColSize) ||
            grid[dr][dc] <= land)
          continue;
        int curr_id = grid[dr][dc];
        if (!vis[curr_id]) {
          curr_size += islands[curr_id];
          vis[curr_id] = true;
        }
      }
      max_size = fmax(max_size, curr_size);
    }
  free(islands);
  return max_size;
}

int main() {
  int g1i[2][2] = {{1, 0}, {0, 1}}, g2i[2][2] = {{1, 1}, {1, 0}},
      g3i[2][2] = {{1, 1}, {1, 1}};
  struct two_d_arr g1, g2, g3;
  two_d_arr_init(&g1, ARRAY_SIZE(g1i), ARRAY_SIZE(g1i[0]), g1i);
  two_d_arr_init(&g2, ARRAY_SIZE(g2i), ARRAY_SIZE(g2i[0]), g2i);
  two_d_arr_init(&g3, ARRAY_SIZE(g3i), ARRAY_SIZE(g3i[0]), g3i);
  printf("%d\n", largestIsland(g1.arr, g1.row_size, g1.col_size)); // expect: 3
  printf("%d\n", largestIsland(g2.arr, g2.row_size, g2.col_size)); // expect: 4
  printf("%d\n", largestIsland(g3.arr, g3.row_size, g3.col_size)); // expect: 4
  two_d_arr_free(&g1);
  two_d_arr_free(&g2);
  two_d_arr_free(&g3);
}