├── leetcode-75.png ├── 136-single-number ├── single-number.cpp └── README.md ├── 338-counting-bits ├── counting-bits.cpp └── README.md ├── README.md ├── 1236-n-th-tribonacci-number ├── n-th-tribonacci-number.cpp └── README.md ├── 1833-find-the-highest-altitude ├── find-the-highest-altitude.cpp └── README.md ├── 392-is-subsequence ├── is-subsequence.cpp └── README.md ├── 747-min-cost-climbing-stairs ├── min-cost-climbing-stairs.cpp └── README.md ├── 806-domino-and-tromino-tiling ├── domino-and-tromino-tiling.cpp └── README.md ├── 283-move-zeroes ├── move-zeroes.cpp └── README.md ├── 714-best-time-to-buy-and-sell-stock-with-transaction-fee ├── best-time-to-buy-and-sell-stock-with-transaction-fee.cpp └── README.md ├── 1441-minimum-flips-to-make-a-or-b-equal-to-c ├── minimum-flips-to-make-a-or-b-equal-to-c.cpp └── README.md ├── 11-container-with-most-water ├── container-with-most-water.cpp └── README.md ├── 1894-merge-strings-alternately ├── merge-strings-alternately.cpp └── README.md ├── 724-find-pivot-index ├── find-pivot-index.cpp └── README.md ├── 62-unique-paths ├── unique-paths.cpp └── README.md ├── 969-number-of-recent-calls ├── number-of-recent-calls.cpp └── README.md ├── 1319-unique-number-of-occurrences ├── unique-number-of-occurrences.cpp └── README.md ├── 1528-kids-with-the-greatest-number-of-candies ├── kids-with-the-greatest-number-of-candies.cpp └── README.md ├── 643-maximum-average-subarray-i ├── maximum-average-subarray-i.cpp └── README.md ├── 198-house-robber ├── house-robber.cpp └── README.md ├── 238-product-of-array-except-self ├── product-of-array-except-self.cpp └── README.md ├── 1250-longest-common-subsequence ├── longest-common-subsequence.cpp └── README.md ├── 215-kth-largest-element-in-an-array ├── kth-largest-element-in-an-array.cpp └── README.md ├── 435-non-overlapping-intervals ├── non-overlapping-intervals.cpp └── README.md ├── 162-find-peak-element ├── find-peak-element.cpp └── README.md ├── 2470-removing-stars-from-a-string ├── removing-stars-from-a-string.cpp └── README.md ├── 345-reverse-vowels-of-a-string ├── reverse-vowels-of-a-string.cpp └── README.md ├── 104-maximum-depth-of-binary-tree ├── maximum-depth-of-binary-tree.cpp └── README.md ├── 937-online-stock-span ├── online-stock-span.cpp └── README.md ├── 334-increasing-triplet-subsequence ├── increasing-triplet-subsequence.cpp └── README.md ├── 1146-greatest-common-divisor-of-strings ├── greatest-common-divisor-of-strings.cpp └── README.md ├── 605-can-place-flowers ├── can-place-flowers.cpp └── README.md ├── 1046-max-consecutive-ones-iii ├── max-consecutive-ones-iii.cpp └── README.md ├── 1567-maximum-number-of-vowels-in-a-substring-of-given-length ├── maximum-number-of-vowels-in-a-substring-of-given-length.cpp └── README.md ├── 739-daily-temperatures ├── daily-temperatures.cpp └── README.md ├── 216-combination-sum-iii ├── combination-sum-iii.cpp └── README.md ├── 452-minimum-number-of-arrows-to-burst-balloons ├── minimum-number-of-arrows-to-burst-balloons.cpp └── README.md ├── 72-edit-distance ├── edit-distance.cpp └── README.md ├── 374-guess-number-higher-or-lower ├── guess-number-higher-or-lower.cpp └── README.md ├── 443-string-compression ├── string-compression.cpp └── README.md ├── 907-koko-eating-bananas ├── koko-eating-bananas.cpp └── README.md ├── 783-search-in-a-binary-search-tree ├── search-in-a-binary-search-tree.cpp └── README.md ├── 1586-longest-subarray-of-1s-after-deleting-one-element ├── longest-subarray-of-1s-after-deleting-one-element.cpp └── README.md ├── 871-keys-and-rooms ├── keys-and-rooms.cpp └── README.md ├── 1777-determine-if-two-strings-are-close ├── determine-if-two-strings-are-close.cpp └── README.md ├── 1224-minimum-falling-path-sum-ii ├── minimum-falling-path-sum-ii.cpp └── README.md ├── 1392-find-the-difference-of-two-arrays ├── find-the-difference-of-two-arrays.cpp └── README.md ├── 2216-delete-the-middle-node-of-a-linked-list ├── delete-the-middle-node-of-a-linked-list.cpp └── README.md ├── 2392-successful-pairs-of-spells-and-potions ├── successful-pairs-of-spells-and-potions.cpp └── README.md ├── 2413-smallest-number-in-infinite-set ├── smallest-number-in-infinite-set.cpp └── README.md ├── 328-odd-even-linked-list ├── odd-even-linked-list.cpp └── README.md ├── 206-reverse-linked-list ├── reverse-linked-list.cpp └── README.md ├── 236-lowest-common-ancestor-of-a-binary-tree ├── lowest-common-ancestor-of-a-binary-tree.cpp └── README.md ├── 1544-count-good-nodes-in-binary-tree ├── count-good-nodes-in-binary-tree.cpp └── README.md ├── 547-number-of-provinces ├── number-of-provinces.cpp └── README.md ├── 735-asteroid-collision ├── asteroid-collision.cpp └── README.md ├── 151-reverse-words-in-a-string ├── reverse-words-in-a-string.cpp └── README.md ├── 1798-max-number-of-k-sum-pairs ├── max-number-of-k-sum-pairs.cpp └── README.md ├── 17-letter-combinations-of-a-phone-number ├── letter-combinations-of-a-phone-number.cpp └── README.md ├── 904-leaf-similar-trees ├── leaf-similar-trees.cpp └── README.md ├── 2636-maximum-subsequence-score ├── maximum-subsequence-score.cpp └── README.md ├── 1576-reorder-routes-to-make-all-paths-lead-to-the-city-zero ├── reorder-routes-to-make-all-paths-lead-to-the-city-zero.cpp └── README.md ├── 2553-total-cost-to-hire-k-workers └── total-cost-to-hire-k-workers.cpp ├── 199-binary-tree-right-side-view ├── binary-tree-right-side-view.cpp └── README.md ├── 450-delete-node-in-a-bst ├── delete-node-in-a-bst.cpp └── README.md ├── 2428-equal-row-and-column-pairs ├── equal-row-and-column-pairs.cpp └── README.md ├── 1474-longest-zigzag-path-in-a-binary-tree ├── longest-zigzag-path-in-a-binary-tree.cpp └── README.md ├── 2236-maximum-twin-sum-of-a-linked-list ├── maximum-twin-sum-of-a-linked-list.cpp └── README.md ├── 437-path-sum-iii ├── path-sum-iii.cpp └── README.md ├── 1116-maximum-level-sum-of-a-binary-tree ├── README.md └── maximum-level-sum-of-a-binary-tree.cpp ├── 394-decode-string ├── decode-string.cpp └── README.md ├── 208-implement-trie-prefix-tree ├── implement-trie-prefix-tree.cpp └── README.md ├── 1036-rotting-oranges ├── rotting-oranges.cpp └── README.md ├── 2038-nearest-exit-from-entrance-in-maze └── nearest-exit-from-entrance-in-maze.cpp ├── 399-evaluate-division ├── evaluate-division.cpp └── README.md ├── 1397-search-suggestions-system ├── README.md └── search-suggestions-system.cpp └── 649-dota2-senate └── README.md /leetcode-75.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ychaaibi/leetcode-75/HEAD/leetcode-75.png -------------------------------------------------------------------------------- /136-single-number/single-number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int singleNumber(vector& nums) { 4 | int ans = 0; 5 | 6 | for ( int i=0; i countBits(int n) { 4 | vector v(n + 1, 0); 5 | 6 | for ( int i=0; i<=n; i++ ) 7 | v[i] = __builtin_popcount(i); 8 | return (v); 9 | } 10 | }; -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
yjs leetcode icon
2 |

LeetCode

3 |

Ace Coding Interview with 75 Qs 🧩🧠

4 | 5 | -------------------------------------------------------------------------------- /1236-n-th-tribonacci-number/n-th-tribonacci-number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int tribonacci(int n) { 4 | vector dp( n + 3, 0 ); 5 | 6 | dp[0] = 0, dp[1] = dp[2] = 1; 7 | 8 | for ( int i=3; i<=n; i++ ) 9 | dp[i] = dp[i - 1] + dp[i - 2] + dp[i - 3]; 10 | return (dp[n]); 11 | } 12 | }; -------------------------------------------------------------------------------- /1833-find-the-highest-altitude/find-the-highest-altitude.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int largestAltitude(vector& gain) { 4 | int point = 0; 5 | int heighest = 0; 6 | 7 | for ( int i=0; i& cost) { 4 | vector dp( cost.size() ); 5 | 6 | dp[0] = cost[0], dp[1] = cost[1]; 7 | for ( int i=2; i dp(n + 4); 7 | 8 | dp[1] = 1; 9 | dp[2] = 2; 10 | dp[3] = 5; 11 | 12 | for ( int i=4; i<=n; i++ ) 13 | dp[i] = ( ( dp[i - 1] * 2 ) % MOD + dp[i - 3] ) % MOD; 14 | 15 | return ( dp[n] ); 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /283-move-zeroes/move-zeroes.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void moveZeroes(vector& nums) { 4 | int p1 = 0, p2 = 0; 5 | 6 | while ( p2 < nums.size() ) 7 | { 8 | if ( nums[p1] != 0 ) 9 | p1++, p2 = max(p1, p2); 10 | else if ( nums[p2] == 0 ) 11 | p2++; 12 | else 13 | swap( nums[p2++], nums[p1++] ); 14 | } 15 | } 16 | }; -------------------------------------------------------------------------------- /714-best-time-to-buy-and-sell-stock-with-transaction-fee/best-time-to-buy-and-sell-stock-with-transaction-fee.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProfit(vector& prices, int fee) { 4 | int sell1=0, sell2=-5 * 1e5 - 1, sell_day; 5 | 6 | for ( int i=0; i>= 1, b >>= 1; c >>= 1; 13 | } 14 | 15 | return ( ans ); 16 | } 17 | }; -------------------------------------------------------------------------------- /11-container-with-most-water/container-with-most-water.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxArea(vector& height) { 4 | int ans = 0, p1 = 0, p2 = height.size() - 1; 5 | 6 | while ( p1 < p2 ) 7 | { 8 | ans = max(min(height[p1], height[p2]) * ( p2 - p1 ), ans ); 9 | 10 | if ( height[p1] < height[p2] ) 11 | p1++; 12 | else 13 | p2--; 14 | } 15 | return ( ans ); 16 | } 17 | }; -------------------------------------------------------------------------------- /1894-merge-strings-alternately/merge-strings-alternately.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string mergeAlternately(string word1, string word2) { 4 | string ans(word1.length() + word2.length(), ' '); 5 | for (int i=0,j=0; i& nums) { 4 | int left = 0, right = 0; 5 | 6 | for ( int i=0; i> dp(n, vector(m,0)); 5 | 6 | for ( int i=0; i q; 4 | 5 | RecentCounter() { 6 | 7 | } 8 | 9 | int ping(int t) { 10 | while ( q.size() and q.front() < t - 3000 ) 11 | q.pop(); 12 | 13 | q.push( t ); 14 | return ( q.size()); 15 | } 16 | }; 17 | 18 | /** 19 | * Your RecentCounter object will be instantiated and called as such: 20 | * RecentCounter* obj = new RecentCounter(); 21 | * int param_1 = obj->ping(t); 22 | */ -------------------------------------------------------------------------------- /1319-unique-number-of-occurrences/unique-number-of-occurrences.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool uniqueOccurrences(vector& arr) { 4 | unordered_map count; 5 | unordered_map hash; 6 | 7 | for ( int i=0; i 1 ) 15 | return ( false ); 16 | } 17 | 18 | return ( true ); 19 | } 20 | }; -------------------------------------------------------------------------------- /1528-kids-with-the-greatest-number-of-candies/kids-with-the-greatest-number-of-candies.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector kidsWithCandies(vector& candies, int extraCandies) { 4 | int mx = 0; 5 | 6 | for ( int i=0; i ans( candies.size(), false) ; 10 | 11 | for ( int i=0; i= mx ); 13 | 14 | return ( ans ); 15 | } 16 | }; -------------------------------------------------------------------------------- /643-maximum-average-subarray-i/maximum-average-subarray-i.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | double findMaxAverage(vector& nums, int k) { 4 | int sum = 0; 5 | 6 | double ans; 7 | 8 | for ( int i=0; i& nums) { 4 | vector dp( nums.size( ) ); 5 | 6 | if ( nums.size() == 1 ) 7 | return ( nums[0] ); 8 | 9 | dp[0] = nums[0], dp[1] = nums[1]; 10 | 11 | for ( int i=2; i=0; j-- ) 16 | dp[i] = max( dp[i], nums[i] + dp[j] ); 17 | } 18 | 19 | 20 | return ( max( dp[dp.size() - 1], dp[dp.size() - 2] ) ); 21 | } 22 | }; -------------------------------------------------------------------------------- /238-product-of-array-except-self/product-of-array-except-self.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector productExceptSelf(vector& nums) { 4 | vector ans( nums.size(), 1); 5 | 6 | ans.back() = nums.back(); 7 | 8 | for ( int i=nums.size() - 2; i>=0; i-- ) 9 | ans[i] = ans[i + 1] * nums[i]; 10 | 11 | int mult = 1; 12 | 13 | for ( int i=0; i > dp(text1.length() + 1, vector(text2.length() + 1, 0)); 5 | 6 | for (int i=0; i& nums, int k) { 4 | priority_queue, greater> pq; 5 | 6 | for ( int i=0; i>& intervals) { 4 | sort(intervals.begin(), intervals.end()); 5 | 6 | int ans = 0; 7 | int right = intervals[0][1]; 8 | 9 | for ( int i=1; i= right ) 12 | right = intervals[i][1]; 13 | else 14 | { 15 | ans++; 16 | right = min(right, intervals[i][1]); 17 | } 18 | } 19 | 20 | return ( ans ); 21 | } 22 | }; -------------------------------------------------------------------------------- /162-find-peak-element/find-peak-element.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findPeakElement(vector& nums) { 4 | int left=0, right=nums.size() - 1; 5 | 6 | while ( left < right ) 7 | { 8 | int md = ( left + right ) / 2; 9 | 10 | if ( ( md == nums.size() - 1 or nums[md] > nums[md + 1] ) 11 | and ( md == 0 or nums[md] > nums[md - 1] ) ) 12 | return ( md ); 13 | else if ( !md or nums[md] > nums[md - 1] ) 14 | left = md + 1; 15 | else 16 | right = md; 17 | } 18 | 19 | return ( left ); 20 | } 21 | }; -------------------------------------------------------------------------------- /2470-removing-stars-from-a-string/removing-stars-from-a-string.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string removeStars(string s) { 4 | stack st; 5 | 6 | for ( int i=0; i=0; i-- ) 17 | { 18 | ans[i] = st.top(); 19 | st.pop(); 20 | } 21 | 22 | return ( ans ); 23 | } 24 | }; -------------------------------------------------------------------------------- /345-reverse-vowels-of-a-string/reverse-vowels-of-a-string.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string vowels; 4 | 5 | bool is_vowel( char c ) 6 | { 7 | return ( vowels.find(c) != string::npos ); 8 | } 9 | 10 | string reverseVowels(string s) { 11 | vowels = "aeiouAEIOU"; 12 | 13 | int p1=0, p2=s.length() - 1; 14 | 15 | while ( p1 < p2 ) 16 | { 17 | if ( !is_vowel(s[p1]) ) 18 | p1++; 19 | else if ( !is_vowel(s[p2]) ) 20 | p2--; 21 | else 22 | swap( s[p2--], s[p1++] ); 23 | } 24 | return ( s ); 25 | } 26 | }; -------------------------------------------------------------------------------- /104-maximum-depth-of-binary-tree/maximum-depth-of-binary-tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | int maxDepth(TreeNode* root) { 15 | if ( !root ) 16 | return (0); 17 | 18 | return ( 1 + max(maxDepth(root->left), maxDepth(root->right) ) ); 19 | } 20 | }; -------------------------------------------------------------------------------- /937-online-stock-span/online-stock-span.cpp: -------------------------------------------------------------------------------- 1 | class StockSpanner { 2 | public: 3 | stack> val; 4 | 5 | StockSpanner() { 6 | 7 | 8 | } 9 | 10 | int next(int price) { 11 | int _val = 1; 12 | 13 | while ( val.size() and val.top().second <= price ) 14 | { 15 | _val += val.top().first; 16 | val.pop(); 17 | } 18 | 19 | val.push(make_pair(_val, price)); 20 | 21 | return ( _val ); 22 | } 23 | }; 24 | 25 | /** 26 | * Your StockSpanner object will be instantiated and called as such: 27 | * StockSpanner* obj = new StockSpanner(); 28 | * int param_1 = obj->next(price); 29 | */ -------------------------------------------------------------------------------- /334-increasing-triplet-subsequence/increasing-triplet-subsequence.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool increasingTriplet(vector& nums) { 4 | int i=-1, j=-1, k=-1; 5 | 6 | for ( int p=0; p nums[i] and j == -1 ) 13 | j = p; 14 | else if ( nums[p] > nums[i] and j != -1 and nums[p] < nums[j] ) 15 | j = p; 16 | else if ( j != -1 and nums[p] > nums[j] ) 17 | return ( true ); 18 | } 19 | 20 | return ( false ); 21 | } 22 | }; -------------------------------------------------------------------------------- /1146-greatest-common-divisor-of-strings/greatest-common-divisor-of-strings.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string gcdOfStrings(string str1, string str2) { 4 | int g = __gcd( str1.length(), str2.length() ); 5 | 6 | string ans = str1.substr(0, g); 7 | 8 | cout << ans << endl; 9 | string s1, s2; 10 | 11 | s1.reserve( s1.length() ); 12 | s2.reserve( s2.length() ); 13 | 14 | for ( int i=0; i& flowerbed, int n) { 4 | int c = 0; 5 | 6 | if ( !flowerbed[0] and ( ( flowerbed.size() > 1 and !flowerbed[1] ) or flowerbed.size() == 1 ) ) 7 | flowerbed[0] = 1, n--; 8 | 9 | for ( int i=0; i& nums, int k) { 4 | int p1 = 0, p2 = 0, ans = 0; 5 | 6 | while ( p2 < nums.size() ) 7 | { 8 | if ( nums[p2] ) 9 | { 10 | ans = max(p2++ - p1 + 1, ans); 11 | continue ; 12 | } 13 | 14 | if ( k > 0 ) 15 | ans = max(++p2 - p1, ans), k--; 16 | else 17 | { 18 | while ( p1 < nums.size() and nums[p1] ) 19 | p1++; 20 | 21 | p1++, p2++, p2 = max(p2, p1); 22 | } 23 | } 24 | 25 | return ( ans ); 26 | } 27 | }; -------------------------------------------------------------------------------- /1567-maximum-number-of-vowels-in-a-substring-of-given-length/maximum-number-of-vowels-in-a-substring-of-given-length.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string vowels; 4 | 5 | bool is_vowel( char c ) 6 | { 7 | return ( vowels.find( c ) != string::npos ); 8 | } 9 | 10 | int maxVowels(string s, int k) { 11 | vowels = "aeoiu"; 12 | 13 | int curr = 0, ans = 0; 14 | 15 | for ( int i=0; i dailyTemperatures(vector& temperatures) { 4 | vector ans(temperatures.size(), 0); 5 | stack st; 6 | 7 | for ( int i=0; i= temperatures[i] ) 10 | st.push( i ); 11 | else 12 | { 13 | while ( !st.empty() and temperatures[st.top()] < temperatures[i] ) 14 | { 15 | ans[st.top()] = i - st.top(); 16 | st.pop(); 17 | } 18 | st.push( i ); 19 | } 20 | } 21 | 22 | return ( ans ); 23 | } 24 | }; -------------------------------------------------------------------------------- /216-combination-sum-iii/combination-sum-iii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> ans; 4 | 5 | void combination( vector& curr, int index, int k, int n) 6 | { 7 | if ( k == 0 ) 8 | { 9 | if ( !n ) 10 | ans.push_back( curr ); 11 | return ; 12 | } 13 | 14 | if ( n < 0 or index == 10 ) 15 | return ; 16 | 17 | curr.push_back( index ); 18 | combination( curr, index + 1, k - 1, n - index ); 19 | curr.pop_back(); 20 | combination( curr, index + 1, k, n); 21 | } 22 | 23 | vector> combinationSum3(int k, int n) { 24 | vector curr; 25 | 26 | combination( curr, 1, k, n ); 27 | return ( ans ); 28 | } 29 | }; -------------------------------------------------------------------------------- /452-minimum-number-of-arrows-to-burst-balloons/minimum-number-of-arrows-to-burst-balloons.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findMinArrowShots(vector>& points) { 4 | sort(points.begin(), points.end()); 5 | 6 | int mn = points[0][0], mx = points[0][1]; 7 | 8 | int ans = 1; 9 | 10 | for ( int i=1; i= mn and points[i][0] <= mx ) 13 | or (points[i][1] >= mn and points[i][1] <= mx ) ) 14 | mn = max(mn, points[i][0] ), mx = min(mx, points[i][1] ); 15 | else 16 | { 17 | mn = points[i][0], mx = points[i][1]; 18 | ans++; 19 | } 20 | } 21 | 22 | return ( ans ); 23 | } 24 | }; -------------------------------------------------------------------------------- /72-edit-distance/edit-distance.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minDistance(string word1, string word2) { 4 | vector> dp( word1.length() + 1, vector( word2.length() + 1, -1) ); 5 | 6 | for ( int i=0; i<=word1.length(); i++ ) 7 | dp[i][0] = i; 8 | 9 | for ( int j=0; j<=word2.length(); j++ ) 10 | dp[0][j] = j; 11 | 12 | for ( int i=1; i<=word1.length(); i++ ) 13 | { 14 | for ( int j=1; j<=word2.length(); j++ ) 15 | { 16 | if ( word1[i - 1] == word2[j - 1] ) dp[i][j] = 0 + dp[i - 1][j - 1]; 17 | else dp[i][j] = 1 + min( dp[i - 1][j], min( dp[i][j - 1], dp[i - 1][j - 1] ) ); 18 | } 19 | } 20 | return dp.back().back(); 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /374-guess-number-higher-or-lower/guess-number-higher-or-lower.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Forward declaration of guess API. 3 | * @param num your guess 4 | * @return -1 if num is higher than the picked number 5 | * 1 if num is lower than the picked number 6 | * otherwise return 0 7 | * int guess(int num); 8 | */ 9 | 10 | class Solution { 11 | public: 12 | int guessNumber(int n) { 13 | long long left=1, right=(long long)n + 1; 14 | 15 | while ( left < right ) 16 | { 17 | long long md = ( right - left ) / 2 + left; 18 | 19 | if ( !guess(md) ) 20 | return ( md ); 21 | else if ( guess(md) == 1 ) 22 | left = md + 1; 23 | else 24 | right = md; 25 | } 26 | return (-1); 27 | } 28 | }; -------------------------------------------------------------------------------- /443-string-compression/string-compression.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int compress(vector& chars) { 4 | int p1=0, p2=0, modif=0; 5 | 6 | while ( p2 < chars.size() ) 7 | { 8 | if ( chars[p2] == chars[p1] ) 9 | p2++; 10 | 11 | if ( p2 == chars.size() or chars[p2] != chars[p1] ) 12 | { 13 | chars[modif++] = chars[p1]; 14 | 15 | if ( p2 - p1 > 1 ) 16 | { 17 | string num = to_string(p2 - p1); 18 | for ( int i=0; i& piles, int k ) 4 | { 5 | int hours = 0; 6 | 7 | for ( int i=0; i& piles, int h) { 14 | int right=piles[0], left = 1; 15 | 16 | for ( int i=0; i h ) 24 | left = md + 1; 25 | else 26 | right = md; 27 | } 28 | 29 | return ( left ); 30 | } 31 | }; -------------------------------------------------------------------------------- /783-search-in-a-binary-search-tree/search-in-a-binary-search-tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : 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* searchBST(TreeNode* root, int val) { 15 | if ( !root ) 16 | return ( NULL ); 17 | 18 | if ( root->val == val ) 19 | return ( root ); 20 | 21 | if ( root->val > val ) 22 | return searchBST( root->left, val ); 23 | else 24 | return searchBST( root->right, val ); 25 | } 26 | }; -------------------------------------------------------------------------------- /1586-longest-subarray-of-1s-after-deleting-one-element/longest-subarray-of-1s-after-deleting-one-element.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int longestSubarray(vector& nums) { 4 | int ans = 0; 5 | 6 | int c1 = 0, c2 = 0; 7 | 8 | for ( int i=0; i>& rooms) { 4 | vector vis(rooms.size(), false); 5 | 6 | vis[0] = true; 7 | 8 | queue q; 9 | 10 | q.push( 0 ); 11 | 12 | while ( !q.empty() ) 13 | { 14 | int curr = q.front(); 15 | 16 | q.pop(); 17 | 18 | for ( int i=0; i hash1(26,0), hash2(26,0); 5 | 6 | if ( word1.length() != word2.length() ) 7 | return ( false ); 8 | 9 | for ( int i=0; i>& grid) { 4 | vector> dp( grid.size(), vector(grid.size(), INT_MAX) ); 5 | 6 | dp.front() = grid.front(); 7 | 8 | for ( int i=1; i> findDifference(vector& nums1, vector& nums2) { 4 | vector> ans(2); 5 | 6 | ans[0].reserve(nums1.size()); 7 | ans[1].reserve(nums2.size()); 8 | 9 | unordered_set hash1, hash2; 10 | 11 | for ( int i=0; inext ) 17 | return ( NULL ); 18 | 19 | while ( hare->next and hare->next->next and hare->next->next->next ) 20 | { 21 | hare = hare->next->next; 22 | turtle = turtle->next; 23 | } 24 | 25 | turtle->next = turtle->next->next; 26 | return ( head ); 27 | } 28 | }; 29 | 30 | // -------------------------------------------------------------------------------- /2392-successful-pairs-of-spells-and-potions/successful-pairs-of-spells-and-potions.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int find( vector& potions, long long success, long long spell ) 4 | { 5 | int left = 0, right = potions.size(); 6 | 7 | while ( left < right ) 8 | { 9 | int md = ( left + right ) / 2; 10 | if ( potions[md] * spell < success ) 11 | left = md + 1; 12 | else 13 | right = md; 14 | } 15 | return ( potions.size() - left ); 16 | } 17 | 18 | vector successfulPairs(vector& spells, vector& potions, long long success) { 19 | sort(potions.begin(), potions.end()); 20 | 21 | vector ans(spells.size(), 0); 22 | 23 | for ( int i=0; i hash; 4 | priority_queue, greater> pq; 5 | 6 | SmallestInfiniteSet() { 7 | hash.resize(1001, true); 8 | 9 | for ( int i=1; i<=1000; i++ ) 10 | pq.push( i ); 11 | } 12 | 13 | int popSmallest() { 14 | int val = pq.top(); 15 | 16 | hash[val] = false; 17 | pq.pop(); 18 | 19 | return ( val ); 20 | } 21 | 22 | void addBack(int num) { 23 | if ( !hash[num] ) 24 | { 25 | hash[num] = true; 26 | pq.push( num ); 27 | } 28 | } 29 | }; 30 | 31 | /** 32 | * Your SmallestInfiniteSet object will be instantiated and called as such: 33 | * SmallestInfiniteSet* obj = new SmallestInfiniteSet(); 34 | * int param_1 = obj->popSmallest(); 35 | * obj->addBack(num); 36 | */ -------------------------------------------------------------------------------- /328-odd-even-linked-list/odd-even-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* oddEvenList(ListNode* head) { 14 | if ( !head or !head->next ) 15 | return ( head ); 16 | 17 | ListNode *odd = head, *even_head = head->next, *even = head->next; 18 | 19 | while ( even and even->next ) 20 | { 21 | odd->next = odd->next->next; 22 | even->next = odd->next->next; 23 | 24 | odd = odd->next; 25 | even = even->next; 26 | } 27 | 28 | odd->next = even_head; 29 | 30 | return ( head ); 31 | 32 | } 33 | }; -------------------------------------------------------------------------------- /206-reverse-linked-list/reverse-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* reverseList(ListNode* head) { 14 | ListNode* temp = NULL; 15 | 16 | if ( !head or !head->next ) 17 | return ( head ); 18 | 19 | ListNode *last_head = NULL; 20 | 21 | while ( head ) 22 | { 23 | last_head = head; 24 | 25 | ListNode* next = head->next; 26 | 27 | head->next = temp; 28 | 29 | temp = head; 30 | head = next; 31 | 32 | } 33 | 34 | return ( last_head ); 35 | } 36 | }; -------------------------------------------------------------------------------- /236-lowest-common-ancestor-of-a-binary-tree/lowest-common-ancestor-of-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(int x) : val(x), left(NULL), right(NULL) {} 8 | * }; 9 | */ 10 | class Solution { 11 | public: 12 | TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { 13 | if ( !root ) 14 | return ( NULL ); 15 | 16 | if ( root == p or root == q ) 17 | return (root); 18 | 19 | TreeNode* left = lowestCommonAncestor( root->left, p, q ); 20 | TreeNode* right = lowestCommonAncestor( root->right, p, q); 21 | 22 | if ( left and right ) 23 | return ( root ); 24 | else if ( left) 25 | return ( left ); 26 | else if ( right ) 27 | return ( right ); 28 | 29 | return ( NULL ); 30 | } 31 | }; -------------------------------------------------------------------------------- /1544-count-good-nodes-in-binary-tree/count-good-nodes-in-binary-tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | int ans; 15 | 16 | void traverse(TreeNode* root, int val ) 17 | { 18 | if ( !root ) 19 | return ; 20 | 21 | if ( root->val >= val ) 22 | ans++; 23 | traverse(root->left, max(val, root->val) ); 24 | traverse(root->right, max(val, root->val) ); 25 | } 26 | 27 | int goodNodes(TreeNode* root) { 28 | ans = 0; 29 | 30 | traverse( root, root->val ); 31 | return ( ans ); 32 | } 33 | }; -------------------------------------------------------------------------------- /547-number-of-provinces/number-of-provinces.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findCircleNum(vector>& isConnected) { 4 | int n = isConnected.size(); 5 | 6 | vector vis( n, false ); 7 | 8 | int ans = 0; 9 | for ( int i=0; i q; 17 | 18 | q.push( i ); 19 | vis[i] = true; 20 | 21 | while ( !q.empty() ) 22 | { 23 | int curr = q.front(); 24 | 25 | q.pop(); 26 | for ( int c=0; c asteroidCollision(vector& asteroids) { 4 | stack st; 5 | 6 | for ( int i=0; i 0 ) 9 | st.push( asteroids[i] ); 10 | else 11 | { 12 | while ( !st.empty() and st.top() > 0 and abs(st.top()) < abs(asteroids[i]) ) 13 | st.pop(); 14 | 15 | if ( st.empty() or st.top() < 0 ) 16 | st.push( asteroids[i] ); 17 | else if ( abs(st.top()) == abs( asteroids[i] ) ) 18 | st.pop(); 19 | 20 | } 21 | } 22 | 23 | vector ans(st.size()); 24 | 25 | for ( int i=ans.size() - 1; i>=0; i-- ) 26 | { 27 | ans[i] = st.top(); 28 | st.pop(); 29 | } 30 | 31 | return ( ans ); 32 | } 33 | }; -------------------------------------------------------------------------------- /151-reverse-words-in-a-string/reverse-words-in-a-string.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string reverseWords(string s) { 4 | string ans; 5 | 6 | ans.reserve(s.length()); 7 | 8 | reverse(s.begin(), s.end()); 9 | 10 | for ( int i=0; i& nums, int k) { 4 | unordered_map mp; 5 | 6 | for ( int i=0; isecond); 21 | 22 | ans += ops; 23 | 24 | count -= ops; 25 | next->second -= ops; 26 | } 27 | else 28 | { 29 | ans += (count / 2); 30 | count = count % 2; 31 | } 32 | } 33 | 34 | return ( ans ); 35 | } 36 | }; -------------------------------------------------------------------------------- /17-letter-combinations-of-a-phone-number/letter-combinations-of-a-phone-number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector tele; 4 | vector ans; 5 | 6 | void dfs(string& digits, string& curr, int index ) 7 | { 8 | if ( index == digits.size() ) 9 | { 10 | ans.push_back( curr ); 11 | return ; 12 | } 13 | 14 | for ( int i=0; i letterCombinations(string digits) { 23 | tele = { string("abc"), string("def"), string("ghi"), string("jkl"), string("mno"), string("pqrs"), string("tuv"), string("wxyz") }; 24 | 25 | if ( digits.empty() ) 26 | return ( vector() ); 27 | 28 | string curr; 29 | 30 | dfs( digits, curr, 0 ); 31 | 32 | return ( ans ); 33 | } 34 | }; -------------------------------------------------------------------------------- /904-leaf-similar-trees/leaf-similar-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 | string s1, s2; 15 | 16 | void traverse( TreeNode* root, string& s ) 17 | { 18 | if ( !root ) 19 | return ; 20 | 21 | if ( !root->left and !root->right ) 22 | { 23 | s += ";"; 24 | s += to_string( root->val ); 25 | return ; 26 | } 27 | traverse( root->left, s ); 28 | traverse( root->right, s ); 29 | } 30 | 31 | bool leafSimilar(TreeNode* root1, TreeNode* root2) { 32 | traverse( root1, s1 ); 33 | traverse( root2, s2 ); 34 | 35 | return ( s1 == s2 ); 36 | } 37 | }; -------------------------------------------------------------------------------- /283-move-zeroes/README.md: -------------------------------------------------------------------------------- 1 |

Move Zeroes

Difficulty: Easy

Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements.

2 | 3 |

Note that you must do this in-place without making a copy of the array.

4 | 5 |

 

6 |

Example 1:

7 |
Input: nums = [0,1,0,3,12]
 8 | Output: [1,3,12,0,0]
 9 | 

Example 2:

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

 

14 |

Constraints:

15 | 16 |
    17 |
  • 1 <= nums.length <= 104
  • 18 |
  • -231 <= nums[i] <= 231 - 1
  • 19 |
20 | 21 |

 

22 | Follow up: Could you minimize the total number of operations done? -------------------------------------------------------------------------------- /345-reverse-vowels-of-a-string/README.md: -------------------------------------------------------------------------------- 1 |

Reverse Vowels of a String

Difficulty: Easy

Given a string s, reverse only all the vowels in the string and return it.

2 | 3 |

The vowels are 'a', 'e', 'i', 'o', and 'u', and they can appear in both lower and upper cases, more than once.

4 | 5 |

 

6 |

Example 1:

7 |
Input: s = "hello"
 8 | Output: "holle"
 9 | 

Example 2:

10 |
Input: s = "leetcode"
11 | Output: "leotcede"
12 | 
13 |

 

14 |

Constraints:

15 | 16 |
    17 |
  • 1 <= s.length <= 3 * 105
  • 18 |
  • s consist of printable ASCII characters.
  • 19 |
20 | -------------------------------------------------------------------------------- /2636-maximum-subsequence-score/maximum-subsequence-score.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long long maxScore(vector& nums1, vector& nums2, int k) { 4 | long long ans = 0, sum = 0; 5 | 6 | vector> small( nums2.size() ); 7 | priority_queue, greater> pq; 8 | 9 | for ( int i=0; i=(int)nums1.size() - k; i-- ) 15 | { 16 | sum += nums1[small[i].second]; 17 | pq.push(nums1[small[i].second]); 18 | } 19 | 20 | ans = sum * small[nums1.size() - k].first; 21 | 22 | for ( int i=(int)nums1.size() - k - 1; i>=0; i-- ) 23 | { 24 | sum = sum - pq.top() + nums1[small[i].second]; 25 | 26 | pq.pop(); 27 | pq.push( nums1[small[i].second] ); 28 | 29 | ans = max(ans, sum * small[i].first); 30 | } 31 | 32 | return ( ans ); 33 | } 34 | }; -------------------------------------------------------------------------------- /1576-reorder-routes-to-make-all-paths-lead-to-the-city-zero/reorder-routes-to-make-all-paths-lead-to-the-city-zero.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int ans = 0; 4 | 5 | void dfs( int node, vector>>& graph, vector& vis ) 6 | { 7 | vis[node] = true; 8 | 9 | for ( int i=0; i < graph[node].size(); i++ ) 10 | { 11 | if ( vis[graph[node][i].first] ) 12 | continue ; 13 | 14 | vis[graph[node][i].first] = true; 15 | ans += graph[node][i].second; 16 | dfs( graph[node][i].first, graph, vis ); 17 | } 18 | } 19 | 20 | int minReorder(int n, vector>& connections) { 21 | vector>> graph( n ); 22 | 23 | for ( int i=0; i vis(n, false); 30 | 31 | dfs( 0, graph, vis ); 32 | 33 | 34 | return ( ans ); 35 | } 36 | }; -------------------------------------------------------------------------------- /2553-total-cost-to-hire-k-workers/total-cost-to-hire-k-workers.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long long totalCost(vector& costs, int k, int candidates) { 4 | int p1 = 0, p2 = costs.size() - 1; 5 | long long ans = 0; 6 | 7 | priority_queue, vector>, greater>> pq; 8 | 9 | for ( int i=0; i p2 ) 28 | continue ; 29 | 30 | if ( curr.second < p1 ) 31 | pq.push( make_pair( costs[p1], p1++ ) ); 32 | else 33 | pq.push( make_pair( costs[p2], p2-- ) ); 34 | 35 | } 36 | return ( ans ); 37 | } 38 | }; -------------------------------------------------------------------------------- /215-kth-largest-element-in-an-array/README.md: -------------------------------------------------------------------------------- 1 |

Kth Largest Element in an Array

Difficulty: Medium

Given an integer array nums and an integer k, return the kth largest element in the array.

2 | 3 |

Note that it is the kth largest element in the sorted order, not the kth distinct element.

4 | 5 |

Can you solve it without sorting?

6 | 7 |

 

8 |

Example 1:

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

Example 2:

12 |
Input: nums = [3,2,3,1,2,4,5,5,6], k = 4
13 | Output: 4
14 | 
15 |

 

16 |

Constraints:

17 | 18 |
    19 |
  • 1 <= k <= nums.length <= 105
  • 20 |
  • -104 <= nums[i] <= 104
  • 21 |
22 | -------------------------------------------------------------------------------- /104-maximum-depth-of-binary-tree/README.md: -------------------------------------------------------------------------------- 1 |

Maximum Depth of Binary Tree

Difficulty: Easy

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

2 | 3 |

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

4 | 5 |

 

6 |

Example 1:

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

Example 2:

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

 

21 |

Constraints:

22 | 23 |
    24 |
  • The number of nodes in the tree is in the range [0, 104].
  • 25 |
  • -100 <= Node.val <= 100
  • 26 |
27 | -------------------------------------------------------------------------------- /1236-n-th-tribonacci-number/README.md: -------------------------------------------------------------------------------- 1 |

N-th Tribonacci Number

Difficulty: Easy

The Tribonacci sequence Tn is defined as follows: 

2 | 3 |

T0 = 0, T1 = 1, T2 = 1, and Tn+3 = Tn + Tn+1 + Tn+2 for n >= 0.

4 | 5 |

Given n, return the value of Tn.

6 | 7 |

 

8 |

Example 1:

9 | 10 |
11 | Input: n = 4
12 | Output: 4
13 | Explanation:
14 | T_3 = 0 + 1 + 1 = 2
15 | T_4 = 1 + 1 + 2 = 4
16 | 
17 | 18 |

Example 2:

19 | 20 |
21 | Input: n = 25
22 | Output: 1389537
23 | 
24 | 25 |

 

26 |

Constraints:

27 | 28 |
    29 |
  • 0 <= n <= 37
  • 30 |
  • The answer is guaranteed to fit within a 32-bit integer, ie. answer <= 2^31 - 1.
  • 31 |
-------------------------------------------------------------------------------- /199-binary-tree-right-side-view/binary-tree-right-side-view.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 rightSideView(TreeNode* root) { 15 | vector ans; 16 | 17 | if ( !root ) 18 | return ( ans ); 19 | 20 | queue q; 21 | q.push( root ); 22 | 23 | while ( !q.empty() ) 24 | { 25 | int size = q.size(); 26 | 27 | ans.push_back( q.front()->val ); 28 | 29 | while ( size-- ) 30 | { 31 | TreeNode* curr = q.front(); 32 | 33 | q.pop(); 34 | 35 | if ( curr->right ) 36 | q.push( curr->right ); 37 | if ( curr->left ) 38 | q.push( curr->left ); 39 | } 40 | } 41 | 42 | return ( ans ); 43 | } 44 | }; -------------------------------------------------------------------------------- /136-single-number/README.md: -------------------------------------------------------------------------------- 1 |

Single Number

Difficulty: Easy

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

2 | 3 |

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

4 | 5 |

 

6 |

Example 1:

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

Example 2:

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

Example 3:

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

 

17 |

Constraints:

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

Binary Tree Right Side View

Difficulty: Medium

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

2 | 3 |

 

4 |

Example 1:

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

Example 2:

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

Example 3:

19 | 20 |
21 | Input: root = []
22 | Output: []
23 | 
24 | 25 |

 

26 |

Constraints:

27 | 28 |
    29 |
  • The number of nodes in the tree is in the range [0, 100].
  • 30 |
  • -100 <= Node.val <= 100
  • 31 |
32 | -------------------------------------------------------------------------------- /1319-unique-number-of-occurrences/README.md: -------------------------------------------------------------------------------- 1 |

Unique Number of Occurrences

Difficulty: 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 |
 7 | Input: arr = [1,2,2,1,1,3]
 8 | Output: true
 9 | Explanation: The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences.
10 | 11 |

Example 2:

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

Example 3:

19 | 20 |
21 | Input: arr = [-3,0,1,-3,1,1,1,-3,10,0]
22 | Output: true
23 | 
24 | 25 |

 

26 |

Constraints:

27 | 28 |
    29 |
  • 1 <= arr.length <= 1000
  • 30 |
  • -1000 <= arr[i] <= 1000
  • 31 |
32 | -------------------------------------------------------------------------------- /450-delete-node-in-a-bst/delete-node-in-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 | public: 14 | TreeNode* deleteNode(TreeNode* root, int key) { 15 | TreeNode* temp; 16 | 17 | if ( !root ) 18 | return ( root ); 19 | 20 | if ( root->val == key ) 21 | { 22 | TreeNode* left = root->left; 23 | TreeNode* right = root->right; 24 | 25 | if ( !left ) 26 | return ( right ); 27 | else if ( !right ) 28 | return ( left ); 29 | 30 | temp = left; 31 | while ( left->right ) 32 | left = left->right; 33 | 34 | left->right = right; 35 | 36 | return ( temp ); 37 | } 38 | 39 | if ( root->val > key ) 40 | root->left = deleteNode(root->left, key); 41 | else 42 | root->right = deleteNode(root->right, key); 43 | 44 | return ( root ); 45 | } 46 | }; -------------------------------------------------------------------------------- /643-maximum-average-subarray-i/README.md: -------------------------------------------------------------------------------- 1 |

Maximum Average Subarray I

Difficulty: Easy

You are given an integer array nums consisting of n elements, and an integer k.

2 | 3 |

Find a contiguous subarray whose length is equal to k that has the maximum average value and return this value. Any answer with a calculation error less than 10-5 will be accepted.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
 9 | Input: nums = [1,12,-5,-6,50,3], k = 4
10 | Output: 12.75000
11 | Explanation: Maximum average is (12 - 5 - 6 + 50) / 4 = 51 / 4 = 12.75
12 | 
13 | 14 |

Example 2:

15 | 16 |
17 | Input: nums = [5], k = 1
18 | Output: 5.00000
19 | 
20 | 21 |

 

22 |

Constraints:

23 | 24 |
    25 |
  • n == nums.length
  • 26 |
  • 1 <= k <= n <= 105
  • 27 |
  • -104 <= nums[i] <= 104
  • 28 |
29 | -------------------------------------------------------------------------------- /2428-equal-row-and-column-pairs/equal-row-and-column-pairs.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int equalPairs(vector>& grid) { 4 | unordered_map c; 5 | unordered_map r; 6 | 7 | for ( int i=0; isecond; 45 | } 46 | 47 | return ( ans ); 48 | } 49 | }; -------------------------------------------------------------------------------- /739-daily-temperatures/README.md: -------------------------------------------------------------------------------- 1 |

Daily Temperatures

Difficulty: 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 | -------------------------------------------------------------------------------- /1474-longest-zigzag-path-in-a-binary-tree/longest-zigzag-path-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 | public: 14 | int ans = 0; 15 | 16 | void traverse( TreeNode* root, int type, int curr ) 17 | { 18 | if ( !root ) 19 | return ; 20 | 21 | ans = max( ans, curr - 1 ); 22 | 23 | if ( type == 0 ) 24 | { 25 | traverse( root->left, 1, curr + 1); 26 | traverse( root->right, 2, curr + 1); 27 | } 28 | else if ( type == 1 ) 29 | { 30 | traverse( root->left, 1, 2); 31 | traverse( root->right, 2, curr + 1); 32 | } 33 | else 34 | { 35 | traverse( root->right, 2, 2); 36 | traverse( root->left, 1, curr + 1); 37 | } 38 | } 39 | 40 | int longestZigZag(TreeNode* root) { 41 | ans = 0; 42 | traverse( root, 0, 1 ); 43 | 44 | return ( ans ); 45 | } 46 | }; -------------------------------------------------------------------------------- /2236-maximum-twin-sum-of-a-linked-list/maximum-twin-sum-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 | int pairSum(ListNode* head) { 14 | ListNode* hare = head, *turtle = head; 15 | 16 | while ( hare and hare->next ) 17 | { 18 | hare = hare->next->next; 19 | turtle = turtle->next; 20 | } 21 | 22 | ListNode* prev_node = turtle, *next, *mid = turtle; 23 | 24 | turtle = turtle->next; 25 | 26 | while ( turtle ) 27 | { 28 | next = turtle->next; 29 | 30 | turtle->next = prev_node; 31 | 32 | prev_node = turtle; 33 | turtle = next; 34 | } 35 | 36 | ListNode* left = head, *right = prev_node; 37 | 38 | int ans = 0; 39 | 40 | while ( left != mid ) 41 | { 42 | ans = max(left->val + right->val, ans); 43 | left = left->next; 44 | right = right->next; 45 | } 46 | return (ans); 47 | 48 | } 49 | }; -------------------------------------------------------------------------------- /437-path-sum-iii/path-sum-iii.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | int ans; 15 | map val; 16 | 17 | void traverse( TreeNode* root, long long target, long long sum ) 18 | { 19 | if ( !root ) 20 | return ; 21 | 22 | if ( val.find( sum + root->val - target ) != val.end() ) 23 | ans += val[sum + root->val - target]; 24 | 25 | cout << sum + root->val << endl; 26 | val[sum + root->val]++; 27 | 28 | if ( root->left ) 29 | traverse( root->left, target, sum + root->val); 30 | 31 | if ( root->right ) 32 | traverse( root->right, target, sum + root->val); 33 | 34 | val[sum + root->val]--; 35 | } 36 | 37 | int pathSum(TreeNode* root, int targetSum) { 38 | if ( !root ) 39 | return (0); 40 | 41 | ans = 0; 42 | val[0] = 1; 43 | traverse( root, targetSum, 0); 44 | 45 | return ( ans ); 46 | } 47 | }; -------------------------------------------------------------------------------- /206-reverse-linked-list/README.md: -------------------------------------------------------------------------------- 1 |

