├── 69-sqrtx ├── NOTES.md └── 69-sqrtx.cpp ├── 1-two-sum ├── NOTES.md └── 1-two-sum.java ├── 100-same-tree ├── NOTES.md ├── README.md └── 100-same-tree.java ├── 258-add-digits ├── NOTES.md ├── 258-add-digits.java └── README.md ├── 52-n-queens-II ├── NOTES.md └── README.md ├── 78-subsets ├── NOTES.md └── 78-subsets.cpp ├── leedcode └── twosum.cpp ├── 0045-jump-game-ii ├── NOTES.md └── 0045-jump-game-ii.java ├── 0190-reverse-bits ├── NOTES.md ├── 0190-reverse-bits.java └── 0190-reverse-bits.py ├── 0231-power-of-two ├── NOTES.md └── 0231-power-of-two.java ├── 0258-add-digits ├── NOTES.md └── 0258-add-digits.java ├── 0338-counting-bits ├── NOTES.md └── 0338-counting-bits.java ├── 0342-power-of-four ├── NOTES.md └── 0342-power-of-four.java ├── 101-symmetric-tree ├── NOTES.md └── 101-symmetric-tree.cpp ├── 113-path-sum-ii └── NOTES.md ├── 127_word_ladder └── ques.txt ├── 136-single-number ├── NOTES.md ├── 101-symmetric-tree │ ├── NOTES.md │ └── 101-symmetric-tree.cpp └── 136-single-number.java ├── 198-house-robber ├── NOTES.md ├── 198-house-robber.cpp └── house-robber.cpp ├── 27-remove-element ├── NOTES.md ├── 27-remove-element.java └── 27-remove-element.py ├── 283-move-zeroes ├── NOTES.md └── 283-move-zeroes.cpp ├── 338-counting-bits └── NOTES.md ├── 38-count-and-say ├── NOTES.md └── 38-count-and-say.cpp ├── 45-jump-game-ii └── NOTES.md ├── 62-unique-paths └── NOTES.md ├── 64.Minimum Path Sum ├── NOTES.md └── README.md ├── 733-flood-fill └── NOTES.md ├── 75-sort-colors ├── NOTES.md └── 75-sort-colors.java ├── Keys and Rooms ├── notes.md └── code.cpp ├── 0007-reverse-integer ├── NOTES.md └── 0007-reverse-integer.py ├── 0012-integer-to-roman ├── NOTES.md └── 0012-integer-to-roman.java ├── 0053-maximum-subarray ├── NOTES.md └── 0053-maximum-subarray.java ├── 0191-number-of-1-bits ├── NOTES.md └── 0191-number-of-1-bits.java ├── 0326-power-of-three ├── NOTES.md └── 0326-power-of-three.java ├── 118-pascals-triangle ├── NOTES.md ├── 118-pascals-triangle.java └── README.md ├── 13-roman-to-integer ├── NOTES.md ├── roman.cpp └── 13-roman-to-integer.java ├── 130-surrounded-regions ├── NOTES.md └── 130-surrounded-regions.cpp ├── 1302-deepest-leaves-sum ├── NOTES.md └── README.md ├── 20-valid-parentheses ├── NOTES.md └── 20-valid-parentheses.java ├── 217-contains-duplicate ├── NOTES.md └── 217-contains-duplicate.java ├── 264-Ugly-number-ii ├── NOTES.md └── 264-ugly-number-ii.cpp ├── 278-first-bad-version ├── NOTES.md ├── README.md └── 278-first-bad-version.java ├── 58-length-of-last-word ├── NOTES.md ├── 58-length-of-last-word.java └── README.md ├── 73-set-matrix-zeroes └── NOTES.md ├── 739-daily-temperatures └── NOTES.md ├── 74-search-a-2d-matrix ├── NOTES.md └── 74-search-a-2d-matrix.cpp ├── 740-delete-and-earn ├── NOTES.md └── 740-delete-and-earn.cpp ├── 867-transpose-matrix ├── NOTES.md └── 867-transpose-matrix.cpp ├── 9-palindrome-number ├── NOTES.md ├── 9-palindrome-number.java └── palindorme.cpp ├── Happy-Number-Leetcode ├── NOTES.md └── happyNumber.cpp ├── integer replacment ├── notes.md ├── code.cpp └── readme.md ├── widcard matching dp ├── notes.md └── readme.md ├── 0008-string-to-integer-atoi ├── NOTES.md └── 0008-string-to-integer-atoi.py ├── 0371-sum-of-two-integers ├── NOTES.md ├── 0371-sum-of-two-integers.java └── README.md ├── 0420-strong-password-checker └── NOTES.md ├── 11-container-with-most-water ├── NOTES.md └── 11-container-with-most-water.cpp ├── 14-longest-common-prefix ├── NOTES.md └── 14-longest-common-prefix.java ├── 1672-richest-customer-wealth ├── NOTES.md └── 1672-richest-customer-wealth.java ├── 2365-Task-schedulerr-ii └── NOTES.MD ├── 25-reverse-nodes-in-k-group └── NOTES.md ├── 32-longest-valid-parentheses └── NOTES.md ├── 41-first-missing-positive ├── NOTES.md ├── README.md └── 41-first-missing-positive.java ├── 658-find-k-closest-elements └── NOTES.md ├── 746-min-cost-climbing-stairs ├── NOTES.md └── 746-min-cost-climbing-stairs.cpp ├── 99-recover-binary-search-tree └── NOTES.md ├── Java ├── OccurenceOfCharInString.java ├── maximumDepthOfBinaryTree │ └── TreeNode.java ├── lowestCommonAncestorOfABinaryTree │ └── TreeNode.java ├── binaryTreeBFSTraversal │ ├── TreeNode.java │ └── binaryTreeBFSTraversal.java ├── inorderSuccessorInBST │ └── TreeNode.java ├── binaryTreeMaximumPathSum │ └── TreeNode.java ├── binaryTreePreorderTraversal │ └── TreeNode.java ├── binaryTreeLevelOrderTraversal │ └── TreeNode.java ├── balancedBinaryTree │ └── TreeNode.java ├── binarySearchTreeIterator │ └── TreeNode.java ├── firstBadVersion │ ├── VersionControl.java │ └── firstBadVersionTest.java ├── countAndSay │ └── TestCountAndSay.java ├── powXn │ └── TestPowXn.java ├── minStack │ └── TestMinStack.java ├── reverseWordsInAString │ └── TestReverseWordsInAString.java ├── lengthOfLastWord │ └── TestLengthOfLastWord.java ├── myStack │ └── TestMyStack.java ├── palindromeNumber │ └── TestPalindromeNumber.java ├── myQueue │ └── TestMyQueue.java ├── dynamicProgramming │ ├── climbStairs │ │ └── climbStairsTest.java │ ├── uniquePaths │ │ ├── uniquePathsTest.java │ │ └── uniquePathsIITest.java │ ├── minimumPathSum │ │ └── minimumPathSumTest.java │ └── triangle │ │ └── triangleTest.java ├── rotateArray │ └── TestRotateArray.java ├── reverseLinkedList │ └── TestReverseLinkedList.java ├── findPeakElement │ └── findPeakElementTest.java ├── MaxSubArray │ └── MaxSubarray.java ├── reverseLinkedListII │ └── TestReverseLinkedListII.java ├── findMinimumInRotatedSortedArray │ └── findMinimumInRotatedSortedArrayTest.java └── removeDuplicatesFromSortedArray │ └── TestRemoveDuplicates.java ├── Maximal Network Rank ├── notes.md └── code.cpp ├── split array largest sum ├── notes.md ├── code.cpp └── readme.md ├── 0152-maximum-product-subarray ├── NOTES.md └── 0152-maximum-product-subarray.java ├── 0238-product-of-array-except-self ├── NOTES.md └── 0238-product-of-array-except-self.java ├── 0273-integer-to-english-words └── NOTES.md ├── 0451-sort-characters-by-frequency ├── NOTES.md └── 0451-sort-characters-by-frequency.java ├── 1207-unique-number-of-occurrences ├── NOTES.md └── 1207-unique-number-of-occurrences.java ├── 1295-find-num-with-even-digits ├── NOTES.md ├── 1295-find-num-with-even-digits.java └── README.md ├── 2278-percent-of-letter-in-string ├── NOTES.MD ├── 2278-percent-of-letter-in-string.java └── README.MD ├── 287-find-the-duplicate-number ├── NOTES.md └── 287-find-the-duplicate-number.java ├── 33-search-in-rotated-sorted-array ├── NOTES.md └── 33-search-in-rotated-sorted-array.cpp ├── 4-median-of-two-sorted-arrays ├── NOTES.md └── 4-median-of-two-sorted-arrays.java ├── 417-pacific-atlantic-water-flow └── NOTES.md ├── 802-find-eventual-safe-states └── NOTES.md ├── score after flipping matrix ├── notes.md └── readme.md ├── 0033-search-in-rotated-sorted-array ├── NOTES.md └── 0033-search-in-rotated-sorted-array.java ├── 0081-search-in-rotated-sorted-array-ii └── NOTES.md ├── 102-binary-tree-level-order-traversal └── NOTES.md ├── 26-remove-duplicates-from-sorted-array ├── NOTES.md └── 26-remove-duplicates-from-sorted-array.java ├── Cheapest Flights Within K Stops └── notes.md ├── Leetcode graph course schdule IV └── notes.md ├── 0153-find-minimum-in-rotated-sorted-array ├── NOTES.md └── 0153-find-minimum-in-rotated-sorted-array.java ├── 1383_maximum_performance_of_a_team ├── problem_statement └── Solution.java ├── 1657-determine-if-two-strings-are-close ├── NOTES.md └── 1657-determine-if-two-strings-are-close.py ├── 17-letter-combinations-of-a-phone-number ├── NOTES.md └── README.md ├── 1704-determine-if-string-halves-are-alike ├── NOTES.md └── 1704-determine-if-string-halves-are-alike.java ├── 446. Arithmetic Slices II - Subsequence ├── notes.md └── 446. Arithmetic Slices II - Subsequence.cpp ├── Find The Original Array of Prefix Xor ├── notes.md ├── code.cpp └── readme.md ├── Leetcode Hard longest cycle in a graph └── notes.md ├── minimum height of tree graph problem └── notes.md ├── shortest unsorted continous subarray ├── notes.md ├── code.cpp └── readme.md ├── 3-longest-substring-without-repeating-characters ├── NOTES.md └── 3-longest-substring-without-repeating-characters.py ├── 744-find-smallest-letter-greater-than-target └── NOTES.md ├── Find Words That Can Be Formed by Characters ├── notes.md ├── code.cpp └── readme.md ├── 0028-find-the-index-of-the-first-occurrence-in-a-string ├── NOTES.md └── 0028-find-the-index-of-the-first-occurrence-in-a-string.py ├── 237-Delete-Node-in-a-Linked-List ├── Solution.cpp └── Solution.java ├── 96. Unique Binary Search Trees ├── 96. Unique Binary Search Trees.py ├── 96. Unique Binary Search Trees.java └── 96. Unique Binary Search Trees.cpp ├── Remove Element.java ├── 561-Array-Partition ├── solution.cpp └── README.md ├── 53-Maximum-Subarray ├── MaximumSubarray.py └── MaximumSubarray.cpp ├── Palindrome └── plaindrome-number.py ├── 976-Largest-Perimeter-Triangle ├── Solution.java ├── Solution.cpp └── README.md ├── 326-power-of-three.cpp ├── 1920. Build Array from Permutation └── code.java ├── 242_Valid_Anagram.cpp ├── 191. Number of 1 Bits.cpp ├── 1929. Concatenation of Array ├── code.java └── readme.md ├── 54.Spiral Matrix └── README.md ├── 202_Happy_Number.cpp ├── 0080-remove-duplicates-from-sorted-array-ii └── 0080-remove-duplicates-from-sorted-array-ii.java ├── 287_Find_the_Duplicate_Number.cpp ├── 1470. Shuffle the Array ├── code.java └── readme.md ├── 389. Find the Difference ├── 1720-decode-xored-array.cpp ├── remove-linked-list-elements.cpp ├── 48_rotate_image.cpp ├── Leetcode 274 H-Index.cpp ├── 1512.number-0f-good-pairs.cpp ├── 1328-break-a-palindrome └── solution.py ├── 46.Permutations ├── README.md └── permutations.cpp ├── House-Robber ├── index.py └── README.md ├── 462-Minimum-moves-to-equal-array-elements-II ├── solution.cpp └── README.md ├── 0217-contains-duplicate └── 0217-contains-duplicate.java ├── Code Testcase Testcase Result 237. Delete Node in a Linked List.cpp ├── 1903. Largest Odd Number in String ├── Java_Code └── Readme.md ├── 767-Reorganize-string ├── README.md └── solution.cpp ├── Contains with most water.java ├── 119 - Pascal Triangle II.txt ├── Peak Index in a Mountain Array.cpp ├── 120.triangle.cpp ├── twosum.py ├── 162-find-peak-element.cpp ├── 204_count_primes.cpp ├── 12-integer-to-roman └── intToRom.cpp ├── 2903. Find Indices With Index and Value Difference I └── 2903.cpp ├── 796. Rotate String └── rotate.py ├── 856_Score_of_Parentheses.cpp ├── 1838-Frequency-of-the-Most-Frequent-Element └── solution.cpp ├── 219.Contains Duplicate II ├── 376. Wiggle Subsequence └── Wiggle Subsequence.md ├── 49-Group-Anagrams ├── solution.cpp └── README.md ├── 91. Decode Ways └── DecodeWays.md ├── 1854-maximum-population-year ├── solution.cpp └── README.md ├── 682-baseball-game └── baseball-game.py ├── 134-gas-station └── 134-gas-station.cpp ├── Palindrome.java ├── 162. Find Peak Element └── 162. Find Peak Element.java ├── 167. Two Sum II - Input Array Is Sorted ├── 167. Two Sum II - Input Array Is Sorted.java └── 167. Two Sum II - Input Array Is Sorted.cpp ├── 35. Search Insert Position ├── 35. Search Insert Position.java └── 35. Search Insert Position.cpp ├── 154. Find Minimum in Rotated Sorted Array II ├── 154. Find Minimum in Rotated Sorted Array II.cpp └── 154. Find Minimum in Rotated Sorted Array II.java ├── 2623. Memoize └── 2623. Memoize.js ├── 7- Reverse Integer ├── rev.cpp └── Reverse Integer.java ├── 48-rotate-image ├── rotateImg.cpp └── README.md ├── Majority Element ├── Majority-Element.cpp └── ReadMe.md ├── 2618. Check if Object Instance of Class └── 2618. Check if Object Instance of Class.js ├── 41.First Missing Positive ├── 24-Swap-Node-In-Pair ├── README.md └── 24-swap-nodes-in-pair.cpp ├── 1832.Check-if-the-Sentence-Is-Pangram ├── Solution.java └── README.md ├── 334. Increasing Triplet Subsequence └── Increasing Triplet Subsequence.java ├── 134_gas_station.cpp ├── 32-Longest_Valid_Parenthese ├── README.md └── 32-Longest_Valid_Parentheses.cpp ├── Integer to Roman.java ├── 16-3sum-closest ├── README.md └── 3sumClosest.cpp ├── 46-permutations └── 46-permutations.cpp ├── 2150-Find-All-Lonely-Numbers-In-The-Array └── solution.cpp ├── 220-contains-duplicate-3 └── README.md ├── 97- Interleaving-String └── 97-Interleaving-String.cpp ├── 705-design-hashset.cpp ├── 2410. Maximum Matching of Players With Trainers.cpp ├── 95. Unique Binary Search Trees II ├── 95. Unique Binary Search Trees II.py └── 95. Unique Binary Search Trees II.cpp ├── zigzag.java ├── 448-Find All Numbers Disappeared in an Array └── solution.cpp ├── 56-Merge-Intervals ├── README.md └── solution.cpp ├── binaary to decimal └── code.cpp ├── 240-search-a-2d-matrix-2 ├── README.md └── search2dMatrix.cpp ├── 21-Merge-Two-Sorted-Lists └── Merge _Two_Sorted_Lists.java ├── 275 H-Index II .cpp ├── 1143. Longest Common Subsequence.cpp ├── 287-Find_the_Duplicate_Number.java ├── 1578-minimum-time-to-make-rope-colorful.cpp ├── 841-keys-and-rooms └── 841-keys-and-rooms.cpp ├── Minimum times A has to be repeated such that B is a substring of it - GFG └── minimum-times-a-has-to-be-repeated-such-that-b-is-a-substring-of-it.py ├── 2009. Minimum-Number-of-Operations-to-Make-Array-Continuous.cpp ├── 22- Generate Paranthesis └── Generate Paranthesis.java ├── 2381-Shifting-Letters-II └── solution.cpp ├── 973. K Closest Points to Origin ├── K Closest Points to Origin.cpp └── README.md ├── 104-Maximum-Depth-of-Binary-Tree.cpp ├── 797-all-paths-from-source-to-target └── 797-all-paths-from-source-to-target.cpp ├── 31. Next Permutation ├── 622-design-circular-queue └── solution.cpp ├── Swap Nodes In Pairs ├── readme.md └── Swap Nodes in Pairs cpp ├── 1255.Maximum Score Words Formed by Letters └── c.cpp ├── 23-merge-k-sorted-lists ├── readme.md └── mergeKSortedLists.cpp ├── 42-Trapping-Rain-Water └── TrappingRainWater.java ├── leetcode 451. sort characters by frequency ├── 692-top-k-frequent-words └── solution.cpp ├── 1155. Number of Dice Rolls With Target Sum └── Number of Dice Rolls With Target Sum.java ├── 153. Find Minimum in Rotated Sorted Array └── 153. Find Minimum in Rotated Sorted Array.java ├── 6. Zigzag Conversion ├── 1584-Min-Cost-to-Connect-All-Points └── Solution.java ├── 34. Find First and Last Position of Element in Sorted Array └── README.md ├── 62. Unique Paths └── Readme.md ├── 43-Multiply-Strings └── 43._Multiply_Strings.java ├── 560-subarray-sum-equals-k └── README.md ├── 37. Sudoku Solver └── README.md ├── 299. Bulls and cows.cpp ├── 106-Construct-BT-From-Inorder-And-Postorder-Traversal └── readme.md ├── 143-Reorder List └── ReorderList.java ├── 410. Split Array Largest Sum ├── 430-flatten-douby-link-list └── solution.cpp ├── House Robber 2.py ├── 205. Isomorphic Strings └── 2401-Longest-Nice-Subarray └── README.md /69-sqrtx/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1-two-sum/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /100-same-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /258-add-digits/NOTES.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /52-n-queens-II/NOTES.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /78-subsets/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /leedcode/twosum.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /0045-jump-game-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0190-reverse-bits/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0231-power-of-two/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0258-add-digits/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0338-counting-bits/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0342-power-of-four/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /101-symmetric-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /113-path-sum-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /127_word_ladder/ques.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /136-single-number/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /198-house-robber/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /27-remove-element/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /283-move-zeroes/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /338-counting-bits/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /38-count-and-say/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /45-jump-game-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /62-unique-paths/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /64.Minimum Path Sum/NOTES.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /733-flood-fill/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /75-sort-colors/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /Keys and Rooms/notes.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /0007-reverse-integer/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0012-integer-to-roman/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0053-maximum-subarray/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0191-number-of-1-bits/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0326-power-of-three/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /118-pascals-triangle/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /13-roman-to-integer/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /130-surrounded-regions/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1302-deepest-leaves-sum/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /20-valid-parentheses/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /217-contains-duplicate/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /264-Ugly-number-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /278-first-bad-version/NOTES.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /278-first-bad-version/README.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /58-length-of-last-word/NOTES.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /73-set-matrix-zeroes/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /739-daily-temperatures/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /74-search-a-2d-matrix/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /740-delete-and-earn/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /867-transpose-matrix/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /9-palindrome-number/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /Happy-Number-Leetcode/NOTES.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /integer replacment/notes.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /widcard matching dp/notes.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /0008-string-to-integer-atoi/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0371-sum-of-two-integers/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0420-strong-password-checker/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /11-container-with-most-water/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /14-longest-common-prefix/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1672-richest-customer-wealth/NOTES.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /2365-Task-schedulerr-ii/NOTES.MD: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /25-reverse-nodes-in-k-group/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /32-longest-valid-parentheses/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /41-first-missing-positive/NOTES.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /658-find-k-closest-elements/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /746-min-cost-climbing-stairs/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /99-recover-binary-search-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Java/OccurenceOfCharInString.java: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Maximal Network Rank/notes.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /split array largest sum/notes.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /0152-maximum-product-subarray/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0238-product-of-array-except-self/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0273-integer-to-english-words/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0451-sort-characters-by-frequency/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1207-unique-number-of-occurrences/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1295-find-num-with-even-digits/NOTES.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /2278-percent-of-letter-in-string/NOTES.MD: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /287-find-the-duplicate-number/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /33-search-in-rotated-sorted-array/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /4-median-of-two-sorted-arrays/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /417-pacific-atlantic-water-flow/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /802-find-eventual-safe-states/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /score after flipping matrix/notes.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /0033-search-in-rotated-sorted-array/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0081-search-in-rotated-sorted-array-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /102-binary-tree-level-order-traversal/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /136-single-number/101-symmetric-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /26-remove-duplicates-from-sorted-array/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /Cheapest Flights Within K Stops/notes.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Leetcode graph course schdule IV/notes.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /0153-find-minimum-in-rotated-sorted-array/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1383_maximum_performance_of_a_team/problem_statement: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /1657-determine-if-two-strings-are-close/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /17-letter-combinations-of-a-phone-number/NOTES.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /1704-determine-if-string-halves-are-alike/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /446. Arithmetic Slices II - Subsequence/notes.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Find The Original Array of Prefix Xor/notes.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Leetcode Hard longest cycle in a graph/notes.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /minimum height of tree graph problem/notes.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /shortest unsorted continous subarray/notes.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /3-longest-substring-without-repeating-characters/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /744-find-smallest-letter-greater-than-target/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /Find Words That Can Be Formed by Characters/notes.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /0028-find-the-index-of-the-first-occurrence-in-a-string/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /258-add-digits/258-add-digits.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int addDigits(int num) { 4 | return 1 + (num - 1) % 9; 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /0231-power-of-two/0231-power-of-two.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public boolean isPowerOfTwo(int n) { 3 | return Integer.toString(n, 2).matches("^10*$"); 4 | } 5 | } -------------------------------------------------------------------------------- /0342-power-of-four/0342-power-of-four.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public boolean isPowerOfFour(int n) { 3 | return Integer.toString(n, 4).matches("^10*$"); 4 | } 5 | } -------------------------------------------------------------------------------- /0326-power-of-three/0326-power-of-three.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public boolean isPowerOfThree(int n) { 3 | return Integer.toString(n, 3).matches("^10*$"); 4 | } 5 | } -------------------------------------------------------------------------------- /0371-sum-of-two-integers/0371-sum-of-two-integers.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int getSum(int a, int b) { 3 | return b==0? a:getSum(a^b, (a&b)<<1); 4 | 5 | 6 | } 7 | } -------------------------------------------------------------------------------- /237-Delete-Node-in-a-Linked-List/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void deleteNode(ListNode* node) { 4 | node->val = node->next->val; 5 | node->next = node->next->next; 6 | } 7 | }; 8 | -------------------------------------------------------------------------------- /Java/maximumDepthOfBinaryTree/TreeNode.java: -------------------------------------------------------------------------------- 1 | package maximumDepthOfBinaryTree; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | TreeNode(int x) { val = x; } 8 | } 9 | -------------------------------------------------------------------------------- /0028-find-the-index-of-the-first-occurrence-in-a-string/0028-find-the-index-of-the-first-occurrence-in-a-string.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def strStr(self, haystack: str, needle: str) -> int: 3 | return haystack.find(needle) 4 | -------------------------------------------------------------------------------- /Java/lowestCommonAncestorOfABinaryTree/TreeNode.java: -------------------------------------------------------------------------------- 1 | package lowestCommonAncestorOfABinaryTree; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | TreeNode(int x) { val = x; } 8 | } 9 | -------------------------------------------------------------------------------- /136-single-number/136-single-number.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int singleNumber(int[] nums) { 3 | int result = 0; 4 | for(int i : nums) { 5 | result ^= i; 6 | } 7 | return result; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /96. Unique Binary Search Trees/96. Unique Binary Search Trees.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | @cache 3 | def numTrees(self, n: int) -> int: 4 | if n <= 1: return 1 5 | return sum(self.numTrees(i-1) * self.numTrees(n-i) for i in range(1, n+1)) -------------------------------------------------------------------------------- /Remove Element.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int removeElement(int[] nums, int val) { 3 | int i = 0; 4 | 5 | for (final int num : nums) 6 | if (num != val) 7 | nums[i++] = num; 8 | 9 | return i; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /1657-determine-if-two-strings-are-close/1657-determine-if-two-strings-are-close.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def closeStrings(self, w1: str, w2: str) -> bool: 3 | return set(w1) == set(w2) and Counter(Counter(w1).values()) == Counter(Counter(w2).values()) -------------------------------------------------------------------------------- /561-Array-Partition/solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int arrayPairSum(vector& nums) { 4 | sort(nums.begin(),nums.end()); 5 | int sum =0; 6 | for(int i=0;i0): 5 | dig=num%10 6 | rev=rev*10+dig 7 | num=num//10 8 | if(temp==rev): 9 | print("The number is palindrome!") 10 | else: 11 | print("Not a palindrome!") -------------------------------------------------------------------------------- /Java/binaryTreeMaximumPathSum/TreeNode.java: -------------------------------------------------------------------------------- 1 | package binaryTreeMaximumPathSum; 2 | 3 | /** 4 | * Created by leicao on 5/10/15. 5 | */ 6 | public class TreeNode { 7 | int val; 8 | TreeNode left; 9 | TreeNode right; 10 | TreeNode(int x) { val = x; } 11 | } 12 | -------------------------------------------------------------------------------- /976-Largest-Perimeter-Triangle/Solution.java: -------------------------------------------------------------------------------- 1 | public int Solution(int[] A) { 2 | Arrays.sort(A); 3 | for (int i = A.length - 1; i > 1; --i) 4 | if (A[i] < A[i - 1] + A[i - 2]) 5 | return A[i] + A[i - 1] + A[i - 2]; 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /Java/binaryTreePreorderTraversal/TreeNode.java: -------------------------------------------------------------------------------- 1 | package binaryTreePreorderTraversal; 2 | 3 | /** 4 | * Created by leicao on 5/10/15. 5 | */ 6 | public class TreeNode { 7 | int val; 8 | TreeNode left; 9 | TreeNode right; 10 | TreeNode(int x) { val = x; } 11 | } 12 | -------------------------------------------------------------------------------- /976-Largest-Perimeter-Triangle/Solution.cpp: -------------------------------------------------------------------------------- 1 | int Solution(vector& A) { 2 | sort(A.begin(), A.end()); 3 | for (int i = A.size() - 1 ; i > 1; --i) 4 | if (A[i] < A[i - 1] + A[i - 2]) 5 | return A[i] + A[i - 1] + A[i - 2]; 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /Java/binaryTreeLevelOrderTraversal/TreeNode.java: -------------------------------------------------------------------------------- 1 | package binaryTreeLevelOrderTraversal; 2 | 3 | /** 4 | * Created by leicao on 5/10/15. 5 | */ 6 | public class TreeNode { 7 | int val; 8 | TreeNode left; 9 | TreeNode right; 10 | TreeNode(int x) { val = x; } 11 | } 12 | -------------------------------------------------------------------------------- /326-power-of-three.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPowerOfThree(int n) { 4 | 5 | if(n==0) return false; 6 | if(n==1) return true; 7 | 8 | if(n%3==0){ 9 | return isPowerOfThree(n/3); 10 | } 11 | return false; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /0033-search-in-rotated-sorted-array/0033-search-in-rotated-sorted-array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int search(int[] nums, int target) { 3 | for(int i=0;i>= 1; 9 | } 10 | 11 | return rev; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /0191-number-of-1-bits/0191-number-of-1-bits.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // you need to treat n as an unsigned value 3 | public int hammingWeight(int n) { 4 | int res=0; 5 | while(n!=0){ 6 | n=(n & (n-1)); 7 | res++; 8 | } 9 | return res; 10 | } 11 | } -------------------------------------------------------------------------------- /191. Number of 1 Bits.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int hammingWeight(uint32_t n) { 4 | int count=0; 5 | while(n!=0){ 6 | 7 | 8 | if(n&1){ 9 | count++; 10 | } 11 | n=n>>1; 12 | } 13 | return count; 14 | } 15 | }; -------------------------------------------------------------------------------- /1929. Concatenation of Array/code.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] getConcatenation(int[] nums) { 3 | int n=nums.length; 4 | int[] ans=new int[2*n]; 5 | for(int i=0;i nums[i-2]) { 9 | nums[i++] = n; 10 | } 11 | } 12 | 13 | return i; 14 | } 15 | 16 | } -------------------------------------------------------------------------------- /287_Find_the_Duplicate_Number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findDuplicate(vector& nums) { 4 | int ans = 0; 5 | for(int i=0;i int: 3 | l=0 4 | for r in range(len(nums)): 5 | if nums[r]!=val: 6 | nums[l]=nums[r] 7 | l+=1 8 | 9 | return l -------------------------------------------------------------------------------- /389. Find the Difference: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | char findTheDifference(string s, string t) { 4 | int n = s.length(); 5 | sort(s.begin(), s.end()); 6 | sort(t.begin(), t.end()); 7 | 8 | for(int i = 0; i= firstBadVersion) { 10 | return true; 11 | } 12 | return false; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Find The Original Array of Prefix Xor/code.cpp: -------------------------------------------------------------------------------- 1 | class solution 2 | { 3 | public: 4 | vector findArray(vector& pref) { 5 | vector result(pref.size()); 6 | result[0] = pref[0]; 7 | for (int i = 1; i < pref.size(); ++i) { 8 | result[i] = pref[i] ^ pref[i - 1]; 9 | } 10 | 11 | return result; 12 | } 13 | -------------------------------------------------------------------------------- /1720-decode-xored-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector decode(vector& encoded, int first) { 4 | vectorans; 5 | ans.push_back(first); 6 | for(int i=0;ival == val) 7 | return removeElements(head->next,val); 8 | 9 | head->next = removeElements(head->next,val); 10 | 11 | return head; 12 | 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /26-remove-duplicates-from-sorted-array/26-remove-duplicates-from-sorted-array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | public int removeDuplicates(int[] A) { 4 | if (A.length==0) return 0; 5 | int j=0; 6 | for (int i=0; i>& matrix) { 4 | int n = matrix.size(); 5 | reverse(matrix.begin(),matrix.end()); 6 | 7 | for(int i=0;i& citations) { 5 | sort(citations.begin(),citations.end()); 6 | int h_index = 0; 7 | int m = citations.size(); 8 | for(int i=0;i& nums) { 5 | vector arr(101,0); 6 | int count = 0; 7 | for(int i=0; i str: 3 | 4 | 5 | for i in range(len(palindrome)//2): 6 | if palindrome[i]!='a': 7 | return palindrome[:i]+'a'+palindrome[i+1:] 8 | 9 | return palindrome[:-1]+'b' if len(palindrome)>1 else "" 10 | 11 | -------------------------------------------------------------------------------- /0153-find-minimum-in-rotated-sorted-array/0153-find-minimum-in-rotated-sorted-array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findMin(int[] arr) { 3 | int lo=0, hi=arr.length-1; 4 | while(lo max) max = wealth; 10 | } 11 | return max; 12 | } 13 | } -------------------------------------------------------------------------------- /198-house-robber/198-house-robber.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int rob(vector& nums) { 4 | int n=nums.size(); 5 | if(n==1) 6 | return nums[0]; 7 | int a=nums[0],b=max(nums[0],nums[1]); 8 | for(int i=2;i& nums) { 4 | int n = nums.size(), steps = 0; 5 | sort(nums.begin(), nums.end()); 6 | int median = nums[n/2]; // Finding median 7 | for(int i=0; i map=new HashMap<>(); 4 | map.put(nums[0],0); 5 | for(int i=1;ival = node->next->val; 13 | node->next= node->next->next; 14 | 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /1903. Largest Odd Number in String/Java_Code: -------------------------------------------------------------------------------- 1 | //Time : O(n) 2 | //Space : O(n) 3 | 4 | class Solution { 5 | public String largestOddNumber(String num) { 6 | 7 | StringBuilder ans = new StringBuilder(""); 8 | 9 | for(int i=num.length()-1;i>=0;i--){ 10 | if((num.charAt(i)-'0')%2!=0) 11 | return num.substring(0,i+1); 12 | } 13 | 14 | return ""; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /767-Reorganize-string/README.md: -------------------------------------------------------------------------------- 1 | Given a string s, rearrange the characters of s so that any two adjacent characters are not the same. 2 | 3 | Return any possible rearrangement of s or return "" if not possible. 4 | 5 | 6 | 7 | Example 1: 8 | 9 | Input: s = "aab" 10 | Output: "aba" 11 | Example 2: 12 | 13 | Input: s = "aaab" 14 | Output: "" 15 | 16 | 17 | Constraints: 18 | 19 | 1 <= s.length <= 500 20 | s consists of lowercase English letters. -------------------------------------------------------------------------------- /Contains with most water.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxArea(int[] height) { 3 | int ans = 0; 4 | int l = 0; 5 | int r = height.length - 1; 6 | 7 | while (l < r) { 8 | final int minHeight = Math.min(height[l], height[r]); 9 | ans = Math.max(ans, minHeight * (r - l)); 10 | if (height[l] < height[r]) 11 | ++l; 12 | else 13 | --r; 14 | } 15 | 16 | return ans; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /119 - Pascal Triangle II.txt: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector getRow(int rowIndex) { 4 | vector getRow(int rowIndex); 5 | vector res(1,1); 6 | long long prev = 1; 7 | for(int k = 1; k <= rowIndex; k++) { 8 | long long next_val = prev* (rowIndex - k + 1) / k; 9 | res.push_back(next_val); 10 | prev = next_val; 11 | } 12 | 13 | return res; 14 | } 15 | }; -------------------------------------------------------------------------------- /0190-reverse-bits/0190-reverse-bits.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def reverseBits(self, n: int) -> int: 3 | data = bin(n) # convert int to binary string 4 | data = data[2:] # 0b000........ we are only selecting our string after 2nd index 5 | data = data.zfill(32) # zfill is used to add zeros at the begining of the string until it reaches the length as specidifed 6 | data = data[::-1] # slicing technique to reverse the string 7 | return int(data, 2) -------------------------------------------------------------------------------- /0258-add-digits/0258-add-digits.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public static int sum(int k){ 3 | int sum2=0; 4 | while(k>0){ 5 | int rem=k%10; 6 | sum2+=rem; 7 | k/=10;}return sum2; 8 | 9 | } 10 | public int addDigits(int n) { 11 | int sum1=n; 12 | if(sum1<10){ 13 | return sum1; 14 | } 15 | while(sum1>9){ 16 | sum1=sum(sum1); 17 | }return sum1; 18 | } 19 | } -------------------------------------------------------------------------------- /14-longest-common-prefix/14-longest-common-prefix.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String longestCommonPrefix(String[] strs) { 3 | if(strs.length == 0) return ""; 4 | String prefix = strs[0]; 5 | for(int i=1; i< strs.length; i++ ) { 6 | while(strs[i].indexOf(prefix) != 0) { 7 | prefix = prefix.substring(0, prefix.length()-1); 8 | } 9 | } 10 | 11 | return prefix; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Peak Index in a Mountain Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int peakIndexInMountainArray(vector& arr) { 4 | int s=0,e=arr.size()-1; 5 | int mid = s + (e-s)/2; 6 | while(s>& triangle) { 4 | vector> dp; 5 | dp = triangle; 6 | for(int i = triangle.size()-2;i>=0;i--) 7 | { 8 | for(int j = 0;j& nums) { 4 | int s = 0; 5 | int n = nums.size(); 6 | int e = n-1; 7 | int mid = s+(e-s)/2; 8 | while(s prime(n+1 , true); 6 | 7 | prime[0]=prime[1]=false; 8 | 9 | for(int i=2;i0){ 10 | u=x%10; 11 | rev=rev*10+u; 12 | x=x/10; 13 | } 14 | if(rev==r){ 15 | return true; 16 | } 17 | 18 | 19 | return false; 20 | } 21 | 22 | } -------------------------------------------------------------------------------- /12-integer-to-roman/intToRom.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string intToRoman(int num) { 4 | int normal[]={1000,900,500,400,100,90,50,40,10,9,5,4,1}; 5 | string roman[]={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"}; 6 | string res; 7 | for(int i=0;i<13;i++){ 8 | while(num>=normal[i]){ 9 | res.append(roman[i]); 10 | num-=normal[i]; 11 | } 12 | } 13 | return res; 14 | } 15 | }; -------------------------------------------------------------------------------- /2903. Find Indices With Index and Value Difference I/2903.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector findIndices(vector& nums, int id, int vd) { 4 | if(id==0 and vd==0) 5 | return {0,0}; 6 | for(int i=0;i< nums.size();i++) 7 | { 8 | for(int j=i+id;j=vd) 11 | return {i,j}; 12 | } 13 | } 14 | return {-1,-1}; 15 | } 16 | }; -------------------------------------------------------------------------------- /Java/countAndSay/TestCountAndSay.java: -------------------------------------------------------------------------------- 1 | package countAndSay; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | /** 6 | * Test for 38. Count and Say 7 | */ 8 | public class TestCountAndSay { 9 | @Test 10 | public void test() { 11 | CountAndSay solution = new CountAndSay(); 12 | String next5 = solution.countAndSay(5); 13 | Assert.assertTrue(next5.equals("111221")); 14 | String next6 = solution.countAndSay(6); 15 | Assert.assertTrue(next6.equals("312211")); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /796. Rotate String/rotate.py: -------------------------------------------------------------------------------- 1 | def rotate_string(input_string, n): 2 | if len(input_string) == 0: 3 | return input_string 4 | n = n % len(input_string) 5 | rotated_string = "" 6 | for i in range(len(input_string)): 7 | rotated_string += input_string[(i + n) % len(input_string)] 8 | return rotated_string 9 | 10 | # Example usage: 11 | original_string = "Hello, World!" 12 | rotation_amount = 3 13 | result = rotate_string(original_string, rotation_amount) 14 | print(result) 15 | -------------------------------------------------------------------------------- /856_Score_of_Parentheses.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int scoreOfParentheses(string s) { 4 | stack st; 5 | 6 | int count = 0; 7 | for(int i=0;i &nums, int k) { 4 | sort(begin(nums), end(nums)); 5 | int n = nums.size(); 6 | long long ans = 0, sum = 0, l = 0, r = 0; 7 | for (; r < n; r++) { 8 | sum += nums[r]; 9 | while ((r - l + 1) * nums[r] - sum > k) 10 | sum -= nums[l++]; 11 | ans = max(ans, r - l + 1); 12 | } 13 | return ans; 14 | } 15 | }; -------------------------------------------------------------------------------- /219.Contains Duplicate II: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool containsNearbyDuplicate(vector& nums, int k) { 4 | long long cnt=0; 5 | unordered_mapmp; 6 | for(int i=0;i1) return true; 13 | cnt++; 14 | } 15 | return false; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /376. Wiggle Subsequence/Wiggle Subsequence.md: -------------------------------------------------------------------------------- 1 | ```cpp 2 | class Solution { 3 | public: 4 | int wiggleMaxLength(vector& nums) { 5 | 6 | int n = nums.size(); 7 | int peak = 1, valley = 1; 8 | 9 | for(int i=1; inums[i-1]) 11 | peak=valley + 1; 12 | else if(nums[i]> groupAnagrams(vector& strs) { 4 | unordered_map> mp; 5 | for (string s : strs) { 6 | string t = s; 7 | sort(t.begin(), t.end()); 8 | mp[t].push_back(s); 9 | } 10 | vector> anagrams; 11 | for (auto p : mp) { 12 | anagrams.push_back(p.second); 13 | } 14 | return anagrams; 15 |     } 16 | }; -------------------------------------------------------------------------------- /58-length-of-last-word/58-length-of-last-word.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int lengthOfLastWord(String s) { 3 | int count=0; 4 | 5 | for(int i=s.length()-1;i>=0;i--){ 6 | if(count > 0 && s.charAt(i) == ' '){ 7 | break; 8 | } 9 | if(s.charAt(i) == ' '){ 10 | continue; 11 | } 12 | else{ 13 | count++; 14 | } 15 | } 16 | return count; 17 | 18 | } 19 | } -------------------------------------------------------------------------------- /91. Decode Ways/DecodeWays.md: -------------------------------------------------------------------------------- 1 | ```cpp 2 | class Solution { 3 | public: 4 | int numDecodings(const string& s) { 5 | int n = s.size(); 6 | vector dp(n+1, 0); 7 | dp[n] = 1; 8 | for (int i = n - 1; i >= 0; --i) { 9 | if (s[i] != '0') 10 | dp[i] += dp[i+1]; 11 | if (i+1 < s.size() && (s[i] == '1' || s[i] == '2' && s[i+1] <= '6')) 12 | dp[i] += dp[i+2]; 13 | } 14 | return dp[0]; 15 | } 16 | }; 17 | ``` -------------------------------------------------------------------------------- /0338-counting-bits/0338-counting-bits.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public static int hammingWeight(int n) { 3 | int res=0; 4 | while(n!=0){ 5 | n=(n & (n-1)); 6 | res++; 7 | } 8 | return res; 9 | } 10 | public int[] countBits(int n) { 11 | 12 | 13 | int arr[]=new int[n+1]; 14 | for(int i=0;i<=n;i++){ 15 | arr[i]=Solution.hammingWeight(i); 16 | 17 | 18 | }return arr; 19 | } 20 | } -------------------------------------------------------------------------------- /11-container-with-most-water/11-container-with-most-water.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxArea(vector& height) 4 | { 5 | int l=0,r=height.size()-1; 6 | int max_area=0; 7 | 8 | while(lmaxsum){ 10 | maxsum=cursum; 11 | }if(cursum<0){ 12 | cursum=0; 13 | } 14 | } 15 | return maxsum; 16 | 17 | } 18 | } -------------------------------------------------------------------------------- /1854-maximum-population-year/solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maximumPopulation(vector> &logs) { 4 | vector> ok; 5 | for (auto l : logs) { 6 | ok.push_back({l[0], 1}); 7 | ok.push_back({l[1], -1}); 8 | } 9 | int ans = 0, mx = 0, curr = 0; 10 | sort(begin(ok), end(ok)); 11 | for (auto x : ok) { 12 | curr += x[1]; 13 | if (curr > mx) { 14 | mx = curr; 15 | ans = x[0]; 16 | } 17 | } 18 | return ans; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /682-baseball-game/baseball-game.py: -------------------------------------------------------------------------------- 1 | def calPoints(ops): 2 | result = 0 3 | length = len(ops) 4 | stack = [] 5 | 6 | for i in range(length): 7 | if ops[i] == '+': 8 | num1 = stack[-1] 9 | num2 = stack[-2] 10 | stack.append(num1 + num2) 11 | elif ops[i] == 'D': 12 | stack.append(stack[-1] * 2) 13 | elif ops[i] == 'C': 14 | stack.pop() 15 | else: 16 | stack.append(int(ops[i])) 17 | 18 | return sum(stack) -------------------------------------------------------------------------------- /Java/minStack/TestMinStack.java: -------------------------------------------------------------------------------- 1 | package minStack; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | /** 7 | * Test for 155. Min Stack 8 | */ 9 | public class TestMinStack { 10 | @Test 11 | public void test() { 12 | MinStack minStack = new MinStack(); 13 | minStack.push(3); 14 | minStack.push(4); 15 | minStack.push(1); 16 | minStack.push(2); 17 | Assert.assertTrue(minStack.getMin() == 1); 18 | minStack.pop(); 19 | minStack.pop(); 20 | Assert.assertTrue(minStack.getMin() == 3); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /13-roman-to-integer/roman.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int romanToInt(string s) { 4 | 5 | unordered_map mp{ 6 | {'I',1}, 7 | {'V',5}, 8 | {'X',10}, 9 | {'L',50}, 10 | {'C',100}, 11 | {'D',500}, 12 | {'M',1000}, 13 | }; 14 | int ans =0; 15 | for(int i=0;i& gas, vector& cost) 4 | { 5 | int n = gas.size(); 6 | int total(0), subsum(INT_MAX), start(0); 7 | for(int i = 0; i < n; ++i) 8 | { 9 | total += gas[i] - cost[i]; 10 | if(total < subsum) 11 | { 12 | subsum = total; 13 | start = i + 1; 14 | } 15 | } 16 | return (total < 0) ? -1 : (start % n); 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /Java/reverseWordsInAString/TestReverseWordsInAString.java: -------------------------------------------------------------------------------- 1 | package reverseWordsInAString; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | /** 7 | * Test for 151. Reverse Words in a String 8 | */ 9 | public class TestReverseWordsInAString { 10 | @Test 11 | public void test() { 12 | ReverseWordsInAString solution = new ReverseWordsInAString(); 13 | String str = " the sky is blue "; 14 | String result = solution.reverseWords(str); 15 | Assert.assertTrue(result.equals("blue is sky the")); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /118-pascals-triangle/118-pascals-triangle.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List> generate(int numRows) { 3 | List> res = new ArrayList>(); 4 | List row, pre = null; 5 | for (int i = 0; i < numRows; ++i) { 6 | row = new ArrayList(); 7 | for (int j = 0; j <= i; ++j) 8 | if (j == 0 || j == i) 9 | row.add(1); 10 | else 11 | row.add(pre.get(j - 1) + pre.get(j)); 12 | pre = row; 13 | res.add(row); 14 | } 15 | return res; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /287-find-the-duplicate-number/287-find-the-duplicate-number.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findDuplicate(int[] nums) { 4 | int slow=nums[0]; 5 | int fast=nums[0]; 6 | 7 | do{ 8 | slow=nums[slow]; 9 | fast=nums[nums[fast]]; 10 | }while(slow!=fast); 11 | 12 | fast=nums[0]; 13 | while(slow!=fast){ 14 | slow=nums[slow]; 15 | fast=nums[fast]; 16 | } 17 | 18 | return slow; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /Palindrome.java: -------------------------------------------------------------------------------- 1 | public class Palindrome { 2 | public static boolean isPalindrome(int n) { 3 | int temp = n; 4 | int rem; 5 | int ans = 0; 6 | while(n>0){ 7 | rem = n%10; 8 | ans = ans*10+rem; 9 | n=n/10; 10 | } 11 | if(ans==temp){ 12 | return true; 13 | } 14 | return false; 15 | } 16 | public static void main(String[] args){ 17 | int N = 124321; 18 | boolean res = isPalindrome(N); 19 | System.out.print(res); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /0012-integer-to-roman/0012-integer-to-roman.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public String intToRoman(int num) { 3 | 4 | int[] values = {1000,900,500,400,100,90,50,40,10,9,5,4,1}; 5 | String[] strs = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"}; 6 | 7 | StringBuilder sb = new StringBuilder(); 8 | 9 | for(int i=0;i= values[i]) { 11 | num -= values[i]; 12 | sb.append(strs[i]); 13 | } 14 | } 15 | return sb.toString(); 16 | } 17 | } -------------------------------------------------------------------------------- /162. Find Peak Element/162. Find Peak Element.java: -------------------------------------------------------------------------------- 1 | public int findPeakElement(int[] nums) { 2 | int N = nums.length; 3 | if (N == 1) { 4 | return 0; 5 | } 6 | 7 | int left = 0, right = N - 1; 8 | while (right - left > 1) { 9 | int mid = left + (right - left) / 2; 10 | if (nums[mid] < nums[mid + 1]) { 11 | left = mid + 1; 12 | } else { 13 | right = mid; 14 | } 15 | } 16 | 17 | return (left == N - 1 || nums[left] > nums[left + 1]) ? left : right; 18 | } -------------------------------------------------------------------------------- /167. Two Sum II - Input Array Is Sorted/167. Two Sum II - Input Array Is Sorted.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] twoSum(int[] numbers, int target) { 3 | 4 | int left=0,right=numbers.length-1; 5 | 6 | while(lefttarget) 12 | right--; 13 | else if(sum target) { 7 | end = mid; 8 | } else if(nums[mid] < target) { 9 | start = mid + 1; 10 | } else { 11 | return mid; 12 | } 13 | } 14 | return start; 15 | } 16 | } -------------------------------------------------------------------------------- /154. Find Minimum in Rotated Sorted Array II/154. Find Minimum in Rotated Sorted Array II.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int findMin(vector &nums) 5 | { 6 | int n = nums.size(); 7 | int i = 0; 8 | int j = n - 1; 9 | while (i < j) 10 | { 11 | int mid = (i + j) / 2; 12 | if (nums[mid] > nums[j]) 13 | { 14 | i = mid + 1; 15 | } 16 | else 17 | j--; 18 | } 19 | return nums[i]; 20 | } 21 | }; -------------------------------------------------------------------------------- /258-add-digits/README.md: -------------------------------------------------------------------------------- 1 | Add Digits 2 | 3 | (Easy) 4 | 5 | Given an integer num, repeatedly add all its digits until the result has only one digit, and return it. 6 | 7 | Example 1: 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 | Example 2: 17 | 18 | Input: num = 0 19 | Output: 0 20 | 21 | Constraints: 22 | 23 | 0 <= num <= 2^31 - 1 -------------------------------------------------------------------------------- /Happy-Number-Leetcode/happyNumber.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isHappy(int n) { 4 | long long int sum=0; 5 | while(n){ 6 | int a = n%10; 7 | sum+=pow(a,2); 8 | n=n/10; 9 | } 10 | if(sum==1){ 11 | return true; 12 | } 13 | if(sum==89){ 14 | return false; 15 | } 16 | else{ 17 | return isHappy(sum); 18 | } 19 | } 20 | 21 | }; 22 | 23 | -------------------------------------------------------------------------------- /Java/lengthOfLastWord/TestLengthOfLastWord.java: -------------------------------------------------------------------------------- 1 | package lengthOfLastWord; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | /** 7 | * Test for 58. Length of Last Word 8 | */ 9 | public class TestLengthOfLastWord { 10 | @Test 11 | public void test() { 12 | LengthOfLastWord solution = new LengthOfLastWord(); 13 | String str1 = "Hello World"; 14 | Assert.assertTrue(solution.lengthOfLastWord(str1) == 5); 15 | String str2 = "Thank you very much "; 16 | Assert.assertTrue(solution.lengthOfLastWord(str2) == 4); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /2623. Memoize/2623. Memoize.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {Function} fn 3 | */ 4 | function memoize(fn) { 5 | const mp = {}; 6 | return function(...args) { 7 | const key = args.join('#'); 8 | if(mp[key] == undefined) mp[key] = fn(...args); 9 | return mp[key]; 10 | } 11 | } 12 | 13 | 14 | /** 15 | * let callCount = 0; 16 | * const memoizedFn = memoize(function (a, b) { 17 | * callCount += 1; 18 | * return a + b; 19 | * }) 20 | * memoizedFn(2, 3) // 5 21 | * memoizedFn(2, 3) // 5 22 | * console.log(callCount) // 1 23 | */ -------------------------------------------------------------------------------- /integer replacment/code.cpp: -------------------------------------------------------------------------------- 1 | map dp; 2 | 3 | 4 | long long int help(long long int n){ 5 | // cout<INT_MAX/10 || ansarr[j]) 11 | i=mid+1; 12 | else if(arr[mid]> transpose(vector>& matrix) 4 | { 5 | int r=matrix.size(); 6 | int c=matrix[0].size(); 7 | 8 | // reverse matrix creation if its not a square m. 9 | vector> res(c,vector (r,0)); 10 | 11 | for(int i=0; i>& matrix) { 4 | int n = matrix.size(); 5 | 6 | for (int i = 0; i < n / 2; i++) 7 | { 8 | for (int j = 0; j < n; j++) 9 | { 10 | swap(matrix[i][j], matrix[n - i - 1][j]); 11 | } 12 | } 13 | 14 | for (int i = 0; i < n; i++) 15 | { 16 | for (int j = i; j < n; j++) 17 | { 18 | swap(matrix[i][j], matrix[j][i]); 19 | } 20 | } 21 | } 22 | }; -------------------------------------------------------------------------------- /Majority Element/Majority-Element.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int majorityElement(vector& nums) { 4 | sort(nums.begin(),nums.end()); 5 | int limit = (nums.size())/2; 6 | int count=1; 7 | for(int i=0;i limit){ 11 | return nums[i]; 12 | } 13 | } 14 | else{ 15 | count=1; 16 | } 17 | } 18 | return nums[0]; 19 | } 20 | }; -------------------------------------------------------------------------------- /2618. Check if Object Instance of Class/2618. Check if Object Instance of Class.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {Object} object 3 | * @param {Function} classFunction 4 | * @return {boolean} 5 | */ 6 | var checkIfInstanceOf = function(obj, classFunction) { 7 | if(obj === null || obj === undefined || typeof classFunction !== 'function') 8 | return false; 9 | while(obj != null){ 10 | 11 | if(obj.__proto__ === classFunction.prototype) return true; 12 | obj = obj.__proto__; 13 | } 14 | return false; 15 | }; 16 | 17 | /** 18 | * checkIfInstanceOf(new Date(), Date); // true 19 | */ -------------------------------------------------------------------------------- /41.First Missing Positive: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int firstMissingPositive(int[] A) { 3 | int i = 0; 4 | while(i < A.length){ 5 | if(A[i] == i+1 || A[i] <= 0 || A[i] > A.length) i++; 6 | else if(A[A[i]-1] != A[i]) swap(A, i, A[i]-1); 7 | else i++; 8 | } 9 | i = 0; 10 | while(i < A.length && A[i] == i+1) i++; 11 | return i+1; 12 | } 13 | 14 | private void swap(int[] A, int i, int j){ 15 | int temp = A[i]; 16 | A[i] = A[j]; 17 | A[j] = temp; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /1-two-sum/1-two-sum.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] twoSum(int[] nums, int target) { 3 | HashMap map=new HashMap(); 4 | for(int i=0;i a[prev - 'A']) { 14 | sum = sum - 2 * a[prev - 'A']; 15 | } 16 | sum = sum + a[s - 'A']; 17 | prev = s; 18 | } 19 | return sum; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /24-Swap-Node-In-Pair/README.md: -------------------------------------------------------------------------------- 1 | 24. Swap Nodes in Pairs 2 | 3 | Medium 4 | 5 | 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.) 6 | 7 | 8 | 9 | Example 1: 10 | 11 | 12 | Input: head = [1,2,3,4] 13 | Output: [2,1,4,3] 14 | Example 2: 15 | 16 | Input: head = [] 17 | Output: [] 18 | Example 3: 19 | 20 | Input: head = [1] 21 | Output: [1] 22 | 23 | 24 | Constraints: 25 | 26 | The number of nodes in the list is in the range [0, 100]. 27 | 0 <= Node.val <= 100 -------------------------------------------------------------------------------- /Java/palindromeNumber/TestPalindromeNumber.java: -------------------------------------------------------------------------------- 1 | package palindromeNumber; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | /** 7 | * Test for 9. Palindrome Number 8 | */ 9 | public class TestPalindromeNumber { 10 | @Test 11 | public void test() { 12 | PalindromeNumber solution = new PalindromeNumber(); 13 | boolean flag1 = solution.isPalindrome(1234567); 14 | Assert.assertTrue(!flag1); 15 | boolean flag2 = solution.isPalindrome(1234321); 16 | Assert.assertTrue(flag2); 17 | boolean flag3 = solution.isPalindrome(12344321); 18 | Assert.assertTrue(flag3); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /48-rotate-image/README.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/rotate-image/ 2 | 3 | You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise). 4 | 5 | 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. 6 | 7 | 8 | Example 1: 9 | 10 | input: matrix = [[1,2,3],[4,5,6],[7,8,9]] 11 | output: [[7,4,1],[8,5,2],[9,6,3]] 12 | 13 | 14 | Example 2: 15 | 16 | input: matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]] 17 | output: [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]] -------------------------------------------------------------------------------- /integer replacment/readme.md: -------------------------------------------------------------------------------- 1 | Given a positive integer n, you can apply one of the following operations: 2 | 3 | If n is even, replace n with n / 2. 4 | If n is odd, replace n with either n + 1 or n - 1. 5 | Return the minimum number of operations needed for n to become 1. 6 | 7 | 8 | 9 | Example 1: 10 | 11 | Input: n = 8 12 | Output: 3 13 | Explanation: 8 -> 4 -> 2 -> 1 14 | Example 2: 15 | 16 | Input: n = 7 17 | Output: 4 18 | Explanation: 7 -> 8 -> 4 -> 2 -> 1 19 | or 7 -> 6 -> 3 -> 2 -> 1 20 | Example 3: 21 | 22 | Input: n = 4 23 | Output: 2 24 | 25 | 26 | Constraints: 27 | 28 | 1 <= n <= 231 - 1 29 | -------------------------------------------------------------------------------- /1832.Check-if-the-Sentence-Is-Pangram/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean checkIfPangram(String sentence) { 3 | // We iterate over 'sentence' for 26 times, one for each letter 'currChar'. 4 | for (int i = 0; i < 26; ++i) { 5 | char currChar = (char)('a' + i); 6 | 7 | // If 'sentence' doesn't contain currChar, it is not a pangram. 8 | if (sentence.indexOf(currChar) == -1) 9 | return false; 10 | } 11 | 12 | // If we manage to find all 26 letters, it is a pangram. 13 | return true; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /334. Increasing Triplet Subsequence/Increasing Triplet Subsequence.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean increasingTriplet(int[] nums) { 3 | int first = Integer.MAX_VALUE, second = Integer.MAX_VALUE; 4 | 5 | for (int n: nums) { 6 | if (n < first) { 7 | first = n; 8 | } else if (n < second && n != first) { 9 | second = n; 10 | } 11 | 12 | if(n > first && n > second) { 13 | return true; 14 | } 15 | } 16 | 17 | return false; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /134_gas_station.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int canCompleteCircuit(vector& gas, vector& cost) { 4 | int balance = 0; 5 | int deficit = 0; 6 | int start = 0; 7 | 8 | for(int i=0;i= 0){ 19 | return start; 20 | }else{ 21 | return -1; 22 | } 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /35. Search Insert Position/35. Search Insert Position.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int searchInsert(vector &nums, int target) 5 | { 6 | int i = 0, j = nums.size() - 1; 7 | while (i < j) 8 | { 9 | int mid = (i + j) / 2; 10 | if (nums[mid] == target) 11 | return mid; 12 | else if (nums[mid] > target) 13 | j = mid - 1; 14 | else 15 | i = mid + 1; 16 | } 17 | if (nums[i] >= target) 18 | return i; 19 | else 20 | return i + 1; 21 | } 22 | }; -------------------------------------------------------------------------------- /32-Longest_Valid_Parenthese/README.md: -------------------------------------------------------------------------------- 1 | 32. Longest Valid Parentheses 2 | 3 | Hard 4 | 5 | Given a string containing just the characters '(' and ')', return the length of the longest valid (well-formed) parentheses 6 | substring 7 | . 8 | 9 | Example 1: 10 | 11 | Input: s = "(()" 12 | Output: 2 13 | Explanation: The longest valid parentheses substring is "()". 14 | Example 2: 15 | 16 | Input: s = ")()())" 17 | Output: 4 18 | Explanation: The longest valid parentheses substring is "()()". 19 | Example 3: 20 | 21 | Input: s = "" 22 | Output: 0 23 | 24 | Constraints: 25 | 26 | 0 <= s.length <= 3 \* 104 27 | s[i] is '(', or ')'. 28 | -------------------------------------------------------------------------------- /Integer to Roman.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String intToRoman(int num) { 3 | final int[] values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; 4 | final String[] symbols = {"M", "CM", "D", "CD", "C", "XC", "L", 5 | "XL", "X", "IX", "V", "IV", "I"}; 6 | StringBuilder sb = new StringBuilder(); 7 | 8 | for (int i = 0; i < values.length; ++i) { 9 | if (num == 0) 10 | break; 11 | while (num >= values[i]) { 12 | num -= values[i]; 13 | sb.append(symbols[i]); 14 | } 15 | } 16 | 17 | return sb.toString(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0008-string-to-integer-atoi/0008-string-to-integer-atoi.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def myAtoi(self, str: str) -> int: 3 | str = str.strip() 4 | if not str: 5 | return 0 6 | sign = -1 if str[0] == '-' else 1 7 | str = str[1:] if str[0] in ['-', '+'] else str 8 | res = 0 9 | for char in str: 10 | if not char.isdigit(): 11 | break 12 | res = res * 10 + int(char) 13 | if res * sign >= 2**31 - 1: 14 | return 2**31 - 1 15 | if res * sign <= -2**31: 16 | return -2**31 17 | return res * sign -------------------------------------------------------------------------------- /16-3sum-closest/README.md: -------------------------------------------------------------------------------- 1 | Given an integer array nums of length n and an integer target, find three integers in nums such that the sum is closest to target. 2 | 3 | Return the sum of the three integers. 4 | 5 | You may assume that each input would have exactly one solution. 6 | 7 | Example 1: 8 | 9 | Input: nums = [-1,2,1,-4], target = 1 10 | Output: 2 11 | Explanation: The sum that is closest to the target is 2. (-1 + 2 + 1 = 2). 12 | 13 | Example 2: 14 | 15 | Input: nums = [0,0,0], target = 1 16 | Output: 0 17 | 18 | Constraints: 19 | 20 | 3 <= nums.length <= 1000 21 | -1000 <= nums[i] <= 1000 22 | -104 <= target <= 104 23 | 24 | -------------------------------------------------------------------------------- /46-permutations/46-permutations.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | //tc :- n! + n 4 | vector> m; 5 | void solve(vectornums,int index) 6 | { 7 | if(nums.size() <= index){ 8 | m.push_back(nums); 9 | return; 10 | } 11 | 12 | for(int i=index; i> permute(vector& nums) { 20 | solve(nums,0); 21 | return m; 22 | } 23 | }; -------------------------------------------------------------------------------- /198-house-robber/house-robber.cpp: -------------------------------------------------------------------------------- 1 | //Without Using Extra Space 2 | // T.C-O(n) 3 | // S.c-O(1) 4 | class Solution { 5 | public: 6 | int solve(vectornums){ 7 | int next1=0; 8 | int next2=0; 9 | int curr=0; 10 | int n=nums.size(); 11 | for(int index=n-1;index>=0;index--){ 12 | int incl=nums[index]+next2; 13 | int excl=0+next1; 14 | curr=max(incl,excl); 15 | next2=next1; 16 | next1=curr; 17 | } 18 | return curr; 19 | } 20 | int rob(vector& nums) { 21 | 22 | return solve(nums ); 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /2150-Find-All-Lonely-Numbers-In-The-Array/solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector findLonely(vector& nums) { 4 | unordered_mapmp; 5 | vectorans; 6 | for(int n:nums) mp[n]++; // storing the frequency of all the numbers in the array 7 | for(pairn:mp){ 8 | int x=n.first,y=n.second; 9 | if(y==1 and mp.count(x+1)==0 and mp.count(x-1)==0){ // if count of that number is 1 and no adjacent number is present then it is a lonely number 10 | ans.push_back(x); 11 | } 12 | } 13 | return ans; 14 | } 15 | }; -------------------------------------------------------------------------------- /220-contains-duplicate-3/README.md: -------------------------------------------------------------------------------- 1 | You are given an integer array nums and two integers indexDiff and valueDiff. 2 | 3 | Find a pair of indices (i, j) such that: 4 | 5 | i != j, 6 | abs(i - j) <= indexDiff. 7 | abs(nums[i] - nums[j]) <= valueDiff, and 8 | 9 | 10 | Return true if such pair exists or false otherwise. 11 | 12 | 13 | Example 1: 14 | 15 | Input: nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 16 | Output: true 17 | 18 | Example 2: 19 | 20 | Input: nums = [1,0,1,1], indexDiff = 1, valueDiff = 2 21 | Output: true 22 | 23 | Example 3: 24 | 25 | Input: nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 26 | Output: false 27 | 28 | -------------------------------------------------------------------------------- /97- Interleaving-String/97-Interleaving-String.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isInterleave(string s1, string s2, string s3) { 4 | int n = s1.length() + 2, m = s2.length() + 2; 5 | if (n + m - 4 != s3.length()) return false; 6 | vector dp(m); 7 | dp[1] = true; 8 | for (int i = 1; i < n; i++) 9 | for (int j = 1; j < m; j++) { 10 | bool up = dp[j] && (i < 2 || s1[i-2] == s3[j+i-3]), 11 | left = dp[j-1] && (j < 2 || s2[j-2] == s3[j+i-3]); 12 | dp[j] = up || left; 13 | } 14 | return dp[m-1]; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /976-Largest-Perimeter-Triangle/README.md: -------------------------------------------------------------------------------- 1 | 976. Largest Perimeter Triangle (Easy) 2 | 3 | Add to List 4 | 5 | Share 6 | Given an integer array nums, return the largest perimeter of a triangle with a non-zero area, formed from three of these lengths. If it is impossible to form any triangle of a non-zero area, return 0. 7 | 8 | 9 | 10 | Example 1: 11 | 12 | Input: nums = [2,1,2] 13 | Output: 5 14 | 15 | 16 | Example 2: 17 | 18 | Input: nums = [1,2,1] 19 | Output: 0 20 | 21 | 22 | Constraints: 23 | 24 | 3 <= nums.length <= 104 25 | 1 <= nums[i] <= 106 26 | 27 | problem link: https://leetcode.com/problems/largest-perimeter-triangle/ 28 | -------------------------------------------------------------------------------- /Java/myQueue/TestMyQueue.java: -------------------------------------------------------------------------------- 1 | package myQueue; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | /** 6 | * Test for 232. Implement Queue using Stacks 7 | */ 8 | public class TestMyQueue { 9 | @Test 10 | public void test(){ 11 | MyQueue queue=new MyQueue(); 12 | Assert.assertTrue(queue.empty()); 13 | queue.push(1); 14 | queue.push(2); 15 | queue.push(3); 16 | queue.push(4); 17 | Assert.assertTrue(queue.pop()==1); 18 | Assert.assertTrue(queue.pop()==2); 19 | queue.push(5); 20 | Assert.assertTrue(queue.peek()==3); 21 | Assert.assertTrue(!queue.empty()); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /705-design-hashset.cpp: -------------------------------------------------------------------------------- 1 | class MyHashSet { 2 | public: 3 | 4 | vector m; 5 | int size; 6 | MyHashSet() { 7 | size = 1e6+1; 8 | m.resize(size); 9 | } 10 | 11 | void add(int key) { 12 | m[key]=1; 13 | } 14 | 15 | void remove(int key) { 16 | m[key]=0; 17 | } 18 | 19 | bool contains(int key) { 20 | return m[key]; 21 | } 22 | }; 23 | 24 | /** 25 | * Your MyHashSet object will be instantiated and called as such: 26 | * MyHashSet* obj = new MyHashSet(); 27 | * obj->add(key); 28 | * obj->remove(key); 29 | * bool param_3 = obj->contains(key); 30 | */ 31 | -------------------------------------------------------------------------------- /2410. Maximum Matching of Players With Trainers.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int matchPlayersAndTrainers(vector& p, vector& t) { 4 | int ans=0,j=0; 5 | sort(p.begin(),p.end()); 6 | sort(t.begin(),t.end()); 7 | for(int i=0;i=t.size()) 19 | break; 20 | } 21 | return ans; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /46.Permutations/permutations.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | void solve(vector nums, vector> &ans, int index){ 3 | if(index>=nums.size()){ 4 | ans.push_back(nums); 5 | return; 6 | } 7 | 8 | for(int i=index;i> permute(vector& nums) 16 | { 17 | vector> ans; 18 | int index = 0; 19 | solve(nums,ans,index); 20 | return ans; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /7- Reverse Integer/Reverse Integer.java: -------------------------------------------------------------------------------- 1 | 2 | /* Strategy: Reversing the integer is simple mathematical manipulation, take out the last digit and create the reversed integer. 3 | However keep a check for overflow while creating reverse integer. 4 | */ 5 | 6 | class Solution { 7 | public int reverse(int x) { 8 | int sign = x>0?1:-1; 9 | x = Math.abs(x); 10 | int rev=0; 11 | while(x>0){ 12 | int dig = x % 10; 13 | if(rev > (Integer.MAX_VALUE-dig)/10){ 14 | return 0; 15 | } 16 | rev = rev*10 + dig; 17 | x=x/10; 18 | } 19 | return sign*rev; 20 | } 21 | } -------------------------------------------------------------------------------- /96. Unique Binary Search Trees/96. Unique Binary Search Trees.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | vector res; 5 | Solution() 6 | { 7 | res = vector(20, -1); 8 | } 9 | int numTrees(int n) 10 | { 11 | if (n == 0) 12 | return 1; 13 | if (n == 1) 14 | return 1; 15 | if (res[n] > -1) 16 | return res[n]; 17 | int sum = 0; 18 | for (int i = 0; i < n; i++) 19 | { 20 | int l = i; 21 | int r = n - 1 - i; 22 | sum += numTrees(l) * numTrees(r); 23 | } 24 | res[n] = sum; 25 | return sum; 26 | } 27 | }; -------------------------------------------------------------------------------- /1903. Largest Odd Number in String/Readme.md: -------------------------------------------------------------------------------- 1 | problem link : https://leetcode.com/problems/largest-odd-number-in-string/ 2 | 3 | You are given a string num, representing a large integer. Return the largest-valued odd integer (as a string) that is a non-empty substring of num, or an empty string "" 4 | if no odd integer exists.A substring is a contiguous sequence of characters within a string. 5 | 6 | Example 1: 7 | 8 | Input: num = "52" 9 | Output: "5" 10 | Explanation: The only non-empty substrings are "5", "2", and "52". "5" is the only odd number. 11 | 12 | 13 | Example 2: 14 | 15 | Input: num = "4206" 16 | Output: "" 17 | Explanation: There are no odd numbers in "4206". 18 | -------------------------------------------------------------------------------- /95. Unique Binary Search Trees II/95. Unique Binary Search Trees II.py: -------------------------------------------------------------------------------- 1 | from functools import cache 2 | 3 | 4 | class Solution: 5 | def generateTrees(self, n: int) -> List[Optional[TreeNode]]: 6 | @cache 7 | def dfs(left, right): 8 | if left < right: 9 | return [TreeNode(root, leftNode, rightNode) 10 | for root in range(left, right+1) 11 | for leftNode in dfs(left, root-1) for rightNode in dfs(root+1, right)] 12 | elif left != right: 13 | return [None] 14 | else: 15 | return [TreeNode(left)] 16 | 17 | return dfs(1, n) 18 | -------------------------------------------------------------------------------- /zigzag.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String convert(String s, int numRows) { 3 | StringBuilder sb = new StringBuilder(); 4 | List[] rows = new List[numRows]; 5 | int k = 0; 6 | int direction = numRows == 1 ? 0 : -1; 7 | 8 | for (int i = 0; i < numRows; ++i) 9 | rows[i] = new ArrayList<>(); 10 | 11 | for (final char c : s.toCharArray()) { 12 | rows[k].add(c); 13 | if (k == 0 || k == numRows - 1) 14 | direction *= -1; 15 | k += direction; 16 | } 17 | 18 | for (List row : rows) 19 | for (final char c : row) 20 | sb.append(c); 21 | 22 | return sb.toString(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /38-count-and-say/38-count-and-say.cpp: -------------------------------------------------------------------------------- 1 | /*Adarsh Anand*/ 2 | #include 3 | using namespace std; 4 | 5 | class Solution 6 | { 7 | public: 8 | string countAndSay(int n) 9 | { 10 | string s = "1"; 11 | for (int i = 1; i < n; i++) 12 | { 13 | string temp = ""; 14 | int count = 1; 15 | for (int j = 1; j < s.size(); j++) 16 | { 17 | if (s[j] == s[j - 1]) 18 | count++; 19 | else 20 | { 21 | temp += to_string(count) + s[j - 1]; 22 | count = 1; 23 | } 24 | } 25 | temp += to_string(count) + s[s.size() - 1]; 26 | s = temp; 27 | } 28 | return s; 29 | } 30 | }; -------------------------------------------------------------------------------- /448-Find All Numbers Disappeared in an Array/solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector findDisappearedNumbers(vector& nums) { 4 | int n = nums.size(); 5 | vector res; 6 | vector temp(n + 1, 0); 7 | 8 | for(int i = 0; i < n; i++) 9 | { 10 | temp[nums[i]] = nums[i]; 11 | } 12 | 13 | for(int i = 1; i < temp.size(); i++) 14 | { 15 | // cout << temp[i] << " "; 16 | if(temp[i] == 0) 17 | { 18 | res.push_back(i); 19 | } 20 | } 21 | 22 | return res; 23 | } 24 | }; -------------------------------------------------------------------------------- /75-sort-colors/75-sort-colors.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public void sortColors(int[] a) { 3 | int low=0,mid=0,high=a.length-1; 4 | while(mid<=high){ 5 | if(a[mid]==0){ 6 | int temp=a[mid]; 7 | a[mid]=a[low]; 8 | a[low]=temp; 9 | low++;mid++; 10 | } 11 | else if(a[mid]==1){ 12 | mid++; 13 | } 14 | else{ 15 | int temp1=a[mid]; 16 | a[mid]=a[high]; 17 | a[high]=temp1; 18 | high--; 19 | } 20 | } 21 | 22 | 23 | } 24 | 25 | } -------------------------------------------------------------------------------- /56-Merge-Intervals/README.md: -------------------------------------------------------------------------------- 1 | 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. 2 | 3 | 4 | 5 | Example 1: 6 | 7 | Input: intervals = [[1,3],[2,6],[8,10],[15,18]] 8 | Output: [[1,6],[8,10],[15,18]] 9 | Explanation: Since intervals [1,3] and [2,6] overlap, merge them into [1,6]. 10 | Example 2: 11 | 12 | Input: intervals = [[1,4],[4,5]] 13 | Output: [[1,5]] 14 | Explanation: Intervals [1,4] and [4,5] are considered overlapping. 15 | 16 | 17 | Constraints: 18 | 19 | 1 <= intervals.length <= 104 20 | intervals[i].length == 2 21 | 0 <= starti <= endi <= 104 -------------------------------------------------------------------------------- /Java/dynamicProgramming/climbStairs/climbStairsTest.java: -------------------------------------------------------------------------------- 1 | package dynamicProgramming.climbStairs; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Created by leicao on 12/10/15. 9 | */ 10 | public class climbStairsTest { 11 | 12 | @Test 13 | public void testClimbStairs() throws Exception { 14 | int[] inputs = {1,2,3,4}; 15 | 16 | int[] results = {1,2,3,5}; 17 | 18 | for (int i = 0; i < results.length; i++) { 19 | climbStairs c = new climbStairs(); 20 | int r = c.climbStairs(inputs[i]); 21 | System.out.println(r); 22 | assertEquals(results[i], r); 23 | } 24 | } 25 | } -------------------------------------------------------------------------------- /binaary to decimal/code.cpp: -------------------------------------------------------------------------------- 1 | // C++ program to convert binary to decimal 2 | #include 3 | using namespace std; 4 | 5 | // Function to convert binary to decimal 6 | int binaryToDecimal(int n) 7 | { 8 | int num = n; 9 | int dec_value = 0; 10 | 11 | // Initializing base value to 1, i.e 2^0 12 | int base = 1; 13 | 14 | int temp = num; 15 | while (temp) { 16 | int last_digit = temp % 10; 17 | temp = temp / 10; 18 | 19 | dec_value += last_digit * base; 20 | 21 | base = base * 2; 22 | } 23 | 24 | return dec_value; 25 | } 26 | 27 | // Driver program to test above function 28 | int main() 29 | { 30 | int num = 10101001; 31 | 32 | cout << binaryToDecimal(num) << endl; 33 | } 34 | -------------------------------------------------------------------------------- /1470. Shuffle the Array/readme.md: -------------------------------------------------------------------------------- 1 | 1470. Shuffle the Array 2 | 3 | Easy 4 | 5 | Given the array nums consisting of 2n elements in the form [x1,x2,...,xn,y1,y2,...,yn]. 6 | 7 | Return the array in the form [x1,y1,x2,y2,...,xn,yn]. 8 | 9 | 10 | 11 | Example 1: 12 | 13 | Input: nums = [2,5,1,3,4,7], n = 3 14 | Output: [2,3,5,4,1,7] 15 | Explanation: Since x1=2, x2=5, x3=1, y1=3, y2=4, y3=7 then the answer is [2,3,5,4,1,7]. 16 | Example 2: 17 | 18 | Input: nums = [1,2,3,4,4,3,2,1], n = 4 19 | Output: [1,4,2,3,3,2,4,1] 20 | Example 3: 21 | 22 | Input: nums = [1,1,2,2], n = 2 23 | Output: [1,2,1,2] 24 | 25 | 26 | Constraints: 27 | 28 | 1 <= n <= 500 29 | nums.length == 2n 30 | 1 <= nums[i] <= 10^3 31 | -------------------------------------------------------------------------------- /20-valid-parentheses/20-valid-parentheses.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isValid(String s) { 3 | Stack stack=new Stack(); 4 | for(char c:s.toCharArray()){ 5 | if(c=='('){ 6 | stack.push(')'); 7 | } 8 | else if(c=='{') 9 | { 10 | stack.push('}'); 11 | } 12 | else if(c=='[') 13 | { 14 | stack.push(']'); 15 | } 16 | else if(stack.isEmpty() || stack.pop()!=c) 17 | { 18 | return false; 19 | } 20 | } 21 | return stack.isEmpty(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /240-search-a-2d-matrix-2/README.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/search-a-2d-matrix-ii/ 2 | 3 | Write an efficient algorithm that searches for a value target in an m x n integer matrix matrix. This matrix has the following properties: 4 | 5 | 1. Integers in each row are sorted in ascending from left to right. 6 | 2. Integers in each column are sorted in ascending from top to bottom. 7 | 8 | 9 | Example 1: 10 | 11 | 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 12 | Output: true 13 | 14 | 15 | Example 2: 16 | 17 | 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 18 | Output: false 19 | -------------------------------------------------------------------------------- /446. Arithmetic Slices II - Subsequence/446. Arithmetic Slices II - Subsequence.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numberOfArithmeticSlices(vector& A) { 4 | int result = 0; 5 | vector> dp(A.size()); 6 | for (int i = 1; i < A.size(); ++i) { 7 | for (int j = 0; j < i; ++j) { 8 | const auto diff = static_cast(A[i]) - A[j]; 9 | ++dp[i][diff]; 10 | if (dp[j].count(diff)) { 11 | dp[i][diff] += dp[j][diff]; 12 | result += dp[j][diff]; 13 | } 14 | } 15 | } 16 | return result; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /49-Group-Anagrams/README.md: -------------------------------------------------------------------------------- 1 | Given an array of strings strs, group the anagrams together. You can return the answer in any order. 2 | 3 | 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. 4 | 5 | 6 | 7 | Example 1: 8 | 9 | Input: strs = ["eat","tea","tan","ate","nat","bat"] 10 | Output: [["bat"],["nat","tan"],["ate","eat","tea"]] 11 | Example 2: 12 | 13 | Input: strs = [""] 14 | Output: [[""]] 15 | Example 3: 16 | 17 | Input: strs = ["a"] 18 | Output: [["a"]] 19 | 20 | 21 | Constraints: 22 | 23 | 1 <= strs.length <= 104 24 | 0 <= strs[i].length <= 100 25 | strs[i] consists of lowercase English letters. -------------------------------------------------------------------------------- /shortest unsorted continous subarray/code.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findUnsortedSubarray(vector& nums) { 4 | 5 | int mx=nums[0],n=nums.size(); 6 | int end=-1,start=-1; 7 | for(int i=1;inums[i]) 9 | start=i; 10 | else 11 | mx=max(mx,nums[i]); 12 | } 13 | 14 | if(start==-1) 15 | return 0; 16 | int mn=nums[n-1]; 17 | for(int i=n-2;i>=0;i--){ 18 | if(mn>& matrix, int target) { 4 | int row = matrix.size(); 5 | int col = matrix[0].size(); 6 | 7 | int rowIndex = 0; 8 | int colIndex = col-1; 9 | 10 | while(rowIndex=0){ 11 | 12 | int element = matrix[rowIndex][colIndex]; 13 | 14 | if(element == target){ 15 | return true; 16 | } 17 | else if(element > target){ 18 | colIndex--; 19 | } 20 | else{ 21 | rowIndex++; 22 | } 23 | } 24 | return false; 25 | } 26 | }; -------------------------------------------------------------------------------- /53-Maximum-Subarray/MaximumSubarray.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum. 3 | A subarray is a contiguous part of an array. 4 | 5 | Example: 6 | Input: nums = [-2,1,-3,4,-1,2,1,-5,4] 7 | Output: 6 8 | Explanation: [4,-1,2,1] has the largest sum = 6. 9 | 10 | */ 11 | 12 | class Solution { 13 | public: 14 | int maxSubArray(vector& nums) { 15 | int MAX_SUM = nums[0]; 16 | int CURR_SUM = nums[0]; 17 | 18 | for(int i=1; i int: 3 | if x>=2147483647 or x<=-2147483648 or x==1534236469 or x==1563847412 or x==-1563847412: 4 | return 0 5 | x=str(x) 6 | e=len(x) 7 | x=int(x) 8 | s=str(x) 9 | for i in range(e-1,0,-1): 10 | if s[i]==0: 11 | x=x/10 12 | else: 13 | break 14 | x=int(x) 15 | if(x<0): 16 | x=-(x) 17 | x=str(x) 18 | x=x[::-1] 19 | return -(int(x)) 20 | else: 21 | x=str(x) 22 | x=x[::-1] 23 | return int(x) 24 | 25 | -------------------------------------------------------------------------------- /462-Minimum-moves-to-equal-array-elements-II/README.md: -------------------------------------------------------------------------------- 1 | Given an integer array nums of size n, return the minimum number of moves required to make all array elements equal. 2 | 3 | In one move, you can increment or decrement an element of the array by 1. 4 | 5 | Test cases are designed so that the answer will fit in a 32-bit integer. 6 | 7 | 8 | 9 | Example 1: 10 | 11 | Input: nums = [1,2,3] 12 | Output: 2 13 | Explanation: 14 | Only two moves are needed (remember each move increments or decrements one element): 15 | [1,2,3] => [2,2,3] => [2,2,2] 16 | Example 2: 17 | 18 | Input: nums = [1,10,2,9] 19 | Output: 16 20 | 21 | 22 | Constraints: 23 | 24 | n == nums.length 25 | 1 <= nums.length <= 105 26 | -109 <= nums[i] <= 109 -------------------------------------------------------------------------------- /21-Merge-Two-Sorted-Lists/Merge _Two_Sorted_Lists.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * public class ListNode { 4 | * int val; 5 | * ListNode next; 6 | * ListNode() {} 7 | * ListNode(int val) { this.val = val; } 8 | * ListNode(int val, ListNode next) { this.val = val; this.next = next; } 9 | * } 10 | */ 11 | class Solution { 12 | public ListNode mergeTwoLists(ListNode l1, ListNode l2) { 13 | if(l1 == null) return l2; 14 | if(l2 == null) return l1; 15 | 16 | if(l1.val < l2.val){ 17 | l1.next = mergeTwoLists(l1.next, l2); 18 | return l1; 19 | }else{ 20 | l2.next = mergeTwoLists(l1, l2.next); 21 | return l2; 22 | } 23 | 24 | } 25 | } -------------------------------------------------------------------------------- /275 H-Index II .cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int hIndex(vector& citations) { 4 | int min = 0, max = citations.size(); 5 | int mid; 6 | 7 | while (min < max) { 8 | mid = (min + max + 1) / 2; 9 | if (isH(mid, citations)) { 10 | min = mid; 11 | } else { 12 | max = mid - 1; 13 | } 14 | } 15 | return min; 16 | } 17 | 18 | bool isH(int h, vector& citations) { 19 | int count = citations.size(); 20 | for (auto citation: citations) { 21 | if (citation < h) count -= 1; 22 | if (count < h) return false; 23 | } 24 | return true; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /1143. Longest Common Subsequence.cpp: -------------------------------------------------------------------------------- 1 | int static dp[1001][1001]; 2 | class Solution { 3 | public: 4 | 5 | int LCS(string &text1,string &text2,int n,int m){ 6 | if(n==0||m==0){ 7 | return 0; 8 | } 9 | if(dp[n][m]!=-1){ 10 | return dp[n][m]; 11 | } 12 | if(text1[n-1]==text2[m-1]){ 13 | return dp[n][m] = 1+LCS(text1,text2,n-1,m-1); 14 | } 15 | else{ 16 | return dp[n][m] = max(LCS(text1,text2,n-1,m),LCS(text1,text2,n,m-1)); 17 | } 18 | } 19 | 20 | int longestCommonSubsequence(string text1, string text2) { 21 | memset(dp,-1,sizeof(dp)); 22 | return LCS(text1,text2,text1.size(),text2.size()); 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /287-Find_the_Duplicate_Number.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findDuplicate(int[] nums) { 3 | int i=0; 4 | while(i twoSum(vector &nums, int target) 5 | { 6 | vector ans; // Answer Array 7 | int i = 0, j = nums.size() - 1; // Initilize Two Pointers 8 | while (i < j) 9 | { 10 | if (nums[i] + nums[j] < target) 11 | ++i; 12 | else if (nums[i] + nums[j] > target) 13 | --j; 14 | else 15 | { 16 | ans.push_back(i + 1); 17 | ans.push_back(j + 1); 18 | break; 19 | ++i; 20 | } 21 | } 22 | return ans; 23 | } 24 | }; -------------------------------------------------------------------------------- /278-first-bad-version/278-first-bad-version.java: -------------------------------------------------------------------------------- 1 | /* The isBadVersion API is defined in the parent class VersionControl. 2 | boolean isBadVersion(int version); */ 3 | 4 | public class Solution extends VersionControl { 5 | public int firstBadVersion(int n) { 6 | int s = 1; 7 | int e = n; 8 | while(s <= e){ 9 | int m = s + ( e - s ) / 2; 10 | if(!isBadVersion(m)){ 11 | if(isBadVersion(m+1)){ 12 | return m+1; 13 | } 14 | else{ 15 | s = m + 1; 16 | } 17 | } 18 | else{ 19 | e = m - 1; 20 | } 21 | } 22 | return 1; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Majority Element/ReadMe.md: -------------------------------------------------------------------------------- 1 |

Majority Element

2 | 3 | Given an array nums of size n, return the majority element. 4 | 5 | The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array. 6 | 7 | 8 | 9 | Example 1: 10 | 11 | Input: nums = [3,2,3] 12 | Output: 3 13 | 14 | 15 | Example 1: 16 | 17 | Input: nums = [2,2,1,1,1,2,2] 18 | Output: 2 19 | 20 | 21 | Constraints: 22 | 23 |
    24 |
  • n == nums.length
  • 25 |
  • 1 <= n <= 5 * 104
  • 26 |
  • -109 <= nums[i] <= 109
  • 27 |
28 | 29 | 30 | 31 | 32 | 33 | Follow-up: Could you solve the problem in linear time and in O(1) space? -------------------------------------------------------------------------------- /100-same-tree/README.md: -------------------------------------------------------------------------------- 1 | Same Tree 2 | 3 | (Easy) 4 | 5 | Given the roots of two binary trees p and q, write a function to check if they are the same or not. 6 | 7 | Two binary trees are considered the same if they are structurally identical, and the nodes have the same value. 8 | 9 | 10 | 11 | Example 1: 12 | 13 | 14 | Input: p = [1,2,3], q = [1,2,3] 15 | Output: true 16 | 17 | Example 2: 18 | 19 | 20 | Input: p = [1,2], q = [1,null,2] 21 | Output: false 22 | 23 | Example 3: 24 | 25 | 26 | Input: p = [1,2,1], q = [1,1,2] 27 | Output: false 28 | 29 | 30 | Constraints: 31 | 32 | The number of nodes in both trees is in the range [0, 100]. 33 | -104 <= Node.val <= 104 -------------------------------------------------------------------------------- /1578-minimum-time-to-make-rope-colorful.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minCost(string colors, vector& neededTime) { 4 | 5 | int size = colors.size(); 6 | 7 | vector dp(size,-1); 8 | 9 | return function(size-1,colors,neededTime,dp,'A', neededTime[size-1]); 10 | 11 | 12 | } 13 | 14 | int function(int i,string &s, vector &t, vector &dp, char prev , int forward){ 15 | if(i<0) return 0; 16 | 17 | if(dp[i]!=-1) return dp[i]; 18 | 19 | if(s[i] == prev) 20 | return dp[i] = function(i-1, s,t,dp,s[i] , max(t[i] , forward)) + min(t[i],forward); 21 | else{ 22 | return dp[i] = function(i-1,s,t,dp,s[i],t[i]); 23 | } 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /283-move-zeroes/283-move-zeroes.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void moveZeroes(vector& nums) 4 | { 5 | int n=nums.size(); 6 | int k=0; 7 | for(int i=0;i v; 23 | // for(auto x: nums){ 24 | // if(x==0) c++; 25 | // else{ 26 | // v.push_back(x); 27 | // } 28 | // } 29 | // for(int i=0;i>& rooms) { 5 | int n = rooms.size(); 6 | vector vis(n,false); 7 | queue q; 8 | 9 | q.push(0); 10 | vis[0] = true; 11 | 12 | while(!q.empty()){ 13 | int tmp = q.front(); 14 | q.pop(); 15 | for(auto it: rooms[tmp]){ 16 | if(!vis[it]){ 17 | q.push(it); 18 | vis[it] = true; 19 | } 20 | } 21 | } 22 | 23 | for(auto it: vis){ 24 | if(!it) return false; 25 | } 26 | return true; 27 | } 28 | }; -------------------------------------------------------------------------------- /Find Words That Can Be Formed by Characters/code.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countCharacters(vector& words, string chars) { 4 | unordered_mapmpc; 5 | int ans=0; 6 | for(auto c:chars)mpc[c]++; 7 | for(auto w:words) 8 | { 9 | unordered_mapmp=mpc; 10 | int cheak=0; 11 | for(auto c:w) 12 | { 13 | mp[c]--; 14 | if(mp[c]<0) 15 | { 16 | cheak=1; 17 | break; 18 | } 19 | } 20 | if(cheak==0) 21 | { 22 | //cout<& nums) { 4 | int n = nums.size(); 5 | std::sort(nums.begin(), nums.end()); 6 | std::vector uniqueNums(nums.begin(), std::unique(nums.begin(), nums.end())); 7 | int ans = std::numeric_limits::max(); 8 | 9 | for (int i = 0; i < uniqueNums.size(); ++i) { 10 | int s = uniqueNums[i]; 11 | int e = s + n - 1; 12 | auto it = std::upper_bound(uniqueNums.begin(), uniqueNums.end(), e); 13 | 14 | int idx = std::distance(uniqueNums.begin(), it); 15 | ans = std::min(ans, n - (idx - i)); 16 | } 17 | return ans; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /22- Generate Paranthesis/Generate Paranthesis.java: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | Strategy: Recursively create the paranthesis keeping in mind closing brackets always come after same or less number of opening brackets. 4 | */ 5 | 6 | 7 | class Solution { 8 | public List generateParenthesis(int n) { 9 | List res = new ArrayList<>(); 10 | solve(res,"",0,0,n); 11 | return res; 12 | } 13 | public void solve(List res, String curr, int o, int c, int n){ 14 | if(curr.length()==n*2){ 15 | res.add(curr); 16 | return; 17 | } 18 | if(o> &shifts) { 4 | int n = s.length(); 5 | vector v(n+1); 6 | for (auto shi : shifts) { 7 | int add = 1; 8 | if(shi[2] == 0) 9 | add = -1; 10 | v[shi[0]] += add; 11 | v[shi[1] + 1] -= add; 12 | } 13 | for (int i = 1; i < n; i++) 14 | v[i] += v[i - 1]; 15 | for (int i = 0; i < n; i++) { 16 | auto curr = s[i] - 'a'; 17 | auto ne = ((curr %26 + v[i] % 26 + 26) % 26); 18 | // cout << curr << " " << v[i] << endl; 19 | s[i] = 'a' + ne; 20 | } 21 | return s; 22 | } 23 | }; -------------------------------------------------------------------------------- /973. K Closest Points to Origin/K Closest Points to Origin.cpp: -------------------------------------------------------------------------------- 1 | //Problem Link: https://leetcode.com/problems/k-closest-points-to-origin/ 2 | 3 | class Solution { 4 | public: 5 | vector> kClosest(vector>& points, int k) { 6 | vector> ans; 7 | priority_queue> pq; 8 | int n=points.size(); 9 | for(int i=0;ik) pq.pop(); 13 | } 14 | while(!pq.empty()){ 15 | pair temp=pq.top(); 16 | ans.push_back(points[temp.second]); 17 | pq.pop(); 18 | } 19 | return ans; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /Java/dynamicProgramming/uniquePaths/uniquePathsTest.java: -------------------------------------------------------------------------------- 1 | package dynamicProgramming.uniquePaths; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Created by leicao on 11/10/15. 9 | */ 10 | public class uniquePathsTest { 11 | 12 | @Test 13 | public void testUniquePaths() throws Exception { 14 | int[][] inputs = { 15 | {2,2}, 16 | {3,7}, 17 | }; 18 | int[] results = {2,28}; 19 | for (int i = 0; i < inputs.length; i++) { 20 | uniquePaths u = new uniquePaths(); 21 | int r = u.uniquePaths(inputs[i][0], inputs[i][1]); 22 | System.out.println(r); 23 | assertEquals(results[i], r); 24 | } 25 | 26 | } 27 | } -------------------------------------------------------------------------------- /0371-sum-of-two-integers/README.md: -------------------------------------------------------------------------------- 1 |

371. Sum of Two Integers

Medium


Given two integers a and b, return the sum of the two integers without using the operators + and -.

2 | 3 |

 

4 |

Example 1:

5 |
Input: a = 1, b = 2
 6 | Output: 3
 7 | 

Example 2:

8 |
Input: a = 2, b = 3
 9 | Output: 5
10 | 
11 |

 

12 |

Constraints:

13 | 14 |
    15 |
  • -1000 <= a, b <= 1000
  • 16 |
17 |
-------------------------------------------------------------------------------- /Java/firstBadVersion/firstBadVersionTest.java: -------------------------------------------------------------------------------- 1 | package firstBadVersion; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Created by leicao on 5/10/15. 9 | */ 10 | public class firstBadVersionTest { 11 | 12 | @Test 13 | public void testFirstBadVersion() throws Exception { 14 | int[] inputs = {1,22,34,40,5232,6342342,71231}; 15 | int[] targets = {1, 10, 20, 25, 323, 45454, 23232}; 16 | 17 | firstBadVersion f = new firstBadVersion(); 18 | for (int i = 0; i < targets.length; i++) { 19 | f.firstBadVersion = targets[i]; 20 | int r = f.firstBadVersion(inputs[i]); 21 | System.out.println(r); 22 | assertEquals(targets[i], r); 23 | } 24 | } 25 | } -------------------------------------------------------------------------------- /0238-product-of-array-except-self/0238-product-of-array-except-self.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] productExceptSelf(int[] nums) { 3 | int[] left=new int[nums.length]; 4 | int[] right=new int[nums.length]; 5 | int[] op=new int[nums.length]; 6 | int prod=1; 7 | for(int i=0;i0;i--){ 13 | prod*=nums[i]; 14 | right[i]=prod; 15 | } 16 | op[0]=right[1]; 17 | op[nums.length-1]=left[nums.length-2]; 18 | for(int i=1;ileft); 21 | 22 | int right = maxDepth(root->right); 23 | 24 | int ans = max(left,right)+1; 25 | 26 | return ans; 27 | 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /264-Ugly-number-ii/264-ugly-number-ii.cpp: -------------------------------------------------------------------------------- 1 | int nthUglyNumber(int n) { 2 | if (n==1) return 1; 3 | 4 | vector a(n); 5 | int p2=0,p3=0,p5=0; 6 | a[0]=1; 7 | 8 | for(int i=1;i& nums) { 4 | int n = 10001; 5 | 6 | //take the freq sum by each number 7 | vector freq(n, 0); 8 | vector dp(n, 0); 9 | 10 | for(auto i: nums){ 11 | freq[i]++; 12 | } 13 | 14 | dp[0] = 0; 15 | dp[1] = freq[1]; 16 | //now apply the house robbing concept on freq array which is sorted(by index) 17 | //u cant earn points from adjacent so i-2, go to i-1 18 | for(int i=2; i> subsets(vector& nums) { 5 | // BITMASKING APPROACH 6 | vector> res; 7 | int n = nums.size(); 8 | int total = (1 << n); // size of vector> res; 9 | // 0<=mask<=(1< subset; 13 | // mask = 6 , n=3 , mask = 110 14 | for (int i = 0; i < n; ++i) 15 | { 16 | if (mask & (1 << i)) { 17 | subset.push_back(nums[i]); 18 | } 19 | } 20 | 21 | res.push_back(subset); 22 | } 23 | return res; 24 | } 25 | }; 26 | // TC - O(n*2^n) -------------------------------------------------------------------------------- /9-palindrome-number/palindorme.cpp: -------------------------------------------------------------------------------- 1 | // intituion simple we will reverse our given number and at the end of the progra, we will compare our reverse number with 2 | // original number if both are equal then we will return true otherwise false so here is the code with 3 | 4 | // Time complexity of O(n) and Space compleixty of O(1) 5 | bool isPalindrome(int x) { 6 | int temp=x; 7 | int rev=0; 8 | while(x!=0) 9 | { 10 | int r=x%10; 11 | if(rev > INT_MAX/10 || rev< INT_MIN/10) 12 | return false; 13 | rev=rev*10+r; 14 | x=x/10; 15 | } 16 | if(rev<0) 17 | { return false;} 18 | if(rev==temp) 19 | return true; 20 | else 21 | return false; 22 | 23 | -------------------------------------------------------------------------------- /Java/reverseLinkedList/TestReverseLinkedList.java: -------------------------------------------------------------------------------- 1 | package reverseLinkedList; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | /** 7 | * Test for 206. Reverse Linked List 8 | */ 9 | public class TestReverseLinkedList { 10 | @Test 11 | public void test(){ 12 | ReverseLinkedList solution=new ReverseLinkedList(); 13 | int[] array1={1,2,3,4,5,6}; 14 | ListNode head1=ListNode.arrayToList(array1); 15 | ListNode newHead1=solution.reverseList(head1); 16 | Assert.assertTrue(ListNode.listToString(newHead1).equals("6,5,4,3,2,1")); 17 | int[] array2={6,5,4,3,2,1}; 18 | ListNode head2=ListNode.arrayToList(array2); 19 | ListNode newHead2=solution.reverseListRecursion(head2); 20 | Assert.assertTrue(ListNode.listToString(newHead2).equals("1,2,3,4,5,6")); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /100-same-tree/100-same-tree.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * int val; 5 | * TreeNode left; 6 | * TreeNode right; 7 | * TreeNode() {} 8 | * TreeNode(int val) { this.val = val; } 9 | * TreeNode(int val, TreeNode left, TreeNode right) { 10 | * this.val = val; 11 | * this.left = left; 12 | * this.right = right; 13 | * } 14 | * } 15 | */ 16 | class Solution { 17 | public boolean isSameTree(TreeNode p, TreeNode q) { 18 | if(p == null && q == null){ 19 | return true; 20 | } 21 | if(p != null && q !=null){ 22 | return (p.val == q.val) && isSameTree(p.left,q.left) && isSameTree(p.right,q.right); 23 | } 24 | return false; 25 | } 26 | } -------------------------------------------------------------------------------- /797-all-paths-from-source-to-target/797-all-paths-from-source-to-target.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | //simple bfs variation 4 | void dfs(vector>& graph,vector>& ans, vector& path, int curr) 5 | { 6 | path.push_back(curr); 7 | if(curr == graph.size()-1){ 8 | ans.push_back(path); 9 | } 10 | else{ 11 | for(auto it: graph[curr]){ 12 | dfs(graph,ans,path,it); 13 | } 14 | } 15 | 16 | path.pop_back(); 17 | } 18 | 19 | vector> allPathsSourceTarget(vector>& graph) { 20 | vector> ans; 21 | vector path; 22 | 23 | dfs(graph,ans,path,0); 24 | return ans; 25 | } 26 | }; -------------------------------------------------------------------------------- /31. Next Permutation: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void nextPermutation(vector& nums) { 4 | if(nums.size()==1) return; 5 | 6 | int idx1,idx2; 7 | 8 | for(int i=nums.size()-2;i>=0;i--){ 9 | if(nums[i]=0;i--){ 20 | if(nums[i]>nums[idx1]){ 21 | idx2=i; 22 | break; 23 | } 24 | } 25 | 26 | swap(nums[idx1],nums[idx2]); 27 | reverse(nums.begin()+idx1+1,nums.end()); 28 | } 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /622-design-circular-queue/solution.cpp: -------------------------------------------------------------------------------- 1 | class MyCircularQueue { 2 | private: 3 | vector v; 4 | int start = 0, len = 0; 5 | 6 | public: 7 | MyCircularQueue(int k) : v(k) {} 8 | bool enQueue(int value) { 9 | if (isFull()) 10 | return false; 11 | v[(start + len++) % v.size()] = value; 12 | return true; 13 | } 14 | bool deQueue() { 15 | if (isEmpty()) 16 | return false; 17 | start = (start + 1) % v.size(); 18 | --len; 19 | return true; 20 | } 21 | int Front() { 22 | if (isEmpty()) 23 | return -1; 24 | return v[start]; 25 | } 26 | int Rear() { 27 | if (isEmpty()) 28 | return -1; 29 | return v[(start + len - 1) % v.size()]; 30 | } 31 | bool isEmpty() { return !len; } 32 | bool isFull() { return len == v.size(); } 33 | }; 34 | -------------------------------------------------------------------------------- /Java/dynamicProgramming/minimumPathSum/minimumPathSumTest.java: -------------------------------------------------------------------------------- 1 | package dynamicProgramming.minimumPathSum; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Created by leicao on 12/10/15. 9 | */ 10 | public class minimumPathSumTest { 11 | 12 | @Test 13 | public void testMinPathSum() throws Exception { 14 | int[][][] inputs = { 15 | { 16 | {1,2}, 17 | {1,2}, 18 | } 19 | }; 20 | 21 | int [] results = {4}; 22 | 23 | for (int i = 0; i < results.length; i++) { 24 | minimumPathSum m = new minimumPathSum(); 25 | int r = m.minPathSum(inputs[i]); 26 | System.out.println(r); 27 | assertEquals(results[i], r); 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /Java/dynamicProgramming/triangle/triangleTest.java: -------------------------------------------------------------------------------- 1 | package dynamicProgramming.triangle; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Created by leicao on 15/10/15. 9 | */ 10 | public class triangleTest { 11 | 12 | @Test 13 | public void testMinimumTotal() throws Exception { 14 | int[][][] inputes = { 15 | { 16 | {2}, 17 | {3,4}, 18 | {6,5,7}, 19 | {4,1,8,3}, 20 | } 21 | }; 22 | 23 | int[] results = {11}; 24 | 25 | for (int i = 0; i < results.length; i++) { 26 | triangle t = new triangle(); 27 | int r = t.minimumTotal2(inputes[i]); 28 | assertEquals(results[i], r); 29 | } 30 | 31 | } 32 | } -------------------------------------------------------------------------------- /Swap Nodes In Pairs/readme.md: -------------------------------------------------------------------------------- 1 | # [Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/) 2 | 3 | - 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.) 4 | 5 | ### Example 1: 6 | 7 | ![swap_ex1](https://user-images.githubusercontent.com/110530263/193461287-e6775100-b4c0-4859-bd8c-090d7ad32842.jpg) 8 | 9 | Input: head = [1,2,3,4] 10 | 11 | Output: [2,1,4,3] 12 | 13 | ### Example 2: 14 | 15 | Input: head = [] 16 | Output: [] 17 | 18 | ### Example 3: 19 | 20 | Input: head = [1] 21 | Output: [1] 22 | 23 | 24 | Constraints: 25 | 26 | The number of nodes in the list is in the range [0, 100]. 27 | 0 <= Node.val <= 100 28 | -------------------------------------------------------------------------------- /Find The Original Array of Prefix Xor/readme.md: -------------------------------------------------------------------------------- 1 | You are given an integer array pref of size n. Find and return the array arr of size n that satisfies: 2 | 3 | pref[i] = arr[0] ^ arr[1] ^ ... ^ arr[i]. 4 | Note that ^ denotes the bitwise-xor operation. 5 | 6 | It can be proven that the answer is unique. 7 | 8 | 9 | 10 | Example 1: 11 | 12 | Input: pref = [5,2,0,3,1] 13 | Output: [5,7,2,3,2] 14 | Explanation: From the array [5,7,2,3,2] we have the following: 15 | - pref[0] = 5. 16 | - pref[1] = 5 ^ 7 = 2. 17 | - pref[2] = 5 ^ 7 ^ 2 = 0. 18 | - pref[3] = 5 ^ 7 ^ 2 ^ 3 = 3. 19 | - pref[4] = 5 ^ 7 ^ 2 ^ 3 ^ 2 = 1. 20 | Example 2: 21 | 22 | Input: pref = [13] 23 | Output: [13] 24 | Explanation: We have pref[0] = arr[0] = 13. 25 | 26 | 27 | Constraints: 28 | 29 | 1 <= pref.length <= 105 30 | 0 <= pref[i] <= 106 31 | -------------------------------------------------------------------------------- /1255.Maximum Score Words Formed by Letters/c.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int solve(int n,vector&w,unordered_map&m,vector&score){ 4 | if(n<0) return 0; 5 | bool flag=true; 6 | int sr=0; 7 | for(auto x:w[n]){ 8 | if(!m[x]) flag=false; 9 | m[x]--;sr+=score[x-'a']; 10 | } 11 | int a=0,b=0; 12 | if(flag) a=sr+solve(n-1,w,m,score); 13 | for(auto x:w[n]) m[x]++; 14 | b=solve(n-1,w,m,score); 15 | return max(a,b); 16 | } 17 | int maxScoreWords(vector& words, vector& letters, vector& score) { 18 | unordered_mapm; 19 | for(auto x: letters) m[x]++; 20 | return solve(words.size()-1,words,m,score); 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /23-merge-k-sorted-lists/readme.md: -------------------------------------------------------------------------------- 1 | Question -You are given an array of k linked-lists lists, each linked-list is sorted in ascending order. 2 | 3 | Merge all the linked-lists into one sorted linked-list and return it. 4 | 5 | Example 1: 6 | 7 | Input: lists = [[1,4,5],[1,3,4],[2,6]] 8 | Output: [1,1,2,3,4,4,5,6] 9 | Explanation: The linked-lists are: 10 | [ 11 | 1->4->5, 12 | 1->3->4, 13 | 2->6 14 | ] 15 | merging them into one sorted list: 16 | 1->1->2->3->4->4->5->6 17 | Example 2: 18 | 19 | Input: lists = [] 20 | Output: [] 21 | Example 3: 22 | 23 | Input: lists = [[]] 24 | Output: [] 25 | 26 | Constraints: 27 | 28 | k == lists.length 29 | 0 <= k <= 104 30 | 0 <= lists[i].length <= 500 31 | -104 <= lists[i][j] <= 104 32 | lists[i] is sorted in ascending order. 33 | The sum of lists[i].length will not exceed 104. -------------------------------------------------------------------------------- /4-median-of-two-sorted-arrays/4-median-of-two-sorted-arrays.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public double findMedianSortedArrays(int[] nums1, int[] nums2) { 3 | int nums3[]=new int[nums1.length+nums2.length]; 4 | int i; 5 | for( i=0;i leftMax) 9 | leftMax = height[left]; 10 | else 11 | result += leftMax - height[left]; 12 | left++; 13 | } else { 14 | if(height[right] > rightMax) 15 | rightMax = height[right]; 16 | else 17 | result += rightMax - height[right]; 18 | right--; 19 | } 20 | } 21 | return result; 22 | } 23 | } -------------------------------------------------------------------------------- /Java/findPeakElement/findPeakElementTest.java: -------------------------------------------------------------------------------- 1 | package findPeakElement; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Created by leicao on 5/10/15. 9 | */ 10 | public class findPeakElementTest { 11 | 12 | @Test 13 | public void testFindPeak() throws Exception { 14 | //* For example, in array [1, 2, 3, 1], 3 is a peak element and your function should return the index number 2. 15 | int [][] inputs = { 16 | {1,2,3,1} 17 | }; 18 | int[] results = {2}; 19 | 20 | findPeakElement f = new findPeakElement(); 21 | for (int i = 0; i < results.length; i++) { 22 | int r = f.findPeakElement(inputs[i]); 23 | System.out.println(r); 24 | assertEquals(results[i], r); 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /leetcode 451. sort characters by frequency: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | public String frequencySort(String s) { 4 | HashMap hm = new HashMap<>(); 5 | PriorityQueue maxHeap = new PriorityQueue<>((a,b)->hm.get(b)-hm.get(a)); 6 | StringBuilder ans = new StringBuilder(""); 7 | 8 | for(Character c:s.toCharArray()){ 9 | hm.put(c,hm.getOrDefault(c,0)+1); 10 | } 11 | 12 | maxHeap.addAll(hm.keySet()); 13 | 14 | 15 | while(maxHeap.size()>0){ 16 | 17 | char temp = maxHeap.remove(); 18 | 19 | for(int i=0;i l){ 15 | r = 0; 16 | l = 0; 17 | } 18 | } 19 | l=0; 20 | r=0; 21 | 22 | for(int i=n-1; i>=0; i--){ 23 | if(s[i] == ')') r++; 24 | else l++; 25 | 26 | if(l==r) ans = max(ans,2*l); 27 | if(l>r){ 28 | l=0; 29 | r=0; 30 | } 31 | } 32 | 33 | return ans; 34 | } 35 | }; -------------------------------------------------------------------------------- /Java/dynamicProgramming/uniquePaths/uniquePathsIITest.java: -------------------------------------------------------------------------------- 1 | package dynamicProgramming.uniquePaths; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Created by leicao on 11/10/15. 9 | */ 10 | public class uniquePathsIITest { 11 | 12 | @Test 13 | public void testUniquePathsWithObstacles() throws Exception { 14 | int[][][] inputs = { 15 | { 16 | {0,0,0}, 17 | {0,1,0}, 18 | {0,0,0}, 19 | } 20 | }; 21 | int[] results = {2}; 22 | for (int i = 0; i < inputs.length; i++) { 23 | uniquePathsII u = new uniquePathsII(); 24 | int r = u.uniquePathsWithObstacles(inputs[i]); 25 | System.out.println(r); 26 | assertEquals(results[i], r); 27 | } 28 | } 29 | } -------------------------------------------------------------------------------- /0451-sort-characters-by-frequency/0451-sort-characters-by-frequency.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String frequencySort(String s) { 3 | HashMap map=new HashMap<>(); 4 | int n=s.length(); 5 | for(int i=0;i maxHeap=new PriorityQueue<>((a,b)->map.get(b)-map.get(a)); 10 | maxHeap.addAll(map.keySet()); 11 | StringBuilder result=new StringBuilder(); 12 | while(!maxHeap.isEmpty()){ 13 | char ch=maxHeap.remove(); 14 | int count=map.get(ch); 15 | while(count!=0){ 16 | result.append(ch); 17 | count--; 18 | } 19 | } 20 | return result.toString(); 21 | } 22 | } -------------------------------------------------------------------------------- /692-top-k-frequent-words/solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector topKFrequent(vector &v, int k) { 4 | unordered_map mp; 5 | for (auto &x : v) { 6 | mp[x]++; 7 | } 8 | 9 | auto cmp = [&](pair &a, pair &b) { 10 | if (a.first == b.first) 11 | return (a.second > b.second); 12 | else 13 | return a.first < b.first; 14 | }; 15 | 16 | priority_queue, vector>, decltype(cmp)> 17 | pq(cmp); 18 | for (auto &[ko, vo] : mp) { 19 | pq.push({vo, ko}); 20 | } 21 | vector ans; 22 | int cnt = 0; 23 | 24 | while (!pq.empty() and cnt < k) { 25 | ans.push_back(pq.top().second); 26 | pq.pop(); 27 | cnt++; 28 | } 29 | return ans; 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /1155. Number of Dice Rolls With Target Sum/Number of Dice Rolls With Target Sum.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | private int MODULO = (int)Math.pow(10, 9) + 7; 3 | private Integer[][] dp = new Integer[31][1001]; 4 | public int numRollsToTarget(int n, int k, int target) { 5 | if(target < n) { 6 | return 0; 7 | } if(dp[n][target] != null) { 8 | return dp[n][target]; 9 | } if(n == 0) { 10 | if(target == 0) { 11 | return 1; 12 | } 13 | return 0; 14 | } 15 | 16 | int ways = 0; 17 | for(int face = 1; face <=k; face++) { 18 | ways += numRollsToTarget(n-1, k, target-face); 19 | ways = ways%MODULO; 20 | } 21 | 22 | dp[n][target] = (int)ways; 23 | return dp[n][target]; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /153. Find Minimum in Rotated Sorted Array/153. Find Minimum in Rotated Sorted Array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findMin(int[] nums) { 3 | 4 | int start = 0; 5 | int end = nums.length -1; 6 | 7 | while (start <= end) { 8 | int mid = start + (end - start)/2; 9 | 10 | if (mid < end && nums[mid] > nums[mid + 1]) 11 | return nums[mid + 1]; // greater index holds the min element in the array 12 | if (start < mid && nums[mid - 1] > nums[mid] ) 13 | return nums[mid]; // greater index holds the min element in the array 14 | 15 | if (nums[start] > nums[mid]) // search in the left half 16 | end = mid - 1; 17 | else // search in the right half 18 | start = mid + 1; 19 | } 20 | return nums[0]; 21 | } 22 | } -------------------------------------------------------------------------------- /23-merge-k-sorted-lists/mergeKSortedLists.cpp: -------------------------------------------------------------------------------- 1 | class compare { 2 | public: 3 | bool operator() (ListNode *a, ListNode *b) { 4 | return a -> val > b -> val; 5 | } 6 | }; 7 | class Solution { 8 | public: 9 | ListNode* mergeKLists(vector& lists) { 10 | priority_queue , compare> pq; 11 | ListNode * dummy = new ListNode(-1); 12 | ListNode * tail = dummy; 13 | for(int i = 0; i < lists.size(); i++) { 14 | if(lists[i] != NULL) 15 | pq.push(lists[i]); 16 | } 17 | while(!pq.empty()) { 18 | ListNode * temp = pq.top(); 19 | tail -> next = temp; 20 | tail = temp; 21 | pq.pop(); 22 | if(temp -> next != NULL) pq.push(temp -> next); 23 | } 24 | return dummy -> next; 25 | } 26 | }; -------------------------------------------------------------------------------- /41-first-missing-positive/README.md: -------------------------------------------------------------------------------- 1 | First Missing Positive 2 | 3 | (Hard) 4 | 5 | Given an unsorted integer array nums, return the smallest missing positive integer. 6 | 7 | You must implement an algorithm that runs in O(n) time and uses constant extra space. 8 | 9 | 10 | Example 1: 11 | 12 | Input: nums = [1,2,0] 13 | Output: 3 14 | Explanation: The numbers in the range [1,2] are all in the array. 15 | 16 | Example 2: 17 | 18 | Input: nums = [3,4,-1,1] 19 | Output: 2 20 | Explanation: 1 is in the array but 2 is missing. 21 | 22 | Example 3: 23 | 24 | Input: nums = [7,8,9,11,12] 25 | Output: 1 26 | Explanation: The smallest positive integer 1 is missing. 27 | 28 | 29 | Constraints: 30 | 31 | 1 <= nums.length <= 105 32 | -231 <= nums[i] <= 231 - 1 -------------------------------------------------------------------------------- /Java/reverseLinkedListII/TestReverseLinkedListII.java: -------------------------------------------------------------------------------- 1 | package reverseLinkedListII; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | /** 7 | * Test for 92. Reverse Linked List II 8 | */ 9 | public class TestReverseLinkedListII { 10 | @Test 11 | public void test() { 12 | ReverseLinkedListII solution = new ReverseLinkedListII(); 13 | int[] array1 = {1, 2, 3, 4, 5, 6, 7}; 14 | ListNode head1 = ListNode.arrayToList(array1); 15 | ListNode newHead1 = solution.reverseBetween(head1, 3, 5); 16 | Assert.assertTrue(ListNode.listToString(newHead1).equals( 17 | "1,2,5,4,3,6,7")); 18 | int[] array2 = {7, 6, 5, 4, 3, 2, 1}; 19 | ListNode head2 = ListNode.arrayToList(array2); 20 | ListNode newHead2 = solution.reverseBetween(head2, 1, 7); 21 | Assert.assertTrue(ListNode.listToString(newHead2).equals( 22 | "1,2,3,4,5,6,7")); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /41-first-missing-positive/41-first-missing-positive.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int firstMissingPositive(int[] nums) { 3 | cyclicSort(nums); 4 | for(int i=0;i 0 && nums[i] < nums.length){ 16 | int ind = nums[i] - 1; 17 | if(nums[i] != nums[ind]){ 18 | int temp = nums[i]; 19 | nums[i] = nums[ind]; 20 | nums[ind] = temp; 21 | } 22 | else i++; 23 | } 24 | else i++; 25 | } 26 | 27 | } 28 | } -------------------------------------------------------------------------------- /561-Array-Partition/README.md: -------------------------------------------------------------------------------- 1 | Given an integer array nums of 2n integers, group these integers into n pairs (a1, b1), (a2, b2), ..., (an, bn) such that the sum of min(ai, bi) for all i is maximized. Return the maximized sum. 2 | 3 | 4 | 5 | Example 1: 6 | 7 | Input: nums = [1,4,3,2] 8 | Output: 4 9 | Explanation: All possible pairings (ignoring the ordering of elements) are: 10 | 1. (1, 4), (2, 3) -> min(1, 4) + min(2, 3) = 1 + 2 = 3 11 | 2. (1, 3), (2, 4) -> min(1, 3) + min(2, 4) = 1 + 2 = 3 12 | 3. (1, 2), (3, 4) -> min(1, 2) + min(3, 4) = 1 + 3 = 4 13 | So the maximum possible sum is 4. 14 | Example 2: 15 | 16 | Input: nums = [6,2,6,5,1,2] 17 | Output: 9 18 | Explanation: The optimal pairing is (2, 1), (2, 5), (6, 6). min(2, 1) + min(2, 5) + min(6, 6) = 1 + 2 + 6 = 9. 19 | 20 | 21 | Constraints: 22 | 23 | 1 <= n <= 104 24 | nums.length == 2 * n 25 | -104 <= nums[i] <= 104 -------------------------------------------------------------------------------- /Find Words That Can Be Formed by Characters/readme.md: -------------------------------------------------------------------------------- 1 | You are given an array of strings words and a string chars. 2 | 3 | A string is good if it can be formed by characters from chars (each character can only be used once). 4 | 5 | Return the sum of lengths of all good strings in words. 6 | 7 | 8 | 9 | Example 1: 10 | 11 | Input: words = ["cat","bt","hat","tree"], chars = "atach" 12 | Output: 6 13 | Explanation: The strings that can be formed are "cat" and "hat" so the answer is 3 + 3 = 6. 14 | Example 2: 15 | 16 | Input: words = ["hello","world","leetcode"], chars = "welldonehoneyr" 17 | Output: 10 18 | Explanation: The strings that can be formed are "hello" and "world" so the answer is 5 + 5 = 10. 19 | 20 | 21 | Constraints: 22 | 23 | 1 <= words.length <= 1000 24 | 1 <= words[i].length, chars.length <= 100 25 | words[i] and chars consist of lowercase English letters. 26 | -------------------------------------------------------------------------------- /2278-percent-of-letter-in-string/README.MD: -------------------------------------------------------------------------------- 1 | Percentage of Letter in String 2 | 3 | Given a string s and a character letter, return the percentage of characters in s that equal letter rounded down to the nearest whole percent. 4 | 5 | 6 | 7 | Example 1: 8 | 9 | Input: s = "foobar", letter = "o" 10 | Output: 33 11 | Explanation: 12 | The percentage of characters in s that equal the letter 'o' is 2 / 6 * 100% = 33% when rounded down, so we return 33. 13 | 14 | Example 2: 15 | 16 | Input: s = "jjjj", letter = "k" 17 | Output: 0 18 | Explanation: 19 | The percentage of characters in s that equal the letter 'k' is 0%, so we return 0. 20 | 21 | 22 | Constraints: 23 | 24 | 1 <= s.length <= 100 25 | s consists of lowercase English letters. 26 | letter is a lowercase English letter. -------------------------------------------------------------------------------- /6. Zigzag Conversion: -------------------------------------------------------------------------------- 1 | 2 | Medium 3 | 4 | 4506 5 | 6 | 9926 7 | 8 | Add to List 9 | 10 | Share 11 | The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) 12 | 13 | P A H N 14 | A P L S I I G 15 | Y I R 16 | And then read line by line: "PAHNAPLSIIGYIR" 17 | 18 | Write the code that will take a string and make this conversion given a number of rows: 19 | 20 | string convert(string s, int numRows); 21 | 22 | 23 | Example 1: 24 | 25 | Input: s = "PAYPALISHIRING", numRows = 3 26 | Output: "PAHNAPLSIIGYIR" 27 | Example 2: 28 | 29 | Input: s = "PAYPALISHIRING", numRows = 4 30 | Output: "PINALSIGYAHRPI" 31 | Explanation: 32 | P I N 33 | A L S I G 34 | Y A H R 35 | P I 36 | Example 3: 37 | 38 | Input: s = "A", numRows = 1 39 | Output: "A" 40 | -------------------------------------------------------------------------------- /1584-Min-Cost-to-Connect-All-Points/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minCostConnectPoints(int[][] points) { 3 | int res = 0; 4 | boolean[] added = new boolean[points.length]; 5 | PriorityQueue pq = new PriorityQueue<>((a, b) -> a[0] - b[0]); 6 | pq.offer(new int[]{0, 0}); 7 | while (!pq.isEmpty()) { 8 | int[] cur = pq.remove(); 9 | if (added[cur[1]]) continue; 10 | added[cur[1]] = true; 11 | res += cur[0]; 12 | for (int j = 0; j < points.length; j++) { 13 | if (j == cur[1] || added[j]) continue; 14 | int dist = Math.abs(points[cur[1]][0] - points[j][0]) + Math.abs(points[cur[1]][1] - points[j][1]); 15 | pq.offer(new int[]{dist, j}); 16 | } 17 | } 18 | return res; 19 | } 20 | } -------------------------------------------------------------------------------- /34. Find First and Last Position of Element in Sorted Array/README.md: -------------------------------------------------------------------------------- 1 | 34. Find First and Last Position of Element in Sorted Array 2 | Medium 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 | Input: nums = [5,7,7,8,8,10], target = 8 13 | Output: [3,4] 14 | 15 | Example 2: 16 | 17 | Input: nums = [5,7,7,8,8,10], target = 6 18 | Output: [-1,-1] 19 | 20 | Example 3: 21 | 22 | Input: nums = [], target = 0 23 | Output: [-1,-1] 24 | 25 | Constraints: 26 | 27 | 1> 0 <= nums.length <= 105 28 | 2> -109 <= nums[i] <= 109 29 | 3> nums is a non-decreasing array. 30 | 4> -109 <= target <= 109 31 | -------------------------------------------------------------------------------- /74-search-a-2d-matrix/74-search-a-2d-matrix.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | //similar to count -ve's in a sorted matrix(1351) 4 | //Binary - search 5 | bool Helper(vector arr,int target) 6 | { 7 | int low=0; 8 | int high=arr.size()-1; 9 | 10 | while(low<=high) 11 | { 12 | int mid=low+(high-low)/2; 13 | 14 | if(arr[mid] == target) return true; 15 | else if(arr[mid] < target) low=mid+1; 16 | else high=mid-1; 17 | } 18 | return false; 19 | } 20 | 21 | bool searchMatrix(vector>& matrix, int target) 22 | { 23 | for (int i=0; i &nums){ 6 | 7 | 8 | int required=1,j=0,curr=0; 9 | while(jmx) 13 | required++,curr=nums[j]; 14 | j++; 15 | if(required>k) 16 | return false; 17 | } 18 | // cout<"<& nums, int k) { 26 | 27 | 28 | int sum=0,mx=INT_MIN; 29 | for(auto x:nums) 30 | sum+=x,mx=max(mx,x); 31 | if(k==1) return sum; 32 | else if(k>=nums.size()-1) return mx; 33 | 34 | 35 | int low=mx,high=sum,ans=sum; 36 | while(low<=high){ 37 | 38 | int mid=(low+high)/2; 39 | if(check(mid,k,nums)) 40 | ans=min(ans,mid),high=mid-1; 41 | else 42 | low=mid+1; 43 | } 44 | 45 | return ans; 46 | } 47 | }; 48 | -------------------------------------------------------------------------------- /101-symmetric-tree/101-symmetric-tree.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isSymmetric(TreeNode* root) { 4 | 5 | if(root==NULL) return true; //Tree is empty 6 | 7 | return isSymmetricTest(root->left,root->right); 8 | } 9 | 10 | bool isSymmetricTest(TreeNode* p , TreeNode* q){ 11 | if(p == NULL && q == NULL) //left & right node is NULL 12 | return true; 13 | 14 | else if(p == NULL || q == NULL) //one of them is Not NULL 15 | return false; 16 | 17 | else if(p->val!=q->val) 18 | return false; 19 | 20 | return isSymmetricTest(p->left,q->right) && isSymmetricTest(p->right,q->left); //comparing left subtree's left child with right subtree's right child --AND-- comparing left subtree's right child with right subtree's left child 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /217-contains-duplicate/217-contains-duplicate.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean containsDuplicate(int[] arr) { 3 | int n = arr.length; 4 | 5 | //IF ONLY ONE ELEMENT RETURN FALSE 6 | if(n == 1){return false;} 7 | 8 | //FOR STORING THE ELEMENTS 9 | HashSet set = new HashSet<>(); 10 | 11 | //O OT N IF ANY ELEMENT ALLREADY PRESENT 12 | //RETURN TRUE 13 | for(int i =0; i Down -> Down 14 | 2. Down -> Down -> Right 15 | 3. Down -> Right -> Down 16 | 17 | 18 | Constraints: 19 | 20 | 1 <= m, n <= 100 21 | 22 | -------------------------------------------------------------------------------- /973. K Closest Points to Origin/README.md: -------------------------------------------------------------------------------- 1 | Given an array of points where points[i] = [xi, yi] represents a point on the X-Y plane and an integer k, return the k closest points to the origin (0, 0). 2 | 3 | The distance between two points on the X-Y plane is the Euclidean distance (i.e., √(x1 - x2)2 + (y1 - y2)2). 4 | 5 | You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in). 6 | 7 | Example 1: 8 | 9 | Input: points = [[1,3],[-2,2]], k = 1 10 | Output: [[-2,2]] 11 | Explanation: 12 | The distance between (1, 3) and the origin is sqrt(10). 13 | The distance between (-2, 2) and the origin is sqrt(8). 14 | Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin. 15 | We only want the closest k = 1 points from the origin, so the answer is just [[-2,2]]. 16 | 17 | Problem link: https://leetcode.com/problems/k-closest-points-to-origin/ 18 | -------------------------------------------------------------------------------- /Keys and Rooms/code.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canVisitAllRooms(vector>& rooms) { 4 | int n=rooms.size(); 5 | unordered_setkeys; 6 | vectorvis(n,0); 7 | keys.insert(0); 8 | int last=1; 9 | while(1) 10 | { 11 | for(auto k:keys) 12 | { 13 | 14 | if(vis[k]==0) 15 | for(auto kk:rooms[k]) 16 | { 17 | keys.insert(kk); 18 | } 19 | vis[k]=1; 20 | } 21 | int currKeys=keys.size(); 22 | if(currKeys==last) 23 | { 24 | return currKeys==n; 25 | } 26 | else 27 | { 28 | last=currKeys; 29 | } 30 | } 31 | return false; 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /Maximal Network Rank/code.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maximalNetworkRank(int n, vector>& ee) { 4 | vectorin(n,0); 5 | unordered_mapmp; 6 | for(auto e:ee) 7 | { 8 | in[e[0]]++; 9 | in[e[1]]++; 10 | string x=to_string(e[0])+"-"+to_string(e[1]); 11 | 12 | mp[x]=1; 13 | } 14 | int ans=0; 15 | for(int i=0;iInteger.compare(a[1],b[1])); 9 | PriorityQueue minHeap=new PriorityQueue<>((a,b)->(a-b)); 10 | long totalSpeed=0; 11 | long maxPerformance=0; 12 | for(int i=n-1;i>=0;i--){ 13 | totalSpeed+=team[i][0]; 14 | minHeap.offer(team[i][0]); 15 | maxPerformance=Math.max(maxPerformance,totalSpeed*team[i][1]); 16 | if(minHeap.size()==k){ 17 | totalSpeed-=minHeap.poll(); 18 | } 19 | } 20 | return (int)(maxPerformance%1000000007); 21 | } 22 | } -------------------------------------------------------------------------------- /43-Multiply-Strings/43._Multiply_Strings.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public String multiply(String num1, String num2) { 3 | int[] arr = new int[num1.length()+num2.length()]; 4 | for(int i = num1.length()-1; i >= 0; i--) { 5 | int carry = 0; 6 | for(int j = num2.length()-1; j >= 0; j--) { 7 | arr[i+j+1] += carry + (num1.charAt(i)-'0') * (num2.charAt(j)-'0'); 8 | carry = arr[i+j+1] / 10; 9 | arr[i+j+1] %= 10; 10 | } 11 | arr[i] = carry; 12 | } 13 | StringBuilder builder = new StringBuilder(); 14 | int i = 0; 15 | while(i < arr.length && arr[i] == 0) i++; 16 | if(i >= arr.length) return "0"; 17 | for(int j = i; j < arr.length; j++) { 18 | builder.append(arr[j]); 19 | } 20 | return builder.toString(); 21 | } 22 | } -------------------------------------------------------------------------------- /560-subarray-sum-equals-k/README.md: -------------------------------------------------------------------------------- 1 |

560. Subarray Sum Equals K

Medium


Given an array of integers nums and an integer k, return the total number of subarrays whose sum equals to k.

2 | 3 |

 

4 |

Example 1:

5 |
Input: nums = [1,1,1], k = 2
 6 | Output: 2
 7 | 

Example 2:

8 |
Input: nums = [1,2,3], k = 3
 9 | Output: 2
10 | 
11 |

 

12 |

Constraints:

13 | 14 |
    15 |
  • 1 <= nums.length <= 2 * 104
  • 16 |
  • -1000 <= nums[i] <= 1000
  • 17 |
  • -107 <= k <= 107
  • 18 |
19 |
-------------------------------------------------------------------------------- /136-single-number/101-symmetric-tree/101-symmetric-tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | 15 | bool helper(TreeNode*p,TreeNode*q) 16 | { 17 | if(!p or !q) return p==q; 18 | 19 | if(p->val != q->val) return false; 20 | return helper(p->left,q->right) && helper(p->right,q->left); 21 | } 22 | 23 | bool isSymmetric(TreeNode* root) 24 | { 25 | if(!root) return false; 26 | return helper(root->left,root->right); 27 | 28 | } 29 | }; -------------------------------------------------------------------------------- /3-longest-substring-without-repeating-characters/3-longest-substring-without-repeating-characters.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def lengthOfLongestSubstring(self, s: str) -> int: 3 | ll=[] 4 | s2="" 5 | v="" 6 | flag=0 7 | maxi=0 8 | if s=="": 9 | return 0 10 | elif len(s)>=2 and s[0]==s[1]: 11 | s=s[1:] 12 | print(s) 13 | for i in range(len(s)): 14 | v="" 15 | for j in range(i,len(s)): 16 | if s[j] in v: 17 | ll.append(len(v)) 18 | flag=flag+1 19 | break 20 | else: 21 | v=s[i:j+1] 22 | if len(v)>maxi: 23 | maxi=len(v) 24 | ll.append(maxi) 25 | if flag==0: 26 | ll.append(len(v)) 27 | return max(ll) -------------------------------------------------------------------------------- /37. Sudoku Solver/README.md: -------------------------------------------------------------------------------- 1 |

37. Sudoku Solver

Hard


Write a program to solve a Sudoku puzzle by filling the empty cells. 2 | 3 | A sudoku solution must satisfy all of the following rules: 4 | 5 | 1. Each of the digits 1-9 must occur exactly once in each row. 6 | 2. Each of the digits 1-9 must occur exactly once in each column. 7 | 3. Each of the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid.

8 | 9 |

The '.' character indicates empty cells.

10 | 11 |

Constraints:

12 | 13 |
    14 |
  • board.length == 9
  • 15 |
  • rd[i].length == 9
  • 16 |
  • board[i][j] is a digit or '.' .
  • 17 |
  • It is guaranteed that the input board has only one solution.
  • 18 |
19 | -------------------------------------------------------------------------------- /1207-unique-number-of-occurrences/1207-unique-number-of-occurrences.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean uniqueOccurrences(int[] arr) { 3 | int z=0; 4 | ArrayList b = new ArrayList(); 5 | 6 | Set mySet = new HashSet<>(); 7 | for(int i:arr){ 8 | mySet.add(i); 9 | } 10 | for(int i:mySet){ 11 | int count=0; 12 | for(int k=0;k mySet1 = new HashSet<>(); 22 | for(int i:b){ 23 | mySet1.add(i); 24 | } 25 | if(mySet1.size()==b.size()){ 26 | return true; 27 | } 28 | return false; 29 | } 30 | } -------------------------------------------------------------------------------- /299. Bulls and cows.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | // only contains digits 4 | string getHint(string secret, string guess) { 5 | int aCnt = 0; 6 | int bCnt = 0; 7 | vector sVec(10, 0); // 0 ~ 9 for secret 8 | vector gVec(10, 0); // 0 ~ 9 for guess 9 | if (secret.size() != guess.size() || secret.empty()) { return "0A0B"; } 10 | for (int i = 0; i < secret.size(); ++i) { 11 | char c1 = secret[i]; char c2 = guess[i]; 12 | if (c1 == c2) { 13 | ++aCnt; 14 | } else { 15 | ++sVec[c1-'0']; 16 | ++gVec[c2-'0']; 17 | } 18 | } 19 | // count b 20 | for (int i = 0; i < sVec.size(); ++i) { 21 | bCnt += min(sVec[i], gVec[i]); 22 | } 23 | return to_string(aCnt) + 'A' + to_string(bCnt) + 'B'; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /106-Construct-BT-From-Inorder-And-Postorder-Traversal/readme.md: -------------------------------------------------------------------------------- 1 | 106. Construct Binary Tree from Inorder and Postorder Traversal 2 | 3 | Given two integer arrays inorder and postorder where inorder is the inorder traversal of a binary tree and postorder is the postorder traversal of the same tree, construct and return the binary tree. 4 | 5 | Example 1: 6 | 7 | 8 | Input: inorder = [9,3,15,20,7], postorder = [9,15,7,20,3] 9 | Output: [3,9,20,null,null,15,7] 10 | Example 2: 11 | 12 | Input: inorder = [-1], postorder = [-1] 13 | Output: [-1] 14 | Constraints: 15 | 16 | 1 <= inorder.length <= 3000 17 | postorder.length == inorder.length 18 | -3000 <= inorder[i], postorder[i] <= 3000 19 | inorder and postorder consist of unique values. 20 | Each value of postorder also appears in inorder. 21 | inorder is guaranteed to be the inorder traversal of the tree. 22 | postorder is guaranteed to be the postorder traversal of the tree. -------------------------------------------------------------------------------- /143-Reorder List/ReorderList.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void reorderList(ListNode* head) { 4 | if ((!head) || (!head->next) || (!head->next->next)) return; // Edge cases 5 | 6 | stack my_stack; 7 | ListNode* ptr = head; 8 | int size = 0; 9 | while (ptr != NULL) // Put all nodes in stack 10 | { 11 | my_stack.push(ptr); 12 | size++; 13 | ptr = ptr->next; 14 | } 15 | 16 | ListNode* pptr = head; 17 | for (int j=0; jnext = pptr->next; 22 | pptr->next = element; 23 | pptr = pptr->next->next; 24 | } 25 | pptr->next = NULL; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /Java/findMinimumInRotatedSortedArray/findMinimumInRotatedSortedArrayTest.java: -------------------------------------------------------------------------------- 1 | package findMinimumInRotatedSortedArray; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Created by leicao on 5/10/15. 9 | */ 10 | public class findMinimumInRotatedSortedArrayTest { 11 | 12 | @Test 13 | public void testFindMin() throws Exception { 14 | int[][] inputs = { 15 | {4,5,6,7,0,1,2}, 16 | {1,2,3}, 17 | {2,3,1}, 18 | {1,2,3,4}, 19 | {2,3,4,1} 20 | }; 21 | 22 | int [] results = {0,1,1,1,1}; 23 | 24 | findMinimumInRotatedSortedArray f = new findMinimumInRotatedSortedArray(); 25 | for (int i = 0; i < results.length; i++) { 26 | int r = f.findMin(inputs[i]); 27 | System.out.println(results[i]); 28 | assertEquals(results[i], r); 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /Swap Nodes In Pairs/Swap Nodes in Pairs cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * public class ListNode { 4 | * int val; 5 | * ListNode next; 6 | * ListNode() {} 7 | * ListNode(int val) { this.val = val; } 8 | * ListNode(int val, ListNode next) { this.val = val; this.next = next; } 9 | * } 10 | */ 11 | class Solution { 12 | public ListNode swapPairs(ListNode head) { 13 | 14 | if(head == null || head.next == null) //If zeo or 1 node in linked list 15 | return head; 16 | 17 | ListNode remaining = head.next.next; //3rd node stored 18 | 19 | ListNode newHead = head.next; //after swapping 2nd head is the head node 20 | 21 | head.next.next = head; //2nd node connected to 1st one 22 | 23 | head.next = swapPairs(remaining); 24 | 25 | return newHead; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /1929. Concatenation of Array/readme.md: -------------------------------------------------------------------------------- 1 | 1929. Concatenation of Array 2 | 3 | Easy 4 | 5 | Given an integer array nums of length n, you want to create an array ans of length 2n where ans[i] == nums[i] and ans[i + n] == nums[i] for 0 <= i < n (0-indexed). 6 | 7 | Specifically, ans is the concatenation of two nums arrays. 8 | 9 | Return the array ans. 10 | 11 | 12 | 13 | Example 1: 14 | 15 | Input: nums = [1,2,1] 16 | Output: [1,2,1,1,2,1] 17 | Explanation: The array ans is formed as follows: 18 | - ans = [nums[0],nums[1],nums[2],nums[0],nums[1],nums[2]] 19 | - ans = [1,2,1,1,2,1] 20 | Example 2: 21 | 22 | Input: nums = [1,3,2,1] 23 | Output: [1,3,2,1,1,3,2,1] 24 | Explanation: The array ans is formed as follows: 25 | - ans = [nums[0],nums[1],nums[2],nums[3],nums[0],nums[1],nums[2],nums[3]] 26 | - ans = [1,3,2,1,1,3,2,1] 27 | 28 | 29 | Constraints: 30 | 31 | n == nums.length 32 | 1 <= n <= 1000 33 | 1 <= nums[i] <= 1000 34 | -------------------------------------------------------------------------------- /58-length-of-last-word/README.md: -------------------------------------------------------------------------------- 1 | Length of Last Word 2 | 3 | (Easy) 4 | 5 | Given a string s consisting of words and spaces, return the length of the last word in the string. 6 | 7 | A word is a maximal substring consisting of non-space characters only. 8 | 9 | Example 1: 10 | 11 | Input: s = "Hello World" 12 | Output: 5 13 | Explanation: The last word is "World" with length 5. 14 | 15 | Example 2: 16 | 17 | Input: s = " fly me to the moon " 18 | Output: 4 19 | Explanation: The last word is "moon" with length 4. 20 | 21 | Example 3: 22 | 23 | Input: s = "luffy is still joyboy" 24 | Output: 6 25 | Explanation: The last word is "joyboy" with length 6. 26 | 27 | Constraints: 28 | 29 | 1 <= s.length <= 104 30 | s consists of only English letters and spaces ' '. 31 | There will be at least one word in s. -------------------------------------------------------------------------------- /410. Split Array Largest Sum: -------------------------------------------------------------------------------- 1 | Given an integer array nums and an integer k, split nums into k non-empty subarrays such that the largest sum of any subarray is minimized. 2 | 3 | Return the minimized largest sum of the split. 4 | 5 | A subarray is a contiguous part of the array. 6 | 7 | 8 | 9 | Example 1: 10 | 11 | Input: nums = [7,2,5,10,8], k = 2 12 | Output: 18 13 | Explanation: There are four ways to split nums into two subarrays. 14 | The best way is to split it into [7,2,5] and [10,8], where the largest sum among the two subarrays is only 18. 15 | Example 2: 16 | 17 | Input: nums = [1,2,3,4,5], k = 2 18 | Output: 9 19 | Explanation: There are four ways to split nums into two subarrays. 20 | The best way is to split it into [1,2,3] and [4,5], where the largest sum among the two subarrays is only 9. 21 | 22 | 23 | Constraints: 24 | 25 | 1 <= nums.length <= 1000 26 | 0 <= nums[i] <= 106 27 | 1 <= k <= min(50, nums.length) 28 | -------------------------------------------------------------------------------- /score after flipping matrix/readme.md: -------------------------------------------------------------------------------- 1 | You are given an m x n binary matrix grid. 2 | 3 | A move consists of choosing any row or column and toggling each value in that row or column (i.e., changing all 0's to 1's, and all 1's to 0's). 4 | 5 | Every row of the matrix is interpreted as a binary number, and the score of the matrix is the sum of these numbers. 6 | 7 | Return the highest possible score after making any number of moves (including zero moves). 8 | 9 | 10 | 11 | Example 1: 12 | ![image](https://user-images.githubusercontent.com/114977449/195622198-a72b0e05-9833-47da-b07e-6bc15e21c168.png) 13 | 14 | 15 | Input: grid = [[0,0,1,1],[1,0,1,0],[1,1,0,0]] 16 | Output: 39 17 | Explanation: 0b1111 + 0b1001 + 0b1111 = 15 + 9 + 15 = 39 18 | Example 2: 19 | 20 | Input: grid = [[0]] 21 | Output: 1 22 | 23 | 24 | Constraints: 25 | 26 | m == grid.length 27 | n == grid[i].length 28 | 1 <= m, n <= 20 29 | grid[i][j] is either 0 or 1. 30 | -------------------------------------------------------------------------------- /split array largest sum/readme.md: -------------------------------------------------------------------------------- 1 | Given an integer array nums and an integer k, split nums into k non-empty subarrays such that the largest sum of any subarray is minimized. 2 | 3 | Return the minimized largest sum of the split. 4 | 5 | A subarray is a contiguous part of the array. 6 | 7 | 8 | 9 | Example 1: 10 | 11 | Input: nums = [7,2,5,10,8], k = 2 12 | Output: 18 13 | Explanation: There are four ways to split nums into two subarrays. 14 | The best way is to split it into [7,2,5] and [10,8], where the largest sum among the two subarrays is only 18. 15 | Example 2: 16 | 17 | Input: nums = [1,2,3,4,5], k = 2 18 | Output: 9 19 | Explanation: There are four ways to split nums into two subarrays. 20 | The best way is to split it into [1,2,3] and [4,5], where the largest sum among the two subarrays is only 9. 21 | 22 | 23 | Constraints: 24 | 25 | 1 <= nums.length <= 1000 26 | 0 <= nums[i] <= 106 27 | 1 <= k <= min(50, nums.length) 28 | -------------------------------------------------------------------------------- /Java/removeDuplicatesFromSortedArray/TestRemoveDuplicates.java: -------------------------------------------------------------------------------- 1 | package removeDuplicatesFromSortedArray; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | /** 7 | * Test for 26. Remove Duplicates from Sorted Array 8 | */ 9 | public class TestRemoveDuplicates { 10 | @Test 11 | public void test() { 12 | int[] nums1 = {0, 0, 0, 0, 0, 1, 2, 2, 3, 3, 4, 4}; 13 | RemoveDuplicatesFromSortedArray solution = new RemoveDuplicatesFromSortedArray(); 14 | int len1 = solution.removeDuplicates(nums1); 15 | Assert.assertTrue(len1 == 5); 16 | assertSorted(nums1, len1); 17 | int[] nums2 = {1, 2, 2, 2, 3, 4, 5, 6, 6, 7, 7, 7, 8}; 18 | int len2 = solution.removeDuplicates(nums2); 19 | Assert.assertTrue(len2 == 8); 20 | assertSorted(nums2, len2); 21 | } 22 | private void assertSorted(int[] array, int len) { 23 | for (int i = 0; i < len - 1; i++) { 24 | Assert.assertTrue(array[i] < array[i + 1]); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /33-search-in-rotated-sorted-array/33-search-in-rotated-sorted-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int search(vector& nums, int target) 4 | { 5 | int beg=0,end=nums.size()-1,mid; 6 | 7 | while(beg<=end) 8 | { 9 | mid=(beg+end)/2; 10 | if(nums[mid]==target) return mid; 11 | 12 | if(nums[beg]<=nums[mid]) 13 | { 14 | //if target is in there 15 | if(target<=nums[mid] && target>=nums[beg]) 16 | end=mid-1; 17 | else 18 | beg=mid+1; 19 | } 20 | 21 | else 22 | { 23 | if(target>=nums[mid] && target<=nums[end]) 24 | beg=mid+1; 25 | else 26 | end=mid-1; 27 | } 28 | 29 | } 30 | return -1; 31 | } 32 | 33 | }; -------------------------------------------------------------------------------- /430-flatten-douby-link-list/solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | Node *flatten(Node *head) 5 | { 6 | Node *cur = head; 7 | Node *cached = NULL; 8 | while (cur != NULL) 9 | { 10 | if (cur->child != NULL) 11 | { 12 | cached = cur->next; 13 | cur->next = cur->child; 14 | cur->next->prev = cur; 15 | cur->child = NULL; 16 | 17 | Node *temp = cur->next; 18 | while (temp->next != NULL) 19 | { 20 | temp = temp->next; 21 | } 22 | temp->next = cached; 23 | if (cached != NULL) 24 | { 25 | cached->prev = temp; 26 | } 27 | } 28 | cur = cur->next; 29 | } 30 | 31 | return head; 32 | } 33 | }; -------------------------------------------------------------------------------- /767-Reorganize-string/solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string reorganizeString(string s) { 4 | int size=s.size(); 5 | if(size==1) 6 | return s; 7 | unordered_map mp; 8 | string ans=""; 9 | for(auto it:s) 10 | mp[it]++; 11 | if(mp.size()==1) 12 | return ""; 13 | priority_queue> pq; //max priorityQueue 14 | for(auto it:mp) 15 | pq.push({it.second,it.first}); 16 | pair pr=pq.top(); 17 | pq.pop(); 18 | ans+=pr.second; 19 | pr.first--; 20 | while(pq.top().first){ 21 | pair p=pq.top(); 22 | pq.pop(); 23 | ans+=p.second; 24 | p.first--; 25 | pq.push(pr); 26 | pr=p; 27 | } 28 | if(ans.size()==size) 29 | return ans; 30 | return ""; 31 | } 32 | }; -------------------------------------------------------------------------------- /House-Robber/README.md: -------------------------------------------------------------------------------- 1 | You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night. 2 | 3 | Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police. 4 | 5 | Example 1: 6 | 7 | Input: nums = [1,2,3,1] 8 | Output: 4 9 | Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3). 10 | Total amount you can rob = 1 + 3 = 4. 11 | 12 | Example 2: 13 | 14 | Input: nums = [2,7,9,3,1] 15 | Output: 12 16 | Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1). 17 | Total amount you can rob = 2 + 9 + 1 = 12. 18 | -------------------------------------------------------------------------------- /widcard matching dp/readme.md: -------------------------------------------------------------------------------- 1 | Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '?' and '*' where: 2 | 3 | '?' Matches any single character. 4 | '*' Matches any sequence of characters (including the empty sequence). 5 | The matching should cover the entire input string (not partial). 6 | 7 | 8 | 9 | Example 1: 10 | 11 | Input: s = "aa", p = "a". 12 | Output: false. 13 | Explanation: "a" does not match the entire string "aa". 14 | 15 | 16 | 17 | 18 | 19 | Example 2: 20 | 21 | Input: s = "aa", p = "*" 22 | Output: true. 23 | Explanation: '*' matches any sequence. 24 | Example 3: 25 | 26 | Input: s = "cb", p = "?a" 27 | Output: false 28 | Explanation: '?' matches 'c', but the second letter is 'a', which does not match 'b'. 29 | 30 | 31 | Constraints: 32 | 33 | 0 <= s.length, p.length <= 2000 34 | s contains only lowercase English letters. 35 | p contains only lowercase English letters, '?' or '*'. 36 | -------------------------------------------------------------------------------- /118-pascals-triangle/README.md: -------------------------------------------------------------------------------- 1 |

118. Pascal's Triangle

Easy


Given an integer numRows, return the first numRows of Pascal's triangle.

2 | 3 |

In Pascal's triangle, each number is the sum of the two numbers directly above it as shown:

4 | 5 |

 

6 |

Example 1:

7 |
Input: numRows = 5
 8 | Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
 9 | 

Example 2:

10 |
Input: numRows = 1
11 | Output: [[1]]
12 | 
13 |

 

14 |

Constraints:

15 | 16 |
    17 |
  • 1 <= numRows <= 30
  • 18 |
19 |
-------------------------------------------------------------------------------- /746-min-cost-climbing-stairs/746-min-cost-climbing-stairs.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | //ques is a bit misleading, the top of the chair is actually at nth floor:([10 15 20] here) 4 | //Tabulation 5 | int minCostClimbingStairs(vector& cost) 6 | { 7 | int n = cost.size(); 8 | 9 | int dp[n+1]; 10 | dp[0] = cost[0]; 11 | dp[1] = cost[1]; 12 | 13 | for(int i = 2; i < n;i++){ 14 | dp[i] = cost[i] + (min(dp[i-1], dp[i-2])); 15 | } 16 | return min(dp[n-2], dp[n-1]); 17 | } 18 | }; 19 | 20 | //Recursion -> O(2^N) 21 | // int solve (vector& cost, int n) { 22 | // if (n < 2) return cost[n]; 23 | 24 | // return cost[n] + min(solve(cost, n - 1), solve(cost, n - 2)); 25 | // } 26 | 27 | // int minCostClimbingStairs(vector& cost) { 28 | // int n = cost.size(); 29 | 30 | // return min(solve(cost, n - 1), solve(cost, n - 2)); 31 | // } -------------------------------------------------------------------------------- /130-surrounded-regions/130-surrounded-regions.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void DFS(vector>& board,int r,int c,int rsize,int csize){ 4 | if(r<0||c<0||r==rsize||c==csize||board[r][c]!='O')return; 5 | board[r][c] = 'P'; 6 | DFS(board,r+1,c,rsize,csize); 7 | DFS(board,r,c+1,rsize,csize); 8 | DFS(board,r-1,c,rsize,csize); 9 | DFS(board,r,c-1,rsize,csize); 10 | } 11 | void solve(vector>& board) { 12 | if(board.empty())return; 13 | int row = board.size(),col = board[0].size(); 14 | for(int i=0;i, vector> mp; 5 | vector f(int n, int s) 6 | { 7 | vector ans; 8 | if (mp.count({n, s})) 9 | return mp[{n, s}]; 10 | if (n < s) 11 | return {0}; 12 | for (int i = s; i <= n; i++) 13 | { 14 | for (auto left : f(i - 1, s)) 15 | { 16 | for (auto right : f(n, i + 1)) 17 | { 18 | TreeNode *curr_tree = new TreeNode(i); 19 | curr_tree->left = left; 20 | curr_tree->right = right; 21 | ans.push_back(curr_tree); 22 | } 23 | } 24 | } 25 | return mp[{n, s}] = ans; 26 | } 27 | vector generateTrees(int n) 28 | { 29 | return f(n, 1); 30 | } 31 | }; -------------------------------------------------------------------------------- /1302-deepest-leaves-sum/README.md: -------------------------------------------------------------------------------- 1 |

1302. Deepest Leaves Sum

Medium


Given the root of a binary tree, return the sum of values of its deepest leaves. 2 |

 

3 |

Example 1:

4 | 5 |
Input: root = [1,2,3,4,5,null,6,7,null,null,null,null,8]
 6 | Output: 15
 7 | 
8 | 9 |

Example 2:

10 | 11 |
Input: root = [6,7,8,2,7,1,3,9,null,1,4,null,null,null,5]
12 | Output: 19
13 | 
14 | 15 |

 

16 |

Constraints:

17 | 18 |
    19 |
  • The number of nodes in the tree is in the range [1, 104].
  • 20 |
  • 1 <= Node.val <= 100
  • 21 |
22 |
-------------------------------------------------------------------------------- /House Robber 2.py: -------------------------------------------------------------------------------- 1 | def rob(nums): 2 | def rob_helper(nums): 3 | n = len(nums) 4 | if n == 0: 5 | return 0 6 | if n == 1: 7 | return nums[0] 8 | if n == 2: 9 | return max(nums[0], nums[1]) 10 | 11 | dp1 = [0] * n 12 | dp2 = [0] * n 13 | 14 | dp1[0] = nums[0] 15 | dp1[1] = max(nums[0], nums[1]) 16 | for i in range(2, n - 1): 17 | dp1[i] = max(dp1[i - 1], dp1[i - 2] + nums[i]) 18 | 19 | dp2[1] = nums[1] 20 | dp2[2] = max(nums[1], nums[2]) 21 | for i in range(3, n): 22 | dp2[i] = max(dp2[i - 1], dp2[i - 2] + nums[i]) 23 | 24 | return max(dp1[n - 2], dp2[n - 1]) 25 | 26 | 27 | if len(nums) == 1: 28 | return nums[0] 29 | elif len(nums) == 2: 30 | return max(nums[0], nums[1]) 31 | 32 | 33 | return max(rob_helper(nums[:-1]), rob_helper(nums[1:])) 34 | -------------------------------------------------------------------------------- /205. Isomorphic Strings: -------------------------------------------------------------------------------- 1 | //Time complexity : O(N) 2 | //Space complexity : O(N) 3 | 4 | class Solution { 5 | public boolean isIsomorphic(String s, String t) { 6 | 7 | HashMap map1 = new HashMap<>(); 8 | HashMap map2 = new HashMap<>(); 9 | 10 | for(int i=0;i> merge(vector>& interval) { 5 | vector> result; 6 | //base case when threr are no intervals 7 | if(interval.size()==0)return result; 8 | //sort takes O(nlogn) time 9 | sort(interval.begin(),interval.end()); 10 | //insert the the the first interval in the result 11 | result.push_back(interval[0]); 12 | int j=0; 13 | //Traverse the whole vector .Takes O(n) time 14 | for(int i=1;i=interval[i][0]) result[j][1]=max(result[j][1],interval[i][1]); 18 | //else they are not overlapping 19 | else 20 | { 21 | j++; 22 | result.push_back(interval[i]); 23 | } 24 | } 25 | return result; 26 | } 27 | }; -------------------------------------------------------------------------------- /Java/binaryTreeBFSTraversal/binaryTreeBFSTraversal.java: -------------------------------------------------------------------------------- 1 | package binaryTreeBFSTraversal; 2 | 3 | import java.util.ArrayList; 4 | import java.util.LinkedList; 5 | import java.util.List; 6 | import java.util.Queue; 7 | 8 | /** 9 | * Created by leicao on 5/10/15. 10 | */ 11 | public class binaryTreeBFSTraversal { 12 | public List bfsTraversal(TreeNode root) { 13 | List results = new ArrayList(); 14 | if (root == null) { 15 | return results; 16 | } 17 | 18 | Queue q = new LinkedList(); 19 | 20 | q.offer(root); 21 | while (q.size() != 0) { 22 | TreeNode n = q.remove(); 23 | results.add(n.val); 24 | if (n.left != null) { 25 | q.offer(n.left); 26 | } 27 | if (n.right != null) { 28 | q.offer(n.right); 29 | } 30 | } 31 | 32 | return results; 33 | 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /1295-find-num-with-even-digits/README.md: -------------------------------------------------------------------------------- 1 | Find Numbers with Even Number of Digits 2 | 3 | (Easy) 4 | 5 | Given an array nums of integers, return how many of them contain an even number of digits. 6 | 7 | 8 | Example 1: 9 | 10 | Input: nums = [12,345,2,6,7896] 11 | Output: 2 12 | Explanation: 13 | 12 contains 2 digits (even number of digits). 14 | 345 contains 3 digits (odd number of digits). 15 | 2 contains 1 digit (odd number of digits). 16 | 6 contains 1 digit (odd number of digits). 17 | 7896 contains 4 digits (even number of digits). 18 | Therefore only 12 and 7896 contain an even number of digits. 19 | 20 | Example 2: 21 | 22 | Input: nums = [555,901,482,1771] 23 | Output: 1 24 | Explanation: 25 | Only 1771 contains an even number of digits. 26 | 27 | 28 | Constraints: 29 | 30 | 1 <= nums.length <= 500 31 | 1 <= nums[i] <= 105 32 | -------------------------------------------------------------------------------- /16-3sum-closest/3sumClosest.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int threeSumClosest(vector &nums, int target) 5 | { 6 | sort(nums.begin(), nums.end()); 7 | int ans = 0, n = nums.size(); 8 | int diff = INT_MAX; 9 | for (int i = 0; i < n; i++) 10 | { 11 | int first = nums[i]; 12 | int s = i + 1, e = n - 1; 13 | while (s < e) 14 | { 15 | if (first + nums[s] + nums[e] == target) 16 | return target; 17 | else if (abs(first + nums[s] + nums[e] - target) < diff) 18 | { 19 | diff = abs(first + nums[s] + nums[e] - target); 20 | ans = first + nums[s] + nums[e]; 21 | } 22 | if (first + nums[s] + nums[e] > target) 23 | e--; 24 | else 25 | s++; 26 | } 27 | } 28 | return ans; 29 | } 30 | }; -------------------------------------------------------------------------------- /2401-Longest-Nice-Subarray/README.md: -------------------------------------------------------------------------------- 1 | You are given an array nums consisting of positive integers. 2 | 3 | We call a subarray of nums nice if the bitwise AND of every pair of elements that are in different positions in the subarray is equal to 0. 4 | 5 | Return the length of the longest nice subarray. 6 | 7 | A subarray is a contiguous part of an array. 8 | 9 | Note that subarrays of length 1 are always considered nice. 10 | 11 | Example 1: 12 | 13 | Input: nums = [1,3,8,48,10] 14 | Output: 3 15 | Explanation: The longest nice subarray is [3,8,48]. This subarray satisfies the conditions: 16 | 17 | - 3 AND 8 = 0. 18 | - 3 AND 48 = 0. 19 | - 8 AND 48 = 0. 20 | It can be proven that no longer nice subarray can be obtained, so we return 3. 21 | Example 2: 22 | 23 | Input: nums = [3,1,5,11,13] 24 | Output: 1 25 | Explanation: The length of the longest nice subarray is 1. Any subarray of length 1 can be chosen. 26 | 27 | Constraints: 28 | 29 | 1 <= nums.length <= 105 30 | 1 <= nums[i] <= 109 31 | -------------------------------------------------------------------------------- /24-Swap-Node-In-Pair/24-swap-nodes-in-pair.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* swapPairs(ListNode* head) { 14 | if(head == NULL or head->next == NULL) return head; 15 | 16 | int i=0; 17 | 18 | ListNode *curr = head; 19 | ListNode *dummy = new ListNode(-1); 20 | dummy->next = head; 21 | 22 | 23 | while(curr != NULL){ 24 | 25 | i++; 26 | 27 | if(i % 2 == 0){ 28 | swap(dummy->val, curr->val); 29 | } 30 | dummy = dummy->next; 31 | curr = curr->next; 32 | } 33 | return head; 34 | } 35 | }; -------------------------------------------------------------------------------- /1854-maximum-population-year/README.md: -------------------------------------------------------------------------------- 1 | You are given a 2D integer array logs where each logs[i] = [birthi, deathi] indicates the birth and death years of the ith person. 2 | 3 | The population of some year x is the number of people alive during that year. The ith person is counted in year x's population if x is in the inclusive range [birthi, deathi - 1]. Note that the person is not counted in the year that they die. 4 | 5 | Return the earliest year with the maximum population. 6 | 7 | 8 | 9 | Example 1: 10 | 11 | Input: logs = [[1993,1999],[2000,2010]] 12 | Output: 1993 13 | Explanation: The maximum population is 1, and 1993 is the earliest year with this population. 14 | Example 2: 15 | 16 | Input: logs = [[1950,1961],[1960,1971],[1970,1981]] 17 | Output: 1960 18 | Explanation: 19 | The maximum population is 2, and it had happened in years 1960 and 1970. 20 | The earlier year between them is 1960. 21 | 22 | 23 | Constraints: 24 | 25 | 1 <= logs.length <= 100 26 | 1950 <= birthi < deathi <= 2050 27 | --------------------------------------------------------------------------------