86% found this document useful (7 votes)
3K views1,364 pages

Leetcode Hard1

The document contains solutions to 53 algorithm problems in multiple programming languages like C++ and Python. Each problem is accompanied by solutions in different languages. The document is organized by problem number and name with corresponding solutions below each problem.

Uploaded by

Osama Iqbal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
86% found this document useful (7 votes)
3K views1,364 pages

Leetcode Hard1

The document contains solutions to 53 algorithm problems in multiple programming languages like C++ and Python. Each problem is accompanied by solutions in different languages. The document is organized by problem number and name with corresponding solutions below each problem.

Uploaded by

Osama Iqbal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1364

Table of Contents

Table of Contents 2
1. Median of Two Sorted Arrays 17
1. Median of Two Sorted Arrays(C++) 18
1. Median of Two Sorted Arrays(Python) 20
2. Regular Expression Matching 22
2. Regular Expression Matching(Python) 23
3. Merge k Sorted Lists 25
3. Merge k Sorted Lists(C++) 26
3. Merge k Sorted Lists(Python) 28
4. Reverse Nodes in k-Group 30
4. Reverse Nodes in k-Group(C++) 32
4. Reverse Nodes in k-Group(Python) 33
5. Substring with Concatenation of All Words 34
5. Substring with Concatenation of All Words(C++) 35
5. Substring with Concatenation of All Words(Python) 37
6. Longest Valid Parentheses 39
6. Longest Valid Parentheses(C++) 40
6. Longest Valid Parentheses(Python) 41
7. Sudoku Solver 42
7. Sudoku Solver(Python) 43
8. First Missing Positive 44
8. First Missing Positive(C++) 45
8. First Missing Positive(Python) 46
9. Trapping Rain Water 47
9. Trapping Rain Water(C++) 48
9. Trapping Rain Water(Python) 49
10. Wildcard Matching 52
10. Wildcard Matching(Python) 53
11. Jump Game II 55
11. Jump Game II(Python) 56
12. N-Queens 57
12. N-Queens(Python) 58
13. N-Queens-II 59
13. N-Queens-II(Python) 60
14. Merge Intervals 61
14. Merge Intervals(C++) 62
14. Merge Intervals(Python) 63
15. Insert Interval 64
15. Insert Interval(C++) 65
15. Insert Interval(Python) 66
16. Valid Number 67
16. Valid Number(C++) 68
16. Valid Number(Python) 70
17. Text Justification 72
17. Text Justification(C++) 74
17. Text Justification(Python) 75
18. Edit Distance 76
18. Edit Distance(Python) 77
19. Minimum Window Substring 78
19. Minimum Window Substring(C++) 79
19. Minimum Window Substring(Python) 81
20. Largest Rectangle in Histogram 83
20. Largest Rectangle in Histogram(C++) 85
20. Largest Rectangle in Histogram(Python) 86
21. Maximal Rectangle 87
21. Maximal Rectangle(C++) 89
21. Maximal Rectangle(Python) 91
22. Scramble String 93
22. Scramble String(Python) 94
23. Interleaving String 95
23. Interleaving String(Python) 96
24. Recover Binary Search Tree 98
24. Recover Binary Search Tree(C++) 100
24. Recover Binary Search Tree(Python) 101
25. Distinct Subsequences 103
25. Distinct Subsequences(Python) 104
26. Populating Next Right Pointers in Each Node II 105
26. Populating Next Right Pointers in Each Node II(Python) 106
27. Best Time to Buy and Sell Stock III 107
27. Best Time to Buy and Sell Stock III(Python) 108
28. Binary Tree Maximum Path Sum 110
28. Binary Tree Maximum Path Sum(C++) 111
28. Binary Tree Maximum Path Sum(Python) 113
29. Word Ladder II 114
29. Word Ladder II(C++) 115
29. Word Ladder II(Python) 117
30. Longest Consecutive Sequence 119
30. Longest Consecutive Sequence(C++) 120
30. Longest Consecutive Sequence(Python) 121
31. Palindrome Partitioning II 122
31. Palindrome Partitioning II(Python) 123
32. Candy 124
32. Candy(C++) 125
32. Candy(Python) 126
33. Word Break II 127
33. Word Break II(C++) 128
33. Word Break II(Python) 129
34. Binary Tree Postorder Traversal 130
34. Binary Tree Postorder Traversal(C++) 132
34. Binary Tree Postorder Traversal(Python) 134
35. LRU Cache 136
35. LRU Cache(C++) 137
35. LRU Cache(Python) 139
36. Max Points on a Line 141
36. Max Points on a Line(C++) 142
36. Max Points on a Line(Python) 143
37. Find Minimum in Rotated Sorted Array II 144
37. Find Minimum in Rotated Sorted Array II(C++) 145
37. Find Minimum in Rotated Sorted Array II(Python) 146
38. Read N Characters Given Read4 II - Call multiple times 147
38. Read N Characters Given Read4 II - Call multiple times(C++) 149
38. Read N Characters Given Read4 II - Call multiple times(Python) 150
39. Longest Substring with At Most Two Distinct Characters 151
39. Longest Substring with At Most Two Distinct Characters(C++) 152
39. Longest Substring with At Most Two Distinct Characters(Python) 153
40. Maximum Gap 154
40. Maximum Gap(C++) 155
40. Maximum Gap(Python) 157
41. Dungeon Game 159
41. Dungeon Game(Python) 160
42. Department Top Three Salaries 162
42. Department Top Three Salaries(Shell) 164
43. Best Time to Buy and Sell Stock IV 165
43. Best Time to Buy and Sell Stock IV(C++) 166
43. Best Time to Buy and Sell Stock IV(Python) 167
44. Word Search II 169
44. Word Search II(C++) 171
44. Word Search II(Python) 173
45. Shortest Palindrome 174
45. Shortest Palindrome(C++) 175
45. Shortest Palindrome(Python) 178
46. The Skyline Problem 180
46. The Skyline Problem(C++) 182
46. The Skyline Problem(Python) 185
47. Basic Calculator 187
47. Basic Calculator(C++) 188
47. Basic Calculator(Python) 191
48. Number of Digit One 193
48. Number of Digit One(C++) 194
48. Number of Digit One(Python) 195
49. Sliding Window Maximum 196
49. Sliding Window Maximum(C++) 197
49. Sliding Window Maximum(Python) 198
50. Strobogrammatic Number III 199
50. Strobogrammatic Number III(C++) 200
50. Strobogrammatic Number III(Python) 202
51. Trips and Users 204
51. Trips and Users(Shell) 206
52. Paint House II 207
52. Paint House II(C++) 208
52. Paint House II(Python) 209
53. Alien Dictionary 210
53. Alien Dictionary(C++) 211
53. Alien Dictionary(Python) 215
54. Closest Binary Search Tree Value II 217
54. Closest Binary Search Tree Value II(C++) 218
54. Closest Binary Search Tree Value II(Python) 220
55. Integer to English Words 222
55. Integer to English Words(C++) 223
55. Integer to English Words(Python) 225
56. Expression Add Operators 226
56. Expression Add Operators(C++) 227
56. Expression Add Operators(Python) 229
57. Find the Duplicate Number 230
57. Find the Duplicate Number(C++) 231
57. Find the Duplicate Number(Python) 233
58. Word Pattern II 235
58. Word Pattern II(C++) 236
58. Word Pattern II(Python) 237
59. Find Median from Data Stream 238
59. Find Median from Data Stream(C++) 239
59. Find Median from Data Stream(Python) 241
60. Best Meeting Point 242
60. Best Meeting Point(C++) 243
60. Best Meeting Point(Python) 244
61. Serialize and Deserialize Binary Tree 245
61. Serialize and Deserialize Binary Tree(C++) 247
61. Serialize and Deserialize Binary Tree(Python) 249
62. Remove Invalid Parentheses 251
62. Remove Invalid Parentheses(C++) 252
62. Remove Invalid Parentheses(Python) 256
63. Smallest Rectangle Enclosing Black Pixels 258
63. Smallest Rectangle Enclosing Black Pixels(C++) 259
63. Smallest Rectangle Enclosing Black Pixels(Python) 261
64. Number of Islands II 262
64. Number of Islands II(C++) 264
64. Number of Islands II(Python) 266
65. Range Sum Query 2D - Mutable 267
65. Range Sum Query 2D - Mutable(C++) 268
65. Range Sum Query 2D - Mutable(Python) 271
66. Burst Balloons 273
66. Burst Balloons(C++) 274
66. Burst Balloons(Python) 275
67. Count of Smaller Numbers After Self 276
67. Count of Smaller Numbers After Self(C++) 277
67. Count of Smaller Numbers After Self(Python) 280
68. Remove Duplicate Letters 283
68. Remove Duplicate Letters(C++) 284
68. Remove Duplicate Letters(Python) 285
69. Shortest Distance from All Buildings 286
69. Shortest Distance from All Buildings(C++) 288
69. Shortest Distance from All Buildings(Python) 289
70. Create Maximum Number 290
70. Create Maximum Number(C++) 291
70. Create Maximum Number(Python) 293
71. Count of Range Sum 294
71. Count of Range Sum(C++) 295
71. Count of Range Sum(Python) 297
72. Longest Increasing Path in a Matrix 299
72. Longest Increasing Path in a Matrix(C++) 301
72. Longest Increasing Path in a Matrix(Python) 303
73. Patching Array 305
73. Patching Array(C++) 306
73. Patching Array(Python) 307
74. Self Crossing 308
74. Self Crossing(C++) 310
74. Self Crossing(Python) 311
75. Palindrome Pairs 312
75. Palindrome Pairs(C++) 313
75. Palindrome Pairs(Python) 316
76. Longest Substring with At Most K Distinct Characters 318
76. Longest Substring with At Most K Distinct Characters(C++) 319
76. Longest Substring with At Most K Distinct Characters(Python) 320
77. Data Stream as Disjoint Intervals 321
77. Data Stream as Disjoint Intervals(C++) 322
77. Data Stream as Disjoint Intervals(Python) 325
78. Russian Doll Envelopes 326
78. Russian Doll Envelopes(C++) 327
78. Russian Doll Envelopes(Python) 328
79. Rearrange String k Distance Apart 329
79. Rearrange String k Distance Apart(C++) 330
79. Rearrange String k Distance Apart(Python) 332
80. Max Sum of Rectangle No Larger Than K 334
80. Max Sum of Rectangle No Larger Than K(C++) 335
80. Max Sum of Rectangle No Larger Than K(Python) 336
81. Insert Delete GetRandom O(1) 338
81. Insert Delete GetRandom O(1)(C++) 339
81. Insert Delete GetRandom O(1)(Python) 340
82. Insert Delete GetRandom O(1) - Duplicates allowed 341
82. Insert Delete GetRandom O(1) - Duplicates allowed(C++) 342
82. Insert Delete GetRandom O(1) - Duplicates allowed(Python) 343
83. Perfect Rectangle 344
83. Perfect Rectangle(C++) 346
83. Perfect Rectangle(Python) 347
84. Frog Jump 348
84. Frog Jump(C++) 349
84. Frog Jump(Python) 350
85. Trapping Rain Water II 351
85. Trapping Rain Water II(C++) 353
85. Trapping Rain Water II(Python) 355
86. Split Array Largest Sum 356
86. Split Array Largest Sum(C++) 357
86. Split Array Largest Sum(Python) 358
87. Minimum Unique Word Abbreviation 359
87. Minimum Unique Word Abbreviation(C++) 360
87. Minimum Unique Word Abbreviation(Python) 362
88. Strong Password Checker 364
88. Strong Password Checker(C++) 365
88. Strong Password Checker(Python) 366
89. Word Squares 367
89. Word Squares(C++) 368
89. Word Squares(Python) 370
90. All O`one Data Structure 371
90. All O`one Data Structure(C++) 372
90. All O`one Data Structure(Python) 374
91. K-th Smallest in Lexicographical Order 376
91. K-th Smallest in Lexicographical Order(C++) 377
91. K-th Smallest in Lexicographical Order(Python) 379
92. Arithmetic Slices II - Subsequence 381
92. Arithmetic Slices II - Subsequence(C++) 382
92. Arithmetic Slices II - Subsequence(Python) 383
93. LFU Cache 384
93. LFU Cache(C++) 386
93. LFU Cache(Python) 388
94. Optimal Account Balancing 391
94. Optimal Account Balancing(C++) 392
94. Optimal Account Balancing(Python) 393
95. Count The Repetitions 394
95. Count The Repetitions(C++) 395
95. Count The Repetitions(Python) 396
96. Robot Room Cleaner 397
96. Robot Room Cleaner(C++) 399
96. Robot Room Cleaner(Python) 401
97. The Maze III 402
97. The Maze III(C++) 404
97. The Maze III(Python) 406
98. Freedom Trail 407
98. Freedom Trail(C++) 409
98. Freedom Trail(Python) 410
99. Super Washing Machines 411
99. Super Washing Machines(C++) 412
99. Super Washing Machines(Python) 413
100. Word Abbreviation 414
100. Word Abbreviation(C++) 415
100. Word Abbreviation(Python) 416
101. Remove Boxes 417
101. Remove Boxes(C++) 418
101. Remove Boxes(Python) 419
102. Student Attendance Record II 420
102. Student Attendance Record II(C++) 421
102. Student Attendance Record II(Python) 422
103. Find the Closest Palindrome 423
103. Find the Closest Palindrome(C++) 424
103. Find the Closest Palindrome(Python) 425
104. Maximum Vacation Days 426
104. Maximum Vacation Days(C++) 428
104. Maximum Vacation Days(Python) 429
105. Erect the Fence 430
105. Erect the Fence(C++) 432
105. Erect the Fence(Python) 433
106. Design In-Memory File System 434
106. Design In-Memory File System(C++) 436
106. Design In-Memory File System(Python) 438
107. Tag Validator 440
107. Tag Validator(C++) 442
107. Tag Validator(Python) 444
108. Non-negative Integers without Consecutive Ones 445
108. Non-negative Integers without Consecutive Ones(C++) 446
108. Non-negative Integers without Consecutive Ones(Python) 447
109. K Inverse Pairs Array 448
109. K Inverse Pairs Array(C++) 449
109. K Inverse Pairs Array(Python) 450
110. Course Schedule III 451
110. Course Schedule III(C++) 452
110. Course Schedule III(Python) 453
111. Design Excel Sum Formula 454
111. Design Excel Sum Formula(C++) 456
111. Design Excel Sum Formula(Python) 458
112. Smallest Range 460
112. Smallest Range(C++) 461
112. Smallest Range(Python) 462
113. Decode Ways II 463
113. Decode Ways II(C++) 465
113. Decode Ways II(Python) 466
114. Design Search Autocomplete System 467
114. Design Search Autocomplete System(C++) 468
114. Design Search Autocomplete System(Python) 470
115. Maximum Average Subarray II 472
115. Maximum Average Subarray II(C++) 473
115. Maximum Average Subarray II(Python) 475
116. Coin Path 476
116. Coin Path(C++) 477
116. Coin Path(Python) 478
117. Remove 9 479
117. Remove 9(C++) 480
117. Remove 9(Python) 481
118. Strange Printer 482
118. Strange Printer(C++) 483
118. Strange Printer(Python) 484
119. Kth Smallest Number in Multiplication Table 485
119. Kth Smallest Number in Multiplication Table(C++) 486
119. Kth Smallest Number in Multiplication Table(Python) 487
120. Cut Off Trees for Golf Event 488
120. Cut Off Trees for Golf Event(C++) 490
120. Cut Off Trees for Golf Event(Python) 493
121. 24 Game 495
121. 24 Game(C++) 496
121. 24 Game(Python) 499
122. K Empty Slots 501
122. K Empty Slots(C++) 502
122. K Empty Slots(Python) 503
123. Redundant Connection II 504
123. Redundant Connection II(C++) 506
123. Redundant Connection II(Python) 507
124. Maximum Sum of 3 Non-Overlapping Subarrays 508
124. Maximum Sum of 3 Non-Overlapping Subarrays(C++) 509
124. Maximum Sum of 3 Non-Overlapping Subarrays(Python) 510
125. Stickers to Spell Word 511
125. Stickers to Spell Word(C++) 512
125. Stickers to Spell Word(Python) 513
126. Falling Squares 514
126. Falling Squares(C++) 516
126. Falling Squares(Python) 520
127. Number of Distinct Islands II 524
127. Number of Distinct Islands II(C++) 526
127. Number of Distinct Islands II(Python) 528
128. Range Module 529
128. Range Module(C++) 530
128. Range Module(Python) 532
129. Find K-th Smallest Pair Distance 534
129. Find K-th Smallest Pair Distance(C++) 535
129. Find K-th Smallest Pair Distance(Python) 536
130. Number of Atoms 537
130. Number of Atoms(C++) 539
130. Number of Atoms(Python) 540
131. Minimum Window Subsequence 541
131. Minimum Window Subsequence(C++) 542
131. Minimum Window Subsequence(Python) 545
132. Count Different Palindromic Subsequences 546
132. Count Different Palindromic Subsequences(C++) 547
132. Count Different Palindromic Subsequences(Python) 549
133. My Calendar III 550
133. My Calendar III(C++) 551
133. My Calendar III(Python) 552
134. Parse Lisp Expression 554
134. Parse Lisp Expression(C++) 556
134. Parse Lisp Expression(Python) 557
135. Cherry Pickup 558
135. Cherry Pickup(C++) 559
135. Cherry Pickup(Python) 560
136. Prefix and Suffix Search 561
136. Prefix and Suffix Search(C++) 562
136. Prefix and Suffix Search(Python) 565
137. Contain Virus 567
137. Contain Virus(C++) 569
137. Contain Virus(Python) 571
138. Cracking the Safe 572
138. Cracking the Safe(C++) 573
138. Cracking the Safe(Python) 575
139. Set Intersection Size At Least Two 577
139. Set Intersection Size At Least Two(C++) 578
139. Set Intersection Size At Least Two(Python) 579
140. Employee Free Time 580
140. Employee Free Time(C++) 581
140. Employee Free Time(Python) 582
141. Special Binary String 583
141. Special Binary String(C++) 584
141. Special Binary String(Python) 585
142. Couples Holding Hands 586
142. Couples Holding Hands(C++) 587
142. Couples Holding Hands(Python) 588
143. Max Chunks To Make Sorted II 589
143. Max Chunks To Make Sorted II(C++) 590
143. Max Chunks To Make Sorted II(Python) 591
144. Basic Calculator IV 592
144. Basic Calculator IV(C++) 594
144. Basic Calculator IV(Python) 598
145. Basic Calculator III 601
145. Basic Calculator III(C++) 602
145. Basic Calculator III(Python) 604
146. Sliding Puzzle 606
146. Sliding Puzzle(C++) 608
146. Sliding Puzzle(Python) 611
147. Minimize Max Distance to Gas Station 613
147. Minimize Max Distance to Gas Station(C++) 614
147. Minimize Max Distance to Gas Station(Python) 615
148. Swim in Rising Water 616
148. Swim in Rising Water(C++) 618
148. Swim in Rising Water(Python) 619
149. Reaching Points 620
149. Reaching Points(C++) 621
149. Reaching Points(Python) 622
150. Transform to Chessboard 623
150. Transform to Chessboard(C++) 625
150. Transform to Chessboard(Python) 627
151. K-th Smallest Prime Fraction 628
151. K-th Smallest Prime Fraction(C++) 629
151. K-th Smallest Prime Fraction(Python) 630
152. Preimage Size of Factorial Zeroes Function 631
152. Preimage Size of Factorial Zeroes Function(C++) 632
152. Preimage Size of Factorial Zeroes Function(Python) 633
153. Smallest Rotation with Highest Score 634
153. Smallest Rotation with Highest Score(C++) 635
153. Smallest Rotation with Highest Score(Python) 636
154. Bricks Falling When Hit 637
154. Bricks Falling When Hit(C++) 639
154. Bricks Falling When Hit(Python) 641
155. Split Array With Same Average 644
155. Split Array With Same Average(C++) 645
155. Split Array With Same Average(Python) 646
156. Chalkboard XOR Game 647
156. Chalkboard XOR Game(Python) 648
157. Bus Routes 649
157. Bus Routes(C++) 650
157. Bus Routes(Python) 651
158. Race Car 652
158. Race Car(C++) 653
158. Race Car(Python) 654
159. Making A Large Island 655
159. Making A Large Island(C++) 656
159. Making A Large Island(Python) 657
160. Count Unique Characters of All Substrings of a Given String 658
160. Count Unique Characters of All Substrings of a Given String(C++) 659
160. Count Unique Characters of All Substrings of a Given String(Python) 660
161. Sum of Distances in Tree 661
161. Sum of Distances in Tree(C++) 663
161. Sum of Distances in Tree(Python) 664
162. Similar String Groups 665
162. Similar String Groups(C++) 666
162. Similar String Groups(Python) 669
163. Guess the Word 671
163. Guess the Word(C++) 672
163. Guess the Word(Python) 675
164. Shortest Path Visiting All Nodes 677
164. Shortest Path Visiting All Nodes(C++) 678
164. Shortest Path Visiting All Nodes(Python) 679
165. Rectangle Area II 680
165. Rectangle Area II(C++) 681
165. Rectangle Area II(Python) 683
166. K-Similar Strings 685
166. K-Similar Strings(C++) 686
166. K-Similar Strings(Python) 687
167. Minimum Cost to Hire K Workers 688
167. Minimum Cost to Hire K Workers(C++) 689
167. Minimum Cost to Hire K Workers(Python) 690
168. Shortest Subarray with Sum at Least K 691
168. Shortest Subarray with Sum at Least K(C++) 692
168. Shortest Subarray with Sum at Least K(Python) 693
169. Random Pick with Blacklist 694
169. Random Pick with Blacklist(C++) 696
169. Random Pick with Blacklist(Python) 698
170. Shortest Path to Get All Keys 700
170. Shortest Path to Get All Keys(C++) 702
170. Shortest Path to Get All Keys(Python) 704
171. Minimum Number of Refueling Stops 706
171. Minimum Number of Refueling Stops(C++) 707
171. Minimum Number of Refueling Stops(Python) 708
172. Nth Magical Number 709
172. Nth Magical Number(C++) 710
172. Nth Magical Number(Python) 711
173. Profitable Schemes 712
173. Profitable Schemes(C++) 713
173. Profitable Schemes(Python) 714
174. Reachable Nodes In Subdivided Graph 715
174. Reachable Nodes In Subdivided Graph(C++) 717
174. Reachable Nodes In Subdivided Graph(Python) 718
175. Super Egg Drop 719
175. Super Egg Drop(C++) 720
175. Super Egg Drop(Python) 721
176. Sum of Subsequence Widths 722
176. Sum of Subsequence Widths(C++) 723
176. Sum of Subsequence Widths(Python) 724
177. Maximum Frequency Stack 725
177. Maximum Frequency Stack(C++) 726
177. Maximum Frequency Stack(Python) 727
178. Orderly Queue 728
178. Orderly Queue(C++) 729
178. Orderly Queue(Python) 730
179. Numbers At Most N Given Digit Set 731
179. Numbers At Most N Given Digit Set(C++) 732
179. Numbers At Most N Given Digit Set(Python) 733
180. Valid Permutations for DI Sequence 734
180. Valid Permutations for DI Sequence(C++) 735
180. Valid Permutations for DI Sequence(Python) 736
181. Super Palindromes 737
181. Super Palindromes(C++) 738
181. Super Palindromes(Python) 739
182. Cat and Mouse 740
182. Cat and Mouse(C++) 742
182. Cat and Mouse(Python) 745
183. Number of Music Playlists 747
183. Number of Music Playlists(C++) 748
183. Number of Music Playlists(Python) 749
184. Minimize Malware Spread 750
184. Minimize Malware Spread(C++) 751
184. Minimize Malware Spread(Python) 752
185. Three Equal Parts 753
185. Three Equal Parts(C++) 754
185. Three Equal Parts(Python) 755
186. Minimize Malware Spread II 756
186. Minimize Malware Spread II(C++) 757
186. Minimize Malware Spread II(Python) 759
187. Stamping The Sequence 761
187. Stamping The Sequence(C++) 762
187. Stamping The Sequence(Python) 764
188. Distinct Subsequences II 765
188. Distinct Subsequences II(C++) 766
188. Distinct Subsequences II(Python) 767
189. Find the Shortest Superstring 768
189. Find the Shortest Superstring(C++) 769
189. Find the Shortest Superstring(Python) 771
190. Largest Component Size by Common Factor 772
190. Largest Component Size by Common Factor(C++) 774
190. Largest Component Size by Common Factor(Python) 776
191. Tallest Billboard 777
191. Tallest Billboard(C++) 778
191. Tallest Billboard(Python) 779
192. Delete Columns to Make Sorted III 780
192. Delete Columns to Make Sorted III(C++) 781
192. Delete Columns to Make Sorted III(Python) 782
193. Least Operators to Express Number 783
193. Least Operators to Express Number(C++) 784
193. Least Operators to Express Number(Python) 785
194. Binary Tree Cameras 786
194. Binary Tree Cameras(C++) 787
194. Binary Tree Cameras(Python) 788
195. Equal Rational Numbers 789
195. Equal Rational Numbers(C++) 790
195. Equal Rational Numbers(Python) 792
196. Odd Even Jump 793
196. Odd Even Jump(C++) 795
196. Odd Even Jump(Python) 797
197. Unique Paths III 798
197. Unique Paths III(C++) 800
197. Unique Paths III(Python) 802
198. Triples with Bitwise AND Equal To Zero 803
198. Triples with Bitwise AND Equal To Zero(C++) 804
198. Triples with Bitwise AND Equal To Zero(Python) 805
199. Subarrays with K Different Integers 806
199. Subarrays with K Different Integers(C++) 807
199. Subarrays with K Different Integers(Python) 809
200. Minimum Number of K Consecutive Bit Flips 811
200. Minimum Number of K Consecutive Bit Flips(C++) 812
200. Minimum Number of K Consecutive Bit Flips(Python) 813
201. Number of Squareful Arrays 814
201. Number of Squareful Arrays(C++) 815
201. Number of Squareful Arrays(Python) 816
202. Minimum Cost to Merge Stones 817
202. Minimum Cost to Merge Stones(C++) 818
202. Minimum Cost to Merge Stones(Python) 819
203. Grid Illumination 820
203. Grid Illumination(C++) 822
203. Grid Illumination(Python) 824
204. Numbers With Repeated Digits 825
204. Numbers With Repeated Digits(C++) 826
204. Numbers With Repeated Digits(Python) 827
205. Recover a Tree From Preorder Traversal 828
205. Recover a Tree From Preorder Traversal(C++) 830
205. Recover a Tree From Preorder Traversal(Python) 832
206. Stream of Characters 834
206. Stream of Characters(C++) 835
206. Stream of Characters(Python) 837
207. Escape a Large Maze 839
207. Escape a Large Maze(C++) 840
207. Escape a Large Maze(Python) 842
208. Longest Duplicate Substring 843
208. Longest Duplicate Substring(C++) 844
208. Longest Duplicate Substring(Python) 845
209. Number of Valid Subarrays 846
209. Number of Valid Subarrays(C++) 847
209. Number of Valid Subarrays(Python) 848
210. Digit Count in Range 849
210. Digit Count in Range(C++) 850
210. Digit Count in Range(Python) 851
211. Number of Submatrices That Sum to Target 852
211. Number of Submatrices That Sum to Target(C++) 853
211. Number of Submatrices That Sum to Target(Python) 854
212. Confusing Number II 855
212. Confusing Number II(C++) 856
212. Confusing Number II(Python) 859
213. Shortest Common Supersequence 861
213. Shortest Common Supersequence(C++) 862
213. Shortest Common Supersequence(Python) 863
214. Find in Mountain Array 864
214. Find in Mountain Array(C++) 865
214. Find in Mountain Array(Python) 866
215. Brace Expansion II 867
215. Brace Expansion II(C++) 868
215. Brace Expansion II(Python) 869
216. Game Play Analysis V 871
216. Game Play Analysis V(Shell) 872
217. Parsing A Boolean Expression 873
217. Parsing A Boolean Expression(C++) 874
217. Parsing A Boolean Expression(Python) 875
218. Building H2O 876
218. Building H2O(C++) 877
218. Building H2O(Python) 879
219. Divide Array Into Increasing Sequences 881
219. Divide Array Into Increasing Sequences(C++) 882
219. Divide Array Into Increasing Sequences(Python) 883
220. Smallest Sufficient Team 884
220. Smallest Sufficient Team(C++) 885
220. Smallest Sufficient Team(Python) 886
221. User Purchase Platform 887
221. User Purchase Platform(Shell) 888
222. Parallel Courses 889
222. Parallel Courses(C++) 891
222. Parallel Courses(Python) 892
223. Longest Chunked Palindrome Decomposition 893
223. Longest Chunked Palindrome Decomposition(C++) 894
223. Longest Chunked Palindrome Decomposition(Python) 895
224. String Transforms Into Another String 896
224. String Transforms Into Another String(C++) 897
224. String Transforms Into Another String(Python) 898
225. Online Majority Element In Subarray 899
225. Online Majority Element In Subarray(C++) 900
225. Online Majority Element In Subarray(Python) 905
226. Market Analysis II 909
226. Market Analysis II(Shell) 911
227. Last Substring in Lexicographical Order 912
227. Last Substring in Lexicographical Order(C++) 913
227. Last Substring in Lexicographical Order(Python) 915
228. Optimize Water Distribution in a Village 916
228. Optimize Water Distribution in a Village(C++) 917
228. Optimize Water Distribution in a Village(Python) 919
229. Dinner Plate Stacks 920
229. Dinner Plate Stacks(C++) 922
229. Dinner Plate Stacks(Python) 923
230. Number of Valid Words for Each Puzzle 924
230. Number of Valid Words for Each Puzzle(C++) 925
230. Number of Valid Words for Each Puzzle(Python) 927
231. Maximum Number of Ones 929
231. Maximum Number of Ones(C++) 930
231. Maximum Number of Ones(Python) 931
232. Make Array Strictly Increasing 932
232. Make Array Strictly Increasing(C++) 933
232. Make Array Strictly Increasing(Python) 934
233. Critical Connections in a Network 935
233. Critical Connections in a Network(C++) 936
233. Critical Connections in a Network(Python) 937
234. Tournament Winners 938
234. Tournament Winners(Shell) 940
235. Minimum Time to Build Blocks 941
235. Minimum Time to Build Blocks(C++) 942
235. Minimum Time to Build Blocks(Python) 943
236. Sort Items by Groups Respecting Dependencies 944
236. Sort Items by Groups Respecting Dependencies(C++) 945
236. Sort Items by Groups Respecting Dependencies(Python) 947
237. Design Skiplist 949
237. Design Skiplist(C++) 951
237. Design Skiplist(Python) 955
238. Minimum Moves to Reach Target with Rotations 957
238. Minimum Moves to Reach Target with Rotations(C++) 959
238. Minimum Moves to Reach Target with Rotations(Python) 961
239. Valid Palindrome III 962
239. Valid Palindrome III(C++) 963
239. Valid Palindrome III(Python) 964
240. Count Vowels Permutation 965
240. Count Vowels Permutation(C++) 966
240. Count Vowels Permutation(Python) 968
241. Maximum Equal Frequency 969
241. Maximum Equal Frequency(C++) 970
241. Maximum Equal Frequency(Python) 971
242. Report Contiguous Dates 972
242. Report Contiguous Dates(Shell) 974
243. Divide Chocolate 975
243. Divide Chocolate(C++) 976
243. Divide Chocolate(Python) 977
244. Maximum Profit in Job Scheduling 978
244. Maximum Profit in Job Scheduling(C++) 980
244. Maximum Profit in Job Scheduling(Python) 981
245. Tiling a Rectangle with the Fewest Squares 982
245. Tiling a Rectangle with the Fewest Squares(C++) 983
245. Tiling a Rectangle with the Fewest Squares(Python) 985
246. Palindrome Removal 986
246. Palindrome Removal(C++) 987
246. Palindrome Removal(Python) 988
247. Check If It Is a Good Array 989
247. Check If It Is a Good Array(C++) 990
247. Check If It Is a Good Array(Python) 991
248. Maximum Score Words Formed by Letters 992
248. Maximum Score Words Formed by Letters(C++) 993
248. Maximum Score Words Formed by Letters(Python) 994
249. Handshakes That Don't Cross 995
249. Handshakes That Don't Cross(C++) 998
249. Handshakes That Don't Cross(Python) 999
250. Minimum Moves to Move a Box to Their Target Location 1000
250. Minimum Moves to Move a Box to Their Target Location(C++) 1002
250. Minimum Moves to Move a Box to Their Target Location(Python) 1004
251. Number of Ways to Stay in the Same Place After Some Steps 1007
251. Number of Ways to Stay in the Same Place After Some Steps(C++) 1008
251. Number of Ways to Stay in the Same Place After Some Steps(Python) 1009
252. Number of Ships in a Rectangle 1010
252. Number of Ships in a Rectangle(C++) 1011
252. Number of Ships in a Rectangle(Python) 1012
253. Palindrome Partitioning III 1013
253. Palindrome Partitioning III(C++) 1014
253. Palindrome Partitioning III(Python) 1015
254. Minimum Number of Flips to Convert Binary Matrix to Zero Matrix 1016
254. Minimum Number of Flips to Convert Binary Matrix to Zero Matrix(C++) 1017
254. Minimum Number of Flips to Convert Binary Matrix to Zero Matrix(Python) 1018
255. Minimum Falling Path Sum II 1019
255. Minimum Falling Path Sum II(C++) 1020
255. Minimum Falling Path Sum II(Python) 1021
256. Shortest Path in a Grid with Obstacles Elimination 1022
256. Shortest Path in a Grid with Obstacles Elimination(C++) 1023
256. Shortest Path in a Grid with Obstacles Elimination(Python) 1025
257. Maximum Candies You Can Get from Boxes 1026
257. Maximum Candies You Can Get from Boxes(C++) 1028
257. Maximum Candies You Can Get from Boxes(Python) 1029
258. Number of Paths with Max Score 1030
258. Number of Paths with Max Score(C++) 1031
258. Number of Paths with Max Score(Python) 1032
259. Verbal Arithmetic Puzzle 1033
259. Verbal Arithmetic Puzzle(C++) 1034
259. Verbal Arithmetic Puzzle(Python) 1036
260. Minimum Insertion Steps to Make a String Palindrome 1037
260. Minimum Insertion Steps to Make a String Palindrome(C++) 1038
260. Minimum Insertion Steps to Make a String Palindrome(Python) 1039
261. Distinct Echo Substrings 1040
261. Distinct Echo Substrings(C++) 1041
261. Distinct Echo Substrings(Python) 1043
262. Minimum Distance to Type a Word Using Two Fingers 1045
262. Minimum Distance to Type a Word Using Two Fingers(C++) 1047
262. Minimum Distance to Type a Word Using Two Fingers(Python) 1049
263. Minimum Number of Taps to Open to Water a Garden 1050
263. Minimum Number of Taps to Open to Water a Garden(C++) 1052
263. Minimum Number of Taps to Open to Water a Garden(Python) 1053
264. Reverse Subarray To Maximize Array Value 1054
264. Reverse Subarray To Maximize Array Value(C++) 1055
264. Reverse Subarray To Maximize Array Value(Python) 1056
265. Minimum Difficulty of a Job Schedule 1057
265. Minimum Difficulty of a Job Schedule(C++) 1059
265. Minimum Difficulty of a Job Schedule(Python) 1060
266. Jump Game V 1061
266. Jump Game V(C++) 1063
266. Jump Game V(Python) 1067
267. Jump Game IV 1070
267. Jump Game IV(C++) 1071
267. Jump Game IV(Python) 1072
268. Maximum Students Taking Exam 1073
268. Maximum Students Taking Exam(C++) 1075
268. Maximum Students Taking Exam(Python) 1080
269. Construct Target Array With Multiple Sums 1084
269. Construct Target Array With Multiple Sums(C++) 1085
269. Construct Target Array With Multiple Sums(Python) 1086
270. Count All Valid Pickup and Delivery Options 1087
270. Count All Valid Pickup and Delivery Options(C++) 1088
270. Count All Valid Pickup and Delivery Options(Python) 1089
271. Largest Multiple of Three 1090
271. Largest Multiple of Three(C++) 1091
271. Largest Multiple of Three(Python) 1093
272. Minimum Cost to Make at Least One Valid Path in a Grid 1094
272. Minimum Cost to Make at Least One Valid Path in a Grid(C++) 1097
272. Minimum Cost to Make at Least One Valid Path in a Grid(Python) 1099
273. Get the Second Most Recent Activity 1102
273. Get the Second Most Recent Activity(Shell) 1103
274. Maximum Sum BST in Binary Tree 1104
274. Maximum Sum BST in Binary Tree(C++) 1106
274. Maximum Sum BST in Binary Tree(Python) 1108
275. Frog Position After T Seconds 1110
275. Frog Position After T Seconds(C++) 1112
275. Frog Position After T Seconds(Python) 1115
276. Maximum Performance of a Team 1117
276. Maximum Performance of a Team(C++) 1118
276. Maximum Performance of a Team(Python) 1119
277. Total Sales Amount by Year 1120
277. Total Sales Amount by Year(Shell) 1122
278. Pizza With 3n Slices 1124
278. Pizza With 3n Slices(C++) 1126
278. Pizza With 3n Slices(Python) 1128
279. Longest Happy Prefix 1129
279. Longest Happy Prefix(C++) 1130
279. Longest Happy Prefix(Python) 1132
280. Find All Good Strings 1133
280. Find All Good Strings(C++) 1134
280. Find All Good Strings(Python) 1136
281. Reducing Dishes 1137
281. Reducing Dishes(C++) 1138
281. Reducing Dishes(Python) 1139
282. Stone Game III 1140
282. Stone Game III(C++) 1142
282. Stone Game III(Python) 1143
283. Number of Ways to Paint N × 3 Grid 1144
283. Number of Ways to Paint N × 3 Grid(C++) 1146
283. Number of Ways to Paint N × 3 Grid(Python) 1147
284. Find the Quiet Students in All Exams 1148
284. Find the Quiet Students in All Exams(Shell) 1150
285. Restore The Array 1151
285. Restore The Array(C++) 1152
285. Restore The Array(Python) 1153
286. Build Array Where You Can Find The Maximum Exactly K Comparisons 1154
286. Build Array Where You Can Find The Maximum Exactly K Comparisons(C++) 1156
286. Build Array Where You Can Find The Maximum Exactly K Comparisons(Python) 1157
287. Constrained Subset Sum 1158
287. Constrained Subset Sum(C++) 1159
287. Constrained Subset Sum(Python) 1160
288. Number of Ways to Wear Different Hats to Each Other 1161
288. Number of Ways to Wear Different Hats to Each Other(C++) 1162
288. Number of Ways to Wear Different Hats to Each Other(Python) 1163
289. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit 1164
289. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit(C++) 1165
289. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit(Python) 1167
290. Find the Kth Smallest Sum of a Matrix With Sorted Rows 1169
290. Find the Kth Smallest Sum of a Matrix With Sorted Rows(C++) 1170
290. Find the Kth Smallest Sum of a Matrix With Sorted Rows(Python) 1172
291. Number of Ways of Cutting a Pizza 1174
291. Number of Ways of Cutting a Pizza(C++) 1176
291. Number of Ways of Cutting a Pizza(Python) 1177
292. Form Largest Integer With Digits That Add up to Target 1178
292. Form Largest Integer With Digits That Add up to Target(C++) 1180
292. Form Largest Integer With Digits That Add up to Target(Python) 1182
293. Maximum Number of Darts Inside of a Circular Dartboard 1185
293. Maximum Number of Darts Inside of a Circular Dartboard(C++) 1187
293. Maximum Number of Darts Inside of a Circular Dartboard(Python) 1188
294. Max Dot Product of Two Subsequences 1189
294. Max Dot Product of Two Subsequences(C++) 1190
294. Max Dot Product of Two Subsequences(Python) 1191
295. Cherry Pickup II 1192
295. Cherry Pickup II(C++) 1194
295. Cherry Pickup II(Python) 1196
296. Probability of a Two Boxes Having The Same Number of Distinct Balls 1197
296. Probability of a Two Boxes Having The Same Number of Distinct Balls(C++) 1199
296. Probability of a Two Boxes Having The Same Number of Distinct Balls(Python) 1200
297. Paint House III 1201
297. Paint House III(C++) 1203
297. Paint House III(Python) 1205
298. Allocate Mailboxes 1206
298. Allocate Mailboxes(C++) 1208
298. Allocate Mailboxes(Python) 1209
299. Sales by Day of the Week 1210
299. Sales by Day of the Week(Shell) 1212
300. Kth Ancestor of a Tree Node 1213
300. Kth Ancestor of a Tree Node(C++) 1214
300. Kth Ancestor of a Tree Node(Python) 1215
301. Find Critical and Pseudo-Critical Edges in Minimum Spanning Tree 1216
301. Find Critical and Pseudo-Critical Edges in Minimum Spanning Tree(C++) 1218
301. Find Critical and Pseudo-Critical Edges in Minimum Spanning Tree(Python) 1220
302. Parallel Courses II 1221
302. Parallel Courses II(C++) 1223
302. Parallel Courses II(Python) 1226
303. Max Value of Equation 1227
303. Max Value of Equation(C++) 1228
303. Max Value of Equation(Python) 1229
304. Minimum Possible Integer After at Most K Adjacent Swaps On Digits 1230
304. Minimum Possible Integer After at Most K Adjacent Swaps On Digits(C++) 1231
304. Minimum Possible Integer After at Most K Adjacent Swaps On Digits(Python) 1232
305. Stone Game IV 1233
305. Stone Game IV(C++) 1234
305. Stone Game IV(Python) 1235
306. Best Position for a Service Centre 1236
306. Best Position for a Service Centre(C++) 1238
306. Best Position for a Service Centre(Python) 1240
307. Move Sub-Tree of N-Ary Tree 1242
307. Move Sub-Tree of N-Ary Tree(C++) 1245
307. Move Sub-Tree of N-Ary Tree(Python) 1249
308. Find a Value of a Mysterious Function Closest to Target 1252
308. Find a Value of a Mysterious Function Closest to Target(C++) 1254
308. Find a Value of a Mysterious Function Closest to Target(Python) 1256
309. Minimum Number of Increments on Subarrays to Form a Target Array 1258
309. Minimum Number of Increments on Subarrays to Form a Target Array(C++) 1259
309. Minimum Number of Increments on Subarrays to Form a Target Array(Python) 1260
310. String Compression II 1261
310. String Compression II(C++) 1262
310. String Compression II(Python) 1263
311. Get the Maximum Score 1264
311. Get the Maximum Score(C++) 1266
311. Get the Maximum Score(Python) 1267
312. Find Longest Awesome Substring 1268
312. Find Longest Awesome Substring(C++) 1269
312. Find Longest Awesome Substring(Python) 1270
313. Minimum Cost to Cut a Stick 1271
313. Minimum Cost to Cut a Stick(C++) 1273
313. Minimum Cost to Cut a Stick(Python) 1274
314. The Most Similar Path in a Graph 1275
314. The Most Similar Path in a Graph(C++) 1278
314. The Most Similar Path in a Graph(Python) 1279
315. Minimum Number of Days to Eat N Oranges 1280
315. Minimum Number of Days to Eat N Oranges(C++) 1281
315. Minimum Number of Days to Eat N Oranges(Python) 1282
316. Detect Cycles in 2D Grid 1283
316. Detect Cycles in 2D Grid(C++) 1285
316. Detect Cycles in 2D Grid(Python) 1287
317. Stone Game V 1289
317. Stone Game V(C++) 1290
317. Stone Game V(Python) 1292
318. Number of Ways to Reorder Array to Get Same BST 1294
318. Number of Ways to Reorder Array to Get Same BST(C++) 1296
318. Number of Ways to Reorder Array to Get Same BST(Python) 1297
319. Count All Possible Routes 1298
319. Count All Possible Routes(C++) 1300
319. Count All Possible Routes(Python) 1302
320. Remove Max Number of Edges to Keep Graph Fully Traversable 1304
320. Remove Max Number of Edges to Keep Graph Fully Traversable(C++) 1306
320. Remove Max Number of Edges to Keep Graph Fully Traversable(Python) 1308
321. Check If String Is Transformable With Substring Sort Operations 1309
321. Check If String Is Transformable With Substring Sort Operations(C++) 1310
321. Check If String Is Transformable With Substring Sort Operations(Python) 1311
322. Strange Printer II 1312
322. Strange Printer II(C++) 1314
322. Strange Printer II(Python) 1317
323. Minimum Cost to Connect Two Groups of Points 1319
323. Minimum Cost to Connect Two Groups of Points(C++) 1321
323. Minimum Cost to Connect Two Groups of Points(Python) 1322
324. Maximum Number of Achievable Transfer Requests 1323
324. Maximum Number of Achievable Transfer Requests(C++) 1325
324. Maximum Number of Achievable Transfer Requests(Python) 1327
325. Find Servers That Handled Most Number of Requests 1328
325. Find Servers That Handled Most Number of Requests(C++) 1330
325. Find Servers That Handled Most Number of Requests(Python) 1332
326. Maximum Number of Visible Points 1334
326. Maximum Number of Visible Points(C++) 1336
326. Maximum Number of Visible Points(Python) 1337
327. Minimum One Bit Operations to Make Integers Zero 1338
327. Minimum One Bit Operations to Make Integers Zero(C++) 1339
327. Minimum One Bit Operations to Make Integers Zero(Python) 1341
328. Count Subtrees With Max Distance Between Cities 1343
328. Count Subtrees With Max Distance Between Cities(C++) 1345
328. Count Subtrees With Max Distance Between Cities(Python) 1347
329. Fancy Sequence 1349
329. Fancy Sequence(C++) 1350
329. Fancy Sequence(Python) 1353
330. Graph Connectivity With Threshold 1355
330. Graph Connectivity With Threshold(C++) 1357
330. Graph Connectivity With Threshold(Python) 1359
331. Rank Transform of a Matrix 1360
331. Rank Transform of a Matrix(C++) 1362
331. Rank Transform of a Matrix(Python) 1364
***************************

1. Median of Two Sorted Arrays


***************************

Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays.

The overall run time complexity should be O(log (m+n)) .

Example 1:

Input: nums1 = [1,3], nums2 = [2]


Output: 2.00000
Explanation: merged array = [1,2,3] and median is 2.

Example 2:

Input: nums1 = [1,2], nums2 = [3,4]


Output: 2.50000
Explanation: merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.

Example 3:

Input: nums1 = [0,0], nums2 = [0,0]


Output: 0.00000

Example 4:

Input: nums1 = [], nums2 = [1]


Output: 1.00000

Example 5:

Input: nums1 = [2], nums2 = []


Output: 2.00000

Constraints:

nums1.length == m
nums2.length == n
0 <= m <= 1000
0 <= n <= 1000
1 <= m + n <= 2000
-106 <= nums1[i], nums2[i] <= 10 6
***************************