Reverse Linked List

Difficulty: Easy

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

2 | 3 |

 

4 |

Example 1:

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

Example 2:

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

Example 3:

19 | 20 |
21 | Input: head = []
22 | Output: []
23 | 
24 | 25 |

 

26 |

Constraints:

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

 

34 |

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

35 | -------------------------------------------------------------------------------- /437-path-sum-iii/README.md: -------------------------------------------------------------------------------- 1 |

Path Sum III

Difficulty: Medium

Given the root of a binary tree and an integer targetSum, return the number of paths where the sum of the values along the path equals targetSum.

2 | 3 |

The path does not need to start or end at the root or a leaf, but it must go downwards (i.e., traveling only from parent nodes to child nodes).

4 | 5 |

 

6 |

Example 1:

7 | 8 |
 9 | Input: root = [10,5,-3,3,2,null,11,3,-2,null,1], targetSum = 8
10 | Output: 3
11 | Explanation: The paths that sum to 8 are shown.
12 | 
13 | 14 |

Example 2:

15 | 16 |
17 | Input: root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
18 | Output: 3
19 | 
20 | 21 |

 

22 |

Constraints:

23 | 24 |
    25 |
  • The number of nodes in the tree is in the range [0, 1000].
  • 26 |
  • -109 <= Node.val <= 109
  • 27 |
  • -1000 <= targetSum <= 1000
  • 28 |
