├── 0001-two-sum ├── 0001-two-sum.cpp ├── NOTES.md └── README.md ├── 0003-longest-substring-without-repeating-characters ├── 0003-longest-substring-without-repeating-characters.cpp ├── NOTES.md └── README.md ├── 0004-median-of-two-sorted-arrays ├── 0004-median-of-two-sorted-arrays.cpp ├── NOTES.md └── README.md ├── 0005-longest-palindromic-substring ├── 0005-longest-palindromic-substring.cpp ├── NOTES.md └── README.md ├── 0012-integer-to-roman ├── 0012-integer-to-roman.cpp ├── NOTES.md └── README.md ├── 0026-remove-duplicates-from-sorted-array ├── 0026-remove-duplicates-from-sorted-array.cpp ├── NOTES.md └── README.md ├── 0034-find-first-and-last-position-of-element-in-sorted-array ├── 0034-find-first-and-last-position-of-element-in-sorted-array.cpp ├── NOTES.md └── README.md ├── 0038-count-and-say ├── 0038-count-and-say.cpp ├── NOTES.md └── README.md ├── 0049-group-anagrams ├── NOTES.md └── README.md ├── 0050-powx-n ├── 0050-powx-n.cpp ├── NOTES.md └── README.md ├── 0055-jump-game ├── 0055-jump-game.cpp ├── NOTES.md └── README.md ├── 0061-rotate-list ├── 0061-rotate-list.cpp ├── NOTES.md └── README.md ├── 0063-unique-paths-ii ├── 0063-unique-paths-ii.cpp ├── NOTES.md └── README.md ├── 0067-add-binary ├── 0067-add-binary.cpp └── README.md ├── 0070-climbing-stairs ├── 0070-climbing-stairs.cpp ├── NOTES.md └── README.md ├── 0076-minimum-window-substring ├── NOTES.md └── README.md ├── 0080-remove-duplicates-from-sorted-array-ii ├── 0080-remove-duplicates-from-sorted-array-ii.cpp ├── NOTES.md └── README.md ├── 0093-restore-ip-addresses ├── 0093-restore-ip-addresses.cpp └── README.md ├── 0094-binary-tree-inorder-traversal ├── 0094-binary-tree-inorder-traversal.cpp ├── NOTES.md └── README.md ├── 0100-same-tree ├── 0100-same-tree.cpp ├── NOTES.md └── README.md ├── 0119-pascals-triangle-ii ├── 0119-pascals-triangle-ii.cpp ├── NOTES.md └── README.md ├── 0128-longest-consecutive-sequence ├── 0128-longest-consecutive-sequence.cpp ├── NOTES.md └── README.md ├── 0131-palindrome-partitioning ├── NOTES.md └── README.md ├── 0134-gas-station ├── 0134-gas-station.cpp └── NOTES.md ├── 0144-binary-tree-preorder-traversal ├── 0144-binary-tree-preorder-traversal.cpp ├── NOTES.md └── README.md ├── 0149-max-points-on-a-line ├── 0149-max-points-on-a-line.cpp ├── NOTES.md └── README.md ├── 0169-majority-element ├── 0169-majority-element.cpp ├── NOTES.md └── README.md ├── 0209-minimum-size-subarray-sum ├── 0209-minimum-size-subarray-sum.cpp ├── NOTES.md └── README.md ├── 0219-contains-duplicate-ii ├── NOTES.md └── README.md ├── 0229-majority-element-ii ├── 0229-majority-element-ii.cpp ├── NOTES.md └── README.md ├── 0232-implement-queue-using-stacks ├── 0232-implement-queue-using-stacks.cpp ├── NOTES.md └── README.md ├── 0237-delete-node-in-a-linked-list ├── 0237-delete-node-in-a-linked-list.cpp ├── NOTES.md └── README.md ├── 0242-valid-anagram ├── 0242-valid-anagram.cpp ├── NOTES.md └── README.md ├── 0290-word-pattern ├── 0290-word-pattern.cpp ├── NOTES.md └── README.md ├── 0300-longest-increasing-subsequence ├── 0300-longest-increasing-subsequence.cpp ├── NOTES.md └── README.md ├── 0322-coin-change ├── 0322-coin-change.cpp ├── NOTES.md └── README.md ├── 0329-longest-increasing-path-in-a-matrix ├── 0329-longest-increasing-path-in-a-matrix.cpp ├── NOTES.md └── README.md ├── 0334-increasing-triplet-subsequence ├── NOTES.md └── README.md ├── 0341-flatten-nested-list-iterator ├── NOTES.md └── README.md ├── 0342-power-of-four ├── 0342-power-of-four.cpp ├── NOTES.md └── README.md ├── 0343-integer-break ├── 0343-integer-break.cpp ├── NOTES.md └── README.md ├── 0347-top-k-frequent-elements ├── 0347-top-k-frequent-elements.cpp ├── NOTES.md └── README.md ├── 0352-data-stream-as-disjoint-intervals └── NOTES.md ├── 0380-insert-delete-getrandom-o1 ├── NOTES.md └── README.md ├── 0389-find-the-difference ├── 0389-find-the-difference.cpp ├── NOTES.md └── README.md ├── 0392-is-subsequence ├── 0392-is-subsequence.cpp ├── NOTES.md └── README.md ├── 0452-minimum-number-of-arrows-to-burst-balloons ├── 0452-minimum-number-of-arrows-to-burst-balloons.cpp ├── NOTES.md └── README.md ├── 0460-lfu-cache ├── 0460-lfu-cache.cpp └── NOTES.md ├── 0472-concatenated-words └── NOTES.md ├── 0491-non-decreasing-subsequences ├── 0491-non-decreasing-subsequences.cpp └── README.md ├── 0501-find-mode-in-binary-search-tree ├── 0501-find-mode-in-binary-search-tree.cpp ├── NOTES.md └── README.md ├── 0515-find-largest-value-in-each-tree-row ├── 0515-find-largest-value-in-each-tree-row.cpp ├── NOTES.md └── README.md ├── 0520-detect-capital ├── NOTES.md └── README.md ├── 0523-continuous-subarray-sum ├── NOTES.md └── README.md ├── 0557-reverse-words-in-a-string-iii ├── 0557-reverse-words-in-a-string-iii.cpp ├── NOTES.md └── README.md ├── 0606-construct-string-from-binary-tree ├── 0606-construct-string-from-binary-tree.cpp ├── NOTES.md └── README.md ├── 0617-merge-two-binary-trees ├── 0617-merge-two-binary-trees.cpp ├── NOTES.md └── README.md ├── 0629-k-inverse-pairs-array ├── 0629-k-inverse-pairs-array.cpp ├── NOTES.md └── README.md ├── 0645-set-mismatch ├── 0645-set-mismatch.cpp ├── NOTES.md └── README.md ├── 0647-palindromic-substrings ├── 0647-palindromic-substrings.cpp ├── NOTES.md └── README.md ├── 0692-top-k-frequent-words ├── NOTES.md └── README.md ├── 0739-daily-temperatures ├── 0739-daily-temperatures.cpp ├── NOTES.md └── README.md ├── 0746-min-cost-climbing-stairs ├── 0746-min-cost-climbing-stairs.cpp ├── NOTES.md └── README.md ├── 0766-toeplitz-matrix ├── 0766-toeplitz-matrix.cpp └── NOTES.md ├── 0779-k-th-symbol-in-grammar ├── 0779-k-th-symbol-in-grammar.cpp ├── NOTES.md └── README.md ├── 0787-cheapest-flights-within-k-stops ├── 0787-cheapest-flights-within-k-stops.cpp ├── NOTES.md └── README.md ├── 0790-domino-and-tromino-tiling ├── 0790-domino-and-tromino-tiling.cpp ├── NOTES.md └── README.md ├── 0797-all-paths-from-source-to-target ├── 0797-all-paths-from-source-to-target.cpp ├── NOTES.md └── README.md ├── 0823-binary-trees-with-factors ├── NOTES.md └── README.md ├── 0834-sum-of-distances-in-tree └── README.md ├── 0835-image-overlap ├── NOTES.md └── README.md ├── 0841-keys-and-rooms ├── 0841-keys-and-rooms.cpp └── README.md ├── 0844-backspace-string-compare ├── 0844-backspace-string-compare.cpp ├── NOTES.md └── README.md ├── 0859-buddy-strings ├── 0859-buddy-strings.cpp ├── NOTES.md └── README.md ├── 0867-transpose-matrix ├── 0867-transpose-matrix.cpp ├── NOTES.md └── README.md ├── 0876-middle-of-the-linked-list ├── 0876-middle-of-the-linked-list.cpp ├── NOTES.md └── README.md ├── 0886-possible-bipartition ├── NOTES.md └── README.md ├── 0907-sum-of-subarray-minimums ├── 0907-sum-of-subarray-minimums.cpp ├── NOTES.md └── README.md ├── 0909-snakes-and-ladders └── NOTES.md ├── 0926-flip-string-to-monotone-increasing └── README.md ├── 0931-minimum-falling-path-sum ├── NOTES.md └── README.md ├── 0938-range-sum-of-bst ├── 0938-range-sum-of-bst.cpp ├── NOTES.md └── README.md ├── 0944-delete-columns-to-make-sorted ├── 0944-delete-columns-to-make-sorted.cpp ├── NOTES.md └── README.md ├── 0974-subarray-sums-divisible-by-k ├── 0974-subarray-sums-divisible-by-k.cpp ├── NOTES.md └── README.md ├── 0976-largest-perimeter-triangle ├── 0976-largest-perimeter-triangle.cpp ├── NOTES.md └── README.md ├── 0980-unique-paths-iii ├── 0980-unique-paths-iii.cpp ├── NOTES.md └── README.md ├── 0997-find-the-town-judge ├── 0997-find-the-town-judge.cpp ├── NOTES.md └── README.md ├── 1026-maximum-difference-between-node-and-ancestor ├── 1026-maximum-difference-between-node-and-ancestor.cpp ├── NOTES.md └── README.md ├── 1061-lexicographically-smallest-equivalent-string ├── 1061-lexicographically-smallest-equivalent-string.cpp ├── NOTES.md └── README.md ├── 1074-number-of-submatrices-that-sum-to-target ├── NOTES.md └── README.md ├── 112-path-sum ├── 112-path-sum.cpp └── NOTES.md ├── 1143-longest-common-subsequence ├── 1143-longest-common-subsequence.cpp └── README.md ├── 1155-number-of-dice-rolls-with-target-sum ├── 1155-number-of-dice-rolls-with-target-sum.cpp ├── NOTES.md └── README.md ├── 1200-minimum-absolute-difference ├── 1200-minimum-absolute-difference.cpp ├── NOTES.md └── README.md ├── 1207-unique-number-of-occurrences ├── 1207-unique-number-of-occurrences.cpp ├── NOTES.md └── README.md ├── 1220-count-vowels-permutation ├── 1220-count-vowels-permutation.cpp ├── NOTES.md └── README.md ├── 1235-maximum-profit-in-job-scheduling ├── 1235-maximum-profit-in-job-scheduling.cpp ├── NOTES.md └── README.md ├── 1239-maximum-length-of-a-concatenated-string-with-unique-characters ├── NOTES.md └── README.md ├── 1269-number-of-ways-to-stay-in-the-same-place-after-some-steps ├── 1269-number-of-ways-to-stay-in-the-same-place-after-some-steps.cpp ├── NOTES.md └── README.md ├── 1287-element-appearing-more-than-25-in-sorted-array ├── 1287-element-appearing-more-than-25-in-sorted-array.cpp ├── NOTES.md └── README.md ├── 1293-shortest-path-in-a-grid-with-obstacles-elimination ├── NOTES.md └── README.md ├── 1335-minimum-difficulty-of-a-job-schedule ├── 1335-minimum-difficulty-of-a-job-schedule.cpp ├── NOTES.md └── README.md ├── 1347-minimum-number-of-steps-to-make-two-strings-anagram ├── 1347-minimum-number-of-steps-to-make-two-strings-anagram.cpp ├── NOTES.md └── README.md ├── 1356-sort-integers-by-the-number-of-1-bits ├── 1356-sort-integers-by-the-number-of-1-bits.cpp ├── NOTES.md └── README.md ├── 1420-build-array-where-you-can-find-the-maximum-exactly-k-comparisons ├── 1420-build-array-where-you-can-find-the-maximum-exactly-k-comparisons.cpp ├── NOTES.md └── README.md ├── 1425-constrained-subsequence-sum ├── 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 ├── 1457-pseudo-palindromic-paths-in-a-binary-tree ├── 1457-pseudo-palindromic-paths-in-a-binary-tree.cpp ├── NOTES.md └── README.md ├── 1458-max-dot-product-of-two-subsequences ├── 1458-max-dot-product-of-two-subsequences.cpp ├── NOTES.md └── README.md ├── 1493-longest-subarray-of-1s-after-deleting-one-element ├── 1493-longest-subarray-of-1s-after-deleting-one-element.cpp ├── NOTES.md └── README.md ├── 1512-number-of-good-pairs ├── 1512-number-of-good-pairs.cpp ├── NOTES.md └── README.md ├── 1519-number-of-nodes-in-the-sub-tree-with-the-same-label ├── 1519-number-of-nodes-in-the-sub-tree-with-the-same-label.cpp ├── NOTES.md └── README.md ├── 1531-string-compression-ii ├── 1531-string-compression-ii.cpp ├── NOTES.md └── README.md ├── 1535-find-the-winner-of-an-array-game ├── 1535-find-the-winner-of-an-array-game.cpp ├── NOTES.md └── README.md ├── 16-3sum-closest ├── NOTES.md └── README.md ├── 1624-largest-substring-between-two-equal-characters ├── 1624-largest-substring-between-two-equal-characters.cpp ├── NOTES.md └── README.md ├── 1626-best-team-with-no-conflicts ├── 1626-best-team-with-no-conflicts.cpp ├── NOTES.md └── README.md ├── 1662-check-if-two-string-arrays-are-equivalent ├── 1662-check-if-two-string-arrays-are-equivalent.cpp ├── NOTES.md └── README.md ├── 1688-count-of-matches-in-tournament ├── 1688-count-of-matches-in-tournament.cpp ├── NOTES.md └── README.md ├── 1704-determine-if-string-halves-are-alike ├── 1704-determine-if-string-halves-are-alike.cpp ├── NOTES.md └── README.md ├── 1706-where-will-the-ball-fall ├── 1706-where-will-the-ball-fall.cpp ├── NOTES.md └── README.md ├── 1793-maximum-score-of-a-good-subarray ├── 1793-maximum-score-of-a-good-subarray.cpp ├── NOTES.md └── README.md ├── 1832-check-if-the-sentence-is-pangram ├── NOTES.md └── README.md ├── 1834-single-threaded-cpu ├── 1834-single-threaded-cpu.cpp ├── NOTES.md └── README.md ├── 1845-seat-reservation-manager ├── 1845-seat-reservation-manager.cpp ├── NOTES.md └── README.md ├── 1897-redistribute-characters-to-make-all-strings-equal └── README.md ├── 1903-largest-odd-number-in-string ├── 1903-largest-odd-number-in-string.cpp ├── NOTES.md └── README.md ├── 1921-eliminate-maximum-number-of-monsters ├── 1921-eliminate-maximum-number-of-monsters.cpp ├── NOTES.md └── README.md ├── 1971-find-if-path-exists-in-graph ├── 1971-find-if-path-exists-in-graph.cpp └── README.md ├── 2009-minimum-number-of-operations-to-make-array-continuous ├── NOTES.md └── README.md ├── 2024-maximize-the-confusion-of-an-exam ├── 2024-maximize-the-confusion-of-an-exam.cpp ├── NOTES.md └── README.md ├── 2038-remove-colored-pieces-if-both-neighbors-are-the-same-color ├── 2038-remove-colored-pieces-if-both-neighbors-are-the-same-color.cpp ├── NOTES.md └── README.md ├── 2050-parallel-courses-iii ├── 2050-parallel-courses-iii.cpp ├── NOTES.md └── README.md ├── 2095-delete-the-middle-node-of-a-linked-list ├── 2095-delete-the-middle-node-of-a-linked-list.cpp ├── NOTES.md └── README.md ├── 2125-number-of-laser-beams-in-a-bank ├── 2125-number-of-laser-beams-in-a-bank.cpp ├── NOTES.md └── README.md ├── 2131-longest-palindrome-by-concatenating-two-letter-words ├── 2131-longest-palindrome-by-concatenating-two-letter-words.cpp └── NOTES.md ├── 2136-earliest-possible-day-of-full-bloom ├── NOTES.md └── README.md ├── 2244-minimum-rounds-to-complete-all-tasks ├── 2244-minimum-rounds-to-complete-all-tasks.cpp ├── NOTES.md └── README.md ├── 2264-largest-3-same-digit-number-in-string ├── 2264-largest-3-same-digit-number-in-string.cpp ├── NOTES.md └── README.md ├── 2359-find-closest-node-to-given-two-nodes └── README.md ├── 2385-amount-of-time-for-binary-tree-to-be-infected ├── 2385-amount-of-time-for-binary-tree-to-be-infected.cpp ├── NOTES.md └── README.md ├── 2389-longest-subsequence-with-limited-sum ├── NOTES.md └── README.md ├── 2421-number-of-good-paths ├── 2421-number-of-good-paths.cpp ├── NOTES.md └── README.md ├── 2433-find-the-original-array-of-prefix-xor ├── 2433-find-the-original-array-of-prefix-xor.cpp ├── NOTES.md └── README.md ├── 2482-difference-between-ones-and-zeros-in-row-and-column ├── 2482-difference-between-ones-and-zeros-in-row-and-column.cpp ├── NOTES.md └── README.md ├── 2551-put-marbles-in-bags ├── 2551-put-marbles-in-bags.cpp ├── NOTES.md └── README.md ├── 2610-convert-an-array-into-a-2d-array-with-conditions ├── 2610-convert-an-array-into-a-2d-array-with-conditions.cpp ├── NOTES.md └── README.md ├── 2642-design-graph-with-shortest-path-calculator ├── 2642-design-graph-with-shortest-path-calculator.cpp ├── NOTES.md └── README.md ├── 2742-painting-the-walls ├── NOTES.md └── README.md ├── 2849-determine-if-a-cell-is-reachable-at-a-given-time ├── 2849-determine-if-a-cell-is-reachable-at-a-given-time.cpp ├── NOTES.md └── README.md ├── 2870-minimum-number-of-operations-to-make-array-empty ├── 2870-minimum-number-of-operations-to-make-array-empty.cpp ├── NOTES.md └── README.md ├── 32-longest-valid-parentheses ├── 32-longest-valid-parentheses.cpp ├── NOTES.md └── README.md ├── 474-ones-and-zeroes ├── 474-ones-and-zeroes.cpp ├── NOTES.md └── README.md ├── 647-palindromic-substrings ├── 647-palindromic-substrings.cpp ├── NOTES.md └── README.md ├── 653-two-sum-iv-input-is-a-bst ├── 653-two-sum-iv-input-is-a-bst.cpp └── README.md ├── 732-my-calendar-iii ├── 732-my-calendar-iii.cpp └── README.md ├── 91-decode-ways ├── 91-decode-ways.cpp ├── NOTES.md └── README.md ├── 981-time-based-key-value-store ├── 981-time-based-key-value-store.cpp ├── NOTES.md └── README.md └── README.md /0001-two-sum/0001-two-sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | public: 4 | vector twoSum(vector& nums, int target) { 5 | 6 | unordered_map mp; 7 | 8 | for(int i =0 ;i < nums.size();i++){ 9 | 10 | int tar = target - nums[i]; 11 | 12 | if (mp.find(tar) != mp.end()) { 13 | 14 | return {i, mp[tar]}; 15 | } 16 | 17 | mp[nums[i]] = i; 18 | } 19 | 20 | return {-1,-1}; 21 | } 22 | }; -------------------------------------------------------------------------------- /0001-two-sum/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0003-longest-substring-without-repeating-characters/0003-longest-substring-without-repeating-characters.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int lengthOfLongestSubstring(string s) 5 | { 6 | set ss; 7 | 8 | int maximum = INT_MIN; 9 | 10 | if (s.length() == 0) 11 | return 0; 12 | 13 | for (int i = 0; i < s.size(); i++) 14 | { 15 | for (int j = i; j < s.size(); j++) 16 | { 17 | if (ss.find(s[j]) != ss.end()) 18 | break; 19 | 20 | ss.insert(s[j]); 21 | } 22 | 23 | maximum = max(maximum, (int) ss.size()); 24 | ss.clear(); 25 | } 26 | 27 | return maximum; 28 | } 29 | }; -------------------------------------------------------------------------------- /0003-longest-substring-without-repeating-characters/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0003-longest-substring-without-repeating-characters/README.md: -------------------------------------------------------------------------------- 1 |

