最大下标距离(算法)
2015-07-13 20:18
204 查看
给定一个整形数组,找出最大下标距离j - i,当且仅当 a[i] <a[j] 和 j >i
int maxIndexDistance(int a[]) { if(a == null || a.length < 2) return 0; boolean inDescSeq[] = new boolean[a.length]; int min = a[0],n = a.length; inDescSeq[0] = true; for(int i = 0; i < n; i++) { if(a[i] < min) { inDescSeq[i] = true; min = a[i]; } } int maxDist = 0,i = n -1,j = n -1; while(i > = 0) { if(inDescSeq[i ] == false) { i--; continue; } while(a[j] < a[i] && j > i) { j --; } if((j - i) > maxDist && j > i) { maxDist = j - i; } i --; } return maxDist; }
相关文章推荐
- swift 集合类
- 浅谈继承关系和接口
- 二维“有序”数组查找问题的解决
- 【上海交大oj】能量项链(动态规划)
- vs2010下C++调用lib或dll文件
- const 和 非const函数重载
- LeetCode Lowest Common Ancestor of a Binary Tree
- Const 重载解析
- const 与重载
- Codeforces Round #305 (Div. 1) B. Mike and Feet
- HDU 1502 Regular Words(DP + JAVA)
- Linux读取目录函数readdir以及inode结构简介
- GitHub 使用手册 - 基础篇
- 解决 HttpURLConnection类中getContentLength()方法返回-1问题
- POJ 2226 最小点覆盖(经典建图)
- GitHub for Mac
- readdir(系统调用)
- 连接IBM MQ原因码报2035的错误解决办法
- 【手势交互】3. Leap Motion
- LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