LeetCode C++ 1464. Maximum Product of Two Elements in an Array【Array/Sort】簡單
Given the array of integers nums
, you will choose two different indices i
and j
of that array. Return the maximum value of (nums[i] - 1) * (nums[j] - 1)
.
Example 1:
Input: nums = [3,4,5,2]
Output: 12
Explanation: If you choose the indices i=1 and j=2 (indexed from 0), you will get the maximum value, that is, (nums[1]-1)*(nums[2]-1) = (4-1)*(5-1) = 3*4 = 12.
Example 2:
Input: nums = [1,5,4,5]
Output: 16
Explanation: Choosing the indices i=1 and j=3 (indexed from 0), you will get the maximum value of (5-1)*(5-1) = 16.
Example 3:
Input: nums = [3,7]
Output: 12
Constraints:
2 <= nums.length <= 500
1 <= nums[i] <= 10^3
題意:給定一個整數陣列 nums
,選擇陣列的兩個不同下標 i
和 j
,使 (nums[i] - 1) * (nums[j] - 1)
取得最大值並返回。
解法1 暴力
class Solution {
public:
int maxProduct(vector<int>& nums) {
int mv = 0, n = nums.size();
for (int i = 0; i < n; ++i)
for (int j = i + 1; j < n; ++j)
mv = max(mv, (nums[i] - 1) * (nums[j] - 1));
return mv;
}
};
執行效率很低:
執行用時:60 ms, 在所有 C++ 提交中擊敗了6.69% 的使用者
記憶體消耗:10.2 MB, 在所有 C++ 提交中擊敗了13.32% 的使用者
解法2 排序
class Solution {
public:
int maxProduct(vector<int>& nums) {
sort(nums.begin(), nums.end(), greater<int>());
return (nums[0] - 1) * (nums[1] - 1);
}
};
執行效率如下:
執行用時:8 ms, 在所有 C++ 提交中擊敗了94.49% 的使用者
記憶體消耗:10 MB, 在所有 C++ 提交中擊敗了33.58% 的使用者
解法3 線性比較
class Solution {
public:
int maxProduct(vector<int>& nums) {
int max = 0, sec = 0, n = nums.size();
for (int i = 0; i < n; ++i) {
if (nums[i] > max) sec = max, max = nums[i];
else if (nums[i] > sec) sec = nums[i];
}
return (max - 1) * (sec - 1);
}
};
執行效率如下:
執行用時:4 ms, 在所有 C++ 提交中擊敗了99.53% 的使用者
記憶體消耗:10.2 MB, 在所有 C++ 提交中擊敗了12.91% 的使用者
相關文章
- Leetcode[421] Maximum XOR of Two Numbers in an ArrayLeetCode
- Leetcode Sort ArrayLeetCode
- LeetCode:Product of Array Except SelfLeetCode
- Sort Array By Parity
- LeetCode 350 [Intersection of Two Array II]LeetCode
- Median of Two Sorted Array leetcode javaLeetCodeJava
- Leetcode 238. Product of Array Except SelfLeetCode
- [LeetCode] 238. Product of Array Except SelfLeetCode
- LeetCode之Sort Array By Parity(Kotlin)LeetCodeKotlin
- Cat, Fox and Maximum Array Split
- 【Leetcode】453. Minimum Moves to Equal Array ElementsLeetCode
- Leetcode 453. Minimum Moves to Equal Array ElementsLeetCode
- Leetcode Maximum Product SubarrayLeetCode
- 【Leetcode】167. Two Sum II - Input array is sortedLeetCode
- Array.sort排序問題排序
- Leetcode-Maximum Product SubarrayLeetCode
- [CareerCup] 17.6 Sort Array 排列陣列陣列
- C# 字典排序Array.SortC#排序
- LeetCode-Maximum Product of Word LengthsLeetCode
- [leetCode][001] Maximum Product SubarrayLeetCode
- LeetCode | 152. Maximum Product SubarrayLeetCode
- Rotate Array@LeetCodeLeetCode
- Leetcode - Patching ArrayLeetCode
- LeetCode-Rotate ArrayLeetCode
- LeetCode-Shuffle an ArrayLeetCode
- .NET 排序 Array.Sort<T> 實現分析排序
- JavaScript object array sort by string bug All In OneJavaScriptObject
- LeetCode-Pathcing ArrayLeetCode
- Leetcode Search in Rotated Sorted ArrayLeetCode
- Leetcode Merge Sorted ArrayLeetCode
- leetcode刷題--Rotate ArrayLeetCode
- array new 與 array deletedelete
- Leetcode每日一題:992.sort-array-by-parity-ii(按奇偶排序陣列Ⅱ)LeetCode每日一題排序陣列
- PHP array_flip() array_merge() array+array的使用總結PHP
- LeetCode #1:Two Sum(簡單題)LeetCode
- JavaScript中陣列Array.sort()排序方法詳解JavaScript陣列排序
- js中陣列(Array)的排序(sort)注意事項JS陣列排序
- LeetCode Kth Largest Element in an ArrayLeetCode