3. Longest Substring Without Repeating Characters

Medium


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

2 | 3 |

 

4 |

Example 1:

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

Example 2:

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

Example 3:

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

 

27 |

Constraints:

28 | 29 |
    30 |
  • 0 <= s.length <= 5 * 104
  • 31 |
  • s consists of English letters, digits, symbols and spaces.
  • 32 |
33 |
-------------------------------------------------------------------------------- /0004-median-of-two-sorted-arrays/0004-median-of-two-sorted-arrays.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | double findMedianSortedArrays(vector& nums1, vector& nums2) { 4 | 5 | vector temp (nums1.size() + nums2.size()); 6 | 7 | int i = 0, j = 0, k = 0; 8 | 9 | while(i < nums1.size() && j < nums2.size()){ 10 | 11 | if (nums1[i] < nums2[j]) 12 | temp[k++]=nums1[i++]; 13 | else 14 | temp[k++]=nums2[j++]; 15 | } 16 | 17 | while(i < nums1.size()) 18 | temp[k++]=nums1[i++]; 19 | 20 | while(j < nums2.size()) 21 | temp[k++]=nums2[j++]; 22 | 23 | if (temp.size() % 2 == 1) 24 | return double(temp[temp.size() / 2]); 25 | else { 26 | int num1 = temp[temp.size() / 2 - 1]; 27 | int num2 = temp[temp.size() / 2]; 28 | 29 | return double(num1 + num2) / 2.0; 30 | } 31 | } 32 | }; -------------------------------------------------------------------------------- /0004-median-of-two-sorted-arrays/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0004-median-of-two-sorted-arrays/README.md: -------------------------------------------------------------------------------- 1 |

4. Median of Two Sorted Arrays

Hard


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

2 | 3 |

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

4 | 5 |

 

6 |

Example 1:

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

Example 2:

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

 

21 |

Constraints:

22 | 23 |
    24 |
  • nums1.length == m
  • 25 |
  • nums2.length == n
  • 26 |
  • 0 <= m <= 1000
  • 27 |
  • 0 <= n <= 1000
  • 28 |
  • 1 <= m + n <= 2000
  • 29 |
  • -106 <= nums1[i], nums2[i] <= 106
  • 30 |
31 |
-------------------------------------------------------------------------------- /0005-longest-palindromic-substring/0005-longest-palindromic-substring.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | string checkPalindrom(string s, int l, int r){ 4 | 5 | while(l >= 0 && r < s.size()) { 6 | 7 | if (s[l] != s[r]) 8 | break; 9 | 10 | l--; 11 | r++; 12 | } 13 | 14 | string temp = ""; 15 | 16 | for(int i = l + 1;i maxPalindrome.size()){ 32 | maxPalindrome = temp; 33 | } 34 | 35 | temp = checkPalindrom(s,i,i+1); 36 | 37 | if (temp.size() > maxPalindrome.size()){ 38 | maxPalindrome = temp; 39 | } 40 | } 41 | 42 | return maxPalindrome; 43 | } 44 | }; -------------------------------------------------------------------------------- /0005-longest-palindromic-substring/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0005-longest-palindromic-substring/README.md: -------------------------------------------------------------------------------- 1 |

5. Longest Palindromic Substring

Medium


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

2 | 3 |

 

4 |

Example 1:

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

Example 2:

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

 

18 |

Constraints:

19 | 20 |
    21 |
  • 1 <= s.length <= 1000
  • 22 |
  • s consist of only digits and English letters.
  • 23 |
24 |
-------------------------------------------------------------------------------- /0012-integer-to-roman/0012-integer-to-roman.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string intToRoman(int num) { 4 | string str[13] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}; 5 | 6 | int val[13] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; 7 | 8 | string ans; 9 | 10 | for (int i=0; i<13; i++) { 11 | 12 | for (int j=0; j<(num/val[i]); j++) { 13 | ans += str[i]; 14 | } 15 | 16 | num %= val[i]; 17 | } 18 | 19 | return ans; 20 | } 21 | }; -------------------------------------------------------------------------------- /0012-integer-to-roman/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0026-remove-duplicates-from-sorted-array/0026-remove-duplicates-from-sorted-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int removeDuplicates(vector& nums) { 4 | 5 | int j = 0 ; 6 | 7 | for(int i =0;i nums, int target){ 3 | 4 | int left = 0,right = nums.size() -1, mid, position = -1; 5 | 6 | while (left <= right) { 7 | mid = left + (right - left) / 2; 8 | 9 | if (target > nums[mid]) 10 | left = mid + 1; 11 | else if (target < nums[mid]) 12 | right = mid - 1; 13 | else { 14 | position = mid; 15 | right = mid-1; 16 | } 17 | } 18 | 19 | return position; 20 | } 21 | 22 | int findLast(vector nums, int target){ 23 | 24 | int left = 0,right = nums.size() -1, mid, position = -1; 25 | 26 | while (left <= right) { 27 | mid = left + (right - left) / 2; 28 | 29 | if (target > nums[mid]) 30 | left = mid + 1; 31 | else if (target < nums[mid]) 32 | right = mid - 1; 33 | else { 34 | position = mid; 35 | left = mid+1; 36 | } 37 | } 38 | 39 | return position; 40 | } 41 | 42 | public: 43 | vector searchRange(vector& nums, int target) { 44 | 45 | return {findFirst(nums,target),findLast(nums,target)}; 46 | } 47 | }; -------------------------------------------------------------------------------- /0034-find-first-and-last-position-of-element-in-sorted-array/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0034-find-first-and-last-position-of-element-in-sorted-array/README.md: -------------------------------------------------------------------------------- 1 |

34. Find First and Last Position of Element in Sorted Array

Medium


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

2 | 3 |

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

4 | 5 |

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

6 | 7 |

 

8 |

Example 1:

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

Example 2:

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

Example 3:

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

 

19 |

Constraints:

20 | 21 |
    22 |
  • 0 <= nums.length <= 105
  • 23 |
  • -109 <= nums[i] <= 109
  • 24 |
  • nums is a non-decreasing array.
  • 25 |
  • -109 <= target <= 109
  • 26 |
27 |
-------------------------------------------------------------------------------- /0038-count-and-say/0038-count-and-say.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string countAndSay(int n) { 4 | 5 | if(n==1) 6 | return "1"; 7 | if(n==2) 8 | return "11"; 9 | 10 | 11 | string str = "11"; 12 | 13 | 14 | for(int i=3; i<=n ; i++) 15 | { 16 | string temp = ""; 17 | str = str+"&"; // add a delimeter at the end 18 | int cnt = 1; // counter 19 | 20 | for(int j = 1; j49. Group Anagrams

Medium


Given an array of strings strs, group the anagrams together. You can return the answer in any order.

2 | 3 |

An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

4 | 5 |

 

6 |

Example 1:

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

Example 2:

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

Example 3:

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

 

17 |

Constraints:

18 | 19 |
    20 |
  • 1 <= strs.length <= 104
  • 21 |
  • 0 <= strs[i].length <= 100
  • 22 |
  • strs[i] consists of lowercase English letters.
  • 23 |
24 |
-------------------------------------------------------------------------------- /0050-powx-n/0050-powx-n.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | double myPow(double x, int n) { 4 | double power = 1.00; 5 | int helper = abs(n); 6 | 7 | if (helper == 0) 8 | return power; 9 | else { 10 | 11 | while (helper && helper >= 0){ 12 | 13 | if (helper % 2 == 1 ){ 14 | power = power * x; 15 | helper--; 16 | } 17 | 18 | x *= x; 19 | helper /= 2; 20 | } 21 | } 22 | 23 | 24 | return (n > 0) ? power : 1/power; 25 | } 26 | }; -------------------------------------------------------------------------------- /0050-powx-n/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0050-powx-n/README.md: -------------------------------------------------------------------------------- 1 |

50. Pow(x, n)

Medium


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

2 | 3 |

 

4 |

Example 1:

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

Example 2:

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

Example 3:

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

 

24 |

Constraints:

25 | 26 |
    27 |
  • -100.0 < x < 100.0
  • 28 |
  • -231 <= n <= 231-1
  • 29 |
  • n is an integer.
  • 30 |
  • Either x is not zero or n > 0.
  • 31 |
  • -104 <= xn <= 104
  • 32 |
33 |
-------------------------------------------------------------------------------- /0055-jump-game/0055-jump-game.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canJump(vector& nums) { 4 | int n=nums.size(); 5 | int maxi=0; 6 | for(int i=0;i dp(n+maxi+1,0); 11 | 12 | for(int i=n-1;i=0;i--){ 17 | 18 | if(nums[i]==0){ 19 | dp[i]=0; 20 | } 21 | else{ 22 | bool ans=0; 23 | for(int j=1;j<=nums[i];j++){ 24 | ans=ans|dp[i+j]; 25 | if(ans) break; 26 | } 27 | dp[i]=ans; 28 | } 29 | } 30 | 31 | return dp[0]; 32 | } 33 | }; -------------------------------------------------------------------------------- /0055-jump-game/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0055-jump-game/README.md: -------------------------------------------------------------------------------- 1 |

55. Jump Game

Medium


You are given an integer array nums. You are initially positioned at the array's first index, and each element in the array represents your maximum jump length at that position.

2 | 3 |

Return true if you can reach the last index, or false otherwise.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: nums = [2,3,1,1,4]
 9 | Output: true
10 | Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.
11 | 
12 | 13 |

Example 2:

14 | 15 |
Input: nums = [3,2,1,0,4]
16 | Output: false
17 | Explanation: You will always arrive at index 3 no matter what. Its maximum jump length is 0, which makes it impossible to reach the last index.
18 | 
19 | 20 |

 

21 |

Constraints:

22 | 23 |
    24 |
  • 1 <= nums.length <= 104
  • 25 |
  • 0 <= nums[i] <= 105
  • 26 |
27 |
-------------------------------------------------------------------------------- /0061-rotate-list/0061-rotate-list.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* rotateRight(ListNode* head, int k) { 14 | int size = 1 ; 15 | ListNode* temp = head; 16 | 17 | if (!head || !head->next || !k) { 18 | return head; 19 | } 20 | 21 | while(temp && temp->next) 22 | { 23 | size++; 24 | temp = temp->next; 25 | } 26 | 27 | if (k > size) 28 | k = k % size; 29 | 30 | while(k--){ 31 | ListNode* temp = head; 32 | ListNode* prev = temp; 33 | while(temp && temp->next) 34 | { 35 | prev = temp; 36 | temp = temp->next; 37 | } 38 | 39 | prev->next = nullptr; 40 | temp->next = head; 41 | head = temp; 42 | } 43 | 44 | return head; 45 | } 46 | }; -------------------------------------------------------------------------------- /0061-rotate-list/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0061-rotate-list/README.md: -------------------------------------------------------------------------------- 1 |

61. Rotate List

Medium


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

2 | 3 |

 

4 |

Example 1:

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

Example 2:

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

 

17 |

Constraints:

18 | 19 |
    20 |
  • The number of nodes in the list is in the range [0, 500].
  • 21 |
  • -100 <= Node.val <= 100
  • 22 |
  • 0 <= k <= 2 * 109
  • 23 |
24 |
-------------------------------------------------------------------------------- /0063-unique-paths-ii/0063-unique-paths-ii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | int solve(int i, int j, vector> &dp, vector>& grid) 3 | { 4 | if(grid[i][j]==1) return 0; 5 | 6 | if(i==0 and j==0) return 1; 7 | if(i==0) return dp[i][j-1]; 8 | if(j==0) return dp[i-1][j]; 9 | 10 | return dp[i-1][j] + dp[i][j-1]; 11 | } 12 | 13 | public: 14 | 15 | int uniquePathsWithObstacles(vector>& obstacleGrid) { 16 | int n = obstacleGrid.size(), m = obstacleGrid[0].size(); 17 | 18 | if(obstacleGrid[n-1][m-1]==1) return 0; 19 | 20 | vector> dp(n, vector (m,0)); 21 | 22 | for(int i=0;i= 0|| q >= 0 ) { 10 | int sum = 0; 11 | 12 | if (p >= 0) 13 | sum += a[p--]-'0'; 14 | 15 | if (q >= 0) 16 | sum += b[q--]-'0'; 17 | 18 | sum += carry; 19 | carry = sum / 2; 20 | sum = sum % 2; 21 | 22 | out = to_string(sum) + out ; 23 | } 24 | 25 | if (carry) 26 | out = to_string(carry) + out; 27 | 28 | return out; 29 | } 30 | }; -------------------------------------------------------------------------------- /0067-add-binary/README.md: -------------------------------------------------------------------------------- 1 |

67. Add Binary

Easy


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

2 | 3 |

 

4 |

Example 1:

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

Example 2:

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

 

12 |

Constraints:

13 | 14 |
    15 |
  • 1 <= a.length, b.length <= 104
  • 16 |
  • a and b consist only of '0' or '1' characters.
  • 17 |
  • Each string does not contain leading zeros except for the zero itself.
  • 18 |
19 |
-------------------------------------------------------------------------------- /0070-climbing-stairs/0070-climbing-stairs.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int climbStairs(int n) { 4 | int first = 1, second = 2, current; 5 | 6 | if( n < 3) 7 | return n; 8 | 9 | for(int i = 3;i<=n;i++){ 10 | 11 | current = first + second; 12 | 13 | first = second; 14 | second = current; 15 | } 16 | 17 | return current; 18 | } 19 | }; -------------------------------------------------------------------------------- /0070-climbing-stairs/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0070-climbing-stairs/README.md: -------------------------------------------------------------------------------- 1 |

70. Climbing Stairs

Easy


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

2 | 3 |

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

4 | 5 |

 

6 |

Example 1:

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

Example 2:

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

 

26 |

Constraints:

27 | 28 |
    29 |
  • 1 <= n <= 45
  • 30 |
31 |
-------------------------------------------------------------------------------- /0076-minimum-window-substring/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0080-remove-duplicates-from-sorted-array-ii/0080-remove-duplicates-from-sorted-array-ii.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int removeDuplicates(vector &nums) 5 | { 6 | 7 | int j = 1; 8 | 9 | int flag = 1; 10 | 11 | for (int i = 1; i < nums.size(); i++) 12 | { 13 | 14 | if (nums[i] == nums[i - 1]) 15 | { 16 | if (flag == 1) 17 | { 18 | nums[j] = nums[i]; 19 | j++; 20 | flag++; 21 | } 22 | else if (flag == 2) 23 | continue; 24 | } 25 | else 26 | { 27 | nums[j] = nums[i]; 28 | j++; 29 | flag = 1; 30 | } 31 | } 32 | return j; 33 | } 34 | }; -------------------------------------------------------------------------------- /0080-remove-duplicates-from-sorted-array-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0093-restore-ip-addresses/0093-restore-ip-addresses.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | bool check(string s){ 3 | int n=s.size(); 4 | 5 | if(n==1){ 6 | return true; 7 | } 8 | 9 | if(n>3||s[0]=='0'){ 10 | return false; 11 | } 12 | 13 | int val=stoi(s); 14 | 15 | if(val>255){ 16 | return false; 17 | } 18 | 19 | return true; 20 | } 21 | 22 | public: 23 | 24 | vector restoreIpAddresses(string s) { 25 | int n=s.size(); 26 | vectorans; 27 | if(n>12){ 28 | return ans; 29 | } 30 | 31 | for(int i=1;i<=3;i++){ 32 | for(int j=1;j<=3;j++){ 33 | for(int k=1;k<=3;k++){ 34 | if(i+j+k=n){ 35 | 36 | string a=s.substr(0,i); 37 | string b=s.substr(i,j); 38 | string c=s.substr(j+i,k); 39 | string d=s.substr(i+j+k); 40 | 41 | if(check(a)&&check(b)&&check(c)&&check(d)){ 42 | ans.push_back(a+"."+b+"."+c+"."+d); 43 | } 44 | } 45 | } 46 | } 47 | } 48 | 49 | return ans; 50 | } 51 | }; -------------------------------------------------------------------------------- /0094-binary-tree-inorder-traversal/0094-binary-tree-inorder-traversal.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 | { 14 | vector vec; 15 | void inorderHelper(TreeNode *root) 16 | { 17 | if (root != nullptr) 18 | { 19 | inorderHelper(root->left); 20 | vec.push_back(root->val); 21 | inorderHelper(root->right); 22 | } 23 | } 24 | public: 25 | vector inorderTraversal(TreeNode *root) 26 | { 27 | inorderHelper(root); 28 | return vec; 29 | } 30 | }; -------------------------------------------------------------------------------- /0094-binary-tree-inorder-traversal/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0094-binary-tree-inorder-traversal/README.md: -------------------------------------------------------------------------------- 1 |

94. Binary Tree Inorder Traversal

Easy


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

2 | 3 |

 

4 |

Example 1:

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

Example 2:

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

Example 3:

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

 

23 |

Constraints:

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

 

31 | Follow up: Recursive solution is trivial, could you do it iteratively?
-------------------------------------------------------------------------------- /0100-same-tree/0100-same-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 | bool isSameTree(TreeNode* p, TreeNode* q) { 15 | 16 | if(p == NULL && q == NULL){ 17 | return true; 18 | } 19 | 20 | if(p == NULL || q == NULL){ 21 | return false; 22 | } 23 | 24 | if(p->val != q->val){ 25 | return false; 26 | } 27 | 28 | 29 | return(isSameTree(p->left, q->left) && isSameTree(p->right, q->right)); 30 | } 31 | }; -------------------------------------------------------------------------------- /0100-same-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0100-same-tree/README.md: -------------------------------------------------------------------------------- 1 |

