Grokking Dynamic Programming Patterns for Coding Interviews
Ask Author
Back to course home

0% completed

Vote For New Content
Mohammed Dh Abbas
My solution

Mohammed Dh Abbas

Nov 30, 2024

class Solution: def findLASLength(self, nums): # DP array that stores LAS and index i and relation < or > between consecutive numbers longest_alt = [(1, '.')] * len(nums) # . is an initial value for i in range(1, len(nums)): if nums[i] < nums[i - 1]: # search for the > and take the LAS + 1 for j in range(i - 1, - 1, - 1): if longest_alt[j][1] == '>' or longest_alt[j][1] == '.': longest_alt[i] = (longest_alt[j][0] + 1, '<') break elif nums[i] > nums[i - 1]: # search for the < and take the LAS + 1 for j in range(i - 1, - 1, - 1): if longest_alt[j][1] == '<' or longest_alt[j][1] == '.': longest_alt[i] = (longest_alt[j][0] + 1, '>') break else: # clone the previous result longest_alt[i] = longest_alt[i - 1] return longest_alt[-1][0]

0

0

Comments
Comments

On this page