Skip to content

Instantly share code, notes, and snippets.

View sourabh2k15's full-sized avatar
🎯
Focusing

sourabh2k15 sourabh2k15

🎯
Focusing
View GitHub Profile
@sourabh2k15
sourabh2k15 / isSubsequence.cpp
Created December 30, 2017 18:29
Determines is string s2 is a subsequence of string s1
bool isSubsequence(string& s1, string& s2){
int l1 = s1.length(), l2 = s2.length();
for(int i = 0, j = 0; i < l1; i++){
if(s1[i] == s2[j] && ++j == l2) return true;
}
return s2.empty();
}
@sourabh2k15
sourabh2k15 / lcs.cpp
Last active December 30, 2017 18:12
Longest Common Subsequence
// recursive version , computes many subproblems again and again
int lcsRecursive(string& s1, string& s2, int i, int j){
if(i >= s1.length() || j >= s2.length()) return 0;
if(s1[i] == s2[j]) return 1 + lcsRecursive(s1, s2, i+1, j+1);
else{
return max(lcsRecursive(s1, s2, i+1, j), lcsRecursive(s1, s2, i, j+1));
}
}
@sourabh2k15
sourabh2k15 / firstUniqChar.cpp
Created December 26, 2017 14:02
Leetcode #387 First Unique Character in a String
class Solution {
public:
int firstUniqChar(string s) {
if(s.length() == 0) return -1;
vector<int> frequency(26, -1);
for(int i = 0; i < s.length(); i++){
if(frequency[s[i] - 'a'] == -1) frequency[s[i] - 'a'] = i;
else{
@sourabh2k15
sourabh2k15 / friendCircleNum.cpp
Last active January 4, 2018 06:52
Leetcode #547 Friend Circles
class Solution {
public:
// M is the friend matrix provided | basic intuition: we need connected components in graph, which can be done by BFS / DFS.
// why does this work ? because applying transitive property to this : a friend of b, b friend of c , c friend of d
// a,b,c,d should fall into 1 friend circle, just imagine a,b,c,d to be nodes of graph and they are connected by an edge if they
// are friends else not connected by an edge, so simply finding the total groups / connected components gives us the answer.
int findCircleNum(vector<vector<int>>& M) {
vector<bool> friendZoned(M.size(), false); // array to keep track of visited in DFS
stack<int> dfs; // stack to execute DFS
@sourabh2k15
sourabh2k15 / Trie.cpp
Created December 26, 2017 04:13
Leetcode #208 Simple Trie Implementation
class Trie {
private:
class TrieNode{
public:
bool isWord;
vector<TrieNode*> next;
TrieNode() : next(vector<TrieNode*>(26, NULL)), isWord(false){}
~TrieNode(){
for(auto node: next) delete node;
@sourabh2k15
sourabh2k15 / isValidSerialization.cpp
Created December 26, 2017 02:16
Leetcode #331. Verify Preorder Serialization of a Binary Tree
class Solution {
public:
/*
Intuition:
1) every node has to be a left or right child of some node, except the root ofcourse
2) every non "#" node means it has 2 leaves so a valid preorder string would have 2 more values
3) Using 1 and 2, set totalnodes = 0. Add 2 to totalnodes if non "#" value encountered and decrement totalnodes for having processed present value
4) Using this scheme, totalnodes should equal 0 at end if it's a valid preorder string.
5) We start with totalnodes = 1 instead of 0 cause inside loop it does totalnodes-- for every value , which includes
root value as well.
@sourabh2k15
sourabh2k15 / KthSmallest.cpp
Created December 26, 2017 01:36
Leetcode #230 Find Kth Smallest element in BST
class Solution {
public:
int kthSmallest(TreeNode* root, int k) {
stack<TreeNode*> s;
while(root != NULL || !s.empty()){
while(root != NULL){
s.push(root);
root = root->left;
}
@sourabh2k15
sourabh2k15 / isValidBST.cpp
Created December 26, 2017 01:35
Leetcode #98 Validate if Tree is BST
class Solution {
public:
bool isValidBST(TreeNode* root) {
if(root == NULL) return true;
stack<TreeNode*> s;
TreeNode* prev = NULL;
while(root != NULL || !s.empty()){
while(root != NULL){
@sourabh2k15
sourabh2k15 / inorderTraversal.cpp
Created December 26, 2017 01:33
Leetcode #94 Inorder Traversal of Binary Tree
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> inorder;
stack<TreeNode*> nodes;
if(root == NULL) return inorder;
while(root != NULL || !nodes.empty()){
//push left children if available
defmodule Async do
@n_clients 100000
def createFollowersSync(n_clients) do
Enum.reduce(1..n_clients, [], fn rank, acc ->
acc ++ [Util.pickRandom(@n_clients, rank)]
end)
end
def createFollowersAsync(n_clients) do