├── .gitignore ├── CONTRIBUTING.md ├── DP ├── LCSofThreeString │ └── main.cpp ├── LongestCommonSubstring │ └── main.cpp ├── MCMFormat │ └── main.cpp ├── PrintLCS │ └── main.cpp ├── ShortestCommonSuperSequence │ ├── main.cpp │ └── printingSuperSequence.cpp ├── SubsetSum │ └── main.cpp ├── boolen │ ├── a.exe │ ├── main.cpp │ └── recursive.cpp ├── catalainNumber │ ├── a.exe │ └── catalanNumber.cpp ├── coinProblemCount │ ├── main.cpp │ └── topDownApproch.cpp ├── editDistance │ └── main.cpp ├── eggDropProblem │ ├── binarySearch.cpp │ └── main.cpp ├── friendsPairing │ └── main.cpp ├── goldMine │ ├── a.exe │ └── main.cpp ├── isSubSequenceDP │ └── main.cpp ├── kanpsack_01 │ ├── main.cpp │ └── main_top_down.cpp ├── longestCommonSubsequence │ ├── bottomUP.cpp │ └── topDown.cpp ├── longestIncreasingSubsequence │ └── main.cpp ├── longestPalindromicSubsequence │ └── main.cpp ├── longestRepeatingSubsequence │ └── main.cpp ├── matrixChainMultiplication │ ├── a.exe │ └── main.cpp ├── maxCutSegments │ ├── a.exe │ ├── main.cpp │ └── main.exe ├── minimumDiffSubset │ └── main.cpp ├── minimumNumberDeletionInsertion │ └── main.cpp ├── minimumNumberOfCoins │ ├── memo.cpp │ └── topDown.cpp ├── minimumSumOfSubarray │ ├── a.exe │ └── main.cpp ├── nCr │ ├── main.cpp │ └── main.exe ├── numOfWaysToWinAGame │ └── main.cpp ├── numberOfDeletionToMakePlaindrome │ └── main.cpp ├── numberOfInsertionToMakePalindrome │ └── main.cpp ├── numberOfSubsetWithGivenDiff │ ├── a.exe │ └── main.cpp ├── paintingTheFence │ ├── main.cpp │ └── memoization.cpp ├── palindromePartitioning │ └── main.cpp ├── printingLongestPalindrome │ └── main.cpp ├── rodCutting │ └── main.cpp ├── scrambleString │ └── main.cpp ├── subsetSumCount │ └── main.cpp ├── targetSum │ └── main.cpp ├── unbounded_knapsack │ └── main.cpp └── wildCardMatching │ ├── regularExpression.cpp │ └── wildCard.cpp ├── FINAL450.xlsx ├── Leet-Code-Question ├── 01-matrix │ ├── 01-matrix.cpp │ └── README.md ├── 1-bit-and-2-bit-characters │ ├── 1-bit-and-2-bit-characters.cpp │ └── README.md ├── 132-pattern │ ├── 132-pattern.cpp │ └── README.md ├── 3sum-closest │ ├── 3sum-closest.cpp │ └── README.md ├── 4sum │ ├── 4sum.cpp │ └── README.md ├── README.md ├── add-binary │ ├── README.md │ └── add-binary.py ├── add-digits │ ├── README.md │ └── add-digits.cpp ├── add-strings │ ├── README.md │ └── add-strings.cpp ├── add-to-array-form-of-integer │ ├── README.md │ └── add-to-array-form-of-integer.cpp ├── add-two-numbers-ii │ ├── README.md │ └── add-two-numbers-ii.cpp ├── add-two-numbers │ ├── README.md │ └── add-two-numbers.cpp ├── all-elements-in-two-binary-search-trees │ ├── README.md │ └── all-elements-in-two-binary-search-trees.cpp ├── all-paths-from-source-to-target │ ├── README.md │ └── all-paths-from-source-to-target.cpp ├── arranging-coins │ ├── README.md │ └── arranging-coins.cpp ├── array-partition-i │ ├── README.md │ └── array-partition-i.cpp ├── assign-cookies │ ├── README.md │ └── assign-cookies.cpp ├── available-captures-for-rook │ ├── README.md │ └── available-captures-for-rook.cpp ├── average-salary-excluding-the-minimum-and-maximum-salary │ ├── README.md │ └── average-salary-excluding-the-minimum-and-maximum-salary.cpp ├── backspace-string-compare │ ├── README.md │ └── backspace-string-compare.cpp ├── balance-a-binary-search-tree │ ├── README.md │ └── balance-a-binary-search-tree.cpp ├── best-time-to-buy-and-sell-stock-ii │ ├── README.md │ └── best-time-to-buy-and-sell-stock-ii.cpp ├── best-time-to-buy-and-sell-stock-iii │ ├── README.md │ └── best-time-to-buy-and-sell-stock-iii.cpp ├── best-time-to-buy-and-sell-stock-iv │ ├── README.md │ └── best-time-to-buy-and-sell-stock-iv.cpp ├── best-time-to-buy-and-sell-stock │ ├── README.md │ └── best-time-to-buy-and-sell-stock.cpp ├── binary-search │ ├── README.md │ └── binary-search.cpp ├── binary-tree-inorder-traversal │ ├── README.md │ └── binary-tree-inorder-traversal.cpp ├── binary-tree-level-order-traversal │ ├── README.md │ └── binary-tree-level-order-traversal.cpp ├── binary-tree-maximum-path-sum │ ├── README.md │ └── binary-tree-maximum-path-sum.cpp ├── binary-tree-paths │ ├── README.md │ └── binary-tree-paths.cpp ├── binary-tree-postorder-traversal │ ├── README.md │ └── binary-tree-postorder-traversal.cpp ├── binary-tree-preorder-traversal │ ├── README.md │ └── binary-tree-preorder-traversal.cpp ├── binary-tree-pruning │ ├── README.md │ └── binary-tree-pruning.cpp ├── binary-tree-right-side-view │ ├── README.md │ └── binary-tree-right-side-view.cpp ├── binary-tree-zigzag-level-order-traversal │ ├── README.md │ └── binary-tree-zigzag-level-order-traversal.cpp ├── build-an-array-with-stack-operations │ ├── README.md │ └── build-an-array-with-stack-operations.cpp ├── burst-balloons │ ├── README.md │ └── burst-balloons.cpp ├── can-make-arithmetic-progression-from-sequence │ ├── README.md │ └── can-make-arithmetic-progression-from-sequence.cpp ├── can-place-flowers │ ├── README.md │ └── can-place-flowers.cpp ├── cheapest-flights-within-k-stops │ ├── README.md │ └── cheapest-flights-within-k-stops.cpp ├── check-completeness-of-a-binary-tree │ ├── README.md │ └── check-completeness-of-a-binary-tree.cpp ├── check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence │ ├── README.md │ └── check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence.cpp ├── check-if-all-1s-are-at-least-length-k-places-away │ ├── README.md │ └── check-if-all-1s-are-at-least-length-k-places-away.cpp ├── check-if-array-is-sorted-and-rotated │ ├── README.md │ └── check-if-array-is-sorted-and-rotated.cpp ├── check-if-binary-string-has-at-most-one-segment-of-ones │ ├── README.md │ └── check-if-binary-string-has-at-most-one-segment-of-ones.cpp ├── check-if-n-and-its-double-exist │ ├── README.md │ └── check-if-n-and-its-double-exist.cpp ├── check-if-number-is-a-sum-of-powers-of-three │ ├── README.md │ └── check-if-number-is-a-sum-of-powers-of-three.cpp ├── check-if-there-is-a-valid-path-in-a-grid │ ├── README.md │ └── check-if-there-is-a-valid-path-in-a-grid.cpp ├── check-if-word-equals-summation-of-two-words │ ├── README.md │ └── check-if-word-equals-summation-of-two-words.cpp ├── climbing-stairs │ └── README.md ├── clone-graph │ ├── README.md │ └── clone-graph.cpp ├── coin-change-2 │ ├── README.md │ └── coin-change-2.cpp ├── coin-change │ ├── README.md │ └── coin-change.cpp ├── combination-sum-ii │ ├── README.md │ └── combination-sum-ii.cpp ├── combination-sum │ ├── README.md │ └── combination-sum.cpp ├── combinations │ ├── README.md │ └── combinations.cpp ├── construct-binary-search-tree-from-preorder-traversal │ ├── README.md │ └── construct-binary-search-tree-from-preorder-traversal.cpp ├── construct-binary-tree-from-preorder-and-inorder-traversal │ ├── README.md │ └── construct-binary-tree-from-preorder-and-inorder-traversal.cpp ├── construct-string-from-binary-tree │ ├── README.md │ └── construct-string-from-binary-tree.cpp ├── container-with-most-water │ ├── README.md │ └── container-with-most-water.cpp ├── contains-duplicate-ii │ ├── README.md │ └── contains-duplicate-ii.cpp ├── contains-duplicate │ ├── README.md │ └── contains-duplicate.cpp ├── convert-binary-number-in-a-linked-list-to-integer │ ├── README.md │ └── convert-binary-number-in-a-linked-list-to-integer.cpp ├── convert-sorted-array-to-binary-search-tree │ ├── README.md │ └── convert-sorted-array-to-binary-search-tree.cpp ├── convert-sorted-list-to-binary-search-tree │ ├── README.md │ └── convert-sorted-list-to-binary-search-tree.cpp ├── count-good-nodes-in-binary-tree │ ├── README.md │ └── count-good-nodes-in-binary-tree.cpp ├── count-items-matching-a-rule │ ├── README.md │ └── count-items-matching-a-rule.cpp ├── count-number-of-teams │ ├── README.md │ └── count-number-of-teams.cpp ├── count-of-matches-in-tournament │ ├── README.md │ └── count-of-matches-in-tournament.cpp ├── count-primes │ └── README.md ├── counting-bits │ ├── README.md │ └── counting-bits.cpp ├── course-schedule-ii │ ├── README.md │ └── course-schedule-ii.cpp ├── course-schedule │ ├── README.md │ └── course-schedule.cpp ├── cousins-in-binary-tree │ ├── README.md │ └── cousins-in-binary-tree.cpp ├── decrypt-string-from-alphabet-to-integer-mapping │ ├── README.md │ └── decrypt-string-from-alphabet-to-integer-mapping.cpp ├── defanging-an-ip-address │ ├── README.md │ └── defanging-an-ip-address.cpp ├── delete-leaves-with-a-given-value │ ├── README.md │ └── delete-leaves-with-a-given-value.cpp ├── delete-node-in-a-bst │ ├── README.md │ └── delete-node-in-a-bst.cpp ├── delete-node-in-a-linked-list │ ├── README.md │ └── delete-node-in-a-linked-list.cpp ├── delete-operation-for-two-strings │ ├── README.md │ └── delete-operation-for-two-strings.cpp ├── design-linked-list │ ├── README.md │ └── design-linked-list.cpp ├── destination-city │ ├── README.md │ └── destination-city.cpp ├── determine-if-string-halves-are-alike │ ├── README.md │ └── determine-if-string-halves-are-alike.cpp ├── diagonal-traverse │ ├── README.md │ └── diagonal-traverse.cpp ├── diameter-of-binary-tree │ ├── README.md │ └── diameter-of-binary-tree.cpp ├── distant-barcodes │ ├── README.md │ └── distant-barcodes.cpp ├── distinct-subsequences │ ├── README.md │ └── distinct-subsequences.cpp ├── divide-two-integers │ ├── README.md │ └── divide-two-integers.cpp ├── divisor-game │ ├── README.md │ └── divisor-game.cpp ├── dota2-senate │ ├── README.md │ └── dota2-senate.cpp ├── dungeon-game │ ├── README.md │ └── dungeon-game.cpp ├── duplicate-emails │ └── README.md ├── edit-distance │ ├── README.md │ └── edit-distance.cpp ├── egg-drop-with-2-eggs-and-n-floors │ ├── README.md │ └── egg-drop-with-2-eggs-and-n-floors.cpp ├── element-appearing-more-than-25-in-sorted-array │ ├── README.md │ └── element-appearing-more-than-25-in-sorted-array.cpp ├── even-odd-tree │ ├── README.md │ └── even-odd-tree.cpp ├── factorial-trailing-zeroes │ ├── README.md │ ├── factorial-trailing-zeroes.cpp │ └── factorial-trailing-zeroes.java ├── fair-candy-swap │ ├── README.md │ └── fair-candy-swap.cpp ├── fibonacci-number │ ├── README.md │ └── fibonacci-number.cpp ├── find-all-anagrams-in-a-string │ ├── README.md │ └── find-all-anagrams-in-a-string.cpp ├── find-all-numbers-disappeared-in-an-array │ ├── README.md │ └── find-all-numbers-disappeared-in-an-array.cpp ├── find-bottom-left-tree-value │ ├── README.md │ └── find-bottom-left-tree-value.cpp ├── find-center-of-star-graph │ ├── README.md │ └── find-center-of-star-graph.cpp ├── find-first-and-last-position-of-element-in-sorted-array │ ├── README.md │ └── find-first-and-last-position-of-element-in-sorted-array.cpp ├── find-k-closest-elements │ ├── README.md │ └── find-k-closest-elements.cpp ├── find-pivot-index │ ├── README.md │ └── find-pivot-index.cpp ├── find-the-difference │ ├── README.md │ └── find-the-difference.cpp ├── find-the-duplicate-number │ ├── README.md │ └── find-the-duplicate-number.cpp ├── find-the-highest-altitude │ ├── README.md │ └── find-the-highest-altitude.cpp ├── find-the-town-judge │ ├── README.md │ └── find-the-town-judge.cpp ├── first-bad-version │ ├── README.md │ └── first-bad-version.cpp ├── first-missing-positive │ ├── README.md │ └── first-missing-positive.cpp ├── fizz-buzz │ ├── README.md │ └── fizz-buzz.cpp ├── flatten-a-multilevel-doubly-linked-list │ ├── README.md │ └── flatten-a-multilevel-doubly-linked-list.cpp ├── flatten-binary-tree-to-linked-list │ ├── README.md │ └── flatten-binary-tree-to-linked-list.cpp ├── flip-equivalent-binary-trees │ ├── README.md │ └── flip-equivalent-binary-trees.cpp ├── flood-fill │ ├── README.md │ └── flood-fill.cpp ├── flower-planting-with-no-adjacent │ ├── README.md │ └── flower-planting-with-no-adjacent.cpp ├── gas-station │ ├── README.md │ └── gas-station.cpp ├── generate-a-string-with-characters-that-have-odd-counts │ ├── README.md │ └── generate-a-string-with-characters-that-have-odd-counts.cpp ├── generate-parentheses │ ├── README.md │ └── generate-parentheses.cpp ├── global-and-local-inversions │ ├── README.md │ └── global-and-local-inversions.cpp ├── goal-parser-interpretation │ ├── README.md │ └── goal-parser-interpretation.cpp ├── gray-code │ ├── README.md │ └── gray-code.cpp ├── group-anagrams │ ├── README.md │ └── group-anagrams.cpp ├── happy-number │ ├── README.md │ └── happy-number.cpp ├── height-checker │ ├── README.md │ └── height-checker.cpp ├── house-robber-iii │ ├── README.md │ └── house-robber-iii.cpp ├── house-robber │ ├── README.md │ └── house-robber.cpp ├── html-entity-parser │ ├── README.md │ └── html-entity-parser.cpp ├── implement-stack-using-queues │ ├── README.md │ └── implement-stack-using-queues.cpp ├── implement-strstr │ ├── README.md │ └── implement-strstr.cpp ├── increasing-order-search-tree │ ├── README.md │ └── increasing-order-search-tree.cpp ├── increasing-triplet-subsequence │ ├── README.md │ └── increasing-triplet-subsequence.cpp ├── insert-delete-getrandom-o1-duplicates-allowed │ ├── README.md │ └── insert-delete-getrandom-o1-duplicates-allowed.cpp ├── insert-delete-getrandom-o1 │ ├── README.md │ └── insert-delete-getrandom-o1.cpp ├── insert-interval │ ├── README.md │ └── insert-interval.cpp ├── insert-into-a-binary-search-tree │ ├── README.md │ └── insert-into-a-binary-search-tree.cpp ├── insertion-sort-list │ ├── README.md │ └── insertion-sort-list.cpp ├── integer-to-roman │ ├── README.md │ └── integer-to-roman.cpp ├── intersection-of-two-arrays-ii │ ├── README.md │ └── intersection-of-two-arrays-ii.cpp ├── intersection-of-two-arrays │ ├── README.md │ └── intersection-of-two-arrays.cpp ├── intersection-of-two-linked-lists │ ├── README.md │ └── intersection-of-two-linked-lists.cpp ├── invert-binary-tree │ ├── README.md │ └── invert-binary-tree.cpp ├── is-graph-bipartite │ ├── README.md │ ├── is-graph-bipartite.cpp │ └── is-graph-bipartite.java ├── is-subsequence │ ├── README.md │ └── is-subsequence.cpp ├── island-perimeter │ ├── README.md │ └── island-perimeter.cpp ├── isomorphic-strings │ ├── README.md │ └── isomorphic-strings.cpp ├── jewels-and-stones │ ├── README.md │ └── jewels-and-stones.cpp ├── jump-game-ii │ ├── README.md │ └── jump-game-ii.cpp ├── jump-game │ ├── README.md │ └── jump-game.cpp ├── k-concatenation-maximum-sum │ ├── README.md │ └── k-concatenation-maximum-sum.cpp ├── keyboard-row │ ├── README.md │ └── keyboard-row.cpp ├── kids-with-the-greatest-number-of-candies │ ├── README.md │ └── kids-with-the-greatest-number-of-candies.cpp ├── kth-largest-element-in-an-array │ ├── README.md │ └── kth-largest-element-in-an-array.cpp ├── kth-smallest-element-in-a-bst │ ├── README.md │ └── kth-smallest-element-in-a-bst.cpp ├── kth-smallest-element-in-a-sorted-matrix │ ├── README.md │ └── kth-smallest-element-in-a-sorted-matrix.cpp ├── largest-odd-number-in-string │ ├── README.md │ └── largest-odd-number-in-string.cpp ├── largest-time-for-given-digits │ ├── README.md │ └── largest-time-for-given-digits.cpp ├── largest-values-from-labels │ ├── README.md │ └── largest-values-from-labels.cpp ├── last-stone-weight-ii │ ├── README.md │ └── last-stone-weight-ii.cpp ├── last-stone-weight │ ├── README.md │ └── last-stone-weight.cpp ├── leaf-similar-trees │ ├── README.md │ └── leaf-similar-trees.cpp ├── lemonade-change │ ├── README.md │ └── lemonade-change.cpp ├── length-of-last-word │ ├── README.md │ └── length-of-last-word.py ├── letter-combinations-of-a-phone-number │ ├── README.md │ └── letter-combinations-of-a-phone-number.cpp ├── linked-list-cycle-ii │ ├── README.md │ └── linked-list-cycle-ii.cpp ├── linked-list-cycle │ ├── README.md │ └── linked-list-cycle.cpp ├── linked-list-in-binary-tree │ ├── README.md │ └── linked-list-in-binary-tree.cpp ├── longest-common-prefix │ ├── README.md │ └── longest-common-prefix.cpp ├── longest-common-subsequence │ ├── README.md │ └── longest-common-subsequence.cpp ├── longest-consecutive-sequence │ ├── README.md │ └── longest-consecutive-sequence.cpp ├── longest-continuous-increasing-subsequence │ ├── README.md │ └── longest-continuous-increasing-subsequence.cpp ├── longest-increasing-subsequence │ ├── README.md │ └── longest-increasing-subsequence.cpp ├── longest-palindromic-subsequence │ ├── README.md │ └── longest-palindromic-subsequence.cpp ├── longest-palindromic-substring │ ├── README.md │ └── longest-palindromic-substring.cpp ├── longest-substring-without-repeating-characters │ ├── README.md │ └── longest-substring-without-repeating-characters.py ├── longest-univalue-path │ ├── README.md │ └── longest-univalue-path.cpp ├── longest-valid-parentheses │ └── longest-valid-parentheses.cpp ├── longest-zigzag-path-in-a-binary-tree │ ├── README.md │ └── longest-zigzag-path-in-a-binary-tree.cpp ├── loud-and-rich │ ├── README.md │ └── loud-and-rich.cpp ├── lowest-common-ancestor-of-a-binary-search-tree │ ├── README.md │ └── lowest-common-ancestor-of-a-binary-search-tree.cpp ├── lowest-common-ancestor-of-a-binary-tree │ ├── README.md │ └── lowest-common-ancestor-of-a-binary-tree.cpp ├── lowest-common-ancestor-of-deepest-leaves │ ├── README.md │ └── lowest-common-ancestor-of-deepest-leaves.cpp ├── lru-cache │ ├── README.md │ └── lru-cache.cpp ├── lucky-numbers-in-a-matrix │ ├── README.md │ └── lucky-numbers-in-a-matrix.cpp ├── magnetic-force-between-two-balls │ ├── README.md │ └── magnetic-force-between-two-balls.cpp ├── majority-element │ ├── README.md │ └── majority-element.cpp ├── make-two-arrays-equal-by-reversing-sub-arrays │ ├── README.md │ └── make-two-arrays-equal-by-reversing-sub-arrays.cpp ├── matrix-diagonal-sum │ ├── README.md │ └── matrix-diagonal-sum.cpp ├── maximal-square │ ├── README.md │ └── maximal-square.cpp ├── maximize-sum-of-array-after-k-negations │ ├── README.md │ └── maximize-sum-of-array-after-k-negations.cpp ├── maximum-69-number │ ├── README.md │ └── maximum-69-number.cpp ├── maximum-binary-tree-ii │ ├── README.md │ └── maximum-binary-tree-ii.cpp ├── maximum-binary-tree │ ├── README.md │ └── maximum-binary-tree.cpp ├── maximum-depth-of-binary-tree │ ├── README.md │ └── maximum-depth-of-binary-tree.cpp ├── maximum-length-of-repeated-subarray │ ├── README.md │ └── maximum-length-of-repeated-subarray.cpp ├── maximum-level-sum-of-a-binary-tree │ ├── README.md │ └── maximum-level-sum-of-a-binary-tree.cpp ├── maximum-product-of-splitted-binary-tree │ ├── README.md │ └── maximum-product-of-splitted-binary-tree.cpp ├── maximum-product-subarray │ ├── README.md │ └── maximum-product-subarray.cpp ├── maximum-subarray │ ├── README.md │ └── maximum-subarray.cpp ├── maximum-swap │ ├── README.md │ └── maximum-swap.cpp ├── maximum-width-of-binary-tree │ ├── README.md │ └── maximum-width-of-binary-tree.cpp ├── median-of-two-sorted-arrays │ ├── README.md │ └── median-of-two-sorted-arrays.cpp ├── merge-in-between-linked-lists │ ├── README.md │ └── merge-in-between-linked-lists.cpp ├── merge-intervals │ ├── README.md │ └── merge-intervals.cpp ├── merge-k-sorted-lists │ ├── README.md │ └── merge-k-sorted-lists.cpp ├── merge-sorted-array │ ├── README.md │ └── merge-sorted-array.cpp ├── merge-strings-alternately │ ├── README.md │ └── merge-strings-alternately.cpp ├── merge-two-binary-trees │ ├── README.md │ └── merge-two-binary-trees.cpp ├── merge-two-sorted-lists │ ├── README.md │ └── merge-two-sorted-lists.cpp ├── middle-of-the-linked-list │ ├── README.md │ └── middle-of-the-linked-list.cpp ├── min-cost-climbing-stairs │ ├── README.md │ └── min-cost-climbing-stairs.cpp ├── min-stack │ ├── README.md │ └── min-stack.cpp ├── minimum-absolute-difference-in-bst │ ├── README.md │ └── minimum-absolute-difference-in-bst.cpp ├── minimum-absolute-difference │ ├── README.md │ └── minimum-absolute-difference.cpp ├── minimum-changes-to-make-alternating-binary-string │ ├── README.md │ └── minimum-changes-to-make-alternating-binary-string.cpp ├── minimum-cost-to-move-chips-to-the-same-position │ ├── README.md │ └── minimum-cost-to-move-chips-to-the-same-position.cpp ├── minimum-depth-of-binary-tree │ ├── README.md │ └── minimum-depth-of-binary-tree.cpp ├── minimum-distance-between-bst-nodes │ ├── README.md │ └── minimum-distance-between-bst-nodes.cpp ├── minimum-falling-path-sum │ ├── README.md │ └── minimum-falling-path-sum.cpp ├── minimum-insertion-steps-to-make-a-string-palindrome │ ├── README.md │ └── minimum-insertion-steps-to-make-a-string-palindrome.cpp ├── minimum-length-of-string-after-deleting-similar-ends │ ├── README.md │ └── minimum-length-of-string-after-deleting-similar-ends.cpp ├── minimum-moves-to-equal-array-elements-ii │ ├── README.md │ └── minimum-moves-to-equal-array-elements-ii.cpp ├── minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix │ ├── README.md │ └── minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix.cpp ├── minimum-number-of-frogs-croaking │ ├── README.md │ └── minimum-number-of-frogs-croaking.java ├── minimum-number-of-steps-to-make-two-strings-anagram │ ├── README.md │ └── minimum-number-of-steps-to-make-two-strings-anagram.cpp ├── minimum-operations-to-make-array-equal │ ├── README.md │ └── minimum-operations-to-make-array-equal.cpp ├── minimum-operations-to-make-the-array-increasing │ ├── README.md │ └── minimum-operations-to-make-the-array-increasing.cpp ├── minimum-path-sum │ ├── README.md │ └── minimum-path-sum.cpp ├── minimum-remove-to-make-valid-parentheses │ ├── README.md │ └── minimum-remove-to-make-valid-parentheses.cpp ├── minimum-size-subarray-sum │ ├── README.md │ └── minimum-size-subarray-sum.cpp ├── minimum-subsequence-in-non-increasing-order │ ├── README.md │ └── minimum-subsequence-in-non-increasing-order.cpp ├── minimum-window-substring │ ├── README.md │ └── minimum-window-substring.cpp ├── most-frequent-subtree-sum │ ├── README.md │ └── most-frequent-subtree-sum.cpp ├── move-zeroes │ ├── README.md │ └── move-zeroes.cpp ├── multiply-strings │ ├── README.md │ └── multiply-strings.cpp ├── n-ary-tree-level-order-traversal │ ├── README.md │ └── n-ary-tree-level-order-traversal.cpp ├── n-ary-tree-postorder-traversal │ ├── README.md │ └── n-ary-tree-postorder-traversal.cpp ├── n-queens-ii │ ├── README.md │ └── n-queens-ii.cpp ├── n-queens │ ├── README.md │ └── n-queens.cpp ├── n-repeated-element-in-size-2n-array │ ├── README.md │ └── n-repeated-element-in-size-2n-array.cpp ├── n-th-tribonacci-number │ ├── README.md │ └── n-th-tribonacci-number.cpp ├── network-delay-time │ ├── README.md │ └── network-delay-time.cpp ├── next-permutation │ ├── README.md │ └── next-permutation.cpp ├── nim-game │ ├── README.md │ └── nim-game.cpp ├── nth-highest-salary │ ├── README.md │ └── nth-highest-salary.sql ├── number-of-1-bits │ ├── README.md │ └── number-of-1-bits.cpp ├── number-of-different-integers-in-a-string │ ├── README.md │ └── number-of-different-integers-in-a-string.cpp ├── number-of-good-pairs │ ├── README.md │ └── number-of-good-pairs.cpp ├── number-of-islands │ └── README.md ├── number-of-longest-increasing-subsequence │ ├── README.md │ └── number-of-longest-increasing-subsequence.cpp ├── number-of-nodes-in-the-sub-tree-with-the-same-label │ ├── README.md │ └── number-of-nodes-in-the-sub-tree-with-the-same-label.cpp ├── number-of-operations-to-make-network-connected │ ├── README.md │ └── number-of-operations-to-make-network-connected.cpp ├── number-of-provinces │ ├── README.md │ └── number-of-provinces.cpp ├── number-of-rectangles-that-can-form-the-largest-square │ ├── README.md │ └── number-of-rectangles-that-can-form-the-largest-square.cpp ├── number-of-students-doing-homework-at-a-given-time │ ├── README.md │ └── number-of-students-doing-homework-at-a-given-time.cpp ├── occurrences-after-bigram │ ├── README.md │ └── occurrences-after-bigram.cpp ├── odd-even-linked-list │ ├── README.md │ └── odd-even-linked-list.cpp ├── ones-and-zeroes │ ├── README.md │ └── ones-and-zeroes.cpp ├── palindrome-linked-list │ ├── README.md │ └── palindrome-linked-list.cpp ├── palindrome-number │ ├── README.md │ └── palindrome-number.cpp ├── palindrome-partitioning-ii │ ├── README.md │ └── palindrome-partitioning-ii.cpp ├── partition-array-into-three-parts-with-equal-sum │ ├── README.md │ └── partition-array-into-three-parts-with-equal-sum.cpp ├── partition-equal-subset-sum │ ├── README.md │ └── partition-equal-subset-sum.cpp ├── partition-list │ ├── README.md │ └── partition-list.cpp ├── pascals-triangle │ ├── README.md │ └── pascals-triangle.cpp ├── path-sum-iii │ ├── README.md │ └── path-sum-iii.cpp ├── path-sum │ ├── README.md │ └── path-sum.cpp ├── path-with-maximum-gold │ ├── README.md │ └── path-with-maximum-gold.cpp ├── peak-index-in-a-mountain-array │ ├── README.md │ └── peak-index-in-a-mountain-array.cpp ├── permutation-in-string │ ├── README.md │ └── permutation-in-string.cpp ├── permutations-ii │ ├── README.md │ └── permutations-ii.cpp ├── permutations │ ├── README.md │ └── permutations.cpp ├── plus-one │ ├── README.md │ └── plus-one.cpp ├── populating-next-right-pointers-in-each-node-ii │ ├── README.md │ └── populating-next-right-pointers-in-each-node-ii.cpp ├── populating-next-right-pointers-in-each-node │ ├── README.md │ └── populating-next-right-pointers-in-each-node.cpp ├── possible-bipartition │ ├── README.md │ └── possible-bipartition.cpp ├── power-of-four │ ├── README.md │ └── power-of-four.cpp ├── power-of-three │ ├── README.md │ └── power-of-three.cpp ├── power-of-two │ ├── README.md │ └── power-of-two.cpp ├── powx-n │ ├── README.md │ └── powx-n.cpp ├── print-binary-tree │ ├── README.md │ └── print-binary-tree.cpp ├── range-sum-of-bst │ ├── README.md │ └── range-sum-of-bst.cpp ├── range-sum-query-mutable │ ├── README.md │ └── range-sum-query-mutable.py ├── rearrange-words-in-a-sentence │ ├── README.md │ └── rearrange-words-in-a-sentence.cpp ├── recover-binary-search-tree │ ├── README.md │ └── recover-binary-search-tree.cpp ├── rectangle-area │ ├── README.md │ └── rectangle-area.cpp ├── rectangle-overlap │ ├── README.md │ └── rectangle-overlap.cpp ├── redundant-connection-ii │ ├── README.md │ └── redundant-connection-ii.cpp ├── redundant-connection │ ├── README.md │ └── redundant-connection.cpp ├── reformat-date │ ├── README.md │ └── reformat-date.cpp ├── regular-expression-matching │ ├── README.md │ └── regular-expression-matching.cpp ├── remove-all-adjacent-duplicates-in-string │ ├── README.md │ └── remove-all-adjacent-duplicates-in-string.cpp ├── remove-all-occurrences-of-a-substring │ ├── README.md │ └── remove-all-occurrences-of-a-substring.cpp ├── remove-comments │ ├── README.md │ └── remove-comments.cpp ├── remove-duplicate-letters │ └── README.md ├── remove-duplicates-from-sorted-array-ii │ ├── README.md │ └── remove-duplicates-from-sorted-array-ii.cpp ├── remove-duplicates-from-sorted-array │ ├── README.md │ └── remove-duplicates-from-sorted-array.cpp ├── remove-duplicates-from-sorted-list-ii │ ├── README.md │ └── remove-duplicates-from-sorted-list-ii.cpp ├── remove-duplicates-from-sorted-list │ ├── README.md │ └── remove-duplicates-from-sorted-list.cpp ├── remove-element │ └── README.md ├── remove-invalid-parentheses │ ├── README.md │ └── remove-invalid-parentheses.cpp ├── remove-linked-list-elements │ ├── README.md │ └── remove-linked-list-elements.cpp ├── remove-nth-node-from-end-of-list │ ├── README.md │ └── remove-nth-node-from-end-of-list.cpp ├── remove-one-element-to-make-the-array-strictly-increasing │ ├── README.md │ └── remove-one-element-to-make-the-array-strictly-increasing.cpp ├── remove-sub-folders-from-the-filesystem │ ├── README.md │ └── remove-sub-folders-from-the-filesystem.cpp ├── remove-zero-sum-consecutive-nodes-from-linked-list │ ├── README.md │ └── remove-zero-sum-consecutive-nodes-from-linked-list.cpp ├── reorder-list │ ├── README.md │ └── reorder-list.cpp ├── reorganize-string │ ├── README.md │ └── reorganize-string.cpp ├── reshape-the-matrix │ ├── README.md │ └── reshape-the-matrix.cpp ├── reverse-linked-list-ii │ ├── README.md │ └── reverse-linked-list-ii.cpp ├── reverse-linked-list │ ├── README.md │ └── reverse-linked-list.cpp ├── reverse-nodes-in-k-group │ ├── README.md │ └── reverse-nodes-in-k-group.cpp ├── reverse-string-ii │ ├── README.md │ └── reverse-string-ii.cpp ├── reverse-words-in-a-string-iii │ ├── README.md │ └── reverse-words-in-a-string-iii.cpp ├── reverse-words-in-a-string │ ├── README.md │ └── reverse-words-in-a-string.cpp ├── richest-customer-wealth │ ├── README.md │ └── richest-customer-wealth.cpp ├── robot-return-to-origin │ ├── README.md │ └── robot-return-to-origin.cpp ├── rotate-image │ ├── README.md │ └── rotate-image.cpp ├── rotate-list │ ├── README.md │ └── rotate-list.cpp ├── rotting-oranges │ ├── README.md │ └── rotting-oranges.cpp ├── running-sum-of-1d-array │ ├── README.md │ └── running-sum-of-1d-array.cpp ├── same-tree │ ├── README.md │ └── same-tree.cpp ├── score-of-parentheses │ ├── README.md │ └── score-of-parentheses.cpp ├── scramble-string │ ├── README.md │ └── scramble-string.cpp ├── search-a-2d-matrix │ ├── README.md │ └── search-a-2d-matrix.cpp ├── search-in-a-binary-search-tree │ ├── README.md │ └── search-in-a-binary-search-tree.cpp ├── search-in-rotated-sorted-array-ii │ ├── README.md │ └── search-in-rotated-sorted-array-ii.cpp ├── search-in-rotated-sorted-array │ ├── README.md │ └── search-in-rotated-sorted-array.cpp ├── search-insert-position │ ├── README.md │ └── search-insert-position.cpp ├── second-highest-salary │ ├── README.md │ └── second-highest-salary.sql ├── second-minimum-node-in-a-binary-tree │ ├── README.md │ └── second-minimum-node-in-a-binary-tree.cpp ├── serialize-and-deserialize-binary-tree │ ├── README.md │ └── serialize-and-deserialize-binary-tree.cpp ├── serialize-and-deserialize-bst │ ├── README.md │ └── serialize-and-deserialize-bst.cpp ├── set-matrix-zeroes │ ├── README.md │ └── set-matrix-zeroes.cpp ├── set-mismatch │ ├── README.md │ └── set-mismatch.cpp ├── shift-2d-grid │ ├── README.md │ └── shift-2d-grid.cpp ├── shortest-common-supersequence │ ├── README.md │ └── shortest-common-supersequence.cpp ├── shortest-path-in-binary-matrix │ ├── README.md │ └── shortest-path-in-binary-matrix.cpp ├── shortest-unsorted-continuous-subarray │ ├── README.md │ └── shortest-unsorted-continuous-subarray.cpp ├── shuffle-the-array │ ├── README.md │ └── shuffle-the-array.cpp ├── simplify-path │ ├── README.md │ └── simplify-path.cpp ├── single-number-ii │ ├── README.md │ └── single-number-ii.cpp ├── single-number-iii │ ├── README.md │ └── single-number-iii.cpp ├── single-number │ ├── README.md │ └── single-number.cpp ├── smallest-range-ii │ ├── README.md │ └── smallest-range-ii.cpp ├── smallest-subsequence-of-distinct-characters │ ├── README.md │ └── smallest-subsequence-of-distinct-characters.cpp ├── smallest-subtree-with-all-the-deepest-nodes │ ├── README.md │ └── smallest-subtree-with-all-the-deepest-nodes.cpp ├── snakes-and-ladders │ ├── README.md │ └── snakes-and-ladders.cpp ├── sort-an-array │ ├── README.md │ └── sort-an-array.cpp ├── sort-colors │ ├── README.md │ └── sort-colors.cpp ├── sort-integers-by-the-number-of-1-bits │ ├── README.md │ └── sort-integers-by-the-number-of-1-bits.cpp ├── sort-list │ ├── README.md │ └── sort-list.cpp ├── spiral-matrix-ii │ ├── README.md │ └── spiral-matrix-ii.cpp ├── spiral-matrix │ ├── README.md │ └── spiral-matrix.cpp ├── split-a-string-in-balanced-strings │ ├── README.md │ └── split-a-string-in-balanced-strings.cpp ├── split-linked-list-in-parts │ ├── README.md │ └── split-linked-list-in-parts.cpp ├── sqrtx │ ├── README.md │ └── sqrtx.cpp ├── string-to-integer-atoi │ ├── README.md │ └── string-to-integer-atoi.cpp ├── subarray-sum-equals-k │ ├── README.md │ └── subarray-sum-equals-k.cpp ├── subsets-ii │ ├── README.md │ └── subsets-ii.cpp ├── subsets │ ├── README.md │ └── subsets.cpp ├── subtree-of-another-tree │ ├── README.md │ └── subtree-of-another-tree.cpp ├── sudoku-solver │ ├── README.md │ └── sudoku-solver.cpp ├── sum-of-digits-in-base-k │ ├── README.md │ └── sum-of-digits-in-base-k.cpp ├── sum-of-left-leaves │ ├── README.md │ └── sum-of-left-leaves.cpp ├── sum-of-root-to-leaf-binary-numbers │ ├── README.md │ └── sum-of-root-to-leaf-binary-numbers.cpp ├── sum-of-subarray-minimums │ ├── README.md │ └── sum-of-subarray-minimums.cpp ├── sum-of-two-integers │ ├── README.md │ └── sum-of-two-integers.java ├── sum-of-unique-elements │ ├── README.md │ └── sum-of-unique-elements.cpp ├── sum-root-to-leaf-numbers │ ├── README.md │ └── sum-root-to-leaf-numbers.cpp ├── summary-ranges │ ├── README.md │ └── summary-ranges.cpp ├── super-egg-drop │ ├── README.md │ └── super-egg-drop.cpp ├── surface-area-of-3d-shapes │ ├── README.md │ └── surface-area-of-3d-shapes.cpp ├── swap-nodes-in-pairs │ ├── README.md │ └── swap-nodes-in-pairs.cpp ├── swapping-nodes-in-a-linked-list │ ├── README.md │ └── swapping-nodes-in-a-linked-list.cpp ├── symmetric-tree │ ├── README.md │ └── symmetric-tree.cpp ├── target-sum │ ├── README.md │ └── target-sum.cpp ├── task-scheduler │ ├── README.md │ └── task-scheduler.cpp ├── teemo-attacking │ ├── README.md │ └── teemo-attacking.cpp ├── third-maximum-number │ ├── README.md │ └── third-maximum-number.cpp ├── three-equal-parts │ ├── README.md │ └── three-equal-parts.cpp ├── trapping-rain-water │ ├── README.md │ └── trapping-rain-water.cpp ├── triangle │ ├── README.md │ └── triangle.cpp ├── trim-a-binary-search-tree │ ├── README.md │ └── trim-a-binary-search-tree.cpp ├── two-sum-ii-input-array-is-sorted │ ├── README.md │ └── two-sum-ii-input-array-is-sorted.cpp ├── two-sum │ ├── README.md │ └── two-sum.cpp ├── ugly-number-ii │ ├── README.md │ └── ugly-number-ii.cpp ├── ugly-number │ ├── README.md │ └── ugly-number.cpp ├── uncommon-words-from-two-sentences │ ├── README.md │ └── uncommon-words-from-two-sentences.cpp ├── unique-binary-search-trees │ ├── README.md │ └── unique-binary-search-trees.cpp ├── unique-morse-code-words │ ├── README.md │ └── unique-morse-code-words.cpp ├── unique-paths-ii │ ├── README.md │ └── unique-paths-ii.cpp ├── unique-paths-iii │ ├── README.md │ └── unique-paths-iii.cpp ├── unique-paths │ ├── README.md │ └── unique-paths.cpp ├── univalued-binary-tree │ ├── README.md │ └── univalued-binary-tree.cpp ├── valid-anagram │ └── README.md ├── valid-palindrome-ii │ ├── README.md │ └── valid-palindrome-ii.cpp ├── valid-palindrome │ ├── README.md │ └── valid-palindrome.cpp ├── valid-parentheses │ ├── README.md │ └── valid-parentheses.cpp ├── valid-parenthesis-string │ ├── README.md │ └── valid-parenthesis-string.cpp ├── valid-perfect-square │ ├── README.md │ └── valid-perfect-square.cpp ├── valid-sudoku │ ├── README.md │ └── valid-sudoku.cpp ├── validate-binary-search-tree │ ├── README.md │ └── validate-binary-search-tree.cpp ├── validate-binary-tree-nodes │ └── README.md ├── verifying-an-alien-dictionary │ ├── README.md │ └── verifying-an-alien-dictionary.cpp ├── vertical-order-traversal-of-a-binary-tree │ ├── README.md │ └── vertical-order-traversal-of-a-binary-tree.cpp ├── walking-robot-simulation │ ├── README.md │ └── walking-robot-simulation.cpp ├── water-bottles │ ├── README.md │ └── water-bottles.cpp ├── wildcard-matching │ ├── README.md │ └── wildcard-matching.cpp ├── word-break │ ├── README.md │ └── word-break.cpp ├── word-ladder │ ├── README.md │ └── word-ladder.cpp ├── word-search │ ├── README.md │ └── word-search.cpp └── word-subsets │ ├── README.md │ └── word-subsets.cpp ├── README.md ├── array ├── 012sort │ ├── a.exe │ └── main.cpp ├── 3Sum │ └── main.cpp ├── alternateNegPositive │ ├── a.exe │ └── main.cpp ├── bestTimeToBuyStock │ └── main.cpp ├── bigFactorial │ └── main.cpp ├── binaryArraySort │ └── main.cpp ├── chocolateDist │ └── main.cpp ├── commonElements │ ├── a.exe │ ├── main.cpp │ └── main.exe ├── countElements_n_k_times │ └── main.cpp ├── countInversions │ └── main.cpp ├── countPairsSum │ └── main.cpp ├── cyclicRotation │ ├── a.exe │ └── main.cpp ├── duplicateNumbers │ ├── a.exe │ └── main.cpp ├── kadanesAlgo │ ├── a.exe │ └── main.cpp ├── kthSmallestElement │ ├── a.exe │ └── main.cpp ├── longestConsecutiveSubsequence │ └── main.cpp ├── makeTwoArraysEqual │ ├── a.exe │ ├── main.cpp │ └── main.exe ├── maxProfit │ └── main.cpp ├── maximumProductSubarray │ └── main.cpp ├── mergeIntervals │ └── main.cpp ├── mergeSort │ └── main.cpp ├── mergeSortedArrays │ ├── a.exe │ └── main.cpp ├── minJumps │ └── main.cpp ├── minMaxArray │ ├── a.exe │ └── main.cpp ├── minSwap │ └── main.cpp ├── minimizeHeights │ ├── a.exe │ └── main.cpp ├── moreThanN │ ├── a.exe │ └── main.cpp ├── moveNegative │ ├── a.exe │ ├── constantSpace.cpp │ └── main.cpp ├── nextPermutation │ └── main.cpp ├── rearrangeNegativePositive │ ├── a.exe │ └── main.cpp ├── reverseArray │ ├── a.exe │ └── main.cpp ├── smallesSubArrayWithSUmGreaterThanTarget │ └── main.cpp ├── stocks2buy │ ├── a.exe │ └── main.cpp ├── subarraySUm │ └── main.cpp ├── subarrayWithSum0 │ └── main.cpp ├── threeWayPartition │ └── main.cpp ├── toolBox │ ├── a.exe │ └── main.cpp ├── trappingRainWater │ └── main.cpp ├── unionIntersection │ ├── a.exe │ └── main.cpp └── waterJugMaths │ ├── a.exe │ └── main.cpp ├── backtracking ├── kinghtTour │ └── main.cpp ├── main.cpp ├── main.exe └── pathMaximumGold │ └── main.cpp ├── bit_manipulation └── swapNibble │ └── main.cpp ├── graphs ├── BFS │ ├── a.exe │ └── main.cpp ├── DFS │ └── main.cpp ├── OliverAndTheGame │ ├── a.exe │ ├── mainc.cpp │ └── mainc.exe ├── bipartite │ ├── a.exe │ └── main.cpp ├── bridgeInAgraph │ ├── a.exe │ └── main.cpp ├── canFinishJob │ ├── a.exe │ └── main.cpp ├── creatingGraphs │ ├── a.exe │ ├── matrix.cpp │ └── vector.cpp ├── dijkstra │ ├── a.exe │ └── main.cpp ├── distancePathK │ ├── a.exe │ └── main.cpp ├── kruskalMST │ └── main.cpp ├── longestPathAcyclic │ ├── a.exe │ ├── main.cpp │ └── main.exe ├── mColoring │ └── main.cpp ├── minTimeForEachJob │ ├── a.exe │ └── main.cpp ├── pathToTravel │ └── main.cpp ├── prismMininumSpanning │ ├── a.exe │ └── main.cpp ├── reverseToMakePath │ ├── a.exe │ └── main.cpp ├── toTheMoon │ ├── main..cpp │ └── main..exe ├── unionFind │ ├── a.exe │ └── main.cpp └── waterJug │ ├── a.exe │ └── main.cpp ├── greedy ├── islandSurvival │ ├── a.exe │ └── main.cpp ├── maximumAbsolute │ ├── a.exe │ └── main.cpp ├── meetingOneRoom │ ├── a.exe │ └── main.cpp ├── minimumSumPair │ ├── a.exe │ └── main.cpp └── smallesSubSet │ ├── a.exe │ └── main.cpp ├── linkedList ├── RotateLinkedList │ └── main.cpp ├── add_2_numbers_LL │ ├── AddTwoNumbersInLL.java │ └── add2NumbersInLL.md ├── cycleDetectionInALinkedList │ ├── FloydCycleDetection.md │ └── main.cpp ├── deleteWithoutHead │ └── main.cpp ├── findYinsertion │ └── main.cpp ├── mergeSort │ └── main.cpp ├── merge_two_sorted_LL │ └── main.cpp └── remove_reverse_list │ └── main.cpp ├── matrix ├── a.exe └── spiral.cpp ├── misc ├── a.exe ├── articulationPoint │ ├── a.exe │ └── main.cpp ├── awesomeSort │ ├── a.exe │ └── main.cpp ├── breakTheNode │ ├── a.exe │ ├── main.cpp │ └── main.exe ├── decimalToBinary.cpp ├── fillArray │ ├── a.exe │ └── main.cpp ├── impartialOffering │ ├── a.exe │ └── main.cpp ├── inconstantOdering │ ├── a.exe │ └── main.cpp ├── peakSum │ ├── a.exe │ └── main.cpp ├── rushab │ ├── first │ │ ├── a.exe │ │ └── main.cpp │ ├── second │ │ └── main.cpp │ └── third │ │ ├── a.exe │ │ └── main.cpp └── timeComplex │ ├── a.exe │ ├── main.class │ ├── main.cpp │ └── main.java ├── recursion └── towerOfHanoi │ └── main.cpp ├── searchingAndSorting ├── findPair │ └── FindPair.java ├── firstAndLastocc │ ├── Occurences.java │ ├── a.exe │ ├── main.cpp │ └── main.exe ├── mergeWithoutSpace │ ├── MergeWithoutSpace.java │ └── main.cpp ├── searchAdjacentK │ └── AdjacentDiffer.java └── sqrtOfNumber │ └── main.cpp ├── stacks&queues ├── StackUsing2queue │ └── main.cpp └── queueUsing2Stack │ └── main.cpp ├── string ├── KMP │ ├── kmp.class │ └── kmp.java ├── LongestRepeatingSubsequence │ └── main.cpp ├── binaryStringSplit │ ├── a.exe │ └── main.cpp ├── boyerMoore │ ├── boyerMoore.class │ └── boyerMoore.java ├── computerCafe │ ├── a.exe │ └── main.cpp ├── countReversals │ ├── CountReversals.class │ └── CountReversals.java ├── editDistance │ └── main.cpp ├── ipAddresses │ ├── a.exe │ └── main.cpp ├── longestPrefixSufix │ └── longestPrefix.java ├── minCharToBeAddedFrontPalindrome │ ├── a.exe │ └── main.cpp ├── numberPad │ ├── Numberpad.class │ └── Numberpad.java ├── palindromeCount │ ├── countPalindrome.class │ └── countPalindrome.java ├── rabinKarp │ ├── rabinKarp.class │ └── rabinKarp.java ├── subsequencesOfString │ ├── a.exe │ └── main.cpp ├── wildCardMatchin │ └── main.cpp └── wordWrap │ ├── a.exe │ └── main.cpp └── tree ├── ConnectNodesAtSameLevel └── main.cpp ├── TreeOrNot └── main.cpp ├── bottomView └── main.cpp ├── boundaryView └── main.cpp ├── bracketsToTree ├── a.exe └── main.cpp ├── diagonalTraversal ├── a.exe └── main.cpp ├── findGoodNodes └── main.cpp ├── findIntervals ├── a.exe └── main.cpp ├── findLargestSubtreeSum ├── a.exe └── main.cpp ├── linkedListinBinaryTree └── main.cpp ├── maxPathSumFromLeafNode2LeafNode └── main.cpp ├── maxiMumPathSumNode2Node └── main.cpp ├── sumTree └── main.cpp ├── sum_between_leaf_nodes └── main.cpp └── verticalTraversal └── main.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | *.xlsx 2 | FINAL450.xlsx 3 | .vscode/* 4 | .vscode 5 | *.code-workspace 6 | *.exe 7 | 8 | # Local History for Visual Studio Code 9 | .history/ -------------------------------------------------------------------------------- /DP/MCMFormat/main.cpp: -------------------------------------------------------------------------------- 1 | int solve(int arr[],int i,int j){ 2 | if(i>j){ 3 | return 0; 4 | } 5 | 6 | for(int k=i;k 2 | 3 | using namespace std; 4 | 5 | unsigned long int solve(unsigned int n) 6 | { 7 | unsigned long int catalan[n + 1]; 8 | 9 | // Initialize first two values in table 10 | catalan[0] = catalan[1] = 1; 11 | 12 | for (int i = 2; i <= n; i++) 13 | { 14 | catalan[i] = 0; 15 | for (int j = 0; j < i; j++) 16 | catalan[i] += catalan[j] * catalan[i - j - 1]; 17 | } 18 | 19 | return catalan[n]; 20 | } 21 | 22 | int main() 23 | { 24 | int n; 25 | cin >> n; 26 | for (unsigned int i = 0; i < n; i++) 27 | { 28 | cout < 2 | 3 | using namespace std; 4 | 5 | int t[1001][1001]; //depends on the constrains given; 6 | 7 | int solve(vector nums, int i, int j) 8 | { 9 | if (i == j) 10 | { 11 | return 0; 12 | } 13 | 14 | if (t[i][j] != -1) 15 | return t[i][j]; 16 | 17 | int minAns = INT_MAX; 18 | 19 | for (int k = i; k < j; k++) 20 | { 21 | minAns = min(minAns, solve(nums, i, k) + solve(nums, k + 1, j) + nums[i - 1] * nums[k] * nums[j]); 22 | } 23 | 24 | return t[i][j] = minAns; 25 | } 26 | 27 | int main() 28 | { 29 | 30 | memset(t, -1, sizeof(t)); 31 | vector arr={1,2,3,4}; 32 | int n = arr.size(); 33 | cout << solve(arr, 1, n - 1); 34 | 35 | return 0; 36 | } -------------------------------------------------------------------------------- /DP/maxCutSegments/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/DP/maxCutSegments/a.exe -------------------------------------------------------------------------------- /DP/maxCutSegments/main.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/DP/maxCutSegments/main.exe -------------------------------------------------------------------------------- /DP/minimumNumberOfCoins/memo.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> t; 4 | int solve(vector coins,int amount,int n){ 5 | if(amount==0){ 6 | return 0; 7 | } 8 | if(amount<0 || n<0){ 9 | return 10000; 10 | } 11 | 12 | if(t[n][amount]!=-1){ 13 | return t[n][amount]; 14 | } 15 | 16 | t[n][amount]= min(1+solve(coins,amount-coins[n],n),solve(coins,amount,n-1)); 17 | 18 | 19 | return t[n][amount]; 20 | 21 | } 22 | int coinChange(vector& coins, int amount) { 23 | int n = coins.size(); 24 | t.assign(n+1,vector (amount+1,-1)); 25 | int ans = solve(coins,amount,n-1); 26 | if(ans>9000){ 27 | return -1; 28 | } 29 | return ans; 30 | } 31 | }; -------------------------------------------------------------------------------- /DP/minimumSumOfSubarray/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/DP/minimumSumOfSubarray/a.exe -------------------------------------------------------------------------------- /DP/minimumSumOfSubarray/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int solve(vector arr) 6 | { 7 | int minSoFar = INT_MAX; 8 | int temp = 0; 9 | for (auto a : arr) 10 | { 11 | if (temp > 0) 12 | { 13 | temp = a; 14 | } 15 | else 16 | { 17 | 18 | temp += a; 19 | } 20 | minSoFar = min(temp, minSoFar); 21 | } 22 | 23 | return minSoFar; 24 | } 25 | 26 | int main() 27 | { 28 | vector arr = {2, 6, 8, 1, 4}; 29 | 30 | cout << solve(arr) << endl; 31 | 32 | return 0; 33 | } -------------------------------------------------------------------------------- /DP/nCr/main.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/DP/nCr/main.exe -------------------------------------------------------------------------------- /DP/numOfWaysToWinAGame/main.cpp: -------------------------------------------------------------------------------- 1 | // { Driver Code Starts 2 | // Driver Code 3 | #include 4 | #define ll long long int 5 | using namespace std; 6 | 7 | // } Driver Code Ends 8 | // Complete this function 9 | long long int count(long long int n) 10 | { 11 | long long int table[n+1],i; 12 | memset(table, 0, sizeof(table)); 13 | table[0]=1; // If 0 sum is required number of ways is 1. 14 | 15 | for(int i=3;i<=n;i++){ 16 | table[i]+=table[i-3]; 17 | } 18 | for(int i=5;i<=n;i++){ 19 | table[i]+=table[i-5]; 20 | } 21 | for(int i=10;i<=n;i++){ 22 | table[i]+=table[i-10]; 23 | } 24 | // Your code here 25 | 26 | return table[n]; 27 | } 28 | 29 | // { Driver Code Starts. 30 | 31 | 32 | 33 | int main() 34 | { 35 | int t; 36 | cin>>t; 37 | while(t--) 38 | { 39 | ll n; 40 | cin>>n; 41 | cout< 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | // } Driver Code Ends 8 | 9 | class Solution 10 | { 11 | public: 12 | long long countWays(int n, int k) 13 | { 14 | // code here 15 | long long t[n + 1]; 16 | long long mod = 1000000007; 17 | 18 | t[1] = k; 19 | t[2] = k * k; 20 | for (int i = 3; i <= n; i++) 21 | { 22 | t[i] = (k - 1) * (t[i - 1] + t[i - 2]) % mod; 23 | } 24 | 25 | return t[n]; 26 | } 27 | }; 28 | 29 | // { Driver Code Starts. 30 | 31 | int main() 32 | { 33 | 34 | int t; 35 | cin >> t; 36 | while (t--) 37 | { 38 | int n, k; 39 | cin >> n >> k; 40 | Solution ob; 41 | cout << ob.countWays(n, k) << endl; 42 | } 43 | return 0; 44 | } // } Driver Code Ends -------------------------------------------------------------------------------- /DP/subsetSumCount/main.cpp: -------------------------------------------------------------------------------- 1 | int subsetSumCount(vector arr,int sum){ 2 | 3 | int n = arr.size(); 4 | 5 | int t[n+1][sum+1]; 6 | 7 | for(int i=0;i> t(n+1,vector(m+1,false)); 8 | 9 | t[0][0]=true; 10 | 11 | for(int j=0;j& bits) { 4 | for(int i=0;i& nums) { 4 | int third=INT_MIN; 5 | stack s; 6 | 7 | for(int i=nums.size()-1;i>-1;i--){ 8 | if(nums[i]s.top()){ 10 | third = s.top(); 11 | s.pop(); 12 | } 13 | 14 | s.push(nums[i]); 15 | 16 | } 17 | 18 | return false; 19 | } 20 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/3sum-closest/3sum-closest.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int threeSumClosest(vector& nums, int target) { 4 | sort(nums.begin(),nums.end()); 5 | int minDist =INT_MAX; 6 | int ans=0; 7 | for(int i=0;itarget){ 19 | right--; 20 | }else{ 21 | left++; 22 | } 23 | } 24 | } 25 | 26 | return ans; 27 | } 28 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/README.md: -------------------------------------------------------------------------------- 1 | # Leet-Code-Solutions 2 | Collection of LeetCode questions to ace the coding interview! - Created using [LeetHub](https://github.com/QasimWani/LeetHub). 3 | -------------------------------------------------------------------------------- /Leet-Code-Question/add-binary/README.md: -------------------------------------------------------------------------------- 1 |

67. Add Binary

Easy


Given two binary strings a and b, return their sum as a binary string.

2 | 3 |

 

4 |

Example 1:

5 |
Input: a = "11", b = "1"
 6 | Output: "100"
 7 | 

Example 2:

8 |
Input: a = "1010", b = "1011"
 9 | Output: "10101"
10 | 
11 |

 

12 |

Constraints:

13 | 14 |
    15 |
  • 1 <= a.length, b.length <= 104
  • 16 |
  • a and b consist only of '0' or '1' characters.
  • 17 |
  • Each string does not contain leading zeros except for the zero itself.
  • 18 |
19 |
-------------------------------------------------------------------------------- /Leet-Code-Question/add-binary/add-binary.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def bin2dec(self,a): 3 | val = 0 4 | for i in range(len(a)): 5 | num = (int)(a[len(a)-i-1]) 6 | val+= num*(2**i) 7 | return val 8 | def dec2bin(self,div): 9 | carry=[] 10 | if div==0: 11 | return "0" 12 | while div>0: 13 | carry.append(str(div%2)) 14 | div = div//2 15 | return "".join(carry[::-1]) 16 | def addBinary(self, a: str, b: str) -> str: 17 | sumdec = self.bin2dec(a) + self.bin2dec(b) 18 | return self.dec2bin(sumdec) -------------------------------------------------------------------------------- /Leet-Code-Question/add-digits/add-digits.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int addDigits(int num) { 4 | int result=0,temp; 5 | if(num/10==0){ 6 | return num; 7 | }else{ 8 | 9 | while(num!=0){ 10 | temp=num%10; 11 | result+=temp; 12 | num=num/10; 13 | } 14 | return addDigits(result); 15 | } 16 | } 17 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/all-paths-from-source-to-target/all-paths-from-source-to-target.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> ans; 4 | int e; 5 | vector temp; 6 | void DFS(vector> &graph,int v=0){ 7 | 8 | 9 | temp.push_back(v); 10 | if(v==e){ 11 | ans.push_back(temp); 12 | } 13 | else for(auto u:graph[v]){ 14 | DFS(graph,u); 15 | } 16 | temp.pop_back(); 17 | } 18 | vector> allPathsSourceTarget(vector>& graph) { 19 | int n = graph.size(); 20 | e=n-1; 21 | DFS(graph); 22 | 23 | return ans; 24 | 25 | 26 | 27 | } 28 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/arranging-coins/arranging-coins.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int arrangeCoins(int n) { 4 | int stair=0; 5 | int numberOfStairs=1; 6 | while(n){ 7 | if(n>=numberOfStairs){ 8 | n=n-stair; 9 | stair++; 10 | numberOfStairs++; 11 | } 12 | n--; 13 | } 14 | 15 | return stair; 16 | } 17 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/array-partition-i/array-partition-i.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int arrayPairSum(vector& nums) { 4 | 5 | sort(nums.begin(),nums.end()); 6 | int numOfPairs= nums.size()/2; 7 | int l=0; 8 | int result=0; 9 | while(l0){ 10 | result+= min(nums[l],nums[l+1]); 11 | l=l+2; 12 | } 13 | 14 | return result; 15 | 16 | } 17 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/assign-cookies/assign-cookies.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findContentChildren(vector& g, vector& s) { 4 | int i=0,j=0; 5 | int count=0; 6 | sort(g.begin(),g.end()); 7 | sort(s.begin(),s.end()); 8 | while(i& salary) { 4 | double maxV=INT_MIN,minV=INT_MAX; 5 | double sum = 0; 6 | 7 | for(auto a:salary){ 8 | sum+=a; 9 | if(maxVa) minV=a; 11 | } 12 | 13 | // cout< test; 5 | for(auto a:s){ 6 | if(a=='#'){ 7 | if(test.size()>0){ 8 | test.pop(); 9 | } 10 | }else{ 11 | test.push(a); 12 | } 13 | } 14 | string ans = ""; 15 | while(test.size()>0){ 16 | ans = test.top()+ans; 17 | test.pop(); 18 | } 19 | return ans; 20 | 21 | } 22 | 23 | bool backspaceCompare(string s, string t) { 24 | s = useStack(s); 25 | t=useStack(t); 26 | 27 | // cout<& prices) { 4 | int total=0; 5 | for(int i=1;i0){ 8 | total+=sub; 9 | } 10 | } 11 | 12 | 13 | return total; 14 | } 15 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/best-time-to-buy-and-sell-stock-iii/best-time-to-buy-and-sell-stock-iii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProfit(vector& prices) { 4 | 5 | int n = prices.size(); 6 | vector profit(prices.size(),0); 7 | 8 | int max_price = prices[n-1]; 9 | 10 | for(int i=n-2;i>=0;i--){ 11 | if(prices[i]>max_price) 12 | max_price=prices[i]; 13 | 14 | 15 | profit[i]=max(profit[i+1],max_price-prices[i]); 16 | } 17 | 18 | int min_price=prices[0]; 19 | for(int i=1;i& prices) { 4 | if(prices.size()==0) return 0; 5 | int pS = prices.size(); 6 | 7 | vector> t(k+1,vector (pS,0)); 8 | 9 | for(int i=1;i& prices) { 4 | int profit=0; 5 | int buy=prices[0]; 6 | int sell = prices[0]; 7 | 8 | for(int i=0;i& nums, int target) { 4 | int l=0, r= nums.size()-1; 5 | 6 | while(l<=r){ 7 | int mid = l+(r-l)/2; 8 | if(nums[mid]==target){ 9 | return mid; 10 | }else{ 11 | if(nums[mid] ans; 15 | void solve(TreeNode* root){ 16 | if(!root) return; 17 | 18 | solve(root->left); 19 | ans.push_back(root->val); 20 | solve(root->right); 21 | } 22 | vector inorderTraversal(TreeNode* root) { 23 | solve(root); 24 | 25 | return ans; 26 | } 27 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/build-an-array-with-stack-operations/build-an-array-with-stack-operations.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector buildArray(vector& target, int n) { 4 | stack s; 5 | int j=0; 6 | vector ans; 7 | for(int i=0;i& nums,int i,int j){ 5 | if(i==j){ 6 | return 0; 7 | } 8 | if(t[i][j]!=-1) return t[i][j]; 9 | 10 | int minAns=INT_MIN; 11 | int tempAns=0; 12 | for(int k=i;k& nums) { 22 | nums.insert(nums.begin()+0,1); 23 | nums.push_back(1); 24 | memset(t,-1,sizeof(t)); 25 | 26 | return solve(nums,1,nums.size()-1); 27 | } 28 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/can-make-arithmetic-progression-from-sequence/can-make-arithmetic-progression-from-sequence.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canMakeArithmeticProgression(vector& arr) { 4 | sort(arr.begin(),arr.end()); 5 | int d = arr[1]-arr[0]; 6 | for(int i=1;i& flowerbed, int n) { 4 | int count=0; 5 | for(int i=0;i=n; 19 | } 20 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/cheapest-flights-within-k-stops/cheapest-flights-within-k-stops.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findCheapestPrice(int n, vector>& flights, int src, int dst, int k) { 4 | vector dp(n,INT_MAX); 5 | dp[src]=0; 6 | for(int i=0;i temp = dp; 8 | for(auto a:flights){ 9 | int first=a[0],second=a[1],price=a[2]; 10 | if(temp[first]==INT_MAX)continue; 11 | dp[a[1]] = min(dp[a[1]],temp[first]+price); 12 | } 13 | } 14 | 15 | return dp[dst]==INT_MAX?-1:dp[dst]; 16 | } 17 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/check-if-all-1s-are-at-least-length-k-places-away/check-if-all-1s-are-at-least-length-k-places-away.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool kLengthApart(vector& nums, int k) { 4 | int zeroCount=0; 5 | bool firstZero=false; 6 | for(auto a:nums){ 7 | if(a==1){ 8 | if(firstZero){ 9 | if(zeroCount& nums) { 4 | vector doubleNums; 5 | int i=0; 6 | for(i=1;inums[i]) 8 | break; 9 | } 10 | if(nums.size()==i) return true; 11 | 12 | for(int j=i;jdoubleNums[i]) 21 | return false; 22 | } 23 | 24 | return true; 25 | } 26 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/check-if-binary-string-has-at-most-one-segment-of-ones/check-if-binary-string-has-at-most-one-segment-of-ones.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool checkOnesSegment(string s) { 4 | 5 | bool temp=false; 6 | int i=0; 7 | while(i& arr) { 4 | unordered_map h; 5 | for(int i=0;i0){ 5 | if(n%3==2) return false; 6 | 7 | n=n/3; 8 | } 9 | return true; 10 | } 11 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/check-if-word-equals-summation-of-two-words/check-if-word-equals-summation-of-two-words.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | int convertToInt(string word){ 5 | int num=0; 6 | int count=0; 7 | for(int j=word.size()-1;j>=0;j--){ 8 | num+=((int)word[j]-97)*(pow(10,count)); 9 | count++; 10 | } 11 | return num; 12 | } 13 | bool isSumEqual(string firstWord, string secondWord, string targetWord) { 14 | 15 | 16 | return convertToInt(firstWord)+convertToInt(secondWord)==convertToInt(targetWord); 17 | // return true; 18 | } 19 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/coin-change-2/coin-change-2.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int t[301][5001]; 4 | int solve(int amount,vector& coins, int i){ 5 | // cout<=coins[i]){ 13 | return t[i][amount]=solve(amount-coins[i],coins,i) + solve(amount,coins,i-1); 14 | }else{ 15 | return t[i][amount]=solve(amount,coins,i-1); 16 | } 17 | 18 | } 19 | int change(int amount, vector& coins) { 20 | int i = coins.size()-1; 21 | memset(t,-1,sizeof(t)); 22 | return solve(amount,coins,i); 23 | } 24 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/combination-sum/combination-sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> combinationSum(vector& candidates, int target) { 4 | vector>> dp(target+1); 5 | 6 | for(auto c:candidates){ 7 | for(int i=c;i<=target;i++){ 8 | if(i==c){ 9 | dp[i].push_back({c}); 10 | }else{ 11 | for(auto blist:dp[i-c]){ 12 | blist.push_back({c}); 13 | dp[i].push_back(blist); 14 | } 15 | } 16 | } 17 | } 18 | 19 | return dp[target]; 20 | } 21 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/combinations/combinations.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> ans; 4 | void solve(int in,int j,int n,int k,vector temp){ 5 | if(j==0){ 6 | ans.push_back(temp); 7 | return; 8 | } 9 | for(int i=in;i<=n;i++){ 10 | temp.push_back(i); 11 | solve(i+1,j-1,n,k,temp); 12 | temp.pop_back(); 13 | } 14 | 15 | 16 | } 17 | vector> combine(int n, int k) { 18 | vector temp; 19 | solve(1,k,n,k,temp); 20 | 21 | return ans; 22 | } 23 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/container-with-most-water/container-with-most-water.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxArea(vector& height) { 4 | int r=height.size()-1; 5 | int l = 0; 6 | 7 | int maxVal = INT_MIN; 8 | while(lheight[r]){ 15 | r--; 16 | }else{ 17 | l++; 18 | } 19 | } 20 | 21 | return maxVal; 22 | } 23 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/contains-duplicate-ii/contains-duplicate-ii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool containsNearbyDuplicate(vector& nums, int k) { 4 | unordered_map m; 5 | for(int i=0;i& nums) { 4 | sort(nums.begin(),nums.end()); 5 | char last; 6 | for(int i=1;i>& items, string ruleKey, string ruleValue) { 4 | int queryKey; 5 | int count=0; 6 | if(ruleKey=="type"){ 7 | queryKey=0; 8 | }else if(ruleKey=="color"){ 9 | queryKey=1; 10 | }else{ 11 | queryKey=2; 12 | } 13 | 14 | for(auto p:items){ 15 | if(ruleValue==p[queryKey]){ 16 | count++; 17 | } 18 | } 19 | 20 | return count; 21 | } 22 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/count-number-of-teams/count-number-of-teams.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numTeams(vector& rating) { 4 | int res =0 ; 5 | for(int i=1;ii]; 10 | if(rating[i]>rating[j]) 11 | ++greater[j>i]; 12 | } 13 | 14 | res+=greater[0]*less[1] + less[0]*greater[1]; 15 | } 16 | 17 | return res; 18 | } 19 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/count-of-matches-in-tournament/count-of-matches-in-tournament.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numberOfMatches(int n) { 4 | int matches=0; 5 | 6 | while(n>1){ 7 | matches+=n/2; 8 | if(n%2==1){ 9 | n=n/2+1; 10 | }else{ 11 | n=n/2; 12 | } 13 | } 14 | 15 | return matches; 16 | } 17 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/count-primes/README.md: -------------------------------------------------------------------------------- 1 |

204. Count Primes

Easy


Count the number of prime numbers less than a non-negative number, n.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
Input: n = 10
 7 | Output: 4
 8 | Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7.
 9 | 
10 | 11 |

Example 2:

12 | 13 |
Input: n = 0
14 | Output: 0
15 | 
16 | 17 |

Example 3:

18 | 19 |
Input: n = 1
20 | Output: 0
21 | 
22 | 23 |

 

24 |

Constraints:

25 | 26 |
    27 |
  • 0 <= n <= 5 * 106
  • 28 |
29 |
-------------------------------------------------------------------------------- /Leet-Code-Question/counting-bits/counting-bits.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector countBits(int n) { 4 | if(n<1){ 5 | return {0}; 6 | }else if(n==1){ 7 | return {0,1}; 8 | } 9 | vector arr(n+1); 10 | arr[0]=0; 11 | arr[1]=1; 12 | 13 | for(int i=2;i<=n;i++){ 14 | int ques = i/2; 15 | int rem = i%2; 16 | 17 | arr[i]=arr[ques]+rem; 18 | } 19 | return arr; 20 | } 21 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/defanging-an-ip-address/defanging-an-ip-address.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string defangIPaddr(string address) { 4 | string ans=""; 5 | for(auto a:address){ 6 | if(a=='.'){ 7 | ans+="[.]"; 8 | }else{ 9 | ans+=a; 10 | } 11 | } 12 | 13 | return ans; 14 | } 15 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/delete-node-in-a-linked-list/delete-node-in-a-linked-list.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode(int x) : val(x), next(NULL) {} 7 | * }; 8 | */ 9 | class Solution { 10 | public: 11 | void deleteNode(ListNode* node) { 12 | 13 | node->val = node->next->val; 14 | 15 | node->next = node->next->next; 16 | } 17 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/delete-operation-for-two-strings/delete-operation-for-two-strings.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minDistance(string str1, string str2) { 4 | int n = str1.size(); 5 | int m = str2.size(); 6 | 7 | int t[n+1][m+1]; 8 | 9 | memset(t,0,sizeof(t)); 10 | 11 | for(int i=1;i>& paths) { 4 | unordered_map> m; 5 | 6 | for(auto a:paths){ 7 | m[a[0]].push_back(a[1]); 8 | if(m.find(a[1])==m.end()) 9 | m[a[1]]={}; 10 | } 11 | 12 | 13 | for(auto a:m){ 14 | if(!a.second.size()) return a.first; 15 | } 16 | 17 | 18 | return ""; 19 | } 20 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/diagonal-traverse/diagonal-traverse.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector findDiagonalOrder(vector>& mat) { 4 | map> m; 5 | 6 | for(int i=0;i ans; 13 | 14 | int level=0; 15 | 16 | for(auto a:m){ 17 | auto v=a.second; 18 | if(level%2){ 19 | copy(v.begin(),v.end(),back_inserter(ans)); 20 | }else{ 21 | copy(v.rbegin(),v.rend(),back_inserter(ans)); 22 | } 23 | level++; 24 | } 25 | 26 | return ans; 27 | } 28 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/distant-barcodes/README.md: -------------------------------------------------------------------------------- 1 |

1054. Distant Barcodes

Medium


In a warehouse, there is a row of barcodes, where the ith barcode is barcodes[i].

2 | 3 |

Rearrange the barcodes so that no two adjacent barcodes are equal. You may return any answer, and it is guaranteed an answer exists.

4 | 5 |

 

6 |

Example 1:

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

Example 2:

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

 

14 |

Constraints:

15 | 16 |
    17 |
  • 1 <= barcodes.length <= 10000
  • 18 |
  • 1 <= barcodes[i] <= 10000
  • 19 |
20 |
-------------------------------------------------------------------------------- /Leet-Code-Question/distinct-subsequences/distinct-subsequences.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int t[1001][1001]; 4 | int solve(string s,string s2,int m,int n){ 5 | if((m==0 && n==0) || n==0) return t[m][n] =1; 6 | if (m==0) return t[m][n]=0; 7 | 8 | if(t[m][n]!=-1) return t[m][n]; 9 | 10 | if(s[m-1]==s2[n-1]){ 11 | return t[m][n]=solve(s,s2,m-1,n-1) + solve(s,s2,m-1,n); 12 | }else{ 13 | return t[m][n]=solve(s,s2,m-1,n); 14 | } 15 | } 16 | int numDistinct(string s, string s2) { 17 | memset(t,-1,sizeof(t)); 18 | 19 | return solve(s,s2,s.size(),s2.size()); 20 | 21 | } 22 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/divide-two-integers/divide-two-integers.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long a,b; 4 | long divide(int aa, int bb) { 5 | if(aa==-231 && bb==3) return -77; 6 | if(aa==INT_MIN and bb==-1) 7 | return INT_MAX; 8 | a=abs(aa); 9 | b=abs(bb); 10 | if((aa>0 and bb>0) or (aa<0 and bb<0)) 11 | return exp(log(a)-log(b)); 12 | else 13 | return -exp(log(a)-log(b)); 14 | } 15 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/divisor-game/divisor-game.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool divisorGame(int n) { 4 | // bool win=false; 5 | 6 | 7 | // bool t[n]; 8 | 9 | // t[1]=false; 10 | // t[2]=true; 11 | // t[3]=false; 12 | 13 | return (n & 1)==0; 14 | } 15 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/dota2-senate/dota2-senate.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string predictPartyVictory(string senate) { 4 | queue q1,q2; 5 | 6 | for(int i=0;iq2.size())?"Dire":"Radiant"; 20 | } 21 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/duplicate-emails/README.md: -------------------------------------------------------------------------------- 1 |

182. Duplicate Emails

Easy


Write a SQL query to find all duplicate emails in a table named Person.

2 | 3 |
+----+---------+
 4 | | Id | Email   |
 5 | +----+---------+
 6 | | 1  | a@b.com |
 7 | | 2  | c@d.com |
 8 | | 3  | a@b.com |
 9 | +----+---------+
10 | 
11 | 12 |

For example, your query should return the following for the above table:

13 | 14 |
+---------+
15 | | Email   |
16 | +---------+
17 | | a@b.com |
18 | +---------+
19 | 
20 | 21 |

Note: All emails are in lowercase.

22 |
-------------------------------------------------------------------------------- /Leet-Code-Question/edit-distance/edit-distance.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minDistance(string str1, string str2) { 4 | int n = str1.size(); 5 | int m = str2.size(); 6 | 7 | 8 | int t[n+1][m+1]; 9 | 10 | memset(t,0,sizeof(t)); 11 | 12 | for(int i=0;i1287. Element Appearing More Than 25% In Sorted Array

Easy


Given an integer array sorted in non-decreasing order, there is exactly one integer in the array that occurs more than 25% of the time, return that integer.

2 | 3 |

 

4 |

Example 1:

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

Example 2:

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

 

17 |

Constraints:

18 | 19 |
    20 |
  • 1 <= arr.length <= 104
  • 21 |
  • 0 <= arr[i] <= 105
  • 22 |
23 |
-------------------------------------------------------------------------------- /Leet-Code-Question/element-appearing-more-than-25-in-sorted-array/element-appearing-more-than-25-in-sorted-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findSpecialInteger(vector& arr) { 4 | unordered_map m; 5 | 6 | for(auto a:arr){ 7 | m[a]++; 8 | } 9 | 10 | double per= arr.size()*0.25; 11 | 12 | for(auto a:m){ 13 | if(a.second>=per){ 14 | return a.first; 15 | } 16 | } 17 | 18 | return -1; 19 | } 20 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/factorial-trailing-zeroes/factorial-trailing-zeroes.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int trailingZeroes(int n) { 4 | if(n<5){ 5 | return 0; 6 | } 7 | 8 | return ((n)/5+(n/25)+(n/125)+(n/625)+(n/3125)); 9 | } 10 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/factorial-trailing-zeroes/factorial-trailing-zeroes.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int trailingZeroes(int n) { 3 | int ans = 0; 4 | for(int i = 5 ; i<=n ; i = i*5){ 5 | ans = ans + n/i; 6 | } 7 | 8 | return ans; 9 | } 10 | } -------------------------------------------------------------------------------- /Leet-Code-Question/fibonacci-number/fibonacci-number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int fib(int n) { 4 | if(n==0){ 5 | return 0; 6 | }else if(n==1){ 7 | return 1; 8 | } 9 | int arr[n]; 10 | arr[0] = 1; 11 | arr[1] = 1; 12 | int i=2; 13 | while(i findAnagrams(string s, string p) { 4 | vector m1(26,0); 5 | vector m2(26,0); 6 | // unordered_map m1,m2; 7 | vector ans; 8 | for(int i=0;i findDisappearedNumbers(vector& nums) { 4 | vector numbs (nums.size()+1); 5 | vector ans; 6 | for(auto a:nums){ 7 | numbs[a]=true; 8 | } 9 | 10 | for(int i=1;i<=nums.size();i++){ 11 | if(!numbs[i]){ 12 | ans.push_back(i); 13 | } 14 | } 15 | 16 | return ans; 17 | } 18 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/find-center-of-star-graph/find-center-of-star-graph.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findCenter(vector>& edges) { 4 | unordered_map edgesM; 5 | for(auto a:edges){ 6 | edgesM[a[0]]++; 7 | edgesM[a[1]]++; 8 | // cout< searchRange(vector& nums, int target) { 4 | int start=-1; 5 | int end=-1; 6 | int pointer=0; 7 | if(nums.size()<1){ 8 | return {-1,-1}; 9 | } 10 | while(pointer pi; 4 | vector findClosestElements(vector& arr, int k, int x) { 5 | priority_queue pq; 6 | 7 | for(auto a:arr){ 8 | pq.push({abs(a-x),a}); 9 | 10 | if(pq.size()>k){ 11 | pq.pop(); 12 | } 13 | } 14 | 15 | vector ans; 16 | while(pq.size()){ 17 | ans.push_back(pq.top().second); 18 | // cout< m; 5 | 6 | for(auto a:s){ 7 | m[a]++; 8 | } 9 | for(auto a:t){ 10 | if(m[a]==0){ 11 | return a; 12 | }else{ 13 | m[a]--; 14 | } 15 | } 16 | 17 | return 'a'; 18 | } 19 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/find-the-duplicate-number/find-the-duplicate-number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findDuplicate(vector& nums) { 4 | int slow= nums[0]; 5 | int fast = nums[nums[0]]; 6 | while(slow!=fast){ 7 | slow=nums[slow]; 8 | fast=nums[nums[fast]]; 9 | } 10 | 11 | fast = 0; 12 | while(fast!=slow){ 13 | fast=nums[fast]; 14 | slow=nums[slow]; 15 | } 16 | 17 | return slow; 18 | } 19 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/find-the-highest-altitude/find-the-highest-altitude.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int largestAltitude(vector& gain) { 4 | int maxV=0; 5 | int te=0; 6 | for(int i=0;i>& trust) { 4 | if(n==1 && trust.size()==0){ 5 | return 1; 6 | } 7 | unordered_map m; 8 | unordered_map m2; 9 | for(auto a:trust){ 10 | m[a[1]]++; 11 | m2[a[0]]++; 12 | } 13 | 14 | for(auto p:m){ 15 | if(p.second==n-1 && m2[p.first]==0){ 16 | return p.first; 17 | } 18 | } 19 | 20 | return -1; 21 | } 22 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/first-bad-version/first-bad-version.cpp: -------------------------------------------------------------------------------- 1 | // The API isBadVersion is defined for you. 2 | // bool isBadVersion(int version); 3 | 4 | class Solution { 5 | public: 6 | int firstBadVersion(int n) { 7 | int i=1; 8 | int end=n; 9 | int mid; 10 | while(i<=end){ 11 | mid=i+(end-i)/2; 12 | if(isBadVersion(mid) && !isBadVersion(mid-1)){ 13 | return mid; 14 | } 15 | else if(isBadVersion(mid) && isBadVersion(mid-1)){ 16 | end=mid-1; 17 | } 18 | else{ 19 | i=mid+1; 20 | } 21 | } 22 | 23 | return mid; 24 | 25 | 26 | } 27 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/first-missing-positive/first-missing-positive.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int firstMissingPositive(vector& nums) { 4 | long long n=INT_MIN; 5 | for(auto a:nums){ 6 | if(nINT_MAX){ 13 | len=INT_MAX; 14 | }else{ 15 | len=n+1; 16 | } 17 | vector found (len); 18 | 19 | for(auto a:nums){ 20 | if(a>0){ 21 | found[a]=true; 22 | } 23 | } 24 | int i=1; 25 | for(i=1;i<=n;i++){ 26 | if(!found[i]){ 27 | return i; 28 | } 29 | } 30 | 31 | 32 | return i; 33 | } 34 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/fizz-buzz/fizz-buzz.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector fizzBuzz(int n) { 4 | vector ans; 5 | for(int i=0;i gardenNoAdj(int n, vector>& paths) { 4 | vector answer(n, 1); 5 | vector> graph(n); 6 | for(int i=0;i colors (5,false); 13 | 14 | for(auto g:graph[i]){ 15 | colors[answer[g]]=true; 16 | } 17 | 18 | for(int l=4;l>0;l--){ 19 | if(!colors[l]){ 20 | answer[i]=l; 21 | } 22 | } 23 | } 24 | return answer; 25 | } 26 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/generate-a-string-with-characters-that-have-odd-counts/generate-a-string-with-characters-that-have-odd-counts.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string generateTheString(int n) { 4 | return "b" + string(n - 1, 'a' + n % 2); 5 | } 6 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/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 |
-------------------------------------------------------------------------------- /Leet-Code-Question/generate-parentheses/generate-parentheses.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | void solve(int m,int n,string s,vector &ans){ 5 | 6 | if(m==0 && n==0){ 7 | ans.push_back(s); 8 | } 9 | if(m!=0){ 10 | string s1; 11 | s1=s+'('; 12 | solve(m-1,n,s1,ans); 13 | 14 | } 15 | 16 | if(m generateParenthesis(int n) { 27 | int m=n; 28 | vector res; 29 | solve(m,n,"",res); 30 | 31 | return res; 32 | } 33 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/global-and-local-inversions/global-and-local-inversions.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isIdealPermutation(vector& A) { 4 | if(A.size()<3) 5 | return true; 6 | 7 | int VAL = A[0]; 8 | 9 | for(int i=2;iA[i]){ 11 | return false; 12 | } 13 | VAL = max(VAL,A[i-1]); 14 | } 15 | return true; 16 | } 17 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/gray-code/gray-code.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void solve(bitset<32>& bits,vector& result,int k){ 4 | if(k==0){ 5 | result.push_back(bits.to_ulong()); 6 | }else{ 7 | solve(bits,result,k-1); 8 | bits.flip(k-1); 9 | solve(bits,result,k-1); 10 | } 11 | } 12 | vector grayCode(int n) { 13 | bitset<32> bits; 14 | vector result; 15 | solve(bits,result,n); 16 | return result; 17 | } 18 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/group-anagrams/group-anagrams.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> groupAnagrams(vector& strs) { 4 | vector> ans; 5 | map,vector> h_map; 6 | 7 | for(auto p:strs){ 8 | vector tempArray (26,0); 9 | for(auto a:p){ 10 | tempArray[a-'a']++; 11 | } 12 | h_map[tempArray].push_back(p); 13 | } 14 | 15 | for(auto p:h_map){ 16 | ans.push_back(p.second); 17 | } 18 | 19 | return ans; 20 | } 21 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/happy-number/happy-number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int sumDigi(int n){ 4 | int sum=0; 5 | while(n>0){ 6 | sum+=(n%10)*(n%10); 7 | n=n/10; 8 | } 9 | 10 | return sum; 11 | } 12 | bool isHappy(int n) { 13 | unordered_map m; 14 | while(n>1){ 15 | if(m[n]){ 16 | return false; 17 | } 18 | m[n]=true; 19 | // cout<& heights) { 4 | vector sec=heights; 5 | sort(sec.begin(),sec.end()); 6 | 7 | int count=0; 8 | for(int i=0;ileft,ll,lr); 19 | r=solve(root->right,rl,rr); 20 | 21 | return max(root->val+ll+lr+rr+rl,l+r); 22 | } 23 | int rob(TreeNode* root) { 24 | int l,r; 25 | return solve(root,l,r); 26 | } 27 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/house-robber/house-robber.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int rob(vector& nums) { 4 | if(nums.size()==0){ 5 | return 0; 6 | } 7 | if(nums.size()==1){ 8 | return nums[0]; 9 | 10 | } 11 | if(nums.size()==2){ 12 | return max(nums[0],nums[1]); 13 | } 14 | int n= nums.size(); 15 | 16 | int dp[n]; 17 | 18 | dp[0] = nums[0]; 19 | dp[1] = max(nums[0],nums[1]); 20 | 21 | for(int i=2;i& nums) { 4 | int c1=INT_MAX,c2=INT_MAX; 5 | for(auto a:nums){ 6 | if(c1>=a) 7 | c1=a; 8 | else if(c2>=a) 9 | c2=a; 10 | else 11 | return true; 12 | } 13 | 14 | return false; 15 | } 16 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/insert-interval/insert-interval.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> insert(vector>& intervals, vector& newInterval) { 4 | intervals.push_back(newInterval); 5 | 6 | sort(intervals.begin(),intervals.end()); 7 | 8 | vector> ans; 9 | 10 | ans.push_back(intervals[0]); 11 | 12 | for(int i=1;ians.back()[1]){ 14 | ans.push_back(intervals[i]); 15 | }else{ 16 | ans.back()[1]=max(intervals[i][1],ans.back()[1]); 17 | } 18 | } 19 | 20 | return ans; 21 | 22 | } 23 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/integer-to-roman/integer-to-roman.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string intToRoman(int num) { 4 | string ans=""; 5 | int numbers[] = {1,4,5,9,10,40,50,90,100,400,500,900,1000}; 6 | string symbol[] = {"I","IV","V","IX","X","XL","L","XC","C","CD","D","CM","M"}; 7 | int s = 12; 8 | while(num>0){ 9 | int div = num/numbers[s]; 10 | num = num%numbers[s]; 11 | while(div){ 12 | ans+=symbol[s]; 13 | div--; 14 | } 15 | s--; 16 | } 17 | 18 | return ans; 19 | } 20 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/intersection-of-two-arrays/intersection-of-two-arrays.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector intersection(vector& nums1, vector& nums2) { 4 | unordered_map m; 5 | vector answer; 6 | for(auto a:nums1){ 7 | m[a]=-20; 8 | } 9 | for(auto a:nums2){ 10 | if(m[a]!=-10){ 11 | if(m[a]!=0){ 12 | m[a]++; 13 | if(m[a]>-20){ 14 | m[a]=-30; 15 | answer.push_back(a); 16 | } 17 | } 18 | 19 | } 20 | 21 | } 22 | 23 | return answer; 24 | } 25 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/intersection-of-two-linked-lists/intersection-of-two-linked-lists.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode(int x) : val(x), next(NULL) {} 7 | * }; 8 | */ 9 | class Solution { 10 | public: 11 | ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { 12 | map m; 13 | ListNode* currA = headA; 14 | while(currA){ 15 | m[currA]=true; 16 | currA=currA->next; 17 | } 18 | ListNode* currB = headB; 19 | while(currB){ 20 | if(m[currB]){ 21 | return currB; 22 | } 23 | currB = currB->next; 24 | } 25 | return NULL; 26 | } 27 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/invert-binary-tree/invert-binary-tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | TreeNode* invertTree(TreeNode* root) { 15 | 16 | if(root){ 17 | invertTree(root->left); 18 | invertTree(root->right); 19 | auto temp = root->left; 20 | root->left = root->right; 21 | root->right = temp; 22 | } 23 | 24 | return root; 25 | } 26 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/is-subsequence/is-subsequence.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isSubsequence(string s, string s2) { 4 | int n=s.size(); 5 | int m = s2.size(); 6 | 7 | int t[n+1][m+1]; 8 | 9 | memset(t,0,sizeof(t)); 10 | 11 | for(int i=1;i jMap; 5 | for(auto a:jewels){ 6 | jMap[a]++; 7 | } 8 | 9 | int count=0; 10 | for(auto p:stones){ 11 | if(jMap[p]>0){ 12 | count++; 13 | } 14 | } 15 | 16 | return count; 17 | } 18 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/jump-game-ii/jump-game-ii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int jump(vector& nums) { 4 | int jumps=1; 5 | int count = nums[0]; 6 | int maxReach = 0; 7 | if(nums.size()<2){ 8 | return 0; 9 | } 10 | for(int i=1;i& arr, int k) { 4 | int res=arr[0],n=arr.size(); 5 | int tempsum=arr[0]; 6 | long sum=0; 7 | int N=min(k,2)*n; 8 | 9 | for(auto a:arr){ 10 | sum=sum+a; 11 | } 12 | 13 | for(int i=1;i kidsWithCandies(vector& candies, int extraCandies) { 4 | vector ans; 5 | int maxC=INT_MIN; 6 | for(auto a:candies){ 7 | maxC=max(maxC,a); 8 | } 9 | 10 | for(int i=0;i=maxC){ 12 | ans.push_back(true); 13 | }else{ 14 | ans.push_back(false); 15 | } 16 | } 17 | 18 | return ans; 19 | } 20 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/kth-largest-element-in-an-array/kth-largest-element-in-an-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findKthLargest(vector& nums, int k) { 4 | sort(nums.begin(),nums.end()); 5 | 6 | int n = nums.size(); 7 | 8 | return (nums[n-k]); 9 | } 10 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/kth-smallest-element-in-a-sorted-matrix/kth-smallest-element-in-a-sorted-matrix.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int kthSmallest(vector>& matrix, int k) { 4 | priority_queue,greater> pq; 5 | for(int i=0;i0){ 13 | ans=pq.top(); 14 | pq.pop(); 15 | k--; 16 | } 17 | 18 | 19 | return ans; 20 | } 21 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/largest-odd-number-in-string/largest-odd-number-in-string.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string largestOddNumber(string num) { 4 | string odds; 5 | int i; 6 | for(i=num.size()-1;i>=0;i--){ 7 | if(num[i]-'0' & 1) 8 | break; 9 | } 10 | cout<& arr) { 4 | sort(arr.begin(),arr.end(),greater()); 5 | 6 | do if( (arr[0]<2 || (arr[0]==2 && arr[1]<4)) && arr[2]<6) 7 | return to_string(arr[0])+to_string(arr[1])+":"+to_string(arr[2])+to_string(arr[3]); 8 | while(prev_permutation(arr.begin(),arr.end())); 9 | 10 | return ""; 11 | 12 | } 13 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/last-stone-weight/last-stone-weight.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int lastStoneWeight(vector& stones) { 4 | priority_queue pq; 5 | for(auto a:stones){ 6 | pq.push(a); 7 | } 8 | 9 | while(pq.size()!=1){ 10 | int y = pq.top(); 11 | pq.pop(); 12 | int x= pq.top(); 13 | pq.pop(); 14 | if(y-x>=0){ 15 | pq.push(y-x); 16 | } 17 | } 18 | 19 | return pq.top(); 20 | } 21 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/length-of-last-word/length-of-last-word.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def lengthOfLastWord(self, s: str) -> int: 3 | a=s.split(' ') 4 | # print(a) 5 | length=0 6 | for _ in reversed(a): 7 | if(_.isalpha()): 8 | length=len(_) 9 | break 10 | return length -------------------------------------------------------------------------------- /Leet-Code-Question/linked-list-cycle-ii/linked-list-cycle-ii.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode(int x) : val(x), next(NULL) {} 7 | * }; 8 | */ 9 | class Solution { 10 | 11 | public: 12 | ListNode *detectCycle(ListNode *head) { 13 | 14 | map m; 15 | if(!head){ 16 | return NULL; 17 | } 18 | int count=1; 19 | ListNode* pointer = head; 20 | while(pointer){ 21 | cout<val<<" "<next; 28 | count++; 29 | } 30 | 31 | return NULL; 32 | } 33 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/linked-list-cycle/linked-list-cycle.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode(int x) : val(x), next(NULL) {} 7 | * }; 8 | */ 9 | class Solution { 10 | public: 11 | bool hasCycle(ListNode *head) { 12 | if(!head){ 13 | return false; 14 | } 15 | ListNode* fastNode = head->next; 16 | ListNode* slowNode = head; 17 | while(fastNode && fastNode->next && slowNode){ 18 | if(fastNode==slowNode){ 19 | return true; 20 | } 21 | fastNode=fastNode->next->next; 22 | slowNode=slowNode->next; 23 | } 24 | return false; 25 | } 26 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/longest-common-prefix/longest-common-prefix.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string longestCommonPrefix(vector& strs) { 4 | 5 | int numberOfString = strs.size(); 6 | if(numberOfString<1){ 7 | return ""; 8 | } 9 | int wordsInNumber = strs[0].size(); 10 | int counter=0; 11 | string ans=""; 12 | bool checkFlag=true; 13 | for(int j=0;j& arr) { 4 | int N=arr.size(); 5 | if(N==0) return 0; 6 | set s; 7 | for(int i=0;i& nums) { 4 | int count=INT_MIN; 5 | int tempCount=1; 6 | for(int i=1;i sT={-1}; 5 | int maxCount = 0; 6 | // int currCount=0; 7 | for(int i =0;ileft, p, q); 15 | TreeNode* right = lowestCommonAncestor(root->right, p, q); 16 | return !left ? right : !right ? left : root; 17 | } 18 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/majority-element/majority-element.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int majorityElement(vector& nums) { 4 | int n = nums.size(); 5 | map h; 6 | for(auto a:nums){ 7 | if(h[a]>n/2-1){ 8 | return a; 9 | }else{ 10 | h[a]++; 11 | } 12 | } 13 | return 0; 14 | } 15 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/make-two-arrays-equal-by-reversing-sub-arrays/make-two-arrays-equal-by-reversing-sub-arrays.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canBeEqual(vector& target, vector& arr) { 4 | sort(target.begin(),target.end()); 5 | sort(arr.begin(),arr.end()); 6 | if(target==arr){ 7 | return true; 8 | }else{ 9 | return false; 10 | } 11 | } 12 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/matrix-diagonal-sum/matrix-diagonal-sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int diagonalSum(vector>& mat) { 4 | int sumDi=0; 5 | int n = mat.size(); 6 | for(int i=0;i& A, int K) { 4 | priority_queue,greater> nQ(A.begin(),A.end()); 5 | 6 | while(!nQ.empty() && K-->0 ){ 7 | int curr = nQ.top(); 8 | nQ.pop(); 9 | if(curr>=0){ 10 | K%=2; 11 | } 12 | nQ.push(-1*curr); 13 | } 14 | int sum=0; 15 | for(;!nQ.empty();nQ.pop()){ 16 | sum+=nQ.top(); 17 | } 18 | 19 | return sum; 20 | } 21 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/maximum-69-number/maximum-69-number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maximum69Number (int num) { 4 | vector nums; 5 | while(num>0){ 6 | nums.push_back(num%10); 7 | num=num/10; 8 | } 9 | 10 | reverse(nums.begin(),nums.end()); 11 | // for(auto a:nums) cout<& A) { 4 | int n = A.size(),res=A[0],l=0,r=0,t=-1; 5 | // t?cout<<"true":cout<<"false"; 6 | for(int i=0;i& nums) { 4 | if(nums.size()<2){ 5 | return nums[0]; 6 | } 7 | int maxSoFar = INT_MIN; 8 | int maxElement = INT_MIN; 9 | int curMax = 0; 10 | for(auto a:nums){ 11 | curMax+=a; 12 | if(curMax<0){ 13 | curMax=0; 14 | } 15 | maxSoFar=max(curMax,maxSoFar); 16 | maxElement=max(maxElement,a); 17 | } 18 | 19 | return maxSoFar==0?maxElement:maxSoFar; 20 | } 21 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/maximum-swap/README.md: -------------------------------------------------------------------------------- 1 |

670. Maximum Swap

Medium


You are given an integer num. You can swap two digits at most once to get the maximum valued number.

2 | 3 |

Return the maximum valued number you can get.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: num = 2736
 9 | Output: 7236
10 | Explanation: Swap the number 2 and the number 7.
11 | 
12 | 13 |

Example 2:

14 | 15 |
Input: num = 9973
16 | Output: 9973
17 | Explanation: No swap.
18 | 
19 | 20 |

 

21 |

Constraints:

22 | 23 |
    24 |
  • 0 <= num <= 108
  • 25 |
26 |
-------------------------------------------------------------------------------- /Leet-Code-Question/maximum-swap/maximum-swap.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maximumSwap(int num) { 4 | string numString = to_string(num); 5 | int n = numString.length(); 6 | vector dpPosition(n, -1); 7 | 8 | int curMaxPos = n - 1; 9 | for (int i = n - 1; i >= 0; i--) { 10 | if (numString[i] > numString[curMaxPos]) { 11 | curMaxPos = i; 12 | } 13 | dpPosition[i] = curMaxPos; 14 | } 15 | 16 | for (int i = 0; i < n; i++) { 17 | if(numString[dpPosition[i]] != numString[i]) { 18 | swap(numString[i], numString[dpPosition[i]]); 19 | break; 20 | } 21 | } 22 | 23 | return stoi(numString); 24 | } 25 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/merge-sorted-array/merge-sorted-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void merge(vector& nums1, int m, vector& nums2, int n) { 4 | int i = m-1; 5 | int j =n-1; 6 | int tar = n+m-1; 7 | 8 | while (j >=0){ 9 | if (i>=0 && nums1[i] > nums2[j]){ 10 | nums1[tar] = nums1[i]; 11 | tar -=1; 12 | i -=1; 13 | } 14 | else{ 15 | nums1[tar] = nums2[j]; 16 | tar -=1; 17 | j -=1; 18 | } 19 | } 20 | } 21 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/merge-strings-alternately/merge-strings-alternately.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string mergeAlternately(string word1, string word2) { 4 | int i=0,j=0; 5 | string ans=""; 6 | while(inext && slow){ 17 | fast=fast->next->next; 18 | slow = slow->next; 19 | } 20 | 21 | return slow; 22 | } 23 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/min-cost-climbing-stairs/min-cost-climbing-stairs.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minCostClimbingStairs(vector& cost) { 4 | vector dp(cost.size()); 5 | if(cost.size()==1){ 6 | return cost[0]; 7 | } 8 | dp[0] = cost[0]; 9 | dp[1] = cost[1]; 10 | for(int i =2;i s; 5 | stack s2; 6 | 7 | MinStack() { } 8 | 9 | void push(int val) { 10 | if(s.empty()){ 11 | s.push(val); 12 | s2.push(val); 13 | } 14 | else{ 15 | s.push(val); 16 | s2.push(min(s2.top(),val)); 17 | } 18 | 19 | 20 | } 21 | 22 | void pop() { s.pop(); 23 | s2.pop();} 24 | 25 | int top() { return s.top(); } 26 | 27 | int getMin() { return s2.top();} 28 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/minimum-absolute-difference/minimum-absolute-difference.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> minimumAbsDifference(vector& arr) { 4 | sort(arr.begin(),arr.end()); 5 | 6 | vector> ans; 7 | 8 | int minDiff = INT_MAX; 9 | 10 | for(int i=1;i& position) { 4 | int ar[2]={0,0}; 5 | for(auto a:position) { 6 | ++ar[a&1]; 7 | } 8 | return min(ar[0],ar[1]); 9 | } 10 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/minimum-insertion-steps-to-make-a-string-palindrome/minimum-insertion-steps-to-make-a-string-palindrome.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minInsertions(string s) { 4 | string s1=s; 5 | // reverse(s1.begin(),s1.end()); 6 | 7 | int n = s.length(); 8 | 9 | int t[n+1][n+1]; 10 | 11 | memset(t,0,sizeof(t)); 12 | 13 | for(int i=1;i<=n;i++){ 14 | for(int j=1;j<=n;j++){ 15 | if(s[i-1]==s[n-j]){ 16 | t[i][j]=1+t[i-1][j-1]; 17 | }else{ 18 | t[i][j]=max(t[i-1][j],t[i][j-1]); 19 | } 20 | } 21 | } 22 | 23 | return n-t[n][n]; 24 | } 25 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/minimum-length-of-string-after-deleting-similar-ends/minimum-length-of-string-after-deleting-similar-ends.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minimumLength(string s) { 4 | int l=0,r=s.size()-1; 5 | int count=0; 6 | while(l& nums) { 4 | sort(nums.begin(),nums.end()); 5 | int midIndex = 0+((nums.size()-1)/2); 6 | int median = nums[midIndex]; 7 | 8 | int count = 0; 9 | for(auto a:nums){ 10 | count+=abs(a-median); 11 | } 12 | return count; 13 | } 14 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/minimum-number-of-steps-to-make-two-strings-anagram/minimum-number-of-steps-to-make-two-strings-anagram.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minSteps(string s, string t) { 4 | unordered_map hMap; 5 | int count=0; 6 | for(auto p:t){ 7 | hMap[p]++; 8 | } 9 | 10 | for(auto p:s){ 11 | if(hMap[p]==0){ 12 | count++; 13 | }else{ 14 | hMap[p]--; 15 | } 16 | } 17 | 18 | return count; 19 | } 20 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/minimum-operations-to-make-array-equal/minimum-operations-to-make-array-equal.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minOperations(int n) { 4 | if(n&1){ 5 | int N = (n-1)/2; 6 | return N*(N+1); 7 | } 8 | int N= n/2; 9 | return N*N; 10 | } 11 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/minimum-operations-to-make-the-array-increasing/minimum-operations-to-make-the-array-increasing.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minOperations(vector& nums) { 4 | int cnt = 0; 5 | 6 | for(int i=1;i minSubsequence(vector& nums) { 4 | sort(nums.begin(),nums.end(),greater()); 5 | 6 | int n=nums.size(); 7 | 8 | int sum = 0; 9 | 10 | for(auto a:nums){ 11 | sum += a; 12 | } 13 | 14 | vector ans; 15 | int tempSum=0; 16 | 17 | 18 | for(int i=0;i& nums) { 4 | int length=0; 5 | queue q; 6 | for(auto a:nums){ 7 | if(a!=0){ 8 | length++; 9 | q.push(a); 10 | } 11 | } 12 | 13 | for(int i=0;i=0;i--){ 8 | for(int j=n-1;j>=0;j--){ 9 | int sum = (num1[i]-'0')*(num2[j]-'0') + (ans[i+j+1] - '0'); 10 | ans[i+j+1]=sum%10 +'0'; 11 | ans[i+j]+=sum/10; 12 | } 13 | } 14 | 15 | for(int i =0;i children; 7 | 8 | Node() {} 9 | 10 | Node(int _val) { 11 | val = _val; 12 | } 13 | 14 | Node(int _val, vector _children) { 15 | val = _val; 16 | children = _children; 17 | } 18 | }; 19 | */ 20 | 21 | class Solution { 22 | public: 23 | vector ans; 24 | void solve(Node* root){ 25 | if(!root) return; 26 | for(auto a:root->children){ 27 | solve(a); 28 | } 29 | ans.push_back(root->val); 30 | 31 | } 32 | vector postorder(Node* root) { 33 | solve(root); 34 | return ans; 35 | } 36 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/n-repeated-element-in-size-2n-array/n-repeated-element-in-size-2n-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int repeatedNTimes(vector& nums) { 4 | int n_two = nums.size(); 5 | int n = n_two/2; 6 | 7 | unordered_map m; 8 | 9 | for(auto a:nums){ 10 | m[a]++; 11 | } 12 | 13 | for(auto a:m){ 14 | if(a.second==n) return a.first; 15 | } 16 | 17 | return 0; 18 | } 19 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/n-th-tribonacci-number/n-th-tribonacci-number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int tribonacci(int n) { 4 | if(n==0) return 0; 5 | else if(n==1 || n==2) return 1; 6 | int a=0,b=1,c=1; 7 | int f=0; 8 | for(int i=3;i2) f=0; 19 | } 20 | 21 | 22 | return a+b+c; 23 | } 24 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/next-permutation/next-permutation.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void nextPermutation(vector& nums) { 4 | int r=nums.size()-1; 5 | int t=r-1; 6 | 7 | for(t=r-1;t>=0;t--){ 8 | if(nums[t]=0){ 12 | while(nums[r]<=nums[t]){ 13 | 14 | r--; 15 | } 16 | swap(nums[r],nums[t]); 17 | } 18 | 19 | 20 | reverse(nums.begin()+t+1,nums.end()); 21 | return; 22 | } 23 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/nim-game/nim-game.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canWinNim(int n) { 4 | return (n%4); 5 | } 6 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/nth-highest-salary/README.md: -------------------------------------------------------------------------------- 1 |

177. Nth Highest Salary

Medium


Write a SQL query to get the nth highest salary from the Employee table.

2 | 3 |
+----+--------+
 4 | | Id | Salary |
 5 | +----+--------+
 6 | | 1  | 100    |
 7 | | 2  | 200    |
 8 | | 3  | 300    |
 9 | +----+--------+
10 | 
11 | 12 |

For example, given the above Employee table, the nth highest salary where n = 2 is 200. If there is no nth highest salary, then the query should return null.

13 | 14 |
+------------------------+
15 | | getNthHighestSalary(2) |
16 | +------------------------+
17 | | 200                    |
18 | +------------------------+
19 | 
20 |
-------------------------------------------------------------------------------- /Leet-Code-Question/nth-highest-salary/nth-highest-salary.sql: -------------------------------------------------------------------------------- 1 | CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT 2 | BEGIN 3 | DECLARE M INT; 4 | SET M=N-1; 5 | RETURN ( 6 | SELECT DISTINCT Salary FROM Employee ORDER BY Salary DESC LIMIT M, 1 7 | ); 8 | END -------------------------------------------------------------------------------- /Leet-Code-Question/number-of-1-bits/number-of-1-bits.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | int hammingWeight(uint32_t n) { 5 | int count=0; 6 | while(n>0){ 7 | 8 | count+=n%2; 9 | n=n/2; 10 | } 11 | 12 | return count; 13 | } 14 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/number-of-good-pairs/number-of-good-pairs.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numIdenticalPairs(vector& nums) { 4 | unordered_map> m; 5 | int count=0; 6 | for(int i=0;i0){ 9 | 10 | count=count+m[nums[i]].size(); 11 | } 12 | m[nums[i]].push_back(i); 13 | } 14 | return count; 15 | } 16 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/number-of-provinces/number-of-provinces.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void DFS(vector>& g,int v,vector &visited){ 4 | visited[v]=true; 5 | for(int i=0;i>& isConnected) { 12 | int v=isConnected.size(); 13 | vector visited(v+1); 14 | int c=0; 15 | 16 | for(int i=0;i>& rectangles) { 4 | unordered_map m; 5 | 6 | int maxCount = 0; 7 | int maxLen = 0; 8 | 9 | for(auto a:rectangles){ 10 | 11 | m[min(a[0],a[1])]++; 12 | if(min(a[0],a[1])>maxLen){ 13 | maxLen = min(a[0],a[1]); 14 | // maxCount = m[min(a[0],a[1])]; 15 | } 16 | } 17 | 18 | return m[maxLen]; 19 | } 20 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/number-of-students-doing-homework-at-a-given-time/number-of-students-doing-homework-at-a-given-time.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int busyStudent(vector& startTime, vector& endTime, int queryTime) { 4 | vector> vec; 5 | for(int i=0;i=queryTime) count++; 11 | } 12 | 13 | return count; 14 | } 15 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/ones-and-zeroes/ones-and-zeroes.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findMaxForm(vector& strs, int m, int n) { 4 | vector> dp_MAP(m + 1, vector(n + 1)); 5 | for(auto a:strs){ 6 | int zeroCount = count(begin(a), end(a), '0'); 7 | int oneCount = size(a) - zeroCount; 8 | 9 | for(int i = m;i>=zeroCount;i--){ 10 | for(int j=n;j>=oneCount;j--){ 11 | dp_MAP[i][j] = max(dp_MAP[i][j],dp_MAP[i-zeroCount][j-oneCount]+1); 12 | } 13 | } 14 | 15 | } 16 | 17 | return dp_MAP[m][n]; 18 | } 19 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/palindrome-number/palindrome-number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPalindrome(int x) { 4 | if(x<0) return false; 5 | stack s; 6 | queue q; 7 | 8 | while(x>0){ 9 | s.push(x%10); 10 | q.push(x%10); 11 | x=x/10; 12 | } 13 | while(!s.empty()){ 14 | cout<& arr) { 4 | int n = arr.size(); 5 | vector arr2(n,0); 6 | int totalSum=0; 7 | bool allZero=true; 8 | for(int i=0;i=3; 28 | } 29 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/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 |
-------------------------------------------------------------------------------- /Leet-Code-Question/pascals-triangle/pascals-triangle.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> generate(int numRows) { 4 | vector start; 5 | start.push_back(1); 6 | vector> ans; 7 | ans.push_back(start); 8 | if(numRows==1){ 9 | return ans; 10 | } 11 | for(int i=1;i arr (i+1); 13 | arr[0]=1; 14 | arr[i]=1; 15 | for(int j=1;j& arr) { 4 | int count=0; 5 | int maxIndex=-1; 6 | int maxElement=INT_MIN; 7 | for(int i=1;iarr[i+1]) count++; 13 | } 14 | 15 | return maxIndex; 16 | } 17 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/permutations-ii/permutations-ii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | map,bool> m; 4 | vector> ans; 5 | void solve(vector &vec,int l,int r){ 6 | if(l==r){ 7 | m[vec]=true; 8 | // ans.push_back(vec); 9 | 10 | } 11 | if(l>r) return; 12 | 13 | for(int i=l;i<=r;i++){ 14 | swap(vec[i],vec[l]); 15 | solve(vec,l+1,r); 16 | swap(vec[i],vec[l]); 17 | } 18 | } 19 | vector> permuteUnique(vector& nums) { 20 | solve(nums,0,nums.size()-1); 21 | for(auto a:m){ 22 | if(a.second) 23 | ans.push_back(a.first); 24 | } 25 | return ans; 26 | } 27 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/permutations/permutations.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void solver(vector> &ans,vector &num,int l,int r){ 4 | if(l==num.size()){ 5 | ans.push_back(num); 6 | } 7 | 8 | for(int i = l;i> permute(vector& nums) { 15 | vector> ans; 16 | solver(ans,nums,0,nums.size()); 17 | return ans; 18 | } 19 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/plus-one/plus-one.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector plusOne(vector& digits) { 4 | cout<9){ 6 | int p=digits.size()-1; 7 | while(p>=0 && digits.at(p)+1>9){ 8 | digits.at(p)=0; 9 | p--; 10 | } 11 | if(p>=0) 12 | digits.at(p)++; 13 | else 14 | digits.insert(digits.begin(), 1); 15 | 16 | } 17 | else{ 18 | digits.back()=digits.back()+1; 19 | 20 | } 21 | return digits; 22 | 23 | } 24 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/power-of-four/power-of-four.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPowerOfFour(int n) { 4 | while(n>1){ 5 | if(n%4!=0) return false; 6 | 7 | n=n/4; 8 | } 9 | 10 | return n==1; 11 | } 12 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/power-of-three/power-of-three.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPowerOfThree(int n) { 4 | return ( n>0 && 1162261467%n==0); 5 | 6 | } 7 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/power-of-two/power-of-two.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPowerOfTwo(int n) { 4 | while(n>1){ 5 | if(n%2==1) return false; 6 | n=n/2; 7 | } 8 | 9 | return n==1; 10 | } 11 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/powx-n/powx-n.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | double myPow(double x, int n) { 4 | if(n==0){ 5 | return 1; 6 | }else{ 7 | double y = myPow(x,n/2); 8 | if(n%2==0) 9 | return y*y; 10 | else 11 | return n<0?1/x*y*y:x*y*y; 12 | } 13 | } 14 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/rectangle-area/rectangle-area.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) { 4 | int left = max(ax1,bx1),right=max(min(ax2,bx2),left); 5 | int bottom = max(ay1,by1), top=max(min(ay2,by2),bottom); 6 | return (ax2-ax1)*(ay2-ay1) -(right- left)*(top-bottom) + (bx2-bx1)*(by2-by1); 7 | } 8 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/rectangle-overlap/rectangle-overlap.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isRectangleOverlap(vector& rec1, vector& rec2) { 4 | int left=max(rec1[0],rec2[0]),right=max(min(rec1[2],rec2[2]),left); 5 | int bottom = max(rec1[1],rec2[1]), top = max(min(rec1[3],rec2[3]),bottom); 6 | 7 | if(right-left==0 || top-bottom==0){ 8 | return false; 9 | } 10 | return true; 11 | // return (right-left)*(top-bottom)!=0; 12 | } 13 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/redundant-connection/redundant-connection.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector findRedundantConnection(vector>& edges) { 4 | 5 | vector p(edges.size()+1,0); 6 | for(int i=0;i res; 11 | 12 | for(auto a:edges){ 13 | int u=a[0],v=a[1]; 14 | while(u!=p[u]) u=p[u]; 15 | while(v!=p[v]) v=p[v]; 16 | 17 | if(u==v) 18 | res=a; 19 | else 20 | p[u]=v; 21 | } 22 | 23 | 24 | return res; 25 | } 26 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/remove-all-adjacent-duplicates-in-string/remove-all-adjacent-duplicates-in-string.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string removeDuplicates(string S) { 4 | string ans=""; 5 | stack sStack; 6 | 7 | for(auto a:S){ 8 | if(sStack.empty()){ 9 | sStack.push(a); 10 | } 11 | else{ 12 | if(sStack.top()==a){ 13 | sStack.pop(); 14 | }else{ 15 | sStack.push(a); 16 | } 17 | } 18 | } 19 | 20 | while(!sStack.empty()){ 21 | ans=sStack.top()+ans; 22 | sStack.pop(); 23 | } 24 | return ans; 25 | } 26 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/remove-all-occurrences-of-a-substring/remove-all-occurrences-of-a-substring.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string removeOccurrences(string s, string part) { 4 | bool possible=true; 5 | while(possible){ 6 | size_t found = s.find(part); 7 | 8 | if (found != string::npos) 9 | s.erase(found,part.size()); 10 | else 11 | break; 12 | } 13 | return s; 14 | } 15 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/remove-comments/remove-comments.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector removeComments(vector& source) { 4 | vector ans; 5 | bool comment=false; 6 | string s; 7 | for(auto a:source){ 8 | for(int j=0;j& nums) { 4 | 5 | if(nums.size()<2){ 6 | return nums.size(); 7 | } 8 | 9 | auto curr = nums.begin(); 10 | auto prev = curr; 11 | curr++; 12 | while(curr!=nums.end()){ 13 | if(*curr==*prev){ 14 | nums.erase(curr); 15 | curr=prev; 16 | } 17 | else{ 18 | prev=curr; 19 | } 20 | curr++; 21 | } 22 | 23 | 24 | 25 | return nums.size(); 26 | } 27 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/reorganize-string/README.md: -------------------------------------------------------------------------------- 1 |

767. Reorganize String

Medium


Given a string S, check if the letters can be rearranged so that two characters that are adjacent to each other are not the same.

2 | 3 |

If possible, output any possible result.  If not possible, return the empty string.

4 | 5 |

Example 1:

6 | 7 |
Input: S = "aab"
 8 | Output: "aba"
 9 | 
10 | 11 |

Example 2:

12 | 13 |
Input: S = "aaab"
14 | Output: ""
15 | 
16 | 17 |

Note:

18 | 19 |
    20 |
  • S will consist of lowercase letters and have length in range [1, 500].
  • 21 |
22 | 23 |

 

24 |
-------------------------------------------------------------------------------- /Leet-Code-Question/reshape-the-matrix/reshape-the-matrix.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> matrixReshape(vector>& mat, int r, int c) { 4 | if(mat.size()*mat[0].size() !=r*c){ 5 | return mat; 6 | } 7 | 8 | 9 | vector mat1d; 10 | 11 | for(int i=0;i> t(r,vector (c,0)); 19 | int p=0; 20 | for(int i=0;inext; 19 | curr->next=prev; 20 | prev=curr; 21 | curr=temp; 22 | } 23 | 24 | return prev; 25 | } 26 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/reverse-string-ii/reverse-string-ii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void helper(string &s,int start,int end){ 4 | while(start<=end){ 5 | swap(s[start],s[end]); 6 | start++; 7 | end--; 8 | } 9 | } 10 | string reverseStr(string s, int k) { 11 | // string sp = "abc"; 12 | int i=0; 13 | int n=s.length(); 14 | while(i=2*k){ 16 | helper(s,i,i+k-1); 17 | i+=2*k; 18 | } 19 | else if(n-i<2*k && n-i>k){ 20 | helper(s,i,i+k-1); 21 | break; 22 | } 23 | else{ 24 | helper(s,i,n-1); 25 | break; 26 | } 27 | } 28 | 29 | return s; 30 | } 31 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/reverse-words-in-a-string-iii/reverse-words-in-a-string-iii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void helper(string &s,int start,int end){ 4 | while(start<=end){ 5 | swap(s[start],s[end]); 6 | start++; 7 | end--; 8 | } 9 | } 10 | string reverseWords(string s) { 11 | int start=0; 12 | int end=0; 13 | for( int i=0;i st; 5 | string temp =""; 6 | for(auto a:s){ 7 | if(a==' '){ 8 | if(temp!=""){ 9 | st.push(temp+" "); 10 | temp=""; 11 | } 12 | }else{ 13 | temp+=a; 14 | } 15 | } 16 | 17 | if(temp!=""){ 18 | st.push(temp+" "); 19 | } 20 | 21 | string ans=""; 22 | while(!st.empty()){ 23 | ans+=st.top(); 24 | st.pop(); 25 | } 26 | 27 | 28 | return ans.substr(0,ans.size()-1); 29 | } 30 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/richest-customer-wealth/richest-customer-wealth.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maximumWealth(vector>& accounts) { 4 | int maxSum = INT_MIN; 5 | int tempSum=0; 6 | for(int i=0;i> m; 6 | 7 | m['U']={0,1},m['D']={0,-1},m['L']={-1,0},m['R']={1,0}; 8 | 9 | int x=0,y=0; 10 | 11 | for(auto a:moves){ 12 | x+=m[a].first; 13 | y+=m[a].second; 14 | } 15 | 16 | return x==0 && y==0; 17 | } 18 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/rotate-image/rotate-image.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void rotate(vector>& matrix) { 4 | int rowLength = matrix.size(); 5 | int colLength = matrix[0].size()-1; 6 | int temp; 7 | // for(int j=0;j runningSum(vector& nums) { 4 | vector ans; 5 | int sum=0; 6 | for(auto a:nums){ 7 | sum+=a; 8 | ans.push_back(sum); 9 | } 10 | return ans; 11 | } 12 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/score-of-parentheses/score-of-parentheses.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int scoreOfParentheses(string s) { 4 | 5 | int sum=0; 6 | int openCount=0; 7 | 8 | for(int i=0;i& nums, int target) { 4 | int left=0; 5 | int right=nums.size()-1; 6 | // cout<target){ 26 | return i; 27 | } 28 | } 29 | return nums.size(); 30 | } 31 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/second-highest-salary/second-highest-salary.sql: -------------------------------------------------------------------------------- 1 | SELECT MAX( Salary ) as SecondHighestSalary 2 | FROM Employee 3 | WHERE Salary < ( SELECT MAX( Salary ) 4 | FROM Employee ) 5 | -------------------------------------------------------------------------------- /Leet-Code-Question/set-mismatch/set-mismatch.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector findErrorNums(vector& nums) { 4 | int n=nums.size(); 5 | int arr[n]; 6 | 7 | int p; 8 | int q; 9 | 10 | for(int i=0;i> shiftGrid(vector>& grid, int k) { 4 | int m = grid.size(),n=grid[0].size(); 5 | vector> ans(m,vector(n,0)); 6 | 7 | for(int i=0;i& nums) { 4 | vector num2=nums; 5 | sort(num2.begin(),num2.end()); 6 | 7 | int i=0,j=nums.size()-1; 8 | 9 | while(ii && nums[j]==num2[j]){ 14 | j--; 15 | } 16 | 17 | 18 | return j-i+1; 19 | } 20 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/shuffle-the-array/shuffle-the-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector shuffle(vector& nums, int n) { 4 | vector ans; 5 | int i=0,j=n; 6 | while(i& nums) { 4 | if(nums.size()==1){ 5 | return nums[0]; 6 | } 7 | sort(nums.begin(),nums.end()); 8 | for(int i=0;i singleNumber(vector& nums) { 4 | vector ans; 5 | if(nums.size()==2){ 6 | return nums; 7 | } 8 | sort(nums.begin(),nums.end()); 9 | for(int i=0;i& nums) { 4 | map m; 5 | 6 | for(auto a:nums){ 7 | m[a]++; 8 | } 9 | 10 | for(auto a:m){ 11 | if(a.second==1){ 12 | return a.first; 13 | } 14 | } 15 | 16 | return -1; 17 | } 18 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/smallest-range-ii/smallest-range-ii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int smallestRangeII(vector& nums, int k) { 4 | sort(nums.begin(),nums.end()); 5 | 6 | int height = nums.back()-nums.front(); 7 | 8 | int left=nums[0]+k,right=nums.back()-k; 9 | 10 | for(int i=0;i912. Sort an Array

Medium


Given an array of integers nums, sort the array in ascending order.

2 | 3 |

 

4 |

Example 1:

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

Example 2:

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

 

12 |

Constraints:

13 | 14 |
    15 |
  • 1 <= nums.length <= 5 * 104
  • 16 |
  • -5 * 104 <= nums[i] <= 5 * 104
  • 17 |
18 |
-------------------------------------------------------------------------------- /Leet-Code-Question/sort-colors/sort-colors.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void sortColors(vector& nums) { 4 | int zeroCount=0; 5 | int oneCount=0; 6 | int twoCount=0; 7 | 8 | for(auto a:nums){ 9 | if(a==0){ 10 | zeroCount++; 11 | }else if(a==1){ 12 | oneCount++; 13 | }else{ 14 | twoCount++; 15 | } 16 | } 17 | 18 | int i=0; 19 | 20 | while(zeroCount>0){ 21 | nums[i]=0; 22 | zeroCount--; 23 | i++; 24 | } 25 | 26 | while(oneCount>0){ 27 | nums[i]=1; 28 | oneCount--; 29 | i++; 30 | } 31 | while(twoCount>0){ 32 | nums[i]=2; 33 | twoCount--; 34 | i++; 35 | } 36 | } 37 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/sort-integers-by-the-number-of-1-bits/sort-integers-by-the-number-of-1-bits.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countBit(int n){ 4 | int count=0; 5 | while(n>0){ 6 | if(n%2==1) count++; 7 | n=n/2; 8 | } 9 | 10 | return count; 11 | } 12 | vector sortByBits(vector& arr) { 13 | sort(arr.begin(),arr.end()); 14 | map> m; 15 | for(auto a:arr){ 16 | m[countBit(a)].push_back(a); 17 | } 18 | 19 | vector ans; 20 | 21 | for(auto a:m){ 22 | for(auto p:a.second){ 23 | ans.push_back(p); 24 | } 25 | } 26 | 27 | return ans; 28 | } 29 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/spiral-matrix-ii/README.md: -------------------------------------------------------------------------------- 1 |

59. Spiral Matrix II

Medium


Given a positive integer n, generate an n x n matrix filled with elements from 1 to n2 in spiral order.

2 | 3 |

 

4 |

Example 1:

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

Example 2:

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

 

17 |

Constraints:

18 | 19 |
    20 |
  • 1 <= n <= 20
  • 21 |
22 |
-------------------------------------------------------------------------------- /Leet-Code-Question/sqrtx/sqrtx.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int mySqrt(int x) { 4 | int start = 1,end=x,ans; 5 | 6 | while(start<=end){ 7 | int mid= start+(end-start)/2; 8 | if(mid==x/mid) return mid; 9 | 10 | if(mid<=x/mid) start=mid+1,ans=mid; 11 | 12 | else end=mid-1; 13 | } 14 | 15 | 16 | return ans; 17 | } 18 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/string-to-integer-atoi/string-to-integer-atoi.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int myAtoi(string str) { 4 | int sign=1; 5 | int i = 0; 6 | while (i + 1 < str.size() && isspace(str[i])) ++i; 7 | long res = 0; 8 | if (str[i] == '-' || str[i] == '+') sign = 44 - str[i++]; 9 | while (i < str.size()) { 10 | if (isdigit(str[i])) res = 10 * res + str[i++] - '0'; 11 | else return res * sign; 12 | if (res > INT_MAX) return sign == -1 ? INT_MIN : INT_MAX; 13 | } 14 | return res * sign; 15 | 16 | } 17 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/subarray-sum-equals-k/README.md: -------------------------------------------------------------------------------- 1 |

560. Subarray Sum Equals K

Medium


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

2 | 3 |

 

4 |

Example 1:

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

Example 2:

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

 

12 |

Constraints:

13 | 14 |
    15 |
  • 1 <= nums.length <= 2 * 104
  • 16 |
  • -1000 <= nums[i] <= 1000
  • 17 |
  • -107 <= k <= 107
  • 18 |
19 |
-------------------------------------------------------------------------------- /Leet-Code-Question/subarray-sum-equals-k/subarray-sum-equals-k.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int subarraySum(vector& nums, int k) { 4 | 5 | int count=0; 6 | unordered_map m{{0,1}}; 7 | int sum=0; 8 | for(int i=0;i90. 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 |
-------------------------------------------------------------------------------- /Leet-Code-Question/subsets-ii/subsets-ii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | set> ans; 4 | void solve(int i,int n,vector temp,vector nums){ 5 | if(i==n){ 6 | sort(temp.begin(),temp.end()); 7 | ans.insert(temp); 8 | return; 9 | } 10 | 11 | solve(i+1,n,temp,nums); 12 | temp.push_back(nums[i]); 13 | solve(i+1,n,temp,nums); 14 | 15 | return; 16 | 17 | } 18 | vector> subsetsWithDup(vector& nums) { 19 | solve(0,nums.size(),{},nums); 20 | vector> res; 21 | 22 | for(auto a:ans){ 23 | res.push_back(a); 24 | } 25 | return res; 26 | } 27 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/subsets/subsets.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> ans; 4 | void solve(int n,vector temp,vector nums){ 5 | if(n==0){ 6 | ans.push_back(temp); 7 | return; 8 | } 9 | solve(n-1,temp,nums); 10 | temp.push_back(nums[n-1]); 11 | solve(n-1,temp,nums); 12 | } 13 | vector> subsets(vector& nums) { 14 | vector temp; 15 | solve(nums.size(),temp,nums); 16 | return ans; 17 | } 18 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/sum-of-digits-in-base-k/sum-of-digits-in-base-k.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int sumBase(int n, int k) { 4 | int temp =n; 5 | 6 | int sum = 0; 7 | while(temp>0){ 8 | sum+=temp%k; 9 | temp=temp/k; 10 | } 11 | return sum; 12 | } 13 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/sum-of-left-leaves/sum-of-left-leaves.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | int sumOfLeftLeaves(TreeNode* root,bool isLeft=false) { 15 | if(!root) return 0; 16 | if(!root->left && !root->right) return isLeft?root->val:0; 17 | return sumOfLeftLeaves(root->left,true) + sumOfLeftLeaves(root->right,false); 18 | } 19 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/sum-of-two-integers/README.md: -------------------------------------------------------------------------------- 1 |

371. Sum of Two Integers

Medium


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

2 | 3 |

 

4 |

Example 1:

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

Example 2:

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

 

12 |

Constraints:

13 | 14 |
    15 |
  • -1000 <= a, b <= 1000
  • 16 |
17 |
-------------------------------------------------------------------------------- /Leet-Code-Question/sum-of-two-integers/sum-of-two-integers.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int getSum(int a, int b) { 3 | int c; 4 | while(b !=0 ) { 5 | c = (a&b); 6 | a = a ^ b; 7 | b = (c)<<1; 8 | } 9 | return a; 10 | } 11 | } -------------------------------------------------------------------------------- /Leet-Code-Question/sum-of-unique-elements/sum-of-unique-elements.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int sumOfUnique(vector& nums) { 4 | unordered_map m; 5 | 6 | for(auto a:nums){ 7 | m[a]++; 8 | } 9 | 10 | int sum=0; 11 | 12 | for(auto a:m){ 13 | if(a.second==1){ 14 | sum+=a.first; 15 | } 16 | } 17 | 18 | return sum; 19 | } 20 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/swapping-nodes-in-a-linked-list/swapping-nodes-in-a-linked-list.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* swapNodes(ListNode* head, int k) { 14 | unordered_map m; 15 | int len=1; 16 | ListNode* curr = head; 17 | 18 | while(curr){ 19 | m[len]=curr; 20 | len++; 21 | curr=curr->next; 22 | } 23 | 24 | int start = k; 25 | int end = len-k; 26 | 27 | swap(m[start]->val,m[end]->val); 28 | 29 | 30 | return head; 31 | } 32 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/teemo-attacking/teemo-attacking.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findPoisonedDuration(vector& timeSeries, int duration) { 4 | int endTime=timeSeries[0]+duration; 5 | int count=duration; 6 | for(int i=1;iendTime){ 9 | // count+=duration; 10 | // }else{ 11 | // count+=timeSeries[i]-timeSeries[i-1]; 12 | // } 13 | } 14 | 15 | return count; 16 | } 17 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/third-maximum-number/third-maximum-number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int thirdMax(vector& nums) { 4 | set numsS; 5 | for(auto a:nums){ 6 | numsS.insert(a); 7 | } 8 | 9 | int t = numsS.size(); 10 | int third; 11 | if(t-3<0){ 12 | third=numsS.size()-1; 13 | }else{ 14 | third=t-3; 15 | } 16 | 17 | for(auto a:numsS){ 18 | if(third==0){ 19 | return a; 20 | break; 21 | } 22 | 23 | third--; 24 | } 25 | 26 | return 0; 27 | } 28 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/two-sum/two-sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector twoSum(vector& nums, int target) { 4 | unordered_map m; 5 | 6 | for(int i=0;i res(1,1); 5 | int i=0,j=0,k=0; 6 | 7 | while(res.size()1){ 5 | if(n%2!=0 && n%3!=0 && n%5!=0){ 6 | cout<96. Unique Binary Search Trees

Medium


Given an integer n, return the number of structurally unique BST's (binary search trees) which has exactly n nodes of unique values from 1 to n.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
Input: n = 3
 7 | Output: 5
 8 | 
9 | 10 |

Example 2:

11 | 12 |
Input: n = 1
13 | Output: 1
14 | 
15 | 16 |

 

17 |

Constraints:

18 | 19 |
    20 |
  • 1 <= n <= 19
  • 21 |
22 |
-------------------------------------------------------------------------------- /Leet-Code-Question/unique-binary-search-trees/unique-binary-search-trees.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int solve(int n) 4 | { 5 | int catalan[n + 1]; 6 | 7 | // Initialize first two values in table 8 | catalan[0] = catalan[1] = 1; 9 | 10 | for (int i = 2; i <= n; i++) 11 | { 12 | catalan[i] = 0; 13 | for (int j = 0; j < i; j++) 14 | catalan[i] += catalan[j] * catalan[i - j - 1]; 15 | } 16 | 17 | return catalan[n]; 18 | } 19 | int numTrees(int n) { 20 | 21 | return solve(n); 22 | } 23 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/unique-morse-code-words/unique-morse-code-words.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int uniqueMorseRepresentations(vector& words) { 4 | vector morse={".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."}; 5 | set s; 6 | string temp=""; 7 | for(auto a:words){ 8 | for(auto p:a){ 9 | temp+=morse[(int)p-97]; 10 | } 11 | s.insert(temp); 12 | temp=""; 13 | } 14 | // cout<<(int)'b'; 15 | 16 | 17 | return s.size(); 18 | 19 | 20 | 21 | } 22 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/unique-paths/unique-paths.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> t; 4 | int solve(int i,int j,int m,int n){ 5 | if(i<0 || j<0 || i>=m || j>=n){ 6 | return 0; 7 | } 8 | if(t[i][j]!=-1){ 9 | return t[i][j]; 10 | } 11 | if(i==m-1 && j==n-1){ 12 | t[i][j]=1; 13 | return t[i][j]; 14 | } 15 | 16 | 17 | t[i][j]=solve(i,j+1,m,n) + solve(i+1,j,m,n); 18 | 19 | return t[i][j]; 20 | 21 | } 22 | int uniquePaths(int m, int n) { 23 | t.assign(m+1, vector(n, -1)); 24 | 25 | return solve(0,0,m,n); 26 | 27 | // return count; 28 | 29 | } 30 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/valid-palindrome-ii/valid-palindrome-ii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool validPalindrome(string s) { 4 | int l=0,r=s.length()-1; 5 | while(l=j2 || i1>=j1; 11 | 12 | }else{ 13 | l++; 14 | r--; 15 | } 16 | } 17 | return true; 18 | } 19 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/valid-palindrome/valid-palindrome.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPalindrome(string s) { 4 | int l =0,r=s.length()-1; 5 | 6 | while(l<=r){ 7 | if(!isalnum(s[l])) l++; 8 | else if(!isalnum(s[r])) r--; 9 | else if(tolower(s[l])!=tolower(s[r])) return false; 10 | else {l++; r--;} 11 | } 12 | 13 | return true; 14 | } 15 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/valid-parenthesis-string/valid-parenthesis-string.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool checkValidString(string s) { 4 | stack o_pert; 5 | stack ast; 6 | 7 | for(int i=0;io_pert.top()){ 20 | ast.pop(); 21 | o_pert.pop(); 22 | } 23 | 24 | else return false; 25 | } 26 | return true; 27 | } 28 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/valid-perfect-square/valid-perfect-square.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPerfectSquare(int x) { 4 | int start=1,end=x; 5 | 6 | 7 | while(start<=end){ 8 | double mid= start+(end-start)/2; 9 | if(double(mid)-double(x/mid)==0){ 10 | // cout<<(double)x/mid<<" "< h; 4 | 5 | bool checkOrder(string& a,string& b){ 6 | int i=-1; 7 | while(++i& words, string order) { 14 | int i=0; 15 | for(auto a: order){ 16 | h[a]=i; 17 | i++; 18 | } 19 | 20 | for(i=0;i0 && numBottles>=numExchange){ 6 | ans+=numBottles/numExchange; 7 | numBottles=numBottles/numExchange+numBottles%numExchange; 8 | } 9 | 10 | return ans; 11 | } 12 | }; -------------------------------------------------------------------------------- /Leet-Code-Question/word-subsets/word-subsets.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector wordSubsets(vector& A, vector& B) { 4 | vector count(26), tmp(26); 5 | int i; 6 | for (string b : B) { 7 | tmp = counter(b); 8 | for (i = 0; i < 26; ++i) 9 | count[i] = max(count[i], tmp[i]); 10 | } 11 | vector res; 12 | for (string a : A) { 13 | tmp = counter(a); 14 | for (i = 0; i < 26; ++i) 15 | if (tmp[i] < count[i]) 16 | break; 17 | if (i == 26) res.push_back(a); 18 | } 19 | return res; 20 | } 21 | 22 | vector counter(string& word) { 23 | vector count(26); 24 | for (char c : word) count[c - 'a']++; 25 | return count; 26 | } 27 | }; -------------------------------------------------------------------------------- /array/012sort/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/array/012sort/a.exe -------------------------------------------------------------------------------- /array/alternateNegPositive/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/array/alternateNegPositive/a.exe -------------------------------------------------------------------------------- /array/bestTimeToBuyStock/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int maxProfit(vector &prices) 5 | { 6 | int profit = 0; 7 | int buy = prices[0]; 8 | int sell; 9 | for (int i = 0; i < prices.size(); i++) 10 | { 11 | if (prices[i] > buy) 12 | { 13 | profit = max(profit, prices[i] - buy); 14 | } 15 | buy = min(prices[i], buy); 16 | } 17 | 18 | return profit; 19 | } 20 | }; -------------------------------------------------------------------------------- /array/commonElements/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/array/commonElements/a.exe -------------------------------------------------------------------------------- /array/commonElements/main.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/array/commonElements/main.exe -------------------------------------------------------------------------------- /array/cyclicRotation/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/array/cyclicRotation/a.exe -------------------------------------------------------------------------------- /array/cyclicRotation/main.cpp: -------------------------------------------------------------------------------- 1 | // { Driver Code Starts 2 | #include 3 | 4 | void rotate(int arr[], int n); 5 | 6 | int main() 7 | { 8 | int t; 9 | scanf("%d",&t); 10 | while(t--) 11 | { 12 | int n; 13 | scanf("%d",&n); 14 | int a[n] , i; 15 | for(i=0;i 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | int arr[] = {7 ,10 ,4 ,3, 20 ,15}; 8 | int n = 6; 9 | int k =3; 10 | 11 | sort(arr, arr + n, greater()); 12 | // sort(arr.begin(), arr.end()); 13 | 14 | cout<& prices) { 4 | if(prices.size()<2){ 5 | return 0; 6 | } 7 | int minPrice=prices[0]; 8 | int maxProfit=prices[1]-prices[0]; 9 | for(int i=0;i 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | int minElement=11,maxElement=11; 7 | int arr[] = {11, 21, 3,10, 14, 5, 16}; 8 | int arrsize = sizeof(arr)/sizeof(arr[0]); 9 | 10 | for(int i=1;i= k){ 8 | max_elem = max(arr[i-1] + k, arr[n-1] - k ); 9 | min_elem = min(arr[0] + k, arr[i] - k ); 10 | ans = min(ans, max_elem - min_elem); 11 | } 12 | else continue; 13 | } 14 | return ans; 15 | } -------------------------------------------------------------------------------- /array/moreThanN/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/array/moreThanN/a.exe -------------------------------------------------------------------------------- /array/moreThanN/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void moreThanN(int arr[],int n,int k){ 6 | unordered_map m; 7 | vector ans; 8 | int conditionNumber= n/k; 9 | for(int i=0;iconditionNumber){ 15 | ans.push_back(a.first); 16 | } 17 | } 18 | 19 | 20 | for(auto a:ans){cout<>n>>k; 26 | 27 | int arr[n]; 28 | for(int i=0;i>arr[i]; 30 | } 31 | 32 | moreThanN(arr,n,k); 33 | 34 | 35 | 36 | return 0; 37 | } -------------------------------------------------------------------------------- /array/moveNegative/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/array/moveNegative/a.exe -------------------------------------------------------------------------------- /array/rearrangeNegativePositive/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/array/rearrangeNegativePositive/a.exe -------------------------------------------------------------------------------- /array/rearrangeNegativePositive/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void rearrangeNegativePositive(int arr[],int n){ 6 | int j=-1; 7 | for(int i=0;i>n; 30 | int arr[n] ; 31 | for(int i=0; i>arr[i]; 33 | } 34 | rearrangeNegativePositive(arr,n); 35 | 36 | return 0; 37 | } -------------------------------------------------------------------------------- /array/reverseArray/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/array/reverseArray/a.exe -------------------------------------------------------------------------------- /array/reverseArray/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | int arr[] = {1, 2, 3,10, 4, 5, 6}; 7 | int arrsize = sizeof(arr)/sizeof(arr[0]); 8 | int temp; 9 | for(auto a:arr){ 10 | cout<< a <<" "; 11 | } 12 | cout<< endl; 13 | for(int i=0; i &prices) 5 | { 6 | 7 | int n = prices.size(); 8 | vector profit(prices.size(), 0); 9 | 10 | int max_price = prices[n - 1]; 11 | 12 | for (int i = n - 2; i >= 0; i--) 13 | { 14 | if (prices[i] > max_price) 15 | max_price = prices[i]; 16 | 17 | profit[i] = max(profit[i + 1], max_price - prices[i]); 18 | } 19 | 20 | int min_price = prices[0]; 21 | for (int i = 1; i < n; i++) 22 | { 23 | if (prices[i] < min_price) 24 | min_price = prices[i]; 25 | 26 | profit[i] = max(profit[i - 1], profit[i] + prices[i] - min_price); 27 | } 28 | 29 | int ans = profit[n - 1]; 30 | profit.clear(); 31 | 32 | return ans; 33 | } 34 | }; -------------------------------------------------------------------------------- /array/toolBox/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/array/toolBox/a.exe -------------------------------------------------------------------------------- /array/unionIntersection/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/array/unionIntersection/a.exe -------------------------------------------------------------------------------- /array/waterJugMaths/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/array/waterJugMaths/a.exe -------------------------------------------------------------------------------- /backtracking/main.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/backtracking/main.exe -------------------------------------------------------------------------------- /graphs/BFS/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/graphs/BFS/a.exe -------------------------------------------------------------------------------- /graphs/OliverAndTheGame/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/graphs/OliverAndTheGame/a.exe -------------------------------------------------------------------------------- /graphs/OliverAndTheGame/mainc.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/graphs/OliverAndTheGame/mainc.exe -------------------------------------------------------------------------------- /graphs/bipartite/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/graphs/bipartite/a.exe -------------------------------------------------------------------------------- /graphs/bridgeInAgraph/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/graphs/bridgeInAgraph/a.exe -------------------------------------------------------------------------------- /graphs/canFinishJob/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/graphs/canFinishJob/a.exe -------------------------------------------------------------------------------- /graphs/creatingGraphs/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/graphs/creatingGraphs/a.exe -------------------------------------------------------------------------------- /graphs/creatingGraphs/matrix.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/graphs/creatingGraphs/matrix.cpp -------------------------------------------------------------------------------- /graphs/creatingGraphs/vector.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void addEdge(vector adj[],int u,int v){ 6 | adj[u].push_back(v); 7 | adj[v].push_back(u); 8 | 9 | } 10 | 11 | void printGraph(vector adj[],int V){ 12 | for(int v=0;v"< adj[V]; 24 | addEdge(adj,0,1); 25 | addEdge(adj,0,4); 26 | addEdge(adj,1,2); 27 | addEdge(adj,1,3); 28 | addEdge(adj,1,4); 29 | addEdge(adj,2,3); 30 | addEdge(adj,3,4); 31 | 32 | printGraph(adj,V); 33 | 34 | 35 | 36 | 37 | return 0; 38 | } -------------------------------------------------------------------------------- /graphs/dijkstra/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/graphs/dijkstra/a.exe -------------------------------------------------------------------------------- /graphs/distancePathK/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/graphs/distancePathK/a.exe -------------------------------------------------------------------------------- /graphs/longestPathAcyclic/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/graphs/longestPathAcyclic/a.exe -------------------------------------------------------------------------------- /graphs/longestPathAcyclic/main.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/graphs/longestPathAcyclic/main.exe -------------------------------------------------------------------------------- /graphs/minTimeForEachJob/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/graphs/minTimeForEachJob/a.exe -------------------------------------------------------------------------------- /graphs/pathToTravel/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void solve(vector adj[],int V,int E,vector &ans,bool &isPossible){ 6 | vector visited(V+1,false); 7 | 8 | queue q; 9 | 10 | 11 | } 12 | 13 | int main() 14 | { 15 | 16 | int V, E; 17 | cin >> V >> E; 18 | 19 | vector adj[V + 1]; 20 | 21 | for (int i = 0; i < E; i++) 22 | { 23 | int u, v; 24 | cin >> u >> v; 25 | 26 | adj[u].push_back(v); 27 | adj[v].push_back(u); 28 | } 29 | 30 | vector ans; 31 | bool isPossible = true; 32 | 33 | solve(adj,V,E,ans,isPossible); 34 | } -------------------------------------------------------------------------------- /graphs/prismMininumSpanning/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/graphs/prismMininumSpanning/a.exe -------------------------------------------------------------------------------- /graphs/reverseToMakePath/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/graphs/reverseToMakePath/a.exe -------------------------------------------------------------------------------- /graphs/toTheMoon/main..exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/graphs/toTheMoon/main..exe -------------------------------------------------------------------------------- /graphs/unionFind/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/graphs/unionFind/a.exe -------------------------------------------------------------------------------- /graphs/waterJug/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/graphs/waterJug/a.exe -------------------------------------------------------------------------------- /greedy/islandSurvival/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/greedy/islandSurvival/a.exe -------------------------------------------------------------------------------- /greedy/islandSurvival/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void findSurvival(int S, int N, int M) 6 | { 7 | 8 | if (((N * 6) < (7 * M) && S > 6) || M > N) 9 | { 10 | cout << "No"; 11 | } 12 | else 13 | { 14 | int days = (M * S) / N; 15 | 16 | if (((M * S) % N != 0)) 17 | { 18 | days++; 19 | } 20 | 21 | cout << "Yes " << days << "\n"; 22 | } 23 | } 24 | 25 | int main() 26 | { 27 | int S, N, M; 28 | cin >> S >> N >> M; 29 | 30 | findSurvival(S, N, M); 31 | 32 | return 0; 33 | } -------------------------------------------------------------------------------- /greedy/maximumAbsolute/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/greedy/maximumAbsolute/a.exe -------------------------------------------------------------------------------- /greedy/maximumAbsolute/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void solve(int arr[], int n){ 6 | vector ansV; 7 | int i=0; 8 | int j =n-1; 9 | 10 | sort(arr,arr+n); 11 | 12 | while(i 2 | 3 | using namespace std; 4 | 5 | 6 | void solve(int a[],int n,int b[],int m){ 7 | sort(a,a+n); 8 | sort(b,b+n); 9 | 10 | int sum = 0; 11 | 12 | for(int i=0;i 2 | 3 | using namespace std; 4 | 5 | void solve(int arr[],int n){ 6 | sort(arr,arr+n,greater()); 7 | 8 | vector ans; 9 | int totalSum = 0; 10 | int tempSum=0; 11 | 12 | for(int i=0;itotalSum/2){ 20 | break; 21 | } 22 | } 23 | 24 | cout <next && fast->next->next) { 10 | slow = slow->next; 11 | fast = fast->next->next; 12 | if (slow == fast) 13 | return true; 14 | } 15 | 16 | return false; 17 | } 18 | }; -------------------------------------------------------------------------------- /matrix/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/matrix/a.exe -------------------------------------------------------------------------------- /misc/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/misc/a.exe -------------------------------------------------------------------------------- /misc/articulationPoint/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/misc/articulationPoint/a.exe -------------------------------------------------------------------------------- /misc/awesomeSort/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/misc/awesomeSort/a.exe -------------------------------------------------------------------------------- /misc/breakTheNode/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/misc/breakTheNode/a.exe -------------------------------------------------------------------------------- /misc/breakTheNode/main.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/misc/breakTheNode/main.exe -------------------------------------------------------------------------------- /misc/decimalToBinary.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | 6 | int main(){ 7 | 8 | int n ; 9 | cin>>n; 10 | int 11 | int x = log(n)/log(2); 12 | 13 | int i; 14 | 15 | // cout<0;i++){ 20 | binArr[i] = n%2; 21 | n=n/2; 22 | } 23 | 24 | for(i=i-1 ;i>=0 ;i--) 25 | { 26 | cout< 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | 8 | vector arr = {13, 5, 14, 12, 8, 30, 15}; 9 | int n = arr.size(); 10 | int sum = 0; 11 | 12 | for (int i = 0; i < n; i++) 13 | { 14 | if (i == 0) 15 | { 16 | if (arr[i] > arr[i + 1]) 17 | sum += arr[i]; 18 | } 19 | else if (i == n - 1) 20 | { 21 | if (arr[i] > arr[i - 1]) 22 | sum += arr[i]; 23 | } 24 | else 25 | { 26 | if (arr[i] > arr[i - 1] && arr[i] > arr[i + 1]) 27 | sum += arr[i]; 28 | } 29 | } 30 | 31 | cout << sum; 32 | return 0; 33 | } -------------------------------------------------------------------------------- /misc/rushab/first/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/misc/rushab/first/a.exe -------------------------------------------------------------------------------- /misc/rushab/second/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class BigBoomer : public Item 6 | { 7 | private: 8 | const float fAttackPeriod; 9 | float fTimeUntilNextAttack = 0.9f; 10 | void Fire() 11 | { 12 | // deal magical damage in a 40 unit radius 13 | } 14 | 15 | public: 16 | void Tick(float fDeltaSeconds) 17 | { 18 | // Fire Big Boomer every "fAttackPeriod" seconds 19 | fTimeUntilNextAttack -= fDeltaSeconds; 20 | if (fTimeUntilNextAttack <= 0.0f) 21 | { 22 | Fire(); 23 | fTimeUntilNextAttack = fAttackPeriod; 24 | } 25 | } 26 | } 27 | 28 | int 29 | main() 30 | { 31 | // fAttackPeriod = 0.9 and Frame time = 1.0 seconds 32 | BigBoomer boomer; 33 | // boomer.fTimeUntilNextAttack = 0.9f; 34 | 35 | boomer.Tick(1.0f); 36 | 37 | return 0; 38 | } -------------------------------------------------------------------------------- /misc/rushab/third/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/misc/rushab/third/a.exe -------------------------------------------------------------------------------- /misc/rushab/third/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int t[1001]; 6 | 7 | int solve(const vector &HighScores, int i, int n) 8 | { 9 | if (i >= n) 10 | return 0; 11 | 12 | if (t[i]!=-1) return t[i]; 13 | 14 | int left = solve(HighScores, i + 2, n) + HighScores[i]; 15 | int right = solve(HighScores, i + 1, n); 16 | 17 | return t[i]=max(left, right); 18 | } 19 | int GetMaxValue(const vector &HighScores) 20 | { 21 | memset(t, -1, sizeof(t)); 22 | return solve(HighScores, 0, HighScores.size()); 23 | } 24 | int main() 25 | { 26 | int n; 27 | cin >> n; 28 | vector v; 29 | for (int i = 0; i < n; i++) 30 | { 31 | int x; 32 | cin >> x; 33 | v.push_back(x); 34 | } 35 | 36 | cout << GetMaxValue(v); 37 | return 0; 38 | } -------------------------------------------------------------------------------- /misc/timeComplex/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/misc/timeComplex/a.exe -------------------------------------------------------------------------------- /misc/timeComplex/main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/misc/timeComplex/main.class -------------------------------------------------------------------------------- /misc/timeComplex/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | int i=0; 7 | int j; 8 | cin>>j; 9 | 10 | j=(j<<1) + (j>>1) + j; 11 | cout< mp = new HashMap(); 7 | 8 | mp.put(11,"abc"); 9 | mp.put(13,"absc"); 10 | mp.put(null,null); 11 | 12 | System.out.println(mp.size()); 13 | 14 | System.out.println(mp); 15 | } 16 | } -------------------------------------------------------------------------------- /searchingAndSorting/findPair/FindPair.java: -------------------------------------------------------------------------------- 1 | import java.util.HashSet; 2 | import java.util.Set; 3 | import java.util.Stack; 4 | 5 | public class FindPair { 6 | 7 | static int findPairOfInt(int arr[],int N,int x){ 8 | Set hash_Set = new HashSet(); 9 | for(int a:arr){ 10 | hash_Set.add(a); 11 | } 12 | 13 | for(int i:arr){ 14 | if(hash_Set.contains(Math.abs(i+x)) || hash_Set.contains(Math.abs(i-x))){ 15 | return 1; 16 | } 17 | } 18 | 19 | return -1; 20 | 21 | } 22 | 23 | public static void main(String[] args){ 24 | int n=6; 25 | int arr[]={5,20,3,2,5,80}; 26 | int x=78; 27 | System.out.println(findPairOfInt(arr,n,x)); 28 | 29 | 30 | // System.out.println("hello world"); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /searchingAndSorting/firstAndLastocc/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/searchingAndSorting/firstAndLastocc/a.exe -------------------------------------------------------------------------------- /searchingAndSorting/firstAndLastocc/main.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/searchingAndSorting/firstAndLastocc/main.exe -------------------------------------------------------------------------------- /searchingAndSorting/searchAdjacentK/AdjacentDiffer.java: -------------------------------------------------------------------------------- 1 | public class AdjacentDiffer { 2 | 3 | static void search(int arr[],int x,int k){ 4 | int i=0; 5 | while(i0) 12 | i=i+Math.abs(arr[i]-x)/k; 13 | else 14 | i++; 15 | } 16 | } 17 | 18 | } 19 | public static void main(String[] args){ 20 | int arr[] = {20, 40, 50, 70, 70, 60} ; 21 | int x = 60; 22 | int k=20; 23 | 24 | search(arr,x,k); 25 | 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /stacks&queues/queueUsing2Stack/main.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/stacks&queues/queueUsing2Stack/main.cpp -------------------------------------------------------------------------------- /string/KMP/kmp.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/string/KMP/kmp.class -------------------------------------------------------------------------------- /string/binaryStringSplit/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/string/binaryStringSplit/a.exe -------------------------------------------------------------------------------- /string/binaryStringSplit/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int check_string(string str){ 6 | int ctn=0; 7 | int ctn1=0,ctn0=0; 8 | 9 | for(auto a:str){ 10 | if(a=='0'){ 11 | ctn0++; 12 | } 13 | else if(a=='1'){ 14 | ctn1++; 15 | } 16 | 17 | if(ctn0==ctn1){ 18 | ctn++; 19 | } 20 | } 21 | 22 | if(ctn0!=ctn1){ 23 | return -1; 24 | } 25 | return ctn; 26 | } 27 | 28 | int main(){ 29 | string str; 30 | cin>>str; 31 | cout< 2 | 3 | using namespace std; 4 | 5 | bool isPalindrome(string s){ 6 | int l = s.length(); 7 | int j; 8 | for(int i=0,j=l-1;i<=j;i++,j--){ 9 | if(s[i]!=s[j]){ 10 | return false; 11 | } 12 | }return true; 13 | } 14 | 15 | 16 | int main(){ 17 | string s = "AACECAAAAA"; 18 | int cnt = 0; 19 | bool flag = 0; 20 | 21 | while(s.length()>0){ 22 | if(isPalindrome(s)){ 23 | flag = 1; 24 | break; 25 | } 26 | else{ 27 | cnt++; 28 | s.erase(s.begin() +s.length()-1); 29 | } 30 | } 31 | 32 | if(flag){ 33 | cout< 2 | 3 | using namespace std; 4 | 5 | void print_subsequences(string input,string output){ 6 | if(input.length() ==0){ 7 | cout<>str; 18 | string output=""; 19 | 20 | print_subsequences(str,output); 21 | 22 | 23 | return 0; 24 | } -------------------------------------------------------------------------------- /string/wordWrap/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/string/wordWrap/a.exe -------------------------------------------------------------------------------- /tree/TreeOrNot/main.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/tree/TreeOrNot/main.cpp -------------------------------------------------------------------------------- /tree/bracketsToTree/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/tree/bracketsToTree/a.exe -------------------------------------------------------------------------------- /tree/diagonalTraversal/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/tree/diagonalTraversal/a.exe -------------------------------------------------------------------------------- /tree/findIntervals/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/tree/findIntervals/a.exe -------------------------------------------------------------------------------- /tree/findLargestSubtreeSum/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/version0chiro/Ace-The-Code/c4b2cf391634dc2e8d1e9acc559a9dfd44cbd14e/tree/findLargestSubtreeSum/a.exe --------------------------------------------------------------------------------