Skip to content

Commit 6067d0f

Browse files
Vikas GolaVikas Gola
Vikas Gola
authored and
Vikas Gola
committed
add leetcode solutions
1 parent 4afbd84 commit 6067d0f

File tree

145 files changed

+2863
-300
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

145 files changed

+2863
-300
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,7 @@ dmypy.json
127127

128128
# Pyre type checker
129129
.pyre/
130+
*.dat
131+
all.html
132+
133+
.vscode/

1.two-sum.cpp

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,12 @@
1-
/*
2-
* @lc app=leetcode id=1 lang=cpp
3-
*
4-
* [1] Two Sum
5-
*/
6-
7-
// @lc code=start
8-
91
class Solution {
102
public:
113
vector<int> twoSum(vector<int>& nums, int target) {
12-
map<int, int> cont;
13-
for(int i = 0; i < nums.size(); i++){
14-
int n = nums[i];
15-
if(cont.count(target-n)) return {i, cont[target-n]};
16-
cont[n] = i;
4+
map<int, int> exists;
5+
for(int i=0;i<nums.size();i++){
6+
auto n = exists.find(target-nums[i]);
7+
if(n != exists.end()) return {exists[target-nums[i]], i};
8+
exists[nums[i]] = i;
179
}
1810
return {};
1911
}
20-
};
21-
// @lc code=end
22-
12+
};

