Skip to content

Commit a448e2d

Browse files
authored
Merge pull request #2 from thepushkarp/MayWeek2
Add May Week 2
2 parents 2565ad1 + eb0a7d3 commit a448e2d

File tree

7 files changed

+195
-0
lines changed

7 files changed

+195
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
int findJudge(int N, vector<vector<int>>& trust) {
4+
int sum = N*(N+1)/2;
5+
int trusts[N + 1], trustedBy[N + 1];
6+
for (int i = 1; i < N + 1; i++) {
7+
trusts[i] = 0;
8+
trustedBy[i] = 0;
9+
}
10+
for (int i = 0; i < trust.size(); i++) {
11+
trusts[trust[i][0]] += trust[i][1];
12+
trustedBy[trust[i][1]] += trust[i][0];
13+
}
14+
for (int i = 1; i < N + 1; i++) {
15+
if (trusts[i] == 0) {
16+
if (sum - trustedBy[i] == i) {
17+
return i;
18+
}
19+
}
20+
}
21+
return -1;
22+
}
23+
};
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColour) {
4+
int rowSize = image.size();
5+
int colSize = image[0].size();
6+
int prevColour = image[sr][sc];
7+
vector<vector<int>> visited(rowSize, vector<int>(colSize, 0));
8+
queue<pair<int, int>> pixelToColour;
9+
pixelToColour.push(make_pair(sr, sc));
10+
while(pixelToColour.size() > 0) {
11+
int r = pixelToColour.front().first;
12+
int c = pixelToColour.front().second;
13+
image[r][c] = newColour;
14+
visited[r][c] = 1;
15+
if (r > 0) {
16+
if (image[r-1][c] == prevColour and !visited[r-1][c]) {
17+
pixelToColour.push(make_pair(r-1, c));
18+
}
19+
}
20+
if (r < rowSize - 1) {
21+
if (image[r+1][c] == prevColour and !visited[r+1][c]) {
22+
pixelToColour.push(make_pair(r+1, c));
23+
}
24+
}
25+
if (c > 0) {
26+
if (image[r][c-1] == prevColour and !visited[r][c-1]) {
27+
pixelToColour.push(make_pair(r, c-1));
28+
}
29+
}
30+
if (c < colSize - 1) {
31+
if (image[r][c+1] == prevColour and !visited[r][c+1]) {
32+
pixelToColour.push(make_pair(r, c+1));
33+
}
34+
}
35+
pixelToColour.pop();
36+
}
37+
return image;
38+
}
39+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public:
3+
int singleNonDuplicate(vector<int>& nums) {
4+
int ans = 0;
5+
for (auto i : nums) {
6+
ans^=i;
7+
}
8+
return ans;
9+
}
10+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public:
3+
string stripLeadingZeros(string str) {
4+
int i = 0;
5+
for (; i < str.size() - 1; i++) {
6+
if (str[i] != '0') break;
7+
}
8+
str.erase(str.begin(), str.begin() + i);
9+
return str;
10+
}
11+
12+
string removeKdigits(string num, int k) {
13+
if (k == num.size()) return "0";
14+
15+
int digitsToKeep = num.size() - k;
16+
17+
string ans = "";
18+
int lastIndex = -1;
19+
while (digitsToKeep) {
20+
int mini = 10;
21+
for (int i = lastIndex + 1; i <= num.size() - digitsToKeep; i++) {
22+
if (num[i] - '0' < mini) {
23+
mini = num[i] - '0';
24+
lastIndex = i;
25+
}
26+
}
27+
ans+=('0' + mini);
28+
digitsToKeep--;
29+
}
30+
return stripLeadingZeros(ans);
31+
}
32+
};
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
class Trie {
2+
public:
3+
/** Initialize your data structure here. */
4+
static const int ALPHA_SIZE = 26;
5+
Trie* character[ALPHA_SIZE];
6+
bool endOfWord;
7+
Trie() {
8+
this -> endOfWord = false;
9+
for (int i = 0; i < ALPHA_SIZE; i++) {
10+
this -> character[i] = nullptr;
11+
}
12+
}
13+
14+
/** Inserts a word into the trie. */
15+
void insert(string word) {
16+
Trie* curr = this;
17+
for (int i = 0; i < word.size(); i++) {
18+
if (curr -> character[word[i] - 'a'] == nullptr) {
19+
curr -> character[word[i] - 'a'] = new Trie();
20+
}
21+
curr = curr -> character[word[i] - 'a'];
22+
}
23+
curr -> endOfWord = true;
24+
}
25+
26+
/** Returns if the word is in the trie. */
27+
bool search(string word) {
28+
if (this == nullptr) {
29+
return false;
30+
}
31+
Trie* curr = this;
32+
for (int i = 0; i < word.size(); i++) {
33+
curr = curr -> character[word[i] - 'a'];
34+
if (curr == nullptr) {
35+
return false;
36+
}
37+
}
38+
return curr -> endOfWord;
39+
}
40+
41+
/** Returns if there is any word in the trie that starts with the given prefix. */
42+
bool startsWith(string prefix) {
43+
if (this == nullptr) {
44+
return false;
45+
}
46+
Trie* curr = this;
47+
for (int i = 0; i < prefix.size(); i++) {
48+
curr = curr -> character[prefix[i] - 'a'];
49+
if (curr == nullptr) {
50+
return false;
51+
}
52+
}
53+
return true;
54+
}
55+
};
56+
57+
/**
58+
* Your Trie object will be instantiated and called as such:
59+
* Trie* obj = new Trie();
60+
* obj->insert(word);
61+
* bool param_2 = obj->search(word);
62+
* bool param_3 = obj->startsWith(prefix);
63+
*/
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
float epsilon = 0.005f;
4+
bool checkStraightLine(vector<vector<int>>& coordinates) {
5+
if (coordinates.size() == 2) {
6+
return true;
7+
}
8+
bool colinear = true;
9+
int x1 = coordinates[0][0], y1 = coordinates[0][1];
10+
int x2 = coordinates[1][0], y2 = coordinates[1][1];
11+
for (int i = 2; i < coordinates.size(); i++) {
12+
int x = coordinates[i][0], y = coordinates[i][1];
13+
if ((x - x1)*(y - y2) != (x - x2)*(y - y1))
14+
colinear = false;
15+
}
16+
return colinear;
17+
}
18+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public:
3+
bool isPerfectSquare(int num) {
4+
long i = 1;
5+
while(i*i < num) {
6+
i++;
7+
}
8+
return i*i == num;
9+
}
10+
};

0 commit comments

Comments
 (0)