29 | -------------------------------------------------------------------------------- /783-search-in-a-binary-search-tree/README.md: -------------------------------------------------------------------------------- 1 |

Search in a Binary Search Tree

Difficulty: Easy

You are given the root of a binary search tree (BST) and an integer val.

2 | 3 |

Find the node in the BST that the node's value equals val and return the subtree rooted with that node. If such a node does not exist, return null.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
 9 | Input: root = [4,2,7,1,3], val = 2
10 | Output: [2,1,3]
11 | 
12 | 13 |

Example 2:

14 | 15 |
16 | Input: root = [4,2,7,1,3], val = 5
17 | Output: []
18 | 
19 | 20 |

 

21 |

Constraints:

22 | 23 |
    24 |
  • The number of nodes in the tree is in the range [1, 5000].
  • 25 |
  • 1 <= Node.val <= 107
  • 26 |
  • root is a binary search tree.
  • 27 |
  • 1 <= val <= 107
  • 28 |
29 | -------------------------------------------------------------------------------- /605-can-place-flowers/README.md: -------------------------------------------------------------------------------- 1 |

Can Place Flowers

Difficulty: Easy

You have a long flowerbed in which some of the plots are planted, and some are not. However, flowers cannot be planted in adjacent plots.

2 | 3 |

Given an integer array flowerbed containing 0's and 1's, where 0 means empty and 1 means not empty, and an integer n, return true if n new flowers can be planted in the flowerbed without violating the no-adjacent-flowers rule and false otherwise.

4 | 5 |

 

6 |

Example 1:

7 |
Input: flowerbed = [1,0,0,0,1], n = 1
 8 | Output: true
 9 | 

Example 2:

10 |
Input: flowerbed = [1,0,0,0,1], n = 2
11 | Output: false
12 | 
13 |

 

14 |

Constraints:

15 | 16 |
    17 |
  • 1 <= flowerbed.length <= 2 * 104
  • 18 |
  • flowerbed[i] is 0 or 1.
  • 19 |
  • There are no two adjacent flowers in flowerbed.
  • 20 |
  • 0 <= n <= flowerbed.length
  • 21 |
22 | -------------------------------------------------------------------------------- /1046-max-consecutive-ones-iii/README.md: -------------------------------------------------------------------------------- 1 |

Max Consecutive Ones III

Difficulty: Medium

Given a binary array nums and an integer k, return the maximum number of consecutive 1's in the array if you can flip at most k 0's.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
 7 | Input: nums = [1,1,1,0,0,0,1,1,1,1,0], k = 2
 8 | Output: 6
 9 | Explanation: [1,1,1,0,0,1,1,1,1,1,1]
10 | Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.
11 | 12 |

Example 2:

13 | 14 |
15 | Input: nums = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], k = 3
16 | Output: 10
17 | Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1]
18 | Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.
19 | 
20 | 21 |

 

22 |

Constraints:

23 | 24 |
    25 |
  • 1 <= nums.length <= 105
  • 26 |
  • nums[i] is either 0 or 1.
  • 27 |
  • 0 <= k <= nums.length
  • 28 |
29 | -------------------------------------------------------------------------------- /1116-maximum-level-sum-of-a-binary-tree/README.md: -------------------------------------------------------------------------------- 1 |

Maximum Level Sum of a Binary Tree

Difficulty: Medium

Given the root of a binary tree, the level of its root is 1, the level of its children is 2, and so on.

2 | 3 |

Return the smallest level x such that the sum of all the values of nodes at level x is maximal.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
 9 | Input: root = [1,7,0,7,-8,null,null]
10 | Output: 2
11 | Explanation: 
12 | Level 1 sum = 1.
13 | Level 2 sum = 7 + 0 = 7.
14 | Level 3 sum = 7 + -8 = -1.
15 | So we return the level with the maximum sum which is level 2.
16 | 
17 | 18 |

Example 2:

19 | 20 |
21 | Input: root = [989,null,10250,98693,-89388,null,null,null,-32127]
22 | Output: 2
23 | 
24 | 25 |

 

26 |

Constraints:

27 | 28 |
    29 |
  • The number of nodes in the tree is in the range [1, 104].
  • 30 |
  • -105 <= Node.val <= 105
  • 31 |
32 | -------------------------------------------------------------------------------- /1833-find-the-highest-altitude/README.md: -------------------------------------------------------------------------------- 1 |

Find the Highest Altitude

Difficulty: Easy

There is a biker going on a road trip. The road trip consists of n + 1 points at different altitudes. The biker starts his trip on point 0 with altitude equal 0.

2 | 3 |

You are given an integer array gain of length n where gain[i] is the net gain in altitude between points i​​​​​​ and i + 1 for all (0 <= i < n). Return the highest altitude of a point.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
 9 | Input: gain = [-5,1,5,0,-7]
10 | Output: 1
11 | Explanation: The altitudes are [0,-5,-4,1,1,-6]. The highest is 1.
12 | 
13 | 14 |

Example 2:

15 | 16 |
17 | Input: gain = [-4,-3,-2,-1,4,3,2]
18 | Output: 0
19 | Explanation: The altitudes are [0,-4,-7,-9,-10,-6,-3,-1]. The highest is 0.
20 | 
21 | 22 |

 

23 |

Constraints:

24 | 25 |
    26 |
  • n == gain.length
  • 27 |
  • 1 <= n <= 100
  • 28 |
  • -100 <= gain[i] <= 100
  • 29 |
30 | -------------------------------------------------------------------------------- /394-decode-string/decode-string.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | string decodeString(string s) { 5 | stack st; 6 | 7 | for ( int i=0; i=0; i-- ) 46 | { 47 | ans[i] = st.top(); 48 | st.pop(); 49 | } 50 | 51 | return ( ans ); 52 | } 53 | }; -------------------------------------------------------------------------------- /806-domino-and-tromino-tiling/README.md: -------------------------------------------------------------------------------- 1 |

Domino and Tromino Tiling

Difficulty: 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 |
11 | Input: n = 3
12 | Output: 5
13 | Explanation: The five different ways are show above.
14 | 
15 | 16 |

Example 2:

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

 

24 |

Constraints:

25 | 26 |
    27 |
  • 1 <= n <= 1000
  • 28 |
29 | -------------------------------------------------------------------------------- /198-house-robber/README.md: -------------------------------------------------------------------------------- 1 |

House Robber

Difficulty: Medium

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night.

2 | 3 |

Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
 9 | Input: nums = [1,2,3,1]
10 | Output: 4
11 | Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
12 | Total amount you can rob = 1 + 3 = 4.
13 | 
14 | 15 |

