Skip to content

Commit 42abbf5

Browse files
committed
Add already solved heap and map problems
1 parent 85658d6 commit 42abbf5

File tree

5 files changed

+124
-0
lines changed

5 files changed

+124
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
### Distinct Numbers in Window
2+
3+
Asked in: [Amazon](#)
4+
5+
You are given an array of N integers, A<sub>1</sub>, A<sub>2</sub> ,…, A<sub>N</sub> and an integer K. Return the of count of distinct numbers in all windows of size K.
6+
7+
Formally, return an array of size N-K+1 where i’th element in this array contains number of distinct elements in sequence A<sub>i</sub>, A<sub>i+1</sub> ,…, A<sub>i+k-1</sub>.
8+
9+
Note:
10+
```
11+
If K > N, return empty array.
12+
A[i] is a signed integer
13+
```
14+
For example,
15+
```
16+
A=[1, 2, 1, 3, 4, 3] and K = 3
17+
18+
All windows of size K are
19+
20+
[1, 2, 1]
21+
[2, 1, 3]
22+
[1, 3, 4]
23+
[3, 4, 3]
24+
25+
So, we return an array [2, 3, 3, 2].
26+
```
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// Created by Islam, Abdullah Al Raqibul on 11/26/19.
3+
//
4+
5+
vector<int> Solution::dNums(vector<int> &A, int B) {
6+
map<int, int> cnt;
7+
vector<int> v;
8+
int i, j, k = B, sz = A.size(), cur = 0;
9+
10+
if(k > sz) return v;
11+
12+
for(i=0; i<sz; i+=1) {
13+
if(i < k) {
14+
if(cnt[A[i]] == 0) cur += 1;
15+
cnt[A[i]] += 1;
16+
if(i==k-1) {
17+
v.push_back(cur);
18+
}
19+
}
20+
else {
21+
if(cnt[A[i-k]] == 1) cur -= 1;
22+
cnt[A[i-k]] -= 1;
23+
if(cnt[A[i]] == 0) cur += 1;
24+
cnt[A[i]] += 1;
25+
26+
v.push_back(cur);
27+
}
28+
}
29+
return v;
30+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
### Merge K Sorted Lists
2+
3+
Asked in: [Flipkart](#) | [Amazon](#) | [Google](#)
4+
5+
Merge k sorted linked lists and return it as one sorted list.
6+
7+
Example :
8+
```
9+
1 -> 10 -> 20
10+
4 -> 11 -> 13
11+
3 -> 8 -> 9
12+
```
13+
will result in
14+
```
15+
1 -> 3 -> 4 -> 8 -> 9 -> 10 -> 11 -> 13 -> 20
16+
```
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//
2+
// Created by Islam, Abdullah Al Raqibul on 11/26/19.
3+
//
4+
5+
/**
6+
* Definition for singly-linked list.
7+
* struct ListNode {
8+
* int val;
9+
* ListNode *next;
10+
* ListNode(int x) : val(x), next(NULL) {}
11+
* };
12+
*/
13+
ListNode* Solution::mergeKLists(vector<ListNode*> &lists) {
14+
// Do not write main() function.
15+
// Do not read input, instead use the arguments to the function.
16+
// Do not print the output, instead return values as specified
17+
// Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details
18+
19+
ListNode* head = new ListNode(0);
20+
21+
if(lists.size()==0) {
22+
return NULL;
23+
}
24+
25+
int sz = lists.size();
26+
head->next = lists[0];
27+
ListNode *p;
28+
ListNode *q;
29+
30+
for(int i=1; i<sz; i+=1) {
31+
p = head;
32+
q = lists[i];
33+
while(q) {
34+
if(!p->next) {
35+
p->next = q;
36+
break;
37+
}
38+
39+
if(p->next->val < q->val) {
40+
p = p->next;
41+
}
42+
else {
43+
ListNode *l = p->next;
44+
p->next = q;
45+
q = q->next;
46+
p->next->next = l;
47+
p = p->next;
48+
}
49+
}
50+
}
51+
return head->next;
52+
}

Heaps And Maps/github_placeholder.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)