Skip to content

Commit c52bed2

Browse files
authored
Add April Week 3
2 parents a448e2d + fdbb044 commit c52bed2

File tree

6 files changed

+170
-0
lines changed

6 files changed

+170
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
vector<int> productExceptSelf(vector<int>& nums) {
4+
vector<int> ans;
5+
int prod = 1;
6+
for (int i = 0; i < nums.size(); i++) {
7+
ans.push_back(prod);
8+
prod*=nums[i];
9+
}
10+
prod = 1;
11+
for (int i = nums.size() - 1; i >= 0; i--) {
12+
ans[i]*=prod;
13+
prod*=nums[i];
14+
}
15+
return ans;
16+
}
17+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
bool checkValidString(string s) {
4+
int lo = 0, hi = 0;
5+
for (int i = 0; i < s.size(); i++) {
6+
if (s[i] == ')') {
7+
hi--;
8+
if (lo > 0) lo--;
9+
}
10+
if (s[i] == '(') {
11+
lo++;
12+
hi++;
13+
}
14+
if (s[i] == '*') {
15+
hi++;
16+
if (lo > 0) {
17+
lo--;
18+
}
19+
}
20+
if (hi < 0) return false;
21+
}
22+
if (lo == 0) return true;
23+
else return false;
24+
}
25+
};
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
class Solution {
2+
public:
3+
int numIslands(vector<vector<char>>& grid) {
4+
int r = grid.size();
5+
if (r == 0) return 0;
6+
int c = grid[0].size();
7+
vector<vector<int>> visited(r, vector<int>(c, 0));
8+
int islands = 0;
9+
queue<pair<int, int>> q;
10+
for (int i = 0; i < r; i++) {
11+
for (int j = 0; j < c; j++) {
12+
if (grid[i][j] == '1' and !visited[i][j]) {
13+
q.push(make_pair(i, j));
14+
visited[i][j] = 1;
15+
islands++;
16+
while (!q.empty()) {
17+
int cr = q.front().first;
18+
int cc = q.front().second;
19+
if (cr > 0) {
20+
if (grid[cr - 1][cc] == '1' and !visited[cr - 1][cc]) {
21+
q.push(make_pair(cr - 1, cc));
22+
visited[cr - 1][cc] = 1;
23+
}
24+
}
25+
if (cc > 0) {
26+
if (grid[cr][cc - 1] == '1' and !visited[cr][cc - 1]) {
27+
q.push(make_pair(cr, cc - 1));
28+
visited[cr][cc - 1] = 1;
29+
}
30+
}
31+
if (cr < r - 1) {
32+
if (grid[cr + 1][cc] == '1' and !visited[cr + 1][cc]) {
33+
q.push(make_pair(cr + 1, cc));
34+
visited[cr + 1][cc] = 1;
35+
}
36+
}
37+
if (cc < c - 1) {
38+
if (grid[cr][cc + 1] == '1' and !visited[cr][cc + 1]) {
39+
q.push(make_pair(cr, cc + 1));
40+
visited[cr][cc + 1] = 1;
41+
}
42+
}
43+
q.pop();
44+
}
45+
}
46+
}
47+
}
48+
return islands;
49+
}
50+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
int minPathSum(vector<vector<int>>& grid) {
4+
int r = grid.size();
5+
if (r == 0) return 0;
6+
int c = grid[0].size();
7+
for (int i = 0; i < r; i++) {
8+
for (int j = 0; j < c; j++) {
9+
if (i or j) {
10+
int mini = INT_MAX;
11+
if (i > 0) mini = grid[i - 1][j];
12+
if (j > 0) mini = min(mini, grid[i][j - 1]);
13+
grid[i][j] = mini + grid[i][j];
14+
}
15+
}
16+
}
17+
return grid[r - 1][c - 1];
18+
}
19+
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
int search(vector<int>& nums, int target) {
4+
int n = nums.size();
5+
if (n == 0) return -1;
6+
if (n == 1) return target==nums[0]?0:-1;
7+
int lo = 0, hi = n - 1;
8+
while (lo <= hi) {
9+
int mid = (lo + hi)/2;
10+
if (target == nums[mid]) return mid;
11+
if (nums[lo] <= nums[mid]) {
12+
if (target >= nums[lo] and target <= nums[mid]) {
13+
hi = mid - 1;
14+
}
15+
else lo = mid + 1;
16+
}
17+
else {
18+
if (target >= nums[mid] and target <= nums[hi]) {
19+
lo = mid + 1;
20+
}
21+
else hi = mid - 1;
22+
}
23+
}
24+
return -1;
25+
}
26+
};
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
int index = 0;
15+
TreeNode* constructTree(vector<int>& preorder, int mini, int maxi, int size) {
16+
if( index >= size )
17+
return nullptr;
18+
TreeNode* curr = nullptr;
19+
int currData = preorder[index];
20+
if ((currData > mini) and (currData < maxi))
21+
{
22+
curr = new TreeNode(currData);
23+
index++;
24+
curr -> left = constructTree(preorder, mini, currData, size );
25+
curr -> right = constructTree(preorder, currData, maxi, size );
26+
}
27+
return curr;
28+
}
29+
TreeNode* bstFromPreorder(vector<int>& preorder) {
30+
int size = preorder.size();
31+
return constructTree(preorder, INT_MIN, INT_MAX, size);
32+
}
33+
};

0 commit comments

Comments
 (0)