100. Same Tree

Easy


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

2 | 3 |

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

4 | 5 |

 

6 |

Example 1:

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

Example 2:

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

Example 3:

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

 

25 |

Constraints:

26 | 27 |
    28 |
  • The number of nodes in both trees is in the range [0, 100].
  • 29 |
  • -104 <= Node.val <= 104
  • 30 |
31 |
-------------------------------------------------------------------------------- /0119-pascals-triangle-ii/0119-pascals-triangle-ii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | vector> result; 3 | public: 4 | vector getRow(int rowIndex) { 5 | result.push_back({1}); 6 | 7 | for(int i = 1; i <= rowIndex; i++){ 8 | vector temp(i + 1, 1); 9 | 10 | for(int j = 1; j < i; j++) 11 | temp[j] = result[i-1][j] + result[i-1][j-1]; 12 | 13 | result.push_back(temp); 14 | } 15 | 16 | return result[rowIndex]; 17 | } 18 | }; -------------------------------------------------------------------------------- /0119-pascals-triangle-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0119-pascals-triangle-ii/README.md: -------------------------------------------------------------------------------- 1 |

119. Pascal's Triangle II

Easy


Given an integer rowIndex, return the rowIndexth (0-indexed) row of the Pascal's triangle.

2 | 3 |

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

4 | 5 |

 

6 |

Example 1:

7 |
Input: rowIndex = 3
 8 | Output: [1,3,3,1]
 9 | 

Example 2:

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

Example 3:

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

 

17 |

Constraints:

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

 

24 |

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

25 |
-------------------------------------------------------------------------------- /0128-longest-consecutive-sequence/0128-longest-consecutive-sequence.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int longestConsecutive(vector &nums) 5 | { 6 | if (nums.size() == 0) 7 | return 0; 8 | 9 | sort(nums.begin(), nums.end()); 10 | 11 | int maximum = 1; 12 | int counter = 1; 13 | 14 | for (int i = 0; i < nums.size() - 1; i++) 15 | { 16 | 17 | if (nums[i] + 1 == nums[i + 1]) 18 | { 19 | counter++; 20 | } 21 | else if (nums[i] == nums[i + 1]) 22 | continue; 23 | else 24 | { 25 | maximum = max(maximum, counter); 26 | counter = 1; 27 | } 28 | } 29 | 30 | maximum = max(maximum, counter); 31 | 32 | return maximum; 33 | } 34 | }; -------------------------------------------------------------------------------- /0128-longest-consecutive-sequence/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0128-longest-consecutive-sequence/README.md: -------------------------------------------------------------------------------- 1 |

128. Longest Consecutive Sequence

Medium


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

2 | 3 |

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

4 | 5 |

 

6 |

Example 1:

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

Example 2:

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

 

20 |

Constraints:

21 | 22 |
    23 |
  • 0 <= nums.length <= 105
  • 24 |
  • -109 <= nums[i] <= 109
  • 25 |
26 |
-------------------------------------------------------------------------------- /0131-palindrome-partitioning/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0131-palindrome-partitioning/README.md: -------------------------------------------------------------------------------- 1 |

131. Palindrome Partitioning

Medium


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

2 | 3 |

 

4 |

Example 1:

5 |
Input: s = "aab"
 6 | Output: [["a","a","b"],["aa","b"]]
 7 | 

Example 2:

8 |
Input: s = "a"
 9 | Output: [["a"]]
10 | 
11 |

 

12 |

Constraints:

13 | 14 |
    15 |
  • 1 <= s.length <= 16
  • 16 |
  • s contains only lowercase English letters.
  • 17 |
18 |
-------------------------------------------------------------------------------- /0134-gas-station/0134-gas-station.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int canCompleteCircuit(vector& gas, vector& cost) { 4 | int n=gas.size(); 5 | int total_gas=0,total_cost=0; 6 | int curr_gas=0, starting_point=0; 7 | for(int i=0;i tree; 14 | 15 | void preorderTraversalHelper(TreeNode* root) { 16 | 17 | if(root!=nullptr){ 18 | tree.push_back(root->val); 19 | preorderTraversalHelper(root->left); 20 | preorderTraversalHelper(root->right); 21 | } 22 | } 23 | 24 | public: 25 | vector preorderTraversal(TreeNode* root) { 26 | 27 | preorderTraversalHelper(root); 28 | return tree; 29 | } 30 | 31 | }; -------------------------------------------------------------------------------- /0144-binary-tree-preorder-traversal/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0144-binary-tree-preorder-traversal/README.md: -------------------------------------------------------------------------------- 1 |

144. Binary Tree Preorder Traversal

Easy


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

2 | 3 |

 

4 |

Example 1:

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

Example 2:

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

Example 3:

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

 

23 |

Constraints:

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

 

31 |

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

32 |
-------------------------------------------------------------------------------- /0149-max-points-on-a-line/0149-max-points-on-a-line.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxPoints(vector>& points) { 4 | int n = points.size(); 5 | if(n<=2) 6 | return n; 7 | 8 | int maxi = 2; 9 | 10 | for(int i=0; i149. Max Points on a Line

Hard


Given an array of points where points[i] = [xi, yi] represents a point on the X-Y plane, return the maximum number of points that lie on the same straight line.

2 | 3 |

 

4 |

Example 1:

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

Example 2:

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

 

17 |

Constraints:

18 | 19 |
    20 |
  • 1 <= points.length <= 300
  • 21 |
  • points[i].length == 2
  • 22 |
  • -104 <= xi, yi <= 104
  • 23 |
  • All the points are unique.
  • 24 |
25 |
-------------------------------------------------------------------------------- /0169-majority-element/0169-majority-element.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int majorityElement(vector& nums) { 4 | unordered_map mp; 5 | 6 | for(int i : nums){ 7 | mp[i]++; 8 | 9 | if (mp[i] > nums.size()/2) 10 | return i; 11 | } 12 | 13 | return 0; 14 | } 15 | }; -------------------------------------------------------------------------------- /0169-majority-element/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0169-majority-element/README.md: -------------------------------------------------------------------------------- 1 |

169. Majority Element

Easy


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

2 | 3 |

The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array.

4 | 5 |

 

6 |

Example 1:

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

Example 2:

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

 

14 |

Constraints:

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

 

23 | Follow-up: Could you solve the problem in linear time and in O(1) space?
-------------------------------------------------------------------------------- /0209-minimum-size-subarray-sum/0209-minimum-size-subarray-sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minSubArrayLen(int target, vector& nums) { 4 | int minimum = INT_MAX, sum =0; 5 | int i =0, j = 0; 6 | 7 | while(j < nums.size()){ 8 | 9 | sum += nums[j++]; 10 | 11 | while (sum >= target){ 12 | minimum = min(j - i, minimum); 13 | sum -= nums[i]; 14 | i++; 15 | } 16 | } 17 | 18 | return minimum == INT_MAX ? 0 : minimum ; 19 | } 20 | }; -------------------------------------------------------------------------------- /0209-minimum-size-subarray-sum/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0219-contains-duplicate-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0219-contains-duplicate-ii/README.md: -------------------------------------------------------------------------------- 1 |

219. Contains Duplicate II

Easy


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

2 | 3 |

 

4 |

Example 1:

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

Example 2:

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

Example 3:

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

 

23 |

Constraints:

24 | 25 |
    26 |
  • 1 <= nums.length <= 105
  • 27 |
  • -109 <= nums[i] <= 109
  • 28 |
  • 0 <= k <= 105
  • 29 |
30 |
-------------------------------------------------------------------------------- /0229-majority-element-ii/0229-majority-element-ii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector majorityElement(vector& nums) { 4 | map mp; 5 | vector v; 6 | int n = nums.size(); 7 | 8 | for(int i : nums) 9 | mp[i]++; 10 | 11 | for(auto it : mp){ 12 | 13 | if (mp[it.first] > n / 3) 14 | v.push_back(it.first); 15 | } 16 | return v; 17 | } 18 | }; -------------------------------------------------------------------------------- /0229-majority-element-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0229-majority-element-ii/README.md: -------------------------------------------------------------------------------- 1 |

229. Majority Element II

Medium


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

2 | 3 |

 

4 |

Example 1:

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

Example 2:

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

Example 3:

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

 

23 |

Constraints:

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

 

31 |

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

32 |
-------------------------------------------------------------------------------- /0232-implement-queue-using-stacks/0232-implement-queue-using-stacks.cpp: -------------------------------------------------------------------------------- 1 | class MyQueue { 2 | stack stk1; 3 | stack temp; 4 | 5 | public: 6 | MyQueue() { 7 | 8 | } 9 | 10 | void push(int x) { 11 | stk1.push(x); 12 | } 13 | 14 | int pop() { 15 | 16 | while(!stk1.empty()){ 17 | 18 | temp.push(stk1.top()); 19 | stk1.pop(); 20 | } 21 | 22 | int num = temp.top(); 23 | temp.pop(); 24 | 25 | while(!temp.empty()){ 26 | stk1.push(temp.top()); 27 | temp.pop(); 28 | } 29 | 30 | return num; 31 | } 32 | 33 | int peek() { 34 | 35 | while(!stk1.empty()){ 36 | 37 | temp.push(stk1.top()); 38 | stk1.pop(); 39 | } 40 | 41 | int num = temp.top(); 42 | 43 | while(!temp.empty()){ 44 | stk1.push(temp.top()); 45 | temp.pop(); 46 | } 47 | 48 | return num; 49 | } 50 | 51 | bool empty() { 52 | 53 | return stk1.empty(); 54 | } 55 | }; 56 | 57 | /** 58 | * Your MyQueue object will be instantiated and called as such: 59 | * MyQueue* obj = new MyQueue(); 60 | * obj->push(x); 61 | * int param_2 = obj->pop(); 62 | * int param_3 = obj->peek(); 63 | * bool param_4 = obj->empty(); 64 | */ -------------------------------------------------------------------------------- /0232-implement-queue-using-stacks/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0237-delete-node-in-a-linked-list/0237-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 | }; -------------------------------------------------------------------------------- /0237-delete-node-in-a-linked-list/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0242-valid-anagram/0242-valid-anagram.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isAnagram(string s, string t) { 4 | if (s.size() != t.size()) 5 | return false; 6 | 7 | sort(s.begin(),s.end()); 8 | sort(t.begin(),t.end()); 9 | 10 | int i =0; 11 | 12 | for(char ch : s) 13 | { 14 | if (ch != t[i]) 15 | return false; 16 | 17 | i++; 18 | } 19 | 20 | 21 | return true; 22 | } 23 | }; -------------------------------------------------------------------------------- /0242-valid-anagram/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0242-valid-anagram/README.md: -------------------------------------------------------------------------------- 1 |

242. Valid Anagram

Easy


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

2 | 3 |

An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

4 | 5 |

 

6 |

Example 1:

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

Example 2:

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

 

14 |

Constraints:

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

 

22 |

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

23 |
-------------------------------------------------------------------------------- /0290-word-pattern/0290-word-pattern.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool wordPattern(string pattern, string s) { 4 | s+=' '; 5 | int c=0; 6 | 7 | int p=pattern.size(); 8 | int n=s.size(); 9 | string str=""; 10 | vectorres; 11 | 12 | for(int i=0;i290. Word Pattern

Easy


Given a pattern and a string s, find if s follows the same pattern.

2 | 3 |

Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in s.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: pattern = "abba", s = "dog cat cat dog"
 9 | Output: true
10 | 
11 | 12 |

Example 2:

13 | 14 |
Input: pattern = "abba", s = "dog cat cat fish"
15 | Output: false
16 | 
17 | 18 |

Example 3:

19 | 20 |
Input: pattern = "aaaa", s = "dog cat cat dog"
21 | Output: false
22 | 
23 | 24 |

 

25 |

Constraints:

26 | 27 |
    28 |
  • 1 <= pattern.length <= 300
  • 29 |
  • pattern contains only lower-case English letters.
  • 30 |
  • 1 <= s.length <= 3000
  • 31 |
  • s contains only lowercase English letters and spaces ' '.
  • 32 |
  • s does not contain any leading or trailing spaces.
  • 33 |
  • All the words in s are separated by a single space.
  • 34 |
35 |
-------------------------------------------------------------------------------- /0300-longest-increasing-subsequence/0300-longest-increasing-subsequence.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int lengthOfLIS(vector& nums) { 4 | 5 | int ans = 0; 6 | 7 | int n =nums.size(); 8 | 9 | vector dp(n, 1); 10 | 11 | for(int i = 0; i < n; i++) { 12 | 13 | for(int j = i + 1; j < n; j++) { 14 | 15 | if(nums[j] > nums[i]) 16 | dp[j] = max(dp[j], dp[i]+1); 17 | } 18 | } 19 | 20 | for(int x : dp) 21 | ans = max(ans, x); 22 | 23 | return ans; 24 | } 25 | }; -------------------------------------------------------------------------------- /0300-longest-increasing-subsequence/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0300-longest-increasing-subsequence/README.md: -------------------------------------------------------------------------------- 1 |

300. Longest Increasing Subsequence

Medium


Given an integer array nums, return the length of the longest strictly increasing subsequence.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
Input: nums = [10,9,2,5,3,7,101,18]
 7 | Output: 4
 8 | Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4.
 9 | 
10 | 11 |

Example 2:

12 | 13 |
Input: nums = [0,1,0,3,2,3]
14 | Output: 4
15 | 
16 | 17 |

Example 3:

18 | 19 |
Input: nums = [7,7,7,7,7,7,7]
20 | Output: 1
21 | 
22 | 23 |

 

24 |

Constraints:

25 | 26 |
    27 |
  • 1 <= nums.length <= 2500
  • 28 |
  • -104 <= nums[i] <= 104
  • 29 |
30 | 31 |

 

32 |

Follow up: Can you come up with an algorithm that runs in O(n log(n)) time complexity?

33 |
-------------------------------------------------------------------------------- /0322-coin-change/0322-coin-change.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int coinChange(vector& coins, int amount) { 4 | vectordp(amount+1, amount+1); 5 | 6 | dp[0]=0; 7 | 8 | for(int i=1; i<=amount; i++){ 9 | 10 | for(int j=0; jamount) 19 | return -1; 20 | 21 | return dp[amount]; 22 | } 23 | }; -------------------------------------------------------------------------------- /0322-coin-change/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0322-coin-change/README.md: -------------------------------------------------------------------------------- 1 |

322. Coin Change

Medium


You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.

2 | 3 |

Return the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.

4 | 5 |

You may assume that you have an infinite number of each kind of coin.

6 | 7 |

 

8 |

Example 1:

9 | 10 |
Input: coins = [1,2,5], amount = 11
11 | Output: 3
12 | Explanation: 11 = 5 + 5 + 1
13 | 
14 | 15 |

Example 2:

16 | 17 |
Input: coins = [2], amount = 3
18 | Output: -1
19 | 
20 | 21 |

Example 3:

22 | 23 |
Input: coins = [1], amount = 0
24 | Output: 0
25 | 
26 | 27 |

 

28 |

Constraints:

29 | 30 |
    31 |
  • 1 <= coins.length <= 12
  • 32 |
  • 1 <= coins[i] <= 231 - 1
  • 33 |
  • 0 <= amount <= 104
  • 34 |
35 |
-------------------------------------------------------------------------------- /0329-longest-increasing-path-in-a-matrix/0329-longest-increasing-path-in-a-matrix.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | public: 4 | int n,m,ans = 0; 5 | using vvi = vector>; 6 | 7 | vector dx{0,0,1,-1}, dy{1,-1,0,0}; 8 | 9 | int dfs(int i,int j,vvi& matrix,vvi& dp){ 10 | 11 | if(dp[i][j]!=1) 12 | return dp[i][j]; 13 | int mx = 0; 14 | 15 | for(int k = 0;k<4;k++){ 16 | 17 | int x = i + dx[k]; 18 | int y = j + dy[k]; 19 | 20 | if(x<0 || y<0 || y>=m || x>=n) 21 | continue; 22 | 23 | if(matrix[x][y]>& matrix) { 31 | n = matrix.size(),m = matrix[0].size(); 32 | vvi dp(n,vector(m,1)); 33 | 34 | for(int i = 0;i342. Power of Four

Easy


Given an integer n, return true if it is a power of four. Otherwise, return false.

2 | 3 |

An integer n is a power of four, if there exists an integer x such that n == 4x.

4 | 5 |

 

6 |

Example 1:

7 |
Input: n = 16
 8 | Output: true
 9 | 

Example 2:

10 |
Input: n = 5
11 | Output: false
12 | 

Example 3:

13 |
Input: n = 1
14 | Output: true
15 | 
16 |

 

17 |

Constraints:

18 | 19 |
    20 |
  • -231 <= n <= 231 - 1
  • 21 |
22 | 23 |

 

24 | Follow up: Could you solve it without loops/recursion?
-------------------------------------------------------------------------------- /0343-integer-break/0343-integer-break.cpp: -------------------------------------------------------------------------------- 1 | static const auto fast = []() 2 | { 3 | std::ios_base::sync_with_stdio(false); 4 | std::cin.tie(0); 5 | std::cout.tie(0); 6 | return 0; 7 | } (); 8 | 9 | class Solution { 10 | public: 11 | 12 | int solution(int n, int dp[]) { 13 | if (n == 0) 14 | return 0; 15 | else if (dp[n] != 0) 16 | return dp[n]; 17 | 18 | int ans = 0; 19 | 20 | for (int i = 1; i <= n / 2; ++i) { 21 | ans = max(ans, max(i, solution(i, dp)) * max(n - i, solution(n - i, dp))); 22 | } 23 | 24 | dp[n] = ans; 25 | 26 | return dp[n]; 27 | } 28 | 29 | int integerBreak(int n) { 30 | int dp[60]{ 0 }; 31 | dp[0] = 0; 32 | dp[1] = 1; 33 | dp[2] = 1; 34 | 35 | return solution(n, dp); 36 | } 37 | }; -------------------------------------------------------------------------------- /0343-integer-break/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0343-integer-break/README.md: -------------------------------------------------------------------------------- 1 |

343. Integer Break

Medium


Given an integer n, break it into the sum of k positive integers, where k >= 2, and maximize the product of those integers.

2 | 3 |

Return the maximum product you can get.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: n = 2
 9 | Output: 1
10 | Explanation: 2 = 1 + 1, 1 × 1 = 1.
11 | 
12 | 13 |

Example 2:

14 | 15 |
Input: n = 10
16 | Output: 36
17 | Explanation: 10 = 3 + 3 + 4, 3 × 3 × 4 = 36.
18 | 
19 | 20 |

 

21 |

Constraints:

22 | 23 |
    24 |
  • 2 <= n <= 58
  • 25 |
26 |
-------------------------------------------------------------------------------- /0347-top-k-frequent-elements/0347-top-k-frequent-elements.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector topKFrequent(vector& nums, int k) { 4 | 5 | priority_queue> pq; 6 | 7 | unordered_map mp; 8 | 9 | for(int i : nums){ 10 | mp[i]++; 11 | } 12 | 13 | for(auto it : mp){ 14 | 15 | pq.push({it.second, it.first}); 16 | } 17 | 18 | vector res; 19 | 20 | while(k--) { 21 | res.push_back(pq.top().second); 22 | pq.pop(); 23 | } 24 | 25 | return res; 26 | } 27 | }; -------------------------------------------------------------------------------- /0347-top-k-frequent-elements/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0347-top-k-frequent-elements/README.md: -------------------------------------------------------------------------------- 1 |

347. Top K Frequent Elements

Medium


Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order.

2 | 3 |

 

4 |

Example 1:

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

Example 2:

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

 

12 |

Constraints:

13 | 14 |
    15 |
  • 1 <= nums.length <= 105
  • 16 |
  • -104 <= nums[i] <= 104
  • 17 |
  • k is in the range [1, the number of unique elements in the array].
  • 18 |
  • It is guaranteed that the answer is unique.
  • 19 |
20 | 21 |

 

22 |

Follow up: Your algorithm's time complexity must be better than O(n log n), where n is the array's size.

23 |
-------------------------------------------------------------------------------- /0352-data-stream-as-disjoint-intervals/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0380-insert-delete-getrandom-o1/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0389-find-the-difference/0389-find-the-difference.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | char findTheDifference(string s, string t) { 4 | map mp; 5 | 6 | for(char ch : s) 7 | mp[ch]++; 8 | 9 | for(char ch : t){ 10 | 11 | if (mp[ch]) 12 | mp[ch]--; 13 | else 14 | return ch; 15 | } 16 | 17 | return 'a'; 18 | } 19 | }; -------------------------------------------------------------------------------- /0389-find-the-difference/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0389-find-the-difference/README.md: -------------------------------------------------------------------------------- 1 |

389. Find the Difference

Easy


You are given two strings s and t.

2 | 3 |

String t is generated by random shuffling string s and then add one more letter at a random position.

4 | 5 |

Return the letter that was added to t.

6 | 7 |

 

8 |

Example 1:

9 | 10 |
Input: s = "abcd", t = "abcde"
11 | Output: "e"
12 | Explanation: 'e' is the letter that was added.
13 | 
14 | 15 |

Example 2:

16 | 17 |
Input: s = "", t = "y"
18 | Output: "y"
19 | 
20 | 21 |

 

22 |

Constraints:

23 | 24 |
    25 |
  • 0 <= s.length <= 1000
  • 26 |
  • t.length == s.length + 1
  • 27 |
  • s and t consist of lowercase English letters.
  • 28 |
29 |
-------------------------------------------------------------------------------- /0392-is-subsequence/0392-is-subsequence.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isSubsequence(string s, string t) { 4 | int c = 0; 5 | for(int i =0,j=0;i>& p) { 5 | 6 | sort(p.begin(), p.end()); 7 | 8 | int lastpoint = p[0][1]; 9 | 10 | int ans = 1; 11 | 12 | for(auto point : p) { 13 | if(point[0] > lastpoint) { 14 | ans++; 15 | lastpoint = point[1]; 16 | } 17 | 18 | lastpoint = min(point[1],lastpoint); 19 | } 20 | 21 | return ans; 22 | } 23 | }; -------------------------------------------------------------------------------- /0452-minimum-number-of-arrows-to-burst-balloons/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0460-lfu-cache/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0472-concatenated-words/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0491-non-decreasing-subsequences/0491-non-decreasing-subsequences.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | void func(int idx,vector&nums,vector&a,set>&ans){ 3 | int n=nums.size(); 4 | 5 | if(idx>=n){ 6 | if(a.size()>=2){ 7 | ans.insert(a); 8 | } 9 | return ; 10 | } 11 | if(!a.size()||nums[idx]>=a.back()){ 12 | a.push_back(nums[idx]); 13 | func(idx+1,nums,a,ans); 14 | a.pop_back(); 15 | } 16 | func(idx+1,nums,a,ans); 17 | } 18 | public: 19 | 20 | vector> findSubsequences(vector& nums) { 21 | vectortemp; 22 | set>ans; 23 | func(0,nums,temp,ans); 24 | return vector(ans.begin(),ans.end()); 25 | } 26 | }; -------------------------------------------------------------------------------- /0491-non-decreasing-subsequences/README.md: -------------------------------------------------------------------------------- 1 |

491. Non-decreasing Subsequences

Medium


Given an integer array nums, return all the different possible non-decreasing subsequences of the given array with at least two elements. You may return the answer in any order.

2 | 3 |

 

4 |

Example 1:

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

Example 2:

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

 

17 |

Constraints:

18 | 19 |
    20 |
  • 1 <= nums.length <= 15
  • 21 |
  • -100 <= nums[i] <= 100
  • 22 |
23 |
-------------------------------------------------------------------------------- /0501-find-mode-in-binary-search-tree/0501-find-mode-in-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() : 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 | vector findMode(TreeNode* root) { 15 | if (root == NULL) 16 | return {}; 17 | 18 | unordered_map times; 19 | 20 | int max = INT_MIN; 21 | 22 | vector answer; 23 | 24 | queue q; 25 | 26 | q.push(root); 27 | 28 | while (!q.empty()) 29 | { 30 | TreeNode* curr = q.front(); 31 | times[curr->val]++; 32 | 33 | max = (times[curr->val] > max) ? times[curr->val] : max; 34 | q.pop(); 35 | 36 | if(curr->left) 37 | q.push(curr->left); 38 | 39 | if(curr->right) 40 | q.push(curr->right); 41 | } 42 | 43 | for(auto num : times) 44 | if(num.second == max) 45 | answer.push_back(num.first); 46 | 47 | return answer; 48 | } 49 | }; -------------------------------------------------------------------------------- /0501-find-mode-in-binary-search-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0515-find-largest-value-in-each-tree-row/0515-find-largest-value-in-each-tree-row.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 | vector largestValues(TreeNode* root) { 15 | if (!root) 16 | return {}; 17 | 18 | vector result; 19 | queue queue; 20 | queue.push(root); 21 | 22 | while (!queue.empty()) { 23 | int curr_level_size = queue.size(); 24 | int max_val = INT_MIN; 25 | 26 | for (int i = 0; i < curr_level_size; ++i) { 27 | 28 | TreeNode* node = queue.front(); 29 | 30 | queue.pop(); 31 | 32 | max_val = max(max_val, node->val); 33 | 34 | if (node->left) 35 | queue.push(node->left); 36 | 37 | if (node->right) 38 | queue.push(node->right); 39 | } 40 | 41 | result.push_back(max_val); 42 | } 43 | 44 | return result; 45 | } 46 | }; -------------------------------------------------------------------------------- /0515-find-largest-value-in-each-tree-row/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0515-find-largest-value-in-each-tree-row/README.md: -------------------------------------------------------------------------------- 1 |

515. Find Largest Value in Each Tree Row

Medium


Given the root of a binary tree, return an array of the largest value in each row of the tree (0-indexed).

2 | 3 |

 

4 |

Example 1:

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

Example 2:

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

 

17 |

Constraints:

18 | 19 |
    20 |
  • The number of nodes in the tree will be in the range [0, 104].
  • 21 |
  • -231 <= Node.val <= 231 - 1
  • 22 |
23 |
-------------------------------------------------------------------------------- /0520-detect-capital/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0520-detect-capital/README.md: -------------------------------------------------------------------------------- 1 |

520. Detect Capital

Easy


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

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

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

10 | 11 |

 

12 |

Example 1:

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

Example 2:

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

 

20 |

Constraints:

21 | 22 |
    23 |
  • 1 <= word.length <= 100
  • 24 |
  • word consists of lowercase and uppercase English letters.
  • 25 |
26 |
-------------------------------------------------------------------------------- /0523-continuous-subarray-sum/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0557-reverse-words-in-a-string-iii/0557-reverse-words-in-a-string-iii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string reverseWords(string s) { 4 | int i =0; 5 | string temp, str; 6 | while(i < s.size()){ 7 | 8 | if (s[i] == ' '){ 9 | reverse(temp.begin(),temp.end()); 10 | str = str + temp + ' '; 11 | temp= ""; 12 | i++; 13 | } 14 | 15 | temp += s[i]; 16 | 17 | if (i == s.size() - 1){ 18 | reverse(temp.begin(),temp.end()); 19 | str = str + temp; 20 | } 21 | 22 | i++; 23 | } 24 | 25 | return str; 26 | } 27 | }; -------------------------------------------------------------------------------- /0557-reverse-words-in-a-string-iii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0557-reverse-words-in-a-string-iii/README.md: -------------------------------------------------------------------------------- 1 |

557. Reverse Words in a String III

Easy


Given a string s, reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.

2 | 3 |

 

4 |

Example 1:

5 |
Input: s = "Let's take LeetCode contest"
 6 | Output: "s'teL ekat edoCteeL tsetnoc"
 7 | 

Example 2:

8 |
Input: s = "God Ding"
 9 | Output: "doG gniD"
10 | 
11 |

 

12 |

Constraints:

13 | 14 |
    15 |
  • 1 <= s.length <= 5 * 104
  • 16 |
  • s contains printable ASCII characters.
  • 17 |
  • s does not contain any leading or trailing spaces.
  • 18 |
  • There is at least one word in s.
  • 19 |
  • All the words in s are separated by a single space.
  • 20 |
21 |
-------------------------------------------------------------------------------- /0606-construct-string-from-binary-tree/0606-construct-string-from-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 | { 14 | void DFS(TreeNode *root, string &str) 15 | { 16 | 17 | if (root == nullptr) 18 | return; 19 | 20 | if (root != nullptr) 21 | { 22 | str += to_string(root->val); 23 | 24 | if (root->left || root->right) 25 | { 26 | str += '('; 27 | DFS(root->left, str); 28 | str += ')'; 29 | } 30 | 31 | if (root->right) 32 | { 33 | str += '('; 34 | DFS(root->right, str); 35 | str += ')'; 36 | } 37 | } 38 | } 39 | 40 | public: 41 | string tree2str(TreeNode *root) 42 | { 43 | string str; 44 | DFS(root, str); 45 | 46 | return str; 47 | } 48 | }; -------------------------------------------------------------------------------- /0606-construct-string-from-binary-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0617-merge-two-binary-trees/0617-merge-two-binary-trees.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* mergeTrees(TreeNode* root1, TreeNode* root2) { 15 | if (root1 == nullptr) 16 | return root2; 17 | 18 | if (root2 == nullptr) 19 | return root1; 20 | 21 | root1->val += root2->val; 22 | root1->left = mergeTrees(root1->left,root2->left); 23 | root1->right = mergeTrees(root1->right,root2->right); 24 | 25 | return root1; 26 | 27 | } 28 | }; -------------------------------------------------------------------------------- /0617-merge-two-binary-trees/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0617-merge-two-binary-trees/README.md: -------------------------------------------------------------------------------- 1 |

617. Merge Two Binary Trees

Easy


You are given two binary trees root1 and root2.

2 | 3 |

Imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. You need to merge the two trees into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of the new tree.

4 | 5 |

Return the merged tree.

6 | 7 |

Note: The merging process must start from the root nodes of both trees.

8 | 9 |

 

10 |

Example 1:

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

Example 2:

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

 

23 |

Constraints:

24 | 25 |
    26 |
  • The number of nodes in both trees is in the range [0, 2000].
  • 27 |
  • -104 <= Node.val <= 104
  • 28 |
29 |
-------------------------------------------------------------------------------- /0629-k-inverse-pairs-array/0629-k-inverse-pairs-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | int mod = 1e9 + 7, N = 1010; 3 | int f[1010][1010]; 4 | 5 | public: 6 | int kInversePairs(int n, int k) { 7 | 8 | f[0][0] = 1; 9 | 10 | for (int i = 1; i <= n; ++ i) // 1000 11 | { 12 | long long s = 0; // maintain a window that is length min(i, j); 13 | 14 | for (int j = 0; j <= k; ++ j) // 1000 15 | { 16 | s += f[i - 1][j]; 17 | 18 | if (j >= i) 19 | s -= f[i - 1][j - i]; 20 | 21 | f[i][j] = ((long long)f[i][j] + s) % mod; 22 | } 23 | } 24 | 25 | return f[n][k]; 26 | } 27 | }; -------------------------------------------------------------------------------- /0629-k-inverse-pairs-array/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0629-k-inverse-pairs-array/README.md: -------------------------------------------------------------------------------- 1 |

629. K Inverse Pairs Array

Hard


For an integer array nums, an inverse pair is a pair of integers [i, j] where 0 <= i < j < nums.length and nums[i] > nums[j].

2 | 3 |

Given two integers n and k, return the number of different arrays consist of numbers from 1 to n such that there are exactly k inverse pairs. Since the answer can be huge, return it modulo 109 + 7.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: n = 3, k = 0
 9 | Output: 1
10 | Explanation: Only the array [1,2,3] which consists of numbers from 1 to 3 has exactly 0 inverse pairs.
11 | 
12 | 13 |

Example 2:

14 | 15 |
Input: n = 3, k = 1
16 | Output: 2
17 | Explanation: The array [1,3,2] and [2,1,3] have exactly 1 inverse pair.
18 | 
19 | 20 |

 

21 |

Constraints:

22 | 23 |
    24 |
  • 1 <= n <= 1000
  • 25 |
  • 0 <= k <= 1000
  • 26 |
27 |
-------------------------------------------------------------------------------- /0645-set-mismatch/0645-set-mismatch.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector findErrorNums(vector& nums) { 4 | 5 | int n = nums.size(); 6 | 7 | vector arr(n + 1, 0); 8 | 9 | vector vec; 10 | 11 | for (auto i: nums) 12 | { 13 | arr[i]++; 14 | 15 | if (arr[i] > 1) 16 | vec.push_back(i); 17 | } 18 | 19 | for (int i = 1; i < n + 1; i++) 20 | { 21 | if (arr[i] == 0) 22 | vec.push_back(i); 23 | } 24 | 25 | return vec; 26 | } 27 | }; -------------------------------------------------------------------------------- /0645-set-mismatch/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0645-set-mismatch/README.md: -------------------------------------------------------------------------------- 1 |

645. Set Mismatch

Easy


You have a set of integers s, which originally contains all the numbers from 1 to n. Unfortunately, due to some error, one of the numbers in s got duplicated to another number in the set, which results in repetition of one number and loss of another number.

2 | 3 |

You are given an integer array nums representing the data status of this set after the error.

4 | 5 |

Find the number that occurs twice and the number that is missing and return them in the form of an array.

6 | 7 |

 

8 |

Example 1:

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

Example 2:

12 |
Input: nums = [1,1]
13 | Output: [1,2]
14 | 
15 |

 

16 |

Constraints:

17 | 18 |
    19 |
  • 2 <= nums.length <= 104
  • 20 |
  • 1 <= nums[i] <= 104
  • 21 |
22 |
-------------------------------------------------------------------------------- /0647-palindromic-substrings/0647-palindromic-substrings.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | int counter = 0; 4 | 5 | void isValidPalindrom(string str, int left, int right) 6 | { 7 | 8 | while (left >= 0 && right < str.size()) 9 | { 10 | if (str[left] != str[right]) 11 | break; 12 | else 13 | counter++; 14 | 15 | left--; 16 | right++; 17 | } 18 | } 19 | 20 | public: 21 | int countSubstrings(string s) 22 | { 23 | counter = s.size(); 24 | 25 | for (int i = 0; i < s.size(); i++) 26 | { 27 | 28 | isValidPalindrom(s, i, i + 1); // find palindromes of Even string length 29 | 30 | isValidPalindrom(s, i - 1, i + 1); // find palindromes of Odd string length 31 | } 32 | 33 | return counter; 34 | } 35 | }; -------------------------------------------------------------------------------- /0647-palindromic-substrings/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0647-palindromic-substrings/README.md: -------------------------------------------------------------------------------- 1 |

647. Palindromic Substrings

Medium


Given a string s, return the number of palindromic substrings in it.

2 | 3 |

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

4 | 5 |

A substring is a contiguous sequence of characters within the string.

6 | 7 |

 

8 |

Example 1:

9 | 10 |
Input: s = "abc"
11 | Output: 3
12 | Explanation: Three palindromic strings: "a", "b", "c".
13 | 
14 | 15 |

Example 2:

16 | 17 |
Input: s = "aaa"
18 | Output: 6
19 | Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa".
20 | 
21 | 22 |

 

23 |

Constraints:

24 | 25 |
    26 |
  • 1 <= s.length <= 1000
  • 27 |
  • s consists of lowercase English letters.
  • 28 |
29 |
-------------------------------------------------------------------------------- /0692-top-k-frequent-words/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0739-daily-temperatures/0739-daily-temperatures.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | vector dailyTemperatures(vector &temperatures) 5 | { 6 | int size = temperatures.size(); 7 | 8 | vector ans(size, 0); 9 | 10 | stack> stk; 11 | 12 | int j = 0; 13 | 14 | for (int i = 0; i < size; i++) 15 | { 16 | while (!stk.empty() && temperatures[i] > stk.top().first) 17 | { 18 | j = stk.top().second; 19 | ans[j] = i - j; 20 | stk.pop(); 21 | } 22 | 23 | stk.push({ temperatures[i], i }); 24 | } 25 | 26 | return ans; 27 | } 28 | }; -------------------------------------------------------------------------------- /0739-daily-temperatures/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0739-daily-temperatures/README.md: -------------------------------------------------------------------------------- 1 |

739. Daily Temperatures

Medium


Given an array of integers temperatures represents the daily temperatures, return an array answer such that answer[i] is the number of days you have to wait after the ith day to get a warmer temperature. If there is no future day for which this is possible, keep answer[i] == 0 instead.

2 | 3 |

 

4 |

Example 1:

5 |
Input: temperatures = [73,74,75,71,69,72,76,73]
 6 | Output: [1,1,4,2,1,1,0,0]
 7 | 

Example 2:

8 |
Input: temperatures = [30,40,50,60]
 9 | Output: [1,1,1,0]
10 | 

Example 3:

11 |
Input: temperatures = [30,60,90]
12 | Output: [1,1,0]
13 | 
14 |

 

15 |

Constraints:

16 | 17 |
    18 |
  • 1 <= temperatures.length <= 105
  • 19 |
  • 30 <= temperatures[i] <= 100
  • 20 |
21 |
-------------------------------------------------------------------------------- /0746-min-cost-climbing-stairs/0746-min-cost-climbing-stairs.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minCostClimbingStairs(vector& cost) { 4 | int dp[2] = {cost[0],cost[1]}; 5 | 6 | for (int i = 2; i < cost.size(); ++i) 7 | dp[i%2] = cost[i] + min(dp[0],dp[1]); 8 | 9 | return min(dp[0],dp[1]); 10 | } 11 | }; -------------------------------------------------------------------------------- /0746-min-cost-climbing-stairs/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0766-toeplitz-matrix/0766-toeplitz-matrix.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isToeplitzMatrix(vector>& matrix) { 4 | 5 | const int rows = matrix.size(); 6 | const int cols = matrix[0].size(); 7 | 8 | for (int r = 1; r < rows; ++r) 9 | for (int c = 1; c < cols; ++c){ 10 | 11 | if (matrix[r][c] != matrix[r - 1][c - 1]) 12 | return false; 13 | } 14 | 15 | return true; 16 | } 17 | }; -------------------------------------------------------------------------------- /0766-toeplitz-matrix/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0779-k-th-symbol-in-grammar/0779-k-th-symbol-in-grammar.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int kthGrammar(int n, int k) { 4 | 5 | if (n == 1 && k == 1) 6 | return 0; 7 | 8 | int mid = pow(2,n-1)/2; 9 | 10 | if (k <= mid) 11 | return kthGrammar(n -1,k); 12 | else 13 | return 1 - kthGrammar(n-1,k-mid); 14 | } 15 | }; -------------------------------------------------------------------------------- /0779-k-th-symbol-in-grammar/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0787-cheapest-flights-within-k-stops/0787-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 | unordered_map>> graph; 5 | for(auto e: flights) 6 | graph[e[0]].push_back({e[1], e[2]}); 7 | vector prices(n, -1); 8 | 9 | queue> q; q.push({src, 0}); 10 | ++K; 11 | 12 | while(!q.empty()) { 13 | if(!K) 14 | break; 15 | 16 | int len = q.size(); 17 | for(int i = 0; i < len; i++) { 18 | auto cur = q.front(); q.pop(); 19 | 20 | for(auto e: graph[cur.first]) { 21 | int price = cur.second + e.second; 22 | if(prices[e.first] == -1 || price < prices[e.first]) { 23 | prices[e.first] = price; 24 | q.push({e.first, price}); 25 | } 26 | } 27 | } 28 | K--; 29 | } 30 | 31 | return prices[dst]; 32 | } 33 | }; -------------------------------------------------------------------------------- /0787-cheapest-flights-within-k-stops/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0790-domino-and-tromino-tiling/0790-domino-and-tromino-tiling.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numTilings(int n) { 4 | vectordp(n+1,0); 5 | if(n==1) 6 | return 1; 7 | 8 | if(n==2) 9 | return 2; 10 | 11 | if(n==3) 12 | return 5; 13 | 14 | if(n==4) 15 | return 11; 16 | 17 | dp[1] = 1; 18 | dp[2] = 2; 19 | dp[3] = 5; 20 | dp[4] = 11; 21 | 22 | for(int i=5; i<=n; ++i) { 23 | dp[i] = (2*dp[i-1] +dp[i-3]) % 1000000007; 24 | } 25 | 26 | return dp[n]; 27 | } 28 | }; -------------------------------------------------------------------------------- /0790-domino-and-tromino-tiling/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0790-domino-and-tromino-tiling/README.md: -------------------------------------------------------------------------------- 1 |

790. Domino and Tromino Tiling

Medium


You have two types of tiles: a 2 x 1 domino shape and a tromino shape. You may rotate these shapes.

2 | 3 |

Given an integer n, return the number of ways to tile an 2 x n board. Since the answer may be very large, return it modulo 109 + 7.

4 | 5 |

In a tiling, every square must be covered by a tile. Two tilings are different if and only if there are two 4-directionally adjacent cells on the board such that exactly one of the tilings has both squares occupied by a tile.

6 | 7 |

 

8 |

Example 1:

9 | 10 |
Input: n = 3
11 | Output: 5
12 | Explanation: The five different ways are show above.
13 | 
14 | 15 |

Example 2:

16 | 17 |
Input: n = 1
18 | Output: 1
19 | 
20 | 21 |

 

22 |

Constraints:

23 | 24 |
    25 |
  • 1 <= n <= 1000
  • 26 |
27 |
-------------------------------------------------------------------------------- /0797-all-paths-from-source-to-target/0797-all-paths-from-source-to-target.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | void dfs(vector adj[] , int src , vector vis , vector temp , vector>& res , int dst){ 4 | if(src == dst){ 5 | temp.push_back(src); 6 | res.push_back(temp); 7 | return; 8 | } 9 | 10 | vis[src] = 1; 11 | temp.push_back(src); 12 | for(auto v : adj[src]){ 13 | if(vis[v] == 0){ 14 | dfs(adj , v , vis , temp , res , dst); 15 | } 16 | } 17 | } 18 | 19 | public: 20 | 21 | vector> allPathsSourceTarget(vector>& graph) { 22 | int n = graph.size(); 23 | vector adj[n]; 24 | 25 | for(int i = 0 ; i < n ; i++){ 26 | for(auto it : graph[i]){ 27 | adj[i].push_back(it); 28 | } 29 | } 30 | vector vis(n , 0); 31 | vector ans; 32 | 33 | vector> res; 34 | dfs(adj , 0 , vis , ans , res , n-1); 35 | 36 | return res; 37 | } 38 | }; -------------------------------------------------------------------------------- /0797-all-paths-from-source-to-target/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0823-binary-trees-with-factors/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0823-binary-trees-with-factors/README.md: -------------------------------------------------------------------------------- 1 |

823. Binary Trees With Factors

Medium


Given an array of unique integers, arr, where each integer arr[i] is strictly greater than 1.

2 | 3 |

We make a binary tree using these integers, and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of its children.

4 | 5 |

Return the number of binary trees we can make. The answer may be too large so return the answer modulo 109 + 7.

6 | 7 |

 

8 |

Example 1:

9 | 10 |
Input: arr = [2,4]
11 | Output: 3
12 | Explanation: We can make these trees: [2], [4], [4, 2, 2]
13 | 14 |

Example 2:

15 | 16 |
Input: arr = [2,4,5,10]
17 | Output: 7
18 | Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].
19 | 20 |

 

21 |

Constraints:

22 | 23 |
    24 |
  • 1 <= arr.length <= 1000
  • 25 |
  • 2 <= arr[i] <= 109
  • 26 |
  • All the values of arr are unique.
  • 27 |
28 |
-------------------------------------------------------------------------------- /0835-image-overlap/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0841-keys-and-rooms/0841-keys-and-rooms.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canVisitAllRooms(vector>& rooms) 4 | { 5 | deque Q({0}); 6 | 7 | unordered_set seen; 8 | 9 | while (!Q.empty()) 10 | { 11 | int r = Q.front(); Q.pop_front(); 12 | seen.insert(r); 13 | 14 | for (int k : rooms[r]) 15 | if (!seen.count(k)) 16 | Q.push_back(k); 17 | } 18 | 19 | return seen.size() == rooms.size(); 20 | } 21 | }; -------------------------------------------------------------------------------- /0844-backspace-string-compare/0844-backspace-string-compare.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | vector sc, tc; 3 | public: 4 | bool backspaceCompare(string s, string t) { 5 | 6 | for(char c: s){ 7 | if (c>='a' && c<='z') 8 | sc.push_back(c); 9 | else if (c='#'&& !sc.empty()) 10 | sc.pop_back(); 11 | } 12 | 13 | for(char c: t){ 14 | if (c>='a' && c<='z') 15 | tc.push_back(c); 16 | else if (c='#'&& !tc.empty()) 17 | tc.pop_back(); 18 | } 19 | 20 | return sc==tc; 21 | } 22 | }; -------------------------------------------------------------------------------- /0844-backspace-string-compare/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0844-backspace-string-compare/README.md: -------------------------------------------------------------------------------- 1 |

844. Backspace String Compare

Easy


Given two strings s and t, return true if they are equal when both are typed into empty text editors. '#' means a backspace character.

2 | 3 |

Note that after backspacing an empty text, the text will continue empty.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: s = "ab#c", t = "ad#c"
 9 | Output: true
10 | Explanation: Both s and t become "ac".
11 | 
12 | 13 |

Example 2:

14 | 15 |
Input: s = "ab##", t = "c#d#"
16 | Output: true
17 | Explanation: Both s and t become "".
18 | 
19 | 20 |

Example 3:

21 | 22 |
Input: s = "a#c", t = "b"
23 | Output: false
24 | Explanation: s becomes "c" while t becomes "b".
25 | 
26 | 27 |

 

28 |

Constraints:

29 | 30 |
    31 |
  • 1 <= s.length, t.length <= 200
  • 32 |
  • s and t only contain lowercase letters and '#' characters.
  • 33 |
34 | 35 |

 

36 |

Follow up: Can you solve it in O(n) time and O(1) space?

37 |
-------------------------------------------------------------------------------- /0859-buddy-strings/0859-buddy-strings.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool buddyStrings(string s, string goal) { 4 | 5 | if(s.size()!=goal.size()) 6 | return false; 7 | 8 | if(s==goal){ 9 | 10 | vector freq(26,0); 11 | 12 | for(auto& character:s){ 13 | freq[character-'a']+=1; 14 | if(freq[character-'a']==2) return true; 15 | } 16 | 17 | return false; 18 | } 19 | 20 | int index1=-1; 21 | int index2=-1; 22 | 23 | for(int i=0;i> transpose(vector> &matrix) 5 | { 6 | vector> temp(matrix[0].size(), vector (matrix.size())); 7 | 8 | for (int i = 0; i < matrix.size(); i++) 9 | { 10 | 11 | for (int j = 0; j < matrix[i].size(); j++) 12 | { 13 | temp[j][i] = matrix[i][j]; 14 | } 15 | } 16 | 17 | return temp; 18 | } 19 | }; -------------------------------------------------------------------------------- /0867-transpose-matrix/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0867-transpose-matrix/README.md: -------------------------------------------------------------------------------- 1 |

867. Transpose Matrix

Easy


Given a 2D integer array matrix, return the transpose of matrix.

2 | 3 |

The transpose of a matrix is the matrix flipped over its main diagonal, switching the matrix's row and column indices.

4 | 5 |

6 | 7 |

 

8 |

Example 1:

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

Example 2:

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

 

21 |

Constraints:

22 | 23 |
    24 |
  • m == matrix.length
  • 25 |
  • n == matrix[i].length
  • 26 |
  • 1 <= m, n <= 1000
  • 27 |
  • 1 <= m * n <= 105
  • 28 |
  • -109 <= matrix[i][j] <= 109
  • 29 |
30 |
-------------------------------------------------------------------------------- /0876-middle-of-the-linked-list/0876-middle-of-the-linked-list.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* middleNode(ListNode* head) { 14 | ListNode* temp = head; 15 | 16 | int counter=0 , i = 0 ; 17 | 18 | while(temp!= nullptr) 19 | { 20 | temp=temp->next; 21 | counter++; 22 | } 23 | 24 | temp = head; 25 | 26 | while(i< counter/2) 27 | { 28 | temp=temp->next; 29 | i++; 30 | } 31 | 32 | return temp; 33 | } 34 | }; -------------------------------------------------------------------------------- /0876-middle-of-the-linked-list/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0876-middle-of-the-linked-list/README.md: -------------------------------------------------------------------------------- 1 |

876. Middle of the Linked List

Easy


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

2 | 3 |

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

4 | 5 |

 

6 |

Example 1:

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

Example 2:

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

 

21 |

Constraints:

22 | 23 |
    24 |
  • The number of nodes in the list is in the range [1, 100].
  • 25 |
  • 1 <= Node.val <= 100
  • 26 |
27 |
-------------------------------------------------------------------------------- /0886-possible-bipartition/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0907-sum-of-subarray-minimums/0907-sum-of-subarray-minimums.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int sumSubarrayMins(vector& arr) { 4 | 5 | int n = arr.size(), mod = 1e9 + 7; 6 | 7 | stack> st; 8 | 9 | vector pse(n, -1); 10 | 11 | for (int i = 0; i < n; i++) 12 | { 13 | while (!st.empty() and st.top().first > arr[i]) 14 | st.pop(); 15 | 16 | if (!st.empty()) 17 | pse[i] = st.top().second; 18 | 19 | st.push({arr[i], i}); 20 | } 21 | 22 | while (!st.empty()) 23 | st.pop(); 24 | 25 | vector nse(n, n); 26 | 27 | for (int i = n - 1; i >= 0; i--) 28 | { 29 | while (!st.empty() and st.top().first >= arr[i]) 30 | st.pop(); 31 | 32 | if (!st.empty()) 33 | nse[i] = st.top().second; 34 | 35 | st.push({arr[i], i}); 36 | } 37 | 38 | long ans = 0; 39 | 40 | for (int i = 0; i < n; i++) 41 | { 42 | long left = i - pse[i]; 43 | long right = nse[i] - i; 44 | 45 | ans = (ans + left * right * arr[i]) % mod; 46 | } 47 | 48 | return ans; 49 | } 50 | }; -------------------------------------------------------------------------------- /0907-sum-of-subarray-minimums/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0907-sum-of-subarray-minimums/README.md: -------------------------------------------------------------------------------- 1 |

907. Sum of Subarray Minimums

Medium


Given an array of integers arr, find the sum of min(b), where b ranges over every (contiguous) subarray of arr. Since the answer may be large, return the answer modulo 109 + 7.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
Input: arr = [3,1,2,4]
 7 | Output: 17
 8 | Explanation: 
 9 | Subarrays are [3], [1], [2], [4], [3,1], [1,2], [2,4], [3,1,2], [1,2,4], [3,1,2,4]. 
10 | Minimums are 3, 1, 2, 4, 1, 1, 2, 1, 1, 1.
11 | Sum is 17.
12 | 
13 | 14 |

Example 2:

15 | 16 |
Input: arr = [11,81,94,43,3]
17 | Output: 444
18 | 
19 | 20 |

 

21 |

Constraints:

22 | 23 |
    24 |
  • 1 <= arr.length <= 3 * 104
  • 25 |
  • 1 <= arr[i] <= 3 * 104
  • 26 |
27 |
-------------------------------------------------------------------------------- /0909-snakes-and-ladders/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0931-minimum-falling-path-sum/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0938-range-sum-of-bst/0938-range-sum-of-bst.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 | 14 | public: 15 | int rangeSumBST(TreeNode* root, int low, int high) { 16 | queue q; 17 | q.push(root); 18 | 19 | long long sum = 0; 20 | 21 | while(!q.empty()){ 22 | 23 | TreeNode* front = q.front(); 24 | 25 | q.pop(); 26 | 27 | if (front->left) 28 | q.push(front->left); 29 | 30 | if (front->right) 31 | q.push(front->right); 32 | 33 | if (front->val >= low && front->val <= high) 34 | sum += front->val; 35 | } 36 | 37 | return sum; 38 | } 39 | }; -------------------------------------------------------------------------------- /0938-range-sum-of-bst/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0938-range-sum-of-bst/README.md: -------------------------------------------------------------------------------- 1 |

938. Range Sum of BST

Easy


Given the root node of a binary search tree and two integers low and high, return the sum of values of all nodes with a value in the inclusive range [low, high].

2 | 3 |

 

4 |

Example 1:

5 | 6 |
Input: root = [10,5,15,3,7,null,18], low = 7, high = 15
 7 | Output: 32
 8 | Explanation: Nodes 7, 10, and 15 are in the range [7, 15]. 7 + 10 + 15 = 32.
 9 | 
10 | 11 |

Example 2:

12 | 13 |
Input: root = [10,5,15,3,7,13,18,1,null,6], low = 6, high = 10
14 | Output: 23
15 | Explanation: Nodes 6, 7, and 10 are in the range [6, 10]. 6 + 7 + 10 = 23.
16 | 
17 | 18 |

 

19 |

Constraints:

20 | 21 |
    22 |
  • The number of nodes in the tree is in the range [1, 2 * 104].
  • 23 |
  • 1 <= Node.val <= 105
  • 24 |
  • 1 <= low <= high <= 105
  • 25 |
  • All Node.val are unique.
  • 26 |
27 |
-------------------------------------------------------------------------------- /0944-delete-columns-to-make-sorted/0944-delete-columns-to-make-sorted.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minDeletionSize(vector& strs) { 4 | int delete_count=0; 5 | int row = strs.size(); 6 | int col = strs[0].size(); 7 | 8 | for(int j=0; jstrs[i+1][j]) 13 | { 14 | delete_count++; 15 | break; 16 | } 17 | } 18 | } 19 | 20 | return delete_count; 21 | } 22 | }; -------------------------------------------------------------------------------- /0944-delete-columns-to-make-sorted/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0974-subarray-sums-divisible-by-k/0974-subarray-sums-divisible-by-k.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int subarraysDivByK(vector& nums, int k) { 4 | int n = nums.size(); 5 | 6 | vector sums(k, 0); 7 | 8 | sums[0]++; 9 | 10 | int cnt = 0; 11 | 12 | int currSum = 0; 13 | 14 | for(int i = 0; i974. Subarray Sums Divisible by K

Medium


Given an integer array nums and an integer k, return the number of non-empty subarrays that have a sum divisible by k.

2 | 3 |

A subarray is a contiguous part of an array.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: nums = [4,5,0,-2,-3,1], k = 5
 9 | Output: 7
10 | Explanation: There are 7 subarrays with a sum divisible by k = 5:
11 | [4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]
12 | 
13 | 14 |

Example 2:

15 | 16 |
Input: nums = [5], k = 9
17 | Output: 0
18 | 
19 | 20 |

 

21 |

Constraints:

22 | 23 |
    24 |
  • 1 <= nums.length <= 3 * 104
  • 25 |
  • -104 <= nums[i] <= 104
  • 26 |
  • 2 <= k <= 104
  • 27 |
28 |
-------------------------------------------------------------------------------- /0976-largest-perimeter-triangle/0976-largest-perimeter-triangle.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int largestPerimeter(vector& nums) { 4 | 5 | sort(nums.begin(),nums.end()); 6 | 7 | int max = 0; 8 | 9 | for(int i = 0; i < nums.size()-2;i++) 10 | { 11 | if (nums[i]+nums[i+1] > nums[i+2]) 12 | max = nums[i]+nums[i+1] + nums[i+2]; 13 | } 14 | 15 | return max; 16 | } 17 | }; -------------------------------------------------------------------------------- /0976-largest-perimeter-triangle/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0976-largest-perimeter-triangle/README.md: -------------------------------------------------------------------------------- 1 |

976. Largest Perimeter Triangle

Easy


Given an integer array nums, return the largest perimeter of a triangle with a non-zero area, formed from three of these lengths. If it is impossible to form any triangle of a non-zero area, return 0.

2 | 3 |

 

4 |

Example 1:

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

Example 2:

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

 

17 |

Constraints:

18 | 19 |
    20 |
  • 3 <= nums.length <= 104
  • 21 |
  • 1 <= nums[i] <= 106
  • 22 |
23 |
-------------------------------------------------------------------------------- /0980-unique-paths-iii/0980-unique-paths-iii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | vector> grids; 3 | int n, m, ans, zero; 4 | 5 | void dfs(int x, int y) { 6 | 7 | if(x < 0 or x >= n or y < 0 or y >= m or grids[x][y] == -1) { 8 | return; 9 | } 10 | 11 | if(grids[x][y] == 2) { 12 | 13 | if(zero == -1) 14 | ans++; 15 | 16 | return; 17 | } 18 | 19 | grids[x][y] = -1; 20 | zero--; 21 | 22 | dfs(x-1, y); 23 | 24 | dfs(x+1, y); 25 | 26 | dfs(x, y-1); 27 | 28 | dfs(x, y+1); 29 | 30 | zero++; 31 | 32 | grids[x][y] = 0; 33 | } 34 | 35 | public: 36 | 37 | 38 | int uniquePathsIII(vector>& grid) { 39 | grids = grid; 40 | 41 | n = grid.size(), m = grid[0].size(); 42 | 43 | int sx, sy;; 44 | 45 | for(int i=0; i>& trust) { 4 | 5 | vector trusting(n+1,0); 6 | vector trusted(n+1,0); 7 | 8 | for(int i=0;ival); 22 | 23 | minmum = min(minmum, root->val); 24 | 25 | result = max(result, abs(maximum - minmum)); 26 | 27 | preOrder(root->left, maximum , minmum); 28 | 29 | preOrder(root->right , maximum , minmum); 30 | } 31 | } 32 | 33 | public: 34 | int maxAncestorDiff(TreeNode* root) { 35 | preOrder(root); 36 | return result; 37 | } 38 | }; -------------------------------------------------------------------------------- /1026-maximum-difference-between-node-and-ancestor/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1061-lexicographically-smallest-equivalent-string/1061-lexicographically-smallest-equivalent-string.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string smallestEquivalentString(string s1, string s2, string baseStr) { 4 | char ch[26]; 5 | 6 | for (int i=0; i<26; i++) 7 | ch[i] = 'a' + i; 8 | 9 | for (int i=0; ival==targetSum && root->left ==nullptr && root->right==nullptr) 19 | return true; 20 | else 21 | return hasPathSum(root->left,targetSum - root->val) || hasPathSum(root->right, targetSum - root->val); 22 | } 23 | }; -------------------------------------------------------------------------------- /112-path-sum/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1143-longest-common-subsequence/1143-longest-common-subsequence.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int longestCommonSubsequence(string s, string t) { 4 | 5 | int n = s.size(); 6 | int m = t.size(); 7 | 8 | int dp[n+1][m+1]; 9 | 10 | 11 | for(int i=0;i<=n;i++){ 12 | for(int j=0;j<=m;j++){ 13 | if(i==0 || j==0){ 14 | dp[i][j] = 0; 15 | } 16 | } 17 | } 18 | 19 | 20 | for(int i=1;i<=n;i++){ 21 | for(int j=1;j<=m;j++){ 22 | 23 | //if sequence as same 24 | if(s[i-1] == t[j-1]){ 25 | dp[i][j] = 1+dp[i-1][j-1]; 26 | } 27 | else{ 28 | dp[i][j] = max(dp[i-1][j],dp[i][j-1]); 29 | } 30 | } 31 | } 32 | 33 | return dp[n][m]; 34 | } 35 | }; -------------------------------------------------------------------------------- /1155-number-of-dice-rolls-with-target-sum/1155-number-of-dice-rolls-with-target-sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numRollsToTarget(int n, int k, int target) { 4 | 5 | int m=pow(10,9)+7; 6 | 7 | vector>dp(n+1, vector(target+1,0)); 8 | 9 | dp[0][0]=1; 10 | 11 | for(int i=1; i<=n; i++){ 12 | 13 | for(int j=1; j<=target; j++){ 14 | int ans=0; 15 | 16 | for(int f=1; f<=k; f++){ 17 | 18 | if(j-f>=0) 19 | ans=(ans+(dp[i-1][j-f]%m))%m; 20 | } 21 | 22 | dp[i][j]=ans%m; 23 | } 24 | } 25 | return dp[n][target]; 26 | } 27 | }; -------------------------------------------------------------------------------- /1155-number-of-dice-rolls-with-target-sum/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1200-minimum-absolute-difference/1200-minimum-absolute-difference.cpp: -------------------------------------------------------------------------------- 1 | using namespace std; 2 | static const auto fast = []() 3 | { 4 | ios_base::sync_with_stdio(false); 5 | cin.tie(0); 6 | cout.tie(0); 7 | return 0; 8 | } (); 9 | 10 | class Solution { 11 | public: 12 | vector> minimumAbsDifference(vector& arr) { 13 | 14 | sort(arr.begin(), arr.end()); 15 | 16 | vector> results; 17 | 18 | int diff = INT_MAX, min_diff = INT_MAX; 19 | 20 | for (int i = 1; i < arr.size(); i++) { 21 | 22 | diff = arr[i] - arr[i-1]; 23 | 24 | if (diff == min_diff) 25 | { 26 | results.push_back({ arr[i - 1],arr[i] }); 27 | min_diff = diff; 28 | } 29 | else if (diff < min_diff){ 30 | results.clear(); 31 | results.push_back({ arr[i - 1],arr[i] }); 32 | min_diff = diff; 33 | } 34 | } 35 | 36 | return results; 37 | } 38 | }; -------------------------------------------------------------------------------- /1200-minimum-absolute-difference/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1207-unique-number-of-occurrences/1207-unique-number-of-occurrences.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | bool uniqueOccurrences(vector &arr) 5 | { 6 | map mp; 7 | set s; 8 | 9 | for (int i: arr) 10 | { 11 | mp[i]++; 12 | } 13 | 14 | for (auto m: mp) 15 | { 16 | s.insert(m.second); 17 | } 18 | 19 | return mp.size() == s.size(); 20 | } 21 | }; -------------------------------------------------------------------------------- /1207-unique-number-of-occurrences/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1207-unique-number-of-occurrences/README.md: -------------------------------------------------------------------------------- 1 |

1207. Unique Number of Occurrences

Easy


Given an array of integers arr, return true if the number of occurrences of each value in the array is unique or false otherwise.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
Input: arr = [1,2,2,1,1,3]
 7 | Output: true
 8 | Explanation: The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences.
9 | 10 |

Example 2:

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

Example 3:

17 | 18 |
Input: arr = [-3,0,1,-3,1,1,1,-3,10,0]
19 | Output: true
20 | 
21 | 22 |

 

23 |

Constraints:

24 | 25 |
    26 |
  • 1 <= arr.length <= 1000
  • 27 |
  • -1000 <= arr[i] <= 1000
  • 28 |
29 |
-------------------------------------------------------------------------------- /1220-count-vowels-permutation/1220-count-vowels-permutation.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countVowelPermutation(int n) { 4 | long long a = 1, e = 1, i = 1, o = 1, u = 1, mod = pow(10, 9)+7; 5 | long long a2, e2, i2, o2, u2; 6 | 7 | for (int j = 2; j <= n; j++) { 8 | a2 = (e + i + u) % mod; 9 | e2 = (a + i) % mod; 10 | i2 = (e + o) % mod; 11 | o2 = i; 12 | u2 = (o + i) % mod; 13 | 14 | a = a2, e = e2, i = i2, o = o2, u = u2; 15 | } 16 | 17 | return (a + e + i + o + u) % mod; 18 | } 19 | }; -------------------------------------------------------------------------------- /1220-count-vowels-permutation/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1235-maximum-profit-in-job-scheduling/1235-maximum-profit-in-job-scheduling.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int jobScheduling(vector& startTime, vector& endTime, vector& profit) { 4 | // step 1: generate the sorting index such that endTime[index[i]] <= endTime[index[i+1]] for 0 <= i < n-1 5 | 6 | auto comp = [&endTime](const int i1, const int i2) { return endTime[i1] < endTime[i2]; }; 7 | int n = endTime.size(); 8 | 9 | vector index(n); 10 | 11 | iota(index.begin(), index.end(), 0); 12 | 13 | sort(index.begin(), index.end(), comp); 14 | 15 | // step 2: in order to perform binary search, we also need the sorting endTime array 16 | 17 | vector endSorted(endTime.begin(), endTime.end()); 18 | 19 | sort(endSorted.begin(), endSorted.end()); 20 | 21 | 22 | // step 3: calculate dp table 23 | 24 | vector dp(n + 1); 25 | 26 | for (int i = 1; i <= n; i++) { 27 | int j = upper_bound(endSorted.begin(), endSorted.end(), startTime[index[i-1]]) - endSorted.begin(); 28 | dp[i] = max(dp[i-1], profit[index[i-1]] + dp[j]); 29 | } 30 | 31 | return dp[n]; 32 | } 33 | }; -------------------------------------------------------------------------------- /1235-maximum-profit-in-job-scheduling/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1239-maximum-length-of-a-concatenated-string-with-unique-characters/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1269-number-of-ways-to-stay-in-the-same-place-after-some-steps/1269-number-of-ways-to-stay-in-the-same-place-after-some-steps.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | const int MOD = 1000000007; 4 | 5 | public: 6 | int numWays(int steps, int arrLen) { 7 | vector> vec(steps); 8 | unordered_map mp, tp; 9 | 10 | mp[0] = 1; 11 | 12 | if(arrLen > 1) 13 | mp[1] = 1; 14 | vec[0] = mp; 15 | 16 | for(int i = 1; i < steps; i++){ 17 | 18 | mp.clear(); 19 | 20 | tp = vec[i-1]; 21 | 22 | for(auto it = tp.begin(); it != tp.end(); it++){ 23 | 24 | int el = it->first; 25 | 26 | if(el + 1 < arrLen) 27 | mp[el+1] = (mp[el+1] + it->second) % MOD; 28 | 29 | if(el - 1 >= 0) 30 | mp[el-1] = (mp[el-1] + it->second) % MOD; 31 | 32 | mp[el] = (mp[el] + it->second) % MOD; 33 | } 34 | 35 | vec[i] = mp; 36 | } 37 | 38 | return (int)vec[steps - 1][0]; 39 | } 40 | }; -------------------------------------------------------------------------------- /1269-number-of-ways-to-stay-in-the-same-place-after-some-steps/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1287-element-appearing-more-than-25-in-sorted-array/1287-element-appearing-more-than-25-in-sorted-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int findSpecialInteger(vector &arr) 5 | { 6 | map mp; 7 | int size = arr.size() / 4; 8 | int elem = 0; 9 | for (int i = 0; i < arr.size(); i++) 10 | { 11 | mp[arr[i]]++; 12 | 13 | if (mp[arr[i]] > size) 14 | elem = arr[i]; 15 | } 16 | 17 | return elem; 18 | } 19 | }; -------------------------------------------------------------------------------- /1287-element-appearing-more-than-25-in-sorted-array/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1287-element-appearing-more-than-25-in-sorted-array/README.md: -------------------------------------------------------------------------------- 1 |

1287. 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 |
-------------------------------------------------------------------------------- /1293-shortest-path-in-a-grid-with-obstacles-elimination/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1335-minimum-difficulty-of-a-job-schedule/1335-minimum-difficulty-of-a-job-schedule.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | int t[301][11]; 3 | int solve(vector& mat, int n, int idx, int d) { 4 | 5 | if(d == 1) 6 | return *max_element(begin(mat)+idx, end(mat)); 7 | 8 | if(t[idx][d] != -1) 9 | return t[idx][d]; 10 | 11 | 12 | int Max = INT_MIN; 13 | int result = INT_MAX; 14 | 15 | for(int i = idx; i<=n-d; i++) { 16 | Max = max(Max, mat[i]); 17 | result = min(result, Max + solve(mat, n, i+1, d-1)); 18 | } 19 | 20 | return t[idx][d] = result; 21 | } 22 | 23 | public: 24 | 25 | int minDifficulty(vector& jobDifficulty, int d) { 26 | 27 | int n = jobDifficulty.size(); 28 | 29 | if(n < d) 30 | return -1; 31 | memset(t, -1, sizeof(t)); 32 | return solve(jobDifficulty, n, 0, d); 33 | } 34 | }; -------------------------------------------------------------------------------- /1335-minimum-difficulty-of-a-job-schedule/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1347-minimum-number-of-steps-to-make-two-strings-anagram/1347-minimum-number-of-steps-to-make-two-strings-anagram.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int minSteps(string s, string t) 5 | { 6 | 7 | int freq[26] = { false 8 | }; 9 | 10 | for (int i = 0; i < s.size(); i++) 11 | { 12 | 13 | freq[t[i] - 'a']++; 14 | freq[s[i] - 'a']--; 15 | } 16 | 17 | int counter = 0; 18 | 19 | for (int it: freq) 20 | { 21 | counter += max(it, 0); 22 | } 23 | 24 | return counter; 25 | } 26 | }; -------------------------------------------------------------------------------- /1347-minimum-number-of-steps-to-make-two-strings-anagram/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1356-sort-integers-by-the-number-of-1-bits/1356-sort-integers-by-the-number-of-1-bits.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | static bool comparator(int a,int b){ 4 | 5 | int p = num_one_bit(a); 6 | int q = num_one_bit(b); 7 | 8 | if( p == q ){ 9 | return a <= b; 10 | } 11 | else return p < q; 12 | } 13 | 14 | static int num_one_bit (int n){ 15 | 16 | int count=0; 17 | 18 | while(n){ 19 | 20 | count += n&1; 21 | 22 | n >>= 1; 23 | 24 | } 25 | 26 | return count; 27 | } 28 | 29 | public: 30 | 31 | vector sortByBits(vector& arr) { 32 | 33 | sort(arr.begin(),arr.end(),comparator); 34 | 35 | return arr; 36 | } 37 | }; -------------------------------------------------------------------------------- /1356-sort-integers-by-the-number-of-1-bits/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1356-sort-integers-by-the-number-of-1-bits/README.md: -------------------------------------------------------------------------------- 1 |

1356. Sort Integers by The Number of 1 Bits

Easy


You are given an integer array arr. Sort the integers in the array in ascending order by the number of 1's in their binary representation and in case of two or more integers have the same number of 1's you have to sort them in ascending order.

2 | 3 |

Return the array after sorting it.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: arr = [0,1,2,3,4,5,6,7,8]
 9 | Output: [0,1,2,4,8,3,5,6,7]
10 | Explantion: [0] is the only integer with 0 bits.
11 | [1,2,4,8] all have 1 bit.
12 | [3,5,6] have 2 bits.
13 | [7] has 3 bits.
14 | The sorted array by bits is [0,1,2,4,8,3,5,6,7]
15 | 
16 | 17 |

Example 2:

18 | 19 |
Input: arr = [1024,512,256,128,64,32,16,8,4,2,1]
20 | Output: [1,2,4,8,16,32,64,128,256,512,1024]
21 | Explantion: All integers have 1 bit in the binary representation, you should just sort them in ascending order.
22 | 
23 | 24 |

 

25 |

Constraints:

26 | 27 |
    28 |
  • 1 <= arr.length <= 500
  • 29 |
  • 0 <= arr[i] <= 104
  • 30 |
31 |
-------------------------------------------------------------------------------- /1420-build-array-where-you-can-find-the-maximum-exactly-k-comparisons/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1425-constrained-subsequence-sum/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1443-minimum-time-to-collect-all-apples-in-a-tree/1443-minimum-time-to-collect-all-apples-in-a-tree.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | vector> adjList; 3 | 4 | int dfs(vector& hasApple,int node,int d,int prev) 5 | { 6 | int result=0,temp; 7 | 8 | for(int &i:adjList[node]) 9 | if(i!=prev) 10 | { 11 | temp=dfs(hasApple,i,d+1,node); 12 | 13 | if(temp) 14 | result+=temp-d; 15 | } 16 | 17 | return result||hasApple[node]?result+d:0; 18 | 19 | } 20 | 21 | public: 22 | 23 | int minTime(int n, vector>& edges, vector& hasApple) 24 | { 25 | adjList.resize(n); 26 | for(vector &e:edges) 27 | adjList[e[0]].push_back(e[1]),adjList[e[1]].push_back(e[0]); 28 | 29 | return dfs(hasApple,0,0,-1)*2; 30 | } 31 | }; -------------------------------------------------------------------------------- /1443-minimum-time-to-collect-all-apples-in-a-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1457-pseudo-palindromic-paths-in-a-binary-tree/1457-pseudo-palindromic-paths-in-a-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 | { 14 | int count = 0; 15 | 16 | void dfs(TreeNode *node, set remaining) 17 | { 18 | if (remaining.count(node->val)) 19 | remaining.erase(node->val); 20 | else 21 | remaining.insert(node->val); 22 | 23 | if (!node->left &!node->right) 24 | { 25 | if (remaining.size() <= 1) 26 | count++; 27 | 28 | return; 29 | } 30 | 31 | if (node->left) 32 | dfs(node->left, remaining); 33 | 34 | if (node->right) 35 | dfs(node->right, remaining); 36 | } 37 | 38 | public: 39 | int pseudoPalindromicPaths(TreeNode *root) 40 | { 41 | if (!root) 42 | return 0; 43 | 44 | set mys; 45 | dfs(root, mys); 46 | return count; 47 | } 48 | }; -------------------------------------------------------------------------------- /1457-pseudo-palindromic-paths-in-a-binary-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1458-max-dot-product-of-two-subsequences/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1493-longest-subarray-of-1s-after-deleting-one-element/1493-longest-subarray-of-1s-after-deleting-one-element.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int longestSubarray(vector& nums) { 4 | 5 | int zeroCount = 0; 6 | int longestWindow = 0; 7 | int start = 0; 8 | 9 | for (int i = 0; i < nums.size(); i++) { 10 | zeroCount += (nums[i] == 0); 11 | 12 | while (zeroCount > 1) { 13 | zeroCount -= (nums[start] == 0); 14 | start++; 15 | } 16 | 17 | longestWindow = max(longestWindow, i - start); 18 | } 19 | 20 | return longestWindow; 21 | } 22 | }; -------------------------------------------------------------------------------- /1493-longest-subarray-of-1s-after-deleting-one-element/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1493-longest-subarray-of-1s-after-deleting-one-element/README.md: -------------------------------------------------------------------------------- 1 |

1493. Longest Subarray of 1's After Deleting One Element

Medium


Given a binary array nums, you should delete one element from it.

2 | 3 |

Return the size of the longest non-empty subarray containing only 1's in the resulting array. Return 0 if there is no such subarray.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: nums = [1,1,0,1]
 9 | Output: 3
10 | Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
11 | 
12 | 13 |

Example 2:

14 | 15 |
Input: nums = [0,1,1,1,0,1,1,0,1]
16 | Output: 5
17 | Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
18 | 
19 | 20 |

Example 3:

21 | 22 |
Input: nums = [1,1,1]
23 | Output: 2
24 | Explanation: You must delete one element.
25 | 
26 | 27 |

 

28 |

Constraints:

29 | 30 |
    31 |
  • 1 <= nums.length <= 105
  • 32 |
  • nums[i] is either 0 or 1.
  • 33 |
34 |
-------------------------------------------------------------------------------- /1512-number-of-good-pairs/1512-number-of-good-pairs.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numIdenticalPairs(vector& nums) { 4 | map mp; 5 | int counter = 0 ; 6 | 7 | for(int i =0;i < nums.size();i++){ 8 | 9 | counter += mp[nums[i]]; 10 | 11 | mp[nums[i]]++; 12 | } 13 | 14 | return counter; 15 | } 16 | }; -------------------------------------------------------------------------------- /1512-number-of-good-pairs/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1512-number-of-good-pairs/README.md: -------------------------------------------------------------------------------- 1 |

1512. Number of Good Pairs

Easy


Given an array of integers nums, return the number of good pairs.

2 | 3 |

A pair (i, j) is called good if nums[i] == nums[j] and i < j.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: nums = [1,2,3,1,1,3]
 9 | Output: 4
10 | Explanation: There are 4 good pairs (0,3), (0,4), (3,4), (2,5) 0-indexed.
11 | 
12 | 13 |

Example 2:

14 | 15 |
Input: nums = [1,1,1,1]
16 | Output: 6
17 | Explanation: Each pair in the array are good.
18 | 
19 | 20 |

