Cho danh sách số, tìm tích lớn nhất của hai phần tử khác nhau.
Ví dụ
Đầu vào: [5, 3, 7, 4].
Đầu ra: 35 (5*7).
Thuật toán
- Khởi tạo
curr_max = -inf. - Duyệt tất cả cặp
(i, j)vớii < j, cập nhậtcurr_max. - Trả về
curr_max.
Mã nguồn Python
class Solution:
def solve(self, nums):
curr_max = float('-inf')
for i in range(len(nums)):
for j in range(i+1, len(nums)):
if nums[i] * nums[j] > curr_max:
curr_max = nums[i] * nums[j]
return curr_max
ob = Solution()
print(ob.solve([5, 3, 7, 4]))
Đầu vào / Đầu ra
Input: [5, 3, 7, 4]
Output: 35