├── 15-3sum ├── NOTES.md ├── README.md └── 15-3sum.go ├── 69-sqrtx ├── NOTES.md ├── 69-sqrtx.go └── README.md ├── 1-two-sum ├── NOTES.md └── 1-two-sum.go ├── 100-same-tree ├── NOTES.md ├── 100-same-tree.go └── README.md ├── 50-powx-n ├── NOTES.md ├── 50-powx-n.go └── README.md ├── 66-plus-one ├── NOTES.md └── 66-plus-one.go ├── 78-subsets ├── NOTES.md ├── README.md └── 78-subsets.go ├── 90-subsets-ii ├── NOTES.md ├── 90-subsets-ii.go └── README.md ├── 133-clone-graph ├── NOTES.md └── 133-clone-graph.go ├── 136-single-number ├── NOTES.md ├── 136-single-number.go └── README.md ├── 2-add-two-numbers ├── NOTES.md └── 2-add-two-numbers.go ├── 242-valid-anagram ├── NOTES.md ├── 242-valid-anagram.go └── README.md ├── 27-remove-element ├── NOTES.md └── 27-remove-element.go ├── 289-game-of-life └── NOTES.md ├── 344-reverse-string ├── NOTES.md ├── 344-reverse-string.go └── README.md ├── 36-valid-sudoku ├── NOTES.md └── 36-valid-sudoku.go ├── 39-combination-sum └── NOTES.md ├── 46-permutations ├── NOTES.md ├── 46-permutations.go └── README.md ├── 49-group-anagrams ├── NOTES.md ├── 49-group-anagrams.go └── README.md ├── 520-detect-capital ├── NOTES.md ├── 520-detect-capital.go └── README.md ├── 56-merge-intervals ├── NOTES.md ├── 56-merge-intervals.go └── README.md ├── 61-rotate-list ├── NOTES.md ├── 61-rotate-list.go └── README.md ├── 62-unique-paths ├── NOTES.md └── 62-unique-paths.go ├── 70-climbing-stairs ├── NOTES.md ├── 70-climbing-stairs.go └── README.md ├── 704-binary-search ├── NOTES.md ├── 704-binary-search.go └── README.md ├── 705-design-hashset ├── NOTES.md └── 705-design-hashset.go ├── 729-my-calendar-i ├── NOTES.md └── 729-my-calendar-i.go ├── 77-combinations ├── NOTES.md ├── 77-combinations.go └── README.md ├── 118-pascals-triangle ├── NOTES.md ├── 118-pascals-triangle.go └── README.md ├── 119-pascals-triangle-ii ├── NOTES.md ├── 119-pascals-triangle-ii.go └── README.md ├── 125-valid-palindrome ├── NOTES.md └── 125-valid-palindrome.go ├── 13-roman-to-integer ├── NOTES.md └── 13-roman-to-integer.go ├── 130-surrounded-regions └── NOTES.md ├── 141-linked-list-cycle ├── NOTES.md └── 141-linked-list-cycle.go ├── 162-find-peak-element ├── NOTES.md └── 162-find-peak-element.go ├── 169-majority-element ├── NOTES.md ├── 169-majority-element.go └── README.md ├── 20-valid-parentheses ├── NOTES.md ├── 20-valid-parentheses.go └── README.md ├── 200-number-of-islands ├── NOTES.md ├── 200-number-of-islands.go └── README.md ├── 2013-detect-squares ├── NOTES.md └── 2013-detect-squares.go ├── 206-reverse-linked-list ├── NOTES.md ├── 206-reverse-linked-list.go └── README.md ├── 207-course-schedule ├── NOTES.md └── 207-course-schedule.go ├── 210-course-schedule-ii └── NOTES.md ├── 216-combination-sum-iii ├── NOTES.md └── 216-combination-sum-iii.go ├── 217-contains-duplicate ├── NOTES.md ├── 217-contains-duplicate.go └── README.md ├── 22-generate-parentheses ├── NOTES.md ├── 22-generate-parentheses.go └── README.md ├── 226-invert-binary-tree ├── NOTES.md ├── 226-invert-binary-tree.go └── README.md ├── 229-majority-element-ii ├── NOTES.md ├── README.md └── 229-majority-element-ii.go ├── 278-first-bad-version ├── NOTES.md ├── 278-first-bad-version.go └── README.md ├── 40-combination-sum-ii ├── NOTES.md └── README.md ├── 461-hamming-distance ├── NOTES.md ├── 461-hamming-distance.go └── README.md ├── 53-maximum-subarray ├── NOTES.md ├── 53-maximum-subarray.go └── README.md ├── 566-reshape-the-matrix ├── NOTES.md └── 566-reshape-the-matrix.go ├── 58-length-of-last-word ├── NOTES.md ├── 58-length-of-last-word.go └── README.md ├── 64-minimum-path-sum ├── NOTES.md ├── 64-minimum-path-sum.go └── README.md ├── 680-valid-palindrome-ii ├── NOTES.md ├── 680-valid-palindrome-ii.go └── README.md ├── 695-max-area-of-island ├── NOTES.md └── 695-max-area-of-island.go ├── 74-search-a-2d-matrix ├── NOTES.md ├── 74-search-a-2d-matrix.go └── README.md ├── 858-mirror-reflection ├── NOTES.md └── 858-mirror-reflection.go ├── 875-koko-eating-bananas ├── NOTES.md └── 875-koko-eating-bananas.go ├── 9-palindrome-number ├── NOTES.md └── 9-palindrome-number.go ├── 997-find-the-town-judge ├── NOTES.md └── 997-find-the-town-judge.go ├── 11-container-with-most-water ├── NOTES.md └── 11-container-with-most-water.go ├── 131-palindrome-partitioning ├── NOTES.md ├── README.md └── 131-palindrome-partitioning.go ├── 14-longest-common-prefix ├── NOTES.md ├── 14-longest-common-prefix.go └── README.md ├── 142-linked-list-cycle-ii ├── NOTES.md └── 142-linked-list-cycle-ii.go ├── 1603-design-parking-system ├── NOTES.md └── 1603-design-parking-system.go ├── 21-merge-two-sorted-lists ├── NOTES.md ├── 21-merge-two-sorted-lists.go └── README.md ├── 219-contains-duplicate-ii ├── NOTES.md ├── 219-contains-duplicate-ii.go └── README.md ├── 2296-design-a-text-editor └── NOTES.md ├── 234-palindrome-linked-list ├── NOTES.md ├── 234-palindrome-linked-list.go └── README.md ├── 240-search-a-2d-matrix-ii ├── NOTES.md └── 240-search-a-2d-matrix-ii.go ├── 328-odd-even-linked-list ├── NOTES.md └── 328-odd-even-linked-list.go ├── 35-search-insert-position ├── NOTES.md ├── 35-search-insert-position.go └── README.md ├── 410-split-array-largest-sum ├── NOTES.md ├── 410-split-array-largest-sum.go └── README.md ├── 543-diameter-of-binary-tree ├── NOTES.md ├── 543-diameter-of-binary-tree.go └── README.md ├── 576-out-of-boundary-paths └── NOTES.md ├── 746-min-cost-climbing-stairs ├── NOTES.md └── 746-min-cost-climbing-stairs.go ├── 804-unique-morse-code-words ├── NOTES.md └── 804-unique-morse-code-words.go ├── 890-find-and-replace-pattern ├── NOTES.md └── 890-find-and-replace-pattern.go ├── 895-maximum-frequency-stack └── NOTES.md ├── 92-reverse-linked-list-ii ├── NOTES.md ├── 92-reverse-linked-list-ii.go └── README.md ├── 104-maximum-depth-of-binary-tree ├── NOTES.md ├── 104-maximum-depth-of-binary-tree.go └── README.md ├── 111-minimum-depth-of-binary-tree ├── NOTES.md ├── 111-minimum-depth-of-binary-tree.go └── README.md ├── 128-longest-consecutive-sequence ├── NOTES.md ├── 128-longest-consecutive-sequence.go └── README.md ├── 1329-sort-the-matrix-diagonally ├── NOTES.md └── 1329-sort-the-matrix-diagonally.go ├── 138-copy-list-with-random-pointer ├── NOTES.md └── 138-copy-list-with-random-pointer.go ├── 1539-kth-missing-positive-number ├── NOTES.md └── 1539-kth-missing-positive-number.go ├── 1791-find-center-of-star-graph ├── NOTES.md └── 1791-find-center-of-star-graph.go ├── 203-remove-linked-list-elements ├── NOTES.md ├── 203-remove-linked-list-elements.go └── README.md ├── 2351-first-letter-to-appear-twice ├── NOTES.md └── 2351-first-letter-to-appear-twice.go ├── 33-search-in-rotated-sorted-array ├── NOTES.md └── 33-search-in-rotated-sorted-array.go ├── 559-maximum-depth-of-n-ary-tree ├── NOTES.md ├── 559-maximum-depth-of-n-ary-tree.go └── README.md ├── 876-middle-of-the-linked-list ├── NOTES.md ├── 876-middle-of-the-linked-list.go └── README.md ├── 94-binary-tree-inorder-traversal ├── NOTES.md ├── 94-binary-tree-inorder-traversal.go └── README.md ├── 98-validate-binary-search-tree ├── NOTES.md └── 98-validate-binary-search-tree.go ├── 121-best-time-to-buy-and-sell-stock └── NOTES.md ├── 144-binary-tree-preorder-traversal ├── NOTES.md ├── 144-binary-tree-preorder-traversal.go └── README.md ├── 1448-count-good-nodes-in-binary-tree ├── NOTES.md └── 1448-count-good-nodes-in-binary-tree.go ├── 145-binary-tree-postorder-traversal ├── NOTES.md ├── 145-binary-tree-postorder-traversal.go └── README.md ├── 160-intersection-of-two-linked-lists ├── NOTES.md └── 160-intersection-of-two-linked-lists.go ├── 19-remove-nth-node-from-end-of-list ├── NOTES.md ├── 19-remove-nth-node-from-end-of-list.go └── README.md ├── 26-remove-duplicates-from-sorted-array ├── NOTES.md └── 26-remove-duplicates-from-sorted-array.go ├── 300-longest-increasing-subsequence ├── NOTES.md └── 300-longest-increasing-subsequence.go ├── 387-first-unique-character-in-a-string ├── NOTES.md ├── 387-first-unique-character-in-a-string.go └── README.md ├── 540-single-element-in-a-sorted-array ├── NOTES.md ├── README.md └── 540-single-element-in-a-sorted-array.go ├── 637-average-of-levels-in-binary-tree ├── NOTES.md ├── 637-average-of-levels-in-binary-tree.go └── README.md ├── 792-number-of-matching-subsequences ├── NOTES.md └── 792-number-of-matching-subsequences.go ├── 81-search-in-rotated-sorted-array-ii ├── NOTES.md └── 81-search-in-rotated-sorted-array-ii.go ├── 83-remove-duplicates-from-sorted-list ├── NOTES.md ├── 83-remove-duplicates-from-sorted-list.go └── README.md ├── 153-find-minimum-in-rotated-sorted-array ├── NOTES.md └── 153-find-minimum-in-rotated-sorted-array.go ├── 17-letter-combinations-of-a-phone-number ├── NOTES.md ├── 17-letter-combinations-of-a-phone-number.go └── README.md ├── 1011-capacity-to-ship-packages-within-d-days └── NOTES.md ├── 108-convert-sorted-array-to-binary-search-tree ├── NOTES.md └── 108-convert-sorted-array-to-binary-search-tree.go ├── 1283-find-the-smallest-divisor-given-a-threshold ├── NOTES.md └── 1283-find-the-smallest-divisor-given-a-threshold.go ├── 1482-minimum-number-of-days-to-make-m-bouquets └── NOTES.md ├── 2095-delete-the-middle-node-of-a-linked-list ├── NOTES.md └── 2095-delete-the-middle-node-of-a-linked-list.go ├── 3-longest-substring-without-repeating-characters ├── NOTES.md ├── 3-longest-substring-without-repeating-characters.go └── README.md ├── 235-lowest-common-ancestor-of-a-binary-search-tree ├── NOTES.md └── 235-lowest-common-ancestor-of-a-binary-search-tree.go ├── 101-symmetric-tree ├── NOTES.md ├── 101-symmetric-tree.go └── README.md ├── 34-find-first-and-last-position-of-element-in-sorted-array ├── NOTES.md ├── 34-find-first-and-last-position-of-element-in-sorted-array.go └── README.md ├── 105-construct-binary-tree-from-preorder-and-inorder-traversal ├── NOTES.md └── 105-construct-binary-tree-from-preorder-and-inorder-traversal.go ├── 106-construct-binary-tree-from-inorder-and-postorder-traversal ├── NOTES.md └── 106-construct-binary-tree-from-inorder-and-postorder-traversal.go ├── 5-longest-palindromic-substring ├── NOTES.md ├── 5-longest-palindromic-substring.go └── README.md ├── 4-median-of-two-sorted-arrays ├── NOTES.md ├── 4-median-of-two-sorted-arrays.go └── README.md ├── 8-string-to-integer-atoi └── NOTES.md ├── 102-binary-tree-level-order-traversal ├── NOTES.md ├── 102-binary-tree-level-order-traversal.go └── README.md ├── 73-set-matrix-zeroes └── NOTES.md ├── 75-sort-colors ├── NOTES.md ├── 75-sort-colors.go └── README.md ├── 287-find-the-duplicate-number ├── 287-find-the-duplicate-number.go └── NOTES.md ├── 350-intersection-of-two-arrays-ii ├── NOTES.md └── 350-intersection-of-two-arrays-ii.go ├── 31-next-permutation ├── NOTES.md └── 31-next-permutation.go ├── 48-rotate-image ├── 48-rotate-image.go ├── NOTES.md └── README.md ├── 12-integer-to-roman ├── NOTES.md └── 12-integer-to-roman.go ├── .github └── ISSUE_TEMPLATE │ └── feature_request.md ├── 199-binary-tree-right-side-view ├── 199-binary-tree-right-side-view.go └── README.md ├── 88-merge-sorted-array ├── NOTES.md └── 88-merge-sorted-array.go ├── LICENSE └── REAL_LIFE_APPLICATION.md /15-3sum/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /69-sqrtx/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1-two-sum/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /100-same-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /50-powx-n/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /66-plus-one/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /78-subsets/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /90-subsets-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /133-clone-graph/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /136-single-number/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /2-add-two-numbers/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /242-valid-anagram/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /27-remove-element/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /289-game-of-life/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /344-reverse-string/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /36-valid-sudoku/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /39-combination-sum/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /46-permutations/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /49-group-anagrams/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /520-detect-capital/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /56-merge-intervals/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /61-rotate-list/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /62-unique-paths/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /70-climbing-stairs/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /704-binary-search/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /705-design-hashset/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /729-my-calendar-i/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /77-combinations/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /118-pascals-triangle/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /119-pascals-triangle-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /125-valid-palindrome/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /13-roman-to-integer/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /130-surrounded-regions/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /141-linked-list-cycle/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /162-find-peak-element/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /169-majority-element/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /20-valid-parentheses/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /200-number-of-islands/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /2013-detect-squares/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /206-reverse-linked-list/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /207-course-schedule/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /210-course-schedule-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /216-combination-sum-iii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /217-contains-duplicate/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /22-generate-parentheses/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /226-invert-binary-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /229-majority-element-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /278-first-bad-version/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /40-combination-sum-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /461-hamming-distance/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /53-maximum-subarray/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /566-reshape-the-matrix/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /58-length-of-last-word/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /64-minimum-path-sum/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /680-valid-palindrome-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /695-max-area-of-island/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /74-search-a-2d-matrix/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /858-mirror-reflection/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /875-koko-eating-bananas/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /9-palindrome-number/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /997-find-the-town-judge/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /11-container-with-most-water/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /131-palindrome-partitioning/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /14-longest-common-prefix/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /142-linked-list-cycle-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1603-design-parking-system/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /21-merge-two-sorted-lists/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /219-contains-duplicate-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /2296-design-a-text-editor/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /234-palindrome-linked-list/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /240-search-a-2d-matrix-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /328-odd-even-linked-list/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /35-search-insert-position/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /410-split-array-largest-sum/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /543-diameter-of-binary-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /576-out-of-boundary-paths/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /746-min-cost-climbing-stairs/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /804-unique-morse-code-words/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /890-find-and-replace-pattern/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /895-maximum-frequency-stack/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /92-reverse-linked-list-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /104-maximum-depth-of-binary-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /111-minimum-depth-of-binary-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /128-longest-consecutive-sequence/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1329-sort-the-matrix-diagonally/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /138-copy-list-with-random-pointer/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1539-kth-missing-positive-number/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1791-find-center-of-star-graph/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /203-remove-linked-list-elements/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /2351-first-letter-to-appear-twice/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /33-search-in-rotated-sorted-array/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /559-maximum-depth-of-n-ary-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /876-middle-of-the-linked-list/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /94-binary-tree-inorder-traversal/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /98-validate-binary-search-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /121-best-time-to-buy-and-sell-stock/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /144-binary-tree-preorder-traversal/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1448-count-good-nodes-in-binary-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /145-binary-tree-postorder-traversal/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /160-intersection-of-two-linked-lists/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /19-remove-nth-node-from-end-of-list/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /26-remove-duplicates-from-sorted-array/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /300-longest-increasing-subsequence/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /387-first-unique-character-in-a-string/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /540-single-element-in-a-sorted-array/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /637-average-of-levels-in-binary-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /792-number-of-matching-subsequences/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /81-search-in-rotated-sorted-array-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /83-remove-duplicates-from-sorted-list/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /153-find-minimum-in-rotated-sorted-array/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /17-letter-combinations-of-a-phone-number/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1011-capacity-to-ship-packages-within-d-days/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /108-convert-sorted-array-to-binary-search-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1283-find-the-smallest-divisor-given-a-threshold/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1482-minimum-number-of-days-to-make-m-bouquets/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /2095-delete-the-middle-node-of-a-linked-list/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /3-longest-substring-without-repeating-characters/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /235-lowest-common-ancestor-of-a-binary-search-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /101-symmetric-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | Rott, left, Right == Root, Right, left -------------------------------------------------------------------------------- /34-find-first-and-last-position-of-element-in-sorted-array/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /105-construct-binary-tree-from-preorder-and-inorder-traversal/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /106-construct-binary-tree-from-inorder-and-postorder-traversal/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /5-longest-palindromic-substring/NOTES.md: -------------------------------------------------------------------------------- 1 | # longest palindromic substring 2 | ​ -------------------------------------------------------------------------------- /4-median-of-two-sorted-arrays/NOTES.md: -------------------------------------------------------------------------------- 1 | ## 1st method - using two pointer and merge -------------------------------------------------------------------------------- /8-string-to-integer-atoi/NOTES.md: -------------------------------------------------------------------------------- 1 | ## string to integer 2 | ​ 3 | loop through the string find the first +/- or digit -------------------------------------------------------------------------------- /102-binary-tree-level-order-traversal/NOTES.md: -------------------------------------------------------------------------------- 1 | [https://go.dev/play/p/DFRhWF05H0Y](https://go.dev/play/p/DFRhWF05H0Y) 2 | -------------------------------------------------------------------------------- /520-detect-capital/520-detect-capital.go: -------------------------------------------------------------------------------- 1 | func detectCapitalUse(word string) bool { 2 | s, _ := regexp.MatchString("^[A-Z]*$|^[a-z]*$|^[A-Z][a-z]*$", word) 3 | return s 4 | } -------------------------------------------------------------------------------- /73-set-matrix-zeroes/NOTES.md: -------------------------------------------------------------------------------- 1 | Using constant extra space 2 | ​ 3 | clearRow := make([]int, m) // which rows needs to be set as 0 4 | clearColumn := make([]int, n) // column needs to be set as 0 5 | ​ 6 | ​ -------------------------------------------------------------------------------- /136-single-number/136-single-number.go: -------------------------------------------------------------------------------- 1 | // 0^number = number 2 | // number^number = 0 3 | func singleNumber(nums []int) int { 4 | xor := 0 5 | for _, n := range nums { 6 | xor ^= n 7 | } 8 | return xor 9 | } -------------------------------------------------------------------------------- /1791-find-center-of-star-graph/1791-find-center-of-star-graph.go: -------------------------------------------------------------------------------- 1 | func findCenter(edges [][]int) int { 2 | if edges[0][0] == edges[1][0] || edges[0][0] == edges[1][1] { 3 | return edges[0][0] 4 | } 5 | return edges[0][1] 6 | } -------------------------------------------------------------------------------- /344-reverse-string/344-reverse-string.go: -------------------------------------------------------------------------------- 1 | func reverseString(s []byte) { 2 | left := 0 3 | right := len(s)-1 4 | 5 | for left < right { 6 | s[left], s[right] = s[right], s[left] 7 | left++ 8 | right-- 9 | } 10 | } -------------------------------------------------------------------------------- /217-contains-duplicate/217-contains-duplicate.go: -------------------------------------------------------------------------------- 1 | func containsDuplicate(nums []int) bool { 2 | appearance := make(map[int]bool) 3 | 4 | for _, num := range nums { 5 | if _, ok := appearance[num]; ok { 6 | return true 7 | } 8 | appearance[num] = true 9 | } 10 | return false 11 | } -------------------------------------------------------------------------------- /27-remove-element/27-remove-element.go: -------------------------------------------------------------------------------- 1 | // two pointer approach 2 | func removeElement(nums []int, val int) int { 3 | lower := 0 4 | 5 | for i:=0; i< len(nums); i++ { 6 | if nums[i] != val { 7 | nums[lower] = nums[i] 8 | lower++ 9 | } 10 | } 11 | return lower 12 | } -------------------------------------------------------------------------------- /219-contains-duplicate-ii/219-contains-duplicate-ii.go: -------------------------------------------------------------------------------- 1 | func containsNearbyDuplicate(nums []int, k int) bool { 2 | mapOfEle := make(map[int]int) 3 | 4 | for i1, n := range nums { 5 | if i2, ok := mapOfEle[n]; ok { 6 | if i1-i2 <= k { 7 | return true 8 | } 9 | } 10 | mapOfEle[n] = i1 11 | } 12 | return false 13 | } -------------------------------------------------------------------------------- /2351-first-letter-to-appear-twice/2351-first-letter-to-appear-twice.go: -------------------------------------------------------------------------------- 1 | func repeatedCharacter(s string) byte { 2 | //Time Taken O(n) 3 | freqMap := make(map[byte]int) 4 | for i, _ := range s { 5 | freqMap[s[i]]++ 6 | if freqMap[s[i]] == 2 { 7 | return s[i] 8 | } 9 | } 10 | return ' ' 11 | } -------------------------------------------------------------------------------- /461-hamming-distance/461-hamming-distance.go: -------------------------------------------------------------------------------- 1 | func hammingDistance(x int, y int) int { 2 | // use XOR operation 3 | // 00 - 0 4 | // 01 - 1 5 | // 10 - 1 6 | // 11 - 0 7 | result := 0 8 | for ;x!=0 || y!=0; { 9 | result = result + (x%2 ^ y%2) 10 | x = x>>1 11 | y = y>>1 12 | } 13 | return result 14 | } -------------------------------------------------------------------------------- /75-sort-colors/NOTES.md: -------------------------------------------------------------------------------- 1 | # Dutch national flag algorithm 2 | 3 | ## Rule 4 | [0 .. low-1] -> 0 \ 5 | [high + 1 ..... n ] -> 2 6 | 7 | ## Initially 8 | high == n-1 \ 9 | mid == 0 \ 10 | low == 0 11 | 12 | ## Conditions 13 | Now mid can be 0, 1, 2 \ 14 | 15 | if mid == 0 swap (low, mid) \ 16 | if mid == 1 mid ++ \ 17 | if mid == 2 swap( high, mid), high -- 18 | 19 | -------------------------------------------------------------------------------- /858-mirror-reflection/858-mirror-reflection.go: -------------------------------------------------------------------------------- 1 | func mirrorReflection(p int, q int) int { 2 | 3 | for p%2==0 && q%2==0 { 4 | p= p/2 5 | q= q/2 6 | } 7 | 8 | if (p%2 == 0 && q%2 != 0) { 9 | return 2 10 | } else if (p%2 != 0 && q%2 == 0) { 11 | return 0 12 | } else { 13 | return 1 14 | } 15 | } -------------------------------------------------------------------------------- /387-first-unique-character-in-a-string/387-first-unique-character-in-a-string.go: -------------------------------------------------------------------------------- 1 | //Time Taken O(n) 2 | func firstUniqChar(s string) int { 3 | freqMap := make(map[byte]int) 4 | for i, _ := range s { 5 | freqMap[s[i]]++ 6 | } 7 | for i, _ := range s { 8 | if freqMap[s[i]] == 1 { 9 | return i 10 | } 11 | } 12 | return -1 13 | } -------------------------------------------------------------------------------- /26-remove-duplicates-from-sorted-array/26-remove-duplicates-from-sorted-array.go: -------------------------------------------------------------------------------- 1 | // Two pointer Approach 2 | func removeDuplicates(nums []int) int { 3 | lower := 1 4 | for i:=1; i< len(nums);i++ { 5 | if nums[i] != nums[i-1] { 6 | // new number found 7 | nums[lower] = nums[i] 8 | lower++ 9 | } 10 | } 11 | return lower 12 | } -------------------------------------------------------------------------------- /287-find-the-duplicate-number/287-find-the-duplicate-number.go: -------------------------------------------------------------------------------- 1 | func findDuplicate(nums []int) int { 2 | slow := nums[0] 3 | fast := nums[nums[0]] 4 | for slow != fast { 5 | slow = nums[slow] 6 | fast = nums[nums[fast]] 7 | } 8 | fast = 0 9 | for slow != fast { 10 | slow = nums[slow] 11 | fast = nums[fast] 12 | } 13 | return slow 14 | } -------------------------------------------------------------------------------- /350-intersection-of-two-arrays-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | * Hashing 2 | ​ 3 | 1. Time Complexity O(n) 4 | ​ 5 | 2. Space complexity - O(n) It uses a map for first array, and array to store answer 6 | ​ 7 | * Populate a map which contains the element of first array as key and the count of it a value. 8 | Traverse through the second array, if the element exist in Map and it count >0 then add the number to answer and decrese the count. 9 | ​ -------------------------------------------------------------------------------- /49-group-anagrams/49-group-anagrams.go: -------------------------------------------------------------------------------- 1 | // TimeComplexity - O(nk) 2 | func groupAnagrams(strs []string) [][]string { 3 | mp := map[[26]int][]string{} 4 | for _, s := range strs { 5 | k := [26]int{} 6 | for i := 0; i < len(s); i++ { 7 | k[s[i]-'a'] += 1 8 | } 9 | mp[k] = append(mp[k], s) 10 | } 11 | res := [][]string{} 12 | for _, v := range mp { 13 | res = append(res, v) 14 | } 15 | return res 16 | } -------------------------------------------------------------------------------- /53-maximum-subarray/53-maximum-subarray.go: -------------------------------------------------------------------------------- 1 | func maxSubArray(nums []int) int { 2 | n := len(nums) 3 | max := nums[0] 4 | sum := 0 5 | // kadane 6 | for i :=0; i < n; i++ { 7 | sum = sum + nums[i] 8 | if sum > max { 9 | max = sum 10 | } 11 | if sum < 0 { 12 | sum =0 13 | } 14 | //fmt.Println(sum) 15 | } 16 | return max 17 | } -------------------------------------------------------------------------------- /66-plus-one/66-plus-one.go: -------------------------------------------------------------------------------- 1 | func plusOne(digits []int) []int { 2 | for i := len(digits)-1; i >=0; i-- { 3 | if digits[i] != 9 { 4 | digits[i]++ 5 | break; 6 | } else { 7 | digits[i] = 0 8 | } 9 | } 10 | if (digits[0] == 0) { 11 | res := make([]int, len(digits)+1) 12 | res[0] = 1; 13 | return res; 14 | } 15 | return digits; 16 | } -------------------------------------------------------------------------------- /169-majority-element/169-majority-element.go: -------------------------------------------------------------------------------- 1 | func majorityElement(nums []int) int { 2 | count := 0 3 | candidate := 0 4 | 5 | for _ ,num := range nums { 6 | if (count == 0) { 7 | candidate = num 8 | } 9 | if num == candidate{ 10 | count = count + 1 11 | }else { 12 | count = count - 1 13 | } 14 | } 15 | return candidate 16 | } -------------------------------------------------------------------------------- /31-next-permutation/NOTES.md: -------------------------------------------------------------------------------- 1 | [https://youtu.be/LuLCLgMElus](https://youtu.be/LuLCLgMElus) 2 | ​ 3 | ​ 4 | [https://takeuforward.org/data-structure/next_permutation-find-next-lexicographically-greater-permutation/](https://takeuforward.org/data-structure/next_permutation-find-next-lexicographically-greater-permutation/) 5 | ​ 6 | ​ 7 | * so in a dictionary the pattern is multiple local maxima, we need to find the first breakpoint from the end. -------------------------------------------------------------------------------- /1-two-sum/1-two-sum.go: -------------------------------------------------------------------------------- 1 | func twoSum(nums []int, target int) []int { 2 | // initialize an answer slice 3 | var ans []int 4 | numToIdx := make(map[int]int) 5 | 6 | for index, num := range nums { 7 | requiredNum := target - num 8 | if value, exists := numToIdx[requiredNum]; exists { 9 | ans = append(ans, value, index) 10 | } 11 | numToIdx[num] = index 12 | } 13 | return ans 14 | } -------------------------------------------------------------------------------- /58-length-of-last-word/58-length-of-last-word.go: -------------------------------------------------------------------------------- 1 | func lengthOfLastWord(s string) int { 2 | wordPos := 0 3 | for i := len(s)-1; i >=0; i-- { 4 | if s[i] != 32 { 5 | wordPos = i 6 | break 7 | } 8 | } 9 | i := wordPos 10 | for ; i >= 0; i-- { 11 | // fmt.Println(s[i]) 12 | if s[i] == 32 { 13 | break 14 | } 15 | } 16 | return wordPos - i 17 | } -------------------------------------------------------------------------------- /876-middle-of-the-linked-list/876-middle-of-the-linked-list.go: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * type ListNode struct { 4 | * Val int 5 | * Next *ListNode 6 | * } 7 | */ 8 | func middleNode(head *ListNode) *ListNode { 9 | left := head 10 | right := left 11 | for right != nil && right.Next != nil { 12 | left = left.Next 13 | right = right.Next.Next 14 | } 15 | return left 16 | } -------------------------------------------------------------------------------- /704-binary-search/704-binary-search.go: -------------------------------------------------------------------------------- 1 | func search(nums []int, target int) int { 2 | low :=0 3 | high := len(nums) - 1 4 | for low <= high { 5 | mid := low + (high - low)/2 6 | if nums[mid] == target { 7 | return mid 8 | } else if nums[mid] > target { 9 | high = mid -1 10 | } else if nums[mid] < target { 11 | low = mid + 1 12 | } 13 | } 14 | return -1 15 | } -------------------------------------------------------------------------------- /100-same-tree/100-same-tree.go: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * type TreeNode struct { 4 | * Val int 5 | * Left *TreeNode 6 | * Right *TreeNode 7 | * } 8 | */ 9 | func isSameTree(p *TreeNode, q *TreeNode) bool { 10 | if p==nil || q==nil{ 11 | return p==q 12 | } 13 | if (p.Val == q.Val) && isSameTree(p.Left, q.Left) && isSameTree(p.Right, q.Right) { 14 | return true 15 | } 16 | return false 17 | } -------------------------------------------------------------------------------- /13-roman-to-integer/13-roman-to-integer.go: -------------------------------------------------------------------------------- 1 | func romanToInt(s string) int { 2 | roman := map[uint8]int{ 3 | 73: 1, 4 | 86: 5, 5 | 88: 10, 6 | 76: 50, 7 | 67: 100, 8 | 68: 500, 9 | 77: 1000, 10 | } 11 | var a int = 0 12 | var i uint8 = 0 13 | for ; i < uint8(len(s))-1; i++ { 14 | if roman[s[i]] < roman[s[i+1]] { 15 | a -= roman[s[i]] 16 | } else { 17 | a += roman[s[i]] 18 | } 19 | } 20 | a += roman[s[i]] 21 | return int(a) 22 | } 23 | -------------------------------------------------------------------------------- /226-invert-binary-tree/226-invert-binary-tree.go: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * type TreeNode struct { 4 | * Val int 5 | * Left *TreeNode 6 | * Right *TreeNode 7 | * } 8 | */ 9 | func invertTree(root *TreeNode) *TreeNode { 10 | if root == nil { 11 | return nil 12 | } 13 | left := invertTree(root.Left) 14 | right := invertTree(root.Right) 15 | root.Left = right 16 | root.Right = left 17 | return root 18 | } -------------------------------------------------------------------------------- /746-min-cost-climbing-stairs/746-min-cost-climbing-stairs.go: -------------------------------------------------------------------------------- 1 | import "math" 2 | // At any given position we need to find the minimum of previous two step 3 | // TimeComplexity - O(n) 4 | // Space complexity - O(1) 5 | func minCostClimbingStairs(cost []int) int { 6 | for i := 2; i < len(cost); i++ { 7 | cost[i] += int(math.Min(float64(cost[i-1]), float64(cost[i-2]))); 8 | } 9 | return int(math.Min(float64(cost[len(cost)-1]), float64(cost[len(cost)-2]))); 10 | } -------------------------------------------------------------------------------- /144-binary-tree-preorder-traversal/144-binary-tree-preorder-traversal.go: -------------------------------------------------------------------------------- 1 | func preorderTraversal(root *TreeNode) []int { 2 | if root == nil { 3 | return nil 4 | } 5 | var ans []int 6 | traverse(root, &ans) 7 | return ans 8 | } 9 | 10 | func traverse(root *TreeNode, ans *[]int) { 11 | if root == nil { 12 | return 13 | } 14 | 15 | *ans = append(*ans, root.Val) 16 | traverse(root.Left, ans) 17 | traverse(root.Right, ans) 18 | } -------------------------------------------------------------------------------- /35-search-insert-position/35-search-insert-position.go: -------------------------------------------------------------------------------- 1 | func searchInsert(nums []int, target int) int { 2 | left :=0 3 | right := len(nums) - 1 4 | for left <= right { 5 | mid := left + (right-left)/2 6 | if nums[mid] == target { 7 | return mid 8 | } else if target < nums[mid] { 9 | right = mid - 1 10 | } else if target > nums[mid] { 11 | left = mid + 1 12 | } 13 | } 14 | return left 15 | } -------------------------------------------------------------------------------- /48-rotate-image/48-rotate-image.go: -------------------------------------------------------------------------------- 1 | func rotate(matrix [][]int) { 2 | // transpose of a matrix 3 | n := len(matrix) 4 | for i:=0; i depth { 17 | depth = d 18 | } 19 | } 20 | return depth + 1 21 | } -------------------------------------------------------------------------------- /104-maximum-depth-of-binary-tree/104-maximum-depth-of-binary-tree.go: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * type TreeNode struct { 4 | * Val int 5 | * Left *TreeNode 6 | * Right *TreeNode 7 | * } 8 | */ 9 | func maxDepth(root *TreeNode) int { 10 | if root == nil { 11 | return 0 12 | } 13 | left := maxDepth(root.Left) 14 | right := maxDepth(root.Right) 15 | if left > right { 16 | return left +1 17 | } 18 | return right + 1 19 | } -------------------------------------------------------------------------------- /242-valid-anagram/242-valid-anagram.go: -------------------------------------------------------------------------------- 1 | // Time Complexity - O(n) 2 | // Space Complexity - O(1) 3 | func isAnagram(s string, t string) bool { 4 | if len(t) != len(s){ 5 | return false 6 | } 7 | chars := [26]int{} 8 | for i := 0; i < len(s); i++ { 9 | chars[int(s[i]-'a')]++ 10 | chars[int(t[i]-'a')]-- 11 | } 12 | for i := 0; i < 26; i ++ { 13 | if chars[i] != 0 { 14 | return false 15 | } 16 | } 17 | return true 18 | } 19 | -------------------------------------------------------------------------------- /997-find-the-town-judge/997-find-the-town-judge.go: -------------------------------------------------------------------------------- 1 | func findJudge(n int, trust [][]int) int { 2 | if n == 1 { 3 | return 1 4 | } 5 | trustPerson := make([]int, n) 6 | 7 | for _, t := range trust { 8 | // the person trust someone 9 | trustPerson[t[0]-1]-- 10 | // the person is trusted by someone 11 | trustPerson[t[1]-1]++ 12 | } 13 | 14 | for k := range trustPerson { 15 | if trustPerson[k] == n-1 { 16 | return k+1 17 | } 18 | } 19 | return -1 20 | } 21 | 22 | -------------------------------------------------------------------------------- /203-remove-linked-list-elements/203-remove-linked-list-elements.go: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * type ListNode struct { 4 | * Val int 5 | * Next *ListNode 6 | * } 7 | */ 8 | func removeElements(head *ListNode, val int) *ListNode { 9 | left := &ListNode{Next: head} 10 | right := left 11 | for right.Next != nil { 12 | if right.Next.Val == val { 13 | right.Next = right.Next.Next 14 | } else { 15 | right = right.Next 16 | } 17 | } 18 | 19 | return left.Next 20 | } -------------------------------------------------------------------------------- /11-container-with-most-water/11-container-with-most-water.go: -------------------------------------------------------------------------------- 1 | func maxArea(height []int) int { 2 | area := 0 3 | for i, j:= 0, len(height)-1; i < j; { 4 | currentarea := 0 5 | if height[i] < height[j] { 6 | currentarea = height[i]* (j-i) 7 | i++ 8 | } else { 9 | currentarea = height[j]* (j-i) 10 | j-- 11 | } 12 | if area < currentarea { 13 | area = currentarea 14 | } 15 | } 16 | return area 17 | } -------------------------------------------------------------------------------- /69-sqrtx/69-sqrtx.go: -------------------------------------------------------------------------------- 1 | func mySqrt(x int) int { 2 | left := 0 3 | right := x 4 | 5 | for left <= right { 6 | mid := left + (right - left)/2 7 | fmt.Println(left, mid, right) 8 | val := mid * mid 9 | nextVal := (mid+1) * (mid+1) 10 | if x >= val && x < nextVal { 11 | return mid 12 | } 13 | if val > x { 14 | right = mid - 1 15 | } else { 16 | left = mid + 1 17 | } 18 | } 19 | 20 | return -1 21 | } -------------------------------------------------------------------------------- /14-longest-common-prefix/14-longest-common-prefix.go: -------------------------------------------------------------------------------- 1 | func longestCommonPrefix(strs []string) string { 2 | result := strings.Builder{} 3 | if len(strs) == 0 { 4 | return result.String() 5 | } 6 | 7 | for i := 0; i < len(strs[0]); i++ { 8 | c := strs[0][i] 9 | same := true 10 | for _, s := range strs { 11 | if i >= len(s) || s[i] != c { 12 | same = false 13 | break 14 | } 15 | } 16 | 17 | if !same { 18 | break 19 | } 20 | 21 | result.WriteByte(c) 22 | } 23 | 24 | return result.String() 25 | } -------------------------------------------------------------------------------- /278-first-bad-version/278-first-bad-version.go: -------------------------------------------------------------------------------- 1 | /** 2 | * Forward declaration of isBadVersion API. 3 | * @param version your guess about first bad version 4 | * @return true if current version is bad 5 | * false if current version is good 6 | * func isBadVersion(version int) bool; 7 | */ 8 | 9 | func firstBadVersion(n int) int { 10 | lo, hi := 1, n 11 | for lo < hi { 12 | if mid := (lo+hi)/2; isBadVersion(mid) { 13 | hi = mid 14 | } else { 15 | lo = mid+1 16 | } 17 | } 18 | 19 | return lo 20 | } -------------------------------------------------------------------------------- /50-powx-n/50-powx-n.go: -------------------------------------------------------------------------------- 1 | // binary Search 2 | func myPow(x float64, n int) float64 { 3 | negative := false 4 | if n < 0 { 5 | negative = true 6 | n = 0-n 7 | } 8 | nn := n 9 | ans := float64(1) 10 | for nn>0 { 11 | if (nn % 2 == 1) { 12 | ans = ans * x 13 | nn-- 14 | } else { 15 | x = x*x // double the x 16 | nn = nn/2 // half the power 17 | } 18 | } 19 | if negative == true { 20 | ans = 1/ans 21 | } 22 | return ans 23 | } -------------------------------------------------------------------------------- /70-climbing-stairs/70-climbing-stairs.go: -------------------------------------------------------------------------------- 1 | func climbStairs(n int) int { 2 | // base cases 3 | if n <= 0 { 4 | return 0 5 | } 6 | if n == 1 { 7 | return 1 8 | } 9 | if n == 2 { 10 | return 2 11 | } 12 | 13 | one_step_before := 2 14 | two_steps_before := 1 15 | all_ways := 0 16 | 17 | for i:=2; i tempArr[i] { 11 | tempArr[i] = tempArr[j] + 1 12 | } 13 | } 14 | } 15 | if tempArr[i] > max { 16 | max = tempArr[i] 17 | } 18 | } 19 | return max 20 | } -------------------------------------------------------------------------------- /111-minimum-depth-of-binary-tree/111-minimum-depth-of-binary-tree.go: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * type TreeNode struct { 4 | * Val int 5 | * Left *TreeNode 6 | * Right *TreeNode 7 | * } 8 | */ 9 | func minDepth(root *TreeNode) int { 10 | if root == nil { 11 | return 0 12 | } 13 | left := minDepth(root.Left) 14 | right := minDepth(root.Right) 15 | if left == 0 { 16 | return right + 1 17 | } 18 | if right == 0 { 19 | return left + 1 20 | } 21 | if left > right { 22 | return right +1 23 | } 24 | return left + 1 25 | } -------------------------------------------------------------------------------- /22-generate-parentheses/22-generate-parentheses.go: -------------------------------------------------------------------------------- 1 | func generateParenthesis(n int) []string { 2 | res := []string{} 3 | // number of paranthesis left to be printed 4 | backtrack(&res, "", n, n) 5 | return res 6 | } 7 | 8 | func backtrack(res *[]string, path string, open int, cl int) { 9 | if open == 0 && cl == 0 { 10 | *res = append(*res, path) 11 | return 12 | } 13 | // open paranthesis left to be added 14 | if open > 0 { 15 | backtrack(res, path+"(", open-1, cl) 16 | } 17 | // done with open paranthesis 18 | if cl > open { 19 | backtrack(res, path+")", open, cl-1) 20 | } 21 | } -------------------------------------------------------------------------------- /3-longest-substring-without-repeating-characters/3-longest-substring-without-repeating-characters.go: -------------------------------------------------------------------------------- 1 | func lengthOfLongestSubstring(s string) int { 2 | left := 0 3 | right := 0 4 | maxlen :=0 5 | posMap := make(map[byte]int, len(s)) 6 | for right < len(s) { 7 | if i , ok := posMap[s[right]]; ok { 8 | if i >= left && i <= right { 9 | left = i + 1 10 | } 11 | } 12 | posMap[s[right]] = right 13 | len := right -left +1 14 | if len > maxlen { 15 | maxlen = len 16 | } 17 | right++ 18 | } 19 | return maxlen 20 | } -------------------------------------------------------------------------------- /12-integer-to-roman/NOTES.md: -------------------------------------------------------------------------------- 1 | # convert slice of string to string using strings.Join 2 | ​ 3 | # improvement -- can be done using while loop 4 | ​ 5 | ​ 6 | ``` 7 | class Solution { 8 | public String intToRoman(int num) { 9 | String roman[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}; 10 | int integer[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; 11 | String result=""; 12 | int i=0; 13 | while(num>0 && i=integer[i]) { 15 | int n= num/integer[i]; 16 | num= num%integer[i]; 17 | while(n-->0) result+=roman[i]; 18 | } 19 | i++; 20 | } 21 | return result; 22 | } 23 | } 24 | ``` -------------------------------------------------------------------------------- /1448-count-good-nodes-in-binary-tree/1448-count-good-nodes-in-binary-tree.go: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * type TreeNode struct { 4 | * Val int 5 | * Left *TreeNode 6 | * Right *TreeNode 7 | * } 8 | */ 9 | func recurse(root *TreeNode, ans *int, maxValueSeen int) { 10 | if root == nil { 11 | return 12 | } 13 | if root.Val >= maxValueSeen { 14 | *ans++ 15 | maxValueSeen = root.Val 16 | } 17 | recurse(root.Left, ans, maxValueSeen) 18 | recurse(root.Right, ans, maxValueSeen) 19 | } 20 | 21 | func goodNodes(root *TreeNode) int { 22 | ans := 0 23 | recurse(root, &ans, root.Val) 24 | return ans 25 | } -------------------------------------------------------------------------------- /145-binary-tree-postorder-traversal/145-binary-tree-postorder-traversal.go: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * type TreeNode struct { 4 | * Val int 5 | * Left *TreeNode 6 | * Right *TreeNode 7 | * } 8 | */ 9 | 10 | func postorderTraversal(root *TreeNode) []int { 11 | if root == nil { 12 | return nil 13 | } 14 | var ans []int 15 | traverse(root, &ans) 16 | return ans 17 | } 18 | 19 | func traverse(root *TreeNode, ans *[]int) { 20 | if root == nil { 21 | return 22 | } 23 | 24 | traverse(root.Left, ans) 25 | traverse(root.Right, ans) 26 | *ans = append(*ans, root.Val) 27 | } -------------------------------------------------------------------------------- /162-find-peak-element/162-find-peak-element.go: -------------------------------------------------------------------------------- 1 | func findPeakElement(nums []int) int { 2 | low :=0 3 | high := len(nums) - 1 4 | n := len(nums) 5 | for low <= high { 6 | mid := low + (high - low)/2 7 | // first case if mid is the answer 8 | if ((mid == 0 || nums[mid - 1] <= nums[mid]) && (mid == n - 1 || nums[mid + 1] <= nums[mid])) { 9 | return mid 10 | } else if (mid > 0 && nums[mid - 1] > nums[mid]) {// move the right pointer 11 | high = mid - 1 12 | } else { 13 | low = mid + 1; // move the left pointer 14 | } 15 | } 16 | return -1 17 | } -------------------------------------------------------------------------------- /235-lowest-common-ancestor-of-a-binary-search-tree/235-lowest-common-ancestor-of-a-binary-search-tree.go: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * type TreeNode struct { 4 | * Val int 5 | * Left *TreeNode 6 | * Right *TreeNode 7 | * } 8 | */ 9 | 10 | func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode { 11 | if root == nil { 12 | return nil 13 | } 14 | if (p.Val < root.Val && q.Val < root.Val) { 15 | return lowestCommonAncestor(root.Left, p, q) 16 | } else if (p.Val > root.Val && q.Val > root.Val) { 17 | return lowestCommonAncestor(root.Right, p, q) 18 | } 19 | return root 20 | } -------------------------------------------------------------------------------- /83-remove-duplicates-from-sorted-list/83-remove-duplicates-from-sorted-list.go: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * type ListNode struct { 4 | * Val int 5 | * Next *ListNode 6 | * } 7 | */ 8 | func deleteDuplicates(head *ListNode) *ListNode { 9 | if head == nil { 10 | return nil 11 | } 12 | 13 | left := head 14 | right := head 15 | 16 | for right.Next != nil { 17 | if right.Next.Val != right.Val { 18 | left.Next = right.Next 19 | left = left.Next 20 | } 21 | right = right.Next 22 | } 23 | 24 | left.Next = nil 25 | 26 | return head 27 | } -------------------------------------------------------------------------------- /94-binary-tree-inorder-traversal/94-binary-tree-inorder-traversal.go: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * type TreeNode struct { 4 | * Val int 5 | * Left *TreeNode 6 | * Right *TreeNode 7 | * } 8 | */ 9 | func inorderTraversal(root *TreeNode) []int { 10 | var ans []int 11 | inorder(root, &ans) 12 | return ans 13 | } 14 | 15 | func inorder(root *TreeNode, ans *[]int) { 16 | if root == nil { 17 | return 18 | } 19 | if root.Left != nil { 20 | inorder(root.Left, ans) 21 | } 22 | *ans = append(*ans, root.Val) 23 | if root.Right != nil { 24 | inorder(root.Right, ans) 25 | } 26 | } -------------------------------------------------------------------------------- /46-permutations/46-permutations.go: -------------------------------------------------------------------------------- 1 | func permute(nums []int) [][]int { 2 | var result [][]int 3 | findPermutation([]int{},nums,&result) 4 | return result 5 | } 6 | 7 | // // T: O(n!) 8 | // // S: O(n!) 9 | func findPermutation(included []int, left []int, result *[][]int) { 10 | if len(left) == 0 { 11 | ans1 := append([]int{}, included...) 12 | *result = append(*result, ans1) 13 | return 14 | } 15 | for idx, l := range left { 16 | findPermutation( 17 | append(included, l), 18 | append(append([]int{}, left[:idx]...),left[idx+1:]...), 19 | result) 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /56-merge-intervals/56-merge-intervals.go: -------------------------------------------------------------------------------- 1 | func merge(intervals [][]int) [][]int { 2 | if len(intervals) == 0 { 3 | return [][]int{} 4 | } 5 | var result [][]int 6 | 7 | sort.Slice(intervals, func(i, j int) bool { 8 | return intervals[i][0] < intervals[j][0] 9 | }) 10 | 11 | in := intervals[0] 12 | for i:=1;i= intervals[i][0] { 14 | in[1] = int(math.Max(float64(in[1]), float64(intervals[i][1]))) 15 | 16 | } else { 17 | result = append(result, in) 18 | in = intervals[i] 19 | } 20 | } 21 | return append(result, in) 22 | } -------------------------------------------------------------------------------- /138-copy-list-with-random-pointer/138-copy-list-with-random-pointer.go: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a Node. 3 | * type Node struct { 4 | * Val int 5 | * Next *Node 6 | * Random *Node 7 | * } 8 | */ 9 | 10 | func copyRandomList(head *Node) *Node { 11 | if head == nil { 12 | return nil 13 | } 14 | cloneMap := map[*Node]*Node{} 15 | for cur := head; cur != nil; cur = cur.Next { 16 | cloneMap[cur] = &Node{} 17 | } 18 | for k, v := range cloneMap { 19 | v.Val = k.Val 20 | if k.Next != nil { 21 | v.Next = cloneMap[k.Next] 22 | } 23 | if k.Random != nil { 24 | v.Random = cloneMap[k.Random] 25 | } 26 | } 27 | return cloneMap[head] 28 | } -------------------------------------------------------------------------------- /5-longest-palindromic-substring/5-longest-palindromic-substring.go: -------------------------------------------------------------------------------- 1 | func longestPalindrome(s string) string { 2 | if len(s) < 2 { 3 | return s 4 | } 5 | l := len(s) 6 | var maxStart, maxLen int 7 | for i := 0; i < l; { 8 | left, right := i, i 9 | // check for same character 10 | for right < l-1 && s[right] == s[right+1] { 11 | right++ 12 | } 13 | i = right + 1 // in next loop start from here 14 | for left > 0 && right < l-1 && s[left-1] == s[right+1] { 15 | left-- 16 | right++ 17 | } 18 | if maxLen < right-left { 19 | maxStart = left 20 | maxLen = right - left 21 | } 22 | } 23 | return s[maxStart : maxStart+maxLen+1] 24 | } 25 | -------------------------------------------------------------------------------- /142-linked-list-cycle-ii/142-linked-list-cycle-ii.go: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * type ListNode struct { 4 | * Val int 5 | * Next *ListNode 6 | * } 7 | */ 8 | func detectCycle(head *ListNode) *ListNode { 9 | slow := head 10 | fast := slow 11 | for fast != nil && fast.Next != nil { 12 | slow = slow.Next 13 | fast = fast.Next.Next 14 | if fast == slow { 15 | slow2 := head 16 | 17 | for slow2 != slow { 18 | slow = slow.Next 19 | slow2 = slow2.Next 20 | } 21 | return slow 22 | } 23 | } 24 | return nil 25 | 26 | } -------------------------------------------------------------------------------- /543-diameter-of-binary-tree/543-diameter-of-binary-tree.go: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * type TreeNode struct { 4 | * Val int 5 | * Left *TreeNode 6 | * Right *TreeNode 7 | * } 8 | */ 9 | func diameterOfBinaryTree(root *TreeNode) int { 10 | var mx float64 11 | mx = 0 12 | heightOfTree(root, &mx) 13 | return int(mx) 14 | } 15 | 16 | func heightOfTree(root *TreeNode, max *float64) float64 { 17 | if root == nil { 18 | return 0 19 | } 20 | left := heightOfTree(root.Left, max) 21 | right := heightOfTree(root.Right, max) 22 | *max = math.Max(*max, left+right) 23 | return 1 + math.Max(left, right) 24 | } -------------------------------------------------------------------------------- /125-valid-palindrome/125-valid-palindrome.go: -------------------------------------------------------------------------------- 1 | func isPalindrome(s string) bool { 2 | n := len(s) 3 | if n <= 0 { 4 | return true 5 | } 6 | 7 | i := 0 8 | j := n - 1 9 | 10 | for j > i { 11 | // check character is alphabet 12 | if (s[i] < 'a' || s[i] > 'z') && (s[i] < 'A' || s[i] > 'Z') && (s[i] < '0' || s[i] > '9') { 13 | i++ 14 | continue 15 | } 16 | if (s[j] < 'a' || s[j] > 'z') && (s[j] < 'A' || s[j] > 'Z') && (s[j] < '0' || s[j] > '9'){ 17 | j-- 18 | continue 19 | } 20 | 21 | if strings.ToLower(string(s[i])) != strings.ToLower(string(s[j])) { 22 | return false 23 | } 24 | 25 | i++ 26 | j-- 27 | 28 | } 29 | return true 30 | } 31 | 32 | -------------------------------------------------------------------------------- /2095-delete-the-middle-node-of-a-linked-list/2095-delete-the-middle-node-of-a-linked-list.go: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * type ListNode struct { 4 | * Val int 5 | * Next *ListNode 6 | * } 7 | */ 8 | func deleteMiddle(head *ListNode) *ListNode { 9 | if head == nil { 10 | return nil 11 | } 12 | dummy := &ListNode{Next : head} 13 | // pointing slow to dummy beacuse this would allow us to delete the node 14 | slow := dummy 15 | fast := dummy 16 | for fast.Next != nil && fast.Next.Next != nil { 17 | fast = fast.Next.Next 18 | slow = slow.Next 19 | } 20 | slow.Next = slow.Next.Next 21 | return dummy.Next 22 | } -------------------------------------------------------------------------------- /804-unique-morse-code-words/804-unique-morse-code-words.go: -------------------------------------------------------------------------------- 1 | func uniqueMorseRepresentations(words []string) int { 2 | trans := make(map[string]struct{}) 3 | for _, word := range words { 4 | morse := GetMorse(word) 5 | trans[morse] = struct{}{} 6 | } 7 | return len(trans) 8 | } 9 | 10 | 11 | func GetMorse(word string) string { 12 | maps := []string{".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."} 13 | out := make([]string, len(word)) 14 | for i, c := range word { 15 | out[i] = maps[c - 'a'] 16 | } 17 | return strings.Join(out, "") 18 | } -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Is your feature request related to a problem? Please describe.** 11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 12 | 13 | **Describe the solution you'd like** 14 | A clear and concise description of what you want to happen. 15 | 16 | **Describe alternatives you've considered** 17 | A clear and concise description of any alternative solutions or features you've considered. 18 | 19 | **Additional context** 20 | Add any other context or screenshots about the feature request here. 21 | -------------------------------------------------------------------------------- /199-binary-tree-right-side-view/199-binary-tree-right-side-view.go: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * type TreeNode struct { 4 | * Val int 5 | * Left *TreeNode 6 | * Right *TreeNode 7 | * } 8 | */ 9 | func rightSideView(root *TreeNode) []int { 10 | result := []int{} 11 | levelOrder(root, &result, 0) 12 | return result 13 | } 14 | 15 | func levelOrder(current *TreeNode, result *[]int, level int) { 16 | if current == nil { 17 | return 18 | } 19 | 20 | if level == len(*result) { 21 | *result = append(*result, current.Val) 22 | } 23 | 24 | (*result)[level] = current.Val 25 | levelOrder(current.Left, result, level+1) 26 | levelOrder(current.Right, result, level+1) 27 | } -------------------------------------------------------------------------------- /328-odd-even-linked-list/328-odd-even-linked-list.go: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * type ListNode struct { 4 | * Val int 5 | * Next *ListNode 6 | * } 7 | */ 8 | func oddEvenList(head *ListNode) *ListNode { 9 | if head == nil { 10 | return nil 11 | } 12 | odd := head 13 | even := head.Next 14 | evenHead := even 15 | for odd.Next != nil && even.Next != nil { 16 | odd.Next = even.Next 17 | odd = odd.Next 18 | even.Next = odd.Next 19 | even = even.Next 20 | } 21 | // now we have the last odd node pointing to last even 22 | // we need to make it point to first even 23 | odd.Next = evenHead 24 | return head 25 | } -------------------------------------------------------------------------------- /133-clone-graph/133-clone-graph.go: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a Node. 3 | * type Node struct { 4 | * Val int 5 | * Neighbors []*Node 6 | * } 7 | */ 8 | 9 | func cloneGraph(node *Node) *Node { 10 | if node == nil { 11 | return nil 12 | } 13 | return clone(node, make(map[int]*Node)) 14 | } 15 | func clone(node *Node, hm map[int]*Node) *Node { 16 | if node, ok := hm[node.Val]; ok { 17 | return node 18 | } 19 | newNode := &Node{} 20 | newNode.Val = node.Val 21 | hm[node.Val] = newNode 22 | for _, neighbor := range node.Neighbors { 23 | newNode.Neighbors = append(newNode.Neighbors, clone(neighbor, hm)) 24 | } 25 | return newNode 26 | } -------------------------------------------------------------------------------- /22-generate-parentheses/README.md: -------------------------------------------------------------------------------- 1 |

