728x90
반응형
✨ 문제
Maximum product of Two Elements in an Array
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).
https://leetcode.com/problems/maximum-product-of-two-elements-in-an-array/description/
✨ 개념
Heap
- 힙은 배열을 기반으로 생성.
PriorityQueue
- 우선순위 큐
✨ 최종코드
public int maxProduct(int[] nums) {
Arrays.sort(nums);
return (nums[nums.length - 1] - 1) * (nums[nums.length - 2] - 1);
}
우선 Arrays의 sort()를 이용해 nums를 정렬한 뒤 nums의 (가장 마지막 인덱스)와 (가장 마지막 인덱스) - 1의 값에서 각각 -1 해준 값을 곱하여 리턴하도록 하였다.
728x90
반응형
'알고리즘 > 99클럽' 카테고리의 다른 글
99클럽 코테 스터디 7일차 TIL + Brute Force 브루트포스, 완전 탐색 (0) | 2024.05.27 |
---|---|
99클럽 코테 스터디 6일차 TIL + Heap, PriorityQueue 우선순위 큐 (0) | 2024.05.26 |
99클럽 코테 스터디 4일차 TIL + PriorityQueue (0) | 2024.05.24 |
99클럽 코테 스터디 3일차 TIL + Stack (0) | 2024.05.23 |
99클럽 코테 스터디 2일차 TIL + Queue, LinkedList (0) | 2024.05.22 |