Skip to content

Commit 21d8917

Browse files
Merge pull request neetcode-gh#2840 from Abe0770/main
Create 0221-maximal-square.cpp
2 parents e644912 + 8dac098 commit 21d8917

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

cpp/0221-maximal-square.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
Given an m x n binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area.
3+
4+
Ex. Input: matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
5+
Output: 4
6+
7+
Time : O(m*n)
8+
Space : O(m*n)
9+
*/
10+
11+
class Solution {
12+
public:
13+
int maximalSquare(vector<vector<char>>& matrix) {
14+
int rows = matrix.size(), cols = matrix[0].size();
15+
16+
vector<vector<int>> dp (rows+1, vector<int>(cols+1, 0));
17+
int maxi = 0;
18+
for(int i = rows-1 ; i >= 0; --i) {
19+
for(int j = cols-1 ; j >=0 ; --j) {
20+
if(matrix[i][j] == '1') {
21+
int right = dp[i][j+1], dia = dp[i+1][j+1], bottom = dp[i+1][j];
22+
23+
dp[i][j] = 1 + min(right, min(dia, bottom));
24+
maxi = max(maxi, dp[i][j]);
25+
}
26+
else {
27+
dp[i][j] = 0;
28+
}
29+
}
30+
}
31+
return maxi*maxi;
32+
}
33+
};

cpp/1024-video-stitching.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
You are given a series of video clips from a sporting event that lasted time seconds. These video clips can be overlapping with each other and have varying lengths.
3+
Each video clip is described by an array clips where clips[i] = [starti, endi] indicates that the ith clip started at starti and ended at endi.
4+
We can cut these clips into segments freely.
5+
For example, a clip [0, 7] can be cut into segments [0, 1] + [1, 3] + [3, 7].
6+
Return the minimum number of clips needed so that we can cut the clips into segments that cover the entire sporting event [0, time]. If the task is impossible, return -1.
7+
8+
Ex. Input: clips = [[0,2],[4,6],[8,10],[1,9],[1,5],[5,9]], time = 10
9+
Output: 3
10+
Explanation: We take the clips [0,2], [8,10], [1,9]; a total of 3 clips.
11+
Then, we can reconstruct the sporting event as follows:
12+
We cut [1,9] into segments [1,2] + [2,8] + [8,9].
13+
Now we have segments [0,2] + [2,8] + [8,10] which cover the sporting event [0, 10].
14+
15+
Time : O(N log N)
16+
Space : O(1)
17+
*/
18+
19+
class Solution {
20+
public:
21+
int videoStitching(vector<vector<int>>& clips, int time) {
22+
int maxi = 0;
23+
sort(clips.begin() , clips.end());
24+
for(int i = 0 ; i < clips.size() ; i++)
25+
maxi = max(maxi, clips[i][1]);
26+
27+
if(maxi < time)
28+
return -1;
29+
30+
int count = 0, endTime = 0, covered = INT_MIN;
31+
for(int i = 0; endTime < time ; ) {
32+
++count;
33+
while(i < clips.size() && clips[i][0] <= endTime)
34+
covered = max(covered, clips[i++][1]);
35+
36+
if(endTime == covered)
37+
return -1;
38+
endTime = covered;
39+
}
40+
return count;
41+
}
42+
};

0 commit comments

Comments
 (0)