sidlglove

Untitled

Nov 17th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.83 KB | None | 0 0
  1. bool isPalindrome(string s){
  2.     int n=s.length();
  3.     for(int i=0;i<n/2;i++)
  4.         if(s[i]!=s[n-1-i])
  5.             return 0;
  6.     return 1;
  7. }
  8. void partition1(vector<vector<string> > &v,vector<string> &row,string &s,int l,int n){
  9.     if(l==n){
  10.         v.push_back(row);
  11.         return;
  12.     }
  13.     string temp="";
  14.     for(int i=l;i<n;i++){
  15.         temp+=s[i];
  16.         if(isPalindrome(temp)){
  17.             row.push_back(temp);
  18.             partition1(v,row,s,i+1,n);
  19.             row.pop_back();
  20.         }
  21.     }
  22. }
  23. vector<vector<string> > Solution::partition(string A) {
  24.   vector<vector<string> > ans;
  25.   vector<string> row;
  26.   partition1(ans,row,A,0,A.length());
  27.   return ans;
  28. }
  29.  
  30.  
  31.  
  32. vector<int> Solution::prevSmaller(vector<int> &A) {
  33.    
  34.     vector<int >sol(A.size(),-1);
  35.     stack<int>st;
  36.     int n=A.size();
  37.     st.push(n-1);
  38.    
  39.     for(int i=n-2;i>=0;i--){
  40.         int j=A[i];
  41.         while(!st.empty()&&j<A[st.top()]){
  42.             sol[st.top()]=j;
  43.             st.pop();
  44.         }
  45.         st.push(i);
  46.     }
  47.     return sol;
  48. }
  49.  
  50. int findMaxUtil(Node* root, int &res)
  51. {
  52.     //Base Case
  53.     if (root == NULL)
  54.         return 0;
  55.  
  56.     // l and r store maximum path sum going through left and
  57.     // right child of root respectively
  58.     int l = findMaxUtil(root->left,res);
  59.     int r = findMaxUtil(root->right,res);
  60.  
  61.     // Max path for parent call of root. This path must
  62.     // include at-most one child of root
  63.     int max_single = max(max(l, r) + root->data, root->data);
  64.  
  65.     // Max Top represents the sum when the Node under
  66.     // consideration is the root of the maxsum path and no
  67.     // ancestors of root are there in max sum path
  68.     int max_top = max(max_single, l + r + root->data);
  69.  
  70.     res = max(res, max_top); // Store the Maximum Result.
  71.  
  72.     return max_single;
  73. }
Add Comment
Please, Sign In to add comment