TheRealSamuel/LeetCodeProblem
0572
1{2 "id": 3190,3 "name": "minimum_operations_to_maximize_last_elements_in_arrays",4 "difficulty": "Medium",5 "link": "https://leetcode.com/problems/minimum-operations-to-maximize-last-elements-in-arrays/",6 "date": "2023-11-05 00:00:00",7 "task_description": "You are given two **0-indexed** integer arrays, `nums1` and `nums2`, both having length `n`. You are allowed to perform a series of **operations** (**possibly none**). In an operation, you select an index `i` in the range `[0, n - 1]` and **swap** the values of `nums1[i]` and `nums2[i]`. Your task is to find the **minimum** number of operations required to satisfy the following conditions: `nums1[n - 1]` is equal to the **maximum value** among all elements of `nums1`, i.e., `nums1[n - 1] = max(nums1[0], nums1[1], ..., nums1[n - 1])`. `nums2[n - 1]` is equal to the **maximum** **value** among all elements of `nums2`, i.e., `nums2[n - 1] = max(nums2[0], nums2[1], ..., nums2[n - 1])`. Return _an integer denoting the **minimum** number of operations needed to meet **both** conditions_, _or _`-1`_ if it is **impossible** to satisfy both conditions._ **Example 1:** ``` **Input:** nums1 = [1,2,7], nums2 = [4,5,3] **Output:** 1 **Explanation:** In this example, an operation can be performed using index i = 2. When nums1[2] and nums2[2] are swapped, nums1 becomes [1,2,3] and nums2 becomes [4,5,7]. Both conditions are now satisfied. It can be shown that the minimum number of operations needed to be performed is 1. So, the answer is 1. ``` **Example 2:** ``` **Input:** nums1 = [2,3,4,5,9], nums2 = [8,8,4,4,4] **Output:** 2 **Explanation:** In this example, the following operations can be performed: First operation using index i = 4. When nums1[4] and nums2[4] are swapped, nums1 becomes [2,3,4,5,4], and nums2 becomes [8,8,4,4,9]. Another operation using index i = 3. When nums1[3] and nums2[3] are swapped, nums1 becomes [2,3,4,4,4], and nums2 becomes [8,8,4,5,9]. Both conditions are now satisfied. It can be shown that the minimum number of operations needed to be performed is 2. So, the answer is 2. ``` **Example 3:** ``` **Input:** nums1 = [1,5,4], nums2 = [2,5,3] **Output:** -1 **Explanation:** In this example, it is not possible to satisfy both conditions. So, the answer is -1. ``` **Constraints:** `1 <= n == nums1.length == nums2.length <= 1000` `1 <= nums1[i] <= 109` `1 <= nums2[i] <= 109`",8 "public_test_cases": [9 {10 "label": "Example 1",11 "input": "nums1 = [1,2,7], nums2 = [4,5,3]",12 "output": "1 "13 },14 {15 "label": "Example 2",16 "input": "nums1 = [2,3,4,5,9], nums2 = [8,8,4,4,4]",17 "output": "2 "18 },19 {20 "label": "Example 3",21 "input": "nums1 = [1,5,4], nums2 = [2,5,3]",22 "output": "-1 "23 }24 ],25 "private_test_cases": [26 {27 "input": [28 [29 447837299,30 315664073,31 716176436,32 238465772,33 956928557,34 729086343,35 707648661,36 768556279,37 261506574,38 530540602,39 974474483,40 856853453,41 128063184,42 121357313,43 906035908,44 707136485,45 543557767,46 761291798,47 917148430,48 785109560,49 919964493,50 938644906,51 949142704,52 593252913,53 846649276,54 942431706,55 400232695,56 218248019,57 636038960,58 969674913,59 132878312,60 791791254,61 429510531,62 655592461,63 253359813,64 922386844,65 756608064,66 751045391,67 136809304,68 603769142,69 174591118,70 164301740,71 744941950,72 512982089,73 14327531,74 991242082,75 945293640,76 527410964,77 830852790,78 290846459,79 701892571,80 237806306,81 489092186,82 880440689,83 670723762,84 845511588,85 615434498,86 573818690,87 184151338,88 380038894,89 352445013,90 42474454,91 764466346,92 103845471,93 295837257,94 630852261,95 988263345,96 768038688,97 445912777,98 299289723,99 511536606,100 489890596,101 890982713,102 634274299,103 229163828,104 125821607,105 69857807,106 901495801,107 614498885,108 276356718,109 520891824,110 487240503,111 459108401,112 546535580,113 989089789,114 499283146,115 664346147,116 353607251,117 773735799,118 600180253,119 891008355,120 802614467,121 726862720,122 84570683,123 258280934,124 244987894,125 979313324,126 80363811,127 883033581,128 432565714,129 301168206,130 983103134,131 575599060,132 898381335,133 362191975,134 866570557,135 67279931,136 370751928,137 945198973,138 220963180,139 702483950,140 398852870,141 369202202,142 941093044,143 252198655,144 371098703,145 282865014,146 211928536,147 428440195,148 701323483,149 730957428,150 91865942,151 301040472,152 586854910,153 693148659,154 536671192,155 744559859,156 134360844,157 835700920,158 606808816,159 979532270,160 456831813,161 386520580,162 217149928,163 940974830,164 806785025,165 25854540,166 33572575,167 104627552,168 151650590,169 67110285,170 738287955,171 925224111,172 536076783,173 244400366,174 992822833,175 431000377,176 835675572,177 443106645,178 789916528,179 265595379,180 386749209,181 220926697,182 433915182,183 832865498,184 553699239,185 864668465,186 977182547,187 174526090,188 765381880,189 880831870,190 560799685,191 970708487,192 550141859,193 709527927,194 341696673,195 414379328,196 88953550,197 909974915,198 280011792,199 125865425,200 863817172,201 841868221,202 259826445,203 372202946,204 71250718,205 890272413,206 495038799,207 282042276,208 76945633,209 548810862,210 113922847,211 399150837,212 462994379,213 721563200,214 646440029,215 636741616,216 715835496,217 535413969,218 310503423,219 902049834,220 881847594,221 799885250,222 178597156,223 434571422,224 168452117,225 801008040,226 975030699,227 272739076,228 939498744,229 530231649,230 234925484,231 622544777,232 143988133,233 617865034,234 894639950,235 364850026,236 219891247,237 646085199,238 959171661,239 808428382,240 150070843,241 134827839,242 582219856,243 359087301,244 279036180,245 551033992,246 732118736,247 184294922,248 135796877,249 471853592,250 889338811,251 772680924,252 499172331,253 119217308,254 91700527,255 362024541,256 116810501,257 419910793,258 108166551,259 914937051,260 595695144,261 799662526,262 780687183,263 818895030,264 131203560,265 849791093,266 412445881,267 602505136,268 879242710,269 386933830,270 687692326,271 266582631,272 219509823,273 359026516,274 696541164,275 194340311,276 852720120,277 645975636,278 372014177,279 168442074,280 560298285,281 372638959,282 520357048,283 305492730,284 57025207,285 166455655,286 656368138,287 893951191,288 991068078,289 207289354,290 81719953,291 971002516,292 931368607,293 97962187,294 465905990,295 417243064,296 230676109,297 582980543,298 629096108,299 819875173,300 653479602,301 869540747,302 739978086,303 13321334,304 318140197,305 718458728,306 353790763307 ],308 [309 207679766,310 236344027,311 833100692,312 168109568,313 52879104,314 774631261,315 731248456,316 729599467,317 471828207,318 732388184,319 738471535,320 646038353,321 927283413,322 134984512,323 345805660,324 947717424,325 632944255,326 232578904,327 176455244,328 865322214,329 761483669,330 273533419,331 139241685,332 296168345,333 408061300,334 30953545,335 663343925,336 781228661,337 130726494,338 269223308,339 5097962,340 664010538,341 833591264,342 389371610,343 34775884,344 226746308,345 347592325,346 527424999,347 188169063,348 475700119,349 575956530,350 656451837,351 274816563,352 458957446,353 139123195,354 437731336,355 123383249,356 53645083,357 606024680,358 391939320,359 90645145,360 837620362,361 803735100,362 988010930,363 9808119,364 174451647,365 442014957,366 582040828,367 530103863,368 752963332,369 99737397,370 739942231,371 869115174,372 155436888,373 926516668,374 274796555,375 937823975,376 405062764,377 717043846,378 394888539,379 143966503,380 112652067,381 115349210,382 932476245,383 4155605,384 730416585,385 846374061,386 60172240,387 363247332,388 50607227,389 614253121,390 834930229,391 142393363,392 822480827,393 400905968,394 25631825,395 510345721,396 272418274,397 963618213,398 120853744,399 261621961,400 296762029,401 176098216,402 641182573,403 772530147,404 264052886,405 115627143,406 395798680,407 515983433,408 828164041,409 722924820,410 411571759,411 302228012,412 121385660,413 348122371,414 499294076,415 513808469,416 713845508,417 810936331,418 349148233,419 705928298,420 64735678,421 205077633,422 994526240,423 692948021,424 505203655,425 110640371,426 700352958,427 669924593,428 338207055,429 441455476,430 541623567,431 568511272,432 932297952,433 209949514,434 46133437,435 609832640,436 124995909,437 187167347,438 41130944,439 284473506,440 202729554,441 83048328,442 65573003,443 325110427,444 96738119,445 859094460,446 707678769,447 130283590,448 386486889,449 466564409,450 554731702,451 102487674,452 320167809,453 981182808,454 465121752,455 64635441,456 50759249,457 949827515,458 341078143,459 506829766,460 47788989,461 230390366,462 644255701,463 121371785,464 743600805,465 381856183,466 817682856,467 538298811,468 689082682,469 126890940,470 842540552,471 588636124,472 652171994,473 838619070,474 117070064,475 147745499,476 403793664,477 919628503,478 972567981,479 602863309,480 458101341,481 566723001,482 282581329,483 39836375,484 826948416,485 354096884,486 112784635,487 398249845,488 357965199,489 177179811,490 186812476,491 582715135,492 38104414,493 705325635,494 219011423,495 214446589,496 634015665,497 436002667,498 361583432,499 299317079,500 245263456,501 609073779,502 615832453,503 790271826,504 648953931,505 49622316,506 768743387,507 82581124,508 186541012,509 715150031,510 163916069,511 254024968,512 625336131,513 259451151,514 873648502,515 23776326,516 638743186,517 337379337,518 567318589,519 441073691,520 120367598,521 487978092,522 476773857,523 636127849,524 274965736,525 75338394,526 533507711,527 935196186,528 366812878,529 431997419,530 468730448,531 332652572,532 958169892,533 529981369,534 668963730,535 18967012,536 908999914,537 490904903,538 495195491,539 385687931,540 571142571,541 977662126,542 335646861,543 550687467,544 357032884,545 912061712,546 202768569,547 780281055,548 100446650,549 834234425,550 281147579,551 596081134,552 738541471,553 239659133,554 908307959,555 692374855,556 581306148,557 796172741,558 518070894,559 325336693,560 633734969,561 441069125,562 618020061,563 275000805,564 697265440,565 908407649,566 742336181,567 896970818,568 746733867,569 99301840,570 496227467,571 478278394,572 336697956,573 562244426,574 794697955,575 916011457,576 935728384,577 780254107,578 429069330,579 694861192,580 445013783,581 71895538,582 89542798,583 958012294,584 783129471,585 225329121,586 316713149587 ]588 ],589 "output": -1590 },591 {592 "input": [593 [594 720692992,595 570435133,596 985062481,597 938912276,598 28670421,599 35032498,600 613117887,601 230217735,602 627369263,603 170226276,604 328443567,605 962908029,606 404356404,607 745050060,608 618943436,609 715986624,610 326994133,611 492871410,612 766535725,613 75313637,614 823895296,615 40330646,616 82130529,617 424962832,618 103980010,619 67169921,620 917310151,621 228910622,622 336274754,623 503262111,624 390687312,625 240674757,626 391258944,627 89636522,628 206871915,629 364386038,630 501180020,631 12147074,632 462775281,633 525242858,634 580504974,635 539592271,636 786900356,637 764885087,638 710274554,639 935093191,640 752145967,641 741526234,642 404329526,643 726366019,644 808769468,645 186583850,646 858448444,647 397964682,648 242841165,649 247244396,650 331950073,651 126438934,652 321784302,653 740274672,654 893545378,655 825791845,656 898787812,657 758198918,658 28690580,659 753434486,660 668286256,661 524209261,662 565077994,663 349688655,664 884010340,665 689459930,666 945046585,667 283871416,668 213149345,669 417090933,670 548834735,671 167851205,672 596396908,673 531640323,674 687709838,675 696574751,676 575508808,677 582436416,678 593662419,679 981406987,680 123298869,681 986171126,682 349103552683 ],684 [685 833612205,686 710430715,687 890142425,688 746648129,689 917480168,690 389233154,691 996266463,692 973400792,693 411909565,694 783143741,695 919909646,696 805263497,697 856265970,698 909782894,699 939220634,700 108702794,701 885814396,702 851843081,703 17410719,704 160710615,705 107955479,706 811883465,707 366418998,708 485128919,709 744751356,710 509772025,711 115446575,712 489569264,713 348611208,714 178516942,715 910804471,716 913356516,717 966892185,718 846203158,719 375089248,720 486982392,721 947460782,722 851275114,723 316791840,724 344393910,725 146119314,726 32256000,727 619958555,728 151740831,729 918937619,730 450758815,731 862574130,732 461543912,733 697366501,734 710801896,735 135083846,736 291711864,737 632410997,738 93132188,739 172684506,740 223722617,741 644941604,742 799016473,743 276880373,744 910784542,745 43071612,746 288211468,747 327328253,748 492898155,749 586990904,750 263532557,751 396329334,752 463501861,753 777040897,754 519630730,755 41488309,756 114193510,757 729339731,758 509450052,759 308985184,760 958517968,761 546371974,762 755363659,763 179346260,764 109237270,765 937655066,766 140312800,767 659322130,768 222184378,769 680305939,770 923298830,771 505958819,772 873755282,773 947404371774 ]775 ],776 "output": -1777 },778 {779 "input": [780 [781 496260330,782 333601166,783 139058485,784 858626077,785 640847908,786 984920800,787 176240806,788 117098962,789 348872647,790 742361704,791 227811508,792 890089985,793 491340008,794 412785333,795 403464638,796 325981763,797 512702885,798 712869042,799 833554000,800 788725270,801 246110205,802 669477558,803 228263869,804 274066377,805 392884307,806 207331358,807 502614028,808 308860312,809 874800129,810 109204727,811 657364597,812 764794615,813 159218006,814 7772112,815 969279506,816 624130014,817 397352457,818 940964677,819 172282230,820 12116799,821 341454012,822 344832701,823 168484342,824 252789931,825 384770671,826 407075877,827 703743172,828 305596379,829 413205141,830 81256973,831 41143019,832 118026705,833 13667962,834 983990597,835 327469933,836 358812269,837 709579468,838 670758273,839 745230305,840 105086081,841 404868473,842 639620752,843 303495438,844 465782550,845 897442088,846 561650720,847 483320683,848 314650050,849 265897943,850 867968642,851 724807787,852 67235986,853 144377026,854 553677097,855 511072948,856 331873950,857 431856560,858 917276884,859 744358678,860 19441502,861 490002728,862 405491349,863 114578768,864 636511817,865 803625806,866 664656979,867 935547925,868 944770542,869 506855947,870 257237488,871 176922802,872 54161314,873 324670927,874 694997318,875 770792497,876 102496072,877 828684217,878 501978153,879 771884540,880 46856420,881 23688630,882 15244012,883 85093775,884 267962002,885 579793593,886 23924406,887 139444850,888 839826305,889 688122422,890 840428983,891 807043695,892 106671130,893 369742726,894 575426506,895 553214621,896 500588570,897 825297225,898 465309285,899 351588515,900 433969210,901 479990125,902 250905899,903 993485908,904 365174913,905 169612693,906 11850677,907 188482691,908 445654803,909 577035166,910 1121879,911 412930021,912 959589199,913 702075779,914 385196901,915 112686155,916 254397618,917 266099700,918 267392718,919 231223109,920 163600884,921 383291453,922 820644602,923 861200002,924 387266024,925 206469505,926 455202561,927 79817714,928 541506969,929 950877542,930 608605090,931 692185212,932 149817665,933 229630762,934 244119559,935 790580992,936 829519409,937 866569986,938 645218129,939 807713627,940 693980388,941 226807384,942 437653013,943 122820492,944 702898964,945 271529900,946 386635686,947 498290495,948 515981363,949 88440917,950 811413570,951 432376522,952 602914998,953 910062558,954 518173583,955 83016289,956 254989618,957 879808315,958 45698841,959 449217525,960 4530749,961 430521133,962 287050242,963 59587705,964 384592849,965 232040911,966 528683777,967 274248506,968 982289279,969 813258222,970 784743510,971 414859362,972 761703776,973 882942109,974 575566459,975 301109261,976 772633215,977 242786041,978 50214754,979 677826508,980 567710786,981 192238893,982 934617067,983 655480641,984 196320680,985 615350946,986 914859559,987 602052740,988 466943989,989 154679454,990 768418443,991 653402614,992 497356320,993 404892804,994 317774523,995 222719399,996 77554248,997 883433942,998 921526342,999 993329693,1000 132449473,1001 570281937,1002 91130646,1003 7551712,1004 921027379,1005 441582390,1006 554539701,1007 824575596,1008 305669880,1009 285771547,1010 993402383,1011 681121797,1012 836323191,1013 365670530,1014 97080348,1015 183318583,1016 658440484,1017 945895587,1018 700405661,1019 112523276,1020 35598668,1021 883891072,1022 166605622,1023 470681133,1024 945319793,1025 677854612,1026 289453520,1027 321922988,1028 426357781,1029 23786560,1030 794916326,1031 92595952,1032 545901264,1033 143233665,1034 80153447,1035 394107523,1036 865750021,1037 50796614,1038 950522272,1039 785334342,1040 423496380,1041 974708008,1042 687589587,1043 6504590,1044 678876824,1045 310427983,1046 328346767,1047 58368708,1048 622757007,1049 148204845,1050 721754228,1051 463252589,1052 755035283,1053 202608521,1054 323993390,1055 437880148,1056 242728597,1057 101616621,1058 226164245,1059 385691349,1060 179554747,1061 225948796,1062 468520780,1063 336674319,1064 561086980,1065 278039196,1066 894708653,1067 950833089,1068 545004884,1069 959458782,1070 112544100,1071 532281507,1072 492662478,1073 621192868,1074 845418664,1075 559499068,1076 163575943,1077 622503361,1078 401928809,1079 125352702,1080 385283207,1081 123745844,1082 561431656,1083 320036238,1084 385935838,1085 440848779,1086 889743860,1087 9404627,1088 879648367,1089 682918332,1090 43258775,1091 89468718,1092 110460800,1093 32193825,1094 849507152,1095 871186320,1096 426968672,1097 510916678,1098 531730093,1099 910543836,1100 991633832,1101 131069596,1102 806925239,1103 77476992,1104 39932331,1105 785995295,1106 36873286,1107 338300287,1108 799490280,1109 766895869,1110 60948064,1111 933937888,1112 43287104,1113 808790300,1114 323100704,1115 973454445,1116 740002904,1117 396510249,1118 273089487,1119 614625322,1120 293328462,1121 446125504,1122 42354752,1123 83431221,1124 659449768,1125 846371436,1126 535471761,1127 103125965,1128 832937711,1129 681483000,1130 929255603,1131 64353882,1132 951779681,1133 482932509,1134 811948746,1135 126163828,1136 586564934,1137 318927179,1138 997897220,1139 535950915,1140 744408900,1141 975847444,1142 492532007,1143 46815723,1144 857859561,1145 675083024,1146 947926533,1147 14751460,1148 608747307,1149 799418601,1150 315620588,1151 955585240,1152 79016427,1153 795490395,1154 386989693,1155 873370208,1156 610798168,1157 194909958,1158 495838142,1159 985028589,1160 546362070,1161 916709653,1162 780665898,1163 853191914,1164 490008370,1165 10470056,1166 76398256,1167 142518707,1168 215764461,1169 725190449,1170 840536722,1171 543408718,1172 992784219,1173 255772908,1174 396228735,1175 158705532,1176 879569224,1177 33218720,1178 806281888,1179 835193018,1180 34818358,1181 367563921,1182 176064467,1183 830490170,1184 597285759,1185 879373884,1186 356838928,1187 537915020,1188 397843649,1189 103329306,1190 935304193,1191 27055888,1192 439053073,1193 79361580,1194 423982863,1195 87461460,1196 497524535,1197 194539380,1198 333502578,1199 720716055,1200 51126582,