Skip to content

Commit d34bcf0

Browse files
authored
Merge pull request neetcode-gh#66 from Ibraam-Nashaat/main
Create 11- Container with most water in c++
2 parents 463b6d6 + 5808cbf commit d34bcf0

6 files changed

+168
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
13+
class Solution {
14+
public:
15+
vector<vector<int>> levelOrder(TreeNode* root) {
16+
queue< TreeNode* > q;
17+
q.push(root);
18+
vector<vector<int>> v;
19+
vector<int> k;
20+
TreeNode* c;
21+
int j;
22+
while(!q.empty() )
23+
{
24+
k.clear();
25+
j=q.size();
26+
for(int i=0;i<j;i++)
27+
{
28+
c=q.front();
29+
q.pop();
30+
if(c)
31+
{
32+
k.push_back(c->val);
33+
q.push(c->left);
34+
q.push(c->right);
35+
}
36+
}
37+
if(!k.empty())
38+
v.push_back(k);
39+
40+
}
41+
return v;
42+
43+
}
44+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
int maxArea(vector<int>& height) {
4+
int i=0,j=height.size()-1;
5+
int maxA=0,minh;
6+
while(i<j)
7+
{
8+
minh=min(height[i],height[j]);
9+
if(minh*(j-i)>maxA)
10+
maxA=minh*(j-i);
11+
if(minh==height[i])
12+
i++;
13+
else
14+
j--;
15+
}
16+
return maxA;
17+
}
18+
};

cpp/141-Linked-List-Cycle.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode(int x) : val(x), next(NULL) {}
7+
* };
8+
*/
9+
class Solution {
10+
public:
11+
bool hasCycle(ListNode *head) {
12+
if(!head) return 0;
13+
unordered_set<ListNode*> s;
14+
s.insert(head);
15+
ListNode * ptr=head->next;
16+
while(ptr)
17+
{
18+
if(s.find(ptr)==s.end())
19+
{
20+
s.insert(ptr);
21+
ptr=ptr->next;
22+
}
23+
else
24+
return 1;
25+
}
26+
return 0;
27+
28+
}
29+
};

cpp/36- Valid-Sudoku.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public:
3+
bool isValidSudoku(vector<vector<char>>& board) {
4+
5+
unordered_set <int> row[9];
6+
unordered_set<int> col[9];
7+
unordered_set<int> squ[3][3];
8+
9+
for(int i=0;i<9;i++)
10+
for(int j=0;j<9;j++)
11+
{
12+
if (board[i][j]=='.')
13+
continue;
14+
else if(row[i].find(board[i][j]) !=row[i].end()
15+
|| col[j].find(board[i][j]) !=col[j].end()
16+
|| squ[i/3][j/3].find(board[i][j]) !=squ[i/3][j/3].end()
17+
)
18+
return false;
19+
else
20+
{
21+
row[i].insert(board[i][j]);
22+
col[j].insert(board[i][j]);
23+
squ[i/3][j/3].insert(board[i][j]);
24+
25+
}
26+
}
27+
return 1;}
28+
};

cpp/74-Search-a-2D-Matrix.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
bool searchMatrix(vector<vector<int>>& matrix, int target) {
4+
int r=matrix.size();
5+
int c=matrix[0].size();
6+
for(int i=0;i<r;i++)
7+
{
8+
9+
if(target>=matrix[i][0] && target<=matrix[i][c-1])
10+
if(binary_search(matrix[i].begin(),matrix[i].end(),target))
11+
return true;
12+
else return false;
13+
14+
15+
}
16+
return false;
17+
}
18+
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
void check(TreeNode* root,vector<int>& v)
13+
{
14+
if(!root) return;
15+
check(root->left,v);
16+
v.push_back(root->val);
17+
check(root->right,v);
18+
}
19+
class Solution {
20+
public:
21+
bool isValidBST(TreeNode* root) {
22+
vector<int> v;
23+
check(root,v);
24+
for(int i=0;i<v.size()-1;i++)
25+
{
26+
if(v[i]>=v[i+1])
27+
return 0;
28+
}
29+
return 1;
30+
}
31+
};

0 commit comments

Comments
 (0)