Example 2:

16 | 17 |
18 | Input: nums = [2,7,9,3,1]
19 | Output: 12
20 | Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1).
21 | Total amount you can rob = 2 + 9 + 1 = 12.
22 | 
23 | 24 |

 

25 |

Constraints:

26 | 27 |
    28 |
  • 1 <= nums.length <= 100
  • 29 |
  • 0 <= nums[i] <= 400
  • 30 |
31 | -------------------------------------------------------------------------------- /1116-maximum-level-sum-of-a-binary-tree/maximum-level-sum-of-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 | public: 14 | int maxLevelSum(TreeNode* root) { 15 | queue q; 16 | 17 | int ans = INT_MIN; 18 | int level = 0; 19 | int c_level = 0, sum = root->val; 20 | 21 | q.push( root ); 22 | 23 | while ( !q.empty() ) 24 | { 25 | 26 | c_level++; 27 | if ( sum > ans ) 28 | ans = sum, level = c_level; 29 | sum = 0; 30 | 31 | int size = q.size(); 32 | 33 | while ( size-- ) 34 | { 35 | TreeNode* curr_node = q.front(); 36 | 37 | q.pop(); 38 | if ( curr_node->left ) 39 | { 40 | sum += curr_node->left->val; 41 | q.push( curr_node->left ); 42 | } 43 | 44 | if ( curr_node->right ) 45 | { 46 | sum += curr_node->right->val; 47 | q.push( curr_node->right ); 48 | } 49 | } 50 | } 51 | return ( level ); 52 | } 53 | }; -------------------------------------------------------------------------------- /208-implement-trie-prefix-tree/implement-trie-prefix-tree.cpp: -------------------------------------------------------------------------------- 1 | class Trie { 2 | public: 3 | vector alpha; 4 | bool end; 5 | 6 | Trie() { 7 | end = false; 8 | 9 | alpha.resize( 26, NULL ); 10 | } 11 | 12 | void insert(string word) { 13 | Trie* curr = this; 14 | 15 | for ( int i=0; ialpha[word[i] - 'a'] ) 18 | curr->alpha[word[i] - 'a'] = new Trie(); 19 | 20 | curr = curr->alpha[word[i] - 'a']; 21 | } 22 | 23 | curr->end = true; 24 | } 25 | 26 | bool search(string word) { 27 | Trie* curr = this; 28 | 29 | for ( int i=0; ialpha[word[i] - 'a']) 32 | return ( false ); 33 | 34 | curr = curr->alpha[word[i] - 'a']; 35 | } 36 | 37 | return ( curr->end ); 38 | } 39 | 40 | bool startsWith(string prefix) { 41 | Trie* curr = this; 42 | 43 | for ( int i=0; ialpha[prefix[i] - 'a']) 46 | return ( false ); 47 | 48 | curr = curr->alpha[prefix[i] - 'a']; 49 | } 50 | 51 | return ( true ); 52 | } 53 | }; 54 | 55 | /** 56 | * Your Trie object will be instantiated and called as such: 57 | * Trie* obj = new Trie(); 58 | * obj->insert(word); 59 | * bool param_2 = obj->search(word); 60 | * bool param_3 = obj->startsWith(prefix); 61 | */ -------------------------------------------------------------------------------- /1224-minimum-falling-path-sum-ii/README.md: -------------------------------------------------------------------------------- 1 |

Minimum Falling Path Sum II

Difficulty: Hard

Given an n x n integer matrix grid, return the minimum sum of a falling path with non-zero shifts.

2 | 3 |

A falling path with non-zero shifts is a choice of exactly one element from each row of grid such that no two elements chosen in adjacent rows are in the same column.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
 9 | Input: grid = [[1,2,3],[4,5,6],[7,8,9]]
10 | Output: 13
11 | Explanation: 
12 | The possible falling paths are:
13 | [1,5,9], [1,5,7], [1,6,7], [1,6,8],
14 | [2,4,8], [2,4,9], [2,6,7], [2,6,8],
15 | [3,4,8], [3,4,9], [3,5,7], [3,5,9]
16 | The falling path with the smallest sum is [1,5,7], so the answer is 13.
17 | 
18 | 19 |

Example 2:

20 | 21 |
22 | Input: grid = [[7]]
23 | Output: 7
24 | 
25 | 26 |

 

27 |

Constraints:

28 | 29 |
    30 |
  • n == grid.length == grid[i].length
  • 31 |
  • 1 <= n <= 200
  • 32 |
  • -99 <= grid[i][j] <= 99
  • 33 |
34 | -------------------------------------------------------------------------------- /1798-max-number-of-k-sum-pairs/README.md: -------------------------------------------------------------------------------- 1 |

Max Number of K-Sum Pairs

Difficulty: Medium

You are given an integer array nums and an integer k.

2 | 3 |

In one operation, you can pick two numbers from the array whose sum equals k and remove them from the array.

4 | 5 |

Return the maximum number of operations you can perform on the array.

6 | 7 |

 

8 |

Example 1:

9 | 10 |
11 | Input: nums = [1,2,3,4], k = 5
12 | Output: 2
13 | Explanation: Starting with nums = [1,2,3,4]:
14 | - Remove numbers 1 and 4, then nums = [2,3]
15 | - Remove numbers 2 and 3, then nums = []
16 | There are no more pairs that sum up to 5, hence a total of 2 operations.
17 | 18 |

Example 2:

19 | 20 |
21 | Input: nums = [3,1,3,4,3], k = 6
22 | Output: 1
23 | Explanation: Starting with nums = [3,1,3,4,3]:
24 | - Remove the first two 3's, then nums = [1,4,3]
25 | There are no more pairs that sum up to 6, hence a total of 1 operation.
26 | 27 |

 

28 |

Constraints:

29 | 30 |
    31 |
  • 1 <= nums.length <= 105
  • 32 |
  • 1 <= nums[i] <= 109
  • 33 |
  • 1 <= k <= 109
  • 34 |
35 | -------------------------------------------------------------------------------- /1146-greatest-common-divisor-of-strings/README.md: -------------------------------------------------------------------------------- 1 |

Greatest Common Divisor of Strings

Difficulty: Easy

For two strings s and t, we say "t divides s" if and only if s = t + t + t + ... + t + t (i.e., t is concatenated with itself one or more times).

2 | 3 |

Given two strings str1 and str2, return the largest string x such that x divides both str1 and str2.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
 9 | Input: str1 = "ABCABC", str2 = "ABC"
10 | Output: "ABC"
11 | 
12 | 13 |

Example 2:

14 | 15 |
16 | Input: str1 = "ABABAB", str2 = "ABAB"
17 | Output: "AB"
18 | 
19 | 20 |

Example 3:

21 | 22 |
23 | Input: str1 = "LEET", str2 = "CODE"
24 | Output: ""
25 | 
26 | 27 |

 

28 |

Constraints:

29 | 30 |
    31 |
  • 1 <= str1.length, str2.length <= 1000
  • 32 |
  • str1 and str2 consist of English uppercase letters.
  • 33 |
34 | -------------------------------------------------------------------------------- /62-unique-paths/README.md: -------------------------------------------------------------------------------- 1 |

Unique Paths

Difficulty: Medium

There is a robot on an m x n grid. The robot is initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in time.

2 | 3 |

Given the two integers m and n, return the number of possible unique paths that the robot can take to reach the bottom-right corner.

4 | 5 |

The test cases are generated so that the answer will be less than or equal to 2 * 109.

6 | 7 |

 

8 |

Example 1:

9 | 10 |
11 | Input: m = 3, n = 7
12 | Output: 28
13 | 
14 | 15 |

Example 2:

16 | 17 |
18 | Input: m = 3, n = 2
19 | Output: 3
20 | Explanation: From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
21 | 1. Right -> Down -> Down
22 | 2. Down -> Down -> Right
23 | 3. Down -> Right -> Down
24 | 
25 | 26 |

 

27 |

Constraints:

28 | 29 |
    30 |
  • 1 <= m, n <= 100
  • 31 |
32 | -------------------------------------------------------------------------------- /338-counting-bits/README.md: -------------------------------------------------------------------------------- 1 |

Counting Bits

Difficulty: Easy

Given an integer n, return an array ans of length n + 1 such that for each i (0 <= i <= n), ans[i] is the number of 1's in the binary representation of i.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
 7 | Input: n = 2
 8 | Output: [0,1,1]
 9 | Explanation:
10 | 0 --> 0
11 | 1 --> 1
12 | 2 --> 10
13 | 
14 | 15 |

Example 2:

16 | 17 |
18 | Input: n = 5
19 | Output: [0,1,1,2,1,2]
20 | Explanation:
21 | 0 --> 0
22 | 1 --> 1
23 | 2 --> 10
24 | 3 --> 11
25 | 4 --> 100
26 | 5 --> 101
27 | 
28 | 29 |

 

30 |

Constraints:

31 | 32 |
    33 |
  • 0 <= n <= 105
  • 34 |
35 | 36 |

 

37 |

Follow up:

38 | 39 |
    40 |
  • It is very easy to come up with a solution with a runtime of O(n log n). Can you do it in linear time O(n) and possibly in a single pass?
  • 41 |
  • Can you do it without using any built-in function (i.e., like __builtin_popcount in C++)?
  • 42 |
43 | -------------------------------------------------------------------------------- /1586-longest-subarray-of-1s-after-deleting-one-element/README.md: -------------------------------------------------------------------------------- 1 |

Longest Subarray of 1's After Deleting One Element

Difficulty: 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 |
 9 | Input: nums = [1,1,0,1]
10 | Output: 3
11 | Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
12 | 
13 | 14 |

Example 2:

15 | 16 |
17 | Input: nums = [0,1,1,1,0,1,1,0,1]
18 | Output: 5
19 | 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].
20 | 
21 | 22 |

Example 3:

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

 

31 |

Constraints:

32 | 33 |
    34 |
  • 1 <= nums.length <= 105
  • 35 |
  • nums[i] is either 0 or 1.
  • 36 |
37 | -------------------------------------------------------------------------------- /17-letter-combinations-of-a-phone-number/README.md: -------------------------------------------------------------------------------- 1 |

Letter Combinations of a Phone Number

Difficulty: Medium

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

2 | 3 |

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

4 | 5 |

 

6 |

Example 1:

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

Example 2:

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

Example 3:

21 | 22 |
23 | Input: digits = "2"
24 | Output: ["a","b","c"]
25 | 
26 | 27 |

 

28 |

Constraints:

29 | 30 |
    31 |
  • 0 <= digits.length <= 4
  • 32 |
  • digits[i] is a digit in the range ['2', '9'].
  • 33 |
34 | -------------------------------------------------------------------------------- /162-find-peak-element/README.md: -------------------------------------------------------------------------------- 1 |

Find Peak Element

Difficulty: Medium

A peak element is an element that is strictly greater than its neighbors.

2 | 3 |

Given a 0-indexed integer array nums, find a peak element, and return its index. If the array contains multiple peaks, return the index to any of the peaks.

4 | 5 |

You may imagine that nums[-1] = nums[n] = -∞. In other words, an element is always considered to be strictly greater than a neighbor that is outside the array.

6 | 7 |

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

8 | 9 |

 

10 |

Example 1:

11 | 12 |
13 | Input: nums = [1,2,3,1]
14 | Output: 2
15 | Explanation: 3 is a peak element and your function should return the index number 2.
16 | 17 |

Example 2:

18 | 19 |
20 | Input: nums = [1,2,1,3,5,6,4]
21 | Output: 5
22 | Explanation: Your function can return either index number 1 where the peak element is 2, or index number 5 where the peak element is 6.
23 | 24 |

 

25 |

Constraints:

26 | 27 |
    28 |
  • 1 <= nums.length <= 1000
  • 29 |
  • -231 <= nums[i] <= 231 - 1
  • 30 |
  • nums[i] != nums[i + 1] for all valid i.
  • 31 |
32 | -------------------------------------------------------------------------------- /238-product-of-array-except-self/README.md: -------------------------------------------------------------------------------- 1 |

Product of Array Except Self

Difficulty: Medium

Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i].

2 | 3 |

The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.

4 | 5 |

You must write an algorithm that runs in O(n) time and without using the division operation.

6 | 7 |

 

8 |

Example 1:

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

Example 2:

12 |
Input: nums = [-1,1,0,-3,3]
13 | Output: [0,0,9,0,0]
14 | 
15 |

 

16 |

Constraints:

17 | 18 |
    19 |
  • 2 <= nums.length <= 105
  • 20 |
  • -30 <= nums[i] <= 30
  • 21 |
  • The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.
  • 22 |
23 | 24 |

 

25 |

Follow up: Can you solve the problem in O(1) extra space complexity? (The output array does not count as extra space for space complexity analysis.)

26 | -------------------------------------------------------------------------------- /11-container-with-most-water/README.md: -------------------------------------------------------------------------------- 1 |

Container With Most Water

Difficulty: Medium

You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]).

2 | 3 |

Find two lines that together with the x-axis form a container, such that the container contains the most water.

4 | 5 |

Return the maximum amount of water a container can store.

6 | 7 |

Notice that you may not slant the container.

8 | 9 |

 

10 |

Example 1:

11 | 12 |
13 | Input: height = [1,8,6,2,5,4,8,3,7]
14 | Output: 49
15 | Explanation: The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.
16 | 
17 | 18 |

Example 2:

19 | 20 |
21 | Input: height = [1,1]
22 | Output: 1
23 | 
24 | 25 |

 

26 |

Constraints:

27 | 28 |
    29 |
  • n == height.length
  • 30 |
  • 2 <= n <= 105
  • 31 |
  • 0 <= height[i] <= 104
  • 32 |
33 | -------------------------------------------------------------------------------- /435-non-overlapping-intervals/README.md: -------------------------------------------------------------------------------- 1 |

Non-overlapping Intervals

Difficulty: Medium

Given an array of intervals intervals where intervals[i] = [starti, endi], return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
 7 | Input: intervals = [[1,2],[2,3],[3,4],[1,3]]
 8 | Output: 1
 9 | Explanation: [1,3] can be removed and the rest of the intervals are non-overlapping.
10 | 
11 | 12 |

Example 2:

13 | 14 |
15 | Input: intervals = [[1,2],[1,2],[1,2]]
16 | Output: 2
17 | Explanation: You need to remove two [1,2] to make the rest of the intervals non-overlapping.
18 | 
19 | 20 |

Example 3:

21 | 22 |
23 | Input: intervals = [[1,2],[2,3]]
24 | Output: 0
25 | Explanation: You don't need to remove any of the intervals since they're already non-overlapping.
26 | 
27 | 28 |

 

29 |

Constraints:

30 | 31 |
    32 |
  • 1 <= intervals.length <= 105
  • 33 |
  • intervals[i].length == 2
  • 34 |
  • -5 * 104 <= starti < endi <= 5 * 104
  • 35 |
36 | -------------------------------------------------------------------------------- /328-odd-even-linked-list/README.md: -------------------------------------------------------------------------------- 1 |

Odd Even Linked List

Difficulty: Medium

Given the head of a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and return the reordered list.

2 | 3 |

The first node is considered odd, and the second node is even, and so on.

4 | 5 |

Note that the relative order inside both the even and odd groups should remain as it was in the input.

6 | 7 |

You must solve the problem in O(1) extra space complexity and O(n) time complexity.

8 | 9 |

 

10 |

Example 1:

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

Example 2:

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

 

25 |

Constraints:

26 | 27 |
    28 |
  • The number of nodes in the linked list is in the range [0, 104].
  • 29 |
  • -106 <= Node.val <= 106
  • 30 |
31 | -------------------------------------------------------------------------------- /334-increasing-triplet-subsequence/README.md: -------------------------------------------------------------------------------- 1 |

Increasing Triplet Subsequence

Difficulty: Medium

Given an integer array nums, return true if there exists a triple of indices (i, j, k) such that i < j < k and nums[i] < nums[j] < nums[k]. If no such indices exists, return false.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
 7 | Input: nums = [1,2,3,4,5]
 8 | Output: true
 9 | Explanation: Any triplet where i < j < k is valid.
10 | 
11 | 12 |

Example 2:

13 | 14 |
15 | Input: nums = [5,4,3,2,1]
16 | Output: false
17 | Explanation: No triplet exists.
18 | 
19 | 20 |

Example 3:

21 | 22 |
23 | Input: nums = [2,1,5,0,4,6]
24 | Output: true
25 | Explanation: The triplet (3, 4, 5) is valid because nums[3] == 0 < nums[4] == 4 < nums[5] == 6.
26 | 
27 | 28 |

 

29 |

Constraints:

30 | 31 |
    32 |
  • 1 <= nums.length <= 5 * 105
  • 33 |
  • -231 <= nums[i] <= 231 - 1
  • 34 |
35 | 36 |

 

37 | Follow up: Could you implement a solution that runs in O(n) time complexity and O(1) space complexity? -------------------------------------------------------------------------------- /1441-minimum-flips-to-make-a-or-b-equal-to-c/README.md: -------------------------------------------------------------------------------- 1 |

Minimum Flips to Make a OR b Equal to c

Difficulty: Medium

Given 3 positives numbers a, b and c. Return the minimum flips required in some bits of a and b to make ( a OR b == c ). (bitwise OR operation).
2 | Flip operation consists of change any single bit 1 to 0 or change the bit 0 to 1 in their binary representation.

3 | 4 |

 

5 |

Example 1:

6 | 7 |

8 | 9 |
10 | Input: a = 2, b = 6, c = 5
11 | Output: 3
12 | Explanation: After flips a = 1 , b = 4 , c = 5 such that (a OR b == c)
13 | 14 |

Example 2:

15 | 16 |
17 | Input: a = 4, b = 2, c = 7
18 | Output: 1
19 | 
20 | 21 |

Example 3:

22 | 23 |
24 | Input: a = 1, b = 2, c = 3
25 | Output: 0
26 | 
27 | 28 |

 

29 |

Constraints:

30 | 31 |
    32 |
  • 1 <= a <= 10^9
  • 33 |
  • 1 <= b <= 10^9
  • 34 |
  • 1 <= c <= 10^9
  • 35 |
-------------------------------------------------------------------------------- /392-is-subsequence/README.md: -------------------------------------------------------------------------------- 1 |

Is Subsequence

Difficulty: Easy

Given two strings s and t, return true if s is a subsequence of t, or false otherwise.

2 | 3 |

A subsequence of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i.e., "ace" is a subsequence of "abcde" while "aec" is not).

4 | 5 |

 

6 |

Example 1:

7 |
Input: s = "abc", t = "ahbgdc"
 8 | Output: true
 9 | 

Example 2:

10 |
Input: s = "axc", t = "ahbgdc"
11 | Output: false
12 | 
13 |

 

14 |

Constraints:

