12
返回列表 发新帖
楼主: lunatic_luna
跳转到指定楼层
上一主题 下一主题
收起左侧

谷苟面经题

全局:
嗯??我怎么感觉这是一个标准的SegTree 的题目。。。
回复

使用道具 举报

🔗
Heinrich 2018-11-21 23:48:47 | 只看该作者
全局:
https://www.1point3acres.com/bbs/forum.php?mod=viewthread&tid=449947&extra=&page=1
个人实现代码:

  1. // 类似如下dfs(下面的code里pair不能作为memo的key,待改)
  2. bool dfs(pair<int, int> temp, pair<int, int> range, vector<pair<int, int>>& buttons, unordered_map<pair<int, int>, bool>& memo) {
  3.     if (memo.count(temp))
  4.         return memo[temp];
  5.     if (range.first <= temp.first && temp.second <= range.second)
  6.         return true;
  7.     if (range.second < temp.second)
  8.         return false;
  9.     for (auto button : buttons) {
  10.         if (dfs({button.first + temp.first, button.second + temp.second}, range, buttons, memo)) {
  11.             memo.insert();
  12.             return true;
  13.         }
  14.     }
  15.     memo[temp] = false;
  16.     return false;
  17. }

  18. bool isInCocaMachineRange(pair<int, int> range, vector<pair<int, int>>& buttons) {
  19.     unordered_map<pair<int, int>, bool> memo;
  20.     return dfs({0,0}, range, buttons, memo);
  21. }

  22. int main() {
  23.     vector<pair<int, int>> button1 = {{100, 120}, {200, 240}, {400, 410}};
  24.     cout << (isInCocaMachineRange({100, 110}, button1) ? "In" : "Not in") << endl; // false
  25.     cout << (isInCocaMachineRange({90, 120}, button1) ? "In" : "Not in") << endl; // true
  26.     cout << (isInCocaMachineRange({300, 360}, button1) ? "In" : "Not in") << endl; // true
  27.     cout << (isInCocaMachineRange({310, 360}, button1) ? "In" : "Not in") << endl; // false
  28.     cout << (isInCocaMachineRange({1, 999999999}, button1) ? "In" : "Not in") << endl; // true
  29.     return 0;   
  30. }
复制代码

评分

参与人数 1大米 +3 收起 理由
bc2615 + 3 给你点个赞!

查看全部评分

回复

使用道具 举报

🔗
jevi 2019-3-31 09:32:06 | 只看该作者
全局:
您好!
本帖隐藏的内容需要积分高于 150 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 150 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表