Skip to content

Commit 739256c

Browse files
Updated codebase and added day23 solution
1 parent 44139f5 commit 739256c

File tree

249 files changed

+8507
-8479
lines changed

Some content is hidden

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

249 files changed

+8507
-8479
lines changed

01matrix.py

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +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)
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)
2727
return A

23.intervalList.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ class Solution {
1010
}
1111
return res;
1212
}
13-
};
13+
};x
Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +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-
}
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+
}
2222
};
Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +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-
}
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+
}
2222
};
Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +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-
}
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+
}
1818
};
Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +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-
}
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+
}
4242
};
Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +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-
}
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+
}
1818
};
Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +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-
}
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+
}
1818
};

0 commit comments

Comments
 (0)