Example 3:

21 | 22 |
Input: nums = [1,2,3]
23 | Output: 0
24 | 
25 | 26 |

 

27 |

Constraints:

28 | 29 |
    30 |
  • 1 <= nums.length <= 100
  • 31 |
  • 1 <= nums[i] <= 100
  • 32 |
33 |
-------------------------------------------------------------------------------- /1519-number-of-nodes-in-the-sub-tree-with-the-same-label/1519-number-of-nodes-in-the-sub-tree-with-the-same-label.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | vector array; 3 | 4 | vector dfs(int prev,int curr,vector>& a,string& labels) 5 | { 6 | vector ans(26); 7 | for(auto x:a[curr]) 8 | { 9 | if(prev!=x) 10 | { 11 | vector res = dfs(curr,x,a,labels); 12 | for(int i=0;i<26;i++) 13 | ans[i]+=res[i]; 14 | } 15 | } 16 | 17 | array[curr] = ++ans[labels[curr]-'a']; 18 | return ans; 19 | } 20 | 21 | public: 22 | vector countSubTrees(int n, vector>& edges, string labels) { 23 | array.resize(n); //output array 24 | 25 | 26 | vector> a(n); 27 | 28 | for(auto x:edges){ 29 | a[x[0]].push_back(x[1]); 30 | a[x[1]].push_back(x[0]); 31 | } 32 | 33 | dfs(-1,0,a,labels); 34 | 35 | return array; 36 | } 37 | }; -------------------------------------------------------------------------------- /1519-number-of-nodes-in-the-sub-tree-with-the-same-label/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1531-string-compression-ii/1531-string-compression-ii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | int dp[101][101]; 4 | int dfs(string &s, int left, int K) { 5 | int k = K; 6 | if(s.size() - left <= k) return 0; 7 | if(dp[left][k] >= 0) return dp[left][k]; 8 | int res = k ? dfs(s, left + 1, k - 1) : 10000, c = 1; 9 | for(int i = left + 1; i <= s.size(); ++i) { 10 | res = min(res, dfs(s, i, k) + 1 + (c >= 100 ? 3 : (c >= 10 ? 2 : (c > 1 ? 1 :0)))); 11 | if(i == s.size()) break; 12 | if(s[i] == s[left]) ++c; 13 | else if(--k < 0) break; 14 | } 15 | return dp[left][K] = res; 16 | } 17 | 18 | public: 19 | 20 | int getLengthOfOptimalCompression(string s, int k) { 21 | memset(dp, -1, sizeof(dp)); 22 | return dfs(s, 0, k); 23 | } 24 | }; -------------------------------------------------------------------------------- /1531-string-compression-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1535-find-the-winner-of-an-array-game/1535-find-the-winner-of-an-array-game.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int getWinner(vector& arr, int k) { 4 | int Maximum = arr[0]; 5 | map mp; 6 | 7 | if (k >= arr.size()) 8 | return *max_element(arr.begin(), arr.end()); 9 | 10 | for (int i = 1; i < arr.size() && k > 0; i++) 11 | { 12 | if (arr[i] > Maximum) 13 | { 14 | Maximum = arr[i]; 15 | } 16 | 17 | mp[Maximum]++; 18 | 19 | if (mp[Maximum] == k) 20 | return Maximum; 21 | } 22 | 23 | return Maximum; 24 | } 25 | }; -------------------------------------------------------------------------------- /1535-find-the-winner-of-an-array-game/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /16-3sum-closest/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /16-3sum-closest/README.md: -------------------------------------------------------------------------------- 1 |

16. 3Sum Closest

Medium


Given an integer array nums of length n and an integer target, find three integers in nums such that the sum is closest to target.

2 | 3 |

Return the sum of the three integers.

4 | 5 |

You may assume that each input would have exactly one solution.

6 | 7 |

 

8 |

Example 1:

9 | 10 |
Input: nums = [-1,2,1,-4], target = 1
11 | Output: 2
12 | Explanation: The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
13 | 
14 | 15 |

Example 2:

16 | 17 |
Input: nums = [0,0,0], target = 1
18 | Output: 0
19 | Explanation: The sum that is closest to the target is 0. (0 + 0 + 0 = 0).
20 | 
21 | 22 |

 

23 |

Constraints:

24 | 25 |
    26 |
  • 3 <= nums.length <= 1000
  • 27 |
  • -1000 <= nums[i] <= 1000
  • 28 |
  • -104 <= target <= 104
  • 29 |
30 |
-------------------------------------------------------------------------------- /1624-largest-substring-between-two-equal-characters/1624-largest-substring-between-two-equal-characters.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxLengthBetweenEqualCharacters(string s) { 4 | 5 | string str ; 6 | int maximum = -1; 7 | 8 | for(int i =0;i maximum) 13 | maximum = j - i - 1; 14 | } 15 | } 16 | 17 | return maximum; 18 | } 19 | }; -------------------------------------------------------------------------------- /1624-largest-substring-between-two-equal-characters/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1624-largest-substring-between-two-equal-characters/README.md: -------------------------------------------------------------------------------- 1 |

1624. Largest Substring Between Two Equal Characters

Easy


Given a string s, return the length of the longest substring between two equal characters, excluding the two characters. If there is no such substring return -1.

2 | 3 |

A substring is a contiguous sequence of characters within a string.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: s = "aa"
 9 | Output: 0
10 | Explanation: The optimal substring here is an empty substring between the two 'a's.
11 | 12 |

Example 2:

13 | 14 |
Input: s = "abca"
15 | Output: 2
16 | Explanation: The optimal substring here is "bc".
17 | 
18 | 19 |

Example 3:

20 | 21 |
Input: s = "cbzxy"
22 | Output: -1
23 | Explanation: There are no characters that appear twice in s.
24 | 
25 | 26 |

 

27 |

Constraints:

28 | 29 |
    30 |
  • 1 <= s.length <= 300
  • 31 |
  • s contains only lowercase English letters.
  • 32 |
33 |
-------------------------------------------------------------------------------- /1626-best-team-with-no-conflicts/1626-best-team-with-no-conflicts.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int bestTeamScore(vector& scores, vector& ages) 4 | { 5 | int n = scores.size(); 6 | 7 | int dp[n], ans = 0; 8 | 9 | vector> players; 10 | 11 | for(int i = 0; i < n; i++) 12 | players.push_back({ages[i], scores[i]}); 13 | 14 | sort(players.begin(), players.end()); 15 | for(int i = 0; i < n; i++) { 16 | dp[i] = players[i].second; 17 | for(int j = 0; j < i; j++) { 18 | 19 | if(players[j].second <= players[i].second) 20 | dp[i] = max(dp[i], dp[j] + players[i].second); 21 | } 22 | 23 | ans = max(ans, dp[i]); 24 | } 25 | return ans; 26 | } 27 | }; -------------------------------------------------------------------------------- /1626-best-team-with-no-conflicts/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1662-check-if-two-string-arrays-are-equivalent/1662-check-if-two-string-arrays-are-equivalent.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool arrayStringsAreEqual(vector& word1, vector& word2) { 4 | 5 | string temp1, temp2; 6 | for(string str :word1 ) 7 | temp1 += str; 8 | 9 | for(string str :word2 ) 10 | temp2 += str; 11 | 12 | return temp1 == temp2; 13 | } 14 | }; -------------------------------------------------------------------------------- /1662-check-if-two-string-arrays-are-equivalent/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1688-count-of-matches-in-tournament/1688-count-of-matches-in-tournament.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numberOfMatches(int n) { 4 | int matches = 0,teams= n; 5 | 6 | while (teams > 1){ 7 | 8 | if (teams % 2 == 1){ 9 | 10 | matches += ((teams-1)/2); 11 | teams = ((teams - 1) / 2) + 1; 12 | 13 | }else { 14 | 15 | matches += (teams/2); 16 | teams = teams/2; 17 | } 18 | } 19 | 20 | return matches; 21 | } 22 | }; -------------------------------------------------------------------------------- /1688-count-of-matches-in-tournament/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1704-determine-if-string-halves-are-alike/1704-determine-if-string-halves-are-alike.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | bool halvesAreAlike(string s) 5 | { 6 | 7 | set vowel = { 'a', 8 | 'e', 9 | 'i', 10 | 'o', 11 | 'u', 12 | 'A', 13 | 'E', 14 | 'I', 15 | 'O', 16 | 'U' }; 17 | 18 | int sum = 0; 19 | 20 | for (int i = 0; i < s.size() / 2; i++) 21 | { 22 | sum += vowel.count(s[i]) - vowel.count(s[s.size() - 1 - i]); 23 | } 24 | 25 | return sum == 0; 26 | } 27 | }; -------------------------------------------------------------------------------- /1704-determine-if-string-halves-are-alike/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1706-where-will-the-ball-fall/1706-where-will-the-ball-fall.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector findBall(vector>& grid) { 4 | 5 | int m = grid.size(), n = grid[0].size(); 6 | vector>dp(m + 1, vector(n, -1)); 7 | for (int j = 0; j < n; j++) 8 | dp[m][j] = j; 9 | for (int i = m - 1; i >= 0; i--) 10 | { 11 | for (int j = 0; j < n; j++) 12 | { 13 | if (grid[i][j] == 1) 14 | { 15 | bool isRightCellRight = (j + 1 == n)? false : (grid[i][j + 1] == 1); 16 | if (isRightCellRight) dp[i][j] = dp[i + 1][j + 1]; 17 | } 18 | else if (grid[i][j] == -1) 19 | { 20 | bool isLeftCellLeft = (j - 1 < 0)? false : (grid[i][j - 1] == -1); 21 | if (isLeftCellLeft) dp[i][j] = dp[i + 1][j - 1]; 22 | } 23 | } 24 | } 25 | 26 | return dp[0]; 27 | } 28 | }; -------------------------------------------------------------------------------- /1706-where-will-the-ball-fall/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1793-maximum-score-of-a-good-subarray/1793-maximum-score-of-a-good-subarray.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maximumScore(vector& nums, int k) { 4 | int left = k, right = k; 5 | int min_val = nums[k]; 6 | int max_score = min_val; 7 | 8 | while (left > 0 || right < nums.size() - 1) { 9 | if (left == 0 || (right < nums.size() - 1 && nums[right + 1] > nums[left - 1])) { 10 | right++; 11 | } else { 12 | left--; 13 | } 14 | min_val = min(min_val, std::min(nums[left], nums[right])); 15 | max_score = max(max_score, min_val * (right - left + 1)); 16 | } 17 | 18 | return max_score; 19 | } 20 | }; -------------------------------------------------------------------------------- /1793-maximum-score-of-a-good-subarray/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1793-maximum-score-of-a-good-subarray/README.md: -------------------------------------------------------------------------------- 1 |

1793. Maximum Score of a Good Subarray

Hard


You are given an array of integers nums (0-indexed) and an integer k.

2 | 3 |

The score of a subarray (i, j) is defined as min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1). A good subarray is a subarray where i <= k <= j.

4 | 5 |

Return the maximum possible score of a good subarray.

6 | 7 |

 

8 |

Example 1:

9 | 10 |
Input: nums = [1,4,3,7,4,5], k = 3
11 | Output: 15
12 | Explanation: The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15. 
13 | 
14 | 15 |

Example 2:

16 | 17 |
Input: nums = [5,5,4,5,4,1,1,1], k = 0
18 | Output: 20
19 | Explanation: The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
20 | 
21 | 22 |

 

23 |

Constraints:

24 | 25 |
    26 |
  • 1 <= nums.length <= 105
  • 27 |
  • 1 <= nums[i] <= 2 * 104
  • 28 |
  • 0 <= k < nums.length
  • 29 |
30 |
-------------------------------------------------------------------------------- /1832-check-if-the-sentence-is-pangram/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1832-check-if-the-sentence-is-pangram/README.md: -------------------------------------------------------------------------------- 1 |

1832. Check if the Sentence Is Pangram

Easy


A pangram is a sentence where every letter of the English alphabet appears at least once.

2 | 3 |

Given a string sentence containing only lowercase English letters, return true if sentence is a pangram, or false otherwise.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: sentence = "thequickbrownfoxjumpsoverthelazydog"
 9 | Output: true
10 | Explanation: sentence contains at least one of every letter of the English alphabet.
11 | 
12 | 13 |

Example 2:

14 | 15 |
Input: sentence = "leetcode"
16 | Output: false
17 | 
18 | 19 |

 

20 |

Constraints:

21 | 22 |
    23 |
  • 1 <= sentence.length <= 1000
  • 24 |
  • sentence consists of lowercase English letters.
  • 25 |
26 |
-------------------------------------------------------------------------------- /1834-single-threaded-cpu/1834-single-threaded-cpu.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector getOrder(vector>& tasks) { 4 | 5 | int n = tasks.size(), i = 0; 6 | vector> v; 7 | 8 | for (int i = 0; i < n; i++) 9 | v.push_back({tasks[i][0], tasks[i][1], i}); 10 | 11 | sort(v.begin(), v.end()); 12 | 13 | priority_queue, vector>, greater>> pq; 14 | 15 | vector ans; 16 | int endtime = v[i][0]; 17 | 18 | while (i < n) 19 | { 20 | if (!pq.empty()) 21 | { 22 | endtime += pq.top()[0]; 23 | ans.push_back(pq.top()[1]); 24 | pq.pop(); 25 | } 26 | 27 | while (i < n and v[i][0] <= endtime) 28 | { 29 | pq.push({v[i][1], v[i][2], v[i][0]}); 30 | i++; 31 | } 32 | 33 | if (i < n and endtime < v[i][0] and pq.empty()) 34 | endtime = v[i][0]; 35 | } 36 | 37 | while (!pq.empty()) 38 | ans.push_back(pq.top()[1]), pq.pop(); 39 | 40 | return ans; 41 | } 42 | }; -------------------------------------------------------------------------------- /1834-single-threaded-cpu/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1845-seat-reservation-manager/1845-seat-reservation-manager.cpp: -------------------------------------------------------------------------------- 1 | class SeatManager { 2 | set ss; 3 | public: 4 | SeatManager(int n) { 5 | for(int i=1;i<=n;i++) 6 | { 7 | ss.insert(i); 8 | } 9 | } 10 | 11 | int reserve() { 12 | auto temp = ss.begin(); 13 | int it = *temp; 14 | 15 | ss.erase(temp); 16 | 17 | return it; 18 | } 19 | 20 | void unreserve(int seatNumber) { 21 | 22 | ss.insert(seatNumber); 23 | } 24 | }; 25 | 26 | /** 27 | * Your SeatManager object will be instantiated and called as such: 28 | * SeatManager* obj = new SeatManager(n); 29 | * int param_1 = obj->reserve(); 30 | * obj->unreserve(seatNumber); 31 | */ -------------------------------------------------------------------------------- /1845-seat-reservation-manager/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1903-largest-odd-number-in-string/1903-largest-odd-number-in-string.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string largestOddNumber(string num) { 4 | 5 | for(int i =num.size() -1;i>= 0;i--){ 6 | int n = num[i] - '0'; 7 | 8 | if (n % 2 != 0) 9 | return num.substr(0, i + 1); 10 | } 11 | 12 | return "" ; 13 | } 14 | }; -------------------------------------------------------------------------------- /1903-largest-odd-number-in-string/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1903-largest-odd-number-in-string/README.md: -------------------------------------------------------------------------------- 1 |

1903. Largest Odd Number in String

Easy


You are given a string num, representing a large integer. Return the largest-valued odd integer (as a string) that is a non-empty substring of num, or an empty string "" if no odd integer exists.

2 | 3 |

A substring is a contiguous sequence of characters within a string.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: num = "52"
 9 | Output: "5"
10 | Explanation: The only non-empty substrings are "5", "2", and "52". "5" is the only odd number.
11 | 
12 | 13 |

Example 2:

14 | 15 |
Input: num = "4206"
16 | Output: ""
17 | Explanation: There are no odd numbers in "4206".
18 | 
19 | 20 |

Example 3:

21 | 22 |
Input: num = "35427"
23 | Output: "35427"
24 | Explanation: "35427" is already an odd number.
25 | 
26 | 27 |

 

28 |

Constraints:

29 | 30 |
    31 |
  • 1 <= num.length <= 105
  • 32 |
  • num only consists of digits and does not contain any leading zeros.
  • 33 |
