question
stringlengths
58
4.29k
label
class label
3 classes
Alice manage company rent floor build office space Alice decide floor special floor relaxation onlyyou give integer denote Alice rent floor inclusive you give integer array special speciali denote special floor Alice designate relaxationReturn maximum number consecutive floor special floor Example 1Input 2 9 special 46output 3explanation the follow range inclusive consecutive floor special floor 2 3 total 2 floor 5 5 total 1 floor 7 9 total 3 floorstherefore return maximum number 3 floorsexample 2input 6 8 special 768output 0explanation every floor rent special floor return 0 Constraints1 speciallength 1051 speciali 109all value special unique
0array
the bitwise and array num bitwise and integer numsfor example num 1 5 3 bitwise and equal 1 5 3 1also num 7 bitwise and 7you give array positive integer candidate evaluate bitwise and combination number candidate each number candidate combinationreturn size large combination candidate bitwise and great 0 example 1input candidate 16177162122414output 4explanation the combination 16176224 bitwise and 16 17 62 24 16 0the size combination 4it show combination size great 4 bitwise and great 0note combination large sizefor example combination 62122414 bitwise and 62 12 24 14 8 0example 2input candidate 88output 2explanation the large combination 88 bitwise and 8 8 8 0the size combination 2 return 2 Constraints1 candidateslength 1051 candidatesi 107
0array
give string s character letter return percentage character s equal letter round near percent Example 1input s foobar letter oOutput 33explanationthe percentage character s equal letter o 2 6 100 33 rounded return 33example 2input s jjjj letter kOutput 0explanationthe percentage character s equal letter k 0 return 0 Constraints1 slength 100s consist lowercase english lettersletter lowercase english letter
2string
you n bag number 0 n 1 you give 0indexed integer array capacity rock the ith bag hold maximum capacityi rock currently contain rocksi rock you give integer additionalrock number additional rock place bagsreturn maximum number bag capacity place additional rock bag Example 1input capacity 2345 rock 1244 additionalrock 2Output 3ExplanationPlace 1 rock bag 0 1 rock bag 1the number rock bag 2344bags 0 1 2 capacitythere 3 bag capacity return 3it show possible 3 bag capacitynote way place rock result answer 3example 2input capacity 1022 rock 220 additionalrock 100Output 3ExplanationPlace 8 rock bag 0 2 rock bag 2the number rock bag 1022bags 0 1 2 capacitythere 3 bag capacity return 3it show possible 3 bag capacitynote use additional rock Constraintsn capacitylength rockslength1 n 5 1041 capacityi 1090 rocksi capacityi1 additionalrock 109
0array
you give 2D integer array stockprice stockpricesi dayi pricei indicate price stock day dayi pricei a line chart create array plot point XY plane Xaxis represent day Yaxis represent price connect adjacent point one example show belowreturn minimum number line need represent line chart example 1input stockprice 1726354454637281output 3explanationthe diagram represent input Xaxis represent day Yaxis represent pricethe follow 3 line draw represent line chart line 1 red 17 44 pass 17 26 35 44 line 2 blue 44 54 line 3 green 54 81 pass 54 63 72 81it show possible represent line chart 3 linesexample 2input stockprice 34127823output 1explanationas show diagram line chart represent single line Constraints1 stockPriceslength 105stockpricesilength 21 dayi pricei 109all dayi distinct
0array
as ruler kingdom army wizard commandyou give 0indexed integer array strength strengthi denote strength ith wizard for contiguous group wizard ie wizard strength form subarray strength total strength define product follow valuesthe strength weak wizard groupThe total individual strength wizard groupreturn sum total strength contiguous group wizard since answer large return modulo 109 7A subarray contiguous nonempty sequence element array example 1input strength 1312output 44explanation the follow contiguous group wizard 1 1312 total strength min1 sum1 1 1 1 3 1312 total strength min3 sum3 3 3 9 1 1312 total strength min1 sum1 1 1 1 2 1312 total strength min2 sum2 2 2 4 13 1312 total strength min13 sum13 1 4 4 31 1312 total strength min31 sum31 1 4 4 12 1312 total strength min12 sum12 1 3 3 131 1312 total strength min131 sum131 1 5 5 312 1312 total strength min312 sum312 1 6 6 1312 1312 total strength min1312 sum1312 1 7 7the sum total strength 1 9 1 4 4 4 3 5 6 7 44example 2input strength 546output 213explanation the follow contiguous group wizard 5 546 total strength min5 sum5 5 5 25 4 546 total strength min4 sum4 4 4 16 6 546 total strength min6 sum6 6 6 36 54 546 total strength min54 sum54 4 9 36 46 546 total strength min46 sum46 4 10 40 546 546 total strength min546 sum546 4 15 60the sum total strength 25 16 36 36 40 60 213 Constraints1 strengthlength 1051 strengthi 109
0array
you give 0indexed string num length n consist digitsreturn true index range 0 n digit occur numi time num return false Example 1input num 1210output trueexplanationnum0 1 the digit 0 occur numnum1 2 the digit 1 occur twice numnum2 1 the digit 2 occur numnum3 0 the digit 3 occur zero time numthe condition hold true index 1210 return trueexample 2input num 030output falseExplanationnum0 0 the digit 0 occur zero time actually occur twice numnum1 3 the digit 1 occur time actually occur zero time numnum2 0 the digit 2 occur zero time numthe index 0 1 violate condition return false Constraintsn numlength1 n 10num consist digit
2string
you give integer n denote number city country the city number 0 n 1you give 2D integer array road roadsi ai bi denote exist bidirectional road connect city ai biyou need assign city integer value 1 n value the importance road define sum value city connectsreturn maximum total importance road possible assign value optimally Example 1input n 5 road 011223021324output 43explanation the figure show country assign value 24531 the road 01 importance 2 4 6 the road 12 importance 4 5 9 the road 23 importance 5 3 8 the road 02 importance 2 5 7 the road 13 importance 4 3 7 the road 24 importance 5 1 6the total importance road 6 9 8 7 7 6 43it show obtain great total importance 43example 2input n 5 road 032413output 20explanation the figure show country assign value 43251 the road 03 importance 4 5 9 the road 24 importance 2 1 3 the road 13 importance 3 5 8the total importance road 9 3 8 20it show obtain great total importance 20 Constraints2 n 5 1041 roadslength 5 104roadsilength 20 ai bi n 1ai biThere duplicate road
1graph
you give 0indexed string s target you letter s rearrange form new stringsreturn maximum number copy target form take letter s rearrange Example 1input s ilovecodingonleetcode target codeoutput 2explanationfor copy code letter indice 4 5 6 7for second copy code letter indice 17 18 19 20the string form ecod code rearrange codeWe copy code return 2example 2Input s abcba target abcoutput 1explanationwe copy abc take letter index 0 1 2we copy abc return 1Note extra b index 3 4 reuse letter c index 2 second copy abcexample 3Input s abbaccaddaeea target aaaaaoutput 1explanationwe copy aaaaa take letter index 0 3 6 9 12we copy aaaaa return 1 Constraints1 slength 1001 targetlength 10s target consist lowercase english letter
2string
a sentence string singlespace separate word word contain digits lowercase letter dollar sign a word represent price sequence digit precede dollar signFor example 100 23 6 represent price 100 1e5 notyou give string sentence represent sentence integer discount for word represent price apply discount discount price update word sentence all update price represent exactly decimal placesreturn string represent modified sentencenote price contain 10 digit Example 1input sentence 1 2 5 candy shop discount 50output 050 100 5 candy shopexplanation the word represent price 1 2 a 50 discount 1 yield 050 1 replace 050 a 50 discount 2 yield 1 since need exactly 2 decimal place price replace 2 100example 2input sentence 1 2 3 4 5 6 7 8 9 10 discount 100output 1 2 000 4 000 000 7 8 000 10explanation apply 100 discount price result 0the word represent price 3 5 6 9each replace 000 Constraints1 sentencelength 105sentence consist lowercase english letter digit sentence lead trail spacesAll word sentence separate single spaceall price positive number lead zerosall price 10 digits0 discount 100
2string
you give 0indexed integer array num in step remove element numsi numsi 1 numsi 0 numslengthreturn number step perform num nondecrease array Example 1input num 5344736118511output 3explanation the following step perform Step 1 5344736118511 544761111 Step 2 544761111 5471111 step 3 5471111 571111571111 nondecrease array therefore return 3example 2input num 457713output 0explanation num nondecrease array therefore return 0 Constraints1 numslength 1051 numsi 109
0array
you give 0indexed integer array num length power 2apply follow algorithm numslet n length num if n 1 end process otherwise create new 0indexed integer array newnums length n 2for index 0 n 2 assign value newNumsi minnums2 nums2 1for odd index 0 n 2 assign value newNumsi maxnums2 nums2 1Replace array num newnumsrepeat entire process start step 1return number remain num apply algorithm Example 1input num 13524822output 1explanation the follow array result apply algorithm repeatedlyFirst num 1542second num 14Third num 11 remain number return 1example 2input num 3output 3explanation 3 remain number return 3 Constraints1 numslength 10241 numsi 109numslength power 2
0array
you give integer array num integer k you partition num subsequence element num appear exactly subsequencesreturn minimum number subsequence need difference maximum minimum value subsequence kA subsequence sequence derive sequence delete element change order remain element Example 1input num 36125 k 2Output 2ExplanationWe partition num subsequence 312 65the difference maximum minimum value subsequence 3 1 2the difference maximum minimum value second subsequence 6 5 1since subsequence create return 2 it show 2 minimum number subsequence neededexample 2input num 123 k 1Output 2ExplanationWe partition num subsequence 12 3the difference maximum minimum value subsequence 2 1 1the difference maximum minimum value second subsequence 3 3 0since subsequence create return 2 note optimal solution partition num subsequence 1 23example 3input num 2245 k 0Output 3ExplanationWe partition num subsequence 22 4 5the difference maximum minimum value subsequence 2 2 0the difference maximum minimum value second subsequence 4 4 0the difference maximum minimum value subsequence 5 5 0since subsequence create return 3 it show 3 minimum number subsequence need Constraints1 numslength 1050 numsi 1050 k 105
0array
you give 0indexed array num consist n distinct positive integer apply m operation array ith operation replace number operationsi0 operationsi1it guarantee ith operationoperationsi0 exist numsoperationsi1 exist numsReturn array obtain apply operation Example 1input num 1246 operation 134761output 3271explanation we perform follow operation num replace number 1 3 num 3246 replace number 4 7 num 3276 replace number 6 1 num 3271we return final array 3271example 2input num 12 operation 132132output 21explanation we perform follow operation num replace number 1 3 num 32 replace number 2 1 num 31 replace number 3 2 num 21we return array 21 Constraintsn numslengthm operationslength1 n m 105All value num distinctoperationsilength 21 numsi operationsi0 operationsi1 106operationsi0 exist num apply ith operationoperationsi1 exist num apply ith operation
0array
design text editor cursor followingAdd text cursor isDelete text cursor simulate backspace keyMove cursor leave rightwhen delete text character leave cursor delete the cursor remain actual text move more formally 0 cursorposition currenttextlength holdsimplement TextEditor classtexteditor initialize object textvoid addtextstre text Appends text cursor the cursor end right textint deletetextint k Deletes k character leave cursor Returns number character actually deletedstre cursorLeftint k Moves cursor leave k time Returns min10 len character leave cursor len number character leave cursorstre cursorrightint k Moves cursor right k time Returns min10 len character leave cursor len number character leave cursor Example 1inputtexteditor addtext deletetext addtext cursorright cursorleft deletetext cursorLeft cursorright leetcode 4 practice 3 8 10 2 6outputnull null 4 null etpractice leet 4 practiExplanationTextEditor texteditor new TextEditor the current text the character represent cursortexteditoraddtextleetcode the current text leetcodetexteditordeletetext4 return 4 the current text leet 4 character deletedtexteditoraddtextpractice the current text leetpractice texteditorcursorright3 return etpractice the current text leetpractice the cursor move actual text etpractice 10 character leave cursortexteditorcursorleft8 return leet the current text leetpractice leet min10 4 4 character leave cursortexteditordeletetext10 return 4 the current text practice only 4 character deletedtextEditorcursorLeft2 return the current text practice the cursor move actual text min10 0 0 character leave cursortexteditorcursorright6 return practi the current text practice practi min10 6 6 character leave cursor Constraints1 textlength k 40text consists lowercase English lettersat 2 104 call total addtext deletetext cursorLeft cursorright Followup could find solution time complexity ok
2string
a password say strong satisfie follow criteriait 8 charactersIt contain lowercase letterIt contain uppercase letterIt contain digitIt contain special character the special character character follow string it contain 2 character adjacent position ie aab violate condition aba notGiven string password return true strong password otherwise return false Example 1input password ilovele3tcodeoutput trueexplanation the password meet requirement therefore return trueexample 2input password meyouismydreamoutput falseexplanation the password contain digit contain 2 character adjacent position therefore return falseexample 3input password 1aboutput falseexplanation the password meet length requirement therefore return false Constraints1 passwordlength 100password consist letter digit special character
2string
you give positive integer array spell potion length n m respectively spellsi represent strength ith spell potionsj represent strength jth potionyou give integer success a spell potion pair consider successful product strength successreturn integer array pair length n pairsi number potion form successful pair ith spell Example 1input spell 513 potion 12345 success 7Output 403Explanation 0th spell 5 12345 510152025 4 pair successful 1st spell 1 12345 12345 0 pair successful 2nd spell 3 12345 3691215 3 pair successfulthus 403 returnedExample 2input spell 312 potion 858 success 16Output 202explanation 0th spell 3 858 241524 2 pair successful 1st spell 1 858 858 0 pair successful 2nd spell 2 858 161016 2 pair successful Thus 202 return Constraintsn spellslengthm potionslength1 n m 1051 spellsi potionsi 1051 success 1010
0array
the score array define product sum lengthfor example score 1 2 3 4 5 1 2 3 4 5 5 75given positive integer array num integer k return number nonempty subarray num score strictly kA subarray contiguous sequence element array Example 1input num 21435 k 10Output 6explanationthe 6 subarray have score 10 2 score 2 1 2 1 score 1 1 1 4 score 4 1 4 3 score 3 1 3 5 score 5 1 5 21 score 2 1 2 6note subarray 14 435 consider score 10 36 respectively need score strictly 10example 2input num 111 k 5output 5ExplanationEvery subarray 111 score 5111 score 1 1 1 3 9 great 5thus 5 subarray have score 5 constraints1 numslength 1051 numsi 1051 k 1015
0array
you give 0indexed 2D integer array bracket bracketsi upperi percenti mean ith tax bracket upper bind upperi tax rate percenti the bracket sort upper bind ie upperi1 upperi 0 bracketslengthTax calculate followsThe upper0 dollar earn tax rate percent0The upper1 upper0 dollar earn tax rate percent1the upper2 upper1 dollar earn tax rate percent2And onyou give integer income represent money earn Return money pay taxis Answers 105 actual answer accept Example 1input bracket 3507101225 income 10output 265000explanationbase income 3 dollar 1st tax bracket 4 dollar 2nd tax bracket 3 dollar 3rd tax bracketThe tax rate tax bracket 50 10 25 respectivelyIn total pay 3 50 4 10 3 25 265 taxesExample 2input bracket 10425550 income 2Output 025000explanationbase income 1 dollar 1st tax bracket 1 dollar 2nd tax bracketThe tax rate tax bracket 0 25 respectivelyIn total pay 1 0 1 25 025 taxesExample 3input bracket 250 income 0output 000000ExplanationYou income tax pay total 0 taxis Constraints1 bracketslength 1001 upperi 10000 percenti 1000 income 1000upperi sort ascend orderall value upperi uniquethe upper bind tax bracket great equal income
0array
you give 0indexed m x n integer matrix grid consist distinct integer 0 m n 1 you matrix cell cell row that cell x y x m 1 cell x 1 0 x 1 1 x 1 n 1 note possible cell roweach possible cost give 0indexed 2d array movecost size m n x n movecostij cost move cell value cell column j row the cost move cell row grid ignoredthe cost path grid sum value cell visit plus sum cost move return minimum cost path start cell row end cell row Example 1input grid 534021 moveCost 9815101218624143output 17explanation the path minimum possible cost path 5 0 1 the sum value cell visit 5 0 1 6 the cost move 5 0 3 the cost move 0 1 8so total cost path 6 3 8 17example 2input grid 512403 movecost 121015202382171811391025532output 6explanation the path minimum possible cost path 2 3 the sum value cell visit 2 3 5 the cost move 2 3 1so total cost path 5 1 6 Constraintsm gridlengthn gridilength2 m n 50grid consist distinct integer 0 m n 1movecostlength m nmovecostilength n1 movecostij 100
0array
you give integer array cookies cookiesi denote number cookie ith bag you give integer k denote number child distribute bag cookie all cookies bag child split upthe unfairness distribution define maximum total cookie obtain single child distributionreturn minimum unfairness distribution Example 1input cookie 81510208 k 2output 31Explanation one optimal distribution 8158 1020 the 1st child receive 8158 total 8 15 8 31 cookie the 2nd child receive 1020 total 10 20 30 cookiesthe unfairness distribution max3130 31it show distribution unfairness 31example 2input cookie 61322412 k 3output 7Explanation one optimal distribution 61 322 412 the 1st child receive 61 total 6 1 7 cookie the 2nd child receive 322 total 3 2 2 7 cookie the 3rd child receive 412 total 4 1 2 7 cookiesthe unfairness distribution max777 7it show distribution unfairness 7 constraints2 cookieslength 81 cookiesi 1052 k cookieslength
0array
give stre english letter s return great english letter occur lowercase uppercase letter s the return letter uppercase if letter exist return stringan english letter b great letter b appear english alphabet Example 1input s leetcodeoutput eexplanationthe letter e letter appear low upper caseExample 2Input s arRAzFifOutput rexplanationthe letter r great letter appear low upper casenote a F appear low upper case r great F AExample 3input s abcdefghijkoutput ExplanationThere letter appear low upper case Constraints1 slength 1000 consist lowercase uppercase english letter
2string
you give binary string s positive integer kReturn length long subsequence s make binary number equal knotethe subsequence contain lead zeroesThe string consider equal 0A subsequence string derive string delete character change order remain character Example 1input s 1001010 k 5output 5explanation the long subsequence s make binary number equal 5 00010 number equal 2 decimalnote 00100 00101 possible equal 4 5 decimal respectivelythe length subsequence 5 5 returnedExample 2input s 00101001 k 1Output 6Explanation 000001 long subsequence s make binary number equal 1 number equal 1 decimalthe length subsequence 6 6 return Constraints1 slength 1000si 0 11 k 109
2string
you give integer m n represent height width rectangular piece wood you give 2D integer array price pricesi hi wi pricei indicate sell rectangular piece wood height hi width wi pricei dollarsTo cut piece wood vertical horizontal cut entire height width piece split small piece after cut piece wood number small piece sell piece accord price you sell multiple piece shape sell shape the grain wood make difference rotate piece swap height widthReturn maximum money earn cut m x n piece woodNote cut piece wood time want Example 1input m 3 n 5 price 142227213output 19explanation the diagram show possible scenario it consist 2 piece wood shape 2 x 2 selling price 2 7 14 1 piece wood shape 2 x 1 selling price 1 3 3 1 piece wood shape 1 x 4 sell price 1 2 2this obtain total 14 3 2 19 money earnedIt show 19 maximum money earnedexample 2input m 4 n 6 price 3210142413output 32explanation the diagram show possible scenario it consist 3 piece wood shape 3 x 2 selling price 3 10 30 1 piece wood shape 1 x 4 sell price 1 2 2this obtain total 30 2 32 money earnedIt show 32 maximum money earnedNotice rotate 1 x 4 piece wood obtain 4 x 1 piece wood Constraints1 m n 2001 priceslength 2 104pricesilength 31 hi m1 wi n1 pricei 106all shape wood hi wi pairwise distinct
0array
you give stre s consecutive vertical bar group pair in word 1st 2nd pair 3rd 4th pair forthreturn number s exclude pair note belong exactly pair example 1input s leetcodeoutput 2explanation the considered character underline leetcodethe character second exclude answeralso character fourth exclude answerthere 2 asterisk consider therefore return 2example 2input s iamprogrammeroutput 0explanation in example asterisk s therefore return 0example 3input s youarebeautifuloutput 5explanation the considered character underline youarebeautiful there 5 asterisk consider therefore return 5 constraints1 slength 1000 consists lowercase english letter vertical bar asterisk s contain number vertical bar
2string
you give integer n there undirected graph n node number 0 n 1 you give 2D integer array edge edgesi ai bi denote exist undirected edge connect node ai bireturn number pair different node unreachable Example 1input n 3 edge 010212output 0explanation there pair node unreachable therefore return 0example 2input n 7 edge 0205241654output 14explanation there 14 pair node unreachable other0103061213141523263435364656therefore return 14 Constraints1 n 1050 edgeslength 2 105edgesilength 20 ai bi nai biThere repeat edge
1graph
you give 0indexed integer array num in operation select nonnegative integer x index update numsi equal numsi and numsi XOR xNote and bitwise and operation XOR bitwise XOR operationreturn maximum possible bitwise XOR element num apply operation number times Example 1input num 3246output 7explanation Apply operation x 4 3 num3 6 and 6 xor 4 6 and 2 2now num 3 2 4 2 bitwise xor element 3 xor 2 xor 4 xor 2 7it show 7 maximum possible bitwise xornote operation achieve bitwise XOR 7Example 2input num 12392output 11explanation Apply operation zero timesthe bitwise XOR element 1 xor 2 xor 3 xor 9 XOR 2 11it show 11 maximum possible bitwise XOR Constraints1 numslength 1050 numsi 108
0array
a square matrix say xmatrix follow condition holdall element diagonal matrix nonzeroall element 0given 2D integer array grid size n x n represent square matrix return true grid XMatrix otherwise return false example 1input grid 2001031005204002output trueexplanation refer diagram an XMatrix green element diagonal nonzero red element 0Thus grid XMatrixExample 2input grid 570031050output falseexplanation Refer diagram aboveAn XMatrix green element diagonal nonzero red element 0Thus grid XMatrix Constraintsn gridlength gridilength3 n 1000 gridij 105
0array
you give 0indexed integer array nums1 nums2 length nYou choose integer leave right 0 leave right n swap subarray nums1leftright subarray nums2leftrightfor example nums1 12345 nums2 1112131415 choose leave 1 right 2 nums1 1121345 nums2 11231415you choose apply mention operation anythingthe score array maximum sumnums1 sumnums2 sumarr sum element array arrreturn maximum possible scorea subarray contiguous sequence element array arrleftright denote subarray contain element num index leave right inclusive Example 1input nums1 606060 nums2 109010output 210explanation choosing leave 1 right 1 nums1 609060 nums2 106010the score maxsumnums1 sumnums2 max210 80 210example 2input nums1 2040207030 nums2 5020504020output 220explanation choosing leave 3 right 4 nums1 2040204020 nums2 5020507030the score maxsumnums1 sumnums2 max140 220 220example 3input nums1 71113 nums2 111output 31explanation we choose swap subarraythe score maxsumnums1 sumnums2 max31 3 31 Constraintsn nums1length nums2length1 n 1051 nums1i nums2i 104
0array
you give string key message represent cipher key secret message respectively the step decode message followsUse appearance 26 lowercase English letter key order substitution tablealign substitution table regular english alphabeteach letter message substitute tablespace transform themselvesfor example give key happy boy actual key instance letter alphabet partial substitution table h b p c y d b e o fReturn decode message Example 1input key quick brown fox jump lazy dog message vkbs bs t suepuvoutput secretexplanation the diagram show substitution tableIt obtain take appearance letter quick brown fox jump lazy dogExample 2input key eljuxhpwnyrdgtqkviszcfmabo message zwx hnfx lqantp mnoeius ycgk vcnjrdbOutput box wizard jump quicklyexplanation the diagram show substitution tableIt obtain take appearance letter eljuxhpwnyrdgtqkviszcfmabo Constraints26 keylength 2000key consist lowercase english letter key contain letter English alphabet z once1 messagelength 2000message consist lowercase english letter
2string
you give integer m n represent dimension matrixyou give head link list integersGenerate m x n matrix contain integer link list present spiral order clockwise start topleft matrix if remaining space fill 1return generate matrix Example 1Input m 3 n 5 head 3026817942550output 302685011152497explanation the diagram show value print matrixnote remain space matrix fill 1Example 2Input m 1 n 4 head 012output 0121explanation the diagram show value print leave right matrixthe space matrix set 1 Constraints1 m n 1051 m n 105the number node list range 1 m n0 Nodeval 1000
0array
you give root binary tree follow propertiesleaf node value 0 1 0 represent false 1 represent TrueNonleaf node value 2 3 2 represent boolean or 3 represent boolean ANDThe evaluation node followsIf node leaf node evaluation value node ie True FalseOtherwise evaluate node child apply boolean operation value children evaluationsreturn boolean result evaluate root nodeA binary tree binary tree node 0 2 childrenA leaf node node zero child Example 1input root 213nullnull01output trueexplanation the diagram illustrate evaluation processThe and node evaluate false and True FalseThe or node evaluate true or False TrueThe root node evaluate true return trueExample 2input root 0output falseexplanation the root node leaf node evaluate false return false ConstraintsThe number nod tree range 1 10000 Nodeval 3every node 0 2 childrenleaf node value 0 1nonleaf node value 2 3
1graph
you give 0indexed integer array bus length n busesi represent departure time ith bus you give 0indexed integer array passenger length m passengersj represent arrival time jth passenger all bus departure time unique all passenger arrival time uniqueyou give integer capacity represent maximum number passenger busWhen passenger arrive wait line available bus you bus depart x minute arrive y minutes y x bus Passengers early arrival time bus firstmore formally bus arrive eitherif capacity few passenger wait bus bus orthe capacity passenger early arrival time busReturn late time arrive bus station catch bus you arrive time passengernote the array bus passenger necessarily sort Example 1input bus 1020 passenger 2171819 capacity 2output 16explanation suppose arrive time 16at time 10 bus depart 0th passenger at time 20 second bus depart 1st passengernote arrive time passenger arrive 1st passenger catch busexample 2input bus 203010 passenger 1913264251121 capacity 2output 20explanation suppose arrive time 20at time 10 bus depart 3rd passenger at time 20 second bus depart 5th 1st passengersat time 30 bus depart 0th passenger younotice arrive later 6th passenger take seat bus Constraintsn buseslengthm passengerslength1 n m capacity 1052 busesi passengersi 109each element bus uniqueEach element passenger unique
0array
you give positive 0indexed integer array nums1 nums2 length nThe sum square difference array nums1 nums2 define sum nums1i nums2i2 0 nYou give positive integer k1 k2 you modify element nums1 1 1 k1 time similarly modify element nums2 1 1 k2 timesreturn minimum sum square difference modifying array nums1 k1 time modifying array nums2 k2 timesnote you allow modify array element negative integer Example 1input nums1 1234 nums2 2102019 k1 0 k2 0output 579explanation the element nums1 nums2 modify k1 0 k2 0 the sum square difference 1 22 2 102 3 202 4 192 579example 2input nums1 141012 nums2 5869 k1 1 k2 1Output 43explanation one way obtain minimum sum square difference increase nums10 increase nums22 oncethe minimum sum square difference 2 52 4 82 10 72 12 92 43note way obtain minimum sum square difference way obtain sum small 43 Constraintsn nums1length nums2length1 n 1050 nums1i nums2i 1050 k1 k2 109
0array
you give integer array num integer thresholdFind subarray nums length k element subarray great threshold kReturn size subarray if subarray return 1A subarray contiguous nonempty sequence element array Example 1input num 13431 threshold 6output 3explanation the subarray 343 size 3 element great 6 3 2note valid subarrayExample 2input num 65658 threshold 7output 1explanation the subarray 8 size 1 8 7 1 7 so 1 returnednote subarray 65 size 2 element great 7 2 35 similarly subarray 656 6565 65658 satisfy give conditionsTherefore 2 3 4 5 return Constraints1 numslength 1051 numsi threshold 109
0array
you water dispenser dispense cold warm hot water every second fill 2 cup different type water 1 cup type waterYou give 0indexed integer array length 3 amount0 amount1 amount2 denote number cold warm hot water cup need fill respectively return minimum number second need fill cup Example 1Input 142output 4explanation one way fill cup isSecond 1 Fill cold cup warm cupsecond 2 Fill warm cup hot cupSecond 3 Fill warm cup hot cupSecond 4 Fill warm cupit prove 4 minimum number second neededExample 2input 544output 7explanation one way fill cup isSecond 1 Fill cold cup hot cupSecond 2 Fill cold cup warm cupsecond 3 Fill cold cup warm cupsecond 4 Fill warm cup hot cupSecond 5 Fill cold cup hot cupSecond 6 Fill cold cup warm cupsecond 7 Fill hot cupexample 3input 500output 5explanation every second fill cold cup constraintsamountlength 30 amounti 100
0array
you give string start target length n each string consist character L R _ wherethe character L R represent piece piece L leave blank space directly leave piece r right blank space directly rightthe character _ represent blank space occupy L R piecesreturn true possible obtain string target move piece string start number time otherwise return false example 1input start _ l__r__r _ target l______rroutput trueexplanation we obtain string target start follow move move piece step leave start equal L___R__R _ move piece step right start equal L___R___R move second piece step right start equal l______rrsince possible string target start return trueExample 2input start r_l _ target _ _ lroutput falseexplanation the r piece string start step right obtain _ rl_after piece anymore impossible obtain string target startexample 3input start _ r target r_output falseexplanation the piece string start right impossible obtain string target start Constraintsn startlength targetlength1 n 105start target consist character L R _
2string
you give 0indexed integer array num in operation followingchoose integer num equalremove integer num form pairthe operation num times possibleReturn 0indexed integer array answer size 2 answer0 number pair form answer1 number leftover integer num operation time possible Example 1input num 1321322output 31explanationform pair nums0 nums3 remove num now nums 32322form pair nums0 nums2 remove num now num 222form pair nums0 nums1 remove num now num 2No pair form a total 3 pair form 1 number leftover numsexample 2input num 11output 10explanation form pair nums0 nums1 remove num now num no pair form a total 1 pair form 0 number leftover numsexample 3input num 0output 01Explanation no pair form 1 number leftover num Constraints1 numslength 1000 numsi 100
0array
you give 0indexed array num consist positive integer you choose index j j sum digits number numsi equal numsjReturn maximum value numsi numsj obtain possible index j satisfy condition Example 1input num 184336137output 54explanation the pair j satisfy condition 0 2 number sum digit equal 9 sum 18 36 54 1 4 number sum digit equal 7 sum 43 7 50so maximum sum obtain 54example 2input num 10121914output 1explanation there number satisfy condition return 1 Constraints1 numslength 1051 numsi 109
0array
you give positive integer array num numsdivide you delete number element numsreturn minimum number deletion small element num divide element numsdivide if possible return 1note integer x divide y y x 0 example 1input num 23243 numsdivide 969315output 2explanation the small element 23243 2 divide element numsdividewe use 2 deletion delete element num equal 2 make num 343the small element 343 3 divide element numsdivideit show 2 minimum number deletion neededexample 2input num 436 numsdivide 82610output 1explanation we want small element num divide element numsdividethere way delete element num allow Constraints1 numslength numsdividelength 1051 numsi numsdividei 109
0array
you give integer array rank character array suit you 5 card ith card rank ranksi suit suitsiThe follow type poker hand good worstflush five card suitthree Kind three card rankpair two card rankHigh Card any single cardreturn string represent good type poker hand give cardsnote return value casesensitive Example 1input rank 132319 suit aaaaaoutput FlushExplanation the hand card consist 5 card suit FlushExample 2input rank 44244 suit daabcoutput three KindExplanation the hand second fourth card consist 3 card rank three KindNote Pair hand three Kind well handalso note card three Kind handexample 3input rank 10102129 suit abcadoutput PairExplanation the hand second card consist 2 card rank PairNote Flush Three Kind Constraintsrankslength suitslength 51 ranksi 13a suitsi dNo cards rank suit
0array
give integer array num return number subarray fill 0A subarray contiguous nonempty sequence element array Example 1input num 13002004output 6explanation there 4 occurrence 0 subarraythere 2 occurrence 00 subarraythere occurrence subarray size 2 fill 0 therefore return 6example 2input num 000200output 9explanationthere 5 occurrence 0 subarraythere 3 occurrence 00 subarraythere 1 occurrence 000 subarraythere occurrence subarray size 3 fill 0 therefore return 9example 3input num 2102019output 0explanation there subarray fill 0 therefore return 0 Constraints1 numslength 105109 numsi 109
0array
you give integer array roll length n integer k you roll k sided dice number 1 k n time result ith roll rollsireturn length short sequence roll take rollsA sequence roll length len result roll k sided dice len timesNote sequence take consecutive long order example 1input roll 421233241 k 4Output 3explanation every sequence roll length 1 1 2 3 4 take rollsevery sequence roll length 2 1 1 1 2 4 4 take rollsThe sequence 1 4 2 take roll return 3note sequence take rollsexample 2input roll 1122 k 2output 2explanation every sequence roll length 1 1 2 take rollsThe sequence 2 1 take roll return 2note sequence take roll 2 1 shortestExample 3input roll 11322233 k 4output 1explanation the sequence 4 take roll return 1note sequence take roll 4 short Constraintsn rollslength1 n 1051 rollsi k 105
0array
give string s consist lowercase English letter return letter appear twiceNoteA letter appear twice letter b second occurrence second occurrence bs contain letter appear twice Example 1input s abccbaaczoutput cexplanationthe letter appear index 0 5 6the letter b appear index 1 4the letter c appear index 2 3 7the letter z appear index 8the letter c letter appear twice letter index second occurrence smallestexample 2Input s abcddoutput dexplanationthe letter appear twice d return d constraints2 slength 100s consist lowercase English letterss repeat letter
2string
give 0indexed n x n integer matrix grid return number pair Ri Cj row Ri column Cj equalA row column pair consider equal contain element order ie equal array Example 1input grid 321176277output 1explanation there 1 equal row column pair row 2 Column 1 277example 2input grid 3122144524222422output 3explanation there 3 equal row column pair row 0 Column 0 3122 row 2 Column 2 2422 row 3 Column 2 2422 Constraintsn gridlength gridilength1 n 2001 gridij 105
0array
you give 0indexe positive integer array num positive integer kA pair number num1 num2 call excellent follow condition satisfiedboth number num1 num2 exist array numsthe sum number set bit num1 or num2 num1 and num2 greater equal k or bitwise or operation and bitwise and operationreturn number distinct excellent pairsTwo pair b c d consider distinct c b d for example 1 2 2 1 distinctnote pair num1 num2 num1 num2 excellent occurrence num1 array Example 1input num 1231 k 3output 5explanation the excellent pair follow 3 3 3 and 3 3 or 3 equal 11 binary the total number set bit 2 2 4 great equal k 3 2 3 3 2 2 and 3 equal 10 binary 2 or 3 equal 11 binary the total number set bit 1 2 3 1 3 3 1 1 and 3 equal 01 binary 1 or 3 equal 11 binary the total number set bit 1 2 3so number excellent pair 5example 2input num 511 k 10output 0explanation there excellent pair array Constraints1 numslength 1051 numsi 1091 k 60
0array
you give nonnegative integer array num in operation mustChoose positive integer x x equal small nonzero element numsSubtract x positive element numsReturn minimum number operation element num equal 0 example 1input num 15035output 3explanationin operation choose x 1 now num 04024in second operation choose x 2 now num 02002in operation choose x 2 now num 00000example 2input num 0output 0explanation each element num 0 operation need Constraints1 numslength 1000 numsi 100
0array
you give positive integer array grade represent grade student university you like enter student competition order nonempty group order meet follow conditionsThe sum grade student ith group sum grade student 1th group group lastthe total number student ith group total number student 1th group group lastreturn maximum number group form Example 1input grade 10612735output 3explanation the follow possible way form 3 group student 1st group student grade 12 Sum grade 12 student count 1 2nd group student grade 67 Sum grade 6 7 13 student count 2 3rd group student grade 1035 Sum grade 10 3 5 18 Student count 3it show possible form 3 groupsExample 2input grade 88output 1explanation we form 1 group form 2 group lead equal number student group Constraints1 gradeslength 1051 gradesi 105
0array
you give direct graph n node number 0 n 1 node outgoing edgethe graph represent give 0indexed array edge size n indicate direct edge node node edgesi if outgoing edge edgesi 1you give integer node1 node2return index node reach node1 node2 maximum distance node1 node node2 node minimize if multiple answer return node small index possible answer exist return 1note edge contain cycle Example 1input edge 2231 node1 0 node2 1output 2explanation the distance node 0 node 2 1 distance node 1 node 2 1the maximum distance 1 it prove node small maximum distance 1 return node 2Example 2input edge 121 node1 0 node2 2output 2explanation the distance node 0 node 2 2 distance node 2 0the maximum distance 2 it prove node small maximum distance 2 return node 2 Constraintsn edgeslength2 n 1051 edgesi nedgesi i0 node1 node2 n
1graph
you give direct graph n node number 0 n 1 node outgoing edgethe graph represent give 0indexed array edge size n indicate direct edge node node edgesi if outgoing edge node edgesi 1return length long cycle graph if cycle exist return 1A cycle path start end node Example 1input edge 33423output 3explanation the long cycle graph cycle 2 4 3 2the length cycle 3 3 returnedExample 2input edge 2131output 1explanation there cycle graph Constraintsn edgeslength2 n 1051 edgesi nedgesi
1graph
you give 2D integer array items1 items2 represent set item each array item follow propertiesitemsi valuei weighti valuei represent value weighti represent weight ith itemThe value item item uniquereturn 2D integer array ret reti valuei weighti weighti sum weight item value valueiNote ret return ascending order value Example 1input items1 114538 items2 3115output 163945explanation the item value 1 occur items1 weight 1 items2 weight 5 total weight 1 5 6the item value 3 occur items1 weight 8 items2 weight 1 total weight 8 1 9the item value 4 occur items1 weight 5 total weight 5 therefore return 163945example 2input items1 113223 items2 213213output 142434explanation the item value 1 occur items1 weight 1 items2 weight 3 total weight 1 3 4the item value 2 occur items1 weight 3 items2 weight 1 total weight 3 1 4the item value 3 occur items1 weight 2 items2 weight 2 total weight 2 2 4therefore return 142434example 3input items1 1322 items2 712214output 172471explanationthe item value 1 occur items1 weight 3 items2 weight 4 total weight 3 4 7 the item value 2 occur items1 weight 2 items2 weight 2 total weight 2 2 4 the item value 7 occur items2 weight 1 total weight 1therefore return 172471 Constraints1 items1length items2length 1000items1ilength items2ilength 21 valuei weighti 1000each valuei items1 uniqueeach valuei items2 unique
0array
you give 0indexed integer array num a pair indice j bad pair j j numsj numsireturn total number bad pair num Example 1input num 4133output 5explanation the pair 0 1 bad pair 1 0 1 4the pair 0 2 bad pair 2 0 3 4 2 1the pair 0 3 bad pair 3 0 3 4 3 1the pair 1 2 bad pair 2 1 3 1 1 2the pair 2 3 bad pair 3 2 3 3 1 0there total 5 bad pair return 5example 2input num 12345output 0explanation there bad pair Constraints1 numslength 1051 numsi 109
0array
you give 0indexed array positive integer task represent task need complete order tasksi represent type ith taskyou give positive integer space represent minimum number day pass completion task task type performedeach day task complete eithercomplete task task ortake breakreturn minimum number day need complete task Example 1input task 121231 space 3Output 9explanationone way complete task 9 day followsDay 1 Complete 0th taskDay 2 Complete 1st taskDay 3 take breakday 4 take breakday 5 Complete 2nd taskday 6 Complete 3rd taskDay 7 take breakday 8 Complete 4th taskday 9 complete 5th taskit show task complete 9 daysexample 2input task 5885 space 2output 6explanationone way complete task 6 day followsDay 1 Complete 0th taskDay 2 Complete 1st taskDay 3 take breakday 4 take breakday 5 Complete 2nd taskday 6 complete 3rd taskit show task complete 6 day Constraints1 taskslength 1051 tasksi 1091 space taskslength
0array
you give 0indexed integer array num in operation replace element array element sum itFor example consider num 567 in operation replace nums1 2 4 convert num 5247return minimum number operation array sort nondecrease order Example 1input num 393output 2explanation here step sort array nondecrease order from 393 replace 9 3 6 array 3363 from 3363 replace 6 3 3 array 33333there 2 step sort array nondecrease order therefore return 2example 2input num 12345output 0explanation the array nondecrease order therefore return 0 Constraints1 numslength 1051 numsi 109
0array
you give 0indexe strictly increase integer array num positive integer diff a triplet j k arithmetic triplet follow condition meti j knumsj numsi diff andnumsk numsj diffreturn number unique arithmetic triplet Example 1input num 0146710 diff 3output 2explanation1 2 4 arithmetic triplet 7 4 3 4 1 32 4 5 arithmetic triplet 10 7 3 7 4 3 Example 2input num 456789 diff 2Output 2explanation0 2 4 arithmetic triplet 8 6 2 6 4 21 3 5 arithmetic triplet 9 7 2 7 5 2 constraints3 numslength 2000 numsi 2001 diff 50nums strictly increase
0array
you give 0indexed integer array num you partition array contiguous subarraysWe partition array valid obtain subarray satisfie follow conditionsthe subarray consist exactly 2 equal element for example subarray 22 goodthe subarray consist exactly 3 equal element for example subarray 444 goodThe subarray consist exactly 3 consecutive increase element difference adjacent element 1 for example subarray 345 good subarray 135 notreturn true array valid partition otherwise return false example 1input num 44456output trueexplanation the array partition subarray 44 456this partition valid return trueexample 2input num 1112output falseexplanation there valid partition array Constraints2 numslength 1051 numsi 106
0array
you give string s consist lowercase letter integer k we string t ideal follow condition satisfiedt subsequence stre sthe absolute difference alphabet order adjacent letter t equal kReturn length long ideal stringa subsequence string derive string delete character change order remain charactersnote alphabet order cyclic for example absolute difference alphabet order z 25 1 Example 1input s acfgbd k 2output 4explanation the long ideal string acbd the length string 4 4 returnednote acfgbd ideal c f difference 3 alphabet orderexample 2input s abcd k 3output 4explanation the long ideal string abcd the length string 4 4 return Constraints1 slength 1050 k 25s consist lowercase english letter
2string
you give n x n integer matrix gridGenerate integer matrix maxlocal size n 2 x n 2 thatmaxLocalij equal large value 3 x 3 matrix grid center row 1 column j 1in word want find large value contiguous 3 x 3 matrix gridReturn generate matrix Example 1input grid 9981562682646222output 9986explanation the diagram show original matrix generate matrixnotice value generate matrix correspond large value contiguous 3 x 3 matrix gridExample 2input grid 1111111111112111111111111output 222222222explanation notice 2 contain contiguous 3 x 3 matrix grid Constraintsn gridlength gridilength3 n 1001 gridij 100
0array
you give direct graph n node label 0 n 1 node exactly outgoing edgethe graph represent give 0indexed integer array edge length n edgesi indicate direct edge node node edgesithe edge score node define sum label nod edge point iReturn node high edge score if multiple node edge score return node small index Example 1input edge 10000775output 7explanation the node 1 2 3 4 edge point node 0 the edge score node 0 1 2 3 4 10 the node 0 edge point node 1 the edge score node 1 0 the node 7 edge point node 5 the edge score node 5 7 the node 5 6 edge point node 7 the edge score node 7 5 6 11node 7 high edge score return 7example 2input edge 2002output 0explanation the node 1 2 edge point node 0 the edge score node 0 1 2 3 the node 0 3 edge point node 2 the edge score node 2 0 3 3nodes 0 2 edge score 3 since node 0 small index return 0 Constraintsn edgeslength2 n 1050 edgesi nedgesi
1graph
you give 0indexed string pattern length n consist character I mean increase d mean decreasinga 0indexed string num length n 1 create follow conditionsnum consist digit 1 9 digit onceif patterni I numi numi 1if patterni D numi numi 1Return lexicographically smallest possible string num meet condition Example 1input pattern IIIDIDDDOutput 123549876explanationat index 0 1 2 4 numi numi1At index 3 5 6 7 numi numi1some possible value num 245639871 135749862 123849765it prove 123549876 small possible num meet conditionsnote 123414321 possible digit 1 onceExample 2input pattern DDDOutput 4321explanationsome possible value num 9876 7321 8742it prove 4321 small possible num meet condition Constraints1 patternlength 8pattern consist letter I d
2string
you give 0indexed string block length n blocksi W B represent color ith block the character W B denote color white black respectivelyYou give integer k desire number consecutive black blocksIn operation recolor white block black blockReturn minimum number operation need occurrence k consecutive black block Example 1input block WBBWWBBWBW k 7Output 3explanationone way achieve 7 consecutive black block recolor 0th 3rd 4th blocksso block BBBBBBBWBW it show way achieve 7 consecutive black block 3 operationstherefore return 3example 2input block WBWBBBW k 2output 0explanationno change need 2 consecutive black block existtherefore return 0 Constraintsn blockslength1 n 100blocksi W B1 k n
2string
you give binary string s in second occurrence 01 simultaneously replace 10 this process repeat occurrence 01 existreturn number second need complete process Example 1input s 0110101output 4explanation after second s 1011010after second s 1101100after second s 1110100after fourth second s 1111000no occurrence 01 exist long process need 4 second completeso return 4example 2input s 11100output 0explanationno occurrence 01 exist s process need 0 second completeso return 0 Constraints1 slength 1000si 0 1 Follow upcan solve problem on time complexity
2string
you give 0indexed integer array num removequerie length n for ith query element num index removeQueriesi remove splitting num different segmentsA segment contiguous sequence positive integer num a segment sum sum element segmentreturn integer array answer length n answeri maximum segment sum apply ith removalNote the index remove Example 1input num 12561 removequerie 03241output 147220explanation using 0 indicate remove element answer followsquery 1 Remove 0th element num 02561 maximum segment sum 14 segment 2561query 2 Remove 3rd element num 02501 maximum segment sum 7 segment 25query 3 Remove 2nd element num 02001 maximum segment sum 2 segment 2 Query 4 Remove 4th element num 02000 maximum segment sum 2 segment 2 Query 5 Remove 1st element num 00000 maximum segment sum 0 segmentsfinally return 147220example 2input num 32111 removequerie 3210output 16530explanation using 0 indicate remove element answer followsquery 1 Remove 3rd element num 32110 maximum segment sum 16 segment 3211query 2 Remove 2nd element num 3200 maximum segment sum 5 segment 32query 3 Remove 1st element num 3000 maximum segment sum 3 segment 3query 4 Remove 0th element num 0000 maximum segment sum 0 segmentsfinally return 16530 Constraintsn numslength removequerieslength1 n 1051 numsi 1090 removeQueriesi nAll value removequerie unique
0array
you enter competition give positive integer initialEnergy initialexperience denote initial energy initial experience respectivelyyou give 0indexed integer array energy experience length nYou face n opponent order the energy experience ith opponent denote energyi experiencei respectively when face opponent need strictly great experience energy defeat opponent availabledefeate ith opponent increase experience experiencei decrease energy energyibefore start competition train number hour after hour training choose increase initial experience increase initial energy onereturn minimum number training hour require defeat n opponent Example 1input initialEnergy 5 initialexperience 3 energy 1432 experience 2631output 8explanation you increase energy 11 6 hour training experience 5 2 hour trainingyou face opponent follow order you energy experience 0th opponent win your energy 11 1 10 experience 5 2 7 you energy experience 1st opponent win your energy 10 4 6 experience 7 6 13 you energy experience 2nd opponent win your energy 6 3 3 experience 13 3 16 you energy experience 3rd opponent win your energy 3 2 1 experience 16 1 17you total 6 2 8 hour training competition return 8it prove small answer existsexample 2input initialEnergy 2 initialexperience 4 energy 1 experience 3output 0explanation you need additional energy experience win competition return 0 constraintsn energylength experiencelength1 n 1001 initialEnergy initialExperience energyi experiencei 100
0array
you give stre num consist digit onlyreturn large palindromic integer form string form digit take num it contain lead zeroesnotesyou need use digit num use digitthe digit reorder Example 1input num 444947137output 7449447explanation Use digit 4449477 444947137 form palindromic integer 7449447it show 7449447 large palindromic integer formedExample 2input num 00009output 9explanation it show 9 large palindromic integer formednote integer return contain lead zero Constraints1 numlength 105num consist digit
2string
you give root binary tree unique value integer start at minute 0 infection start node value starteach minute node infect ifthe node currently uninfectedthe node adjacent infect nodeReturn number minute need entire tree infect Example 1input root 153null410692 start 3output 4explanation the follow node infect Minute 0 Node 3 Minute 1 Nodes 1 10 6 Minute 2 Node 5 Minute 3 Node 4 Minute 4 Nodes 9 2it take 4 minute tree infect return 4example 2input root 1 start 1output 0explanation at minute 0 node tree infect return 0 ConstraintsThe number nod tree range 1 1051 Nodeval 105each node unique valueA node value start exist tree
1graph
you give integer array num positive integer k you choose subsequence array sum element togetherWe define KSum array kth large subsequence sum obtain necessarily distinctreturn KSum arrayA subsequence array derive array delete element change order remain elementsnote subsequence consider sum 0 example 1input num 242 k 5output 2explanation all possible subsequence sum obtain follow sorted decrease order 6 4 4 2 2 0 0 2the 5sum array 2example 2input num 12341012 k 16Output 10explanation the 16sum array 10 Constraintsn numslength1 n 105109 numsi 1091 k min2000 2n
0array
you give integer array num length n integer array query length mReturn array answer length m answeri maximum size subsequence num sum element equal queriesiA subsequence array derive array delete element change order remain element Example 1input num 4521 query 31021output 234explanation we answer query follow the subsequence 21 sum equal 3 it prove 2 maximum size subsequence answer0 2 the subsequence 451 sum equal 10 it prove 3 maximum size subsequence answer1 3 the subsequence 4521 sum equal 21 it prove 4 maximum size subsequence answer2 4example 2input num 2345 query 1output 0explanation the subsequence subsequence sum equal 1 answer0 0 Constraintsn numslengthm querieslength1 n m 10001 numsi queriesi 106
0array
you give string s contain star in operation canchoose star sRemove close nonstar character leave remove star itselfReturn string star removednotethe input generate operation possibleIt show result string unique Example 1input s leetcodeoutput lecoeexplanation perform removal leave right the close character 1st star t leetcode s leecode the close character 2nd star e leecode s lecode the close character 3rd star d lecode s lecoethere star return lecoeexample 2input s eraseoutput Explanation the entire string remove return string Constraints1 slength 105s consist lowercase english letter star the operation perform s
2string
give 0indexed integer array num determine exist subarray length 2 equal sum note subarray begin different indicesReturn true subarray exist false otherwisea subarray contiguous nonempty sequence element array example 1input num 424output trueexplanation the subarray element 42 24 sum 6Example 2input num 12345output falseexplanation no subarray size 2 sumexample 3input num 000output trueexplanation the subarray nums0nums1 nums1nums2 sum 0 note subarray content subarray consider different different position original array Constraints2 numslength 1000109 numsi 109
0array
you give 0indexed m x n binary matrix matrix integer numSelect denote number distinct column select matrixlet consider s c1 c2 cnumselect set column select a row row cover s iffor cell matrixrowcol 0 col n 1 matrixrowcol 1 col present s orno cell row value 1you need choose numselect column number row cover maximizedreturn maximum number row cover set numselect column example 1input matrix 000101011001 numselect 2output 3explanation one possible way cover 3 row show diagram aboveWe choose s 0 2 row 0 cover occurrence 1 row 1 cover column value 1 ie 0 2 present s row 2 cover matrix21 1 1 present s row 3 cover matrix22 1 2 present sThus cover rowsnote s 1 2 cover 3 row show row coveredexample 2input matrix 10 numSelect 1output 2explanation Selecting column result row cover entire matrix selectedTherefore return 2 Constraintsm matrixlengthn matrixilength1 m n 12matrixij 0 11 numselect n
0array
you n robot you give 0indexed integer array chargetime runningcost length n the ith robot cost chargetimesi unit charge cost runningcostsi unit run you give integer budgetthe total cost run k choose robot equal maxchargetime k sumrunningcost maxchargetime large charge cost k robot sumrunningcost sum run cost k robotsreturn maximum number consecutive robot run total cost exceed budget Example 1input chargetime 36134 runningcost 21345 budget 25output 3explanation it possible run individual consecutive pair robot budgetTo obtain answer 3 consider 3 robot the total cost max361 3 sum213 6 3 6 24 25it show possible run 3 consecutive robot budget return 3example 2input chargetime 111219 runningcost 1087 budget 19output 0explanation no robot run exceed budget return 0 ConstraintschargeTimeslength runningcostslength n1 n 5 1041 chargetimesi runningcostsi 1051 budget 1015
0array
you give array num consist positive integersWe subarray num nice bitwise and pair element different position subarray equal 0return length long nice subarrayA subarray contiguous arraynote subarray length 1 consider nice example 1input num 1384810output 3explanation the long nice subarray 3848 this subarray satisfy condition 3 and 8 0 3 and 48 0 8 and 48 0it prove long nice subarray obtain return 3example 2input num 3151113output 1explanation the length long nice subarray 1 any subarray length 1 choose Constraints1 numslength 1051 numsi 109
0array
you give integer n there n room number 0 n 1you give 2D integer array meeting meetingsi starti endi mean meet hold halfclosed time interval starti endi all value starti uniquemeeting allocate room follow mannereach meeting place unused room low numberIf available room meeting delay room free the delay meeting duration original meetingwhen room unused meeting early original start time give roomreturn number room hold meeting if multiple room return room low numberA halfclose interval b interval b include include b Example 1input n 2 meeting 010152734output 0explanation at time 0 room the meeting start room 0 at time 1 room 1 the second meeting start room 1 at time 2 room the meeting delay at time 3 room the fourth meeting delay at time 5 meeting room 1 finish the meeting start room 1 time period 510 at time 10 meeting room finish the fourth meeting start room 0 time period 1011both room 0 1 hold 2 meeting return 0 example 2input n 3 meeting 120210354968output 1explanation at time 1 room the meeting start room 0 at time 2 room 1 2 the second meeting start room 1 at time 3 room 2 the meeting start room 2 at time 4 room the fourth meeting delay at time 5 meeting room 2 finish the fourth meeting start room 2 time period 510 at time 6 room the fifth meeting delay at time 10 meeting room 1 2 finish the fifth meeting start room 1 time period 1012room 0 hold 1 meeting room 1 2 hold 2 meeting return 1 Constraints1 n 1001 meetingslength 105meetingsilength 20 starti endi 5 105All value starti unique
0array
give integer array num return frequent elementIf tie return smallest if element return 1 example 1input num 0122441output 2explanationthe element 0 2 4 of 2 4 appear mostwe return small 2example 2input num 444924output 4Explanation 4 element appear mostExample 3input num 294721411337257output 1explanation there element Constraints1 numslength 20000 numsi 105
0array
give string s partition string substring character substre unique that letter appear single substre oncereturn minimum number substring partitionnote character belong exactly substre partition Example 1input s abacabaoutput 4explanationtwo possible partition abacaba abacabait show 4 minimum number substring neededexample 2input s ssssssoutput 6explanationthe valid partition ssssss Constraints1 slength 105s consist english lowercase letter
2string
you give 2D integer array intervals intervalsi lefti righti represent inclusive interval lefti rightiyou divide interval group interval exactly group interval group intersect otherreturn minimum number group need makeTwo interval intersect common number for example interval 1 5 5 8 intersect Example 1input interval 510681523110output 3explanation we divide interval follow group Group 1 1 5 6 8 Group 2 2 3 5 10 Group 3 1 10it prove possible divide interval few 3 groupsExample 2input interval 13568101113output 1explanation none interval overlap group Constraints1 intervalslength 105intervalsilength 21 lefti righti 106
0array
you give integer array num integer kFind long subsequence num meet follow requirementsthe subsequence strictly increase andthe difference adjacent element subsequence kreturn length long subsequence meet requirementsA subsequence array derive array delete element change order remain element Example 1input num 4214345815 k 3Output 5explanationthe long subsequence meet requirement 13458the subsequence length 5 return 5note subsequence 1345815 meet requirement 15 8 7 large 3example 2input num 745181247 k 5output 4explanationthe long subsequence meet requirement 45812the subsequence length 4 return 4example 3input num 15 k 1Output 1explanationthe long subsequence meet requirement 1the subsequence length 1 return 1 Constraints1 numslength 1051 numsi k 105
0array
Alice Bob travel Rome separate business meetingsyou give 4 string arrivealice leaveAlice arriveBob leaveBob Alice city date arriveAlice leaveAlice inclusive Bob city date arriveBob leaveBob inclusive each 5character string format MMDD corresponding month day datereturn total number day Alice Bob Rome togetheryou assume date occur calendar year leap year note number day month represent 31 28 31 30 31 30 31 31 30 31 30 31 Example 1input arrivealice 0815 leaveAlice 0818 arriveBob 0816 leaveBob 0819output 3explanation Alice Rome August 15 August 18 Bob Rome August 16 August 19 they Rome August 16th 17th 18th answer 3example 2input arriveAlice 1001 leaveAlice 1031 arriveBob 1101 leaveBob 1231output 0explanation there day Alice Bob Rome return 0 ConstraintsAll date provide format MMDDAlice Bobs arrival date early equal leaving datesthe give date valid date nonleap year
2string
you give 0indexed integer array player playersi represent ability ith player you give 0indexed integer array trainer trainersj represent training capacity jth trainerthe ith player match jth trainer player ability equal trainer training capacity additionally ith player match trainer jth trainer match playerreturn maximum number matching player trainer satisfy condition Example 1input player 479 trainer 8258output 2explanationone way form matching follow players0 match trainers0 4 8 players1 match trainers3 7 8it prove 2 maximum number matching formedExample 2input player 111 trainer 10output 1explanationthe trainer match 3 playersEach player match trainer maximum answer 1 Constraints1 playerslength trainerslength 1051 playersi trainersj 109
0array
you give 0indexed array num length n consist nonnegative integer for index 0 n 1 determine size minimum sized nonempty subarray num start inclusive maximum possible bitwise ORIn word let Bij bitwise or subarray numsij you need find small subarray start bitwise or subarray equal maxbik k n 1the bitwise or array bitwise or number itreturn integer array answer size n answeri length minimum size subarray start maximum bitwise ORA subarray contiguous nonempty sequence element array example 1input num 10213output 33221explanationthe maximum possible bitwise OR start index 3 start index 0 short subarray yield 102 start index 1 short subarray yield maximum bitwise or 021 start index 2 short subarray yield maximum bitwise or 21 Starting index 3 short subarray yield maximum bitwise or 13 Starting index 4 short subarray yield maximum bitwise or 3therefore return 33221 Example 2input num 12Output 21ExplanationStarting index 0 short subarray yield maximum bitwise or length 2starte index 1 short subarray yield maximum bitwise or length 1therefore return 21 Constraintsn numslength1 n 1050 numsi 109
0array
you give 0indexed 2D integer array transaction transactionsi costi cashbackiThe array describe transaction transaction complete exactly order at give moment certain money in order complete transaction money costi hold true after perform transaction money money costi cashbackireturn minimum money require transaction transaction complete regardless order transaction example 1input transaction 215042output 10explanationstarting money 10 transaction perform orderIt show start money 10 fail complete transaction orderexample 2input transaction 3003output 3explanation if transaction order 3003 minimum money require complete transaction 3 if transaction order 0330 minimum money require complete transaction 0Thus start money 3 transaction perform order Constraints1 transactionslength 105transactionsilength 20 costi cashbacki 109
0array
an alphabetical continuous string string consist consecutive letter alphabet in word substre string abcdefghijklmnopqrstuvwxyzfor example abc alphabetical continuous string acb za notGiven string s consist lowercase letter return length long alphabetical continuous substre Example 1input s abacabaoutput 2explanation there 4 distinct continuous substring b c abab long continuous substringExample 2input s abcdeoutput 5explanation abcde long continuous substre Constraints1 slength 105s consist english lowercase letter
2string
give root perfect binary tree reverse node value odd level treefor example suppose node value level 3 21347112918 18291174312return root reverse treeA binary tree perfect parent node child leave levelthe level node number edge path root node Example 1input root 2358132134output 2538132134explanation the tree odd levelThe node level 1 3 5 respectively reverse 5 3example 2input root 71311output 71113explanation the node level 1 13 11 reverse 11 13example 3input root 012000011112222output 021000022221111explanation the odd level nonzero valuesThe node level 1 1 2 2 1 reversalthe node level 3 1 1 1 1 2 2 2 2 2 2 2 2 1 1 1 1 reversal ConstraintsThe number nod tree range 1 2140 Nodeval 105root perfect binary tree
1graph
you give integer array num size nConsider nonempty subarray num maximum possible bitwise andin word let k maximum value bitwise and subarray num then subarray bitwise and equal k consideredreturn length long subarraythe bitwise and array bitwise and number itA subarray contiguous sequence element array Example 1input num 123322output 2explanationthe maximum possible bitwise and subarray 3the long subarray value 33 return 2example 2input num 1234output 1explanationthe maximum possible bitwise and subarray 4the long subarray value 4 return 1 Constraints1 numslength 1051 numsi 106
0array
you give 0indexed integer array num size n positive integer kWe index range k n k good follow condition satisfiedThe k element index nonincrease orderthe k element index nondecrease orderreturn array good index sort increase order Example 1input num 2111341 k 2output 23explanation there good index array index 2 the subarray 21 nonincrease order subarray 13 nondecrease order index 3 the subarray 11 nonincrease order subarray 34 nondecrease ordernote index 4 good 41 nondecreasingExample 2input num 2112 k 2output Explanation there good index array Constraintsn numslength3 n 1051 numsi 1061 k n 2
0array
you give 0indexed string word consist lowercase english letter you need select index remove letter index word frequency letter present word equalreturn true possible remove letter frequency letter word equal false otherwisenotethe frequency letter x number time occur stringYou remove exactly letter choose Example 1input word abccoutput trueexplanation Select index 3 delete word abc character frequency 1example 2Input word aazzOutput falseexplanation we delete character frequency 1 frequency z 2 vice versa it impossible present letter equal frequency Constraints2 wordlength 100word consist lowercase english letter
2string
you give 0indexe array nums1 nums2 consist nonnegative integer there exist array nums3 contain bitwise XOR pairing integer nums1 nums2 integer nums1 pair integer nums2 exactly oncereturn bitwise XOR integer nums3 example 1input nums1 213 nums2 10250output 13explanationa possible nums3 array 8072113419163the bitwise XOR number 13 return 13example 2input nums1 12 nums2 34output 0explanationall possible pair bitwise XORs nums10 nums20 nums10 nums21 nums11 nums20and nums11 nums21thu possible nums3 array 25162 5 1 6 0 return 0 Constraints1 nums1length nums2length 1050 nums1i nums2j 109
0array
you give 0indexed integer array nums1 nums2 size n integer diff Find number pair j that0 j n 1 andnums1i nums1j nums2i nums2j diffreturn number pair satisfy condition Example 1input nums1 325 nums2 221 diff 1Output 3explanationthere 3 pair satisfy conditions1 0 j 1 3 2 2 2 1 since j 1 1 pair satisfie conditions2 0 j 2 3 5 2 1 1 since j 2 2 pair satisfie conditions3 1 j 2 2 5 2 1 1 since j 3 2 pair satisfie conditionstherefore return 3example 2input nums1 31 nums2 22 diff 1Output 0ExplanationSince exist pair satisfie condition return 0 Constraintsn nums1length nums2length2 n 105104 nums1i nums2i 104104 diff 104
0array
you give m x n integer matrix gridwe define hourglass matrix follow formreturn maximum sum element hourglassnote hourglass rotate entirely contain matrix example 1input grid 6213421592874129output 30explanation the cell show represent hourglass maximum sum 6 2 1 2 9 2 8 30example 2input grid 123456789output 35explanation there hourglass matrix sum 1 2 3 5 7 8 9 35 Constraintsm gridlengthn gridilength3 m n 1500 gridij 106
0array
you give string s consist lowercase english letter in operation candelete entire string s orDelete letter s letter s equal follow letter s range 1 slength 2for example s ababc operation delete letter s abc letter s follow letter s equal abreturn maximum number operation need delete s Example 1input s abcabcdabcoutput 2explanation Delete 3 letter abc 3 letter equal now s abcdabc Delete lettersWe 2 operation return 2 it prove 2 maximum number operation needednote second operation delete abc occurrence abc happen 3 lettersexample 2input s aaabaabOutput 4explanation Delete letter letter equal now s aabaab Delete 3 letter aab 3 letter equal now s aab Delete letter letter equal now s ab Delete lettersWe 4 operation return 4 it prove 4 maximum number operation neededExample 3input s aaaaaoutput 5explanation in operation delete letter s Constraints1 slength 4000s consists lowercase english letter
2string
there n employee unique i d 0 n 1you give 2D integer array log logsi idi leaveTimei whereidi i d employee work ith task andleaveTimei time employee finish ith task all value leaveTimei uniquenote ith task start moment right 1th task end 0th task start time 0return i d employee work task long time if tie employee return smallest i d Example 1input n 10 log 032509115output 1explanation Task 0 start 0 end 3 3 unit timesTask 1 start 3 end 5 2 unit timesTask 2 start 5 end 9 4 unit timestask 3 start 9 end 15 6 unit timesthe task long time task 3 employee i d 1 work return 1example 2input n 26 log 1137212717output 3explanation Task 0 start 0 end 1 1 unit timesTask 1 start 1 end 7 6 unit timesTask 2 start 7 end 12 5 unit timestask 3 start 12 end 17 5 unit timesthe task long time task 1 the employee work 3 return 3example 3input n 2 log 010120output 0explanation Task 0 start 0 end 10 10 unit timesTask 1 start 10 end 20 10 unit timesthe task long time task 0 1 the employee work 0 1 return smallest i d 0 Constraints2 n 5001 logslength 500logsilength 20 idi n 11 leaveTimei 500idi idi1leavetimei sort strictly increase order
0array
you give integer array pref size n find return array arr size n satisfiesprefi arr0 arr1 arriNote denote bitwisexor operationIt prove answer unique Example 1input pref 52031output 57232explanation from array 57232 follow pref0 5 pref1 5 7 2 pref2 5 7 2 0 pref3 5 7 2 3 3 pref4 5 7 2 3 2 1example 2input pref 13output 13explanation we pref0 arr0 13 Constraints1 preflength 1050 prefi 106
0array
you give stre s robot currently hold string t apply follow operation s t emptyremove character string s robot the robot append character string tRemove character string t robot the robot write character paperreturn lexicographically small string write paper Example 1input s zzaoutput azzexplanation let p denote write stringinitially p szza tPerform operation time p s tzzaperform second operation time pazz s tExample 2input s bacoutput abcexplanation let p denote write stringperform operation twice p sc tba Perform second operation twice pab sc t perform operation pab s tc perform second operation pabc s tExample 3input s bddaOutput addbexplanation let p denote write stringinitially p sbdda tperform operation time p s tbddaperform second operation time paddb s t Constraints1 slength 105s consist english lowercase letter
2string
you give 0indexed m x n integer matrix grid integer k you currently position 0 0 want reach position m 1 n 1 move rightreturn number path sum element path divisible k since answer large return modulo 109 7 Example 1input grid 524305072 k 3Output 2explanation there path sum element path divisible kThe path highlight red sum 5 2 4 5 2 18 divisible 3the second path highlight blue sum 5 3 0 5 2 15 divisible 3example 2input grid 00 k 5output 1explanation the path highlight red sum 0 0 0 divisible 5example 3input grid 734923622370 k 1output 10explanation every integer divisible 1 sum element possible path divisible k Constraintsm gridlengthn gridilength1 m n 5 1041 m n 5 1040 gridij 1001 k 50
0array
you give string length 5 call time represent current time digital clock format hhmm the early possible time 0000 late possible time 2359in string time digit represent symbol unknown replace digit 0 9Return integer answer number valid clock time create replace digit 0 9 example 1input time 500output 2explanation we replace 0 1 produce 0500 1500 note replace 2 time 2500 invalid in total choicesexample 2input time 00output 100explanation each replace digit 0 9 100 total choicesexample 3input time output 1440explanation there 24 possible choice hour 60 possible choice minute in total 24 60 1440 choice Constraintstime valid string length 5 format hhmm00 hh 2300 mm 59some digit replace need replace digit 0 9
2string
give positive integer n exist 0indexed array call power compose minimum number power 2 sum n the array sort nondecrease order way form arrayYou give 0indexed 2D integer array query queriesi lefti righti each queriesi represent query find product powersj lefti j rightireturn array answer equal length query answersi answer ith query since answer ith query large answersi return modulo 109 7 Example 1input n 15 query 012203output 2464explanationfor n 15 power 1248 it show power small sizeanswer 1st query powers0 powers1 1 2 2answer 2nd query powers2 4answer 3rd query powers0 powers1 powers2 powers3 1 2 4 8 64each answer modulo 109 7 yield answer 2464 returnedExample 2input n 2 query 00output 2explanationfor n 2 power 2the answer query powers0 2 the answer modulo 109 7 2 return Constraints1 n 1091 querieslength 1050 starti endi powerslength
0array
you give 0indexed array num comprise n nonnegative integersIn operation mustchoose integer 1 n numsi 0decrease numsi 1increase numsi 1 1Return minimum possible value maximum integer num perform number operation Example 1input num 3716output 5explanationone set optimal operation follows1 choose 1 num 46162 choose 3 num 46253 choose 1 num 5525the maximum integer num 5 it show maximum number 5therefore return 5example 2input num 101Output 10explanationit optimal leave num 10 maximum value return 10 Constraintsn numslength2 n 1050 numsi 109
0array
give integer array num contain zero find large positive integer k k exist arrayreturn positive integer k if integer return 1 example 1input num 1233output 3explanation 3 valid k find arrayExample 2input num 1106771output 7explanation both 1 7 correspond negative value array 7 large valueExample 3input num 1086723output 1explanation there single valid k return 1 Constraints1 numslength 10001000 numsi 1000numsi 0
0array
you give array num consist positive integersyou integer array reverse digit add end array you apply operation original integer numsreturn number distinct integer final array Example 1input num 113101231output 6explanation after include reverse number result array 11310123113112113the reverse integer add end array underline Note integer 10 reverse 01 1the number distinct integer array 6 the number 1 10 12 13 21 31example 2input num 222output 1explanation after include reverse number result array 222222the number distinct integer array 1 the number 2 Constraints1 numslength 1051 numsi 106
0array