Given an array a[] of N real numbers, design a linear-time algorithm to find the maximum value of a[j] - a[i] where j ≥ i.

 package algorithms.analysis14;

 public class Best {

     public static void main(String[] args) {
double[] a = {5.0, 4.0, 3.0 ,6.0,1.0};
int N = a.length;
double best = 0.0;
double min = a[0];
for (int i = 0; i < N; i++) {
min = Math.min(a[i], min);
best = Math.max(a[i] - min, best);
}
System.out.println(best);
System.out.println(min);
}
}

算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-007按位置,找出数组相关最大值的更多相关文章

  1. 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-005计测试算法

    1. package algorithms.analysis14; import algorithms.util.StdOut; import algorithms.util.StdRandom; / ...

  2. 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-002如何改进算法

    1. package algorithms.analysis14; import algorithms.util.In; import algorithms.util.StdOut; /******* ...

  3. 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-006BitonicMax

    package algorithms.analysis14; import algorithms.util.StdOut; import algorithms.util.StdRandom; /*** ...

  4. 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-004计算内存

    1. 2. 3.字符串

  5. 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-003定理

    1. 2. 3. 4. 5. 6.

  6. 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-001分析步骤

    For many programs, developing a mathematical model of running timereduces to the following steps:■De ...

  7. 算法Sedgewick第四版-第1章基础-001递归

    一. 方法可以调用自己(如果你对递归概念感到奇怪,请完成练习 1.1.16 到练习 1.1.22).例如,下面给出了 BinarySearch 的 rank() 方法的另一种实现.我们会经常使用递归, ...

  8. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-001选择排序法(Selection sort)

    一.介绍 1.算法的时间和空间间复杂度 2.特点 Running time is insensitive to input. The process of finding the smallest i ...

  9. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-007归并排序(自下而上)

    一. 1. 2. 3. 二.代码 package algorithms.mergesort22; import algorithms.util.StdIn; import algorithms.uti ...

随机推荐

  1. ps6-工具的基础使用

    1.图像的移动与对齐 ctrl+j:复制图层,然后再移动不损坏原来的图像. Ctrl+Z =返回键 Shift+单击最下方图层 选择全部 Alt+鼠标移动 复制并粘贴 2.规则选择工具组 shift键 ...

  2. git教程2-删除修改和文件

    文件处于三种状态: 1.位于工作区,未修改状态: 2.位于工作区,已经修改状态: 3.位于暂存区,已经暂存但未commit. 4.已经commit. 一.文件删除修改: 1.已经修改,但未add: g ...

  3. onclick调用函数的几种!

    ()是个操作,表示执行displayDate方法,你把displayDate方法执行完的返回值赋给onclick能对吗?onclick接受的是Function类型的变量,要么用匿名的方法赋值就是doc ...

  4. 一次调用cloudera的的API和JSON想到的

    JSON的null字段 在我的项目中修改了YARN的资源池,到cloudera中就报错,查询还不报错,但是修改的时候,就报错,大致意思就是信息有异常,“包含了尖括号. 因为通过API+json交互,我 ...

  5. 全球知名的HTTPS网站检测工具-Qualys SSL Labs

    推荐一个在线版全球知名的HTTPS网站检测工具-Qualys SSL Labs.Qualys SSL Labs同时也是很具有影响力的SSL安全和性能研究机构. SSL Labs会对HTTPS网站的证书 ...

  6. Python:常用正则表达式(一)

    文章转载于:http://www.cnblogs.com/Akeke/(博主:Akeke) https://www.cnblogs.com/Akeke/p/6649589.html (基于JavaSc ...

  7. 已有项目使用Asset Pipeline管理静态资源

    1.    修改项目中指向静态资源文件的链接 a) 访问静态资源文件 <%= stylesheet_link_tag "application", media: " ...

  8. C Primer Plus学习笔记(二)- 数据和C

    从一个简单的程序开始 #include <stdio.h> int main(void) { float weight; float value; printf("Please ...

  9. dtgrid 手动条件删除表格中的某一行

    dtgrid 手动条件删除表格中的某一行 var grid = $.fn.DtGrid.init(dtGridOption); $(function () { grid.load(); }); fun ...

  10. ClientDataSet + DataSetProvider + FDQuery 的bug

    ClientDataSet + DataSetProvider  +FDQuery 有 bug ClientDataSet + DataSetProvider  +ADOQuery正常. Client ...