Skip to content

Commit 8e440de

Browse files
Updated repository structure
1 parent fbe1409 commit 8e440de

File tree

273 files changed

+9557
-0
lines changed

Some content is hidden

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

273 files changed

+9557
-0
lines changed

01matrix.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution:
2+
def updateMatrix(self, A: List[List[int]]) -> List[List[int]]:
3+
def validNeighbours(i, j, d):
4+
for dx, dy in (1, 0), (-1, 0), (0, 1), (0, -1):
5+
ni, nj, dn = i + dx, j + dy, d + 1
6+
if 0 <= ni < len(A) and 0 <= nj < len(A[0]):
7+
yield ni, nj, dn
8+
9+
def bfs(i, j):
10+
dist = collections.defaultdict(lambda: float('inf'))
11+
queue = collections.deque([(i, j, 0)])
12+
while queue:
13+
i, j, d = queue.popleft()
14+
if A[i][j] == 0:
15+
return d
16+
for ni, nj, dn in validNeighbours(i, j, d):
17+
if dn < dist[(ni, nj)]:
18+
dist[(ni, nj)] = dn
19+
queue.append((ni, nj, dn))
20+
return A[i][j]
21+
22+
23+
for i in range(len(A)):
24+
for j in range(len(A[0])):
25+
if A[i][j] == 1:
26+
A[i][j] = bfs(i, j)
27+
return A

23.intervalList.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> intervalIntersection(vector<vector<int>>& A, vector<vector<int>>& B) {
4+
vector<vector<int>> res;
5+
for(int i = 0, j = 0; i < A.size() && j < B.size();) {
6+
int lo = max(A[i][0], B[j][0]), hi = min(A[i][1], B[j][1]);
7+
if(lo <= hi) res.push_back({lo, hi});
8+
if(hi == A[i][1]) i++;
9+
else j++;
10+
}
11+
return res;
12+
}
13+
};x
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// The API isBadVersion is defined for you.
2+
// bool isBadVersion(int version);
3+
4+
class Solution {
5+
public:
6+
int firstBadVersion(int n) {
7+
8+
int start = 1, end = n, mid;
9+
10+
while(start < end){
11+
12+
mid = start + (end - start)/2;
13+
if(isBadVersion(mid)){
14+
end = mid;
15+
}
16+
else{
17+
start = mid + 1;
18+
}
19+
}
20+
return start;
21+
}
22+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public:
3+
int numJewelsInStones(string J, string S) {
4+
if(J.size() == 0 or S.size() == 0) return 0;
5+
6+
map<char, int> stones;
7+
8+
for(int i=0; i<S.size();i++){
9+
if(stones.find(S[i]) == stones.end())
10+
stones[S[i]] = 1;
11+
else
12+
stones[S[i]]++;
13+
}
14+
int c = 0;
15+
for(char j:J){
16+
if(stones.find(j) != stones.end()){
17+
c+=stones[j];
18+
}
19+
}
20+
return c;
21+
}
22+
};

30-days-of-code-may/03.ransomNote.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
class Solution {
3+
public:
4+
bool canConstruct(string ransomNote, string magazine) {
5+
6+
map<int,int> mp;
7+
8+
for(char m : magazine)
9+
mp[m-'a']++;
10+
11+
for(char r : ransomNote){
12+
if(--mp[r-'a'] < 0)
13+
return false;
14+
15+
}
16+
return true;
17+
}
18+
};
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class Solution {
2+
public:
3+
int findComplement(int num) {
4+
5+
6+
if(num == 0) return 0;
7+
vector<int> bits;
8+
9+
while(num != 0){
10+
bits.push_back(num % 2);
11+
num /= 2;
12+
}
13+
//for(auto i : bits)cout<<i<<" ";
14+
//cout<<"\n";
15+
16+
reverse(bits.begin(), bits.end());
17+
18+
//for(auto i : bits)cout<<i<<" ";
19+
vector<int> complement;
20+
for(auto i : bits){
21+
if(i == 0)
22+
complement.push_back(1);
23+
else
24+
complement.push_back(0);
25+
}
26+
27+
//cout<<"\n";
28+
//for(auto i : complement)cout<<i<<" ";
29+
30+
int sz = complement.size()- 1;
31+
unsigned int complement_no = 0;
32+
for(int i=sz; i>=0; i--){
33+
//cout<<"sz-i: "<<sz-i<<"\t";
34+
int expr = (complement[i] * pow(2, (sz- i)));
35+
//cout<<expr<<"\t\n";
36+
complement_no += expr;
37+
}
38+
39+
//cout<<complement_no<<"\n";
40+
return complement_no;
41+
}
42+
};
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 firstUniqChar(string s) {
4+
5+
if(s.size() == 0) return -1;
6+
7+
map<char, int> mp;
8+
for(auto ch : s)
9+
mp[ch]++;
10+
11+
for(int i=0; i<s.size(); i++){
12+
if(mp[s[i]] == 1){
13+
return i;
14+
}
15+
}
16+
return -1;
17+
}
18+
};
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 majorityElement(vector<int>& nums) {
4+
if(nums.size() == 0) return 0;
5+
6+
map<int, int> hash;
7+
for(auto num : nums)
8+
hash[num]++;
9+
int sz = nums.size();
10+
11+
for(auto const& p : hash){
12+
if(p.second > sz/2){
13+
return p.first;
14+
}
15+
}
16+
return -1;
17+
}
18+
};
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(int x) : val(x), left(NULL), right(NULL) {}
8+
* };
9+
*/
10+
class Solution {
11+
public:
12+
int parent_x = -1, parent_y = -1, depth_x = -1, depth_y = -1, x, y;
13+
14+
void findDepthAndCheckParent(TreeNode* root, int depth) {
15+
if(root == NULL) return;
16+
if(root->left && root->left->val == x || root->right && root->right->val == x) parent_x = root->val;
17+
18+
if(root->left && root->left->val == y || root->right && root->right->val == y) parent_y = root->val;
19+
if(root->val == x) depth_x = depth;
20+
if(root->val == y) depth_y = depth;
21+
22+
findDepthAndCheckParent(root->left, depth + 1);
23+
findDepthAndCheckParent(root->right, depth + 1);
24+
}
25+
26+
bool isCousins(TreeNode* root, int x, int y) {
27+
if(root == NULL) return false;
28+
this->x = x, this->y = y;
29+
findDepthAndCheckParent(root, 0);
30+
return (depth_x == depth_y) && (parent_x != parent_y);
31+
32+
}
33+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
bool checkStraightLine(vector<vector<int>>& coordinates) {
4+
5+
if(coordinates.size() <= 2) return true;
6+
int x1 = coordinates[0][0];
7+
int y1 = coordinates[0][1];
8+
int x2 = coordinates[1][0];
9+
int y2 = coordinates[1][1];
10+
for(int i=2;i<coordinates.size();i++){
11+
int x = coordinates[i][0];
12+
int y = coordinates[i][1];
13+
if((y2 - y1) * (x1 - x) != (y1 - y) * (x2 - x1))
14+
return false;
15+
}
16+
return true;
17+
18+
}
19+
};

0 commit comments

Comments
 (0)