Skip to content

Commit a6970a3

Browse files
committed
Added Graph and Heap Qs
1 parent 3d9c8cc commit a6970a3

9 files changed

+336
-1
lines changed

Binary-Search/ArrangingCoins441.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// You have n coins and you want to build a staircase with these coins. The staircase consists of k rows where
2+
// the ith row has exactly i coins. The last row of the staircase may be incomplete.
3+
// Given the integer n, return the number of complete rows of the staircase you will build.
4+
5+
// Example 1:
6+
// Input: n = 5
7+
// Output: 2
8+
// Explanation: Because the 3rd row is incomplete, we return 2.
9+
// Example 2:
10+
// Input: n = 8
11+
// Output: 3
12+
// Explanation: Because the 4th row is incomplete, we return 3.
13+
14+
// Constraints:
15+
// 1 <= n <= 231 - 1
16+
17+
class Solution {
18+
public:
19+
int arrangeCoins(int n) {
20+
long left = 0, right = n, curr, mid;
21+
while(left <= right){
22+
mid = left + (right - left) / 2;
23+
curr = (mid * (mid + 1)) / 2;
24+
if(curr == n)
25+
return mid;
26+
else if(curr < n)
27+
left = mid + 1;
28+
else right = mid - 1;
29+
}
30+
return right;
31+
}
32+
};

Binary-Search/ArrangingCoins441.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// You have n coins and you want to build a staircase with these coins. The staircase consists of k rows where
2+
// the ith row has exactly i coins. The last row of the staircase may be incomplete.
3+
// Given the integer n, return the number of complete rows of the staircase you will build.
4+
5+
// Example 1:
6+
// Input: n = 5
7+
// Output: 2
8+
// Explanation: Because the 3rd row is incomplete, we return 2.
9+
// Example 2:
10+
// Input: n = 8
11+
// Output: 3
12+
// Explanation: Because the 4th row is incomplete, we return 3.
13+
14+
// Constraints:
15+
// 1 <= n <= 231 - 1
16+
17+
class Solution {
18+
public int arrangeCoins(int n) {
19+
long curr = 0, mid = 0, left = 0, right = n;
20+
while(left <= right){
21+
mid = left + (right - left) / 2;
22+
curr = mid * (mid + 1) / 2;
23+
if(curr == n) return (int) mid;
24+
if(curr < n) left = mid + 1;
25+
else right = mid - 1;
26+
}
27+
return (int) right;
28+
}
29+
}

Graphs/BFS-Traversal.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// https://practice.geeksforgeeks.org/problems/bfs-traversal-of-graph/1
2+
// Given a directed graph. The task is to do Breadth First Traversal of this graph starting from 0.
3+
// Note: One can move from node u to node v only if there's an edge from u to v and find the BFS traversal of the graph starting from the 0th vertex, from left to right according to the graph. Also, you should only take nodes directly or indirectly connected from Node 0 in consideration.
4+
// Example 1:
5+
6+
// Input:
7+
8+
// Output: 0 1 2 3 4
9+
// Explanation:
10+
// 0 is connected to 1 , 2 , 3.
11+
// 2 is connected to 4.
12+
// so starting from 0, it will go to 1 then 2
13+
// then 3.After this 2 to 4, thus bfs will be
14+
// 0 1 2 3 4.
15+
// Example 2:
16+
17+
// Input:
18+
19+
// Output: 0 1 2
20+
// Explanation:
21+
// 0 is connected to 1 , 2.
22+
// so starting from 0, it will go to 1 then 2,
23+
// thus bfs will be 0 1 2 3 4.
24+
25+
// Expected Time Complexity: O(V + E)
26+
// Expected Auxiliary Space: O(V)
27+
28+
29+
// Constraints:
30+
// 1 ≤ V, E ≤ 104
31+
32+
// Company Tags
33+
// Topic Tags
34+
// Related Courses
35+
// Related Interview Experiences
36+
37+
// If you are facing any issue on this page. Please let us know.
38+
39+
// Output Window
40+
41+
class Solution {
42+
// Function to return Breadth First Traversal of given graph.
43+
public ArrayList<Integer> bfsOfGraph(int V, ArrayList<ArrayList<Integer>> adj) {
44+
int[] visited = new int[V];
45+
ArrayList<Integer> ans = new ArrayList<>();
46+
Queue<Integer> q = new LinkedList<>();
47+
q.offer(0);
48+
visited[0] = 1;
49+
while(!q.isEmpty()){
50+
int curr = q.poll();
51+
ans.add(curr);
52+
for(int num : adj.get(curr)){
53+
if(visited[num] == 0){
54+
visited[num] = 1;
55+
q.offer(num);
56+
}
57+
}
58+
}
59+
return ans;
60+
}
61+
}