100.same-tree.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
/*
2-
* @lc app=leetcode id=100 lang=cpp
3-
*
4-
* [100] Same Tree
5-
*/
6-
7-
// @lc code=start
81
/**
92
* Definition for a binary tree node.
103
* struct TreeNode {
@@ -22,6 +15,4 @@ class Solution {
2215
if(!p || !q) return p == q;
2316
return p->val == q->val && isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
2417
}
25-
};
26-
// @lc code=end
27-
18+
};

101.symmetric-tree.cpp

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
/*
2-
* @lc app=leetcode id=101 lang=cpp
3-
*
4-
* [101] Symmetric Tree
5-
*/
6-
7-
// @lc code=start
81
/**
92
* Definition for a binary tree node.
103
* struct TreeNode {
@@ -23,8 +16,6 @@ class Solution {
2316
return p->val == q->val && isSameTree(p->left, q->right) && isSameTree(p->right, q->left);
2417
}
2518
bool isSymmetric(TreeNode* root) {
26-
return isSameTree(root->left, root->right);
19+
return isSameTree(root->left, root->right);
2720
}
28-
};
29-
// @lc code=end
30-
21+
};

1019.squares-of-a-sorted-array.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution {
2+
public:
3+
vector<int> sortedSquares(vector<int>& A) {
4+
for_each(A.begin(), A.end(), [](int &a){a=a*a;});
5+
sort(A.begin(), A.end());
6+
return A;
7+
}
8+
};
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class Solution {
2+
public:
3+
4+
int getparent(int loc[], int a){
5+
int t = loc[a];
6+
while(t != loc[t]) t = loc[t];
7+
return t;
8+
}
9+
10+
11+
bool check(int loc[], int a, int b){
12+
int t = getparent(loc, a);
13+
int t2 = getparent(loc, b);
14+
return t != t2;
15+
}
16+
17+
void add(int loc[], int a, int b){
18+
loc[b] = loc[a];
19+
}
20+
21+
bool equationsPossible(vector<string>& equations) {
22+
int n = equations.size();
23+
//if(n < 1) return true;
24+
int loc[26];
25+
for(int i=0;i<26;i++) loc[i] = i;
26+
27+
for(int i=0;i<n;i++){
28+
int a = getparent(loc, equations[i][0] - 'a');
29+
int b = getparent(loc, equations[i][3] - 'a');
30+
if(equations[i][1] == '='){
31+
a < b ? add(loc, a, b) : add(loc, b, a);
32+
}
33+
}
34+
35+
for(int i=0;i<n;i++){
36+
int a = equations[i][0] - 'a';
37+
int b = equations[i][3] - 'a';
38+
if(equations[i][1] == '!' && !check(loc, a, b)){
39+
return false;
40+
}
41+
}
42+
return true;
43+
}
44+
};

1036.rotting-oranges.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public:
3+
int orangesRotting(vector<vector<int>>& grid) {
4+
bool progress, rot, completed;
5+
int ans = 0;
6+
while(true){
7+
vector<vector<int>> grid2(grid);
8+
progress = false;
9+
completed = true;
10+
for(int i=0;i<grid.size();i++){
11+
for(int j=0;j<grid[i].size();j++){
12+
rot = false;
13+
if(grid[i][j] == 1){
14+
completed = false;
15+
rot |= j-1 >= 0 && grid[i][j-1] == 2 ? true: false;
16+
rot |= j+1 < grid[i].size() && grid[i][j+1] == 2 ? true: false;
17+
rot |= i-1 >= 0 && grid[i-1][j] == 2 ? true: false;
18+
rot |= i+1 < grid.size() && grid[i+1][j] == 2 ? true: false;
19+
}
20+
if(rot) grid2[i][j] = 2;
21+
progress |= rot;
22+
}
23+
}
24+
if(!completed && !progress) return -1;
25+
if(!progress) break;
26+
ans++;
27+
grid = grid2;
28+
}
29+
return ans;
30+
}
31+
};

104.maximum-depth-of-binary-tree.cpp

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
/*
2-
* @lc app=leetcode id=104 lang=cpp
3-
*
4-
* [104] Maximum Depth of Binary Tree
5-
*/
6-
7-
// @lc code=start
81
/**
92
* Definition for a binary tree node.
103
* struct TreeNode {
@@ -20,8 +13,6 @@ class Solution {
2013
public:
2114
int maxDepth(TreeNode* root) {
2215
if(!root) return 0;
23-
return 1+max(maxDepth(root->left), maxDepth(root->right));
16+
return max(maxDepth(root->left), maxDepth(root->right))+1;
2417
}
25-
};
26-
// @lc code=end
27-
18+
};

108.convert-sorted-array-to-binary-search-tree.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
/*
2-
* @lc app=leetcode id=108 lang=cpp
3-
*
4-
* [108] Convert Sorted Array to Binary Search Tree
5-
*/
6-
7-
// @lc code=start
81
/**
92
* Definition for a binary tree node.
103
* struct TreeNode {
@@ -31,6 +24,4 @@ class Solution {
3124
return root;
3225
}
3326

34-
};
35-
// @lc code=end
36-
27+
};

11.container-with-most-water.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
int maxArea(vector<int>& height) {
4+
int l=0, r=height.size()-1;
5+
int maxA = 0;
6+
while(l<r){
7+
maxA = max(maxA, min(height[l], height[r])*abs(l-r));
8+
if(height[l] < height[r]){
9+
l++;
10+
}else{
11+
r--;
12+
}
13+
}
14+
return maxA;
15+
}
16+
};

110.balanced-binary-tree.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
/*
2-
* @lc app=leetcode id=110 lang=cpp
3-
*
4-
* [110] Balanced Binary Tree
5-
*/
6-
7-
// @lc code=start
81
/**
92
* Definition for a binary tree node.
103
* struct TreeNode {
@@ -31,6 +24,4 @@ class Solution {
3124
if(abs(ldepth-rdepth) > 1) return -1;
3225
return 1+max(ldepth, rdepth);
3326
}
34-
};
35-
// @lc code=end
36-
27+
};

111.minimum-depth-of-binary-tree.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
/*
2-
* @lc app=leetcode id=111 lang=cpp
3-
*
4-
* [111] Minimum Depth of Binary Tree
5-
*/
6-
7-
// @lc code=start
81
/**
92
* Definition for a binary tree node.
103
* struct TreeNode {
@@ -25,6 +18,4 @@ class Solution {
2518
if(!ml || !mr) return max(mr, ml)+1;
2619
return 1+min(ml, mr);
2720
}
28-
};
29-
// @lc code=end
30-
21+
};

112.path-sum.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
bool hasPathSum(TreeNode* root, int targetSum) {
15+
if(!root) return false;
16+
targetSum = targetSum-root->val;
17+
if(targetSum==0&&!root->left&&!root->right) return true;
18+
return hasPathSum(root->left, targetSum) || hasPathSum(root->right, targetSum);
19+
}
20+
};

1137.height-checker.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
int heightChecker(vector<int>& heights) {
4+
vector<int> temp(heights);
5+
sort(temp.begin(), temp.end());
6+
int ans =0;
7+
for(int i=0;i<heights.size();i++){
8+
if(heights[i]!=temp[i]){
9+
ans++;
10+
}
11+
}
12+
return ans;
13+
}
14+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
string gcdOfStrings(string str1, string str2) {
4+
while(str1 != str2){
5+
int i=0;
6+
while(i < min(str1.length(), str2.length()) && str1[i] == str2[i]){
7+
i++;
8+
}
9+
if(i != str1.length() && i != str2.length()) return "";
10+
11+
if(str1.length() < str2.length()){
12+
str2 = str2.substr(i);
13+
continue;
14+
}
15+
if(str1.length() > str2.length()) str1 = str1.substr(i);
16+
}
17+
18+
return str1;
19+
}
20+
};

118.pascals-triangle.cpp

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<vector<int>> generate(int numRows) {
4+
vector<vector<int>> pascal;
5+
pascal.push_back({1});
6+
for(int i=0;i<numRows-1;i++){
7+
vector<int> row;
8+
row.push_back(1);
9+
for(int j=0;j<pascal[i].size()-1;j++){
10+
row.push_back(pascal[i][j]+pascal[i][j+1]);
11+
}
12+
row.push_back(1);
13+
pascal.push_back(row);
14+
}
15+
return pascal;
16+
}
17+
};

1187.print-foobar-alternately.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class FooBar {
2+
private:
3+
int n;
4+
mutex f;
5+
mutex b;
6+
7+
public:
8+
FooBar(int n) {
9+
this->n = n;
10+
b.lock();
11+
}
12+
13+
void foo(function<void()> printFoo) {
14+
15+
for (int i = 0; i < n; i++) {
16+
f.lock();
17+
// printFoo() outputs "foo". Do not change or remove this line.
18+
printFoo();
19+
b.unlock();
20+
}
21+
}
22+
23+
void bar(function<void()> printBar) {
24+
25+
for (int i = 0; i < n; i++) {
26+
b.lock();
27+
// printBar() outputs "bar". Do not change or remove this line.
28+
printBar();
29+
f.unlock();
30+
}
31+
}
32+
};

0 commit comments

Comments
 (0)