Leetcode Note: Go - Maximum Product of Two Elements in an Array

Maximum Product of Two Elements in an Array - LeetCode
https://leetcode.com/problems/maximum-product-of-two-elements-in-an-array/

  • Go 言語で取り組んだメモ

回答

golang 100% speed using something like PQ - Maximum Product of Two Elements in an Array - LeetCode
https://leetcode.com/problems/maximum-product-of-two-elements-in-an-array/solutions/712797/golang-100-speed-using-something-like-pq/

func maxProduct(nums []int) int {
	res := [2]int{}
	for i := 0; i < len(nums); i++ {
		if nums[i] > res[0] {
			res[0] = nums[i]
			if res[0] > res[1] { // sink
				res[0], res[1] = res[1], res[0]
			}
		}
	}
	return (res[1]-1) * (res[0]-1)
}