Skip to content

Commit d20fcd2

Browse files
committed
🚀 24-Aug-2020
1 parent f5708d8 commit d20fcd2

14 files changed

+850
-0
lines changed

competitive programming/leetcode/1. Two Sum.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,15 @@ class Solution {
6363
return res;
6464
}
6565
};
66+
67+
68+
69+
70+
/*
71+
Approach1:
72+
The brute force approach is simple.
73+
Loop through each element xx and find if there is another value that equals to target - xtarget−x.
74+
75+
Approach2:
76+
Sort the array, and use the two pointer technique.
77+
*/
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
Implement the StreamChecker class as follows:
2+
3+
StreamChecker(words): Constructor, init the data structure with the given words.
4+
query(letter): returns true if and only if for some k >= 1, the last k characters queried (in order from oldest to newest, including this letter just queried) spell one of the words in the given list.
5+
6+
7+
Example:
8+
9+
StreamChecker streamChecker = new StreamChecker(["cd","f","kl"]); // init the dictionary.
10+
streamChecker.query('a'); // return false
11+
streamChecker.query('b'); // return false
12+
streamChecker.query('c'); // return false
13+
streamChecker.query('d'); // return true, because 'cd' is in the wordlist
14+
streamChecker.query('e'); // return false
15+
streamChecker.query('f'); // return true, because 'f' is in the wordlist
16+
streamChecker.query('g'); // return false
17+
streamChecker.query('h'); // return false
18+
streamChecker.query('i'); // return false
19+
streamChecker.query('j'); // return false
20+
streamChecker.query('k'); // return false
21+
streamChecker.query('l'); // return true, because 'kl' is in the wordlist
22+
23+
24+
Note:
25+
26+
1 <= words.length <= 2000
27+
1 <= words[i].length <= 2000
28+
Words will only consist of lowercase English letters.
29+
Queries will only consist of lowercase English letters.
30+
The number of queries is at most 40000.
31+
32+
33+
34+
35+
36+
37+
38+
struct TrieNode {
39+
bool ew; // end of word
40+
struct TrieNode* child[26];
41+
};
42+
43+
struct TrieNode* createNode(){
44+
struct TrieNode* newNode=new TrieNode;
45+
newNode->ew=false;
46+
for(int i=0;i<26;i++)
47+
newNode->child[i]=NULL;
48+
return newNode;
49+
}
50+
51+
void insert(struct TrieNode* root, string word){
52+
struct TrieNode* cur=root;
53+
int wordLen=word.length();
54+
for(int i=wordLen-1;i>=0;i--){
55+
int index=word[i]-'a';
56+
// create new node if not already exist
57+
if(cur->child[index]==NULL)
58+
cur->child[index]=createNode();
59+
// point to new node
60+
cur=cur->child[index];
61+
}
62+
cur->ew=true; // end of word
63+
}
64+
65+
66+
67+
class StreamChecker {
68+
public:
69+
struct TrieNode* root= createNode();
70+
string stream="";
71+
72+
73+
StreamChecker(vector<string>& words) {
74+
75+
int n=words.size();
76+
for(int i=0;i<n;i++)
77+
insert(root, words[i]);
78+
79+
}
80+
81+
bool query(char letter) {
82+
stream+=letter;
83+
TrieNode* cur=root;
84+
int sLen=stream.length();
85+
for(int i=sLen-1; i>=0;i--){
86+
int index=stream[i]-'a';
87+
if(cur->ew==true) return true;
88+
if(cur->child[index]==NULL) return false;
89+
cur=cur->child[index];
90+
}
91+
return (cur!=NULL && cur->ew);
92+
}
93+
};
94+
95+
/**
96+
* Your StreamChecker object will be instantiated and called as such:
97+
* StreamChecker* obj = new StreamChecker(words);
98+
* bool param_1 = obj->query(letter);
99+
*/
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.
2+
3+
Your algorithm should run in O(n) complexity.
4+
5+
Example:
6+
7+
Input: [100, 4, 200, 1, 3, 2]
8+
Output: 4
9+
Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4.
10+
11+
12+
13+
14+
15+
16+
17+
18+
19+
20+
21+
22+
// TC : O(nlgn)
23+
24+
class Solution {
25+
public:
26+
int longestConsecutive(vector<int>& nums) {
27+
int n=nums.size();
28+
if(n==0) return 0;
29+
sort(nums.begin(), nums.end());
30+
int res=1, cnt=1;
31+
for(int i=0;i<n-1;i++){
32+
if(nums[i+1]==nums[i]) continue;
33+
if(abs(nums[i+1]-nums[i]==1)){
34+
cnt++;
35+
} else cnt=1;
36+
if(cnt>res) res=cnt;
37+
}
38+
return res;
39+
}
40+
};
41+
42+
43+
44+
45+
46+
/*
47+
Brute Force
48+
Considers each number in nums, attempting to count as high as possible from that number using only numbers in nums.
49+
After it counts too high (i.e. currentNum refers to a number that nums does not contain),
50+
it records the length of the sequence if it is larger than the current best.
51+
*/
52+
53+
// TLE
54+
class Solution {
55+
bool arrayContains(vector<int> nums, int num) {
56+
int n=nums.size();
57+
for (int i = 0; i < n; i++) {
58+
if (nums[i] == num) {
59+
return true;
60+
}
61+
}
62+
return false;
63+
}
64+
public:
65+
int longestConsecutive(vector<int> &nums) {
66+
int longestStreak = 0;
67+
68+
for (int num : nums) {
69+
int currentNum = num;
70+
int currentStreak = 1;
71+
72+
while (arrayContains(nums, currentNum + 1)) {
73+
currentNum += 1;
74+
currentStreak += 1;
75+
}
76+
longestStreak = max(longestStreak, currentStreak);
77+
}
78+
return longestStreak;
79+
}
80+
};
81+
82+
83+
84+
85+
86+
// O(N)
87+
88+
class Solution {
89+
public:
90+
int longestConsecutive(vector<int> &nums) {
91+
int longestStreak = 0;
92+
set<int> s;
93+
// Hash all the array elements
94+
for(int num: nums){
95+
s.insert(num);
96+
}
97+
98+
// check each possible sequence from the start then update optimal length
99+
for (int num : nums) {
100+
int currentNum = num;
101+
int currentStreak = 1;
102+
// if current element is the starting element of a sequence
103+
if(s.find(currentNum-1)==s.end()){
104+
// Then check for next elements in the sequence
105+
while (s.find(currentNum+1)!=s.end()) {
106+
currentNum += 1;
107+
currentStreak += 1;
108+
}
109+
}
110+
longestStreak = max(longestStreak, currentStreak);
111+
}
112+
return longestStreak;
113+
}
114+
};
115+
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.
2+
3+
Return a deep copy of the list.
4+
5+
The Linked List is represented in the input/output as a list of n nodes. Each node is represented as a pair of [val, random_index] where:
6+
7+
val: an integer representing Node.val
8+
random_index: the index of the node (range from 0 to n-1) where random pointer points to, or null if it does not point to any node.
9+
10+
11+
Example 1:
12+
13+
14+
Input: head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
15+
Output: [[7,null],[13,0],[11,4],[10,2],[1,0]]
16+
Example 2:
17+
18+
19+
Input: head = [[1,1],[2,1]]
20+
Output: [[1,1],[2,1]]
21+
Example 3:
22+
23+
24+
25+
Input: head = [[3,null],[3,0],[3,null]]
26+
Output: [[3,null],[3,0],[3,null]]
27+
Example 4:
28+
29+
Input: head = []
30+
Output: []
31+
Explanation: Given linked list is empty (null pointer), so return null.
32+
33+
34+
Constraints:
35+
36+
-10000 <= Node.val <= 10000
37+
Node.random is null or pointing to a node in the linked list.
38+
Number of Nodes will not exceed 1000.
39+
40+
41+
42+
43+
44+
45+
46+
47+
48+
49+
50+
/*
51+
// Definition for a Node.
52+
class Node {
53+
public:
54+
int val;
55+
Node* next;
56+
Node* random;
57+
58+
Node(int _val) {
59+
val = _val;
60+
next = NULL;
61+
random = NULL;
62+
}
63+
};
64+
*/
65+
66+
class Solution {
67+
public:
68+
Node* copyRandomList(Node* head) {
69+
if(!head) return head;
70+
Node *curr=head;
71+
while(curr!=NULL){
72+
Node *tmp=curr->next;
73+
curr->next=new Node(curr->val);
74+
curr->next->next=tmp;
75+
curr=tmp;
76+
}
77+
curr=head;
78+
while(curr!=NULL){
79+
if(curr->next!=NULL){
80+
curr->next->random=curr->random ? curr->random->next : curr->random;
81+
}
82+
curr=curr->next ? curr->next->next : curr->next;
83+
}
84+
Node *original=head, *copy=head->next;
85+
Node* tmp=copy; // save start of the copied list
86+
while(original && copy){
87+
original->next=original->next ? original->next->next: original->next;
88+
copy->next=copy->next ? copy->next->next : copy->next;
89+
original=original->next;
90+
copy=copy->next;
91+
}
92+
return tmp;
93+
}
94+
};
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
Given a string s, the power of the string is the maximum length of a non-empty substring that contains only one unique character.
2+
3+
Return the power of the string.
4+
5+
6+
7+
Example 1:
8+
9+
Input: s = "leetcode"
10+
Output: 2
11+
Explanation: The substring "ee" is of length 2 with the character 'e' only.
12+
Example 2:
13+
14+
Input: s = "abbcccddddeeeeedcba"
15+
Output: 5
16+
Explanation: The substring "eeeee" is of length 5 with the character 'e' only.
17+
Example 3:
18+
19+
Input: s = "triplepillooooow"
20+
Output: 5
21+
Example 4:
22+
23+
Input: s = "hooraaaaaaaaaaay"
24+
Output: 11
25+
Example 5:
26+
27+
Input: s = "tourist"
28+
Output: 1
29+
30+
31+
Constraints:
32+
33+
1 <= s.length <= 500
34+
s contains only lowercase English letters.
35+
36+
37+
38+
39+
40+
41+
42+
43+
44+
45+
class Solution {
46+
public:
47+
int maxPower(string s) {
48+
int n=s.length();
49+
int maxi=1, cnt=1;
50+
for(int i=0;i<n-1;i++){
51+
if(s[i]==s[i+1]){
52+
cnt++;
53+
} else cnt=1;
54+
if(cnt>maxi) maxi=cnt;
55+
}
56+
return maxi;
57+
}
58+
};

0 commit comments

Comments
 (0)