题目描述
给定某产品多少天的价格,记录于prices中,请找出任意两天之间的最小价格差(即abs(prices[i] - prices[j]))的最小值,i!=j)并计算最小介个差组合的个数
样例1
输入
[1,3,7,5,12]
输出
3
样例2
输入
[90,98,90,90,1,1]
输出
4
Java源码
import java.nio.charset.StandardCharsets;
import java.util.Scanner;public class MinPriceDiff {public static void main(String[] args) {Scanner cin = new Scanner(System.in, StandardCharsets.UTF_8.name());while (cin.hasNext()) {String[] inputStr = cin.nextLine().split(",");int[] prices = new int[inputStr.length];for (int i = 0; i < inputStr.length; i++) {prices[i] = Integer.parseInt(inputStr[i]);}System.out.println(getMinPriceDiff(prices));}}static int getMinPriceDiff(int[] prices) {int n = prices.length;// 最小差值int minDiff = Integer.MAX_VALUE;// 最小差值组数int count = 0;for (int i = 0; i < n; i++) {for (int j = i + 1; j < n; j++) {// 差值int diff = Math.abs(prices[i] - prices[j]);// 若果差值小于最小差值计数初始化为1if (diff < minDiff) {minDiff = diff;count = 1;} else if (diff == minDiff) {// 如果差值等于最小差值则计数+1count++;}}}return count;}
}