comparison asyncio_threads/frontend/longest_subinteger.py @ 48:46daba6e3cf4

Few python scrtips to show how to use asychio.
author MrJuneJune <me@mrjunejune.com>
date Sat, 13 Dec 2025 14:23:02 -0800
parents
children
comparison
equal deleted inserted replaced
47:829623189a57 48:46daba6e3cf4
1 # Analyze a given array of integers and write a function to determine the length of the longest subsequent growing sequence contained within the array.
2
3
4 # 1,2,3,4,2,1,3
5 # |
6
7 # Example 1:
8 #
9 # Input: nums = [10,9,2,5,3,7,101,18]
10 # [(2, 1), 3, 5, 7, 9, 10, 18, 101]
11 # |
12 # Output: 4
13
14 # Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4.
15
16 # Example 2:
17 #
18 # Input: nums = [0,1,0,3,2,3]
19 # Output: 4
20 # Example 3:
21 #
22 # Input: nums = [7,7,7,7,7,7,7]
23 # Output: 1
24
25
26 # Longest Increasing Subsequence
27 def main(nums):
28 ans = 0
29 cache = set()
30
31 def dfs(val, pos, curr_ans):
32 nonlocal ans
33
34 if ((val, pos, curr_ans) in cache):
35 return
36
37 if pos > len(nums):
38 return
39 if nums[pos] > val:
40 curr_ans += 1
41 else:
42 ans = max(ans, curr_ans)
43 return
44 for i in range(pos, len(nums)):
45 dfs(nums[pos], i, curr_ans)
46
47 for i in range(len(nums)):
48 dfs(float("-inf"), i, 0)
49
50 return ans
51
52
53 print(main([10,9,2,5,3,7,101,18]))