How to Solve "1464 Maximum Product of Two Elements in an Array" on LeetCode? - Javascript

Опубликовано: 05 Октябрь 2024
на канале: Coding David
231
2

Do you need more help with coding?
════════════════════════════
✅ Apply for 1-1 coaching https://form.jotform.com/230156286763056
════════════════════════════

How to Solve "1464 Maximum Product of Two Elements in an Array" on LeetCode?

Problem: #1464 Maximum Product of Two Elements in an Array
Language: Javascript
Difficulty: Easy

Strategy: Create variable to hold max value and 2nd max value.
Time Complexity: O(n)
Space Complexity: O(1)

Pseudo Code:
1. Create a max variable.
2. Create a max2 variable.
3. Loop through nums.
a. Create condition if current index value is greater than max and max2.
i. Change max2 to max
ii. Change the max to current index value.
b. Else if the current index value is greater than max2.
i. Change max2 to the current index value.
4. Return (max -1) * (max2 -1)

GitHub Repo:
https://github.com/anusontarangkul/le...

Let's Connect 💯:
════════════════════════════
LinkedIn:   / anusontarangkul  
TikTok:   / david.anu_  
Instagram:   / david.anu_  
GitHub: https://github.com/anusontarangkul