Skip to content

Commit

Permalink
July 5: moddify matrix [E]
Browse files Browse the repository at this point in the history
use loop to traverse the matrix, easy. 2D traverse
  • Loading branch information
aucker committed Jul 5, 2024
1 parent e7807a8 commit c78d31b
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
30 changes: 30 additions & 0 deletions daily/Jul5.cpp
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;
}
};
34 changes: 34 additions & 0 deletions daily/June28.cpp
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);
}
};

0 comments on commit c78d31b

Please sign in to comment.