From 42f3110723b21f9cdb5ea77ef289a08ec27bc57e Mon Sep 17 00:00:00 2001 From: yanglbme Date: Fri, 26 Jul 2024 08:31:29 +0800 Subject: [PATCH 1/2] feat: add solutions to lc problem: No.0723 No.0723.Candy Crush --- solution/0700-0799/0723.Candy Crush/README.md | 176 ++++++++++++------ .../0700-0799/0723.Candy Crush/README_EN.md | 176 ++++++++++++------ .../0700-0799/0723.Candy Crush/Solution.go | 43 +++-- .../0700-0799/0723.Candy Crush/Solution.java | 40 ++-- .../0700-0799/0723.Candy Crush/Solution.py | 32 ++-- .../0700-0799/0723.Candy Crush/Solution.ts | 50 +++++ .../README.md | 34 ++-- solution/README.md | 2 +- 8 files changed, 372 insertions(+), 181 deletions(-) create mode 100644 solution/0700-0799/0723.Candy Crush/Solution.ts diff --git a/solution/0700-0799/0723.Candy Crush/README.md b/solution/0700-0799/0723.Candy Crush/README.md index 0f192402f72e8..63e161a4dcfb9 100644 --- a/solution/0700-0799/0723.Candy Crush/README.md +++ b/solution/0700-0799/0723.Candy Crush/README.md @@ -71,7 +71,11 @@ tags: -### 方法一 +### 方法一:模拟 + +我们可以逐行和逐列遍历矩阵,找到连续三个相同的元素,将它们标记为负数。如果成功标记,我们需要将矩阵中的元素下移,直到没有元素可以下移为止。 + +时间复杂度 $O(m^2 \times n^2)$,其中 $m$ 和 $n$ 分别是矩阵的行数和列数。空间复杂度 $O(1)$。 @@ -85,37 +89,33 @@ class Solution: while run: run = False for i in range(m): - for j in range(n - 2): - if ( - board[i][j] != 0 - and abs(board[i][j]) == abs(board[i][j + 1]) - and abs(board[i][j]) == abs(board[i][j + 2]) + for j in range(2, n): + if board[i][j] and abs(board[i][j]) == abs(board[i][j - 1]) == abs( + board[i][j - 2] ): run = True - board[i][j] = board[i][j + 1] = board[i][j + 2] = -abs( + board[i][j] = board[i][j - 1] = board[i][j - 2] = -abs( board[i][j] ) for j in range(n): - for i in range(m - 2): - if ( - board[i][j] != 0 - and abs(board[i][j]) == abs(board[i + 1][j]) - and abs(board[i][j]) == abs(board[i + 2][j]) + for i in range(2, m): + if board[i][j] and abs(board[i][j]) == abs(board[i - 1][j]) == abs( + board[i - 2][j] ): run = True - board[i][j] = board[i + 1][j] = board[i + 2][j] = -abs( + board[i][j] = board[i - 1][j] = board[i - 2][j] = -abs( board[i][j] ) if run: for j in range(n): - curr = m - 1 + k = m - 1 for i in range(m - 1, -1, -1): if board[i][j] > 0: - board[curr][j] = board[i][j] - curr -= 1 - while curr > -1: - board[curr][j] = 0 - curr -= 1 + board[k][j] = board[i][j] + k -= 1 + while k >= 0: + board[k][j] = 0 + k -= 1 return board ``` @@ -126,42 +126,46 @@ class Solution { public int[][] candyCrush(int[][] board) { int m = board.length, n = board[0].length; boolean run = true; + while (run) { run = false; - for (int i = 0; i < m; ++i) { - for (int j = 0; j < n - 2; ++j) { - if (board[i][j] != 0 && Math.abs(board[i][j]) == Math.abs(board[i][j + 1]) - && Math.abs(board[i][j]) == Math.abs(board[i][j + 2])) { + for (int i = 0; i < m; i++) { + for (int j = 2; j < n; j++) { + if (board[i][j] != 0 && Math.abs(board[i][j]) == Math.abs(board[i][j - 1]) + && Math.abs(board[i][j]) == Math.abs(board[i][j - 2])) { run = true; - board[i][j] = board[i][j + 1] = board[i][j + 2] = -Math.abs(board[i][j]); + int val = Math.abs(board[i][j]); + board[i][j] = board[i][j - 1] = board[i][j - 2] = -val; } } } - for (int j = 0; j < n; ++j) { - for (int i = 0; i < m - 2; ++i) { - if (board[i][j] != 0 && Math.abs(board[i][j]) == Math.abs(board[i + 1][j]) - && Math.abs(board[i][j]) == Math.abs(board[i + 2][j])) { + for (int j = 0; j < n; j++) { + for (int i = 2; i < m; i++) { + if (board[i][j] != 0 && Math.abs(board[i][j]) == Math.abs(board[i - 1][j]) + && Math.abs(board[i][j]) == Math.abs(board[i - 2][j])) { run = true; - board[i][j] = board[i + 1][j] = board[i + 2][j] = -Math.abs(board[i][j]); + int val = Math.abs(board[i][j]); + board[i][j] = board[i - 1][j] = board[i - 2][j] = -val; } } } if (run) { - for (int j = 0; j < n; ++j) { - int curr = m - 1; - for (int i = m - 1; i >= 0; --i) { + for (int j = 0; j < n; j++) { + int k = m - 1; + for (int i = m - 1; i >= 0; i--) { if (board[i][j] > 0) { - board[curr][j] = board[i][j]; - --curr; + board[k][j] = board[i][j]; + k--; } } - while (curr > -1) { - board[curr][j] = 0; - --curr; + while (k >= 0) { + board[k][j] = 0; + k--; } } } } + return board; } } @@ -218,52 +222,114 @@ public: ```go func candyCrush(board [][]int) [][]int { - m, n := len(board), len(board[0]) + m := len(board) + n := len(board[0]) run := true + for run { run = false for i := 0; i < m; i++ { - for j := 0; j < n-2; j++ { - if board[i][j] != 0 && abs(board[i][j]) == abs(board[i][j+1]) && abs(board[i][j]) == abs(board[i][j+2]) { + for j := 2; j < n; j++ { + if board[i][j] != 0 && abs(board[i][j]) == abs(board[i][j-1]) && abs(board[i][j]) == abs(board[i][j-2]) { run = true - t := -abs(board[i][j]) - board[i][j], board[i][j+1], board[i][j+2] = t, t, t + val := abs(board[i][j]) + board[i][j] = -val + board[i][j-1] = -val + board[i][j-2] = -val } } } for j := 0; j < n; j++ { - for i := 0; i < m-2; i++ { - if board[i][j] != 0 && abs(board[i][j]) == abs(board[i+1][j]) && abs(board[i][j]) == abs(board[i+2][j]) { + for i := 2; i < m; i++ { + if board[i][j] != 0 && abs(board[i][j]) == abs(board[i-1][j]) && abs(board[i][j]) == abs(board[i-2][j]) { run = true - t := -abs(board[i][j]) - board[i][j], board[i+1][j], board[i+2][j] = t, t, t + val := abs(board[i][j]) + board[i][j] = -val + board[i-1][j] = -val + board[i-2][j] = -val } } } if run { for j := 0; j < n; j++ { - curr := m - 1 + k := m - 1 for i := m - 1; i >= 0; i-- { if board[i][j] > 0 { - board[curr][j] = board[i][j] - curr-- + board[k][j] = board[i][j] + k-- } } - for curr > -1 { - board[curr][j] = 0 - curr-- + for k >= 0 { + board[k][j] = 0 + k-- } } } } + return board } func abs(x int) int { - if x >= 0 { - return x + if x < 0 { + return -x } - return -x + return x +} +``` + +#### TypeScript + +```ts +function candyCrush(board: number[][]): number[][] { + const m = board.length; + const n = board[0].length; + let run = true; + while (run) { + run = false; + for (let i = 0; i < m; i++) { + for (let j = 2; j < n; j++) { + if ( + board[i][j] !== 0 && + Math.abs(board[i][j]) === Math.abs(board[i][j - 1]) && + Math.abs(board[i][j]) === Math.abs(board[i][j - 2]) + ) { + run = true; + const val = Math.abs(board[i][j]); + board[i][j] = board[i][j - 1] = board[i][j - 2] = -val; + } + } + } + for (let j = 0; j < n; j++) { + for (let i = 2; i < m; i++) { + if ( + board[i][j] !== 0 && + Math.abs(board[i][j]) === Math.abs(board[i - 1][j]) && + Math.abs(board[i][j]) === Math.abs(board[i - 2][j]) + ) { + run = true; + const val = Math.abs(board[i][j]); + board[i][j] = board[i - 1][j] = board[i - 2][j] = -val; + } + } + } + if (run) { + for (let j = 0; j < n; j++) { + let k = m - 1; + for (let i = m - 1; i >= 0; i--) { + if (board[i][j] > 0) { + board[k][j] = board[i][j]; + k--; + } + } + while (k >= 0) { + board[k][j] = 0; + k--; + } + } + } + } + return board; } ``` diff --git a/solution/0700-0799/0723.Candy Crush/README_EN.md b/solution/0700-0799/0723.Candy Crush/README_EN.md index ecf1d5548c52a..946223f5170af 100644 --- a/solution/0700-0799/0723.Candy Crush/README_EN.md +++ b/solution/0700-0799/0723.Candy Crush/README_EN.md @@ -65,7 +65,11 @@ tags: -### Solution 1 +### Solution 1: Simulation + +We can traverse the matrix row by row and column by column to find three consecutive identical elements and mark them as negative numbers. If marking is successful, we need to move the elements in the matrix down until no elements can move down. + +The time complexity is $O(m^2 \times n^2)$, where $m$ and $n$ are the number of rows and columns of the matrix, respectively. The space complexity is $O(1)$. @@ -79,37 +83,33 @@ class Solution: while run: run = False for i in range(m): - for j in range(n - 2): - if ( - board[i][j] != 0 - and abs(board[i][j]) == abs(board[i][j + 1]) - and abs(board[i][j]) == abs(board[i][j + 2]) + for j in range(2, n): + if board[i][j] and abs(board[i][j]) == abs(board[i][j - 1]) == abs( + board[i][j - 2] ): run = True - board[i][j] = board[i][j + 1] = board[i][j + 2] = -abs( + board[i][j] = board[i][j - 1] = board[i][j - 2] = -abs( board[i][j] ) for j in range(n): - for i in range(m - 2): - if ( - board[i][j] != 0 - and abs(board[i][j]) == abs(board[i + 1][j]) - and abs(board[i][j]) == abs(board[i + 2][j]) + for i in range(2, m): + if board[i][j] and abs(board[i][j]) == abs(board[i - 1][j]) == abs( + board[i - 2][j] ): run = True - board[i][j] = board[i + 1][j] = board[i + 2][j] = -abs( + board[i][j] = board[i - 1][j] = board[i - 2][j] = -abs( board[i][j] ) if run: for j in range(n): - curr = m - 1 + k = m - 1 for i in range(m - 1, -1, -1): if board[i][j] > 0: - board[curr][j] = board[i][j] - curr -= 1 - while curr > -1: - board[curr][j] = 0 - curr -= 1 + board[k][j] = board[i][j] + k -= 1 + while k >= 0: + board[k][j] = 0 + k -= 1 return board ``` @@ -120,42 +120,46 @@ class Solution { public int[][] candyCrush(int[][] board) { int m = board.length, n = board[0].length; boolean run = true; + while (run) { run = false; - for (int i = 0; i < m; ++i) { - for (int j = 0; j < n - 2; ++j) { - if (board[i][j] != 0 && Math.abs(board[i][j]) == Math.abs(board[i][j + 1]) - && Math.abs(board[i][j]) == Math.abs(board[i][j + 2])) { + for (int i = 0; i < m; i++) { + for (int j = 2; j < n; j++) { + if (board[i][j] != 0 && Math.abs(board[i][j]) == Math.abs(board[i][j - 1]) + && Math.abs(board[i][j]) == Math.abs(board[i][j - 2])) { run = true; - board[i][j] = board[i][j + 1] = board[i][j + 2] = -Math.abs(board[i][j]); + int val = Math.abs(board[i][j]); + board[i][j] = board[i][j - 1] = board[i][j - 2] = -val; } } } - for (int j = 0; j < n; ++j) { - for (int i = 0; i < m - 2; ++i) { - if (board[i][j] != 0 && Math.abs(board[i][j]) == Math.abs(board[i + 1][j]) - && Math.abs(board[i][j]) == Math.abs(board[i + 2][j])) { + for (int j = 0; j < n; j++) { + for (int i = 2; i < m; i++) { + if (board[i][j] != 0 && Math.abs(board[i][j]) == Math.abs(board[i - 1][j]) + && Math.abs(board[i][j]) == Math.abs(board[i - 2][j])) { run = true; - board[i][j] = board[i + 1][j] = board[i + 2][j] = -Math.abs(board[i][j]); + int val = Math.abs(board[i][j]); + board[i][j] = board[i - 1][j] = board[i - 2][j] = -val; } } } if (run) { - for (int j = 0; j < n; ++j) { - int curr = m - 1; - for (int i = m - 1; i >= 0; --i) { + for (int j = 0; j < n; j++) { + int k = m - 1; + for (int i = m - 1; i >= 0; i--) { if (board[i][j] > 0) { - board[curr][j] = board[i][j]; - --curr; + board[k][j] = board[i][j]; + k--; } } - while (curr > -1) { - board[curr][j] = 0; - --curr; + while (k >= 0) { + board[k][j] = 0; + k--; } } } } + return board; } } @@ -212,52 +216,114 @@ public: ```go func candyCrush(board [][]int) [][]int { - m, n := len(board), len(board[0]) + m := len(board) + n := len(board[0]) run := true + for run { run = false for i := 0; i < m; i++ { - for j := 0; j < n-2; j++ { - if board[i][j] != 0 && abs(board[i][j]) == abs(board[i][j+1]) && abs(board[i][j]) == abs(board[i][j+2]) { + for j := 2; j < n; j++ { + if board[i][j] != 0 && abs(board[i][j]) == abs(board[i][j-1]) && abs(board[i][j]) == abs(board[i][j-2]) { run = true - t := -abs(board[i][j]) - board[i][j], board[i][j+1], board[i][j+2] = t, t, t + val := abs(board[i][j]) + board[i][j] = -val + board[i][j-1] = -val + board[i][j-2] = -val } } } for j := 0; j < n; j++ { - for i := 0; i < m-2; i++ { - if board[i][j] != 0 && abs(board[i][j]) == abs(board[i+1][j]) && abs(board[i][j]) == abs(board[i+2][j]) { + for i := 2; i < m; i++ { + if board[i][j] != 0 && abs(board[i][j]) == abs(board[i-1][j]) && abs(board[i][j]) == abs(board[i-2][j]) { run = true - t := -abs(board[i][j]) - board[i][j], board[i+1][j], board[i+2][j] = t, t, t + val := abs(board[i][j]) + board[i][j] = -val + board[i-1][j] = -val + board[i-2][j] = -val } } } if run { for j := 0; j < n; j++ { - curr := m - 1 + k := m - 1 for i := m - 1; i >= 0; i-- { if board[i][j] > 0 { - board[curr][j] = board[i][j] - curr-- + board[k][j] = board[i][j] + k-- } } - for curr > -1 { - board[curr][j] = 0 - curr-- + for k >= 0 { + board[k][j] = 0 + k-- } } } } + return board } func abs(x int) int { - if x >= 0 { - return x + if x < 0 { + return -x } - return -x + return x +} +``` + +#### TypeScript + +```ts +function candyCrush(board: number[][]): number[][] { + const m = board.length; + const n = board[0].length; + let run = true; + while (run) { + run = false; + for (let i = 0; i < m; i++) { + for (let j = 2; j < n; j++) { + if ( + board[i][j] !== 0 && + Math.abs(board[i][j]) === Math.abs(board[i][j - 1]) && + Math.abs(board[i][j]) === Math.abs(board[i][j - 2]) + ) { + run = true; + const val = Math.abs(board[i][j]); + board[i][j] = board[i][j - 1] = board[i][j - 2] = -val; + } + } + } + for (let j = 0; j < n; j++) { + for (let i = 2; i < m; i++) { + if ( + board[i][j] !== 0 && + Math.abs(board[i][j]) === Math.abs(board[i - 1][j]) && + Math.abs(board[i][j]) === Math.abs(board[i - 2][j]) + ) { + run = true; + const val = Math.abs(board[i][j]); + board[i][j] = board[i - 1][j] = board[i - 2][j] = -val; + } + } + } + if (run) { + for (let j = 0; j < n; j++) { + let k = m - 1; + for (let i = m - 1; i >= 0; i--) { + if (board[i][j] > 0) { + board[k][j] = board[i][j]; + k--; + } + } + while (k >= 0) { + board[k][j] = 0; + k--; + } + } + } + } + return board; } ``` diff --git a/solution/0700-0799/0723.Candy Crush/Solution.go b/solution/0700-0799/0723.Candy Crush/Solution.go index 1b38bb2651eba..13a79d4adc701 100644 --- a/solution/0700-0799/0723.Candy Crush/Solution.go +++ b/solution/0700-0799/0723.Candy Crush/Solution.go @@ -1,48 +1,55 @@ func candyCrush(board [][]int) [][]int { - m, n := len(board), len(board[0]) + m := len(board) + n := len(board[0]) run := true + for run { run = false for i := 0; i < m; i++ { - for j := 0; j < n-2; j++ { - if board[i][j] != 0 && abs(board[i][j]) == abs(board[i][j+1]) && abs(board[i][j]) == abs(board[i][j+2]) { + for j := 2; j < n; j++ { + if board[i][j] != 0 && abs(board[i][j]) == abs(board[i][j-1]) && abs(board[i][j]) == abs(board[i][j-2]) { run = true - t := -abs(board[i][j]) - board[i][j], board[i][j+1], board[i][j+2] = t, t, t + val := abs(board[i][j]) + board[i][j] = -val + board[i][j-1] = -val + board[i][j-2] = -val } } } for j := 0; j < n; j++ { - for i := 0; i < m-2; i++ { - if board[i][j] != 0 && abs(board[i][j]) == abs(board[i+1][j]) && abs(board[i][j]) == abs(board[i+2][j]) { + for i := 2; i < m; i++ { + if board[i][j] != 0 && abs(board[i][j]) == abs(board[i-1][j]) && abs(board[i][j]) == abs(board[i-2][j]) { run = true - t := -abs(board[i][j]) - board[i][j], board[i+1][j], board[i+2][j] = t, t, t + val := abs(board[i][j]) + board[i][j] = -val + board[i-1][j] = -val + board[i-2][j] = -val } } } if run { for j := 0; j < n; j++ { - curr := m - 1 + k := m - 1 for i := m - 1; i >= 0; i-- { if board[i][j] > 0 { - board[curr][j] = board[i][j] - curr-- + board[k][j] = board[i][j] + k-- } } - for curr > -1 { - board[curr][j] = 0 - curr-- + for k >= 0 { + board[k][j] = 0 + k-- } } } } + return board } func abs(x int) int { - if x >= 0 { - return x + if x < 0 { + return -x } - return -x + return x } \ No newline at end of file diff --git a/solution/0700-0799/0723.Candy Crush/Solution.java b/solution/0700-0799/0723.Candy Crush/Solution.java index f7348e9821c9f..301297b502f74 100644 --- a/solution/0700-0799/0723.Candy Crush/Solution.java +++ b/solution/0700-0799/0723.Candy Crush/Solution.java @@ -2,42 +2,46 @@ class Solution { public int[][] candyCrush(int[][] board) { int m = board.length, n = board[0].length; boolean run = true; + while (run) { run = false; - for (int i = 0; i < m; ++i) { - for (int j = 0; j < n - 2; ++j) { - if (board[i][j] != 0 && Math.abs(board[i][j]) == Math.abs(board[i][j + 1]) - && Math.abs(board[i][j]) == Math.abs(board[i][j + 2])) { + for (int i = 0; i < m; i++) { + for (int j = 2; j < n; j++) { + if (board[i][j] != 0 && Math.abs(board[i][j]) == Math.abs(board[i][j - 1]) + && Math.abs(board[i][j]) == Math.abs(board[i][j - 2])) { run = true; - board[i][j] = board[i][j + 1] = board[i][j + 2] = -Math.abs(board[i][j]); + int val = Math.abs(board[i][j]); + board[i][j] = board[i][j - 1] = board[i][j - 2] = -val; } } } - for (int j = 0; j < n; ++j) { - for (int i = 0; i < m - 2; ++i) { - if (board[i][j] != 0 && Math.abs(board[i][j]) == Math.abs(board[i + 1][j]) - && Math.abs(board[i][j]) == Math.abs(board[i + 2][j])) { + for (int j = 0; j < n; j++) { + for (int i = 2; i < m; i++) { + if (board[i][j] != 0 && Math.abs(board[i][j]) == Math.abs(board[i - 1][j]) + && Math.abs(board[i][j]) == Math.abs(board[i - 2][j])) { run = true; - board[i][j] = board[i + 1][j] = board[i + 2][j] = -Math.abs(board[i][j]); + int val = Math.abs(board[i][j]); + board[i][j] = board[i - 1][j] = board[i - 2][j] = -val; } } } if (run) { - for (int j = 0; j < n; ++j) { - int curr = m - 1; - for (int i = m - 1; i >= 0; --i) { + for (int j = 0; j < n; j++) { + int k = m - 1; + for (int i = m - 1; i >= 0; i--) { if (board[i][j] > 0) { - board[curr][j] = board[i][j]; - --curr; + board[k][j] = board[i][j]; + k--; } } - while (curr > -1) { - board[curr][j] = 0; - --curr; + while (k >= 0) { + board[k][j] = 0; + k--; } } } } + return board; } } \ No newline at end of file diff --git a/solution/0700-0799/0723.Candy Crush/Solution.py b/solution/0700-0799/0723.Candy Crush/Solution.py index 5264343d7f3d4..9749812515dbe 100644 --- a/solution/0700-0799/0723.Candy Crush/Solution.py +++ b/solution/0700-0799/0723.Candy Crush/Solution.py @@ -5,35 +5,31 @@ def candyCrush(self, board: List[List[int]]) -> List[List[int]]: while run: run = False for i in range(m): - for j in range(n - 2): - if ( - board[i][j] != 0 - and abs(board[i][j]) == abs(board[i][j + 1]) - and abs(board[i][j]) == abs(board[i][j + 2]) + for j in range(2, n): + if board[i][j] and abs(board[i][j]) == abs(board[i][j - 1]) == abs( + board[i][j - 2] ): run = True - board[i][j] = board[i][j + 1] = board[i][j + 2] = -abs( + board[i][j] = board[i][j - 1] = board[i][j - 2] = -abs( board[i][j] ) for j in range(n): - for i in range(m - 2): - if ( - board[i][j] != 0 - and abs(board[i][j]) == abs(board[i + 1][j]) - and abs(board[i][j]) == abs(board[i + 2][j]) + for i in range(2, m): + if board[i][j] and abs(board[i][j]) == abs(board[i - 1][j]) == abs( + board[i - 2][j] ): run = True - board[i][j] = board[i + 1][j] = board[i + 2][j] = -abs( + board[i][j] = board[i - 1][j] = board[i - 2][j] = -abs( board[i][j] ) if run: for j in range(n): - curr = m - 1 + k = m - 1 for i in range(m - 1, -1, -1): if board[i][j] > 0: - board[curr][j] = board[i][j] - curr -= 1 - while curr > -1: - board[curr][j] = 0 - curr -= 1 + board[k][j] = board[i][j] + k -= 1 + while k >= 0: + board[k][j] = 0 + k -= 1 return board diff --git a/solution/0700-0799/0723.Candy Crush/Solution.ts b/solution/0700-0799/0723.Candy Crush/Solution.ts new file mode 100644 index 0000000000000..85328a2e6cd6e --- /dev/null +++ b/solution/0700-0799/0723.Candy Crush/Solution.ts @@ -0,0 +1,50 @@ +function candyCrush(board: number[][]): number[][] { + const m = board.length; + const n = board[0].length; + let run = true; + while (run) { + run = false; + for (let i = 0; i < m; i++) { + for (let j = 2; j < n; j++) { + if ( + board[i][j] !== 0 && + Math.abs(board[i][j]) === Math.abs(board[i][j - 1]) && + Math.abs(board[i][j]) === Math.abs(board[i][j - 2]) + ) { + run = true; + const val = Math.abs(board[i][j]); + board[i][j] = board[i][j - 1] = board[i][j - 2] = -val; + } + } + } + for (let j = 0; j < n; j++) { + for (let i = 2; i < m; i++) { + if ( + board[i][j] !== 0 && + Math.abs(board[i][j]) === Math.abs(board[i - 1][j]) && + Math.abs(board[i][j]) === Math.abs(board[i - 2][j]) + ) { + run = true; + const val = Math.abs(board[i][j]); + board[i][j] = board[i - 1][j] = board[i - 2][j] = -val; + } + } + } + if (run) { + for (let j = 0; j < n; j++) { + let k = m - 1; + for (let i = m - 1; i >= 0; i--) { + if (board[i][j] > 0) { + board[k][j] = board[i][j]; + k--; + } + } + while (k >= 0) { + board[k][j] = 0; + k--; + } + } + } + } + return board; +} diff --git a/solution/3200-3299/3231.Minimum Number of Increasing Subsequence to Be Removed/README.md b/solution/3200-3299/3231.Minimum Number of Increasing Subsequence to Be Removed/README.md index fb6ed09a68c9a..e79f9f6d009f5 100644 --- a/solution/3200-3299/3231.Minimum Number of Increasing Subsequence to Be Removed/README.md +++ b/solution/3200-3299/3231.Minimum Number of Increasing Subsequence to Be Removed/README.md @@ -6,7 +6,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3231.Mi -# [3231. Minimum Number of Increasing Subsequence to Be Removed 🔒](https://leetcode.cn/problems/minimum-number-of-increasing-subsequence-to-be-removed) +# [3231. 要删除的递增子序列的最小数量 🔒](https://leetcode.cn/problems/minimum-number-of-increasing-subsequence-to-be-removed) [English Version](/solution/3200-3299/3231.Minimum%20Number%20of%20Increasing%20Subsequence%20to%20Be%20Removed/README_EN.md) @@ -14,45 +14,47 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3231.Mi -

Given an array of integers nums, you are allowed to perform the following operation any number of times:

+

给定一个整数数组 nums,你可以执行任意次下面的操作:

-

Your task is to find the minimum number of operations required to make the array empty.

+

您的任务是找到使数组为 所需的 最小 操作数。

 

-

Example 1:

+ +

示例 1:

-

Input: nums = [5,3,1,4,2]

+

输入:nums = [5,3,1,4,2]

-

Output: 3

+

输出:3

-

Explanation:

+

解释:

-

We remove subsequences [1, 2], [3, 4], [5].

+

我们删除子序列 [1, 2][3, 4][5]

-

Example 2:

+

示例 2:

-

Input: nums = [1,2,3,4,5]

+

输入:nums = [1,2,3,4,5]

-

Output: 1

+

输出:1

-

Example 3:

+

示例 3:

-

Input: nums = [5,4,3,2,1]

+

输入:nums = [5,4,3,2,1]

-

Output: 5

+

输出:5

 

-

Constraints:

+ +

提示: