├── 0 - 1 Knapsack Problem - GFG ├── 0-1-knapsack-problem.cpp └── README.md ├── 01-matrix ├── 01-matrix.cpp └── README.md ├── 1-bit-and-2-bit-characters ├── 1-bit-and-2-bit-characters.cpp └── README.md ├── 1-two-sum ├── 1-two-sum.cpp ├── NOTES.md └── README.md ├── 100-same-tree ├── 100-same-tree.cpp ├── NOTES.md └── README.md ├── 1009-complement-of-base-10-integer ├── 1009-complement-of-base-10-integer.cpp ├── NOTES.md └── README.md ├── 1010-pairs-of-songs-with-total-durations-divisible-by-60 ├── 1010-pairs-of-songs-with-total-durations-divisible-by-60.cpp ├── NOTES.md └── README.md ├── 1026-maximum-difference-between-node-and-ancestor ├── 1026-maximum-difference-between-node-and-ancestor.cpp ├── NOTES.md └── README.md ├── 1029-two-city-scheduling ├── 1029-two-city-scheduling.cpp ├── NOTES.md └── README.md ├── 1041-robot-bounded-in-circle ├── 1041-robot-bounded-in-circle.cpp ├── NOTES.md └── README.md ├── 1044-longest-duplicate-substring ├── 1044-longest-duplicate-substring.cpp ├── NOTES.md └── README.md ├── 1048-longest-string-chain ├── 1048-longest-string-chain.cpp ├── NOTES.md └── README.md ├── 105-construct-binary-tree-from-preorder-and-inorder-traversal ├── 105-construct-binary-tree-from-preorder-and-inorder-traversal.cpp ├── NOTES.md └── README.md ├── 1080-insufficient-nodes-in-root-to-leaf-paths ├── 1080-insufficient-nodes-in-root-to-leaf-paths.cpp ├── NOTES.md └── README.md ├── 1094-car-pooling ├── 1094-car-pooling.cpp ├── NOTES.md └── README.md ├── 1186-maximum-subarray-sum-with-one-deletion ├── 1186-maximum-subarray-sum-with-one-deletion.cpp ├── NOTES.md └── README.md ├── 1192-critical-connections-in-a-network ├── 1192-critical-connections-in-a-network.cpp ├── NOTES.md └── README.md ├── 124-binary-tree-maximum-path-sum ├── 124-binary-tree-maximum-path-sum.cpp ├── NOTES.md └── README.md ├── 1291-sequential-digits ├── 1291-sequential-digits.cpp ├── NOTES.md └── README.md ├── 132-pattern ├── 132-pattern.cpp └── README.md ├── 1332-remove-palindromic-subsequences ├── 1332-remove-palindromic-subsequences.cpp ├── NOTES.md └── README.md ├── 134-gas-station ├── 134-gas-station.cpp ├── NOTES.md └── README.md ├── 1377-frog-position-after-t-seconds ├── 1377-frog-position-after-t-seconds.cpp ├── NOTES.md └── README.md ├── 139-word-break ├── 139-word-break.cpp ├── NOTES.md └── README.md ├── 1390-four-divisors ├── 1390-four-divisors.cpp ├── NOTES.md └── README.md ├── 142-linked-list-cycle-ii ├── 142-linked-list-cycle-ii.cpp ├── NOTES.md └── README.md ├── 1443-minimum-time-to-collect-all-apples-in-a-tree ├── 1443-minimum-time-to-collect-all-apples-in-a-tree.cpp ├── NOTES.md └── README.md ├── 146-lru-cache ├── 146-lru-cache.cpp ├── NOTES.md └── README.md ├── 1480-running-sum-of-1d-array ├── 1480-running-sum-of-1d-array.cpp ├── NOTES.md └── README.md ├── 1556-thousand-separator ├── 1556-thousand-separator.cpp └── NOTES.md ├── 160-intersection-of-two-linked-lists ├── 160-intersection-of-two-linked-lists.cpp ├── NOTES.md └── README.md ├── 1658-minimum-operations-to-reduce-x-to-zero ├── 1658-minimum-operations-to-reduce-x-to-zero.cpp ├── NOTES.md └── README.md ├── 1678-goal-parser-interpretation ├── 1678-goal-parser-interpretation.cpp ├── NOTES.md └── README.md ├── 1680-concatenation-of-consecutive-binary-numbers ├── 1680-concatenation-of-consecutive-binary-numbers.cpp ├── NOTES.md └── README.md ├── 1695-maximum-erasure-value ├── 1695-maximum-erasure-value.cpp ├── NOTES.md └── README.md ├── 1710-maximum-units-on-a-truck ├── 1710-maximum-units-on-a-truck.cpp └── NOTES.md ├── 172-factorial-trailing-zeroes ├── 172-factorial-trailing-zeroes.cpp ├── NOTES.md └── README.md ├── 179-largest-number ├── 179-largest-number.cpp ├── NOTES.md └── README.md ├── 181-employees-earning-more-than-their-managers ├── 181-employees-earning-more-than-their-managers.sql ├── NOTES.md └── README.md ├── 1823-find-the-winner-of-the-circular-game ├── 1823-find-the-winner-of-the-circular-game.cpp ├── NOTES.md └── README.md ├── 1926-nearest-exit-from-entrance-in-maze ├── 1926-nearest-exit-from-entrance-in-maze.cpp ├── NOTES.md └── README.md ├── 1953-maximum-number-of-weeks-for-which-you-can-work ├── 1953-maximum-number-of-weeks-for-which-you-can-work.cpp ├── NOTES.md └── README.md ├── 1961-check-if-string-is-a-prefix-of-array ├── 1961-check-if-string-is-a-prefix-of-array.cpp ├── NOTES.md └── README.md ├── 200-number-of-islands ├── 200-number-of-islands.cpp ├── NOTES.md └── README.md ├── 2096-step-by-step-directions-from-a-binary-tree-node-to-another ├── 2096-step-by-step-directions-from-a-binary-tree-node-to-another.cpp ├── NOTES.md └── README.md ├── 2129-capitalize-the-title ├── 2129-capitalize-the-title.cpp ├── NOTES.md └── README.md ├── 226-invert-binary-tree ├── 226-invert-binary-tree.cpp ├── NOTES.md └── README.md ├── 235-lowest-common-ancestor-of-a-binary-search-tree ├── 235-lowest-common-ancestor-of-a-binary-search-tree.cpp ├── NOTES.md └── README.md ├── 238-product-of-array-except-self ├── 238-product-of-array-except-self.cpp ├── NOTES.md └── README.md ├── 287-find-the-duplicate-number ├── 287-find-the-duplicate-number.cpp ├── NOTES.md └── README.md ├── 295-find-median-from-data-stream ├── 295-find-median-from-data-stream.cpp ├── NOTES.md └── README.md ├── 3 sum closest - GFG ├── 3-sum-closest.cpp └── README.md ├── 3-longest-substring-without-repeating-characters ├── 3-longest-substring-without-repeating-characters.cpp ├── NOTES.md └── README.md ├── 304-range-sum-query-2d-immutable ├── 304-range-sum-query-2d-immutable.cpp ├── NOTES.md └── README.md ├── 31-next-permutation ├── 31-next-permutation.cpp ├── NOTES.md └── README.md ├── 32-longest-valid-parentheses ├── 32-longest-valid-parentheses.cpp └── README.md ├── 389-find-the-difference ├── 389-find-the-difference.cpp ├── NOTES.md └── README.md ├── 392-is-subsequence ├── 392-is-subsequence.cpp └── README.md ├── 394-decode-string ├── 394-decode-string.cpp ├── NOTES.md └── README.md ├── 397-integer-replacement ├── 397-integer-replacement.cpp ├── NOTES.md └── README.md ├── 3sum-closest ├── 3sum-closest.cpp └── README.md ├── 4-median-of-two-sorted-arrays ├── 4-median-of-two-sorted-arrays.cpp ├── NOTES.md └── README.md ├── 402-remove-k-digits ├── 402-remove-k-digits.cpp ├── NOTES.md └── README.md ├── 42-trapping-rain-water ├── 42-trapping-rain-water.cpp ├── NOTES.md └── README.md ├── 438-find-all-anagrams-in-a-string ├── 438-find-all-anagrams-in-a-string.cpp ├── NOTES.md └── README.md ├── 442-find-all-duplicates-in-an-array ├── 442-find-all-duplicates-in-an-array.cpp ├── NOTES.md └── README.md ├── 450-delete-node-in-a-bst ├── 450-delete-node-in-a-bst.cpp ├── NOTES.md └── README.md ├── 456-132-pattern ├── 456-132-pattern.cpp ├── NOTES.md └── README.md ├── 476-number-complement ├── 476-number-complement.cpp ├── NOTES.md └── README.md ├── 496-next-greater-element-i ├── 496-next-greater-element-i.cpp ├── NOTES.md └── README.md ├── 4sum ├── 4sum.cpp └── README.md ├── 5-longest-palindromic-substring ├── 5-longest-palindromic-substring.cpp ├── NOTES.md └── README.md ├── 503-next-greater-element-ii ├── 503-next-greater-element-ii.cpp ├── NOTES.md └── README.md ├── 51-n-queens ├── 51-n-queens.cpp ├── NOTES.md └── README.md ├── 52-n-queens-ii ├── 52-n-queens-ii.cpp ├── NOTES.md └── README.md ├── 520-detect-capital ├── 520-detect-capital.cpp ├── NOTES.md └── README.md ├── 529-minesweeper ├── 529-minesweeper.cpp ├── NOTES.md └── README.md ├── 547-number-of-provinces └── NOTES.md ├── 56-merge-intervals ├── 56-merge-intervals.cpp ├── NOTES.md └── README.md ├── 583-delete-operation-for-two-strings ├── 583-delete-operation-for-two-strings.cpp ├── NOTES.md └── README.md ├── 605-can-place-flowers ├── 605-can-place-flowers.cpp └── NOTES.md ├── 662-maximum-width-of-binary-tree ├── 662-maximum-width-of-binary-tree.cpp └── README.md ├── 69-sqrtx ├── 69-sqrtx.cpp ├── NOTES.md └── README.md ├── 690-employee-importance ├── 690-employee-importance.cpp ├── NOTES.md └── README.md ├── 709-to-lower-case ├── 709-to-lower-case.cpp ├── NOTES.md └── README.md ├── 71-simplify-path ├── 71-simplify-path.cpp ├── NOTES.md └── README.md ├── 739-daily-temperatures ├── 739-daily-temperatures.cpp ├── NOTES.md └── README.md ├── 76-minimum-window-substring ├── 76-minimum-window-substring.cpp ├── NOTES.md └── README.md ├── 763-partition-labels ├── 763-partition-labels.cpp ├── NOTES.md └── README.md ├── 767-reorganize-string ├── 767-reorganize-string.cpp ├── NOTES.md └── README.md ├── 802-find-eventual-safe-states ├── 802-find-eventual-safe-states.cpp ├── NOTES.md └── README.md ├── 804-unique-morse-code-words ├── 804-unique-morse-code-words.cpp └── NOTES.md ├── 819-most-common-word ├── 819-most-common-word.cpp ├── NOTES.md └── README.md ├── 84-largest-rectangle-in-histogram ├── 84-largest-rectangle-in-histogram.cpp ├── NOTES.md └── README.md ├── 85-maximal-rectangle ├── 85-maximal-rectangle.cpp ├── NOTES.md └── README.md ├── 863-all-nodes-distance-k-in-binary-tree ├── 863-all-nodes-distance-k-in-binary-tree.cpp ├── NOTES.md └── README.md ├── 867-transpose-matrix ├── 867-transpose-matrix.cpp ├── NOTES.md └── README.md ├── 88-merge-sorted-array ├── 88-merge-sorted-array.cpp ├── NOTES.md └── README.md ├── 894-all-possible-full-binary-trees ├── 894-all-possible-full-binary-trees.cpp ├── NOTES.md └── README.md ├── 901-online-stock-span ├── 901-online-stock-span.cpp ├── NOTES.md └── README.md ├── 904-fruit-into-baskets ├── 904-fruit-into-baskets.cpp ├── NOTES.md └── README.md ├── 91-decode-ways ├── 91-decode-ways.cpp ├── NOTES.md └── README.md ├── 921-minimum-add-to-make-parentheses-valid ├── 921-minimum-add-to-make-parentheses-valid.cpp ├── NOTES.md └── README.md ├── 937-reorder-data-in-log-files ├── 937-reorder-data-in-log-files.cpp ├── NOTES.md └── README.md ├── 941-valid-mountain-array ├── 941-valid-mountain-array.cpp ├── NOTES.md └── README.md ├── 97-interleaving-string ├── 97-interleaving-string.cpp ├── NOTES.md └── README.md ├── 973-k-closest-points-to-origin ├── 973-k-closest-points-to-origin.cpp ├── NOTES.md └── README.md ├── Absolute List Sorting - GFG ├── README.md └── absolute-list-sorting.java ├── Add 1 to a number represented as linked list - GFG ├── README.md └── add-1-to-a-number-represented-as-linked-list.cpp ├── Add Binary Strings - GFG ├── README.md └── add-binary-strings.cpp ├── Add all greater values to every node in a BST - GFG ├── README.md └── add-all-greater-values-to-every-node-in-a-bst.cpp ├── Add two numbers represented by linked lists - GFG ├── README.md └── add-two-numbers-represented-by-linked-lists.cpp ├── Alien Dictionary - GFG ├── README.md └── alien-dictionary.cpp ├── Ancestors in Binary Tree - GFG ├── README.md └── ancestors-in-binary-tree.cpp ├── Anti Diagonal Traversal of Matrix - GFG ├── README.md └── anti-diagonal-traversal-of-matrix.cpp ├── BFS of graph - GFG ├── README.md └── bfs-of-graph.cpp ├── BST to greater sum tree - GFG ├── README.md └── bst-to-greater-sum-tree.cpp ├── Binary Tree to DLL - GFG ├── README.md └── binary-tree-to-dll.cpp ├── Binary Tree to CDLL - GFG ├── README.md └── binary-tree-to-cdll.cpp ├── Binary Tree to DLL - GFG ├── README.md └── binary-tree-to-dll.cpp ├── Bipartite Graph - GFG ├── README.md └── bipartite-graph.cpp ├── Boolean Matrix - GFG ├── README.md └── boolean-matrix.cpp ├── Bottom View of Binary Tree - GFG ├── README.md └── bottom-view-of-binary-tree.cpp ├── Boundary Traversal of binary tree - GFG ├── README.md └── boundary-traversal-of-binary-tree.cpp ├── Bridge edge in a graph - GFG ├── README.md └── bridge-edge-in-a-graph.cpp ├── Burning Tree - GFG ├── README.md └── burning-tree.cpp ├── Buy and Sell a Share at most twice - GFG ├── README.md └── buy-and-sell-a-share-at-most-twice.cpp ├── Check for BST - GFG ├── README.md └── check-for-bst.cpp ├── Check for Balanced Tree - GFG ├── README.md └── check-for-balanced-tree.cpp ├── Check if Linked List is Palindrome - GFG ├── README.md └── check-if-linked-list-is-palindrome.cpp ├── Check if string is rotated by two places - GFG ├── README.md └── check-if-string-is-rotated-by-two-places.cpp ├── Check if subtree - GFG ├── README.md └── check-if-subtree.cpp ├── Circular tour - GFG ├── README.md └── circular-tour.cpp ├── Clone a Binary Tree - GFG ├── README.md └── clone-a-binary-tree.cpp ├── Clone a linked list with next and random pointer - GFG ├── README.md └── clone-a-linked-list-with-next-and-random-pointer.cpp ├── Combination Sum - GFG ├── README.md └── combination-sum.cpp ├── Comment Removal - GFG ├── README.md └── comment-removal.cpp ├── Complement - GFG ├── README.md └── complement.cpp ├── Connect Nodes at Same Level - GFG ├── README.md └── connect-nodes-at-same-level.cpp ├── Construct Binary Tree from Parent Array - GFG ├── README.md └── construct-binary-tree-from-parent-array.cpp ├── Construct Tree from Inorder & Preorder - GFG ├── README.md └── construct-tree-from-inorder-preorder.cpp ├── Construct Tree from Preorder Traversal - GFG ├── README.md └── construct-tree-from-preorder-traversal.cpp ├── Count Inversions - GFG ├── README.md └── count-inversions.cpp ├── Count Occurences of Anagrams - GFG ├── README.md └── count-occurences-of-anagrams.cpp ├── Count Pairs whose sum is equal to X - GFG ├── README.md └── count-pairs-whose-sum-is-equal-to-x.cpp ├── Count possible ways to construct buildings - GFG ├── README.md └── count-possible-ways-to-construct-buildings.java ├── Count subsequences of type a^i, b^j, c^k - GFG ├── README.md └── count-subsequences-of-type-ai-bj-ck.cpp ├── Count the number of possible triangles - GFG ├── README.md └── count-the-number-of-possible-triangles.cpp ├── Count the triplets - GFG └── README.md ├── Count ways to reach the n'th stair - GFG ├── README.md └── count-ways-to-reach-the-nth-stair.cpp ├── Covid Spread - GFG ├── README.md └── covid-spread.cpp ├── Critical Connections - GFG ├── README.md └── critical-connections.java ├── Cyclically rotate an array by one - GFG ├── README.md └── cyclically-rotate-an-array-by-one.cpp ├── Decode the string - GFG ├── README.md └── decode-the-string.cpp ├── Delete Middle of Linked List - GFG ├── README.md └── delete-middle-of-linked-list.cpp ├── Delete a node from BST - GFG ├── README.md └── delete-a-node-from-bst.cpp ├── Delete nodes having greater value on right - GFG ├── README.md └── delete-nodes-having-greater-value-on-right.cpp ├── Delete without head pointer - GFG ├── README.md └── delete-without-head-pointer.cpp ├── Detect Loop in linked list - GFG ├── README.md └── detect-loop-in-linked-list.cpp ├── Detect cycle in a directed graph - GFG ├── README.md └── detect-cycle-in-a-directed-graph.cpp ├── Detect cycle in an undirected graph - GFG ├── README.md └── detect-cycle-in-an-undirected-graph.cpp ├── Diagonal Traversal of Binary Tree - GFG ├── README.md └── diagonal-traversal-of-binary-tree.cpp ├── Diameter of a Binary Tree - GFG ├── README.md └── diameter-of-a-binary-tree.cpp ├── Distance from the Source (Bellman-Ford Algorithm) - GFG ├── README.md └── distance-from-the-source-bellman-ford-algorithm.cpp ├── Distinct occurrences - GFG ├── README.md └── distinct-occurrences.cpp ├── Does robot moves circular - GFG ├── README.md └── does-robot-moves-circular.cpp ├── Edit Distance - GFG ├── README.md └── edit-distance.cpp ├── Equilibrium Point - GFG ├── README.md └── equilibrium-point.cpp ├── Euler Circuit in an Undirected Graph - GFG ├── README.md └── euler-circuit-in-an-undirected-graph.cpp ├── Euler circuit and Path - GFG ├── README.md └── euler-circuit-and-path.cpp ├── Expression Tree - GFG ├── README.md └── expression-tree.cpp ├── Extreme nodes in alternate order - GFG ├── README.md └── extreme-nodes-in-alternate-order.cpp ├── Find All Four Sum Numbers - GFG ├── README.md └── find-all-four-sum-numbers.cpp ├── Find Missing And Repeating - GFG ├── README.md └── find-missing-and-repeating.cpp ├── Find Transition Point - GFG ├── README.md └── find-transition-point.cpp ├── Find a pair with given target in BST - GFG ├── README.md └── find-a-pair-with-given-target-in-bst.cpp ├── Find duplicates in an array - GFG ├── README.md └── find-duplicates-in-an-array.cpp ├── Find median in a stream - GFG ├── README.md └── find-median-in-a-stream.cpp ├── Find minimum and maximum element in an array - GFG ├── README.md └── find-minimum-and-maximum-element-in-an-array.cpp ├── Find nth element of spiral matrix - GFG ├── README.md └── find-nth-element-of-spiral-matrix.cpp ├── Find the Closest Element in BST - GFG ├── README.md └── find-the-closest-element-in-bst.cpp ├── Find the element that appears once - GFG ├── README.md └── find-the-element-that-appears-once.cpp ├── Find the number of islands - GFG ├── README.md └── find-the-number-of-islands.cpp ├── Find triplets with zero sum - GFG ├── README.md └── find-triplets-with-zero-sum.cpp ├── Find whether path exist - GFG ├── README.md └── find-whether-path-exist.cpp ├── Finding middle element in a linked list - GFG ├── README.md └── finding-middle-element-in-a-linked-list.cpp ├── First Repeating Element - GFG ├── README.md └── first-repeating-element.cpp ├── First negative integer in every window of size k - GFG ├── README.md └── first-negative-integer-in-every-window-of-size-k.cpp ├── First non-repeating character in a stream - GFG ├── README.md └── first-nonrepeating-character-in-a-stream.cpp ├── Fixing Two nodes of a BST - GFG ├── README.md └── fixing-two-nodes-of-a-bst.cpp ├── Form a palindrome - GFG ├── README.md └── form-a-palindrome.cpp ├── Generate Binary Numbers - GFG ├── README.md └── generate-binary-numbers.cpp ├── Generate Parentheses - GFG ├── README.md └── generate-parentheses.cpp ├── Get Minimum Squares - GFG └── README.md ├── Get minimum element from stack - GFG ├── README.md └── get-minimum-element-from-stack.cpp ├── Hamiltonian Path - GFG ├── README.md └── hamiltonian-path.cpp ├── Height of Binary Tree - GFG ├── README.md └── height-of-binary-tree.cpp ├── Height of Spiral Tree - GFG ├── README.md └── height-of-spiral-tree.cpp ├── Help Classmates - GFG ├── README.md └── help-classmates.cpp ├── IPL 2021 - Match Day 2 - GFG ├── README.md └── ipl-2021-match-day-2.cpp ├── Implement Atoi - GFG ├── README.md └── implement-atoi.cpp ├── Implementing Dijkstra Algorithm - GFG ├── README.md └── implementing-dijkstra-algorithm.cpp ├── Index Of an Extra Element - GFG ├── README.md └── index-of-an-extra-element.cpp ├── Inorder Traversal - GFG ├── README.md └── inorder-traversal.cpp ├── Interleaved Strings - GFG ├── README.md └── interleaved-strings.cpp ├── Intersection Point in Y Shapped Linked Lists - GFG ├── README.md └── intersection-point-in-y-shapped-linked-lists.cpp ├── Intersection of two arrays - GFG ├── README.md └── intersection-of-two-arrays.cpp ├── Is Sudoku Valid - GFG ├── README.md └── is-sudoku-valid.cpp ├── Jump Game - GFG ├── README.md └── jump-game.cpp ├── K Sum Paths - GFG ├── README.md └── k-sum-paths.cpp ├── K distance from root - GFG ├── README.md └── k-distance-from-root.cpp ├── K largest elements - GFG ├── README.md └── k-largest-elements.cpp ├── Kadane's Algorithm - GFG ├── README.md └── kadanes-algorithm.cpp ├── Knight Walk - GFG └── knight-walk.cpp ├── Kth smallest element - GFG ├── README.md └── kth-smallest-element.cpp ├── LRU Cache - GFG ├── README.md └── lru-cache.cpp ├── Largest BST - GFG ├── README.md └── largest-bst.cpp ├── Largest Number formed from an Array - GFG ├── README.md └── largest-number-formed-from-an-array.cpp ├── Largest number in K swaps - GFG ├── README.md └── largest-number-in-k-swaps.cpp ├── Largest square formed in a matrix - GFG ├── README.md └── largest-square-formed-in-a-matrix.cpp ├── Largest subarray of 0's and 1's - GFG ├── README.md └── largest-subarray-of-0s-and-1s.cpp ├── Largest subarray with 0 sum - GFG ├── README.md └── largest-subarray-with-0-sum.cpp ├── Leaders in an array - GFG ├── README.md └── leaders-in-an-array.cpp ├── Leaf at same level - GFG ├── README.md └── leaf-at-same-level.cpp ├── Leaves to DLL - GFG ├── README.md └── leaves-to-dll.cpp ├── Left View of Binary Tree - GFG ├── README.md └── left-view-of-binary-tree.cpp ├── Leftmost and rightmost nodes of binary tree - GFG ├── README.md └── leftmost-and-rightmost-nodes-of-binary-tree.cpp ├── Length of the longest substring - GFG ├── README.md └── length-of-the-longest-substring.cpp ├── Level order traversal Line by Line - GFG └── README.md ├── Level order traversal in spiral form - GFG ├── README.md └── level-order-traversal-in-spiral-form.cpp ├── Longest Arithmetic Progression - GFG ├── README.md └── longest-arithmetic-progression.cpp ├── Longest Common Subsequence - GFG ├── README.md └── longest-common-subsequence.cpp ├── Longest Increasing Subsequence - GFG ├── README.md └── longest-increasing-subsequence.cpp ├── Longest Palindromic Subsequence - GFG ├── README.md └── longest-palindromic-subsequence.cpp ├── Longest Palindromic Substring - GFG ├── README.md └── longest-palindromic-substring.cpp ├── Longest Sub-Array with Sum K - GFG ├── README.md └── longest-sub-array-with-sum-k.cpp ├── Longest consecutive subsequence - GFG ├── README.md └── longest-consecutive-subsequence.cpp ├── Lowest Common Ancestor in a BST - GFG ├── README.md └── lowest-common-ancestor-in-a-bst.cpp ├── Lowest Common Ancestor in a Binary Tree - GFG ├── README.md └── lowest-common-ancestor-in-a-binary-tree.cpp ├── M-Coloring Problem - GFG ├── README.md └── mcoloring-problem.cpp ├── Majority Element - GFG ├── README.md └── majority-element.cpp ├── Make Binary Tree From Linked List - GFG ├── README.md └── make-binary-tree-from-linked-list.cpp ├── Max Level Sum in Binary Tree - GFG ├── README.md └── max-level-sum-in-binary-tree.cpp ├── Max Sum Subarray of size K - GFG ├── README.md └── max-sum-subarray-of-size-k.cpp ├── Max Sum without Adjacents - GFG ├── README.md └── max-sum-without-adjacents.cpp ├── Max length chain - GFG ├── README.md └── max-length-chain.cpp ├── Max possible amount - GFG ├── README.md └── max-possible-amount.cpp ├── Max rectangle - GFG ├── README.md └── max-rectangle.cpp ├── Max sum in the configuration - GFG ├── README.md └── max-sum-in-the-configuration.cpp ├── Maximize Toys - GFG ├── README.md └── maximize-toys.cpp ├── Maximum Index - GFG ├── README.md └── maximum-index.cpp ├── Maximum Path Sum between 2 Leaf Nodes - GFG ├── README.md └── maximum-path-sum-between-2-leaf-nodes.cpp ├── Maximum Product Subarray - GFG ├── README.md └── maximum-product-subarray.cpp ├── Maximum Rectangular Area in a Histogram - GFG ├── README.md └── maximum-rectangular-area-in-a-histogram.cpp ├── Maximum difference between node and its ancestor - GFG ├── README.md └── maximum-difference-between-node-and-its-ancestor.cpp ├── Maximum money - GFG ├── README.md └── maximum-money.cpp ├── Maximum of all subarrays of size k - GFG ├── README.md └── maximum-of-all-subarrays-of-size-k.cpp ├── Maximum path sum in matrix - GFG ├── README.md └── maximum-path-sum-in-matrix.cpp ├── Maximum sum increasing subsequence - GFG ├── README.md └── maximum-sum-increasing-subsequence.cpp ├── Maximum sum of Non-adjacent nodes - GFG ├── README.md └── maximum-sum-of-nonadjacent-nodes.cpp ├── Merge K sorted linked lists - GFG ├── README.md └── merge-k-sorted-linked-lists.cpp ├── Merge Sort - GFG ├── README.md └── merge-sort.cpp ├── Merge Without Extra Space - GFG ├── README.md └── merge-without-extra-space.cpp ├── Merge k Sorted Arrays - GFG ├── README.md └── merge-k-sorted-arrays.cpp ├── Min distance between two given nodes of a Binary Tree - GFG ├── README.md └── min-distance-between-two-given-nodes-of-a-binary-tree.cpp ├── Minimize the Heights II - GFG ├── README.md └── minimize-the-heights-ii.cpp ├── Minimum Cost Path - GFG ├── README.md └── minimum-cost-path.cpp ├── Minimum Platforms - GFG ├── README.md └── minimum-platforms.cpp ├── Minimum Spanning Tree - GFG ├── README.md └── minimum-spanning-tree.cpp ├── Minimum Swaps to Sort - GFG ├── README.md └── minimum-swaps-to-sort.cpp ├── Minimum number of jumps - GFG ├── README.md └── minimum-number-of-jumps.cpp ├── Minimum steps to make product equal to one - GFG ├── README.md └── minimum-steps-to-make-product-equal-to-one.cpp ├── Minimum sum partition - GFG ├── README.md └── minimum-sum-partition.cpp ├── Mirror Tree - GFG ├── README.md └── mirror-tree.cpp ├── Missing number in array - GFG ├── README.md └── missing-number-in-array.cpp ├── Move all negative elements to end - GFG ├── README.md └── move-all-negative-elements-to-end.cpp ├── Multiply two strings - GFG ├── README.md └── multiply-two-strings.cpp ├── N-Queen Problem - GFG ├── README.md └── nqueen-problem.cpp ├── Nearly sorted - GFG ├── README.md └── nearly-sorted.cpp ├── Next Greater Element - GFG ├── README.md └── next-greater-element.cpp ├── Next Permutation - GFG ├── README.md └── next-permutation.cpp ├── Nodes at given distance in binary tree - GFG ├── README.md └── nodes-at-given-distance-in-binary-tree.cpp ├── Nth catalan number - GFG ├── README.md └── nth-catalan-number.cpp ├── Number of Coins - GFG ├── README.md └── number-of-coins.cpp ├── Number of Provinces - GFG ├── README.md └── number-of-provinces.cpp ├── Number of Turns in Binary Tree - GFG ├── README.md └── number-of-turns-in-binary-tree.cpp ├── Number of occurrence - GFG ├── README.md └── number-of-occurrence.cpp ├── Number of paths - GFG ├── README.md └── number-of-paths.cpp ├── Number of paths in a matrix with k coins - GFG ├── README.md └── number-of-paths-in-a-matrix-with-k-coins.cpp ├── Number of ways - GFG ├── README.md └── number-of-ways.cpp ├── Nuts and Bolts Problem - GFG ├── README.md └── nuts-and-bolts-problem.cpp ├── Overlapping Intervals - GFG ├── README.md └── overlapping-intervals.cpp ├── Pairwise swap elements of a linked list - GFG ├── README.md └── pairwise-swap-elements-of-a-linked-list.cpp ├── Parenthesis Checker - GFG ├── README.md └── parenthesis-checker.cpp ├── Partition Equal Subset Sum - GFG ├── README.md └── partition-equal-subset-sum.cpp ├── Pascal Triangle - GFG ├── README.md └── pascal-triangle.cpp ├── Peak element - GFG ├── README.md └── peak-element.cpp ├── Player with max score - GFG ├── README.md └── player-with-max-score.cpp ├── Polynomial Addition - GFG ├── README.md └── polynomial-addition.cpp ├── Possible Words From Phone Digits - GFG ├── README.md └── possible-words-from-phone-digits.cpp ├── Preorder Traversal - GFG ├── README.md └── preorder-traversal.cpp ├── Preorder to Postorder - GFG ├── README.md └── preorder-to-postorder.cpp ├── Prerequisite Tasks - GFG ├── README.md └── prerequisite-tasks.cpp ├── Print Anagrams Together - GFG ├── README.md └── print-anagrams-together.cpp ├── Print Nodes having K leaves - GFG ├── README.md └── print-nodes-having-k-leaves.cpp ├── Print adjacency list - GFG ├── README.md └── print-adjacency-list.cpp ├── Print all nodes that don't have sibling - GFG ├── README.md └── print-all-nodes-that-dont-have-sibling.cpp ├── Print first letter of every word in the string - GFG ├── README.md └── print-first-letter-of-every-word-in-the-string.cpp ├── Pythagorean Triplet - GFG ├── README.md └── pythagorean-triplet.cpp ├── Queue Reversal - GFG ├── README.md └── queue-reversal.cpp ├── Queue using two Stacks - GFG ├── README.md └── queue-using-two-stacks.cpp ├── Quick Sort - GFG ├── README.md └── quick-sort.cpp ├── README.md ├── Rat in a Maze Problem - I - GFG ├── README.md └── rat-in-a-maze-problem-i.cpp ├── Rearrange a linked list - GFG ├── README.md └── rearrange-a-linked-list.cpp ├── Rearrange an array with O(1) extra space - GFG ├── README.md └── rearrange-an-array-with-o1-extra-space.cpp ├── Rearrange characters - GFG ├── README.md └── rearrange-characters.cpp ├── Remove duplicates from an unsorted linked list - GFG ├── README.md └── remove-duplicates-from-an-unsorted-linked-list.cpp ├── Remove loop in Linked List - GFG ├── README.md └── remove-loop-in-linked-list.cpp ├── Reverse First K elements of Queue - GFG ├── README.md └── reverse-first-k-elements-of-queue.cpp ├── Reverse Level Order Traversal - GFG ├── README.md └── reverse-level-order-traversal.cpp ├── Reverse a Linked List in groups of given size. - GFG ├── README.md └── reverse-a-linked-list-in-groups-of-given-size..cpp ├── Reverse alternate nodes in Link List - GFG ├── README.md └── reverse-alternate-nodes-in-link-list.cpp ├── Reverse an Array - GFG ├── README.md └── reverse-an-array.cpp ├── Reverse each word in a given string - GFG ├── README.md └── reverse-each-word-in-a-given-string.cpp ├── Right View of Binary Tree - GFG ├── README.md └── right-view-of-binary-tree.cpp ├── Robots - GFG ├── README.md └── robots.cpp ├── Root to leaf paths sum - GFG ├── README.md └── root-to-leaf-paths-sum.cpp ├── Rotate a 2D array without using extra space - GFG ├── README.md └── rotate-a-2d-array-without-using-extra-space.cpp ├── Rotate a Linked List - GFG ├── README.md └── rotate-a-linked-list.cpp ├── Rotate by 90 degree - GFG ├── README.md └── rotate-by-90-degree.cpp ├── Rotten Oranges - GFG ├── README.md └── rotten-oranges.cpp ├── Row with max 1s - GFG ├── README.md └── row-with-max-1s.cpp ├── Search in a Rotated Array - GFG ├── README.md └── search-in-a-rotated-array.cpp ├── Search in a row-column sorted Matrix - GFG ├── README.md └── search-in-a-rowcolumn-sorted-matrix.cpp ├── Serialize and Deserialize a Binary Tree - GFG ├── README.md └── serialize-and-deserialize-a-binary-tree.cpp ├── Shortest Source to Destination Path - GFG ├── README.md └── shortest-source-to-destination-path.cpp ├── Shortest path from 1 to n - GFG ├── README.md └── shortest-path-from-1-to-n.cpp ├── Smallest Positive missing number - GFG ├── README.md └── smallest-positive-missing-number.cpp ├── Smallest distinct window - GFG ├── README.md ├── smallest-distinct-window.cpp └── smallest-distinct-window.java ├── Smallest number on left - GFG ├── README.md └── smallest-number-on-left.cpp ├── Smallest window in a string containing all the characters of another string - GFG ├── README.md └── smallest-window-in-a-string-containing-all-the-characters-of-another-string.cpp ├── Solve the Sudoku - GFG ├── README.md └── solve-the-sudoku.cpp ├── Sort an array according to the other - GFG ├── README.md └── sort-an-array-according-to-the-other.cpp ├── Sort an array of 0s, 1s and 2s - GFG ├── README.md └── sort-an-array-of-0s-1s-and-2s.cpp ├── Sorted Link List to BST - GFG ├── README.md └── sorted-link-list-to-bst.cpp ├── Special Keyboard - GFG ├── README.md └── special-keyboard.cpp ├── Special Matrix - GFG ├── README.md └── special-matrix.cpp ├── Special Stack - GFG ├── README.md └── special-stack.cpp ├── Square root of a number - GFG ├── README.md └── square-root-of-a-number.cpp ├── Stack using two queues - GFG ├── README.md └── stack-using-two-queues.cpp ├── Steps by Knight - GFG ├── README.md └── steps-by-knight.cpp ├── Stock buy and sell - GFG ├── README.md └── stock-buy-and-sell.cpp ├── Stock span problem - GFG ├── README.md └── stock-span-problem.cpp ├── Strongly Connected Components (Kosaraju's Algo) - GFG ├── README.md └── strongly-connected-components-kosarajus-algo.cpp ├── Subarray with given sum - GFG ├── README.md └── subarray-with-given-sum.cpp ├── Subset Sum Problem - GFG ├── README.md └── subset-sum-problem.cpp ├── Sum Tree - GFG ├── README.md └── sum-tree.cpp ├── Sum of two elements with sum nearest to zero - GFG ├── README.md └── sum-of-two-elements-with-sum-nearest-to-zero.cpp ├── Sum of two large numbers - GFG ├── README.md └── sum-of-two-large-numbers.cpp ├── Swap Kth nodes from ends - GFG ├── README.md └── swap-kth-nodes-from-ends.cpp ├── Symmetric Tree - GFG ├── README.md └── symmetric-tree.cpp ├── The Celebrity Problem - GFG ├── README.md └── the-celebrity-problem.cpp ├── Top View of Binary Tree - GFG ├── README.md └── top-view-of-binary-tree.cpp ├── Topological sort - GFG ├── README.md └── topological-sort.cpp ├── Total Decoding Messages - GFG ├── README.md └── total-decoding-messages.cpp ├── Trailing zeroes in factorial - GFG ├── README.md └── trailing-zeroes-in-factorial.cpp ├── Transform to Sum Tree - GFG ├── README.md └── transform-to-sum-tree.cpp ├── Trapping Rain Water - GFG ├── README.md └── trapping-rain-water.cpp ├── Tree from Postorder and Inorder - GFG ├── README.md └── tree-from-postorder-and-inorder.cpp ├── Triplet Sum in Array - GFG ├── README.md └── triplet-sum-in-array.cpp ├── Union of two arrays - GFG ├── README.md └── union-of-two-arrays.cpp ├── Unique BST's - GFG ├── README.md └── unique-bsts.cpp ├── Unit Area of largest region of 1's - GFG ├── README.md └── unit-area-of-largest-region-of-1s.cpp ├── Valid Substring - GFG ├── README.md └── valid-substring.cpp ├── Validate an IP Address - GFG ├── README.md └── validate-an-ip-address.cpp ├── Vertical Traversal of Binary Tree - GFG ├── README.md └── vertical-traversal-of-binary-tree.cpp ├── Wave Array - GFG ├── README.md └── wave-array.cpp ├── Ways to sum to N - GFG ├── README.md └── ways-to-sum-to-n.cpp ├── Wildcard Pattern Matching - GFG ├── README.md └── wildcard-pattern-matching.cpp ├── Word Boggle - GFG ├── README.md └── word-boggle.cpp ├── Word Break - GFG ├── README.md └── word-break.cpp ├── Word Ladder I - GFG ├── README.md └── word-ladder-i.cpp ├── Word Search - GFG ├── README.md └── word-search.cpp ├── X Total Shapes - GFG ├── README.md └── x-total-shapes.cpp ├── Zero Sum Subarrays - GFG ├── README.md └── zero-sum-subarrays.cpp ├── ZigZag Tree Traversal - GFG ├── README.md └── zig-zag-tree-traversal.cpp ├── add-binary ├── README.md └── add-binary.py ├── add-digits ├── README.md └── add-digits.cpp ├── add-one-row-to-tree ├── README.md └── add-one-row-to-tree.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-tree-iterator ├── README.md └── binary-search-tree-iterator.cpp ├── binary-search-tree-to-greater-sum-tree ├── README.md └── binary-search-tree-to-greater-sum-tree.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-tilt ├── README.md └── binary-tree-tilt.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-inorder-and-postorder-traversal ├── README.md └── construct-binary-tree-from-inorder-and-postorder-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-bst-to-greater-tree ├── README.md └── convert-bst-to-greater-tree.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 ├── copy-list-with-random-pointer ├── README.md └── copy-list-with-random-pointer.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-nodes-and-return-forest ├── README.md └── delete-nodes-and-return-forest.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 ├── duplicate-zeros ├── README.md └── duplicate-zeros.cpp ├── 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-a-corresponding-node-of-a-binary-tree-in-a-clone-of-that-tree ├── README.md └── find-a-corresponding-node-of-a-binary-tree-in-a-clone-of-that-tree.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-duplicate-subtrees ├── README.md └── find-duplicate-subtrees.cpp ├── find-elements-in-a-contaminated-binary-tree ├── README.md └── find-elements-in-a-contaminated-binary-tree.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 ├── invalid-transactions ├── README.md └── invalid-transactions.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 largest elements - GFG ├── README.md └── k-largest-elements.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-a-stream ├── README.md └── kth-largest-element-in-a-stream.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 ├── making-a-large-island ├── README.md └── making-a-large-island.cpp ├── matrix-diagonal-sum ├── README.md └── matrix-diagonal-sum.cpp ├── max-area-of-island ├── README.md └── max-area-of-island.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-difference-between-node-and-ancestor ├── README.md └── maximum-difference-between-node-and-ancestor.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-number-of-balls-in-a-box ├── README.md └── maximum-number-of-balls-in-a-box.cpp ├── maximum-product-of-splitted-binary-tree ├── README.md └── maximum-product-of-splitted-binary-tree.cpp ├── maximum-product-of-three-numbers ├── README.md └── maximum-product-of-three-numbers.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-leaf-nodes-pairs ├── README.md └── number-of-good-leaf-nodes-pairs.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-ii ├── README.md └── path-sum-ii.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 ├── relative-ranks ├── README.md └── relative-ranks.cpp ├── relative-sort-array ├── README.md └── relative-sort-array.py ├── 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-largest-digit-in-a-string ├── README.md └── second-largest-digit-in-a-string.cpp ├── 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-element-in-a-sorted-array ├── README.md └── single-element-in-a-sorted-array.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-string-starting-from-leaf ├── README.md └── smallest-string-starting-from-leaf.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 ├── sorting-the-sentence ├── README.md └── sorting-the-sentence.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-nodes-with-even-valued-grandparent ├── README.md └── sum-of-nodes-with-even-valued-grandparent.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 ├── time-needed-to-inform-all-employees ├── README.md └── time-needed-to-inform-all-employees.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-iv-input-is-a-bst ├── README.md └── two-sum-iv-input-is-a-bst.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 /1-bit-and-2-bit-characters/1-bit-and-2-bit-characters.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isOneBitCharacter(vector& bits) { 4 | for(int i=0;i twoSum(vector& nums, int target) { 4 | unordered_map m; 5 | 6 | for(int i=0;ival!=q->val) return false; 20 | 21 | return isSameTree(p->left,q->left) && isSameTree(p->right,q->right); 22 | } 23 | }; -------------------------------------------------------------------------------- /100-same-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1009-complement-of-base-10-integer/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1010-pairs-of-songs-with-total-durations-divisible-by-60/1010-pairs-of-songs-with-total-durations-divisible-by-60.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numPairsDivisibleBy60(vector& time) { 4 | vector c(60); 5 | int res = 0; 6 | for (int t : time) { 7 | res += c[(600 - t) % 60]; 8 | c[t % 60] += 1; 9 | } 10 | return res; 11 | } 12 | }; -------------------------------------------------------------------------------- /1010-pairs-of-songs-with-total-durations-divisible-by-60/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1026-maximum-difference-between-node-and-ancestor/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1029-two-city-scheduling/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1041-robot-bounded-in-circle/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1044-longest-duplicate-substring/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1048-longest-string-chain/1048-longest-string-chain.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | static bool cmp(string a,string b){ 4 | return a.size()& words) { 7 | 8 | sort(words.begin(),words.end(),cmp); 9 | 10 | unordered_map m; 11 | 12 | int res = 0; 13 | 14 | for(string a:words){ 15 | int longest = 0; 16 | for(int i=0;ileft==root->right) return root->valleft) root->left = sufficientSubset(root->left,limit-root->val); 17 | if(root->right) root->right = sufficientSubset(root->right,limit-root->val); 18 | return root->left==root->right?NULL:root; 19 | } 20 | }; -------------------------------------------------------------------------------- /1080-insufficient-nodes-in-root-to-leaf-paths/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1094-car-pooling/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1186-maximum-subarray-sum-with-one-deletion/1186-maximum-subarray-sum-with-one-deletion.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maximumSum(vector& arr) { 4 | vector> dp(arr.size(),vector (2,0)); 5 | 6 | 7 | dp[0][0]=arr[0]; 8 | 9 | int res = arr[0]; 10 | 11 | for(int i=1;i sequentialDigits(int low, int high) { 4 | 5 | 6 | vector seq = {}; 7 | 8 | int temp = 0; 9 | 10 | for(int i=1;i<9;i++){ 11 | // cout<=low && temp<=high) seq.push_back(temp); 16 | // cout<& 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 | }; -------------------------------------------------------------------------------- /1332-remove-palindromic-subsequences/1332-remove-palindromic-subsequences.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int removePalindromeSub(string s) { 4 | int i = 0; 5 | int j = s.size()-1; 6 | 7 | int ans = 1; 8 | 9 | while(i runningSum(vector& nums) { 4 | int s = nums[0]; 5 | for(int i=1;i& nums) { 4 | unordered_map m; 5 | 6 | int sum = 0; 7 | 8 | int l = 0; 9 | int r = 0; 10 | 11 | int ans = 0; 12 | 13 | while(r &a,vector &b){ 4 | if(a[1]>b[1]) return 1; 5 | return 0; 6 | } 7 | int maximumUnits(vector>& boxTypes, int truckSize) { 8 | sort(boxTypes.begin(),boxTypes.end(),cmp); 9 | 10 | int ans =0; 11 | 12 | for(int i=0;i& nums) { 4 | vector arr; 5 | for(auto i:nums){ 6 | arr.push_back(to_string(i)); 7 | } 8 | 9 | sort(arr.begin(),arr.end(),[](string s1,string s2){return s1+s2>s2+s1;}); 10 | 11 | string res; 12 | 13 | for(auto s:arr) 14 | res+=s; 15 | 16 | while(res[0]=='0' && res.length()>1) 17 | res.erase(0,1); 18 | 19 | 20 | return res; 21 | } 22 | }; -------------------------------------------------------------------------------- /179-largest-number/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /181-employees-earning-more-than-their-managers/181-employees-earning-more-than-their-managers.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | SELECT a.name as Employee 3 | FROM employee a, employee b 4 | # where salary a,salary b 5 | where a.managerId = b.id and a.salary>b.salary -------------------------------------------------------------------------------- /181-employees-earning-more-than-their-managers/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1823-find-the-winner-of-the-circular-game/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1926-nearest-exit-from-entrance-in-maze/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1953-maximum-number-of-weeks-for-which-you-can-work/1953-maximum-number-of-weeks-for-which-you-can-work.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long long numberOfWeeks(vector& milestones) { 4 | long long sum = 0; 5 | long long max = *max_element(begin(milestones),end(milestones)); 6 | for(auto a:milestones) sum+=a; 7 | 8 | 9 | 10 | return min(sum,2*(sum-max)+1); 11 | 12 | } 13 | }; -------------------------------------------------------------------------------- /1953-maximum-number-of-weeks-for-which-you-can-work/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1961-check-if-string-is-a-prefix-of-array/1961-check-if-string-is-a-prefix-of-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPrefixString(string s, vector& words) { 4 | 5 | int c = 0; 6 | 7 | for(int i=0;i=s.size()) return true; 9 | for(int j=0;j=s.size() || (words[i][j]!=s[c])) return false; 11 | c++; 12 | } 13 | } 14 | 15 | if(c2){ 12 | temp[0] = toupper(temp[0]); 13 | 14 | } 15 | ans+=temp+" "; 16 | temp=""; 17 | } 18 | else{ 19 | temp+=a; 20 | } 21 | } 22 | 23 | if(temp.size()>2){ 24 | temp[0] = toupper(temp[0]); 25 | 26 | } 27 | ans+=temp; 28 | 29 | 30 | return ans; 31 | } 32 | }; -------------------------------------------------------------------------------- /2129-capitalize-the-title/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /226-invert-binary-tree/226-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 | if(!root) return NULL; 16 | 17 | auto temp = root->left; 18 | root->left = root->right; 19 | root->right = temp; 20 | 21 | auto l = invertTree(root->left); 22 | auto r = invertTree(root->right); 23 | 24 | 25 | return root; 26 | 27 | } 28 | }; -------------------------------------------------------------------------------- /226-invert-binary-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /235-lowest-common-ancestor-of-a-binary-search-tree/235-lowest-common-ancestor-of-a-binary-search-tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 | * }; 9 | */ 10 | 11 | class Solution { 12 | public: 13 | TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { 14 | if(!root) return NULL; 15 | 16 | if(root==p || root==q) return root; 17 | 18 | if(root->val>p->val && root->val>q->val) return lowestCommonAncestor(root->left,p,q); 19 | 20 | if(root->valval && root->valval) return lowestCommonAncestor(root->right,p,q); 21 | 22 | return root; 23 | } 24 | }; -------------------------------------------------------------------------------- /235-lowest-common-ancestor-of-a-binary-search-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /238-product-of-array-except-self/238-product-of-array-except-self.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector productExceptSelf(vector& nums) { 4 | vector res(nums.size(),0); 5 | 6 | int prefix =1; 7 | 8 | for(int i=0;i=0;i--){ 16 | res[i]*=postfix; 17 | postfix=postfix*nums[i]; 18 | } 19 | 20 | return res; 21 | } 22 | }; -------------------------------------------------------------------------------- /238-product-of-array-except-self/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /287-find-the-duplicate-number/287-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 | 7 | while(slow!=fast){ 8 | fast=nums[nums[fast]]; 9 | slow=nums[slow]; 10 | } 11 | 12 | fast = 0; 13 | 14 | while(slow!=fast){ 15 | slow=nums[slow]; 16 | fast=nums[fast]; 17 | } 18 | 19 | 20 | return slow; 21 | } 22 | }; -------------------------------------------------------------------------------- /287-find-the-duplicate-number/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /295-find-median-from-data-stream/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /3-longest-substring-without-repeating-characters/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /304-range-sum-query-2d-immutable/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /31-next-permutation/31-next-permutation.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void nextPermutation(vector& nums) { 4 | int i; 5 | for(i=nums.size()-2;i>=0;i--){ 6 | if(nums[i]=0){ 13 | while(nums[r]<=nums[i]){ 14 | r--; 15 | } 16 | swap(nums[r],nums[i]); 17 | } 18 | 19 | reverse(nums.begin()+i+1,nums.end()); 20 | 21 | 22 | 23 | return; 24 | 25 | } 26 | }; -------------------------------------------------------------------------------- /31-next-permutation/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /32-longest-valid-parentheses/32-longest-valid-parentheses.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int longestValidParentheses(string s) { 4 | stack st; 5 | 6 | st.push(-1); 7 | int ans = 0; 8 | 9 | for(int i=0;i& 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 | }; -------------------------------------------------------------------------------- /4-median-of-two-sorted-arrays/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /402-remove-k-digits/402-remove-k-digits.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string removeKdigits(string num, int k) { 4 | int n = num.size(); 5 | if (n <= k) return "0"; 6 | stack s; 7 | for (auto &&c : num) { 8 | while (!s.empty() and k > 0 and s.top() > c) s.pop(), k--; 9 | if (!s.empty() or c != '0') s.push(c); 10 | } 11 | while (!s.empty() and k--) s.pop(); 12 | if (s.empty()) return "0"; 13 | while (!s.empty()) { 14 | num[n - 1] = s.top(); 15 | s.pop(), n--; 16 | } 17 | return num.substr(n); 18 | } 19 | }; -------------------------------------------------------------------------------- /402-remove-k-digits/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /42-trapping-rain-water/42-trapping-rain-water.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int trap(vector& height) { 4 | int lPointer = 0,rPointer = height.size() -1; 5 | 6 | int lW = 0,rW=0; 7 | int ans = 0; 8 | 9 | while(lPointer<=rPointer){ 10 | if(rW<=lW){ 11 | ans=ans+max(0,rW-height[rPointer]); 12 | rW = max(rW,height[rPointer]); 13 | rPointer--; 14 | }else{ 15 | ans=ans+max(0,lW-height[lPointer]); 16 | lW = max(lW,height[lPointer]); 17 | lPointer++; 18 | } 19 | } 20 | 21 | return ans; 22 | } 23 | }; -------------------------------------------------------------------------------- /42-trapping-rain-water/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /438-find-all-anagrams-in-a-string/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /442-find-all-duplicates-in-an-array/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /450-delete-node-in-a-bst/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /456-132-pattern/456-132-pattern.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool find132pattern(vector& nums) { 4 | stack> st; 5 | int minN = nums[0]; 6 | 7 | for(auto a:nums){ 8 | while (st.size() && st.top().first<=a) st.pop(); 9 | 10 | 11 | if(st.size() && st.top().second5. Longest Palindromic Substring

Medium


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

2 | 3 |

 

4 |

Example 1:

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

Example 2:

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

 

18 |

Constraints:

19 | 20 |
    21 |
  • 1 <= s.length <= 1000
  • 22 |
  • s consist of only digits and English letters.
  • 23 |
24 |
-------------------------------------------------------------------------------- /503-next-greater-element-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /51-n-queens/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /52-n-queens-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /520-detect-capital/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /529-minesweeper/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /547-number-of-provinces/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /56-merge-intervals/56-merge-intervals.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> merge(vector>& intervals) { 4 | sort(intervals.begin(),intervals.end()); 5 | int endT = intervals[0][1]; 6 | int startT = intervals[0][0]; 7 | vector> ans; 8 | 9 | for(auto a:intervals){ 10 | if(a[0]>endT){ 11 | ans.push_back({startT,endT}); 12 | startT=a[0]; 13 | endT=a[1]; 14 | }else{ 15 | endT=max(endT,a[1]); 16 | } 17 | 18 | } 19 | 20 | ans.push_back({startT,endT}); 21 | 22 | 23 | return ans; 24 | } 25 | }; -------------------------------------------------------------------------------- /56-merge-intervals/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /583-delete-operation-for-two-strings/583-delete-operation-for-two-strings.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | 5 | int minDistance(string word1, string word2) { 6 | 7 | int n = word1.size(); 8 | int m = word2.size(); 9 | 10 | int t[n+1][m+1]; 11 | 12 | for(int i=0;i& flowerbed, int n) { 4 | int count=0; 5 | for(int i=0;i=n; 19 | } 20 | }; -------------------------------------------------------------------------------- /605-can-place-flowers/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /69-sqrtx/69-sqrtx.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int mySqrt(int x) { 4 | if(x<1) return x; 5 | int l=1; 6 | int h=x; 7 | int ans = 1; 8 | while(l<=h){ 9 | int mid = l+(h-l)/2; 10 | unsigned long long int curr = mid; 11 | 12 | if(curr==x/mid) return mid; 13 | else if(curr>x/mid) h=mid-1; 14 | else{ 15 | ans=mid; 16 | l=mid+1; 17 | } 18 | } 19 | 20 | return ans; 21 | } 22 | }; -------------------------------------------------------------------------------- /69-sqrtx/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /690-employee-importance/690-employee-importance.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | // Definition for Employee. 3 | class Employee { 4 | public: 5 | int id; 6 | int importance; 7 | vector subordinates; 8 | }; 9 | */ 10 | 11 | class Solution { 12 | public: 13 | 14 | unordered_map m; 15 | 16 | int recur(int id){ 17 | int importance = m[id]->importance; 18 | 19 | for(auto a:m[id]->subordinates){ 20 | importance += recur(a); 21 | } 22 | 23 | return importance; 24 | } 25 | 26 | int getImportance(vector employees, int id) { 27 | 28 | 29 | 30 | for(auto a:employees){ 31 | m[a->id] = a; 32 | } 33 | 34 | 35 | int ans = recur(id); 36 | 37 | 38 | 39 | return ans; 40 | } 41 | }; -------------------------------------------------------------------------------- /690-employee-importance/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /709-to-lower-case/709-to-lower-case.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string toLowerCase(string s) { 4 | 5 | cout<<(int)'a'<<" "<<(int)'A'<<" "<<(int)'Z'; 6 | 7 | 8 | for(int i=0;i=65 && ((int)s[i]<=90)){ 10 | s[i]=s[i]+32; 11 | } 12 | } 13 | 14 | 15 | 16 | return s; 17 | } 18 | }; -------------------------------------------------------------------------------- /709-to-lower-case/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /71-simplify-path/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /739-daily-temperatures/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /76-minimum-window-substring/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /763-partition-labels/763-partition-labels.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector partitionLabels(string s) { 4 | unordered_map m; 5 | 6 | for(int i=0;i ans; 12 | 13 | int end=m[s[0]]; 14 | int size = 1; 15 | 16 | for(int i=1;iend){ 19 | ans.push_back(size); 20 | size=0; 21 | } 22 | size++; 23 | end=max(end,m[s[i]]); 24 | } 25 | 26 | ans.push_back(size); 27 | 28 | return ans; 29 | } 30 | }; -------------------------------------------------------------------------------- /763-partition-labels/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /767-reorganize-string/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /802-find-eventual-safe-states/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /804-unique-morse-code-words/804-unique-morse-code-words.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int uniqueMorseRepresentations(vector& words) { 4 | vector dict = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."}; 5 | 6 | 7 | set finalS; 8 | 9 | for(auto a:words){ 10 | string temp = ""; 11 | 12 | for(auto p:a){ 13 | int id = int(p) - 97; 14 | temp+=dict[id]; 15 | } 16 | 17 | finalS.insert(temp); 18 | } 19 | 20 | return finalS.size(); 21 | } 22 | }; -------------------------------------------------------------------------------- /804-unique-morse-code-words/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /819-most-common-word/819-most-common-word.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string mostCommonWord(string p, vector& banned) { 4 | unordered_set ban(banned.begin(), banned.end()); 5 | unordered_map count; 6 | for (auto & c: p) c = isalpha(c) ? tolower(c) : ' '; 7 | istringstream iss(p); 8 | string w; 9 | pair res ("", 0); 10 | while (iss >> w) 11 | if (ban.find(w) == ban.end() && ++count[w] > res.second) 12 | res = make_pair(w, count[w]); 13 | return res.first; 14 | } 15 | }; -------------------------------------------------------------------------------- /819-most-common-word/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /84-largest-rectangle-in-histogram/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /85-maximal-rectangle/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /863-all-nodes-distance-k-in-binary-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /867-transpose-matrix/867-transpose-matrix.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> transpose(vector>& matrix) { 4 | int n = matrix.size(); 5 | int m = matrix[0].size(); 6 | 7 | vector> ans(m,vector(n,0)); 8 | 9 | vector temp; 10 | 11 | 12 | for(int i=0;i& nums1, int m, vector& nums2, int n) { 4 | int i = m-1; 5 | int j = n-1; 6 | int tar = m+n-1; 7 | 8 | while(j>=0){ 9 | if(i>=0 && nums1[i]>nums2[j]){ 10 | nums1[tar]=nums1[i]; 11 | tar--; 12 | i--; 13 | }else{ 14 | nums1[tar]=nums2[j]; 15 | j--; 16 | tar--; 17 | } 18 | } 19 | 20 | } 21 | }; -------------------------------------------------------------------------------- /88-merge-sorted-array/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /894-all-possible-full-binary-trees/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /901-online-stock-span/901-online-stock-span.cpp: -------------------------------------------------------------------------------- 1 | class StockSpanner { 2 | public: 3 | stack> st; 4 | StockSpanner() { 5 | 6 | } 7 | 8 | int next(int price) { 9 | int res = 1; 10 | while(!st.empty() && st.top().first<=price) { 11 | res+=st.top().second; 12 | st.pop(); 13 | } 14 | 15 | st.push({price,res}); 16 | 17 | return res; 18 | 19 | 20 | 21 | } 22 | }; 23 | 24 | /** 25 | * Your StockSpanner object will be instantiated and called as such: 26 | * StockSpanner* obj = new StockSpanner(); 27 | * int param_1 = obj->next(price); 28 | */ -------------------------------------------------------------------------------- /901-online-stock-span/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /904-fruit-into-baskets/904-fruit-into-baskets.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int totalFruit(vector& fruits) { 4 | int a= 0, b=0, count_a = 0, count_b = 0 , res=0,curr=0; 5 | for(auto c:fruits){ 6 | curr = c==a||b==c? curr+1:count_b+1; 7 | count_b = c ==b? count_b+1:1; 8 | if(b!=c) a=b,b=c; 9 | res = max(res,curr); 10 | } 11 | 12 | 13 | return res; 14 | } 15 | }; -------------------------------------------------------------------------------- /904-fruit-into-baskets/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /91-decode-ways/91-decode-ways.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numDecodings(string s) { 4 | 5 | int count[s.size()+1]; 6 | 7 | count[0]=1,count[1]=1; 8 | 9 | if(s[0]=='0') return 0; 10 | 11 | for(int i=2;i<=s.size();i++){ 12 | 13 | count[i]=0; 14 | 15 | if(s[i-1]>'0') 16 | count[i]=count[i-1]; 17 | 18 | if(s[i-2]=='1' || (s[i-2]=='2' && s[i-1]<'7') ) 19 | count[i]+=count[i-2]; 20 | 21 | 22 | 23 | } 24 | 25 | return count[s.size()]; 26 | } 27 | }; -------------------------------------------------------------------------------- /91-decode-ways/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /921-minimum-add-to-make-parentheses-valid/921-minimum-add-to-make-parentheses-valid.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minAddToMakeValid(string s) { 4 | stack st; 5 | int ans=0; 6 | for(auto a:s){ 7 | if(a==')'){ 8 | if(st.size() && st.top()=='('){ 9 | st.pop(); 10 | }else{ 11 | ans++; 12 | } 13 | }else{ 14 | st.push(a); 15 | } 16 | } 17 | 18 | 19 | return st.size()+ans; 20 | } 21 | }; -------------------------------------------------------------------------------- /921-minimum-add-to-make-parentheses-valid/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /937-reorder-data-in-log-files/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /941-valid-mountain-array/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /97-interleaving-string/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /973-k-closest-points-to-origin/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /Cyclically rotate an array by one - GFG/cyclically-rotate-an-array-by-one.cpp: -------------------------------------------------------------------------------- 1 | // { Driver Code Starts 2 | //Initial Template for C++ 3 | 4 | #include 5 | using namespace std; 6 | void rotate(int arr[], int n); 7 | 8 | int main() 9 | { 10 | int t; 11 | scanf("%d",&t); 12 | while(t--) 13 | { 14 | int n; 15 | scanf("%d",&n); 16 | int a[n] , i; 17 | for(i=0;i=1;i--){ 35 | arr[i]=arr[i-1]; 36 | } 37 | 38 | arr[0]=temp; 39 | 40 | } -------------------------------------------------------------------------------- /Maximum money - GFG/maximum-money.cpp: -------------------------------------------------------------------------------- 1 | // { Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | class Solution { 7 | public: 8 | int maximizeMoney(int N , int K) { 9 | // code here 10 | int temp = (N+1)/2; 11 | return K*temp; 12 | } 13 | }; 14 | 15 | // { Driver Code Starts. 16 | int main() { 17 | int t; 18 | cin >> t; 19 | while (t--) { 20 | int N,K; 21 | 22 | cin>>N>>K; 23 | 24 | Solution ob; 25 | cout << ob.maximizeMoney(N,K) << endl; 26 | } 27 | return 0; 28 | } // } Driver Code Ends -------------------------------------------------------------------------------- /Number of paths - GFG/number-of-paths.cpp: -------------------------------------------------------------------------------- 1 | // { Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | 6 | // } Driver Code Ends 7 | long long numberOfPaths(int m, int n,int i=0,int j=0) 8 | { 9 | // Code 10 | if(i==m-1 && j==n-1) return 1; 11 | if(i>=m || j>=n) return 0; 12 | 13 | return numberOfPaths(m,n,i+1,j)+numberOfPaths(m,n,i,j+1); 14 | } 15 | 16 | // { Driver Code Starts. 17 | 18 | 19 | int main() 20 | { 21 | int t; 22 | cin>>t; 23 | while(t--) 24 | { 25 | int n,m; 26 | cin>>m>>n; 27 | cout << numberOfPaths(m, n)< 5 | using namespace std; 6 | 7 | // } Driver Code Ends 8 | // User function Template for C++ 9 | 10 | class Solution{ 11 | public: 12 | long long int arrangeTiles(int N){ 13 | // code here 14 | vector dp(N+1,0); 15 | 16 | if(N<4) return 1; 17 | 18 | dp[0]=dp[1]=dp[2]=dp[3]=1; 19 | 20 | for(int i=4;i<=N;i++) dp[i]=dp[i-1]+dp[i-4]; 21 | 22 | return dp[N]; 23 | } 24 | }; 25 | 26 | // { Driver Code Starts. 27 | 28 | int main(){ 29 | int t; 30 | cin>>t; 31 | while(t--){ 32 | int N; 33 | cin>>N; 34 | 35 | Solution ob; 36 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | //code 6 | int t; 7 | cin>>t; 8 | while(t--){ 9 | int n; 10 | cin>>n; 11 | int arr[n]; 12 | for(int i=0;i>temp; 15 | arr[i]=temp; 16 | } 17 | 18 | for(int i=0;i 5 | using namespace std; 6 | 7 | // } Driver Code Ends 8 | //User function Template for C++ 9 | 10 | class Solution{ 11 | public: 12 | int minimumStep(int n){ 13 | //complete the function here 14 | if(n==1) return 0; 15 | if(n%3==0) return 1+minimumStep(n/3); 16 | return 1+minimumStep(n-1); 17 | } 18 | }; 19 | 20 | // { Driver Code Starts. 21 | 22 | int main() 23 | { 24 | int t; 25 | cin>>t; 26 | while(t--) 27 | { 28 | int n; 29 | cin >> n; 30 | Solution ob; 31 | cout << ob.minimumStep(n) << endl; 32 | 33 | } 34 | return 0; 35 | } 36 | 37 | // } Driver Code Ends -------------------------------------------------------------------------------- /Trailing zeroes in factorial - GFG/trailing-zeroes-in-factorial.cpp: -------------------------------------------------------------------------------- 1 | // { Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | //User function Template for C++ 7 | class Solution 8 | { 9 | public: 10 | int trailingZeroes(int N) 11 | { 12 | // Write Your Code here 13 | int ans = 0; 14 | 15 | for(int i=0;i<13;i++){ 16 | ans+=N/pow(5,i+1); 17 | } 18 | 19 | return ans; 20 | } 21 | }; 22 | 23 | // { Driver Code Starts. 24 | int main() 25 | { 26 | int t; 27 | cin >> t; 28 | while (t--) 29 | { 30 | int N; 31 | cin >> N; 32 | Solution ob; 33 | int ans = ob.trailingZeroes(N); 34 | cout<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 |
-------------------------------------------------------------------------------- /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) -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /best-time-to-buy-and-sell-stock-iv/best-time-to-buy-and-sell-stock-iv.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProfit(int k, vector& 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;iright) bstToGst(root->right); 17 | temp=root->val=root->val+temp; 18 | if(root->left) bstToGst(root->left); 19 | return root; 20 | } 21 | }; -------------------------------------------------------------------------------- /binary-search/binary-search.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int search(vector& 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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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;iright) convertBST(root->right); 18 | temp=root->val=root->val+temp; 19 | if(root->left) convertBST(root->left); 20 | return root; 21 | } 22 | }; -------------------------------------------------------------------------------- /count-items-matching-a-rule/count-items-matching-a-rule.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countMatches(vector>& 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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 |
-------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 |
-------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 |
-------------------------------------------------------------------------------- /duplicate-zeros/duplicate-zeros.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void duplicateZeros(vector& arr) { 4 | int n = arr.size(); 5 | vector newArr(n,-1); 6 | int fI=0,sI=0; 7 | 8 | while(fI1287. 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 |
-------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | } -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 |
-------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /kth-largest-element-in-a-stream/kth-largest-element-in-a-stream.cpp: -------------------------------------------------------------------------------- 1 | class KthLargest { 2 | public: 3 | priority_queue,greater> pq; 4 | int eleK; 5 | KthLargest(int k, vector& nums) { 6 | eleK = k; 7 | for(auto a:nums){ 8 | pq.push(a); 9 | if(pq.size()>k) pq.pop(); 10 | } 11 | } 12 | 13 | int add(int val) { 14 | pq.push(val); 15 | if(pq.size()>eleK) pq.pop(); 16 | return pq.top(); 17 | } 18 | }; 19 | 20 | /** 21 | * Your KthLargest object will be instantiated and called as such: 22 | * KthLargest* obj = new KthLargest(k, nums); 23 | * int param_1 = obj->add(val); 24 | */ -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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<0){ 6 | sum+=num%10; 7 | num=num/10; 8 | } 9 | 10 | return sum; 11 | } 12 | int countBalls(int lowLimit, int highLimit) { 13 | unordered_map m; 14 | int ans = INT_MIN; 15 | for(int i=lowLimit;i<=highLimit;i++){ 16 | int temp = findDigSum(i); 17 | m[temp]++; 18 | ans=max(m[temp],ans); 19 | } 20 | 21 | return ans; 22 | } 23 | }; -------------------------------------------------------------------------------- /maximum-product-of-three-numbers/README.md: -------------------------------------------------------------------------------- 1 |

628. Maximum Product of Three Numbers

Easy


Given an integer array nums, find three numbers whose product is maximum and return the maximum product.

2 | 3 |

 

4 |

Example 1:

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

Example 2:

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

Example 3:

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

 

15 |

Constraints:

16 | 17 |
    18 |
  • 3 <= nums.length <= 104
  • 19 |
  • -1000 <= nums[i] <= 1000
  • 20 |
21 |
-------------------------------------------------------------------------------- /maximum-product-of-three-numbers/maximum-product-of-three-numbers.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maximumProduct(vector& nums) { 4 | int n = nums.size(); 5 | sort(nums.begin(),nums.end()); 6 | 7 | return max(nums[n-1]*nums[n-2]*nums[n-3],nums[0]*nums[1]*nums[n-1]); 8 | } 9 | }; -------------------------------------------------------------------------------- /maximum-product-subarray/maximum-product-subarray.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProduct(vector& 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 | }; -------------------------------------------------------------------------------- /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 |
-------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /nim-game/nim-game.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canWinNim(int n) { 4 | return (n%4); 5 | } 6 | }; -------------------------------------------------------------------------------- /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 |
-------------------------------------------------------------------------------- /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 -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 |
-------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /relative-sort-array/relative-sort-array.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def relativeSortArray(self, A, B): 3 | k = {b: i for i, b in enumerate(B)} 4 | return sorted(A, key=lambda a: k.get(a, 1000 + a)) 5 | """ 6 | :type arr1: List[int] 7 | :type arr2: List[int] 8 | :rtype: List[int] 9 | """ 10 | -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 |
-------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 m; 5 | priority_queue pq; 6 | for(auto a:s){ 7 | if(isdigit(a) && m.find(a)==m.end()){ 8 | pq.push(a - '0'); 9 | m[a]=true; 10 | } 11 | } 12 | 13 | if(pq.size()<2) return -1; 14 | pq.pop(); 15 | return pq.top(); 16 | } 17 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | int start =0,end = nums.size()-1; 5 | while(start& 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 | }; -------------------------------------------------------------------------------- /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 |
-------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /sorting-the-sentence/sorting-the-sentence.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string sortSentence(string s) { 4 | priority_queue,vector>,greater>> pq; 5 | string temp = ""; 6 | for(auto a:s){ 7 | if(a==' ') continue; 8 | if(isdigit(a)){ 9 | 10 | pq.push({(int)a,temp}); 11 | temp=""; 12 | }else{ 13 | temp+=a; 14 | } 15 | } 16 | 17 | string ans = ""; 18 | while(pq.size()){ 19 | ans+=pq.top().second+" "; 20 | pq.pop(); 21 | } 22 | ans.pop_back(); 23 | return ans; 24 | } 25 | }; -------------------------------------------------------------------------------- /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 |
-------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 |
-------------------------------------------------------------------------------- /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 |
-------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 |
-------------------------------------------------------------------------------- /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 | } -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 |
-------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /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 | }; --------------------------------------------------------------------------------