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

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

LeetCode 392 判断子序列


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

代码如下:

java 复制代码
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();
    }
}

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


简单模拟

代码如下:

cpp 复制代码
#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;
}

第二题 小美走公路


简单模拟

代码如下:

cpp 复制代码
#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;
}

第三题 小美的蛋糕切割


二维前缀和

代码如下:

cpp 复制代码
#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;
}
相关推荐
孫治AllenSun8 分钟前
【算法】图相关算法和递归
windows·python·算法
格图素书1 小时前
数学建模算法案例精讲500篇-【数学建模】DBSCAN聚类算法
算法·数据挖掘·聚类
DashVector2 小时前
向量检索服务 DashVector产品计费
数据库·数据仓库·人工智能·算法·向量检索
AI纪元故事会2 小时前
【计算机视觉目标检测算法对比:R-CNN、YOLO与SSD全面解析】
人工智能·算法·目标检测·计算机视觉
夏鹏今天学习了吗2 小时前
【LeetCode热题100(59/100)】分割回文串
算法·leetcode·深度优先
卡提西亚2 小时前
C++笔记-10-循环语句
c++·笔记·算法
还是码字踏实2 小时前
基础数据结构之数组的双指针技巧之对撞指针(两端向中间):三数之和(LeetCode 15 中等题)
数据结构·算法·leetcode·双指针·对撞指针
Coovally AI模型快速验证4 小时前
当视觉语言模型接收到相互矛盾的信息时,它会相信哪个信号?
人工智能·深度学习·算法·机器学习·目标跟踪·语言模型
电院工程师5 小时前
SIMON64/128算法Verilog流水线实现(附Python实现)
python·嵌入式硬件·算法·密码学
轮到我狗叫了5 小时前
力扣.84柱状图中最大矩形 力扣.134加油站牛客.abb(hard 动态规划+哈希表)牛客.哈夫曼编码
算法·leetcode·职场和发展