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

+27
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

+13
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
+22
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+
};
+22
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

+18
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+
};
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+
};
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+
};
+18
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+
};
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+
};
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+
};

30-days-of-code-may/09.townJudege.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
int findJudge(int N, vector<vector<int>>& trust) {
4+
vector<int> count(N + 1, 0);
5+
for (auto& t : trust)
6+
count[t[0]]--, count[t[1]]++;
7+
for (int i = 1; i <= N; ++i) {
8+
if (count[i] == N - 1) return i;
9+
}
10+
return -1;
11+
}
12+
13+
};

30-days-of-code-may/10.floodFill.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
4+
if (image[sr][sc] != newColor)
5+
dfs(image, sr, sc, image[sr][sc], newColor);
6+
return image;
7+
}
8+
9+
private:
10+
void dfs(vector<vector<int>>& image, int i, int j, int c0, int c1) {
11+
if (i < 0 || j < 0 || i >= image.size() || j >= image[0].size() || image[i][j] != c0) return;
12+
image[i][j] = c1;
13+
dfs(image, i, j - 1, c0, c1);
14+
dfs(image, i, j + 1, c0, c1);
15+
dfs(image, i - 1, j, c0, c1);
16+
dfs(image, i + 1, j, c0, c1);
17+
}
18+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
int singleNonDuplicate(vector<int>& nums) {
4+
5+
int start = 0, end = nums.size() - 1, mid;
6+
while(start < end){
7+
mid = start + (end - start) / 2;
8+
if(mid % 2 == 0){
9+
if(nums[mid] == nums[mid+1]) start = mid + 2;
10+
else if(nums[mid] == nums[mid-1]) end = mid - 2;
11+
else return nums[mid];
12+
}
13+
else{
14+
if(nums[mid] == nums[mid-1]) start = mid + 1;
15+
if(nums[mid] == nums[mid+1]) end = mid - 1;
16+
}
17+
}
18+
return nums[start];
19+
}
20+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
class Trie {
2+
3+
private:
4+
Trie *next[26] = {};
5+
bool isWord = false;
6+
7+
8+
public:
9+
/** Initialize your data structure here. */
10+
Trie() {}
11+
12+
/** Inserts a word into the trie. */
13+
void insert(string word) {
14+
Trie *node = this;
15+
for(auto ch : word){
16+
ch -= 'a';
17+
18+
if(!node->next[ch]){
19+
node->next[ch] = new Trie();
20+
}
21+
node = node->next[ch];
22+
}
23+
node->isWord = true;
24+
}
25+
26+
/** Returns if the word is in the trie. */
27+
bool search(string word) {
28+
Trie *node = this;
29+
for(auto ch : word){
30+
ch -= 'a';
31+
32+
if(!node->next[ch]) return false;
33+
node = node->next[ch];
34+
}
35+
return node->isWord;
36+
}
37+
38+
/** Returns if there is any word in the trie that starts with the given prefix. */
39+
bool startsWith(string prefix) {
40+
41+
Trie *node = this;
42+
for(auto ch : prefix){
43+
ch -= 'a';
44+
if(!node->next[ch]) return false;
45+
node = node->next[ch];
46+
}
47+
return true;
48+
}
49+
};
50+
51+
/**
52+
* Your Trie object will be instantiated and called as such:
53+
* Trie* obj = new Trie();
54+
* obj->insert(word);
55+
* bool param_2 = obj->search(word);
56+
* bool param_3 = obj->startsWith(prefix);
57+
*/

0 commit comments

Comments
 (0)