Graphs/DFS-Traversal.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// https://practice.geeksforgeeks.org/problems/depth-first-traversal-for-a-graph/1
2+
3+
// Given a connected undirected graph. Perform a Depth First Traversal of the graph.
4+
// Note: Use recursive approach to find the DFS traversal of the graph starting from the 0th vertex from left to right according to the graph..
5+
6+
// Example 1:
7+
8+
// Input:
9+
10+
// Output: 0 1 2 4 3
11+
// Explanation:
12+
// 0 is connected to 1, 2, 4.
13+
// 1 is connected to 0.
14+
// 2 is connected to 0.
15+
// 3 is connected to 0.
16+
// 4 is connected to 0, 3.
17+
// so starting from 0, it will go to 1 then 2
18+
// then 4, and then from 4 to 3.
19+
// Thus dfs will be 0 1 2 4 3.
20+
// Example 2:
21+
22+
// Input:
23+
24+
// Output: 0 1 2 3
25+
// Explanation:
26+
// 0 is connected to 1 , 3.
27+
// 1 is connected to 2.
28+
// 2 is connected to 1.
29+
// 3 is connected to 0.
30+
// so starting from 0, it will go to 1 then 2
31+
// then back to 0 then 0 to 3
32+
// thus dfs will be 0 1 2 3.
33+
34+
// Expected Time Complexity: O(V + E)
35+
// Expected Auxiliary Space: O(V)
36+
37+
// Constraints:
38+
// 1 ≤ V, E ≤ 104
39+
40+
41+
class Solution {
42+
public void dfs(int i, ArrayList<ArrayList<Integer>> adj, int[] visited, ArrayList<Integer> ans){
43+
visited[i] = 1;
44+
ans.add(i);
45+
for(int num : adj.get(i)){
46+
if(visited[num] == 0)
47+
dfs(num, adj, visited, ans);
48+
}
49+
return;
50+
}
51+
public ArrayList<Integer> dfsOfGraph(int V, ArrayList<ArrayList<Integer>> adj) {
52+
int[] visited = new int[V];
53+
ArrayList<Integer> ans = new ArrayList<>();
54+
dfs(0, adj, visited, ans);
55+
return ans;
56+
}
57+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// https://practice.geeksforgeeks.org/problems/detect-cycle-in-an-undirected-graph/1/?page=1&category[]=Graph&query=page1category[]Graph#
2+
// Given an undirected graph with V vertices and E edges, check whether it contains any cycle or not.
3+
4+
bool bfs(vector<int> adj[], int source, int V, vector<bool> &visited) {
5+
queue<int> q;
6+
q.push(source);
7+
// mark the source node as visited
8+
visited[source] = true;
9+
vector<int> parent(V, -1);
10+
11+
// apply BFS
12+
while(!q.empty()) {
13+
int front = q.front();
14+
q.pop();
15+
16+
// visit the neighbours of this node
17+
for(int &nbr : adj[front]) {
18+
if(!visited[nbr]) {
19+
visited[nbr] = true;
20+
parent[nbr] = front;
21+
q.push(nbr);
22+
}
23+
else if(parent[front] != nbr)
24+
return true;
25+
}
26+
}
27+
return false;
28+
}
29+
30+
// the graph could contain disconnected components as well
31+
bool isCycle(int V, vector<int> adj[]) {
32+
// initially mark all the nodes as unvisited
33+
vector<bool> visited(V, false);
34+
// go through each node and call BFS on each unvisited node
35+
for(int i = 0; i < V; i++) {
36+
// cycle is detected
37+
if(!visited[i] && bfs(adj, i, V, visited))
38+
return true;
39+
}
40+
return false;
41+
}
42+
© 2021 GitHub, Inc.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Given an integer array nums and an integer k, return the kth largest element in the array.
2+
// Note that it is the kth largest element in the sorted order, not the kth distinct element.
3+
// Example 1:
4+
5+
// Input: nums = [3,2,1,5,6,4], k = 2
6+
// Output: 5
7+
// Example 2:
8+
9+
// Input: nums = [3,2,3,1,2,4,5,5,6], k = 4
10+
// Output: 4
11+
12+
13+
// Constraints:
14+
15+
// 1 <= k <= nums.length <= 104
16+
// -104 <= nums[i] <= 104
17+
class Solution {
18+
public int findKthLargest(int[] nums, int k) {
19+
Queue<Integer> q = new PriorityQueue<>((a, b) -> b - a);
20+
for(int num : nums){
21+
q.offer(num);
22+
}
23+
for(int i = 1; i < k; i++)
24+
q.poll();
25+
return q.poll();
26+
}
27+
}
28+

Heap/Kth-Largest-Element-in-Stream-703.cpp

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
// -104 <= val <= 104
2929
// At most 104 calls will be made to add.
3030
// It is guaranteed that there will be at least k elements in the array when you search for the kth element.
31-
31+
// Method 1
3232
class KthLargest {
3333
public:
3434
priority_queue<int, vector<int>, greater<int>> minHeap;
@@ -56,3 +56,30 @@ class KthLargest {
5656
* KthLargest* obj = new KthLargest(k, nums);
5757
* int param_1 = obj->add(val);
5858
*/
59+
60+
// Method 2
61+
62+
class MedianFinder {
63+
public:
64+
priority_queue<long> maxHeap;
65+
priority_queue<long, vector<long>, greater<long>> minHeap;
66+
MedianFinder() {
67+
}
68+
void addNum(int num) {
69+
if(maxHeap.empty() || maxHeap.top() > num) maxHeap.push(num);
70+
else minHeap.push(num);
71+
if(maxHeap.size() > minHeap.size() + 1){
72+
minHeap.push(maxHeap.top());
73+
maxHeap.pop();
74+
}
75+
else if(minHeap.size() > maxHeap.size() + 1){
76+
maxHeap.push(minHeap.top());
77+
minHeap.pop();
78+
}
79+
}
80+
81+
double findMedian() {
82+
if(minHeap.size() == maxHeap.size()) return maxHeap.empty() ? 0 : (maxHeap.top() + minHeap.top()) / 2.0;
83+
else return maxHeap.size() > minHeap.size() ? maxHeap.top() : minHeap.top();
84+
}
85+
};
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Design a class to find the kth largest element in a stream. Note that it is the kth largest element in the sorted order, not the kth distinct element.
2+
// Implement KthLargest class:
3+
// KthLargest(int k, int[] nums) Initializes the object with the integer k and the stream of integers nums.
4+
// int add(int val) Returns the element representing the kth largest element in the stream.
5+
6+
// Example 1:
7+
8+
// Input
9+
// ["KthLargest", "add", "add", "add", "add", "add"]
10+
// [[3, [4, 5, 8, 2]], [3], [5], [10], [9], [4]]
11+
// Output
12+
// [null, 4, 5, 5, 8, 8]
13+
14+
// Explanation
15+
// KthLargest kthLargest = new KthLargest(3, [4, 5, 8, 2]);
16+
// kthLargest.add(3); // return 4
17+
// kthLargest.add(5); // return 5
18+
// kthLargest.add(10); // return 5
19+
// kthLargest.add(9); // return 8
20+
// kthLargest.add(4); // return 8
21+
22+
23+
// Constraints:
24+
25+
// 1 <= k <= 104
26+
// 0 <= nums.length <= 104
27+
// -104 <= nums[i] <= 104
28+
// -104 <= val <= 104
29+
// At most 104 calls will be made to add.
30+
// It is guaranteed that there will be at least k elements in the array when you search for the kth element.
31+
32+
class MedianFinder {
33+
private PriorityQueue<Integer> minHeap = new PriorityQueue<>(Collections.reverseOrder());
34+
private PriorityQueue<Integer> maxHeap = new PriorityQueue<>();
35+
public MedianFinder() {
36+
}
37+
public void addNum(int num) {
38+
if(minHeap.isEmpty() || minHeap.peek() > num) minHeap.offer(num);
39+
else maxHeap.offer(num);
40+
if(minHeap.size() > maxHeap.size() + 1){
41+
maxHeap.offer(minHeap.poll());
42+
}
43+
else if(maxHeap.size() > minHeap.size() + 1){
44+
minHeap.offer(maxHeap.poll());
45+
}
46+
}
47+
48+
public double findMedian() {
49+
if(minHeap.size() == maxHeap.size()) return maxHeap.size() == 0 ? 0 : (minHeap.peek() + maxHeap.peek()) / 2.0;
50+
else return minHeap.size() > maxHeap.size() ? minHeap.peek() : maxHeap.peek();
51+
}
52+
}
53+
54+
/**
55+
* Your MedianFinder object will be instantiated and called as such:
56+
* MedianFinder obj = new MedianFinder();
57+
* obj.addNum(num);
58+
* double param_2 = obj.findMedian();
59+
*/

0 commit comments

Comments
 (0)