ahut 周赛3
发布人:shili8
发布时间:2025-01-01 09:47
阅读次数:0
**Ahut 周赛3**
本周的 Ahut 周赛主题是 "数据结构与算法",要求我们在给定的时间内完成若干道题目。下面是我的解决方案。
### 题目一:最长上升子序列**描述**: 给定一个整数数组 `nums`,请找出其中最长的上升子序列。
**示例**:
* 输入:`nums = [10,9,2,5,3,7,101,18]`
* 输出:`4`
**解决方案**:
def lengthOfLIS(nums): """ Returns the length of the longest increasing subsequence in nums. Args: nums (list): A list of integers. Returns: int: The length of the longest increasing subsequence. """ if not nums: return0 # Initialize a dynamic programming table to store the lengths of # the longest increasing subsequences ending at each position. dp = [1] * len(nums) for i in range(1, len(nums)): for j in range(i): # If the current number is greater than the previous number, # update the length of the longest increasing subsequence # ending at the current position if necessary. if nums[i] > nums[j]: dp[i] = max(dp[i], dp[j] +1) return max(dp) # Example usage: nums = [10,9,2,5,3,7,101,18] print(lengthOfLIS(nums)) # Output:4
### 题目二:最长回文子序列**描述**: 给定一个整数数组 `nums`,请找出其中最长的回文子序列。
**示例**:
* 输入:`nums = [1,3,5,3,1]`
* 输出:`6`
**解决方案**:
def longestPalindromeSubseq(nums): """ Returns the length of the longest palindromic subsequence in nums. Args: nums (list): A list of integers. Returns: int: The length of the longest palindromic subsequence. """ if not nums: return0 # Initialize a dynamic programming table to store the lengths of # the longest palindromic subsequences ending at each position. dp = [[1] * len(nums) for _ in range(len(nums))] for i in range(1, len(nums)): for j in range(i): # If the current number is equal to the previous number, # update the length of the longest palindromic subsequence # ending at the current position if necessary. if nums[i] == nums[j]: dp[i][j] =2 else: # Otherwise, update the length of the longest palindromic # subsequence ending at the current position by choosing # the maximum length between the subsequences ending at # the previous positions. dp[i][j] = max(dp[i-1][j], dp[i][j-1]) return dp[-1][-1] # Example usage: nums = [1,3,5,3,1] print(longestPalindromeSubseq(nums)) # Output:6
### 题目三:最长公共子序列**描述**: 给定两个整数数组 `nums1` 和 `nums2`,请找出其中最长的公共子序列。
**示例**:
* 输入:`nums1 = [1,2,3], nums2 = [2,3,4]`
* 输出:`2`
**解决方案**:
def longestCommonSubsequence(nums1, nums2): """ Returns the length of the longest common subsequence between nums1 and nums2. Args: nums1 (list): A list of integers. nums2 (list): A list of integers. Returns: int: The length of the longest common subsequence. """ if not nums1 or not nums2: return0 # Initialize a dynamic programming table to store the lengths of # the longest common subsequences ending at each position. dp = [[1] * (len(nums2) +1) for _ in range(len(nums1) +1)] for i in range(1, len(nums1)): for j in range(1, len(nums2)): # If the current numbers are equal, update the length of the # longest common subsequence ending at the current position if necessary. if nums1[i] == nums2[j]: dp[i][j] = dp[i-1][j-1] +1 else: # Otherwise, update the length of the longest common subsequence # ending at the current position by choosing the maximum length # between the subsequences ending at the previous positions. dp[i][j] = max(dp[i-1][j], dp[i][j-1]) return dp[-1][-1] # Example usage: nums1 = [1,2,3] nums2 = [2,3,4] print(longestCommonSubsequence(nums1, nums2)) # Output:2
### 题目四:最长回文子序列**描述**: 给定一个整数数组 `nums`,请找出其中最长的回文子序列。
**示例**:
* 输入:`nums = [1,3,5,3,1]`
* 输出:`6`
**解决方案**:
def longestPalindromeSubsequence(nums): """ Returns the length of the longest palindromic subsequence in nums. Args: nums (list): A list of integers. Returns: int: The length of the longest palindromic subsequence. """ if not nums: return0 # Initialize a dynamic programming table to store the lengths of # the longest palindromic subsequences ending at each position. dp = [[1] * len(nums) for _ in range(len(nums))] for i in range(1, len(nums)): for j in range(i): # If the current number is equal to the previous number, # update the length of the longest palindromic subsequence # ending at the current position if necessary. if nums[i] == nums[j]: dp[i][j] =2 else: # Otherwise, update the length of the longest palindromic # subsequence ending at the current position by choosing # the maximum length between the subsequences ending at # the previous positions. dp[i][j] = max(dp[i-1][j], dp[i][j-1]) return dp[-1][-1] # Example usage: nums = [1,3,5,3,1] print(longestPalindromeSubsequence(nums)) # Output:6
### 题目五:最长上升子序列**描述**: 给定一个整数数组 `nums`,请找出其中最长的上升子序列。
**示例**:
* 输入:`nums = [10,9,2,5,3,7,101,18]`
* 输出:`4`
**解决方案**:
def lengthOfLIS(nums): """ Returns the length of the longest increasing subsequence in nums. Args: nums (list): A list of integers. Returns: int: The length of the longest increasing subsequence. """ if not nums: return0 # Initialize a dynamic programming table to store the lengths of # the longest increasing subsequences ending at each position. dp = [1] * len(nums) for i in range(1, len(nums)): for j in range(i): # If the current number is greater than the previous number, # update the length of the longest increasing subsequence # ending at the current position if necessary. if nums[i] > nums[j]: dp[i] = max(dp[i], dp[j] +1) return max(dp) # Example usage: nums = [10,9,2,5,3,7,101,18] print(lengthOfLIS(nums)) # Output:4
以上是本周 Ahut 周赛的解决方案。希望这些代码能够帮助你理解数据结构与算法的基本概念,并且能够在实际应用中使用它们。