-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use loop to traverse the matrix, easy. 2D traverse
- Loading branch information
Showing
2 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
class Solution { | ||
public: | ||
/** | ||
* @brief LC: 3033: modify the matrix [E] | ||
* Time: O(N*M), Space: O(N*M) | ||
* | ||
* @param matrix | ||
* @return vector<vector<int>> | ||
*/ | ||
vector<vector<int>> modifiedMatrix(vector<vector<int>>& matrix) { | ||
vector<vector<int>> ans = matrix; | ||
int row = matrix.size(); | ||
int col = matrix[0].size(); | ||
for (int i = 0; i < col; i++) { | ||
int max = matrix[0][i]; | ||
for (int j = 0; j < row; j++) { | ||
max = max > matrix[j][i] ? max : matrix[j][i]; | ||
} | ||
for (int j = 0; j < row; j++) { | ||
if (ans[j][i] == -1) { | ||
ans[j][i] = max; | ||
} | ||
} | ||
} | ||
return ans; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
class Solution { | ||
public: | ||
/** | ||
* @brief LC: 2742: Painting the walls | ||
* Time & Space: O(n^2), DP: nums of state * time per state | ||
* | ||
* @param cost | ||
* @param time | ||
* @return int | ||
*/ | ||
int paintWalls(vector<int>& cost, vector<int>& time) { | ||
int n = cost.size(); | ||
vector<vector<int>> memo(n, vector<int>(n * 2 + 1, -1)); | ||
auto dfs = [&](auto&& dfs, int i, int j) -> int { | ||
if (j > i) { // all walls can be painted free | ||
return 0; | ||
} | ||
if (i < 0) { | ||
// j < 0 | ||
return INT_MAX / 2; | ||
} | ||
int& res = memo[i][j + n]; // add offset | ||
if (res != -1) { // calculated before | ||
return res; | ||
} | ||
return res = min(dfs(dfs, i - 1, j + time[i]) + cost[i], | ||
dfs(dfs, i - 1, j - 1)); | ||
}; | ||
return dfs(dfs, n - 1, 0); | ||
} | ||
}; |