算法训练营第五十八天 | LeetCode 392 判断子序列、卡码网模拟美团笔试第一、二、三题(300/500有待提高)

卡码网图论更新了可以去看看,模拟笔试第四题就是深搜/广搜还不太会

LeetCode 392 判断子序列


其实就是最长公共子序列翻版

代码如下:

class Solution {
    public boolean isSubsequence(String s, String t) {
        int[][] dp = new int[s.length() + 1][t.length() + 1];
        int result = 0;
        for (int i = 1; i <= s.length(); i++) {
            for (int j = 1; j <= t.length(); j++) {
                if (s.charAt(i-1) == t.charAt(j-1))
                    dp[i][j] = Math.max(dp[i][j], dp[i-1][j-1]+1);
                else dp[i][j] = Math.max(dp[i][j-1], dp[i-1][j]);
                if (result < dp[i][j]) result = dp[i][j];
            }
        }
        return result == s.length();
    }
}

模拟美团笔试第一题 小美的排列询问


简单模拟

代码如下:

#include <iostream>

using namespace std;

int main() {
    int n;
    cin >> n;
    int a[n];
    for (int i = 0; i < n; i++) cin >> a[i];
    int x, y;
    cin >> x >> y;
    for (int i = 0; i < n; i++) {
        if (a[i] == x || a[i] == y) {
            if (i + 1 < n && (a[i+1] == x || a[i+1] == y) && a[i+1] != a[i]) {
                cout << "Yes" << endl;
                return 0;
            } else break;
        }
    }
    cout << "No" << endl;
}

第二题 小美走公路


简单模拟

代码如下:

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int main() {
    int n;
    cin >> n;
    long long a[n];
    long long sum = 0;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        sum += a[i];
    }    
    int x, y;
    cin >> x >> y;
    if (x > y) {
        long long t = y;
        y = x;
        x = t;
    }
    if (x == y) {
        cout << 0 << endl;
        return 0;
    }
    long long cost = 0;
    for (int i = x; i < y; i++) {
        cost += a[i];
    }
    cost = min(cost, sum - cost);
    cout << cost << endl;
}

第三题 小美的蛋糕切割


二维前缀和

代码如下:

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int main() {
    int n;
    cin >> n;
    long long a[n];
    long long sum = 0;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        sum += a[i];
    }    
    int x, y;
    cin >> x >> y;
    if (x > y) {
        long long t = y;
        y = x;
        x = t;
    }
    if (x == y) {
        cout << 0 << endl;
        return 0;
    }
    long long cost = 0;
    for (int i = x; i < y; i++) {
        cost += a[i];
    }
    cost = min(cost, sum - cost);
    cout << cost << endl;
}

最近更新

  1. TCP协议是安全的吗?

    2024-06-14 09:10:01       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-14 09:10:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-14 09:10:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-14 09:10:01       18 阅读

热门阅读

  1. MySQL CDC

    2024-06-14 09:10:01       9 阅读
  2. 璨与序列 题解(stl,dfs)

    2024-06-14 09:10:01       8 阅读
  3. 后端主流框架--Spring

    2024-06-14 09:10:01       5 阅读
  4. 响应式网页开发方法与实践

    2024-06-14 09:10:01       6 阅读
  5. 602. 好友申请 II :谁有最多的好友

    2024-06-14 09:10:01       6 阅读
  6. AI学习指南机器学习篇-支持向量机模型评估

    2024-06-14 09:10:01       8 阅读
  7. C语言中数组和指针的关系

    2024-06-14 09:10:01       5 阅读
  8. HTML 颜色名

    2024-06-14 09:10:01       8 阅读
  9. HTML的a标签如何做返回顶部的功能

    2024-06-14 09:10:01       6 阅读
  10. 《电力网络安全事件应急预案》

    2024-06-14 09:10:01       8 阅读
  11. 百度之星2024题目记录

    2024-06-14 09:10:01       46 阅读
  12. .NET C# ‘string‘ 类型思考与解析

    2024-06-14 09:10:01       7 阅读
  13. QT day01

    QT day01

    2024-06-14 09:10:01      6 阅读