lintcode Permutation Index
来源:互联网 发布:淘宝卖家中心登录网 编辑:程序博客网 时间:2024/05/29 18:19
class Solution { public: /** * @param A an integer array * @return a long integer */ long long permutationIndex(vector<int>& A) { // Write your code here // 找出A中每一个位置其后有多少个数比它小 // 然后再相加这些数与位对应的权 int len = A.size(); int c[len]; c[len - 1] = 0;// 最后一个数之后就没有比它小的数了 vector<int> a; a.push_back(A[len - 1]); for(int i = len - 2;i >= 0; --i){ auto iter = lower_bound(a.begin(), a.end(), A[i]); c[i] = iter - a.begin(); a.insert(iter, A[i]); } long long ans = 1, fac = 1, cc = 1; for(int i = len - 2;i >= 0; --i) ans += (fac*=cc++) * c[i]; return ans; } };
class Solution {public: long getFacctor(int n){ long result = 1; while(n>=1) { result =result*n; n--; } return result; }public: /** * @param A an integer array * @return a long integer */ long long permutationIndex(vector<int>& A) { // Write your code here int len = A.size(); if(len==0) return 0; long number = 0; for(int i = 0; i<len-1; i++) { long lesscurrent =0; for(int j = i+1;j<len; j++) { if(A[i]>A[j]) lesscurrent++; } number+=lesscurrent*getFactor(len-1-i); } return number+1; }};
阅读全文
0 0
- [Lintcode]Permutation Index
- lintcode Permutation Index
- [LintCode] Permutation Index I & Permutation Index II
- Lintcode(198) Permutation Index II
- lintcode Permutation Index 排列序号
- **[Lintcode]Permutation Index II排列序号II
- Lintcode - Previous Permutation
- lintcode:Next Permutation
- *[Lintcode]Next Permutation
- liintcode: Permutation Index
- LintCode_198 Permutation Index II
- #197 Permutation Index
- #198 Permutation Index II
- LintCode Inverted Index
- [LintCode] 字符串置换 String Permutation
- 197.Permutation Index-排列序号(容易题)
- 198.Permutation Index II-排列序号II(中等题)
- Permutation
- 计蒜课 跳跃游戏
- 你使用Rxjava时,内存泄漏了吗?
- 使用ADB遇到的问题
- android databinding不出现具体错误':app:compileDebugJavaWithJavac'. > Compilation failed;
- 申请深圳户口真他妈麻烦
- lintcode Permutation Index
- 【ArcGIS】利用ArcGIS 的Java API实现SDE数据库的直连和服务连接
- Go的组合继承
- 我使用过的框架的基本涵盖所有版本CDN网址
- 最小生成树 堆优化的Prim算法
- jsp 基础语法
- throw和throws的区别
- 各个JAVA场景下的内存图
- CodeForces 834C The Meaningless Game