forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main4.cpp
60 lines (44 loc) · 1.41 KB
/
main4.cpp
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/// Source : https://leetcode.com/problems/01-matrix/description/
/// Author : liuyubobobo
/// Time : 2018-09-30
#include <iostream>
#include <vector>
#include <queue>
#include <cassert>
using namespace std;
/// Dynamic Programming
/// Time Complexity: O(m*n)
/// Space Complexity: O(1)
class Solution {
public:
vector<vector<int>> updateMatrix(vector<vector<int>>& matrix) {
if(matrix.size() == 0 || matrix[0].size() == 0)
return matrix;
int m = matrix.size(), n = matrix[0].size();
vector<vector<int>> res(m, vector<int>(n, m + n + 1));
for(int i = 0; i < m; i ++)
for(int j = 0; j < n; j ++)
if(matrix[i][j] == 0)
res[i][j] = 0;
// top
for(int i = 1; i < m; i ++)
for(int j = 0; j < n; j ++)
res[i][j] = min(1 + res[i - 1][j], res[i][j]);
// left
for(int i = 0; i < m; i ++)
for(int j = 1; j < n; j ++)
res[i][j] = min(1 + res[i][j - 1], res[i][j]);
// bottom
for(int i = m - 2; i >= 0; i --)
for(int j = n - 1; j >= 0; j --)
res[i][j] = min(1 + res[i + 1][j], res[i][j]);
// right
for(int i = m - 1; i >= 0; i --)
for(int j = n - 2; j >= 0; j --)
res[i][j] = min(1 + res[i][j + 1], res[i][j]);
return res;
}
};
int main() {
return 0;
}