The product difference between two pairs (a, b) and (c, d) is defined as (a * b) - (c * d).
For example, the product difference between (5, 6) and (2, 7) is (5 * 6) - (2 * 7) = 16.
Given an integer array nums, choose four distinct indices w, x, y, and z such that the product difference between pairs (nums[w], nums[x]) and (nums[y], nums[z]) is maximized.
Return the maximum such product difference.
翻譯時間:
我的解題思路是這樣:
印象中有看過可以按照大小排列數字的語法,
後來去查了一下,的確有這種事:Arrays.sort(數組)
所以很簡單地完成了第一步~
Arrays.sort(nums);
然後取出最大的兩個數與最小的兩個數
int max1 = nums[nums.length - 1]; // 最大值
int max2 = nums[nums.length - 2]; // 次大值
int min1 = nums[0]; // 最小值
int min2 = nums[1]; // 次小值
計算乘積差
return (max1 * max2) - (min1 * min2);
丟去leetcode看結果,結束這回合
老實說我覺得這題是迄今為止最簡單的題目,
等假日時間充裕就可以練練看中等難度的題目了