Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 75d5203

Browse files
authored
Merge branch 'neetcode-gh:main' into main
2 parents 9fd2c65 + 51bcb14 commit 75d5203

36 files changed

+743
-30
lines changed

cpp/1046-Last-Stone-Weight.cpp

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 lastStoneWeight(vector<int>& stones) {
4+
priority_queue<int, vector<int>> pq(stones.begin(), stones.end());
5+
pq.push(0);
6+
7+
while (pq.size()>=1) {
8+
int t1 = pq.top();
9+
pq.pop();
10+
if(pq.empty()) break;
11+
int t2 = pq.top();
12+
pq.pop();
13+
pq.push(abs(t1-t2));
14+
}
15+
16+
return pq.top();
17+
}
18+
};
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode() : val(0), next(nullptr) {}
7+
* ListNode(int x) : val(x), next(nullptr) {}
8+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
9+
* };
10+
*/
11+
class Solution {
12+
public:
13+
ListNode* removeNthFromEnd(ListNode* head, int n) {
14+
if(!head) return head;
15+
ListNode* f,*s,*prev;
16+
f=head;
17+
s=head;
18+
prev=nullptr;
19+
for(int i=0;i<n;i++)
20+
f=f->next;
21+
while(f)
22+
{
23+
prev=s;
24+
s=s->next;
25+
f=f->next;
26+
}
27+
if(prev)
28+
prev->next=s->next;
29+
else
30+
head=head->next;
31+
delete s;
32+
return head;
33+
}
34+
};

cpp/208-Implement-Trie.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
struct TrieNode {
2+
TrieNode* children[26];
3+
bool isWordEnd;
4+
5+
TrieNode() : isWordEnd(false) {
6+
for (int i = 0; i < 26; ++i)
7+
children[i] = nullptr;
8+
}
9+
};
10+
11+
class Trie {
12+
private:
13+
TrieNode* root;
14+
public:
15+
Trie() {
16+
root = new TrieNode;
17+
}
18+
19+
void insert(string word) {
20+
TrieNode* cur = root;
21+
22+
int idx;
23+
for (int i = 0; i < word.size(); ++i) {
24+
idx = word[i] - 'a';
25+
if (cur->children[idx] == nullptr)
26+
cur->children[idx] = new TrieNode;
27+
cur = cur->children[idx];
28+
}
29+
// mark the last node as end of a word
30+
cur->isWordEnd = true;
31+
}
32+
33+
bool search(string word) {
34+
TrieNode* cur = root;
35+
36+
int idx;
37+
for (int i = 0; i < word.size(); ++i) {
38+
idx = word[i] - 'a';
39+
if (cur->children[idx] == nullptr)
40+
return false;
41+
cur = cur->children[idx];
42+
}
43+
// check if the node is end of any word
44+
return cur->isWordEnd;
45+
}
46+
47+
bool startsWith(string prefix) {
48+
TrieNode* cur = root;
49+
50+
int idx;
51+
for (int i = 0; i < prefix.size(); ++i) {
52+
idx = prefix[i] - 'a';
53+
if (cur->children[idx] == nullptr)
54+
return false;
55+
cur = cur->children[idx];
56+
}
57+
// only need to check if the node exists
58+
return true;
59+
}
60+
};
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
struct TrieNode {
2+
TrieNode* children[26];
3+
bool isWordEnd;
4+
5+
TrieNode() : isWordEnd(false) {
6+
for (int i = 0; i < 26; ++i)
7+
children[i] = nullptr;
8+
}
9+
};
10+
11+
class WordDictionary {
12+
private:
13+
TrieNode* root;
14+
public:
15+
WordDictionary() {
16+
root = new TrieNode;
17+
}
18+
19+
void addWord(string word) {
20+
// simple trie insertion
21+
TrieNode* cur = root;
22+
23+
int idx;
24+
for (int i = 0; i < word.size(); ++i) {
25+
idx = word[i] - 'a';
26+
if (cur->children[idx] == nullptr)
27+
cur->children[idx] = new TrieNode;
28+
cur = cur->children[idx];
29+
}
30+
// mark the last node as end of a word
31+
cur->isWordEnd = true;
32+
}
33+
34+
bool recursiveSearch(const string &word, int curIdx, const TrieNode *node) {
35+
auto cur = node;
36+
37+
for (int i = curIdx; i < word.size(); ++i) {
38+
if (word[i] == '.') {
39+
// can match any character - backtracking is required
40+
for (int j = 0; j < 26; ++j) {
41+
if (cur->children[j] == nullptr) // skip non-existing characters
42+
continue;
43+
if (recursiveSearch(word, i + 1, cur->children[j])) // try and backtrack if fails
44+
return true;
45+
}
46+
// search with backtracking failed in all children
47+
return false;
48+
}
49+
else {
50+
// simple trie search
51+
int idx = word[i] - 'a';
52+
if (cur->children[idx] == nullptr)
53+
return false;
54+
cur = cur->children[idx];
55+
}
56+
}
57+
// check if the node is end of any word
58+
return cur->isWordEnd;
59+
}
60+
61+
bool search(string word) {
62+
return recursiveSearch(word, 0, root);
63+
}
64+
};

