├── .DS_Store ├── .github ├── ISSUE_TEMPLATE │ ├── bug_report.md │ └── feature_request.md ├── config.yml ├── pull_request_template.md └── workflows │ └── greetings.yml ├── .vscode └── settings.json ├── 1. Two_Sum ├── README.md ├── solution.cpp ├── solution.java ├── solution.js └── solution.py ├── 100. Same Tree ├── README.md └── solution.cpp ├── 1003. Check If Word Is Valid After Substitutions ├── README.md └── solution.cpp ├── 1008. Construct Binary Search Tree from Preorder Traversal ├── README.md └── solution.cpp ├── 1009. Complement of Base 10 Integer ├── README.md └── solution.cpp ├── 101. Symmetric Tree ├── README.md └── solution.cpp ├── 1011. Capacity To Ship Packages Within D Days ├── README.md ├── solution.cpp └── solution.js ├── 102. Binary Tree Level Order Traversal ├── README.md └── solution.cpp ├── 1020. Number of Enclaves ├── README.md └── solution.cpp ├── 1026. Maximum Difference Between Node and Ancestor ├── README.md └── solution.cpp ├── 1029. Two City Scheduling ├── README.md └── solution.cpp ├── 103. Binary Tree Zigzag Level Order Traversal ├── README.md └── solution.cpp ├── 1035. Uncrossed Lines ├── README.md └── solution.cpp ├── 1038. Binary Search Tree to Greater Sum Tree ├── README.md └── solution.cpp ├── 1039. Minimum Score Triangulation of Polygon ├── README.md └── solution.cpp ├── 104. Maximum Depth of Binary Tree ├── README.md └── solution.cpp ├── 1046. Last Stone Weight ├── README.md └── solution.cpp ├── 1047. Remove All Adjacent Duplicates In String ├── README.md └── solution.cpp ├── 105. Construct Binary Tree from Preorder and Inorder Traversal ├── README.md └── solution.cpp ├── 106. Construct Binary Tree from Inorder and Postorder Traversal ├── README.md └── solution.cpp ├── 1061. Lexicographically Smallest Equivalent String ├── README.md └── solution.cpp ├── 107. Binary Tree Level Order Traversal II ├── README.md └── solution.cpp ├── 1071. Greatest Common Divisor of Strings ├── README.md └── solution.cpp ├── 108. Convert Sorted Array to Binary Search Tree ├── README.md └── solution.cpp ├── 109. Convert Sorted List to Binary Search Tree ├── README.md └── solution.cpp ├── 11. Container With Most Water ├── README.md └── solution.cpp ├── 110. Balanced Binary Tree ├── README.md └── solution.cpp ├── 111. Minimum Depth of Binary Tree ├── README.md └── solution.cpp ├── 112. Path Sum ├── README.md └── solution.cpp ├── 1129. Shortest Path with Alternating Colors ├── README.md └── solution.cpp ├── 114. Flatten Binary Tree to Linked List ├── README.md └── solution.cpp ├── 1143. Longest Common Subsequence ├── README.md ├── brute-force.cpp └── solution.cpp ├── 1155. Number of Dice Rolls With Target Sum ├── README.md └── solution.cpp ├── 1162. As Far from Land as Possible ├── README.md └── solution.cpp ├── 120. Triangle ├── README.md └── solution.cpp ├── 1207. Unique Number of Occurrences ├── README.md └── solution.cpp ├── 121. Best Time to Buy and Sell Stock ├── README.md ├── solution.cpp └── solution2.cpp ├── 124. Binary Tree Maximum Path Sum ├── README.md └── solution.cpp ├── 1254. Number of Closed Islands ├── README.md └── solution.cpp ├── 1281. Subtract the Product and Sum of Digits of an Integer ├── README.md └── solution.cpp ├── 129. Sum Root to Leaf Numbers ├── README.md └── solution.cpp ├── 1290. Convert Binary Number in a Linked List to Integer ├── README.md └── solution.cpp ├── 13. Roman to Integer ├── README.md └── solution.cpp ├── 130. Surrounded Regions ├── README.md └── solution.cpp ├── 131. Palindrome Partitioning ├── README.md └── solution.cpp ├── 1312. Minimum Insertion Steps to Make a String Palindrome ├── README.md └── solution.cpp ├── 1319. Number of Operations to Make Network Connected ├── README.md └── solution.cpp ├── 1325. Delete Leaves With a Given Value ├── README.md └── solution.cpp ├── 133. Clone Graph ├── README.md └── solution.cpp ├── 1334. Find the City With the Smallest Number of Neighbors at a Threshold Distance ├── README.md └── solution.cpp ├── 1339. Maximum Product of Splitted Binary Tree ├── README.md └── solution.cpp ├── 134. Gas Station ├── README.md └── solution.cpp ├── 1342. Number of Steps to Reduce a Number to Zero ├── README.md └── solution.cpp ├── 1345. Jump Game IV ├── README.md └── solution.cpp ├── 136. Single Number ├── README.md └── solution.cpp ├── 137. Single Number II ├── README.md └── solution.cpp ├── 1373. Maximum Sum BST in Binary Tree ├── README.md └── solution.cpp ├── 138. Copy List with Random Pointer ├── README.md └── solution.cpp ├── 1382. Balance a Binary Search Tree ├── README.md └── solution.cpp ├── 1388. Pizza With 3n Slices ├── README.md └── solution.cpp ├── 1390. Four Divisors ├── README.md └── solution.cpp ├── 14. Longest Common Prefix ├── README.md └── solution.cpp ├── 140. Word Break II ├── README.md └── solution.cpp ├── 1402. Reducing Dishes ├── README.md └── solution.cpp ├── 141. Linked List Cycle ├── README.md └── solution.cpp ├── 1416. Restore The Array ├── README.md └── solution.cpp ├── 142. Linked List Cycle II ├── README.md └── solution.cpp ├── 143. Reorder List ├── README.md └── solution.cpp ├── 1431. Kids With the Greatest Number of Candies ├── README.md └── solution.js ├── 144. Binary Tree Preorder Traversal ├── README.md └── solution.cpp ├── 1443. Minimum Time to Collect All Apples in a Tree ├── README.md └── solution.cpp ├── 1444. Number of Ways of Cutting a Pizza ├── README.md └── solution.cpp ├── 1448. Count Good Nodes in Binary Tree ├── README.md └── solution.cpp ├── 145. Binary Tree Postorder Traversal ├── README.md └── solution.cpp ├── 146. LRU Cache ├── README.md └── solution.cpp ├── 1466. Reorder Routes to Make All Paths Lead to the City Zero ├── README.md └── solution.cpp ├── 1470. Shuffle the Array ├── README.md └── solution.cpp ├── 1472. Design Browser History ├── README.md └── solution.cpp ├── 148. Sort List ├── README.md └── solution.cpp ├── 1487. Making File Names Unique ├── README.md └── solution.cpp ├── 149. Max Points on a Line ├── README.md └── solution.cpp ├── 1491. Average Salary Excluding the Minimum and Maximum Salary ├── README.md └── solution.cpp ├── 1492. The kth Factor of n ├── README.md └── solution.cpp ├── 1498. Number of Subsequences That Satisfy the Given Sum Condition ├── README.md └── solution.cpp ├── 15. 3Sum ├── README.md └── solution.cpp ├── 150. Evaluate Reverse Polish Notation ├── README.md └── solution.cpp ├── 151. Reverse Words in a String ├── README.md └── solution.cpp ├── 1512. Number of Good Pairs ├── README.md └── solution.cpp ├── 1518. Water Bottles ├── README.md └── solution.cpp ├── 1519. Number of Nodes in the Sub-Tree With the Same Label ├── README.md └── solution.cpp ├── 1523. Count Odd Numbers in an Interval Range ├── README.md └── solution.cpp ├── 153. Find Minimum in Rotated Sorted Array ├── README.md └── solution.cpp ├── 1539. Kth Missing Positive Number ├── README.md └── solution.cpp ├── 1544. Make The String Great ├── README.md └── solution.cpp ├── 1557. Minimum Number of Vertices to Reach All Nodes ├── README.md └── solution.cpp ├── 1572. Matrix Diagonal Sum ├── README.md └── solution.cpp ├── 1579. Remove Max Number of Edges to Keep Graph Fully Traversable ├── README.md └── solution.cpp ├── 160. Intersection of Two Linked Lists ├── README.md └── solution.cpp ├── 162. Find Peak Element ├── README.md └── solution.cpp ├── 1626. Best Team With No Conflicts ├── README.md └── solution.cpp ├── 1639. Number of Ways to Form a Target String Given a Dictionary ├── README.md └── solution.cpp ├── 1655. Distribute Repeating Integers ├── README.md └── solution.cpp ├── 1657. Determine if Two Strings Are Close ├── README.md └── solution.cpp ├── 1662. Check If Two String Arrays are Equivalent ├── README.md └── solution.cpp ├── 167. Two Sum II - Input Array Is Sorted ├── README.md └── solution.cpp ├── 1672. Richest Customer Wealth ├── README.md └── solution.cpp ├── 1691. Maximum Height by Stacking Cuboids ├── README.md └── solution.cpp ├── 1697. Checking Existence of Edge Length Limited Paths ├── README.md └── solution.js ├── 17. Letter Combinations of a Phone Number ├── README.md └── solution.cpp ├── 1704. Determine if String Halves Are Alike ├── README.md └── solution.cpp ├── 171. Excel Sheet Column Number ├── README.md └── solution.cpp ├── 172. Factorial Trailing Zeroes ├── README.md └── solution.cpp ├── 1721. Swapping Nodes in a Linked List ├── README.md └── solution.cpp ├── 1723. Find Minimum Time to Finish All Jobs ├── README.md ├── brute-force.cpp └── solution.cpp ├── 1752. Check if Array Is Sorted and Rotated ├── README.md └── solution.cpp ├── 1768. Merge Strings Alternately ├── README.md └── solution.cpp ├── 1791. Find Center of Star Graph ├── README.md └── solution.cpp ├── 18. 4Sum ├── README.md └── solution.cpp ├── 1822. Sign of the Product of an Array ├── README.md └── solution.cpp ├── 1823. Find the Winner of the Circular Game ├── README.md └── solution.cpp ├── 1824. Minimum Sideway Jumps ├── README.md └── solution.cpp ├── 1833. Maximum Ice Cream Bars ├── README.md └── solution.cpp ├── 1834. Single-Threaded CPU ├── README.md └── solution.cpp ├── 1857. Largest Color Value in a Directed Graph ├── README.md └── solution.cpp ├── 1863. Sum of All Subset XOR Totals ├── README.md ├── solution.cpp └── solution2.cpp ├── 189. Rotate Array ├── README.md └── solution.cpp ├── 1894. Find the Student that Will Replace the Chalk ├── README.md └── solution.cpp ├── 19. Remove Nth Node From End of List ├── README.md └── solution.cpp ├── 191. Number of 1 Bits ├── README.md └── solution.cpp ├── 1910. Remove All Occurrences of a Substring ├── README.md └── solution.cpp ├── 1962. Remove Stones to Minimize the Total ├── README.md └── solution.cpp ├── 1963. Minimum Number of Swaps to Make the String Balanced ├── README.md └── solution.cpp ├── 1964. Find the Longest Valid Obstacle Course at Each Position ├── README.md └── solution.cpp ├── 1971. Find if Path Exists in Graph ├── README.md └── solution.cpp ├── 198. House Robber ├── README.md ├── brute-force.cpp └── solution.cpp ├── 199. Binary Tree Right Side View ├── README.md └── solution.cpp ├── 2. Add Two Numbers ├── README.md ├── solution.cpp └── solution.js ├── 20. Valid Parentheses ├── README.md ├── solution.cpp └── solution.js ├── 200. Number of Islands ├── README.md └── solution.cpp ├── 202. Happy Number ├── README.md ├── solution.cpp └── solution2.cpp ├── 203. Remove Linked List Elements ├── README.md └── solution.cpp ├── 204. Count Primes ├── README.md └── solution.cpp ├── 206. Reverse Linked List ├── README.md ├── solution.cpp └── solution2.cpp ├── 207. Course Schedule ├── README.md └── solution.cpp ├── 208. Implement Trie (Prefix Tree) ├── README.md └── solution.cpp ├── 2095. Delete the Middle Node of a Linked List ├── README.md └── solution.cpp ├── 21. Merge Two Sorted Lists ├── README.md ├── solution.cpp └── solution2.cpp ├── 211. Design Add and Search Words Data Structure ├── README.md └── solution.cpp ├── 213. House Robber II ├── README.md ├── brute-force.cpp └── solution.cpp ├── 2130. Maximum Twin Sum of a Linked List ├── README.md └── solution.cpp ├── 2140. Solving Questions With Brainpower ├── README.md └── solution.cpp ├── 215. Kth Largest Element in an Array ├── README.md └── solution.cpp ├── 2169. Count Operations to Obtain Zero ├── README.md └── solution.cpp ├── 217. Contains Duplicate ├── README.md └── solution.cpp ├── 2181. Merge Nodes in Between Zeros ├── README.md └── solution.cpp ├── 2187. Minimum Time to Complete Trips ├── README.md └── solution.cpp ├── 22. Generate Parentheses ├── README.md └── solution.cpp ├── 221. Maximal Square ├── README.md └── solution.cpp ├── 2215. Find the Difference of Two Arrays ├── README.md └── solution.cpp ├── 222. Count Complete Tree Nodes ├── README.md └── solution.cpp ├── 2225. Find Players With Zero or One Losses ├── README.md └── solution.cpp ├── 2244. Minimum Rounds to Complete All Tasks ├── README.md └── solution.cpp ├── 2246. Longest Path With Different Adjacent Characters ├── README.md └── solution.cpp ├── 2256. Minimum Average Difference ├── README.md └── solution.cpp ├── 226. Invert Binary Tree ├── README.md ├── solution.cpp └── solution.js ├── 2265. Count Nodes Equal to Average of Subtree ├── README.md └── solution.cpp ├── 2279. Maximum Bags With Full Capacity of Rocks ├── README.md └── solution.cpp ├── 228. Summary Ranges ├── README.md └── solution.cpp ├── 23. Merge k Sorted Lists ├── README.md └── solution.cpp ├── 230. Kth Smallest Element in a BST ├── README.md └── solution.cpp ├── 2300. Successful Pairs of Spells and Potions ├── README.md └── solution.cpp ├── 2306. Naming a Company ├── README.md └── solution.cpp ├── 231. Power of Two ├── README.md ├── solution.cpp ├── solution2.cpp └── solution3.cpp ├── 2316. Count Unreachable Pairs of Nodes in an Undirected Graph ├── README.md └── solution.cpp ├── 232. Implement Queue using Stacks ├── README.md └── solution.cpp ├── 233. Number of Digit One ├── README.md └── solution.cpp ├── 2336. Smallest Number in Infinite Set ├── README.md └── solution.cpp ├── 234. Palindrome Linked List ├── README.md └── solution.cpp ├── 2348. Number of Zero-Filled Subarrays ├── README.md └── solution.cpp ├── 235. Lowest Common Ancestor of a Binary Search Tree ├── README.md ├── solution.cpp └── solution2.cpp ├── 2359. Find Closest Node to Given Two Nodes ├── README.md └── solution.cpp ├── 236. Lowest Common Ancestor of a Binary Tree ├── README.md └── solution.cpp ├── 2360. Longest Cycle in a Graph ├── README.md └── solution.cpp ├── 2367. Number of Arithmetic Triplets ├── README.md └── solution.cpp ├── 237. Delete Node in a Linked List ├── README.md └── solution.cpp ├── 238. Product of Array Except Self ├── README.md └── solution.cpp ├── 2385. Amount of Time for Binary Tree to Be Infected ├── README.md └── solution.cpp ├── 2389. Longest Subsequence With Limited Sum ├── README.md └── solution.cpp ├── 239. Sliding Window Maximum ├── README.md └── solution.cpp ├── 2390. Removing Stars From a String ├── README.md └── solution.cpp ├── 24. Swap Nodes in Pairs ├── README.md └── solution.cpp ├── 240. Search a 2D Matrix II ├── README.md └── solution.cpp ├── 2405. Optimal Partition of String ├── README.md └── solution.cpp ├── 242. Valid Anagram ├── README.md └── solution.cpp ├── 2421. Number of Good Paths ├── README.md └── solution.cpp ├── 2439. Minimize Maximum of Array ├── README.md └── solution.js ├── 2444. Count Subarrays With Fixed Bounds ├── README.md └── solution.js ├── 2466. Count Ways To Build Good Strings ├── README.md └── solution.cpp ├── 2469. Convert the Temperature ├── README.md └── solution.cpp ├── 2471. Minimum Number of Operations to Sort a Binary Tree by Level ├── README.md └── solution.cpp ├── 2477. Minimum Fuel Cost to Report to the Capital ├── README.md └── solution.cpp ├── 2481. Minimum Cuts to Divide a Circle ├── README.md └── solution.cpp ├── 2485. Find the Pivot Integer ├── README.md └── solution.cpp ├── 2486. Append Characters to String to Make Subsequence ├── README.md └── solution.cpp ├── 2487. Remove Nodes From Linked List ├── README.md └── solution.cpp ├── 2490. Circular Sentence ├── README.md └── solution.cpp ├── 2491. Divide Players Into Teams of Equal Skill ├── README.md └── solution.cpp ├── 2496. Maximum Value of a String in an Array ├── README.md └── solution.cpp ├── 25. Reverse Nodes in k-Group ├── README.md └── solution.cpp ├── 2500. Delete Greatest Value in Each Row ├── README.md └── solution.cpp ├── 2501. Longest Square Streak in an Array ├── README.md └── solution.cpp ├── 2502. Design Memory Allocator ├── README.md └── solution.cpp ├── 2506. Count Pairs Of Similar Strings ├── README.md └── solution.cpp ├── 2507. Smallest Value After Replacing With Sum of Prime Factors ├── README.md └── solution.cpp ├── 2515. Shortest Distance to Target String in a Circular Array ├── README.md └── solution.cpp ├── 2520. Count the Digits That Divide a Number ├── README.md └── solution.cpp ├── 2521. Distinct Prime Factors of Product of Array ├── README.md ├── brute-force.cpp └── solution.cpp ├── 2523. Closest Prime Numbers in Range ├── README.md └── solution.cpp ├── 2535. Difference Between Element Sum and Digit Sum of an Array ├── README.md └── solution.cpp ├── 2540. Minimum Common Value ├── README.md └── solution.cpp ├── 2544. Alternating Digit Sum ├── README.md └── solution.cpp ├── 2545. Sort the Students by Their Kth Score ├── README.md └── solution.cpp ├── 2549. Count Distinct Numbers on Board ├── README.md └── solution.cpp ├── 2551. Put Marbles in Bags ├── README.md └── solution.cpp ├── 2553. Separate the Digits in an Array ├── README.md └── solution.cpp ├── 2554. Maximum Number of Integers to Choose From a Range I ├── README.md └── solution.cpp ├── 2558. Take Gifts From the Richest Pile ├── README.md ├── solution.cpp └── solution.js ├── 2562. Find the Array Concatenation Value ├── README.md └── solution.cpp ├── 2563. Count the Number of Fair Pairs ├── README.md └── solution.cpp ├── 257. Binary Tree Paths ├── README.md ├── solution.cpp └── solution2.cpp ├── 2570. Merge Two 2D Arrays by Summing Values ├── README.md └── solution.cpp ├── 258. Add Digits ├── README.md ├── solution.cpp └── solution.js ├── 26. Remove Duplicates from Sorted Array ├── README.md └── solution.cpp ├── 260. Single Number III ├── README.md └── solution.cpp ├── 2620. Counter ├── README.md └── solution.js ├── 263. Ugly Number ├── README.md └── solution.cpp ├── 2665. Counter II ├── README.md └── solution.js ├── 2667. Create Hello World Function ├── README.md └── solution.js ├── 268. Missing Number ├── README.md ├── solution.cpp ├── solution2.cpp └── solution3.cpp ├── 2696. Minimum String Length After Removing Substrings ├── README.md └── solution.cpp ├── 2697. Lexicographically Smallest Palindrome ├── README.md └── solution.cpp ├── 27. Remove Element ├── README.md └── solution.cpp ├── 273. Integer to English Words ├── README.md └── solution.cpp ├── 278. First Bad Version ├── README.md └── solution.cpp ├── 279. Perfect Squares ├── README.md └── solution.cpp ├── 28. Find the Index of the First Occurrence in a String ├── README.md └── solution.cpp ├── 283. Move Zeroes ├── README.md └── solution.cpp ├── 290. Word Pattern ├── README.md ├── solution.cpp └── solution.java ├── 292. Nim Game ├── README.md └── solution.cpp ├── 3. Longest Substring Without Repeating Characters ├── README.md └── solution.cpp ├── 300. Longest Increasing Subsequence ├── README.md └── solution.cpp ├── 301. Remove Invalid Parentheses ├── README.md └── solution.cpp ├── 309. Best Time to Buy and Sell Stock with Cooldown ├── README.md ├── brute-force.cpp └── solution.cpp ├── 319. Bulb Switcher ├── README.md └── solution.cpp ├── 322. Coin Change ├── README.md ├── brute-force.cpp └── solution.cpp ├── 326. Power of Three ├── README.md └── solution.cpp ├── 328. Odd Even Linked List ├── README.md └── solution.cpp ├── 33. Search in Rotated Sorted Array ├── README.md └── solution.cpp ├── 337. House Robber III ├── README.md └── solution.cpp ├── 338. Counting Bits ├── README.md ├── solution.cpp └── solution.js ├── 34. Find First and Last Position of Element in Sorted Array ├── README.md ├── solution.cpp ├── solution.py └── solution2.cpp ├── 342. Power of Four ├── README.md └── solution.cpp ├── 344. Reverse String ├── README.md └── solution.cpp ├── 347. Top K Frequent Elements ├── README.md └── solution.cpp ├── 349. Intersection of Two Arrays ├── README.md └── solution.cpp ├── 35. Search Insert Position ├── README.md ├── solution.cpp └── solution.js ├── 350. Intersection of Two Arrays II ├── README.md └── solution.cpp ├── 352. Data Stream as Disjoint Intervals ├── README.md └── solution.cpp ├── 354. Russian Doll Envelopes ├── README.md └── solution.cpp ├── 367. Valid Perfect Square ├── README.md └── solution.cpp ├── 37. Sudoku Solver ├── README.md └── solution.cpp ├── 374. Guess Number Higher or Lower ├── README.md └── solution.cpp ├── 382. Linked List Random Node ├── README.md └── solution.cpp ├── 383. Ransom Note ├── README.md └── solution.cpp ├── 387. First Unique Character in a String ├── README.md └── solution.cpp ├── 390. Elimination Game ├── README.md └── solution.cpp ├── 392. Is Subsequence ├── README.md └── solution.cpp ├── 394. Decode String ├── README.md └── solution.cpp ├── 4. Median of Two Sorted Arrays ├── README.md └── solution.cpp ├── 404. Sum of Left Leaves ├── README.md └── solution.cpp ├── 41. First Missing Positive ├── README.md └── solution.cpp ├── 416. Partition Equal Subset Sum ├── README.md └── solution.cpp ├── 42. Trapping Rain Water ├── README.md └── solution.cpp ├── 427. Construct Quad Tree ├── README.md └── solution.cpp ├── 437. Path Sum III ├── README.md └── solution.cpp ├── 438. Find All Anagrams in a String ├── README.md └── solution.cpp ├── 441. Arranging Coins ├── README.md └── solution.cpp ├── 442. Find All Duplicates in an Array ├── README.md └── solution.cpp ├── 443. String Compression ├── README.md └── solution.cpp ├── 445. Add Two Numbers II ├── README.md └── solution.cpp ├── 45. Jump Game II ├── README.md ├── solution.cpp └── solution2.cpp ├── 450. Delete Node in a BST ├── README.md └── solution.cpp ├── 451. Sort Characters By Frequency ├── README.md └── solution.cpp ├── 452. Minimum Number of Arrows to Burst Balloons ├── README.md └── solution.cpp ├── 46. Permutations ├── README.md └── solution.cpp ├── 460. LFU Cache ├── README.md └── solution.cpp ├── 472. Concatenated Words ├── README.md └── solution.cpp ├── 48. Rotate Image ├── README.md └── solution.cpp ├── 486. Predict the Winner ├── README.md ├── solution.cpp └── solution2.cpp ├── 49. Group Anagrams ├── README.md └── solution.cpp ├── 491. Non-decreasing Subsequences ├── README.md └── solution.cpp ├── 494. Target Sum ├── README.md └── solution.cpp ├── 496. Next Greater Element I ├── README.md └── solution.cpp ├── 5. Longest Palindromic Substring ├── README.md └── solution.cpp ├── 50. Pow(x, n) ├── README.md └── solution.cpp ├── 502. IPO ├── README.md └── solution.cpp ├── 506. Relative Ranks ├── README.md └── solution.cpp ├── 508. Most Frequent Subtree Sum ├── README.md ├── solution.cpp └── solution.js ├── 509. Fibonacci Number ├── README.md ├── solution.cpp ├── solution2.cpp ├── solution3.cpp └── solution4.cpp ├── 51. N-Queens ├── README.md ├── solution.cpp └── solution2.cpp ├── 513. Find Bottom Left Tree Value ├── README.md └── solution.cpp ├── 515. Find Largest Value in Each Tree Row ├── README.md └── solution.cpp ├── 52. N-Queens II ├── README.md └── solution.cpp ├── 520. Detect Capital ├── README.md └── solution.cpp ├── 53. Maximum Subarray ├── README.md └── solution.cpp ├── 530. Minimum Absolute Difference in BST ├── README.md └── solution.cpp ├── 538. Convert BST to Greater Tree ├── README.md └── solution.cpp ├── 54. Spiral Matrix ├── README.md └── solution.cpp ├── 540. Single Element in a Sorted Array ├── README.md ├── solution.cpp ├── solution.js ├── solution2.cpp ├── solution2.js └── solution3.cpp ├── 542. 01 Matrix ├── README.md └── solution.cpp ├── 543. Diameter of Binary Tree ├── README.md └── solution.cpp ├── 547. Number of Provinces ├── README.md └── solution.cpp ├── 55. Jump Game ├── README.md ├── solution.cpp └── solution2.cpp ├── 557. Reverse Words in a String III ├── README.md └── solution.cpp ├── 56. Merge Intervals ├── README.md └── solution.cpp ├── 563. Binary Tree Tilt ├── README.md ├── solution.cpp └── solution.js ├── 567. Permutation in String ├── README.md └── solution.cpp ├── 57. Insert Interval ├── README.md └── solution.cpp ├── 58. Length of Last Word ├── README.md └── solution.cpp ├── 59. Spiral Matrix II ├── README.md └── solution.cpp ├── 593. Valid Square ├── README.md └── solution.cpp ├── 6. Zigzag Conversion ├── README.md └── solution.cpp ├── 61. Rotate List ├── README.md └── solution.cpp ├── 617. Merge Two Binary Trees ├── README.md └── solution.cpp ├── 62. Unique Paths ├── README.md └── solution.cpp ├── 622. Design Circular Queue ├── README.md └── solution.cpp ├── 63. Unique Paths II ├── README.md └── solution.cpp ├── 632. Smallest Range Covering Elements from K Lists ├── README.md └── solution.cpp ├── 633. Sum of Square Numbers ├── README.md └── solution.cpp ├── 637. Average of Levels in Binary Tree ├── README.md └── solution.cpp ├── 64. Minimum Path Sum ├── README.md └── solution.cpp ├── 649. Dota2 Senate ├── README.md └── solution.cpp ├── 652. Find Duplicate Subtrees ├── README.md └── solution.cpp ├── 653. Two Sum IV - Input is a BST ├── README.md └── solution.cpp ├── 654. Maximum Binary Tree ├── README.md └── solution.cpp ├── 66. Plus One ├── README.md └── solution.cpp ├── 662. Maximum Width of Binary Tree ├── README.md └── solution.cpp ├── 67. Add Binary ├── README.md └── solution.cpp ├── 670. Maximum Swap ├── README.md └── solution.cpp ├── 69. Sqrt(x) ├── README.md └── solution.cpp ├── 7. Reverse Integer ├── README.md └── solution.cpp ├── 70. Climbing Stairs ├── README.md └── solution.cpp ├── 700. Search in a Binary Search Tree ├── README.md └── solution.cpp ├── 701. Insert into a Binary Search Tree ├── README.md └── solution.cpp ├── 703. Kth Largest Element in a Stream ├── README.md └── solution.cpp ├── 704. Binary Search ├── README.md └── solution.cpp ├── 705. Design HashSet ├── README.md └── solution.cpp ├── 71. Simplify Path ├── README.md └── solution.js ├── 724. Find Pivot Index ├── README.md └── solution.cpp ├── 73. Set Matrix Zeroes ├── README.md └── solution.cpp ├── 733. Flood Fill ├── README.md └── solution.cpp ├── 739. Daily Temperatures ├── README.md └── solution.cpp ├── 74. Search a 2D Matrix ├── README.md └── solution.cpp ├── 740. Delete and Earn ├── README.md └── solution.cpp ├── 743. Network Delay Time ├── README.md └── solution.cpp ├── 744. Find Smallest Letter Greater Than Target ├── README.md └── solution.cpp ├── 746. Min Cost Climbing Stairs ├── README.md ├── brute-force.cpp ├── solution.cpp └── solution.js ├── 748. Shortest Completing Word ├── README.md └── solution.cpp ├── 75. Sort Colors ├── README.md ├── solution.cpp └── solution.js ├── 779. K-th Symbol in Grammar ├── README.md └── solution.cpp ├── 78. Subsets ├── README.md └── solution.cpp ├── 783. Minimum Distance Between BST Nodes ├── README.md └── solution.cpp ├── 785. Is Graph Bipartite? ├── README.md └── solution.cpp ├── 787. Cheapest Flights Within K Stops ├── README.md └── solution.cpp ├── 79. Word Search ├── README.md └── solution.cpp ├── 790. Domino and Tromino Tiling ├── README.md ├── brute-force.cpp └── solution.cpp ├── 797. All Paths From Source to Target ├── README.md └── solution.cpp ├── 80. Remove Duplicates from Sorted Array II ├── README.md └── solution.cpp ├── 82. Remove Duplicates from Sorted List II ├── README.md └── solution.cpp ├── 83. Remove Duplicates from Sorted List ├── README.md └── solution.cpp ├── 832. Flipping an Image ├── README.md └── solution.cpp ├── 834. Sum of Distances in Tree ├── README.md └── solution.cpp ├── 837. New 21 Game ├── README.md └── solution.cpp ├── 839. Similar String Groups ├── README.md └── solution.cpp ├── 84. Largest Rectangle in Histogram ├── README.md └── solution.cpp ├── 841. Keys and Rooms ├── README.md └── solution.cpp ├── 844. Backspace String Compare ├── README.md └── solution.cpp ├── 85. Maximal Rectangle ├── README.md └── solution.cpp ├── 852. Peak Index in a Mountain Array ├── README.md ├── solution.cpp └── solution2.cpp ├── 86. Partition List ├── README.md └── solution.cpp ├── 863. All Nodes Distance K in Binary Tree ├── README.md └── solution.cpp ├── 87. Scramble String ├── README.md └── solution.cpp ├── 872. Leaf-Similar Trees ├── README.md └── solution.cpp ├── 875. Koko Eating Bananas ├── README.md └── solution.js ├── 876. Middle of the Linked List ├── README.md └── solution.cpp ├── 879. Profitable Schemes ├── README.md └── solution.cpp ├── 88. Merge Sorted Array ├── README.md └── solution.cpp ├── 881. Boats to Save People ├── README.md └── solution.cpp ├── 886. Possible Bipartition ├── README.md └── solution.cpp ├── 9. Palindrome Number ├── README.md ├── solution.cpp └── solution.js ├── 904. Fruit Into Baskets ├── README.md └── solution.cpp ├── 909. Snakes and Ladders ├── README.md └── solution.cpp ├── 91. Decode Ways ├── README.md └── solution.cpp ├── 912. Sort an Array ├── README.md └── solution.cpp ├── 92. Reverse Linked List II ├── README.md └── solution.cpp ├── 921. Minimum Add to Make Parentheses Valid ├── README.md └── solution.cpp ├── 926. Flip String to Monotone Increasing ├── README.md └── solution.cpp ├── 93. Restore IP Addresses ├── README.md └── solution.cpp ├── 931. Minimum Falling Path Sum ├── README.md ├── brute-force.cpp └── solution.cpp ├── 934. Shortest Bridge ├── README.md └── solution.cpp ├── 938. Range Sum of BST ├── README.md └── solution.cpp ├── 94. Binary Tree Inorder Traversal ├── README.md ├── solution.cpp └── solution2.cpp ├── 944. Delete Columns to Make Sorted ├── README.md └── solution.cpp ├── 946. Validate Stack Sequences ├── README.md └── solution.cpp ├── 953. Verifying an Alien Dictionary ├── README.md └── solution.cpp ├── 958. Check Completeness of a Binary Tree ├── README.md └── solution.cpp ├── 96. Unique Binary Search Trees ├── README.md └── solution.cpp ├── 974. Subarray Sums Divisible by K ├── README.md └── solution.cpp ├── 977. Squares of a Sorted Array ├── README.md └── solution.cpp ├── 98. Validate Binary Search Tree ├── README.md └── solution.cpp ├── 980. Unique Paths III ├── README.md └── solution.cpp ├── 983. Minimum Cost For Tickets ├── README.md └── solution.cpp ├── 989. Add to Array-Form of Integer ├── README.md └── solution.cpp ├── 994. Rotting Oranges ├── README.md └── solution.cpp ├── 997. Find the Town Judge ├── README.md ├── solution.cpp └── solution2.cpp ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── LICENCE ├── README.md └── SECURITY.md /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SahilK-027/LeetCode/a9a7d4dc6b1005b6e58ed27d688740a00fc690fa/.DS_Store -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Is your feature request related to a problem? Please describe.** 11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 12 | 13 | **Describe the solution you'd like** 14 | A clear and concise description of what you want to happen. 15 | 16 | **Describe alternatives you've considered** 17 | A clear and concise description of any alternative solutions or features you've considered. 18 | 19 | **Additional context** 20 | Add any other context or screenshots about the feature request here. 21 | -------------------------------------------------------------------------------- /.github/config.yml: -------------------------------------------------------------------------------- 1 | # Configuration for welcome - https://github.com/behaviorbot/welcome 2 | 3 | newIssueWelcomeComment: > 4 | 💖 Thanks for opening your first issue here! 💖 Be sure to follow the issue template! 5 | 6 | newPRWelcomeComment: | 7 | 💖 Thanks for opening this pull request! 💖 Be sure to follow the PR template! 8 | Make sure you have read [contribution guidelines](https://github.com/SahilK-027/LeetCode/blob/main/CONTRIBUTING.md) 9 | 10 | firstPRMergeComment: > 11 | Congrats on merging your first pull request! We are proud of you! 🎉🎉🎉 12 | -------------------------------------------------------------------------------- /.github/workflows/greetings.yml: -------------------------------------------------------------------------------- 1 | name: Greetings 2 | 3 | on: [pull_request_target, issues] 4 | 5 | jobs: 6 | greeting: 7 | runs-on: ubuntu-latest 8 | permissions: 9 | issues: write 10 | pull-requests: write 11 | steps: 12 | - uses: actions/first-interaction@v1 13 | with: 14 | repo-token: ${{ secrets.GITHUB_TOKEN }} 15 | issue-message: "If your issue is relevant, we will address it." 16 | pr-message: "We will respond as soon as possible." 17 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "vector": "cpp", 4 | "iosfwd": "cpp", 5 | "unordered_set": "cpp", 6 | "string": "cpp", 7 | "filesystem": "cpp" 8 | } 9 | } -------------------------------------------------------------------------------- /1. Two_Sum/README.md: -------------------------------------------------------------------------------- 1 | # Two Sum 2 | ## Description 3 | 4 | Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. 5 | You may assume that each input would have exactly one solution, and you may not use the same element twice. 6 | You can return the answer in any order. 7 | 8 | 9 | 10 | Example 1: 11 | ``` 12 | Input: nums = [2,7,11,15], target = 9 13 | Output: [0,1] 14 | Explanation: Because nums[0] + nums[1] == 9, we return [0, 1]. 15 | ``` 16 | Example 2: 17 | ``` 18 | Input: nums = [3,2,4], target = 6 19 | Output: [1,2] 20 | ``` 21 | Example 3: 22 | ``` 23 | Input: nums = [3,3], target = 6 24 | Output: [0,1] 25 | ``` 26 | 27 | Follow up: Can you come up with an algorithm that is less than O(n2) time complexity? 28 | -------------------------------------------------------------------------------- /1. Two_Sum/solution.py: -------------------------------------------------------------------------------- 1 | """ 2 | 4739 ms 3 | 14.5 MB 4 | TC : O(N^2) 5 | SC : O(1) 6 | """ 7 | 8 | """ 9 | * @author : ShubhamAsbe 10 | * @brief : Iteration 11 | """ 12 | 13 | class Solution(object): 14 | def twoSum(self, nums, target): 15 | #for each number in the array we will check there exist another which will sum up to give the target 16 | for i in range(len(nums)): 17 | for j in range(len(nums)): 18 | if nums[i]+nums[j]==target and j!=i: 19 | return [i,j] 20 | -------------------------------------------------------------------------------- /100. Same Tree/README.md: -------------------------------------------------------------------------------- 1 | # Same Tree 2 | ## Description 3 | 4 | Given the roots of two binary trees p and q, write a function to check if they are the same or not. 5 | 6 | Two binary trees are considered the same if they are structurally identical, and the nodes have the same value. 7 | 8 | 9 | Example 1: 10 | 11 | ![alt](https://assets.leetcode.com/uploads/2020/12/20/ex1.jpg) 12 | ``` 13 | Input: p = [1,2,3], q = [1,2,3] 14 | Output: true 15 | ``` 16 | Example 2: 17 | 18 | ![alt](https://assets.leetcode.com/uploads/2020/12/20/ex3.jpg) 19 | ``` 20 | Input: p = [1,2,1], q = [1,1,2] 21 | Output: false 22 | ``` 23 | -------------------------------------------------------------------------------- /101. Symmetric Tree/README.md: -------------------------------------------------------------------------------- 1 | # Symmetric Tree 2 | ## Description 3 | 4 | Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center). 5 | 6 | 7 | 8 | Example 1: 9 | 10 | ![alt](https://assets.leetcode.com/uploads/2021/02/19/symtree1.jpg) 11 | ``` 12 | Input: root = [1,2,2,3,4,4,3] 13 | Output: true 14 | ``` 15 | Example 2: 16 | 17 | ![alt](https://assets.leetcode.com/uploads/2021/02/19/symtree2.jpg) 18 | ``` 19 | Input: root = [1,2,2,null,3,null,3] 20 | Output: false 21 | ``` 22 | Follow up: Could you solve it both recursively and iteratively? 23 | -------------------------------------------------------------------------------- /102. Binary Tree Level Order Traversal/README.md: -------------------------------------------------------------------------------- 1 | # Binary Tree Level Order Traversal 2 | ## Description 3 | 4 | Given the root of a binary tree, return the level order traversal of its nodes' values. (i.e., from left to right, level by level). 5 | 6 | Example 1: 7 | 8 | ![alt](https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg) 9 | ``` 10 | Input: root = [3,9,20,null,null,15,7] 11 | Output: [[3],[9,20],[15,7]] 12 | ``` 13 | Example 2: 14 | ``` 15 | Input: root = [1] 16 | Output: [[1]] 17 | ``` 18 | Example 3: 19 | ``` 20 | Input: root = [] 21 | Output: [] 22 | ``` 23 | -------------------------------------------------------------------------------- /103. Binary Tree Zigzag Level Order Traversal/README.md: -------------------------------------------------------------------------------- 1 | # Binary Tree Zigzag Level Order Traversal 2 | ## Description 3 | 4 | Given the root of a binary tree, return the zigzag level order traversal of its nodes' values. (i.e., from left to right, then right to left for the next level and alternate between). 5 | 6 | 7 | Example 1: 8 | 9 | ![alt](https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg) 10 | ``` 11 | Input: root = [3,9,20,null,null,15,7] 12 | Output: [[3],[20,9],[15,7]] 13 | ``` 14 | Example 2: 15 | ``` 16 | Input: root = [1] 17 | Output: [[1]] 18 | ``` 19 | Example 3: 20 | ``` 21 | Input: root = [] 22 | Output: [] 23 | ``` 24 | -------------------------------------------------------------------------------- /104. Maximum Depth of Binary Tree/README.md: -------------------------------------------------------------------------------- 1 | # Maximum Depth of Binary Tree 2 | ## Description 3 | 4 | Given the root of a binary tree, return its maximum depth. 5 | 6 | A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 7 | 8 | 9 | 10 | Example 1: 11 | ![alt](https://assets.leetcode.com/uploads/2020/11/26/tmp-tree.jpg) 12 | ``` 13 | Input: root = [3,9,20,null,null,15,7] 14 | Output: 3 15 | ``` 16 | Example 2: 17 | ``` 18 | Input: root = [1,null,2] 19 | Output: 2 20 | ``` 21 | -------------------------------------------------------------------------------- /1046. Last Stone Weight/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 5 ms 3 | 7.7 MB 4 | 5 | TC : O(N log N) 6 | SC : O(N) 7 | */ 8 | 9 | /** 10 | * @author : SahilK-027 11 | * @brief : Heap 12 | */ 13 | 14 | class Solution { 15 | public: 16 | int lastStoneWeight(vector& stones) { 17 | priority_queue pq; 18 | for(auto i: stones){ 19 | pq.push(i); 20 | } 21 | while(pq.size() > 1){ 22 | int first = pq.top(); // y 23 | pq.pop(); 24 | int second = pq.top(); // x 25 | pq.pop(); 26 | if(first != second){ 27 | pq.push(first - second); 28 | } 29 | } 30 | return pq.size() == 1 ? pq.top(): 0; 31 | } 32 | }; -------------------------------------------------------------------------------- /1047. Remove All Adjacent Duplicates In String/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 35 ms 3 | 11.6 MB 4 | 5 | TC : O(N) 6 | SC : O(N) 7 | */ 8 | 9 | /** 10 | * @author : SahilK-027 11 | * @brief : Stack 12 | */ 13 | 14 | class Solution { 15 | public: 16 | string removeDuplicates(string s) 17 | { 18 | stack st; 19 | string answer; 20 | for(int i=0;i& nums) { 19 | if(nums.size() == 1) return nums[0]; 20 | int ans = nums[0]; 21 | for(int i = 1; i < nums.size(); i++){ 22 | ans = ans ^ nums[i]; 23 | } 24 | return ans; 25 | } 26 | }; -------------------------------------------------------------------------------- /137. Single Number II/README.md: -------------------------------------------------------------------------------- 1 | # Single Number II 2 | ## Description 3 | 4 | Given an integer array nums where every element appears three times except for one, which appears exactly once. Find the single element and return it. 5 | 6 | You must implement a solution with a linear runtime complexity and use only constant extra space. 7 | 8 | Example 1: 9 | 10 | ``` 11 | Input: nums = [2,2,3,2] 12 | Output: 3 13 | ``` 14 | 15 | Example 2: 16 | 17 | ``` 18 | Input: nums = [0,1,0,1,0,1,99] 19 | Output: 99 20 | ``` -------------------------------------------------------------------------------- /137. Single Number II/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 11 ms 3 | 10.2 MB 4 | */ 5 | /** 6 | * TC: O(n) 7 | * SC: O(n) 8 | */ 9 | /** 10 | * @author : SahilK-027 11 | * @brief : Hashing 12 | */ 13 | 14 | class Solution { 15 | public: 16 | int singleNumber(vector& nums) { 17 | unordered_mapmp; 18 | for(auto i: nums){ 19 | mp[i]++; 20 | } 21 | for(auto i: mp){ 22 | if(i.second == 1){ 23 | return i.first; 24 | } 25 | } 26 | return -1; 27 | } 28 | }; -------------------------------------------------------------------------------- /1382. Balance a Binary Search Tree/README.md: -------------------------------------------------------------------------------- 1 | # Balance a Binary Search Tree 2 | ## Description 3 | 4 | Given the root of a binary search tree, return a balanced binary search tree with the same node values. If there is more than one answer, return any of them. 5 | 6 | A binary search tree is balanced if the depth of the two subtrees of every node never differs by more than 1. 7 | 8 | Example 1: 9 | 10 | ![alt](https://assets.leetcode.com/uploads/2021/08/10/balance1-tree.jpg) 11 | ``` 12 | Input: root = [1,null,2,null,3,null,4,null,null] 13 | Output: [2,1,3,null,null,null,4] 14 | Explanation: This is not the only correct answer, [3,1,4,null,2] is also correct. 15 | ``` 16 | 17 | Example 2: 18 | 19 | ![alt](https://assets.leetcode.com/uploads/2021/08/10/balanced2-tree.jpg) 20 | ``` 21 | Input: root = [2,1,3] 22 | Output: [2,1,3] 23 | ``` -------------------------------------------------------------------------------- /1390. Four Divisors/README.md: -------------------------------------------------------------------------------- 1 | # Four Divisors 2 | 3 | ## Description 4 | Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors. If there is no such integer in the array, return 0. 5 | 6 | Example 1: 7 | 8 | ``` 9 | Input: nums = [21,4,7] 10 | Output: 32 11 | Explanation: 12 | 21 has 4 divisors: 1, 3, 7, 21 13 | 4 has 3 divisors: 1, 2, 4 14 | 7 has 2 divisors: 1, 7 15 | The answer is the sum of divisors of 21 only. 16 | ``` 17 | 18 | Example 2: 19 | 20 | ``` 21 | Input: nums = [21,21] 22 | Output: 64 23 | ``` 24 | 25 | Example 3: 26 | 27 | ``` 28 | Input: nums = [1,2,3,4,5] 29 | Output: 0 30 | ``` -------------------------------------------------------------------------------- /14. Longest Common Prefix/README.md: -------------------------------------------------------------------------------- 1 | # Longest Common Prefix 2 | ## Description 3 | 4 | Write a function to find the longest common prefix string amongst an array of strings. 5 | 6 | If there is no common prefix, return an empty string "". 7 | 8 | Example 1: 9 | 10 | ``` 11 | Input: strs = ["flower","flow","flight"] 12 | Output: "fl" 13 | ``` 14 | 15 | Example 2: 16 | 17 | ``` 18 | Input: strs = ["dog","racecar","car"] 19 | Output: "" 20 | Explanation: There is no common prefix among the input strings. 21 | ``` 22 | -------------------------------------------------------------------------------- /143. Reorder List/README.md: -------------------------------------------------------------------------------- 1 | # Reorder List 2 | ## Description 3 | 4 | You are given the head of a singly linked-list. The list can be represented as: 5 | 6 | ``` 7 | L0 → L1 → … → Ln - 1 → Ln 8 | ``` 9 | Reorder the list to be on the following form: 10 | 11 | ``` 12 | L0 → Ln → L1 → Ln - 1 → L2 → Ln - 2 → … 13 | ``` 14 | You may not modify the values in the list's nodes. Only nodes themselves may be changed. 15 | 16 | Example 1: 17 | 18 | ![alt](https://assets.leetcode.com/uploads/2021/03/04/reorder1linked-list.jpg) 19 | ``` 20 | Input: head = [1,2,3,4] 21 | Output: [1,4,2,3] 22 | ``` 23 | 24 | Example 2: 25 | 26 | ![alt](https://assets.leetcode.com/uploads/2021/03/09/reorder2-linked-list.jpg) 27 | ``` 28 | Input: head = [1,2,3,4,5] 29 | Output: [1,5,2,4,3] 30 | ``` 31 | -------------------------------------------------------------------------------- /1431. Kids With the Greatest Number of Candies/solution.js: -------------------------------------------------------------------------------- 1 | /* 2 | Runtime : 71 ms 3 | Memory : 42 MB 4 | 5 | TC : O(N) 6 | SC : O(1) 7 | */ 8 | 9 | /** 10 | * @author : Sahilk-027 11 | * @brief : Iteration 12 | */ 13 | 14 | /** 15 | * @param {number[]} candies 16 | * @param {number} extraCandies 17 | * @return {boolean[]} 18 | */ 19 | var kidsWithCandies = function(candies, extraCandies) { 20 | let maxi = candies[0]; 21 | let ans = []; 22 | candies.forEach((ele, i)=>{ 23 | if(ele > maxi){ 24 | maxi = ele; 25 | } 26 | }); 27 | candies.forEach((ele, i)=>{ 28 | if(ele + extraCandies >= maxi){ 29 | ans.push(true); 30 | } 31 | else{ 32 | ans.push(false); 33 | } 34 | }); 35 | return ans; 36 | }; 37 | -------------------------------------------------------------------------------- /144. Binary Tree Preorder Traversal/README.md: -------------------------------------------------------------------------------- 1 | # Binary Tree Preorder Traversal 2 | ## Description 3 | 4 | Given the root of a binary tree, return the preorder traversal of its nodes' values. 5 | 6 | Example 1: 7 | 8 | ![alt](https://assets.leetcode.com/uploads/2020/09/15/inorder_1.jpg) 9 | ``` 10 | Input: root = [1,null,2,3] 11 | Output: [1,2,3] 12 | ``` 13 | 14 | Example 2: 15 | 16 | ``` 17 | Input: root = [] 18 | Output: [] 19 | ``` 20 | Example 3: 21 | 22 | ``` 23 | Input: root = [1] 24 | Output: [1] 25 | ``` 26 | 27 | Follow up: Recursive solution is trivial, could you do it iteratively? -------------------------------------------------------------------------------- /145. Binary Tree Postorder Traversal/README.md: -------------------------------------------------------------------------------- 1 | # Binary Tree Postorder Traversal 2 | ## Description 3 | 4 | Given the root of a binary tree, return the postorder traversal of its nodes' values. 5 | 6 | Example 1: 7 | 8 | ![alt](https://assets.leetcode.com/uploads/2020/08/28/pre1.jpg) 9 | ``` 10 | Input: root = [1,null,2,3] 11 | Output: [3,2,1] 12 | ``` 13 | 14 | Example 2: 15 | 16 | ``` 17 | Input: root = [] 18 | Output: [] 19 | ``` 20 | Example 3: 21 | 22 | ``` 23 | Input: root = [1] 24 | Output: [1] 25 | ``` 26 | 27 | Follow up: Recursive solution is trivial, could you do it iteratively? -------------------------------------------------------------------------------- /1470. Shuffle the Array/README.md: -------------------------------------------------------------------------------- 1 | # Shuffle the Array 2 | ## Description 3 | 4 | Given the array nums consisting of 2n elements in the form [x1,x2,...,xn,y1,y2,...,yn]. 5 | 6 | Return the array in the form [x1,y1,x2,y2,...,xn,yn]. 7 | 8 | 9 | Example 1: 10 | 11 | ``` 12 | Input: nums = [2,5,1,3,4,7], n = 3 13 | Output: [2,3,5,4,1,7] 14 | Explanation: Since x1=2, x2=5, x3=1, y1=3, y2=4, y3=7 then the answer is [2,3,5,4,1,7]. 15 | ``` 16 | 17 | Example 2: 18 | 19 | ``` 20 | Input: nums = [1,2,3,4,4,3,2,1], n = 4 21 | Output: [1,4,2,3,3,2,4,1] 22 | ``` 23 | 24 | Example 3: 25 | 26 | ``` 27 | Input: nums = [1,1,2,2], n = 2 28 | Output: [1,2,1,2] 29 | ``` -------------------------------------------------------------------------------- /1470. Shuffle the Array/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 7 ms 3 | 9.7 MB 4 | */ 5 | 6 | /** 7 | * TC: O(n) 8 | * SC: O(1) 9 | */ 10 | 11 | /** 12 | * @author : SahilK-027 13 | * @brief : Traversal 14 | */ 15 | 16 | class Solution { 17 | public: 18 | vector shuffle(vector& nums, int n) { 19 | int i = 0; 20 | int j = n; 21 | vector ans; 22 | while(j < 2*n){ 23 | ans.push_back(nums[i]); 24 | ans.push_back(nums[j]); 25 | i++; 26 | j++; 27 | } 28 | return ans; 29 | } 30 | }; -------------------------------------------------------------------------------- /148. Sort List/README.md: -------------------------------------------------------------------------------- 1 | # Binary Tree Postorder Traversal 2 | ## Description 3 | 4 | Given the head of a linked list, return the list after sorting it in ascending order. 5 | 6 | Example 1: 7 | 8 | ![alt](https://assets.leetcode.com/uploads/2020/09/14/sort_list_1.jpg) 9 | ``` 10 | Input: head = [4,2,1,3] 11 | Output: [1,2,3,4] 12 | ``` 13 | 14 | Example 2: 15 | 16 | ![alt](https://assets.leetcode.com/uploads/2020/09/14/sort_list_2.jpg) 17 | ``` 18 | Input: head = [-1,5,3,4,0] 19 | Output: [-1,0,3,4,5] 20 | ``` 21 | 22 | Example 3: 23 | 24 | ``` 25 | Input: head = [] 26 | Output: [] 27 | ``` 28 | 29 | Follow up: Can you sort the linked list in O(n logn) time and O(1) memory (i.e. constant space)? -------------------------------------------------------------------------------- /149. Max Points on a Line/README.md: -------------------------------------------------------------------------------- 1 | # Max Points on a Line 2 | ## Description 3 | 4 | Given an array of points where points[i] = [xi, yi] represents a point on the X-Y plane, return the maximum number of points that lie on the same straight line. 5 | 6 | Example 1: 7 | 8 | ![alt](https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg) 9 | ``` 10 | Input: points = [[1,1],[2,2],[3,3]] 11 | Output: 3 12 | ``` 13 | 14 | Example 2: 15 | 16 | ![alt](https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg) 17 | ``` 18 | Input: points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]] 19 | Output: 4 20 | ``` -------------------------------------------------------------------------------- /1491. Average Salary Excluding the Minimum and Maximum Salary/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 ms 3 | 7.2 MB 4 | 5 | TC : O(N) 6 | SC : O(1) 7 | */ 8 | 9 | /** 10 | * @author : SahilK-027 11 | * @brief : Iteration 12 | */ 13 | 14 | class Solution { 15 | public: 16 | double average(vector& salary) { 17 | if(salary.size()==2) return 0; 18 | int mini = INT_MAX; 19 | int maxi = INT_MIN; 20 | double sum = 0; 21 | for(auto x: salary){ 22 | mini = min(mini,x); 23 | maxi = max(maxi,x); 24 | sum += (double)x; 25 | } 26 | return (sum - (mini + maxi)) / (salary.size()-2); 27 | } 28 | }; -------------------------------------------------------------------------------- /1492. The kth Factor of n/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 0 ms 3 | 6 MB 4 | 5 | TC : O(N log N) 6 | SC : O(1) 7 | */ 8 | 9 | /** 10 | * @author : SahilK-027 11 | * @brief : Sorting 12 | */ 13 | 14 | class Solution { 15 | public: 16 | int kthFactor(int n, int k) { 17 | vector factors; 18 | for(int i = 1 ; i * i <= n; i++){ 19 | if(n % i == 0){ 20 | factors.push_back(i); 21 | if(n / i != i){ 22 | factors.push_back(n / i); 23 | } 24 | } 25 | } 26 | sort(factors.begin(), factors.end()); 27 | return factors.size() >= k ? *(factors.begin() + (k - 1)) : -1; 28 | } 29 | }; -------------------------------------------------------------------------------- /1512. Number of Good Pairs/README.md: -------------------------------------------------------------------------------- 1 | # Number of Good Pairs 2 | ## Description 3 | 4 | Given an array of integers nums, return the number of good pairs. 5 | 6 | A pair (i, j) is called good if nums[i] == nums[j] and i < j. 7 | 8 | Example 1: 9 | 10 | ``` 11 | Input: nums = [1,2,3,1,1,3] 12 | Output: 4 13 | Explanation: There are 4 good pairs (0,3), (0,4), (3,4), (2,5) 0-indexed. 14 | ``` 15 | 16 | Example 2: 17 | 18 | ``` 19 | Input: nums = [1,1,1,1] 20 | Output: 6 21 | Explanation: Each pair in the array are good. 22 | ``` 23 | 24 | Example 3: 25 | 26 | ``` 27 | Input: nums = [1,2,3] 28 | Output: 0 29 | ``` -------------------------------------------------------------------------------- /1512. Number of Good Pairs/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 0 ms 3 | 7.3 MB 4 | */ 5 | 6 | /** 7 | * TC: O(n) 8 | * SC: O(n) 9 | */ 10 | 11 | /** 12 | * @author : SahilK-027 13 | * @brief : Hashing 14 | */ 15 | 16 | 17 | #define combinetrics(i) (i * (i - 1)) / 2; 18 | class Solution { 19 | public: 20 | int numIdenticalPairs(vector& nums) { 21 | // Map to store number and it's count 22 | unordered_mapmp; 23 | for(auto i: nums){ 24 | mp[i]++; 25 | } 26 | // Final answer 27 | int ans = 0; 28 | for(auto i: mp){ 29 | if(i.second != 1){ 30 | // This function will return nc2 31 | ans += combinetrics(i.second); 32 | } 33 | } 34 | return ans; 35 | } 36 | }; -------------------------------------------------------------------------------- /1518. Water Bottles/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 ms 3 | 6.1 MB 4 | */ 5 | 6 | /** 7 | * TC: O(n) 8 | * SC: O(1) 9 | */ 10 | 11 | /** 12 | * @author : SahilK-027 13 | * @brief : Iterative 14 | */ 15 | 16 | class Solution { 17 | public: 18 | int numWaterBottles(int numBottles, int numExchange) { 19 | int ans = 0; 20 | ans += numBottles; 21 | while(numBottles >= numExchange){ 22 | numBottles -= numExchange; 23 | numBottles++; 24 | ans++; 25 | } 26 | return ans; 27 | } 28 | }; -------------------------------------------------------------------------------- /1523. Count Odd Numbers in an Interval Range/README.md: -------------------------------------------------------------------------------- 1 | # Count Odd Numbers in an Interval Range 2 | ## Description 3 | 4 | Given two non-negative integers low and high. Return the count of odd numbers between low and high (inclusive). 5 | 6 | Example 1: 7 | 8 | ``` 9 | Input: low = 3, high = 7 10 | Output: 3 11 | Explanation: The odd numbers between 3 and 7 are [3,5,7]. 12 | ``` 13 | 14 | Example 2: 15 | 16 | ``` 17 | Input: low = 8, high = 10 18 | Output: 1 19 | Explanation: The odd numbers between 8 and 10 are [9]. 20 | ``` -------------------------------------------------------------------------------- /1523. Count Odd Numbers in an Interval Range/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 0 ms 3 | 5.8 MB 4 | 5 | TC : O(N) 6 | SC : O(1) 7 | */ 8 | 9 | /** 10 | * @author : SahilK-027 11 | * @brief : Math 12 | */ 13 | 14 | class Solution { 15 | private: 16 | int isOdd(int num){ 17 | if(num % 2 != 0){ 18 | return 1; 19 | } 20 | return 0; 21 | } 22 | public: 23 | int countOdds(int low, int high) { 24 | if(isOdd(low)){ 25 | low = low - 1; 26 | } 27 | if(isOdd(high)){ 28 | high = high + 1; 29 | } 30 | return (high - low) /2; 31 | } 32 | }; -------------------------------------------------------------------------------- /153. Find Minimum in Rotated Sorted Array/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 6 ms 3 | 10.2 MB 4 | 5 | TC : O(log N) 6 | SC : O(1) 7 | */ 8 | 9 | /** 10 | * @author : SahilK-027 11 | * @brief : Binary Search 12 | */ 13 | 14 | class Solution { 15 | public: 16 | int findMin(vector& nums) { 17 | int s =0; 18 | int e = nums.size()-1; 19 | int m = s + (e-s)/2; 20 | while(s=nums[0]) 22 | { 23 | s = m+1; 24 | } 25 | else{ 26 | e = m; 27 | } 28 | m = s + (e-s)/2; 29 | } 30 | if(nums[e]>nums[0]){ 31 | //Array is never rotated 32 | return nums[0]; 33 | } 34 | return nums[e]; 35 | } 36 | }; -------------------------------------------------------------------------------- /1539. Kth Missing Positive Number/README.md: -------------------------------------------------------------------------------- 1 | # Kth Missing Positive Number 2 | ## Description 3 | 4 | Given an array arr of positive integers sorted in a strictly increasing order, and an integer k. 5 | 6 | Return the kth positive integer that is missing from this array. 7 | 8 | Example 1: 9 | 10 | ``` 11 | Input: arr = [2,3,4,7,11], k = 5 12 | Output: 9 13 | Explanation: The missing positive integers are [1,5,6,8,9,10,12,13,...]. The 5th missing positive integer is 9. 14 | ``` 15 | 16 | Example 2: 17 | 18 | ``` 19 | Input: arr = [1,2,3,4], k = 2 20 | Output: 6 21 | Explanation: The missing positive integers are [5,6,7,...]. The 2nd missing positive integer is 6. 22 | ``` 23 | 24 | Follow up: 25 | 26 | - Could you solve this problem in less than O(n) complexity? -------------------------------------------------------------------------------- /1572. Matrix Diagonal Sum/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 28 ms 3 | 11.3 MB 4 | 5 | TC : O(N^2) 6 | SC : O(1) 7 | */ 8 | 9 | /** 10 | * @author : SahilK-027 11 | * @brief : Matrix traversal 12 | */ 13 | 14 | class Solution { 15 | public: 16 | int diagonalSum(vector>& mat) { 17 | int row,col; 18 | row=col= mat.size(); 19 | int sum = 0; 20 | for(int i = 0 ; i& word1, vector& word2) { 17 | string s1 = ""; 18 | string s2 = ""; 19 | 20 | for(auto i: word1) s1 += i; 21 | for(auto i: word2) s2 += i; 22 | 23 | return s1 == s2; 24 | } 25 | }; -------------------------------------------------------------------------------- /167. Two Sum II - Input Array Is Sorted/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 38 ms 3 | 15.6 MB 4 | 5 | TC : O(N) 6 | SC : O(1) 7 | */ 8 | 9 | /** 10 | * @author : SahilK-027 11 | * @brief : Two pointer 12 | */ 13 | 14 | class Solution { 15 | public: 16 | vector twoSum(vector& arr, int t) { 17 | vector ans; 18 | int s = 0; 19 | int e = arr.size()-1; 20 | while(s>& accounts) { 17 | int row = accounts.size(); 18 | int col = accounts[0].size(); 19 | int maximum = INT_MIN; 20 | for(int i = 0;i 1 10 | B -> 2 11 | C -> 3 12 | ... 13 | Z -> 26 14 | AA -> 27 15 | AB -> 28 16 | ... 17 | ``` 18 | 19 | Example 1: 20 | 21 | ``` 22 | Input: columnTitle = "A" 23 | Output: 1 24 | ``` 25 | 26 | Example 2: 27 | 28 | ``` 29 | Input: columnTitle = "AB" 30 | Output: 28 31 | ``` 32 | 33 | Example 3: 34 | 35 | ``` 36 | Input: columnTitle = "ZY" 37 | Output: 701 38 | ``` -------------------------------------------------------------------------------- /171. Excel Sheet Column Number/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 7 ms 3 | 6.1 MB 4 | */ 5 | 6 | /** 7 | * TC: O(n) 8 | * SC: O(1) 9 | */ 10 | 11 | /** 12 | * @author : SahilK-027 13 | * @brief : Iterative + ASCII 14 | */ 15 | 16 | class Solution { 17 | public: 18 | int titleToNumber(string s) { 19 | int cnt = 0; 20 | int ans = 0; 21 | for(int i = s.length()-1; i >= 0 ; i--){ 22 | ans += pow(26, cnt) * (s[i] - 'A' + 1); 23 | cnt++; 24 | } 25 | return ans; 26 | } 27 | }; -------------------------------------------------------------------------------- /172. Factorial Trailing Zeroes/README.md: -------------------------------------------------------------------------------- 1 | # Factorial Trailing Zeroes 2 | 3 | ## Description 4 | 5 | Given an integer n, return the number of trailing zeroes in n!. 6 | 7 | Note that n! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1. 8 | 9 | Example 1: 10 | 11 | ``` 12 | Input: n = 3 13 | Output: 0 14 | Explanation: 3! = 6, no trailing zero. 15 | ``` 16 | 17 | Example 2: 18 | 19 | ``` 20 | Input: n = 5 21 | Output: 1 22 | Explanation: 5! = 120, one trailing zero. 23 | ``` 24 | 25 | Example 3: 26 | 27 | ``` 28 | Input: n = 0 29 | Output: 0 30 | ``` 31 | 32 | - Follow up: Could you write a solution that works in logarithmic time complexity? -------------------------------------------------------------------------------- /172. Factorial Trailing Zeroes/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 8 ms 3 | 6.3 MB 4 | */ 5 | 6 | /** 7 | * TC: O(n) 8 | * SC: O(1) 9 | */ 10 | 11 | /** 12 | * @author : SahilK-027 13 | * @brief : Math 14 | */ 15 | 16 | class Solution { 17 | public: 18 | int trailingZeroes(int n) { 19 | int ans = 0; 20 | for(int i = 1; i < n; i++) 21 | { 22 | if( n / pow(5, i) > 0){ 23 | ans += n / pow(5, i); 24 | } 25 | else{ 26 | break; 27 | } 28 | } 29 | return ans; 30 | } 31 | }; -------------------------------------------------------------------------------- /1721. Swapping Nodes in a Linked List/README.md: -------------------------------------------------------------------------------- 1 | # Swapping Nodes in a Linked List 2 | 3 | ## Description 4 | You are given the head of a linked list, and an integer k. 5 | 6 | Return the head of the linked list after swapping the values of the kth node from the beginning and the kth node from the end (the list is 1-indexed). 7 | 8 | 9 | Example 1: 10 | 11 | ![alt](https://assets.leetcode.com/uploads/2020/09/21/linked1.jpg) 12 | ``` 13 | Input: head = [1,2,3,4,5], k = 2 14 | Output: [1,4,3,2,5] 15 | ``` 16 | 17 | Example 2: 18 | 19 | ``` 20 | Input: head = [7,9,6,6,7,8,3,0,9,5], k = 5 21 | Output: [7,9,6,6,8,7,3,0,9,5] 22 | ``` -------------------------------------------------------------------------------- /1752. Check if Array Is Sorted and Rotated/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 43 ms 3 | 8.3 MB 4 | 5 | TC : O(N) 6 | SC : O(1) 7 | */ 8 | 9 | /** 10 | * @author : SahilK-027 11 | * @brief : counting 12 | */ 13 | 14 | class Solution { 15 | public: 16 | bool check(vector& nums) 17 | { 18 | int break_pt=0; //Keeping count of break_pts 19 | for(int i=0;i& nums) { 17 | int neg_count = 0; 18 | bool zero_present = false; 19 | for(auto i: nums){ 20 | if(i < 0){ 21 | neg_count++; 22 | } 23 | if(i == 0){ 24 | zero_present = true; 25 | break; 26 | } 27 | } 28 | if(zero_present) return 0; 29 | if(neg_count & 1) return -1; 30 | return 1; 31 | } 32 | }; -------------------------------------------------------------------------------- /1823. Find the Winner of the Circular Game/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 0 ms 3 | 6 MB 4 | 5 | TC : O(N) 6 | SC : O(N) 7 | */ 8 | 9 | /** 10 | * @author : SahilK-027 11 | * @brief : Recursive solution 12 | */ 13 | 14 | class Solution { 15 | private: 16 | int solve(int n , int k){ 17 | if(n == 1) return 0; 18 | return (solve (n-1,k) + k ) % n; 19 | } 20 | public: 21 | int findTheWinner(int n, int k) { 22 | return solve(n,k) + 1; 23 | } 24 | }; -------------------------------------------------------------------------------- /1863. Sum of All Subset XOR Totals/solution2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 4 ms 3 | 7 MB 4 | 5 | TC : O(2^N) 6 | SC : O(N) 7 | */ 8 | 9 | /** 10 | * @author : SahilK-027 11 | * @brief : Recursive solution 12 | */ 13 | 14 | class Solution { 15 | private: 16 | void solve(vector& arr, int& grand_total, int xorAns, int i =0){ 17 | if(i == arr.size()){ 18 | grand_total += xorAns; 19 | return; 20 | } 21 | // Include 22 | solve(arr, grand_total,xorAns ^ arr[i], i+1); 23 | // Exclude 24 | solve(arr, grand_total,xorAns, i+1); 25 | } 26 | public: 27 | int subsetXORSum(vector& arr) { 28 | // Step 1: 29 | int grand_total = 0; 30 | int xorAns = 0; 31 | solve(arr,grand_total,xorAns); 32 | return grand_total; 33 | } 34 | }; 35 | -------------------------------------------------------------------------------- /189. Rotate Array/README.md: -------------------------------------------------------------------------------- 1 | # Rotate Array 2 | ## Description 3 | 4 | Given an array, rotate the array to the right by k steps, where k is non-negative. 5 | 6 | Example 1: 7 | 8 | ``` 9 | Input: nums = [1,2,3,4,5,6,7], k = 3 10 | Output: [5,6,7,1,2,3,4] 11 | Explanation: 12 | rotate 1 steps to the right: [7,1,2,3,4,5,6] 13 | rotate 2 steps to the right: [6,7,1,2,3,4,5] 14 | rotate 3 steps to the right: [5,6,7,1,2,3,4] 15 | ``` 16 | 17 | Example 2: 18 | 19 | ``` 20 | Input: nums = [-1,-100,3,99], k = 2 21 | Output: [3,99,-1,-100] 22 | Explanation: 23 | rotate 1 steps to the right: [99,-1,-100,3] 24 | rotate 2 steps to the right: [3,99,-1,-100] 25 | ``` 26 | 27 | Follow up: 28 | 29 | - Try to come up with as many solutions as you can. There are at least three different ways to solve this problem. 30 | - Could you do it in-place with O(1) extra space? -------------------------------------------------------------------------------- /189. Rotate Array/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 78 ms 3 | 25 MB 4 | 5 | TC : O(N) 6 | SC : O(1) 7 | */ 8 | 9 | /** 10 | * @author : SahilK-027 11 | * @brief : Math 12 | */ 13 | 14 | /* 15 | LOGIC: 16 | 1. REVERSE FIRST N-K 17 | 2. REVERSE LATS K 18 | 3. REVERSE WHOLE ARRAY 19 | */ 20 | 21 | class Solution { 22 | private: 23 | void reverse(vector& arr, int s, int e){ 24 | while(s& arr, int k) { 32 | int n = arr.size(); 33 | if(k == n){ 34 | return; 35 | } 36 | k = k % n; 37 | //Step 1 38 | reverse(arr,0,n-k-1); 39 | //Step 2 40 | reverse(arr,n-k,n-1); 41 | //Step 3 42 | reverse(arr,0,n-1); 43 | } 44 | }; -------------------------------------------------------------------------------- /1894. Find the Student that Will Replace the Chalk/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Runtime : 141 ms 3 | Memory : 74.5 MB 4 | 5 | TC: O(N) 6 | SC: O(1) 7 | */ 8 | 9 | /** 10 | * @author : Sahilk-027 11 | * @brief : Math 12 | */ 13 | 14 | #define ll long long 15 | class Solution { 16 | public: 17 | int chalkReplacer(vector& arr, int k) { 18 | ll sum = 0; 19 | for(auto i: arr){ 20 | sum += i; 21 | } 22 | cout<>y = x/2y 23 | } 24 | return set_bit_count; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /1910. Remove All Occurrences of a Substring/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 ms 3 | 6.6 MB 4 | */ 5 | 6 | /** 7 | * @author : SahilK-027 8 | * @brief : Iterative 9 | */ 10 | 11 | class Solution { 12 | public: 13 | string removeOccurrences(string s, string part) 14 | { 15 | //Initially as there will be at least one substring we will make count 1 16 | int count=1; 17 | while(s.length()!=0 && count == 1) 18 | { 19 | count = 0; 20 | if(s.find(part)& nums, int k) { 17 | priority_queue, greater> pq; 18 | // Step 1: Build heap from first k elements 19 | for(int i = 0 ; i < k ; i++){ 20 | pq.push(nums[i]); 21 | } 22 | 23 | // Step 2: For remaining n -k elements 24 | for(int i = k ; i < nums.size() ; i++){ 25 | if(nums[i] > pq.top()){ 26 | pq.pop(); 27 | pq.push(nums[i]); 28 | } 29 | } 30 | 31 | // Step 3: Your answer will be top of heap 32 | int ans = pq.top(); 33 | return ans; 34 | } 35 | }; -------------------------------------------------------------------------------- /2169. Count Operations to Obtain Zero/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 0 ms 3 | 6 MB 4 | 5 | TC : O(N) 6 | SC : O(N) 7 | */ 8 | 9 | /** 10 | * @author : SahilK-027 11 | * @brief : Recursive solution 12 | */ 13 | 14 | class Solution { 15 | private: 16 | int solve(int n1, int n2, int ans = 0){ 17 | if(n1 == 0 || n2 == 0) return ans; 18 | if(n1 >= n2) return solve(n1 - n2, n2, ans+1); 19 | else return solve(n1, n2-n1, ans+1); 20 | } 21 | public: 22 | int countOperations(int n1, int n2) { 23 | ios_base::sync_with_stdio(0); 24 | cin.tie(0); 25 | return solve(n1,n2); 26 | } 27 | }; -------------------------------------------------------------------------------- /217. Contains Duplicate/README.md: -------------------------------------------------------------------------------- 1 | # Contains Duplicate 2 | ## Description 3 | 4 | Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct. 5 | 6 | Example 1: 7 | 8 | 9 | ``` 10 | Input: nums = [1,2,3,1] 11 | Output: true 12 | ``` 13 | 14 | Example 2: 15 | 16 | ``` 17 | Input: nums = [1,2,3,4] 18 | Output: false 19 | ``` 20 | 21 | Example 3: 22 | 23 | ``` 24 | Input: nums = [1,1,1,3,3,4,3,2,4,2] 25 | Output: true 26 | ``` -------------------------------------------------------------------------------- /217. Contains Duplicate/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 279 ms 3 | 66.8 MB 4 | 5 | TC : O(N) 6 | SC : O(N) 7 | */ 8 | 9 | /** 10 | * @author : SahilK-027 11 | * @brief : Set 12 | */ 13 | 14 | //Using set as it stores only unique numbers 15 | class Solution { 16 | public: 17 | bool containsDuplicate(vector& nums) { 18 | unordered_set set; 19 | for(int i = 0 ;i4->5, 16 | 1->3->4, 17 | 2->6 18 | ] 19 | merging them into one sorted list: 20 | 1->1->2->3->4->4->5->6 21 | ``` 22 | Example 2: 23 | ``` 24 | Input: lists = [] 25 | Output: [] 26 | ``` 27 | Example 3: 28 | ``` 29 | Input: lists = [[]] 30 | Output: [] 31 | ``` 32 | -------------------------------------------------------------------------------- /230. Kth Smallest Element in a BST/README.md: -------------------------------------------------------------------------------- 1 | # Kth Smallest Element in a BST 2 | ## Description 3 | 4 | Given the root of a binary search tree, and an integer k, return the kth smallest value (1-indexed) of all the values of the nodes in the tree. 5 | 6 | 7 | Example 1: 8 | 9 | ![alt](https://assets.leetcode.com/uploads/2021/01/28/kthtree1.jpg) 10 | ``` 11 | Input: root = [3,1,4,null,2], k = 1 12 | Output: 1 13 | ``` 14 | 15 | Example 2: 16 | 17 | ![alt](https://assets.leetcode.com/uploads/2021/01/28/kthtree2.jpg) 18 | ``` 19 | Input: root = [5,3,6,2,4,null,null,1], k = 3 20 | Output: 3 21 | ``` 22 | 23 | Follow up: If the BST is modified often (i.e., we can do insert and delete operations) and you need to find the kth smallest frequently, how would you optimize? -------------------------------------------------------------------------------- /231. Power of Two/README.md: -------------------------------------------------------------------------------- 1 | # Power of Two 2 | ## Description 3 | 4 | Given an integer n, return true if it is a power of two. Otherwise, return false. 5 | 6 | An integer n is a power of two, if there exists an integer x such that n == 2x. 7 | 8 | 9 | Example 1: 10 | 11 | ``` 12 | Input: n = 1 13 | Output: true 14 | Explanation: 20 = 1 15 | ``` 16 | 17 | Example 2: 18 | 19 | ``` 20 | Input: n = 16 21 | Output: true 22 | Explanation: 24 = 16 23 | ``` 24 | 25 | Example 3: 26 | 27 | ``` 28 | Input: n = 3 29 | Output: false 30 | ``` 31 | 32 | Follow up: Could you solve it without loops/recursion? -------------------------------------------------------------------------------- /231. Power of Two/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 ms 3 | 6 MB 4 | 5 | TC : O(N) 6 | SC : O(1) 7 | */ 8 | 9 | /** 10 | * @author : SahilK-027 11 | * @brief : Math 12 | */ 13 | 14 | /* 15 | logic of counting number of set bits 16 | */ 17 | class Solution { 18 | public: 19 | bool isPowerOfTwo(int n) { 20 | if(n<0){ 21 | return false; 22 | } 23 | int set_bit_count=0; 24 | while(n!=0){ 25 | int bit = n % 2; //Convert the number to binary 26 | if(bit) 27 | set_bit_count++; //If bit == 1 count++ 28 | n=n/2; 29 | } 30 | if(set_bit_count == 1) 31 | return true; 32 | return false; 33 | } 34 | }; -------------------------------------------------------------------------------- /231. Power of Two/solution2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Runtime - 4ms 3 | Memory - 6MB 4 | */ 5 | class Solution { 6 | public: 7 | bool isPowerOfTwo(int n) { 8 | return n>0 and !(n&(n-1)); 9 | } 10 | }; 11 | -------------------------------------------------------------------------------- /231. Power of Two/solution3.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Runtime - 0ms 3 | Memory - 6MB 4 | */ 5 | class Solution { 6 | public: 7 | bool isPowerOfTwo(int n) { 8 | if(n <= 0) { 9 | return false; 10 | } 11 | // since the numbers who are powers of 2 have atleast 1 true bit 12 | // we perform logical 'and' operation with the number and its previous one 13 | return ((n&(n-1)) == 0); 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /233. Number of Digit One/README.md: -------------------------------------------------------------------------------- 1 | # Number of Digit One 2 | ## Description 3 | 4 | Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n. 5 | 6 | 7 | Example 1: 8 | 9 | ``` 10 | Input: n = 13 11 | Output: 6 12 | ``` 13 | 14 | Example 2: 15 | 16 | ``` 17 | Input: n = 0 18 | Output: 0 19 | ``` -------------------------------------------------------------------------------- /2336. Smallest Number in Infinite Set/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 121 ms 3 | 47.7 MB 4 | 5 | TC : O(N) 6 | SC : O(N) 7 | */ 8 | 9 | /** 10 | * @author : SahilK-027 11 | * @brief : SET 12 | */ 13 | 14 | class SmallestInfiniteSet { 15 | private: 16 | sets; 17 | public: 18 | SmallestInfiniteSet() { 19 | for(int i=1; i<= 1001; i++){ 20 | s.emplace(i); 21 | } 22 | } 23 | 24 | int popSmallest() { 25 | int top = *s.begin(); 26 | s.erase(s.begin()); 27 | return top; 28 | } 29 | 30 | void addBack(int i) { 31 | s.emplace(i); 32 | } 33 | }; 34 | 35 | /** 36 | * Your SmallestInfiniteSet object will be instantiated and called as such: 37 | * SmallestInfiniteSet* obj = new SmallestInfiniteSet(); 38 | * int param_1 = obj->popSmallest(); 39 | * obj->addBack(num); 40 | */ -------------------------------------------------------------------------------- /234. Palindrome Linked List/README.md: -------------------------------------------------------------------------------- 1 | # Palindrome Linked List 2 | ## Description 3 | 4 | Given the head of a singly linked list, return true if it is a 5 | palindrome or false otherwise. 6 | 7 | Example 1: 8 | 9 | ![alt](https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg) 10 | ``` 11 | Input: head = [1,2,2,1] 12 | Output: true 13 | ``` 14 | 15 | Example 2: 16 | 17 | ![alt](https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg) 18 | ``` 19 | Input: head = [1,2] 20 | Output: false 21 | ``` 22 | 23 | Follow up: Could you do it in O(n) time and O(1) space? -------------------------------------------------------------------------------- /2348. Number of Zero-Filled Subarrays/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Runtime : 175 ms 3 | Memory : 107.6 MB 4 | 5 | TC: O(N) 6 | SC: O(1) 7 | */ 8 | 9 | /** 10 | * @author : Sahilk-027 11 | * @brief : iterative 12 | */ 13 | 14 | #define ll long long 15 | class Solution { 16 | public: 17 | ll zeroFilledSubarray(vector& nums) { 18 | ll ans = 0; 19 | for (int i = 0, j = 0; i < nums.size(); i++) 20 | { 21 | if (nums[i] != 0) j = i + 1; 22 | ans += i - j + 1; 23 | } 24 | return ans; 25 | } 26 | }; -------------------------------------------------------------------------------- /235. Lowest Common Ancestor of a Binary Search Tree/solution2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 68 ms 3 | 23.1 MB 4 | 5 | TC : O(N) 6 | SC : O(height) 7 | */ 8 | 9 | /** 10 | * @author : SahilK-027 11 | * @brief : Recursive solution 12 | */ 13 | 14 | class Solution { 15 | public: 16 | TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* a, TreeNode* b) { 17 | if(root == NULL){ 18 | return NULL; 19 | } 20 | if(root->val < a->val && root->val < b->val){ 21 | return lowestCommonAncestor(root->right, a,b); 22 | } 23 | if(root->val > a->val && root->val > b->val){ 24 | return lowestCommonAncestor(root->left, a,b); 25 | } 26 | return root; 27 | } 28 | }; -------------------------------------------------------------------------------- /2367. Number of Arithmetic Triplets/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 323 ms 3 | 8.6 MB 4 | 5 | TC : O(N^3) 6 | SC : O(1) 7 | */ 8 | 9 | /** 10 | * @author : SahilK-027 11 | * @brief : Iteration 12 | */ 13 | 14 | class Solution { 15 | public: 16 | int arithmeticTriplets(vector& nums, int diff) { 17 | int count = 0; 18 | for(int i = 0 ;i productExceptSelf(vector& arr) { 17 | vectorans(arr.size()); 18 | vector pre(arr.size(), 1); 19 | vector post(arr.size(), 1); 20 | for(int i = 1; i < arr.size(); i++){ 21 | pre[i] = arr[i-1] * pre[i-1]; 22 | } 23 | for(int i = arr.size()-2; i >=0 ; i--){ 24 | post[i] = arr[i+1] * post[i+1]; 25 | } 26 | for(int i = 0; i < arr.size(); i++){ 27 | ans[i] = pre[i] * post[i]; 28 | } 29 | return ans; 30 | } 31 | }; -------------------------------------------------------------------------------- /2390. Removing Stars From a String/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Runtime : 81 ms 3 | Memory : 24.1 MB 4 | 5 | TC: O(N) 6 | SC: O(1) 7 | */ 8 | 9 | /** 10 | * @author : Sahilk-027 11 | * @brief : Iteration 12 | */ 13 | 14 | class Solution { 15 | public: 16 | string removeStars(string s) { 17 | int j = 0; 18 | for (int i = 0; i < s.size(); ++i){ 19 | if (s[i] == '*') j--; 20 | else s[j++] = s[i]; 21 | } 22 | return s.substr(0, j); 23 | } 24 | }; -------------------------------------------------------------------------------- /24. Swap Nodes in Pairs/README.md: -------------------------------------------------------------------------------- 1 | # Swap Nodes in Pairs 2 | ## Description 3 | 4 | Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list's nodes (i.e., only nodes themselves may be changed.) 5 | 6 | 7 | Example 1: 8 | 9 | ![alt](https://assets.leetcode.com/uploads/2020/10/03/swap_ex1.jpg) 10 | ``` 11 | Input: head = [1,2,3,4] 12 | Output: [2,1,4,3] 13 | Explanation: Because nums[0] + nums[1] == 9, we return [0, 1]. 14 | ``` 15 | Example 2: 16 | ``` 17 | Input: head = [] 18 | Output: [] 19 | ``` 20 | Example 3: 21 | ``` 22 | Input: head = [1] 23 | Output: [1] 24 | ``` 25 | -------------------------------------------------------------------------------- /240. Search a 2D Matrix II/README.md: -------------------------------------------------------------------------------- 1 | # Search a 2D Matrix II 2 | ## Description 3 | 4 | Write an efficient algorithm that searches for a value target in an m x n integer matrix matrix. This matrix has the following properties: 5 | 6 | - Integers in each row are sorted in ascending from left to right. 7 | - Integers in each column are sorted in ascending from top to bottom. 8 | 9 | 10 | Example 1: 11 | 12 | ![alt](https://assets.leetcode.com/uploads/2020/11/24/searchgrid2.jpg) 13 | ``` 14 | Input: matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 5 15 | Output: true 16 | ``` 17 | 18 | Example 2: 19 | 20 | ![alt](https://assets.leetcode.com/uploads/2020/11/24/searchgrid.jpg) 21 | ``` 22 | Input: matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 20 23 | Output: false 24 | ``` -------------------------------------------------------------------------------- /2405. Optimal Partition of String/README.md: -------------------------------------------------------------------------------- 1 | # Optimal Partition of String 2 | ## Description 3 | 4 | Given a string s, partition the string into one or more substrings such that the characters in each substring are unique. That is, no letter appears in a single substring more than once. 5 | 6 | Return the minimum number of substrings in such a partition. 7 | 8 | Note that each character should belong to exactly one substring in a partition. 9 | 10 | Example 1: 11 | 12 | ``` 13 | Input: s = "abacaba" 14 | Output: 4 15 | Explanation: 16 | Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba"). 17 | It can be shown that 4 is the minimum number of substrings needed. 18 | ``` 19 | 20 | Example 2: 21 | 22 | ``` 23 | Input: s = "ssssss" 24 | Output: 6 25 | Explanation: 26 | The only valid partition is ("s","s","s","s","s","s"). 27 | ``` -------------------------------------------------------------------------------- /2405. Optimal Partition of String/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Runtime : 242 ms 3 | Memory : 44.6 MB 4 | 5 | TC: O(N) 6 | SC: O(N) 7 | */ 8 | 9 | /** 10 | * @author : Sahilk-027 11 | * @brief : Hashing 12 | */ 13 | 14 | class Solution { 15 | public: 16 | int partitionString(string s) { 17 | int cnt =0; 18 | int i = 0; 19 | unordered_mapmp; 20 | while(i < s.size()){ 21 | while(i < s.size() && mp[s[i]] == 0){ 22 | mp[s[i]] = 1; 23 | i++; 24 | } 25 | mp.clear(); 26 | cnt++; 27 | } 28 | return cnt; 29 | } 30 | }; -------------------------------------------------------------------------------- /242. Valid Anagram/README.md: -------------------------------------------------------------------------------- 1 | # Valid Anagram 2 | ## Description 3 | 4 | Given two strings s and t, return true if t is an anagram of s, and false otherwise. 5 | 6 | An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. 7 | 8 | Example 1: 9 | 10 | ``` 11 | Input: s = "anagram", t = "nagaram" 12 | Output: true 13 | ``` 14 | 15 | Example 2: 16 | 17 | ``` 18 | Input: s = "rat", t = "car" 19 | Output: false 20 | ``` 21 | 22 | - Follow up: What if the inputs contain Unicode characters? How would you adapt your solution to such a case? -------------------------------------------------------------------------------- /242. Valid Anagram/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Runtime : 13 ms 3 | Memory : 7.2 MB 4 | 5 | TC : O(N) 6 | SC : O(N) 7 | */ 8 | 9 | /** 10 | * @author : Sahilk-027 11 | * @brief : Hashing 12 | */ 13 | 14 | class Solution { 15 | public: 16 | bool isAnagram(string s, string t) { 17 | map mp1; 18 | map mp2; 19 | for(auto i: s){ 20 | mp1[i]++; 21 | } 22 | for(auto i: t){ 23 | mp2[i]++; 24 | } 25 | if(mp1.size() != mp2.size()){ 26 | return false; 27 | } 28 | for(auto i: mp1){ 29 | if(mp2[i.first] != i.second){ 30 | return false; 31 | } 32 | } 33 | return true; 34 | } 35 | }; -------------------------------------------------------------------------------- /2439. Minimize Maximum of Array/solution.js: -------------------------------------------------------------------------------- 1 | /* 2 | Runtime : 76 ms 3 | Memory : 52.3 MB 4 | 5 | TC: O(N) 6 | SC: O(1) 7 | */ 8 | 9 | /** 10 | * @author : Sahilk-027 11 | * @brief : Iteration 12 | */ 13 | 14 | /** 15 | * @param {number[]} nums 16 | * @return {number} 17 | */ 18 | var minimizeArrayValue = function(nums) { 19 | let ans = nums[0]; 20 | let curr_total = nums[0]; 21 | for(let i = 1; i < nums.length; i++){ 22 | curr_total += nums[i]; 23 | let curr_ans = Math.ceil(curr_total / (i +1)); 24 | ans = Math.max(ans, curr_ans); 25 | } 26 | return ans; 27 | }; 28 | -------------------------------------------------------------------------------- /2466. Count Ways To Build Good Strings/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 7 ms 3 | 20.1 MB 4 | 5 | TC : O(N) 6 | SC : O(N) 7 | */ 8 | 9 | /** 10 | * @author : SahilK-027 11 | * @brief : Dynamic Programming 12 | */ 13 | 14 | #define ll long long 15 | #define mod 1000000007 16 | class Solution { 17 | public: 18 | ll solve(ll len,ll low,ll high,vector&dp,int zero,int one){ 19 | if(len>high)return 0; 20 | if(dp[len]!=-1)return dp[len]; 21 | ll ans=0; 22 | if(len>=low && len<=high)ans=1; 23 | ans += solve(len+zero,low,high,dp,zero,one)%mod; 24 | ans = (ans%mod + solve(len+one,low,high,dp,zero,one)%mod)%mod; 25 | dp[len]=ans; 26 | return ans; 27 | } 28 | int countGoodStrings(int low, int high, int zero, int one) { 29 | vectordp(high+1,-1); 30 | return solve(0,low,high,dp,zero,one); 31 | } 32 | }; -------------------------------------------------------------------------------- /2469. Convert the Temperature/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 ms 3 | 6.1 MB 4 | 5 | TC : O(N) 6 | SC : O(1) 7 | */ 8 | 9 | /** 10 | * @author : SahilK-027 11 | * @brief : Math 12 | */ 13 | 14 | class Solution { 15 | public: 16 | vector convertTemperature(double celsius) { 17 | double Kelvin = celsius + 273.15 ; 18 | double Fahrenheit = celsius * 1.80 + 32.00; 19 | vector ans; 20 | ans.push_back(Kelvin); 21 | ans.push_back(Fahrenheit); 22 | return ans; 23 | } 24 | }; -------------------------------------------------------------------------------- /2481. Minimum Cuts to Divide a Circle/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 0 ms 3 | 6 MB 4 | */ 5 | 6 | /** 7 | * TC: O(1) 8 | * SC: O(1) 9 | */ 10 | 11 | /** 12 | * @author : SahilK-027 13 | * @brief : Math 14 | */ 15 | 16 | class Solution { 17 | public: 18 | int numberOfCuts(int n) { 19 | if(n==1) return 0; 20 | if(n & 1) return n; 21 | else return n/2; 22 | } 23 | }; -------------------------------------------------------------------------------- /2485. Find the Pivot Integer/README.md: -------------------------------------------------------------------------------- 1 | # Find the Pivot Integer 2 | ## Description 3 | 4 | Given a positive integer n, find the pivot integer x such that: 5 | 6 | - The sum of all elements between 1 and x inclusively equals the sum of all elements between x and n inclusively. 7 | Return the pivot integer x. If no such integer exists, return -1. It is guaranteed that there will be at most one pivot index for the given input. 8 | 9 | 10 | Example 1: 11 | 12 | ``` 13 | Input: n = 8 14 | Output: 6 15 | Explanation: 6 is the pivot integer since: 1 + 2 + 3 + 4 + 5 + 6 = 6 + 7 + 8 = 21. 16 | ``` 17 | 18 | Example 2: 19 | 20 | ``` 21 | Input: n = 1 22 | Output: 1 23 | Explanation: 1 is the pivot integer since: 1 = 1. 24 | ``` 25 | 26 | Example 3: 27 | 28 | ``` 29 | Input: n = 4 30 | Output: -1 31 | Explanation: It can be proved that no such integer exist. 32 | ``` -------------------------------------------------------------------------------- /2487. Remove Nodes From Linked List/README.md: -------------------------------------------------------------------------------- 1 | # Remove Nodes From Linked List 2 | ## Description 3 | 4 | You are given the head of a linked list. 5 | 6 | Remove every node which has a node with a strictly greater value anywhere to the right side of it. 7 | 8 | Return the head of the modified linked list. 9 | 10 | Example 1: 11 | 12 | ![alt](https://assets.leetcode.com/uploads/2022/10/02/drawio.png) 13 | ``` 14 | Input: head = [5,2,13,3,8] 15 | Output: [13,8] 16 | Explanation: The nodes that should be removed are 5, 2 and 3. 17 | - Node 13 is to the right of node 5. 18 | - Node 13 is to the right of node 2. 19 | - Node 8 is to the right of node 3. 20 | ``` 21 | 22 | Example 2: 23 | 24 | ``` 25 | Input: head = [1,1,1,1] 26 | Output: [1,1,1,1] 27 | Explanation: Every node has value 1, so no nodes are removed. 28 | ``` -------------------------------------------------------------------------------- /2520. Count the Digits That Divide a Number/README.md: -------------------------------------------------------------------------------- 1 | # Count the Digits That Divide a Number 2 | ## Description 3 | 4 | Given an integer num, return the number of digits in num that divide num. 5 | 6 | An integer val divides nums if nums % val == 0. 7 | 8 | 9 | Example 1: 10 | 11 | ``` 12 | Input: num = 7 13 | Output: 1 14 | Explanation: 7 divides itself, hence the answer is 1. 15 | ``` 16 | 17 | Example 2: 18 | 19 | ``` 20 | Input: num = 121 21 | Output: 2 22 | Explanation: 121 is divisible by 1, but not 2. Since 1 occurs twice as a digit, we return 2. 23 | ``` 24 | 25 | Example 3: 26 | 27 | ``` 28 | Input: num = 1248 29 | Output: 4 30 | Explanation: 1248 is divisible by all of its digits, hence the answer is 4. 31 | ``` -------------------------------------------------------------------------------- /2520. Count the Digits That Divide a Number/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 0 ms 3 | 6 MB 4 | 5 | TC : O(N) 6 | SC : O(N) 7 | */ 8 | 9 | /** 10 | * @author : SahilK-027 11 | * @brief : Hashmap 12 | */ 13 | 14 | class Solution { 15 | public: 16 | int countDigits(int num) { 17 | // Map to store digit and its count 18 | unordered_mapmp; 19 | int n = num; 20 | while(n>0){ 21 | int temp = n%10; 22 | n /= 10; 23 | // Store digit in map 24 | mp[temp]++; 25 | } 26 | int ans = 0; 27 | // Dry dividing num by i.first 28 | // If able to divide store in ans its count 29 | for(auto i: mp){ 30 | if(num % i.first == 0){ 31 | ans += i.second; 32 | } 33 | } 34 | return ans; 35 | } 36 | }; -------------------------------------------------------------------------------- /2535. Difference Between Element Sum and Digit Sum of an Array/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 16 ms 3 | 15.7 MB 4 | 5 | TC : O(N ) 6 | SC : O(1) 7 | */ 8 | 9 | /** 10 | * @author : SahilK-027 11 | * @brief : Iteration 12 | */ 13 | 14 | class Solution { 15 | private: 16 | void calculate(int& ds, int num){ 17 | while(num){ 18 | int digit = num % 10; 19 | ds += digit; 20 | num /= 10; 21 | } 22 | } 23 | public: 24 | int differenceOfSum(vector& nums) { 25 | int num_sum = 0; 26 | for(auto i: nums){ 27 | num_sum += i; 28 | } 29 | int digit_sum =0; 30 | for(int i =0 ; i< nums.size(); i++){ 31 | calculate(digit_sum, nums[i]); 32 | } 33 | 34 | return abs(num_sum-digit_sum); 35 | } 36 | }; -------------------------------------------------------------------------------- /2540. Minimum Common Value/README.md: -------------------------------------------------------------------------------- 1 | # Minimum Common Value 2 | 3 | ## Description 4 | 5 | Given two integer arrays nums1 and nums2, sorted in non-decreasing order, return the minimum integer common to both arrays. If there is no common integer amongst nums1 and nums2, return -1. 6 | 7 | Note that an integer is said to be common to nums1 and nums2 if both arrays have at least one occurrence of that integer. 8 | 9 | Example 1: 10 | 11 | ``` 12 | Input: nums1 = [1,2,3], nums2 = [2,4] 13 | Output: 2 14 | Explanation: The smallest element common to both arrays is 2, so we return 2. 15 | ``` 16 | 17 | Example 2: 18 | 19 | ``` 20 | Input: nums1 = [1,2,3,6], nums2 = [2,3,4,5] 21 | Output: 2 22 | Explanation: There are two common elements in the array 2 and 3 out of which 2 is the smallest, so 2 is returned. 23 | ``` -------------------------------------------------------------------------------- /2544. Alternating Digit Sum/README.md: -------------------------------------------------------------------------------- 1 | # Alternating Digit Sum 2 | ## Description 3 | 4 | You are given a positive integer n. Each digit of n has a sign according to the following rules: 5 | 6 | - The most significant digit is assigned a positive sign. 7 | - Each other digit has an opposite sign to its adjacent digits. 8 | Return the sum of all digits with their corresponding sign. 9 | 10 | 11 | 12 | Example 1: 13 | 14 | ``` 15 | Input: n = 521 16 | Output: 4 17 | Explanation: (+5) + (-2) + (+1) = 4. 18 | ``` 19 | 20 | Example 2: 21 | 22 | ``` 23 | Input: n = 111 24 | Output: 1 25 | Explanation: (+1) + (-1) + (+1) = 1. 26 | ``` 27 | 28 | Example 3: 29 | 30 | ``` 31 | Input: n = 886996 32 | Output: 0 33 | Explanation: (+8) + (-8) + (+6) + (-9) + (+9) + (-6) = 0. 34 | ``` -------------------------------------------------------------------------------- /2553. Separate the Digits in an Array/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 10 ms 3 | 10.1 MB 4 | */ 5 | /** 6 | * TC: O( nums[i] * nums.length) 7 | * SC: O(1) 8 | */ 9 | /** 10 | * @author : SahilK-027 11 | * @brief : Array traversal 12 | */ 13 | 14 | class Solution { 15 | public: 16 | vector separateDigits(vector& arr) { 17 | vector ans; 18 | int n = arr.size()-1; 19 | // Traverse array from back 20 | for(int i = n; i >= 0; i--){ 21 | // For each number in array extract digits and store in answer 22 | while(arr[i]){ 23 | ans.push_back(arr[i] % 10); 24 | arr[i] /= 10; 25 | } 26 | } 27 | // Reverse the ans as we had traversed from back 28 | reverse(ans.begin(), ans.end()); 29 | return ans; 30 | } 31 | }; -------------------------------------------------------------------------------- /2558. Take Gifts From the Richest Pile/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 4 ms 3 | 9.2 MB 4 | */ 5 | /** 6 | * TC: O(N log N) 7 | * SC: O(N) 8 | */ 9 | /** 10 | * @author : SahilK-027 11 | * @brief : Heap 12 | */ 13 | 14 | class Solution { 15 | public: 16 | long long pickGifts(vector& gifts, int k) { 17 | priority_queuepq; 18 | int n = gifts.size(); 19 | for(int i = 0 ; i < n ; i++){ 20 | pq.push(gifts[i]); 21 | } 22 | while(k--){ 23 | int top = pq.top(); 24 | pq.pop(); 25 | int rem = floor(sqrt(top)); 26 | pq.push(rem); 27 | } 28 | long long ans = 0; 29 | while(!pq.empty()){ 30 | ans += pq.top(); 31 | pq.pop(); 32 | } 33 | return ans; 34 | } 35 | }; -------------------------------------------------------------------------------- /2563. Count the Number of Fair Pairs/README.md: -------------------------------------------------------------------------------- 1 | # Count the Number of Fair Pairs 2 | ## Description 3 | 4 | Given a 0-indexed integer array nums of size n and two integers lower and upper, return the number of fair pairs. 5 | 6 | A pair (i, j) is fair if: 7 | 8 | - 0 <= i < j < n, and 9 | - lower <= nums[i] + nums[j] <= upper 10 | 11 | Example 1: 12 | 13 | ``` 14 | Input: nums = [0,1,7,4,4,5], lower = 3, upper = 6 15 | Output: 6 16 | Explanation: There are 6 fair pairs: (0,3), (0,4), (0,5), (1,3), (1,4), and (1,5). 17 | ``` 18 | 19 | Example 2: 20 | 21 | ``` 22 | Input: nums = [1,7,9,2,5], lower = 11, upper = 11 23 | Output: 1 24 | Explanation: There is a single fair pair: (2,3). 25 | ``` -------------------------------------------------------------------------------- /257. Binary Tree Paths/README.md: -------------------------------------------------------------------------------- 1 | # Binary Tree Paths 2 | ## Description 3 | 4 | Given the root of a binary tree, return all root-to-leaf paths in any order. 5 | 6 | A leaf is a node with no children. 7 | 8 | Example 1: 9 | 10 | ![alt](https://assets.leetcode.com/uploads/2021/03/12/paths-tree.jpg) 11 | ``` 12 | Input: root = [1,2,3,null,5] 13 | Output: ["1->2->5","1->3"] 14 | ``` 15 | 16 | Example 2: 17 | 18 | ![alt](https://assets.leetcode.com/uploads/2021/03/09/reorder2-linked-list.jpg) 19 | ``` 20 | Input: root = [1] 21 | Output: ["1"] 22 | ``` -------------------------------------------------------------------------------- /258. Add Digits/README.md: -------------------------------------------------------------------------------- 1 | # Add Digits 2 | 3 | ## Description 4 | Given an integer num, repeatedly add all its digits until the result has only one digit, and return it. 5 | 6 | Example 1: 7 | 8 | ``` 9 | Input: num = 38 10 | Output: 2 11 | Explanation: The process is 12 | 38 --> 3 + 8 --> 11 13 | 11 --> 1 + 1 --> 2 14 | Since 2 has only one digit, return it. 15 | ``` 16 | 17 | Example 2: 18 | 19 | ``` 20 | Input: num = 0 21 | Output: 0 22 | ``` 23 | 24 | Follow up: Could you do it without any loop/recursion in O(1) runtime? -------------------------------------------------------------------------------- /258. Add Digits/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 0 ms 3 | 5.9 MB 4 | 5 | TC : O(N*N) 6 | SC : O(N) 7 | */ 8 | 9 | /** 10 | * @author : SahilK-027 11 | * @brief : Recursive solution 12 | */ 13 | 14 | class Solution { 15 | private: 16 | int solve(int n){ 17 | if(n / 10 <= 0){ 18 | return n; 19 | } 20 | int sum = 0; 21 | while(n > 0){ 22 | sum += n % 10; 23 | n /= 10; 24 | } 25 | return solve(sum); 26 | } 27 | public: 28 | int addDigits(int num) { 29 | return solve(num); 30 | } 31 | }; -------------------------------------------------------------------------------- /258. Add Digits/solution.js: -------------------------------------------------------------------------------- 1 | /* 2 | 76 ms 3 | 43.6 MB 4 | 5 | TC : O(1) 6 | SC : O(1) 7 | */ 8 | 9 | /** 10 | * @author : SahilK-027 11 | * @brief : Math 12 | */ 13 | 14 | /** 15 | * @param {number} num 16 | * @return {number} 17 | */ 18 | var addDigits = function(num) { 19 | if(num==0) return 0; 20 | else if(num%9==0) return 9; 21 | else return num%9; 22 | }; -------------------------------------------------------------------------------- /260. Single Number III/README.md: -------------------------------------------------------------------------------- 1 | # Single Number III 2 | ## Description 3 | 4 | Given an integer array nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. You can return the answer in any order. 5 | 6 | You must write an algorithm that runs in linear runtime complexity and uses only constant extra space. 7 | 8 | Example 1: 9 | 10 | ``` 11 | Input: nums = [1,2,1,3,2,5] 12 | Output: [3,5] 13 | Explanation: [5, 3] is also a valid answer. 14 | ``` 15 | 16 | Example 2: 17 | 18 | ``` 19 | Input: nums = [-1,0] 20 | Output: [-1,0] 21 | ``` 22 | 23 | Example 3: 24 | 25 | ``` 26 | Input: nums = [0,1] 27 | Output: [1,0] 28 | ``` -------------------------------------------------------------------------------- /260. Single Number III/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 11 ms 3 | 10.7 MB 4 | */ 5 | /** 6 | * TC: O(n) 7 | * SC: O(n) 8 | */ 9 | /** 10 | * @author : SahilK-027 11 | * @brief : Hashing 12 | */ 13 | 14 | class Solution { 15 | public: 16 | vector singleNumber(vector& arr) { 17 | if(arr.size() == 2) return arr; 18 | vector ans; 19 | unordered_mapmp; 20 | for(auto i: arr){ 21 | mp[i]++; 22 | } 23 | for(auto i: mp){ 24 | if(i.second == 1){ 25 | ans.push_back(i.first); 26 | } 27 | } 28 | return ans; 29 | } 30 | }; -------------------------------------------------------------------------------- /2620. Counter/README.md: -------------------------------------------------------------------------------- 1 | # Counter 2 | ## Description 3 | 4 | Given an integer n, return a counter function. This counter function initially returns n and then returns 1 more than the previous value every subsequent time it is called (n, n + 1, n + 2, etc). 5 | 6 | 7 | Example 1: 8 | 9 | ``` 10 | Input: 11 | n = 10 12 | ["call","call","call"] 13 | Output: [10,11,12] 14 | Explanation: 15 | counter() = 10 // The first time counter() is called, it returns n. 16 | counter() = 11 // Returns 1 more than the previous time. 17 | counter() = 12 // Returns 1 more than the previous time. 18 | ``` 19 | 20 | Example 2: 21 | 22 | ``` 23 | Input: 24 | n = -2 25 | ["call","call","call","call","call"] 26 | Output: [-2,-1,0,1,2] 27 | Explanation: counter() initially returns -2. Then increases after each sebsequent call. 28 | ``` 29 | -------------------------------------------------------------------------------- /2620. Counter/solution.js: -------------------------------------------------------------------------------- 1 | /* 2 | Runtime : 55 ms 3 | Memory : 41.5 MB 4 | 5 | TC : O(1) 6 | SC : O(1) 7 | */ 8 | 9 | /** 10 | * @author : Sahilk-027 11 | * @brief : Looping 12 | */ 13 | 14 | /** 15 | * @param {number} n 16 | * @return {Function} counter 17 | */ 18 | 19 | var createCounter = function(n) { 20 | let counter = -1; 21 | return function() { 22 | counter++; 23 | return counter + n; 24 | }; 25 | }; 26 | 27 | /** 28 | * const counter = createCounter(10) 29 | * counter() // 10 30 | * counter() // 11 31 | * counter() // 12 32 | */ -------------------------------------------------------------------------------- /263. Ugly Number/README.md: -------------------------------------------------------------------------------- 1 | # Ugly Number 2 | ## Description 3 | 4 | An ugly number is a positive integer whose prime factors are limited to 2, 3, and 5. 5 | 6 | Given an integer n, return true if n is an ugly number. 7 | 8 | Example 1: 9 | 10 | ``` 11 | Input: n = 6 12 | Output: true 13 | Explanation: 6 = 2 × 3 14 | ``` 15 | 16 | Example 2: 17 | 18 | ``` 19 | Input: n = 1 20 | Output: true 21 | Explanation: 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5. 22 | ``` 23 | 24 | Example 3: 25 | 26 | ``` 27 | Input: n = 14 28 | Output: false 29 | Explanation: 14 is not ugly since it includes the prime factor 7. 30 | ``` 31 | -------------------------------------------------------------------------------- /2665. Counter II/solution.js: -------------------------------------------------------------------------------- 1 | /* 2 | 75 ms 3 | 45 MB 4 | */ 5 | 6 | /** 7 | * @author : SahilK-027 8 | * @brief : JavaScript 9 | */ 10 | 11 | /** 12 | * @param {integer} init 13 | * @return { increment: Function, decrement: Function, reset: Function } 14 | */ 15 | var createCounter = function(init) { 16 | let presentCount = init; 17 | 18 | const increment = ()=> { 19 | return ++presentCount; 20 | } 21 | 22 | const decrement = ()=> { 23 | return --presentCount; 24 | } 25 | 26 | const reset = ()=> { 27 | return (presentCount = init); 28 | } 29 | 30 | return { increment, decrement, reset }; 31 | }; 32 | 33 | /** 34 | * const counter = createCounter(5) 35 | * counter.increment(); // 6 36 | * counter.reset(); // 5 37 | * counter.decrement(); // 4 38 | */ -------------------------------------------------------------------------------- /2667. Create Hello World Function/README.md: -------------------------------------------------------------------------------- 1 | # Create Hello World Function 2 | 3 | ## Description 4 | 5 | Write a function createHelloWorld. It should return a new function that always returns "Hello World". 6 | 7 | 8 | Example 1: 9 | 10 | ``` 11 | Input: args = [] 12 | Output: "Hello World" 13 | Explanation: 14 | const f = createHelloWorld(); 15 | f(); // "Hello World" 16 | 17 | The function returned by createHelloWorld should always return "Hello World". 18 | ``` 19 | 20 | Example 2: 21 | 22 | ``` 23 | Input: args = [{},null,42] 24 | Output: "Hello World" 25 | Explanation: 26 | const f = createHelloWorld(); 27 | f({}, null, 42); // "Hello World" 28 | 29 | Any arguments could be passed to the function but it should still always return "Hello World". 30 | ``` -------------------------------------------------------------------------------- /2667. Create Hello World Function/solution.js: -------------------------------------------------------------------------------- 1 | /* 2 | 61 ms 3 | 42.4 MB 4 | */ 5 | 6 | /** 7 | * @author : SahilK-027 8 | * @brief : JavaScript 9 | */ 10 | 11 | /** 12 | * @return {Function} 13 | */ 14 | var createHelloWorld = function() { 15 | return function(...args) { 16 | return "Hello World"; 17 | } 18 | }; 19 | 20 | /** 21 | * const f = createHelloWorld(); 22 | * f(); // "Hello World" 23 | */ -------------------------------------------------------------------------------- /268. Missing Number/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Runtime: 15ms 3 | Memory: 18 MB 4 | 5 | TC : O(N) 6 | SC: O(1) 7 | */ 8 | 9 | /** 10 | * @author: nirupamarajeevan 11 | * 12 | * @brief: Using Bit Manipulation 13 | * 14 | */ 15 | class Solution { 16 | public: 17 | int missingNumber(vector& nums) { 18 | int ans = 0; 19 | // first we perform XOR operation with the numbers in the arrray 20 | // 0 XOR number returns the same number 21 | for(int i = 0; i < nums.size(); i++) { 22 | ans ^= nums[i]; 23 | } 24 | // we perform XOR operation 0 to n 25 | // there would be two occurrences for every number in the array except the missing one 26 | for(int i = 0; i <= nums.size(); i++) { 27 | ans ^= i; 28 | } 29 | return ans; 30 | } 31 | }; 32 | 33 | -------------------------------------------------------------------------------- /27. Remove Element/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 11 ms 3 | 8.8 MB 4 | 5 | TC : O(N) 6 | SC : O(1) 7 | */ 8 | 9 | /** 10 | * @author : SahilK-027 11 | * @brief : Two pointer 12 | */ 13 | 14 | class Solution { 15 | public: 16 | int removeElement(vector& nums, int val) { 17 | int ptr = 0; //0 1 2 18 | int n = nums.size(); //8 19 | while(ptr& dp){ 19 | // Base case 20 | if(n == 0) return 0; 21 | if(dp[n] != -1) return dp[n]; 22 | 23 | // Initial answer 24 | int ans = INT_MAX; 25 | for(int i = 1; i * i <= n; i++){ 26 | // Answer will be minimum of ans, subproblems solution 27 | ans = min(ans, 1 + solveMemoization(n - i * i, dp)); 28 | } 29 | 30 | return dp[n] = ans; 31 | } 32 | public: 33 | int numSquares(int n) { 34 | vector dp(n+1, -1); 35 | return solveMemoization(n, dp); 36 | } 37 | }; -------------------------------------------------------------------------------- /28. Find the Index of the First Occurrence in a String/README.md: -------------------------------------------------------------------------------- 1 | # Find the Index of the First Occurrence in a String 2 | ## Description 3 | 4 | Given two strings needle and haystack, return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 5 | 6 | 7 | 8 | Example 1: 9 | ``` 10 | Input: haystack = "sadbutsad", needle = "sad" 11 | Output: 0 12 | Explanation: "sad" occurs at index 0 and 6. 13 | The first occurrence is at index 0, so we return 0. 14 | ``` 15 | Example 2: 16 | ``` 17 | Input: haystack = "leetcode", needle = "leeto" 18 | Output: -1 19 | Explanation: "leeto" did not occur in "leetcode", so we return -1. 20 | ``` 21 | -------------------------------------------------------------------------------- /283. Move Zeroes/README.md: -------------------------------------------------------------------------------- 1 | # Move Zeroes 2 | ## Description 3 | 4 | Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements. 5 | 6 | Note that you must do this in-place without making a copy of the array. 7 | 8 | Example 1: 9 | 10 | ``` 11 | Input: nums = [0,1,0,3,12] 12 | Output: [1,3,12,0,0] 13 | ``` 14 | 15 | Example 2: 16 | 17 | ``` 18 | Input: nums = [0] 19 | Output: [0] 20 | ``` 21 | 22 | Follow up: Could you minimize the total number of operations done? -------------------------------------------------------------------------------- /283. Move Zeroes/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 59 ms 3 | 19.2 MB 4 | 5 | TC : O(N) 6 | SC : O(1) 7 | */ 8 | 9 | /** 10 | * @author : SahilK-027 11 | * @brief : Iterative + Two Pointer 12 | */ 13 | 14 | class Solution { 15 | public: 16 | void moveZeroes(vector& nums) 17 | { 18 | int n=nums.size(); 19 | int i=0; 20 | for (int j = 0; j < n; j++) // N rounds 21 | { 22 | if(nums[j]!=0) //whenever non zero number is found swap 23 | { 24 | swap(nums[j],nums[i]); 25 | i++; 26 | } 27 | /*else 28 | { 29 | continue; 30 | }*/ 31 | } 32 | } 33 | }; -------------------------------------------------------------------------------- /290. Word Pattern/README.md: -------------------------------------------------------------------------------- 1 | # Word Pattern 2 | ## Description 3 | 4 | Given a pattern and a string s, find if s follows the same pattern. 5 | 6 | Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in s. 7 | 8 | 9 | 10 | Example 1: 11 | 12 | ``` 13 | Input: pattern = "abba", s = "dog cat cat dog" 14 | Output: true 15 | ``` 16 | 17 | Example 2: 18 | 19 | ``` 20 | Input: pattern = "abba", s = "dog cat cat fish" 21 | Output: false 22 | ``` 23 | 24 | Example 3: 25 | ``` 26 | Input: pattern = "aaaa", s = "dog cat cat dog" 27 | Output: false 28 | ``` 29 | -------------------------------------------------------------------------------- /3. Longest Substring Without Repeating Characters/README.md: -------------------------------------------------------------------------------- 1 | # Longest Substring Without Repeating Characters 2 | ## Description 3 | 4 | Given a string s, find the length of the longest 5 | substring without repeating characters. 6 | 7 | 8 | 9 | Example 1: 10 | ``` 11 | Input: s = "abcabcbb" 12 | Output: 3 13 | Explanation: The answer is "abc", with the length of 3. 14 | ``` 15 | Example 2: 16 | ``` 17 | Input: s = "bbbbb" 18 | Output: 1 19 | Explanation: The answer is "b", with the length of 1. 20 | ``` 21 | Example 3: 22 | ``` 23 | Input: s = "pwwkew" 24 | Output: 3 25 | Explanation: The answer is "wke", with the length of 3. 26 | Notice that the answer must be a substring, "pwke" is a subsequence and not a substring. 27 | ``` 28 | -------------------------------------------------------------------------------- /300. Longest Increasing Subsequence/README.md: -------------------------------------------------------------------------------- 1 | # Longest Increasing Subsequence 2 | ## Description 3 | 4 | Given an integer array nums, return the length of the longest strictly increasing 5 | subsequence 6 | 7 | 8 | Example 1: 9 | 10 | ``` 11 | Input: nums = [10,9,2,5,3,7,101,18] 12 | Output: 4 13 | Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4. 14 | ``` 15 | 16 | Example 2: 17 | 18 | ``` 19 | Input: nums = [0,1,0,3,2,3] 20 | Output: 4 21 | ``` 22 | 23 | Example 3: 24 | 25 | ``` 26 | Input: nums = [7,7,7,7,7,7,7] 27 | Output: 1 28 | ``` 29 | 30 | - Follow up: Can you come up with an algorithm that runs in O(n log(n)) time complexity? -------------------------------------------------------------------------------- /301. Remove Invalid Parentheses/README.md: -------------------------------------------------------------------------------- 1 | # Remove Invalid Parentheses 2 | ## Description 3 | 4 | Given a string s that contains parentheses and letters, remove the minimum number of invalid parentheses to make the input string valid. 5 | 6 | Return all the possible results. You may return the answer in any order. 7 | 8 | Example 1: 9 | 10 | ``` 11 | Input: s = "()())()" 12 | Output: ["(())()","()()()"] 13 | ``` 14 | 15 | Example 2: 16 | 17 | ``` 18 | Input: s = "(a)())()" 19 | Output: ["(a())()","(a)()()"] 20 | ``` 21 | 22 | Example 3: 23 | 24 | ``` 25 | Input: s = ")(" 26 | Output: [""] 27 | ``` -------------------------------------------------------------------------------- /309. Best Time to Buy and Sell Stock with Cooldown/README.md: -------------------------------------------------------------------------------- 1 | # Best Time to Buy and Sell Stock with Cooldown 2 | ## Description 3 | 4 | You are given an array prices where prices[i] is the price of a given stock on the ith day. 5 | 6 | Find the maximum profit you can achieve. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the following restrictions: 7 | 8 | - After you sell your stock, you cannot buy stock on the next day (i.e., cooldown one day). 9 | Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again). 10 | 11 | 12 | Example 1: 13 | 14 | ``` 15 | Input: prices = [1,2,3,0,2] 16 | Output: 3 17 | Explanation: transactions = [buy, sell, cooldown, buy, sell] 18 | ``` 19 | 20 | Example 2: 21 | 22 | ``` 23 | Input: prices = [1] 24 | Output: 0 25 | ``` -------------------------------------------------------------------------------- /319. Bulb Switcher/solution.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SahilK-027/LeetCode/a9a7d4dc6b1005b6e58ed27d688740a00fc690fa/319. Bulb Switcher/solution.cpp -------------------------------------------------------------------------------- /322. Coin Change/README.md: -------------------------------------------------------------------------------- 1 | # Coin Change 2 | ## Description 3 | 4 | You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money. 5 | 6 | Return the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1. 7 | 8 | You may assume that you have an infinite number of each kind of coin. 9 | 10 | Example 1: 11 | 12 | ``` 13 | Input: coins = [1,2,5], amount = 11 14 | Output: 3 15 | Explanation: 11 = 5 + 5 + 1 16 | ``` 17 | 18 | Example 2: 19 | 20 | ``` 21 | Input: coins = [2], amount = 3 22 | Output: -1 23 | ``` 24 | 25 | Example 3: 26 | 27 | ``` 28 | Input: coins = [1], amount = 0 29 | Output: 0 30 | ``` -------------------------------------------------------------------------------- /326. Power of Three/README.md: -------------------------------------------------------------------------------- 1 | # Power of Three 2 | ## Description 3 | 4 | Given an integer n, return true if it is a power of three. Otherwise, return false. 5 | 6 | An integer n is a power of three, if there exists an integer x such that n == 3x. 7 | 8 | Example 1: 9 | 10 | ``` 11 | Input: n = 27 12 | Output: true 13 | Explanation: 27 = 33 14 | ``` 15 | 16 | Example 2: 17 | 18 | ``` 19 | Input: n = 0 20 | Output: false 21 | Explanation: There is no x where 3x = 0. 22 | ``` 23 | 24 | Example 3: 25 | 26 | ``` 27 | Input: n = -1 28 | Output: false 29 | Explanation: There is no x where 3x = (-1). 30 | ``` 31 | 32 | Follow up: Could you solve it without loops/recursion? -------------------------------------------------------------------------------- /326. Power of Three/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 32 ms 3 | 5.9 MB 4 | 5 | TC : O(N) 6 | SC : O(N) 7 | */ 8 | 9 | /** 10 | * @author : SahilK-027 11 | * @brief : Recursive solution 12 | */ 13 | 14 | class Solution { 15 | private: 16 | bool solve(int n,long long ans = 1){ 17 | //Base case 18 | if(ans == n){ 19 | return true; 20 | } 21 | //Recursion 22 | if(ans>n){ 23 | return false; 24 | } 25 | return solve(n, 3 * ans); 26 | } 27 | public: 28 | bool isPowerOfThree(int n) { 29 | return solve(n); 30 | } 31 | }; -------------------------------------------------------------------------------- /338. Counting Bits/README.md: -------------------------------------------------------------------------------- 1 | # Counting Bits 2 | ## Description 3 | 4 | Given an integer n, return an array ans of length n + 1 such that for each i (0 <= i <= n), ans[i] is the number of 1's in the binary representation of i. 5 | 6 | Example 1: 7 | 8 | ``` 9 | Input: n = 2 10 | Output: [0,1,1] 11 | Explanation: 12 | 0 --> 0 13 | 1 --> 1 14 | 2 --> 10 15 | ``` 16 | 17 | Example 2: 18 | 19 | ``` 20 | Input: n = 5 21 | Output: [0,1,1,2,1,2] 22 | Explanation: 23 | 0 --> 0 24 | 1 --> 1 25 | 2 --> 10 26 | 3 --> 11 27 | 4 --> 100 28 | 5 --> 101 29 | ``` 30 | 31 | Follow up: 32 | 33 | - It is very easy to come up with a solution with a runtime of O(n log n). Can you do it in linear time O(n) and possibly in a single pass? 34 | - Can you do it without using any built-in function (i.e., like __builtin_popcount in C++)? -------------------------------------------------------------------------------- /338. Counting Bits/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 ms 3 | 7.9 MB 4 | */ 5 | 6 | /** 7 | * TC: O(n) 8 | * SC: O(1) 9 | */ 10 | 11 | /** 12 | * @author : SahilK-027 13 | * @brief : Dynamic Programming 14 | */ 15 | 16 | class Solution { 17 | public: 18 | vector countBits(int n) { 19 | // Build dp 20 | vector dp(n+1, 0); 21 | // Initial round will be 1 22 | // We will be changing round at [1,2,4,8,16,32,64...] 23 | int curr_round = 1; 24 | for(int i = 1 ; i <= n ; i++){ 25 | if(curr_round * 2 == i){ 26 | curr_round = i; 27 | } 28 | dp[i] = dp[i - curr_round] + 1; 29 | } 30 | return dp; 31 | } 32 | }; -------------------------------------------------------------------------------- /338. Counting Bits/solution.js: -------------------------------------------------------------------------------- 1 | /* 2 | 86 ms 3 | 54.1MB 4 | */ 5 | 6 | /** 7 | * TC: O(n log n) 8 | * SC: O(1) 9 | */ 10 | 11 | /** 12 | * @author : SahilK-027 13 | * @brief : iterative 14 | */ 15 | /** 16 | * @param {number} n 17 | * @return {number[]} 18 | */ 19 | var countBits = function(n) { 20 | ans = []; 21 | for(let i = 0 ;i <= n; i++){ 22 | let curr_num = i; 23 | let cnt =0; 24 | while(curr_num){ 25 | let remender = curr_num % 2; 26 | if(remender == 1) cnt++; 27 | curr_num = parseInt(curr_num /2); 28 | } 29 | ans.push(cnt); 30 | } 31 | return ans; 32 | }; -------------------------------------------------------------------------------- /34. Find First and Last Position of Element in Sorted Array/README.md: -------------------------------------------------------------------------------- 1 | # Find First and Last Position of Element in Sorted Array 2 | ## Description 3 | 4 | Given an array of integers nums sorted in non-decreasing order, find the starting and ending position of a given target value. 5 | 6 | If target is not found in the array, return [-1, -1]. 7 | 8 | You must write an algorithm with O(log n) runtime complexity. 9 | 10 | Example 1: 11 | 12 | ``` 13 | Input: nums = [5,7,7,8,8,10], target = 8 14 | Output: [3,4] 15 | ``` 16 | 17 | Example 2: 18 | 19 | ``` 20 | Input: nums = [5,7,7,8,8,10], target = 6 21 | Output: [-1,-1] 22 | ``` 23 | 24 | Example 3: 25 | 26 | ``` 27 | Input: nums = [], target = 0 28 | Output: [-1,-1] 29 | ``` -------------------------------------------------------------------------------- /342. Power of Four/README.md: -------------------------------------------------------------------------------- 1 | # Power of Four 2 | ## Description 3 | 4 | Given an integer n, return true if it is a power of four. Otherwise, return false. 5 | 6 | An integer n is a power of four, if there exists an integer x such that n == 4x. 7 | 8 | Example 1: 9 | 10 | ``` 11 | Input: n = 16 12 | Output: true 13 | ``` 14 | 15 | Example 2: 16 | 17 | ``` 18 | Input: n = 5 19 | Output: false 20 | ``` 21 | 22 | Example 3: 23 | 24 | ``` 25 | Input: n = 1 26 | Output: true 27 | ``` 28 | 29 | Follow up: Could you solve it without loops/recursion? -------------------------------------------------------------------------------- /342. Power of Four/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 0 ms 3 | 5.7 MB 4 | 5 | TC : O(N) 6 | SC : O(N) 7 | */ 8 | 9 | /** 10 | * @author : SahilK-027 11 | * @brief : Recursive solution 12 | */ 13 | 14 | class Solution { 15 | private: 16 | bool solve(int n,long long ans = 1){ 17 | //Base case 18 | if(ans == n){ 19 | return true; 20 | } 21 | //Recursion 22 | if(ans>n){ 23 | return false; 24 | } 25 | return solve(n, 4 * ans); 26 | } 27 | public: 28 | bool isPowerOfFour(int n) { 29 | return solve(n); 30 | } 31 | }; -------------------------------------------------------------------------------- /344. Reverse String/README.md: -------------------------------------------------------------------------------- 1 | # Reverse String 2 | ## Description 3 | 4 | Write a function that reverses a string. The input string is given as an array of characters s. 5 | 6 | You must do this by modifying the input array in-place with O(1) extra memory. 7 | 8 | Example 1: 9 | 10 | ``` 11 | Input: s = ["h","e","l","l","o"] 12 | Output: ["o","l","l","e","h"] 13 | ``` 14 | 15 | Example 2: 16 | 17 | ``` 18 | Input: s = ["H","a","n","n","a","h"] 19 | Output: ["h","a","n","n","a","H"] 20 | ``` -------------------------------------------------------------------------------- /344. Reverse String/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 53 ms 3 | 23.1 MB 4 | 5 | TC : O(N) 6 | SC : O(1) 7 | */ 8 | 9 | /** 10 | * @author : SahilK-027 11 | * @brief : SWAP TWO POINTERS 12 | */ 13 | 14 | class Solution { 15 | public: 16 | void reverseString(vector& s) { 17 | int start=0; 18 | int end=s.size()-1; 19 | while(start<=end){ 20 | swap(s[start++],s[end--]); 21 | } 22 | } 23 | }; -------------------------------------------------------------------------------- /347. Top K Frequent Elements/README.md: -------------------------------------------------------------------------------- 1 | # Top K Frequent Elements 2 | ## Description 3 | 4 | Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order. 5 | 6 | Example 1: 7 | ``` 8 | Input: nums = [1,1,1,2,2,3], k = 2 9 | Output: [1,2] 10 | ``` 11 | Example 2: 12 | ``` 13 | Input: nums = [1], k = 1 14 | Output: [1] 15 | ``` 16 | 17 | Follow up: Your algorithm's time complexity must be better than O(n log n), where n is the array's size. -------------------------------------------------------------------------------- /347. Top K Frequent Elements/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 20 ms 3 | 13.7 MB 4 | 5 | TC : O(N log N) 6 | SC : O(N) 7 | */ 8 | 9 | /** 10 | * @author : SahilK-027 11 | * @brief : Heap + Map 12 | */ 13 | 14 | class Solution { 15 | public: 16 | vector topKFrequent(vector& nums, int k) { 17 | vector ans; 18 | unordered_mapmp; 19 | for(auto i: nums){ 20 | mp[i]++; 21 | } 22 | priority_queue>pq; 23 | for(auto i: mp){ 24 | pq.push({i.second, i.first}); 25 | } 26 | while(k--){ 27 | int num = pq.top().second; 28 | pq.pop(); 29 | ans.push_back(num); 30 | } 31 | return ans; 32 | } 33 | }; -------------------------------------------------------------------------------- /349. Intersection of Two Arrays/README.md: -------------------------------------------------------------------------------- 1 | # Intersection of Two Arrays 2 | 3 | ## Description 4 | 5 | Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must be unique and you may return the result in any order. 6 | 7 | Example 1: 8 | 9 | ``` 10 | Input: nums1 = [1,2,2,1], nums2 = [2,2] 11 | Output: [2] 12 | ``` 13 | Example 2: 14 | 15 | ``` 16 | Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4] 17 | Output: [9,4] 18 | Explanation: [4,9] is also accepted. 19 | ``` 20 | -------------------------------------------------------------------------------- /35. Search Insert Position/README.md: -------------------------------------------------------------------------------- 1 | # Search Insert Position 2 | ## Description 3 | 4 | Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. 5 | 6 | You must write an algorithm with O(log n) runtime complexity. 7 | 8 | 9 | Example 1: 10 | ``` 11 | Input: nums = [1,3,5,6], target = 5 12 | Output: 2 13 | ``` 14 | Example 2: 15 | ``` 16 | Input: nums = [1,3,5,6], target = 2 17 | Output: 1 18 | ``` 19 | Example 3: 20 | ``` 21 | Input: nums = [1,3,5,6], target = 7 22 | Output: 4 23 | ``` 24 | -------------------------------------------------------------------------------- /35. Search Insert Position/solution.js: -------------------------------------------------------------------------------- 1 | /* 2 | 70ms 3 | 42.2 MB 4 | 5 | TC: O(log N) 6 | SC: O(1) 7 | 8 | @author: SahilK-027 9 | @breif: Binary search 10 | 11 | */ 12 | /** 13 | * @param {number[]} nums 14 | * @param {number} target 15 | * @return {number} 16 | */ 17 | var searchInsert = function(arr, t) { 18 | let s = 0; 19 | let e = arr.length-1; 20 | let m = parseInt(s + (e-s) / 2); 21 | console.log(m); 22 | while(s <= e){ 23 | if(arr[m] === t ){ 24 | return m; 25 | } 26 | else if(arr[m] < t){ 27 | s = m + 1; 28 | } 29 | else{ 30 | e = m - 1; 31 | } 32 | m = parseInt(s + (e-s) / 2); 33 | } 34 | return s; 35 | }; 36 | -------------------------------------------------------------------------------- /350. Intersection of Two Arrays II/README.md: -------------------------------------------------------------------------------- 1 | # Arranging Coins 2 | 3 | ## Description 4 | 5 | You have n coins and you want to build a staircase with these coins. The staircase consists of k rows where the ith row has exactly i coins. The last row of the staircase may be incomplete. 6 | 7 | Given the integer n, return the number of complete rows of the staircase you will build. 8 | 9 | Example 1: 10 | 11 | ![alt](https://assets.leetcode.com/uploads/2021/04/09/arrangecoins1-grid.jpg) 12 | ``` 13 | Input: n = 5 14 | Output: 2 15 | Explanation: Because the 3rd row is incomplete, we return 2. 16 | ``` 17 | Example 2: 18 | 19 | ![alt](https://assets.leetcode.com/uploads/2021/04/09/arrangecoins2-grid.jpg) 20 | ``` 21 | Input: n = 8 22 | Output: 3 23 | Explanation: Because the 4th row is incomplete, we return 3. 24 | ``` -------------------------------------------------------------------------------- /354. Russian Doll Envelopes/README.md: -------------------------------------------------------------------------------- 1 | # Russian Doll Envelopes 2 | ## Description 3 | 4 | You are given a 2D array of integers envelopes where envelopes[i] = [wi, hi] represents the width and the height of an envelope. 5 | 6 | One envelope can fit into another if and only if both the width and height of one envelope are greater than the other envelope's width and height. 7 | 8 | Return the maximum number of envelopes you can Russian doll (i.e., put one inside the other). 9 | 10 | Note: You cannot rotate an envelope. 11 | 12 | 13 | Example 1: 14 | 15 | ``` 16 | Input: envelopes = [[5,4],[6,4],[6,7],[2,3]] 17 | Output: 3 18 | Explanation: The maximum number of envelopes you can Russian doll is 3 ([2,3] => [5,4] => [6,7]). 19 | ``` 20 | 21 | Example 2: 22 | 23 | ``` 24 | Input: envelopes = [[1,1],[1,1],[1,1]] 25 | Output: 1 26 | ``` -------------------------------------------------------------------------------- /367. Valid Perfect Square/README.md: -------------------------------------------------------------------------------- 1 | # Valid Perfect Square 2 | ## Description 3 | 4 | Given a positive integer num, write a function which returns True if num is a perfect square else False. 5 | 6 | 7 | Example 1: 8 | 9 | ``` 10 | Input: num = 16 11 | Output: true 12 | ``` 13 | 14 | Example 2: 15 | 16 | ``` 17 | Input: num = 16 18 | Output: true 19 | ``` 20 | 21 | Follow up: Do not use any built-in library function such as sqrt. 22 | -------------------------------------------------------------------------------- /367. Valid Perfect Square/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 4 ms 3 | 5.9 MB 4 | 5 | TC : O(log N) 6 | SC : O(1) 7 | */ 8 | 9 | /** 10 | * @author : SahilK-027 11 | * @brief : Binary search 12 | */ 13 | 14 | class Solution { 15 | public: 16 | bool isPerfectSquare(int num) { 17 | int s = 0; 18 | int e = num; 19 | long int m = s + (e-s)/2; 20 | while(s<=e){ 21 | if(m * m == num){ 22 | return true; 23 | } 24 | else if ( m * m > num){ 25 | e = m -1; 26 | } 27 | else{ 28 | s = m + 1; 29 | } 30 | m = s + (e-s)/2; 31 | } 32 | return false; 33 | } 34 | }; -------------------------------------------------------------------------------- /383. Ransom Note/README.md: -------------------------------------------------------------------------------- 1 | # Ransom Note 2 | ## Description 3 | 4 | Given two strings ransomNote and magazine, return true if ransomNote can be constructed by using the letters from magazine and false otherwise. 5 | 6 | Each letter in magazine can only be used once in ransomNote. 7 | 8 | 9 | Example 1: 10 | 11 | ``` 12 | Input: ransomNote = "a", magazine = "b" 13 | Output: false 14 | ``` 15 | 16 | Example 2: 17 | 18 | ``` 19 | Input: ransomNote = "aa", magazine = "ab" 20 | Output: false 21 | ``` 22 | 23 | Example 3: 24 | 25 | ``` 26 | Input: ransomNote = "aa", magazine = "aab" 27 | Output: true 28 | ``` 29 | -------------------------------------------------------------------------------- /383. Ransom Note/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 38 ms 3 | 8.8 MB 4 | 5 | TC : O(N) 6 | SC : O(N) 7 | */ 8 | 9 | /** 10 | * @author : SahilK-027 11 | * @brief : Counting + Hashing 12 | */ 13 | 14 | class Solution { 15 | public: 16 | bool canConstruct(string ransomNote, string magazine) 17 | { 18 | array a1={0}; 19 | array a2={0}; 20 | for(int i=0;i= 109, and you want to check one by one to see if t has its subsequence. In this scenario, how would you change your code? -------------------------------------------------------------------------------- /392. Is Subsequence/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 136 ms 3 | 276.8 MB 4 | 5 | TC : O(N) 6 | SC : O(1) 7 | */ 8 | 9 | /** 10 | * @author : SahilK-027 11 | * @brief : Two pointer approach 12 | */ 13 | 14 | class Solution { 15 | public: 16 | bool isSubsequence(string s, string t) { 17 | int i = 0; 18 | int j = 0; 19 | while(j> permute(vector& nums) { 17 | sort(nums.begin(),nums.end()); 18 | vector> ans; 19 | do{ 20 | ans.push_back(nums); 21 | }while(next_permutation(nums.begin(),nums.end())); 22 | return ans; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /48. Rotate Image/README.md: -------------------------------------------------------------------------------- 1 | # Rotate Image 2 | ## Description 3 | 4 | You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise). 5 | 6 | You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation. 7 | 8 | 9 | Example 1: 10 | 11 | ![alt](https://assets.leetcode.com/uploads/2020/08/28/mat1.jpg) 12 | ``` 13 | Input: matrix = [[1,2,3],[4,5,6],[7,8,9]] 14 | Output: [[7,4,1],[8,5,2],[9,6,3]] 15 | ``` 16 | Example 2: 17 | 18 | ![alt](https://assets.leetcode.com/uploads/2020/08/28/mat2.jpg) 19 | ``` 20 | Input: matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]] 21 | Output: [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]] 22 | ``` 23 | -------------------------------------------------------------------------------- /49. Group Anagrams/README.md: -------------------------------------------------------------------------------- 1 | # Group Anagrams 2 | ## Description 3 | 4 | Given an array of strings strs, group the anagrams together. You can return the answer in any order. 5 | 6 | An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. 7 | 8 | 9 | Example 1: 10 | 11 | ``` 12 | Input: strs = ["eat","tea","tan","ate","nat","bat"] 13 | Output: [["bat"],["nat","tan"],["ate","eat","tea"]] 14 | ``` 15 | 16 | Example 2: 17 | 18 | ``` 19 | Input: strs = [""] 20 | Output: [[""]] 21 | ``` 22 | 23 | Example 3: 24 | 25 | ``` 26 | Input: strs = ["a"] 27 | Output: [["a"]] 28 | ``` 29 | -------------------------------------------------------------------------------- /491. Non-decreasing Subsequences/README.md: -------------------------------------------------------------------------------- 1 | # Non-decreasing Subsequences 2 | ## Description 3 | 4 | Given an integer array nums, return all the different possible non-decreasing subsequences of the given array with at least two elements. You may return the answer in any order. 5 | 6 | Example 1: 7 | 8 | ``` 9 | Input: nums = [4,6,7,7] 10 | Output: [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]] 11 | ``` 12 | 13 | Example 2: 14 | 15 | ``` 16 | Input: nums = [4,4,3,2,1] 17 | Output: [[4,4]] 18 | ``` -------------------------------------------------------------------------------- /5. Longest Palindromic Substring/README.md: -------------------------------------------------------------------------------- 1 | # Longest Palindromic Substring 2 | 3 | ## Description 4 | 5 | Given a string s, return the longest 6 | palindromic substring in s. 7 | 8 | Example 1: 9 | 10 | ``` 11 | Input: s = "babad" 12 | Output: "bab" 13 | Explanation: "aba" is also a valid answer. 14 | ``` 15 | 16 | Example 2: 17 | 18 | ``` 19 | Input: s = "cbbd" 20 | Output: "bb" 21 | ``` -------------------------------------------------------------------------------- /50. Pow(x, n)/README.md: -------------------------------------------------------------------------------- 1 | # Pow(x, n) 2 | ## Description 3 | 4 | Implement pow(x, n), which calculates x raised to the power n (i.e., xn). 5 | 6 | 7 | 8 | Example 1: 9 | ``` 10 | Input: x = 2.00000, n = 10 11 | Output: 1024.00000 12 | ``` 13 | Example 2: 14 | ``` 15 | Input: x = 2.10000, n = 3 16 | Output: 9.26100 17 | ``` 18 | Example 3: 19 | ``` 20 | Input: x = 2.00000, n = -2 21 | Output: 0.25000 22 | Explanation: 2-2 = 1/22 = 1/4 = 0.25 23 | ``` 24 | -------------------------------------------------------------------------------- /508. Most Frequent Subtree Sum/README.md: -------------------------------------------------------------------------------- 1 | # Most Frequent Subtree Sum 2 | ## Description 3 | 4 | Given the root of a binary tree, return the most frequent subtree sum. If there is a tie, return all the values with the highest frequency in any order. 5 | 6 | The subtree sum of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself). 7 | 8 | Example 1: 9 | 10 | ![alt](https://assets.leetcode.com/uploads/2021/04/24/freq1-tree.jpg) 11 | ``` 12 | Input: root = [5,2,-3] 13 | Output: [2,-3,4] 14 | ``` 15 | 16 | Example 2: 17 | 18 | ![alt](https://assets.leetcode.com/uploads/2021/04/24/freq2-tree.jpg) 19 | ``` 20 | Input: root = [5,2,-5] 21 | Output: [2] 22 | ``` -------------------------------------------------------------------------------- /509. Fibonacci Number/README.md: -------------------------------------------------------------------------------- 1 | # Fibonacci Number 2 | ## Description 3 | 4 | The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is, 5 | 6 | ``` 7 | F(0) = 0, F(1) = 1 8 | F(n) = F(n - 1) + F(n - 2), for n > 1. 9 | ``` 10 | Given n, calculate F(n). 11 | 12 | 13 | Example 1: 14 | 15 | ``` 16 | Input: n = 2 17 | Output: 1 18 | Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1. 19 | ``` 20 | 21 | Example 2: 22 | 23 | ``` 24 | Input: n = 3 25 | Output: 2 26 | Explanation: F(3) = F(2) + F(1) = 1 + 1 = 2. 27 | ``` 28 | 29 | Example 3: 30 | 31 | ``` 32 | Input: n = 4 33 | Output: 3 34 | Explanation: F(4) = F(3) + F(2) = 2 + 1 = 3. 35 | ``` 36 | -------------------------------------------------------------------------------- /509. Fibonacci Number/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 16 ms 3 | 5.9 MB 4 | 5 | TC : O(2^N) 6 | SC : O(N) 7 | */ 8 | 9 | /** 10 | * @author : SahilK-027 11 | * @brief : Recursive solution 12 | */ 13 | 14 | 15 | //Recursiom 16 | class Solution { 17 | public: 18 | int fib(int n) { 19 | if(n==0) 20 | return 0; 21 | if(n==1) 22 | return 1; 23 | int partial_ans = fib(n-1)+fib(n-2); 24 | return partial_ans; 25 | } 26 | }; -------------------------------------------------------------------------------- /509. Fibonacci Number/solution2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 6 ms 3 | 6.2 MB 4 | 5 | TC : O(N) 6 | SC : O(N) 7 | */ 8 | 9 | /** 10 | * @author : SahilK-027 11 | * @brief : Recursive solution + Memoization 12 | */ 13 | 14 | class Solution { 15 | public: 16 | int fibo(int n, vector &arr) { 17 | if(n < 2) 18 | return n; 19 | if(arr[n] != -1) 20 | return arr[n]; 21 | int answer = fibo(n-1, arr) + fibo(n-2, arr); 22 | arr[n] = answer; 23 | return answer; 24 | } 25 | 26 | int fib(int n) { 27 | vector arr(n+1, -1); 28 | return fibo(n, arr); 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /509. Fibonacci Number/solution3.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 0 ms 3 | 6 MB 4 | 5 | TC : O(N) 6 | SC : O(N) 7 | */ 8 | 9 | /** 10 | * @author : SahilK-027 11 | * @brief : Recursive solution + Tabularization 12 | */ 13 | 14 | class Solution { 15 | public: 16 | int solve(int n){ 17 | vector dp(n+1, -1); 18 | dp[0] = 0; 19 | if(n >= 1){ 20 | dp[1] = 1; 21 | } 22 | for(int i = 2 ; i <= n ; i++){ 23 | dp[i] = dp[i-1] + dp[i-2]; 24 | } 25 | return dp[n]; 26 | } 27 | int fib(int n) { 28 | return solve(n); 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /509. Fibonacci Number/solution4.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 0 ms 3 | 5.8 MB 4 | 5 | TC : O(N) 6 | SC : O(1) 7 | */ 8 | 9 | /** 10 | * @author : SahilK-027 11 | * @brief : Tabularization + space optimization 12 | */ 13 | 14 | class Solution { 15 | public: 16 | int solve(int n){ 17 | int prev2 = 0; 18 | int prev1 = 1; 19 | int curr; 20 | for(int i = 2 ; i <= n ; i++){ 21 | curr = prev1 + prev2; 22 | prev2 = prev1; 23 | prev1 = curr; 24 | } 25 | return curr; 26 | } 27 | int fib(int n) { 28 | if(n < 2){ 29 | return n; 30 | } 31 | return solve(n); 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /51. N-Queens/README.md: -------------------------------------------------------------------------------- 1 | # N-Queens 2 | ## Description 3 | 4 | The n-queens puzzle is the problem of placing n queens on an n x n chessboard such that no two queens attack each other. 5 | 6 | Given an integer n, return all distinct solutions to the n-queens puzzle. You may return the answer in any order. 7 | 8 | Each solution contains a distinct board configuration of the n-queens' placement, where 'Q' and '.' both indicate a queen and an empty space, respectively. 9 | 10 | 11 | Example 1: 12 | 13 | ![alt](https://assets.leetcode.com/uploads/2020/11/13/queens.jpg) 14 | ``` 15 | Input: n = 4 16 | Output: [[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]] 17 | Explanation: There exist two distinct solutions to the 4-queens puzzle as shown above 18 | ``` 19 | 20 | Example 2: 21 | 22 | ``` 23 | Input: n = 1 24 | Output: [["Q"]] 25 | ``` -------------------------------------------------------------------------------- /513. Find Bottom Left Tree Value/README.md: -------------------------------------------------------------------------------- 1 | # Find Bottom Left Tree Value 2 | ## Description 3 | 4 | Given the root of a binary tree, return the leftmost value in the last row of the tree. 5 | 6 | Example 1: 7 | 8 | ![alt](https://assets.leetcode.com/uploads/2020/12/14/tree1.jpg) 9 | ``` 10 | Input: root = [2,1,3] 11 | Output: 1 12 | ``` 13 | 14 | Example 2: 15 | 16 | ![alt](https://assets.leetcode.com/uploads/2020/12/14/tree2.jpg) 17 | ``` 18 | Input: root = [1,2,3,4,null,5,6,null,null,7] 19 | Output: 7 20 | ``` -------------------------------------------------------------------------------- /515. Find Largest Value in Each Tree Row/README.md: -------------------------------------------------------------------------------- 1 | # Find Largest Value in Each Tree Row 2 | ## Description 3 | 4 | Given the root of a binary tree, return an array of the largest value in each row of the tree (0-indexed). 5 | 6 | Example 1: 7 | 8 | ![alt](https://assets.leetcode.com/uploads/2020/08/21/largest_e1.jpg) 9 | ``` 10 | Input: root = [1,3,2,5,3,null,9] 11 | Output: [1,3,9] 12 | ``` 13 | 14 | Example 2: 15 | 16 | ``` 17 | Input: root = [1,2,3] 18 | Output: [1,3] 19 | ``` -------------------------------------------------------------------------------- /52. N-Queens II/README.md: -------------------------------------------------------------------------------- 1 | # N-Queens II 2 | ## Description 3 | 4 | The n-queens puzzle is the problem of placing n queens on an n x n chessboard such that no two queens attack each other. 5 | 6 | Given an integer n, return the number of distinct solutions to the n-queens puzzle. 7 | 8 | 9 | Example 1: 10 | 11 | ![alt](https://assets.leetcode.com/uploads/2020/11/13/queens.jpg) 12 | 13 | ``` 14 | Input: n = 4 15 | Output: 2 16 | Explanation: There are two distinct solutions to the 4-queens puzzle as shown. 17 | ``` 18 | 19 | Example 2: 20 | 21 | ``` 22 | Input: n = 1 23 | Output: 1 24 | ``` -------------------------------------------------------------------------------- /520. Detect Capital/README.md: -------------------------------------------------------------------------------- 1 | # Detect Capital 2 | ## Description 3 | 4 | We define the usage of capitals in a word to be right when one of the following cases holds: 5 | 6 | - All letters in this word are capitals, like "USA". 7 | - All letters in this word are not capitals, like "leetcode". 8 | - Only the first letter in this word is capital, like "Google". 9 | Given a string word, return true if the usage of capitals in it is right. 10 | 11 | 12 | 13 | Example 1: 14 | 15 | ``` 16 | Input: word = "USA" 17 | Output: true 18 | ``` 19 | 20 | Example 2: 21 | 22 | ``` 23 | Input: word = "FlaG" 24 | Output: false 25 | ``` -------------------------------------------------------------------------------- /53. Maximum Subarray/README.md: -------------------------------------------------------------------------------- 1 | # Maximum Subarray 2 | ## Description 3 | 4 | Given an integer array nums, find the 5 | subarray which has the largest sum and return its sum. 6 | 7 | 8 | Example 1: 9 | 10 | ``` 11 | Input: nums = [-2,1,-3,4,-1,2,1,-5,4] 12 | Output: 6 13 | Explanation: [4,-1,2,1] has the largest sum = 6. 14 | ``` 15 | 16 | Example 2: 17 | 18 | ``` 19 | Input: nums = [1] 20 | Output: 1 21 | ``` 22 | 23 | Example 3: 24 | 25 | ``` 26 | Input: nums = [5,4,-1,7,8] 27 | Output: 23 28 | ``` 29 | 30 | Follow up: If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle. -------------------------------------------------------------------------------- /53. Maximum Subarray/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 127 ms 3 | 67.7 MB 4 | 5 | TC : O(N) 6 | SC : O(1) 7 | */ 8 | 9 | /** 10 | * @author : SahilK-027 11 | * @brief : Iterative Kadane's Algo 12 | */ 13 | 14 | class Solution { 15 | public: 16 | int maxSubArray(vector& nums) { 17 | // Keep track of current sum and maxSum 18 | int maxi = INT_MIN; 19 | int currSum = 0; 20 | for(auto i: nums){ 21 | // If at any point current Sum is decrasing set it back to 0 22 | if(currSum < 0){ 23 | currSum = 0; 24 | } 25 | // Add current num to sum 26 | currSum += i; 27 | // Update maxi 28 | maxi = max(maxi, currSum); 29 | } 30 | return maxi; 31 | } 32 | }; 33 | -------------------------------------------------------------------------------- /530. Minimum Absolute Difference in BST/README.md: -------------------------------------------------------------------------------- 1 | # Minimum Absolute Difference in BST 2 | 3 | ## Description 4 | 5 | Given the root of a Binary Search Tree (BST), return the minimum absolute difference between the values of any two different nodes in the tree. 6 | 7 | 8 | Example 1: 9 | 10 | ![alt](https://assets.leetcode.com/uploads/2021/02/05/bst1.jpg) 11 | ``` 12 | Input: root = [4,2,6,1,3] 13 | Output: 1 14 | ``` 15 | Example 2: 16 | 17 | ![alt](https://assets.leetcode.com/uploads/2021/02/05/bst2.jpg) 18 | ``` 19 | Input: root = [1,0,48,null,null,12,49] 20 | Output: 1 21 | ``` -------------------------------------------------------------------------------- /54. Spiral Matrix/README.md: -------------------------------------------------------------------------------- 1 | # Spiral Matrix 2 | ## Description 3 | 4 | Given an m x n matrix, return all elements of the matrix in spiral order. 5 | 6 | 7 | Example 1: 8 | 9 | ![alt](https://assets.leetcode.com/uploads/2020/11/13/spiral1.jpg) 10 | ``` 11 | Input: matrix = [[1,2,3],[4,5,6],[7,8,9]] 12 | Output: [1,2,3,6,9,8,7,4,5] 13 | ``` 14 | Example 2: 15 | 16 | ![alt](https://assets.leetcode.com/uploads/2020/11/13/spiral.jpg) 17 | ``` 18 | Input: matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]] 19 | Output: [1,2,3,4,8,12,11,10,9,5,6,7] 20 | ``` 21 | -------------------------------------------------------------------------------- /540. Single Element in a Sorted Array/README.md: -------------------------------------------------------------------------------- 1 | # Single Element in a Sorted Array 2 | ## Description 3 | 4 | You are given a sorted array consisting of only integers where every element appears exactly twice, except for one element which appears exactly once. 5 | 6 | Return the single element that appears only once. 7 | 8 | Your solution must run in O(log n) time and O(1) space. 9 | 10 | Example 1: 11 | 12 | ``` 13 | Input: nums = [1,1,2,3,3,4,4,8,8] 14 | Output: 2 15 | ``` 16 | 17 | Example 2: 18 | 19 | ``` 20 | Input: nums = [3,3,7,7,10,11,11] 21 | Output: 10 22 | ``` -------------------------------------------------------------------------------- /540. Single Element in a Sorted Array/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 49 ms 3 | 27.5 MB 4 | */ 5 | /** 6 | * TC: O(n) 7 | * SC: O(n) 8 | */ 9 | /** 10 | * @author : SahilK-027 11 | * @brief : map 12 | */ 13 | 14 | 15 | class Solution { 16 | public: 17 | int singleNonDuplicate(vector& nums) { 18 | unordered_map mp; 19 | for(auto i: nums){ 20 | mp[i]++; 21 | } 22 | for(auto i: mp){ 23 | if(i.second == 1){ 24 | return i.first; 25 | } 26 | } 27 | return -1; 28 | } 29 | }; -------------------------------------------------------------------------------- /540. Single Element in a Sorted Array/solution.js: -------------------------------------------------------------------------------- 1 | /* 2 | 71 ms 3 | 44.8 MB 4 | */ 5 | /** 6 | * TC: O(n) 7 | * SC: O(1) 8 | */ 9 | /** 10 | * @author : SahilK-027 11 | * @brief : XOR 12 | */ 13 | 14 | /** 15 | * @param {number[]} nums 16 | * @return {number} 17 | */ 18 | var singleNonDuplicate = function(nums) { 19 | let ans = 0; 20 | nums.forEach((i, idx)=>{ 21 | ans ^= i; 22 | }) 23 | return ans; 24 | };; -------------------------------------------------------------------------------- /540. Single Element in a Sorted Array/solution2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 25 ms 3 | 22.4 MB 4 | */ 5 | /** 6 | * TC: O(n) 7 | * SC: O(1) 8 | */ 9 | /** 10 | * @author : SahilK-027 11 | * @brief : XOR 12 | */ 13 | class Solution { 14 | public: 15 | int singleNonDuplicate(vector& nums) { 16 | int ans = 0; 17 | for(auto i: nums){ 18 | ans ^= i; 19 | } 20 | return ans; 21 | } 22 | }; -------------------------------------------------------------------------------- /542. 01 Matrix/README.md: -------------------------------------------------------------------------------- 1 | # 01 Matrix 2 | ## Description 3 | 4 | Given an m x n binary matrix mat, return the distance of the nearest 0 for each cell. 5 | 6 | The distance between two adjacent cells is 1. 7 | 8 | Example 1: 9 | 10 | ![alt](https://assets.leetcode.com/uploads/2021/04/24/01-1-grid.jpg) 11 | ``` 12 | Input: mat = [[0,0,0],[0,1,0],[0,0,0]] 13 | Output: [[0,0,0],[0,1,0],[0,0,0]] 14 | ``` 15 | Example 2: 16 | 17 | ![alt](https://assets.leetcode.com/uploads/2021/04/24/01-2-grid.jpg) 18 | ``` 19 | Input: mat = [[0,0,0],[0,1,0],[1,1,1]] 20 | Output: [[0,0,0],[0,1,0],[1,2,1]] 21 | ``` 22 | 23 | -------------------------------------------------------------------------------- /543. Diameter of Binary Tree/README.md: -------------------------------------------------------------------------------- 1 | # Diameter of Binary Tree 2 | ## Description 3 | 4 | Given the root of a binary tree, return the length of the diameter of the tree. 5 | 6 | The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root. 7 | 8 | The length of a path between two nodes is represented by the number of edges between them. 9 | 10 | Example 1: 11 | 12 | ![alt](https://assets.leetcode.com/uploads/2021/03/06/diamtree.jpg) 13 | ``` 14 | Input: root = [1,2,3,4,5] 15 | Output: 3 16 | Explanation: 3 is the length of the path [4,2,1,3] or [5,2,1,3]. 17 | ``` 18 | 19 | Example 2: 20 | 21 | ``` 22 | Input: root = [1,2] 23 | Output: 1 24 | ``` -------------------------------------------------------------------------------- /55. Jump Game/README.md: -------------------------------------------------------------------------------- 1 | # Jump Game 2 | ## Description 3 | 4 | You are given an integer array nums. You are initially positioned at the array's first index, and each element in the array represents your maximum jump length at that position. 5 | 6 | Return true if you can reach the last index, or false otherwise. 7 | 8 | Example 1: 9 | 10 | ``` 11 | Input: nums = [2,3,1,1,4] 12 | Output: true 13 | Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index. 14 | ``` 15 | 16 | Example 2: 17 | 18 | ``` 19 | Input: nums = [3,2,1,0,4] 20 | Output: false 21 | Explanation: You will always arrive at index 3 no matter what. Its maximum jump length is 0, which makes it impossible to reach the last index. 22 | ``` -------------------------------------------------------------------------------- /557. Reverse Words in a String III/README.md: -------------------------------------------------------------------------------- 1 | # Reverse Words in a String III 2 | ## Description 3 | 4 | Given a string s, reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order. 5 | 6 | Example 1: 7 | 8 | ``` 9 | Input: s = "Let's take LeetCode contest" 10 | Output: "s'teL ekat edoCteeL tsetnoc" 11 | ``` 12 | 13 | Example 2: 14 | 15 | ``` 16 | Input: s = "God Ding" 17 | Output: "doG gniD" 18 | ``` -------------------------------------------------------------------------------- /56. Merge Intervals/README.md: -------------------------------------------------------------------------------- 1 | # Merge Intervals 2 | ## Description 3 | 4 | Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input. 5 | 6 | Example 1: 7 | 8 | ``` 9 | Input: intervals = [[1,3],[2,6],[8,10],[15,18]] 10 | Output: [[1,6],[8,10],[15,18]] 11 | Explanation: Since intervals [1,3] and [2,6] overlap, merge them into [1,6]. 12 | ``` 13 | 14 | Example 2: 15 | 16 | ``` 17 | Input: intervals = [[1,4],[4,5]] 18 | Output: [[1,5]] 19 | Explanation: Intervals [1,4] and [4,5] are considered overlapping. 20 | ``` -------------------------------------------------------------------------------- /56. Merge Intervals/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 40 ms 3 | 19.1 MB 4 | */ 5 | /** 6 | * TC: O(N log N) 7 | * SC: O(N) 8 | */ 9 | /** 10 | * @author : SahilK-027 11 | * @brief : Sorting 12 | */ 13 | 14 | 15 | class Solution { 16 | public: 17 | vector> merge(vector>& intervals) { 18 | sort(intervals.begin(), intervals.end()); 19 | vector> ans; 20 | ans.push_back(intervals[0]); 21 | int curr = 0; 22 | for(int i = 1; i < intervals.size(); ++i){ 23 | curr = ans.size()-1; 24 | if(ans[curr][1] >= intervals[i][0]){ 25 | ans[curr][1] = max(ans[curr][1], intervals[i][1]); 26 | } 27 | else{ 28 | ans.push_back(intervals[i]); 29 | } 30 | } 31 | return ans; 32 | } 33 | }; -------------------------------------------------------------------------------- /567. Permutation in String/README.md: -------------------------------------------------------------------------------- 1 | # Permutation in String 2 | ## Description 3 | 4 | Given two strings s1 and s2, return true if s2 contains a permutation of s1, or false otherwise. 5 | 6 | In other words, return true if one of s1's permutations is the substring of s2. 7 | 8 | Example 1: 9 | 10 | ``` 11 | Input: s1 = "ab", s2 = "eidbaooo" 12 | Output: true 13 | Explanation: s2 contains one permutation of s1 ("ba"). 14 | ``` 15 | 16 | Example 2: 17 | 18 | ``` 19 | Input: s1 = "ab", s2 = "eidboaoo" 20 | Output: false 21 | ``` -------------------------------------------------------------------------------- /58. Length of Last Word/README.md: -------------------------------------------------------------------------------- 1 | # Length of Last Word 2 | ## Description 3 | 4 | Given a string s consisting of words and spaces, return the length of the last word in the string. 5 | 6 | A word is a maximal 7 | substring 8 | consisting of non-space characters only. 9 | 10 | 11 | 12 | Example 1: 13 | 14 | ``` 15 | Input: s = "Hello World" 16 | Output: 5 17 | Explanation: The last word is "World" with length 5. 18 | ``` 19 | 20 | Example 2: 21 | 22 | ``` 23 | Input: s = " fly me to the moon " 24 | Output: 4 25 | Explanation: The last word is "moon" with length 4. 26 | ``` 27 | 28 | Example 3: 29 | 30 | ``` 31 | Input: s = "luffy is still joyboy" 32 | Output: 6 33 | Explanation: The last word is "joyboy" with length 6. 34 | ``` 35 | 36 | - All the values in score are unique. -------------------------------------------------------------------------------- /59. Spiral Matrix II/README.md: -------------------------------------------------------------------------------- 1 | # Spiral Matrix II 2 | ## Description 3 | 4 | Given a positive integer n, generate an n x n matrix filled with elements from 1 to n2 in spiral order. 5 | 6 | 7 | 8 | Example 1: 9 | 10 | ![alt](https://assets.leetcode.com/uploads/2020/11/13/spiraln.jpg) 11 | ``` 12 | Input: n = 3 13 | Output: [[1,2,3],[8,9,4],[7,6,5]] 14 | ``` 15 | Example 2: 16 | ``` 17 | Input: n = 1 18 | Output: [[1]] 19 | ``` 20 | -------------------------------------------------------------------------------- /593. Valid Square/README.md: -------------------------------------------------------------------------------- 1 | # Valid Square 2 | ## Description 3 | 4 | Given the coordinates of four points in 2D space p1, p2, p3 and p4, return true if the four points construct a square. 5 | 6 | The coordinate of a point pi is represented as [xi, yi]. The input is not given in any order. 7 | 8 | A valid square has four equal sides with positive length and four equal angles (90-degree angles). 9 | 10 | Example 1: 11 | 12 | ``` 13 | Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1] 14 | Output: true 15 | ``` 16 | 17 | Example 2: 18 | 19 | ``` 20 | Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,12] 21 | Output: false 22 | ``` 23 | 24 | Example 3: 25 | 26 | ``` 27 | Input: p1 = [1,0], p2 = [-1,0], p3 = [0,1], p4 = [0,-1] 28 | Output: true 29 | ``` -------------------------------------------------------------------------------- /61. Rotate List/README.md: -------------------------------------------------------------------------------- 1 | # Rotate List 2 | ## Description 3 | 4 | Given the head of a linked list, rotate the list to the right by k places. 5 | 6 | 7 | 8 | Example 1: 9 | 10 | ![alt](https://assets.leetcode.com/uploads/2020/11/13/rotate1.jpg) 11 | ``` 12 | Input: head = [1,2,3,4,5], k = 2 13 | Output: [4,5,1,2,3] 14 | ``` 15 | Example 2: 16 | 17 | ![alt](https://assets.leetcode.com/uploads/2020/11/13/roate2.jpg) 18 | ``` 19 | Input: head = [0,1,2], k = 4 20 | Output: [2,0,1] 21 | ``` 22 | -------------------------------------------------------------------------------- /632. Smallest Range Covering Elements from K Lists/README.md: -------------------------------------------------------------------------------- 1 | # Smallest Range Covering Elements from K Lists 2 | ## Description 3 | 4 | You have k lists of sorted integers in non-decreasing order. Find the smallest range that includes at least one number from each of the k lists. 5 | 6 | We define the range [a, b] is smaller than range [c, d] if b - a < d - c or a < c if b - a == d - c. 7 | 8 | 9 | 10 | Example 1: 11 | 12 | ``` 13 | Input: nums = [[4,10,15,24,26],[0,9,12,20],[5,18,22,30]] 14 | Output: [20,24] 15 | Explanation: 16 | List 1: [4, 10, 15, 24,26], 24 is in range [20,24]. 17 | List 2: [0, 9, 12, 20], 20 is in range [20,24]. 18 | List 3: [5, 18, 22, 30], 22 is in range [20,24]. 19 | ``` 20 | 21 | Example 2: 22 | 23 | ``` 24 | Input: nums = [[1,2,3],[1,2,3],[1,2,3]] 25 | Output: [1,1] 26 | ``` -------------------------------------------------------------------------------- /633. Sum of Square Numbers/README.md: -------------------------------------------------------------------------------- 1 | # Sum of Square Numbers 2 | 3 | ## Description 4 | 5 | Given a non-negative integer c, decide whether there're two integers a and b such that a2 + b2 = c. 6 | 7 | Example 1: 8 | 9 | ``` 10 | Input: c = 5 11 | Output: true 12 | Explanation: 1 * 1 + 2 * 2 = 5 13 | ``` 14 | 15 | Example 2: 16 | 17 | ``` 18 | Input: c = 3 19 | Output: false 20 | ``` -------------------------------------------------------------------------------- /633. Sum of Square Numbers/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 0 ms 3 | 5.8 MB 4 | */ 5 | 6 | /** 7 | * TC: O(sqrt(n)) 8 | * SC: O(1) 9 | */ 10 | 11 | /** 12 | * @author : SahilK-027 13 | * @brief : Two pointers 14 | */ 15 | 16 | class Solution { 17 | public: 18 | bool judgeSquareSum(int c) { 19 | long long i = 0; 20 | long long j = sqrt(c); 21 | while(i <= j){ 22 | // If sum is equal to c return true 23 | if(i * i + j * j == c){ 24 | return true; 25 | } 26 | // If value is greater then decrement j 27 | if(i * i + j * j > c){ 28 | j--; 29 | } 30 | // Less value so increment i 31 | else { 32 | i++; 33 | } 34 | } 35 | // Unable to find output 36 | return false; 37 | } 38 | }; -------------------------------------------------------------------------------- /637. Average of Levels in Binary Tree/README.md: -------------------------------------------------------------------------------- 1 | # Average of Levels in Binary Tree 2 | ## Description 3 | 4 | Given the root of a binary tree, return the average value of the nodes on each level in the form of an array. Answers within 10-5 of the actual answer will be accepted. 5 | 6 | Example 1: 7 | 8 | ![alt](https://assets.leetcode.com/uploads/2021/03/09/avg1-tree.jpg) 9 | ``` 10 | Input: root = [3,9,20,null,null,15,7] 11 | Output: [3.00000,14.50000,11.00000] 12 | Explanation: The average value of nodes on level 0 is 3, on level 1 is 14.5, and on level 2 is 11. 13 | Hence return [3, 14.5, 11]. 14 | ``` 15 | 16 | 17 | Example 2: 18 | 19 | ![alt](https://assets.leetcode.com/uploads/2021/03/09/avg2-tree.jpg) 20 | ``` 21 | Input: root = [3,9,20,15,7] 22 | Output: [3.00000,14.50000,11.00000] 23 | ``` -------------------------------------------------------------------------------- /64. Minimum Path Sum/README.md: -------------------------------------------------------------------------------- 1 | # Minimum Path Sum 2 | ## Description 3 | 4 | Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right, which minimizes the sum of all numbers along its path. 5 | 6 | Note: You can only move either down or right at any point in time. 7 | 8 | 9 | Example 1: 10 | 11 | ![alt](https://assets.leetcode.com/uploads/2020/11/05/minpath.jpg) 12 | ``` 13 | Input: grid = [[1,3,1],[1,5,1],[4,2,1]] 14 | Output: 7 15 | Explanation: Because the path 1 → 3 → 1 → 1 → 1 minimizes the sum. 16 | ``` 17 | 18 | Example 2: 19 | 20 | ``` 21 | Input: grid = [[1,2,3],[4,5,6]] 22 | Output: 12 23 | ``` -------------------------------------------------------------------------------- /653. Two Sum IV - Input is a BST/README.md: -------------------------------------------------------------------------------- 1 | # Two Sum IV - Input is a BST 2 | ## Description 3 | 4 | Given the root of a Binary Search Tree and a target number k, return true if there exist two elements in the BST such that their sum is equal to the given target. 5 | 6 | Example 1: 7 | 8 | ![alt](https://assets.leetcode.com/uploads/2020/09/21/sum_tree_1.jpg) 9 | ``` 10 | Input: root = [5,3,6,2,4,null,7], k = 9 11 | Output: true 12 | ``` 13 | 14 | Example 2: 15 | 16 | ![alt](https://assets.leetcode.com/uploads/2020/09/21/sum_tree_2.jpg) 17 | ``` 18 | Input: root = [5,3,6,2,4,null,7], k = 28 19 | Output: false 20 | ``` -------------------------------------------------------------------------------- /67. Add Binary/README.md: -------------------------------------------------------------------------------- 1 | # Add Binary 2 | ## Description 3 | 4 | Given two binary strings a and b, return their sum as a binary string. 5 | 6 | Example 1: 7 | 8 | ``` 9 | Input: a = "11", b = "1" 10 | Output: "100" 11 | ``` 12 | 13 | Example 2: 14 | 15 | ``` 16 | Input: a = "1010", b = "1011" 17 | Output: "10101" 18 | ``` -------------------------------------------------------------------------------- /670. Maximum Swap/README.md: -------------------------------------------------------------------------------- 1 | # Maximum Swap 2 | ## Description 3 | 4 | You are given an integer num. You can swap two digits at most once to get the maximum valued number. 5 | 6 | Return the maximum valued number you can get. 7 | 8 | 9 | Example 1: 10 | 11 | ``` 12 | Input: num = 2736 13 | Output: 7236 14 | Explanation: Swap the number 2 and the number 7. 15 | ``` 16 | 17 | Example 2: 18 | 19 | ``` 20 | Input: num = 9973 21 | Output: 9973 22 | Explanation: No swap. 23 | ``` -------------------------------------------------------------------------------- /69. Sqrt(x)/README.md: -------------------------------------------------------------------------------- 1 | # Sqrt(x) 2 | ## Description 3 | 4 | Given a non-negative integer x, compute and return the square root of x. 5 | 6 | Since the return type is an integer, the decimal digits are truncated, and only the integer part of the result is returned. 7 | 8 | Note: You are not allowed to use any built-in exponent function or operator, such as pow(x, 0.5) or x ** 0.5. 9 | 10 | Example 1: 11 | ``` 12 | Input: x = 4 13 | Output: 2 14 | ``` 15 | Example 2: 16 | ``` 17 | Input: x = 8 18 | Output: 2 19 | Explanation: The square root of 8 is 2.82842..., and since the decimal part is truncated, 2 is returned. 20 | ``` 21 | -------------------------------------------------------------------------------- /7. Reverse Integer/README.md: -------------------------------------------------------------------------------- 1 | # Reverse Integer 2 | ## Description 3 | 4 | Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0. 5 | 6 | ***Assume the environment does not allow you to store 64-bit integers (signed or unsigned).*** 7 | 8 | 9 | 10 | Example 1: 11 | ``` 12 | Input: x = 123 13 | Output: 321 14 | ``` 15 | Example 2: 16 | ``` 17 | Input: x = -123 18 | Output: -321 19 | ``` 20 | Example 3: 21 | ``` 22 | Input: x = 120 23 | Output: 21 24 | ``` 25 | -------------------------------------------------------------------------------- /7. Reverse Integer/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 ms 3 | 5.7 MB 4 | 5 | TC : O(N) 6 | SC : O(1) 7 | */ 8 | 9 | /** 10 | * @author : SahilK-027 11 | * @brief : Iterative solution 12 | */ 13 | class Solution { 14 | public: 15 | int reverse(int x) { 16 | int l_digit; 17 | long long ans = 0; 18 | while(x!=0){ 19 | l_digit = x%10; 20 | ans = ans * 10 + l_digit; 21 | x = x / 10; 22 | } 23 | // Checking bounds 24 | if(ans >= -2147483648 && ans <= 2147483647){ 25 | return ans; 26 | } 27 | return 0; 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /70. Climbing Stairs/README.md: -------------------------------------------------------------------------------- 1 | # Climbing Stairs 2 | ## Description 3 | 4 | You are climbing a staircase. It takes n steps to reach the top. 5 | 6 | Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? 7 | 8 | Example 1: 9 | 10 | ``` 11 | Input: n = 2 12 | Output: 2 13 | Explanation: There are two ways to climb to the top. 14 | 1. 1 step + 1 step 15 | 2. 2 steps 16 | ``` 17 | 18 | Example 2: 19 | 20 | ``` 21 | Input: n = 3 22 | Output: 3 23 | Explanation: There are three ways to climb to the top. 24 | 1. 1 step + 1 step + 1 step 25 | 2. 1 step + 2 steps 26 | 3. 2 steps + 1 step 27 | ``` -------------------------------------------------------------------------------- /700. Search in a Binary Search Tree/README.md: -------------------------------------------------------------------------------- 1 | # Search in a Binary Search Tree 2 | ## Description 3 | 4 | You are given the root of a binary search tree (BST) and an integer val. 5 | 6 | Find the node in the BST that the node's value equals val and return the subtree rooted with that node. If such a node does not exist, return null. 7 | 8 | Example 1: 9 | 10 | ![alt](https://assets.leetcode.com/uploads/2021/01/12/tree1.jpg) 11 | ``` 12 | Input: root = [4,2,7,1,3], val = 2 13 | Output: [2,1,3] 14 | ``` 15 | 16 | Example 2: 17 | 18 | ![alt](https://assets.leetcode.com/uploads/2021/01/12/tree2.jpg) 19 | ``` 20 | Input: root = [4,2,7,1,3], val = 5 21 | Output: [] 22 | ``` -------------------------------------------------------------------------------- /704. Binary Search/README.md: -------------------------------------------------------------------------------- 1 | # Binary Search 2 | ## Description 3 | 4 | Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1. 5 | 6 | You must write an algorithm with O(log n) runtime complexity. 7 | 8 | Example 1: 9 | 10 | ``` 11 | Input: nums = [-1,0,3,5,9,12], target = 9 12 | Output: 4 13 | Explanation: 9 exists in nums and its index is 4 14 | ``` 15 | 16 | Example 2: 17 | 18 | ``` 19 | Input: nums = [-1,0,3,5,9,12], target = 2 20 | Output: -1 21 | Explanation: 2 does not exist in nums so return -1 22 | ``` -------------------------------------------------------------------------------- /704. Binary Search/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 120 ms 3 | 27.4 MB 4 | 5 | TC : O(log N) 6 | SC : O(1) 7 | */ 8 | 9 | /** 10 | * @author : SahilK-027 11 | * @brief : Iterative 12 | */ 13 | 14 | class Solution { 15 | public: 16 | int search(vector& nums, int target) { 17 | int s=0; 18 | int e=nums.size()-1; 19 | int mid=s+(e-s)/2; 20 | while(s<=e){ 21 | if(nums[mid]==target){ 22 | return mid; 23 | } 24 | else if(nums[mid]>target){ 25 | e=mid-1; 26 | } 27 | else{ 28 | s=mid+1; 29 | } 30 | mid=s+(e-s)/2; 31 | } 32 | return -1; 33 | } 34 | }; -------------------------------------------------------------------------------- /71. Simplify Path/solution.js: -------------------------------------------------------------------------------- 1 | /* 2 | Runtime : 64 ms 3 | Memory : 44.4 MB 4 | 5 | TC : O(N) 6 | SC : O(N) 7 | */ 8 | 9 | /** 10 | * @author : Sahilk-027 11 | * @brief : Strings + Stack 12 | */ 13 | 14 | /** 15 | * @param {string} path 16 | * @return {string} 17 | */ 18 | var simplifyPath = function(path) { 19 | const stack = []; 20 | const directories = path.split("/"); 21 | for (const dir of directories) { 22 | if (dir === "." || !dir) { 23 | continue; 24 | } else if (dir === "..") { 25 | if (stack.length > 0) { 26 | stack.pop(); 27 | } 28 | } else { 29 | stack.push(dir); 30 | } 31 | } 32 | return "/" + stack.join("/"); 33 | }; -------------------------------------------------------------------------------- /724. Find Pivot Index/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Runtime : 20 ms 3 | Memory : 31.1 MB 4 | 5 | TC : O(n) 6 | SC : O(1) 7 | */ 8 | 9 | /** 10 | * @author : Shashank Bhosagi 11 | * @brief : Array 12 | */ 13 | 14 | class Solution 15 | { 16 | public: 17 | int pivotIndex(vector &nums) 18 | { 19 | int totalsum = 0; 20 | int leftsum = 0; 21 | 22 | for (auto i : nums) 23 | { 24 | totalsum = totalsum + i; 25 | } 26 | 27 | for (int i = 0; i < nums.size(); i++) 28 | { 29 | if (leftsum == totalsum - nums[i]) 30 | { 31 | return i; 32 | } 33 | leftsum = leftsum + nums[i]; 34 | totalsum = totalsum - nums[i]; 35 | } 36 | 37 | return -1; 38 | } 39 | }; -------------------------------------------------------------------------------- /739. Daily Temperatures/README.md: -------------------------------------------------------------------------------- 1 | # Daily Temperatures 2 | ## Description 3 | 4 | Given an array of integers temperatures represents the daily temperatures, return an array answer such that answer[i] is the number of days you have to wait after the ith day to get a warmer temperature. If there is no future day for which this is possible, keep answer[i] == 0 instead. 5 | 6 | 7 | Example 1: 8 | 9 | ``` 10 | Input: temperatures = [73,74,75,71,69,72,76,73] 11 | Output: [1,1,4,2,1,1,0,0] 12 | ``` 13 | 14 | Example 2: 15 | 16 | ``` 17 | Input: temperatures = [30,40,50,60] 18 | Output: [1,1,1,0] 19 | ``` 20 | 21 | Example 3: 22 | 23 | ``` 24 | Input: temperatures = [30,60,90] 25 | Output: [1,1,0] 26 | ``` -------------------------------------------------------------------------------- /74. Search a 2D Matrix/README.md: -------------------------------------------------------------------------------- 1 | # Search a 2D Matrix 2 | ## Description 3 | 4 | Write an efficient algorithm that searches for a value target in an m x n integer matrix matrix. This matrix has the following properties: 5 | 6 | - Integers in each row are sorted from left to right. 7 | - The first integer of each row is greater than the last integer of the previous row. 8 | 9 | 10 | 11 | Example 1: 12 | 13 | ![alt](https://assets.leetcode.com/uploads/2020/10/05/mat.jpg) 14 | ``` 15 | Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3 16 | Output: true 17 | ``` 18 | Example 2: 19 | 20 | ![alt](https://assets.leetcode.com/uploads/2020/10/05/mat2.jpg) 21 | ``` 22 | Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13 23 | Output: false 24 | ``` 25 | -------------------------------------------------------------------------------- /74. Search a 2D Matrix/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 7 ms 3 | 9.3 MB 4 | 5 | TC : O(N * log N) 6 | SC : O(1) 7 | */ 8 | 9 | /** 10 | * @author : SahilK-027 11 | * @brief : Binary search 12 | */ 13 | 14 | class Solution { 15 | public: 16 | bool searchMatrix(vector>& matrix, int target) { 17 | int row = matrix.size(); 18 | int col = matrix[0].size(); 19 | int s = 0 ; 20 | int e = (row*col) -1 ; 21 | int m = s + (e-s)/2; 22 | while(s<=e) 23 | { 24 | if(matrix[m/col][m%col]==target) 25 | return true; 26 | else if (matrix[m/col][m%col]& letters, char target) { 17 | int s = 0; 18 | int e = letters.size() - 1; 19 | int m = s + (e-s)/2; 20 | char ans = '0'; 21 | while(s <= e){ 22 | if(int(letters[m]) > int(target)){ 23 | ans = letters[m]; 24 | e = m -1; 25 | } 26 | else if(int(letters[m]) <= int(target)){ 27 | s = m + 1; 28 | } 29 | m = s + (e-s)/2; 30 | } 31 | if(ans == '0') return letters[0]; 32 | return ans; 33 | } 34 | }; -------------------------------------------------------------------------------- /746. Min Cost Climbing Stairs/brute-force.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | TLE 3 | 4 | TC : O(2^N) 5 | SC : O(N) 6 | */ 7 | 8 | /** 9 | * @author : SahilK-027 10 | * @brief : Recursion 11 | */ 12 | 13 | // Reciursive solution 14 | class Solution { 15 | private: 16 | int solve(int n, vector& cost){ 17 | if(n < 0 ){ 18 | return 0; 19 | } 20 | if(n == 0 || n == 1){ 21 | return cost[n]; 22 | } 23 | return cost[n] + min(solve(n-1, cost), solve(n-2, cost)); 24 | } 25 | public: 26 | int minCostClimbingStairs(vector& cost) { 27 | int n = cost.size(); 28 | return min(solve(n-1, cost), solve(n-2, cost)); 29 | } 30 | }; -------------------------------------------------------------------------------- /75. Sort Colors/README.md: -------------------------------------------------------------------------------- 1 | # Sort Colors 2 | ## Description 3 | Given an array nums with n objects colored red, white, or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white, and blue. We will use the integers 0, 1, and 2 to represent the color red, white, and blue, respectively. You must solve this problem without using the library's sort function. 4 | 5 | Example 1: 6 | ``` 7 | Input: nums = [2,0,2,1,1,0] 8 | Output: [0,0,1,1,2,2] 9 | ``` 10 | Example 2: 11 | ``` 12 | Input: nums = [2,0,1] 13 | Output: [0,1,2] 14 | ``` 15 | Follow-up: Could you come up with a one-pass algorithm using only constant extra space? 16 | -------------------------------------------------------------------------------- /779. K-th Symbol in Grammar/README.md: -------------------------------------------------------------------------------- 1 | # Determine if String Halves Are Alike 2 | ## Description 3 | 4 | We build a table of n rows (1-indexed). We start by writing 0 in the 1st row. Now in every subsequent row, we look at the previous row and replace each occurrence of 0 with 01, and each occurrence of 1 with 10. 5 | 6 | - For example, for n = 3, the 1st row is 0, the 2nd row is 01, and the 3rd row is 0110. 7 | Given two integer n and k, return the kth (1-indexed) symbol in the nth row of a table of n rows. 8 | 9 | Example 1: 10 | 11 | ``` 12 | Input: n = 1, k = 1 13 | Output: 0 14 | Explanation: row 1: 0 15 | ``` 16 | 17 | Example 2: 18 | 19 | ``` 20 | Input: n = 2, k = 1 21 | Output: 0 22 | Explanation: 23 | row 1: 0 24 | row 2: 01 25 | ``` 26 | 27 | Example 3: 28 | 29 | ``` 30 | Input: n = 2, k = 2 31 | Output: 1 32 | Explanation: 33 | row 1: 0 34 | row 2: 01 35 | ``` 36 | -------------------------------------------------------------------------------- /78. Subsets/README.md: -------------------------------------------------------------------------------- 1 | # Subsets 2 | ## Description 3 | 4 | Given an integer array nums of unique elements, return all possible subsets (the power set). 5 | 6 | The solution set must not contain duplicate subsets. Return the solution in any order. 7 | 8 | 9 | 10 | Example 1: 11 | ``` 12 | Input: nums = [1,2,3] 13 | Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]] 14 | ``` 15 | Example 2: 16 | ``` 17 | Input: nums = [0] 18 | Output: [[],[0]] 19 | ``` 20 | -------------------------------------------------------------------------------- /783. Minimum Distance Between BST Nodes/README.md: -------------------------------------------------------------------------------- 1 | # Minimum Distance Between BST Nodes 2 | ## Description 3 | 4 | Given the root of a Binary Search Tree (BST), return the minimum difference between the values of any two different nodes in the tree. 5 | 6 | Example 1: 7 | 8 | ![alt](https://assets.leetcode.com/uploads/2021/02/05/bst1.jpg) 9 | ``` 10 | Input: root = [4,2,6,1,3] 11 | Output: 1 12 | ``` 13 | 14 | Example 2: 15 | 16 | ![alt](https://assets.leetcode.com/uploads/2021/02/05/bst2.jpg) 17 | ``` 18 | Input: root = [1,0,48,null,null,12,49] 19 | Output: 1 20 | ``` -------------------------------------------------------------------------------- /790. Domino and Tromino Tiling/brute-force.cpp: -------------------------------------------------------------------------------- 1 | // Recursive solution 2 | // TC : O(2^n) 3 | // Recursive Solution 4 | class Solution { 5 | private: 6 | long long solve(int n){ 7 | if(n <= 2){ 8 | return n; 9 | } 10 | if(n == 3){ 11 | return 5; 12 | } 13 | long long ans = (2 * solve(n-1) + solve(n-3)) % 1000000007; 14 | return ans ; 15 | } 16 | public: 17 | int numTilings(int n) { 18 | return solve(n) / 1ll; 19 | } 20 | }; -------------------------------------------------------------------------------- /82. Remove Duplicates from Sorted List II/README.md: -------------------------------------------------------------------------------- 1 | # Remove Duplicates from Sorted List II 2 | ## Description 3 | 4 | Given the head of a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. Return the linked list sorted as well. 5 | 6 | 7 | Example 1: 8 | 9 | ![alt](https://assets.leetcode.com/uploads/2021/01/04/linkedlist1.jpg) 10 | ``` 11 | Input: head = [1,2,3,3,4,4,5] 12 | Output: [1,2,5] 13 | ``` 14 | Example 2: 15 | 16 | ![alt](https://assets.leetcode.com/uploads/2021/01/04/linkedlist2.jpg) 17 | ``` 18 | Input: head = [1,1,1,2,3] 19 | Output: [2,3] 20 | ``` 21 | -------------------------------------------------------------------------------- /83. Remove Duplicates from Sorted List/README.md: -------------------------------------------------------------------------------- 1 | # Remove Duplicates from Sorted List 2 | ## Description 3 | 4 | Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well. 5 | 6 | Example 1: 7 | 8 | ![alty](https://assets.leetcode.com/uploads/2021/01/04/list1.jpg) 9 | ``` 10 | Input: head = [1,1,2] 11 | Output: [1,2] 12 | ``` 13 | Example 2: 14 | 15 | ![alt](https://assets.leetcode.com/uploads/2021/01/04/list2.jpg) 16 | ``` 17 | Input: head = [1,1,2,3,3] 18 | Output: [1,2,3] 19 | ``` 20 | -------------------------------------------------------------------------------- /832. Flipping an Image/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3ms 3 | 8.9 MB 4 | 5 | TC : O(N ^ 2) 6 | SC : O(1) 7 | */ 8 | 9 | /** 10 | * @author : SahilK-027 11 | * @brief : Matrix traversal 12 | */ 13 | 14 | class Solution { 15 | public: 16 | vector> flipAndInvertImage(vector>& image) { 17 | int row,col; 18 | row = col = image.size() ; 19 | for(int i = 0 ; i= 3 7 | - There exists some i with 0 < i < arr.length - 1 such that: 8 | * arr[0] < arr[1] < ... < arr[i - 1] < arr[i] 9 | * arr[i] > arr[i + 1] > ... > arr[arr.length - 1] 10 | Given a mountain array arr, return the index i such that arr[0] < arr[1] < ... < arr[i - 1] < arr[i] > arr[i + 1] > ... > arr[arr.length - 1]. 11 | 12 | You must solve it in O(log(arr.length)) time complexity. 13 | 14 | Example 1: 15 | 16 | ``` 17 | Input: arr = [0,1,0] 18 | Output: 1 19 | ``` 20 | 21 | Example 2: 22 | 23 | ``` 24 | Input: arr = [0,2,1,0] 25 | Output: 1 26 | ``` 27 | 28 | Example 3: 29 | 30 | ``` 31 | Input: arr = [0,10,5,2] 32 | Output: 1 33 | ``` -------------------------------------------------------------------------------- /852. Peak Index in a Mountain Array/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 167 ms 3 | 59.3 MB 4 | 5 | TC : O(log N) 6 | SC : O(1) 7 | */ 8 | 9 | /** 10 | * @author : SahilK-027 11 | * @brief : Binary search 12 | */ 13 | 14 | class Solution { 15 | public: 16 | int peakIndexInMountainArray(vector& arr) { 17 | int start = 0; 18 | int end = arr.size() - 1 ; 19 | int mid = start + (end - start)/2; 20 | while(start& arr, int key) { 17 | sort(arr.begin(), arr.end(), greater()); 18 | int i = 0; 19 | int j = arr.size() - 1; 20 | int ans = 0; 21 | while(i <= j){ 22 | if(arr[i] + arr[j] <= key){ 23 | i++; 24 | j--; 25 | } 26 | else{ 27 | i++; 28 | } 29 | ans++; 30 | } 31 | return ans; 32 | } 33 | }; -------------------------------------------------------------------------------- /9. Palindrome Number/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 16 ms 3 | 5.9 MB 4 | 5 | TC : O(N) 6 | SC : O(1) 7 | */ 8 | 9 | /** 10 | * @author : SahilK-027 11 | * @brief : Using the logic of reverse an integer 12 | */ 13 | 14 | class Solution { 15 | public: 16 | bool isPalindrome(int x) { 17 | long long reverse_num = 0; 18 | int temp=x; 19 | if(x<0) 20 | return false; 21 | while(temp!=0) 22 | { 23 | reverse_num = reverse_num*10+temp % 10; 24 | temp=temp/10; 25 | } 26 | return (reverse_num == x); 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /9. Palindrome Number/solution.js: -------------------------------------------------------------------------------- 1 | /* 2 | 200 ms 3 | 50.4 MB 4 | 5 | TC: O(n) 6 | sc: O(1) 7 | 8 | @author: SahilK-027 9 | @brief: Iterative 10 | */ 11 | 12 | 13 | /** 14 | * @param {number} x 15 | * @return {boolean} 16 | */ 17 | var isPalindrome = function(x) { 18 | if(x < 0) return false; 19 | let reverse_x = 0; 20 | let num = x; 21 | while(num){ 22 | let last_digit = num % 10; 23 | reverse_x = reverse_x * 10 + last_digit; 24 | num = parseInt(num / 10); 25 | } 26 | return (reverse_x === x); 27 | }; 28 | -------------------------------------------------------------------------------- /904. Fruit Into Baskets/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 178 ms 3 | 71.3 MB 4 | */ 5 | 6 | /** 7 | * @author : SahilK-027 8 | * @brief : Hash Map 9 | */ 10 | 11 | class Solution 12 | { 13 | 14 | public: 15 | int totalFruit(vector &fruits) 16 | { 17 | 18 | unordered_map mp; 19 | int i = 0, j = 0, res = 0; 20 | while (j < fruits.size()) 21 | { 22 | mp[fruits[j]]++; 23 | if (mp.size() <= 2) 24 | res = max(res, j - i + 1); 25 | else 26 | { 27 | mp[fruits[i]]--; 28 | if (mp[fruits[i]] == 0) 29 | mp.erase(fruits[i]); 30 | i++; 31 | } 32 | j++; 33 | } 34 | return res; 35 | } 36 | }; -------------------------------------------------------------------------------- /92. Reverse Linked List II/README.md: -------------------------------------------------------------------------------- 1 | # Reverse Linked List II 2 | ## Description 3 | 4 | Given the head of a singly linked list and two integers left and right where left <= right, reverse the nodes of the list from position left to position right, and return the reversed list. 5 | 6 | 7 | Example 1: 8 | 9 | ![alt](https://assets.leetcode.com/uploads/2021/02/19/rev2ex2.jpg) 10 | ``` 11 | Input: head = [1,2,3,4,5], left = 2, right = 4 12 | Output: [1,4,3,2,5] 13 | ``` 14 | Example 2: 15 | ``` 16 | Input: head = [5], left = 1, right = 1 17 | Output: [5] 18 | ``` 19 | Follow up: Could you do it in one pass ? 20 | -------------------------------------------------------------------------------- /921. Minimum Add to Make Parentheses Valid/README.md: -------------------------------------------------------------------------------- 1 | # Minimum Add to Make Parentheses Valid 2 | ## Description 3 | 4 | A parentheses string is valid if and only if: 5 | 6 | - It is the empty string, 7 | - It can be written as AB (A concatenated with B), where A and B are valid strings, or 8 | - It can be written as (A), where A is a valid string. 9 | 10 | You are given a parentheses string s. In one move, you can insert a parenthesis at any position of the string. 11 | 12 | - For example, if s = "()))", you can insert an opening parenthesis to be "(()))" or a closing parenthesis to be "())))". 13 | Return the minimum number of moves required to make s valid. 14 | 15 | Example 1: 16 | 17 | ``` 18 | Input: s = "())" 19 | Output: 1 20 | ``` 21 | 22 | Example 2: 23 | 24 | ``` 25 | Input: s = "(((" 26 | Output: 3 27 | ``` -------------------------------------------------------------------------------- /926. Flip String to Monotone Increasing/README.md: -------------------------------------------------------------------------------- 1 | # Flip String to Monotone Increasing 2 | ## Description 3 | 4 | A binary string is monotone increasing if it consists of some number of 0's (possibly none), followed by some number of 1's (also possibly none). 5 | 6 | You are given a binary string s. You can flip s[i] changing it from 0 to 1 or from 1 to 0. 7 | 8 | Return the minimum number of flips to make s monotone increasing. 9 | 10 | Example 1: 11 | 12 | ``` 13 | Input: s = "00110" 14 | Output: 1 15 | Explanation: We flip the last digit to get 00111. 16 | ``` 17 | 18 | Example 2: 19 | 20 | ``` 21 | Input: s = "010110" 22 | Output: 2 23 | Explanation: We flip to get 011111, or alternatively 000111. 24 | ``` 25 | 26 | Example 4: 27 | 28 | ``` 29 | Input: s = "00011000" 30 | Output: 2 31 | Explanation: We flip to get 00000000. 32 | ``` 33 | -------------------------------------------------------------------------------- /934. Shortest Bridge/README.md: -------------------------------------------------------------------------------- 1 | # Shortest Bridge 2 | 3 | ## Description 4 | 5 | You are given an n x n binary matrix grid where 1 represents land and 0 represents water. 6 | 7 | An island is a 4-directionally connected group of 1's not connected to any other 1's. There are exactly two islands in grid. 8 | 9 | You may change 0's to 1's to connect the two islands to form one island. 10 | 11 | Return the smallest number of 0's you must flip to connect the two islands. 12 | 13 | Example 1: 14 | 15 | ``` 16 | Input: grid = [[0,1],[1,0]] 17 | Output: 1 18 | ``` 19 | 20 | Example 2: 21 | 22 | ``` 23 | Input: grid = [[0,1,0],[0,0,0],[0,0,1]] 24 | Output: 2 25 | ``` 26 | 27 | Example 3: 28 | 29 | ``` 30 | Input: grid = [[1,1,1,1,1],[1,0,0,0,1],[1,0,1,0,1],[1,0,0,0,1],[1,1,1,1,1]] 31 | Output: 1 32 | ``` -------------------------------------------------------------------------------- /938. Range Sum of BST/README.md: -------------------------------------------------------------------------------- 1 | # Range Sum of BST 2 | ## Description 3 | 4 | Given the root node of a binary search tree and two integers low and high, return the sum of values of all nodes with a value in the inclusive range [low, high]. 5 | 6 | 7 | Example 1: 8 | 9 | ![alt](https://assets.leetcode.com/uploads/2020/11/05/bst1.jpg) 10 | ``` 11 | Input: root = [10,5,15,3,7,null,18], low = 7, high = 15 12 | Output: 32 13 | Explanation: Nodes 7, 10, and 15 are in the range [7, 15]. 7 + 10 + 15 = 32. 14 | ``` 15 | 16 | Example 2: 17 | 18 | ![alt](https://assets.leetcode.com/uploads/2020/11/05/bst2.jpg) 19 | ``` 20 | Input: root = [10,5,15,3,7,13,18,1,null,6], low = 6, high = 10 21 | Output: 23 22 | Explanation: Nodes 6, 7, and 10 are in the range [6, 10]. 6 + 7 + 10 = 23. 23 | ``` 24 | -------------------------------------------------------------------------------- /94. Binary Tree Inorder Traversal/README.md: -------------------------------------------------------------------------------- 1 | # Binary Tree Inorder Traversal 2 | ## Description 3 | 4 | Given the root of a binary tree, return the inorder traversal of its nodes' values. 5 | 6 | 7 | Example 1: 8 | 9 | ![alt](https://assets.leetcode.com/uploads/2020/09/15/inorder_1.jpg) 10 | ``` 11 | Input: root = [1,null,2,3] 12 | Output: [1,3,2] 13 | ``` 14 | Example 2: 15 | ``` 16 | Input: root = [] 17 | Output: [] 18 | ``` 19 | Example 3: 20 | ``` 21 | Input: root = [1] 22 | Output: [1] 23 | ``` 24 | Follow up: Recursive solution is trivial, could you do it iteratively? 25 | -------------------------------------------------------------------------------- /946. Validate Stack Sequences/README.md: -------------------------------------------------------------------------------- 1 | # Validate Stack Sequences 2 | ## Description 3 | 4 | Given two integer arrays pushed and popped each with distinct values, return true if this could have been the result of a sequence of push and pop operations on an initially empty stack, or false otherwise. 5 | 6 | 7 | Example 1: 8 | 9 | ``` 10 | Input: pushed = [1,2,3,4,5], popped = [4,5,3,2,1] 11 | Output: true 12 | Explanation: We might do the following sequence: 13 | push(1), push(2), push(3), push(4), 14 | pop() -> 4, 15 | push(5), 16 | pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1 17 | ``` 18 | 19 | Example 2: 20 | 21 | ``` 22 | Input: pushed = [1,2,3,4,5], popped = [4,3,5,1,2] 23 | Output: false 24 | Explanation: 1 cannot be popped before 2. 25 | ``` -------------------------------------------------------------------------------- /96. Unique Binary Search Trees/README.md: -------------------------------------------------------------------------------- 1 | # Unique Binary Search Trees 2 | ## Description 3 | 4 | Given an integer n, return the number of structurally unique BST's (binary search trees) which has exactly n nodes of unique values from 1 to n. 5 | 6 | Example 1: 7 | 8 | ![alt](https://assets.leetcode.com/uploads/2021/01/18/uniquebstn3.jpg) 9 | ``` 10 | Input: n = 3 11 | Output: 5 12 | ``` 13 | 14 | Example 2: 15 | 16 | ``` 17 | Input: n = 1 18 | Output: 1 19 | ``` -------------------------------------------------------------------------------- /96. Unique Binary Search Trees/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 0 ms 3 | 6.1 MB 4 | */ 5 | 6 | /** 7 | * @author : SahilK-027 8 | * @brief : Recursion + DP 9 | */ 10 | 11 | // Catlen number logic 12 | class Solution { 13 | private: 14 | int solve(int n, vector& dp){ 15 | // Base case 16 | if (n <= 1) return 1; 17 | if(dp[n] != -1){ 18 | return dp[n]; 19 | } 20 | unsigned long int res = 0; 21 | for (int i = 0; i < n; i++){ 22 | res += solve(i, dp) * solve(n - i - 1, dp); 23 | } 24 | dp[n] = res; 25 | return res / 1ll; 26 | } 27 | public: 28 | int numTrees(int n) { 29 | vectordp(n+1, -1); 30 | return solve(n, dp); 31 | } 32 | }; -------------------------------------------------------------------------------- /974. Subarray Sums Divisible by K/README.md: -------------------------------------------------------------------------------- 1 | # Subarray Sums Divisible by K 2 | ## Description 3 | 4 | Given an integer array nums and an integer k, return the number of non-empty subarrays that have a sum divisible by k. 5 | 6 | A subarray is a contiguous part of an array. 7 | 8 | Example 1: 9 | 10 | ``` 11 | Input: nums = [4,5,0,-2,-3,1], k = 5 12 | Output: 7 13 | Explanation: There are 7 subarrays with a sum divisible by k = 5: 14 | [4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3] 15 | ``` 16 | 17 | Example 2: 18 | 19 | ``` 20 | Input: nums = [5], k = 9 21 | Output: 0 22 | ``` -------------------------------------------------------------------------------- /977. Squares of a Sorted Array/README.md: -------------------------------------------------------------------------------- 1 | # Squares of a Sorted Array 2 | ## Description 3 | 4 | Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order. 5 | 6 | Example 1: 7 | 8 | ``` 9 | Input: nums = [-4,-1,0,3,10] 10 | Output: [0,1,9,16,100] 11 | Explanation: After squaring, the array becomes [16,1,0,9,100]. 12 | After sorting, it becomes [0,1,9,16,100]. 13 | ``` 14 | 15 | Example 2: 16 | 17 | ``` 18 | Input: nums = [-7,-3,2,3,11] 19 | Output: [4,9,9,49,121] 20 | ``` 21 | 22 | Follow up: Squaring each element and sorting the new array is very trivial, could you find an O(n) solution using a different approach? -------------------------------------------------------------------------------- /977. Squares of a Sorted Array/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 39 ms 3 | 25.9 MB 4 | 5 | TC : O(N) 6 | SC : O(1) 7 | */ 8 | 9 | /** 10 | * @author : SahilK-027 11 | * @brief : Two pointers 12 | */ 13 | 14 | class Solution { 15 | public: 16 | vector sortedSquares(vector& arr) { 17 | int s = 0; 18 | int e = arr.size()-1; 19 | int k = arr.size()-1; 20 | vector ans(k+1); 21 | while(s <= e){ 22 | if(abs(arr[s]) > abs(arr[e])){ 23 | ans[k--] = arr[s]*arr[s]; 24 | s++; 25 | } 26 | else { 27 | ans[k--] = arr[e]*arr[e]; 28 | e--; 29 | } 30 | } 31 | return ans; 32 | } 33 | }; -------------------------------------------------------------------------------- /989. Add to Array-Form of Integer/README.md: -------------------------------------------------------------------------------- 1 | # Add to Array-Form of Integer 2 | ## Description 3 | 4 | The array-form of an integer num is an array representing its digits in left to right order. 5 | 6 | - For example, for num = 1321, the array form is [1,3,2,1]. 7 | Given num, the array-form of an integer, and an integer k, return the array-form of the integer num + k. 8 | 9 | Example 1: 10 | 11 | ``` 12 | Input: num = [1,2,0,0], k = 34 13 | Output: [1,2,3,4] 14 | Explanation: 1200 + 34 = 1234 15 | ``` 16 | 17 | Example 2: 18 | 19 | ``` 20 | Input: num = [2,7,4], k = 181 21 | Output: [4,5,5] 22 | Explanation: 274 + 181 = 455 23 | ``` 24 | 25 | Example 3: 26 | 27 | ``` 28 | Input: num = [2,1,5], k = 806 29 | Output: [1,0,2,1] 30 | Explanation: 215 + 806 = 1021 31 | ``` -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | ## Supported Versions 4 | 5 | Use this section to tell people about which versions of your project are 6 | currently being supported with security updates. 7 | 8 | | Version | Supported | 9 | | ------- | ------------------ | 10 | | 5.1.x | :white_check_mark: | 11 | | 5.0.x | :x: | 12 | | 4.0.x | :white_check_mark: | 13 | | < 4.0 | :x: | 14 | 15 | ## Reporting a Vulnerability 16 | 17 | Use this section to tell people how to report a vulnerability. 18 | 19 | Tell them where to go, how often they can expect to get an update on a 20 | reported vulnerability, what to expect if the vulnerability is accepted or 21 | declined, etc. 22 | --------------------------------------------------------------------------------