Re: [閒聊] 每日LeetCode已回收
https://leetcode.com/problems/maximum-product-of-two-elements-in-an-array/description
1464. Maximum Product of Two Elements in an Array
給你一個陣列找出任意兩個數-1相乘的最大值。
思路:
1.找最大的兩個數減一之後相乘
Java Code:
-------------------------------------
class Solution {
public int maxProduct(int[] nums) {
int max1 = Integer.MIN_VALUE;
int max2 = Integer.MIN_VALUE;
for (int num : nums) {
if (num > max1) {
max2 = max1;
max1 = num;
} else if (num > max2) {
max2 = num;
}
}
return (max1 - 1) * (max2 - 1);
}
}
-------------------------------------
LeetCode溫柔善良月
--
https://i.imgur.com/PIoxddO.jpg

--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 60.250.69.212 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1702349557.A.21D.html
推
12/12 11:05,
2年前
, 1F
12/12 11:05, 1F
→
12/12 11:15,
2年前
, 2F
12/12 11:15, 2F
討論串 (同標題文章)
完整討論串 (本文為第 574 之 719 篇):