From df8878b79192527f3d8617e09c93227340d03c10 Mon Sep 17 00:00:00 2001 From: gdstzmy <79707886+gdstzmy@users.noreply.github.com> Date: Tue, 20 Feb 2024 18:09:07 +0800 Subject: [PATCH 1/2] =?UTF-8?q?Update=200053.=E6=9C=80=E5=A4=A7=E5=AD=90?= =?UTF-8?q?=E5=BA=8F=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加go语言贪心法 --- problems/0053.最大子序和.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/problems/0053.最大子序和.md b/problems/0053.最大子序和.md index 78c8b382..74ff2ca4 100644 --- a/problems/0053.最大子序和.md +++ b/problems/0053.最大子序和.md @@ -230,7 +230,25 @@ class Solution: ``` ### Go +贪心法 +```go +func maxSubArray(nums []int) int { + max := nums[0] + count := 0 + for i := 0; i < len(nums); i++{ + count += nums[i] + if count > max{ + max = count + } + if count < 0 { + count = 0 + } + } + return max +} +``` +动态规划 ```go func maxSubArray(nums []int) int { maxSum := nums[0] From a45fd00a27d03000eb3b327d94abd007ee0ae305 Mon Sep 17 00:00:00 2001 From: lhp <932606153@qq.com> Date: Fri, 23 Feb 2024 10:54:04 +0800 Subject: [PATCH 2/2] =?UTF-8?q?0827:=E6=9C=80=E5=A4=A7=E4=BA=BA=E5=B7=A5?= =?UTF-8?q?=E5=B2=9B----=E6=B7=BB=E5=8A=A0js=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0827.最大人工岛.md | 65 ++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/problems/0827.最大人工岛.md b/problems/0827.最大人工岛.md index 4feb78de..3f32dd7a 100644 --- a/problems/0827.最大人工岛.md +++ b/problems/0827.最大人工岛.md @@ -348,6 +348,71 @@ class Solution: ``` +### JavaScript + +```JavaScript + +var largestIsland = function(grid) { +let res = 0; +const m = grid.length; +const n = grid[0].length; +const tag = new Array(n).fill().map(_ => new Array(m).fill(0)); +const area = new Map(); +const dir = [[0,1],[0,-1],[1,0],[-1,0]]; +const dfs = (grid,tag,x,y,mark) => { + let res = 1; + tag[x][y] = mark; + for(let i = 0; i < dir.length; i++) { + let nextX = x + dir[i][0]; + let nextY = y + dir[i][1]; + if(nextX < 0 || nextX >= m || nextY < 0 || nextY >= n) { + continue; + } + if(grid[nextX][nextY] === 1 && tag[nextX][nextY] === 0) { + res += dfs(grid,tag,nextX,nextY,mark); + } + } + return res; +} +let mark = 2; +//将岛屿用mark标记 +for(let i = 0; i < m; i++) { + for(let j = 0; j < n; j++) { + if(grid[i][j] === 1 && tag[i][j] === 0) { + area.set(mark,dfs(grid,tag,i,j,mark)); + res = Math.max(res,area.get(mark)); + mark++; + } + } +} +//将一个非岛屿格子变为岛屿 +for(let i = 0; i < m; i++) { + for(let j = 0; j < n; j++) { + if(grid[i][j] === 0) { + let z = 1; + const connected = new Set(); + for(let k = 0; k < dir.length; k++) { + let nextX = i + dir[k][0]; + let nextY = j + dir[k][1]; + if(nextX < 0 || nextX >= m || nextY < 0 || nextY >= n || tag[nextX][nextY] === 0 || connected.has(tag[nextX][nextY])) { + continue; + } + z += area.get(tag[nextX][nextY]); + connected.add(tag[nextX][nextY]); + } + res = Math.max(res,z); + } + } +} +return res; +}; + + +``` + + + +