---
# System prepended metadata

title: 'Week 1: June 1st - June 7th'
tags: [pixnet, june, leetcode, '2021']

---

#  Week 1: June 1st - June 7th

## Max Area of Island

### ==Solution:==
- Shan
```javascript
/**
 * @param {number[][]} grid
 * @return {number}
 */
var maxAreaOfIsland = function(grid) {
    let area = 0;
    let row = grid.length;
    let column = grid[0].length;

    for (let i = 0; i < row; i++) {
      for (let j = 0; j < column; j++) {
        if (grid[i][j] == 1) { // island!
          area = Math.max(area, islandAround(i, j));
        }
      }
    }

    // 回傳 area
    function islandAround(i, j) {
      // 終止條件
      if (i < 0 || j < 0 || i >= row || j >= column || !grid[i][j] ) {
        return 0;
      }
      // 將目前 (i,j) 位置標為0, 代表已算過
      grid[i][j] = 0;
      // area +1, 繼續執行上下左右位置
      return 1 + islandAround(i + 1, j) + islandAround(i - 1, j) + islandAround(i, j + 1) + islandAround(i, j - 1);

    }
    return area;
};
```
:::info
遞迴方式去計算島嶼四個方向是否有值
(i, j) :arrow_right:  `(i-1,j)`, `(i+1, j)`, `(i, j-1)`, `(i, j+1)`
:::








###### tags: `leetcode` `pixnet` `june` `2021`