22. Generate Parentheses

Medium


Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.

2 | 3 |

 

4 |

Example 1:

5 |
Input: n = 3
 6 | Output: ["((()))","(()())","(())()","()(())","()()()"]
 7 | 

Example 2:

8 |
Input: n = 1
 9 | Output: ["()"]
10 | 
11 |

 

12 |

Constraints:

13 | 14 |
    15 |
  • 1 <= n <= 8
  • 16 |
17 |
-------------------------------------------------------------------------------- /21-merge-two-sorted-lists/21-merge-two-sorted-lists.go: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * type ListNode struct { 4 | * Val int 5 | * Next *ListNode 6 | * } 7 | */ 8 | func mergeTwoLists(list1 *ListNode, list2 *ListNode) *ListNode { 9 | dummy := &ListNode{} 10 | prev := dummy 11 | for list1 != nil || list2 != nil { 12 | if list1 == nil { 13 | prev.Next = list2 14 | break 15 | } 16 | 17 | if list2 == nil { 18 | prev.Next = list1 19 | break 20 | } 21 | 22 | if list1.Val < list2.Val { 23 | prev.Next = list1 24 | list1 = list1.Next 25 | } else { 26 | prev.Next = list2 27 | list2 = list2.Next 28 | } 29 | 30 | prev = prev.Next 31 | } 32 | 33 | return dummy.Next 34 | } -------------------------------------------------------------------------------- /98-validate-binary-search-tree/98-validate-binary-search-tree.go: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * type TreeNode struct { 4 | * Val int 5 | * Left *TreeNode 6 | * Right *TreeNode 7 | * } 8 | */ 9 | func isValidBST(root *TreeNode) bool { 10 | return isValid(root, math.MinInt, math.MaxInt) 11 | } 12 | 13 | func isValid(root *TreeNode, min int, max int) bool { 14 | if root == nil { 15 | return true 16 | } 17 | if root.Val <= min { 18 | return false 19 | } 20 | if root.Val >= max { 21 | return false 22 | } 23 | 24 | if isValid(root.Left, min, root.Val) && isValid(root.Right, root.Val, max) { 25 | return true 26 | } 27 | return false 28 | } -------------------------------------------------------------------------------- /64-minimum-path-sum/64-minimum-path-sum.go: -------------------------------------------------------------------------------- 1 | func minPathSum(grid [][]int) int { 2 | var ans [][]int 3 | m := len(grid) 4 | n := len(grid[0]) 5 | for i:=0; i< m; i++ { 6 | ans = append(ans, make([]int, n)) 7 | } 8 | ans[0][0] = grid[0][0] 9 | 10 | for i:=1; i< m; i++ { 11 | ans[i][0] = grid[i][0] + ans[i-1][0] 12 | } 13 | 14 | for j:=1; j< n; j++ { 15 | ans[0][j] = grid[0][j] + ans[0][j-1] 16 | } 17 | 18 | 19 | for i:= 1; i< m; i++{ 20 | for j:= 1; j< n; j++ { 21 | ans[i][j] = grid[i][j] + int((math.Min(float64(ans[i-1][j]), float64(ans[i][j-1])))) 22 | } 23 | } 24 | return ans[m-1][n-1] 25 | } -------------------------------------------------------------------------------- /88-merge-sorted-array/NOTES.md: -------------------------------------------------------------------------------- 1 | # Solution: Optimal approach 2 | ​ 3 | ## Approach: 4 | Linearly iterate over the array if the data structure is empty insert the interval in the data structure. If the last element in the data structure overlaps with the current interval we merge the intervals by updating the last element in the data structure, and if the current interval does not overlap with the last element in the data structure simply insert it into the data structure. 5 | ​ 6 | ## Intuition: 7 | Since we have sorted the intervals, the intervals which will be merging are bound to be adjacent. We kept on merging simultaneously as we were traversing through the array and when the element was non-overlapping we simply inserted the element in our data structure. -------------------------------------------------------------------------------- /118-pascals-triangle/118-pascals-triangle.go: -------------------------------------------------------------------------------- 1 | // Property of pascal triangle 2 | 3 | // 1 4 | // 1,1 5 | // 1.2.1 6 | // 1,3,3,1 7 | 8 | // first and last element of a row is 1 always 9 | // the count of element in each row is rowNumber+1 10 | 11 | 12 | func generate(numRows int) [][]int { 13 | result := make([][]int, 0) 14 | for i := 0; i < numRows; i++ { 15 | rowElement := make([]int, i+1) 16 | rowElement[0] = 1 // set first column of every row to 1 17 | result = append(result, rowElement) // allot space for each row 18 | for j := 1; j < i+1; j++ { 19 | 20 | if i == j { // last element is always 1 21 | result[i][j] = 1 22 | } else { 23 | result[i][j] = result[i-1][j] + result[i-1][j-1] 24 | } 25 | } 26 | } 27 | return result 28 | } -------------------------------------------------------------------------------- /102-binary-tree-level-order-traversal/102-binary-tree-level-order-traversal.go: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * type TreeNode struct { 4 | * Val int 5 | * Left *TreeNode 6 | * Right *TreeNode 7 | * } 8 | */ 9 | func levelOrder(root *TreeNode) [][]int { 10 | var ans [][]int 11 | addNodetolevel(root, 0, &ans) 12 | return ans 13 | } 14 | 15 | func addNodetolevel(root *TreeNode, level int, ans *[][]int) { 16 | if root == nil { 17 | return 18 | } 19 | if len(*ans)-1 < level { 20 | var newLevel []int 21 | *ans = append(*ans, newLevel) 22 | } 23 | (*ans)[level] = append((*ans)[level], root.Val) 24 | addNodetolevel(root.Left, level+1, ans) 25 | addNodetolevel(root.Right, level+1, ans) 26 | } -------------------------------------------------------------------------------- /1539-kth-missing-positive-number/1539-kth-missing-positive-number.go: -------------------------------------------------------------------------------- 1 | // Apply a binary search. Since the array is sorted we can find at any given index how many numbers are missing as arr[index] – (index+1) 2 | func findKthPositive(arr []int, k int) int { 3 | low := 0 4 | high := len(arr) - 1 5 | var mid int 6 | for low <= high { 7 | mid = low + (high-low) / 2 8 | missing_number := arr[mid] - (1 + mid) 9 | if missing_number >= k { //A[m]-(m+1) --> This gives umber of missing number before m'th index because m+1 is the actual value that should be there 10 | high = mid - 1 11 | } else { 12 | low = mid + 1 13 | } 14 | } 15 | //return arr[high] + k - (arr[high] - (1 + high)); // or simply low+k 16 | return low+k 17 | } -------------------------------------------------------------------------------- /77-combinations/77-combinations.go: -------------------------------------------------------------------------------- 1 | func combine(n int, k int) [][]int { 2 | var result [][]int 3 | candidates := []int{} 4 | for i := 1; i <= n; i++ { 5 | candidates = append(candidates, i) 6 | } 7 | findCombination([]int{}, k, candidates, &result) 8 | return result 9 | } 10 | 11 | // T: O(n!) 12 | // S: O(n!) 13 | func findCombination(included []int, k int, left []int, result *[][]int) { 14 | if len(included) == k { 15 | ans1 := append([]int{}, included...) 16 | *result = append(*result, ans1) 17 | return 18 | } 19 | for idx, l := range left{ 20 | findCombination( 21 | append(included, l), 22 | k, 23 | append([]int{}, left[idx+1:]...), 24 | result) 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /240-search-a-2d-matrix-ii/240-search-a-2d-matrix-ii.go: -------------------------------------------------------------------------------- 1 | // Time Complexity O(m+n) 2 | // Space Complexity O(1) 3 | func searchMatrix(matrix [][]int, target int) bool { 4 | return RecursiveSolution(matrix, 0, len(matrix[0])-1, target) 5 | } 6 | 7 | // Start from top right element 8 | func RecursiveSolution(matrix [][]int, m, n, target int) bool { 9 | if m >= len(matrix) || m < 0 || n >= len(matrix[0]) || n < 0 { 10 | return false 11 | } 12 | if target == matrix[m][n] { 13 | return true 14 | } 15 | // if target > the current element search left 16 | if target >= matrix[m][n] { 17 | return RecursiveSolution(matrix, m+1, n, target) 18 | } 19 | // if target < the current element search down 20 | return RecursiveSolution(matrix, m, n-1, target) 21 | } 22 | -------------------------------------------------------------------------------- /105-construct-binary-tree-from-preorder-and-inorder-traversal/105-construct-binary-tree-from-preorder-and-inorder-traversal.go: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * type TreeNode struct { 4 | * Val int 5 | * Left *TreeNode 6 | * Right *TreeNode 7 | * } 8 | */ 9 | func buildTree(preorder []int, inorder []int) *TreeNode { 10 | if len(preorder) == 0 { 11 | return nil 12 | } 13 | 14 | index := search(preorder[0], inorder) 15 | node := &TreeNode{ 16 | Val: preorder[0], 17 | Left: buildTree(preorder[1:index+1], inorder[:index]), 18 | Right: buildTree(preorder[index+1:], inorder[index+1:]), 19 | } 20 | 21 | return node 22 | } 23 | 24 | func search(val int, inorder []int) int { 25 | for i := 0; i < len(inorder); i++ { 26 | if val == inorder[i] { 27 | return i 28 | } 29 | } 30 | return 0 31 | } -------------------------------------------------------------------------------- /31-next-permutation/31-next-permutation.go: -------------------------------------------------------------------------------- 1 | func nextPermutation(nums []int) { 2 | n := len(nums) 3 | 4 | localMaximaBreak := -1 5 | // O(n) 6 | for i:= n-2; i>=0; i-- { 7 | if nums[i] < nums[i+1]{ 8 | localMaximaBreak = i 9 | break 10 | } 11 | } 12 | // o(n) 13 | if localMaximaBreak != -1 { 14 | for i:= n-1; i>=localMaximaBreak; i-- { 15 | if nums[i] > nums[localMaximaBreak] { 16 | nums[i], nums[localMaximaBreak] = nums[localMaximaBreak], nums[i] 17 | break 18 | } 19 | } 20 | } 21 | // this is going to take O(n) 22 | for i, j :=(localMaximaBreak+1), n-1; i < j; i, j = i+1, j-1 { 23 | nums[i], nums[j] = nums[j], nums[i] 24 | } 25 | 26 | 27 | //Time complexity o(1) 28 | 29 | } -------------------------------------------------------------------------------- /88-merge-sorted-array/88-merge-sorted-array.go: -------------------------------------------------------------------------------- 1 | func merge(nums1 []int, m int, nums2 []int, n int) { 2 | if (n == 0) { 3 | return 4 | } 5 | 6 | // check for the first element in 2nd sorted array if greater then swap and then we need to sort the second array because after swaaping that would be unsorted 7 | for i := 0; i arr[i] { 22 | arr[i], arr[i-1] = arr[i-1], arr[i] 23 | } 24 | } 25 | } -------------------------------------------------------------------------------- /19-remove-nth-node-from-end-of-list/19-remove-nth-node-from-end-of-list.go: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * type ListNode struct { 4 | * Val int 5 | * Next *ListNode 6 | * } 7 | */ 8 | func removeNthFromEnd(head *ListNode, n int) *ListNode { 9 | if head == nil { 10 | return nil 11 | } 12 | start := &ListNode{Next : head} 13 | slow := start 14 | fast := start 15 | for i := 1; i <= n; i++ { 16 | fast = fast.Next 17 | } 18 | // fast id pointing to nth node 19 | 20 | for fast.Next != nil { 21 | fast = fast.Next 22 | slow = slow.Next 23 | } 24 | // slow is at one position previous from n-1 th node from beginning we need to delete node next to slow 25 | slow.Next = slow.Next.Next 26 | // head is always going to point to first node 27 | return start.Next 28 | } -------------------------------------------------------------------------------- /5-longest-palindromic-substring/README.md: -------------------------------------------------------------------------------- 1 |

