├── 64 ├── main.py ├── main.java └── main.cpp ├── 221 ├── main.cpp ├── main.py └── main.java ├── 279 ├── main.cpp ├── main.py └── main.java ├── 650 ├── main.java └── main.py ├── 746 ├── main.java ├── main.cpp └── main.py ├── 797 └── 797.cpp ├── 841 └── 841.cpp ├── 931 ├── main.java ├── main.py └── main.cpp ├── 983 └── main.py ├── 1046 ├── main.py ├── main.java └── main.cpp ├── 1049 ├── main.cpp └── main.java ├── 1376 └── 1376.cpp ├── 15-3sum └── NOTES.md ├── Session 2019 ├── 66 ├── 498 ├── 1100 ├── 1176 ├── x.cpp ├── 344.cpp ├── github-test │ ├── patch2.cpp │ ├── hello.cpp │ └── bye.cpp ├── Topological Sorting │ ├── input.txt │ ├── a.out │ └── Kahns Algorithm ├── test ├── 496NGE ├── a.out ├── Leet1.pdf ├── Leet2.pdf ├── Misc │ ├── NGE │ ├── NGE2 │ ├── a.out │ ├── KadaneSUM │ ├── Segregate positve and negative numbers │ ├── NGE2.cpp │ └── Polynomial_mul.cpp ├── leet3.pdf ├── leet4.pdf ├── Trees | Graph Implementations │ ├── kruskal.txt │ ├── input.txt │ ├── a.out │ └── IterativeInOrder ├── DP │ └── 01Knapsack │ │ ├── a.out │ │ ├── 01Knapsack │ │ ├── equalSumPartition │ │ └── 01Knapsack.cpp ├── Sliding Window │ ├── 76 │ ├── a.out │ └── 76(modified) ├── input.txt ├── 371.cpp ├── 58.py ├── 136.cpp ├── 977.cpp ├── 453.cpp ├── 191.cpp ├── 172.cpp ├── Hash Table │ └── 217.cpp ├── 342.cpp ├── 326.cpp ├── 66.py ├── 122.cpp ├── 171.cpp ├── 189(reverse-sleek).cpp ├── 560.cpp ├── 190.cpp ├── 268(xor).cpp ├── 283.cpp ├── 70(DP).cpp ├── 1394.cpp ├── 561.cpp ├── 137.cpp ├── 387.cpp ├── height-checker.cpp ├── Old │ ├── 53-Lengthoflastword.cpp │ ├── 70.ClimbingStairs.cpp │ ├── 1.TwoSum.cpp │ ├── 28.ImplementstrStr().cpp │ ├── 53-MaximumSubarrayProblem.cpp │ ├── 27.RemoveElement.cpp │ ├── 45.JumpGameII.cpp │ ├── 53.MaximumSubarray.cpp │ ├── 35.SearchInsertPosition.cpp │ ├── 13.RomanToInteger(UnorderedMap).cpp │ ├── 7.ReverseInteger.cpp │ ├── 9.PalindromeNumber.cpp │ ├── 21.MergeTwoSortedLinkedLists.cpp │ └── _152-MaximumProductSubarray.cpp ├── 268.cpp ├── 976.cpp ├── 392.cpp ├── 168.cpp ├── 258.cpp ├── 1228.cpp ├── 454.cpp ├── 169.cpp ├── 120.cpp ├── Youtube │ ├── Consecutive Numbers Sum.cpp │ └── Split.cpp ├── 482.cpp ├── 389.cpp ├── 383.cpp ├── 455.cpp ├── 485.cpp ├── 349.cpp ├── 189(ONextraspace).cpp ├── 58.cpp ├── 198.cpp ├── 646.cpp ├── 350.cpp ├── 300.cpp ├── 141.cpp ├── 997(graph).cpp ├── 1277.cpp ├── 367.cpp ├── 921.cpp ├── 94.cpp ├── 202.cpp ├── 137(bitwise).cpp ├── 121.cpp ├── 1392.cpp ├── 492.cpp ├── 1390.cpp ├── 167.cpp ├── 100(better).cpp ├── 1013.cpp ├── 1047.cpp ├── 1.cpp ├── 125.cpp ├── 263.cpp ├── 1043.cpp ├── 404.cpp ├── 451.cpp ├── 985better.cpp ├── 102(rec).cpp ├── 416.cpp ├── 970.py ├── 997.cpp ├── 5.cpp ├── 303.cpp ├── 60.cpp ├── 409.cpp ├── 523.cpp ├── 144(BT Preorder Traversal).cpp ├── 56(updt).cpp ├── 119.cpp ├── 20.cpp ├── 459.cpp ├── 236.cpp ├── 239.cpp ├── 682.cpp ├── 937.cpp ├── 101.cpp ├── 230(kth smallest element in BST).cpp ├── 437(dfs).cpp ├── 45.cpp ├── 543.cpp ├── 98(Validate BST).cpp ├── 414.cpp ├── 155.cpp ├── catalan_dp.cpp ├── 901.cpp ├── 102.cpp ├── 784.cpp ├── 434.cpp └── 785(dfs).cpp ├── 1-two-sum └── NOTES.md ├── 20-valid-parentheses ├── NOTES.md └── 20-valid-parentheses.cpp ├── 23-merge-k-sorted-lists └── NOTES.md ├── 11-container-with-most-water ├── NOTES.md └── 11-container-with-most-water.cpp ├── 21-merge-two-sorted-lists └── NOTES.md ├── 5-longest-palindromic-substring ├── NOTES.md └── README.md ├── 19-remove-nth-node-from-end-of-list └── NOTES.md ├── 3-longest-substring-without-repeating-characters ├── NOTES.md └── 3-longest-substring-without-repeating-characters.cpp ├── 743-network-delay-time └── NOTES.md ├── not-boring-movies └── not-boring-movies.sql ├── README.md ├── Top Qs ├── Array │ ├── Single Number.cpp │ ├── Contains Duplicate.cpp │ ├── Rotate Array.cpp │ ├── Best Time to Buy and Sell Stock II.cpp │ ├── Move Zeroes.cpp │ ├── Two Sum.cpp │ ├── Rotate Image.cpp │ ├── Remove Duplicates from Array.cpp │ ├── Plus One.cpp │ └── Intersection of Two Arrays II.cpp └── Strings │ ├── Reverse String.cpp │ ├── Valid Anagram.cpp │ ├── First Unique Character In a String.cpp │ ├── Reverse Integer.cpp │ ├── implement strStr().cpp │ ├── Valid Palindrome.cpp │ └── LCP.cpp ├── valid-anagram └── valid-anagram.cpp ├── 1334-find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance └── NOTES.md ├── 1976-number-of-ways-to-arrive-at-destination └── NOTES.md ├── Apr'20 ├── 189(Rotate Array).cpp ├── 53(Kadane).cpp ├── Counting_Elements.cpp ├── 122(Best Time to Buy and Sell Stock II).cpp ├── 104(Max Depth of BT).cpp ├── 48(Rotate Matrix).cpp ├── 287(Find the Duplicate Number).cpp ├── 49(Group Anagrams).cpp ├── 405(Convert a Number to Hex).cpp ├── 309(BestTimeToBuySellStockCooldown).cpp ├── 141(LInked List Cycle).cpp ├── 3(Longest Substring Without Repeating Characters).cpp ├── 714(BestTimeToBuyAndSellStockWithTransactionFee).cpp ├── 559(Max Depth of N-ary Tree).cpp ├── 235(LCA of BST).cpp ├── 206(Reverse LL).cpp ├── 160(Palindrome LL).cpp ├── 1029(Two City Scheduling).cpp ├── 406(Queue Reconstruction by Height).cpp ├── 38(Count and Say).cpp ├── 72(Edit Distance).cpp ├── 283(Move Zeroes).cpp ├── 90(Subsets II).cpp ├── 1072(Flip Columns For Maximum Number of Equal Rows).cpp ├── 236(LCA of BT).cpp ├── 880(Decoded String at Index).cpp ├── 221(Maximal Square).cpp └── 538(Convert BST To Greater Tree).cpp ├── is-subsequence └── is-subsequence.cpp ├── minimum-cost-to-move-chips-to-the-same-position └── minimum-cost-to-move-chips-to-the-same-position.cpp ├── base-7 ├── README.md └── base-7.cpp ├── delete-and-earn └── delete-and-earn.cpp ├── water-bottles └── water-bottles.cpp ├── split-a-string-in-balanced-strings └── split-a-string-in-balanced-strings.cpp ├── climbing-stairs └── climbing-stairs.cpp ├── robot-return-to-origin └── robot-return-to-origin.cpp ├── escape-the-ghosts └── escape-the-ghosts.cpp ├── find-center-of-star-graph └── find-center-of-star-graph.cpp ├── permutation-sequence └── permutation-sequence.cpp ├── minimum-number-of-vertices-to-reach-all-nodes └── minimum-number-of-vertices-to-reach-all-nodes.cpp ├── reach-a-number └── reach-a-number.cpp ├── 797-all-paths-from-source-to-target ├── NOTES.md └── 797-all-paths-from-source-to-target.cpp ├── subarray-sums-divisible-by-k └── subarray-sums-divisible-by-k.cpp ├── delete-columns-to-make-sorted └── delete-columns-to-make-sorted.cpp ├── group-anagrams └── group-anagrams.cpp ├── last-stone-weight └── last-stone-weight.cpp ├── longest-harmonious-subsequence └── longest-harmonious-subsequence.cpp ├── can-place-flowers └── can-place-flowers.cpp ├── contiguous-array ├── contiguous-array.cpp └── README.md ├── house-robber └── house-robber.cpp ├── monotone-increasing-digits ├── monotone-increasing-digits.cpp └── README.md ├── assign-cookies └── assign-cookies.cpp ├── find-first-and-last-position-of-element-in-sorted-array └── find-first-and-last-position-of-element-in-sorted-array.cpp ├── self-dividing-numbers ├── self-dividing-numbers.cpp └── README.md ├── minimum-number-of-swaps-to-make-the-string-balanced └── minimum-number-of-swaps-to-make-the-string-balanced.cpp ├── sliding-window-maximum └── sliding-window-maximum.cpp ├── keys-and-rooms └── keys-and-rooms.cpp ├── maximum-points-you-can-obtain-from-cards └── maximum-points-you-can-obtain-from-cards.cpp ├── corporate-flight-bookings └── corporate-flight-bookings.cpp ├── valid-parentheses └── valid-parentheses.cpp ├── longest-common-subsequence └── longest-common-subsequence.cpp ├── best-time-to-buy-and-sell-stock-ii └── best-time-to-buy-and-sell-stock-ii.cpp ├── valid-triangle-number ├── valid-triangle-number.cpp └── README.md ├── subarray-product-less-than-k ├── subarray-product-less-than-k.cpp └── README.md ├── k-diff-pairs-in-an-array └── k-diff-pairs-in-an-array.cpp ├── pairs-of-songs-with-total-durations-divisible-by-60 └── pairs-of-songs-with-total-durations-divisible-by-60.cpp ├── encode-and-decode-tinyurl ├── encode-and-decode-tinyurl.cpp └── README.md ├── champagne-tower └── champagne-tower.cpp ├── first-missing-positive ├── first-missing-positive.cpp └── README.md ├── random-pick-with-weight └── random-pick-with-weight.cpp ├── kth-largest-element-in-a-stream └── kth-largest-element-in-a-stream.cpp ├── Premium └── Paint Fence.cpp ├── minimum-subsequence-in-non-increasing-order └── minimum-subsequence-in-non-increasing-order.cpp ├── number-of-good-ways-to-split-a-string └── number-of-good-ways-to-split-a-string.cpp ├── peak-index-in-a-mountain-array └── peak-index-in-a-mountain-array.cpp ├── student-attendance-record-i └── student-attendance-record-i.cpp ├── bitwise-ors-of-subarrays └── bitwise-ors-of-subarrays.cpp ├── minimum-number-of-arrows-to-burst-balloons └── minimum-number-of-arrows-to-burst-balloons.cpp ├── random-pick-index └── README.md ├── n-ary-tree-preorder-traversal └── n-ary-tree-preorder-traversal.cpp ├── leaf-similar-trees └── leaf-similar-trees.cpp ├── validate-binary-search-tree └── validate-binary-search-tree.cpp ├── maximum-product-subarray └── maximum-product-subarray.cpp ├── longest-mountain-in-array └── longest-mountain-in-array.cpp ├── next-permutation └── next-permutation.cpp ├── maximal-network-rank └── maximal-network-rank.cpp ├── non-decreasing-array └── non-decreasing-array.cpp ├── optimal-division └── optimal-division.cpp ├── check-array-formation-through-concatenation └── check-array-formation-through-concatenation.cpp ├── merge-intervals └── merge-intervals.cpp ├── minimum-number-of-refueling-stops └── minimum-number-of-refueling-stops.cpp ├── pyramid-transition-matrix └── pyramid-transition-matrix.cpp ├── lemonade-change └── lemonade-change.cpp ├── robot-bounded-in-circle └── robot-bounded-in-circle.cpp ├── search-in-a-binary-search-tree └── search-in-a-binary-search-tree.cpp ├── find-if-path-exists-in-graph └── find-if-path-exists-in-graph.cpp ├── permutation-in-string └── README.md ├── keyboard-row └── keyboard-row.cpp ├── stone-game └── stone-game.cpp ├── length-of-longest-fibonacci-subsequence └── length-of-longest-fibonacci-subsequence.cpp ├── longest-palindromic-subsequence └── longest-palindromic-subsequence.cpp └── split-a-string-into-the-max-number-of-unique-substrings └── split-a-string-into-the-max-number-of-unique-substrings.cpp /15-3sum/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /Session 2019/x.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /1-two-sum/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /Session 2019/344.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /20-valid-parentheses/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /23-merge-k-sorted-lists/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /11-container-with-most-water/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /21-merge-two-sorted-lists/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /5-longest-palindromic-substring/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /19-remove-nth-node-from-end-of-list/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /3-longest-substring-without-repeating-characters/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /Session 2019/github-test/patch2.cpp: -------------------------------------------------------------------------------- 1 | Patch 2 added. 2 | -------------------------------------------------------------------------------- /Session 2019/Topological Sorting/input.txt: -------------------------------------------------------------------------------- 1 | 4 4 2 | 0 1 3 | 1 2 4 | 2 3 5 | 1 3 -------------------------------------------------------------------------------- /Session 2019/1100: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JanaSabuj/Leetcode-solutions/HEAD/Session 2019/1100 -------------------------------------------------------------------------------- /Session 2019/1176: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JanaSabuj/Leetcode-solutions/HEAD/Session 2019/1176 -------------------------------------------------------------------------------- /Session 2019/498: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JanaSabuj/Leetcode-solutions/HEAD/Session 2019/498 -------------------------------------------------------------------------------- /Session 2019/66: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JanaSabuj/Leetcode-solutions/HEAD/Session 2019/66 -------------------------------------------------------------------------------- /Session 2019/test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JanaSabuj/Leetcode-solutions/HEAD/Session 2019/test -------------------------------------------------------------------------------- /Session 2019/496NGE: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JanaSabuj/Leetcode-solutions/HEAD/Session 2019/496NGE -------------------------------------------------------------------------------- /Session 2019/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JanaSabuj/Leetcode-solutions/HEAD/Session 2019/a.out -------------------------------------------------------------------------------- /Session 2019/Leet1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JanaSabuj/Leetcode-solutions/HEAD/Session 2019/Leet1.pdf -------------------------------------------------------------------------------- /Session 2019/Leet2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JanaSabuj/Leetcode-solutions/HEAD/Session 2019/Leet2.pdf -------------------------------------------------------------------------------- /Session 2019/Misc/NGE: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JanaSabuj/Leetcode-solutions/HEAD/Session 2019/Misc/NGE -------------------------------------------------------------------------------- /Session 2019/Misc/NGE2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JanaSabuj/Leetcode-solutions/HEAD/Session 2019/Misc/NGE2 -------------------------------------------------------------------------------- /Session 2019/leet3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JanaSabuj/Leetcode-solutions/HEAD/Session 2019/leet3.pdf -------------------------------------------------------------------------------- /Session 2019/leet4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JanaSabuj/Leetcode-solutions/HEAD/Session 2019/leet4.pdf -------------------------------------------------------------------------------- /Session 2019/Misc/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JanaSabuj/Leetcode-solutions/HEAD/Session 2019/Misc/a.out -------------------------------------------------------------------------------- /Session 2019/Trees | Graph Implementations/kruskal.txt: -------------------------------------------------------------------------------- 1 | 4 5 2 | 0 2 6 3 | 0 1 10 4 | 0 3 5 5 | 2 3 4 6 | 1 3 15 -------------------------------------------------------------------------------- /Session 2019/Misc/KadaneSUM: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JanaSabuj/Leetcode-solutions/HEAD/Session 2019/Misc/KadaneSUM -------------------------------------------------------------------------------- /Session 2019/DP/01Knapsack/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JanaSabuj/Leetcode-solutions/HEAD/Session 2019/DP/01Knapsack/a.out -------------------------------------------------------------------------------- /Session 2019/Sliding Window/76: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JanaSabuj/Leetcode-solutions/HEAD/Session 2019/Sliding Window/76 -------------------------------------------------------------------------------- /Session 2019/Sliding Window/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JanaSabuj/Leetcode-solutions/HEAD/Session 2019/Sliding Window/a.out -------------------------------------------------------------------------------- /Session 2019/DP/01Knapsack/01Knapsack: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JanaSabuj/Leetcode-solutions/HEAD/Session 2019/DP/01Knapsack/01Knapsack -------------------------------------------------------------------------------- /Session 2019/Trees | Graph Implementations/input.txt: -------------------------------------------------------------------------------- 1 | 5 8 2 | 0 1 10 3 | 0 2 5 4 | 1 3 1 5 | 2 4 2 6 | 4 3 6 7 | 2 1 3 8 | 4 0 2 9 | 2 3 9 -------------------------------------------------------------------------------- /743-network-delay-time/NOTES.md: -------------------------------------------------------------------------------- 1 | - dijkstra SSSP 2 | - dijkstra D.N need any visited array 3 | - because you are already using the dist array -------------------------------------------------------------------------------- /Session 2019/Topological Sorting/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JanaSabuj/Leetcode-solutions/HEAD/Session 2019/Topological Sorting/a.out -------------------------------------------------------------------------------- /Session 2019/Sliding Window/76(modified): -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JanaSabuj/Leetcode-solutions/HEAD/Session 2019/Sliding Window/76(modified) -------------------------------------------------------------------------------- /Session 2019/DP/01Knapsack/equalSumPartition: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JanaSabuj/Leetcode-solutions/HEAD/Session 2019/DP/01Knapsack/equalSumPartition -------------------------------------------------------------------------------- /Session 2019/input.txt: -------------------------------------------------------------------------------- 1 | 6 2 | 4 3 | 0011 4 | 8 5 | 00010111 6 | 5 7 | 01001 8 | 9 9 | 011011011 10 | 9 11 | 010111011 12 | 18 13 | 100010101011101101 -------------------------------------------------------------------------------- /Session 2019/Topological Sorting/Kahns Algorithm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JanaSabuj/Leetcode-solutions/HEAD/Session 2019/Topological Sorting/Kahns Algorithm -------------------------------------------------------------------------------- /Session 2019/Trees | Graph Implementations/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JanaSabuj/Leetcode-solutions/HEAD/Session 2019/Trees | Graph Implementations/a.out -------------------------------------------------------------------------------- /Session 2019/Misc/Segregate positve and negative numbers: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JanaSabuj/Leetcode-solutions/HEAD/Session 2019/Misc/Segregate positve and negative numbers -------------------------------------------------------------------------------- /not-boring-movies/not-boring-movies.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select * from cinema 3 | where description <> "boring" and id % 2 = 1 4 | order by rating desc -------------------------------------------------------------------------------- /Session 2019/Trees | Graph Implementations/IterativeInOrder: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JanaSabuj/Leetcode-solutions/HEAD/Session 2019/Trees | Graph Implementations/IterativeInOrder -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![LeetCode](https://cdn-images-1.medium.com/max/1600/1*gBkMCGTAdSk4tu17SCa7RQ.png) 2 | *** 3 | 4 | # Leetcode-solutions 5 | Solutions for Leetcode according to ascending difficulty 6 | -------------------------------------------------------------------------------- /Session 2019/371.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int getSum(int a, int b) { 4 | if (b == 0) 5 | return a; 6 | unsigned int carry = a&b; 7 | return getSum(a^b, carry << 1); 8 | } 9 | }; -------------------------------------------------------------------------------- /Top Qs/Array/Single Number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int singleNumber(vector& nums) { 4 | int ans = 0; 5 | for(auto x: nums) 6 | ans = ans ^ x; 7 | return ans; 8 | } 9 | }; -------------------------------------------------------------------------------- /Session 2019/58.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def lengthOfLastWord(self, s: str) -> int: 3 | arr = s.split() 4 | if len(arr) == 0: 5 | return 0 6 | else: 7 | return len(arr[-1]) 8 | -------------------------------------------------------------------------------- /valid-anagram/valid-anagram.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isAnagram(string s, string t) { 4 | sort(s.begin(), s.end()); 5 | sort(t.begin(), t.end()); 6 | 7 | return s == t; 8 | } 9 | }; -------------------------------------------------------------------------------- /1334-find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/NOTES.md: -------------------------------------------------------------------------------- 1 | - floyd warshall, n relaxations 2 | - O(N^3), N <= 500 3 | - if k can be a reachable internal node from i and j, then i->k->j 4 | `- place the bomb such that the least no of neighbouring cities are affected` -------------------------------------------------------------------------------- /Session 2019/136.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int singleNumber(vector& nums) { 4 | 5 | int ans=0; 6 | 7 | for(auto x:nums){ 8 | ans = ans^x; 9 | } 10 | 11 | return ans; 12 | 13 | } 14 | }; -------------------------------------------------------------------------------- /Session 2019/977.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector sortedSquares(vector& A) { 4 | vector ans; 5 | for(auto x:A) 6 | ans.push_back(x*x); 7 | sort(ans.begin(), ans.end()); 8 | 9 | return ans; 10 | } 11 | }; -------------------------------------------------------------------------------- /Session 2019/453.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minMoves(vector& nums) { 4 | int y = *min_element(nums.begin(), nums.end()); 5 | int ans=0; 6 | for(auto x:nums) 7 | ans += abs(x - y); 8 | 9 | return ans; 10 | } 11 | }; -------------------------------------------------------------------------------- /Session 2019/191.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int hammingWeight(uint32_t n) { 4 | 5 | int cnt=0; 6 | while(n>0){ 7 | if(n&1) 8 | cnt++; 9 | n=n>>1; 10 | } 11 | 12 | return cnt; 13 | } 14 | }; -------------------------------------------------------------------------------- /1976-number-of-ways-to-arrive-at-destination/NOTES.md: -------------------------------------------------------------------------------- 1 | - Atcoder 2 | ``` 3 | No of shortest paths in undirected unweighted graph 4 | You do a modified standard BFS 5 | ``` 6 | ​ 7 | - https://www.youtube.com/watch?v=8HLGOX0cjYQ 8 | - https://atcoder.jp/contests/abc211/tasks/abc211_d 9 | ​ 10 | ​ 11 | ​ 12 | ​ -------------------------------------------------------------------------------- /650/main.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minSteps(int n) { 3 | int sum = 0; 4 | 5 | for (int i = 2; i * i <= n; i++) { 6 | while (n % i == 0) { 7 | n /= i; 8 | sum += i; 9 | } 10 | } 11 | 12 | if (n > 1) 13 | sum += n; 14 | 15 | return sum; 16 | } 17 | } -------------------------------------------------------------------------------- /Session 2019/github-test/hello.cpp: -------------------------------------------------------------------------------- 1 | /*-------------------------------@greenindia-Sabuj-Jana----------------------------------*/ 2 | 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | freopen("input.txt", "r", stdin); 8 | 9 | 10 | 11 | return 0; 12 | } 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /Session 2019/172.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long long int trailingZeroes(int n) { 4 | 5 | long long int count=0; 6 | 7 | for(long long int i=5; (n/i)>=1; i*=5){ 8 | count += n/i; 9 | } 10 | 11 | return count; 12 | } 13 | }; -------------------------------------------------------------------------------- /Top Qs/Array/Contains Duplicate.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool containsDuplicate(vector& nums) { 4 | map mp; 5 | for(auto x: nums){ 6 | if(mp[x]) 7 | return true; 8 | mp[x]++; 9 | } 10 | return false; 11 | } 12 | }; -------------------------------------------------------------------------------- /Session 2019/Hash Table/217.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool containsDuplicate(vector& nums) { 4 | map mp; 5 | for(auto x: nums){ 6 | if(mp[x]) 7 | return true; 8 | mp[x]++; 9 | } 10 | 11 | return false; 12 | } 13 | }; -------------------------------------------------------------------------------- /Apr'20/189(Rotate Array).cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void rotate(vector& nums, int k) { 4 | int n = nums.size(); 5 | k = k % n; 6 | reverse(nums.begin(), nums.end()); 7 | reverse(nums.begin(), nums.begin() + k); 8 | reverse(nums.begin() + k, nums.end()); 9 | } 10 | }; 11 | -------------------------------------------------------------------------------- /Top Qs/Array/Rotate Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void rotate(vector& nums, int k) { 4 | int n = nums.size(); 5 | k = k%n; 6 | reverse(nums.begin(), nums.begin() + n - k); 7 | reverse(nums.begin() + n - k, nums.end());; 8 | reverse(nums.begin(), nums.end()); 9 | } 10 | }; -------------------------------------------------------------------------------- /Session 2019/342.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPowerOfFour(int n) { 4 | if(n<1) 5 | return false; 6 | 7 | while(n%4==0){ 8 | n/=4; 9 | } 10 | 11 | if(n==1) 12 | return true; 13 | else 14 | return false; 15 | } 16 | }; -------------------------------------------------------------------------------- /650/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minSteps(self, n: int) -> int: 3 | sum = 0 4 | # prime factorise 5 | for i in range(2, int(sqrt(n)) + 1): 6 | while n % i == 0: 7 | n//= i; 8 | sum += i 9 | 10 | if n > 1: 11 | sum += n 12 | return sum -------------------------------------------------------------------------------- /Session 2019/326.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPowerOfThree(int n) { 4 | 5 | if(n==0) 6 | return false; 7 | while(n%3==0){ 8 | n/=3; 9 | } 10 | 11 | if(n==1) 12 | return true; 13 | else 14 | return false; 15 | } 16 | }; -------------------------------------------------------------------------------- /Session 2019/66.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def plusOne(self, digits: List[int]) -> List[int]: 3 | num="" 4 | for x in digits: 5 | num += str(x) 6 | 7 | num = int(num) 8 | num = num + 1 9 | num = str(num) 10 | arr = [x for x in num] 11 | return arr 12 | 13 | -------------------------------------------------------------------------------- /Session 2019/github-test/bye.cpp: -------------------------------------------------------------------------------- 1 | /*-------------------------------@greenindia-Sabuj-Jana----------------------------------*/ 2 | 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | freopen("input.txt", "r", stdin); 8 | cout <<"Git remote upstream and pruning tested" << endl; 9 | 10 | 11 | return 0; 12 | } 13 | 14 | -------------------------------------------------------------------------------- /Apr'20/53(Kadane).cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxSubArray(vector& nums) { 4 | int ans = INT_MIN; 5 | int a = 0; 6 | 7 | for(int x: nums){ 8 | a += x; 9 | ans = max(ans, a); 10 | a = max(a, 0); 11 | } 12 | 13 | return ans; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /Top Qs/Strings/Reverse String.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void reverseString(vector& s) { 4 | int n = s.size(); 5 | int i = 0, j = n-1; 6 | 7 | while(i < j){ 8 | swap(s[i], s[j]); 9 | i++; 10 | j--; 11 | } 12 | 13 | 14 | 15 | } 16 | }; -------------------------------------------------------------------------------- /Apr'20/Counting_Elements.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countElements(vector& arr) { 4 | map mp; 5 | 6 | for(auto x: arr) 7 | mp[x]++; 8 | 9 | int cnt = 0; 10 | for(auto x: arr) 11 | if(mp[x+1]) 12 | cnt++; 13 | return cnt; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /Top Qs/Array/Best Time to Buy and Sell Stock II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProfit(vector& prices) { 4 | int profit = 0; 5 | int n = prices.size(); 6 | 7 | for(int i = 1; i < n; i++){ 8 | profit += max(0, prices[i] - prices[i-1]); 9 | } 10 | 11 | return profit; 12 | } 13 | }; -------------------------------------------------------------------------------- /Session 2019/122.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProfit(vector& prices) { 4 | int n = prices.size(); 5 | 6 | int profit = 0; 7 | for(int i=0; i prices[i]) ? prices[i+1] - prices[i] : 0; 9 | } 10 | 11 | return profit; 12 | 13 | } 14 | }; -------------------------------------------------------------------------------- /Session 2019/171.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int titleToNumber(string s) { 4 | 5 | // reverse(s.begin(), s.end()); 6 | 7 | int ans=0; 8 | for(int i=0; i& nums, int k) { 4 | 5 | int n = nums.size(); 6 | k = k%n; 7 | 8 | reverse(nums.begin(), nums.end()); 9 | reverse(nums.begin(), nums.begin() + k); 10 | reverse(nums.begin() + k, nums.end()); 11 | 12 | } 13 | }; -------------------------------------------------------------------------------- /Session 2019/560.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int subarraySum(vector& nums, int k) { 4 | int n = nums.size(); 5 | 6 | map mp; 7 | mp[0] = 1; 8 | 9 | int ans = 0; 10 | int sum = 0; 11 | for (int i = 0; i < n; i++) { 12 | sum += nums[i]; 13 | ans += mp[sum - k]; 14 | mp[sum]++; 15 | } 16 | 17 | return ans; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /Session 2019/190.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | uint32_t reverseBits(uint32_t n) { 4 | 5 | uint32_t ans= 0; 6 | uint32_t cnt=1; 7 | 8 | for(int i=31; i>=0; i--){ 9 | if(n&(1<& nums) { 4 | 5 | int n = nums.size(); 6 | 7 | int tmp = n; 8 | 9 | for(int i=0; i& nums) { 4 | int index = 0; 5 | int n = nums.size(); 6 | for(int i=0; i mp; 5 | for(auto c: s) 6 | mp[c]++; 7 | for(auto c: t) 8 | mp[c]--; 9 | 10 | for(auto x: mp) 11 | if(x.second != 0) 12 | return false; 13 | return true; 14 | } 15 | }; -------------------------------------------------------------------------------- /Session 2019/1394.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findLucky(vector& arr) { 4 | map mp; 5 | for(auto x: arr) 6 | mp[x]++; 7 | int ans = -1; 8 | for(auto x: arr) 9 | if(mp[x] == x) 10 | ans = max(ans, x); 11 | 12 | return ans; 13 | 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /746/main.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minCostClimbingStairs(int[] cost) { 3 | int n = cost.length; 4 | int f[] = new int[n + 1]; 5 | f[0] = 0; 6 | f[1] = 0; 7 | 8 | for (int i = 2; i <= n; i++) { 9 | f[i] = Math.min(f[i - 1] + cost[i - 1], f[i - 2] + cost[i - 2]); 10 | } 11 | 12 | return f[n]; 13 | } 14 | } -------------------------------------------------------------------------------- /Top Qs/Strings/First Unique Character In a String.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int firstUniqChar(string s) { 4 | map mp; 5 | for(auto c: s) 6 | mp[c]++; 7 | 8 | int n = s.length(); 9 | for(int i = 0; i < n; i++) 10 | if(mp[s[i]] == 1) 11 | return i; 12 | 13 | return -1; 14 | } 15 | }; -------------------------------------------------------------------------------- /746/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minCostClimbingStairs(vector& cost) { 4 | int n = cost.size(); 5 | vector f(n + 1); 6 | f[0] = 0; 7 | f[1] = 0; 8 | 9 | for (int i = 2; i <= n; i++) { 10 | f[i] = min(f[i - 1] + cost[i - 1], f[i - 2] + cost[i - 2]); 11 | } 12 | 13 | return f[n]; 14 | } 15 | }; -------------------------------------------------------------------------------- /746/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minCostClimbingStairs(self, cost: List[int]) -> int: 3 | n = len(cost) 4 | f = [None] * (n + 1) 5 | 6 | f[0] = 0 7 | f[1] = 0 8 | 9 | for i in range(2, n + 1): 10 | f[i] = min(f[i - 1] + cost[i - 1], f[i - 2] + cost[i - 2]) 11 | return f[n] 12 | 13 | 14 | -------------------------------------------------------------------------------- /279/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numSquares(int n) { 4 | vector f(n + 1); 5 | f[0] = 0; 6 | f[1] = 1; 7 | for (int i = 2; i <= n; i++) { 8 | f[i] = f[i - 1] + 1; 9 | for (int j = 1; j * j <= i; j++) 10 | f[i] = min(f[i], f[i - j * j] + 1); 11 | } 12 | 13 | return f[n]; 14 | } 15 | }; -------------------------------------------------------------------------------- /1046/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def lastStoneWeight(self, stones: List[int]) -> int: 3 | h = [-x for x in stones] 4 | heapq.heapify(h) 5 | 6 | while len(h) >= 2: 7 | a = heapq.heappop(h) 8 | b = heapq.heappop(h) 9 | 10 | heapq.heappush(h, a - b) 11 | 12 | if len(h) == 0: 13 | return 0 14 | else: 15 | return -h[0] -------------------------------------------------------------------------------- /Apr'20/122(Best Time to Buy and Sell Stock II).cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProfit(vector& prices) { 4 | int profit = 0; 5 | int n = prices.size(); 6 | 7 | for(int i = 1; i < n; i++){ 8 | if(prices[i] > prices[i-1]) 9 | profit += prices[i] - prices[i - 1]; 10 | } 11 | 12 | return profit; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /Session 2019/561.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int arrayPairSum(vector& nums) { 4 | int n = nums.size(); 5 | 6 | sort(nums.begin(), nums.end()); 7 | 8 | int sum = 0; 9 | 10 | for(int i = 0; i < n; i++){ 11 | if(i % 2 == 0) 12 | sum += nums[i]; 13 | } 14 | 15 | return sum; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /Session 2019/137.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int singleNumber(vector& nums) { 4 | map mp; 5 | 6 | for(auto x:nums) 7 | mp[x]++; 8 | 9 | int val; 10 | for(auto x:nums) 11 | if(mp[x] == 1) 12 | { 13 | val = x; 14 | break; 15 | } 16 | return val; 17 | } 18 | }; -------------------------------------------------------------------------------- /Session 2019/387.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int firstUniqChar(string s) { 4 | map mp; 5 | for(auto x:s) 6 | mp[x]++; 7 | 8 | int n = s.length(); 9 | 10 | for(int i=0; i& heights) { 4 |        vector arr = heights; 5 |        sort(arr.begin(), arr.end()); 6 |         7 |        int n = arr.size(); 8 |        int cnt = 0; 9 |        for(int i = 0; i < n; i++){ 10 |            cnt += heights[i] != arr[i]; 11 |       } 12 |         13 |        return cnt; 14 |   } 15 | }; 16 | -------------------------------------------------------------------------------- /Session 2019/Old/53-Lengthoflastword.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int lengthOfLastWord(string s) { 4 | 5 | stringstream sso; 6 | sso<>x) 10 | cnt++; 11 | 12 | if(cnt==0) 13 | return 0; 14 | else 15 | return x.length(); 16 | 17 | 18 | } 19 | }; -------------------------------------------------------------------------------- /Session 2019/Old/70.ClimbingStairs.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | int climbStairs(int n) { 5 | 6 | if(n==1) return 1; 7 | if(n==2) return 2; 8 | int memo[n+1]; 9 | memo[1]=1; 10 | memo[2]=2; 11 | for(int i=3; i<=n; i++){ 12 | memo[i]=memo[i-1]+memo[i-2]; 13 | } 14 | 15 | return memo[n]; 16 | } 17 | }; -------------------------------------------------------------------------------- /Top Qs/Strings/Reverse Integer.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int reverse(int x) { 4 | long long ans = 0; 5 | 6 | while(x){ 7 | 8 | ans = ans * 10 + x % 10; 9 | x = x/10; 10 | 11 | if(ans > INT_MAX or ans < INT_MIN) 12 | return 0; 13 | 14 | } 15 | 16 | return ans; 17 | } 18 | }; -------------------------------------------------------------------------------- /279/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numSquares(self, n: int) -> int: 3 | f = [None] * (n + 1) 4 | f[0]=0 5 | f[1]=1 6 | for i in range(2, n+1): 7 | f[i] = f[i-1]+1 8 | for j in range(1, i): 9 | if j * j <= i: 10 | f[i] = min(f[i], f[i - j*j] + 1) 11 | else: 12 | break 13 | return f[n] -------------------------------------------------------------------------------- /Session 2019/268.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int missingNumber(vector& nums) { 4 | 5 | int sum = 0; 6 | int n = nums.size(); 7 | 8 | for(auto x:nums){ 9 | sum += x; 10 | } 11 | 12 | int total = (n*(n+1)) / 2; 13 | int rem = total - sum; 14 | 15 | return rem; 16 | 17 | } 18 | }; -------------------------------------------------------------------------------- /Session 2019/976.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int largestPerimeter(vector& A) { 4 | sort(A.begin(), A.end()); 5 | 6 | int n = A.size(); 7 | for(int i = n -3; i>=0; i--){ 8 | if((A[i] + A[i + 1]) > A[i + 2]){ 9 | //valid 10 | return A[i] + A[i+1] +A[i+2]; 11 | } 12 | } 13 | 14 | return 0; 15 | } 16 | }; -------------------------------------------------------------------------------- /minimum-cost-to-move-chips-to-the-same-position/minimum-cost-to-move-chips-to-the-same-position.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 |    int minCostToMoveChips(vector& pos) { 4 |        int odd = 0; 5 |        int even = 0; 6 |        for(auto x: pos){ 7 |            if(x & 1) 8 |                odd++; 9 |            else 10 |                even++; 11 |       } 12 |        return min(odd,even); 13 |   } 14 | }; 15 | -------------------------------------------------------------------------------- /base-7/README.md: -------------------------------------------------------------------------------- 1 |

