since 3 is prime, it can only be factored as multiples of 3, hence this solution works (get the factor 3^k and then check if its same as n)..for other numbers it won't work.
rohitkgp
@rohitkgp
Posts made by rohitkgp

RE: C++ solution no loop / recursion

RE: I'm not sure what the question is even asking
haha, i haven't solved it yet but it is trying to say that convert 0 to x only if it is bounded by Xs (bounded means that it has X on all 4 sides of it) in a 4connected manner. I guess you are thinking of 8connectivity and hence got confused.

RE: Sharing my straightforward recursive solution
c++ recursive solution (easy to understand)
int findv(int a, vector<int> arr) { for(int i=0; i< arr.size(); i++) if(arr[i] == a) return i; } TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) { TreeNode *root = 0; int id, N; vector<int> temp; if(! inorder.empty()) { N = inorder.size(); root = new TreeNode(preorder[0]); id = findv(preorder[0], inorder); preorder.erase(preorder.begin()); if(id!=0) { temp = vector<int>(inorder.begin(), inorder.begin()+id); root>left = buildTree(preorder, temp); } if(id< N1) { temp = vector<int>(inorder.begin()+id+1,inorder.begin()+ N); root>right = buildTree(preorder, temp); } } return root; }

RE: Can Any One Solve this Question?
hmm, maybe this could be the catch. to find unique elements would take memory. thanks for pointing out.

RE: Can Any One Solve this Question?
you have to traverse it twice actually and its O(n) as is the other solution, but there might be a better solution to do that

RE: DP Python Solution
c++ DP solution (12ms)
int minimumTotal(vector<vector<int> > &triangle) { int n = triangle.size(); if(n == 0) return 0; int sum=triangle[0][0], id=0, left, right; vector<int> store; //array to store min values (bottom up manner) //store the leaves in the array for(int i=0; i<n; i++) { store.push_back(triangle[n1][i]); } for(int i=n2; i>=0; i) { for(id=0; id<=i; id++) { left = triangle[i][id] + store[id]; right = triangle[i][id] + store[id+1]; // update store array (ignore end values) if(left<right) store[id] = left; else store[id] = right; } } return store[0]; //result will be first element }

RE: Can Any One Solve this Question?
if it is so, then also my method also works..its a simple solving nonlinear equation style (i never used the concept of continuous series), but yes michaelalan's solution is better and my method has problem of overflow if numbers are huge.

RE: A concise and efficient solution (14 lines / 398ms)
not short but quite easy solution (13ms)
class Solution { public: //simple string flip function void flip(string &s, int start, int end) { if(start>=end) return; while(start<end) { char temp = s[start]; s[start]= s[end]; s[end] = temp; start++; end; } return; } void reverseWords(string &s) { int n = s.length(); if(n==0) return; int start=0; int end = n1; //reverse the whole string flip(s,0,end); //flip all substrings between spaces and last null (included) for(int i=0; i<=n; i++) { if(s[i] == ' '  s[i]== '\0') { flip(s,start,i1); start = i+1; } } //remove starting spaces start = 0; while(s[start] == ' ' && start<n) { start++; } if(start>0) s.erase(0,start); //remove ending spaces end = s.length()1; start =0; if(end>0) while(s[end] == ' ' && end>0) { end; start++; } else return; //remove all adjacent spaces if(start>0) s.erase(end+1,start); for(start=0; start< s.length()1; start++) { if(s[start] == s[start+1] && s[start]==' ') { s.erase(start,1);start; //1 index as for loop will increment it again } } } };

RE: For this program, why I input " a b " and output "a b", but in my visual stadio the output is "b a"@[email protected]
i got the same error initially, open the string in vs you'll see 2 spaces b/w a and b.