-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathLC0542.cpp
More file actions
executable file
·36 lines (32 loc) · 911 Bytes
/
LC0542.cpp
File metadata and controls
executable file
·36 lines (32 loc) · 911 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*
Problem Statement: https://leetcode.com/problems/01-matrix/
Time: O(m • n)
Space: O(m • n)
Author: Mohammed Shoaib, github.com/Mohammed-Shoaib
*/
class Solution {
public:
vector<vector<int>> updateMatrix(vector<vector<int>>& matrix) {
int m = matrix.size(), n = matrix[0].size();
vector<vector<int>> dp(m, vector<int>(n, 1e9));
// dynamic programming, move right or down
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++) {
if (matrix[i][j] == 0)
dp[i][j] = 0;
if (i > 0)
dp[i][j] = min(1 + dp[i - 1][j], dp[i][j]);
if (j > 0)
dp[i][j] = min(1 + dp[i][j - 1], dp[i][j]);
}
// dynamic programming, move up or left
for (int i = m - 1; i >= 0; i--)
for (int j = n - 1; j >= 0; j--) {
if (i < m - 1)
dp[i][j] = min(1 + dp[i + 1][j], dp[i][j]);
if (j < n - 1)
dp[i][j] = min(1 + dp[i][j + 1], dp[i][j]);
}
return dp;
}
};