504. Base 7

Easy


Given an integer, return its base 7 string representation.

2 | 3 |

Example 1:
4 |

Input: 100
 5 | Output: "202"
 6 | 
7 |

8 | 9 |

Example 2:
10 |

Input: -7
11 | Output: "-10"
12 | 
13 |

14 | 15 |

Note: 16 | The input will be in range of [-1e7, 1e7]. 17 |

-------------------------------------------------------------------------------- /delete-and-earn/delete-and-earn.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int deleteAndEarn(vector& nums) { 4 | int n = 10002; 5 | vector dp(n, 0); 6 | 7 | for(auto x: nums) 8 | dp[x] += x; 9 | 10 | for(int i = 2; i < n; i++){ 11 | dp[i] = max(dp[i - 1], dp[i - 2] + dp[i]); 12 | } 13 | 14 | return dp[n - 1]; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /Session 2019/392.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isSubsequence(string s, string t) { 4 | int j=0; 5 | int n = t.length(); 6 | int m = s.length(); 7 | 8 | for(int i=0; i0){ 8 | int val = n%26 ; 9 | char c = val + 65; 10 | ans = ans + c; 11 | 12 | n/=26; 13 | } 14 | 15 | // cout << ans; 16 | reverse(ans.begin(), ans.end()); 17 | return ans; 18 | } 19 | }; -------------------------------------------------------------------------------- /279/main.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numSquares(int n) { 3 | // vector f(n + 1); 4 | int[] f = new int[n + 1]; 5 | f[0] = 0; 6 | f[1] = 1; 7 | 8 | for (int i = 2; i <= n; i++) { 9 | f[i] = f[i - 1] + 1; 10 | for (int j = 1; j * j <= i; j++) 11 | f[i] = Math.min(f[i], f[i - j * j] + 1); 12 | } 13 | 14 | return f[n]; 15 | } 16 | } -------------------------------------------------------------------------------- /Session 2019/258.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | int digSum(int n){ 5 | int sum=0; 6 | while(n>0){ 7 | sum +=n%10; 8 | n/=10; 9 | } 10 | return sum; 11 | } 12 | int addDigits(int num) { 13 | 14 | if(num>=0 and num<=9) 15 | return num; 16 | 17 | int sum = digSum(num); 18 | return addDigits(sum); 19 | 20 | } 21 | }; -------------------------------------------------------------------------------- /base-7/base-7.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string convertToBase7(int num) { 4 | int x = abs(num); 5 | string ans = ""; 6 | do{ 7 | int rem = x % 7; 8 | x /= 7; 9 | ans += to_string(rem); 10 | }while(x); 11 | 12 | if(num < 0) 13 | ans += "-"; 14 | reverse(ans.begin(), ans.end()); 15 | 16 | return ans; 17 | } 18 | }; -------------------------------------------------------------------------------- /Session 2019/Old/1.TwoSum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int reverse(int x) { 4 | int rev=0; 5 | int sign=1; 6 | if(x<0) 7 | { 8 | sign=-1; 9 | x=-x; 10 | } 11 | 12 | 13 | while(x>0){ 14 | 15 | if(rev*10.0+ x%10 > 2147483648) return 0; 16 | rev=rev*10.0+ x%10; 17 | 18 | x/=10; 19 | } 20 | return sign*rev; 21 | } 22 | 23 | }; 24 | -------------------------------------------------------------------------------- /water-bottles/water-bottles.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 |    int numWaterBottles(int numBottles, int numExchange) { 4 |        int a = numBottles; 5 |        int b = 0; 6 |        int k = numExchange; 7 |         8 |        int ans = 0; 9 |        while(a != 0){ 10 |            ans += a; 11 |             12 |            b = b + a; 13 |            a = b/k; 14 |            b = b%k; 15 |       } 16 |         17 |        return ans; 18 |   } 19 | }; 20 | -------------------------------------------------------------------------------- /Apr'20/104(Max Depth of BT).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 | int maxDepth(TreeNode* root) { 13 | if(root == NULL) 14 | return 0; 15 | return 1 + max(maxDepth(root->left), maxDepth(root->right)); 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /Session 2019/1228.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int missingNumber(vector& arr) { 4 | 5 | sort(arr.begin(), arr.end()); 6 | 7 | int n = arr.size(); 8 | 9 | int total = ( (n+1) * (arr[0] + arr[n-1])) / 2; 10 | 11 | int sum=0; 12 | for(auto x:arr) 13 | sum += x; 14 | 15 | return total - sum; 16 | 17 | 18 | 19 | 20 | } 21 | }; -------------------------------------------------------------------------------- /Session 2019/454.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int fourSumCount(vector& A, vector& B, vector& C, vector& D) { 4 | map mp; 5 | for(auto x:A) 6 | for(auto y:B) 7 | mp[x+y]++; 8 | 9 | int cnt=0; 10 | for(auto x:C) 11 | for(auto y:D) 12 | if(mp[-(x+y)]) 13 | cnt += mp[-x-y]; 14 | 15 | return cnt; 16 | } 17 | }; -------------------------------------------------------------------------------- /Top Qs/Array/Move Zeroes.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void moveZeroes(vector& nums) { 4 | int snow = 0; 5 | int n = nums.size(); 6 | 7 | for(int i = 0; i < n; i++){ 8 | if(nums[i] == 0) 9 | snow++; 10 | else{ 11 | int t = nums[i]; 12 | nums[i] = 0; 13 | nums[i-snow] = t; 14 | } 15 | } 16 | 17 | 18 | } 19 | }; -------------------------------------------------------------------------------- /split-a-string-in-balanced-strings/split-a-string-in-balanced-strings.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 |    int balancedStringSplit(string str) { 4 |        int cnt = 0; 5 |        int ans = 0; 6 |        for(auto x: str){ 7 |            if(x == 'R') 8 |                cnt++; 9 |            else 10 |                cnt--; 11 |             12 |            if(cnt == 0) 13 |                ans++; 14 |       } 15 |         16 |        return ans; 17 |   } 18 | }; 19 | -------------------------------------------------------------------------------- /Apr'20/48(Rotate Matrix).cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void rotate(vector>& matrix) { 4 | int n = matrix.size(); 5 | int m = matrix[0].size(); 6 | 7 | for(int i = 0; i < n; i++){ 8 | for(int j = i + 1; j < m; j++){ 9 | swap(matrix[i][j], matrix[j][i]); 10 | } 11 | } 12 | 13 | for(auto& vi: matrix) 14 | reverse(vi.begin(), vi.end()); 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /Session 2019/169.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int majorityElement(vector& nums) { 4 | map hash; 5 | for(auto x:nums) 6 | hash[x]++; 7 | 8 | int n = nums.size(); 9 | int ans; 10 | for(auto x:hash){ 11 | if(x.second > n/2){ 12 | ans = x.first; 13 | break; 14 | } 15 | 16 | } 17 | 18 | return ans; 19 | } 20 | }; -------------------------------------------------------------------------------- /Session 2019/Old/28.ImplementstrStr().cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int strStr(string haystack, string needle) { 4 | 5 | 6 | int nsz=needle.size(); 7 | if(nsz==0) 8 | return 0; 9 | 10 | int index=haystack.find(needle); 11 | if(index!=string::npos) 12 | return index; 13 | else 14 | return -1; 15 | 16 | 17 | 18 | 19 | } 20 | }; -------------------------------------------------------------------------------- /climbing-stairs/climbing-stairs.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | vector dp; 3 | public: 4 | Solution() { 5 | dp.assign(46, -1); 6 | } 7 | 8 | int climbStairs(int n) { 9 | // base 10 | if(n < 0) 11 | return 0; 12 | if(n == 0) 13 | return 1; 14 | 15 | if(dp[n] != -1) 16 | return dp[n]; 17 | 18 | // main 19 | return dp[n] = climbStairs(n-1) + climbStairs(n-2); 20 | } 21 | }; -------------------------------------------------------------------------------- /1046/main.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int lastStoneWeight(int[] stones) { 3 | PriorityQueue pq = new PriorityQueue<>(31, Collections.reverseOrder()); 4 | for (int x : stones) 5 | pq.add(x); 6 | 7 | while (pq.size() >= 2) { 8 | int a = pq.poll(); 9 | int b = pq.poll(); 10 | 11 | if (a == b) 12 | continue; 13 | else 14 | pq.add(a - b); 15 | } 16 | 17 | if (pq.size() == 0) 18 | return 0; 19 | else 20 | return pq.peek(); 21 | } 22 | } -------------------------------------------------------------------------------- /Session 2019/Old/53-MaximumSubarrayProblem.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxSubArray(vector& nums) { 4 | 5 | int sz=nums.size(); 6 | int sum=0,ans=INT_MIN; 7 | for(int i=0; ians) 10 | ans=sum; 11 | if(sum<0) 12 | sum=0; 13 | 14 | 15 | } 16 | 17 | 18 | return ans; 19 | 20 | } 21 | }; -------------------------------------------------------------------------------- /11-container-with-most-water/11-container-with-most-water.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxArea(vector& height) { 4 | int n = height.size(); 5 | int l = 0, r = n - 1; 6 | int maxArea = 0; 7 | while(l <= r) { 8 | maxArea = max(maxArea, (r - l) * min(height[l], height[r])); 9 | if(height[l] <= height[r]) 10 | l++; 11 | else 12 | r--; 13 | } 14 | 15 | return maxArea; 16 | } 17 | }; -------------------------------------------------------------------------------- /Session 2019/120.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minimumTotal(vector>& triangle) { 4 | int n = triangle.size(); 5 | int dp[n] ; 6 | for(int j=0; j=0; i--){ 11 | for(int j=0; j<=i; j++){ 12 | dp[j] = min(dp[j], dp[j+1]) + triangle[i][j]; 13 | } 14 | } 15 | 16 | return dp[0]; 17 | 18 | } 19 | }; -------------------------------------------------------------------------------- /robot-return-to-origin/robot-return-to-origin.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool judgeCircle(string moves) { 4 | int lr = 0, ud = 0; 5 | for(auto x: moves){ 6 | switch(x){ 7 | case 'L': lr--;break; 8 | case 'R': lr++; break; 9 | case 'U': ud--; break; 10 | case 'D': ud++; break; 11 | default: break; 12 | } 13 | } 14 | 15 | return (lr == 0 and ud == 0); 16 | } 17 | }; -------------------------------------------------------------------------------- /Session 2019/Youtube/Consecutive Numbers Sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int consecutiveNumbersSum(int S) { 5 | int cnt = 0; 6 | for(int n = 1; n <= sqrt(2*S) ; n++){ 7 | if ((2 * S + n*n - n) % (2 * n) == 0 ) 8 | cnt++; 9 | } 10 | 11 | return cnt; 12 | } 13 | 14 | // To execute C++, please define "int main()" 15 | int main() { 16 | 17 | int N = 43156417; 18 | cout << consecutiveNumbersSum(N) < twoSum(vector& nums, int target) { 4 | map mp; 5 | int n = nums.size(); 6 | vector ans; 7 | for(int i = 0; i < n; i++){ 8 | if(mp[target - nums[i]] > 0){ 9 | ans = {mp[target - nums[i]] - 1, i}; 10 | break; 11 | }else{ 12 | mp[nums[i]] = i + 1; 13 | } 14 | } 15 | 16 | return ans; 17 | } 18 | }; -------------------------------------------------------------------------------- /Session 2019/482.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string licenseKeyFormatting(string str, int k) { 4 | string res = ""; 5 | int n = str.size(); 6 | 7 | for(int i = n - 1; i >= 0; i--){ 8 | if(str[i] != '-'){ 9 | if(res.size() % (k + 1) == k) 10 | res += '-'; 11 | res += toupper(str[i]); 12 | } 13 | } 14 | 15 | reverse(res.begin(), res.end()); 16 | return res; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /escape-the-ghosts/escape-the-ghosts.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool escapeGhosts(vector>& ghosts, vector& target) { 4 | int md = 0; 5 | 6 | int tx = target[0]; 7 | int ty = target[1]; 8 | 9 | int stt = abs(tx) + abs(ty); 10 | 11 | for(auto g: ghosts){ 12 | if((abs(tx - g[0]) + abs(ty - g[1])) <= stt) 13 | return false; 14 | } 15 | 16 | return true; 17 | } 18 | }; -------------------------------------------------------------------------------- /Top Qs/Array/Rotate Image.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void rotate(vector>& matrix) { 4 | int n = matrix.size(); 5 | for(int i = 0; i < n; i++){ 6 | for(int j = 0; j <= i; j++){ 7 | int t = matrix[i][j]; 8 | matrix[i][j] = matrix[j][i]; 9 | matrix[j][i] = t; 10 | } 11 | } 12 | 13 | for(int i = 0; i < n; i++){ 14 | reverse(matrix[i].begin(), matrix[i].end()); 15 | } 16 | } 17 | }; -------------------------------------------------------------------------------- /find-center-of-star-graph/find-center-of-star-graph.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findCenter(vector>& edges) { 4 | int n = edges.size(); 5 | vector deg(n + 2, 0); 6 | 7 | for(auto& v: edges){ 8 | deg[v[0]]++; 9 | deg[v[1]]++; 10 | 11 | if(deg[v[0]] == n) 12 | return v[0]; 13 | if(deg[v[1]] == n) 14 | return v[1]; 15 | } 16 | 17 | return -1; 18 | } 19 | }; -------------------------------------------------------------------------------- /permutation-sequence/permutation-sequence.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string getPermutation(int n, int k) { 4 | string str = ""; 5 | for (int i = 1; i <= n; ++i) { 6 | str += to_string(i); 7 | } 8 | 9 | int cnt = 1; 10 | if (k == 1) 11 | return str; 12 | while (next_permutation(str.begin(), str.end())) { 13 | cnt++; 14 | if (cnt == k) 15 | return str; 16 | } 17 | 18 | return str; 19 | } 20 | }; -------------------------------------------------------------------------------- /minimum-number-of-vertices-to-reach-all-nodes/minimum-number-of-vertices-to-reach-all-nodes.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector findSmallestSetOfVertices(int n, vector>& edges) { 4 | vector indeg(n, 0); 5 | for(auto v: edges){ 6 | indeg[v[1]]++; 7 | } 8 | 9 | vector ans; 10 | for(int i = 0; i < n; i++){ 11 | if(indeg[i] == 0) 12 | ans.push_back(i); 13 | } 14 | 15 | return ans; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /Session 2019/389.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | char findTheDifference(string s, string t) { 4 | 5 | map mp; 6 | for(auto x:s) 7 | mp[x]++; 8 | 9 | map np; 10 | for(auto x:t) 11 | np[x]++; 12 | 13 | char ans; 14 | for(auto x:np) 15 | if(np[x.first] > mp[x.first]) 16 | { 17 | ans = x.first; 18 | break; 19 | } 20 | 21 | return ans; 22 | } 23 | }; -------------------------------------------------------------------------------- /Session 2019/Old/27.RemoveElement.cpp: -------------------------------------------------------------------------------- 1 | //greenindia 2 | class Solution { 3 | public: 4 | int removeElement(vector& nums, int val) { 5 | 6 | int sz=nums.size(); 7 | // return sz; 8 | 9 | auto it=nums.begin(); 10 | while(it!=nums.end()){ 11 | if(*it==val) 12 | it=nums.erase(it); 13 | else 14 | it++; 15 | } 16 | 17 | 18 | 19 | return nums.size(); 20 | 21 | } 22 | }; -------------------------------------------------------------------------------- /Session 2019/383.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canConstruct(string ransomNote, string magazine) { 4 | map mp; 5 | for(auto x: ransomNote) 6 | mp[x]++; 7 | 8 | map np; 9 | for(auto x:magazine) 10 | np[x]++; 11 | 12 | for(auto x:ransomNote){ 13 | if(mp[x] <= np[x]) 14 | continue; 15 | else 16 | return false; 17 | } 18 | 19 | return true; 20 | 21 | } 22 | }; -------------------------------------------------------------------------------- /reach-a-number/reach-a-number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int reachNumber(int target) { 4 | target = abs(target); 5 | if(target == 0) 6 | return 0; 7 | 8 | int sum = 0; 9 | int idx = 1; 10 | 11 | while(1){ 12 | sum += idx; 13 | // cout << sum << endl; 14 | if(sum >= target and (sum - target) % 2 == 0) 15 | return idx; 16 | idx++; 17 | } 18 | 19 | return -1; 20 | } 21 | }; -------------------------------------------------------------------------------- /Session 2019/455.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findContentChildren(vector& g, vector& s) { 4 | 5 | sort(g.begin(), g.end()); 6 | sort(s.begin(), s.end()); 7 | 8 | int n = g.size(); 9 | int m = s.size(); 10 | 11 | int i=0,j=0; 12 | while(i < n and j < m){ 13 | if(g[i] <= s[j]){ 14 | i++; 15 | j++; 16 | }else 17 | j++; 18 | } 19 | 20 | return i; 21 | 22 | } 23 | }; -------------------------------------------------------------------------------- /Session 2019/485.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findMaxConsecutiveOnes(vector& nums) { 4 | int cnt=0; 5 | vector req; 6 | int n = nums.size(); 7 | 8 | for(auto x:nums){ 9 | if(x == 1) 10 | cnt++; 11 | else{ 12 | req.push_back(cnt); 13 | cnt = 0; 14 | } 15 | } 16 | 17 | req.push_back(cnt);// if seq ends in 1 18 | 19 | return *max_element(req.begin(), req.end()); 20 | } 21 | }; -------------------------------------------------------------------------------- /Session 2019/349.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector intersection(vector& nums1, vector& nums2) { 4 | 5 | map mp; 6 | for(auto x: nums1) 7 | mp[x]++; 8 | 9 | map np; 10 | for(auto x: nums2) 11 | np[x]++; 12 | 13 | vector req; 14 | 15 | for(auto x: mp){ 16 | if(mp[x.first] and np[x.first]) 17 | req.push_back(x.first); 18 | } 19 | 20 | return req; 21 | } 22 | }; -------------------------------------------------------------------------------- /Session 2019/189(ONextraspace).cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void rotate(vector& nums, int k) { 4 | 5 | vector tmp; 6 | int n = nums.size(); 7 | 8 | if(n==0) 9 | return ; 10 | 11 | k = k%n;// imp 12 | 13 | for(int i=n-k; i=0;j--){ 9 | if(s[j]!=' ') 10 | break; 11 | } 12 | 13 | 14 | int l = 0; 15 | for(int i=j; i>=0; i--){ 16 | if(s[i] == ' ') 17 | break; 18 | l++; 19 | } 20 | 21 | return l; 22 | 23 | 24 | 25 | } 26 | }; -------------------------------------------------------------------------------- /Apr'20/287(Find the Duplicate Number).cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findDuplicate(vector& nums) { 4 | int slow = nums[0]; 5 | int fast = nums[0]; 6 | 7 | do{ 8 | slow = nums[slow]; 9 | fast = nums[nums[fast]]; 10 | }while(slow != fast); 11 | 12 | // slow = fast now 13 | slow = nums[0]; 14 | while(slow != fast){ 15 | slow = nums[slow]; 16 | fast = nums[fast]; 17 | } 18 | 19 | return slow; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /797-all-paths-from-source-to-target/NOTES.md: -------------------------------------------------------------------------------- 1 | - DAG ensures no cycle 2 | - since we need all possible paths, no need of visited aray, we can simply backtrack 3 | - by popping back 4 | - K = number of paths = 2^(N-2) 5 | Time comlexity = E + k*V = E + 2^(N-2)*V, 6 | - nc0+nC1+nC2+...+nCn = 2^n 7 | - O(E + k*V) 8 | - k is the max no of times a node can be visited 9 | -max no of paths = max no of times a node can be visited 10 | ​ 11 | ``` 12 | So k = no of paths (start + (N-2 optional nodes) + end) 13 | so k = 2^N-2 14 | ``` 15 | ​ 16 | - DFS with stack pop, stack populates all paths 17 | ​ -------------------------------------------------------------------------------- /subarray-sums-divisible-by-k/subarray-sums-divisible-by-k.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int subarraysDivByK(vector& A, int k) { 4 | unordered_map mp; 5 | mp[0] = 1; 6 | 7 | int n = A.size(); 8 | 9 | int pref = 0; 10 | int cnt = 0; 11 | for(int i = 0; i < n; i++){ 12 | pref = (pref + A[i]) % k; 13 | if(pref < 0) 14 | pref += k; 15 | cnt += mp[pref]; 16 | mp[pref]++; 17 | } 18 | 19 | return cnt; 20 | } 21 | }; -------------------------------------------------------------------------------- /Apr'20/49(Group Anagrams).cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> groupAnagrams(vector& strs) { 4 | map> mp; 5 | 6 | for(auto str: strs){ 7 | string can = str; 8 | sort(can.begin(), can.end());// can 9 | 10 | mp[can].push_back(str); 11 | } 12 | 13 | vector> vec; 14 | for(auto pp: mp){ 15 | vec.push_back(pp.second); 16 | } 17 | 18 | return vec; 19 | 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /Session 2019/198.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int rob(vector& nums) { 4 | 5 | int n = nums.size(); 6 | if(n == 0) 7 | return 0; 8 | if(n==1) 9 | return nums[0]; 10 | if(n==2) 11 | return max(nums[0], nums[1]); 12 | 13 | int dp[n]; 14 | dp[0]=nums[0]; 15 | dp[1]= max(nums[0],nums[1]); 16 | 17 | for(int i=2; i& nums) { 4 | int n = nums.size(); 5 | if(n == 0 or n == 1) 6 | return n; 7 | int i = 0, j = 1; 8 | while(j < n){ 9 | if(nums[i] == nums[j]) 10 | j++; 11 | else{ 12 | i++; 13 | //swap 14 | int t = nums[i];nums[i]=nums[j];nums[j]=t; 15 | j++; 16 | } 17 | } 18 | 19 | return i+1; 20 | } 21 | }; -------------------------------------------------------------------------------- /delete-columns-to-make-sorted/delete-columns-to-make-sorted.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 |    int minDeletionSize(vector& A) { 4 |        int n = A.size(); 5 |        int m = A[0].size(); 6 |         7 |        int cnt = 0; 8 |        for(int j = 0; j < m; j++){ 9 |            for(int i = 0; i < n; i++){ 10 |                if(i != 0 and A[i][j] < A[i - 1][j]){ 11 |                    cnt++; 12 |                    break; 13 |               } 14 |           }                             15 |        } 16 |         17 |        return cnt; 18 |   } 19 | }; 20 | -------------------------------------------------------------------------------- /group-anagrams/group-anagrams.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> groupAnagrams(vector& strs) { 4 | unordered_map> mp; 5 | 6 | for(auto str: strs){ 7 | 8 | string t = str; 9 | sort(t.begin(), t.end()); 10 | mp[t].push_back(str); 11 | } 12 | 13 | vector> vec; 14 | for(auto pp: mp){ 15 | vec.push_back(pp.second); 16 | } 17 | 18 | return vec; 19 | } 20 | }; -------------------------------------------------------------------------------- /Session 2019/646.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findLongestChain(vector>& pairs) { 4 | int n = pairs.size(); 5 | vector lis(n, 1); 6 | 7 | sort(pairs.begin(), pairs.end()); 8 | 9 | for(int i = 1; i < n; i++){ 10 | for(int j = i - 1; j >= 0; j--){ 11 | if(pairs[j][1] < pairs[i][0]){ 12 | lis[i] = max(lis[i], lis[j] + 1); 13 | } 14 | } 15 | } 16 | 17 | return *max_element(lis.begin(), lis.end()); 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /Apr'20/405(Convert a Number to Hex).cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string toHex(int n) { 4 | 5 | string ans = ""; 6 | if(n == 0) 7 | return "0"; 8 | 9 | unsigned int num = n; 10 | while(num){ 11 | int x = num % 16; 12 | num >>= 4; 13 | 14 | string t = "" ; 15 | t = t + char(x - 10 + 'a'); 16 | ans += (x <= 9 ? to_string(x) : t); 17 | } 18 | 19 | reverse(ans.begin(), ans.end()); 20 | return ans; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Session 2019/350.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector intersect(vector& nums1, vector& nums2) { 4 | 5 | map mp; 6 | for(auto x:nums1) 7 | mp[x]++; 8 | 9 | map np; 10 | for(auto x:nums2) 11 | np[x]++; 12 | 13 | vector req; 14 | 15 | for(auto x: mp){ 16 | int y = min(mp[x.first], np[x.first]); 17 | while(y--) 18 | req.push_back(x.first); 19 | } 20 | 21 | return req; 22 | } 23 | }; -------------------------------------------------------------------------------- /Top Qs/Array/Plus One.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector plusOne(vector& digits) { 4 | int sum = 0, dig = 0, carry = 1; 5 | int n = digits.size(); 6 | vector ans; 7 | 8 | for(int i = n - 1; i >= 0; i--){ 9 | sum = digits[i] + carry; 10 | carry = sum/10; 11 | dig = sum % 10; 12 | ans.push_back(dig); 13 | } 14 | 15 | if(carry) 16 | ans.push_back(1); 17 | 18 | reverse(ans.begin(), ans.end()); 19 | return ans; 20 | } 21 | }; -------------------------------------------------------------------------------- /last-stone-weight/last-stone-weight.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 |    int lastStoneWeight(vector& stones) { 4 |        priority_queue pq; 5 |        for(auto x: stones) 6 |            pq.push(x); 7 |         8 |        while(pq.size() > 1){ 9 |            int a = pq.top();pq.pop(); 10 |            int b = pq.top();pq.pop(); 11 |             12 |            if(a != b) 13 |                pq.push(a - b);                 14 |       } 15 |         16 |        if(!pq.empty()) 17 |            return pq.top(); 18 |        else 19 |            return 0; 20 |   } 21 | }; 22 | -------------------------------------------------------------------------------- /Session 2019/300.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int lengthOfLIS(vector& nums) { 4 | int n = nums.size(); 5 | if(n == 0) 6 | return 0; 7 | 8 | vector tail(nums.size(), 0); 9 | 10 | tail[0] = nums[0]; 11 | int sz = 1; 12 | 13 | for(int i = 1; i < n; i++){ 14 | auto itr = lower_bound(tail.begin(), tail.begin() + sz, nums[i]); 15 | if(itr == tail.begin() + sz) 16 | tail[sz++] = nums[i]; 17 | else 18 | *itr = nums[i]; 19 | } 20 | 21 | return sz; 22 | 23 | } 24 | }; -------------------------------------------------------------------------------- /1046/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int lastStoneWeight(vector& stones) { 4 | priority_queue pq; 5 | for (auto x : stones) 6 | pq.push(x); 7 | 8 | while (pq.size() >= 2) { 9 | int a = pq.top(); pq.pop(); 10 | int b = pq.top(); pq.pop(); 11 | 12 | if (a == b) 13 | continue; 14 | else 15 | pq.push(a - b); 16 | } 17 | 18 | if (pq.empty()) 19 | return 0; 20 | else 21 | return pq.top(); 22 | } 23 | }; -------------------------------------------------------------------------------- /Session 2019/141.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode(int x) : val(x), next(NULL) {} 7 | * }; 8 | */ 9 | typedef struct ListNode node; 10 | class Solution { 11 | public: 12 | bool hasCycle(ListNode *head) { 13 | node* p = head; 14 | node* q = head; 15 | 16 | while (p and q and q->next) { 17 | p = p->next; 18 | q = q->next->next; 19 | if (p == q) 20 | return true; 21 | } 22 | 23 | return false; 24 | } 25 | }; -------------------------------------------------------------------------------- /longest-harmonious-subsequence/longest-harmonious-subsequence.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findLHS(vector& nums) { 4 | unordered_map mp; 5 | int n = nums.size(); 6 | 7 | for(auto x: nums) 8 | mp[x]++; 9 | 10 | int mx = 0; 11 | for(auto x: mp){ 12 | int f = x.first; 13 | 14 | // both must exist 15 | if(mp.count(f + 1) > 0) 16 | mx = max(mx, mp[f] + mp[f + 1]); 17 | } 18 | 19 | return mx; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /64/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minPathSum(self, grid: List[List[int]]) -> int: 3 | n = len(grid) 4 | m = len(grid[0]) 5 | f = [[None for _ in range(m)] for _ in range(n)] 6 | 7 | f[0][0] = grid[0][0] 8 | for j in range(1, m): 9 | f[0][j] = f[0][j-1] + grid[0][j] 10 | for i in range(1, n): 11 | f[i][0] = f[i - 1][0] + grid[i][0] 12 | 13 | for i in range(1, n): 14 | for j in range(1, m): 15 | f[i][j] = min(f[i - 1][j] , f[i][j - 1]) + grid[i][j] 16 | 17 | return f[n - 1][m - 1] -------------------------------------------------------------------------------- /Session 2019/997(graph).cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findJudge(int N, vector>& trust) { 4 | 5 | vector in(N+1, 0); 6 | vector out(N+1, 0); 7 | 8 | 9 | for(auto vec : trust){ 10 | in[vec[1]]++; 11 | out[vec[0]]++; 12 | } 13 | 14 | 15 | for(int i = 1; i <= N; i++){ 16 | if(in[i] == (N-1) and (out[i]==0)) 17 | { 18 | return i; 19 | } 20 | } 21 | 22 | return -1; 23 | 24 | } 25 | }; -------------------------------------------------------------------------------- /Apr'20/309(BestTimeToBuySellStockCooldown).cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProfit(vector& prices) { 4 | int n = prices.size(); 5 | 6 | if(n <= 0) 7 | return 0; 8 | int buy[n], sell[n]; 9 | buy[0] = -prices[0]; 10 | sell[0] = 0; 11 | 12 | for(int i = 1; i < n; i++){ 13 | buy[i] = max(buy[i - 1],- prices[i] + ((i - 2) >= 0 ? sell[i - 2] : 0)); 14 | sell[i] = max(sell[i - 1], buy[i - 1] + prices[i]); 15 | } 16 | 17 | return max(buy[n - 1], sell[n - 1]); 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /Session 2019/1277.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countSquares(vector>& matrix) { 4 | int n = matrix.size(); 5 | int m = matrix[0].size(); 6 | 7 | vector> dp(n, vector(m)); 8 | int ans = 0; 9 | for (int i = 0; i < n; i++) { 10 | for (int j = 0; j < m; j++) { 11 | if (i == 0 or j == 0) 12 | dp[i][j] = matrix[i][j]; 13 | else if (matrix[i][j] == 0) 14 | dp[i][j] = 0; 15 | else 16 | dp[i][j] = 1 + min({dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]}); 17 | 18 | ans += dp[i][j]; 19 | } 20 | } 21 | 22 | return ans; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /can-place-flowers/can-place-flowers.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 |    bool canPlaceFlowers(vector& flower, int k) { 4 |        int n = flower.size(); 5 |         6 |        for(int i = 0; i < n; i++){ 7 |            if(k == 0) 8 |                return true; 9 |             10 |            if(flower[i] != 1){ 11 |                if((i == 0 || flower[i - 1] != 1) and (i == n-1 || flower[i+1] != 1)){ 12 |                    flower[i] = 1;// plant it 13 |                    k--; 14 |               } 15 |           } 16 |       } 17 |         18 |        return (k == 0); 19 |   } 20 | }; 21 | -------------------------------------------------------------------------------- /contiguous-array/contiguous-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findMaxLength(vector& nums) { 4 | int n = nums.size(); 5 | unordered_map mp; 6 | mp[0] = -1; 7 | 8 | int pref = 0; 9 | int mx = 0; 10 | for(int i = 0; i < n; i++){ 11 | pref += (nums[i] == 0 ? -1 : nums[i]); 12 | if(mp.find(pref) != mp.end()){ 13 | mx = max(mx, i - mp[pref]); 14 | }else{ 15 | mp[pref] = i; 16 | } 17 | } 18 | 19 | return mx; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /house-robber/house-robber.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int rob(vector& nums) { 4 | // dp[i] = max amt of money till ith index 5 | 6 | 7 | int n = nums.size(); 8 | if(n == 0) 9 | return 0; 10 | 11 | vector dp(n); 12 | dp[0] = nums[0]; 13 | 14 | for(int i = 1; i < n; i++){ 15 | if(i == 1) 16 | dp[i] = max(nums[i], dp[i-1]); 17 | else 18 | dp[i] = max(dp[i-2] + nums[i], dp[i - 1]); 19 | } 20 | 21 | return dp[n-1]; 22 | } 23 | }; -------------------------------------------------------------------------------- /monotone-increasing-digits/monotone-increasing-digits.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int monotoneIncreasingDigits(int N) { 4 | string str = to_string(N); 5 | int n = str.size(); 6 | 7 | int idx = n - 1; 8 | 9 | for(int i = n - 1; i > 0; i--){ 10 | if(str[i - 1] > str[i]){ 11 | str[i - 1]--; 12 | idx = i - 1; 13 | } 14 | } 15 | 16 | 17 | for(int i = idx + 1; i < n; i++){ 18 | str[i] = '9'; 19 | } 20 | 21 | return stoi(str); 22 | } 23 | }; -------------------------------------------------------------------------------- /Apr'20/141(LInked List Cycle).cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode(int x) : val(x), next(NULL) {} 7 | * }; 8 | */ 9 | typedef ListNode node; 10 | class Solution { 11 | public: 12 | bool hasCycle(ListNode *head) { 13 | node* p , *q = head; 14 | 15 | while(p and q and q->next){ 16 | p = p->next; 17 | q = q->next->next; 18 | 19 | if(p == q) 20 | return true; 21 | } 22 | 23 | return false; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /assign-cookies/assign-cookies.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 |    int findContentChildren(vector& g, vector& s) { 4 |        sort(g.begin(), g.end()); 5 |        int n = s.size(); 6 |         7 |        int cnt = 0; 8 |        multiset st; 9 |        for(auto x: s) 10 |            st.insert(x); 11 |         12 |        for(auto x: g){             13 |            if(st.lower_bound(x) == st.end()) 14 |                break; 15 |            auto it = st.lower_bound(x); 16 |            st.erase(it); 17 |            cnt++; 18 |       } 19 |         20 |        return cnt; 21 |   } 22 | }; 23 | -------------------------------------------------------------------------------- /find-first-and-last-position-of-element-in-sorted-array/find-first-and-last-position-of-element-in-sorted-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector searchRange(vector& nums, int target) { 4 | int idx1 = lower_bound(nums.begin(), nums.end(), target) - nums.begin(); 5 | int idx2 = upper_bound(nums.begin(), nums.end(), target) - nums.begin(); 6 | idx2--; 7 | 8 | if(idx1 >= 0 and idx1 < (int)nums.size() and nums[idx1] == target) 9 | return {idx1, idx2}; 10 | else 11 | return {-1, -1}; 12 | 13 | 14 | } 15 | }; -------------------------------------------------------------------------------- /Session 2019/367.cpp: -------------------------------------------------------------------------------- 1 | typedef long long ll; 2 | class Solution { 3 | public: 4 | bool isPerfectSquare(int num) { 5 | 6 | if(num==0) 7 | return true; 8 | 9 | ll l=1, r=num; 10 | while(l<=r){ 11 | ll mid = l + (r - l)/2;// assumed sqrt 12 | ll tmp = mid*mid; 13 | 14 | if(tmp == num) 15 | return true; 16 | else if(tmp > num) 17 | r = mid - 1; 18 | else 19 | l = mid + 1; 20 | 21 | } 22 | 23 | return false; 24 | } 25 | }; -------------------------------------------------------------------------------- /Session 2019/921.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minAddToMakeValid(string S) { 4 | int cnt = 0; 5 | int global = 0; 6 | for(auto x: S){ 7 | if(x == '('){ 8 | cnt++; 9 | }else{ 10 | cnt--; 11 | if(cnt < 0){ 12 | cnt = 0; 13 | global++;// extra opening bracket needed 14 | } 15 | } 16 | } 17 | 18 | if(cnt > 0) 19 | global += cnt;// no of closing brackets needed 20 | return global; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /self-dividing-numbers/self-dividing-numbers.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool selfDiv(int x){ 4 | int num = x; 5 | 6 | while(x){ 7 | int dig = x % 10; 8 | if(dig == 0 || num % dig) return false; 9 | x /= 10; 10 | } 11 | 12 | return true; 13 | } 14 | vector selfDividingNumbers(int left, int right) { 15 | vector ans; 16 | for(int i = left; i <= right; i++){ 17 | if(selfDiv(i)) 18 | ans.push_back(i); 19 | } 20 | 21 | return ans; 22 | } 23 | }; -------------------------------------------------------------------------------- /Session 2019/94.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 | typedef struct TreeNode node; 11 | class Solution { 12 | public: 13 | 14 | void inorder(node* root, vector& ans){ 15 | if(root == NULL) 16 | return; 17 | 18 | inorder(root->left,ans); 19 | ans.push_back(root->val); 20 | inorder(root->right,ans); 21 | } 22 | 23 | vector inorderTraversal(TreeNode* root) { 24 | vector ans; 25 | inorder(root,ans); 26 | } 27 | }; -------------------------------------------------------------------------------- /minimum-number-of-swaps-to-make-the-string-balanced/minimum-number-of-swaps-to-make-the-string-balanced.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minSwaps(string str) { 4 | int n = str.size(); 5 | int sz = 0; 6 | int cnt = 0; 7 | 8 | for(int i = 0; i < n; i++){ 9 | if(str[i] == '[') 10 | cnt++; 11 | else { 12 | if(cnt <= 0) 13 | sz++; 14 | else 15 | cnt--; 16 | } 17 | } 18 | 19 | // cout << sz << endl; 20 | return (sz + 1)/2; 21 | } 22 | }; -------------------------------------------------------------------------------- /sliding-window-maximum/sliding-window-maximum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector maxSlidingWindow(vector& nums, int k) { 4 | int n = nums.size(); 5 | deque dq; 6 | vector ans; 7 | 8 | for(int i = 0; i < n; i++){ 9 | while(!dq.empty() and dq.front() <= i-k) dq.pop_front(); 10 | while(!dq.empty() and nums[dq.back()] <= nums[i]) dq.pop_back(); 11 | dq.push_back(i); 12 | 13 | if(i >= k-1) 14 | ans.push_back(nums[dq.front()]); 15 | } 16 | 17 | return ans; 18 | } 19 | }; -------------------------------------------------------------------------------- /Apr'20/3(Longest Substring Without Repeating Characters).cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int lengthOfLongestSubstring(string str) { 4 | map mp; 5 | int n = str.size(); 6 | int i = 0, j = 0; 7 | 8 | int ans = 0; 9 | while(j < n){ 10 | mp[str[j]]++; 11 | while(mp[str[j]] > 1){ 12 | mp[str[i]]--; 13 | i++; 14 | } 15 | ans = max(ans, j - i + 1); 16 | j++; 17 | } 18 | 19 | return ans; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /Session 2019/202.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int getSumSquares(int n) { 4 | int sum = 0; 5 | while (n) { 6 | int dig = n % 10; 7 | n /= 10; 8 | sum += dig * dig; 9 | } 10 | return sum; 11 | } 12 | bool isHappy(int n) { 13 | map mp; 14 | while (n) { 15 | int sum = getSumSquares(n); 16 | if (sum == 1) 17 | return true; 18 | if (mp[sum]) 19 | return false; 20 | mp[sum]++; 21 | n = sum; 22 | } 23 | return false; 24 | } 25 | }; -------------------------------------------------------------------------------- /841/841.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void dfs(vector>& rooms, vector& visited, int x){ 4 | visited[x] = 1; 5 | for(auto v: rooms[x]){ 6 | if(!visited[v]) 7 | dfs(rooms, visited, v); 8 | } 9 | } 10 | 11 | bool canVisitAllRooms(vector>& rooms) { 12 | 13 | int n = rooms.size(); 14 | vector visited(n); 15 | dfs(rooms, visited, 0); 16 | 17 | for(int i = 0; i < n; i++) 18 | if(!visited[i]) 19 | return false; 20 | 21 | return true; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /Session 2019/137(bitwise).cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int singleNumber(vector& nums) { 4 | 5 | int result = 0; 6 | 7 | for(int i=0; i<=31; i++){ 8 | // ith - bit sum will be done for all numbers in array 9 | int sum=0; 10 | int y = (1 << i); 11 | 12 | for(auto x: nums){ 13 | if(x & y) 14 | sum++; 15 | } 16 | 17 | if(sum%3 != 0) 18 | result |= y; 19 | 20 | } 21 | 22 | return result; 23 | } 24 | }; -------------------------------------------------------------------------------- /Session 2019/121.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProfit(vector& prices) { 4 | 5 | // maintain a right-max 6 | 7 | int n = prices.size(); 8 | if(n==0) 9 | return 0; 10 | int dp[n];// maxR 11 | int ans = prices[n-1]; 12 | 13 | for(int i=n-1; i>=0; i--){ 14 | ans = max(ans,prices[i]); 15 | dp[i] = ans; 16 | } 17 | 18 | int profit = 0; 19 | for(int i=0; i LPS(string pattern){ 4 | int n=pattern.length(); 5 | vector lps(n); 6 | lps[0]=0; 7 | int j=0,i=1; 8 | 9 | while(i lps = LPS(str); 30 | int n = lps.size(); 31 | int k = lps[n-1]; 32 | return str.substr(0,k); 33 | } 34 | }; 35 | -------------------------------------------------------------------------------- /keys-and-rooms/keys-and-rooms.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void dfs(int x, vector>& adj, vector& vis){ 4 | vis[x] = 1; 5 | for(auto v: adj[x]){ 6 | if(!vis[v]) 7 | dfs(v, adj, vis); 8 | } 9 | } 10 | 11 | bool canVisitAllRooms(vector>& rooms) { 12 | int n = rooms.size(); 13 | vector vis(n,0); 14 | dfs(0, rooms, vis); 15 | 16 | for(int i = 0; i < n; i++){ 17 | if(!vis[i]) 18 | return false; 19 | } 20 | 21 | return true; 22 | } 23 | }; -------------------------------------------------------------------------------- /Session 2019/492.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector constructRectangle(int area) { 4 | 5 | int l,b; 6 | int n = area; 7 | for(int i=1; i<= sqrt(area); i++){ 8 | 9 | if(n%i == 0){ 10 | if(n/i == i){ 11 | l = i; 12 | b = i; 13 | }else{ 14 | b = i; 15 | l = n/i; 16 | } 17 | } 18 | } 19 | 20 | vector vec; 21 | vec.push_back(l); 22 | vec.push_back(b); 23 | 24 | return vec; 25 | } 26 | }; -------------------------------------------------------------------------------- /maximum-points-you-can-obtain-from-cards/maximum-points-you-can-obtain-from-cards.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxScore(vector& cardPoints, int k) { 4 | int n = cardPoints.size(); 5 | int sum = 0; 6 | int ans = INT_MIN; 7 | for (int i = 0; i < k; i++) { 8 | sum += cardPoints[i]; 9 | ans = max(ans, sum); 10 | } 11 | 12 | int j = k; 13 | for (int i = n - 1; i >= n - k; i--) { 14 | sum = (sum - cardPoints[j - 1] + cardPoints[i]); 15 | j--; 16 | ans = max(ans, sum); 17 | } 18 | 19 | return ans; 20 | } 21 | }; -------------------------------------------------------------------------------- /corporate-flight-bookings/corporate-flight-bookings.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector corpFlightBookings(vector>& bookings, int n) { 4 | vector ans(n); 5 | for(auto& tup: bookings){ 6 | int a = tup[0] - 1; 7 | int b = tup[1] - 1; 8 | int c = tup[2]; 9 | 10 | ans[a] += c; 11 | if(b + 1 < n) 12 | ans[b+1] -= c; 13 | } 14 | 15 | for(int i = 0; i < n; i++){ 16 | if(i == 0) continue; 17 | ans[i] += ans[i - 1]; 18 | } 19 | 20 | return ans; 21 | } 22 | }; -------------------------------------------------------------------------------- /valid-parentheses/valid-parentheses.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isValid(string str) { 4 | stack s; 5 | for(auto x: str){ 6 | if(x == '(' or x == '{' or x == '[') 7 | s.push(x); 8 | else{ 9 | if(s.empty()) 10 | return false; 11 | char ch = s.top(); 12 | s.pop(); 13 | 14 | if((x == ')' and ch != '(') || (x == '}' and ch != '{') || (x == ']' and ch != '[')) 15 | return false; 16 | } 17 | } 18 | 19 | return s.empty(); 20 | } 21 | }; -------------------------------------------------------------------------------- /221/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maximalSquare(vector>& mat) { 4 | int n = mat.size(); 5 | int m = mat[0].size(); 6 | 7 | int mx = 0; 8 | vector> dp(n, vector(m, 0)); 9 | 10 | for (int i = 0; i < n; i++) { 11 | for (int j = 0; j < m; ++j) { 12 | if (i == 0 || j == 0) 13 | dp[i][j] = (mat[i][j] == '0' ? 0 : 1); 14 | else if (mat[i][j] == '1') 15 | dp[i][j] = min({dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]}) + 1; 16 | else 17 | dp[i][j] = 0; 18 | 19 | mx = max(mx, dp[i][j]); 20 | 21 | } 22 | } 23 | 24 | return mx * mx;// return area 25 | } 26 | }; -------------------------------------------------------------------------------- /Apr'20/714(BestTimeToBuyAndSellStockWithTransactionFee).cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProfit(vector& prices, int fee) { 4 | int n = prices.size(); 5 | if(n == 0) 6 | return 0; 7 | int buy[n], sell[n]; 8 | 9 | buy[0] = -prices[0]; 10 | sell[0] = 0; 11 | 12 | for(int i = 1; i < n; i++){ 13 | buy[i] = max(buy[i - 1], sell[i - 1] - prices[i]); 14 | sell[i] = max(sell[i - 1], buy[i - 1] + prices[i] - fee); 15 | } 16 | 17 | return max(buy[n - 1], sell[n - 1]); 18 | // return sell[n - 1]; 19 | 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /Session 2019/1390.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | int fourDivs(int n) { 5 | set s; 6 | int sum = 0; 7 | for (int i = 1; i <= sqrt(n); i++) { 8 | if (n % i == 0) { 9 | if (n / i == i) { 10 | s.insert(i); 11 | sum += i; 12 | } else { 13 | s.insert(i); 14 | s.insert(n / i); 15 | sum += i; 16 | sum += n / i; 17 | } 18 | if (s.size() > 4) 19 | return 0; 20 | } 21 | } 22 | 23 | return (s.size() == 4 ? sum : 0); 24 | } 25 | 26 | int sumFourDivisors(vector& nums) { 27 | int ans = 0; 28 | for (auto x : nums) { 29 | ans += fourDivs(x); 30 | } 31 | 32 | return ans; 33 | } 34 | }; 35 | -------------------------------------------------------------------------------- /Session 2019/167.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector twoSum(vector& numbers, int target) { 4 | 5 | int n = numbers.size(); 6 | int i=0, j=n-1; 7 | 8 | vector ans; 9 | while(i<=j){ 10 | int sum = numbers[i] + numbers[j]; 11 | if(sum>target) 12 | j--; 13 | if(sum> dp(n + 1, vector(m + 1, 0)); 8 | 9 | for(int i = 1; i <= n; i++){ 10 | for(int j = 1; j <= m; j++){ 11 | if(t1[i - 1] == t2[j - 1]) 12 | dp[i][j] = 1 + dp[i - 1][j - 1]; 13 | else 14 | dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); 15 | } 16 | } 17 | 18 | return dp[n][m]; 19 | } 20 | }; -------------------------------------------------------------------------------- /Apr'20/559(Max Depth of N-ary Tree).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | // Definition for a Node. 3 | class Node { 4 | public: 5 | int val; 6 | vector children; 7 | 8 | Node() {} 9 | 10 | Node(int _val) { 11 | val = _val; 12 | } 13 | 14 | Node(int _val, vector _children) { 15 | val = _val; 16 | children = _children; 17 | } 18 | }; 19 | */ 20 | 21 | class Solution { 22 | public: 23 | int maxDepth(Node* root) { 24 | if(root == NULL) 25 | return 0; 26 | int mx = 0; 27 | 28 | for(auto x: root->children) 29 | mx = max(mx, maxDepth(x)); 30 | return 1 + mx; 31 | } 32 | }; 33 | -------------------------------------------------------------------------------- /best-time-to-buy-and-sell-stock-ii/best-time-to-buy-and-sell-stock-ii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 |    int maxProfit(vector& prices) { 4 |        int n = prices.size(); 5 |        int v = prices[0], p = prices[0]; 6 |        int i = 0; 7 |        int prof=0; 8 |        while(i < n - 1){ 9 |            while(i < n - 1 and prices[i] >= prices[i+1]) 10 |                i++; 11 |            v = prices[i];// buy 12 |             13 |            while(i < n - 1 and prices[i] <= prices[i+1]) 14 |                i++; 15 |            p = prices[i];// sell 16 |            prof += (p - v); 17 |       } 18 |         19 |        return prof; 20 |   } 21 | }; 22 | -------------------------------------------------------------------------------- /valid-triangle-number/valid-triangle-number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int triangleNumber(vector& nums) { 4 | sort(nums.begin(), nums.end()); 5 | int n = nums.size(); 6 | 7 | if(n < 3) 8 | return 0; 9 | 10 | int cnt = 0; 11 | for(int k = 2; k < n; k++){ 12 | int i = 0, j = k - 1; 13 | while(i <= j){ 14 | if(nums[i] + nums[j] > nums[k]){ 15 | cnt += j - i; 16 | j--; 17 | }else 18 | i++; 19 | } 20 | } 21 | 22 | return cnt; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /221/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maximalSquare(self, mat: List[List[str]]) -> int: 3 | n = len(mat) 4 | m = len(mat[0]) 5 | 6 | mx = 0 7 | dp = [[0 for _ in range(m)] for _ in range(n)] 8 | 9 | for i in range(n): 10 | for j in range(m): 11 | if i == 0 or j == 0: 12 | dp[i][j] = 0 if mat[i][j] == "0" else 1 13 | elif mat[i][j] == "1": 14 | dp[i][j] = min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]) + 1 15 | else: 16 | dp[i][j] = 0 17 | 18 | mx = max(mx, dp[i][j]) 19 | 20 | return mx*mx -------------------------------------------------------------------------------- /Apr'20/235(LCA of BST).cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode(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->val < p->val and root->val < q->val) 14 | return lowestCommonAncestor(root->right, p, q); 15 | else if(root->val > p->val and root->val > q->val) 16 | return lowestCommonAncestor(root->left, p, q); 17 | else 18 | return root; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /Session 2019/100(better).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 | typedef struct TreeNode node; 11 | class Solution { 12 | public: 13 | bool isSameTree(TreeNode* p, TreeNode* q) { 14 | if(p==NULL and q==NULL) 15 | return true; 16 | if(p!=NULL and q!=NULL){ 17 | if(p->val == q->val and isSameTree(p->left,q->left) and isSameTree(p->right,q->right)) 18 | return true; 19 | } 20 | 21 | return false; 22 | } 23 | }; -------------------------------------------------------------------------------- /subarray-product-less-than-k/subarray-product-less-than-k.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numSubarrayProductLessThanK(vector& nums, int k) { 4 | int prod = 1; 5 | int n = nums.size(); 6 | 7 | int cnt = 0; 8 | int i = 0;// left ptr 9 | for(int j = 0; j < n; j++){ 10 | // include 11 | prod *= nums[j]; 12 | 13 | // validate 14 | while(i <= j and prod >= k) 15 | prod /= nums[i++]; 16 | 17 | // i-j is the valid subarray 18 | cnt += (j - i + 1); 19 | } 20 | 21 | return cnt; 22 | } 23 | }; -------------------------------------------------------------------------------- /Apr'20/206(Reverse LL).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 | typedef struct ListNode node; 12 | class Solution { 13 | public: 14 | ListNode* reverseList(ListNode* head) { 15 | if(head == NULL or head->next == NULL) 16 | return head; 17 | node* tail = reverseList(head->next); 18 | head->next->next = head; 19 | head->next = NULL; 20 | return tail; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Session 2019/1013.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canThreePartsEqualSum(vector& A) { 4 | int n = A.size(); 5 | int sum = 0; 6 | for(auto x: A) 7 | sum += x; 8 | if(sum % 3 != 0) 9 | return false; 10 | 11 | int par_sum = sum/3; 12 | 13 | int par = 0; 14 | 15 | int temp = 0; 16 | for(int i = 0; i < n; i++){ 17 | temp += A[i]; 18 | 19 | if(temp == par_sum){ 20 | par++; 21 | temp = 0; 22 | } 23 | } 24 | 25 | return par >=3 ; 26 | 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /k-diff-pairs-in-an-array/k-diff-pairs-in-an-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findPairs(vector& nums, int k) { 4 | map mp; 5 | int n = nums.size(); 6 | 7 | for(auto x: nums) 8 | mp[x]++; 9 | 10 | int cnt = 0; 11 | for(auto val: mp){ 12 | int x = val.first; 13 | 14 | 15 | if(k == 0){ 16 | if(mp[x] >= 2) 17 | cnt++; 18 | }else{ 19 | if(mp.count(x + k)) 20 | cnt++; 21 | } 22 | } 23 | 24 | return cnt; 25 | 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /pairs-of-songs-with-total-durations-divisible-by-60/pairs-of-songs-with-total-durations-divisible-by-60.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numPairsDivisibleBy60(vector& time) { 4 | vector rems(60, 0); 5 | for(auto x: time) { 6 | rems[x%60]++; 7 | } 8 | 9 | int cnt = 0; 10 | int i = 1, j = 59; 11 | while(i < j) { 12 | // cout << rems[i] << "-" << rems[j] << endl; 13 | cnt += rems[i] * rems[j]; 14 | i++,j--; 15 | } 16 | cnt += rems[30] * (rems[30] - 1)/2; 17 | cnt += rems[0] * (rems[0] - 1)/2; 18 | return cnt; 19 | } 20 | }; -------------------------------------------------------------------------------- /64/main.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minPathSum(int[][] grid) { 3 | int n = grid.length; 4 | int m = grid[0].length; 5 | 6 | int[][] f = new int[n][m]; 7 | f[0][0] = grid[0][0]; 8 | for (int j = 1; j < m; j++) 9 | f[0][j] = f[0][j - 1] + grid[0][j]; 10 | for (int i = 1; i < n; i++) 11 | f[i][0] = f[i - 1][0] + grid[i][0]; 12 | 13 | for (int i = 1; i < n; i++) { 14 | for (int j = 1; j < m; j++) { 15 | f[i][j] = Math.min(f[i - 1][j], f[i][j - 1]) + grid[i][j]; 16 | } 17 | } 18 | 19 | return f[n - 1][m - 1]; 20 | 21 | } 22 | } -------------------------------------------------------------------------------- /encode-and-decode-tinyurl/encode-and-decode-tinyurl.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | int cnt = 0; 3 | unordered_map mp; 4 | string base = "http://tinyurl.com/"; 5 | public: 6 | // Encodes a URL to a shortened URL. 7 | string encode(string longUrl) { 8 | string shot = base + to_string(cnt++); 9 | mp[shot] = longUrl; 10 | return shot; 11 | } 12 | 13 | // Decodes a shortened URL to its original URL. 14 | string decode(string shortUrl) { 15 | return mp[shortUrl]; 16 | } 17 | }; 18 | 19 | // Your Solution object will be instantiated and called as such: 20 | // Solution solution; 21 | // solution.decode(solution.encode(url)); 22 | -------------------------------------------------------------------------------- /Session 2019/1047.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string removeDuplicates(string str) { 4 | stack s; 5 | 6 | for(auto x: str){ 7 | if(s.empty()) 8 | s.push(x); 9 | else{ 10 | char c = s.top(); 11 | if(c != x) 12 | s.push(x); 13 | else 14 | s.pop(); 15 | } 16 | } 17 | 18 | string req = ""; 19 | while(!s.empty()){ 20 | req += s.top(); 21 | s.pop(); 22 | } 23 | 24 | reverse(req.begin(), req.end()); 25 | return req; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /champagne-tower/champagne-tower.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | double champagneTower(int poured, int qr, int qc) { 4 | double A[qr + 2][qc + 2]; 5 | memset(A, 0, sizeof(A)); 6 | 7 | A[0][0] = poured; 8 | for(int i = 0; i <= qr; i++){ 9 | for(int j = 0; j <= qc; j++){ 10 | double nxt = (double)(A[i][j] - 1.0)/2.0; 11 | 12 | if(nxt > 0){ 13 | A[i+1][j] += nxt; 14 | A[i+1][j+1] += nxt; 15 | } 16 | 17 | } 18 | } 19 | 20 | return min(1.0, A[qr][qc]); 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /first-missing-positive/first-missing-positive.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int firstMissingPositive(vector& nums) { 4 | int n = nums.size(); 5 | // mex - [1, n+1] 6 | for (int i = 0; i < n; ++i) { 7 | if (nums[i] <= 0) 8 | nums[i] = n + 1; 9 | } 10 | 11 | for (int i = 0; i < n; ++i) { 12 | if (abs(nums[i]) > n) continue; 13 | int ind = abs(nums[i]) % n; 14 | nums[ind] = -1 * abs(nums[ind]); 15 | } 16 | 17 | for (int i = 1; i <= n; ++i) { 18 | if (nums[i%n] > 0) 19 | return i; 20 | } 21 | 22 | return n + 1; 23 | } 24 | }; -------------------------------------------------------------------------------- /random-pick-with-weight/random-pick-with-weight.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | vector cum; 3 | public: 4 | 5 | Solution(vector& w) { 6 | int prev = 0; 7 | for(auto x: w){ 8 | cum.push_back(x + prev); 9 | prev = cum.back(); 10 | } 11 | } 12 | 13 | int pickIndex() { 14 | int total = cum.back(); 15 | int val = rand() % total + 1; 16 | return lower_bound(cum.begin(), cum.end(), val) - cum.begin(); 17 | } 18 | }; 19 | 20 | /** 21 | * Your Solution object will be instantiated and called as such: 22 | * Solution* obj = new Solution(w); 23 | * int param_1 = obj->pickIndex(); 24 | */ -------------------------------------------------------------------------------- /221/main.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maximalSquare(char[][] mat) { 3 | int n = mat.length; 4 | int m = mat[0].length; 5 | 6 | int mx = 0; 7 | // vector> vec(n, vector(m, 0)); 8 | int[][] dp = new int[n][m]; 9 | 10 | for (int i = 0; i < n; i++) { 11 | for (int j = 0; j < m; ++j) { 12 | if (i == 0 || j == 0) 13 | dp[i][j] = (mat[i][j] == '0' ? 0 : 1); 14 | else if (mat[i][j] == '1') 15 | dp[i][j] = Math.min(dp[i - 1][j], Math.min(dp[i][j - 1], dp[i - 1][j - 1])) + 1; 16 | else 17 | dp[i][j] = 0; 18 | 19 | mx = Math.max(mx, dp[i][j]); 20 | 21 | } 22 | } 23 | 24 | return mx * mx;// return area 25 | } 26 | } -------------------------------------------------------------------------------- /Session 2019/1.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector twoSum(vector& nums, int target) { 4 | map hash; 5 | int n = nums.size(); 6 | 7 | for(int i = 0; i < n; i++){ 8 | hash[nums[i]] = i + 1; 9 | } 10 | 11 | vector t; 12 | for(int i = 0; i < n; i++){ 13 | int val = target - nums[i]; 14 | 15 | if(hash[val] and hash[val]!= i+1 ) 16 | { 17 | 18 | t.push_back(i); 19 | t.push_back(hash[val] - 1); 20 | return t; 21 | } 22 | } 23 | 24 | return t; 25 | } 26 | }; -------------------------------------------------------------------------------- /Session 2019/Old/45.JumpGameII.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int jump(vector& nums) { 4 | 5 | int n=nums.size(); 6 | int jumps[n]; 7 | for(int i=0; i=i){ 15 | if(jumps[j]+1next); 19 | b = (b == NULL ? headA : b->next); 20 | } 21 | 22 | return a; 23 | 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /Session 2019/125.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPalindrome(string s) { 4 | 5 | int n = s.length(); 6 | int i=0,j=n-1; 7 | 8 | while(i<=j){ 9 | 10 | if(!isalnum(s[i])){ 11 | i++; 12 | continue; 13 | } 14 | 15 | if(!isalnum(s[j])){ 16 | j--; 17 | continue; 18 | } 19 | 20 | if(tolower(s[i]) != tolower(s[j])) 21 | return false; 22 | 23 | i++; 24 | j--; 25 | 26 | } 27 | 28 | return true; 29 | } 30 | }; -------------------------------------------------------------------------------- /Top Qs/Array/Intersection of Two Arrays II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector intersect(vector& nums1, vector& nums2) { 4 | sort(nums1.begin(), nums1.end()); 5 | sort(nums2.begin(), nums2.end()); 6 | vector ans; 7 | int n = nums1.size(); 8 | int m = nums2.size(); 9 | int i = 0, j = 0; 10 | while(i < n and j < m){ 11 | if(nums1[i] < nums2[j]) 12 | i++; 13 | else if(nums1[i] > nums2[j]) 14 | j++; 15 | else{ 16 | ans.push_back(nums1[i]); 17 | i++,j++; 18 | } 19 | } 20 | 21 | return ans; 22 | } 23 | }; -------------------------------------------------------------------------------- /kth-largest-element-in-a-stream/kth-largest-element-in-a-stream.cpp: -------------------------------------------------------------------------------- 1 | class KthLargest { 2 | priority_queue, greater> pq;// minheap 3 | int k; 4 | public: 5 | KthLargest(int k, vector& nums) { 6 | this->k = k; 7 | for(auto x: nums){ 8 | this->add(x); 9 | } 10 | } 11 | 12 | int add(int val) { 13 | pq.push(val); 14 | 15 | if(pq.size() > k) 16 | pq.pop(); 17 | 18 | return pq.top(); 19 | } 20 | }; 21 | 22 | /** 23 | * Your KthLargest object will be instantiated and called as such: 24 | * KthLargest* obj = new KthLargest(k, nums); 25 | * int param_1 = obj->add(val); 26 | */ -------------------------------------------------------------------------------- /3-longest-substring-without-repeating-characters/3-longest-substring-without-repeating-characters.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int lengthOfLongestSubstring(string str) { 4 | // abcasdfsjfsdllkdf 5 | 6 | //l ^ 7 | //r ^ 8 | 9 | // substring - think of sliding window 10 | int n = str.size(); 11 | 12 | unordered_map mp; 13 | int sz = 0; 14 | for(int i = 0, j = 0; i <= j and j < n; j++) { 15 | mp[str[j]]++; 16 | while(mp[str[j]] > 1) { 17 | mp[str[i]]--; 18 | i++; 19 | } 20 | sz = max(sz, j - i + 1); 21 | } 22 | return sz; 23 | } 24 | }; -------------------------------------------------------------------------------- /Session 2019/263.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isUgly(int n) { 4 | if(n<=0) 5 | return false; 6 | 7 | while(!(n&1)){ 8 | //2- factor 9 | n/=2; 10 | } 11 | 12 | // n- odd 13 | 14 | for(int i=3; i<=sqrt(n); i+=2){ 15 | 16 | while(n%i==0){ 17 | if(i!=3 and i!=5) 18 | return false; 19 | n/=i; 20 | } 21 | } 22 | 23 | if(n>1){ 24 | if(n!=2 and n!=3 and n!=5) 25 | return false; 26 | } 27 | 28 | 29 | return true; 30 | 31 | } 32 | }; -------------------------------------------------------------------------------- /contiguous-array/README.md: -------------------------------------------------------------------------------- 1 |

525. Contiguous Array

Medium


Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1.

2 | 3 | 4 |

Example 1:
5 |

Input: [0,1]
 6 | Output: 2
 7 | Explanation: [0, 1] is the longest contiguous subarray with equal number of 0 and 1.
 8 | 
9 |

10 | 11 |

Example 2:
12 |

Input: [0,1,0]
13 | Output: 2
14 | Explanation: [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1.
15 | 
16 |

17 | 18 |

Note: 19 | The length of the given binary array will not exceed 50,000. 20 |

-------------------------------------------------------------------------------- /Apr'20/1029(Two City Scheduling).cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int twoCitySchedCost(vector>& costs) { 4 | vector> diff; 5 | 6 | for(int i = 0; i < (int)costs.size(); i++){ 7 | diff.push_back({costs[i][1] - costs[i][0], i}); 8 | } 9 | 10 | sort(diff.begin(), diff.end()); 11 | int n = costs.size(); 12 | 13 | int cst = 0; 14 | for(int i = 0; i < n; i++){ 15 | if(i < n/2) 16 | cst += costs[diff[i].second][1];//B 17 | else 18 | cst += costs[diff[i].second][0];//A 19 | } 20 | 21 | return cst; 22 | 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /Apr'20/406(Queue Reconstruction by Height).cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | static bool comp(const vector& v1, const vector& v2){ 5 | if(v1[0] == v2[0]) 6 | return v1[1] < v2[1]; 7 | else 8 | return v1[0] > v2[0]; 9 | } 10 | vector> reconstructQueue(vector>& people) { 11 | sort(people.begin(), people.end(), comp); 12 | 13 | int n = people.size(); 14 | vector> ans; 15 | for(auto p: people){ 16 | int idx = p[1]; 17 | ans.insert(ans.begin() + idx, p); 18 | } 19 | 20 | return ans; 21 | 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /Session 2019/1043.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxSumAfterPartitioning(vector& A, int k) { 4 | 5 | int n = A.size(); 6 | int dp[n]; 7 | dp[0] = A[0]; 8 | 9 | int mx = A[0]; 10 | for(int i = 1; i < k; i++){ 11 | mx = max(mx, A[i]); 12 | dp[i] = mx * (i + 1); 13 | } 14 | 15 | for(int i = k; i < n; i++){ 16 | dp[i] = dp[i-1] + A[i]; 17 | int nx = A[i]; 18 | 19 | for(int j = i - 1; j >= (i-(k-1)); j--){ 20 | nx = max(nx, A[j]); 21 | dp[i] = max(dp[j-1] + nx * (i-j+1), dp[i]); 22 | } 23 | } 24 | 25 | return dp[n-1]; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /valid-triangle-number/README.md: -------------------------------------------------------------------------------- 1 |

611. Valid Triangle Number

Medium


Given an array consists of non-negative integers, your task is to count the number of triplets chosen from the array that can make triangles if we take them as side lengths of a triangle. 2 | 3 |

Example 1:
4 |

Input: [2,2,3,4]
 5 | Output: 3
 6 | Explanation:
 7 | Valid combinations are: 
 8 | 2,3,4 (using the first 2)
 9 | 2,3,4 (using the second 2)
10 | 2,2,3
11 | 
12 |

13 | 14 |

Note:
15 |

    16 |
  1. The length of the given array won't exceed 1000.
  2. 17 |
  3. The integers in the given array are in the range of [0, 1000].
  4. 18 |
19 |

20 |
-------------------------------------------------------------------------------- /20-valid-parentheses/20-valid-parentheses.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isValid(string str) { 4 | // (', ')', '{', '}', '[' and ']' 5 | int n = str.size(); 6 | stack st; 7 | 8 | for (int i = 0; i < n; i++) { 9 | char c = str[i]; 10 | if (c == '(' or c == '{' or c == '[') 11 | st.push(c); 12 | else if (st.empty() || (c == ')' and st.top() != '(') || (c == ']' and st.top() != '[') || (c == '}') and st.top() != '{') 13 | return false; 14 | else 15 | st.pop(); 16 | } 17 | 18 | return st.empty(); 19 | } 20 | }; 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /Premium/Paint Fence.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | /** 4 | * @param n: non-negative integer, n posts 5 | * @param k: non-negative integer, k colors 6 | * @return: an integer, the total number of ways 7 | */ 8 | int numWays(int n, int k) { 9 | // write your code here 10 | int same = 0; 11 | int diff = k; 12 | int res = same + diff; 13 | 14 | for(int i = 1; i < n; i++){ 15 | // int old_diff = diff; 16 | same = diff * 1; 17 | diff = res * (k - 1); 18 | res = same + diff; 19 | } 20 | 21 | return res; 22 | } 23 | }; 24 | // https://www.lintcode.com/problem/paint-fence/description 25 | -------------------------------------------------------------------------------- /Session 2019/404.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 | int sumOfLeftLeaves(TreeNode* root) { 13 | 14 | if(root == NULL) 15 | return 0; 16 | 17 | if(root->left) 18 | if(root->left->left == NULL and root->left->right == NULL) 19 | return root->left->val + sumOfLeftLeaves(root->right); 20 | 21 | return sumOfLeftLeaves(root->left) + sumOfLeftLeaves(root->right); 22 | 23 | 24 | 25 | } 26 | }; -------------------------------------------------------------------------------- /minimum-subsequence-in-non-increasing-order/minimum-subsequence-in-non-increasing-order.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 |    vector minSubsequence(vector& nums) { 4 |        sort(nums.begin(), nums.end());// nlogn 5 |        int n = nums.size(); 6 |        int sum = 0; 7 |        for(auto x: nums) 8 |            sum += x; 9 |         10 |        int suff = 0; 11 |        int rem; 12 |        vector vec; 13 |        for(int i = n - 1; i >= 0; i--){ 14 |            suff += nums[i]; 15 |            rem = sum - suff; 16 |            vec.push_back(nums[i]); 17 |            if(suff > rem){ 18 |                break; 19 |           } 20 |       } 21 |         22 |        return vec; 23 |    } 24 | }; 25 | -------------------------------------------------------------------------------- /Session 2019/451.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string frequencySort(string s) { 4 | map mp; 5 | for(auto x:s) 6 | mp[x]++; 7 | 8 | vector> vec; 9 | 10 | for(auto x:mp) 11 | vec.push_back({x.second, x.first}); 12 | 13 | sort(vec.begin(), vec.end()); 14 | reverse(vec.begin(), vec.end()); 15 | 16 | string ans = ""; 17 | 18 | for(auto x: vec){ 19 | 20 | int t = x.first; 21 | cout << t <<" " << x.second <> adj; 4 | 5 | int maxTime(int root, vector& cost){ 6 | // base 7 | 8 | // main 9 | int mx = 0; 10 | for(auto v: adj[root]) 11 | mx = max(mx, maxTime(v, cost)); 12 | return mx + cost[root]; 13 | } 14 | 15 | int numOfMinutes(int n, int headID, vector& manager, vector& informTime) { 16 | adj.assign(n + 1, {}); 17 | 18 | for(int i = 0; i < n; i++){ 19 | int par = manager[i]; 20 | if(par != -1) 21 | adj[par].push_back(i); 22 | } 23 | 24 | return maxTime(headID, informTime); 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /Session 2019/985better.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector sumEvenAfterQueries(vector& A, vector>& queries) { 4 | int even = 0; 5 | for(auto x: A) 6 | if(!(x&1)) 7 | even += x; 8 | 9 | vector ans; 10 | for(auto x: queries){ 11 | int idx = x[1]; 12 | int val = x[0]; 13 | 14 | if(A[idx]%2 == 0) 15 | even -= A[idx]; 16 | 17 | A[idx] += val; 18 | 19 | if(A[idx]%2 == 0) 20 | even += A[idx]; 21 | 22 | ans.push_back(even); 23 | } 24 | 25 | return ans; 26 | } 27 | }; -------------------------------------------------------------------------------- /number-of-good-ways-to-split-a-string/number-of-good-ways-to-split-a-string.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numSplits(string str) { 4 | int n = str.size(); 5 | vector pref(n), suff(n); 6 | 7 | set s; 8 | for (int i = 0; i < n; ++i) { 9 | s.insert(str[i]); 10 | pref[i] = s.size(); 11 | } 12 | 13 | s.clear(); 14 | for (int i = n - 1; i >= 0; i--) { 15 | s.insert(str[i]); 16 | suff[i] = s.size(); 17 | } 18 | 19 | int spl = 0; 20 | for (int i = 0; i < n - 1; ++i) { 21 | if (pref[i] == suff[i + 1]) 22 | spl++; 23 | } 24 | 25 | return spl; 26 | } 27 | }; -------------------------------------------------------------------------------- /peak-index-in-a-mountain-array/peak-index-in-a-mountain-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int peakIndexInMountainArray(vector& arr) { 4 | int n = arr.size(); 5 | int low = 0, high = n-1; 6 | while(low <= high) { 7 | int mid = (low + high) / 2; 8 | if(mid == 0) 9 | low = mid + 1; 10 | else if(mid == n-1) 11 | high = mid - 1; 12 | else if(arr[mid-1] < arr[mid] and arr[mid] > arr[mid + 1]) 13 | return mid; 14 | else if(arr[mid] < arr[mid+1]) 15 | low = mid + 1; 16 | else 17 | high = mid - 1; 18 | } 19 | 20 | return -1; 21 | } 22 | }; -------------------------------------------------------------------------------- /student-attendance-record-i/student-attendance-record-i.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool checkRecord(string str) { 4 | int a = 0; 5 | int l = 0; 6 | 7 | bool flag = true; 8 | for(auto x: str){ 9 | 10 | if(x == 'L'){ 11 | l++; 12 | if(l >= 3){ 13 | flag = false; 14 | break; 15 | } 16 | }else { 17 | if(x == 'A') { a++; if(a >= 2) { 18 | flag = false; 19 | break; 20 | }} 21 | l = 0; 22 | } 23 | 24 | } 25 | 26 | return flag; 27 | } 28 | }; -------------------------------------------------------------------------------- /Session 2019/102(rec).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 | typedef struct TreeNode node; 11 | class Solution { 12 | public: 13 | bool isMirror(node* a, node* b){ 14 | if(a == NULL and b == NULL) 15 | return true; 16 | if(a == NULL or b == NULL) 17 | return false; 18 | return (a->val == b->val) and isMirror(a->left, b->right) and isMirror(a->right, b->left); 19 | } 20 | bool isSymmetric(TreeNode* root) { 21 | if(root == NULL) 22 | return true; 23 | return isMirror(root->left, root->right); 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /Session 2019/416.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canPartition(vector& nums) { 4 | // s = sum/2 5 | int sum = 0; 6 | for (auto x : nums) 7 | sum += x; 8 | 9 | if (sum & 1) 10 | return false; 11 | 12 | sum = sum / 2; 13 | 14 | int n = nums.size(); 15 | bool dp[n + 1][sum + 1]; 16 | 17 | 18 | for (int i = 0; i <= n; i++) { 19 | for (int j = 0; j <= sum; j++) { 20 | if (j == 0) 21 | dp[i][j] = true; 22 | else if (i == 0) 23 | dp[i][j] = false; 24 | else { 25 | if (nums[i - 1] <= j) 26 | dp[i][j] = dp[i - 1][j] or dp[i - 1][j - nums[i - 1]]; 27 | else 28 | dp[i][j] = dp[i - 1][j]; 29 | } 30 | 31 | } 32 | } 33 | 34 | return dp[n][sum]; 35 | } 36 | }; 37 | -------------------------------------------------------------------------------- /Session 2019/970.py: -------------------------------------------------------------------------------- 1 | import math 2 | class Solution: 3 | def powerfulIntegers(self, x: int, y: int, bound: int) -> List[int]: 4 | imax = int(math.log(1000000)/math.log(2)) + 1 5 | jmax = imax 6 | ans = [] 7 | hash = {} 8 | for j in range(0,jmax): 9 | for i in range(0,imax): 10 | val = (int) (x**i + y**j) 11 | print(i,j,val) 12 | if( val <= bound): 13 | if(hash.get(val,-1) == -1): 14 | ans.append(val) 15 | hash[val] = hash.get(val,0) + 1 16 | else: 17 | break 18 | 19 | return ans 20 | 21 | 22 | -------------------------------------------------------------------------------- /Session 2019/Old/53.MaximumSubarray.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxSubArray(vector& nums) { 4 | 5 | int ans=INT_MIN,max_ending_here=0,start=0,end=0,s=0; 6 | int sz=nums.size(); 7 | 8 | for(int i=0; i& arr) { 4 | int n = arr.size(); 5 | // [1] 6 | // [1|2] [2] 7 | // [1|2|3] [2|3] [3] 8 | 9 | set s; 10 | for (int i = 0; i < n; ++i){ 11 | if(i == 0) s.insert(arr[i]); 12 | else { 13 | s.insert(arr[i]); 14 | for(int j = i - 1; j >= 0; j--) { 15 | if((arr[j] | arr[i]) == arr[j]) 16 | break; 17 | s.insert(arr[i] | arr[j]); 18 | arr[j] |= arr[i]; 19 | } 20 | } 21 | } 22 | 23 | return s.size(); 24 | } 25 | }; -------------------------------------------------------------------------------- /931/main.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minFallingPathSum(int[][] A) { 3 | int n = A.length; 4 | int m = A[0].length; 5 | 6 | // vector> f(n, vector(m, 0)); 7 | int[][] f = new int[n][m]; 8 | 9 | for (int i = 0; i < n; i++) { 10 | for (int j = 0; j < m; j++) { 11 | if (i == 0) 12 | f[i][j] = A[i][j]; 13 | else { 14 | int val = f[i - 1][j]; 15 | for (int k = j - 1; k <= j + 1; k++) 16 | if (k >= 0 && k < m) 17 | val = Math.min(val, f[i - 1][k]); 18 | 19 | f[i][j] = val + A[i][j]; 20 | } 21 | } 22 | } 23 | 24 | int ans = Integer.MAX_VALUE; 25 | for (int j = 0; j < m; j++) 26 | ans = Math.min(ans, f[n - 1][j]); 27 | 28 | return ans; 29 | } 30 | } -------------------------------------------------------------------------------- /931/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minFallingPathSum(self, A: List[List[int]]) -> int: 3 | n = len(A) 4 | m = len(A[0]) 5 | 6 | f = [[None for _ in range(m)] for _ in range(n)] 7 | 8 | for i in range(n): 9 | for j in range(m): 10 | if i == 0: 11 | f[i][j] = A[i][j] 12 | else: 13 | val = f[i-1][j] 14 | for k in [j-1,j,j+1]: 15 | if k >= 0 and k < m: 16 | val = min(val, f[i-1][k]) 17 | f[i][j] = val + A[i][j] 18 | 19 | ans = float('inf') 20 | for j in range(m): 21 | ans = min(ans, f[n-1][j]) 22 | return ans -------------------------------------------------------------------------------- /Session 2019/997.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findJudge(int N, vector>& trust) { 4 | map> mp; 5 | map np; 6 | for(auto vec : trust){ 7 | mp[vec[1]].insert(vec[0]); 8 | np[vec[0]]++; 9 | } 10 | 11 | 12 | 13 | 14 | bool found = false; 15 | int val; 16 | for(int i = 1; i <= N; i++){ 17 | if(mp[i].size() == (N-1) and (np[i] == 0)) 18 | { 19 | found = true; 20 | val = i; 21 | break; 22 | } 23 | } 24 | 25 | if(found) 26 | return val; 27 | else 28 | return -1; 29 | } 30 | }; -------------------------------------------------------------------------------- /Session 2019/5.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int n; 4 | int expand(string str, int i, int j) { 5 | while (i >= 0 and j < n and str[i] == str[j]) { 6 | i--; 7 | j++; 8 | } 9 | 10 | return (j - i- 1); 11 | 12 | } 13 | string longestPalindrome(string str) { 14 | n = str.length(); 15 | 16 | if (n == 0) 17 | return ""; 18 | int start = 0; 19 | int end = 0; 20 | 21 | for (int i = 0; i < n; i++) { 22 | int len1 = expand(str, i, i); 23 | int len2 = expand(str, i, i + 1); 24 | 25 | int len = max(len1, len2); 26 | if (len > end - start + 1) { 27 | start = i - (len-1)/2; 28 | end = i + len/2; 29 | } 30 | } 31 | cout << start << " " << end; 32 | return str.substr(start, end - start + 1); 33 | } 34 | 35 | }; 36 | -------------------------------------------------------------------------------- /Session 2019/Old/35.SearchInsertPosition.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int searchInsert(vector& nums, int target) { 4 | 5 | int sz=nums.size(); 6 | int low=0; 7 | int high=sz-1; 8 | int expected; 9 | while(low<=high){ 10 | 11 | int mid=low+(high-low)/2; 12 | if(nums[mid]==target) 13 | return mid; 14 | if(target>nums[mid]){ 15 | low=mid+1; 16 | expected=low; 17 | } 18 | else{ 19 | high=mid-1; 20 | expected=mid; 21 | } 22 | 23 | } 24 | 25 | return expected; 26 | 27 | } 28 | }; -------------------------------------------------------------------------------- /Session 2019/303.cpp: -------------------------------------------------------------------------------- 1 | class NumArray { 2 | vector vec; 3 | public: 4 | NumArray(vector& nums) { 5 | for(auto x:nums) 6 | vec.push_back(x); 7 | } 8 | 9 | int sumRange(int i, int j) { 10 | 11 | int n = vec.size(); 12 | int dp[n + 1]; 13 | 14 | 15 | 16 | for(int i=0; i<=n; i++){ 17 | if(i==0) 18 | dp[i] = 0; 19 | else 20 | dp[i] = dp[i-1] + vec[i - 1]; 21 | } 22 | 23 | return dp[j+1] - dp[i]; 24 | 25 | } 26 | }; 27 | 28 | /** 29 | * Your NumArray object will be instantiated and called as such: 30 | * NumArray* obj = new NumArray(nums); 31 | * int param_1 = obj->sumRange(i,j); 32 | */ -------------------------------------------------------------------------------- /Session 2019/60.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string getPermutation(int n, int k) { 4 | vector arr; 5 | for(int i = 1; i <= n; i++) 6 | arr.push_back(i); 7 | 8 | vector fac(n+1); 9 | fac[0] = 1; 10 | for(int i = 1; i < n; i++) 11 | fac[i] = fac[i-1]*i; 12 | 13 | k = k - 1; 14 | 15 | string ans = ""; 16 | 17 | for(int i = n; i > 0; i--){ 18 | 19 | int idx = k/fac[i-1]; 20 | k = k % fac[i-1]; 21 | cout << idx << endl; 22 | ans = ans + to_string(arr[idx]); 23 | arr.erase(arr.begin() + idx); 24 | } 25 | 26 | return ans; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /Session 2019/Old/13.RomanToInteger(UnorderedMap).cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int romanToInt(string s) { 4 | 5 | std::unordered_map jjh({ 6 | {'I',1}, 7 | {'V',5}, 8 | {'X',10}, 9 | {'L',50}, 10 | {'C',100}, 11 | {'D',500}, 12 | {'M',1000} 13 | }); 14 | 15 | int sz=s.length(); 16 | int ans=0; 17 | for(int i=0; i mp; 12 | for(auto x:s) 13 | mp[x]++; 14 | 15 | int len=0; 16 | bool odd = false; 17 | for(auto x:mp){ 18 | if(x.second%2 == 0) 19 | len += x.second; 20 | else{ 21 | odd = true; 22 | len +=(x.second - 1); 23 | } 24 | 25 | } 26 | 27 | if(odd) 28 | return len + 1; 29 | else 30 | return len; 31 | 32 | } 33 | }; -------------------------------------------------------------------------------- /797/797.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> dfs(vector>& adj, int x, int n) { 4 | // base 5 | if (x == n - 1) { 6 | return {{x}}; 7 | } 8 | 9 | // main 10 | vector> ans; 11 | for (auto v : adj[x]) { 12 | // x -> v 13 | vector> paths = dfs(adj, v, n); 14 | for (auto subpath : paths) { 15 | subpath.push_back(x); 16 | ans.push_back(subpath); 17 | } 18 | } 19 | 20 | return ans; 21 | } 22 | 23 | vector> allPathsSourceTarget(vector>& graph) { 24 | int n = graph.size(); 25 | vector> paths = dfs(graph, 0, n); 26 | 27 | for(auto& path: paths){ 28 | reverse(path.begin(), path.end()); 29 | } 30 | return paths; 31 | } 32 | }; 33 | -------------------------------------------------------------------------------- /Apr'20/38(Count and Say).cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | string solve(string str){ 5 | int n = str.size(); 6 | string ans = ""; 7 | 8 | for(int i = 0; i < n; i++){ 9 | int cnt = 1; 10 | while(i + 1 < n and str[i] == str[i+1]){ 11 | cnt++; 12 | i++; 13 | } 14 | ans = ans + to_string(cnt) + str[i]; 15 | } 16 | 17 | return ans; 18 | } 19 | 20 | string countAndSay(int n) { 21 | vector vec(n + 1); 22 | vec[1] = "1"; 23 | 24 | for(int i = 2; i <= n; i++){ 25 | vec[i] = solve(vec[i - 1]); 26 | } 27 | 28 | return vec[n]; 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /Session 2019/Old/7.ReverseInteger.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPalindrome(int x) { 4 | 5 | if(x<0) 6 | return false; 7 | 8 | 9 | //log n time to extract the highest divisor 10 | int divisor=1; 11 | int orig=x; 12 | while(x>0) 13 | { 14 | x/=10; 15 | divisor*=10; 16 | } 17 | 18 | divisor/=10; 19 | 20 | x=orig; 21 | while(x>0) 22 | { 23 | int lead= x/divisor; 24 | int trail= x%10; 25 | 26 | if(lead!=trail) 27 | return false; 28 | 29 | x=(x%divisor)/10; 30 | 31 | divisor/=100; 32 | 33 | } 34 | 35 | return true; 36 | 37 | 38 | } 39 | }; 40 | -------------------------------------------------------------------------------- /Top Qs/Strings/LCP.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | string LCP(string a, string b){ 5 | int i = 0, j = 0; 6 | while(i < (int)a.length() and j < (int)b.length()){ 7 | if(a[i] == b[j]){ 8 | i++; 9 | j++; 10 | }else{ 11 | return a.substr(0, i); 12 | } 13 | } 14 | 15 | return a.substr(0, i); 16 | 17 | } 18 | 19 | string longestCommonPrefix(vector& strs) { 20 | int n = strs.size(); 21 | if(n == 0) 22 | return ""; 23 | 24 | string ans = strs[0]; 25 | for(auto str: strs){ 26 | ans = LCP(ans, str); 27 | } 28 | return ans; 29 | } 30 | }; -------------------------------------------------------------------------------- /minimum-number-of-arrows-to-burst-balloons/minimum-number-of-arrows-to-burst-balloons.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | static bool cmp(vector& lhs, vector& rhs){ 4 | // sort asc by ending point 5 | return lhs[1] < rhs[1]; 6 | } 7 | 8 | int findMinArrowShots(vector>& points) { 9 | int n = points.size(); 10 | sort(points.begin(), points.end(), cmp); 11 | 12 | int shoot = points[0][1]; 13 | int bullets = 1; 14 | 15 | for(int i = 1; i < n; i++){ 16 | if(points[i][0] <= shoot) continue; 17 | else { 18 | bullets++; 19 | shoot = points[i][1]; 20 | } 21 | } 22 | 23 | return bullets; 24 | } 25 | }; -------------------------------------------------------------------------------- /random-pick-index/README.md: -------------------------------------------------------------------------------- 1 |

398. Random Pick Index

Medium


Given an array of integers with possible duplicates, randomly output the index of a given target number. You can assume that the given target number must exist in the array.

2 | 3 |

Note:
4 | The array size can be very large. Solution that uses too much extra space will not pass the judge.

5 | 6 |

Example:

7 | 8 |
int[] nums = new int[] {1,2,3,3,3};
 9 | Solution solution = new Solution(nums);
10 | 
11 | // pick(3) should return either index 2, 3, or 4 randomly. Each index should have equal probability of returning.
12 | solution.pick(3);
13 | 
14 | // pick(1) should return 0. Since in the array only nums[0] is equal to 1.
15 | solution.pick(1);
16 | 
17 |
-------------------------------------------------------------------------------- /Session 2019/523.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool checkSubarraySum(vector& nums, int k) { 4 | int n = nums.size(); 5 | 6 | if (n <= 1) 7 | return false; 8 | 9 | for (int i = 0; i < n-1; i++) { 10 | if (nums[i] == 0 && nums[i + 1] == 0) return true; 11 | } 12 | 13 | if(k == 0) 14 | return false; 15 | 16 | map mp; 17 | 18 | mp[0] = -1; 19 | int sum = 0; 20 | 21 | for (int i = 0; i < n; i++) { 22 | sum += nums[i]; 23 | 24 | if (k != 0) { 25 | sum = sum % k; 26 | 27 | 28 | if (mp[sum] != 0 and (i - (mp[sum]) + 1 ) >= 2) 29 | return true; 30 | else 31 | mp[sum] = i + 1; 32 | } 33 | 34 | } 35 | 36 | return false; 37 | } 38 | }; 39 | -------------------------------------------------------------------------------- /Session 2019/Old/9.PalindromeNumber.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPalindrome(int x) { 4 | 5 | if(x<0) 6 | return false; 7 | 8 | 9 | //log n time to extract the highest divisor 10 | int divisor=1; 11 | int orig=x; 12 | while(x>0) 13 | { 14 | x/=10; 15 | divisor*=10; 16 | } 17 | 18 | divisor/=10; 19 | 20 | x=orig; 21 | while(x>0) 22 | { 23 | int lead= x/divisor; 24 | int trail= x%10; 25 | 26 | if(lead!=trail) 27 | return false; 28 | 29 | x=(x%divisor)/10; 30 | 31 | divisor/=100; 32 | 33 | } 34 | 35 | return true; 36 | 37 | 38 | } 39 | }; 40 | -------------------------------------------------------------------------------- /n-ary-tree-preorder-traversal/n-ary-tree-preorder-traversal.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | // Definition for a Node. 3 | class Node { 4 | public: 5 | int val; 6 | vector children; 7 | 8 | Node() {} 9 | 10 | Node(int _val) { 11 | val = _val; 12 | } 13 | 14 | Node(int _val, vector _children) { 15 | val = _val; 16 | children = _children; 17 | } 18 | }; 19 | */ 20 | 21 | class Solution { 22 | vector pre; 23 | public: 24 | void dfs(Node* root){ 25 | if(root == NULL) 26 | return; 27 | pre.push_back(root->val); 28 | for(auto x: root->children){ 29 | dfs(x); 30 | } 31 | } 32 | vector preorder(Node* root) { 33 | dfs(root); 34 | return pre; 35 | } 36 | }; -------------------------------------------------------------------------------- /Apr'20/72(Edit Distance).cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | int dp[1000][1000]; 5 | int ED(string& A, string& B, int m, int n){ 6 | if(m == 0) 7 | return n; 8 | if(n == 0) 9 | return m; 10 | 11 | if(dp[m][n] != -1) 12 | return dp[m][n]; 13 | 14 | if(A[m - 1] == B[n - 1]) 15 | return dp[m][n] = ED(A, B, m - 1, n - 1); 16 | 17 | return dp[m][n] = 1 + min({ED(A,B,m-1,n),ED(A,B,m,n - 1),ED(A,B,m-1,n - 1)}); 18 | } 19 | 20 | int minDistance(string word1, string word2) { 21 | int m = word1.length(); 22 | int n = word2.length(); 23 | 24 | memset(dp, -1, sizeof(dp)); 25 | return ED(word1, word2, m, n); 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /Session 2019/144(BT Preorder Traversal).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 | typedef struct TreeNode node; 11 | class Solution { 12 | public: 13 | void preorder(node* root, vector& ans){ 14 | stack s; 15 | s.push(root); 16 | 17 | while(!s.empty()){ 18 | node* curr = s.top();s.pop(); 19 | if(curr != NULL){ 20 | s.push(curr->right); 21 | s.push(curr->left); 22 | ans.push_back(curr->val); 23 | } 24 | } 25 | 26 | 27 | } 28 | vector preorderTraversal(TreeNode* root) { 29 | vector ans; 30 | preorder(root, ans); 31 | return ans; 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /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(int x) : val(x), left(NULL), right(NULL) {} 8 | * }; 9 | */ 10 | class Solution { 11 | vector l1; 12 | vector l2; 13 | public: 14 | void dfs(TreeNode* root, vector& l){ 15 | if(root == NULL) 16 | return; 17 | if(root->left == NULL and root->right == NULL) 18 | l.push_back(root->val); 19 | dfs(root->left, l); 20 | dfs(root->right, l); 21 | } 22 | bool leafSimilar(TreeNode* root1, TreeNode* root2) { 23 | dfs(root1, l1); 24 | dfs(root2, l2); 25 | 26 | return (l1 == l2); 27 | } 28 | }; -------------------------------------------------------------------------------- /validate-binary-search-tree/validate-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 | bool isValidBST(TreeNode* root, long minval = LLONG_MIN, long maxval = LLONG_MAX) { 15 | if(root == NULL) 16 | return true; 17 | return isValidBST(root->left, minval, root->val) and (minval < root->val and root->val < maxval) and isValidBST(root->right, root->val, maxval); 18 | } 19 | }; -------------------------------------------------------------------------------- /Session 2019/56(updt).cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isOverlap(const vector& x, const vector& y) { 4 | int a = max(x[0], y[0]); 5 | int b = min(x[1], y[1]); 6 | 7 | return a <= b; 8 | } 9 | vector> merge(vector>& intervals) { 10 | sort(intervals.begin(), intervals.end()); 11 | int n = intervals.size(); 12 | if (n == 0) 13 | return {}; 14 | vector> ans; 15 | 16 | vector t = intervals[0]; 17 | 18 | for (int i = 1; i < n; ++i) { 19 | if (isOverlap(t, intervals[i])) { 20 | // overlap 21 | t[0] = min(t[0], intervals[i][0]); 22 | t[1] = max(t[1], intervals[i][1]); 23 | } else { 24 | ans.push_back(t); 25 | t = intervals[i]; 26 | } 27 | } 28 | ans.push_back(t); 29 | 30 | return ans; 31 | } 32 | }; 33 | -------------------------------------------------------------------------------- /maximum-product-subarray/maximum-product-subarray.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProduct(vector& nums) { 4 | // the req subarray will end at some index i, so max till i 5 | 6 | int n = nums.size(); 7 | if(n == 1) 8 | return nums[0]; 9 | 10 | int ans = 0; 11 | int imax = 0; 12 | int imin = 0; 13 | 14 | for(int i = 0; i < n; i++){ 15 | 16 | if(nums[i] < 0) 17 | swap(imax, imin); 18 | 19 | imax = max(nums[i], imax * nums[i]); 20 | imin = min(nums[i], imin * nums[i]); 21 | 22 | ans = max(ans, imax); 23 | } 24 | 25 | return ans; 26 | } 27 | 28 | 29 | }; -------------------------------------------------------------------------------- /Apr'20/283(Move Zeroes).cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void moveZeroes(vector& nums) { 4 | int n = nums.size(); 5 | int nxt = 0; 6 | for(int i = 0; i < n; i++){ 7 | if(nums[i] != 0){ 8 | nums[nxt] = nums[i]; 9 | nxt++; 10 | } 11 | } 12 | 13 | for(int i = nxt; i < n; i++){ 14 | nums[i] = 0; 15 | } 16 | } 17 | }; 18 | 19 | class Solution { 20 | public: 21 | void moveZeroes(vector& nums) { 22 | int n = nums.size(); 23 | int nxt = 0; 24 | for(int i = 0; i < n; i++){ 25 | if(nums[i] != 0){ 26 | swap(nums[i], nums[nxt++]); 27 | } 28 | } 29 | 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /Session 2019/119.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | vector pascal(int n){ 5 | int dp[n+1][n+1]; 6 | 7 | vector ans; 8 | for(int i=0; i<=n; i++){ 9 | 10 | 11 | for(int j=0; j<=i; j++){ 12 | if(j==0 or j==i) 13 | dp[i][j] = 1; 14 | else 15 | dp[i][j] = dp[i-1][j-1] + dp[i-1][j]; 16 | 17 | } 18 | } 19 | 20 | for(int j=0; j<=n; j++) 21 | ans.push_back(dp[n][j]); 22 | 23 | return ans; 24 | 25 | } 26 | 27 | vector getRow(int rowIndex) { 28 | 29 | vector ans = pascal(rowIndex); 30 | 31 | return ans; 32 | } 33 | }; -------------------------------------------------------------------------------- /longest-mountain-in-array/longest-mountain-in-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int longestMountain(vector& arr) { 4 | int n = arr.size(); 5 | if(n < 3) 6 | return 0; 7 | vector lhill(n, 1), rhill(n, 1); 8 | for (int i = 1; i < n; ++i) { 9 | if (arr[i - 1] < arr[i]) 10 | lhill[i] = lhill[i - 1] + 1; 11 | } 12 | 13 | for(int i = n - 2; i >= 0; i--) { 14 | if(arr[i] > arr[i+1]) 15 | rhill[i] = rhill[i + 1] + 1; 16 | } 17 | 18 | int mx = 0; 19 | for (int i = 1; i < n - 1; ++i){ 20 | if(lhill[i] != 1 and rhill[i] != 1) 21 | mx = max(mx, lhill[i] + rhill[i] - 1); 22 | } 23 | 24 | return mx; 25 | } 26 | }; -------------------------------------------------------------------------------- /Session 2019/Youtube/Split.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // To execute C++, please define "int main()" 5 | int main() { 6 | 7 | string str = "Harry-Potter-And-The-Prisoner-Of-Azkaban"; 8 | string delim = "-"; 9 | 10 | int dl = delim.length(); 11 | 12 | auto start = 0U; 13 | auto end = str.find(delim); 14 | 15 | vector ans; 16 | 17 | while(end != std::string::npos){ 18 | ans.push_back(str.substr(start, end - start)); 19 | start = end + dl; 20 | end = str.find(delim, start); 21 | } 22 | 23 | ans.push_back(str.substr(start, end - start)); 24 | 25 | 26 | for(auto x: ans) 27 | cout << x <> s; 3 | vector vec; 4 | public: 5 | 6 | void helper(int idx, int n, const vector& nums){ 7 | if(idx == n){ 8 | s.insert(vec); 9 | return; 10 | } 11 | 12 | vec.push_back(nums[idx]); 13 | helper(idx + 1, n, nums); 14 | vec.pop_back(); 15 | helper(idx + 1, n, nums); 16 | } 17 | 18 | vector> subsetsWithDup(vector& nums) { 19 | sort(nums.begin(), nums.end()); 20 | int n = nums.size(); 21 | 22 | s.clear(); 23 | vec.clear(); 24 | vector> global; 25 | helper(0, n, nums); 26 | global.assign(s.begin(), s.end()); 27 | return global; 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /next-permutation/next-permutation.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void nextPermutation(vector& nums) { 4 | // 5 | int idx = -1; 6 | int n = nums.size(); 7 | for(int i = n - 1; i > 0; i--){ 8 | if(nums[i - 1] < nums[i]){ 9 | idx = i - 1; 10 | break; 11 | } 12 | } 13 | 14 | if(idx == -1){ 15 | 16 | reverse(nums.begin(), nums.end()); 17 | return; 18 | } 19 | 20 | for(int i = n - 1; i > idx; i--){ 21 | if(nums[i] > nums[idx]){ 22 | swap(nums[i], nums[idx]); 23 | break; 24 | } 25 | } 26 | 27 | reverse(nums.begin() + idx + 1, nums.end()); 28 | 29 | } 30 | }; -------------------------------------------------------------------------------- /64/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minPathSum(vector>& grid) { 4 | int n = grid.size(); 5 | int m = grid[0].size(); 6 | 7 | // dp grid f 8 | vector> f(n, vector(m, 0)); 9 | f[0][0] = grid[0][0]; 10 | for (int j = 1; j < m; j++) 11 | f[0][j] = f[0][j - 1] + grid[0][j]; 12 | for (int i = 1; i < n; i++) 13 | f[i][0] = f[i - 1][0] + grid[i][0]; 14 | 15 | // work 16 | // f[i][j] = min cost to reach cell(i,j) from 0,0 17 | for (int i = 1; i < n; i++) { 18 | for (int j = 1; j < m; j++) { 19 | f[i][j] = min(f[i - 1][j], f[i][j - 1]) + grid[i][j]; 20 | } 21 | } 22 | 23 | return f[n - 1][m - 1]; 24 | 25 | } 26 | }; -------------------------------------------------------------------------------- /Apr'20/1072(Flip Columns For Maximum Number of Equal Rows).cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxEqualRowsAfterFlips(vector>& matrix) { 4 | int n = matrix.size(); 5 | int m = matrix[0].size(); 6 | 7 | int mx = 0; 8 | 9 | for(int i = 0; i < n; i++){ 10 | // row 11 | vector flip; 12 | for(int j = 0; j < m; j++){ 13 | flip.push_back(matrix[i][j] ^ 1); 14 | } 15 | 16 | int cnt = 0; 17 | for(int k = i; k < n; k++){ 18 | if(matrix[k] == flip or matrix[k] == matrix[i]) 19 | cnt++; 20 | } 21 | 22 | mx = max(mx, cnt); 23 | } 24 | 25 | return mx; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /Apr'20/236(LCA of BT).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 | typedef struct TreeNode node; 11 | class Solution { 12 | public: 13 | TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { 14 | if(root == NULL) 15 | return NULL; 16 | if(root == p or root == q) 17 | return root; 18 | node* left = lowestCommonAncestor(root->left, p, q); 19 | node* right = lowestCommonAncestor(root->right, p, q); 20 | 21 | if(left!= NULL and right!= NULL) 22 | return root; 23 | else 24 | return (left ? left : right); 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /Session 2019/Misc/NGE2.cpp: -------------------------------------------------------------------------------- 1 | /*-------------------------------@greenindia-Sabuj-Jana----------------------------------*/ 2 | 3 | #include 4 | using namespace std; 5 | 6 | void NGE2(const vector& arr, int n){ 7 | 8 | vector ans(n); 9 | // iterate from right 10 | stack s; 11 | for(int i = n - 1; i >= 0; i--){ 12 | while(!s.empty() and arr[i] > s.top()) 13 | s.pop(); 14 | 15 | if(s.empty()) 16 | ans[i] = -1; 17 | else 18 | ans[i] = s.top(); 19 | 20 | s.push(arr[i]); 21 | } 22 | 23 | for(int i = 0; i < n; i++) 24 | cout << arr[i] << "-->" << ans[i] << endl; 25 | 26 | } 27 | 28 | int main() { 29 | freopen("input.txt", "r", stdin); 30 | 31 | vector vec = {11, 13, 21, 3}; 32 | int n = vec.size(); 33 | 34 | NGE2(vec,n); 35 | 36 | 37 | return 0; 38 | } 39 | 40 | 41 | -------------------------------------------------------------------------------- /maximal-network-rank/maximal-network-rank.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maximalNetworkRank(int n, vector>& roads) { 4 | vector deg(n, 0); 5 | set> st; 6 | 7 | for(auto v: roads){ 8 | deg[v[0]]++; 9 | deg[v[1]]++; 10 | st.insert({v[0], v[1]}); 11 | st.insert({v[1], v[0]}); 12 | } 13 | 14 | int ans = 0; 15 | for(int i = 0; i < n; i++){ 16 | for(int j = i+1; j < n; j++){ 17 | if(st.count({i,j}) > 0 or st.count({j,i}) > 0) 18 | ans = max(ans, deg[i] + deg[j] - 1); 19 | else 20 | ans = max(ans, deg[i] + deg[j]); 21 | } 22 | } 23 | 24 | return ans; 25 | } 26 | }; -------------------------------------------------------------------------------- /non-decreasing-array/non-decreasing-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool checkPossibility(vector& arr) { 4 | int n = arr.size(); 5 | int cnt = 0; 6 | 7 | if(n == 1) 8 | return true; 9 | 10 | for(int i = 0; i < n; i++){ 11 | if(i == 0){ 12 | continue; 13 | } 14 | else{ 15 | if(arr[i] >= arr[i-1]) 16 | continue; 17 | else{ 18 | cnt++; 19 | if(i - 2 >= 0 and arr[i-2] > arr[i]) 20 | arr[i] = arr[i-1]; 21 | else 22 | arr[i-1] = arr[i]; 23 | } 24 | } 25 | } 26 | 27 | return cnt<=1; 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /Session 2019/20.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isValid(string str) { 4 | if(str == "") 5 | return true; 6 | 7 | stack s; 8 | for(auto x: str){ 9 | if(x == '(' or x== '[' or x=='{') 10 | s.push(x); 11 | else{ 12 | if(s.empty()) return false; 13 | 14 | char y = s.top(); 15 | s.pop(); 16 | 17 | if(x == ')' and y != '(') 18 | return false; 19 | if(x == '}' and y != '{') 20 | return false; 21 | if(x == ']' and y != '[') 22 | return false; 23 | 24 | } 25 | } 26 | 27 | return s.empty(); 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /Session 2019/459.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool repeatedSubstringPattern(string s) { 4 | 5 | int n = s.length(); 6 | 7 | for(int sz = 1; sz <= n/2; sz++){ 8 | // iterate through all possible sizes 9 | if(n % sz == 0){ 10 | // if a possible substr length 11 | 12 | int t = n/sz; // no of substrs 13 | string tmp; 14 | 15 | string part = s.substr(0,sz); 16 | 17 | while(t--)// append the part 't' times to get tmp 18 | tmp += part; 19 | 20 | if (tmp == s) 21 | return true; 22 | } 23 | } 24 | 25 | return false; 26 | 27 | } 28 | }; -------------------------------------------------------------------------------- /Session 2019/236.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 | * }; 9 | */ 10 | 11 | typedef struct TreeNode node; 12 | 13 | class Solution { 14 | public: 15 | TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { 16 | if(root == NULL) 17 | return NULL; 18 | 19 | if(root == p or root == q) 20 | return root; 21 | 22 | node* left = lowestCommonAncestor(root->left, p, q); 23 | node* right = lowestCommonAncestor(root->right, p, q); 24 | 25 | if(left and right) 26 | return root; 27 | else 28 | return (left? left : right); 29 | } 30 | }; -------------------------------------------------------------------------------- /optimal-division/optimal-division.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string optimalDivision(vector& nums) { 4 | int n = nums.size(); 5 | if(n == 1) 6 | return to_string(nums[0]); 7 | else if(n == 2){ 8 | return to_string(nums[0]) + "/" + to_string(nums[1]); 9 | }else{ 10 | string ans = ""; 11 | ans += to_string(nums[0]); 12 | for(int i = 1; i < n; i++){ 13 | if(i == 1) 14 | ans += ("/(" + to_string(nums[i])); 15 | else if(i == n-1) 16 | ans += ("/" + to_string(nums[i]) + ")"); 17 | else 18 | ans += ("/" + to_string(nums[i])); 19 | } 20 | return ans; 21 | } 22 | 23 | return ""; 24 | } 25 | }; -------------------------------------------------------------------------------- /check-array-formation-through-concatenation/check-array-formation-through-concatenation.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canFormArray(vector& arr, vector>& pieces) { 4 | unordered_map mp; 5 | int n = arr.size(); 6 | for(int i = 0; i < n; i++){ 7 | mp[arr[i]] = i + 1; 8 | } 9 | 10 | 11 | for(auto vec: pieces){ 12 | int x = vec.size(); 13 | // if(x == 1) continue; // true 14 | 15 | for(int i = 0; i < x; i++){ 16 | if(mp.find(vec[i]) == mp.end()) return false; 17 | 18 | if(i > 0) 19 | if(mp[vec[i - 1]] + 1 != mp[vec[i]]) return false; 20 | } 21 | } 22 | 23 | return true; 24 | } 25 | }; -------------------------------------------------------------------------------- /merge-intervals/merge-intervals.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> merge(vector>& intervals) { 4 | vector> req; 5 | sort(intervals.begin(), intervals.end()); 6 | 7 | int n = intervals.size(); 8 | if(n == 0) 9 | return {}; 10 | 11 | int s = intervals[0][0]; 12 | int e = intervals[0][1]; 13 | req.push_back({s,e}); 14 | 15 | for(int i = 1; i < n; i++){ 16 | if(req.back()[1] < intervals[i][0]){ 17 | // disjoint 18 | req.push_back(intervals[i]); 19 | }else { 20 | // overlap 21 | req.back()[1] = max(req.back()[1], intervals[i][1]); 22 | } 23 | } 24 | 25 | return req; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /Session 2019/239.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector maxSlidingWindow(vector& nums, int k) { 4 | int n = nums.size(); 5 | if(n == 0) 6 | return {}; 7 | deque dq; 8 | vector ans; 9 | 10 | for(int i = 0; i < n; i++){ 11 | //cond 1 - check if out of window size k 12 | while(!dq.empty() and dq.front() < i - k + 1) 13 | dq.pop_front(); 14 | //cond 2 - check if useless element less than curr element 15 | while(!dq.empty() and nums[dq.back()] < nums[i]) 16 | dq.pop_back(); 17 | 18 | dq.push_back(i); 19 | 20 | if(i>= k-1) 21 | ans.push_back(nums[dq.front()]); 22 | } 23 | 24 | return ans; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /Session 2019/682.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int calPoints(vector& ops) { 4 | stack s; 5 | int req = 0; 6 | for(auto str: ops){ 7 | if(str[0] == 'D'){ 8 | s.push(s.top() * 2); 9 | } 10 | else if(str[0] == 'C'){ 11 | s.pop(); 12 | }else if(str[0] == '+'){ 13 | int x = s.top(); 14 | s.pop(); 15 | int y = s.top(); 16 | s.push(x); 17 | s.push(x + y); 18 | }else{ 19 | int val = stoi(str); 20 | s.push(val); 21 | } 22 | } 23 | 24 | while(!s.empty()) 25 | req += s.top(), s.pop(); 26 | 27 | return req; 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /Session 2019/937.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | static bool isLess(const string& x, const string& y){ 5 | size_t i = x.find(' '); 6 | string id1 = x.substr(0,i); 7 | string word1 = x.substr(i+1); 8 | 9 | i = y.find(' '); 10 | string id2 = y.substr(0,i); 11 | string word2 = y.substr(i+1); 12 | 13 | return (word1==word2)? (id1 < id2) : (word1 < word2); 14 | } 15 | 16 | static bool isLetter(const string& x){ 17 | size_t i = x.find(' '); 18 | return isalpha(x[i+1]); 19 | } 20 | 21 | vector reorderLogFiles(vector& logs) { 22 | auto it = stable_partition(logs.begin(), logs.end(), isLetter); 23 | 24 | sort(logs.begin(), it, isLess); 25 | 26 | return logs; 27 | } 28 | }; -------------------------------------------------------------------------------- /minimum-number-of-refueling-stops/minimum-number-of-refueling-stops.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minRefuelStops(int target, int startFuel, vector>& stations) { 4 | // dp[i] = max dist you can go using i stops 5 | int n = stations.size(); 6 | vector dp(n+1,0); 7 | dp[0] = startFuel; 8 | 9 | // for each station limit 10 | for(int i = 0; i < n; i++){ 11 | for(int t = i; t >= 0; t--){ 12 | if(dp[t] >= stations[i][0]){ 13 | dp[t+1] = max(dp[t+1], dp[t] + stations[i][1]); 14 | } 15 | } 16 | } 17 | 18 | for(int i = 0; i <= n; i++){ 19 | if(dp[i] >= target) 20 | return i; 21 | } 22 | 23 | return -1; 24 | } 25 | }; -------------------------------------------------------------------------------- /pyramid-transition-matrix/pyramid-transition-matrix.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | unordered_map> mp; 3 | public: 4 | 5 | bool dfs(string bottom, string tmp){ 6 | // base 7 | if(bottom.size() == 1) 8 | return true; 9 | if(bottom.size() == tmp.size() + 1) 10 | return dfs(tmp, ""); 11 | 12 | // main 13 | for(auto ch: mp[bottom.substr(tmp.size(), 2)]){ 14 | if(dfs(bottom, tmp + ch)) 15 | return true; 16 | } 17 | 18 | return false; 19 | } 20 | 21 | bool pyramidTransition(string bottom, vector& allowed) { 22 | for(auto str: allowed){ 23 | mp[str.substr(0, 2)].push_back(str[2]); 24 | } 25 | 26 | return dfs(bottom, ""); 27 | } 28 | }; -------------------------------------------------------------------------------- /Session 2019/101.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 | typedef struct TreeNode node; 11 | class Solution { 12 | public: 13 | 14 | bool isSymmetric(node* left, node* right){ 15 | if(left == NULL or right == NULL) 16 | return left==right; 17 | 18 | if(left->val != right->val) 19 | return false; 20 | 21 | return isSymmetric(left->left,right->right) and isSymmetric(left->right,right->left); 22 | } 23 | 24 | bool isSymmetric(TreeNode* root) { 25 | if(root == NULL) 26 | return true; 27 | 28 | return isSymmetric(root->left,root->right); 29 | } 30 | }; -------------------------------------------------------------------------------- /Session 2019/230(kth smallest element in BST).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 | typedef struct TreeNode node; 11 | class Solution { 12 | public: 13 | int inorder(node* root, int k) { 14 | int cnt = 1; 15 | node* curr = root; 16 | stack s; 17 | 18 | while (1) { 19 | while (curr) { 20 | s.push(curr); 21 | curr = curr->left; 22 | } 23 | if (s.empty()) 24 | break; 25 | curr = s.top(); 26 | s.pop(); 27 | if (cnt == k) 28 | return curr->val; 29 | cnt++; 30 | curr = curr->right; 31 | } 32 | return 0; 33 | 34 | } 35 | int kthSmallest(TreeNode* root, int k) { 36 | return inorder(root, k); 37 | } 38 | }; 39 | -------------------------------------------------------------------------------- /lemonade-change/lemonade-change.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 |    bool lemonadeChange(vector& bills) { 4 |        int five = 0, ten = 0, twenty = 0; 5 |        for(auto x: bills){ 6 |            if(x == 5) 7 |                five++; 8 |            else if(x == 10){ 9 |                if(five == 0) 10 |                    return false; 11 |                five--; 12 |                ten++; 13 |           }else if(x == 20){ 14 |                if((ten == 0 || five == 0) and (five < 3)) 15 |                    return false; 16 |                if(ten and five){ 17 |                    ten--; 18 |                    five--; 19 |                    twenty++; 20 |                }else{ 21 |                    five -= 3; 22 |               } 23 |           } 24 |       } 25 |         26 |        return true; 27 |   } 28 | }; 29 | -------------------------------------------------------------------------------- /robot-bounded-in-circle/robot-bounded-in-circle.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | // N E S W 4 | // 0 1 2 3 5 | vector> dirs = { 6 | {0, 1}, 7 | {1, 0}, 8 | {0, -1}, 9 | {-1, 0} 10 | }; 11 | 12 | bool isRobotBounded(string instr) { 13 | int x = 0, y = 0, d = 0; 14 | for(auto ch: instr){ 15 | switch(ch) { 16 | case 'G': 17 | x += dirs[d][0]; 18 | y += dirs[d][1]; 19 | break; 20 | case 'R': 21 | d = (d + 1) % 4; 22 | break; 23 | case 'L': 24 | d = (d - 1 + 4) % 4; 25 | break; 26 | } 27 | } 28 | 29 | return (x == 0 and y == 0) || d; 30 | } 31 | }; -------------------------------------------------------------------------------- /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 | typedef struct TreeNode node; 13 | class Solution { 14 | public: 15 | TreeNode* searchBST(TreeNode* root, int val) { 16 | if(root == NULL) 17 | return root; 18 | if(root->val == val) 19 | return root; 20 | else 21 | return val < root->val ? searchBST(root->left, val) : searchBST(root->right, val); 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /Session 2019/437(dfs).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 | typedef struct TreeNode node; 11 | class Solution { 12 | public: 13 | 14 | int helper(node* root, int sum){ 15 | if(root == NULL) 16 | return 0; 17 | 18 | int cnt = 0; 19 | if(root->val == sum) 20 | cnt++; 21 | return cnt + helper(root->left,sum - root->val) + helper(root->right,sum - root->val); 22 | } 23 | 24 | int pathSum(TreeNode* root, int sum) { 25 | if(root == NULL) 26 | return 0; 27 | 28 | return helper(root,sum) + pathSum(root->left,sum) + pathSum(root->right,sum); 29 | } 30 | }; -------------------------------------------------------------------------------- /5-longest-palindromic-substring/README.md: -------------------------------------------------------------------------------- 1 |

5. Longest Palindromic Substring

Medium


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

2 | 3 |

 

4 |

Example 1:

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

Example 2:

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

 

18 |

Constraints:

19 | 20 |
    21 |
  • 1 <= s.length <= 1000
  • 22 |
  • s consist of only digits and English letters.
  • 23 |
24 |
-------------------------------------------------------------------------------- /Apr'20/880(Decoded String at Index).cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string decodeAtIndex(string S, int K) { 4 | long long int len = 0; 5 | 6 | for(int i = 0; i < (int)S.length(); i++){ 7 | if(isalpha(S[i])) 8 | len++; 9 | else 10 | len = len * (S[i] - '0'); 11 | } 12 | 13 | if(K > len) 14 | return "invalid"; 15 | 16 | K--; 17 | 18 | for(int i = (int)S.size() - 1; i >= 0; i--){ 19 | if(isalpha(S[i])){ 20 | if(K == len - 1) 21 | return (string)"" + S[i]; 22 | len--; 23 | }else{ 24 | len = len / (S[i] - '0'); 25 | K = K % len; 26 | } 27 | } 28 | 29 | return ""; 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /Session 2019/45.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int jump(vector& nums) { 4 | int n = nums.size(); 5 | if(n == 1) 6 | return 0; 7 | // if(nums[0] == 0) 8 | // return false; 9 | 10 | int ladder = 0 + nums[0]; 11 | int stairs = nums[0]; 12 | int jump = 1; // from 0 to 1 13 | 14 | 15 | for(int i = 1; i < n; i++){ 16 | if(i == n - 1) 17 | return jump; 18 | //updt ladder 19 | if(i + nums[i] > ladder) 20 | ladder = i + nums[i]; 21 | 22 | stairs--; 23 | if(stairs == 0){ 24 | jump++; 25 | stairs = ladder - i; 26 | if(stairs <= 0) 27 | return INT_MAX; 28 | } 29 | } 30 | 31 | return jump; 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /Session 2019/543.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 | typedef struct TreeNode node; 11 | class Solution { 12 | public: 13 | 14 | int ht(node* root){ 15 | if(root == NULL) 16 | return -1; 17 | return 1 + max(ht(root->left), ht(root->right)); 18 | } 19 | 20 | int diameterOfBinaryTree(node* root) { 21 | 22 | if(root == NULL) 23 | return 0; 24 | 25 | int x = 2 + ht(root->left) + ht(root->right); 26 | int y = diameterOfBinaryTree(root->left); 27 | int z = diameterOfBinaryTree(root->right); 28 | 29 | return max(x, max(y,z)) ; 30 | 31 | } 32 | }; -------------------------------------------------------------------------------- /Session 2019/98(Validate BST).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 | typedef struct TreeNode node; 11 | class Solution { 12 | public: 13 | bool inorder(node* root) { 14 | stack s; 15 | node* prev = NULL; 16 | node* curr = root; 17 | 18 | while (1) { 19 | while (curr) { 20 | s.push(curr); 21 | curr = curr->left; 22 | } 23 | if (s.empty()) 24 | break; 25 | curr = s.top(); 26 | s.pop(); 27 | if (prev != NULL and prev->val >= curr->val) 28 | return false; 29 | prev = curr; 30 | curr = curr->right; 31 | } 32 | 33 | return true; 34 | 35 | } 36 | bool isValidBST(TreeNode* root) { 37 | return inorder(root); 38 | } 39 | }; 40 | -------------------------------------------------------------------------------- /find-if-path-exists-in-graph/find-if-path-exists-in-graph.cpp: -------------------------------------------------------------------------------- 1 | const int N = 2 * 1e5 + 7; 2 | 3 | class Solution { 4 | public: 5 | bool dfs(int x, int end, vector adj[], vector& visited){ 6 | visited[x] = 1; 7 | cout << x << endl; 8 | if(x == end) 9 | return true; 10 | 11 | for(auto v: adj[x]){ 12 | if(!visited[v]) 13 | if(dfs(v, end, adj, visited)) 14 | return true; 15 | } 16 | 17 | return false; 18 | } 19 | 20 | bool validPath(int n, vector>& edges, int start, int end) { 21 | vector adj[n+1]; 22 | vector visited(n+1, 0); 23 | 24 | for(auto e: edges){ 25 | int a = e[0]; 26 | int b = e[1]; 27 | adj[a].push_back(b); 28 | adj[b].push_back(a); 29 | } 30 | 31 | return dfs(start, end, adj, visited); 32 | } 33 | }; -------------------------------------------------------------------------------- /subarray-product-less-than-k/README.md: -------------------------------------------------------------------------------- 1 |

713. Subarray Product Less Than K

Medium


Your are given an array of positive integers nums.

2 |

Count and print the number of (contiguous) subarrays where the product of all the elements in the subarray is less than k.

3 | 4 |

Example 1:
5 |

Input: nums = [10, 5, 2, 6], k = 100
 6 | Output: 8
 7 | Explanation: The 8 subarrays that have product less than 100 are: [10], [5], [2], [6], [10, 5], [5, 2], [2, 6], [5, 2, 6].
 8 | Note that [10, 5, 2] is not included as the product of 100 is not strictly less than k.
 9 | 
10 |

11 | 12 |

Note: 13 |

  • 0 < nums.length <= 50000.
  • 14 |
  • 0 < nums[i] < 1000.
  • 15 |
  • 0 <= k < 10^6.
  • 16 |

    -------------------------------------------------------------------------------- /Session 2019/414.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int thirdMax(vector& nums) { 4 | 5 | 6 | set s; 7 | for(auto x:nums) 8 | s.insert(x); 9 | 10 | int n = s.size(); 11 | 12 | if(n==1) 13 | return nums[0]; 14 | else if(n==2) 15 | return *max_element(nums.begin(), nums.end()); 16 | 17 | int max1 = *max_element(nums.begin(), nums.end()); 18 | 19 | int max2 = INT_MIN; 20 | for(auto x:nums){ 21 | if(x > max2 and x!=max1) 22 | max2 = x; 23 | } 24 | 25 | int max3 = INT_MIN; 26 | for(auto x:nums){ 27 | if(x>max3 and x!=max2 and x!=max1) 28 | max3 = x; 29 | } 30 | 31 | return max3; 32 | 33 | } 34 | }; -------------------------------------------------------------------------------- /Session 2019/Old/21.MergeTwoSortedLinkedLists.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode(int x) : val(x), next(NULL) {} 7 | * }; 8 | */ 9 | class Solution { 10 | public: 11 | ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { 12 | 13 | ListNode* dummy=NULL; 14 | 15 | if(l1==NULL) 16 | return l2; 17 | else if(l2==NULL) 18 | return l1; 19 | 20 | if(l1->val<=l2->val){ 21 | dummy=l1; 22 | l1->next=mergeTwoLists(l1->next,l2); 23 | 24 | } 25 | 26 | else{ 27 | dummy=l2; 28 | l2->next=mergeTwoLists(l1,l2->next); 29 | } 30 | 31 | return dummy; 32 | } 33 | }; -------------------------------------------------------------------------------- /Session 2019/155.cpp: -------------------------------------------------------------------------------- 1 | class MinStack { 2 | public: 3 | stack s1, s2; 4 | /** initialize your data structure here. */ 5 | MinStack() { 6 | 7 | } 8 | 9 | void push(int x) { 10 | s1.push(x); 11 | if(s2.empty() or x <= s2.top()) 12 | s2.push(x); 13 | } 14 | 15 | void pop() { 16 | int x = s1.top(); 17 | s1.pop(); 18 | if(!s2.empty() and x == s2.top()) 19 | s2.pop(); 20 | } 21 | 22 | int top() { 23 | return s1.top(); 24 | } 25 | 26 | int getMin() { 27 | return s2.top(); 28 | } 29 | }; 30 | 31 | /** 32 | * Your MinStack object will be instantiated and called as such: 33 | * MinStack* obj = new MinStack(); 34 | * obj->push(x); 35 | * obj->pop(); 36 | * int param_3 = obj->top(); 37 | * int param_4 = obj->getMin(); 38 | */ 39 | -------------------------------------------------------------------------------- /self-dividing-numbers/README.md: -------------------------------------------------------------------------------- 1 |

    728. Self Dividing Numbers

    Easy


    2 | A self-dividing number is a number that is divisible by every digit it contains. 3 |

    4 | For example, 128 is a self-dividing number because 128 % 1 == 0, 128 % 2 == 0, and 128 % 8 == 0. 5 |

    6 | Also, a self-dividing number is not allowed to contain the digit zero. 7 |

    8 | Given a lower and upper number bound, output a list of every possible self dividing number, including the bounds if possible. 9 |

    10 |

    Example 1:
    11 |

    Input: 
    12 | left = 1, right = 22
    13 | Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22]
    14 | 
    15 |

    16 | 17 |

    Note: 18 |

  • The boundaries of each input argument are 1 <= left <= right <= 10000.
  • 19 |

    -------------------------------------------------------------------------------- /Session 2019/DP/01Knapsack/01Knapsack.cpp: -------------------------------------------------------------------------------- 1 | /*-------------------------------@greenindia-Sabuj-Jana----------------------------------*/ 2 | 3 | #include 4 | using namespace std; 5 | 6 | int dp[102][102]; 7 | 8 | int knapSack(int wt[], int val[], int n, int W) { 9 | if (n == 0 or W == 0) 10 | return 0; 11 | if (dp[n][W] != -1) 12 | return dp[n][W];// memoise 13 | if (wt[n - 1] <= W) 14 | return dp[n][W] = max(val[n - 1] + knapSack(wt, val, n - 1, W - wt[n - 1]), knapSack(wt, val, n - 1, W)); 15 | else 16 | return dp[n][W] = knapSack(wt, val, n - 1, W); 17 | } 18 | 19 | int main() { 20 | freopen("input.txt", "r", stdin); 21 | int val[] = {60, 100, 120}; 22 | int wt[] = {10, 20, 30}; 23 | int W = 50; 24 | int n = sizeof(val) / sizeof(val[0]); 25 | memset(dp, -1, sizeof(dp)); 26 | cout << knapSack(wt, val, n, W) << endl; 27 | return 0; 28 | } 29 | 30 | 31 | -------------------------------------------------------------------------------- /Session 2019/catalan_dp.cpp: -------------------------------------------------------------------------------- 1 | /*--------------------------"SABUJ-JANA"------"JADAVPUR UNIVERSITY"--------*/ 2 | /*-------------------------------@greenindia-----------------------------------*/ 3 | /*---------------------- Magic. Do not touch.-----------------------------*/ 4 | /*------------------------------God is Great/\---------------------------------*/ 5 | #include 6 | using namespace std; 7 | 8 | #define MOD 1000000007 9 | 10 | int catalan(int n){ 11 | int dp[n + 1]; 12 | dp[0] = 1; 13 | dp[1] = 1; 14 | for(int i = 2; i <= n; i++){ 15 | dp[i] = 0; 16 | for(int j = 0; j < i; j++){ 17 | dp[i] += (dp[j] * dp[i - j - 1]) % MOD; 18 | } 19 | dp[i] = dp[i] % MOD; 20 | } 21 | 22 | return dp[n]; 23 | } 24 | 25 | int main() { 26 | freopen("input.txt", "r", stdin); 27 | int n = 17; 28 | cout << catalan(n) <567. Permutation in String

    Medium


    Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. In other words, one of the first string's permutations is the substring of the second string.

    2 | 3 |

     

    4 | 5 |

    Example 1:

    6 | 7 |
    Input: s1 = "ab" s2 = "eidbaooo"
     8 | Output: True
     9 | Explanation: s2 contains one permutation of s1 ("ba").
    10 | 
    11 | 12 |

    Example 2:

    13 | 14 |
    Input:s1= "ab" s2 = "eidboaoo"
    15 | Output: False
    16 | 
    17 | 18 |

     

    19 |

    Constraints:

    20 | 21 |
      22 |
    • The input strings only contain lower case letters.
    • 23 |
    • The length of both given strings is in range [1, 10,000].
    • 24 |
    25 |
    -------------------------------------------------------------------------------- /797-all-paths-from-source-to-target/797-all-paths-from-source-to-target.cpp: -------------------------------------------------------------------------------- 1 | using vi = vector; 2 | using vvi = vector>; 3 | class Solution { 4 | vvi ans; 5 | vi curr_stack; 6 | public: 7 | void dfs(int x, int dest, vvi& graph) { 8 | curr_stack.push_back(x); 9 | 10 | if(x == dest) { 11 | ans.push_back(curr_stack); 12 | curr_stack.pop_back(); 13 | return; 14 | } 15 | 16 | for(auto v: graph[x]) { 17 | dfs(v, dest, graph); 18 | } 19 | 20 | curr_stack.pop_back(); 21 | } 22 | 23 | vector> allPathsSourceTarget(vector>& graph) { 24 | int src = 0; 25 | int n = graph.size(); 26 | int dest = n - 1; 27 | 28 | ans.clear(); 29 | curr_stack.clear(); 30 | 31 | dfs(src, dest, graph); 32 | return ans; 33 | } 34 | }; -------------------------------------------------------------------------------- /Apr'20/221(Maximal Square).cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maximalSquare(vector>& mat) { 4 | int n = mat.size(); 5 | if(n == 0) 6 | return 0; 7 | int m = mat[0].size(); 8 | 9 | int dp[n][m]; 10 | memset(dp, 0, sizeof(0)); 11 | 12 | int mx = 0; 13 | for(int i = 0; i < n; i++){ 14 | for(int j = 0; j < m; j++){ 15 | if(i == 0 or j == 0) 16 | dp[i][j] = (mat[i][j] - '0'); 17 | else if(mat[i][j] - '0' == 0) 18 | dp[i][j] = 0; 19 | else 20 | dp[i][j] = 1 + min({dp[i-1][j], dp[i][j-1], dp[i-1][j-1]}); 21 | 22 | mx = max(mx, dp[i][j]); 23 | } 24 | } 25 | 26 | return mx * mx; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /1049/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int lastStoneWeightII(vector& stones) { 4 | // + + - - + - + - 5 | int sum = 0; 6 | for (int x : stones) 7 | sum += x; 8 | int m = sum / 2; 9 | int n = stones.size(); 10 | 11 | vector> dp(n + 1, vector(m + 1, 0)); 12 | for (int i = 0; i <= n; i++) { 13 | for (int j = 0; j <= m; j++) { 14 | if (i == 0 and j == 0) 15 | dp[i][j] = 1; 16 | else if (i == 0) 17 | dp[i][j] = 0; 18 | else if (j == 0) 19 | dp[i][j] = 1; 20 | else if (stones[i - 1] <= j) 21 | dp[i][j] = dp[i - 1][j - stones[i - 1]] || dp[i - 1][j]; 22 | else 23 | dp[i][j] = dp[i - 1][j]; 24 | } 25 | } 26 | 27 | int ans = 0; 28 | for (int j = m; j >= 0; j--) { 29 | if (dp[n][j]) { 30 | ans = j; 31 | break; 32 | } 33 | } 34 | 35 | return sum - 2 * ans; 36 | } 37 | }; -------------------------------------------------------------------------------- /983/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def mincostTickets(self, days: List[int], costs: List[int]) -> int: 3 | n = 365 4 | f = [None] * (n + 1) 5 | f[0] = 0 6 | m = len(days) 7 | last_day = days[m-1] 8 | 9 | s = set() 10 | for val in days: 11 | s.add(val) 12 | 13 | for i in range(1, n + 1): 14 | if i not in s: 15 | f[i] = f[i - 1] 16 | continue 17 | 18 | f[i] = f[i - 1] + costs[0] 19 | if i - 7 >= 0: 20 | f[i] = min(f[i], f[i - 7] + costs[1]) 21 | else: 22 | f[i] = min(f[i], costs[1]) 23 | 24 | if i - 30 >= 0: 25 | f[i] = min(f[i], f[i - 30] + costs[2]) 26 | else: 27 | f[i] = min(f[i], costs[2]) 28 | 29 | if i == last: 30 | break 31 | 32 | return f[last] 33 | 34 | 35 | -------------------------------------------------------------------------------- /Session 2019/Misc/Polynomial_mul.cpp: -------------------------------------------------------------------------------- 1 | /*-------------------------------@greenindia-Sabuj-Jana----------------------------------*/ 2 | 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | freopen("input.txt", "r", stdin); 8 | int n,m; 9 | cin>>n>>m; 10 | 11 | int A[n]; 12 | for (int i = 0; i < n; ++i) 13 | { 14 | /* code */ 15 | cin>>A[i]; 16 | } 17 | 18 | int B[m]; 19 | for (int i = 0; i < m; ++i) 20 | { 21 | /* code */ 22 | cin>>B[i]; 23 | } 24 | 25 | int prod[n + m - 1]; 26 | memset(prod,0,sizeof(prod)); 27 | for (int i = 0; i < n; ++i) 28 | { 29 | /* code */ 30 | for(int j=0; j535. Encode and Decode TinyURL

    Medium


    Note: This is a companion problem to the System Design problem: Design TinyURL.
    2 | 3 |

    TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problems/design-tinyurl and it returns a short URL such as http://tinyurl.com/4e9iAk.

    4 | 5 |

    Design the encode and decode methods for the TinyURL service. There is no restriction on how your encode/decode algorithm should work. You just need to ensure that a URL can be encoded to a tiny URL and the tiny URL can be decoded to the original URL.

    6 |
    -------------------------------------------------------------------------------- /monotone-increasing-digits/README.md: -------------------------------------------------------------------------------- 1 |

    738. Monotone Increasing Digits

    Medium


    2 | Given a non-negative integer N, find the largest number that is less than or equal to N with monotone increasing digits. 3 |

    4 | (Recall that an integer has monotone increasing digits if and only if each pair of adjacent digits x and y satisfy x <= y.) 5 |

    6 | 7 |

    Example 1:
    8 |

    Input: N = 10
     9 | Output: 9
    10 | 
    11 |

    12 | 13 |

    Example 2:
    14 |

    Input: N = 1234
    15 | Output: 1234
    16 | 
    17 |

    18 | 19 |

    Example 3:
    20 |

    Input: N = 332
    21 | Output: 299
    22 | 
    23 |

    24 | 25 |

    Note: 26 | N is an integer in the range [0, 10^9]. 27 |

    -------------------------------------------------------------------------------- /Session 2019/Old/_152-MaximumProductSubarray.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProduct(vector& arr) { 4 | 5 | int maxval=1,minval=1,ans=INT_MIN; 6 | int sz=arr.size(); 7 | for(int i=0; i0){ 10 | maxval=maxval*arr[i]; 11 | minval=min(minval*arr[i],1); 12 | } 13 | 14 | else if(arr[i]==0){ 15 | minval=1; 16 | maxval=0; 17 | } 18 | 19 | else { 20 | int temp=maxval; 21 | maxval=minval*arr[i]; 22 | minval=temp*arr[i]; 23 | } 24 | 25 | ans=max(ans,maxval); 26 | if(maxval<=0) 27 | maxval=1; 28 | } 29 | 30 | return ans; 31 | 32 | } 33 | }; -------------------------------------------------------------------------------- /Session 2019/901.cpp: -------------------------------------------------------------------------------- 1 | class StockSpanner { 2 | public: 3 | stack s; 4 | 5 | int idx; 6 | vector vec; 7 | StockSpanner() { 8 | idx = -1; 9 | } 10 | 11 | int next(int price) { 12 | idx++; 13 | vec.push_back(price); 14 | 15 | if(idx == 0){ 16 | s.push(-1); 17 | s.push(idx); 18 | return 1; 19 | 20 | } 21 | 22 | if(vec[s.top()] <= price ) { 23 | while(s.top() != -1 and vec[s.top()] <= price) 24 | s.pop(); 25 | } 26 | 27 | 28 | int ans = idx - s.top(); 29 | s.push(idx); 30 | return ans; 31 | 32 | 33 | } 34 | }; 35 | 36 | /** 37 | * Your StockSpanner object will be instantiated and called as such: 38 | * StockSpanner* obj = new StockSpanner(); 39 | * int param_1 = obj->next(price); 40 | */ -------------------------------------------------------------------------------- /keyboard-row/keyboard-row.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector findWords(vector& words) { 4 | vector rows = {"qwertyuiop", "asdfghjkl", "zxcvbnm"}; 5 | 6 | unordered_map mp; 7 | for(int i = 0; i < rows.size(); i++){ 8 | for(auto x: rows[i]){ 9 | mp[x] = i + 1; 10 | } 11 | } 12 | 13 | vector ans; 14 | for(auto str: words){ 15 | int row_id = mp[tolower(str[0])]; 16 | 17 | bool flag = true; 18 | for(auto x: str){ 19 | if(mp[tolower(x)] != row_id){ 20 | flag = false; 21 | break; 22 | } 23 | } 24 | 25 | if(flag) 26 | ans.push_back(str); 27 | } 28 | 29 | return ans; 30 | } 31 | }; -------------------------------------------------------------------------------- /931/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minFallingPathSum(vector>& A) { 4 | int n = A.size(); 5 | int m = A[0].size(); 6 | 7 | vector> f(n, vector(m, 0)); 8 | 9 | for (int i = 0; i < n; i++) { 10 | for (int j = 0; j < m; j++) { 11 | if (i == 0) 12 | f[i][j] = A[i][j]; 13 | else { 14 | int val = f[i - 1][j]; 15 | for (int k = j - 1; k <= j + 1; k++) 16 | if (k >= 0 and k < m) 17 | val = min(val, f[i - 1][k]); 18 | 19 | f[i][j] = val + A[i][j]; 20 | } 21 | } 22 | } 23 | 24 | int ans = INT_MAX; 25 | for (int j = 0; j < m; j++) 26 | ans = min(ans, f[n - 1][j]); 27 | 28 | return ans; 29 | } 30 | }; -------------------------------------------------------------------------------- /Apr'20/538(Convert BST To Greater 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 | typedef struct TreeNode node; 13 | class Solution { 14 | int globalSum; 15 | public: 16 | Solution(){ 17 | globalSum = 0; 18 | } 19 | 20 | TreeNode* convertBST(TreeNode* root) { 21 | 22 | if(root == NULL) 23 | return root; 24 | 25 | convertBST(root->right); 26 | root->val = globalSum + root->val; 27 | globalSum = root->val; 28 | convertBST(root->left); 29 | 30 | return root; 31 | } 32 | }; 33 | -------------------------------------------------------------------------------- /Session 2019/102.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 | typedef struct TreeNode node; 11 | class Solution { 12 | public: 13 | vector> bfs(node* root) { 14 | if(root == NULL) 15 | return {}; 16 | queue q; 17 | q.push(root); 18 | 19 | vector> ans; 20 | while (!q.empty()) { 21 | int sz = q.size(); 22 | vector t; 23 | while (sz--) { 24 | node* curr = q.front(); 25 | q.pop(); 26 | t.push_back(curr->val); 27 | if (curr->left) 28 | q.push(curr->left); 29 | if (curr->right) 30 | q.push(curr->right); 31 | } 32 | ans.push_back(t); 33 | } 34 | return ans; 35 | } 36 | vector> levelOrder(TreeNode* root) { 37 | return bfs(root); 38 | } 39 | }; 40 | -------------------------------------------------------------------------------- /stone-game/stone-game.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool stoneGame(vector& piles) { 4 | int n = piles.size(); 5 | int dp[n][n][2];// max pts Alice will get [i-j] if BOTH play optimally 6 | // l - 1 7 | for (int i = 0; i < n; ++i){ 8 | dp[i][i][0] = piles[i]; 9 | dp[i][i][1] = 0; 10 | } 11 | 12 | for (int i = 0; i < n - 1; ++i){ 13 | dp[i][i+1][0] = max(piles[i], piles[i+1]); 14 | dp[i][i+1][1] = min(piles[i], piles[i+1]); 15 | } 16 | 17 | for(int l = 3; l <= n; l++) { 18 | for(int i = 0; i + l - 1 < n; i++) { 19 | int j = i + l - 1; 20 | 21 | dp[i][j][0] = max(piles[i] + dp[i+1][j][1], piles[j] + dp[i][j-1][1]); 22 | dp[i][j][1] = min(dp[i+1][j][0], dp[i][j-1][0]); 23 | } 24 | } 25 | 26 | return dp[0][n-1][0] > dp[0][n-1][1]; 27 | } 28 | }; -------------------------------------------------------------------------------- /1049/main.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int lastStoneWeightII(int[] stones) { 3 | // + + - - + - + - 4 | int sum = 0; 5 | for (int x : stones) 6 | sum += x; 7 | int m = sum / 2; 8 | int n = stones.length; 9 | 10 | // vector> dp(n + 1, vector(m + 1, 0)); 11 | int[][] dp = new int[n + 1][m + 1]; 12 | 13 | for (int i = 0; i <= n; i++) { 14 | for (int j = 0; j <= m; j++) { 15 | if (i == 0 && j == 0) 16 | dp[i][j] = 1; 17 | else if (i == 0) 18 | dp[i][j] = 0; 19 | else if (j == 0) 20 | dp[i][j] = 1; 21 | else if (stones[i - 1] <= j) 22 | dp[i][j] = dp[i - 1][j - stones[i - 1]] | dp[i - 1][j]; 23 | else 24 | dp[i][j] = dp[i - 1][j]; 25 | } 26 | } 27 | 28 | int ans = 0; 29 | for (int j = m; j >= 0; j--) { 30 | if (dp[n][j] != 0) { 31 | ans = j; 32 | break; 33 | } 34 | } 35 | 36 | return sum - 2 * ans; 37 | } 38 | } -------------------------------------------------------------------------------- /Session 2019/784.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | vector vec; 3 | string temp; 4 | public: 5 | void helper(string str, int n, int idx){ 6 | cout << temp << endl; 7 | // base 8 | if(idx == n){ 9 | vec.push_back(temp); 10 | return; 11 | } 12 | 13 | if(isalpha(str[idx])){ 14 | temp[idx] = tolower(str[idx]); 15 | helper(str,n, idx + 1 ); 16 | temp[idx] = str[idx]; 17 | 18 | temp[idx] = toupper(str[idx]); 19 | helper(str,n, idx + 1 ); 20 | temp[idx] = str[idx]; 21 | }else{ 22 | helper(str, n, idx + 1); 23 | } 24 | 25 | return; 26 | } 27 | vector letterCasePermutation(string S) { 28 | int n = S.length(); 29 | temp = S; 30 | helper(S,n,0); 31 | return vec; 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /first-missing-positive/README.md: -------------------------------------------------------------------------------- 1 |

    41. First Missing Positive

    Hard


    Given an unsorted integer array nums, return the smallest missing positive integer.

    2 | 3 |

    You must implement an algorithm that runs in O(n) time and uses constant extra space.

    4 | 5 |

     

    6 |

    Example 1:

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

    Example 2:

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

    Example 3:

    13 |
    Input: nums = [7,8,9,11,12]
    14 | Output: 1
    15 | 
    16 |

     

    17 |

    Constraints:

    18 | 19 |
      20 |
    • 1 <= nums.length <= 5 * 105
    • 21 |
    • -231 <= nums[i] <= 231 - 1
    • 22 |
    23 |
    -------------------------------------------------------------------------------- /Session 2019/434.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countSegments(string s) { 4 | 5 | int n =s.length(); 6 | // cout << n < ans; 15 | while( end != string::npos){ 16 | 17 | string str = s.substr(start, end-start); 18 | if(start!=end){ 19 | cout << start <<" " <>& adj, vector& visited, vector& color , int v, int c) { 4 | visited[v] = 1; 5 | color[v] = c; 6 | 7 | for (auto child : adj[v]) { 8 | if (!visited[child]) { 9 | if (!dfs(adj, visited, color, child, c ^ 1)) 10 | return false; 11 | } 12 | else { 13 | if ((color[child] == color[v])) 14 | return false; 15 | } 16 | } 17 | return true; 18 | } 19 | bool isBipartite(vector>& adj) { 20 | int nodes = adj.size(); 21 | vector visited(nodes, 0);// false 22 | vector color(nodes, -1);// no color 23 | bool is_bipartite = true; 24 | 25 | for (int i = 0; i < nodes; i++) { 26 | if (!visited[i]) 27 | is_bipartite = is_bipartite & dfs(adj, visited, color, i, 1); 28 | 29 | } 30 | for (int i = 0; i < nodes; i++) 31 | cout << color[i] << endl; 32 | 33 | return is_bipartite; 34 | } 35 | }; 36 | -------------------------------------------------------------------------------- /length-of-longest-fibonacci-subsequence/length-of-longest-fibonacci-subsequence.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int lenLongestFibSubseq(vector& arr) { 4 | // dp[j][k] = dp[i][j] + 1 if A[i] + A[j] == A[k] 5 | unordered_map mp; 6 | int n = arr.size(); 7 | for(int i = 0;i < n; i++) 8 | mp[arr[i]] = i; 9 | 10 | int dp[n][n]; 11 | memset(dp, -1, sizeof(dp)); 12 | 13 | int mx = 0; 14 | for(int k = 0; k < n; k++) { 15 | for(int j = 0; j < k; j++) { 16 | int ai = arr[k] - arr[j]; 17 | if(mp.find(ai) == mp.end()) continue; 18 | int i = mp[ai]; 19 | 20 | if(i >= 0 and i < j) 21 | dp[j][k] = (dp[i][j] == -1 ? 2 : dp[i][j]) + 1; 22 | mx = max(mx, dp[j][k]); 23 | } 24 | } 25 | 26 | return (mx >= 3) ? mx : 0; 27 | } 28 | }; -------------------------------------------------------------------------------- /longest-palindromic-subsequence/longest-palindromic-subsequence.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int longestPalindromeSubseq(string str) { 4 | int n = str.size(); 5 | 6 | int dp[n][n]; 7 | // dp[i][j] - str[i..j] 8 | 9 | // len1 10 | for(int i = 0; i < n; i++) 11 | dp[i][i] = 1; 12 | 13 | // len2 14 | for(int i = 0; i < n - 1; i++) 15 | dp[i][i+1] = (str[i] == str[i+1] ? 2 : 1); 16 | 17 | // len3 - n 18 | for(int l = 3; l <= n; l++){ 19 | for(int i = 0; i <= n - l; i++){ 20 | int j = i + l - 1; 21 | 22 | if(str[i] == str[j]) 23 | dp[i][j] = 2 + dp[i+1][j-1]; 24 | else 25 | dp[i][j] = max(dp[i+1][j], dp[i][j-1]); 26 | } 27 | } 28 | 29 | return dp[0][n-1]; 30 | } 31 | }; -------------------------------------------------------------------------------- /split-a-string-into-the-max-number-of-unique-substrings/split-a-string-into-the-max-number-of-unique-substrings.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int ans; 4 | void solve(string& str, int idx, set& st, int subcnt){ 5 | // base 6 | int n = str.size(); 7 | if(idx == n){ 8 | ans = max(ans,subcnt); 9 | } 10 | 11 | // main 12 | string sub = ""; 13 | for(int i = idx; i < n; i++){ 14 | sub += str[i]; 15 | if(st.find(sub) == st.end()){ 16 | st.insert(sub); 17 | cout << sub << endl; 18 | solve(str, i + 1, st, subcnt + 1); 19 | st.erase(st.find(sub)); 20 | } 21 | } 22 | } 23 | 24 | int maxUniqueSplit(string str) { 25 | set st; 26 | ans = 0; 27 | solve(str, 0, st, 0); 28 | 29 | return ans; 30 | } 31 | }; --------------------------------------------------------------------------------