34 |
-------------------------------------------------------------------------------- /1921-eliminate-maximum-number-of-monsters/1921-eliminate-maximum-number-of-monsters.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int eliminateMaximum(vector& dist, vector& speed) { 4 | 5 | vector time(dist.size()); 6 | 7 | for(int i=0;i= time[i]) 18 | break; 19 | else 20 | startTime++; 21 | } 22 | 23 | return startTime; 24 | } 25 | }; -------------------------------------------------------------------------------- /1921-eliminate-maximum-number-of-monsters/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1971-find-if-path-exists-in-graph/1971-find-if-path-exists-in-graph.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool validPath(int n, vector>& edges, int source, int destination) { 4 | vector adj[n]; // Adjacency list 5 | if(source==destination) 6 | return true; 7 | 8 | for(auto &edge:edges){ 9 | adj[edge[1]].push_back(edge[0]); 10 | adj[edge[0]].push_back(edge[1]); 11 | } 12 | vector vis(n,false); 13 | 14 | queue q; 15 | q.push(source); 16 | 17 | vis[source]=true; 18 | 19 | while(!q.empty()){ 20 | int node =q.front(); 21 | q.pop(); 22 | if(node == destination){ 23 | return true; // we got the destination 24 | } 25 | 26 | for(int connected_node:adj[node]){ 27 | 28 | if(!vis[connected_node]){ 29 | vis[connected_node]=true; 30 | 31 | q.push(connected_node); 32 | } 33 | } 34 | } 35 | return false; // not reach to the destination 36 | } 37 | }; -------------------------------------------------------------------------------- /2009-minimum-number-of-operations-to-make-array-continuous/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /2024-maximize-the-confusion-of-an-exam/2024-maximize-the-confusion-of-an-exam.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | int maxConsecutiveAnswersHelper(string answerKey, int k, char target){ 3 | int ans = -1,counter=0; 4 | 5 | for(int i =0,j=0; j k){ 11 | 12 | if (answerKey[i] == target) 13 | counter--; 14 | 15 | i++; 16 | } 17 | 18 | ans = max(ans,j - i +1); 19 | } 20 | 21 | return ans; 22 | } 23 | public: 24 | int maxConsecutiveAnswers(string answerKey, int k) { 25 | 26 | return max(maxConsecutiveAnswersHelper(answerKey,k,'T'), maxConsecutiveAnswersHelper(answerKey,k,'F')); 27 | } 28 | }; -------------------------------------------------------------------------------- /2024-maximize-the-confusion-of-an-exam/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /2038-remove-colored-pieces-if-both-neighbors-are-the-same-color/2038-remove-colored-pieces-if-both-neighbors-are-the-same-color.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool winnerOfGame(string arr) { 4 | 5 | int alice = 0; 6 | 7 | for(int i = 2; i < arr.size();i++){ 8 | 9 | if (arr[i] == arr[i-1] && arr[i] == arr[i -2] && arr[i] == 'A') 10 | alice++; 11 | 12 | if (arr[i] == arr[i-1] && arr[i] == arr[i -2] && arr[i] == 'B') 13 | alice--; 14 | } 15 | 16 | return alice > 0; 17 | } 18 | }; -------------------------------------------------------------------------------- /2038-remove-colored-pieces-if-both-neighbors-are-the-same-color/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /2050-parallel-courses-iii/2050-parallel-courses-iii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | unordered_map> graph; 4 | 5 | vector memo; 6 | 7 | int dfs(int node, vector& time) { 8 | if (memo[node] != -1) { 9 | return memo[node]; 10 | } 11 | 12 | if (graph[node].size() == 0) { 13 | return time[node]; 14 | } 15 | 16 | int ans = 0; 17 | for (int neighbor: graph[node]) { 18 | ans = max(ans, dfs(neighbor, time)); 19 | } 20 | 21 | memo[node] = time[node] + ans; 22 | return memo[node]; 23 | } 24 | 25 | public: 26 | int minimumTime(int n, vector>& relations, vector& time) { 27 | for (vector& edge: relations) { 28 | int x = edge[0] - 1; 29 | int y = edge[1] - 1; 30 | graph[x].push_back(y); 31 | } 32 | 33 | memo = vector(n, -1); 34 | int ans = 0; 35 | for (int node = 0; node < n; node++) { 36 | ans = max(ans, dfs(node, time)); 37 | } 38 | 39 | return ans; 40 | } 41 | }; -------------------------------------------------------------------------------- /2050-parallel-courses-iii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /2095-delete-the-middle-node-of-a-linked-list/2095-delete-the-middle-node-of-a-linked-list.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* deleteMiddle(ListNode* head) { 14 | 15 | if(head==NULL || head->next==NULL) 16 | return NULL; 17 | 18 | ListNode *prev=NULL, *slow = head, *fast = head; 19 | 20 | while(fast!=NULL and fast->next!=NULL){ 21 | prev=slow; 22 | slow=slow->next; 23 | fast=fast->next->next; 24 | } 25 | 26 | prev->next=slow->next; 27 | 28 | delete slow; 29 | 30 | return head; 31 | } 32 | }; -------------------------------------------------------------------------------- /2095-delete-the-middle-node-of-a-linked-list/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /2125-number-of-laser-beams-in-a-bank/2125-number-of-laser-beams-in-a-bank.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numberOfBeams(vector& bank) { 4 | int prev = 0, ans = 0; 5 | 6 | for (string s : bank) { 7 | 8 | int counter = 0; 9 | 10 | for (char c : s) { 11 | if (c == '1') { 12 | counter++; 13 | } 14 | } 15 | if (counter != 0) { 16 | ans += (prev * counter); 17 | prev = counter; 18 | } 19 | } 20 | 21 | return ans; 22 | } 23 | }; -------------------------------------------------------------------------------- /2125-number-of-laser-beams-in-a-bank/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /2131-longest-palindrome-by-concatenating-two-letter-words/2131-longest-palindrome-by-concatenating-two-letter-words.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int longestPalindrome(vector& words) { 4 | int res = 0; 5 | unordered_map hm; 6 | 7 | for(string s:words){ 8 | 9 | string p = s; 10 | reverse(p.begin(),p.end()); 11 | 12 | if(hm[p]>0){ 13 | res+= 4; 14 | hm[p]--; 15 | } 16 | else{ 17 | hm[s]++; 18 | } 19 | } 20 | 21 | for(auto i:hm){ 22 | if(i.first[0]==i.first[1] && i.second>0){ 23 | return res+2; 24 | } 25 | } 26 | 27 | return res; 28 | } 29 | }; -------------------------------------------------------------------------------- /2131-longest-palindrome-by-concatenating-two-letter-words/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /2136-earliest-possible-day-of-full-bloom/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /2244-minimum-rounds-to-complete-all-tasks/2244-minimum-rounds-to-complete-all-tasks.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minimumRounds(vector& tasks) { 4 | int ans=0; 5 | unordered_mapfreq; 6 | 7 | for(int i=0;i& y,int i) { 3 | 4 | if(i==y[i]) 5 | return i; 6 | 7 | y[i]=find(y,y[i]); 8 | return y[i]; 9 | } 10 | public: 11 | 12 | int numberOfGoodPaths(vector& vals, vector>& edges) { 13 | int n = vals.size(),m=edges.size(),ans=0; 14 | vector> x(n); 15 | vector y(n); 16 | for(int i=0;i& a,vector& b){ 22 | return max(vals[a[0]],vals[a[1]])x[b][0]) 30 | y[b]=a; 31 | else 32 | y[a]=b; 33 | } 34 | else{ 35 | y[a]=b; 36 | ans+=x[a][1]*x[b][1]; 37 | x[b][1]+=x[a][1]; 38 | } 39 | } 40 | 41 | return ans+n; 42 | } 43 | }; -------------------------------------------------------------------------------- /2421-number-of-good-paths/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /2433-find-the-original-array-of-prefix-xor/2433-find-the-original-array-of-prefix-xor.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector findArray(vector& pref) { 4 | int prev = pref[0]; 5 | 6 | for (int i = 1; i < pref.size(); i++) { 7 | pref[i] ^= prev; 8 | prev ^= pref[i]; 9 | } 10 | 11 | return pref; 12 | } 13 | }; -------------------------------------------------------------------------------- /2433-find-the-original-array-of-prefix-xor/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /2433-find-the-original-array-of-prefix-xor/README.md: -------------------------------------------------------------------------------- 1 |

2433. Find The Original Array of Prefix Xor

Medium


You are given an integer array pref of size n. Find and return the array arr of size n that satisfies:

2 | 3 |
    4 |
  • pref[i] = arr[0] ^ arr[1] ^ ... ^ arr[i].
  • 5 |
6 | 7 |

Note that ^ denotes the bitwise-xor operation.

8 | 9 |

It can be proven that the answer is unique.

10 | 11 |

 

12 |

Example 1:

13 | 14 |
Input: pref = [5,2,0,3,1]
15 | Output: [5,7,2,3,2]
16 | Explanation: From the array [5,7,2,3,2] we have the following:
17 | - pref[0] = 5.
18 | - pref[1] = 5 ^ 7 = 2.
19 | - pref[2] = 5 ^ 7 ^ 2 = 0.
20 | - pref[3] = 5 ^ 7 ^ 2 ^ 3 = 3.
21 | - pref[4] = 5 ^ 7 ^ 2 ^ 3 ^ 2 = 1.
22 | 
23 | 24 |

Example 2:

25 | 26 |
Input: pref = [13]
27 | Output: [13]
28 | Explanation: We have pref[0] = arr[0] = 13.
29 | 
30 | 31 |

 

32 |

Constraints:

33 | 34 |
    35 |
  • 1 <= pref.length <= 105
  • 36 |
  • 0 <= pref[i] <= 106
  • 37 |
38 |
-------------------------------------------------------------------------------- /2482-difference-between-ones-and-zeros-in-row-and-column/2482-difference-between-ones-and-zeros-in-row-and-column.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | vector> onesMinusZeros(vector> &grid) 5 | { 6 | 7 | int m = grid.size(); 8 | int n = grid[0].size(); 9 | 10 | vector row(m,0); 11 | vector col(n,0); 12 | 13 | for (int i = 0; i < m; ++i) 14 | { 15 | for (int j = 0; j < n; ++j) 16 | { 17 | row[i] += 2 *grid[i][j] - 1; 18 | col[j] += 2 *grid[i][j] - 1; 19 | } 20 | } 21 | 22 | for (int i = 0; i < m; ++i) 23 | { 24 | for (int j = 0; j < n; ++j) 25 | { 26 | grid[i][j] = row[i] + col[j]; 27 | } 28 | } 29 | 30 | return grid; 31 | } 32 | }; -------------------------------------------------------------------------------- /2482-difference-between-ones-and-zeros-in-row-and-column/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /2551-put-marbles-in-bags/2551-put-marbles-in-bags.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long long putMarbles(vector& v, int k) { 4 | int n = v.size(); 5 | 6 | long long mx = v[0] + v[n-1]; 7 | long long mn = v[0] + v[n-1]; 8 | 9 | vector adjSum; 10 | 11 | for(int i = 0;i()); 15 | 16 | for(int i = 0;i> findMatrix(vector &nums) 5 | { 6 | 7 | vector> ans; 8 | unordered_map mp; 9 | 10 | for (auto i: nums) 11 | { 12 | mp[i]++; 13 | } 14 | 15 | while (!mp.empty()) 16 | { 17 | 18 | vector temp; 19 | 20 | for (auto it = mp.begin(); it != mp.end();) 21 | { 22 | temp.push_back(it->first); 23 | 24 | it->second--; 25 | 26 | if (it->second == 0) 27 | { 28 | it = mp.erase(it); 29 | } 30 | else 31 | { 32 | ++it; 33 | } 34 | } 35 | 36 | ans.push_back(temp); 37 | } 38 | 39 | return ans; 40 | } 41 | }; -------------------------------------------------------------------------------- /2610-convert-an-array-into-a-2d-array-with-conditions/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /2642-design-graph-with-shortest-path-calculator/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /2742-painting-the-walls/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /2849-determine-if-a-cell-is-reachable-at-a-given-time/2849-determine-if-a-cell-is-reachable-at-a-given-time.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isReachableAtTime(int sx, int sy, int fx, int fy, int t) { 4 | 5 | if(sx == fx && sy == fy) 6 | return t != 1; 7 | 8 | return (max(abs(sx - fx), abs(sy - fy)) <= t); 9 | } 10 | }; -------------------------------------------------------------------------------- /2849-determine-if-a-cell-is-reachable-at-a-given-time/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /2870-minimum-number-of-operations-to-make-array-empty/2870-minimum-number-of-operations-to-make-array-empty.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int minOperations(vector &nums) 5 | { 6 | map mp; 7 | 8 | for (int i = 0; i < nums.size(); i++) 9 | mp[nums[i]]++; 10 | 11 | int counter = 0; 12 | 13 | for(auto it : mp){ 14 | 15 | if (it.second == 1) 16 | return -1; 17 | else 18 | counter += ceil(double (it.second) / 3); 19 | 20 | } 21 | 22 | return counter; 23 | } 24 | }; -------------------------------------------------------------------------------- /2870-minimum-number-of-operations-to-make-array-empty/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /32-longest-valid-parentheses/32-longest-valid-parentheses.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | int work(string s){ 3 | 4 | int res = 0; 5 | 6 | for(int i = 0, start = 0, cnt = 0; i < s.size(); i++){ 7 | if(s[i] == '(') 8 | cnt++; 9 | else{ 10 | cnt --; 11 | 12 | if(cnt < 0) 13 | start = i+1, cnt = 0; 14 | else if(!cnt) 15 | res = max(res, i-start+1); 16 | } 17 | } 18 | 19 | return res; 20 | } 21 | public: 22 | int longestValidParentheses(string s) { 23 | int res = work(s); 24 | reverse(s.begin(), s.end()); 25 | 26 | for(auto& c: s) 27 | c ^= 1; 28 | 29 | return max(res, work(s)); 30 | } 31 | 32 | 33 | }; -------------------------------------------------------------------------------- /32-longest-valid-parentheses/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /32-longest-valid-parentheses/README.md: -------------------------------------------------------------------------------- 1 |

32. Longest Valid Parentheses

Hard


Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
Input: s = "(()"
 7 | Output: 2
 8 | Explanation: The longest valid parentheses substring is "()".
 9 | 
10 | 11 |

Example 2:

12 | 13 |
Input: s = ")()())"
14 | Output: 4
15 | Explanation: The longest valid parentheses substring is "()()".
16 | 
17 | 18 |

Example 3:

19 | 20 |
Input: s = ""
21 | Output: 0
22 | 
23 | 24 |

 

25 |

Constraints:

26 | 27 |
    28 |
  • 0 <= s.length <= 3 * 104
  • 29 |
  • s[i] is '(', or ')'.
  • 30 |
31 |
-------------------------------------------------------------------------------- /474-ones-and-zeroes/474-ones-and-zeroes.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findMaxForm(vector& strs, int m, int n) { 4 | 5 | vector> dp(m + 1, vector(n + 1)); 6 | 7 | for(auto& s : strs) { 8 | 9 | int zeros = count(s.begin(), s.end(), '0'); 10 | 11 | int ones = s.size(); 12 | 13 | for(int i = m; i >= zeros; i--) { 14 | 15 | for(int j = n; j >= (ones - zeros); j--) 16 | dp[i][j] = max(dp[i][j], 1 + dp[i - zeros][j - ones + zeros]); 17 | } 18 | } 19 | 20 | return dp[m][n]; 21 | } 22 | }; -------------------------------------------------------------------------------- /474-ones-and-zeroes/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /647-palindromic-substrings/647-palindromic-substrings.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | private: 3 | int mTotal = 0; 4 | 5 | void countPalindromes(int l, int r, const string& s) { 6 | 7 | while (l >= 0 && r < s.size() && s[l] == s[r]) 8 | isValidPalindrome(l, r); 9 | } 10 | 11 | void isValidPalindrome(int& l, int& r) { 12 | mTotal++; 13 | l--; r++; 14 | } 15 | 16 | public: 17 | int countSubstrings(string s) { 18 | 19 | mTotal = s.size(); 20 | 21 | if (mTotal == 0) 22 | return mTotal; 23 | 24 | for (int i = 0; i < s.length(); i++) { 25 | countPalindromes(i-1, i+1, s); // find palindromes of odd string length 26 | countPalindromes(i, i+1, s); // find palindromes of even string length 27 | } 28 | 29 | return mTotal; 30 | } 31 | }; -------------------------------------------------------------------------------- /647-palindromic-substrings/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /647-palindromic-substrings/README.md: -------------------------------------------------------------------------------- 1 |

647. Palindromic Substrings

Medium


Given a string s, return the number of palindromic substrings in it.

2 | 3 |

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

4 | 5 |

A substring is a contiguous sequence of characters within the string.

6 | 7 |

 

8 |

Example 1:

9 | 10 |
Input: s = "abc"
11 | Output: 3
12 | Explanation: Three palindromic strings: "a", "b", "c".
13 | 
14 | 15 |

Example 2:

16 | 17 |
Input: s = "aaa"
18 | Output: 6
19 | Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa".
20 | 
21 | 22 |

 

23 |

Constraints:

24 | 25 |
    26 |
  • 1 <= s.length <= 1000
  • 27 |
  • s consists of lowercase English letters.
  • 28 |
29 |
-------------------------------------------------------------------------------- /653-two-sum-iv-input-is-a-bst/653-two-sum-iv-input-is-a-bst.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 | 14 | bool DFS(TreeNode * temp, int k, set & ss){ 15 | 16 | if (temp == nullptr) 17 | return false; 18 | 19 | int num = k - temp->val; 20 | 21 | if (ss.find(num) != ss.end()) 22 | return true; 23 | else 24 | ss.insert(temp->val); 25 | 26 | return DFS(temp->left,k,ss) || DFS(temp->right,k,ss); 27 | } 28 | 29 | public: 30 | 31 | bool findTarget(TreeNode* root, int k) { 32 | 33 | set ss; 34 | 35 | if (root==nullptr) 36 | return false; 37 | else 38 | return DFS(root , k ,ss ); 39 | 40 | } 41 | }; -------------------------------------------------------------------------------- /653-two-sum-iv-input-is-a-bst/README.md: -------------------------------------------------------------------------------- 1 |

653. Two Sum IV - Input is a BST

Easy


Given the root of a Binary Search Tree and a target number k, return true if there exist two elements in the BST such that their sum is equal to the given target.

2 | 3 |

 

4 |

Example 1:

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

Example 2:

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

 

17 |

Constraints:

18 | 19 |
    20 |
  • The number of nodes in the tree is in the range [1, 104].
  • 21 |
  • -104 <= Node.val <= 104
  • 22 |
  • root is guaranteed to be a valid binary search tree.
  • 23 |
  • -105 <= k <= 105
  • 24 |
25 |
-------------------------------------------------------------------------------- /732-my-calendar-iii/732-my-calendar-iii.cpp: -------------------------------------------------------------------------------- 1 | class MyCalendarThree { 2 | mapmapping; 3 | int maxi; 4 | 5 | public: 6 | MyCalendarThree() { 7 | maxi = 0; 8 | } 9 | 10 | int book(int start, int end) { 11 | 12 | int count=0; 13 | mapping[start]++; 14 | mapping[end]--; 15 | 16 | for(auto it = mapping.begin();it!=mapping.end();it++){ 17 | count+=it->second; 18 | maxi=max(maxi,count); 19 | } 20 | 21 | return maxi; 22 | } 23 | }; 24 | 25 | /** 26 | * Your MyCalendarThree object will be instantiated and called as such: 27 | * MyCalendarThree* obj = new MyCalendarThree(); 28 | * int param_1 = obj->book(start,end); 29 | */ -------------------------------------------------------------------------------- /91-decode-ways/91-decode-ways.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numDecodings(string s) { 4 | 5 | vectordp(s.length()+1,0); 6 | 7 | if(s[0]=='0') // for cases like 06 as said in question 8 | dp[0] = 0; 9 | else 10 | dp[0] = 1; 11 | 12 | for(int i=1 ;i < s.length(); i++) 13 | { 14 | if(s[i-1] == '0' and s[i] == '0') 15 | dp[i] = 0; 16 | else if(s[i-1] == '0' and s[i] != '0') 17 | dp[i] = dp[i-1]; 18 | else if (s[i-1]!='0' and s[i] == '0') 19 | { 20 | if(s[i-1] == '1' or s[i-1] == '2') 21 | dp[i] = (i >=2 ? dp[i-2] : 1); 22 | else 23 | dp[i] = 0; 24 | } 25 | else 26 | { 27 | if(stoi(s.substr(i-1 , 2)) <= 26) 28 | dp[i] = dp[i-1] + (i >=2 ? dp[i-2] : 1); 29 | else 30 | dp[i] = dp[i-1]; 31 | } 32 | } 33 | return dp[s.length()-1]; 34 | } 35 | }; -------------------------------------------------------------------------------- /91-decode-ways/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /981-time-based-key-value-store/981-time-based-key-value-store.cpp: -------------------------------------------------------------------------------- 1 | class TimeMap { 2 | unordered_map> m; 3 | 4 | public: 5 | TimeMap() { 6 | 7 | } 8 | 9 | void set(string key, string value, int timestamp) { 10 | m[key][timestamp] = value; 11 | } 12 | 13 | string get(string key, int timestamp) { 14 | 15 | auto it = m[key].upper_bound(timestamp); 16 | 17 | if(it == m[key].begin()) 18 | return ""; 19 | else 20 | return prev(it)->second; 21 | } 22 | }; 23 | 24 | /** 25 | * Your TimeMap object will be instantiated and called as such: 26 | * TimeMap* obj = new TimeMap(); 27 | * obj->set(key,value,timestamp); 28 | * string param_2 = obj->get(key,timestamp); 29 | */ -------------------------------------------------------------------------------- /981-time-based-key-value-store/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LeetCode 2 | # This Repo made for save every problem solved. 3 | 4 | ## The Link of the Extension LeetHub 5 | --------------------------------------------------------------------------------