算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-007按位置,找出数组相关最大值
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按位置,找出数组相关最大值的更多相关文章
- 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-005计测试算法
1. package algorithms.analysis14; import algorithms.util.StdOut; import algorithms.util.StdRandom; / ...
- 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-002如何改进算法
1. package algorithms.analysis14; import algorithms.util.In; import algorithms.util.StdOut; /******* ...
- 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-006BitonicMax
package algorithms.analysis14; import algorithms.util.StdOut; import algorithms.util.StdRandom; /*** ...
- 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-004计算内存
1. 2. 3.字符串
- 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-003定理
1. 2. 3. 4. 5. 6.
- 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-001分析步骤
For many programs, developing a mathematical model of running timereduces to the following steps:■De ...
- 算法Sedgewick第四版-第1章基础-001递归
一. 方法可以调用自己(如果你对递归概念感到奇怪,请完成练习 1.1.16 到练习 1.1.22).例如,下面给出了 BinarySearch 的 rank() 方法的另一种实现.我们会经常使用递归, ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-001选择排序法(Selection sort)
一.介绍 1.算法的时间和空间间复杂度 2.特点 Running time is insensitive to input. The process of finding the smallest i ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-007归并排序(自下而上)
一. 1. 2. 3. 二.代码 package algorithms.mergesort22; import algorithms.util.StdIn; import algorithms.uti ...
随机推荐
- 配合Jenkins自动化构建,bat脚本(二)
批量通过模板,拷贝文件,然后替换模板文件中的标记位为预制的内容. 1 Set servicePath=Ehong.MedicareReview.Web\地区配置\ Set webPath=Ehong. ...
- C++结构体成员列表初始化
C++关于struct和class的区别,可以看上一篇文章:c ++ class和struct[转] 结构体成员列表初始化,来个例子: #include <iostream> #inclu ...
- Magic Index 寻找数组中A[i]=i的位置(原题转自微信号待字闺中)
有一个有意思的题目叫做Magic Index:给定一个数组A,其中有一个位置被称为Magic Index,含义是:如果i是Magic Index,则A[i] = i.假设A中的元素递增有序.且不重复, ...
- C#面向对象(二):封装和继承
前文链接:C#面向对象(一):明确几个简单的概念作为开胃菜 面向对象开发有三大特性(特点 / 特征) : 封装, 继承, 多态.我们今天主要讨论封装和继承,多态会在下篇中讨论. 一.封装: 所谓封装, ...
- BZOJ1280: Emmy卖猪pigs
BZOJ1280: Emmy卖猪pigs https://lydsy.com/JudgeOnline/problem.php?id=1280 分析: 这题感觉还好,因为是有时间顺序,所以拆点做最大流即 ...
- Java处理乱码问题
中文乱码分为GET乱码和POST乱码 GET乱码在Tomcat中配置编码 <Connector port="8080" protocol="HTTP/1.1&quo ...
- xftp连接不上阿里云服务器
打开xftp默认是使用FTP协议,要连接到云服务器,需要将协议改为SFTP 连接成功
- 转:Oracle下创建ASM磁盘总结
Oracle下创建ASM磁盘总结 文章转载:https://blog.csdn.net/okhymok/article/details/78791841?utm_source=blogxgwz1 2. ...
- tomcat 基础知识学习
1: 直接将web项目文件件拷贝到webapps 目录中,Tomcat的Webapps目录是Tomcat默认的应用目录,当服务器启动时,会加载所有这个目录下的应用,所以可以将JSP程序打包成一个 wa ...
- c# 实用精华知识点全解
本文介绍c#的实用知识点 写在前面(通识) vs常用快捷键 F5 调试运行程序 ctrl F5 不调试运行程序 F11 逐条语句调试 F10 逐过程调试程序 注释快捷键 ctrl + k + c 代码 ...