This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Find single source shortest path - Dijkstra's algo | |
and print the distance as well as path | |
Input: | |
n-vertices , m-edges | |
u , v , w - undirected edge with weight w | |
9 14 | |
0 1 4 | |
0 7 8 | |
1 7 11 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Coin Change Problem: | |
Given a value N, find the number of ways to make change for N cents, if we have infinite supply of each of | |
S = { S1, S2, .. , Sm} valued coins. The order of coins doesn’t matter. | |
For example, for N = 4 and S = {1,2,3}, there are four solutions: {1,1,1,1},{1,1,2},{2,2},{1,3}. | |
So output should be 4. | |
For N = 10 and S = {2, 5, 3, 6}, there are five solutions: {2,2,2,2,2}, {2,2,3,3}, {2,2,6}, {2,3,5} and {5,5}. | |
So the output should be 5. | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Binary Tree Zigzag Level Order Traversal:: | |
https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ | |
https://www.interviewbit.com/problems/zigzag-level-order-traversal-bt/ | |
Given a binary tree, return the zigzag level order traversal of its nodes' values. | |
(ie, from left to right, then right to left for the next level and alternate between). | |
For example: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
void display_vector(vector<int> &arr ) | |
{ | |
for(int i = 0 ; i<arr.size() ; i++) | |
{ | |
cout<<arr[i]<<" " ; | |
} | |
cout<<"\n" ; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
string minWindow(string s, string t) { | |
unordered_map<char, int> table; | |
// initialize frequency table for t | |
for(char c : t){ | |
table[c]++; | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
vector<vector<int> > Solution::fourSum(vector<int> &A, int B) { | |
int N = A.size(); | |
//mapping sum value to a vector of pair indices | |
unordered_map<int, vector<pair<int, int> > > Hash; | |
vector<vector<int> >Ans; | |
for(int i = 0; i < N; ++i) { | |
for(int j = i + 1; j < N; ++j) { | |
//calculate sum with i and j indices | |
int Sum = A[i] + A[j]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Largest Continuous Sequence Zero Sum : | |
(https://www.interviewbit.com/problems/largest-continuous-sequence-zero-sum/) | |
Find the largest continuous sequence in a array which sums to zero. | |
Example: | |
Input: {1 ,2 ,-2 ,4 ,-4} | |
Output: {2 ,-2 ,4 ,-4} | |
Input: arr[] = {15, -2, 2, -8, 1, 7, 10, 23}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
The gray code is a binary numeral system where two successive values differ in only one bit. | |
Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. | |
A gray code sequence must begin with 0. | |
Example 1: | |
Input: 2 | |
Output: [0,1,3,2] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Permutation II :: | |
Problem Statement:: | |
Given a collection of numbers that might contain duplicates, return all possible unique permutations. | |
(https://leetcode.com/problems/permutations-ii/) | |
Example: | |
Input: [1,1,2] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Given a collection of numbers, return all possible permutations. | |
Example: | |
[1,2,3] will have the following permutations: | |
[1,2,3] | |
[1,3,2] | |
[2,1,3] |
NewerOlder