15 | 16 |
    17 |
  • 0 <= s.length <= 100
  • 18 |
  • 0 <= t.length <= 104
  • 19 |
  • s and t consist only of lowercase English letters.
  • 20 |
21 | 22 |

 

23 | Follow up: Suppose there are lots of incoming s, say s1, s2, ..., sk where k >= 109, and you want to check one by one to see if t has its subsequence. In this scenario, how would you change your code? -------------------------------------------------------------------------------- /72-edit-distance/README.md: -------------------------------------------------------------------------------- 1 |

Edit Distance

Difficulty: Medium

Given two strings word1 and word2, return the minimum number of operations required to convert word1 to word2.

2 | 3 |

You have the following three operations permitted on a word:

4 | 5 |
    6 |
  • Insert a character
  • 7 |
  • Delete a character
  • 8 |
  • Replace a character
  • 9 |
10 | 11 |

 

12 |

Example 1:

13 | 14 |
15 | Input: word1 = "horse", word2 = "ros"
16 | Output: 3
17 | Explanation: 
18 | horse -> rorse (replace 'h' with 'r')
19 | rorse -> rose (remove 'r')
20 | rose -> ros (remove 'e')
21 | 
22 | 23 |

Example 2:

24 | 25 |
26 | Input: word1 = "intention", word2 = "execution"
27 | Output: 5
28 | Explanation: 
29 | intention -> inention (remove 't')
30 | inention -> enention (replace 'i' with 'e')
31 | enention -> exention (replace 'n' with 'x')
32 | exention -> exection (replace 'n' with 'c')
33 | exection -> execution (insert 'u')
34 | 
35 | 36 |

 

37 |

Constraints:

38 | 39 |
    40 |
  • 0 <= word1.length, word2.length <= 500
  • 41 |
  • word1 and word2 consist of lowercase English letters.
  • 42 |
43 | -------------------------------------------------------------------------------- /374-guess-number-higher-or-lower/README.md: -------------------------------------------------------------------------------- 1 |

Guess Number Higher or Lower

Difficulty: Easy

We are playing the Guess Game. The game is as follows:

2 | 3 |

I pick a number from 1 to n. You have to guess which number I picked.

4 | 5 |

Every time you guess wrong, I will tell you whether the number I picked is higher or lower than your guess.

6 | 7 |

You call a pre-defined API int guess(int num), which returns three possible results:

8 | 9 |
    10 |
  • -1: Your guess is higher than the number I picked (i.e. num > pick).
  • 11 |
  • 1: Your guess is lower than the number I picked (i.e. num < pick).
  • 12 |
  • 0: your guess is equal to the number I picked (i.e. num == pick).
  • 13 |
14 | 15 |

Return the number that I picked.

16 | 17 |

 

18 |

Example 1:

19 | 20 |
21 | Input: n = 10, pick = 6
22 | Output: 6
23 | 
24 | 25 |

Example 2:

26 | 27 |
28 | Input: n = 1, pick = 1
29 | Output: 1
30 | 
31 | 32 |

Example 3:

33 | 34 |
35 | Input: n = 2, pick = 1
36 | Output: 1
37 | 
38 | 39 |

 

40 |

Constraints:

41 | 42 |
    43 |
  • 1 <= n <= 231 - 1
  • 44 |
  • 1 <= pick <= n
  • 45 |
46 | -------------------------------------------------------------------------------- /747-min-cost-climbing-stairs/README.md: -------------------------------------------------------------------------------- 1 |

Min Cost Climbing Stairs

Difficulty: Easy

You are given an integer array cost where cost[i] is the cost of ith step on a staircase. Once you pay the cost, you can either climb one or two steps.

2 | 3 |

You can either start from the step with index 0, or the step with index 1.

4 | 5 |

Return the minimum cost to reach the top of the floor.

6 | 7 |

 

8 |

Example 1:

9 | 10 |
11 | Input: cost = [10,15,20]
12 | Output: 15
13 | Explanation: You will start at index 1.
14 | - Pay 15 and climb two steps to reach the top.
15 | The total cost is 15.
16 | 
17 | 18 |

Example 2:

19 | 20 |
21 | Input: cost = [1,100,1,1,1,100,1,1,100,1]
22 | Output: 6
23 | Explanation: You will start at index 0.
24 | - Pay 1 and climb two steps to reach index 2.
25 | - Pay 1 and climb two steps to reach index 4.
26 | - Pay 1 and climb two steps to reach index 6.
27 | - Pay 1 and climb one step to reach index 7.
28 | - Pay 1 and climb two steps to reach index 9.
29 | - Pay 1 and climb one step to reach the top.
30 | The total cost is 6.
31 | 
32 | 33 |

 

34 |

Constraints:

35 | 36 |
    37 |
  • 2 <= cost.length <= 1000
  • 38 |
  • 0 <= cost[i] <= 999
  • 39 |
40 | -------------------------------------------------------------------------------- /1567-maximum-number-of-vowels-in-a-substring-of-given-length/README.md: -------------------------------------------------------------------------------- 1 |

Maximum Number of Vowels in a Substring of Given Length

Difficulty: Medium

Given a string s and an integer k, return the maximum number of vowel letters in any substring of s with length k.

2 | 3 |

Vowel letters in English are 'a', 'e', 'i', 'o', and 'u'.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
 9 | Input: s = "abciiidef", k = 3
10 | Output: 3
11 | Explanation: The substring "iii" contains 3 vowel letters.
12 | 
13 | 14 |

Example 2:

15 | 16 |
17 | Input: s = "aeiou", k = 2
18 | Output: 2
19 | Explanation: Any substring of length 2 contains 2 vowels.
20 | 
21 | 22 |

Example 3:

23 | 24 |
25 | Input: s = "leetcode", k = 3
26 | Output: 2
27 | Explanation: "lee", "eet" and "ode" contain 2 vowels.
28 | 
29 | 30 |

 

31 |

Constraints:

32 | 33 |
    34 |
  • 1 <= s.length <= 105
  • 35 |
  • s consists of lowercase English letters.
  • 36 |
  • 1 <= k <= s.length
  • 37 |
38 | -------------------------------------------------------------------------------- /2428-equal-row-and-column-pairs/README.md: -------------------------------------------------------------------------------- 1 |

Equal Row and Column Pairs

Difficulty: Medium

Given a 0-indexed n x n integer matrix grid, return the number of pairs (ri, cj) such that row ri and column cj are equal.

2 | 3 |

A row and column pair is considered equal if they contain the same elements in the same order (i.e., an equal array).

4 | 5 |

 

6 |

Example 1:

7 | 8 |
 9 | Input: grid = [[3,2,1],[1,7,6],[2,7,7]]
10 | Output: 1
11 | Explanation: There is 1 equal row and column pair:
12 | - (Row 2, Column 1): [2,7,7]
13 | 
14 | 15 |

Example 2:

16 | 17 |
18 | Input: grid = [[3,1,2,2],[1,4,4,5],[2,4,2,2],[2,4,2,2]]
19 | Output: 3
20 | Explanation: There are 3 equal row and column pairs:
21 | - (Row 0, Column 0): [3,1,2,2]
22 | - (Row 2, Column 2): [2,4,2,2]
23 | - (Row 3, Column 2): [2,4,2,2]
24 | 
25 | 26 |

 

27 |

Constraints:

28 | 29 |
    30 |
  • n == grid.length == grid[i].length
  • 31 |
  • 1 <= n <= 200
  • 32 |
  • 1 <= grid[i][j] <= 105
  • 33 |
34 | -------------------------------------------------------------------------------- /735-asteroid-collision/README.md: -------------------------------------------------------------------------------- 1 |

Asteroid Collision

Difficulty: Medium

We are given an array asteroids of integers representing asteroids in a row.

2 | 3 |

For each asteroid, the absolute value represents its size, and the sign represents its direction (positive meaning right, negative meaning left). Each asteroid moves at the same speed.

4 | 5 |

Find out the state of the asteroids after all collisions. If two asteroids meet, the smaller one will explode. If both are the same size, both will explode. Two asteroids moving in the same direction will never meet.

6 | 7 |

 

8 |

Example 1:

9 | 10 |
11 | Input: asteroids = [5,10,-5]
12 | Output: [5,10]
13 | Explanation: The 10 and -5 collide resulting in 10. The 5 and 10 never collide.
14 | 
15 | 16 |

Example 2:

17 | 18 |
19 | Input: asteroids = [8,-8]
20 | Output: []
21 | Explanation: The 8 and -8 collide exploding each other.
22 | 
23 | 24 |

Example 3:

25 | 26 |
27 | Input: asteroids = [10,2,-5]
28 | Output: [10]
29 | Explanation: The 2 and -5 collide resulting in -5. The 10 and -5 collide resulting in 10.
30 | 
31 | 32 |

 

33 |

Constraints:

34 | 35 |
    36 |
  • 2 <= asteroids.length <= 104
  • 37 |
  • -1000 <= asteroids[i] <= 1000
  • 38 |
  • asteroids[i] != 0
  • 39 |
40 | -------------------------------------------------------------------------------- /216-combination-sum-iii/README.md: -------------------------------------------------------------------------------- 1 |

Combination Sum III

Difficulty: Medium

Find all valid combinations of k numbers that sum up to n such that the following conditions are true:

2 | 3 |
    4 |
  • Only numbers 1 through 9 are used.
  • 5 |
  • Each number is used at most once.
  • 6 |
7 | 8 |

Return a list of all possible valid combinations. The list must not contain the same combination twice, and the combinations may be returned in any order.

9 | 10 |

 

11 |

Example 1:

12 | 13 |
14 | Input: k = 3, n = 7
15 | Output: [[1,2,4]]
16 | Explanation:
17 | 1 + 2 + 4 = 7
18 | There are no other valid combinations.
19 | 20 |

Example 2:

21 | 22 |
23 | Input: k = 3, n = 9
24 | Output: [[1,2,6],[1,3,5],[2,3,4]]
25 | Explanation:
26 | 1 + 2 + 6 = 9
27 | 1 + 3 + 5 = 9
28 | 2 + 3 + 4 = 9
29 | There are no other valid combinations.
30 | 
31 | 32 |

Example 3:

33 | 34 |
35 | Input: k = 4, n = 1
36 | Output: []
37 | Explanation: There are no valid combinations.
38 | Using 4 different numbers in the range [1,9], the smallest sum we can get is 1+2+3+4 = 10 and since 10 > 1, there are no valid combination.
39 | 
40 | 41 |

 

42 |

Constraints:

43 | 44 |
    45 |
  • 2 <= k <= 9
  • 46 |
  • 1 <= n <= 60
  • 47 |
48 | -------------------------------------------------------------------------------- /907-koko-eating-bananas/README.md: -------------------------------------------------------------------------------- 1 |

Koko Eating Bananas

Difficulty: Medium

Koko loves to eat bananas. There are n piles of bananas, the ith pile has piles[i] bananas. The guards have gone and will come back in h hours.

2 | 3 |

Koko can decide her bananas-per-hour eating speed of k. Each hour, she chooses some pile of bananas and eats k bananas from that pile. If the pile has less than k bananas, she eats all of them instead and will not eat any more bananas during this hour.

4 | 5 |

Koko likes to eat slowly but still wants to finish eating all the bananas before the guards return.

6 | 7 |

Return the minimum integer k such that she can eat all the bananas within h hours.

8 | 9 |

 

10 |

Example 1:

11 | 12 |
13 | Input: piles = [3,6,7,11], h = 8
14 | Output: 4
15 | 
16 | 17 |

Example 2:

18 | 19 |
20 | Input: piles = [30,11,23,4,20], h = 5
21 | Output: 30
22 | 
23 | 24 |

Example 3:

25 | 26 |
27 | Input: piles = [30,11,23,4,20], h = 6
28 | Output: 23
29 | 
30 | 31 |

 

32 |

Constraints:

33 | 34 |
    35 |
  • 1 <= piles.length <= 104
  • 36 |
  • piles.length <= h <= 109
  • 37 |
  • 1 <= piles[i] <= 109
  • 38 |
39 | -------------------------------------------------------------------------------- /904-leaf-similar-trees/README.md: -------------------------------------------------------------------------------- 1 |

Leaf-Similar Trees

Difficulty: Easy

Consider all the leaves of a binary tree, from left to right order, the values of those leaves form a leaf value sequence.

2 | 3 |

4 | 5 |

For example, in the given tree above, the leaf value sequence is (6, 7, 4, 9, 8).

6 | 7 |

Two binary trees are considered leaf-similar if their leaf value sequence is the same.

8 | 9 |

Return true if and only if the two given trees with head nodes root1 and root2 are leaf-similar.

10 | 11 |

 

12 |

Example 1:

13 | 14 |
15 | Input: root1 = [3,5,1,6,2,9,8,null,null,7,4], root2 = [3,5,1,6,7,4,2,null,null,null,null,null,null,9,8]
16 | Output: true
17 | 
18 | 19 |

Example 2:

20 | 21 |
22 | Input: root1 = [1,2,3], root2 = [1,3,2]
23 | Output: false
24 | 
25 | 26 |

 

27 |

Constraints:

28 | 29 |
    30 |
  • The number of nodes in each tree will be in the range [1, 200].
  • 31 |
  • Both of the given trees will have values in the range [0, 200].
  • 32 |
33 | -------------------------------------------------------------------------------- /714-best-time-to-buy-and-sell-stock-with-transaction-fee/README.md: -------------------------------------------------------------------------------- 1 |

Best Time to Buy and Sell Stock with Transaction Fee

Difficulty: Medium

You are given an array prices where prices[i] is the price of a given stock on the ith day, and an integer fee representing a transaction fee.

2 | 3 |

Find the maximum profit you can achieve. You may complete as many transactions as you like, but you need to pay the transaction fee for each transaction.

4 | 5 |

Note:

6 | 7 |
    8 |
  • You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
  • 9 |
  • The transaction fee is only charged once for each stock purchase and sale.
  • 10 |
11 | 12 |

 

13 |

Example 1:

14 | 15 |
16 | Input: prices = [1,3,2,8,4,9], fee = 2
17 | Output: 8
18 | Explanation: The maximum profit can be achieved by:
19 | - Buying at prices[0] = 1
20 | - Selling at prices[3] = 8
21 | - Buying at prices[4] = 4
22 | - Selling at prices[5] = 9
23 | The total profit is ((8 - 1) - 2) + ((9 - 4) - 2) = 8.
24 | 
25 | 26 |

Example 2:

27 | 28 |
29 | Input: prices = [1,3,7,5,10,3], fee = 3
30 | Output: 6
31 | 
32 | 33 |

 

34 |

Constraints:

35 | 36 |
    37 |
  • 1 <= prices.length <= 5 * 104
  • 38 |
  • 1 <= prices[i] < 5 * 104
  • 39 |
  • 0 <= fee < 5 * 104
  • 40 |
