Posts

Preorder Traversal without recursion

  Preorder Traversal  without recursion   vector <int> preorder(Node* root) {     vector<int>v;     stack<Node*>st;         if(root==NULL)        return v;           st.push(root);         while(!st.empty())     {         Node * temp;         temp=st.top();         st.pop();                 v.push_back(temp->data);                 if(temp->right)            st.push(temp->right);          if(temp->left)       ...

detecting cycle in the undirected graph(dfs,cpp))

 detecting cycle in the undirected  graph(dfs,cpp)) ================================================== bool dfs ( vector < vector < int >> g , int s , int par ) { visited [ s ] = 1 ; for ( int i = 0 ; i < g [ s ] . size (); i ++) { if ( visited [ g [ s ] [ i ] ] == 0 ) { par = s ; if ( dfs ( g , g [ s ] [ i ] , par )) return true ; } else { if ( g [ s ] [ i ] != par ) return true ; } } return false ; } /////////////////////////////////////////////////////////////// //// Returns true if the graph contains a cycle, else false. // /////////////////////////////////////////////////////////////// bool isCyclic ( vector < vector < int >> g ) { for ( int i = 0 ; i < g . size (); i ++) visited [ i ] = false ; for ( int u = 0 ; u < g . size (); u ++) if (! visited [ u ] ...

Number of Islands(leetcode,cpp)

Number of Islands(leetcode,cpp) Given a 2d grid map of '1' s (land) and '0' s (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.   Example 1: Input: grid = [ ["1","1","1","1","0"], ["1","1","0","1","0"], ["1","1","0","0","0"], ["0","0","0","0","0"] ] Output: 1 Example 2: Input: grid = [ ["1","1","0","0","0"], ["1","1","0","0","0"], ["0","0","1","0","0"], ["0","0","0","1","1"] ] Output: 3...

Lowest Common Ancestor in a Binary Tree(###,cpp)

     Lowest Common Ancestor in a Binary Tree(###,cpp) Given a Binary Tree with all unique values and two nodes value  n1 and n2 . The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them is present.  Example 1: Input: n1 = 2 , n2 = 3     1   /  \   2   3 Output: 1     Example 2: Input: n1 = 3 , n2 = 4         5        /       2     /  \    3   4 Output: 2   ---------------------------------------------------------------------------------------------------------------------------- Node* lca(Node* root ,int n1 ,int n2 ) {     if(root==NULL)       return root;          if(root->data==n1  || root->data=...

Longest Increasing Subsequence(cpp,dp)

Longest Increasing Subsequence   Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Input: [10,9,2,5,3,7,101,18] Output: 4 Explanation: The longest increasing subsequence is [2,3,7,101] , therefore the length is 4 .    ============================================================================== code= class Solution { public: int lengthOfLIS(vector<int>& a) { if(a.size()==0) return 0; int dp[a.size()]; for(int i=0;i<a.size();i++) dp[i]=1; int j=0; for(int i=1;i<a.size();i++) { while(j<i) { if(a[j]<a[i]) { dp[i]=max(dp[i],1+dp[j]); } j++; } j=0; } sort(dp,dp+a.size()); return dp[a.size()-1]; } };

Maximal Rectangle in given matrix with 1,0(cpp, leetcode)

 Maximal Rectangle in given matrix with 1,0(cpp, leetcode) ============================================== Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area. Example: Input: [ ["1","0","1","0","0"], ["1","0"," 1 "," 1 "," 1 "], ["1","1"," 1 "," 1 "," 1 "], ["1","0","0","1","0"] ] Output: 6 ======================================================================================== code .... class Solution { public: int largestRectangleArea(vector<int>& a) { if(a.size()==0) return 0; else if(a.size()==1) return a[0]; stack <int>s; int i=0; int maxm=0; int area=0; while(i<a.size()) ...

Largest Rectangle in Histogram(cpp,using stack,leetcode)

Image
Largest Rectangle in Histogram (cpp,using stack,leetcode) =========================   Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.   Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3] .   The largest rectangle is shown in the shaded area, which has area = 10 unit.   Example: Input: [2,1,5,6,2,3] Output: 10  ============================================================================================  code: class Solution { public: int largestRectangleArea(vector<int>& a) { if(a.size()==0) return 0; else if(a.size()==1) return a[0]; stack <int>s; int i=0; int maxm=0; int area=0; while(i<a.size()) { if(s.empty() || a[i]>=a[s.top()]) { ...