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. MariaDB Galera Cluster环境搭建及高可用测试

    一.服务器概况Galera Cluster需要至少三个节点,在此次实验过程中,三个节点IP地址:192.168.56.101192.168.56.102192.168.56.103OS为centos ...

  2. python--匿名函数lambda

    Python语言使用lambda关键字来创建匿名函数. 所谓匿名,即不再使用def语句这样标准的形式定义一个函数. lambda只是一个表达式,而不是一个代码块,函数体比def简单很多. 仅仅能在la ...

  3. 20165210 Java第三周学习总结

    20165210 Java第三周学习总结 教材学习内容总结 - 第四章学习总结 编程语言的几个发展阶段: 面向机器语言 面向过程语言 面向对象语言 类: 类声明: class People { ... ...

  4. python虚拟开发环境搭建(virtualenv和virtualenvwrapper)

    虚拟开发环境的搭建 (0) 搭建虚拟环境的意义 使不同的开发环境独立 环境升级不影响其他开发环境,也不影响全局 防止包管理的混乱 (1) 指定 虚拟环境的创建目录 环境变量设置 创建 WORKON_H ...

  5. 观后感|当幸福来敲门 The Pursuit of Happyness

    更好的阅读体验请点击:当幸福来敲门 The Pursuit of Happyness 看到时光机点亮的那一刻,我想儿子克里斯托夫正在侏罗纪的世界内探险,看着山川河流,穿梭在恐龙的脚下,在山洞中安稳的度 ...

  6. swing之borderlayout

    import java.awt.BorderLayout; import javax.swing.JButton; import javax.swing.JFrame; //1.继承 JFrame类 ...

  7. WebSphere & Log4j

    IBM的东西,真是太麻烦了,一个日志都给你替换掉,太霸道了,Google了下,居然是个普遍想象,不过也有相关的解决方 案,基本好像都是在 WAS5,WAS6.1上的,我没环境,不过我这边的WAS7 没 ...

  8. Spark Shuffle大揭秘

    什么是Shuffle: Shuffle中文翻译为“洗牌”,需要Shuffle的关键原因是某种具有共同特征的数据需要最终汇聚到一个计算节点上进行计算. Shuffle面临的问题: 1. 数据量非常大: ...

  9. 一:HTML文本编译器 kindeditor-4.1.10 的使用 SpringMVC+jsp的实现

    一:我用的kindeditor版本是4.1.10 下载完成打开目录结构如下: 二:下面是工程目录也很重要,          三: 好了,准备工作已经做好了,现在就直接上代码了. 首先是页面JSP代码 ...

  10. Java-API-Package:javax.http.servlet

    ylbtech-Java-API-Package:javax.http.servlet 1.返回顶部 1. Package javax.servlet.http This chapter descri ...