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

【25届秋招】Shopee 0825算法岗笔试

目录

  • 1. 第一题
  • 2. 第二题
  • 3. 第三题

⏰ 时间:2024/08/25
🔄 输入输出:LeetCode格式
⏳ 时长:2h

本试卷有10道单选,5道多选,3道编程。

整体难度非常简单,博主20min成功AK,这里只给出编程部分的题解。

1. 第一题

第一题是LC原题:32. 最长有效括号,可以用dp也可以用栈。

2. 第二题

第二题也是LC原题:123. 买卖股票的最佳时机 III,官网给的是dp解法,不过这里有一个更加通俗易懂的做法。

我们可以开两个数组 profit1profit2,其中 profit1[i] 代表的是从第 0 0 0 天到第 i i i 天为止进行一次交易能够获得的最大利润,profit2[i] 代表的是从第 i i i 天到最后一天为止进行一次交易能够获得的最大利润。

前者从前往后遍历,维护的是最低点。后者从后往前遍历,维护的是最高点。最终答案就是:

max ⁡ i p r o f i t 1 [ i ] + p r o f i t 2 [ i ] \max_i profit1[i]+profit2[i] imaxprofit1[i]+profit2[i]

class Solution {
public:int maxProfit(vector<int>& prices) {int n = prices.size();if (n == 0) return 0;vector<int> profit1(n, 0);int min_price = prices[0];for (int i = 1; i < n; i++) {min_price = min(min_price, prices[i]);profit1[i] = max(profit1[i - 1], prices[i] - min_price);}vector<int> profit2(n, 0);int max_price = prices[n - 1];for (int i = n - 2; i >= 0; i--) {max_price = max(max_price, prices[i]);profit2[i] = max(profit2[i + 1], max_price - prices[i]);}int ans = 0;for (int i = 0; i < n; i++) {ans = max(ans, profit1[i] + profit2[i]);}return ans;}
};

3. 第三题

题目描述

小明在计算货车承载体积时陷入了苦恼,他想从一堆包裹中找到 N N N 个箱子,使其总重量等于货车最大载重 M M M

假设每个箱子的重量都不一样,每组输入可能有多个结果或者不存在结果。

请你实现一个算法,找出所有符合条件的结果的数量。

数据规模

1 ≤ N ≤ 30 , 1 ≤ M ≤ 100 1\leq N\leq 30, \,1\leq M \leq 100 1N30,1M100

题解

暴搜即可。从左往右枚举每一个箱子,选择放或不放。

class Solution {
public:using i64 = long long;i64 find_boxes_combinations(vector<long>& boxes, i64 target) {return dfs(boxes, target, 0, 0);}private:i64 dfs(vector<long>& boxes, i64 target, int index, i64 current_sum) {if (current_sum == target) {return 1;}if (current_sum > target || index >= boxes.size()) {return 0;}i64 include_current_box = dfs(boxes, target, index + 1, current_sum + boxes[index]);i64 exclude_current_box = dfs(boxes, target, index + 1, current_sum);return include_current_box + exclude_current_box;}
};

http://www.mrgr.cn/news/9692.html

相关文章:

  • Long Short-Term Memory
  • JavaScript性能对决:左移运算符VS乘法运算,谁更胜一筹?
  • 自动驾驶-机器人-slam-定位面经和面试知识系列10之高频面试题(04)
  • Facebook的AI助手:如何提升用户社交体验的智能化
  • Linux系统下的容器安全:深入解析与最佳实践
  • ThinkPHP6异步请求的全面解析
  • Linux文件IO缓存
  • Web API 学习笔记 第四弹
  • JavaScript学习文档(5):为什么需要函数、函数使用、函数传参、函数返回值、作用域、匿名函数、逻辑中断
  • SQLite使用datetime函数
  • 集合及数据结构第七节————LinkedList的模拟实现与使用
  • Redis下载安装使用教程图文教程(超详细)
  • 海莲花活跃木马KSRAT加密通信分析
  • 本题目要求计算分段函数的值:
  • 能源与节能
  • 2-73 基于matlab的weber能量法求解齿轮时变啮合刚度的程序
  • 3.5、matlab打开显示保存点云文件(.ply/.pcd)以及经典点云模型数据
  • Mybatis基础操作教程
  • AI 音频/文本对话机器人:Whisper+Edge TTS+OpenAI API构建语音与文本交互系统(简易版)
  • 从行或列的角度思考矩阵-向量乘法(matrix-vector multiplication)