Skip to content

Commit 69dcc15

Browse files
author
Prashant Jain
committed
Added Maps and Queue
1 parent 9d2a4e2 commit 69dcc15

3 files changed

Lines changed: 168 additions & 0 deletions

File tree

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package in.knowledgegate.dsa.linkedlist;
2+
3+
/**
4+
* You are given the heads of two sorted linked
5+
* lists list1 and list2.
6+
*
7+
* Merge the two lists in a one sorted list.
8+
* The list should be made by splicing together
9+
* the nodes of the first two lists.
10+
*
11+
* Return the head of the merged linked list.
12+
*
13+
* Example 1:
14+
* Input: list1 = [1,2,4], list2 = [1,3,4]
15+
* Output: [dummy, 1,1,2,3,4,4]
16+
*
17+
* Example 2:
18+
* Input: list1 = [], list2 = []
19+
* Output: []
20+
*
21+
* Example 3:
22+
* Input: list1 = [], list2 = [0]
23+
* Output: [0]
24+
*
25+
* Constraints:
26+
* The number of nodes in both lists is in the
27+
* range [0, 50].
28+
* -100 <= Node.val <= 100
29+
* Both list1 and list2 are sorted in
30+
* non-decreasing order.
31+
*/
32+
public class MergeTwoSortedList {
33+
private class ListNode {
34+
int val;
35+
ListNode next;
36+
}
37+
38+
public ListNode mergeTwoLists(ListNode list1,
39+
ListNode list2) {
40+
ListNode temp = new ListNode();
41+
ListNode dummy = temp;
42+
43+
while (list1 != null && list2 != null) {
44+
if (list1.val <= list2.val) {
45+
temp.next = list1;
46+
list1 = list1.next;
47+
} else {
48+
temp.next = list2;
49+
list2 = list2.next;
50+
}
51+
temp = temp.next;
52+
}
53+
54+
temp.next = list1 != null ? list1 : list2;
55+
return dummy.next;
56+
}
57+
}
58+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package in.knowledgegate.dsa.maps;
2+
3+
import java.util.HashSet;
4+
5+
/**
6+
* Given two integer arrays nums1 and nums2,
7+
* return an array of their intersection.
8+
* Each element in the result must be unique
9+
* and you may return the result in any order.
10+
*
11+
* Example 1:
12+
* Input: nums1 = [1,2,2,1], nums2 = [2,2]
13+
* Output: [2]
14+
*
15+
* Example 2:
16+
* Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
17+
* Output: [9,4]
18+
* Explanation: [4,9] is also accepted.
19+
*
20+
* Constraints:
21+
* 1 <= nums1.length, nums2.length <= 1000
22+
* 0 <= nums1[i], nums2[i] <= 1000
23+
*/
24+
public class IntersectionOfTwoArrays {
25+
public int[] intersection(int[] nums1, int[] nums2) {
26+
HashSet<Integer> set = new HashSet<>();
27+
for (int num : nums1) {
28+
set.add(num);
29+
}
30+
31+
HashSet<Integer> result = new HashSet<>();
32+
for (int num : nums2) {
33+
if (set.contains(num)) {
34+
result.add(num);
35+
}
36+
}
37+
38+
int[] resultArr = new int[result.size()];
39+
int i = 0;
40+
for (Integer num : result) {
41+
resultArr[i++] = num;
42+
}
43+
return resultArr;
44+
}
45+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package in.knowledgegate.dsa.queue;
2+
3+
import java.util.LinkedList;
4+
import java.util.Queue;
5+
6+
/**
7+
* You have a RecentCounter class which counts the
8+
* number of recent requests within a certain time
9+
* frame.
10+
*
11+
* Implement the RecentCounter class:
12+
*
13+
* RecentCounter() Initializes the counter with
14+
* zero recent requests.
15+
*
16+
* int ping(int t) Adds a new request at time t,
17+
* where t represents some time in milliseconds,
18+
* and returns the number of requests that has
19+
* happened in the past 3000 milliseconds
20+
* (including the new request). Specifically,
21+
* return the number of requests that have happened
22+
* in the inclusive range [t - 3000, t].
23+
*
24+
* It is guaranteed that every call to ping uses
25+
* a strictly larger value of t than the previous
26+
* call.
27+
*
28+
* Example 1:
29+
* Input
30+
* ["RecentCounter", "ping", "ping", "ping", "ping"]
31+
* [[], [1], [100], [3001], [3002]]
32+
* Output
33+
* [null, 1, 2, 3, 3]
34+
*
35+
* Explanation
36+
* RecentCounter recentCounter = new RecentCounter();
37+
* recentCounter.ping(1); // requests = [1], range is [-2999,1], return 1
38+
* recentCounter.ping(100); // requests = [1, 100], range is [-2900,100], return 2
39+
* recentCounter.ping(3001); // requests = [1, 100, 3001], range is [1,3001], return 3
40+
* recentCounter.ping(3002); // requests = [1, 100, 3001, 3002], range is [2,3002], return 3
41+
*
42+
*
43+
* Constraints:
44+
*
45+
* 1 <= t <= 109
46+
* Each test case will call ping with strictly
47+
* increasing values of t.
48+
* At most 104 calls will be made to ping
49+
*/
50+
public class RecentCounter {
51+
52+
private Queue<Integer> queue;
53+
54+
RecentCounter() {
55+
queue = new LinkedList<>();
56+
}
57+
58+
public int ping(int t) {
59+
queue.offer(t);
60+
while (queue.peek() < t - 3000) {
61+
queue.poll();
62+
}
63+
return queue.size();
64+
}
65+
}

0 commit comments

Comments
 (0)