首页 > 编程语言 > 详细

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

时间:2016-04-21 13:23:38      阅读:144      评论:0      收藏:0      [点我收藏+]

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.

 1 package algorithms.analysis14;
 2 
 3 public class Best {
 4 
 5     public static void main(String[] args) {
 6         double[] a = {5.0, 4.0, 3.0 ,6.0,1.0};
 7         int N = a.length;
 8         double best = 0.0;
 9         double min = a[0];
10         for (int i = 0; i < N; i++) {
11             min  = Math.min(a[i], min);
12             best = Math.max(a[i] - min, best);
13         }
14         System.out.println(best);
15         System.out.println(min);
16     }
17 }

 

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

原文:http://www.cnblogs.com/shamgod/p/5416406.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!