cpp/212-Word-Search-II.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
struct TrieNode {
2+
TrieNode* children[26];
3+
int wordEndCnt;
4+
string word;
5+
6+
TrieNode() : wordEndCnt(0), word("") {
7+
for (int i = 0; i < 26; ++i)
8+
children[i] = nullptr;
9+
}
10+
};
11+
12+
class Solution {
13+
public:
14+
void addWord(TrieNode* node, const string& word) {
15+
// simple trie insertion
16+
TrieNode* cur = node;
17+
18+
int idx;
19+
for (int i = 0; i < word.size(); ++i) {
20+
idx = word[i] - 'a';
21+
if (cur->children[idx] == nullptr)
22+
cur->children[idx] = new TrieNode;
23+
cur = cur->children[idx];
24+
}
25+
// increase the word end counter
26+
++cur->wordEndCnt;
27+
cur->word = word;
28+
}
29+
30+
void solve(vector<vector<char>>& board, int ROWS, int COLS, int r, int c, vector<string>& result, TrieNode* cur) {
31+
// boundary check
32+
if (r < 0 || r >= ROWS || c < 0 || c >= COLS)
33+
return;
34+
// visit check
35+
if (board[r][c] == '$')
36+
return;
37+
// existence check
38+
int idx = board[r][c] - 'a';
39+
if (cur->children[idx] == nullptr)
40+
return;
41+
cur = cur->children[idx];
42+
if (cur->wordEndCnt > 0) {
43+
// found word
44+
result.push_back(cur->word);
45+
--cur->wordEndCnt;
46+
}
47+
48+
// mark as visited and backtrack
49+
char origin = board[r][c];
50+
board[r][c] = '$';
51+
solve(board, ROWS, COLS, r + 1, c, result, cur);
52+
solve(board, ROWS, COLS, r - 1, c, result, cur);
53+
solve(board, ROWS, COLS, r, c + 1, result, cur);
54+
solve(board, ROWS, COLS, r, c - 1, result, cur);
55+
board[r][c] = origin;
56+
}
57+
58+
vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
59+
TrieNode* root = new TrieNode;
60+
for (auto w : words) {
61+
addWord(root, w);
62+
}
63+
int ROWS = board.size(), COLS = board[0].size();
64+
65+
vector<string> result;
66+
// check every cells
67+
for (int i = 0; i < ROWS; ++i)
68+
for (int j = 0; j < COLS; ++j)
69+
solve(board, ROWS, COLS, i, j, result, root);
70+
return result;
71+
}
72+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution {
2+
public:
3+
int findKthLargest(vector<int>& nums, int k) {
4+
priority_queue<int, vector<int>, greater<int>> pq(nums.begin(), nums.end());
5+
while (pq.size() > k)pq.pop();
6+
return pq.top();
7+
}
8+
};
File renamed without changes.
File renamed without changes.

cpp/543-Diameter-of-Binary-Tree.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
15+
int MaxHeight(TreeNode* root, int& res) {
16+
if (root == NULL) return 0;
17+
int l = MaxHeight(root->left, res);
18+
int r = MaxHeight(root->right, res);
19+
res = max(res, max(max(l, r), l + r));
20+
return 1 + max(l, r);
21+
}
22+
23+
int diameterOfBinaryTree(TreeNode* root) {
24+
int breadth = 0;
25+
MaxHeight(root, breadth);
26+
return breadth;
27+
}
28+
};
File renamed without changes.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class KthLargest {
2+
public:
3+
priority_queue<int, vector<int>, greater<int>> pq;
4+
int k;
5+
KthLargest(int k, vector<int>& nums) {
6+
this->k = k;
7+
for (auto a: nums) pq.push(a);
8+
while(pq.size()>k){
9+
pq.pop();
10+
}
11+
}
12+
13+
int add(int val) {
14+
pq.push(val);
15+
while(pq.size()>k){
16+
pq.pop();
17+
}
18+
return pq.top();
19+
}
20+
};
21+
22+
/**
23+
* Your KthLargest object will be instantiated and called as such:
24+
* KthLargest* obj = new KthLargest(k, nums);
25+
* int param_1 = obj->add(val);
26+
*/
File renamed without changes.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Solution {
2+
public:
3+
struct cords {
4+
int x, y;
5+
cords(vector<int> a) {
6+
this -> x = a[0];
7+
this -> y = a[1];
8+
}
9+
cords(int x, int y){
10+
this -> x = x;
11+
this -> y = y;
12+
}
13+
int dist(){
14+
return x*x+y*y;
15+
}
16+
};
17+
18+
struct compare {
19+
bool operator()(cords a, cords b) {
20+
return a.dist() < b.dist();
21+
}
22+
};
23+
24+
vector<vector<int>> kClosest(vector<vector<int>>& points, int k) {
25+
priority_queue<cords, vector<cords>, compare> pq;
26+
for (auto a: points) {
27+
pq.push(cords(a));
28+
}
29+
30+
while (pq.size() > k) pq.pop();
31+
32+
vector<vector<int>> res;
33+
while (!pq.empty()) {
34+
res.push_back({pq.top().x, pq.top().y});
35+
pq.pop();
36+
}
37+
return res;
38+
}
39+
};

java/131-Palindrome-Partitioning.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
public class Solution {
2+
public List<List<String>> partition(String s) {
3+
List<List<String>> res = new ArrayList<List<String>>();
4+
if (s.equals("")) {
5+
res.add(new ArrayList<String>());
6+
return res;
7+
}
8+
for (int i = 0; i < s.length(); i++) {
9+
if (isPalindrome(s, i + 1)) {
10+
for (List<String> list : partition(s.substring(i+1))) {
11+
list.add(0, s.substring(0, i + 1));
12+
res.add(list);
13+
}
14+
}
15+
}
16+
return res;
17+
}
18+
19+
public boolean isPalindrome(String s, int n) {
20+
for (int i = 0; i < n / 2; i++) {
21+
if (s.charAt(i) != s.charAt(n - i - 1))
22+
return false;
23+
}
24+
return true;
25+
}
26+
}

0 commit comments

Comments
 (0)