首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >电子商店

电子商店
EN

Stack Overflow用户
提问于 2019-01-22 16:05:51
回答 3查看 3.2K关注 0票数 4

我一直在研究这个问题,在提交了我的代码之后,7个案例通过了,但是9例失败了。问题链接在这里的HackerRank:电子商店

问题陈述

一个女孩想买一个键盘和一个USB驱动器,她想花尽可能多的钱,但不超出预算(两者结合在一起)。如果两个项目的价格都超过了她的预算,返回-1或她不能买。强制性的是,她想同时购买这两样东西,而不是一件。

示例

键盘的范围是以数组[40, 50, 60]的形式给出的,而USB驱动器则是[5,8,12]。她的最高预算是60。现在,如果我们把这两件事相加,我们得到了两个组合的最大值:

  • 40 + 12 = 52
  • 50 +8= 58

因为一个人更大,所以她会得到价值50和8的物品。

输入格式

  • 第一行包含三个空格分隔的整数b、n和m、她的预算、键盘型号数和USB驱动器型号数。
  • 第二行包含空格分隔的整数,每个键盘模型的价格.
  • 第三行包含空格分隔的整数,USB驱动器的价格。

输出格式

打印一个表示莫妮卡将花费多少钱的整数。如果她没有足够的钱买一个键盘和一个USB驱动器,那就打印-1。

我的Algo

代码语言:javascript
复制
1. Take answer variable as 0, and max value initialize it with first element of keyboard + first element of usb drives

2. loop through keyboard, make an inner loop for usb

3. Compare keyboard[i] + drives[j], if greater than b, then return -1.

4. Else find the max value and assign it to answer

5. return answer

我的逻辑和需求一样简单,但是对于数组中有大量元素的情况,我的逻辑却以某种方式失败了。

代码语言:javascript
复制
static int getMoneySpent(int[] keyboards, int[] drives, int b) {
    int answer = 0, maxAmount = keyboards[0] + drives[0];

    //This will compare the value of i+j throughout the loop and returns the max one 
    for(int i: keyboards){
      for(int j: drives){
        // Ofcourse if all i+j values will be greater than the max budget then -1
        if((i+j) > b)
          answer = -1;
        else if((i+j) == b){
           answer = i+j;
        }else{
           /*If the value is smaller than the max budget, the finding the max value we get after adding them and comparing with the maxAmount variable */
          if((i+j) > maxAmount){
            maxAmount = i+j;
            answer = maxAmount;
        } 
      }
    }
  }

  return answer;
}

我有两个案子失败了,在这里:

失败测试用例1

Input =>

代码语言:javascript
复制
539855 818 628
380710 674456 878173 532602 868253 721585 806107 141310 790209 212031 
304748 818920 80938 322601 403071 22899 173564 153826 695108 223665 
346178 957539 975830 573171 641117 932941 822666 575293 132555 479463 
862209 313799 922966 606508 487172 139230 606390 898464 764983 829520 
174879 317603 502680 953013 398753 825387 146407 666457 367618 121790 
68188 478342 25818 506222 135197 232604 963333 79984 549654 776899 
966040 122063 432596 594425 311887 936661 506256 876303 439611 277816 
105689 851641 640971 333 216087 17692 619728 602689 650348 364881 
152060 386548 61364 564569 780938 191826 459905 211804 58177 484711 
995091 754424 57794 619638 695192 297423 983901 430435 239234 170704 
142282 74647 121413 782873 303344 265448 101069 177807 692318 691774 
62306 618191 509537 633333 996922 228947 814154 232698 615359 220853 
306323 173792 624037 655872 527161 848207 426180 724481 130740 792273 
886804 404890 449886 654224 194667 354317 367843 525624 414224 481744 
827725 176927 733780 387166 769479 964040 1{-truncated-}

Expected Output

代码语言:javascript
复制
539854

对于完整的输入数据,这里的链接是:输入阵列数据满

失败测试用例2

Input =>

代码语言:javascript
复制
374625 797 951
183477 732159 779867 598794 596985 156054 445934 156030 99998 58097 
459353 866372 333784 601251 142899 708233 651036 20590 56425 970129 
722162 832631 938765 212387 779 181866 992436 183446 617621 304311 
611791 524875 7068 432043 23068 291295 524893 611991 399952 139526 
46677 292211 973975 366445 232824 456173 90627 785353 618526 199719 
382549 514351 983453 592549 466869 46461 860135 607682 680461 170563 
450601 65067 13268 949100 942415 965850 563416 808580 385504 304683 
15970 97695 230946 684388 241080 440252 683418 122066 610135 495289 
833383 34397 173404 909526 391149 258839 182278 662672 755532 311782 
425252 520186 207989 546834 567829 184897 31321 969804 842475 775308 
449856 939711 395240 895029 926868 598035 727436 922082 326615 88513 
570573 196028 520952 45238 961389 325404 844725 388765 747489 271411 
539814 828925 586884 356834 965473 280998 607171 542819 276062 140956 
296341 802378 165305 74568 15640 987110 423497 772419 394971 198761 
293555 5524 14083 815646 198888 707017 711503 729172{-truncated-}

Expected Output

代码语言:javascript
复制
374625

对于这个完整的输入数组数据,请按下面的链接:失败测试用例2全输入

我几乎在那里,但不知为何我的代码不适用于长输入数组元素。任何帮助都将不胜感激,因为这将使我在今后的努力中学到新的东西。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-01-22 16:26:30

你误解了这个问题。如果你买不到任何keyborad+usb,答案应该是-1。不是如果有一套是负担不起的,而是如果它们都是。使用您当前的代码,如果最后一组是无法负担的,它会返回什么?

下面是一个应该有效的代码。并提出解释意见:

代码语言:javascript
复制
int answer = -1; // that's the default answer.
int maxAmount = 0; // what if first keyboard + first usb are unaffordable? let's put it to 0

//This will compare the value of i+j throughout the loop and returns the max one 
for(int i: keyboards){
  for(int j: drives){
    if((i+j) > b) {
      // do nothing, it's unaffordable (and remove this block)
    }else if((i+j) == b){
       answer = i+j; 
       return answer;// it can't be more, stop the loop and return the solution
    } else if((i+j) > maxAmount){ // no need to put an else, then an if both conditions are related
        maxAmount = i+j;
        answer = maxAmount;
    } 
  }
}

当然,如果从上面的代码中删除第一个空的if块,则必须更改最后一个条件,以检查它是否低于允许的最大值:

代码语言:javascript
复制
if((i+j)>maxAmount && (i+j)<=b)
票数 1
EN

Stack Overflow用户

发布于 2020-06-02 12:01:14

代码语言:javascript
复制
int getMoneySpent(int keyboards_count, int* keyboards, int drives_count, int* drives, int b) {

     int price=-1;

         for(int i=0;i<drives_count;i++)
         {
             for(int j=0;j<keyboards_count;j++)
             {
                 if((drives[i]+keyboards[j]>price) && (drives[i]+keyboards[j]<=b))
                 {
                     price=drives[i]+keyboards[j];
                 }
             }
         }
         return price;
}
票数 0
EN

Stack Overflow用户

发布于 2022-04-20 13:49:46

Javascript解决方案:

代码语言:javascript
复制
function getMoneySpent(keyboards, drives, b) {
    const combos = [];
    let maxCost = 0
    keyboards.forEach(keyboard => {
        drives.forEach(drive => {
            let currentComboCost = keyboard+drive;
            maxCost = ((currentComboCost <= b) && (currentComboCost > maxCost)) ? currentComboCost : maxCost;
        })
    })
    return maxCost || -1;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54312152

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档