标签:
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