1035. Uncrossed Lines

题目:

We write the integers of nums1 and nums2 (in the order they are given) on two separate horizontal lines.

Now, we may draw connecting lines: a straight line connecting two numbers nums1[i] and nums2[j] such that:

  • nums1[i] == nums2[j];
  • The line we draw does not intersect any other connecting (non-horizontal) line.

Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line.

Return the maximum number of connecting lines we can draw in this way.

 

Example 1:

Input: nums1 = [1,4,2], nums2 = [1,2,4]
Output: 2
Explanation: We can draw 2 uncrossed lines as in the diagram.
We cannot draw 3 uncrossed lines, because the line from nums1[1]=4 to nums2[2]=4 will intersect the line from nums1[2]=2 to nums2[1]=2.

Example 2:

Input: nums1 = [2,5,1,2,5], nums2 = [10,5,2,1,5,2]
Output: 3

Example 3:

Input: nums1 = [1,3,7,1,7,5], nums2 = [1,9,2,5,1]
Output: 2

 

Note:

  1. 1 <= nums1.length <= 500
  2. 1 <= nums2.length <= 500
  3. 1 <= nums1[i], nums2[i] <= 2000

 

 

 

 

思路:

如果用常规思路贪心的话很明显会错并且复杂度不好判断,这里显然当作字符串处理用DP。首先建立二维DP数组,初始化为0即可,假设最开始一对没有。之后从左到右从上到下遍历,设定row是A,col是B,那么当某A[i]和B[j]相等时,可以增加一种连法,否则当前格子的DP值就取左或者上的最大值即可。本题不是特别难,之要想清楚用DP即可。

 

 

 

 

 

代码:

class Solution {
public:
    int maxUncrossedLines(vector<int>& A, vector<int>& B) {
        vector<vector<int>> dp(A.size()+1, vector<int>(B.size()+1, 0));
        for(int i = 0; i < A.size(); i++){
            for(int j = 0; j < B.size(); j++){
                if(A[i] == B[j]){
                    dp[i+1][j+1] = dp[i][j] + 1;
                }else
                    dp[i+1][j+1] = max(dp[i][j+1], dp[i+1][j]);
            }
        }
        return dp[A.size()][B.size()];
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`pygame.draw.lines()` 函数用于在 Pygame 窗口上绘制多条连接的线段。 函数原型如下: ```python pygame.draw.lines(surface, color, closed, pointlist, width=1) ``` 参数说明: - `surface`:要绘制线段的 Surface 对象。 - `color`:线段的颜色,可以是 RGB 元组 `(R, G, B)` 或者通过 `pygame.Color()` 创建的颜色对象。 - `closed`:一个布尔值,表示是否连接起点和终点以形成封闭的多边形。若为 `True`,则会连接起点和终点;若为 `False`,则不连接起点和终点。 - `pointlist`:一个包含多个点坐标的列表,用于指定线段的路径。每个点坐标是一个包含两个整数的元组 `(x, y)`。 - `width`(可选):线段的宽度,默认为 1。 注意事项: - `pointlist` 中的点坐标应按照线段的路径顺序排列。 - 若 `closed` 参数为 `True`,则最后一个点坐标会自动与第一个点坐标连接。 以下是一个示例代码,演示如何使用 `pygame.draw.lines()` 函数绘制多条连接的线段: ```python import pygame # 初始化 Pygame pygame.init() # 创建窗口 screen = pygame.display.set_mode((400, 300)) # 设置窗口标题 pygame.display.set_caption("Lines Demo") # 设置线段的路径 points = [(50, 50), (100, 100), (150, 50), (200, 100), (250, 50), (300, 100)] running = True while running: # 填充窗口背景色 screen.fill((255, 255, 255)) # 绘制线段 pygame.draw.lines(screen, (0, 0, 255), False, points, 2) # 更新窗口显示 pygame.display.flip() # 处理事件 for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # 退出 Pygame pygame.quit() ``` 运行以上代码,会在窗口中绘制一条连接的蓝色线段。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值