首页 > 解决方案 > 电子产品商店

问题描述

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

问题陈述

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

例子

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

由于 58 大于 1,因此她将获得价值 50 和 8 的物品。

输入格式

输出格式

打印一个整数,表示 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

Input =>

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

539854

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

失败的测试用例 2

Input =>

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

374625

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

我几乎在那里,但不知何故我很困惑为什么我的代码不适用于长输入数组元素。任何帮助将不胜感激,因为它会让我在未来的努力中学习新事物。

标签: javaarrays

解决方案


你误解了这个问题。如果你买不到任何键盘+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;
    } 
  }
}

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

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

推荐阅读