diff --git a/Math/SortedPermutationRank.cpp b/Math/SortedPermutationRank.cpp new file mode 100644 index 0000000..4693785 --- /dev/null +++ b/Math/SortedPermutationRank.cpp @@ -0,0 +1,46 @@ +#define mod 1000003 + +void buildFact(vector &fact,int len){ + fact.push_back(1); + fact.push_back(1); + for(int i = 2; i<= len ; i++){ + fact.push_back((fact[i-1]*i)%mod); + } +} + +int Solution::findRank(string A){ + vector fact; + // create a vector filled with factorial value of that index modulated + buildFact(fact,A.length()); + + // created a sorted version of given string + string b = A; + sort(b.begin() , b.end()); + + // create to Hash to store actual location of each character in sorted manner + unordered_map pos; + for(int i = 0; i v; + for(int i = 0; i v[i]) + v[j]--; + } + } + reverse(v.begin() , v.end()); + + // factorial arithmatics + long n = 0; + for(int i = 0; i < v.size() ; i++){ + n = (n + v[i]*fact[i])%mod; + } + n++; + return n; +} diff --git a/Trees/MinimumDepthOfABinaryTree.cpp b/Trees/MinimumDepthOfABinaryTree.cpp deleted file mode 100644 index 2eb4952..0000000 --- a/Trees/MinimumDepthOfABinaryTree.cpp +++ /dev/null @@ -1,36 +0,0 @@ -// https://www.interviewbit.com/problems/min-depth-of-binary-tree/ - -/** - * Definition for binary tree - * struct TreeNode { - * int val; - * TreeNode *left; - * TreeNode *right; - * TreeNode(int x) : val(x), left(NULL), right(NULL) {} - * }; - */ -int depth(TreeNode* root){ - if(root == NULL){ - return 0; - } - else if(root->left == NULL && root->right == NULL){ - return 1; - } - else if(root->left == NULL && root->right != NULL){ - return 1 + depth(root->right); - } - else if(root->left != NULL && root->right == NULL){ - return 1 + depth(root->left); - } - return min(1 + depth(root->right), 1 + depth(root->left)); -} - -int Solution::minDepth(TreeNode* A) { - // Do not write main() function. - // Do not read input, instead use the arguments to the function. - // Do not print the output, instead return values as specified - // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details - - return depth(A); - -}