当前位置:网站首页>Yyds dry goods inventory # solve the real problem of famous enterprises: maximum difference

Yyds dry goods inventory # solve the real problem of famous enterprises: maximum difference

2022-07-07 02:16:00 51CTO

1. sketch :

describe

There's a long one for n Array of A , Please 0 ≤ a ≤ b < n Of A[b] - A[a] The maximum of .

Given array  A  And its size  n , Please return the maximum difference .

Data range : #yyds Dry inventory # Solve the real problems of famous enterprises : Maximum difference _ integer  , The values in the array meet  #yyds Dry inventory # Solve the real problems of famous enterprises : Maximum difference _ Array _02

Example 1

Input :

      
      
[5,1],2
  • 1.

Return value :

      
      
0
  • 1.
Example 2

Input :

      
      
[5,6],2
  • 1.

Return value :

      
      
1
  • 1.

2. Code implementation :

      
      
import java.util.*;


public class Solution {
/**
* The class name in the code 、 Method name 、 The parameter name has been specified , Do not modify , Return the value specified by the method directly
*
*
* @param A int Integer one-dimensional array
* @param n int integer
* @return int integer
*/
public int getDis (int[] A, int n) {
// write code here
int max = 0; // The minimum result is 0
int min = A[0]; // The default minimum is the first
for(int i = 1 ; i < n; i ++){
min = Math.min(A[i], min); // The first is the comparison with traversal , Take the small one
max = Math.max(A[i] - min, max); // A[i] Subtract the smallest before A[i], Take the smallest
}
return max;

}
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
原网站

版权声明
本文为[51CTO]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207061833046382.html