Skip to content

Commit 77435fb

Browse files
committed
🚀 04-Sep-2020
1 parent c88cb67 commit 77435fb

8 files changed

+449
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
void countDistinct(int arr[], int k, int n){
5+
unordered_map<int, int> m;
6+
int dist_count = 0;
7+
8+
// Traverse the first window and store count of every element in hash map
9+
for(int i = 0; i < k; i++){
10+
if(m[arr[i]] == 0)
11+
dist_count++;
12+
m[arr[i]]++;
13+
}
14+
15+
// Print count of first window
16+
cout<<dist_count<<" ";
17+
18+
// Traverse through the remaining array
19+
for (int i = k; i < n; i++){
20+
// Remove first element of previous window
21+
// If there was only one occurrence, then reduce distinct count
22+
if(m[arr[i - k]] == 1)
23+
dist_count--;
24+
25+
// reduce count of the removed element
26+
m[arr[i - k]]--;
27+
28+
// Add new element of current window
29+
// If this element appears first time, increment distinct element count
30+
31+
if (m[arr[i]] == 0)
32+
dist_count++;
33+
34+
m[arr[i]]++;
35+
36+
// Print count of current window
37+
cout<<dist_count<<" ";
38+
}
39+
}
40+
41+
int main(){
42+
int arr[] = { 1, 2, 1, 3, 4, 2, 3 };
43+
int size = sizeof(arr) / sizeof(arr[0]);
44+
int k = 4;
45+
countDistinct(arr, k, size);
46+
47+
return 0;
48+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
The key here is to double the string, that is, append the string to itself.
3+
In this way, the pattern would be duplicated.
4+
On removing the first and the last characters,
5+
if there exists some pattern,
6+
we would still be able to find the original string somewhere in the middle,
7+
taking some characters from the first half and some from the second half.
8+
9+
For example,
10+
11+
Example 1.
12+
13+
s = "abab"
14+
s+s = "abababab"
15+
16+
On removing the first and the last characters, we get:
17+
(s+s).substr(1, 2*s.size()-2) = "bababa"
18+
19+
This new string, "bababa" still contains the original string, "abab".
20+
Thus there exists some repeated pattern in the original string itself.
21+
Example 2.
22+
23+
s = "aba"
24+
s+s = "abaaba"
25+
26+
On removing the first and the last characters, we get:
27+
(s+s).substr(1, 2*s.size()-2) = "baab"
28+
29+
This new string, "baab" does not contain the original string, "aba".
30+
This implies that there does not exist any pattern in the original string itself.
31+
32+
33+
Source: https://leetcode.com/problems/repeated-substring-pattern/discuss/826135/C%2B%2B-O(N)-time-or-One-liner-without-KMP-Explained-or-Beats-99
34+
*/
35+
36+
37+
38+
39+
40+
41+
class Solution {
42+
public:
43+
bool repeatedSubstringPattern(string s) {
44+
return (s+s).substr(1, 2*s.length()-2).find(s)!=-1;
45+
}
46+
};
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
Find the kth largest element in an unsorted array.
2+
Note that it is the kth largest element in the sorted order, not the kth distinct element.
3+
4+
Example 1:
5+
6+
Input: [3,2,1,5,6,4] and k = 2
7+
Output: 5
8+
Example 2:
9+
10+
Input: [3,2,3,1,2,4,5,5,6] and k = 4
11+
Output: 4
12+
Note:
13+
You may assume k is always valid, 1 ≤ k ≤ array's length.
14+
15+
16+
17+
18+
19+
20+
21+
class Solution {
22+
public:
23+
int findKthLargest(vector<int>& nums, int k) {
24+
priority_queue<int, vector<int>, greater<int> > q; // min heap
25+
26+
if(nums.size()<k) return -1;
27+
for(auto &num: nums){
28+
q.push(num);
29+
if(q.size()>k) q.pop();
30+
}
31+
32+
return q.top();
33+
}
34+
};
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position. Return the max sliding window.
2+
3+
Follow up:
4+
Could you solve it in linear time?
5+
6+
Example:
7+
8+
Input: nums = [1,3,-1,-3,5,3,6,7], and k = 3
9+
Output: [3,3,5,5,6,7]
10+
Explanation:
11+
12+
Window position Max
13+
--------------- -----
14+
[1 3 -1] -3 5 3 6 7 3
15+
1 [3 -1 -3] 5 3 6 7 3
16+
1 3 [-1 -3 5] 3 6 7 5
17+
1 3 -1 [-3 5 3] 6 7 5
18+
1 3 -1 -3 [5 3 6] 7 6
19+
1 3 -1 -3 5 [3 6 7] 7
20+
21+
22+
Constraints:
23+
24+
1 <= nums.length <= 10^5
25+
-10^4 <= nums[i] <= 10^4
26+
1 <= k <= nums.length
27+
28+
29+
30+
31+
32+
33+
34+
35+
36+
class Solution {
37+
public:
38+
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
39+
int n=nums.size();
40+
vector<int> res;
41+
deque<int> q;
42+
43+
for(int i=0;i<k;i++){
44+
while(!q.empty() && nums[i]>=nums[q.back()])
45+
q.pop_back(); // Remove from rear
46+
47+
q.push_back(i); // Add new element at rear of queue
48+
}
49+
50+
for(int i=k;i<n;i++){
51+
res.push_back(nums[q.front()]);
52+
53+
// Remove the elements which are out of this window
54+
while(!q.empty() && q.front()<=i-k)
55+
q.pop_front();
56+
57+
while(!q.empty() && nums[i]>=nums[q.back()])
58+
q.pop_back(); // Remove from rear
59+
60+
q.push_back(i);
61+
}
62+
// Adding maximum element of last window
63+
res.push_back(nums[q.front()]);
64+
65+
return res;
66+
}
67+
};
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
Median is the middle value in an ordered integer list.
2+
If the size of the list is even, there is no middle value.
3+
So the median is the mean of the two middle value.
4+
5+
For example,
6+
[2,3,4], the median is 3
7+
8+
[2,3], the median is (2 + 3) / 2 = 2.5
9+
10+
Design a data structure that supports the following two operations:
11+
12+
void addNum(int num) - Add a integer number from the data stream to the data structure.
13+
double findMedian() - Return the median of all elements so far.
14+
15+
16+
Example:
17+
18+
addNum(1)
19+
addNum(2)
20+
findMedian() -> 1.5
21+
addNum(3)
22+
findMedian() -> 2
23+
24+
25+
Follow up:
26+
27+
If all integer numbers from the stream are between 0 and 100, how would you optimize it?
28+
If 99% of all integer numbers from the stream are between 0 and 100, how would you optimize it?
29+
30+
31+
32+
33+
34+
35+
36+
37+
class MedianFinder {
38+
public:
39+
40+
priority_queue<int> q1; // max heap
41+
priority_queue<int, vector<int>, greater<int> > q2; // min heap
42+
43+
/** initialize your data structure here. */
44+
MedianFinder() {
45+
46+
}
47+
48+
void addNum(int num) {
49+
if(q1.empty() || q1.top()>num)
50+
q1.push(num);
51+
else q2.push(num);
52+
53+
if(q1.size()>q2.size()+1){
54+
q2.push(q1.top());
55+
q1.pop();
56+
} else if(q2.size()>q1.size()+1){
57+
q1.push(q2.top());
58+
q2.pop();
59+
}
60+
61+
}
62+
63+
double findMedian() {
64+
int q1size=q1.size();
65+
int q2size=q2.size();
66+
if(q1size>q2size) return q1.top();
67+
else if(q2size>q1size)return q2.top();
68+
else return (double)(q1.top()+q2.top())/2;
69+
}
70+
};
71+
72+
/**
73+
* Your MedianFinder object will be instantiated and called as such:
74+
* MedianFinder* obj = new MedianFinder();
75+
* obj->addNum(num);
76+
* double param_2 = obj->findMedian();
77+
*/
78+
79+
80+
81+
82+
// Approach 1: Simple Sorting
83+
// Time complexity: O(nlog n) + O(1) = O(nlogn)
84+
// Space complexity: O(n)
85+
86+
// Approach 2: Insertion Sort
87+
// When a new number comes, we just have to put it in it's right place in the already sorted array
88+
// We can use binary search for it
89+
// Time complexity: O(n) + O(logn) ≈ O(n)
90+
// Space complexity: O(n)
91+
92+
93+
94+
// Approach 3: Two Heaps
95+
// Time complexity: (O(logn)
96+
// Space complexity: O(n)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
The key here is to double the string, that is, append the string to itself.
3+
In this way, the pattern would be duplicated.
4+
On removing the first and the last characters,
5+
if there exists some pattern,
6+
we would still be able to find the original string somewhere in the middle,
7+
taking some characters from the first half and some from the second half.
8+
9+
For example,
10+
11+
Example 1.
12+
13+
s = "abab"
14+
s+s = "abababab"
15+
16+
On removing the first and the last characters, we get:
17+
(s+s).substr(1, 2*s.size()-2) = "bababa"
18+
19+
This new string, "bababa" still contains the original string, "abab".
20+
Thus there exists some repeated pattern in the original string itself.
21+
Example 2.
22+
23+
s = "aba"
24+
s+s = "abaaba"
25+
26+
On removing the first and the last characters, we get:
27+
(s+s).substr(1, 2*s.size()-2) = "baab"
28+
29+
This new string, "baab" does not contain the original string, "aba".
30+
This implies that there does not exist any pattern in the original string itself.
31+
32+
33+
Source: https://leetcode.com/problems/repeated-substring-pattern/discuss/826135/C%2B%2B-O(N)-time-or-One-liner-without-KMP-Explained-or-Beats-99
34+
*/
35+
36+
37+
38+
39+
40+
41+
class Solution {
42+
public:
43+
bool repeatedSubstringPattern(string s) {
44+
return (s+s).substr(1, 2*s.length()-2).find(s)!=-1;
45+
}
46+
};
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
Design a class to find the kth largest element in a stream.
2+
Note that it is the kth largest element in the sorted order, not the kth distinct element.
3+
4+
Your KthLargest class will have a constructor which accepts an integer k and an integer array nums,
5+
which contains initial elements from the stream.
6+
For each call to the method KthLargest.add, return the element representing the kth largest element in the stream.
7+
8+
Example:
9+
10+
int k = 3;
11+
int[] arr = [4,5,8,2];
12+
KthLargest kthLargest = new KthLargest(3, arr);
13+
kthLargest.add(3); // returns 4
14+
kthLargest.add(5); // returns 5
15+
kthLargest.add(10); // returns 5
16+
kthLargest.add(9); // returns 8
17+
kthLargest.add(4); // returns 8
18+
Note:
19+
You may assume that nums' length ≥ k-1 and k ≥ 1.
20+
21+
22+
23+
24+
25+
26+
27+
28+
29+
class KthLargest {
30+
public:
31+
32+
priority_queue<int, vector<int>, greater<int> > q; // min heap
33+
int qcap; // k
34+
35+
KthLargest(int k, vector<int>& nums) {
36+
qcap=k;
37+
for(auto &num: nums){
38+
q.push(num);
39+
if(q.size()>k) q.pop();
40+
}
41+
}
42+
43+
int add(int val) {
44+
q.push(val);
45+
if(q.size()>qcap) q.pop();
46+
return q.top();
47+
}
48+
};
49+
50+
/**
51+
* Your KthLargest object will be instantiated and called as such:
52+
* KthLargest* obj = new KthLargest(k, nums);
53+
* int param_1 = obj->add(val);
54+
*/

0 commit comments

Comments
 (0)