回复: 9
跳转到指定楼层
上一主题 下一主题
收起左侧

空气床电面面经

全局:

2019(7-9月) 码农类General 硕士 全职@airbnb - 内推 - 技术电面  | | Fail | 在职跳槽

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
题很简答,有一个菜单,求菜品总价达到target的combination。自
您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
面试前还是到coderpad上练一练

评分

参与人数 2大米 +12 收起 理由
匿名用户-RZECS + 10
漫漫人生路 + 2 给你点个赞!

查看全部评分


上一篇:蓝鸟白票OA 2019
下一篇:骨骼new grad_OA
🔗
 楼主| longmeier2008 2019-9-12 11:04:09 | 只看该作者
全局:
本帖最后由 longmeier2008 于 2019-9-12 11:41 编辑

题和解答如下图。。。。新建的类要写成static还满坑的。
来点大米吧~


补充内容 (2019-9-12 15:18):
这道题按图这样做是不行的!!!Double运算很坑,建议认真复习!

本帖子中包含更多资源

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
回复

使用道具 举报

全局:
请问 "a, b" 和 "b, a"算两个吗?结果可以有顺序不同的重复是吗?
回复

使用道具 举报

🔗
 楼主| longmeier2008 2019-9-12 15:00:03 | 只看该作者
全局:
奋斗的叉子 发表于 2019-9-12 14:38
请问 "a, b" 和 "b, a"算两个吗?结果可以有顺序不同的重复是吗?

不算。。。。。。。。。。。
回复

使用道具 举报

全局:
longmeier2008 发表于 2019-9-12 15:00
不算。。。。。。。。。。。

那楼主的答案好像会是"a,d" "d,a"
Given target = 2.8
如果我没搞错的话
回复

使用道具 举报

全局:
longmeier2008 发表于 2019-9-12 11:04
题和解答如下图。。。。新建的类要写成static还满坑的。
来点大米吧~

我在coderpad上试了一下,其实新建的类也可以不用写成static
回复

使用道具 举报

🔗
gongzhen 2019-9-19 07:23:07 | 只看该作者
全局:
本帖最后由 gongzhen 于 2019-9-19 07:26 编辑

发一下我的代码吧 是OC写的。

  1. #import <Foundation/Foundation.h>
  2. #import <stdio.h>

  3. @interface MenuItemSolution : NSObject

  4. - (NSString *)findMenuFor:(double)target;

  5. @end

  6. @interface MenuItem : NSObject
  7. @property(nonatomic, strong) NSString *name;
  8. @property(nonatomic, assign) double price;
  9. - (instancetype)initWith:(NSString *)name price:(double)price;
  10. @end

  11. @implementation MenuItem

  12. - (instancetype)initWith:(NSString *)name price:(double)price
  13. {
  14.     if (self = [super init]) {
  15.         self.name = name;
  16.         self.price = price;
  17.     }
  18.     return self;
  19. }

  20. @end

  21. @interface MenuItemSolution()

  22. @property(nonatomic, strong) NSMutableArray<MenuItem *> *list;

  23. @end

  24. @implementation MenuItemSolution

  25. -(instancetype)init {
  26.     if (self = [super init]) {
  27.         self.list = [NSMutableArray array];
  28.         [self.list addObject:[[MenuItem alloc] initWith:@"a" price:1.8]];
  29.         [self.list addObject:[[MenuItem alloc] initWith:@"b" price:1.5]];
  30.         [self.list addObject:[[MenuItem alloc] initWith:@"c" price:1.5]];
  31.         [self.list addObject:[[MenuItem alloc] initWith:@"d" price:1.5]];
  32.         [self.list sortUsingComparator:^NSComparisonResult(MenuItem * _Nonnull obj1, MenuItem  * _Nonnull obj2) {
  33.             if (obj1.price == obj2.price) {
  34.                 return 0;
  35.             } else if (obj1.price > obj2.price) {
  36.                 return 1;
  37.             } else {
  38.                 return -1;
  39.             }
  40.         }];
  41.         [self.list enumerateObjectsUsingBlock:^(MenuItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  42.             NSLog(@"%@:%f", obj.name, obj.price);
  43.         }];
  44.     }
  45.     return self;
  46. }

  47. - (NSString *)findMenuFor:(double)target {
  48.     NSMutableArray<NSString *> *temp = [NSMutableArray array];
  49.     NSMutableArray<NSMutableArray<NSString *> *> *result = [NSMutableArray array];
  50.     NSMutableArray *visited = [NSMutableArray new];
  51.     for(int i = 0; i < self.list.count; i++) {
  52.         [visited addObject:@(0)];
  53.     }
  54.     [self dfs:result temp:temp target:target visited:visited index:0];
  55.     NSMutableString *str = [NSMutableString string];
  56.     for(NSMutableArray<NSString *> *subList in result) {
  57.         [str appendString:@"["];
  58.         for(NSString *item in subList) {
  59.             [str appendString:item];
  60.             [str appendString:@","];
  61.         }
  62.         [str appendString:@"],"];
  63.     }
  64.     return [str copy];
  65. }

  66. - (void)dfs:(NSMutableArray<NSMutableArray<NSString *> *> *)result
  67.        temp:(NSMutableArray<NSString *> *)temp
  68.      target:(double)target
  69.      visited:(NSMutableArray *)visited
  70.       index:(int)index {
  71.     if (index >= (int)self.list.count || target < 0) {
  72.         return;
  73.     }
  74.     if (target == 0) {
  75.         [result addObject:[temp copy]];
  76.         return;
  77.     }
  78.    
  79.     for (int i = index; i < self.list.count; i++) {
  80.         MenuItem *item = [self.list objectAtIndex:i];
  81.         [temp addObject:item.name];
  82.         [visited replaceObjectAtIndex:i withObject:@(1)];
  83.         [self dfs:result temp:temp target:target - item.price visited:visited index:i + 1];
  84.         [temp removeLastObject];
  85.         [visited replaceObjectAtIndex:i withObject:@(0)];
  86.     }
  87. }

  88. @end


  89. int main (int argc, const char * argv[])
  90. {
  91.   @autoreleasepool {
  92.     MenuItemSolution *obj = [[MenuItemSolution alloc] init];
  93.     NSString *res = [obj findMenuFor:3.0];
  94.     NSLog(@"res:%@", res); ///  res:[b,c,],[b,d,],[c,d,],
  95.   }
  96. }
复制代码
回复

使用道具 举报

🔗
Cerax 2019-9-26 02:35:05 | 只看该作者
全局:
backtrack,lc三九。顺便想问下设计input是什么意思?是说不能再hard code里写testcase而是一定要写cin的意思吗?
回复

使用道具 举报

🔗
 楼主| longmeier2008 2019-9-26 13:36:46 | 只看该作者
全局:
Cerax 发表于 2019-9-26 02:35
backtrack,lc三九。顺便想问下设计input是什么意思?是说不能再hard code里写testcase而是一定要写cin的意 ...

就是method的输入输出自己设计
回复

使用道具 举报

🔗
alexisheeee 2019-10-1 05:33:18 | 只看该作者
全局:
本帖最后由 alexisheeee 于 2019-10-1 05:34 编辑

                 
回复

使用道具 举报

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

本版积分规则

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