5. Longest Palindromic Substring

Medium


Given a string s, return the longest palindromic substring in s.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
Input: s = "babad"
 7 | Output: "bab"
 8 | Explanation: "aba" is also a valid answer.
 9 | 
10 | 11 |

Example 2:

12 | 13 |
Input: s = "cbbd"
14 | Output: "bb"
15 | 
16 | 17 |

 

18 |

Constraints:

19 | 20 |
    21 |
  • 1 <= s.length <= 1000
  • 22 |
  • s consist of only digits and English letters.
  • 23 |
24 |
-------------------------------------------------------------------------------- /128-longest-consecutive-sequence/128-longest-consecutive-sequence.go: -------------------------------------------------------------------------------- 1 | // MakeMyTrip 2 | func longestConsecutive(nums []int) int { 3 | hmap := make(map[int]int, len(nums)) 4 | for _, ele := range nums { 5 | hmap[ele] = 1 6 | } 7 | 8 | longestStreak := 0 9 | for num, _ := range hmap { 10 | if _, ok := hmap[num-1]; ok { 11 | continue 12 | } 13 | currentNum := num 14 | currentStreak := 1 15 | 16 | for ;; { 17 | if hmap[currentNum+1] == 1 { 18 | currentNum = currentNum + 1 19 | currentStreak = currentStreak + 1 20 | }else { 21 | break 22 | } 23 | } 24 | if currentStreak > longestStreak { 25 | longestStreak = currentStreak 26 | } 27 | } 28 | return longestStreak 29 | } -------------------------------------------------------------------------------- /2-add-two-numbers/2-add-two-numbers.go: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * type ListNode struct { 4 | * Val int 5 | * Next *ListNode 6 | * } 7 | */ 8 | func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode { 9 | // set this to zero 10 | var dummy = &ListNode{Val: 0} 11 | var currentAddress = dummy 12 | var sum int 13 | for l1 != nil || l2 != nil { 14 | if l1 != nil { 15 | sum += l1.Val 16 | l1 = l1.Next 17 | } 18 | if l2 != nil { 19 | sum += l2.Val 20 | l2 = l2.Next 21 | } 22 | // add a new node to the list 23 | currentAddress.Next = &ListNode{Val: sum % 10} 24 | currentAddress = currentAddress.Next 25 | if sum > 9 { 26 | sum = 1 // carry forward 27 | } else { 28 | sum = 0 29 | } 30 | } 31 | if sum == 1 { 32 | currentAddress.Next = &ListNode{Val: sum} 33 | } 34 | return dummy.Next 35 | } -------------------------------------------------------------------------------- /74-search-a-2d-matrix/74-search-a-2d-matrix.go: -------------------------------------------------------------------------------- 1 | func searchMatrix(matrix [][]int, target int) bool { 2 | lenRow := len(matrix) 3 | if lenRow == 1 { 4 | return binarySearch(matrix[0], target) 5 | } 6 | 7 | for i := 0; i< lenRow-1;i++ { 8 | if target < matrix[i+1][0] { 9 | return binarySearch(matrix[i], target) 10 | } 11 | } 12 | return binarySearch(matrix[lenRow-1], target) 13 | } 14 | 15 | func binarySearch(arr []int, target int) bool { 16 | low := 0 17 | high := len(arr) -1 18 | for low <= high { 19 | mid := (low+high)/2 20 | 21 | if arr[mid] == target { 22 | return true 23 | } 24 | if target > arr[mid]{ 25 | low= mid + 1 26 | }else if target < arr[mid] { 27 | high = mid-1 28 | } 29 | } 30 | return false 31 | } -------------------------------------------------------------------------------- /61-rotate-list/61-rotate-list.go: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * type ListNode struct { 4 | * Val int 5 | * Next *ListNode 6 | * } 7 | */ 8 | func rotateRight(head *ListNode, k int) *ListNode { 9 | if head == nil ||head.Next == nil || k== 0 { 10 | return head 11 | } 12 | // calculate length 13 | cur := head 14 | count := 1 15 | for cur.Next != nil { 16 | count++ 17 | cur = cur.Next 18 | } 19 | 20 | if k >= count { 21 | k= k % count 22 | } 23 | fmt.Println(k) 24 | // we need to find (length - k)th node from start 25 | k = count - k 26 | 27 | // creates a circular linked list 28 | cur.Next = head 29 | 30 | for ;k>=1;k-- { 31 | cur = cur.Next 32 | } 33 | head = cur.Next 34 | cur.Next = nil 35 | 36 | return head 37 | 38 | } -------------------------------------------------------------------------------- /90-subsets-ii/90-subsets-ii.go: -------------------------------------------------------------------------------- 1 | func subsetsWithDup(nums []int) [][]int { 2 | var res [][]int 3 | var entry []int 4 | sort.Ints(nums) 5 | backtrack(0, nums, entry, &res) 6 | return res 7 | 8 | } 9 | 10 | func backtrack(pos int, candidates []int, entry []int, res *[][]int) { 11 | cpy := make([]int, len(entry)) 12 | copy(cpy, entry) 13 | *res = append(*res, cpy) 14 | 15 | for i := pos; i < len(candidates); i++ { 16 | // the solution set must not contain same list, that's why continue in case of same subtree 17 | if i > pos && candidates[i] == candidates[i-1] { 18 | continue 19 | } 20 | 21 | entry = append(entry, candidates[i]) 22 | // the number should not repeat itself that's why i+1 23 | backtrack(i+1, candidates, entry, res) 24 | entry = (entry)[:len(entry)-1] 25 | } 26 | } -------------------------------------------------------------------------------- /90-subsets-ii/README.md: -------------------------------------------------------------------------------- 1 |

