Difficulty: Easy
So my initial thoughts after reading this question, I thought this was a sliding window question as I thought i
, j
, k
was continuous, however, it does not have to be and once again, I am reminded I’m goofy.
We can still go over this as a sliding window problem but it would be a little bit more complicated than I initially thought..
I reread the question and noticed that a brute force implementation would work here since the length of the array is restricted to 100
numbers.
For brute force: I can iterate through all possible i
, j
, k
and keep track of the maximum value.
Return the maximum value at the end.
class Solution(object):
def maximumTripletValue(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
n = len(nums)
res = 0
for i in range(n - 2):
for j in range(i + 1, n - 1):
for k in range(j + 1, n):
value = (nums[i] - nums[j]) * nums[k]
res = max(res, value)
return res
Time Complexity:
O(n^3)
Space Complexity:
O(1)
Time Taken:
3m 56s
Extra: Brute force way is kind of boring but I could not think of that optimal solution in the editorial solution… it does seem obvious once you see it though