41 | -------------------------------------------------------------------------------- /1036-rotting-oranges/rotting-oranges.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int dirs[4][2] = {{-1,0}, {0,-1}, {1,0}, {0,1}}; 4 | 5 | int orangesRotting(vector>& grid) { 6 | queue> q; 7 | 8 | int count = 0; 9 | 10 | for ( int i=0; iMerge Strings Alternately Difficulty: Easy

You are given two strings word1 and word2. Merge the strings by adding letters in alternating order, starting with word1. If a string is longer than the other, append the additional letters onto the end of the merged string.

2 | 3 |

Return the merged string.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
 9 | Input: word1 = "abc", word2 = "pqr"
10 | Output: "apbqcr"
11 | Explanation: The merged string will be merged as so:
12 | word1:  a   b   c
13 | word2:    p   q   r
14 | merged: a p b q c r
15 | 
16 | 17 |

Example 2:

18 | 19 |
20 | Input: word1 = "ab", word2 = "pqrs"
21 | Output: "apbqrs"
22 | Explanation: Notice that as word2 is longer, "rs" is appended to the end.
23 | word1:  a   b 
24 | word2:    p   q   r   s
25 | merged: a p b q   r   s
26 | 
27 | 28 |

Example 3:

29 | 30 |
31 | Input: word1 = "abcd", word2 = "pq"
32 | Output: "apbqcd"
33 | Explanation: Notice that as word1 is longer, "cd" is appended to the end.
34 | word1:  a   b   c   d
35 | word2:    p   q 
36 | merged: a p b q c   d
37 | 
38 | 39 |

 

40 |

Constraints:

41 | 42 |
    43 |
  • 1 <= word1.length, word2.length <= 100
  • 44 |
  • word1 and word2 consist of lowercase English letters.
  • 45 |
-------------------------------------------------------------------------------- /394-decode-string/README.md: -------------------------------------------------------------------------------- 1 |

Decode String

Difficulty: Medium

Given an encoded string, return its decoded string.

2 | 3 |

The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note that k is guaranteed to be a positive integer.

4 | 5 |

You may assume that the input string is always valid; there are no extra white spaces, square brackets are well-formed, etc. Furthermore, you may assume that the original data does not contain any digits and that digits are only for those repeat numbers, k. For example, there will not be input like 3a or 2[4].

6 | 7 |

The test cases are generated so that the length of the output will never exceed 105.

8 | 9 |

 

10 |

Example 1:

11 | 12 |
13 | Input: s = "3[a]2[bc]"
14 | Output: "aaabcbc"
15 | 
16 | 17 |

Example 2:

18 | 19 |
20 | Input: s = "3[a2[c]]"
21 | Output: "accaccacc"
22 | 
23 | 24 |

Example 3:

25 | 26 |
27 | Input: s = "2[abc]3[cd]ef"
28 | Output: "abcabccdcdcdef"
29 | 
30 | 31 |

 

32 |

Constraints:

33 | 34 |
    35 |
  • 1 <= s.length <= 30
  • 36 |
  • s consists of lowercase English letters, digits, and square brackets '[]'.
  • 37 |
  • s is guaranteed to be a valid input.
  • 38 |
  • All the integers in s are in the range [1, 300].
  • 39 |
40 | -------------------------------------------------------------------------------- /1036-rotting-oranges/README.md: -------------------------------------------------------------------------------- 1 |

Rotting Oranges

Difficulty: Medium

You are given an m x n grid where each cell can have one of three values:

2 | 3 |
    4 |
  • 0 representing an empty cell,
  • 5 |
  • 1 representing a fresh orange, or
  • 6 |
  • 2 representing a rotten orange.
  • 7 |
8 | 9 |

Every minute, any fresh orange that is 4-directionally adjacent to a rotten orange becomes rotten.

10 | 11 |

Return the minimum number of minutes that must elapse until no cell has a fresh orange. If this is impossible, return -1.

12 | 13 |

 

14 |

Example 1:

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

Example 2:

22 | 23 |
24 | Input: grid = [[2,1,1],[0,1,1],[1,0,1]]
25 | Output: -1
26 | Explanation: The orange in the bottom left corner (row 2, column 0) is never rotten, because rotting only happens 4-directionally.
27 | 
28 | 29 |

Example 3:

30 | 31 |
32 | Input: grid = [[0,2]]
33 | Output: 0
34 | Explanation: Since there are already no fresh oranges at minute 0, the answer is just 0.
35 | 
36 | 37 |

 

38 |

Constraints:

39 | 40 |
    41 |
  • m == grid.length
  • 42 |
  • n == grid[i].length
  • 43 |
  • 1 <= m, n <= 10
  • 44 |
  • grid[i][j] is 0, 1, or 2.
  • 45 |
46 | -------------------------------------------------------------------------------- /547-number-of-provinces/README.md: -------------------------------------------------------------------------------- 1 |

Number of Provinces

Difficulty: Medium

There are n cities. Some of them are connected, while some are not. If city a is connected directly with city b, and city b is connected directly with city c, then city a is connected indirectly with city c.

2 | 3 |

A province is a group of directly or indirectly connected cities and no other cities outside of the group.

4 | 5 |

You are given an n x n matrix isConnected where isConnected[i][j] = 1 if the ith city and the jth city are directly connected, and isConnected[i][j] = 0 otherwise.

6 | 7 |

Return the total number of provinces.

8 | 9 |

 

10 |

Example 1:

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

Example 2:

18 | 19 |
20 | Input: isConnected = [[1,0,0],[0,1,0],[0,0,1]]
21 | Output: 3
22 | 
23 | 24 |

 

25 |

Constraints:

26 | 27 |
    28 |
  • 1 <= n <= 200
  • 29 |
  • n == isConnected.length
  • 30 |
  • n == isConnected[i].length
  • 31 |
  • isConnected[i][j] is 1 or 0.
  • 32 |
  • isConnected[i][i] == 1
  • 33 |
  • isConnected[i][j] == isConnected[j][i]
  • 34 |
35 | -------------------------------------------------------------------------------- /2038-nearest-exit-from-entrance-in-maze/nearest-exit-from-entrance-in-maze.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int dirs[4][2] = {{0,1}, {1,0}, {-1,0}, {0,-1}}; 4 | 5 | int nearestExit(vector>& maze, vector& entrance) { 6 | queue> q; 7 | 8 | q.push( make_pair(entrance[0], entrance[1] ) ); 9 | 10 | vector> vis( maze.size(), vector( maze[0].size(), false) ); 11 | 12 | vis[entrance[0]][entrance[1]] = true; 13 | 14 | int steps = 0; 15 | 16 | while ( !q.empty() ) 17 | { 18 | int size = q.size(); 19 | 20 | while ( size-- ) 21 | { 22 | auto curr = q.front(); 23 | 24 | q.pop(); 25 | 26 | if ( steps and 27 | ( curr.first + 1 == maze.size() 28 | or curr.second + 1 == maze[0].size() 29 | or curr.first - 1 == -1 30 | or curr.second - 1 == -1 ) ) 31 | return ( steps ); 32 | 33 | for ( int i=0; i<4; i++ ) 34 | { 35 | if ( curr.first + dirs[i][0] == maze.size() 36 | or curr.second + dirs[i][1]== maze[0].size() 37 | or curr.first + dirs[i][0] == -1 38 | or curr.second + dirs[i][1]== -1 ) 39 | continue ; 40 | 41 | if ( vis[curr.first + dirs[i][0]][curr.second + dirs[i][1]] ) 42 | continue ; 43 | 44 | if ( maze[curr.first + dirs[i][0]][curr.second + dirs[i][1]] != '.' ) 45 | continue ; 46 | 47 | vis[curr.first + dirs[i][0]][curr.second + dirs[i][1]] = true; 48 | 49 | q.push( make_pair( curr.first + dirs[i][0], curr.second + dirs[i][1] ) ); 50 | } 51 | } 52 | cout << q.size() << endl; 53 | 54 | steps++; 55 | } 56 | return (-1); 57 | } 58 | }; -------------------------------------------------------------------------------- /1250-longest-common-subsequence/README.md: -------------------------------------------------------------------------------- 1 |

Longest Common Subsequence

Difficulty: Medium

Given two strings text1 and text2, return the length of their longest common subsequence. If there is no common subsequence, return 0.

2 | 3 |

A subsequence of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters.

4 | 5 |
    6 |
  • For example, "ace" is a subsequence of "abcde".
  • 7 |
8 | 9 |

A common subsequence of two strings is a subsequence that is common to both strings.

10 | 11 |

 

12 |

Example 1:

13 | 14 |
15 | Input: text1 = "abcde", text2 = "ace" 
16 | Output: 3  
17 | Explanation: The longest common subsequence is "ace" and its length is 3.
18 | 
19 | 20 |

Example 2:

21 | 22 |
23 | Input: text1 = "abc", text2 = "abc"
24 | Output: 3
25 | Explanation: The longest common subsequence is "abc" and its length is 3.
26 | 
27 | 28 |

Example 3:

29 | 30 |
31 | Input: text1 = "abc", text2 = "def"
32 | Output: 0
33 | Explanation: There is no such common subsequence, so the result is 0.
34 | 
35 | 36 |

 

37 |

Constraints:

38 | 39 |
    40 |
  • 1 <= text1.length, text2.length <= 1000
  • 41 |
  • text1 and text2 consist of only lowercase English characters.
  • 42 |
43 | -------------------------------------------------------------------------------- /871-keys-and-rooms/README.md: -------------------------------------------------------------------------------- 1 |

Keys and Rooms

Difficulty: Medium

There are n rooms labeled from 0 to n - 1 and all the rooms are locked except for room 0. Your goal is to visit all the rooms. However, you cannot enter a locked room without having its key.

2 | 3 |

When you visit a room, you may find a set of distinct keys in it. Each key has a number on it, denoting which room it unlocks, and you can take all of them with you to unlock the other rooms.

4 | 5 |

Given an array rooms where rooms[i] is the set of keys that you can obtain if you visited room i, return true if you can visit all the rooms, or false otherwise.

6 | 7 |

 

8 |

Example 1:

9 | 10 |
11 | Input: rooms = [[1],[2],[3],[]]
12 | Output: true
13 | Explanation: 
14 | We visit room 0 and pick up key 1.
15 | We then visit room 1 and pick up key 2.
16 | We then visit room 2 and pick up key 3.
17 | We then visit room 3.
18 | Since we were able to visit every room, we return true.
19 | 
20 | 21 |

Example 2:

22 | 23 |
24 | Input: rooms = [[1,3],[3,0,1],[2],[0]]
25 | Output: false
26 | Explanation: We can not enter room number 2 since the only key that unlocks it is in that room.
27 | 
28 | 29 |

 

30 |

Constraints:

31 | 32 |
    33 |
  • n == rooms.length
  • 34 |
  • 2 <= n <= 1000
  • 35 |
  • 0 <= rooms[i].length <= 1000
  • 36 |
  • 1 <= sum(rooms[i].length) <= 3000
  • 37 |
  • 0 <= rooms[i][j] < n
  • 38 |
  • All the values of rooms[i] are unique.
  • 39 |
40 | -------------------------------------------------------------------------------- /1544-count-good-nodes-in-binary-tree/README.md: -------------------------------------------------------------------------------- 1 |

Count Good Nodes in Binary Tree

Difficulty: Medium

Given a binary tree root, a node X in the tree is named good if in the path from root to X there are no nodes with a value greater than X.

2 | 3 |

Return the number of good nodes in the binary tree.

4 | 5 |

 

6 |

Example 1:

7 | 8 |

9 | 10 |
11 | Input: root = [3,1,4,3,null,1,5]
12 | Output: 4
13 | Explanation: Nodes in blue are good.
14 | Root Node (3) is always a good node.
15 | Node 4 -> (3,4) is the maximum value in the path starting from the root.
16 | Node 5 -> (3,4,5) is the maximum value in the path
17 | Node 3 -> (3,1,3) is the maximum value in the path.
18 | 19 |

Example 2:

20 | 21 |

22 | 23 |
24 | Input: root = [3,3,null,4,2]
25 | Output: 3
26 | Explanation: Node 2 -> (3, 3, 2) is not good, because "3" is higher than it.
27 | 28 |

Example 3:

29 | 30 |
31 | Input: root = [1]
32 | Output: 1
33 | Explanation: Root is considered as good.
34 | 35 |

 

36 |

Constraints:

37 | 38 |
    39 |
  • The number of nodes in the binary tree is in the range [1, 10^5].
  • 40 |
  • Each node's value is between [-10^4, 10^4].
  • 41 |
-------------------------------------------------------------------------------- /1392-find-the-difference-of-two-arrays/README.md: -------------------------------------------------------------------------------- 1 |

Find the Difference of Two Arrays

Difficulty: Easy

Given two 0-indexed integer arrays nums1 and nums2, return a list answer of size 2 where:

2 | 3 |
    4 |
  • answer[0] is a list of all distinct integers in nums1 which are not present in nums2.
  • 5 |
  • answer[1] is a list of all distinct integers in nums2 which are not present in nums1.
  • 6 |
7 | 8 |

Note that the integers in the lists may be returned in any order.

9 | 10 |

 

11 |

Example 1:

12 | 13 |
14 | Input: nums1 = [1,2,3], nums2 = [2,4,6]
15 | Output: [[1,3],[4,6]]
16 | Explanation:
17 | For nums1, nums1[1] = 2 is present at index 0 of nums2, whereas nums1[0] = 1 and nums1[2] = 3 are not present in nums2. Therefore, answer[0] = [1,3].
18 | For nums2, nums2[0] = 2 is present at index 1 of nums1, whereas nums2[1] = 4 and nums2[2] = 6 are not present in nums2. Therefore, answer[1] = [4,6].
19 | 20 |

Example 2:

21 | 22 |
23 | Input: nums1 = [1,2,3,3], nums2 = [1,1,2,2]
24 | Output: [[3],[]]
25 | Explanation:
26 | For nums1, nums1[2] and nums1[3] are not present in nums2. Since nums1[2] == nums1[3], their value is only included once and answer[0] = [3].
27 | Every integer in nums2 is present in nums1. Therefore, answer[1] = [].
28 | 
29 | 30 |

 

31 |

Constraints:

32 | 33 |
    34 |
  • 1 <= nums1.length, nums2.length <= 1000
  • 35 |
  • -1000 <= nums1[i], nums2[i] <= 1000
  • 36 |
37 | -------------------------------------------------------------------------------- /236-lowest-common-ancestor-of-a-binary-tree/README.md: -------------------------------------------------------------------------------- 1 |

Lowest Common Ancestor of a Binary Tree

Difficulty: Medium

Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.

2 | 3 |

According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”

4 | 5 |

 

6 |

Example 1:

7 | 8 |
 9 | Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
10 | Output: 3
11 | Explanation: The LCA of nodes 5 and 1 is 3.
12 | 
13 | 14 |

Example 2:

15 | 16 |
17 | Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
18 | Output: 5
19 | Explanation: The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
20 | 
21 | 22 |

Example 3:

23 | 24 |
25 | Input: root = [1,2], p = 1, q = 2
26 | Output: 1
27 | 
28 | 29 |

 

30 |

Constraints:

31 | 32 |
    33 |
  • The number of nodes in the tree is in the range [2, 105].
  • 34 |
  • -109 <= Node.val <= 109
  • 35 |
  • All Node.val are unique.
  • 36 |
  • p != q
  • 37 |
  • p and q will exist in the tree.
  • 38 |
39 | -------------------------------------------------------------------------------- /969-number-of-recent-calls/README.md: -------------------------------------------------------------------------------- 1 |

Number of Recent Calls

Difficulty: Easy

You have a RecentCounter class which counts the number of recent requests within a certain time frame.

2 | 3 |

Implement the RecentCounter class:

4 | 5 |
    6 |
  • RecentCounter() Initializes the counter with zero recent requests.
  • 7 |
  • int ping(int t) Adds a new request at time t, where t represents some time in milliseconds, and returns the number of requests that has happened in the past 3000 milliseconds (including the new request). Specifically, return the number of requests that have happened in the inclusive range [t - 3000, t].
  • 8 |
9 | 10 |

It is guaranteed that every call to ping uses a strictly larger value of t than the previous call.

11 | 12 |

 

13 |

Example 1:

14 | 15 |
16 | Input
17 | ["RecentCounter", "ping", "ping", "ping", "ping"]
18 | [[], [1], [100], [3001], [3002]]
19 | Output
20 | [null, 1, 2, 3, 3]
21 | 
22 | Explanation
23 | RecentCounter recentCounter = new RecentCounter();
24 | recentCounter.ping(1);     // requests = [1], range is [-2999,1], return 1
25 | recentCounter.ping(100);   // requests = [1, 100], range is [-2900,100], return 2
26 | recentCounter.ping(3001);  // requests = [1, 100, 3001], range is [1,3001], return 3
27 | recentCounter.ping(3002);  // requests = [1, 100, 3001, 3002], range is [2,3002], return 3
28 | 
29 | 30 |

 

31 |

Constraints:

32 | 33 |
    34 |
  • 1 <= t <= 109
  • 35 |
  • Each test case will call ping with strictly increasing values of t.
  • 36 |
  • At most 104 calls will be made to ping.
  • 37 |
38 | -------------------------------------------------------------------------------- /399-evaluate-division/evaluate-division.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | map > > div; 4 | 5 | double Bfs(string maqam, string bast) 6 | { 7 | queue > q; 8 | set vis; 9 | 10 | q.push(make_pair(bast, 1.000)); 11 | vis.insert(bast); 12 | while (!q.empty()) 13 | { 14 | double res = q.front().second; 15 | bast = q.front().first; 16 | q.pop(); 17 | 18 | if ( div.find( bast ) == div.end() ) 19 | break ; 20 | 21 | for (int i=0; i calcEquation(vector>& equations, vector& values, vector>& queries) { 35 | for (int i=0; i >(); 39 | if (div.find(equations[i][0]) == div.end()) 40 | div[equations[i][0]] = vector >(); 41 | div[equations[i][1]].push_back(make_pair(equations[i][0],1 / values[i])); 42 | div[equations[i][0]].push_back(make_pair(equations[i][1],values[i])); 43 | } 44 | 45 | vector ans(queries.size(), -1.000 ); 46 | 47 | for (int i=0; iRemoving Stars From a String Difficulty: Medium

You are given a string s, which contains stars *.

2 | 3 |

In one operation, you can:

4 | 5 |
    6 |
  • Choose a star in s.
  • 7 |
  • Remove the closest non-star character to its left, as well as remove the star itself.
  • 8 |
9 | 10 |

Return the string after all stars have been removed.

11 | 12 |

Note:

13 | 14 |
    15 |
  • The input will be generated such that the operation is always possible.
  • 16 |
  • It can be shown that the resulting string will always be unique.
  • 17 |
18 | 19 |

 

20 |

Example 1:

21 | 22 |
23 | Input: s = "leet**cod*e"
24 | Output: "lecoe"
25 | Explanation: Performing the removals from left to right:
26 | - The closest character to the 1st star is 't' in "leet**cod*e". s becomes "lee*cod*e".
27 | - The closest character to the 2nd star is 'e' in "lee*cod*e". s becomes "lecod*e".
28 | - The closest character to the 3rd star is 'd' in "lecod*e". s becomes "lecoe".
29 | There are no more stars, so we return "lecoe".
30 | 31 |

Example 2:

32 | 33 |
34 | Input: s = "erase*****"
35 | Output: ""
36 | Explanation: The entire string is removed, so we return an empty string.
37 | 
38 | 39 |

 

40 |

Constraints:

41 | 42 |
    43 |
  • 1 <= s.length <= 105
  • 44 |
  • s consists of lowercase English letters and stars *.
  • 45 |
  • The operation above can be performed on s.
  • 46 |
47 | -------------------------------------------------------------------------------- /724-find-pivot-index/README.md: -------------------------------------------------------------------------------- 1 |

Find Pivot Index

Difficulty: Easy

Given an array of integers nums, calculate the pivot index of this array.

2 | 3 |

The pivot index is the index where the sum of all the numbers strictly to the left of the index is equal to the sum of all the numbers strictly to the index's right.

4 | 5 |

If the index is on the left edge of the array, then the left sum is 0 because there are no elements to the left. This also applies to the right edge of the array.

6 | 7 |

Return the leftmost pivot index. If no such index exists, return -1.

8 | 9 |

 

10 |

Example 1:

11 | 12 |
13 | Input: nums = [1,7,3,6,5,6]
14 | Output: 3
15 | Explanation:
16 | The pivot index is 3.
17 | Left sum = nums[0] + nums[1] + nums[2] = 1 + 7 + 3 = 11
18 | Right sum = nums[4] + nums[5] = 5 + 6 = 11
19 | 
20 | 21 |

Example 2:

22 | 23 |
24 | Input: nums = [1,2,3]
25 | Output: -1
26 | Explanation:
27 | There is no index that satisfies the conditions in the problem statement.
28 | 29 |

Example 3:

30 | 31 |
32 | Input: nums = [2,1,-1]
33 | Output: 0
34 | Explanation:
35 | The pivot index is 0.
36 | Left sum = 0 (no elements to the left of index 0)
37 | Right sum = nums[1] + nums[2] = 1 + -1 = 0
38 | 
39 | 40 |

 

41 |

Constraints:

42 | 43 |
    44 |
  • 1 <= nums.length <= 104
  • 45 |
  • -1000 <= nums[i] <= 1000
  • 46 |
47 | 48 |

 

49 |

Note: This question is the same as 1991: https://leetcode.com/problems/find-the-middle-index-in-array/

50 | -------------------------------------------------------------------------------- /1474-longest-zigzag-path-in-a-binary-tree/README.md: -------------------------------------------------------------------------------- 1 |

Longest ZigZag Path in a Binary Tree

Difficulty: Medium

You are given the root of a binary tree.

2 | 3 |

A ZigZag path for a binary tree is defined as follow:

4 | 5 |
    6 |
  • Choose any node in the binary tree and a direction (right or left).
  • 7 |
  • If the current direction is right, move to the right child of the current node; otherwise, move to the left child.
  • 8 |
  • Change the direction from right to left or from left to right.
  • 9 |
  • Repeat the second and third steps until you can't move in the tree.
  • 10 |
11 | 12 |

Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0).

13 | 14 |

Return the longest ZigZag path contained in that tree.

15 | 16 |

 

17 |

Example 1:

18 | 19 |
20 | Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1]
21 | Output: 3
22 | Explanation: Longest ZigZag path in blue nodes (right -> left -> right).
23 | 
24 | 25 |

Example 2:

26 | 27 |
28 | Input: root = [1,1,1,null,1,null,null,1,1,null,1]
29 | Output: 4
30 | Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right).
31 | 
32 | 33 |

Example 3:

34 | 35 |
36 | Input: root = [1]
37 | Output: 0
38 | 
39 | 40 |

 

41 |

Constraints:

42 | 43 |
    44 |
  • The number of nodes in the tree is in the range [1, 5 * 104].
  • 45 |
  • 1 <= Node.val <= 100
  • 46 |
47 | -------------------------------------------------------------------------------- /151-reverse-words-in-a-string/README.md: -------------------------------------------------------------------------------- 1 |

Reverse Words in a String

Difficulty: Medium

Given an input string s, reverse the order of the words.

2 | 3 |

A word is defined as a sequence of non-space characters. The words in s will be separated by at least one space.

4 | 5 |

Return a string of the words in reverse order concatenated by a single space.

6 | 7 |

Note that s may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces.

8 | 9 |

 

10 |

Example 1:

11 | 12 |
13 | Input: s = "the sky is blue"
14 | Output: "blue is sky the"
15 | 
16 | 17 |

Example 2:

18 | 19 |
20 | Input: s = "  hello world  "
21 | Output: "world hello"
22 | Explanation: Your reversed string should not contain leading or trailing spaces.
23 | 
24 | 25 |

Example 3:

26 | 27 |
28 | Input: s = "a good   example"
29 | Output: "example good a"
30 | Explanation: You need to reduce multiple spaces between two words to a single space in the reversed string.
31 | 
32 | 33 |

 

34 |

Constraints:

35 | 36 |
    37 |
  • 1 <= s.length <= 104
  • 38 |
  • s contains English letters (upper-case and lower-case), digits, and spaces ' '.
  • 39 |
  • There is at least one word in s.
  • 40 |
41 | 42 |

 

43 |

Follow-up: If the string data type is mutable in your language, can you solve it in-place with O(1) extra space?

44 | -------------------------------------------------------------------------------- /450-delete-node-in-a-bst/README.md: -------------------------------------------------------------------------------- 1 |

Delete Node in a BST

Difficulty: Medium

Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference (possibly updated) of the BST.

2 | 3 |

Basically, the deletion can be divided into two stages:

4 | 5 |
    6 |
  1. Search for a node to remove.
  2. 7 |
  3. If the node is found, delete the node.
  4. 8 |
9 | 10 |

 

11 |

Example 1:

12 | 13 |
14 | Input: root = [5,3,6,2,4,null,7], key = 3
15 | Output: [5,4,6,2,null,null,7]
16 | Explanation: Given key to delete is 3. So we find the node with value 3 and delete it.
17 | One valid answer is [5,4,6,2,null,null,7], shown in the above BST.
18 | Please notice that another valid answer is [5,2,6,null,4,null,7] and it's also accepted.
19 | 
20 | 
21 | 22 |

Example 2:

23 | 24 |
25 | Input: root = [5,3,6,2,4,null,7], key = 0
26 | Output: [5,3,6,2,4,null,7]
27 | Explanation: The tree does not contain a node with value = 0.
28 | 
29 | 30 |

Example 3:

31 | 32 |
33 | Input: root = [], key = 0
34 | Output: []
35 | 
36 | 37 |

 

38 |

Constraints:

39 | 40 |
    41 |
  • The number of nodes in the tree is in the range [0, 104].
  • 42 |
  • -105 <= Node.val <= 105
  • 43 |
  • Each node has a unique value.
  • 44 |
  • root is a valid binary search tree.
  • 45 |
  • -105 <= key <= 105
  • 46 |
47 | 48 |

 

49 |

Follow up: Could you solve it with time complexity O(height of tree)?

50 | -------------------------------------------------------------------------------- /208-implement-trie-prefix-tree/README.md: -------------------------------------------------------------------------------- 1 |

Implement Trie (Prefix Tree)

Difficulty: Medium

A trie (pronounced as "try") or prefix tree is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There are various applications of this data structure, such as autocomplete and spellchecker.

2 | 3 |

Implement the Trie class:

4 | 5 |
    6 |
  • Trie() Initializes the trie object.
  • 7 |
  • void insert(String word) Inserts the string word into the trie.
  • 8 |
  • boolean search(String word) Returns true if the string word is in the trie (i.e., was inserted before), and false otherwise.
  • 9 |
  • boolean startsWith(String prefix) Returns true if there is a previously inserted string word that has the prefix prefix, and false otherwise.
  • 10 |
11 | 12 |

 

13 |

Example 1:

14 | 15 |
16 | Input
17 | ["Trie", "insert", "search", "search", "startsWith", "insert", "search"]
18 | [[], ["apple"], ["apple"], ["app"], ["app"], ["app"], ["app"]]
19 | Output
20 | [null, null, true, false, true, null, true]
21 | 
22 | Explanation
23 | Trie trie = new Trie();
24 | trie.insert("apple");
25 | trie.search("apple");   // return True
26 | trie.search("app");     // return False
27 | trie.startsWith("app"); // return True
28 | trie.insert("app");
29 | trie.search("app");     // return True
30 | 
31 | 32 |

 

33 |

Constraints:

34 | 35 |
    36 |
  • 1 <= word.length, prefix.length <= 2000
  • 37 |
  • word and prefix consist only of lowercase English letters.
  • 38 |
  • At most 3 * 104 calls in total will be made to insert, search, and startsWith.
  • 39 |
40 | -------------------------------------------------------------------------------- /2413-smallest-number-in-infinite-set/README.md: -------------------------------------------------------------------------------- 1 |

Smallest Number in Infinite Set

Difficulty: Medium

You have a set which contains all positive integers [1, 2, 3, 4, 5, ...].

2 | 3 |

Implement the SmallestInfiniteSet class:

4 | 5 |
    6 |
  • SmallestInfiniteSet() Initializes the SmallestInfiniteSet object to contain all positive integers.
  • 7 |
  • int popSmallest() Removes and returns the smallest integer contained in the infinite set.
  • 8 |
  • void addBack(int num) Adds a positive integer num back into the infinite set, if it is not already in the infinite set.
  • 9 |
10 | 11 |

 

12 |

Example 1:

13 | 14 |
15 | Input
16 | ["SmallestInfiniteSet", "addBack", "popSmallest", "popSmallest", "popSmallest", "addBack", "popSmallest", "popSmallest", "popSmallest"]
17 | [[], [2], [], [], [], [1], [], [], []]
18 | Output
19 | [null, null, 1, 2, 3, null, 1, 4, 5]
20 | 
21 | Explanation
22 | SmallestInfiniteSet smallestInfiniteSet = new SmallestInfiniteSet();
23 | smallestInfiniteSet.addBack(2);    // 2 is already in the set, so no change is made.
24 | smallestInfiniteSet.popSmallest(); // return 1, since 1 is the smallest number, and remove it from the set.
25 | smallestInfiniteSet.popSmallest(); // return 2, and remove it from the set.
26 | smallestInfiniteSet.popSmallest(); // return 3, and remove it from the set.
27 | smallestInfiniteSet.addBack(1);    // 1 is added back to the set.
28 | smallestInfiniteSet.popSmallest(); // return 1, since 1 was added back to the set and
29 |                                    // is the smallest number, and remove it from the set.
30 | smallestInfiniteSet.popSmallest(); // return 4, and remove it from the set.
31 | smallestInfiniteSet.popSmallest(); // return 5, and remove it from the set.
32 | 
33 | 34 |

 

35 |

Constraints:

36 | 37 |
    38 |
  • 1 <= num <= 1000
  • 39 |
  • At most 1000 calls will be made in total to popSmallest and addBack.
  • 40 |
41 | -------------------------------------------------------------------------------- /2636-maximum-subsequence-score/README.md: -------------------------------------------------------------------------------- 1 |

Maximum Subsequence Score

Difficulty: Medium

You are given two 0-indexed integer arrays nums1 and nums2 of equal length n and a positive integer k. You must choose a subsequence of indices from nums1 of length k.

2 | 3 |

For chosen indices i0, i1, ..., ik - 1, your score is defined as:

4 | 5 |
    6 |
  • The sum of the selected elements from nums1 multiplied with the minimum of the selected elements from nums2.
  • 7 |
  • It can defined simply as: (nums1[i0] + nums1[i1] +...+ nums1[ik - 1]) * min(nums2[i0] , nums2[i1], ... ,nums2[ik - 1]).
  • 8 |
9 | 10 |

Return the maximum possible score.

11 | 12 |

A subsequence of indices of an array is a set that can be derived from the set {0, 1, ..., n-1} by deleting some or no elements.

13 | 14 |

 

15 |

Example 1:

16 | 17 |
18 | Input: nums1 = [1,3,3,2], nums2 = [2,1,3,4], k = 3
19 | Output: 12
20 | Explanation: 
21 | The four possible subsequence scores are:
22 | - We choose the indices 0, 1, and 2 with score = (1+3+3) * min(2,1,3) = 7.
23 | - We choose the indices 0, 1, and 3 with score = (1+3+2) * min(2,1,4) = 6. 
24 | - We choose the indices 0, 2, and 3 with score = (1+3+2) * min(2,3,4) = 12. 
25 | - We choose the indices 1, 2, and 3 with score = (3+3+2) * min(1,3,4) = 8.
26 | Therefore, we return the max score, which is 12.
27 | 
28 | 29 |

Example 2:

30 | 31 |
32 | Input: nums1 = [4,2,3,1,1], nums2 = [7,5,10,9,6], k = 1
33 | Output: 30
34 | Explanation: 
35 | Choosing index 2 is optimal: nums1[2] * nums2[2] = 3 * 10 = 30 is the maximum possible score.
36 | 
37 | 38 |

 

39 |

Constraints:

40 | 41 |
    42 |
  • n == nums1.length == nums2.length
  • 43 |
  • 1 <= n <= 105
  • 44 |
  • 0 <= nums1[i], nums2[j] <= 105
  • 45 |
  • 1 <= k <= n
  • 46 |
47 | -------------------------------------------------------------------------------- /937-online-stock-span/README.md: -------------------------------------------------------------------------------- 1 |

Online Stock Span

Difficulty: Medium

Design an algorithm that collects daily price quotes for some stock and returns the span of that stock's price for the current day.

2 | 3 |

The span of the stock's price in one day is the maximum number of consecutive days (starting from that day and going backward) for which the stock price was less than or equal to the price of that day.

4 | 5 |
    6 |
  • For example, if the prices of the stock in the last four days is [7,2,1,2] and the price of the stock today is 2, then the span of today is 4 because starting from today, the price of the stock was less than or equal 2 for 4 consecutive days.
  • 7 |
  • Also, if the prices of the stock in the last four days is [7,34,1,2] and the price of the stock today is 8, then the span of today is 3 because starting from today, the price of the stock was less than or equal 8 for 3 consecutive days.
  • 8 |
9 | 10 |

Implement the StockSpanner class:

11 | 12 |
    13 |
  • StockSpanner() Initializes the object of the class.
  • 14 |
  • int next(int price) Returns the span of the stock's price given that today's price is price.
  • 15 |
16 | 17 |

 

18 |

Example 1:

19 | 20 |
21 | Input
22 | ["StockSpanner", "next", "next", "next", "next", "next", "next", "next"]
23 | [[], [100], [80], [60], [70], [60], [75], [85]]
24 | Output
25 | [null, 1, 1, 1, 2, 1, 4, 6]
26 | 
27 | Explanation
28 | StockSpanner stockSpanner = new StockSpanner();
29 | stockSpanner.next(100); // return 1
30 | stockSpanner.next(80);  // return 1
31 | stockSpanner.next(60);  // return 1
32 | stockSpanner.next(70);  // return 2
33 | stockSpanner.next(60);  // return 1
34 | stockSpanner.next(75);  // return 4, because the last 4 prices (including today's price of 75) were less than or equal to today's price.
35 | stockSpanner.next(85);  // return 6
36 | 
37 | 38 |

 

39 |

Constraints:

40 | 41 |
    42 |
  • 1 <= price <= 105
  • 43 |
  • At most 104 calls will be made to next.
  • 44 |
45 | -------------------------------------------------------------------------------- /1528-kids-with-the-greatest-number-of-candies/README.md: -------------------------------------------------------------------------------- 1 |

Kids With the Greatest Number of Candies

Difficulty: Easy

There are n kids with candies. You are given an integer array candies, where each candies[i] represents the number of candies the ith kid has, and an integer extraCandies, denoting the number of extra candies that you have.

2 | 3 |

Return a boolean array result of length n, where result[i] is true if, after giving the ith kid all the extraCandies, they will have the greatest number of candies among all the kids, or false otherwise.

4 | 5 |

Note that multiple kids can have the greatest number of candies.

6 | 7 |

 

8 |

Example 1:

9 | 10 |
11 | Input: candies = [2,3,5,1,3], extraCandies = 3
12 | Output: [true,true,true,false,true] 
13 | Explanation: If you give all extraCandies to:
14 | - Kid 1, they will have 2 + 3 = 5 candies, which is the greatest among the kids.
15 | - Kid 2, they will have 3 + 3 = 6 candies, which is the greatest among the kids.
16 | - Kid 3, they will have 5 + 3 = 8 candies, which is the greatest among the kids.
17 | - Kid 4, they will have 1 + 3 = 4 candies, which is not the greatest among the kids.
18 | - Kid 5, they will have 3 + 3 = 6 candies, which is the greatest among the kids.
19 | 
20 | 21 |

Example 2:

22 | 23 |
24 | Input: candies = [4,2,1,1,2], extraCandies = 1
25 | Output: [true,false,false,false,false] 
26 | Explanation: There is only 1 extra candy.
27 | Kid 1 will always have the greatest number of candies, even if a different kid is given the extra candy.
28 | 
29 | 30 |

Example 3:

31 | 32 |
33 | Input: candies = [12,1,12], extraCandies = 10
34 | Output: [true,false,true]
35 | 
36 | 37 |

 

38 |

Constraints:

39 | 40 |
    41 |
  • n == candies.length
  • 42 |
  • 2 <= n <= 100
  • 43 |
  • 1 <= candies[i] <= 100
  • 44 |
  • 1 <= extraCandies <= 50
  • 45 |
46 | -------------------------------------------------------------------------------- /1576-reorder-routes-to-make-all-paths-lead-to-the-city-zero/README.md: -------------------------------------------------------------------------------- 1 |

Reorder Routes to Make All Paths Lead to the City Zero

Difficulty: Medium

There are n cities numbered from 0 to n - 1 and n - 1 roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.

2 | 3 |

Roads are represented by connections where connections[i] = [ai, bi] represents a road from city ai to city bi.

4 | 5 |

This year, there will be a big event in the capital (city 0), and many people want to travel to this city.

6 | 7 |

Your task consists of reorienting some roads such that each city can visit the city 0. Return the minimum number of edges changed.

8 | 9 |

It's guaranteed that each city can reach city 0 after reorder.

10 | 11 |

 

12 |

Example 1:

13 | 14 |
15 | Input: n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
16 | Output: 3
17 | Explanation: Change the direction of edges show in red such that each node can reach the node 0 (capital).
18 | 
19 | 20 |

Example 2:

21 | 22 |
23 | Input: n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
24 | Output: 2
25 | Explanation: Change the direction of edges show in red such that each node can reach the node 0 (capital).
26 | 
27 | 28 |

Example 3:

29 | 30 |
31 | Input: n = 3, connections = [[1,0],[2,0]]
32 | Output: 0
33 | 
34 | 35 |

 

36 |

Constraints:

37 | 38 |
    39 |
  • 2 <= n <= 5 * 104
  • 40 |
  • connections.length == n - 1
  • 41 |
  • connections[i].length == 2
  • 42 |
  • 0 <= ai, bi <= n - 1
  • 43 |
  • ai != bi
  • 44 |
45 | -------------------------------------------------------------------------------- /452-minimum-number-of-arrows-to-burst-balloons/README.md: -------------------------------------------------------------------------------- 1 |

Minimum Number of Arrows to Burst Balloons

Difficulty: Medium

There are some spherical balloons taped onto a flat wall that represents the XY-plane. The balloons are represented as a 2D integer array points where points[i] = [xstart, xend] denotes a balloon whose horizontal diameter stretches between xstart and xend. You do not know the exact y-coordinates of the balloons.

2 | 3 |

Arrows can be shot up directly vertically (in the positive y-direction) from different points along the x-axis. A balloon with xstart and xend is burst by an arrow shot at x if xstart <= x <= xend. There is no limit to the number of arrows that can be shot. A shot arrow keeps traveling up infinitely, bursting any balloons in its path.

4 | 5 |

Given the array points, return the minimum number of arrows that must be shot to burst all balloons.

6 | 7 |

 

8 |

Example 1:

9 | 10 |
11 | Input: points = [[10,16],[2,8],[1,6],[7,12]]
12 | Output: 2
13 | Explanation: The balloons can be burst by 2 arrows:
14 | - Shoot an arrow at x = 6, bursting the balloons [2,8] and [1,6].
15 | - Shoot an arrow at x = 11, bursting the balloons [10,16] and [7,12].
16 | 
17 | 18 |

Example 2:

19 | 20 |
21 | Input: points = [[1,2],[3,4],[5,6],[7,8]]
22 | Output: 4
23 | Explanation: One arrow needs to be shot for each balloon for a total of 4 arrows.
24 | 
25 | 26 |

Example 3:

27 | 28 |
29 | Input: points = [[1,2],[2,3],[3,4],[4,5]]
30 | Output: 2
31 | Explanation: The balloons can be burst by 2 arrows:
32 | - Shoot an arrow at x = 2, bursting the balloons [1,2] and [2,3].
33 | - Shoot an arrow at x = 4, bursting the balloons [3,4] and [4,5].
34 | 
35 | 36 |

 

37 |

Constraints:

38 | 39 |
    40 |
  • 1 <= points.length <= 105
  • 41 |
  • points[i].length == 2
  • 42 |
  • -231 <= xstart < xend <= 231 - 1
  • 43 |
44 | -------------------------------------------------------------------------------- /2392-successful-pairs-of-spells-and-potions/README.md: -------------------------------------------------------------------------------- 1 |

Successful Pairs of Spells and Potions

Difficulty: Medium

You are given two positive integer arrays spells and potions, of length n and m respectively, where spells[i] represents the strength of the ith spell and potions[j] represents the strength of the jth potion.

2 | 3 |

You are also given an integer success. A spell and potion pair is considered successful if the product of their strengths is at least success.

4 | 5 |

Return an integer array pairs of length n where pairs[i] is the number of potions that will form a successful pair with the ith spell.

6 | 7 |

 

8 |

Example 1:

9 | 10 |
11 | Input: spells = [5,1,3], potions = [1,2,3,4,5], success = 7
12 | Output: [4,0,3]
13 | Explanation:
14 | - 0th spell: 5 * [1,2,3,4,5] = [5,10,15,20,25]. 4 pairs are successful.
15 | - 1st spell: 1 * [1,2,3,4,5] = [1,2,3,4,5]. 0 pairs are successful.
16 | - 2nd spell: 3 * [1,2,3,4,5] = [3,6,9,12,15]. 3 pairs are successful.
17 | Thus, [4,0,3] is returned.
18 | 
19 | 20 |

Example 2:

21 | 22 |
23 | Input: spells = [3,1,2], potions = [8,5,8], success = 16
24 | Output: [2,0,2]
25 | Explanation:
26 | - 0th spell: 3 * [8,5,8] = [24,15,24]. 2 pairs are successful.
27 | - 1st spell: 1 * [8,5,8] = [8,5,8]. 0 pairs are successful. 
28 | - 2nd spell: 2 * [8,5,8] = [16,10,16]. 2 pairs are successful. 
29 | Thus, [2,0,2] is returned.
30 | 
31 | 32 |

 

33 |

Constraints:

34 | 35 |
    36 |
  • n == spells.length
  • 37 |
  • m == potions.length
  • 38 |
  • 1 <= n, m <= 105
  • 39 |
  • 1 <= spells[i], potions[i] <= 105
  • 40 |
  • 1 <= success <= 1010
  • 41 |
42 | -------------------------------------------------------------------------------- /1397-search-suggestions-system/README.md: -------------------------------------------------------------------------------- 1 |

Search Suggestions System

Difficulty: Medium

You are given an array of strings products and a string searchWord.

2 | 3 |

Design a system that suggests at most three product names from products after each character of searchWord is typed. Suggested products should have common prefix with searchWord. If there are more than three products with a common prefix return the three lexicographically minimums products.

4 | 5 |

Return a list of lists of the suggested products after each character of searchWord is typed.

6 | 7 |

 

8 |

Example 1:

9 | 10 |
11 | Input: products = ["mobile","mouse","moneypot","monitor","mousepad"], searchWord = "mouse"
12 | Output: [["mobile","moneypot","monitor"],["mobile","moneypot","monitor"],["mouse","mousepad"],["mouse","mousepad"],["mouse","mousepad"]]
13 | Explanation: products sorted lexicographically = ["mobile","moneypot","monitor","mouse","mousepad"].
14 | After typing m and mo all products match and we show user ["mobile","moneypot","monitor"].
15 | After typing mou, mous and mouse the system suggests ["mouse","mousepad"].
16 | 
17 | 18 |

Example 2:

19 | 20 |
21 | Input: products = ["havana"], searchWord = "havana"
22 | Output: [["havana"],["havana"],["havana"],["havana"],["havana"],["havana"]]
23 | Explanation: The only word "havana" will be always suggested while typing the search word.
24 | 
25 | 26 |

 

27 |

Constraints:

28 | 29 |
    30 |
  • 1 <= products.length <= 1000
  • 31 |
  • 1 <= products[i].length <= 3000
  • 32 |
  • 1 <= sum(products[i].length) <= 2 * 104
  • 33 |
  • All the strings of products are unique.
  • 34 |
  • products[i] consists of lowercase English letters.
  • 35 |
  • 1 <= searchWord.length <= 1000
  • 36 |
  • searchWord consists of lowercase English letters.
  • 37 |
38 | -------------------------------------------------------------------------------- /2216-delete-the-middle-node-of-a-linked-list/README.md: -------------------------------------------------------------------------------- 1 |

Delete the Middle Node of a Linked List

Difficulty: Medium

You are given the head of a linked list. Delete the middle node, and return the head of the modified linked list.

2 | 3 |

The middle node of a linked list of size n is the ⌊n / 2⌋th node from the start using 0-based indexing, where ⌊x⌋ denotes the largest integer less than or equal to x.

4 | 5 |
    6 |
  • For n = 1, 2, 3, 4, and 5, the middle nodes are 0, 1, 1, 2, and 2, respectively.
  • 7 |
8 | 9 |

 

10 |

Example 1:

11 | 12 |
13 | Input: head = [1,3,4,7,1,2,6]
14 | Output: [1,3,4,1,2,6]
15 | Explanation:
16 | The above figure represents the given linked list. The indices of the nodes are written below.
17 | Since n = 7, node 3 with value 7 is the middle node, which is marked in red.
18 | We return the new list after removing this node. 
19 | 
20 | 21 |

Example 2:

22 | 23 |
24 | Input: head = [1,2,3,4]
25 | Output: [1,2,4]
26 | Explanation:
27 | The above figure represents the given linked list.
28 | For n = 4, node 2 with value 3 is the middle node, which is marked in red.
29 | 
30 | 31 |

Example 3:

32 | 33 |
34 | Input: head = [2,1]
35 | Output: [2]
36 | Explanation:
37 | The above figure represents the given linked list.
38 | For n = 2, node 1 with value 1 is the middle node, which is marked in red.
39 | Node 0 with value 2 is the only node remaining after removing node 1.
40 | 41 |

 

42 |

Constraints:

43 | 44 |
    45 |
  • The number of nodes in the list is in the range [1, 105].
  • 46 |
  • 1 <= Node.val <= 105
  • 47 |
48 | -------------------------------------------------------------------------------- /2236-maximum-twin-sum-of-a-linked-list/README.md: -------------------------------------------------------------------------------- 1 |

Maximum Twin Sum of a Linked List

Difficulty: Medium

In a linked list of size n, where n is even, the ith node (0-indexed) of the linked list is known as the twin of the (n-1-i)th node, if 0 <= i <= (n / 2) - 1.

2 | 3 |
    4 |
  • For example, if n = 4, then node 0 is the twin of node 3, and node 1 is the twin of node 2. These are the only nodes with twins for n = 4.
  • 5 |
6 | 7 |

The twin sum is defined as the sum of a node and its twin.

8 | 9 |

Given the head of a linked list with even length, return the maximum twin sum of the linked list.

10 | 11 |

 

12 |

Example 1:

13 | 14 |
15 | Input: head = [5,4,2,1]
16 | Output: 6
17 | Explanation:
18 | Nodes 0 and 1 are the twins of nodes 3 and 2, respectively. All have twin sum = 6.
19 | There are no other nodes with twins in the linked list.
20 | Thus, the maximum twin sum of the linked list is 6. 
21 | 
22 | 23 |

Example 2:

24 | 25 |
26 | Input: head = [4,2,2,3]
27 | Output: 7
28 | Explanation:
29 | The nodes with twins present in this linked list are:
30 | - Node 0 is the twin of node 3 having a twin sum of 4 + 3 = 7.
31 | - Node 1 is the twin of node 2 having a twin sum of 2 + 2 = 4.
32 | Thus, the maximum twin sum of the linked list is max(7, 4) = 7. 
33 | 
34 | 35 |

Example 3:

36 | 37 |
38 | Input: head = [1,100000]
39 | Output: 100001
40 | Explanation:
41 | There is only one node with a twin in the linked list having twin sum of 1 + 100000 = 100001.
42 | 
43 | 44 |

 

45 |

Constraints:

46 | 47 |
    48 |
  • The number of nodes in the list is an even integer in the range [2, 105].
  • 49 |
  • 1 <= Node.val <= 105
  • 50 |
51 | -------------------------------------------------------------------------------- /1397-search-suggestions-system/search-suggestions-system.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | private: 3 | class Trie { 4 | public: 5 | vector alpha; 6 | bool end; 7 | 8 | Trie() { 9 | end = false; 10 | 11 | alpha.resize( 26, NULL ); 12 | } 13 | 14 | void insert(string word) { 15 | Trie* curr = this; 16 | 17 | for ( int i=0; ialpha[word[i] - 'a'] ) 20 | curr->alpha[word[i] - 'a'] = new Trie(); 21 | 22 | curr = curr->alpha[word[i] - 'a']; 23 | } 24 | 25 | curr->end = true; 26 | } 27 | 28 | bool search(string word) { 29 | Trie* curr = this; 30 | 31 | for ( int i=0; ialpha[word[i] - 'a']) 34 | return ( false ); 35 | 36 | curr = curr->alpha[word[i] - 'a']; 37 | } 38 | 39 | return ( curr->end ); 40 | } 41 | 42 | void startsWith(string& prefix, vector& ans, string& curr, Trie* system, int index ) { 43 | if ( ans.size() >= 3 ) 44 | return ; 45 | 46 | if ( index < prefix.length() and !system->alpha[ prefix[index] - 'a' ] ) 47 | return ; 48 | 49 | if ( index < prefix.length() ) 50 | { 51 | startsWith( prefix, ans, curr, system->alpha[prefix[index] - 'a'], index + 1 ); 52 | return ; 53 | } 54 | 55 | if ( system->end ) 56 | ans.push_back( curr ); 57 | 58 | for ( int i=0; i<26; i++ ) 59 | { 60 | if ( !system->alpha[i] ) 61 | continue ; 62 | curr.push_back( i + 'a' ); 63 | startsWith( prefix, ans, curr, system->alpha[i], index + 1); 64 | if ( ans.size() >= 3 ) 65 | return ; 66 | curr.pop_back( ); 67 | } 68 | } 69 | }; 70 | 71 | /** 72 | * Your Trie object will be instantiated and called as such: 73 | * Trie* obj = new Trie(); 74 | * obj->insert(word); 75 | * bool param_2 = obj->search(word); 76 | * bool param_3 = obj->startsWith(prefix); 77 | */ 78 | public: 79 | vector> suggestedProducts(vector& products, string searchWord) { 80 | Trie* system_complet = new Trie(); 81 | 82 | vector> ans( searchWord.length() ); 83 | 84 | sort( products.begin(), products.end() ); 85 | 86 | for ( int i=0; iinsert( products[i] ); 88 | 89 | for ( int i=0; istartsWith( prefix, ans[i], curr, system_complet, 0 ); 95 | } 96 | 97 | return ( ans ); 98 | } 99 | }; -------------------------------------------------------------------------------- /443-string-compression/README.md: -------------------------------------------------------------------------------- 1 |

String Compression

Difficulty: Medium

Given an array of characters chars, compress it using the following algorithm:

2 | 3 |

Begin with an empty string s. For each group of consecutive repeating characters in chars:

4 | 5 |
    6 |
  • If the group's length is 1, append the character to s.
  • 7 |
  • Otherwise, append the character followed by the group's length.
  • 8 |
9 | 10 |

The compressed string s should not be returned separately, but instead, be stored in the input character array chars. Note that group lengths that are 10 or longer will be split into multiple characters in chars.

11 | 12 |

After you are done modifying the input array, return the new length of the array.

13 | 14 |

You must write an algorithm that uses only constant extra space.

15 | 16 |

 

17 |

Example 1:

18 | 19 |
20 | Input: chars = ["a","a","b","b","c","c","c"]
21 | Output: Return 6, and the first 6 characters of the input array should be: ["a","2","b","2","c","3"]
22 | Explanation: The groups are "aa", "bb", and "ccc". This compresses to "a2b2c3".
23 | 
24 | 25 |

Example 2:

26 | 27 |
28 | Input: chars = ["a"]
29 | Output: Return 1, and the first character of the input array should be: ["a"]
30 | Explanation: The only group is "a", which remains uncompressed since it's a single character.
31 | 
32 | 33 |

Example 3:

34 | 35 |
36 | Input: chars = ["a","b","b","b","b","b","b","b","b","b","b","b","b"]
37 | Output: Return 4, and the first 4 characters of the input array should be: ["a","b","1","2"].
38 | Explanation: The groups are "a" and "bbbbbbbbbbbb". This compresses to "ab12".
39 | 40 |

 

41 |

Constraints:

42 | 43 |
    44 |
  • 1 <= chars.length <= 2000
  • 45 |
  • chars[i] is a lowercase English letter, uppercase English letter, digit, or symbol.
  • 46 |
47 | -------------------------------------------------------------------------------- /1777-determine-if-two-strings-are-close/README.md: -------------------------------------------------------------------------------- 1 |

Determine if Two Strings Are Close

Difficulty: Medium

Two strings are considered close if you can attain one from the other using the following operations:

2 | 3 |
    4 |
  • Operation 1: Swap any two existing characters. 5 | 6 |
      7 |
    • For example, abcde -> aecdb
    • 8 |
    9 |
  • 10 |
  • Operation 2: Transform every occurrence of one existing character into another existing character, and do the same with the other character. 11 |
      12 |
    • For example, aacabb -> bbcbaa (all a's turn into b's, and all b's turn into a's)
    • 13 |
    14 |
  • 15 |
16 | 17 |

You can use the operations on either string as many times as necessary.

18 | 19 |

Given two strings, word1 and word2, return true if word1 and word2 are close, and false otherwise.

20 | 21 |

 

22 |

Example 1:

23 | 24 |
25 | Input: word1 = "abc", word2 = "bca"
26 | Output: true
27 | Explanation: You can attain word2 from word1 in 2 operations.
28 | Apply Operation 1: "abc" -> "acb"
29 | Apply Operation 1: "acb" -> "bca"
30 | 
31 | 32 |

Example 2:

33 | 34 |
35 | Input: word1 = "a", word2 = "aa"
36 | Output: false
37 | Explanation: It is impossible to attain word2 from word1, or vice versa, in any number of operations.
38 | 
39 | 40 |

Example 3:

41 | 42 |
43 | Input: word1 = "cabbba", word2 = "abbccc"
44 | Output: true
45 | Explanation: You can attain word2 from word1 in 3 operations.
46 | Apply Operation 1: "cabbba" -> "caabbb"
47 | Apply Operation 2: "caabbb" -> "baaccc"
48 | Apply Operation 2: "baaccc" -> "abbccc"
49 | 
50 | 51 |

 

52 |

Constraints:

53 | 54 |
    55 |
  • 1 <= word1.length, word2.length <= 105
  • 56 |
  • word1 and word2 contain only lowercase English letters.
  • 57 |
58 | -------------------------------------------------------------------------------- /649-dota2-senate/README.md: -------------------------------------------------------------------------------- 1 |

Dota2 Senate

Difficulty: Medium

In the world of Dota2, there are two parties: the Radiant and the Dire.

2 | 3 |

The Dota2 senate consists of senators coming from two parties. Now the Senate wants to decide on a change in the Dota2 game. The voting for this change is a round-based procedure. In each round, each senator can exercise one of the two rights:

4 | 5 |
    6 |
  • Ban one senator's right: A senator can make another senator lose all his rights in this and all the following rounds.
  • 7 |
  • Announce the victory: If this senator found the senators who still have rights to vote are all from the same party, he can announce the victory and decide on the change in the game.
  • 8 |
9 | 10 |

Given a string senate representing each senator's party belonging. The character 'R' and 'D' represent the Radiant party and the Dire party. Then if there are n senators, the size of the given string will be n.

11 | 12 |

The round-based procedure starts from the first senator to the last senator in the given order. This procedure will last until the end of voting. All the senators who have lost their rights will be skipped during the procedure.

13 | 14 |

Suppose every senator is smart enough and will play the best strategy for his own party. Predict which party will finally announce the victory and change the Dota2 game. The output should be "Radiant" or "Dire".

15 | 16 |

 

17 |

Example 1:

18 | 19 |
20 | Input: senate = "RD"
21 | Output: "Radiant"
22 | Explanation: 
23 | The first senator comes from Radiant and he can just ban the next senator's right in round 1. 
24 | And the second senator can't exercise any rights anymore since his right has been banned. 
25 | And in round 2, the first senator can just announce the victory since he is the only guy in the senate who can vote.
26 | 
27 | 28 |

Example 2:

29 | 30 |
31 | Input: senate = "RDD"
32 | Output: "Dire"
33 | Explanation: 
34 | The first senator comes from Radiant and he can just ban the next senator's right in round 1. 
35 | And the second senator can't exercise any rights anymore since his right has been banned. 
36 | And the third senator comes from Dire and he can ban the first senator's right in round 1. 
37 | And in round 2, the third senator can just announce the victory since he is the only guy in the senate who can vote.
38 | 
39 | 40 |

 

41 |

Constraints:

42 | 43 |
    44 |
  • n == senate.length
  • 45 |
  • 1 <= n <= 104
  • 46 |
  • senate[i] is either 'R' or 'D'.
  • 47 |
48 | -------------------------------------------------------------------------------- /399-evaluate-division/README.md: -------------------------------------------------------------------------------- 1 |

Evaluate Division

Difficulty: Medium

You are given an array of variable pairs equations and an array of real numbers values, where equations[i] = [Ai, Bi] and values[i] represent the equation Ai / Bi = values[i]. Each Ai or Bi is a string that represents a single variable.

2 | 3 |

You are also given some queries, where queries[j] = [Cj, Dj] represents the jth query where you must find the answer for Cj / Dj = ?.

4 | 5 |

Return the answers to all queries. If a single answer cannot be determined, return -1.0.

6 | 7 |

Note: The input is always valid. You may assume that evaluating the queries will not result in division by zero and that there is no contradiction.

8 | 9 |

Note: The variables that do not occur in the list of equations are undefined, so the answer cannot be determined for them.

10 | 11 |

 

12 |

Example 1:

13 | 14 |
15 | Input: equations = [["a","b"],["b","c"]], values = [2.0,3.0], queries = [["a","c"],["b","a"],["a","e"],["a","a"],["x","x"]]
16 | Output: [6.00000,0.50000,-1.00000,1.00000,-1.00000]
17 | Explanation: 
18 | Given: a / b = 2.0, b / c = 3.0
19 | queries are: a / c = ?, b / a = ?, a / e = ?, a / a = ?, x / x = ? 
20 | return: [6.0, 0.5, -1.0, 1.0, -1.0 ]
21 | note: x is undefined => -1.0
22 | 23 |

Example 2:

24 | 25 |
26 | Input: equations = [["a","b"],["b","c"],["bc","cd"]], values = [1.5,2.5,5.0], queries = [["a","c"],["c","b"],["bc","cd"],["cd","bc"]]
27 | Output: [3.75000,0.40000,5.00000,0.20000]
28 | 
29 | 30 |

Example 3:

31 | 32 |
33 | Input: equations = [["a","b"]], values = [0.5], queries = [["a","b"],["b","a"],["a","c"],["x","y"]]
34 | Output: [0.50000,2.00000,-1.00000,-1.00000]
35 | 
36 | 37 |

 

38 |

Constraints:

39 | 40 |
    41 |
  • 1 <= equations.length <= 20
  • 42 |
  • equations[i].length == 2
  • 43 |
  • 1 <= Ai.length, Bi.length <= 5
  • 44 |
  • values.length == equations.length
  • 45 |
  • 0.0 < values[i] <= 20.0
  • 46 |
  • 1 <= queries.length <= 20
  • 47 |
  • queries[i].length == 2
  • 48 |
  • 1 <= Cj.length, Dj.length <= 5
  • 49 |
  • Ai, Bi, Cj, Dj consist of lower case English letters and digits.
  • 50 |
51 | --------------------------------------------------------------------------------