Hi, I am doing today's February Challenge Question of Largest Harmonius Subsequence. I am doing a brute force method with some optimisations, it is passing 29 test cases but I get either a Memory Limit Exceeded Error or a Time Limit Exceeded Error for the rest.
Here is my code:
import itertools
class Solution:
def findLHS(self, nums: List[int]) -> int:
# All possibilities
all_pos = list()
for i in range(2, len(nums) + 1):
for subset in itertools.combinations(nums, i):
temp = list(subset)
if max(temp) - min(temp) == 1:
all_pos.append(temp)
# Iterate through them and find longest
length = 0
if len(all_pos) > 0:
all_pos.sort(key= lambda s: len(s))
length = len(all_pos[-1])
return length
Any help would be appreciated!