90. Subsets II

Medium


Given an integer array nums that may contain duplicates, return all possible subsets (the power set).

2 | 3 |

The solution set must not contain duplicate subsets. Return the solution in any order.

4 | 5 |

 

6 |

Example 1:

7 |
Input: nums = [1,2,2]
 8 | Output: [[],[1],[1,2],[1,2,2],[2],[2,2]]
 9 | 

Example 2:

10 |
Input: nums = [0]
11 | Output: [[],[0]]
12 | 
13 |

 

14 |

Constraints:

15 | 16 |
    17 |
  • 1 <= nums.length <= 10
  • 18 |
  • -10 <= nums[i] <= 10
  • 19 |
20 |
-------------------------------------------------------------------------------- /17-letter-combinations-of-a-phone-number/17-letter-combinations-of-a-phone-number.go: -------------------------------------------------------------------------------- 1 | var dict = map[byte]string{ 2 | '2': "abc", 3 | '3': "def", 4 | '4': "ghi", 5 | '5': "jkl", 6 | '6': "mno", 7 | '7': "pqrs", 8 | '8': "tuv", 9 | '9': "wxyz", 10 | } 11 | 12 | func letterCombinations(digits string) []string { 13 | var res []string 14 | //for input "" 15 | if digits == "" { 16 | return res 17 | } 18 | backtrack(0, "", digits, &res) 19 | return res 20 | } 21 | 22 | 23 | func backtrack(pos int, entry string, candidates string, res *[]string) { 24 | // fmt.Println("backtrack called with", entry) 25 | if pos == len(candidates) { 26 | *res = append(*res, entry) 27 | return 28 | } 29 | 30 | strs := dict[candidates[pos]] 31 | for i := 0; i < len(strs); i++ { 32 | backtrack(pos+1, entry+string(strs[i]), candidates, res) 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /33-search-in-rotated-sorted-array/33-search-in-rotated-sorted-array.go: -------------------------------------------------------------------------------- 1 | func search(nums []int, target int) int { 2 | if len(nums) == 0 { 3 | return -1 4 | } 5 | left := 0 6 | right := len(nums) - 1 7 | //binary seacrh 8 | for left <= right { 9 | //find mid 10 | mid := (left + right) / 2 11 | if nums[mid] == target { 12 | return mid 13 | } 14 | // if left array is sorted 15 | if nums[left] <= nums[mid] { 16 | // if target lies between left array 17 | if nums[left] <= target && target <= nums[mid] { 18 | right = mid - 1 19 | } else { 20 | left = mid + 1 21 | } 22 | } else { // else right array must be sorted 23 | // if target lies between right array 24 | if nums[mid] <= target && target <= nums[right] { 25 | left = mid + 1 26 | } else { 27 | right = mid - 1 28 | } 29 | } 30 | } 31 | 32 | return -1 33 | } -------------------------------------------------------------------------------- /875-koko-eating-bananas/875-koko-eating-bananas.go: -------------------------------------------------------------------------------- 1 | func minEatingSpeed(piles []int, h int) int { 2 | sort.Ints(piles) 3 | 4 | left := 1 5 | right := piles[len(piles)-1] 6 | 7 | for left < right { 8 | mid := left + (right - left)/2 9 | 10 | // calculate time spent on piles 11 | timeSpent := 0 12 | for _, pile := range piles { 13 | timeSpent += (pile/mid) // find the ceiling 14 | if pile % mid != 0 { 15 | timeSpent++ 16 | } 17 | } 18 | 19 | if timeSpent <= h { //we found the solution but we need to find the minimum 20 | right = mid 21 | } else { 22 | left = mid + 1 23 | } 24 | } 25 | 26 | return right // right is workable solution 27 | } 28 | 29 | 30 | // TimeComplexity =naive approach -- max(pile)*n 31 | // TimeComplexity = log(max(pile)) -------------------------------------------------------------------------------- /153-find-minimum-in-rotated-sorted-array/153-find-minimum-in-rotated-sorted-array.go: -------------------------------------------------------------------------------- 1 | func findMin(nums []int) int { 2 | if len(nums) == 0 { 3 | return -1 4 | } 5 | left := 0 6 | right := len(nums) - 1 7 | min := math.MaxInt 8 | //binary seacrh 9 | for left <= right { 10 | //find mid 11 | mid := (left + right) / 2 12 | if nums[left] <= nums[mid] && nums[mid] <= nums[right] { 13 | min = Min(min, nums[left]) 14 | } 15 | // if left array is sorted 16 | if nums[left] <= nums[mid] { 17 | min = Min(min, nums[left]) 18 | left = mid + 1 19 | } else if nums[left] >= nums[mid] { // right array sorted 20 | min = Min(min, nums[mid]) 21 | right = mid - 1 22 | } 23 | } 24 | 25 | return min 26 | } 27 | 28 | func Min(x, y int) int { 29 | if x > y { 30 | return y 31 | } 32 | return x 33 | } -------------------------------------------------------------------------------- /20-valid-parentheses/20-valid-parentheses.go: -------------------------------------------------------------------------------- 1 | type ByteStack struct { 2 | values []byte 3 | } 4 | 5 | func (s *ByteStack) push(b byte) { 6 | s.values = append(s.values, b) 7 | } 8 | 9 | func (s *ByteStack) pop() byte { 10 | if len(s.values) == 0 { 11 | return 0 12 | } 13 | ret := s.values[len(s.values)-1] 14 | s.values = s.values[:len(s.values)-1] 15 | return ret 16 | } 17 | 18 | func isValid(s string) bool { 19 | stack := &ByteStack{} 20 | for i :=0; i < len(s); i++ { 21 | if s[i] == '(' || s[i] == '{' || s[i] == '[' { 22 | stack.push(s[i]) 23 | continue 24 | } 25 | switch s[i] { 26 | case '(', '{', '[': stack.push(s[i]) 27 | case ')': if stack.pop() != '(' {return false} 28 | case '}': if stack.pop() != '{' {return false} 29 | case ']': if stack.pop() != '[' {return false} 30 | } 31 | } 32 | return stack.pop() == 0 33 | } 34 | -------------------------------------------------------------------------------- /34-find-first-and-last-position-of-element-in-sorted-array/34-find-first-and-last-position-of-element-in-sorted-array.go: -------------------------------------------------------------------------------- 1 | func searchRange(nums []int, target int) []int { 2 | res := []int{-1,-1} 3 | // find last index 4 | for left, right := 0, len(nums) - 1; left <= right ;{ 5 | mid := left + (right - left) / 2 6 | if nums[mid] == target { 7 | res[1] = mid 8 | } 9 | if nums[mid] > target { 10 | right = mid - 1 11 | } else { 12 | left = mid + 1 13 | } 14 | } 15 | // find first index 16 | for left, right := 0, res[1]; left <= right; { 17 | mid := left + (right - left) / 2 18 | if nums[mid] == target{ 19 | res[0] = mid 20 | } 21 | if nums[mid] < target { 22 | left = mid + 1 23 | } else { 24 | right = mid - 1 25 | } 26 | } 27 | return res 28 | } -------------------------------------------------------------------------------- /48-rotate-image/NOTES.md: -------------------------------------------------------------------------------- 1 | ## Solution 1:Brute force 2 | ​ 3 | * Approach: Take another dummy matrix of n*n, and then take the first row of the matrix and put it in the last column of the dummy matrix, take the second row of the matrix, and put it in the second last column of the matrix and so. 4 | ​ 5 | Space Complexity - O(N*N) 6 | Time Complexity - O(N*N) 7 | ​ 8 | `rotated[j][n - i - 1] = matrix[i][j]` 9 | ​ 10 | ## Solution 2: Optimized approach 11 | ​ 12 | * Intuition: By observation, we see that the first column of the original matrix is the reverse of the first row of the rotated matrix, so that’s why we transpose the matrix and then reverse each row, and since we are making changes in the matrix itself space complexity gets reduced to O(1). 13 | ​ 14 | * Approach: / 15 | ​ 16 | **Step1:** Transpose of the matrix. (transposing means changing columns to rows and rows to columns) / 17 | ​ 18 | **Step2:** Reverse each row of the matrix. 19 | ​ -------------------------------------------------------------------------------- /206-reverse-linked-list/206-reverse-linked-list.go: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * type ListNode struct { 4 | * Val int 5 | * Next *ListNode 6 | * } 7 | */ 8 | // func reverseList(head *ListNode) *ListNode { 9 | // if head == nil { 10 | // return nil 11 | // } 12 | // var prev *ListNode // this needs to be nil 13 | // curr := head 14 | // for curr != nil { 15 | // // Store next 16 | // next := curr.Next 17 | // curr.Next = prev 18 | // prev = curr 19 | // curr = next 20 | // } 21 | // head = prev 22 | // return head 23 | // } 24 | 25 | //Using reverseLst 26 | func reverseList(head *ListNode) *ListNode { 27 | if head == nil || head.Next == nil{ 28 | return head 29 | } 30 | 31 | newHead := reverseList(head.Next) 32 | tmp := head.Next 33 | tmp.Next = head 34 | head.Next = nil 35 | return newHead 36 | } 37 | -------------------------------------------------------------------------------- /637-average-of-levels-in-binary-tree/637-average-of-levels-in-binary-tree.go: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * type TreeNode struct { 4 | * Val int 5 | * Left *TreeNode 6 | * Right *TreeNode 7 | * } 8 | */ 9 | func averageOfLevels(root *TreeNode) []float64 { 10 | result := []float64{} 11 | if root == nil{ 12 | return []float64{} 13 | } 14 | queue := []*TreeNode{root} 15 | for len(queue) > 0{ 16 | temp := []*TreeNode{} 17 | sum := 0 18 | for _, node := range queue{ 19 | sum += node.Val 20 | if node.Left != nil{ 21 | temp = append(temp, node.Left) 22 | } 23 | if node.Right != nil{ 24 | temp = append(temp, node.Right) 25 | } 26 | 27 | } 28 | result = append(result,float64(sum)/float64(len(queue))) 29 | queue = temp 30 | } 31 | return result 32 | } -------------------------------------------------------------------------------- /77-combinations/README.md: -------------------------------------------------------------------------------- 1 |

77. Combinations

Medium


Given two integers n and k, return all possible combinations of k numbers out of the range [1, n].

2 | 3 |

You may return the answer in any order.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: n = 4, k = 2
 9 | Output:
10 | [
11 |   [2,4],
12 |   [3,4],
13 |   [2,3],
14 |   [1,2],
15 |   [1,3],
16 |   [1,4],
17 | ]
18 | 
19 | 20 |

Example 2:

21 | 22 |
Input: n = 1, k = 1
23 | Output: [[1]]
24 | 
25 | 26 |

 

27 |

Constraints:

28 | 29 |
    30 |
  • 1 <= n <= 20
  • 31 |
  • 1 <= k <= n
  • 32 |
33 |
-------------------------------------------------------------------------------- /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 |
-------------------------------------------------------------------------------- /695-max-area-of-island/695-max-area-of-island.go: -------------------------------------------------------------------------------- 1 | func maxAreaOfIsland(grid [][]int) int { 2 | R := len(grid) 3 | C := len(grid[0]) 4 | max := 0 5 | for i:=0; i< R; i++ { 6 | for j:=0; j= R || j >=C || i < 0 || j < 0 { 18 | return 0 19 | } 20 | if grid[i][j] == 0 || grid[i][j] == 2{ 21 | return 0 22 | } 23 | // mark the grid visited 24 | grid[i][j] = 2 25 | return 1+traverse(i+1, j, R, C, grid)+traverse(i, j+1, R, C, grid)+traverse(i-1, j, R, C, grid)+traverse(i, j-1, R, C, grid) 26 | } 27 | 28 | func maximum(x, y int) int { 29 | if x > y { 30 | return x 31 | } 32 | return y 33 | } -------------------------------------------------------------------------------- /9-palindrome-number/9-palindrome-number.go: -------------------------------------------------------------------------------- 1 | func isPalindrome(x int) bool { 2 | // Special cases: 3 | // When x < 0, x is not a palindrome. 4 | // Also if the last digit of the number is 0, in order to be a palindrome, 5 | // the first digit of the number also needs to be 0. 6 | // Only 0 satisfy this property. 7 | if(x < 0 || (x % 10 == 0 && x != 0)) { 8 | return false 9 | } 10 | revertedNumber := 0 11 | for x > revertedNumber { 12 | revertedNumber = revertedNumber * 10 + x % 10 13 | x = x/10 14 | } 15 | // When the length is an odd number, we can get rid of the middle digit by revertedNumber/10 16 | // For example when the input is 12321, at the end of the while loop we get x = 12, revertedNumber = 123, 17 | // since the middle digit doesn't matter in palidrome(it will always equal to itself), we can simply get rid of it. 18 | return (x == revertedNumber || x == revertedNumber/10) 19 | } -------------------------------------------------------------------------------- /890-find-and-replace-pattern/890-find-and-replace-pattern.go: -------------------------------------------------------------------------------- 1 | // Encoding Decoding, Two map Problem 2 | func findAndReplacePattern(words []string, pattern string) []string { 3 | result := []string{} 4 | 5 | for _, word := range words { 6 | if check(word, pattern) { 7 | result = append(result, word) 8 | } 9 | } 10 | 11 | return result 12 | } 13 | 14 | func check(word string, pattern string) bool { 15 | encode, decode := map[byte]byte{}, map[byte]byte{} 16 | for i := range word { 17 | if c, found := encode[word[i]]; found { 18 | if pattern[i] != c { 19 | return false 20 | } 21 | 22 | continue 23 | } 24 | 25 | if _, found := decode[pattern[i]]; found { 26 | return false 27 | } 28 | 29 | decode[pattern[i]] = word[i] 30 | encode[word[i]] = pattern[i] 31 | } 32 | return true 33 | } -------------------------------------------------------------------------------- /131-palindrome-partitioning/README.md: -------------------------------------------------------------------------------- 1 |

131. Palindrome Partitioning

Medium


Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s.

2 | 3 |

A palindrome string is a string that reads the same backward as forward.

4 | 5 |

 

6 |

Example 1:

7 |
Input: s = "aab"
 8 | Output: [["a","a","b"],["aa","b"]]
 9 | 

Example 2:

10 |
Input: s = "a"
11 | Output: [["a"]]
12 | 
13 |

 

14 |

Constraints:

15 | 16 |
    17 |
  • 1 <= s.length <= 16
  • 18 |
  • s contains only lowercase English letters.
  • 19 |
20 |
-------------------------------------------------------------------------------- /160-intersection-of-two-linked-lists/160-intersection-of-two-linked-lists.go: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * type ListNode struct { 4 | * Val int 5 | * Next *ListNode 6 | * } 7 | */ 8 | func getIntersectionNode(headA, headB *ListNode) *ListNode { 9 | curA := headA 10 | curB := headB 11 | // go till one of the poiter is nil, this will have the bigger list having extra node 12 | for curA != nil && curB != nil { 13 | curA = curA.Next 14 | curB = curB.Next 15 | } 16 | // if curA is bigger then traverse that much so that curA is nil 17 | for curA != nil { 18 | headA = headA.Next 19 | curA = curA.Next 20 | } 21 | // if curB is bigger then traverse that much so that curB is nil 22 | for curB != nil { 23 | headB = headB.Next 24 | curB = curB.Next 25 | } 26 | // now headA and headB have equal number of element 27 | for headA != headB { 28 | headA = headA.Next 29 | headB = headB.Next 30 | } 31 | 32 | return headA 33 | } -------------------------------------------------------------------------------- /387-first-unique-character-in-a-string/README.md: -------------------------------------------------------------------------------- 1 |

387. First Unique Character in a String

Easy


Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1.

2 | 3 |

 

4 |

Example 1:

5 |
Input: s = "leetcode"
 6 | Output: 0
 7 | 

Example 2:

8 |
Input: s = "loveleetcode"
 9 | Output: 2
10 | 

Example 3:

11 |
Input: s = "aabb"
12 | Output: -1
13 | 
14 |

 

15 |

Constraints:

16 | 17 |
    18 |
  • 1 <= s.length <= 105
  • 19 |
  • s consists of only lowercase English letters.
  • 20 |
21 |
-------------------------------------------------------------------------------- /1283-find-the-smallest-divisor-given-a-threshold/1283-find-the-smallest-divisor-given-a-threshold.go: -------------------------------------------------------------------------------- 1 | // max divisor is max of numbers, min divisor is 1 2 | func smallestDivisor(nums []int, threshold int) int { 3 | sort.Ints(nums) 4 | 5 | left := 1 6 | right := nums[len(nums)-1] 7 | 8 | for left < right { 9 | mid := left + (right - left)/2 10 | 11 | // calculate the result 12 | result := 0 13 | for _, num := range nums { 14 | result += (num/mid) // find the ceiling 15 | if num % mid != 0 { 16 | result++ 17 | } 18 | } 19 | 20 | if result <= threshold { //we found the solution but we need to find the minimum 21 | right = mid 22 | } else { 23 | left = mid + 1 24 | } 25 | } 26 | 27 | return right // right is workable solution 28 | } 29 | 30 | // TimeComplexity =naive approach -- max(nums)*n 31 | // TimeComplexity = log(max(nums)) -------------------------------------------------------------------------------- /217-contains-duplicate/README.md: -------------------------------------------------------------------------------- 1 |

217. Contains Duplicate

Easy


Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.

2 | 3 |

 

4 |

Example 1:

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

Example 2:

8 |
Input: nums = [1,2,3,4]
 9 | Output: false
10 | 

Example 3:

11 |
Input: nums = [1,1,1,3,3,4,3,2,4,2]
12 | Output: true
13 | 
14 |

 

15 |

Constraints:

16 | 17 |
    18 |
  • 1 <= nums.length <= 105
  • 19 |
  • -109 <= nums[i] <= 109
  • 20 |
21 |
-------------------------------------------------------------------------------- /78-subsets/README.md: -------------------------------------------------------------------------------- 1 |

78. Subsets

Medium


Given an integer array nums of unique elements, return all possible subsets (the power set).

2 | 3 |

The solution set must not contain duplicate subsets. Return the solution in any order.

4 | 5 |

 

6 |

Example 1:

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

Example 2:

13 | 14 |
Input: nums = [0]
15 | Output: [[],[0]]
16 | 
17 | 18 |

 

19 |

Constraints:

20 | 21 |
    22 |
  • 1 <= nums.length <= 10
  • 23 |
  • -10 <= nums[i] <= 10
  • 24 |
  • All the numbers of nums are unique.
  • 25 |
26 |
-------------------------------------------------------------------------------- /46-permutations/README.md: -------------------------------------------------------------------------------- 1 |

46. Permutations

Medium


Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order.

2 | 3 |

 

4 |

Example 1:

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

Example 2:

8 |
Input: nums = [0,1]
 9 | Output: [[0,1],[1,0]]
10 | 

Example 3:

11 |
Input: nums = [1]
12 | Output: [[1]]
13 | 
14 |

 

15 |

Constraints:

16 | 17 |
    18 |
  • 1 <= nums.length <= 6
  • 19 |
  • -10 <= nums[i] <= 10
  • 20 |
  • All the integers of nums are unique.
  • 21 |
22 |
-------------------------------------------------------------------------------- /540-single-element-in-a-sorted-array/README.md: -------------------------------------------------------------------------------- 1 |

540. Single Element in a Sorted Array

Medium


You are given a sorted array consisting of only integers where every element appears exactly twice, except for one element which appears exactly once.

2 | 3 |

Return the single element that appears only once.

4 | 5 |

Your solution must run in O(log n) time and O(1) space.

6 | 7 |

 

8 |

Example 1:

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

Example 2:

12 |
Input: nums = [3,3,7,7,10,11,11]
13 | Output: 10
14 | 
15 |

 

16 |

Constraints:

17 | 18 |
    19 |
  • 1 <= nums.length <= 105
  • 20 |
  • 0 <= nums[i] <= 105
  • 21 |
22 |
-------------------------------------------------------------------------------- /14-longest-common-prefix/README.md: -------------------------------------------------------------------------------- 1 |

14. Longest Common Prefix

Easy


Write a function to find the longest common prefix string amongst an array of strings.

2 | 3 |

If there is no common prefix, return an empty string "".

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: strs = ["flower","flow","flight"]
 9 | Output: "fl"
10 | 
11 | 12 |

Example 2:

13 | 14 |
Input: strs = ["dog","racecar","car"]
15 | Output: ""
16 | Explanation: There is no common prefix among the input strings.
17 | 
18 | 19 |

 

20 |

Constraints:

21 | 22 |
    23 |
  • 1 <= strs.length <= 200
  • 24 |
  • 0 <= strs[i].length <= 200
  • 25 |
  • strs[i] consists of only lowercase English letters.
  • 26 |
27 |
-------------------------------------------------------------------------------- /101-symmetric-tree/101-symmetric-tree.go: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * type TreeNode struct { 4 | * Val int 5 | * Left *TreeNode 6 | * Right *TreeNode 7 | * } 8 | */ 9 | func isSymmetric(root *TreeNode) bool { 10 | if root == nil || IsSymmetricTree(root.Left,root.Right) { 11 | return true 12 | } 13 | return false 14 | } 15 | // For two trees to be mirror images, the following 16 | // three conditions must be true 17 | // 1.) Their root node's key must be same 18 | // 2.) left subtree of left tree and right subtree of 19 | // right tree have to be mirror images 20 | // 3.) right subtree of left tree and left subtree of 21 | // right tree have to be mirror images 22 | func IsSymmetricTree(p *TreeNode, q *TreeNode) bool { 23 | if p== nil && q == nil{ 24 | return true 25 | } 26 | if p!= nil && q!= nil && p.Val== q.Val && IsSymmetricTree(p.Left, q.Right) && IsSymmetricTree(p.Right,q.Left) { 27 | return true 28 | } 29 | return false 30 | } -------------------------------------------------------------------------------- /70-climbing-stairs/README.md: -------------------------------------------------------------------------------- 1 |

70. Climbing Stairs

Easy


You are climbing a staircase. It takes n steps to reach the top.

2 | 3 |

Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: n = 2
 9 | Output: 2
10 | Explanation: There are two ways to climb to the top.
11 | 1. 1 step + 1 step
12 | 2. 2 steps
13 | 
14 | 15 |

Example 2:

16 | 17 |
Input: n = 3
18 | Output: 3
19 | Explanation: There are three ways to climb to the top.
20 | 1. 1 step + 1 step + 1 step
21 | 2. 1 step + 2 steps
22 | 3. 2 steps + 1 step
23 | 
24 | 25 |

 

26 |

Constraints:

27 | 28 |
    29 |
  • 1 <= n <= 45
  • 30 |
31 |
-------------------------------------------------------------------------------- /15-3sum/README.md: -------------------------------------------------------------------------------- 1 |

15. 3Sum

Medium


Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.

2 | 3 |

Notice that the solution set must not contain duplicate triplets.

4 | 5 |

 

6 |

Example 1:

7 |
Input: nums = [-1,0,1,2,-1,-4]
 8 | Output: [[-1,-1,2],[-1,0,1]]
 9 | 

Example 2:

10 |
Input: nums = []
11 | Output: []
12 | 

Example 3:

13 |
Input: nums = [0]
14 | Output: []
15 | 
16 |

 

17 |

Constraints:

18 | 19 |
    20 |
  • 0 <= nums.length <= 3000
  • 21 |
  • -105 <= nums[i] <= 105
  • 22 |
23 |
-------------------------------------------------------------------------------- /2013-detect-squares/2013-detect-squares.go: -------------------------------------------------------------------------------- 1 | type DetectSquares struct { 2 | Counts map[[2]int]int 3 | Points [][]int 4 | } 5 | 6 | func Constructor() DetectSquares { 7 | return DetectSquares{ 8 | Counts: map[[2]int]int{}, 9 | Points: [][]int{}, 10 | } 11 | } 12 | 13 | func (this *DetectSquares) Add(point []int) { 14 | key:=[2]int{point[0],point[1]} 15 | this.Counts[key]+=1 16 | this.Points=append(this.Points,point) 17 | } 18 | 19 | func (this *DetectSquares) Count(point []int) int { 20 | res:=0 21 | px,py:=point[0],point[1] 22 | for _,p := range this.Points{ 23 | if math.Abs(float64(px-p[0]))!=math.Abs(float64(py-p[1])) || px==p[0] || py==p[1] { 24 | continue 25 | } 26 | res+=this.Counts[[2]int{px,p[1]}]*this.Counts[[2]int{p[0],py}] 27 | } 28 | return res 29 | } 30 | 31 | 32 | /** 33 | * Your DetectSquares object will be instantiated and called as such: 34 | * obj := Constructor(); 35 | * obj.Add(point); 36 | * param_2 := obj.Count(point); 37 | */ -------------------------------------------------------------------------------- /680-valid-palindrome-ii/README.md: -------------------------------------------------------------------------------- 1 |

680. Valid Palindrome II

Easy


Given a string s, return true if the s can be palindrome after deleting at most one character from it.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
Input: s = "aba"
 7 | Output: true
 8 | 
9 | 10 |

Example 2:

11 | 12 |
Input: s = "abca"
13 | Output: true
14 | Explanation: You could delete the character 'c'.
15 | 
16 | 17 |

Example 3:

18 | 19 |
Input: s = "abc"
20 | Output: false
21 | 
22 | 23 |

 

24 |

Constraints:

25 | 26 |
    27 |
  • 1 <= s.length <= 105
  • 28 |
  • s consists of lowercase English letters.
  • 29 |
30 |
-------------------------------------------------------------------------------- /81-search-in-rotated-sorted-array-ii/81-search-in-rotated-sorted-array-ii.go: -------------------------------------------------------------------------------- 1 | func search(nums []int, target int) bool { 2 | if len(nums) == 0 { 3 | return false 4 | } 5 | left := 0 6 | right := len(nums) - 1 7 | //binary seacrh 8 | for left <= right { 9 | //find mid 10 | mid := (left + right) / 2 11 | if nums[mid] == target { 12 | return true 13 | } 14 | // if left array is sorted 15 | if nums[left] < nums[mid] { 16 | // if target lies between left array 17 | if nums[left] <= target && target <= nums[mid] { 18 | right = mid - 1 19 | } else { 20 | left = mid + 1 21 | } 22 | } else if nums[left] > nums[mid] { // check for right array sorted 23 | // if target lies between right array 24 | if nums[mid] <= target && target <= nums[right] { 25 | left = mid + 1 26 | } else { 27 | right = mid - 1 28 | } 29 | } else { 30 | left++ // not sure of the status of error but we are sure that left is equal to mid 31 | } 32 | } 33 | 34 | return false 35 | } -------------------------------------------------------------------------------- /92-reverse-linked-list-ii/92-reverse-linked-list-ii.go: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * type ListNode struct { 4 | * Val int 5 | * Next *ListNode 6 | * } 7 | */ 8 | func reverseBetween(head *ListNode, left int, right int) *ListNode { 9 | if head == nil || left==right { 10 | return head 11 | } 12 | 13 | // 1 -> 2 -> 3 -> 4 -> 5 -> 6 14 | dummy := &ListNode{ 15 | Val : -1, 16 | } 17 | // to handle cases if left and riht are first and last node 18 | dummy.Next = head 19 | prev := dummy 20 | for i:=1; i <= left-1; i++ { 21 | prev = prev.Next 22 | } 23 | // 1 -> 2 -> 3 -> 4 -> 5 -> 6 24 | // head,prev pointing at 1 25 | 26 | tail := prev.Next 27 | // 1 -> 2 -> 3 -> 4 -> 5 -> 6 28 | // head,prev pointing at 1 29 | // tail pointing at 2 30 | 31 | for i:= 1; i <= right-left; i++ { 32 | tmp := prev.Next 33 | prev.Next = tail.Next 34 | tail.Next = tail.Next.Next 35 | prev.Next.Next = tmp 36 | } 37 | 38 | return dummy.Next 39 | } -------------------------------------------------------------------------------- /705-design-hashset/705-design-hashset.go: -------------------------------------------------------------------------------- 1 | type MyHashSet struct { 2 | Set [10000][]int 3 | Size int 4 | } 5 | 6 | 7 | func Constructor() MyHashSet { 8 | var hashset MyHashSet 9 | hashset.Size = 10000 10 | return hashset 11 | } 12 | 13 | 14 | func (this *MyHashSet) Add(key int) { 15 | hash := key % this.Size 16 | this.Set[hash] = append(this.Set[hash], key) 17 | } 18 | 19 | 20 | func (this *MyHashSet) Remove(key int) { 21 | hash := key % this.Size 22 | for idx, ele := range this.Set[hash] { 23 | if ele == key { 24 | this.Set[hash][idx] = -1 25 | } 26 | } 27 | } 28 | 29 | func (this *MyHashSet) Contains(key int) bool { 30 | hash := key % this.Size 31 | for _, ele := range this.Set[hash] { 32 | if ele == key { 33 | return true 34 | } 35 | } 36 | return false 37 | } 38 | 39 | /** 40 | * Your MyHashSet object will be instantiated and called as such: 41 | * obj := Constructor(); 42 | * obj.Add(key); 43 | * obj.Remove(key); 44 | * param_3 := obj.Contains(key); 45 | */ -------------------------------------------------------------------------------- /344-reverse-string/README.md: -------------------------------------------------------------------------------- 1 |

344. Reverse String

Easy


Write a function that reverses a string. The input string is given as an array of characters s.

2 | 3 |

You must do this by modifying the input array in-place with O(1) extra memory.

4 | 5 |

 

6 |

Example 1:

7 |
Input: s = ["h","e","l","l","o"]
 8 | Output: ["o","l","l","e","h"]
 9 | 

Example 2:

10 |
Input: s = ["H","a","n","n","a","h"]
11 | Output: ["h","a","n","n","a","H"]
12 | 
13 |

 

14 |

Constraints:

15 | 16 | 20 |
-------------------------------------------------------------------------------- /169-majority-element/README.md: -------------------------------------------------------------------------------- 1 |

169. Majority Element

Easy


Given an array nums of size n, return the majority element.

2 | 3 |

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.

4 | 5 |

 

6 |

Example 1:

7 |
Input: nums = [3,2,3]
 8 | Output: 3
 9 | 

Example 2:

10 |
Input: nums = [2,2,1,1,1,2,2]
11 | Output: 2
12 | 
13 |

 

14 |

Constraints:

15 | 16 |
    17 |
  • n == nums.length
  • 18 |
  • 1 <= n <= 5 * 104
  • 19 |
  • -109 <= nums[i] <= 109
  • 20 |
21 | 22 |

 

23 | Follow-up: Could you solve the problem in linear time and in O(1) space?
-------------------------------------------------------------------------------- /234-palindrome-linked-list/234-palindrome-linked-list.go: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * type ListNode struct { 4 | * Val int 5 | * Next *ListNode 6 | * } 7 | */ 8 | func isPalindrome(head *ListNode) bool { 9 | fast := head 10 | slow := head 11 | for fast.Next != nil && fast.Next.Next != nil { 12 | fast = fast.Next.Next 13 | slow = slow.Next 14 | } 15 | // slow is the middle node 16 | // reverse iteratively from next of middle node 17 | 18 | var prev *ListNode 19 | curr := slow.Next 20 | next := &ListNode{} 21 | for curr != nil { 22 | next = curr.Next 23 | curr.Next = prev 24 | prev = curr 25 | curr = next 26 | } 27 | lasthead := prev 28 | // situation after reverse would be two seperate linkedlist 29 | // reset 30 | fast = head 31 | for lasthead != nil { 32 | if fast.Val != lasthead.Val { 33 | return false 34 | } 35 | lasthead = lasthead.Next 36 | fast = fast.Next 37 | } 38 | return true 39 | 40 | } -------------------------------------------------------------------------------- /61-rotate-list/README.md: -------------------------------------------------------------------------------- 1 |

61. Rotate List

Medium


Given the head of a linked list, rotate the list to the right by k places.

2 | 3 |

 

4 |

Example 1:

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

Example 2:

11 | 12 |
Input: head = [0,1,2], k = 4
13 | Output: [2,0,1]
14 | 
15 | 16 |

 

17 |

Constraints:

18 | 19 |
    20 |
  • The number of nodes in the list is in the range [0, 500].
  • 21 |
  • -100 <= Node.val <= 100
  • 22 |
  • 0 <= k <= 2 * 109
  • 23 |
24 |
-------------------------------------------------------------------------------- /128-longest-consecutive-sequence/README.md: -------------------------------------------------------------------------------- 1 |

128. Longest Consecutive Sequence

Medium


Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence.

2 | 3 |

You must write an algorithm that runs in O(n) time.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: nums = [100,4,200,1,3,2]
 9 | Output: 4
10 | Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4.
11 | 
12 | 13 |

Example 2:

14 | 15 |
Input: nums = [0,3,7,2,5,8,4,6,0,1]
16 | Output: 9
17 | 
18 | 19 |

 

20 |

Constraints:

21 | 22 |
    23 |
  • 0 <= nums.length <= 105
  • 24 |
  • -109 <= nums[i] <= 109
  • 25 |
26 |
-------------------------------------------------------------------------------- /229-majority-element-ii/README.md: -------------------------------------------------------------------------------- 1 |

229. Majority Element II

Medium


Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
Input: nums = [3,2,3]
 7 | Output: [3]
 8 | 
9 | 10 |

Example 2:

11 | 12 |
Input: nums = [1]
13 | Output: [1]
14 | 
15 | 16 |

Example 3:

17 | 18 |
Input: nums = [1,2]
19 | Output: [1,2]
20 | 
21 | 22 |

 

23 |

Constraints:

24 | 25 |
    26 |
  • 1 <= nums.length <= 5 * 104
  • 27 |
  • -109 <= nums[i] <= 109
  • 28 |
29 | 30 |

 

31 |

Follow up: Could you solve the problem in linear time and in O(1) space?

32 |
-------------------------------------------------------------------------------- /104-maximum-depth-of-binary-tree/README.md: -------------------------------------------------------------------------------- 1 |

104. Maximum Depth of Binary Tree

Easy


Given the root of a binary tree, return its maximum depth.

2 | 3 |

A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: root = [3,9,20,null,null,15,7]
 9 | Output: 3
10 | 
11 | 12 |

Example 2:

13 | 14 |
Input: root = [1,null,2]
15 | Output: 2
16 | 
17 | 18 |

 

19 |

Constraints:

20 | 21 |
    22 |
  • The number of nodes in the tree is in the range [0, 104].
  • 23 |
  • -100 <= Node.val <= 100
  • 24 |
25 |
-------------------------------------------------------------------------------- /461-hamming-distance/README.md: -------------------------------------------------------------------------------- 1 |

461. Hamming Distance

Easy


The Hamming distance between two integers is the number of positions at which the corresponding bits are different.

2 | 3 |

Given two integers x and y, return the Hamming distance between them.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: x = 1, y = 4
 9 | Output: 2
10 | Explanation:
11 | 1   (0 0 0 1)
12 | 4   (0 1 0 0)
13 |        ↑   ↑
14 | The above arrows point to positions where the corresponding bits are different.
15 | 
16 | 17 |

Example 2:

18 | 19 |
Input: x = 3, y = 1
20 | Output: 1
21 | 
22 | 23 |

 

24 |

Constraints:

25 | 26 |
    27 |
  • 0 <= x, y <= 231 - 1
  • 28 |
29 |
-------------------------------------------------------------------------------- /106-construct-binary-tree-from-inorder-and-postorder-traversal/106-construct-binary-tree-from-inorder-and-postorder-traversal.go: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * type TreeNode struct { 4 | * Val int 5 | * Left *TreeNode 6 | * Right *TreeNode 7 | * } 8 | */ 9 | 10 | /** 11 | * Example - 12 | * 13 | * inorder [4, 8, 2, 5, 1, 6, 3, 7] 14 | * postorder [8, 4, 5, 2, 6, 7, 3, 1] 15 | * 16 | * initially index is 4 17 | */ 18 | func buildTree(inorder []int, postorder []int) *TreeNode { 19 | if len(postorder) == 0 { 20 | return nil 21 | } 22 | index := searchElement(postorder[len(postorder)-1], inorder) 23 | node := &TreeNode{ 24 | Val: postorder[len(postorder)-1], 25 | Left: buildTree(inorder[:index], postorder[:index]), 26 | Right: buildTree(inorder[index+1:], postorder[index:len(postorder)-1]), 27 | } 28 | return node 29 | } 30 | 31 | func searchElement(val int, inorder []int) int { 32 | for i, elem := range inorder { 33 | if elem == val { 34 | return i 35 | } 36 | } 37 | return 0 38 | } 39 | -------------------------------------------------------------------------------- /131-palindrome-partitioning/131-palindrome-partitioning.go: -------------------------------------------------------------------------------- 1 | func partition(s string) [][]string { 2 | result := [][]string{} 3 | current := []string{} 4 | backtrack(0, s, ¤t, &result) 5 | return result 6 | } 7 | 8 | func backtrack(pos int, s string, current *[]string, result *[][]string) { 9 | if pos >= len(s) { 10 | *result = append(*result, append([]string{}, *current...)) 11 | } 12 | // Candidate substring is [begin, end), includes begin, excludes end. 13 | for end := pos ; end < len(s); end++ { 14 | // You can save result for each substring in a dp[n][n] to 15 | // avoid repeating check in the future. 16 | if isPalindrome(s[pos:end+1]) { 17 | *current = append(*current, s[pos:end+1]) 18 | backtrack(end+1, s, current, result) 19 | *current = (*current)[:len(*current)-1] 20 | } 21 | } 22 | 23 | } 24 | 25 | 26 | func isPalindrome(str string) bool { 27 | for i, j := 0, len(str)-1; i < j; i, j = i+1, j-1 { 28 | if str[i] != str[j] { 29 | return false 30 | } 31 | } 32 | return true 33 | } -------------------------------------------------------------------------------- /4-median-of-two-sorted-arrays/4-median-of-two-sorted-arrays.go: -------------------------------------------------------------------------------- 1 | func findMedianSortedArrays(nums1 []int, nums2 []int) float64 { 2 | return usingTwoPointer(nums1, nums2) 3 | } 4 | 5 | func usingTwoPointer(nums1 []int, nums2 []int) float64 { 6 | // initialize two pointer 7 | p1 := 0 8 | p2 := 0 9 | var ans []int 10 | for i := 0; p1 < len(nums1) && p2 < len(nums2); i++ { 11 | if nums1[p1] <= nums2[p2] { 12 | ans = append(ans, nums1[p1]) 13 | p1++ 14 | } else { 15 | ans = append(ans, nums2[p2]) 16 | p2++ 17 | } 18 | } 19 | if p1 == len(nums1) { 20 | ans = append(ans, nums2[p2:]...) 21 | } else if p2 == len(nums2){ 22 | ans = append(ans, nums1[p1:]...) 23 | } 24 | fmt.Println(ans) 25 | 26 | // find median 27 | totalLength := len(ans) 28 | var median float64 29 | if totalLength%2 == 0 { 30 | median = float64((ans[(totalLength-1)/2]) + (ans[(totalLength-1)/2 + 1]))/2 31 | } else { 32 | median = float64(ans[(totalLength-1)/2]) 33 | } 34 | return median 35 | } -------------------------------------------------------------------------------- /111-minimum-depth-of-binary-tree/README.md: -------------------------------------------------------------------------------- 1 |

111. Minimum Depth of Binary Tree

Easy


Given a binary tree, find its minimum depth.

2 | 3 |

The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

4 | 5 |

Note: A leaf is a node with no children.

6 | 7 |

 

8 |

Example 1:

9 | 10 |
Input: root = [3,9,20,null,null,15,7]
11 | Output: 2
12 | 
13 | 14 |

Example 2:

15 | 16 |
Input: root = [2,null,3,null,4,null,5,null,6]
17 | Output: 5
18 | 
19 | 20 |

 

21 |

Constraints:

22 | 23 |
    24 |
  • The number of nodes in the tree is in the range [0, 105].
  • 25 |
  • -1000 <= Node.val <= 1000
  • 26 |
27 |
-------------------------------------------------------------------------------- /520-detect-capital/README.md: -------------------------------------------------------------------------------- 1 |

520. Detect Capital

Easy


We define the usage of capitals in a word to be right when one of the following cases holds:

2 | 3 |
    4 |
  • All letters in this word are capitals, like "USA".
  • 5 |
  • All letters in this word are not capitals, like "leetcode".
  • 6 |
  • Only the first letter in this word is capital, like "Google".
  • 7 |
8 | 9 |

Given a string word, return true if the usage of capitals in it is right.

10 | 11 |

 

12 |

Example 1:

13 |
Input: word = "USA"
14 | Output: true
15 | 

Example 2:

16 |
Input: word = "FlaG"
17 | Output: false
18 | 
19 |

 

20 |

Constraints:

21 | 22 |
    23 |
  • 1 <= word.length <= 100
  • 24 |
  • word consists of lowercase and uppercase English letters.
  • 25 |
26 |
-------------------------------------------------------------------------------- /69-sqrtx/README.md: -------------------------------------------------------------------------------- 1 |

69. Sqrt(x)

Easy


Given a non-negative integer x, compute and return the square root of x.

2 | 3 |

Since the return type is an integer, the decimal digits are truncated, and only the integer part of the result is returned.

4 | 5 |

Note: You are not allowed to use any built-in exponent function or operator, such as pow(x, 0.5) or x ** 0.5.

6 | 7 |

 

8 |

Example 1:

9 | 10 |
Input: x = 4
11 | Output: 2
12 | 
13 | 14 |

Example 2:

15 | 16 |
Input: x = 8
17 | Output: 2
18 | Explanation: The square root of 8 is 2.82842..., and since the decimal part is truncated, 2 is returned.
19 | 20 |

 

21 |

Constraints:

22 | 23 |
    24 |
  • 0 <= x <= 231 - 1
  • 25 |
26 |
-------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Akanksha kumari 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /1603-design-parking-system/1603-design-parking-system.go: -------------------------------------------------------------------------------- 1 | type ParkingSystem struct { 2 | bigSlot int 3 | midSlot int 4 | smallSlot int 5 | } 6 | 7 | 8 | func Constructor(big int, medium int, small int) ParkingSystem { 9 | return ParkingSystem{ 10 | bigSlot : big, 11 | midSlot: medium, 12 | smallSlot: small, 13 | } 14 | } 15 | 16 | 17 | func (this *ParkingSystem) AddCar(carType int) bool { 18 | if carType == 1 && this.bigSlot <= 0 { 19 | return false 20 | } else if carType == 1 { 21 | this.bigSlot-- 22 | return true 23 | } 24 | if carType == 2 && this.midSlot <= 0 { 25 | return false 26 | } else if carType == 2 { 27 | this.midSlot-- 28 | return true 29 | } 30 | if carType == 3 && this.smallSlot <= 0 { 31 | return false 32 | } else if carType == 3 { 33 | this.smallSlot-- 34 | return true 35 | } 36 | return true 37 | } 38 | 39 | 40 | /** 41 | * Your ParkingSystem object will be instantiated and called as such: 42 | * obj := Constructor(big, medium, small); 43 | * param_1 := obj.AddCar(carType); 44 | */ -------------------------------------------------------------------------------- /101-symmetric-tree/README.md: -------------------------------------------------------------------------------- 1 |

101. Symmetric Tree

Easy


Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).

2 | 3 |

 

4 |

Example 1:

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

Example 2:

11 | 12 |
Input: root = [1,2,2,null,3,null,3]
13 | Output: false
14 | 
15 | 16 |

 

17 |

Constraints:

18 | 19 |
    20 |
  • The number of nodes in the tree is in the range [1, 1000].
  • 21 |
  • -100 <= Node.val <= 100
  • 22 |
23 | 24 |

 

25 | Follow up: Could you solve it both recursively and iteratively?
-------------------------------------------------------------------------------- /136-single-number/README.md: -------------------------------------------------------------------------------- 1 |

136. Single Number

Easy


Given a non-empty array of integers nums, every element appears twice except for one. Find that single one.

2 | 3 |

You must implement a solution with a linear runtime complexity and use only constant extra space.

4 | 5 |

 

6 |

Example 1:

7 |
Input: nums = [2,2,1]
 8 | Output: 1
 9 | 

Example 2:

10 |
Input: nums = [4,1,2,1,2]
11 | Output: 4
12 | 

Example 3:

13 |
Input: nums = [1]
14 | Output: 1
15 | 
16 |

 

17 |

Constraints:

18 | 19 |
    20 |
  • 1 <= nums.length <= 3 * 104
  • 21 |
  • -3 * 104 <= nums[i] <= 3 * 104
  • 22 |
  • Each element in the array appears twice except for one element which appears only once.
  • 23 |
24 |
-------------------------------------------------------------------------------- /102-binary-tree-level-order-traversal/README.md: -------------------------------------------------------------------------------- 1 |

102. Binary Tree Level Order Traversal

Medium


Given the root of a binary tree, return the level order traversal of its nodes' values. (i.e., from left to right, level by level).

2 | 3 |

 

4 |

Example 1:

5 | 6 |
Input: root = [3,9,20,null,null,15,7]
 7 | Output: [[3],[9,20],[15,7]]
 8 | 
9 | 10 |

Example 2:

11 | 12 |
Input: root = [1]
13 | Output: [[1]]
14 | 
15 | 16 |

Example 3:

17 | 18 |
Input: root = []
19 | Output: []
20 | 
21 | 22 |

 

23 |

Constraints:

24 | 25 |
    26 |
  • The number of nodes in the tree is in the range [0, 2000].
  • 27 |
  • -1000 <= Node.val <= 1000
  • 28 |
29 |
-------------------------------------------------------------------------------- /199-binary-tree-right-side-view/README.md: -------------------------------------------------------------------------------- 1 |

199. Binary Tree Right Side View

Medium


Given the root of a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.

2 | 3 |

 

4 |

Example 1:

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

Example 2:

11 | 12 |
Input: root = [1,null,3]
13 | Output: [1,3]
14 | 
15 | 16 |

Example 3:

17 | 18 |
Input: root = []
19 | Output: []
20 | 
21 | 22 |

 

23 |

Constraints:

24 | 25 |
    26 |
  • The number of nodes in the tree is in the range [0, 100].
  • 27 |
  • -100 <= Node.val <= 100
  • 28 |
29 |
-------------------------------------------------------------------------------- /226-invert-binary-tree/README.md: -------------------------------------------------------------------------------- 1 |

226. Invert Binary Tree

Easy


Given the root of a binary tree, invert the tree, and return its root.

2 | 3 |

 

4 |

Example 1:

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

Example 2:

11 | 12 |
Input: root = [2,1,3]
13 | Output: [2,3,1]
14 | 
15 | 16 |

Example 3:

17 | 18 |
Input: root = []
19 | Output: []
20 | 
21 | 22 |

 

23 |

Constraints:

24 | 25 |
    26 |
  • The number of nodes in the tree is in the range [0, 100].
  • 27 |
  • -100 <= Node.val <= 100
  • 28 |
29 |
-------------------------------------------------------------------------------- /50-powx-n/README.md: -------------------------------------------------------------------------------- 1 |

50. Pow(x, n)

Medium


Implement pow(x, n), which calculates x raised to the power n (i.e., xn).

2 | 3 |

 

4 |

Example 1:

5 | 6 |
Input: x = 2.00000, n = 10
 7 | Output: 1024.00000
 8 | 
9 | 10 |

Example 2:

11 | 12 |
Input: x = 2.10000, n = 3
13 | Output: 9.26100
14 | 
15 | 16 |

Example 3:

17 | 18 |
Input: x = 2.00000, n = -2
19 | Output: 0.25000
20 | Explanation: 2-2 = 1/22 = 1/4 = 0.25
21 | 
22 | 23 |

 

24 |

Constraints:

25 | 26 |
    27 |
  • -100.0 < x < 100.0
  • 28 |
  • -231 <= n <= 231-1
  • 29 |
  • -104 <= xn <= 104
  • 30 |
31 |
-------------------------------------------------------------------------------- /792-number-of-matching-subsequences/792-number-of-matching-subsequences.go: -------------------------------------------------------------------------------- 1 | // Using HashMap and Queue 2 | func numMatchingSubseq(s string, words []string) int { 3 | slice := []byte(s) 4 | hashmap := make(map[byte][]int) 5 | for i, element := range slice { 6 | hashmap[element] = append(hashmap[element], i) 7 | } 8 | 9 | count := 0 10 | for _, word := range words { 11 | prev_pos := -1 12 | i := 0 13 | for ; i prev_pos { 36 | return elem 37 | } 38 | } 39 | return -1 40 | } 41 | -------------------------------------------------------------------------------- /94-binary-tree-inorder-traversal/README.md: -------------------------------------------------------------------------------- 1 |

94. Binary Tree Inorder Traversal

Easy


Given the root of a binary tree, return the inorder traversal of its nodes' values.

2 | 3 |

 

4 |

Example 1:

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

Example 2:

11 | 12 |
Input: root = []
13 | Output: []
14 | 
15 | 16 |

Example 3:

17 | 18 |
Input: root = [1]
19 | Output: [1]
20 | 
21 | 22 |

 

23 |

Constraints:

24 | 25 |
    26 |
  • The number of nodes in the tree is in the range [0, 100].
  • 27 |
  • -100 <= Node.val <= 100
  • 28 |
29 | 30 |

 

31 | Follow up: Recursive solution is trivial, could you do it iteratively?
-------------------------------------------------------------------------------- /145-binary-tree-postorder-traversal/README.md: -------------------------------------------------------------------------------- 1 |

145. Binary Tree Postorder Traversal

Easy


Given the root of a binary tree, return the postorder traversal of its nodes' values.

2 | 3 |

 

4 |

Example 1:

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

Example 2:

11 | 12 |
Input: root = []
13 | Output: []
14 | 
15 | 16 |

Example 3:

17 | 18 |
Input: root = [1]
19 | Output: [1]
20 | 
21 | 22 |

 

23 |

Constraints:

24 | 25 |
    26 |
  • The number of the nodes in the tree is in the range [0, 100].
  • 27 |
  • -100 <= Node.val <= 100
  • 28 |
29 | 30 |

 

31 | Follow up: Recursive solution is trivial, could you do it iteratively?
-------------------------------------------------------------------------------- /219-contains-duplicate-ii/README.md: -------------------------------------------------------------------------------- 1 |

219. Contains Duplicate II

Easy


Given an integer array nums and an integer k, return true if there are two distinct indices i and j in the array such that nums[i] == nums[j] and abs(i - j) <= k.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
Input: nums = [1,2,3,1], k = 3
 7 | Output: true
 8 | 
9 | 10 |

Example 2:

11 | 12 |
Input: nums = [1,0,1,1], k = 1
13 | Output: true
14 | 
15 | 16 |

Example 3:

17 | 18 |
Input: nums = [1,2,3,1,2,3], k = 2
19 | Output: false
20 | 
21 | 22 |

 

23 |

Constraints:

24 | 25 |
    26 |
  • 1 <= nums.length <= 105
  • 27 |
  • -109 <= nums[i] <= 109
  • 28 |
  • 0 <= k <= 105
  • 29 |
30 |
-------------------------------------------------------------------------------- /12-integer-to-roman/12-integer-to-roman.go: -------------------------------------------------------------------------------- 1 | func intToRoman(num int) string { 2 | var ans []string 3 | for ;num > 0; { 4 | a, n := Category(num) 5 | ans = append(ans, a) 6 | num = n 7 | } 8 | // slice of string to string using join 9 | return strings.Join(ans, "") 10 | } 11 | 12 | func Category(num int) (string, int) { 13 | if num / 1000 >= 1 { 14 | return "M", num-1000 15 | } else if num >= 900 { 16 | return "CM", num-900 17 | }else if num / 500 >= 1 { 18 | return "D", num-500 19 | } else if num >= 400 { 20 | return "CD", num-400 21 | }else if num / 100 >= 1 { 22 | return "C", num-100 23 | } else if num >= 90 { 24 | return "XC", num-90 25 | }else if num / 50 >= 1 { 26 | return "L", num-50 27 | } else if num >= 40 { 28 | return "XL", num-40 29 | }else if num / 10 >= 1 { 30 | return "X", num-10 31 | } else if num == 9 { 32 | return "IX", num-9 33 | }else if num / 5 >= 1 { 34 | return "V", num-5 35 | } else if num == 4 { 36 | return "IV", num-4 37 | }else if num <= 3 { 38 | return "I", num-1 39 | } 40 | return "", 0 41 | } -------------------------------------------------------------------------------- /144-binary-tree-preorder-traversal/README.md: -------------------------------------------------------------------------------- 1 |

144. Binary Tree Preorder Traversal

Easy


Given the root of a binary tree, return the preorder traversal of its nodes' values.

2 | 3 |

 

4 |

Example 1:

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

Example 2:

11 | 12 |
Input: root = []
13 | Output: []
14 | 
15 | 16 |

Example 3:

17 | 18 |
Input: root = [1]
19 | Output: [1]
20 | 
21 | 22 |

 

23 |

Constraints:

24 | 25 |
    26 |
  • The number of nodes in the tree is in the range [0, 100].
  • 27 |
  • -100 <= Node.val <= 100
  • 28 |
29 | 30 |

 

31 |

Follow up: Recursive solution is trivial, could you do it iteratively?

32 |
-------------------------------------------------------------------------------- /234-palindrome-linked-list/README.md: -------------------------------------------------------------------------------- 1 |

234. Palindrome Linked List

Easy


Given the head of a singly linked list, return true if it is a palindrome or false otherwise.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
Input: head = [1,2,2,1]
 7 | Output: true
 8 | 
9 | 10 |

Example 2:

11 | 12 |
Input: head = [1,2]
13 | Output: false
14 | 
15 | 16 |

 

17 |

Constraints:

18 | 19 |
    20 |
  • The number of nodes in the list is in the range [1, 105].
  • 21 |
  • 0 <= Node.val <= 9
  • 22 |
23 | 24 |

 

25 | Follow up: Could you do it in O(n) time and O(1) space?
-------------------------------------------------------------------------------- /200-number-of-islands/200-number-of-islands.go: -------------------------------------------------------------------------------- 1 | func numIslands(grid [][]byte) int { 2 | var island int 3 | visitedElement := make(map[string]bool) 4 | 5 | for row := 0; row < len(grid); row++ { 6 | for column := 0; column < len(grid[row]); column++ { 7 | island += checkIsland(visitedElement, grid, row, column) 8 | } 9 | } 10 | return island 11 | } 12 | 13 | func checkIsland(visitedElement map[string]bool, grid [][]byte, row, column int) int { 14 | if _, isVisited := visitedElement[key(row, column)]; isVisited { 15 | return 0 16 | } 17 | 18 | if row < 0 || row >= len(grid) || column < 0 || column >= len(grid[0]) { 19 | return 0 20 | } 21 | 22 | visitedElement[key(row, column)] = true 23 | 24 | if grid[row][column] == '0' { 25 | return 0 26 | } 27 | 28 | checkIsland(visitedElement, grid, row, column+1) 29 | checkIsland(visitedElement, grid, row, column-1) 30 | checkIsland(visitedElement, grid, row+1, column) 31 | checkIsland(visitedElement, grid, row-1, column) 32 | 33 | return 1 34 | } 35 | 36 | func key(row, column int) string { 37 | return fmt.Sprintf("%d,%d", row, column) 38 | } -------------------------------------------------------------------------------- /242-valid-anagram/README.md: -------------------------------------------------------------------------------- 1 |

242. Valid Anagram

Easy


Given two strings s and t, return true if t is an anagram of s, and false otherwise.

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 |

Example 1:

7 |
Input: s = "anagram", t = "nagaram"
 8 | Output: true
 9 | 

Example 2:

10 |
Input: s = "rat", t = "car"
11 | Output: false
12 | 
13 |

 

14 |

Constraints:

15 | 16 |
    17 |
  • 1 <= s.length, t.length <= 5 * 104
  • 18 |
  • s and t consist of lowercase English letters.
  • 19 |
20 | 21 |

 

22 |

Follow up: What if the inputs contain Unicode characters? How would you adapt your solution to such a case?

23 |
-------------------------------------------------------------------------------- /36-valid-sudoku/36-valid-sudoku.go: -------------------------------------------------------------------------------- 1 | // Time Complexity O(n2) 2 | func isValidSudoku(board [][]byte) bool { 3 | 4 | hashMap := make(map[string]bool) 5 | 6 | for i:=0; i<9; i ++ { 7 | for j:=0;j<9; j++ { 8 | 9 | row:= i 10 | column :=j 11 | 12 | current_val :=string(board[i][j]) 13 | 14 | if current_val =="." { 15 | continue 16 | } 17 | _,ok1 := hashMap[current_val + "found in row" + string(row)] 18 | _,ok2 := hashMap[current_val + "found in column"+ string(column)] 19 | _,ok3 := hashMap[current_val + "found in grid" + string(i/3) + "-" + string(j/3)] 20 | 21 | if ok1 ||ok2||ok3{ 22 | 23 | return false 24 | } else { 25 | hashMap[current_val + "found in row" + string(row)] = true 26 | hashMap[current_val + "found in column"+ string(column)] = true 27 | hashMap[current_val + "found in grid" + string(i/3) + "-" + string(j/3)]= true 28 | } 29 | } 30 | 31 | } 32 | return true 33 | 34 | } -------------------------------------------------------------------------------- /410-split-array-largest-sum/410-split-array-largest-sum.go: -------------------------------------------------------------------------------- 1 | func findMaxSum(weights []int) (int, int) { 2 | sum := 0 3 | max := 0 4 | for _, weight := range weights { 5 | sum = sum + weight 6 | if weight > max { 7 | max = weight 8 | } 9 | } 10 | return max, sum 11 | } 12 | 13 | func splitArray(nums []int , m int ) (int) { 14 | // if len(A) < B { 15 | // return -1 16 | // } 17 | left, right := findMaxSum(nums) 18 | 19 | for left < right { 20 | mid := left + (right - left)/2 21 | 22 | // calculate the total number of subarray required to if the sum is mid for each subarray 23 | totalSubarray := 1 24 | totalSum :=0 25 | for _, num := range nums { 26 | totalSum = totalSum + num 27 | if totalSum > mid { 28 | totalSubarray++ 29 | totalSum = num 30 | } 31 | } 32 | 33 | if totalSubarray <= m { //we found the solution but we need to find the minimum 34 | right = mid 35 | } else { 36 | left = mid + 1 37 | } 38 | } 39 | 40 | return right // right is workable solution 41 | } 42 | -------------------------------------------------------------------------------- /49-group-anagrams/README.md: -------------------------------------------------------------------------------- 1 |

49. Group Anagrams

Medium


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 |

Example 1:

7 |
Input: strs = ["eat","tea","tan","ate","nat","bat"]
 8 | Output: [["bat"],["nat","tan"],["ate","eat","tea"]]
 9 | 

Example 2:

10 |
Input: strs = [""]
11 | Output: [[""]]
12 | 

Example 3:

13 |
Input: strs = ["a"]
14 | Output: [["a"]]
15 | 
16 |

 

17 |

Constraints:

18 | 19 |
    20 |
  • 1 <= strs.length <= 104
  • 21 |
  • 0 <= strs[i].length <= 100
  • 22 |
  • strs[i] consists of lowercase English letters.
  • 23 |
24 |
-------------------------------------------------------------------------------- /64-minimum-path-sum/README.md: -------------------------------------------------------------------------------- 1 |

64. Minimum Path Sum

Medium


Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right, which minimizes the sum of all numbers along its path.

2 | 3 |

Note: You can only move either down or right at any point in time.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: grid = [[1,3,1],[1,5,1],[4,2,1]]
 9 | Output: 7
10 | Explanation: Because the path 1 → 3 → 1 → 1 → 1 minimizes the sum.
11 | 
12 | 13 |

Example 2:

14 | 15 |
Input: grid = [[1,2,3],[4,5,6]]
16 | Output: 12
17 | 
18 | 19 |

 

20 |

Constraints:

21 | 22 |
    23 |
  • m == grid.length
  • 24 |
  • n == grid[i].length
  • 25 |
  • 1 <= m, n <= 200
  • 26 |
  • 0 <= grid[i][j] <= 100
  • 27 |
28 |
-------------------------------------------------------------------------------- /78-subsets/78-subsets.go: -------------------------------------------------------------------------------- 1 | // func subsets(nums []int) [][]int { 2 | // // number of paranthesis left to be printed 3 | // res := [][]int{} 4 | // powerset(&res, []int{}, nums, 0) 5 | // return res 6 | // } 7 | 8 | func powerset(res *[][]int, currentSet []int, nums []int, level int){ 9 | if level == len(nums) { 10 | *res = append(*res, currentSet) 11 | currentSet = []int{} 12 | return 13 | } 14 | // not taking ith character at ith level 15 | powerset(res, currentSet, nums, level+1) 16 | // taking ith character at ith level 17 | powerset(res, append(currentSet, nums[level]), nums, level+1) 18 | } 19 | 20 | func subsets(nums []int) [][]int { 21 | res := make([][]int, 0, len(nums)) 22 | entry := make([]int, 0, len(nums)) 23 | 24 | backtrack(0, nums, &entry, &res) 25 | return res 26 | } 27 | 28 | func backtrack(start int, nums []int, entry *[]int, res *[][]int) { 29 | cpy := make([]int, len(*entry)) 30 | copy(cpy, *entry) 31 | *res = append(*res, cpy) 32 | 33 | for i := start; i < len(nums); i++ { 34 | *entry = append(*entry, nums[i]) 35 | backtrack(i+1, nums, entry, res) 36 | *entry = (*entry)[:len(*entry)-1] 37 | } 38 | } -------------------------------------------------------------------------------- /1329-sort-the-matrix-diagonally/1329-sort-the-matrix-diagonally.go: -------------------------------------------------------------------------------- 1 | func diagonalSort(mat [][]int) [][]int { 2 | m:=len(mat) 3 | n:=len(mat[0]) 4 | // col 0 5 | for i:=0; i 0 { 51 | mat[x][y] = i 52 | temp[i]-- 53 | x++ 54 | y++ 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /20-valid-parentheses/README.md: -------------------------------------------------------------------------------- 1 |

20. Valid Parentheses

Easy


Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.

2 | 3 |

An input string is valid if:

4 | 5 |
    6 |
  1. Open brackets must be closed by the same type of brackets.
  2. 7 |
  3. Open brackets must be closed in the correct order.
  4. 8 |
9 | 10 |

 

11 |

Example 1:

12 | 13 |
Input: s = "()"
14 | Output: true
15 | 
16 | 17 |

Example 2:

18 | 19 |
Input: s = "()[]{}"
20 | Output: true
21 | 
22 | 23 |

Example 3:

24 | 25 |
Input: s = "(]"
26 | Output: false
27 | 
28 | 29 |

 

30 |

Constraints:

31 | 32 |
    33 |
  • 1 <= s.length <= 104
  • 34 |
  • s consists of parentheses only '()[]{}'.
  • 35 |
36 |
-------------------------------------------------------------------------------- /83-remove-duplicates-from-sorted-list/README.md: -------------------------------------------------------------------------------- 1 |

83. Remove Duplicates from Sorted List

Easy


Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
Input: head = [1,1,2]
 7 | Output: [1,2]
 8 | 
9 | 10 |

Example 2:

11 | 12 |
Input: head = [1,1,2,3,3]
13 | Output: [1,2,3]
14 | 
15 | 16 |

 

17 |

Constraints:

18 | 19 |
    20 |
  • The number of nodes in the list is in the range [0, 300].
  • 21 |
  • -100 <= Node.val <= 100
  • 22 |
  • The list is guaranteed to be sorted in ascending order.
  • 23 |
24 |
-------------------------------------------------------------------------------- /75-sort-colors/75-sort-colors.go: -------------------------------------------------------------------------------- 1 | func sortColors(nums []int) { 2 | //Time complexity O(2N) 3 | 4 | // count0 := 0 5 | // count1 := 0 6 | // count2 := 0 7 | // for _, num := range nums { 8 | // if num == 0 { 9 | // count0 = count0 + 1 10 | // }else if num == 1 { 11 | // count1 = count1 + 1 12 | // }else if num == 2 { 13 | // count2 = count2 + 1 14 | // } 15 | // } 16 | // for i:= 0; i < count0 ; i = i+1 { 17 | // nums[i] = 0 18 | // } 19 | // for i:= count0; i < count0+count1 ; i = i+1 { 20 | // nums[i] = 1 21 | // } 22 | // for i:= count0 + count1 ; i < count0 + count1 + count2 ; i = i+1 { 23 | // nums[i] = 2 24 | // } 25 | 26 | // Dutch national flag algorithm 27 | 28 | for low, mid, high := 0, 0, len(nums)-1 ; mid <= high; { 29 | switch nums[mid] { 30 | case 0 : 31 | nums[low], nums[mid] = nums[mid], nums[low] 32 | mid = mid + 1 33 | low = low + 1 34 | case 1 : 35 | mid = mid + 1 36 | case 2 : 37 | nums[high], nums[mid] = nums[mid], nums[high] 38 | high = high - 1 39 | } 40 | } 41 | } -------------------------------------------------------------------------------- /203-remove-linked-list-elements/README.md: -------------------------------------------------------------------------------- 1 |

203. Remove Linked List Elements

Easy


Given the head of a linked list and an integer val, remove all the nodes of the linked list that has Node.val == val, and return the new head.

2 | 3 |

 

4 |

Example 1:

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

Example 2:

11 | 12 |
Input: head = [], val = 1
13 | Output: []
14 | 
15 | 16 |

Example 3:

17 | 18 |
Input: head = [7,7,7,7], val = 7
19 | Output: []
20 | 
21 | 22 |

 

23 |

Constraints:

24 | 25 |
    26 |
  • The number of nodes in the list is in the range [0, 104].
  • 27 |
  • 1 <= Node.val <= 50
  • 28 |
  • 0 <= val <= 50
  • 29 |
30 |
-------------------------------------------------------------------------------- /56-merge-intervals/README.md: -------------------------------------------------------------------------------- 1 |

56. Merge Intervals

Medium


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 |

Example 1:

5 | 6 |
Input: intervals = [[1,3],[2,6],[8,10],[15,18]]
 7 | Output: [[1,6],[8,10],[15,18]]
 8 | Explanation: Since intervals [1,3] and [2,6] overlap, merge them into [1,6].
 9 | 
10 | 11 |

Example 2:

12 | 13 |
Input: intervals = [[1,4],[4,5]]
14 | Output: [[1,5]]
15 | Explanation: Intervals [1,4] and [4,5] are considered overlapping.
16 | 
17 | 18 |

 

19 |

Constraints:

20 | 21 |
    22 |
  • 1 <= intervals.length <= 104
  • 23 |
  • intervals[i].length == 2
  • 24 |
  • 0 <= starti <= endi <= 104
  • 25 |
26 |
-------------------------------------------------------------------------------- /119-pascals-triangle-ii/README.md: -------------------------------------------------------------------------------- 1 |

119. Pascal's Triangle II

Easy


Given an integer rowIndex, return the rowIndexth (0-indexed) row of the 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: rowIndex = 3
 8 | Output: [1,3,3,1]
 9 | 

Example 2:

10 |
Input: rowIndex = 0
11 | Output: [1]
12 | 

Example 3:

13 |
Input: rowIndex = 1
14 | Output: [1,1]
15 | 
16 |

 

17 |

Constraints:

18 | 19 |
    20 |
  • 0 <= rowIndex <= 33
  • 21 |
22 | 23 |

 

24 |

Follow up: Could you optimize your algorithm to use only O(rowIndex) extra space?

25 |
-------------------------------------------------------------------------------- /REAL_LIFE_APPLICATION.md: -------------------------------------------------------------------------------- 1 | Array: 2 | Seat of a theathre represents an 2D array 3 | 4 | StacK: 5 | 6 | The browser history is stored in stack usually so we can go back to the last visited pages. 7 | 8 | Singly Linked List: 9 | 10 | In a music player songs are connected, we play one song then we get the other song in the playlist automatically, that's because the songs are connected and we can consider them as nodes. 11 | 12 | Doubly Linked List: 13 | 14 | Sometime we would like to go back to the previous song played so that's where doubly Linked list comes in picture. From each of the song we can either go back or play the next song. . 15 | 16 | Circular Linked List: 17 | 18 | Sometime we would like the playlist to play the first song again once it finishes playing all or in simple terms repeat itself. That's where Circular linked list comes in picture. 19 | 20 | Graph: 21 | 22 | Google Map uses graph, each cities(nodes) are connected with some edges(path) that have some weight(distance). Google map uses graph algorithm to find the shortest path between two cities. 23 | 24 | BST(Binary Search Tree): 25 | 26 | It would take at worst n element to insert/delete an element from a sorted array. So we represent it using BST. So searching and Retrieval is mostly done using BST. 27 | -------------------------------------------------------------------------------- /207-course-schedule/207-course-schedule.go: -------------------------------------------------------------------------------- 1 | func canFinish(numCourses int, prerequisites [][]int) bool { 2 | // figure out the length 3 | length := len(prerequisites) 4 | // return true if zero length 5 | if numCourses == 0 || length == 0 { 6 | return true 7 | } 8 | 9 | counter := make([]int, numCourses) 10 | for i := 0; i < length; i++ { 11 | counter[prerequisites[i][0]]++ 12 | } 13 | fmt.Println("counter", counter) 14 | var queue []int 15 | // if no dependency or free node 16 | for i := 0; i < numCourses; i++ { 17 | if counter[i] == 0 { 18 | queue = append(queue, i) 19 | } 20 | } 21 | 22 | noPre := len(queue) 23 | for { 24 | if len(queue) == 0 { 25 | break 26 | } 27 | // pop one element from queue 28 | n := queue[0] 29 | queue = queue[1:] 30 | 31 | for i := 0; i < length; i++ { 32 | // since are visiting n so remove dependency wherever it is present 33 | if prerequisites[i][1] == n { 34 | counter[prerequisites[i][0]]-- 35 | // if the dependent element are all visited add the element to queue 36 | if counter[prerequisites[i][0]] == 0 { 37 | noPre++ 38 | queue = append(queue, prerequisites[i][0]) 39 | } 40 | } 41 | } 42 | } 43 | 44 | return noPre == numCourses 45 | } -------------------------------------------------------------------------------- /729-my-calendar-i/729-my-calendar-i.go: -------------------------------------------------------------------------------- 1 | type MyCalendar struct { 2 | root *Event 3 | } 4 | 5 | 6 | type Event struct { 7 | start, end int 8 | left, right *Event 9 | } 10 | 11 | 12 | func Constructor() MyCalendar { 13 | return MyCalendar{ 14 | root: nil, 15 | } 16 | } 17 | 18 | 19 | func (this *MyCalendar) Book(start int, end int) bool { 20 | if end=end { 38 | return false 39 | } 40 | 41 | if (*root).start >= end { 42 | return Helper (start, end, &(*root).right) 43 | } 44 | if (*root).end <= start { 45 | return Helper (start, end, &(*root).left) 46 | } 47 | return false 48 | } 49 | 50 | /** 51 | * Your MyCalendar object will be instantiated and called as such: 52 | * obj := Constructor(); 53 | * param_1 := obj.Book(start,end); 54 | */ -------------------------------------------------------------------------------- /92-reverse-linked-list-ii/README.md: -------------------------------------------------------------------------------- 1 |

92. Reverse Linked List II

Medium


Given the head of a singly linked list and two integers left and right where left <= right, reverse the nodes of the list from position left to position right, and return the reversed list.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
Input: head = [1,2,3,4,5], left = 2, right = 4
 7 | Output: [1,4,3,2,5]
 8 | 
9 | 10 |

Example 2:

11 | 12 |
Input: head = [5], left = 1, right = 1
13 | Output: [5]
14 | 
15 | 16 |

 

17 |

Constraints:

18 | 19 |
    20 |
  • The number of nodes in the list is n.
  • 21 |
  • 1 <= n <= 500
  • 22 |
  • -500 <= Node.val <= 500
  • 23 |
  • 1 <= left <= right <= n
  • 24 |
25 | 26 |

 

27 | Follow up: Could you do it in one pass?
-------------------------------------------------------------------------------- /540-single-element-in-a-sorted-array/540-single-element-in-a-sorted-array.go: -------------------------------------------------------------------------------- 1 | func singleNonDuplicate(nums []int) int { 2 | n := len(nums) 3 | low, high:=0, n-2 4 | for ; low <= high; { 5 | mid := (high+low)/2 6 | //heavily depend on mid element 7 | // [3,3,7,7,10,11,11] 8 | if (mid % 2 == 0) { 9 | if (nums[mid] != nums[mid + 1]) { 10 | //Checking whether we are in right half 11 | 12 | high = mid - 1 //Shrinking the right half 13 | 14 | } else { 15 | low = mid + 1 //Shrinking the left half 16 | } 17 | } else { 18 | // [1,1,2,3,3,4,4,8,8] 19 | //Checking whether we are in right half 20 | if (nums[mid] == nums[mid + 1]) { 21 | high = mid - 1 //Shrinking the right half 22 | } else { 23 | low = mid + 1 //Shrinking the left half 24 | } 25 | } 26 | } 27 | return nums[low] 28 | } -------------------------------------------------------------------------------- /19-remove-nth-node-from-end-of-list/README.md: -------------------------------------------------------------------------------- 1 |

19. Remove Nth Node From End of List

Medium


Given the head of a linked list, remove the nth node from the end of the list and return its head.

2 | 3 |

 

4 |

Example 1:

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

Example 2:

11 | 12 |
Input: head = [1], n = 1
13 | Output: []
14 | 
15 | 16 |

Example 3:

17 | 18 |
Input: head = [1,2], n = 1
19 | Output: [1]
20 | 
21 | 22 |

 

23 |

Constraints:

24 | 25 |
    26 |
  • The number of nodes in the list is sz.
  • 27 |
  • 1 <= sz <= 30
  • 28 |
  • 0 <= Node.val <= 100
  • 29 |
  • 1 <= n <= sz
  • 30 |
31 | 32 |

 

33 |

Follow up: Could you do this in one pass?

34 |
-------------------------------------------------------------------------------- /40-combination-sum-ii/README.md: -------------------------------------------------------------------------------- 1 |

40. Combination Sum II

Medium


Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sum to target.

2 | 3 |

Each number in candidates may only be used once in the combination.

4 | 5 |

Note: The solution set must not contain duplicate combinations.

6 | 7 |

 

8 |

Example 1:

9 | 10 |
Input: candidates = [10,1,2,7,6,1,5], target = 8
11 | Output: 
12 | [
13 | [1,1,6],
14 | [1,2,5],
15 | [1,7],
16 | [2,6]
17 | ]
18 | 
19 | 20 |

Example 2:

21 | 22 |
Input: candidates = [2,5,2,1,2], target = 5
23 | Output: 
24 | [
25 | [1,2,2],
26 | [5]
27 | ]
28 | 
29 | 30 |

 

31 |

Constraints:

32 | 33 |
    34 |
  • 1 <= candidates.length <= 100
  • 35 |
  • 1 <= candidates[i] <= 50
  • 36 |
  • 1 <= target <= 30
  • 37 |
38 |
-------------------------------------------------------------------------------- /206-reverse-linked-list/README.md: -------------------------------------------------------------------------------- 1 |

206. Reverse Linked List

Easy


Given the head of a singly linked list, reverse the list, and return the reversed list.

2 | 3 |

 

4 |

Example 1:

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

Example 2:

11 | 12 |
Input: head = [1,2]
13 | Output: [2,1]
14 | 
15 | 16 |

Example 3:

17 | 18 |
Input: head = []
19 | Output: []
20 | 
21 | 22 |

 

23 |

Constraints:

24 | 25 |
    26 |
  • The number of nodes in the list is the range [0, 5000].
  • 27 |
  • -5000 <= Node.val <= 5000
  • 28 |
29 | 30 |

 

31 |

Follow up: A linked list can be reversed either iteratively or recursively. Could you implement both?

32 |
-------------------------------------------------------------------------------- /876-middle-of-the-linked-list/README.md: -------------------------------------------------------------------------------- 1 |

876. Middle of the Linked List

Easy


Given the head of a singly linked list, return the middle node of the linked list.

2 | 3 |

If there are two middle nodes, return the second middle node.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: head = [1,2,3,4,5]
 9 | Output: [3,4,5]
10 | Explanation: The middle node of the list is node 3.
11 | 
12 | 13 |

Example 2:

14 | 15 |
Input: head = [1,2,3,4,5,6]
16 | Output: [4,5,6]
17 | Explanation: Since the list has two middle nodes with values 3 and 4, we return the second one.
18 | 
19 | 20 |

 

21 |

Constraints:

22 | 23 |
    24 |
  • The number of nodes in the list is in the range [1, 100].
  • 25 |
  • 1 <= Node.val <= 100
  • 26 |
27 |
-------------------------------------------------------------------------------- /35-search-insert-position/README.md: -------------------------------------------------------------------------------- 1 |

35. Search Insert Position

Easy


Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.

2 | 3 |

You must write an algorithm with O(log n) runtime complexity.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: nums = [1,3,5,6], target = 5
 9 | Output: 2
10 | 
11 | 12 |

Example 2:

13 | 14 |
Input: nums = [1,3,5,6], target = 2
15 | Output: 1
16 | 
17 | 18 |

Example 3:

19 | 20 |
Input: nums = [1,3,5,6], target = 7
21 | Output: 4
22 | 
23 | 24 |

 

25 |

Constraints:

26 | 27 |
    28 |
  • 1 <= nums.length <= 104
  • 29 |
  • -104 <= nums[i] <= 104
  • 30 |
  • nums contains distinct values sorted in ascending order.
  • 31 |
  • -104 <= target <= 104
  • 32 |
33 |
-------------------------------------------------------------------------------- /3-longest-substring-without-repeating-characters/README.md: -------------------------------------------------------------------------------- 1 |

3. Longest Substring Without Repeating Characters

Medium


Given a string s, find the length of the longest substring without repeating characters.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
Input: s = "abcabcbb"
 7 | Output: 3
 8 | Explanation: The answer is "abc", with the length of 3.
 9 | 
10 | 11 |

Example 2:

12 | 13 |
Input: s = "bbbbb"
14 | Output: 1
15 | Explanation: The answer is "b", with the length of 1.
16 | 
17 | 18 |

Example 3:

19 | 20 |
Input: s = "pwwkew"
21 | Output: 3
22 | Explanation: The answer is "wke", with the length of 3.
23 | Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
24 | 
25 | 26 |

 

27 |

Constraints:

28 | 29 |
    30 |
  • 0 <= s.length <= 5 * 104
  • 31 |
  • s consists of English letters, digits, symbols and spaces.
  • 32 |
33 |
-------------------------------------------------------------------------------- /543-diameter-of-binary-tree/README.md: -------------------------------------------------------------------------------- 1 |

543. Diameter of Binary Tree

Easy


Given the root of a binary tree, return the length of the diameter of the tree.

2 | 3 |

The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root.

4 | 5 |

The length of a path between two nodes is represented by the number of edges between them.

6 | 7 |

 

8 |

Example 1:

9 | 10 |
Input: root = [1,2,3,4,5]
11 | Output: 3
12 | Explanation: 3 is the length of the path [4,2,1,3] or [5,2,1,3].
13 | 
14 | 15 |

Example 2:

16 | 17 |
Input: root = [1,2]
18 | Output: 1
19 | 
20 | 21 |

 

22 |

Constraints:

23 | 24 |
    25 |
  • The number of nodes in the tree is in the range [1, 104].
  • 26 |
  • -100 <= Node.val <= 100
  • 27 |
28 |
-------------------------------------------------------------------------------- /4-median-of-two-sorted-arrays/README.md: -------------------------------------------------------------------------------- 1 |

4. Median of Two Sorted Arrays

Hard


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

2 | 3 |

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

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: nums1 = [1,3], nums2 = [2]
 9 | Output: 2.00000
10 | Explanation: merged array = [1,2,3] and median is 2.
11 | 
12 | 13 |

Example 2:

14 | 15 |
Input: nums1 = [1,2], nums2 = [3,4]
16 | Output: 2.50000
17 | Explanation: merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.
18 | 
19 | 20 |

 

21 |

Constraints:

22 | 23 |
    24 |
  • nums1.length == m
  • 25 |
  • nums2.length == n
  • 26 |
  • 0 <= m <= 1000
  • 27 |
  • 0 <= n <= 1000
  • 28 |
  • 1 <= m + n <= 2000
  • 29 |
  • -106 <= nums1[i], nums2[i] <= 106
  • 30 |
31 |
-------------------------------------------------------------------------------- /704-binary-search/README.md: -------------------------------------------------------------------------------- 1 |

704. Binary Search

Easy


Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1.

2 | 3 |

You must write an algorithm with O(log n) runtime complexity.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: nums = [-1,0,3,5,9,12], target = 9
 9 | Output: 4
10 | Explanation: 9 exists in nums and its index is 4
11 | 
12 | 13 |

Example 2:

14 | 15 |
Input: nums = [-1,0,3,5,9,12], target = 2
16 | Output: -1
17 | Explanation: 2 does not exist in nums so return -1
18 | 
19 | 20 |

 

21 |

Constraints:

22 | 23 |
    24 |
  • 1 <= nums.length <= 104
  • 25 |
  • -104 < nums[i], target < 104
  • 26 |
  • All the integers in nums are unique.
  • 27 |
  • nums is sorted in ascending order.
  • 28 |
29 |
-------------------------------------------------------------------------------- /229-majority-element-ii/229-majority-element-ii.go: -------------------------------------------------------------------------------- 1 | import "math" 2 | func majorityElement(nums []int) []int { 3 | // Important point - there can be atmost 2 majority element that appears more than n/3 times. 4 | element1 := math.MinInt 5 | element2 := math.MinInt 6 | c1 := 0 7 | c2 := 0 8 | for i:=0; i < len(nums); i++ { 9 | if element1 != math.MinInt && nums[i] == element1 { 10 | c1++ 11 | } else if element2 != math.MinInt && nums[i] == element2 { 12 | c2++ 13 | } else if c1 == 0 { 14 | element1 = nums[i] 15 | c1++ 16 | } else if c2 == 0 { 17 | element2 = nums[i] 18 | c2++ 19 | } else { 20 | c1-- 21 | c2-- 22 | } 23 | } 24 | // initialize ans array 25 | var ans []int 26 | // reset the count 27 | c1 = 0 28 | c2 = 0 29 | for _, ele := range nums { 30 | if element1 != math.MinInt && element1 == ele { 31 | c1++ 32 | } 33 | if element2 != math.MinInt && element2 == ele { 34 | c2++ 35 | } 36 | } 37 | // check if the count is greater than n/3 38 | if c1 > len(nums)/3 { 39 | ans = append(ans, element1) 40 | } 41 | if c2 > len(nums)/3 { 42 | ans = append(ans, element2) 43 | } 44 | return ans 45 | } -------------------------------------------------------------------------------- /200-number-of-islands/README.md: -------------------------------------------------------------------------------- 1 |

200. Number of Islands

Medium


Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands.

2 | 3 |

An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: grid = [
 9 |   ["1","1","1","1","0"],
10 |   ["1","1","0","1","0"],
11 |   ["1","1","0","0","0"],
12 |   ["0","0","0","0","0"]
13 | ]
14 | Output: 1
15 | 
16 | 17 |

Example 2:

18 | 19 |
Input: grid = [
20 |   ["1","1","0","0","0"],
21 |   ["1","1","0","0","0"],
22 |   ["0","0","1","0","0"],
23 |   ["0","0","0","1","1"]
24 | ]
25 | Output: 3
26 | 
27 | 28 |

 

29 |

Constraints:

30 | 31 |
    32 |
  • m == grid.length
  • 33 |
  • n == grid[i].length
  • 34 |
  • 1 <= m, n <= 300
  • 35 |
  • grid[i][j] is '0' or '1'.
  • 36 |
37 |
-------------------------------------------------------------------------------- /53-maximum-subarray/README.md: -------------------------------------------------------------------------------- 1 |

53. Maximum Subarray

Easy


Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.

2 | 3 |

A subarray is a contiguous part of an array.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: nums = [-2,1,-3,4,-1,2,1,-5,4]
 9 | Output: 6
10 | Explanation: [4,-1,2,1] has the largest sum = 6.
11 | 
12 | 13 |

Example 2:

14 | 15 |
Input: nums = [1]
16 | Output: 1
17 | 
18 | 19 |

Example 3:

20 | 21 |
Input: nums = [5,4,-1,7,8]
22 | Output: 23
23 | 
24 | 25 |

 

26 |

Constraints:

27 | 28 |
    29 |
  • 1 <= nums.length <= 105
  • 30 |
  • -104 <= nums[i] <= 104
  • 31 |
32 | 33 |

 

34 |

Follow up: If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.

35 |
-------------------------------------------------------------------------------- /17-letter-combinations-of-a-phone-number/README.md: -------------------------------------------------------------------------------- 1 |

17. Letter Combinations of a Phone Number

Medium


Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. Return the answer in any order.

2 | 3 |

A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: digits = "23"
 9 | Output: ["ad","ae","af","bd","be","bf","cd","ce","cf"]
10 | 
11 | 12 |

Example 2:

13 | 14 |
Input: digits = ""
15 | Output: []
16 | 
17 | 18 |

Example 3:

19 | 20 |
Input: digits = "2"
21 | Output: ["a","b","c"]
22 | 
23 | 24 |

 

25 |

Constraints:

26 | 27 |
    28 |
  • 0 <= digits.length <= 4
  • 29 |
  • digits[i] is a digit in the range ['2', '9'].
  • 30 |
31 |
-------------------------------------------------------------------------------- /637-average-of-levels-in-binary-tree/README.md: -------------------------------------------------------------------------------- 1 |

637. Average of Levels in Binary Tree

Easy


Given the root of a binary tree, return the average value of the nodes on each level in the form of an array. Answers within 10-5 of the actual answer will be accepted. 2 |

 

3 |

Example 1:

4 | 5 |
Input: root = [3,9,20,null,null,15,7]
 6 | Output: [3.00000,14.50000,11.00000]
 7 | Explanation: The average value of nodes on level 0 is 3, on level 1 is 14.5, and on level 2 is 11.
 8 | Hence return [3, 14.5, 11].
 9 | 
10 | 11 |

Example 2:

12 | 13 |
Input: root = [3,9,20,15,7]
14 | Output: [3.00000,14.50000,11.00000]
15 | 
16 | 17 |

 

18 |

Constraints:

19 | 20 |
    21 |
  • The number of nodes in the tree is in the range [1, 104].
  • 22 |
  • -231 <= Node.val <= 231 - 1
  • 23 |
24 |
-------------------------------------------------------------------------------- /58-length-of-last-word/README.md: -------------------------------------------------------------------------------- 1 |

58. Length of Last Word

Easy


Given a string s consisting of words and spaces, return the length of the last word in the string.

2 | 3 |

A word is a maximal substring consisting of non-space characters only.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: s = "Hello World"
 9 | Output: 5
10 | Explanation: The last word is "World" with length 5.
11 | 
12 | 13 |

Example 2:

14 | 15 |
Input: s = "   fly me   to   the moon  "
16 | Output: 4
17 | Explanation: The last word is "moon" with length 4.
18 | 
19 | 20 |

Example 3:

21 | 22 |
Input: s = "luffy is still joyboy"
23 | Output: 6
24 | Explanation: The last word is "joyboy" with length 6.
25 | 
26 | 27 |

 

28 |

Constraints:

29 | 30 |
    31 |
  • 1 <= s.length <= 104
  • 32 |
  • s consists of only English letters and spaces ' '.
  • 33 |
  • There will be at least one word in s.
  • 34 |
35 |
-------------------------------------------------------------------------------- /410-split-array-largest-sum/README.md: -------------------------------------------------------------------------------- 1 |

410. Split Array Largest Sum

Hard


Given an array nums which consists of non-negative integers and an integer m, you can split the array into m non-empty continuous subarrays.

2 | 3 |

Write an algorithm to minimize the largest sum among these m subarrays.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: nums = [7,2,5,10,8], m = 2
 9 | Output: 18
10 | Explanation:
11 | There are four ways to split nums into two subarrays.
12 | The best way is to split it into [7,2,5] and [10,8],
13 | where the largest sum among the two subarrays is only 18.
14 | 
15 | 16 |

Example 2:

17 | 18 |
Input: nums = [1,2,3,4,5], m = 2
19 | Output: 9
20 | 
21 | 22 |

Example 3:

23 | 24 |
Input: nums = [1,4,4], m = 3
25 | Output: 4
26 | 
27 | 28 |

 

29 |

Constraints:

30 | 31 |
    32 |
  • 1 <= nums.length <= 1000
  • 33 |
  • 0 <= nums[i] <= 106
  • 34 |
  • 1 <= m <= min(50, nums.length)
  • 35 |
36 |
-------------------------------------------------------------------------------- /75-sort-colors/README.md: -------------------------------------------------------------------------------- 1 |

75. Sort Colors

Medium


Given an array nums with n objects colored red, white, or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white, and blue.

2 | 3 |

We will use the integers 0, 1, and 2 to represent the color red, white, and blue, respectively.

4 | 5 |

You must solve this problem without using the library's sort function.

6 | 7 |

 

8 |

Example 1:

9 | 10 |
Input: nums = [2,0,2,1,1,0]
11 | Output: [0,0,1,1,2,2]
12 | 
13 | 14 |

Example 2:

15 | 16 |
Input: nums = [2,0,1]
17 | Output: [0,1,2]
18 | 
19 | 20 |

 

21 |

Constraints:

22 | 23 |
    24 |
  • n == nums.length
  • 25 |
  • 1 <= n <= 300
  • 26 |
  • nums[i] is either 0, 1, or 2.
  • 27 |
28 | 29 |

 

30 |

Follow up: Could you come up with a one-pass algorithm using only constant extra space?

31 |
-------------------------------------------------------------------------------- /21-merge-two-sorted-lists/README.md: -------------------------------------------------------------------------------- 1 |

21. Merge Two Sorted Lists

Easy


You are given the heads of two sorted linked lists list1 and list2.

2 | 3 |

Merge the two lists in a one sorted list. The list should be made by splicing together the nodes of the first two lists.

4 | 5 |

Return the head of the merged linked list.

6 | 7 |

 

8 |

Example 1:

9 | 10 |
Input: list1 = [1,2,4], list2 = [1,3,4]
11 | Output: [1,1,2,3,4,4]
12 | 
13 | 14 |

Example 2:

15 | 16 |
Input: list1 = [], list2 = []
17 | Output: []
18 | 
19 | 20 |

Example 3:

21 | 22 |
Input: list1 = [], list2 = [0]
23 | Output: [0]
24 | 
25 | 26 |

 

27 |

Constraints:

28 | 29 |
    30 |
  • The number of nodes in both lists is in the range [0, 50].
  • 31 |
  • -100 <= Node.val <= 100
  • 32 |
  • Both list1 and list2 are sorted in non-decreasing order.
  • 33 |
34 |
-------------------------------------------------------------------------------- /100-same-tree/README.md: -------------------------------------------------------------------------------- 1 |

100. Same Tree

Easy


Given the roots of two binary trees p and q, write a function to check if they are the same or not.

2 | 3 |

Two binary trees are considered the same if they are structurally identical, and the nodes have the same value.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: p = [1,2,3], q = [1,2,3]
 9 | Output: true
10 | 
11 | 12 |

Example 2:

13 | 14 |
Input: p = [1,2], q = [1,null,2]
15 | Output: false
16 | 
17 | 18 |

Example 3:

19 | 20 |
Input: p = [1,2,1], q = [1,1,2]
21 | Output: false
22 | 
23 | 24 |

 

25 |

Constraints:

26 | 27 |
    28 |
  • The number of nodes in both trees is in the range [0, 100].
  • 29 |
  • -104 <= Node.val <= 104
  • 30 |
31 |
-------------------------------------------------------------------------------- /216-combination-sum-iii/216-combination-sum-iii.go: -------------------------------------------------------------------------------- 1 | func combinationSum3(k int, n int) [][]int { 2 | var twoD [][]int 3 | var a []int 4 | candidates := make([]int, 9) 5 | for i := 1; i<=9; i++ { 6 | candidates[i-1] = i 7 | } 8 | 9 | //findCombinations(0, target, a, candidates, &twoD) 10 | backtrack(0, n, a, candidates, &twoD, k) 11 | return twoD 12 | } 13 | 14 | //backtrack --- answer at every node 15 | func backtrack(pos int, target int, entry []int, candidates []int, res *[][]int, k int) { 16 | // fmt.Println("backtrack called for with ", entry) 17 | if target < 0 { 18 | return 19 | } 20 | if target == 0 && len(entry) == k { 21 | cpy := make([]int, len(entry)) 22 | copy(cpy, entry) 23 | *res = append(*res, cpy) 24 | } 25 | for i := pos; i < len(candidates); i++ { 26 | // backtracking to avoid duplication. This is not required here as the candidates don't have duplicate values 27 | // if i > pos && candidates[i] == candidates[i - 1]{ 28 | // // fmt.Println("returning as i is ", i ,"pos ", pos, "entry ", entry) 29 | // continue 30 | // } 31 | 32 | entry = append(entry, candidates[i]) 33 | // because a number should not repeat itself so use i+1 34 | backtrack(i+1, target-candidates[i], entry, candidates, res, k) 35 | entry = (entry)[:len(entry)-1] 36 | } 37 | } -------------------------------------------------------------------------------- /15-3sum/15-3sum.go: -------------------------------------------------------------------------------- 1 | import "sort" 2 | func threeSum(nums []int) [][]int { 3 | var ans [][]int 4 | // sort the array 5 | sort.Ints(nums) 6 | fmt.Println(nums) 7 | for i:=0; i < len(nums)-2; i++ { 8 | if (i == 0) || (i>0 && nums[i] != nums[i-1]){ 9 | // Use Two pointer approach 10 | low, high := i+1, len(nums)-1 11 | sum := 0 - nums[i] 12 | for ;low < high; { 13 | 14 | if nums[high] + nums[low] == sum { 15 | // append the values to ans array 16 | ans = append(ans, []int{nums[i], nums[low], nums[high]} ) 17 | // If next element is same increment the low to avoid processing duplicate element 18 | for ;low < high && nums[low] == nums[low + 1];{ 19 | low++ 20 | } 21 | // If next element is same decrement the high to avoid processing duplicate element 22 | for ;low < high && nums[high] == nums[high-1]; { 23 | high-- 24 | } 25 | low ++ 26 | high -- 27 | } else if nums[low] + nums[high] > sum { 28 | high-- 29 | } else { 30 | low++ 31 | } 32 | } 33 | } 34 | } 35 | return ans 36 | } -------------------------------------------------------------------------------- /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

Medium


Given an array of integers nums sorted in non-decreasing order, find the starting and ending position of a given target value.

2 | 3 |

If target is not found in the array, return [-1, -1].

4 | 5 |

You must write an algorithm with O(log n) runtime complexity.

6 | 7 |

 

8 |

Example 1:

9 |
Input: nums = [5,7,7,8,8,10], target = 8
10 | Output: [3,4]
11 | 

Example 2:

12 |
Input: nums = [5,7,7,8,8,10], target = 6
13 | Output: [-1,-1]
14 | 

Example 3:

15 |
Input: nums = [], target = 0
16 | Output: [-1,-1]
17 | 
18 |

 

19 |

Constraints:

20 | 21 |
    22 |
  • 0 <= nums.length <= 105
  • 23 |
  • -109 <= nums[i] <= 109
  • 24 |
  • nums is a non-decreasing array.
  • 25 |
  • -109 <= target <= 109
  • 26 |
27 |
-------------------------------------------------------------------------------- /559-maximum-depth-of-n-ary-tree/README.md: -------------------------------------------------------------------------------- 1 |

559. Maximum Depth of N-ary Tree

Easy


Given a n-ary tree, find its maximum depth.

2 | 3 |

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

4 | 5 |

Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples).

6 | 7 |

 

8 |

Example 1:

9 | 10 |

11 | 12 |
Input: root = [1,null,3,2,4,null,5,6]
13 | Output: 3
14 | 
15 | 16 |

Example 2:

17 | 18 |

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

 

25 |

Constraints:

26 | 27 |
    28 |
  • The total number of nodes is in the range [0, 104].
  • 29 |
  • The depth of the n-ary tree is less than or equal to 1000.
  • 30 |
31 |
-------------------------------------------------------------------------------- /74-search-a-2d-matrix/README.md: -------------------------------------------------------------------------------- 1 |

74. Search a 2D Matrix

Medium


Write an efficient algorithm that searches for a value target in an m x n integer matrix matrix. This matrix has the following properties:

2 | 3 |
    4 |
  • Integers in each row are sorted from left to right.
  • 5 |
  • The first integer of each row is greater than the last integer of the previous row.
  • 6 |
7 | 8 |

 

9 |

Example 1:

10 | 11 |
Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
12 | Output: true
13 | 
14 | 15 |

Example 2:

16 | 17 |
Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13
18 | Output: false
19 | 
20 | 21 |

 

22 |

Constraints:

23 | 24 |
    25 |
  • m == matrix.length
  • 26 |
  • n == matrix[i].length
  • 27 |
  • 1 <= m, n <= 100
  • 28 |
  • -104 <= matrix[i][j], target <= 104
  • 29 |
30 |
-------------------------------------------------------------------------------- /62-unique-paths/62-unique-paths.go: -------------------------------------------------------------------------------- 1 | func uniquePaths(m int, n int) int { 2 | return uniquePathsCombinations(m, n) 3 | 4 | // return uniquePathsDP(m, n) 5 | //return countPaths(0,0,m,n) 6 | } 7 | // recursive approach 8 | func countPaths(i int,j int,m int,n int) int { 9 | if(i==(m-1)&&j==(n-1)) { 10 | return 1 11 | } 12 | if(i>=m||j>=n) { 13 | return 0 14 | } 15 | return countPaths(i+1,j,m,n)+countPaths(i,j+1,m,n) 16 | } 17 | 18 | 19 | // dynamic approach 20 | // DP[m-1][n-1] = 1 21 | // DP[m-1][0...n-2] = 1 22 | // DP[0...m-2][n-1] = 1 23 | 24 | func uniquePathsDP(m int, n int) int { 25 | dp := make([][]int, m) // initialize a slice of m elements 26 | for idx, _ := range dp { 27 | dp[idx] = make([]int, n) // initialize each row 28 | dp[idx][n-1] = 1 //initailize the last column as 1 29 | } 30 | for idx, _ := range dp[0] { 31 | dp[m-1][idx] = 1 // intialize last row as 1 32 | } 33 | for i:=m-2; i >= 0; i-- { 34 | for j:=n-2; j >= 0; j-- { 35 | dp[i][j] = dp[i+1][j] + dp[i][j+1] 36 | } 37 | } 38 | return dp[0][0] 39 | } 40 | 41 | // mathematical calculatiosns - combinatiosns 42 | 43 | func uniquePathsCombinations(m int, n int) int{ 44 | N := n + m - 2 // steps in each path 45 | r := m - 1 46 | res := 1 47 | 48 | for i := 1; i <= r; i++ { 49 | res = res * (N - r + i) / i //NCr 50 | } 51 | return res 52 | } -------------------------------------------------------------------------------- /48-rotate-image/README.md: -------------------------------------------------------------------------------- 1 |

48. Rotate Image

Medium


You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise).

2 | 3 |

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.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
 9 | Output: [[7,4,1],[8,5,2],[9,6,3]]
10 | 
11 | 12 |

Example 2:

13 | 14 |
Input: matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]
15 | Output: [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]
16 | 
17 | 18 |

 

19 |

Constraints:

20 | 21 |
    22 |
  • n == matrix.length == matrix[i].length
  • 23 |
  • 1 <= n <= 20
  • 24 |
  • -1000 <= matrix[i][j] <= 1000
  • 25 |
26 |
-------------------------------------------------------------------------------- /278-first-bad-version/README.md: -------------------------------------------------------------------------------- 1 |

278. First Bad Version

Easy


You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.

2 | 3 |

Suppose you have n versions [1, 2, ..., n] and you want to find out the first bad one, which causes all the following ones to be bad.

4 | 5 |

You are given an API bool isBadVersion(version) which returns whether version is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.

6 | 7 |

 

8 |

Example 1:

9 | 10 |
Input: n = 5, bad = 4
11 | Output: 4
12 | Explanation:
13 | call isBadVersion(3) -> false
14 | call isBadVersion(5) -> true
15 | call isBadVersion(4) -> true
16 | Then 4 is the first bad version.
17 | 
18 | 19 |

Example 2:

20 | 21 |
Input: n = 1, bad = 1
22 | Output: 1
23 | 
24 | 25 |

 

26 |

Constraints:

27 | 28 |
    29 |
  • 1 <= bad <= n <= 231 - 1
  • 30 |
31 |
--------------------------------------------------------------------------------