일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
- Array
- leetcode1880
- translate
- 청년전용전세대출
- javascript
- sort numbers
- Sorting
- Kagnew
- 1859
- Python
- a
- 1913
- 청년전세대출
- Ethiopian veterans
- leetcode832
- 청년맞춤형전세대출
- maketrans
- Ethiopian Veterans of the Korean War
- Sort
- 버팀목청년전세대출
- 중기청대출
- string
- revers
- 청년무직자전세대출
- 청년전세
- LeetCode
- REVERSE
- python3
- 1880
- Today
- Total
Momentary Comfort
[LeetCode][JavaScript] | 1913. Maximum Product Difference Between Two Pairs 본문
[LeetCode][JavaScript] | 1913. Maximum Product Difference Between Two Pairs
Sujeong Ji 2021. 7. 10. 00:22The 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.
Example 1:
Input: nums = [5,6,2,7,4]
Output: 34
Explanation: We can choose indices 1 and 3 for the first pair (6, 7) and indices 2 and 4 for the second pair (2, 4). The product difference is (6 * 7) - (2 * 4) = 34.
Example 2:
Input: nums = [4,2,5,9,7,4,8]
Output: 64
Explanation: We can choose indices 3 and 6 for the first pair (9, 8) and indices 1 and 5 for the second pair (2, 4). The product difference is (9 * 8) - (2 * 4) = 64.
Constraints:
- 4 <= nums.length <= 104
- 1 <= nums[i] <= 104
var maxProductDifference = function(nums) {
nums.sort((a, b) => a - b);
return (nums[nums.length - 1] * nums[nums.length - 2]) - (nums[0] * nums[1])
};
// (runtime / memory)
// 96 ms / 41.9 MB
var maxProductDifference = function(nums) {
nums = nums.sort(compare)
let n = nums.length
return nums[n - 1] * nums[n - 2] - nums[0] * nums[1]
function compare(a, b) {
return a - b;
}
};
// (runtime / memory)
// 132 ms / 41.9 MB
Click the link if you want to check a solution using Python: https://momentarycomfort.tistory.com/698
[LeetCode][Python] | 1859. Sorting the Sentence
A sentence is a list of words that are separated by a single space with no leading or trailing spaces. Each word consists of lowercase and uppercase English letters. A sentence can be shuffled b..
momentarycomfort.tistory.com
'Algorithm' 카테고리의 다른 글
[LeetCode][Python] | 1929. Concatenation of Array (0) | 2021.07.17 |
---|---|
[LeetCode][Python] | 1913. Maximum Product Difference Between Two Pairs (0) | 2021.07.10 |
[LeetCode][JavaScript] | 1859. Sorting the Sentence (0) | 2021.06.22 |
[LeetCode][Python] | 1859. Sorting the Sentence (0) | 2021.06.22 |