File tree Expand file tree Collapse file tree 5 files changed +162
-0
lines changed
longest-common-subsequence
longest-increasing-subsequence Expand file tree Collapse file tree 5 files changed +162
-0
lines changed Original file line number Diff line number Diff line change 1+ """
2+ 55. Jump Game
3+ https://leetcode.com/problems/jump-game/
4+
5+ Solution:
6+ To solve this problem, we can use the greedy approach.
7+ We iterate through the array and keep track of the maximum index we can reach.
8+ If the current index is greater than the maximum index we can reach, we return False.
9+ Otherwise, we update the maximum index we can reach.
10+ If we reach the end of the array, we return True.
11+
12+ Time complexity: O(n)
13+ - We iterate through each element in the array once.
14+
15+ Space complexity: O(1)
16+ - We use a constant amount of extra space.
17+ """
18+
19+ from typing import List
20+
21+
22+ class Solution :
23+ def canJump (self , nums : List [int ]) -> bool :
24+ max_reach = 0
25+ for i , jump in enumerate (nums ):
26+ if i > max_reach :
27+ return False
28+ max_reach = max (max_reach , i + jump )
29+ return max_reach >= len (nums ) - 1
Original file line number Diff line number Diff line change 1+ """
2+ 1143. Longest Common Subsequence
3+ https://leetcode.com/problems/longest-common-subsequence
4+
5+ Solution:
6+ To solve this problem, we can use the dynamic programming approach.
7+ We create a 2D list to store the length of the longest common subsequence of the two strings.
8+ We iterate through the two strings and update the length of the longest common subsequence.
9+ We return the length of the longest common subsequence.
10+
11+ Time complexity: O(n1 * n2)
12+ - We iterate through each character in the two strings once.
13+ - The time complexity is O(n1 * n2) for updating the length of the longest common subsequence.
14+
15+ Space complexity: O(n1 * n2)
16+ - We use a 2D list to store the length of the longest common subsequence of the two strings.
17+ """
18+
19+
20+ class Solution :
21+ def longestCommonSubsequence (self , text1 : str , text2 : str ) -> int :
22+ n1 = len (text1 )
23+ n2 = len (text2 )
24+ dp = [[0 for i in range (n2 + 1 )] for j in range (n1 + 1 )]
25+ maximum = 0
26+ for i in range (1 , n1 + 1 ):
27+ for j in range (1 , n2 + 1 ):
28+ if text1 [i - 1 ] == text2 [j - 1 ]:
29+ dp [i ][j ] = dp [i - 1 ][j - 1 ] + 1
30+ else :
31+ dp [i ][j ] = max (dp [i - 1 ][j ], dp [i ][j - 1 ])
32+ maximum = max (dp [i ][j ], maximum )
33+
34+ return maximum
Original file line number Diff line number Diff line change 1+ """
2+ 300. Longest Increasing Subsequence
3+ https://leetcode.com/problems/longest-increasing-subsequence/
4+
5+ Solution:
6+ To solve this problem, we can use the dynamic programming approach.
7+ We create a list to store the longest increasing subsequence ending at each index.
8+ We iterate through the array and update the longest increasing subsequence ending at each index.
9+ We return the maximum length of the longest increasing subsequence.
10+
11+ Time complexity: O(n log n)
12+ - We iterate through each element in the array once.
13+ - We use the bisect_left function to find the position to insert the element in the sub list.
14+ - The time complexity is O(n log n) for inserting elements in the sub list.
15+
16+ Space complexity: O(n)
17+ - We use a list to store the longest increasing subsequence ending at each index.
18+ """
19+
20+ from typing import List
21+ from bisect import bisect_left
22+
23+
24+ class Solution :
25+ def lengthOfLIS (self , nums : List [int ]) -> int :
26+ sub = []
27+ for num in nums :
28+ pos = bisect_left (sub , num )
29+ if pos < len (sub ):
30+ sub [pos ] = num
31+ else :
32+ sub .append (num )
33+ return len (sub )
Original file line number Diff line number Diff line change 1+ """
2+ 53. Maximum Subarray
3+ https://leetcode.com/problems/maximum-subarray
4+
5+ Solution:
6+ To solve this problem, we can use the Kadane's algorithm.
7+ We keep track of the current sum and the maximum sum.
8+ We iterate through the array and update the current sum and maximum sum.
9+ If the current sum is greater than the maximum sum, we update the maximum sum.
10+ We return the maximum sum at the end.
11+
12+ Time complexity: O(n)
13+ - We iterate through each element in the array once.
14+
15+ Space complexity: O(1)
16+ - We use a constant amount of extra space.
17+ """
18+
19+
20+ from typing import List
21+
22+
23+ class Solution :
24+ def maxSubArray (self , nums : List [int ]) -> int :
25+ current_sum = max_sum = nums [0 ]
26+
27+ for num in nums [1 :]:
28+ current_sum = max (num , current_sum + num )
29+ max_sum = max (max_sum , current_sum )
30+
31+ return max_sum
Original file line number Diff line number Diff line change 1+ """
2+ 62. Unique Paths
3+ https://leetcode.com/problems/unique-paths/
4+
5+ Solution:
6+ To solve this problem, we can use the combinatorics approach.
7+ We can calculate the number of unique paths using the formula C(m+n-2, m-1).
8+ We can create a helper function to calculate the factorial of a number.
9+ We calculate the numerator and denominator separately and return the result.
10+
11+ Time complexity: O(m+n)
12+ - We calculate the factorial of m+n-2, m-1, and n-1.
13+ - The time complexity is O(m+n) for calculating the factorials.
14+
15+ Space complexity: O(1)
16+ - We use a constant amount of extra space.
17+ """
18+
19+
20+ class Solution :
21+ def uniquePaths (self , m : int , n : int ) -> int :
22+ def factorial (num ):
23+ previous = 1
24+ current = 1
25+ for i in range (2 , num + 1 ):
26+ current = previous * i
27+ previous = current
28+ return current
29+
30+ max_num = m + n - 2
31+ numerator = factorial (max_num )
32+ factorial_m_minus_1 = factorial (m - 1 )
33+ factorial_n_minus_1 = factorial (n - 1 )
34+ result = numerator // (factorial_m_minus_1 * factorial_n_minus_1 )
35+ return result
You can’t perform that action at this time.
0 commit comments