当前位置: 首页 > news >正文

算法训练营第五十八天 | 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;
}

http://www.lryc.cn/news/371633.html

相关文章:

  • Sa-Token鉴权与网关服务实现
  • 企事业单位安全生产月活动怎样向媒体投稿?
  • MySQL8.0默认TCP端口介绍
  • Javaweb避坑指北(持续更新)
  • Web前端知道:深入探索与无尽挑战
  • QT调用vs2019生成的c++动态库
  • C语言TC中有⼏个画线函数?怎么使⽤?
  • 掌握WhoisAPI,提升域名管理的效率
  • Docker与Docker-Compose详解
  • 微服务之熔断器
  • 【高校科研前沿】北京大学赵鹏军教授团队在Nature Communications发文:揭示城市人群移动的空间方向性
  • 徐州存储服务器会应用在哪些场景?
  • 个人博客搭建
  • 服务器数据库三级等保的一些修改步骤
  • Python私教张大鹏 Vue3整合AntDesignVue之DatePicker 日期选择框
  • springboot+vue前后端分离项目中使用jwt实现登录认证
  • leetcode hot100 之 编辑距离
  • 杨校老师项目之基于SpringBoot的理发店的预约管理系统
  • SpringAI学习及搭建AI原生应用
  • CobaltStrike权限传递MSF
  • 白嫖 kimi 接口 api
  • 借助ChatGPT完成课题申报书中框架思路写作指南
  • SuntoryProgrammingContest2024(AtCoder Beginner Contest 357)
  • 重温共射放大电路
  • [DDR5 Jedec] 读操作 Read Command 精讲
  • opencv 通过滑动条调整阈值处理、边缘检测、轮廓检测、模糊、色调调整和对比度增强参数 并实时预览效果
  • 防火墙安全管理
  • MyQueue(队列)
  • 【Pytorch】一文向您详细介绍 torch.nn.DataParallel() 的作用和用法
  • Windows本地使用SSH连接VM虚拟机