java – 电子商店

我一直在研究这个问题,在提交我的代码后,7个案例通过,但是9个案例都失败了.问题链接在这里是HackerRank:
Electronic Shop

问题陈述

一个女孩想买一个键盘和一个USB驱动器,她想尽可能多地花费但在预算范围内(结合两个项目).如果这两个项目的价格超过了她的预算,则返回-1或她无法购买.强制性的是,她想要购买这两件物品而不是一件物品.

一系列键盘以阵列[40,50,60]的形式给出,USB驱动器给出[5,8,12].她的最高预算是60.现在如果我们总结两件事我们得到两个组合的最大值:

> 40 12 = 52
> 50 8 = 58

由于58一个更大,因此她将得到价值50和8的物品.

输入格式

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

输出格式

打印一个表示Monica将花费的金额的整数.如果她没有足够的钱购买一个键盘和一个USB驱动器,请打印-1.

我的算法

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

我的逻辑与需求一样简单,但对于在数组中具有大量元素的情况,某种程度上是失败的.

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

输入=>

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-}

预期产出

539854

对于完整的输入数据,这里是链接:Input Array Data Full

测试用例2失败

输入=>

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-}

预期产出

374625

有关此输入的完整输入数组数据,请访问以下链接:Failed Test Case 2 Full Input

我几乎在那里,但不知怎的,我很困惑为什么我的代码不适用于长输入数组元素.任何帮助将不胜感激,因为它将使我在未来的努力中学到新的东西.

最佳答案 你误解了这个问题.如果您不能购买任何keyborad USB,答案应为-1.如果有一套是难以承受的,但如果它们都是,那就不是了.使用您当前的代码,如果最后一组是无法承受的,它会返回什么?

这是一个应该有效的代码.并提出意见解释:

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;
    } 
  }
}

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

if((i+j)>maxAmount && (i+j)<=b)
点赞