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
/* | |
Simple logic. | |
if char is '(': store that locations in stack. push 0 into vec ( for storing the longest parantheses value until there in vec ) | |
else: | |
if it is not empty: valid parantheses, take the location from stack. calculate length. update it via the value stored in vec. update max | |
else: just push 0 into vec ( for storing longest parantheses length until there ) | |
return total_max | |
*/ |
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
/* | |
O(n) solution using BFS. | |
Let's say we have [2,3,1,1,4]. Divide values into nodes such that nodes at level i can be reached by jump from level (i-1) | |
now didiving into levels, [2 || 3,1 || 1,4] | |
*/ | |
class Solution { | |
public: |
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
/* | |
Solution still not complete. | |
*/ | |
class Solution{ | |
public: | |
void dfs(vector<vector<int> > paths, vector<string> words, vector<string> v, string start, string end){ | |
if(start == end){ | |
result.push_back(v); | |
return ; |
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
/* | |
For ref: | |
https://oj.leetcode.com/discuss/12780/my-concise-c-solution-ac-90-ms | |
This is the classic problem. | |
1) Know how to solve largest rectangle in histogram. | |
Solution:- | |
for every index i { | |
for every index j less than its height{ | |
calculate height * (distance between this rectangle and that rectangle) |
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
''' | |
Only trciky part is dictioanry mod. | |
How did he do it?? | |
let's say 0.147851... | |
First when 1 encouters mod[1] = 0(index) will be stored | |
now when again 1 encounters, it's already present | |
so first for loop is from (0 to 0) | |
and second for loop is from (1 to 5)(5 is the index where it again occured) | |
and return the solution |
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
// A divide and conquer program in C/C++ to find the smallest distance from a | |
// given set of points. | |
#include <stdio.h> | |
#include <float.h> | |
#include <stdlib.h> | |
#include <math.h> | |
// A structure to represent a Point in 2D plane | |
struct Point |
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
/* | |
Amazing algorithm:- | |
inorder traversal without using stacks and recursion just by manipulating threads between them. | |
http://www.geeksforgeeks.org/inorder-tree-traversal-without-recursion-and-without-stack/ | |
*/ | |
void inorder_morris(node *root){ | |
if(root == NULL) return ; | |
node *cur, *pre; | |
cur = root; |
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
/* | |
Using morris algorithm | |
Just traverse the tree and whenerv u find that there are two elements that values neeeds are not in order, | |
just store them as first and second and swap them. | |
*/ | |
TreeNode *first = NULL; | |
TreeNode *second = NULL; | |
TreeNode *previous = NULL; | |
void recoverTree(TreeNode *root) { |
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
/* | |
1) using two queues | |
2) using cnt method | |
3) using height method | |
*/ | |
//using two queues method | |
class Solution { | |
public: | |
vector<vector<int> > levelOrder(TreeNode *root) { |
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
This consists of common interview questions i need to see before i go for an interview:- | |
1) https://github.com/sagivo/algorithms | |
2) http://www.ardendertat.com |