算法训练营第五十八天 | 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;
}
相关推荐
sp_fyf_202413 分钟前
计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-10-02
人工智能·神经网络·算法·计算机视觉·语言模型·自然语言处理·数据挖掘
我是哈哈hh2 小时前
专题十_穷举vs暴搜vs深搜vs回溯vs剪枝_二叉树的深度优先搜索_算法专题详细总结
服务器·数据结构·c++·算法·机器学习·深度优先·剪枝
Tisfy2 小时前
LeetCode 2187.完成旅途的最少时间:二分查找
算法·leetcode·二分查找·题解·二分
Mephisto.java2 小时前
【力扣 | SQL题 | 每日四题】力扣2082, 2084, 2072, 2112, 180
sql·算法·leetcode
robin_suli2 小时前
滑动窗口->dd爱框框
算法
丶Darling.2 小时前
LeetCode Hot100 | Day1 | 二叉树:二叉树的直径
数据结构·c++·学习·算法·leetcode·二叉树
labuladuo5202 小时前
Codeforces Round 977 (Div. 2) C2 Adjust The Presentation (Hard Version)(思维,set)
数据结构·c++·算法
jiyisuifeng19913 小时前
代码随想录训练营第54天|单调栈+双指针
数据结构·算法
꧁༺❀氯ྀൢ躅ྀൢ❀༻꧂3 小时前
实验4 循环结构
c语言·算法·基础题
新晓·故知3 小时前
<基于递归实现线索二叉树的构造及遍历算法探讨>
数据结构·经验分享·笔记·算法·链表