├── .DS_Store ├── 112. Path Sum.cpp ├── 1170. Compare Strings by Frequency of the Smallest Character.cpp ├── 121. Best Time to Buy and Sell Stock.js ├── 121. Best Time to Buy and Sell Stock.py ├── 1356. Sort Integers by The Number of 1 Bits.cpp ├── 1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree.cpp ├── 1512. Number of Good Pairs.cpp ├── 1561. Maximum Number of Coins You Can Get.cpp ├── 1793. Maximum Score of a Good Subarray.cpp ├── 189. Rotate Array.cpp ├── 242. Valid Anagram.cpp ├── 278. First Bad Version.js ├── 33. Search in Rotated Sorted Array.cpp ├── 349. Intersection of Two Arrays solution.cpp ├── 349. Intersection of Two Arrays.cpp ├── 35. Search Insert Position.js ├── 350. Intersection of Two Arrays II.cpp ├── 350. Intersection of Two Arrays II.js ├── 350. Intersection of Two Arrays II.py ├── 38. Count and Say.cpp ├── 390. Elimination Game.cpp ├── 506. Relative Ranks.cpp ├── 54. Spiral Matrix.cpp ├── 55. Jump Game.cpp ├── 705. Design HashSet solution.cpp ├── 88. Merge Sorted Array.js ├── 905. Sort Array By Parity.cpp ├── 922. Sort Array By Parity II.cpp ├── 976. Largest Perimeter Triangle.java ├── 977. Squares of a Sorted Array.js ├── Adora └── adora.md ├── Amazon ├── 102. Binary Tree Level Order Traversal.cpp ├── 12. Integer to Roman.cpp ├── 13. Roman to Integer.cpp ├── Diameter of Binary Tree.cpp ├── Easy │ ├── 1470. Shuffle the Array.cpp │ ├── 1480. Running Sum of 1d Array.cpp │ ├── 1710. Maximum Units on a Truck.cpp │ └── 1920. Build Array from Permutation.cpp ├── Longest Palindrome Substring.cpp ├── Median of Two Sorted Arrays.cpp ├── Medium │ ├── 1302. Deepest Leaves Sum.cpp │ ├── 1315. Sum of Nodes with Even-Valued Grandparent.cpp │ └── 654. Maximum Binary Tree.cpp ├── Valid parenthese.cpp ├── length of longest palindromic substring.cpp ├── merge k lists.cpp └── rotten oranges.cpp ├── April Leetcode Challenge ├── 1046. Last Stone Weight.cpp ├── 1046. Last Stone Weight.java ├── 11. Container With Most Water.cpp ├── 11. Container With Most Water.java ├── 1202. Smallest String With Swaps.cpp ├── 1260. Shift 2D Grid.cpp ├── 1396. Design Underground System.cpp ├── 1584. Min Cost to Connect All Points.cpp ├── 1631. Path With Minimum Effort.cpp ├── 1721. Swapping Nodes in a Linked List.cpp ├── 173. Binary Search Tree Iterator.cpp ├── 230. Kth Smallest Element in a BST.cpp ├── 284. Peeking Iterator.cpp ├── 289. Game of Life.cpp ├── 31. Next Permutation.cpp ├── 31. Next Permutation.java ├── 347. Top K Frequent Elements.cpp ├── 399. Evaluate Division.cpp ├── 535. Encode and Decode TinyURL.cpp ├── 538. Convert BST to Greater Tree.cpp ├── 59. Spiral Matrix II.cpp ├── 669. Trim a Binary Search Tree.cpp ├── 680. Valid Palindrome II.cpp ├── 682. Baseball Game.cpp ├── 703. Kth Largest Element in a Stream.cpp ├── 703. Kth Largest Element in a Stream.java ├── 705. Design HashSet solution.cpp ├── 705. Design HashSet.cpp ├── 706. Design HashMap.cpp ├── 785. Is Graph Bipartite?.CPP ├── 897. Increasing Order Search Tree.cpp ├── 923. 3Sum With Multiplicity.cpp ├── 99. Recover Binary Search Tree.cpp ├── BST Iterator.cpp ├── Reverse a String.cpp ├── Search in a Binary Search Tree.cpp └── Swapping nodes.java ├── Array ├── 1266. Minimum Time Visiting All Points.cpp ├── 1313. Decompress Run-Length Encoded List.cpp ├── 1365. How Many Numbers Are Smaller Than the Current Number.cpp ├── 1389. Create Target Array in the Given Order.cpp ├── 1450. Number of Students Doing Homework at a Given Time.cpp ├── 1464. Maximum Product of Two Elements in an Array.cpp ├── 1476. Subrectangle Queries.cpp ├── 1480. Running Sum of 1d Array.cpp ├── 1486. XOR Operation in an Array.cpp ├── 1572. Matrix Diagonal Sum.cpp ├── 1588. Sum of All Odd Length Subarrays.cpp ├── 1656. Design an Ordered Stream.cpp ├── 1732. Find the Highest Altitude .cpp ├── 1769. Minimum Number of Operations to Move All Balls to Each Box.cpp ├── 1773. Count Items Matching a Rule.cpp ├── 832. Flipping an Image.cpp ├── Number of Subarrays with Bounded Maximum Solution.cpp └── Pascal's Triangle Solution.cpp ├── August_Leetcode_Challenge_2022 ├── 1338. Reduce Array Size to The Half.cpp ├── 200. Number of Islands.cpp ├── 342. Power of Four │ ├── bit_manipulation.cpp │ ├── iteration.cpp │ └── recursion.cpp ├── 383. Ransom Note.cpp ├── 417. Pacific Atlantic Water Flow.cpp ├── 871. Minimum Number of Refueling Stops.cpp ├── DAY3.cpp ├── Day10.cpp ├── Day11.cpp ├── Day12.cpp ├── Day13.cpp ├── Day14.cpp ├── Day17.cpp ├── Day4.cpp ├── Day5.cpp ├── Day6.cpp ├── Day8.cpp ├── Day9.cpp └── stamping-the-sequence.cpp ├── BFS ├── 1261. Find Elements in a Contaminated Binary Tree.cpp ├── 127. Word Ladder.cpp ├── 226. Invert Binary Tree.cpp ├── 637. Average of Levels in Binary Tree.cpp ├── 841. Keys and Rooms.cpp └── 965. Univalued Binary Tree.cpp ├── Backtracking └── Generate Parentheses Solution.cpp ├── Binary Search Solution.cpp ├── Binary Search.js ├── Binary Search ├── 1385. Find the Distance Value Between Two Arrays.cpp └── 540. Single Element in a Sorted Array.cpp ├── DFS ├── 1123. Lowest Common Ancestor of Deepest Leaves.cpp ├── 1305. All Elements in Two Binary Search Trees.cpp ├── 1325. Delete Leaves With a Given Value.cpp ├── 1377. Frog Position After T Seconds.cpp ├── 1382. Balance a Binary Search Tree.cpp ├── 2265. Count Nodes Equal to Average of Subtree.cpp ├── 2331. Evaluate Boolean Binary Tree.cpp ├── 419. Battleships in a Board.cpp ├── 463. Island Perimeter.cpp └── 797. All Paths From Source to Target.cpp ├── DYNAMIC PROGRAMMING ├── 0-1 Knapsack Problems │ ├── Partition Equal Subset Sum with memoization.cpp │ └── Partition Equal Subset Sum with recursion.cpp ├── 1143. Longest Common Subsequence.cpp ├── 300. Longest Increasing Subsequence.cpp ├── 62. Unique Paths.cpp ├── 63. Unique Paths II.cpp ├── 64. Minimum Path Sum.cpp ├── House_Robber_II.cpp ├── House_robber_1.cpp └── frog jump.cpp ├── Day1 53. Maximum Subarray.js ├── Day1 Contains Duplicates.js ├── Day15.cpp ├── Day2 Two Sum.js ├── Day29.cpp ├── Expedia ├── 1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts.cpp ├── 1492. The kth Factor of n.cpp ├── 3. Longest Substring Without Repeating Characters.cpp └── 70. Climbing Stairs.cpp ├── Find the Difference.cpp ├── Find-positive-integer-solution-for-a-given-equation.cpp ├── Good Pair.scala ├── Google ├── 31. Next Permutation.cpp ├── 42. Trapping Rain Water.cpp ├── 50. Pow(x, n) ├── 54. Spiral Matrix.cpp ├── 855. Exam Room.cpp ├── Find all triplets with zero sum.cpp └── GFG │ ├── Find triplets with zero sum.cpp │ └── Generate binary string.cpp ├── Graph ├── 1038. Binary Search Tree to Greater Sum Tree.cpp ├── 110. Balanced Binary Tree.cpp ├── 1161. Maximum Level Sum of a Binary Tree.cpp ├── 1197. Minimum Knight Moves.cpp ├── 127. Word Ladder ├── 1302. Deepest Leaves Sum.cpp ├── 1306. Jump Game III.cpp ├── 1315. Sum of Nodes with Even-Valued Grandparent.cpp ├── 1448. Count Good Nodes in Binary Tree.cpp ├── 1625. Lexicographically Smallest String After Applying Operations.cpp ├── 199. Binary Tree Right Side View.cpp ├── 200. Number of Islands BFS Solution.cpp ├── 200. Number of Islands.cpp ├── 207. Course Schedule.cpp ├── 210. Course Schedule II.cpp ├── 257. Binary Tree Paths.cpp ├── 429. N-ary Tree Level Order Traversal.cpp ├── 513. Find Bottom Left Tree Value.cpp ├── 515. Find Largest Value in Each Tree Row.cpp ├── 563. Binary Tree Tilt.cpp ├── 733. Flood Fill.cpp ├── 785. Is Graph Bipartite?.cpp ├── 865. Smallest Subtree with all the Deepest Nodes.cpp ├── 872. Leaf-Similar Trees.cpp └── 897. Increasing Order Search Tree.cpp ├── Greedy Algorithm ├── 1725. Number Of Rectangles That Can Form The Largest Square.cpp ├── 2160. Minimum Sum of Four Digit Number After Splitting Digits.cpp └── Maximum Units on a Truck Solution.cpp ├── House Robber III.cpp ├── July Leetcode Challenge 2022 ├── 315. Count of Smaller Numbers After Self.cpp ├── Day1.cpp ├── Day11.cpp ├── Day11BFS.cpp ├── Day12.cpp ├── Day13.cpp ├── Day14.cpp ├── Day15.cpp ├── Day16.cpp ├── Day16.py ├── Day17.cpp ├── Day18.cpp ├── Day19.cpp ├── Day2.cpp ├── Day20.cpp ├── Day21.cpp ├── Day22.cpp ├── Day24.cpp ├── Day24BinarySearch.cpp ├── Day25.cpp ├── Day26.cpp ├── Day3.cpp ├── Day31.cpp ├── Day4.cpp ├── Day4candy2.cpp ├── Day5.cpp ├── Day6dp.cpp ├── Day6rec.cpp └── Day6var.cpp ├── June Leetcode Challenge ├── DAY2.CPP ├── Day1.cpp ├── Day10.cpp ├── Day16.cpp ├── Day17.cpp ├── Day21.cpp ├── Day22.cpp ├── Day23.cpp ├── Day24.cpp ├── Day25.cpp ├── Day26.cpp ├── Day28.cpp ├── Day3.cpp ├── Day4.cpp ├── Day5.cpp ├── Day6.cpp ├── Day7.cpp ├── Day8.cpp ├── Day9.cpp ├── day15.cpp ├── day27.cpp ├── day29.cpp └── day30.cpp ├── March LeetCode Challenge ├── 1029. Two City Scheduling.CPP ├── 1249. Minimum Remove to Make Valid Parentheses.cpp ├── 1359. Count All Valid Pickup and Delivery Options.cpp ├── 138. Copy List with Random Pointer.cpp ├── 1663. Smallest String With A Given Numeric Value.cpp ├── 20. Valid Parentheses.cpp ├── 316. Remove Duplicate Letters.cpp ├── 392. Is Subsequence.cpp ├── 410. Split Array Largest Sum.cpp ├── 413. Arithmetic Slices.cpp ├── 704. Binary Search.cpp ├── 71. Simplify Path.cpp ├── 74. Search a 2D Matrix.cpp ├── 740. Delete and Earn.cpp ├── 763. Partition Labels.cpp ├── 799. Champagne Tower.cpp ├── 81. Search in Rotated Sorted Array II.cpp ├── 82. Remove Duplicates from Sorted List II.cpp ├── 856. Score of Parentheses.cpp ├── 881. Boats to Save People.cpp ├── 895. Maximum Frequency Stack.cpp ├── 946. Validate Stack Sequences.cpp ├── 991. Broken Calculator.cpp ├── Add two numbers represented by linked lists.cpp ├── Counting Bits.cpp ├── Day30.java ├── Find the Duplicate Number.cpp ├── Rotate a Linked List .cpp ├── Simplify Path.cpp └── kWeakestRows.cpp ├── May Leetcode Challenge ├── 117. Populating Next Right Pointers in Each Node II.cpp ├── 1209. Remove All Adjacent Duplicates in String II Method 2.cpp ├── 1209. Remove All Adjacent Duplicates in String II Method1.cpp ├── 1641. Count Sorted Vowel Strings.cpp ├── 1679. Max Number of K-Sum Pairs.cpp ├── 17. Letter Combinations of a Phone Number.CPP ├── 225. Implement Stack using Queues.cpp ├── 456. 132 Pattern.cpp ├── 46. Permutations.cpp ├── 47. Permutations II.cpp ├── 581. Shortest Unsorted Continuous Subarray.cpp ├── 743. Network Delay Time.cpp ├── 844. Backspace String Compare.cpp ├── Day17.cpp ├── Day18.cpp ├── Day19.cpp ├── Day20.cpp ├── Day21.cpp ├── Day22.cpp ├── Day23.cpp ├── Day25.cpp ├── Day26.cpp ├── Day27.cpp ├── Day28.cpp ├── Day30.cpp ├── Day31.cpp ├── Implement Stack using Queues Method1.cpp ├── Implement Stack using QueuesMethod2.cpp ├── Max Number of K-Sum Pairs.cpp ├── Shortest Path in Binary Matrix.cpp ├── combinationSum3.cpp ├── day24.cpp ├── deepestLeavesSum.cpp ├── sortArrayByParity.cpp ├── stack day24.cpp ├── subsets.cpp └── uniquePaths.cpp ├── Median of Two Sorted Array.cpp ├── Merge-Sorted-Array.cpp ├── Microsoft ├── Easy │ └── 1822. Sign of the Product of an Array.cpp └── Medium │ └── 1448. Count Good Nodes in Binary Tree.cpp ├── My Calendar I Solution.cpp ├── N-ary Tree Level Order Traversal Solution.cpp ├── November_Leetcode_Challenge_2022 ├── Day1 │ ├── 1706._Where_Will _the_Ball_Fall.cpp │ └── 1706._Where_Will _the_Ball_Fall.java ├── Day2 │ └── 433._Minimum_Genetic_Mutation.cpp └── Day4 │ └── reverse_vowels_of_a_string.cpp ├── October leetcode challenge ├── Day1 │ └── 91. Decode Ways.cpp ├── Day10 │ └── 1328. Break a Palindrome.cpp ├── Day11 │ └── 334. Increasing Triplet Subsequence.cpp ├── Day12 │ └── 976. Largest Perimeter Triangle.cpp ├── Day13 │ └── 237. Delete Node in a Linked List.cpp ├── Day15 │ ├── 1531. String Compression II with Recursion.cpp │ └── memoization.cpp ├── Day16 │ └── 1335. Minimum Difficulty of a Job Schedule │ │ ├── memoization.cpp │ │ └── recursion.cpp ├── Day17 │ └── bit_manipulation.cpp ├── Day18 │ └── countAndSay.cpp ├── Day2 │ └── 1155. Number of Dice Rolls With Target Sum │ │ └── memoization.cpp ├── Day20 │ └── Integer_to_Roman.cpp ├── Day21 │ └── 219_Contains_Duplicate_II.cpp ├── Day26 │ └── Continuous Subarray Sum.cpp ├── Day28 │ └── 49_Group_Anagrams.cpp ├── Day29 │ ├── 2136-Earliest-Possible-Day-of-Full-Bloom.java │ └── Earliest Possible Day of Full Bloom.cpp ├── Day3 │ └── 1578. Minimum Time to Make Rope Colorful.cpp ├── Day4 │ └── 112. Path Sum.cpp ├── Day5 │ └── 623. Add One Row to Tree.cpp ├── Day6 │ └── 981. Time Based Key-Value Store.cpp ├── Day7 │ ├── 732. My Calendar III.cpp │ └── 732. My Calendar III.java ├── Day9 │ └── 653. Two Sum IV - Input is a BST.cpp └── day14 │ ├── 2095. Delete the Middle Node of a Linked List.cpp │ └── deleteMiddleNode.cpp ├── Palindrome Partitioning II Solution.cpp ├── Pascal_Triangle.cpp ├── Phonepe └── check-array-formation-through-concatenation.cpp ├── Plus-One.cpp ├── Prefix Sum ├── 1413. Minimum Value to Get Positive Step by Step Sum.cpp ├── 1480. Running Sum of 1d Array.js ├── 1588. Sum of All Odd Length Subarrays.cpp ├── 1588. Sum of All Odd Length Subarrays.js ├── 1991. Find the Middle Index in Array.cpp ├── 1991. Find the Middle Index in Array.js └── 560. Subarray Sum Equals K.cpp ├── Recursion ├── 273. Integer to English Words.cpp ├── 326. Power of Three.cpp ├── 342. Power of Four.cpp ├── Combination Sum │ └── 39. Combination Sum.cpp ├── Delete middle element of a stack.cpp ├── Height of Binary Tree.js ├── Reverse a Stack.cpp ├── Sort (Asc) An Stack Recursion.cpp ├── Sort (Desc) An Stack Recursion.cpp ├── Sort An Array Recursion.cpp ├── Subset.cpp └── find all subsets of string.cpp ├── Segment Tree ├── 307.cpp └── flip coins.cpp ├── September Leetcode Challenge ├── Day1 │ └── 1448. Count Good Nodes in Binary Tree.cpp ├── Day10 │ └── 188. Best Time to Buy and Sell Stock IV │ │ ├── dp.cpp │ │ ├── memoization.cpp │ │ └── recursion.cpp ├── Day11 │ └── 1383. Maximum Performance of a Team.cpp ├── Day12 │ └── 948. Bag of Tokens.cpp ├── Day13 │ └── 393. UTF-8 Validation.cpp ├── Day14 │ └── 1457. Pseudo-Palindromic Paths in a Binary Tree.cpp ├── Day15 │ └── 2007. Find Original Array From Doubled Array.cpp ├── Day16 │ └── 1770. Maximum Score from Performing Multiplication Operations │ │ ├── memoization.cpp │ │ └── recursion.cpp ├── Day18 │ └── 42. Trapping Rain Water.cpp ├── Day19 │ └── 609. Find Duplicate File in System.cpp ├── Day2 │ └── 637. Average of Levels in Binary Tree.cpp ├── Day20 │ └── 718. Maximum Length of Repeated Subarray.cpp ├── Day21 │ └── 985. Sum of Even Numbers After Queries.cpp ├── Day22 │ └── 557. Reverse Words in a String III.cpp ├── Day23 │ └── 1680. Concatenation of Consecutive Binary Numbers.cpp ├── Day25 │ └── 622. Design Circular Queue.cpp ├── Day26 │ └── 990. Satisfiability of Equality Equations.cpp ├── Day27 │ └── 838. Push Dominoes.cpp ├── Day29 │ └── 658. Find K Closest Elements.cpp ├── Day3 │ ├── 967. Numbers With Same Consecutive Differences.cpp │ └── ans.cpp ├── Day30 │ └── 218. The Skyline Problem.cpp ├── Day4 │ └── 987. Vertical Order Traversal of a Binary Tree.cpp ├── Day5 │ ├── 429. N-ary Tree Level Order Traversal.cpp │ └── 429. N-ary Tree Level Order Traversal.java ├── Day6 │ ├── 814. Binary Tree Pruning.cpp │ └── 814. Binary Tree Pruning.java ├── Day7 │ └── 606. Construct String from Binary Tree.cpp ├── Day8 │ └── 94. Binary Tree Inorder Traversal.cpp ├── Day9 │ └── 1996. The Number of Weak Characters in the Game.cpp └── day24 │ └── 113. Path Sum II.cpp ├── Stamping The Sequence.cpp ├── String ├── 1614. Maximum Nesting Depth of the Parentheses.cpp ├── 1662. Check If Two String Arrays are Equivalent.cpp ├── 1678. Goal Parser Interpretation.cpp ├── 1768. Merge Strings Alternately.cpp ├── 1812. Determine Color of a Chessboard Square.cpp ├── 1832. Check if the Sentence Is Pangram.cpp └── Medium │ └── 1689. Partitioning Into Minimum Number Of Deci-Binary Numbers.cpp ├── Striver_SDE_Sheet ├── 31_Next_Permutation.cpp ├── 53_Maximum_Subarray.cpp ├── Pascals_Triangle.cpp └── Set_Matrix_Zeroes.cpp ├── Trapping Rain Water Solution.cpp ├── Trie ├── 208. Implement Trie (Prefix Tree).cpp └── implementation.cpp ├── Two sum.cpp ├── Two-Sum.cpp ├── best-time-to-buy-and-sell.cpp ├── count negative numbers in sorted matrix.cpp ├── dfspathsum.cpp ├── find-positive-integer-solution-for-a-given-equation.cpp ├── iscousin.cpp ├── levelorder_zigZag.cpp ├── maximum-subarray.cpp ├── merge-sorted-array.cpp ├── pascals-triangle-ii.cpp ├── pascals-triangle.cpp ├── peak index in a mountain array.cpp ├── remove-duplicates-from-sorted-array.cpp ├── remove-element.cpp ├── search-insert-position.cpp ├── subsets II.cpp └── valid anagram.cpp /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/niveditaprity/LeetCode/af8869e9cc019cf81b478285c1c5118a41f71c8a/.DS_Store -------------------------------------------------------------------------------- /112. Path Sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool has(TreeNode *root,int sum) 4 | { 5 | if(!root)return false; 6 | sum=sum-root->val; 7 | if(!root->left&&!root->right&&sum==0)return true; 8 | return has(root->left,sum)|| 9 | has(root->right,sum); 10 | } 11 | 12 | bool hasPathSum(TreeNode* root, int sum) { 13 | if(root==NULL)return false; 14 | return has(root,sum); 15 | 16 | 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /1170. Compare Strings by Frequency of the Smallest Character.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int f(string s) 4 | { 5 | char c = *min_element(s.begin(),s.end()); 6 | return count(s.begin(),s.end(),c); 7 | } 8 | vector numSmallerByFrequency(vector& queries, vector& words) { 9 | 10 | vectorfre,res; 11 | for(auto word:words) 12 | { 13 | fre.push_back(f(word)); 14 | } 15 | sort(fre.begin(),fre.end()); 16 | for(auto q:queries) 17 | { 18 | int i=upper_bound(fre.begin(),fre.end(),f(q))-fre.begin(); 19 | res.push_back(words.size()-i); 20 | } 21 | return res; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /121. Best Time to Buy and Sell Stock.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} prices 3 | * @return {number} 4 | */ 5 | var maxProfit = function(prices) { 6 | var maxprice =0,minprice=prices[0]; 7 | for(let i=0;i int: 3 | minprice = prices[0] 4 | maxprice = 0 5 | 6 | for x in prices: 7 | minprice = min(minprice,x) 8 | maxprice = max(maxprice,x-minprice) 9 | 10 | return maxprice 11 | -------------------------------------------------------------------------------- /1356. Sort Integers by The Number of 1 Bits.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | static bool compare(int a, int b){ 4 | int c=__builtin_popcount(a); 5 | int d=__builtin_popcount(b); 6 | if(c==d) 7 | return a sortByBits(vector& arr) { 12 | sort(arr.begin(),arr.end(),compare); 13 | return arr; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 | * }; 9 | */ 10 | 11 | class Solution { 12 | public: 13 | TreeNode*targ,*res; 14 | TreeNode* getTargetCopy(TreeNode* original, TreeNode* cloned, TreeNode* target) { 15 | targ=target; 16 | return inorder(original,cloned); 17 | } 18 | TreeNode*inorder(TreeNode* original, TreeNode* cloned) 19 | { 20 | if(original!=NULL) 21 | { 22 | inorder(original->left,cloned->left); 23 | if(original==targ) 24 | { 25 | res=cloned; 26 | } 27 | inorder(original->right,cloned->right); 28 | 29 | } 30 | return res; 31 | } 32 | }; 33 | -------------------------------------------------------------------------------- /1512. Number of Good Pairs.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numIdenticalPairs(vector& nums) { 4 | unordered_mapmp; 5 | for(int i=0;i& nums, int k) { 4 | int maxi=0; 5 | int i=k,j=k,minim=nums[k],n=nums.size(); 6 | if (n==1) return nums[0]; 7 | while(i>0 || j&nums,int l,int h) 4 | { 5 | while(l& nums, int k) { 15 | 16 | if(nums.size() < 2) return; 17 | int diff = k%nums.size(); 18 | if(diff == 0) return; 19 | 20 | reverse(nums, 0, nums.size()-1); 21 | reverse(nums, 0, diff-1); 22 | reverse(nums, diff, nums.size()-1); 23 | 24 | 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /242. Valid Anagram.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isAnagram(string s, string t) { 4 | int n1=s.size(); 5 | int n2=t.size(); 6 | if(n1!=n2) 7 | { 8 | return false; 9 | } 10 | sort(s.begin(),s.end()); 11 | sort(t.begin(),t.end()); 12 | for(int i=0;i& nums, int target) { 4 | int left=0,right=nums.size()-1; 5 | while(left<=right) 6 | { 7 | if(nums[left]==target) 8 | { 9 | return left; 10 | } 11 | else if(nums[right]==target) 12 | { 13 | return right; 14 | } 15 | else if(target>nums[left]) 16 | { 17 | left++; 18 | } 19 | else if(target intersection(vector& nums1, vector& nums2) { 4 | unordered_set s1(nums1.begin(),nums1.end()), s2(nums2.begin(),nums2.end()), ans; 5 | for(auto x:s2) if(s1.find(x)!=s1.end()) ans.insert(x); 6 | return vector(ans.begin(),ans.end()); 7 | 8 | } 9 | }; 10 | -------------------------------------------------------------------------------- /349. Intersection of Two Arrays.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector intersection(vector& nums1, vector& nums2) { 4 | mapmp; 5 | vectorv; 6 | for(int i=0;i target) end = mid - 1; 16 | 17 | if (nums[mid] < target) start = mid + 1; 18 | } 19 | 20 | return start; 21 | }; 22 | -------------------------------------------------------------------------------- /350. 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 | vectorv(min(nums1.size(),nums2.size())); 7 | auto it=set_intersection(nums1.begin(),nums1.end(),nums2.begin(),nums2.end(),v.begin()); 8 | v.resize(it-v.begin()); 9 | return v; 10 | 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /350. Intersection of Two Arrays II.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums1 3 | * @param {number[]} nums2 4 | * @return {number[]} 5 | */ 6 | var intersect = function(nums1, nums2) { 7 | nums1.sort((a,b)=>a-b); 8 | nums2.sort((a,b)=>a-b); 9 | let i=0,j=0; 10 | var arr = new Array(); 11 | while(inums2[j]) 19 | { 20 | j++; 21 | } 22 | else if(nums1[i]==nums2[j]) 23 | { 24 | arr.push(nums1[i]); 25 | i++; 26 | j++; 27 | } 28 | } 29 | return arr; 30 | }; 31 | -------------------------------------------------------------------------------- /350. Intersection of Two Arrays II.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]: 3 | nums1.sort() 4 | nums2.sort() 5 | 6 | i=0 7 | j=0 8 | arr = [] 9 | 10 | while inums2[j]: 14 | j=j+1; 15 | elif nums1[i]==nums2[j]: 16 | arr.append(nums1[i]) 17 | i=i+1 18 | j=j+1 19 | return arr 20 | 21 | -------------------------------------------------------------------------------- /390. Elimination Game.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int lastRemaining(int n) { 4 | int a=1,d=0,r=1; 5 | while(n>1) 6 | { 7 | if(n&1 || d==0) 8 | { 9 | r+=a; 10 | } 11 | n /= 2; 12 | a*= 2; 13 | d = !d; 14 | } 15 | return r; 16 | 17 | 18 | 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /55. Jump Game.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canJump(vector& nums) { 4 | int n=nums.size()-1; 5 | int lastp=n; 6 | for(int i=n;i>=0;i--) 7 | { 8 | if(i+nums[i]>=lastp) 9 | { 10 | lastp=i; 11 | } 12 | } 13 | return lastp==0; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /88. Merge Sorted Array.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums1 3 | * @param {number} m 4 | * @param {number[]} nums2 5 | * @param {number} n 6 | * @return {void} Do not return anything, modify nums1 in-place instead. 7 | */ 8 | var merge = function(nums1, m, nums2, n) { 9 | while(n>0||m>0) 10 | { 11 | if(m>0 && (n==0 || (nums1[m-1]>nums2[n-1]))) 12 | { 13 | nums1[m+n-1]=nums1[m-1]; 14 | m--; 15 | } 16 | else 17 | { 18 | nums1[m+n-1]=nums2[n-1]; 19 | n--; 20 | } 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /905. Sort Array By Parity.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector sortArrayByParity(vector& A) { 4 | int i=0,j=A.size()-1; 5 | while(i sortArrayByParityII(vector& A) { 4 | vectora; 5 | vectorb; 6 | for(int i=0;i=0){ 8 | 9 | if(nums[j]n*n).sort((a,b)=>a-b); 7 | }; 8 | -------------------------------------------------------------------------------- /Adora/adora.md: -------------------------------------------------------------------------------- 1 | #I am Adora 2 | -------------------------------------------------------------------------------- /Amazon/13. Roman to Integer.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int romanToInt(string s) { 4 | mapmp; 5 | mp.insert({'I',1}); 6 | mp.insert({'V',5}); 7 | mp.insert({'X',10}); 8 | mp.insert({'L',50}); 9 | mp.insert({'C',100}); 10 | mp.insert({'D',500}); 11 | mp.insert({'M',1000}); 12 | int ans =0; 13 | for(int i=0;i shuffle(vector& nums, int n) { 4 | 5 | vectorv; 6 | for(int i=0;i runningSum(vector& nums) { 4 | vectorv1(nums.size()); 5 | v1[0]=nums[0]; 6 | for(int i=1;i& v1,const vector& v2 ) 4 | { 5 | return v1[1] > v2[1]; 6 | } 7 | int maximumUnits(vector>& boxTypes, int truckSize) { 8 | sort(boxTypes.begin(),boxTypes.end(),sortcol); 9 | 10 | int boxno=0; 11 | int total=0; 12 | 13 | for(int i=0;itruckSize) 17 | { 18 | boxno=boxno-boxTypes[i][0]; 19 | total+=(truckSize-boxno)*boxTypes[i][1]; 20 | break; 21 | } 22 | else{ 23 | total+=(boxTypes[i][0])*boxTypes[i][1]; 24 | } 25 | } 26 | 27 | return total; 28 | 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /Amazon/Easy/1920. Build Array from Permutation.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector buildArray(vector& nums) { 4 | vectorv; 5 | for(int i=0;ilevel; 15 | int maxlevel; 16 | void deep(TreeNode*root,int h) 17 | { 18 | if(root==NULL) 19 | { 20 | return; 21 | } 22 | maxlevel=max(maxlevel,h); 23 | level[h]+=root->val; 24 | deep(root->left,h+1); 25 | deep(root->right,h+1); 26 | 27 | 28 | } 29 | int deepestLeavesSum(TreeNode* root) { 30 | deep(root,1); 31 | return level[maxlevel]; 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /Amazon/Valid parenthese.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isValid(string s) { 4 | stackst; 5 | for(int i=0;i& stones) { 4 | priority_queuepq; 5 | for(int i=0;i1) 10 | { 11 | int y=pq.top(); 12 | pq.pop(); 13 | int x=pq.top(); 14 | pq.pop(); 15 | if(x==y) 16 | { 17 | continue; 18 | } 19 | else 20 | { 21 | pq.push(y-x); 22 | } 23 | } 24 | if(pq.size()==0) 25 | { 26 | return 0; 27 | } 28 | else 29 | { 30 | return pq.top(); 31 | } 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /April Leetcode Challenge/1046. Last Stone Weight.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int lastStoneWeight(int[] stones) { 3 | int n=stones.length; 4 | PriorityQueue pq = new PriorityQueue<>(n, Collections.reverseOrder()); 5 | for(int i=0;i1) 11 | { 12 | int y=pq.poll(); 13 | int x=pq.poll(); 14 | if(y==x) 15 | { 16 | continue; 17 | } 18 | else 19 | { 20 | pq.add(y-x); 21 | } 22 | 23 | } 24 | if(pq.size()==0) 25 | { 26 | return 0; 27 | } 28 | return pq.peek(); 29 | 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /April Leetcode Challenge/11. Container With Most Water.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxArea(vector& height) { 4 | int i=0,j=height.size()-1; 5 | int amount=0; 6 | while(i> shiftGrid(vector>& grid, int k) { 4 | vector>ans(grid.size(),vector(grid[0].size(),0)); 5 | 6 | for(int i=0;ileft,k,ans); 21 | k--; 22 | if(k==0) 23 | { 24 | ans=root->val; 25 | } 26 | inorder(root->right,k,ans); 27 | } 28 | int kthSmallest(TreeNode* root, int k) { 29 | int ans=0; 30 | inorder(root,k,ans); 31 | return ans; 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /April Leetcode Challenge/31. Next Permutation.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void nextPermutation(vector& nums) { 4 | int i,j; 5 | int n=nums.size(); 6 | for(i=n-2;i>=0;i--) 7 | { 8 | if(nums[i]=0;j--) 20 | { 21 | if(nums[j]>nums[i]) 22 | { 23 | break; 24 | } 25 | } 26 | swap(nums[i],nums[j]); 27 | reverse(nums.begin()+i+1,nums.end()); 28 | } 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /April Leetcode Challenge/347. Top K Frequent Elements.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector topKFrequent(vector& nums, int k) { 4 | priority_queue>pq; 5 | mapmp; 6 | for(auto x:nums) 7 | { 8 | mp[x]++; 9 | } 10 | 11 | for(auto x:mp) 12 | { 13 | pq.push({x.second,x.first}); 14 | } 15 | vectorv; 16 | while(k--) 17 | { 18 | v.push_back(pq.top().second); 19 | pq.pop(); 20 | } 21 | return v; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /April Leetcode Challenge/535. Encode and Decode TinyURL.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | mapmp; 4 | int n=0; 5 | // Encodes a URL to a shortened URL. 6 | string encode(string longUrl) { 7 | string res= "http://tinyurl.com/" +to_string(n); 8 | n++; 9 | mp[res]=longUrl; 10 | return res; 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 | -------------------------------------------------------------------------------- /April Leetcode Challenge/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 | class Solution { 13 | public: 14 | int sum=0; 15 | void inorder(TreeNode*root) 16 | { 17 | if(root==NULL) 18 | { 19 | return; 20 | } 21 | inorder(root->right); 22 | sum+=root->val; 23 | root->val=sum; 24 | inorder(root->left); 25 | } 26 | TreeNode* convertBST(TreeNode* root) { 27 | inorder(root); 28 | return root; 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /April Leetcode Challenge/680. Valid Palindrome II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool palindrome(string s) 4 | { 5 | int i=0,j=s.size()-1; 6 | while(i<=j) 7 | { 8 | if(s[i]!=s[j]) 9 | { 10 | return false; 11 | } 12 | i++; 13 | j--; 14 | } 15 | return true; 16 | } 17 | bool validPalindrome(string s) { 18 | int i=0,j=s.size()-1; 19 | if(palindrome(s)) 20 | { 21 | return true; 22 | } 23 | while(i<=j) 24 | { 25 | if(s[i]==s[j]) 26 | { 27 | i++; 28 | j--; 29 | } 30 | else 31 | { 32 | return palindrome(s.substr(i,j-i)) || palindrome(s.substr(i+1,j-i)); 33 | } 34 | } 35 | return false; 36 | } 37 | }; 38 | -------------------------------------------------------------------------------- /April Leetcode Challenge/682. Baseball Game.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int calPoints(vector& ops) { 4 | stackst; 5 | for(int i=0;i,greater>pq; 4 | int kth; 5 | KthLargest(int k, vector& nums) { 6 | 7 | kth=k; 8 | for(auto x:nums) 9 | { 10 | add(x); 11 | } 12 | } 13 | 14 | int add(int val) { 15 | if(pq.size()add(val); 33 | */ 34 | -------------------------------------------------------------------------------- /April Leetcode Challenge/703. Kth Largest Element in a Stream.java: -------------------------------------------------------------------------------- 1 | class KthLargest { 2 | PriorityQueue pq = new PriorityQueue<>(); 3 | int kth; 4 | public KthLargest(int k, int[] nums) { 5 | kth=k; 6 | for(int i=0;ihash; 4 | MyHashSet() { 5 | hash.resize(1e6+1); 6 | } 7 | 8 | void add(int key) { 9 | hash[key]=true; 10 | } 11 | 12 | void remove(int key) { 13 | hash[key]=false; 14 | } 15 | 16 | bool contains(int key) { 17 | return hash[key]; 18 | } 19 | }; 20 | 21 | /** 22 | * Your MyHashSet object will be instantiated and called as such: 23 | * MyHashSet* obj = new MyHashSet(); 24 | * obj->add(key); 25 | * obj->remove(key); 26 | * bool param_3 = obj->contains(key); 27 | */ 28 | -------------------------------------------------------------------------------- /April Leetcode Challenge/897. Increasing Order Search Tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | TreeNode*res=new TreeNode(-1); 15 | TreeNode*ans=res; 16 | void inorder(TreeNode*root) 17 | { 18 | if(root==NULL) 19 | { 20 | return; 21 | } 22 | inorder(root->left); 23 | res->right=new TreeNode(root->val); 24 | res=res->right; 25 | inorder(root->right); 26 | } 27 | TreeNode* increasingBST(TreeNode* root) { 28 | inorder(root); 29 | return ans->right; 30 | 31 | } 32 | }; 33 | -------------------------------------------------------------------------------- /April Leetcode Challenge/Reverse a String.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void reverseString(vector& s) { 4 | int l=0,r=s.size()-1; 5 | while(l<=r) 6 | { 7 | char temp=s[l]; 8 | s[l++]=s[r]; 9 | s[r--]=temp; 10 | } 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /Array/1266. Minimum Time Visiting All Points.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minTimeToVisitAllPoints(vector>& points) { 4 | int t=0; 5 | for(int i=1;idy)?dx:dy; 10 | 11 | } 12 | return t; 13 | 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /Array/1313. Decompress Run-Length Encoded List.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector decompressRLElist(vector& nums) { 4 | vectorv; 5 | for(int i=0;i0) 10 | { 11 | v.push_back(val); 12 | f--; 13 | } 14 | } 15 | return v; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /Array/1365. How Many Numbers Are Smaller Than the Current Number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector smallerNumbersThanCurrent(vector& nums) { 4 | mapmp; 5 | for(int i=0;i createTargetArray(vector& nums, vector& index) { 4 | 5 | vectorvect; 6 | for(int i=0;i& startTime, vector& endTime, int queryTime) { 4 | int count=0; 5 | int m=startTime.size(); 6 | int n=endTime.size(); 7 | for(int i=0;i=queryTime) 10 | { 11 | count++; 12 | } 13 | } 14 | return count; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /Array/1464. Maximum Product of Two Elements in an Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProduct(vector& nums) { 4 | sort(nums.begin(),nums.end()); 5 | int n=nums.size(); 6 | int p=nums[n-1]-1; 7 | int q=nums[n-2]-1; 8 | return p*q; 9 | } 10 | }; 11 | -------------------------------------------------------------------------------- /Array/1476. Subrectangle Queries.cpp: -------------------------------------------------------------------------------- 1 | class SubrectangleQueries { 2 | public: 3 | vector>v; 4 | SubrectangleQueries(vector>& rectangle) { 5 | swap(v,rectangle); 6 | } 7 | 8 | void updateSubrectangle(int row1, int col1, int row2, int col2, int newValue) { 9 | for(int i=row1;i<=row2;i++) 10 | { 11 | for(int j=col1;j<=col2;j++) 12 | { 13 | v[i][j]=newValue; 14 | } 15 | } 16 | } 17 | 18 | int getValue(int row, int col) { 19 | return v[row][col]; 20 | } 21 | }; 22 | 23 | /** 24 | * Your SubrectangleQueries object will be instantiated and called as such: 25 | * SubrectangleQueries* obj = new SubrectangleQueries(rectangle); 26 | * obj->updateSubrectangle(row1,col1,row2,col2,newValue); 27 | * int param_2 = obj->getValue(row,col); 28 | */ 29 | -------------------------------------------------------------------------------- /Array/1480. Running Sum of 1d Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector runningSum(vector& nums) { 4 | vectorv(nums.size()); 5 | v[0]=nums[0]; 6 | for(int i=1;i>& mat) { 4 | int sum=0; 5 | int n=mat.size(); 6 | int m=n-1; 7 | for(int i=0;i& arr) { 4 | int n=arr.size(); 5 | int sum=0; 6 | for(int i=0;iv(10001); 14 | int p=0; 15 | for(int i=0;i& gain) { 4 | vectorv(gain.size()+1); 5 | v[0]=0; 6 | int max=v[0]; 7 | for(int i=1;i minOperations(string boxes) { 4 | vectorv; 5 | for(int i=0;ians; 13 | for(int i=0;i>& items, string ruleKey, string ruleValue) { 4 | 5 | int count=0; 6 | for(int i=0;i> flipAndInvertImage(vector>& A) { 4 | for (auto& v : A) { 5 | reverse(v.begin(), v.end()); 6 | for (auto& n : v) 7 | n = !n; 8 | } 9 | return A; 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /Array/Number of Subarrays with Bounded Maximum Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numSubarrayBoundedMax(vector& nums, int left, int right) { 4 | int res = 0; 5 | int prev_bigger_than_r = -1; 6 | int count_prev = 0; 7 | 8 | for (int i = 0; i < nums.size(); i++) { 9 | if (nums[i] > right) { 10 | prev_bigger_than_r = i; 11 | count_prev = 0; 12 | } 13 | 14 | else if (nums[i] < left) { 15 | res += count_prev; 16 | } 17 | 18 | else { 19 | count_prev = i - prev_bigger_than_r; 20 | res += count_prev; 21 | 22 | } 23 | } 24 | return res; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /Array/Pascal's Triangle Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> generate(int numRows) { 4 | vector>rows(numRows); 5 | for(int i=0;i0&&j& arr) { 4 | mapmp; 5 | for(auto x:arr) 6 | { 7 | mp[x]++; 8 | } 9 | priority_queuepq; 10 | for(auto x:mp) 11 | { 12 | pq.push(x.second); 13 | } 14 | int ans = 0; 15 | int size =0; 16 | while(!pq.empty()) 17 | { 18 | ans++; 19 | size+=pq.top(); 20 | pq.pop(); 21 | if(size >= arr.size()/2) 22 | { 23 | return ans; 24 | } 25 | 26 | } 27 | return 0; 28 | 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /August_Leetcode_Challenge_2022/200. Number of Islands.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | void dfs(vector>& grid,int i,int j,int r,int c) 5 | { 6 | if(i<0 or j<0 or j==c or i==r or grid[i][j]=='0') 7 | { 8 | return; 9 | } 10 | grid[i][j]='0'; 11 | dfs(grid,i+1,j,r,c); 12 | dfs(grid,i-1,j,r,c); 13 | dfs(grid,i,j+1,r,c); 14 | dfs(grid,i,j-1,r,c); 15 | return; 16 | } 17 | int numIslands(vector>& grid) { 18 | int r = grid.size(); 19 | int c = grid[0].size(); 20 | int count = 0; 21 | for(int i=0;ir(26,0); 5 | vectorm(26,0); 6 | for(int i=0;im[i]) 17 | { 18 | return false; 19 | } 20 | } 21 | return true; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /August_Leetcode_Challenge_2022/871. Minimum Number of Refueling Stops.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minRefuelStops(int target, int startFuel, vector>& stations) { 4 | int ans =0; 5 | int curd = startFuel; 6 | int i=0,n=stations.size(); 7 | priority_queue pq; 8 | while(curd>v; 4 | MyCalendar() { 5 | 6 | } 7 | 8 | bool book(int start, int end) { 9 | for(auto x:v) 10 | { 11 | if(startbook(start,end); 25 | */ 26 | -------------------------------------------------------------------------------- /August_Leetcode_Challenge_2022/Day10.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | TreeNode*BST(vector& nums, int l, int r) 15 | { 16 | if(l>r) 17 | { 18 | return NULL; 19 | } 20 | int mid = l+(r-l)/2; 21 | TreeNode*temp = new TreeNode(nums[mid]); 22 | temp->left = BST(nums,l,mid-1); 23 | temp->right = BST(nums,mid+1,r); 24 | return temp; 25 | } 26 | TreeNode* sortedArrayToBST(vector& nums) { 27 | int l=0,r=nums.size()-1; 28 | return BST(nums,l,r); 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /August_Leetcode_Challenge_2022/Day11.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 isBST(TreeNode*root,long min,long max) 15 | { 16 | if(root==NULL) 17 | { 18 | return true; 19 | } 20 | if(root->val>min && root->valleft,min, root->val); 23 | bool right = isBST(root->right,root->val,max); 24 | return left && right; 25 | } 26 | return false; 27 | } 28 | bool isValidBST(TreeNode* root) { 29 | return isBST(root,LONG_MIN,LONG_MAX); 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /August_Leetcode_Challenge_2022/Day12.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 | * }; 9 | */ 10 | 11 | class Solution { 12 | public: 13 | TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { 14 | TreeNode*curr=root; 15 | if(curr->val>p->val && curr->val>q->val) 16 | { 17 | return lowestCommonAncestor(root->left,p,q); 18 | } 19 | if(curr->valval && curr->valval) 20 | { 21 | return lowestCommonAncestor(root->right,p,q); 22 | } 23 | return curr; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /August_Leetcode_Challenge_2022/Day17.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int uniqueMorseRepresentations(vector& words) { 4 | 5 | vectortable ={".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--", 6 | "--.."}; 7 | 8 | unordered_mapmp; 9 | for(int i=0;i& nums, int target,int currsum,vector&dp) 4 | { 5 | if(currsum>target) 6 | { 7 | return 0; 8 | } 9 | if(currsum==target) 10 | { 11 | return 1; 12 | } 13 | if(dp[currsum]!=-1) 14 | { 15 | return dp[currsum]; 16 | } 17 | int ans = 0; 18 | for(int i=0;i& nums, int target) { 26 | vectordp; 27 | dp.assign(1001,-1); 28 | int currsum=0; 29 | return findways(nums,target,currsum,dp); 30 | 31 | } 32 | }; 33 | -------------------------------------------------------------------------------- /August_Leetcode_Challenge_2022/Day6.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int poorPigs(int buckets, int minutesToDie, int minutesToTest) { 4 | int t = minutesToTest / minutesToDie; 5 | return ceil(log(buckets) / log(t+1)); 6 | } 7 | }; 8 | -------------------------------------------------------------------------------- /August_Leetcode_Challenge_2022/Day8.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int binary_search(vector&v , int l ,int r,int x) 4 | { 5 | while(l=x) 9 | { 10 | r=mid; 11 | } 12 | else 13 | { 14 | l=mid+1; 15 | } 16 | } 17 | return r; 18 | } 19 | 20 | int lengthOfLIS(vector& nums) { 21 | vectorv; 22 | int n = nums.size(); 23 | v.push_back(nums[0]); 24 | for(int i=1;iv[v.size()-1]) 27 | { 28 | v.push_back(nums[i]); 29 | } 30 | else 31 | { 32 | int j= binary_search(v,0,v.size()-1,nums[i]); 33 | v[j]=nums[i]; 34 | } 35 | } 36 | return v.size(); 37 | } 38 | }; 39 | -------------------------------------------------------------------------------- /August_Leetcode_Challenge_2022/Day9.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int mod = pow(10,9) + 7; 4 | int numFactoredBinaryTrees(vector& arr) { 5 | mapmp; 6 | sort(arr.begin(),arr.end()); 7 | mp[arr[0]]=1; 8 | for(int i=1;i&visited,vector>& rooms) 4 | { 5 | queueq; 6 | q.push(i); 7 | while(!q.empty()) 8 | { 9 | int j=q.front(); 10 | q.pop(); 11 | visited[j]=true; 12 | for(auto room: rooms[j]) 13 | { 14 | if(!visited[room]) 15 | { 16 | q.push(room); 17 | } 18 | } 19 | } 20 | } 21 | bool canVisitAllRooms(vector>& rooms) { 22 | int n = rooms.size(); 23 | vectorvisited(n,false); 24 | bfs(0,visited,rooms); 25 | for(int i=0;iv; 4 | void generate(string &s,int open,int close) 5 | { 6 | if(close==0&&open==0) 7 | { 8 | v.push_back(s); 9 | return; 10 | } 11 | if(open>0) 12 | { 13 | s.push_back('('); 14 | generate(s,open-1,close); 15 | s.pop_back(); 16 | } 17 | if(close>0) 18 | { 19 | if(open generateParenthesis(int n) 29 | { 30 | string s; 31 | generate(s,n,n); 32 | return v; 33 | } 34 | }; 35 | -------------------------------------------------------------------------------- /Binary Search Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int bsearch(vector&nums,int l,int r,int target) 4 | { 5 | if(r>=l) 6 | { 7 | int m=l+(r-l)/2; 8 | if(nums[m]==target) 9 | { 10 | return m; 11 | } 12 | if(nums[m]& nums, int target) { 21 | int n=nums.size(); 22 | return bsearch(nums,0,n,target); 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /Binary Search.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @param {number} target 4 | * @return {number} 5 | */ 6 | var search = function(nums, target) { 7 | 8 | let i=0,j=nums.length-1; 9 | 10 | while(i<=j) 11 | { 12 | let mid=i + Math.floor((j - i) / 2); 13 | 14 | if(nums[mid]==target) 15 | { 16 | return mid; 17 | } 18 | if(nums[mid]& arr1, vector& arr2, int d) { 4 | int count=0; 5 | sort(arr2.begin(),arr2.end()); 6 | for(int i=0;iarr1[i]) 18 | { 19 | r=mid-1; 20 | } 21 | else 22 | { 23 | l=mid+1; 24 | } 25 | 26 | } 27 | 28 | } 29 | return arr1.size()-count; 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /Binary Search/540. Single Element in a Sorted Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int singleNonDuplicate(vector& nums) { 4 | int l=0,r=nums.size()-1; 5 | if(r==0) 6 | { 7 | return nums[r]; 8 | } 9 | if(nums[l]!=nums[l+1]) 10 | { 11 | return nums[l]; 12 | } 13 | if(nums[r]!=nums[r-1]) 14 | { 15 | return nums[r]; 16 | } 17 | while(l<=r) 18 | { 19 | int mid=r+(l-r)/2; 20 | if(nums[mid]!=nums[mid+1] && nums[mid]!=nums[mid-1]) 21 | { 22 | return nums[mid]; 23 | } 24 | if(((mid%2)==0 && nums[mid]==nums[mid+1]) || ((mid%2)==1) && nums[mid]==nums[mid-1]) 25 | { 26 | l=mid+1; 27 | } 28 | else 29 | { 30 | r=mid-1; 31 | } 32 | } 33 | return -1; 34 | } 35 | }; 36 | -------------------------------------------------------------------------------- /DFS/1325. Delete Leaves With a Given Value.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | TreeNode* removeLeafNodes(TreeNode* root, int target) { 15 | if(!root) 16 | { 17 | return NULL; 18 | } 19 | root->left=removeLeafNodes(root->left,target); 20 | root->right= removeLeafNodes(root->right,target); 21 | if(root->val ==target && root->left==NULL && root->right==NULL ) 22 | { 23 | return NULL; 24 | } 25 | return root; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /DFS/419. Battleships in a Board.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void dfs(int i,int j,vector>& board) 4 | { 5 | if(i<0 || j<0 || i>=board.size() || j>=board[0].size() || board[i][j]=='.') 6 | { 7 | return; 8 | } 9 | board[i][j]='.'; 10 | dfs(i+1,j,board); 11 | dfs(i-1,j,board); 12 | dfs(i,j+1,board); 13 | dfs(i,j-1,board); 14 | } 15 | int countBattleships(vector>& board) { 16 | int ans = 0; 17 | for(int i=0;i>& grid,int&ans) 4 | { 5 | if(i<0 or j<0 or i>=grid.size() or j>=grid[0].size() or grid[i][j]==0) 6 | { 7 | ans++; 8 | return; 9 | } 10 | if(grid[i][j]==2) 11 | { 12 | return; 13 | } 14 | grid[i][j]=2; 15 | dfs(i+1,j,grid,ans); 16 | dfs(i,j+1,grid,ans); 17 | dfs(i-1,j,grid,ans); 18 | dfs(i,j-1,grid,ans); 19 | } 20 | int islandPerimeter(vector>& grid) { 21 | int ans = 0; 22 | for(int i=0;i>& graph,vectortemp,vector>&res,int start,int end) 4 | { 5 | temp.push_back(start); 6 | if(start == end) 7 | { 8 | res.push_back(temp); 9 | return; 10 | } 11 | for(auto x:graph[start]) 12 | { 13 | dfs(graph,temp,res,x,end); 14 | } 15 | } 16 | vector> allPathsSourceTarget(vector>& graph) { 17 | vector>res; 18 | vectortemp; 19 | int n = graph.size(); 20 | dfs(graph,temp,res,0,n-1); 21 | return res; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /DYNAMIC PROGRAMMING/0-1 Knapsack Problems/Partition Equal Subset Sum with recursion.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPartion(int i,vector& nums,int n,vector&dp) 4 | { 5 | if(n == 0) 6 | { 7 | return true; 8 | } 9 | if(i>=nums.size()) 10 | { 11 | return false ; 12 | } 13 | if(n<0) 14 | { 15 | return false; 16 | } 17 | 18 | 19 | 20 | bool yes = isPartion(i+1,nums,n-nums[i],dp); 21 | bool no = isPartion(i+1,nums,n,dp); 22 | 23 | return yes || no; 24 | 25 | } 26 | bool canPartition(vector& nums) { 27 | int sum = 0; 28 | vectordp(nums.size()+1,-1); 29 | for(auto x:nums) 30 | { 31 | sum+=x; 32 | } 33 | if(sum%2==1) 34 | { 35 | return false; 36 | } 37 | int n = sum/2; 38 | return isPartion(0,nums,n,dp); 39 | 40 | } 41 | }; 42 | -------------------------------------------------------------------------------- /DYNAMIC PROGRAMMING/1143. Longest Common Subsequence.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int longestCommonSubsequence(string text1, string text2) { 4 | int n1=text1.size(); 5 | int n2=text2.size(); 6 | int dp[n1+1][n2+1]; 7 | for(int i=0;i<=n1;i++) 8 | { 9 | for(int j=0;j<=n2;j++) 10 | { 11 | if(i==0||j==0) 12 | { 13 | dp[i][j]=0; 14 | } 15 | } 16 | } 17 | for(int i=1;i<=n1;i++) 18 | { 19 | for(int j=1;j<=n2;j++) 20 | { 21 | if(text1[i-1]==text2[j-1]) 22 | { 23 | dp[i][j]=1+dp[i-1][j-1]; 24 | } 25 | else 26 | { 27 | dp[i][j]=max(dp[i-1][j],dp[i][j-1]); 28 | } 29 | } 30 | } 31 | return dp[n1][n2]; 32 | 33 | } 34 | }; 35 | -------------------------------------------------------------------------------- /DYNAMIC PROGRAMMING/300. Longest Increasing Subsequence.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int lengthOfLIS(vector& nums) { 4 | int n=nums.size(); 5 | vectorlist(n,1); 6 | 7 | for(int i=1;inums[j] && list[j]>=list[i]) 12 | { 13 | list[i]=1+list[j]; 14 | } 15 | } 16 | } 17 | return *max_element(list.begin(),list.end()); 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /DYNAMIC PROGRAMMING/62. Unique Paths.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int dp[101][101]; 4 | int solve(int i,int j,int m,int n) 5 | { 6 | if(i==m-1 && j==n-1) 7 | { 8 | return 1; 9 | } 10 | if(i+1>m) 11 | { 12 | return 0; 13 | } 14 | if(j+1>n) 15 | { 16 | return 0; 17 | } 18 | if(dp[i][j]!=-1) 19 | { 20 | return dp[i][j]; 21 | } 22 | dp[i][j] = solve(i+1,j,m,n)+solve(i,j+1,m,n); 23 | return dp[i][j]; 24 | } 25 | int uniquePaths(int m, int n) { 26 | memset(dp,-1,sizeof(dp)); 27 | return solve(0,0,m,n); 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /DYNAMIC PROGRAMMING/63. Unique Paths II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int dp[101][101]; 4 | int solve(int i,int j,vector>&grid,int m,int n) 5 | { 6 | if(i>=m || j>=n|| grid[i][j]==1) 7 | { 8 | return 0; 9 | } 10 | if(i==m-1 && j == n-1) 11 | { 12 | return 1; 13 | } 14 | if(dp[i][j]!=-1) 15 | { 16 | return dp[i][j]; 17 | } 18 | dp[i][j] = solve(i+1,j,grid,m,n) + solve(i,j+1,grid,m,n); 19 | return dp[i][j]; 20 | } 21 | int uniquePathsWithObstacles(vector>& obstacleGrid) { 22 | int m = obstacleGrid.size(); 23 | int n = obstacleGrid[0].size(); 24 | memset(dp,-1,sizeof(dp)); 25 | return solve(0,0,obstacleGrid,m,n); 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /DYNAMIC PROGRAMMING/64. Minimum Path Sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int dp[201][201]; 4 | int solve(int i,int j,vector>& grid,int m,int n) 5 | { 6 | if(i==m-1 && j == n-1) 7 | { 8 | return grid[i][j]; 9 | } 10 | if(i>=m || j>=n) 11 | { 12 | return 1000; 13 | } 14 | if(dp[i][j]!=-1) 15 | { 16 | return dp[i][j]; 17 | } 18 | 19 | dp[i][j]= min(solve(i+1,j,grid,m,n) + grid[i][j],solve(i,j+1,grid,m,n)+ grid[i][j]); 20 | return dp[i][j]; 21 | } 22 | int minPathSum(vector>& grid) { 23 | memset(dp,-1,sizeof(dp)); 24 | int m = grid.size(); 25 | int n = grid[0].size(); 26 | return solve(0,0,grid,m,n); 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /DYNAMIC PROGRAMMING/House_Robber_II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int solve(int i,int n,vector& nums,vector&dp) 4 | { 5 | if(i>=n) 6 | { 7 | return 0; 8 | } 9 | if(dp[i]!=-1) 10 | { 11 | return dp[i]; 12 | } 13 | dp[i] = max(nums[i]+solve(i+2,n,nums,dp),solve(i+1,n,nums,dp)); 14 | return dp[i]; 15 | } 16 | int rob(vector& nums) { 17 | 18 | int n = nums.size(); 19 | vectordp1(n+1,-1); 20 | vectordp2(n+1,-1); 21 | if(n==1) 22 | { 23 | return nums[0]; 24 | } 25 | return max(solve(0,n-1,nums,dp1),solve(1,n,nums,dp2)); 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /DYNAMIC PROGRAMMING/House_robber_1.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int dp[101]; 4 | int solve(int i,vector& nums) 5 | { 6 | if(i>=nums.size()) 7 | { 8 | return 0; 9 | } 10 | if(dp[i]!=-1) 11 | { 12 | return dp[i]; 13 | } 14 | int take = solve(i+2,nums) + nums[i]; 15 | int notake = solve(i+1,nums)+0; 16 | dp[i]=max(take,notake); 17 | return dp[i]; 18 | } 19 | int rob(vector& nums) { 20 | memset(dp,-1,sizeof(dp)); 21 | return solve(0,nums); 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /Day1 53. Maximum Subarray.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @return {number} 4 | */ 5 | var maxSubArray = function(nums) { 6 | 7 | let currsum=nums[0]; 8 | let totalsum=nums[0]; 9 | for(let i=1;i>& grid,int&area,int i,int j, int r,int c) 4 | { 5 | if(i<0 || j<0 || i>=r || j>=c || grid[i][j]==0) 6 | { 7 | return; 8 | } 9 | area++; 10 | grid[i][j]=0; 11 | dfs(grid,area,i+1,j,r,c); 12 | dfs(grid,area,i-1,j,r,c); 13 | dfs(grid,area,i,j+1,r,c); 14 | dfs(grid,area,i,j-1,r,c); 15 | } 16 | int maxAreaOfIsland(vector>& grid) { 17 | int r=grid.size(); 18 | int c=grid[0].size(); 19 | int maxa=0; 20 | for(int i=0;i& words) { 13 | int n=words.size(); 14 | vector state; 15 | for(auto a:words) 16 | { 17 | state.push_back(findState(a)); 18 | } 19 | int ans=INT_MIN; 20 | for(int i=0;i& horizontalCuts, vector& verticalCuts) { 4 | int i,maxh=0,maxv=0,p1=0,p2=0; 5 | 6 | horizontalCuts.push_back(h); 7 | verticalCuts.push_back(w); 8 | 9 | sort(horizontalCuts.begin(),horizontalCuts.end()); 10 | sort(verticalCuts.begin(), verticalCuts.end()); 11 | 12 | for(i=0;imp; 6 | while(p1<=p2&&p10) 9 | { 10 | while(mp[s[p2]]>0) 11 | { 12 | mp[s[p1]]=0; 13 | p1++; 14 | } 15 | count=p2-p1; 16 | } 17 | if(mp[s[p2]]==0) 18 | 19 | { 20 | mp[s[p2]]=1; 21 | count++; 22 | } 23 | if(count>max) 24 | { 25 | max=count; 26 | } 27 | p2++; 28 | } 29 | return max; 30 | 31 | } 32 | }; 33 | -------------------------------------------------------------------------------- /Expedia/70. Climbing Stairs.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int climbStairs(int n) { 4 | vector steps(n+1, 0); 5 | steps[0] = 1, steps[1] = 1; 6 | for (int i = 2; i <= n; i++) 7 | steps[i] = steps[i-1] + steps[i-2]; 8 | return steps[n]; 9 | } 10 | }; 11 | -------------------------------------------------------------------------------- /Find the Difference.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | char findTheDifference(string s, string t) { 4 | int str[27]={0}; 5 | for(int i=0;i& nums) { 4 | 5 | int n=nums.size(); 6 | int i,j; 7 | for(i=n-2;i>=0;i--) 8 | { 9 | if(nums[i]=0;j--) 21 | { 22 | if(nums[j]>nums[i]) 23 | { 24 | break; 25 | } 26 | } 27 | swap(nums[i],nums[j]); 28 | reverse(nums.begin()+i+1,nums.end()); 29 | } 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /Google/42. Trapping Rain Water.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int trap(vector& height) { 4 | int size=height.size(); 5 | if(size==0) 6 | { 7 | return 0; 8 | } 9 | vectormaxl(size); 10 | vectormaxr(size); 11 | maxl[0]=height[0]; 12 | for(int i=1;i=0;i--) 18 | { 19 | maxr[i]=max(maxr[i+1],height[i]); 20 | } 21 | int water=0; 22 | for(int i=0;i0) 13 | { 14 | x=x*x; 15 | n=n/2; 16 | } 17 | return 1/x; 18 | } 19 | while(n>0) 20 | { 21 | x=x*x; 22 | n=n/2; 23 | } 24 | return x; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /Google/Find all triplets with zero sum.cpp: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Google/GFG/Find triplets with zero sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | //Function to find triplets with zero sum. 4 | bool findTriplets(int arr[], int n) 5 | { 6 | sort(arr,arr+n); 7 | int count = 0; 8 | for(int i=0;i&ans) 5 | { 6 | if(i==s.size()) 7 | { 8 | ans.push_back(s); 9 | return; 10 | } 11 | if(s[i]== '?') 12 | { 13 | s[i]='0'; 14 | generate(i+1,s,ans); 15 | s[i]='1'; 16 | generate(i+1,s,ans); 17 | } 18 | else 19 | { 20 | generate(i+1,s,ans); 21 | } 22 | 23 | } 24 | vector generate_binary_string(string s) 25 | { 26 | vectorans; 27 | generate(0,s,ans); 28 | return ans;// Code here 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /Graph/110. Balanced Binary Tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | bool answer=true; 15 | int height(TreeNode*root) 16 | { 17 | if(root==NULL) 18 | { 19 | return 0; 20 | } 21 | int l=height(root->left); 22 | int r=height(root->right); 23 | if(abs(l-r)>1) 24 | { 25 | answer=false; 26 | } 27 | return max(l,r)+1; 28 | } 29 | bool isBalanced(TreeNode* root) { 30 | height(root); 31 | return answer; 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /Graph/1306. Jump Game III.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canReach(vector& arr, int start) { 4 | queueq; 5 | q.push(start); 6 | int n=arr.size(); 7 | vectorvisited(n,false); 8 | visited[start]=true; 9 | while(!q.empty()) 10 | { 11 | int i=q.front(); 12 | q.pop(); 13 | visited[i]=true; 14 | if(arr[i]==0) 15 | { 16 | return true; 17 | } 18 | if(i+arr[i]=0&&!visited[i-arr[i]]) 27 | { 28 | q.push(i-arr[i]); 29 | if(arr[i-arr[i]]==0) 30 | { 31 | return true; 32 | } 33 | } 34 | 35 | } 36 | return false; 37 | 38 | } 39 | }; 40 | -------------------------------------------------------------------------------- /Graph/513. Find Bottom Left Tree Value.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | int findBottomLeftValue(TreeNode* root) { 15 | queueq; 16 | q.push(root); 17 | while(!q.empty()) 18 | { 19 | root=q.front(); 20 | q.pop(); 21 | if(root->right) 22 | { 23 | q.push(root->right); 24 | } 25 | if(root->left) 26 | { 27 | q.push(root->left); 28 | } 29 | } 30 | return root->val; 31 | 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /Graph/563. Binary Tree Tilt.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | int total; 15 | int dfs(TreeNode*root) 16 | { 17 | if(root==NULL) 18 | { 19 | return 0; 20 | } 21 | int l=dfs(root->left); 22 | int r=dfs(root->right); 23 | total+=abs(l-r); 24 | return l+r+root->val; 25 | } 26 | int findTilt(TreeNode* root) { 27 | dfs(root); 28 | return total; 29 | 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /Greedy Algorithm/1725. Number Of Rectangles That Can Form The Largest Square.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countGoodRectangles(vector>& rectangles) { 4 | 5 | int maxLen = 0; 6 | int cnt = 0; 7 | 8 | for(int i=0 ; i maxLen) 13 | { 14 | maxLen = len; 15 | cnt = 1; 16 | } 17 | else if(len == maxLen) cnt++; 18 | } 19 | 20 | return cnt; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Greedy Algorithm/2160. Minimum Sum of Four Digit Number After Splitting Digits.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minimumSum(int num) { 4 | vectorv; 5 | while(num>0) 6 | { 7 | int d=num%10; 8 | num=num/10; 9 | v.push_back(d); 10 | } 11 | sort(v.begin(),v.end()); 12 | int p=v[0]*10+v[2]; 13 | int q=v[1]*10+v[3]; 14 | return p+q; 15 | 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /Greedy Algorithm/Maximum Units on a Truck Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool static sorted( vectorv1,vectorv2 ) 4 | { 5 | return v1[1] > v2[1]; 6 | } 7 | int maximumUnits(vector>& vect, int truckSize) { 8 | sort(vect.begin(), vect.end(),sorted); 9 | int sum=0; 10 | int n=0,i; 11 | int t=0; 12 | for( i=0;i&a,vector&b) 4 | { 5 | return a[1]>b[1]; 6 | } 7 | int maximumUnits(vector>& boxTypes, int truckSize) { 8 | sort(boxTypes.begin(),boxTypes.end(),comp); 9 | int tt=0,nbox=0,ans=0,i=0; 10 | for(i=0;i>& grid,int&area,int i,int j, int r,int c) 4 | { 5 | if(i<0 || j<0 || i>=r || j>=c || grid[i][j]==0) 6 | { 7 | return; 8 | } 9 | area++; 10 | grid[i][j]=0; 11 | dfs(grid,area,i+1,j,r,c); 12 | dfs(grid,area,i-1,j,r,c); 13 | dfs(grid,area,i,j+1,r,c); 14 | dfs(grid,area,i,j-1,r,c); 15 | } 16 | int maxAreaOfIsland(vector>& grid) { 17 | int r=grid.size(); 18 | int c=grid[0].size(); 19 | int maxa=0; 20 | for(int i=0;i=m ||c<0 || c>=n) 8 | { 9 | return 1; 10 | } 11 | if(maxMove<=0) 12 | { 13 | return 0; 14 | } 15 | if(dp[r][c][maxMove]!=-1) 16 | { 17 | return dp[r][c][maxMove]; 18 | } 19 | 20 | return dp[r][c][maxMove] = 21 | (dfs(m,n,maxMove-1,r-1,c)%mod + 22 | dfs(m,n,maxMove-1,r+1,c)%mod + 23 | dfs(m,n,maxMove-1,r,c-1)%mod + 24 | dfs(m,n,maxMove-1,r,c+1)%mod)%mod; 25 | } 26 | int findPaths(int m, int n, int maxMove, int startRow, int startColumn) { 27 | 28 | memset(dp,-1,sizeof(dp)); 29 | return dfs(m,n,maxMove,startRow,startColumn); 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /July Leetcode Challenge 2022/Day16.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | 3 | def findPaths(self, m: int, n: int, maxMove: int, startRow: int, startColumn: int) -> int: 4 | dp = [[[-1 for i in range(51)] for j in range(51)] for k in range(51)] 5 | mod = 10**9 + 7 6 | def dfs(m,n,maxMove,r,c): 7 | if r<0 or r>=m or c<0 or c>=n: 8 | return 1 9 | if maxMove <=0 : 10 | return 0 11 | if dp[r][c][maxMove] !=-1: 12 | return dp[r][c][maxMove] 13 | dp[r][c][maxMove] = dfs(m,n,maxMove-1,r-1,c) % mod + dfs(m,n,maxMove-1,r+1,c)%mod + dfs(m,n,maxMove-1,r,c-1)%mod + dfs(m,n,maxMove-1,r,c+1)%mod 14 | return dp[r][c][maxMove]%mod 15 | return dfs(m,n,maxMove,startRow,startColumn) 16 | 17 | 18 | -------------------------------------------------------------------------------- /July Leetcode Challenge 2022/Day17.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long int mod =1000000007; 4 | int kInversePairs(int n, int k) { 5 | vector> dp(n+1,vector(k+1,0)); 6 | dp[0][0] =1; 7 | for(int i=1;i<=n;i++) 8 | { 9 | long int val=0; 10 | for(int j=0;j<=k;j++) 11 | { 12 | val+=dp[i-1][j]; 13 | if(j>=i) 14 | { 15 | val-=dp[i-1][j-i]; 16 | } 17 | dp[i][j]=val%mod; 18 | } 19 | } 20 | return dp[n][k]; 21 | 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /July Leetcode Challenge 2022/Day19.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> generate(int numRows) { 4 | vector>rows(numRows); 5 | for(int i=0;i0&&j& words) { 4 | int count=words.size(); 5 | vector>charIndex(26); 6 | 7 | for(int i=0;inext=head; 17 | for(int i=0;inext; 20 | } 21 | ListNode*cur=prev->next; 22 | for(int i=0;inext; 25 | prev->next = cur->next; 26 | cur->next = cur->next->next; 27 | prev->next->next = ptr; 28 | } 29 | return temp->next; 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /July Leetcode Challenge 2022/Day24.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool searchMatrix(vector>& matrix, int target) { 4 | int m= matrix.size(); 5 | if(m==0) 6 | return false; 7 | int n = matrix[0]. size(); 8 | int i=0, j=n-1; 9 | while(i=0){ 10 | if(matrix[i][j]==target) 11 | return true; 12 | 13 | if(matrix[i][j]>target) 14 | j--; 15 | else 16 | i++; 17 | } 18 | return false; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /July Leetcode Challenge 2022/Day24BinarySearch.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool binarySearch(vector>& matrix,int target,int row) 4 | { 5 | int l = 0; 6 | int r = matrix[0].size()-1; 7 | while(l<=r) 8 | { 9 | int mid = r+(l-r)/2; 10 | if(matrix[row][mid]==target) 11 | { 12 | return true; 13 | } 14 | else if(matrix[row][mid] > target) 15 | { 16 | r = mid-1; 17 | } 18 | else 19 | { 20 | l = mid+1; 21 | } 22 | } 23 | return false; 24 | } 25 | bool searchMatrix(vector>& matrix, int target) { 26 | int n=matrix.size(); 27 | for(int i=0;ileft,p,q); 23 | TreeNode*right = lowestCommonAncestor(root->right,p,q); 24 | if(left && right) 25 | { 26 | return root; 27 | } 28 | 29 | if(left) 30 | { 31 | return left; 32 | } 33 | return right; 34 | 35 | 36 | } 37 | }; 38 | -------------------------------------------------------------------------------- /July Leetcode Challenge 2022/Day3.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int wiggleMaxLength(vector& nums) { 4 | int n=nums.size(); 5 | if(n==1) 6 | { 7 | return 1; 8 | } 9 | int pdiff = nums[1]-nums[0]; 10 | int total=0; 11 | if(pdiff!=0) 12 | { 13 | total=2; 14 | } 15 | else 16 | { 17 | total=1; 18 | } 19 | for(int i=2;i=0 && cdiff<0) ||(cdiff>0 && pdiff<=0)) 23 | { 24 | total++; 25 | pdiff=cdiff; 26 | } 27 | } 28 | return total; 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /July Leetcode Challenge 2022/Day4.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int candy(vector& ratings) { 4 | int n=ratings.size(); 5 | vectorcandy(n,1); 6 | for(int i=1;iratings[i-1]) 9 | candy[i]=candy[i-1]+1; 10 | } 11 | for(int i=n-2;i>=0;i--) 12 | { 13 | if(ratings[i]>ratings[i+1] && candy[i]<=candy[i+1]) 14 | candy[i]=candy[i+1]+1; 15 | } 16 | return accumulate(candy.begin(),candy.end(),0); 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /July Leetcode Challenge 2022/Day5.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int longestConsecutive(vector& nums) { 4 | unordered_setsets(nums.begin(), nums.end()); 5 | int ans=0; 6 | for(int it: nums){ 7 | if(sets.find(it)==sets.end()) 8 | {continue; 9 | } 10 | int prev = it-1, next = it+1; 11 | while(sets.find(prev)!=sets.end()) 12 | { 13 | prev--; 14 | } 15 | while(sets.find(next)!=sets.end()) 16 | { 17 | next++; 18 | } 19 | ans = max(ans, next-prev-1); 20 | sets.erase(it); 21 | } 22 | return ans; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /July Leetcode Challenge 2022/Day6dp.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int dp[31]; 4 | int fib(int n) { 5 | if(n<=1) 6 | { 7 | return n; 8 | } 9 | dp[0]=0,dp[1]=1; 10 | for(int i=2;i<=n;i++) 11 | { 12 | dp[i]=dp[i-1]+dp[i-2]; 13 | } 14 | return dp[n]; 15 | } 16 | 17 | }; 18 | -------------------------------------------------------------------------------- /July Leetcode Challenge 2022/Day6rec.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int fib(int n) { 4 | 5 | if(n<=1) 6 | { 7 | return n; 8 | } 9 | return fib(n-1)+fib(n-2); 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /July Leetcode Challenge 2022/Day6var.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int fib(int n) { 4 | int p2=1,p1=0,curr; 5 | if(n<=1) 6 | { 7 | return n; 8 | } 9 | for(int i=1;i> transpose(vector>& matrix) { 4 | int n = matrix.size(); 5 | int m = matrix[0].size(); 6 | 7 | vector> res(m,vector (n,0)); 8 | for(int i=0;i runningSum(vector& nums) { 4 | for(int i=1;imp(127,-1); 5 | int pos=-1, maxlen=0; 6 | 7 | for (int i = 0; i < s.size(); i++) 8 | { 9 | int ch = s[i]; 10 | pos = max(pos, mp[ch]); 11 | maxlen = max(maxlen, i - pos); 12 | mp[ch] = i; 13 | } 14 | 15 | return maxlen; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /June Leetcode Challenge/Day21.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int furthestBuilding(vector& heights, int bricks, int ladders) { 4 | int hsum=0; 5 | int i=0; 6 | priority_queuemaxheap; 7 | while(ibricks && ladders>0) 14 | { 15 | hsum-=maxheap.top(); 16 | maxheap.pop(); 17 | ladders-=1; 18 | } 19 | if(hsum>bricks && ladders==0) 20 | { 21 | break; 22 | } 23 | } 24 | i=i+1; 25 | } 26 | return i; 27 | 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /June Leetcode Challenge/Day22.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findKthLargest(vector& nums, int k) { 4 | // Creates a min heap 5 | priority_queue , greater > pq; 6 | 7 | for(int i=0;i&a,vector&b) 4 | { 5 | return a[1]>& courses) { 8 | sort(courses.begin(),courses.end(),comp); 9 | if(courses.size() <= 0) return 0; 10 | 11 | priority_queue q; 12 | int sum = 0; 13 | for(auto i : courses) { 14 | sum += i[0]; 15 | q.push(i[0]); 16 | if(sum > i[1]) { 17 | sum -= q.top(); 18 | q.pop(); 19 | } 20 | } 21 | return q.size(); 22 | 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /June Leetcode Challenge/Day24.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPossible(vector& target) { 4 | long tsum=0; 5 | priority_queuemaxq; 6 | for(auto x:target) 7 | { 8 | tsum+=x; 9 | maxq.push(x); 10 | 11 | } 12 | while(maxq.top()!=1) 13 | { 14 | int large=maxq.top(); 15 | maxq.pop(); 16 | tsum=tsum-large; 17 | if(tsum<=0 || tsum>=large) 18 | { 19 | return false; 20 | } 21 | large=large%tsum; 22 | maxq.push(large?large:tsum); 23 | tsum=tsum+large; 24 | } 25 | return true; 26 | 27 | 28 | 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /June Leetcode Challenge/Day25.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool checkPossibility(vector& nums) { 4 | int count = 0; 5 | for(int i = 1; i < nums.size(); i++){ 6 | if(nums[i] < nums[i-1]){ 7 | count++; 8 | if(count > 1) return false; 9 | if(i == 1 || nums[i-2] <= nums[i]) nums[i-1] = nums[i]; 10 | else nums[i] = nums[i-1]; 11 | } 12 | } 13 | return true; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /June Leetcode Challenge/Day26.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxScore(vector& cardPoints, int k) { 4 | int sum=0; 5 | for(int i=0;i=0;i--) 11 | { 12 | sum-=cardPoints[i]; 13 | sum+=cardPoints[cardPoints.size()-k+i]; 14 | maxsum=max(sum,maxsum); 15 | } 16 | return maxsum; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /June Leetcode Challenge/Day28.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minDeletions(string s) { 4 | vectorfreq(26); 5 | for(auto x:s) 6 | { 7 | freq[x-'a']++; 8 | } 9 | sort(freq.begin(),freq.end(),greater()); 10 | int maxf=freq[0]-1; 11 | int totald=0; 12 | for(int i=1;i<=25;i++) 13 | { 14 | if(freq[i]!=0) 15 | { 16 | if(freq[i]>maxf) 17 | { 18 | if(maxf>0){ 19 | totald+=(freq[i]-maxf) ; 20 | } 21 | else 22 | { 23 | totald+=freq[i]; 24 | } 25 | } 26 | } 27 | maxf=min(maxf-1,freq[i]-1); 28 | } 29 | return totald; 30 | 31 | } 32 | }; 33 | -------------------------------------------------------------------------------- /June Leetcode Challenge/Day7.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void merge(vector& nums1, int m, vector& nums2, int n) { 4 | while(n>0||m>0) 5 | { 6 | if(m>0 and (n==0 or (nums1[m-1]>nums2[n-1]))) 7 | { 8 | nums1[m+n-1]=nums1[m-1]; 9 | m--; 10 | } 11 | else 12 | { 13 | nums1[m+n-1]=nums2[n-1]; 14 | n--; 15 | } 16 | } 17 | 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /June Leetcode Challenge/Day8.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int removePalindromeSub(string s) { 4 | if(s.size()==0) return 0; 5 | int i=0, j=s.size()-1; 6 | while(i twoSum(vector& numbers, int target) {vectorv; 4 | int l=0,r=numbers.size()-1; 5 | 6 | while(ltarget) 15 | { 16 | r--; 17 | } 18 | else 19 | { 20 | l++; 21 | } 22 | } 23 | return {}; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /June Leetcode Challenge/day15.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool static compare(string &s1,string &s2) 4 | { 5 | return s1.size() > s2.size(); 6 | } 7 | int longestStrChain(vector& words) { 8 | int n=words.size(); 9 | sort(words.begin(),words.end(),compare); 10 | unordered_mapmp; 11 | for(int i=0;idp(n+1,1); 16 | int ans=1; 17 | for(int i=0;i&a,vector&b) 4 | { 5 | if(a[0]==b[0]) 6 | { 7 | return a[1]b[0]; 10 | } 11 | vector> reconstructQueue(vector>& people) { 12 | sort(people.begin(),people.end(),comp); 13 | vector>ans; 14 | for(auto p:people) 15 | { 16 | ans.insert(ans.begin()+p[1],p); 17 | } 18 | return ans; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /June Leetcode Challenge/day30.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minMoves2(vector& nums) { 4 | sort(nums.begin(),nums.end()); 5 | int n = nums.size(); 6 | int moves=0; 7 | int mide=nums[n/2]; 8 | for(int i=0;i &v1, const vector &v2){ 6 | 7 | return v1[1] - v1[0] < v2[1] - v2[0]; 8 | 9 | } 10 | 11 | int twoCitySchedCost(vector>& costs) { 12 | 13 | int ans = 0; 14 | 15 | sort(costs.begin(), costs.end(), sortDiff); 16 | 17 | for(int i = 0; i < costs.size(); i++){ 18 | 19 | if(i < costs.size() / 2) 20 | 21 | ans += costs[i][1]; 22 | 23 | else 24 | 25 | ans += costs[i][0]; 26 | 27 | } 28 | 29 | return ans; 30 | 31 | } 32 | 33 | }; 34 | 35 | 36 | -------------------------------------------------------------------------------- /March LeetCode Challenge/1359. Count All Valid Pickup and Delivery Options.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int MOD = 1e9 + 7; 4 | int countOrders(int n) { 5 | long places=2*n; 6 | long seq=1; 7 | for(int i=n;i>=1;i--) 8 | { 9 | seq=(seq*((places*(places-1)))/2)%MOD; 10 | places=places-2; 11 | } 12 | 13 | return (int)seq; 14 | 15 | } 16 | }; 17 | 18 | -------------------------------------------------------------------------------- /March LeetCode Challenge/138. Copy List with Random Pointer.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | // Definition for a Node. 3 | class Node { 4 | public: 5 | int val; 6 | Node* next; 7 | Node* random; 8 | 9 | Node(int _val) { 10 | val = _val; 11 | next = NULL; 12 | random = NULL; 13 | } 14 | }; 15 | */ 16 | 17 | class Solution { 18 | public: 19 | Node* copyRandomList(Node* head) { 20 | Node*curr=head; 21 | unordered_mapmp; 22 | while(curr!=NULL) 23 | { 24 | Node*temp= new Node(curr->val); 25 | mp[curr]=temp; 26 | curr=curr->next; 27 | } 28 | curr=head; 29 | 30 | while(curr!=NULL) 31 | { 32 | mp[curr]->next=mp[curr->next]; 33 | mp[curr]->random=mp[curr->random]; 34 | curr=curr->next; 35 | } 36 | return mp[head]; 37 | 38 | } 39 | }; 40 | -------------------------------------------------------------------------------- /March LeetCode Challenge/1663. Smallest String With A Given Numeric Value.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string getSmallestString(int n, int k) { 4 | string ans(n,'a'); 5 | k=k-n; 6 | int i=n-1; 7 | while(k>0) 8 | { 9 | int t=min(25,k); 10 | ans[i]+=t; 11 | k=k-t; 12 | i--; 13 | } 14 | return ans; 15 | 16 | 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /March LeetCode Challenge/392. Is Subsequence.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isSubsequence(string s, string t) { 4 | int i, j; 5 | 6 | i= j = 0; 7 | 8 | while (s[i] != '\0') { 9 | while ((s[i] != t[j]) && t[j] != '\0') { 10 | j++; 11 | } 12 | if (t[j] == '\0') 13 | break; 14 | i++; 15 | j++; 16 | } 17 | if (s[i] == '\0') 18 | return 1; 19 | else 20 | return 0; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /March LeetCode Challenge/410. Split Array Largest Sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int splitArray(vector& nums, int m) { 4 | 5 | int l=0,r=0; 6 | for(auto x:nums) 7 | { 8 | l=max(x,l); 9 | r+=x; 10 | } 11 | while(l<=r) 12 | { 13 | int mid=l+(r-l)/2; 14 | 15 | int totals=0,count=0; 16 | for(auto i:nums) 17 | { 18 | if(totals+i>mid) 19 | { 20 | totals=0; 21 | count++; 22 | } 23 | totals+=i; 24 | if(count>m) 25 | { 26 | break; 27 | } 28 | } 29 | if(count& nums) { 4 | int n=nums.size(); 5 | int curr=1,diff,total=0; 6 | if(n==1) 7 | { 8 | return 0; 9 | } 10 | diff=nums[1]-nums[0]; 11 | for(int i=2;i=2) 17 | { 18 | total+=(curr-1); 19 | } 20 | } 21 | else 22 | { 23 | diff=nums[i]-nums[i-1]; 24 | curr=1; 25 | } 26 | } 27 | return total; 28 | 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /March LeetCode Challenge/704. Binary Search.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int search(vector& nums, int target) { 4 | int l=0,r=nums.size(); 5 | int p=l+(r-l)/2; 6 | while(ltarget) 18 | { 19 | r--; 20 | } 21 | } 22 | return -1; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /March LeetCode Challenge/71. Simplify Path.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string simplifyPath(string path) { 4 | vectorv; 5 | istringstream iss(path); 6 | string str; 7 | while(getline(iss,str,'/')) 8 | { 9 | if( str == "..") 10 | { 11 | if(v.size()) 12 | { 13 | v.pop_back(); 14 | } 15 | } 16 | else if(str.size()&&str!=".") 17 | { 18 | v.push_back(str); 19 | } 20 | } 21 | 22 | string ans; 23 | for(auto &x:v) 24 | { 25 | ans+='/'+x; 26 | } 27 | return ans.size()?ans:"/"; 28 | 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /March LeetCode Challenge/74. Search a 2D Matrix.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool searchMatrix(vector>& matrix, int target) { 4 | 5 | int r=0,c=matrix[0].size()-1; 6 | while(r<=matrix.size()-1 && c>=0) 7 | { 8 | if(matrix[r][c]==target) 9 | { 10 | return 1; 11 | } 12 | else if(matrix[r][c]& nums) { 4 | vectorfreq(10001); 5 | for(int i=0;i partitionLabels(string s) { 4 | 5 | maplast_index; 6 | int n=s.size(); 7 | for(int i=0;ians; 12 | int start=0,maxindex=0; 13 | for(int i=0;ilist; 12 | list.push_back((double)poured); 13 | for(int i=1;i<=query_row;i++) 14 | { 15 | 16 | vectortemp; 17 | temp.push_back(max((list[0]-1)/2,0.0)); 18 | for(int j=1;j& nums, int target) { 4 | int left=0,right=nums.size()-1; 5 | while(left<=right) 6 | { 7 | if(nums[left]==target || nums[right]==target) 8 | { 9 | return true; 10 | } 11 | else if(nums[left]target) 16 | { 17 | right--; 18 | } 19 | else 20 | { 21 | return false; 22 | } 23 | } 24 | return false; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /March LeetCode Challenge/82. Remove Duplicates from Sorted List II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | ListNode* deleteDuplicates(ListNode* head) { 4 | if(head==NULL || head->next==NULL) 5 | { 6 | return head; 7 | } 8 | 9 | ListNode*p=new ListNode(-1); 10 | p->next=head; 11 | ListNode*head1=p; 12 | while(p->next!=NULL && p->next->next!=NULL) 13 | { 14 | ListNode*p1=p->next; 15 | ListNode*p2=p->next->next; 16 | 17 | if(p1->val==p2->val) 18 | { 19 | ListNode*temp=p2; 20 | while(temp!=NULL&&p1->val==temp->val) 21 | { 22 | temp=temp->next; 23 | } 24 | p->next=temp; 25 | } 26 | else 27 | { 28 | p=p->next; 29 | } 30 | } 31 | return head1->next; 32 | 33 | } 34 | }; 35 | -------------------------------------------------------------------------------- /March LeetCode Challenge/856. Score of Parentheses.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int scoreOfParentheses(string s) { 4 | stackst; 5 | int score=0; 6 | for(auto ch:s) 7 | { 8 | if(ch=='(') 9 | { 10 | st.push(score); 11 | score=0; 12 | } 13 | else 14 | { 15 | score=st.top()+max(2*score,1); 16 | st.pop(); 17 | } 18 | } 19 | return score; 20 | } 21 | }; 22 | 23 | -------------------------------------------------------------------------------- /March LeetCode Challenge/881. Boats to Save People.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numRescueBoats(vector& people, int limit) { 4 | 5 | sort(people.begin(),people.end()); 6 | int left=0,right=people.size()-1; 7 | int boat=0; 8 | while(left<=right) 9 | { 10 | if(people[left]+people[right]<=limit) 11 | { 12 | left++; 13 | } 14 | right--; 15 | boat++; 16 | } 17 | return boat; 18 | } 19 | }; 20 | 21 | -------------------------------------------------------------------------------- /March LeetCode Challenge/895. Maximum Frequency Stack.cpp: -------------------------------------------------------------------------------- 1 | class FreqStack { 2 | public: 3 | mapfreq; 4 | map>st; 5 | int maxfreq=0; 6 | FreqStack() { 7 | 8 | } 9 | 10 | void push(int val) { 11 | freq[val]++; 12 | st[freq[val]].push(val); 13 | maxfreq=max(maxfreq,freq[val]); 14 | 15 | } 16 | 17 | int pop() { 18 | if(st[maxfreq].empty()) 19 | { 20 | maxfreq--; 21 | } 22 | int x= st[maxfreq].top(); 23 | freq[x]--; 24 | st[maxfreq].pop(); 25 | return x; 26 | 27 | } 28 | }; 29 | 30 | /** 31 | * Your FreqStack object will be instantiated and called as such: 32 | * FreqStack* obj = new FreqStack(); 33 | * obj->push(val); 34 | * int param_2 = obj->pop(); 35 | */ 36 | -------------------------------------------------------------------------------- /March LeetCode Challenge/946. Validate Stack Sequences.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool validateStackSequences(vector& pushed, vector& popped) { 4 | 5 | int m=pushed.size(); 6 | int n=popped.size(); 7 | if(m!=n) 8 | { 9 | return false; 10 | } 11 | 12 | int i=0,j=0; 13 | stackst; 14 | while(istartValue) 6 | { 7 | if(target%2==0) 8 | { 9 | target=target/2; 10 | } 11 | else 12 | { 13 | target++; 14 | } 15 | op++; 16 | } 17 | return op+(startValue-target); 18 | 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /March LeetCode Challenge/Counting Bits.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector countBits(int n) { 4 | 5 | vectorv(n+1); 6 | v[0]=0; 7 | for(int i=1;i<=n;i++) 8 | { 9 | v[i]=1+v[i&(i-1)]; 10 | } 11 | return v; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /March LeetCode Challenge/Day30.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean searchMatrix(int[][] matrix, int target) { 3 | int r=0,c=matrix[0].length-1; 4 | while(r<=matrix.length-1 && c>=0) 5 | { 6 | if(matrix[r][c]==target) 7 | { 8 | return true; 9 | } 10 | else if(matrix[r][c]& nums) { 4 | int s=0,f=0; 5 | do{ 6 | s=nums[s]; 7 | f=nums[nums[f]]; 8 | }while(s!=f); 9 | s=0; 10 | while(s!=f) 11 | { 12 | s=nums[s]; 13 | f=nums[f]; 14 | } 15 | return s; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /March LeetCode Challenge/Rotate a Linked List .cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* rotateRight(ListNode* head, int k) { 14 | if(head==NULL) 15 | { 16 | return NULL; 17 | } 18 | int len=1; 19 | ListNode*curr=head; 20 | while(curr->next!=NULL) 21 | { 22 | len++; 23 | curr=curr->next; 24 | } 25 | ListNode*p=head; 26 | curr->next=head; 27 | int move=len-k%len-1; 28 | while(move--) 29 | { 30 | p=p->next; 31 | } 32 | ListNode*Head=p->next; 33 | p->next=NULL; 34 | return Head; 35 | 36 | } 37 | }; 38 | -------------------------------------------------------------------------------- /March LeetCode Challenge/kWeakestRows.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector kWeakestRows(vector>& mat, int k) { 4 | set>s; 5 | for(int i=0;iv; 18 | for(auto x=begin(s);k>0;k--,x++) 19 | { 20 | v.push_back(x->second); 21 | } 22 | return v; 23 | 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /May Leetcode Challenge/1209. Remove All Adjacent Duplicates in String II Method 2.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string removeDuplicates(string s, int k) { 4 | vector> counts; 5 | for (int i = 0; i < s.size(); ++i) { 6 | if (counts.empty() || s[i] != counts.back().first) { 7 | counts.push_back({s[i],1 }); 8 | } else if (++counts.back().second == k) { 9 | counts.pop_back(); 10 | } 11 | } 12 | s = ""; 13 | for (auto &x : counts) { 14 | s += string(x.second, x.first); 15 | } 16 | return s; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /May Leetcode Challenge/1209. Remove All Adjacent Duplicates in String II Method1.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string removeDuplicates(string s, int k) { 4 | stack counts; 5 | for (int i = 0; i < s.size(); ++i) { 6 | if (i == 0 || s[i] != s[i - 1]) { 7 | counts.push(1); 8 | } else if (++counts.top() == k) { 9 | counts.pop(); 10 | s.erase(i - k + 1, k); 11 | i = i - k; 12 | } 13 | } 14 | return s; 15 | 16 | 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /May Leetcode Challenge/1641. Count Sorted Vowel Strings.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countVowelStrings(int n) { 4 | vector> matrix(n+1, vector(5, 0)); 5 | if(n == 1) return 5; 6 | 7 | for(int j = 0; j <5 ; j++) matrix[0][j] = 1; 8 | 9 | for(int i = 1; i <= n; i++) for(int j = 0 ; j <5; j++) 10 | 11 | matrix[i][j] = accumulate(matrix[i-1].begin(), matrix[i-1].begin() + j+1, 0); 12 | 13 | return matrix[n][4]; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /May Leetcode Challenge/1679. Max Number of K-Sum Pairs.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxOperations(vector& nums, int k) { 4 | sort(nums.begin(),nums.end()); 5 | int i=0,j=nums.size()-1; 6 | int count=0; 7 | while(i& nums) { 4 | int n = nums.size(); 5 | vector minarr(n); 6 | minarr[0] = nums[0]; 7 | for(int i = 1; i st; 11 | for(int j = n - 1; j>=0; j--){ 12 | while(!st.empty() && st.top() <= minarr[j]) st.pop(); 13 | if(!st.empty() && st.top() < nums[j]){ 14 | return true; 15 | } 16 | st.push(nums[j]); 17 | } 18 | return false; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /May Leetcode Challenge/46. Permutations.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector>ans; 4 | void permutation(vector&nums,int l,int r) 5 | { 6 | if(l==nums.size()) 7 | { 8 | ans.push_back(nums); 9 | return; 10 | } 11 | for(int i=l;i<=r;i++) 12 | { 13 | swap(nums[i],nums[l]); 14 | permutation(nums,l+1,r); 15 | swap(nums[i],nums[l]); 16 | } 17 | } 18 | vector> permute(vector& nums) { 19 | permutation(nums,0,nums.size()-1); 20 | return ans; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /May Leetcode Challenge/47. Permutations II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector>ans; 4 | set>s; 5 | void permutation(vector&nums,int l,int r) 6 | { 7 | if(l==nums.size() && s.find(nums)==s.end()) 8 | { 9 | ans.push_back(nums); 10 | s.insert(nums); 11 | return; 12 | } 13 | for(int i=l;i<=r;i++) 14 | { 15 | swap(nums[i],nums[l]); 16 | permutation(nums,l+1,r); 17 | swap(nums[i],nums[l]); 18 | } 19 | } 20 | vector> permuteUnique(vector& nums) { 21 | permutation(nums,0,nums.size()-1); 22 | return ans; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /May Leetcode Challenge/581. Shortest Unsorted Continuous Subarray.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findUnsortedSubarray(vector& nums) { 4 | int n=nums.size(); 5 | int l=0,r=n-1,maxn=0,minn=n-1; 6 | for(int i=0;inums[maxn]) 9 | { 10 | maxn=i; 11 | } 12 | else if(nums[maxn]>nums[i]) 13 | { 14 | l=i; 15 | } 16 | if(nums[n-i-1]nums[minn]) 21 | { 22 | r=n-i-1; 23 | } 24 | } 25 | if(r>=l) 26 | { 27 | return 0; 28 | } 29 | return l-r+1; 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /May Leetcode Challenge/Day21.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int dp[10001]; 4 | 5 | int solve(vector& coins, int amount) 6 | { 7 | int ans=10001; 8 | 9 | if(amount==0) 10 | return 0; 11 | 12 | if(dp[amount]!=-1) 13 | return dp[amount]; 14 | 15 | for(int i=0;i=0) 18 | ans=min(ans,solve(coins,amount-coins[i])+1); 19 | } 20 | 21 | return dp[amount]=ans; 22 | 23 | } 24 | 25 | int coinChange(vector& coins, int amount) { 26 | 27 | memset(dp,-1,sizeof(dp)); 28 | 29 | int res=solve(coins,amount); 30 | 31 | if(res==10001) 32 | return -1; 33 | return res; 34 | } 35 | 36 | }; 37 | -------------------------------------------------------------------------------- /May Leetcode Challenge/Day22.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countSubstrings(string s) { 4 | 5 | int n=s.size(); 6 | 7 | int dp[n][n]; 8 | memset(dp, 0, sizeof(dp)); 9 | int count=0; 10 | for(int i=0;i& a,vector& b){ 4 | if(a[0]==b[0]) 5 | return a[1]>b[1]; 6 | return a[0]>& envelopes) { 9 | sort(envelopes.begin(),envelopes.end(),cmp); 10 | vector dp; 11 | for (auto& env : envelopes) { 12 | int height = env[1]; 13 | int left = lower_bound(dp.begin(), dp.end(), height) - dp.begin(); 14 | if (left == dp.size()) dp.push_back(height); 15 | dp[left] = height; 16 | } 17 | return dp.size(); 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /May Leetcode Challenge/Day26.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int hammingWeight(uint32_t n) { 4 | int count=0; 5 | while(n>0) 6 | { 7 | count+=(n&1); 8 | n>>=1; 9 | } 10 | return count; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /May Leetcode Challenge/Day27.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numberOfSteps(int num) { 4 | int count=0; 5 | while(num) 6 | { 7 | if(num&1) num-=1; 8 | else num >>= 1; 9 | count++; 10 | } 11 | return count; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /May Leetcode Challenge/Day28.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int missingNumber(vector& nums) { 4 | int n=0; 5 | int result=nums.size(); 6 | for(auto x:nums) 7 | { 8 | result=result^x; 9 | result=result^n; 10 | n++; 11 | } 12 | return result; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /May Leetcode Challenge/Day30.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int divide(int dividend, int divisor) { 4 | 5 | if(dividend==INT_MIN && divisor==-1) return INT_MAX; 6 | if(dividend==INT_MIN && divisor==1) return INT_MIN; 7 | 8 | 9 | 10 | long int dd = abs(dividend), dv = abs(divisor); 11 | 12 | 13 | int res=0; 14 | while(dv<=dd) { 15 | long int sum=dv, count=1; 16 | while(sum<=dd-sum) { 17 | sum+=sum; 18 | count+=count; 19 | } 20 | res+=count; 21 | dd-=sum; 22 | } 23 | 24 | if((dividend<0&&divisor>0) || (dividend>0&&divisor<0)) return -res; 25 | 26 | return res; 27 | 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /May Leetcode Challenge/Day31.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool hasAllCodes(string s, int k) { 4 | int n = s.size(); 5 | unordered_set set; 6 | for(int i = 0; i <= n - k; i++) 7 | set.insert(s.substr(i, k)); 8 | return set.size() == (1 << k); 9 | } 10 | }; 11 | -------------------------------------------------------------------------------- /May Leetcode Challenge/Max Number of K-Sum Pairs.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxOperations(vector& nums, int k) { 4 | unordered_mapmp; 5 | int count=0; 6 | for(auto x:nums) 7 | { 8 | if(mp[k-x]) 9 | { 10 | mp[(k-x)]--; 11 | mp[x]--; 12 | count++; 13 | } 14 | mp[x]++; 15 | } 16 | return count; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /May Leetcode Challenge/combinationSum3.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector>subsetss; 4 | void generate(vector&subset,int i,int k,int n) 5 | { 6 | if(k==subset.size() && n==0) 7 | { 8 | subsetss.push_back(subset); 9 | return; 10 | } 11 | if (i >= 10 || (n != 0 && i > n)) 12 | return; 13 | generate(subset,i+1,k,n); 14 | subset.push_back(i); 15 | generate(subset,i+1,k,n-i); 16 | subset.pop_back(); 17 | 18 | } 19 | 20 | vector> combinationSum3(int k, int n) { 21 | vectorsubset; 22 | generate(subset,1,k,n); 23 | return subsetss; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /May Leetcode Challenge/sortArrayByParity.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector sortArrayByParity(vector&nums) { 4 | int n=nums.size(); 5 | int i=0,j=n-1; 6 | while(ist; 5 | st.push(-1); 6 | int maxl=0; 7 | for(int i=0;i>subsetss; 4 | void generate(vector&subset,int i,vector&nums) 5 | { 6 | if(i==nums.size()) 7 | { 8 | subsetss.push_back(subset); 9 | return; 10 | } 11 | //not included 12 | generate(subset,i+1,nums); 13 | //include 14 | subset.push_back(nums[i]); 15 | generate(subset,i+1,nums); 16 | subset.pop_back(); 17 | } 18 | 19 | vector> subsets(vector& nums) { 20 | vectorsubset; 21 | generate(subset,0,nums); 22 | return subsetss; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /May Leetcode Challenge/uniquePaths.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int uniquePaths(int m, int n) { 4 | vector>dp(m,vector(n)); 5 | for(int i=0;i& nums1, int m, vector& nums2, int n) { 4 | while(n>0||m>0) 5 | { 6 | if(m>0 and (n==0 or (nums1[m-1]>nums2[n-1]))) 7 | { 8 | nums1[m+n-1]=nums1[m-1]; 9 | m--; 10 | } 11 | else 12 | { 13 | nums1[m+n-1]=nums2[n-1]; 14 | n--; 15 | } 16 | } 17 | 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /Microsoft/Easy/1822. Sign of the Product of an Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int arraySign(vector& nums) { 4 | int c=0; 5 | for(auto x:nums) 6 | { 7 | if(x==0) 8 | { 9 | return 0; 10 | } 11 | if(x<0) 12 | { 13 | c++; 14 | } 15 | } 16 | return c%2==0?1:-1; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /Microsoft/Medium/1448. Count Good Nodes in Binary Tree.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int m=INT_MIN; 4 | int count=0; 5 | void helper(TreeNode*root,int m) 6 | { 7 | if(!root)return; 8 | if(root->val>=m) 9 | { 10 | count++; 11 | m=root->val; 12 | } 13 | helper(root->left,m); 14 | helper(root->right,m); 15 | 16 | } 17 | int goodNodes(TreeNode* root) { 18 | helper(root,m); 19 | return count; 20 | 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /My Calendar I Solution.cpp: -------------------------------------------------------------------------------- 1 | class MyCalendar { 2 | public: 3 | vector>v; 4 | MyCalendar() { 5 | 6 | } 7 | 8 | bool book(int start, int end) { 9 | for(auto [s,e]:v) 10 | { 11 | if(max(s,start)> levelOrder(Node* root) { 4 | vector>v; 5 | if(root==NULL) 6 | { 7 | return v; 8 | } 9 | queueq; 10 | q.push(root); 11 | while(!q.empty()) 12 | { 13 | vectorvec; 14 | int n=q.size(); 15 | for(int i=0;ival); 20 | for(auto x:temp->children) 21 | { 22 | q.push(x); 23 | } 24 | 25 | } 26 | v.push_back(vec); 27 | 28 | } 29 | return v; 30 | 31 | } 32 | }; 33 | -------------------------------------------------------------------------------- /November_Leetcode_Challenge_2022/Day1/1706._Where_Will _the_Ball_Fall.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] findBall(int[][] grid) { 3 | int col=grid[0].length; 4 | int []ans= new int[col]; 5 | for(int j=0;j=grid.length){ 12 | return j; 13 | } 14 | if(grid[i][j]==1 && j+1=0 && grid[i][j-1]==-1){ 18 | return dfs(i+1,j-1,grid); 19 | } 20 | else if(grid[i][j]==1 && j+1>=grid[0].length){ 21 | return -1; 22 | } 23 | else{ 24 | return -1; 25 | } 26 | // return -1; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /November_Leetcode_Challenge_2022/Day4/reverse_vowels_of_a_string.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isVowel(char c) { 4 | return c == 'a' || c == 'i' || c == 'e' || c == 'o' || c == 'u' 5 | || c == 'A' || c == 'I' || c == 'E' || c == 'O' || c == 'U'; 6 | } 7 | string reverseVowels(string s) { 8 | int i = 0,j=s.size()-1; 9 | while(i&dp) 4 | { 5 | if(s[i]=='0') 6 | { 7 | return 0; 8 | } 9 | if(i==s.size()) 10 | { 11 | return 1; 12 | } 13 | if(dp[i]!=-1) 14 | { 15 | return dp[i]; 16 | } 17 | int one = find(i+1,s,dp); 18 | int two=0; 19 | if(i < s.size() -1 && (s[i] == '1' || s[i] == '2' && s[i+1] <= '6')) 20 | { 21 | two+=find(i+2,s,dp); 22 | } 23 | return dp[i] = one+two; 24 | } 25 | int numDecodings(string s) { 26 | vectordp(s.size()+1,-1); 27 | return find(0,s,dp); 28 | 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /October leetcode challenge/Day10/1328. Break a Palindrome.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string breakPalindrome(string palindrome) { 4 | int n = palindrome.size(); 5 | if(n <= 1) return ""; 6 | 7 | for(int i=0; i& nums) { 4 | int first = INT_MAX,second = INT_MAX; 5 | for(auto x: nums) 6 | { 7 | if(x second) 16 | { 17 | return true; 18 | } 19 | } 20 | return false; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /October leetcode challenge/Day12/976. Largest Perimeter Triangle.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int largestPerimeter(vector& nums) { 4 | sort(nums.begin(),nums.end()); 5 | for(int i=nums.size()-3;i>=0;i--) 6 | { 7 | if(nums[i]+nums[i+1]>nums[i+2]) 8 | { 9 | return nums[i]+nums[i+1]+nums[i+2]; 10 | } 11 | } 12 | return 0; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /October leetcode challenge/Day13/237. Delete Node in a Linked List.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode(int x) : val(x), next(NULL) {} 7 | * }; 8 | */ 9 | class Solution { 10 | public: 11 | void deleteNode(ListNode* node) { 12 | node->val = node->next->val; 13 | node->next = node->next->next; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /October leetcode challenge/Day15/1531. String Compression II with Recursion.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string str; 4 | int solve(int i, int prev, int len, int k ){ 5 | if(k<0)return INT_MAX; 6 | if(i>=str.size())return 0; 7 | int del = solve(i+1,prev, len,k-1); 8 | int keep=0; 9 | if(str[i]-'a'==prev){ 10 | if(len==1||len==9||len==99) 11 | { 12 | keep+=1; 13 | } 14 | keep = keep + solve(i+1,prev, len+1, k); 15 | } 16 | else { 17 | keep=1+solve(i+1, str[i]-'a', 1, k); 18 | } 19 | return min(del,keep); 20 | } 21 | int getLengthOfOptimalCompression(string s, int k) { 22 | str=s; 23 | return solve(0, 26, 0, k); 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /October leetcode challenge/Day16/1335. Minimum Difficulty of a Job Schedule/memoization.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int dp[301][11]; 4 | int solve(vector& jobd,int n,int i, int d) 5 | { 6 | if(d==1) 7 | { 8 | return *max_element(begin(jobd)+i, end(jobd)); 9 | } 10 | if(dp[i][d]!=-1) 11 | { 12 | return dp[i][d]; 13 | } 14 | int result = INT_MAX; 15 | int currD = INT_MIN; 16 | for(int j=i;j<=n-d;j++) 17 | { 18 | currD=max(currD,jobd[j]); 19 | result = min(result,currD + solve(jobd,n,j+1,d-1)); 20 | } 21 | dp[i][d]=result; 22 | return dp[i][d]; 23 | } 24 | int minDifficulty(vector& jobDifficulty, int d) { 25 | memset(dp,-1,sizeof(dp)); 26 | int n = jobDifficulty.size(); 27 | if(n < d) 28 | return -1; 29 | return solve(jobDifficulty,n,0,d); 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /October leetcode challenge/Day16/1335. Minimum Difficulty of a Job Schedule/recursion.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | int solve(vector& jobd,int n,int i, int d) 5 | { 6 | if(d==1) 7 | { 8 | return *max_element(begin(jobd)+i, end(jobd)); 9 | } 10 | 11 | int result = INT_MAX; 12 | int currD = INT_MIN; 13 | for(int j=i;j<=n-d;j++) 14 | { 15 | currD=max(currD,jobd[j]); 16 | result = min(result,currD + solve(jobd,n,j+1,d-1)); 17 | } 18 | 19 | return result; 20 | } 21 | int minDifficulty(vector& jobDifficulty, int d) { 22 | int n = jobDifficulty.size(); 23 | if(n < d) 24 | return -1; 25 | return solve(jobDifficulty,n,0,d); 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /October leetcode challenge/Day17/bit_manipulation.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool checkIfPangram(string sentence) { 4 | int tsb = 0; 5 | for(auto x:sentence) 6 | { 7 | int ch = x-'a'; 8 | int bits = 1<& nums, int k) { 4 | unordered_mapmp; 5 | for(int i=0;i0 && abs(mp[nums[i]]-i)<=k) 8 | { 9 | return true; 10 | } 11 | mp[nums[i]] = i; 12 | } 13 | return false; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /October leetcode challenge/Day26/Continuous Subarray Sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool checkSubarraySum(vector& nums, int k) { 4 | unordered_mapmp; 5 | mp[0] = 0; 6 | int sum = 0; 7 | for(int i=0;i> groupAnagrams(vector& strs) { 4 | 5 | vector>ans; 6 | unordered_map>mp; 7 | 8 | for(auto str : strs) 9 | { 10 | string s = str; 11 | sort(s.begin(),s.end()); 12 | mp[s].push_back(str); 13 | } 14 | for(auto x:mp) 15 | { 16 | ans.push_back(x.second); 17 | } 18 | 19 | 20 | return ans; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /October leetcode challenge/Day29/2136-Earliest-Possible-Day-of-Full-Bloom.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int earliestFullBloom(int[] plantTime, int[] growTime) { 3 | ArrayList> pair=new ArrayList<>(); 4 | for(int i=0;i>() { 8 | @Override 9 | public int compare(Pair p1, Pair p2) { 10 | return Integer.compare(p2.getValue(),p1.getValue()); 11 | } 12 | }); 13 | int start=0; 14 | int ans=0; 15 | for(Pair temp:pair){ 16 | start+=temp.getKey(); 17 | ans=Math.max(ans,start+temp.getValue()); 18 | } 19 | return ans; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /October leetcode challenge/Day29/Earliest Possible Day of Full Bloom.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | static bool comp(pairp1,pairp2){ 4 | 5 | return p1.second > p2.second; 6 | } 7 | int earliestFullBloom(vector& plantTime, vector& growTime) { 8 | 9 | int ans =0,start=0; 10 | vector>pair; 11 | 12 | for(int i=0;i& neededTime) { 4 | int i = 0,j = 0; 5 | int totalTime = 0; 6 | int n = colors.size(); 7 | while(imp; 4 | int maxCount = 0; 5 | MyCalendarThree() { 6 | 7 | } 8 | 9 | int book(int start, int end) { 10 | mp[start]++; 11 | mp[end]--; 12 | int count = 0; 13 | for (auto it = mp.begin(); it != mp.end(); it++) 14 | { 15 | count += it->second; 16 | maxCount = max(count, maxCount); 17 | } 18 | return maxCount; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /October leetcode challenge/Day7/732. My Calendar III.java: -------------------------------------------------------------------------------- 1 | class MyCalendarThree { 2 | TreeMapmp = new TreeMap<>(); 3 | int maxCount = 0; 4 | public MyCalendarThree() { 5 | 6 | } 7 | 8 | public int book(int start, int end) { 9 | int count = 0; 10 | mp.put(start,mp.getOrDefault(start,0)+1); 11 | mp.put(end,mp.getOrDefault(end,0)-1); 12 | for(Map.Entrye :mp.entrySet()) 13 | { 14 | int x = e.getValue(); 15 | count = count + x; 16 | maxCount = Math.max(maxCount,count); 17 | } 18 | return maxCount; 19 | } 20 | } 21 | 22 | /** 23 | * Your MyCalendarThree object will be instantiated and called as such: 24 | * MyCalendarThree obj = new MyCalendarThree(); 25 | * int param_1 = obj.book(start,end); 26 | */ 27 | -------------------------------------------------------------------------------- /October leetcode challenge/day14/2095. Delete the Middle Node of a Linked List.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* deleteMiddle(ListNode* head) { 14 | if(head->next == NULL ) 15 | { 16 | return NULL; 17 | } 18 | ListNode*fast = head->next->next; 19 | ListNode*slow = head; 20 | while(fast!=NULL && fast->next!=NULL) 21 | { 22 | fast = fast->next->next; 23 | slow = slow->next; 24 | } 25 | slow->next = slow->next->next; 26 | return head; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /October leetcode challenge/day14/deleteMiddleNode.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | ListNode* deleteMiddle(ListNode* head) { 4 | if(head->next == NULL) 5 | { 6 | return NULL; 7 | } 8 | int countNodes = 0; 9 | ListNode*temp = head; 10 | while(temp!=NULL) 11 | { 12 | temp = temp->next; 13 | countNodes++; 14 | } 15 | int n = (countNodes/2)-1; 16 | temp = head; 17 | while(n--) 18 | { 19 | temp = temp->next; 20 | } 21 | temp->next = temp->next->next; 22 | return head; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /Pascal_Triangle.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> generate(int numRows) { 4 | 5 | vector>ans(numRows); 6 | for(int i=0;i0 &&j& arr, vector>& pieces) { 4 | mapmp; 5 | for(int i=0;i plusOne(vector& digits) { 4 | int k=0; 5 | for(int i=digits.size()-1;i>=0;i--) 6 | { 7 | if(digits[i]==9) 8 | { 9 | digits[i]=0; 10 | k=1; 11 | } 12 | else 13 | { 14 | if(digits[i]+1<=9) 15 | { 16 | digits[i]+=1; 17 | k=0; 18 | return digits; 19 | } 20 | else 21 | { 22 | digits[i]=0; 23 | k=1; 24 | } 25 | } 26 | } 27 | if(k==1) 28 | { 29 | digits.insert(digits.begin(),1); 30 | } 31 | return digits; 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /Prefix Sum/1413. Minimum Value to Get Positive Step by Step Sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minStartValue(vector& nums) { 4 | int num=0,sum=0; 5 | for(auto x:nums) 6 | { 7 | sum+=x; 8 | num=min(num,sum); 9 | } 10 | return -num+1; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /Prefix Sum/1480. Running Sum of 1d Array.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @return {number[]} 4 | */ 5 | var runningSum = function(nums) { 6 | var arr=new Array(); 7 | arr[0]=nums[0]; 8 | for(var i=1;i& arr) { 4 | int ans = 0; 5 | for(int i=0;i& nums) { 4 | int m,startsum=0,ttsum=0; 5 | ttsum=accumulate(nums.begin(),nums.end(),0); 6 | 7 | 8 | for(int i=0;i& nums, int k) { 4 | int count=0,sum=0; 5 | mapmp; 6 | for(int i=0;i>ans; 4 | void findways(int index,vector&v,vector& candidates, int target,int currsum) 5 | { 6 | if(currsum>target) 7 | { 8 | return; 9 | } 10 | if(target==currsum) 11 | { 12 | ans.push_back(v); 13 | return; 14 | } 15 | for(int i=index;i> combinationSum(vector& candidates, int target) { 24 | int currsum=0; 25 | vectorv; 26 | findways(0,v,candidates,target,currsum); 27 | return ans; 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /Recursion/Delete middle element of a stack.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | //Function to delete middle element of a stack. 5 | 6 | int x=0; 7 | void deleteMid(stack&s, int sizeOfStack) 8 | { 9 | if(x==sizeOfStack/2) 10 | { 11 | s.pop(); 12 | return; 13 | } 14 | int val=s.top(); 15 | x+=1; 16 | s.pop(); 17 | deleteMid(s,sizeOfStack); 18 | s.push(val); 19 | // code here.. 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /Recursion/Height of Binary Tree.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val, left, right) { 4 | * this.val = (val===undefined ? 0 : val) 5 | * this.left = (left===undefined ? null : left) 6 | * this.right = (right===undefined ? null : right) 7 | * } 8 | */ 9 | /** 10 | * @param {TreeNode} root 11 | * @return {number} 12 | */ 13 | var maxDepth = function(root) { 14 | if(root==null) 15 | { 16 | return 0; 17 | } 18 | return 1 + Math.max(maxDepth(root.left),maxDepth(root.right)); 19 | 20 | }; 21 | -------------------------------------------------------------------------------- /Recursion/Sort (Asc) An Stack Recursion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void insert(stack&arr,int val) 4 | { 5 | if(arr.size()==0 || arr.top()>=val) 6 | { 7 | arr.push(val); 8 | return; 9 | } 10 | int v = arr.top(); 11 | arr.pop(); 12 | insert(arr,val); 13 | arr.push(v); 14 | } 15 | stack sortStack(stack& nums) { 16 | if(nums.size()==1) 17 | { 18 | return nums; 19 | } 20 | int val=nums.top(); 21 | nums.pop(); 22 | sortStack(nums); 23 | insert(nums,val); 24 | return nums; 25 | 26 | } 27 | int main() { 28 | stacknums; 29 | int n; 30 | cin>>n; 31 | for(int i=0;i>t; 35 | nums.push(t); 36 | } 37 | 38 | stackv=sortStack(nums); 39 | for(int i=0;i>subsets; 4 | void generate(vector&subset,int i,vector&nums) 5 | { 6 | if(i==nums.size()) 7 | { 8 | subsets.push_back(subset); 9 | return; 10 | } 11 | generate(subset,i+1,nums); 12 | subset.push_back(nums[i]); 13 | generate(subset,i+1,nums); 14 | subset.pop_back(); 15 | } 16 | vector> subsetsWithDup(vector& nums) { 17 | vectorv; 18 | generate(v,0,nums); 19 | return subsets; 20 | 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Recursion/find all subsets of string.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void solve(string i,string o) 4 | { 5 | if(i.size()==0) 6 | { 7 | cout<& prices) 4 | { 5 | if(k==0) 6 | { 7 | return 0; 8 | } 9 | if(i == prices.size()) 10 | { 11 | return 0; 12 | } 13 | int maxProfit=0; 14 | if(canBuy) 15 | { 16 | int buy = -prices[i] + findMaxProfit(i+1,0,k,prices); 17 | int notbuy = 0 + findMaxProfit(i+1,1,k,prices); 18 | maxProfit = max(buy,notbuy); 19 | } 20 | else 21 | { 22 | int sell = prices[i] + findMaxProfit(i+1,1,k-1,prices); 23 | int notsell = 0 + findMaxProfit(i+1,0,k,prices); 24 | maxProfit = max(sell,notsell); 25 | } 26 | return maxProfit; 27 | } 28 | int maxProfit(int k, vector& prices) { 29 | return findMaxProfit(0,1,k,prices); 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /September Leetcode Challenge/Day12/948. Bag of Tokens.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int bagOfTokensScore(vector& tokens, int power) { 4 | sort(tokens.begin(),tokens.end()); 5 | int score = 0; 6 | int ans = 0; 7 | int i=0,j=tokens.size()-1; 8 | while(i<=j) 9 | { 10 | if(tokens[i]<=power) 11 | { 12 | score+=1; 13 | if(ans0){ 21 | power+=tokens[j]; 22 | score-=1; 23 | j--; 24 | } 25 | else 26 | { 27 | break; 28 | } 29 | } 30 | return ans; 31 | } 32 | }; 33 | -------------------------------------------------------------------------------- /September Leetcode Challenge/Day15/2007. Find Original Array From Doubled Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector findOriginalArray(vector& changed) { 4 | mapmp; 5 | vectorans; 6 | int n = changed.size(); 7 | if(n%2) 8 | { 9 | return ans; 10 | } 11 | for(auto x : changed) 12 | { 13 | mp[x]++; 14 | } 15 | sort(changed.begin(),changed.end()); 16 | for(auto x:changed) 17 | { 18 | if(mp[x]==0) 19 | { 20 | continue; 21 | } 22 | if(mp[2*x]==0) 23 | { 24 | return {}; 25 | } 26 | if(mp[x] && mp[2*x]) 27 | { 28 | mp[2*x]--; 29 | ans.push_back(x); 30 | mp[x]--; 31 | } 32 | } 33 | 34 | return ans; 35 | } 36 | }; 37 | -------------------------------------------------------------------------------- /September Leetcode Challenge/Day16/1770. Maximum Score from Performing Multiplication Operations/recursion.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findScore(int i,int left,int right, vector& nums, vector& multipliers) 4 | { 5 | if(i==multipliers.size()) 6 | { 7 | return 0; 8 | } 9 | 10 | int l = multipliers[i]*nums[left]+findScore(i+1,left+1,right,nums,multipliers); 11 | int r = multipliers[i]*nums[right] + findScore(i+1,left,right-1,nums,multipliers); 12 | 13 | return max(l,r); 14 | } 15 | int maximumScore(vector& nums, vector& multipliers) { 16 | int n=nums.size(); 17 | return findScore(0,0,n-1,nums,multipliers); 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /September Leetcode Challenge/Day18/42. Trapping Rain Water.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int trap(vector& height) { 4 | int size=height.size(); 5 | if(size==0) 6 | { 7 | return 0; 8 | } 9 | vectormaxl(size); 10 | vectormaxr(size); 11 | maxl[0]=height[0]; 12 | for(int i=1;i=0;i--) 18 | { 19 | maxr[i]=max(maxr[i+1],height[i]); 20 | } 21 | int water=0; 22 | for(int i=0;i& nums1, vector& nums2) { 4 | int m = nums1.size(); 5 | int n = nums2.size(); 6 | int maxCount=0; 7 | for(int i=-n+1;i=m) 17 | { 18 | break; 19 | } 20 | else if(nums1[i+j]==nums2[j]) 21 | { 22 | count++;; 23 | maxCount = max(maxCount,count); 24 | } 25 | else 26 | { 27 | count =0; 28 | } 29 | } 30 | } 31 | return maxCount; 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /September Leetcode Challenge/Day21/985. Sum of Even Numbers After Queries.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector sumEvenAfterQueries(vector& nums, vector>& queries) { 4 | 5 | vectorans; 6 | 7 | int sum = 0; 8 | for(auto x :nums) 9 | { 10 | if(x%2 == 0) 11 | { 12 | sum+=x; 13 | } 14 | } 15 | for(auto x:queries) 16 | { 17 | int index = x[1]; 18 | int val = x[0]; 19 | 20 | if(nums[index]%2==0) 21 | { 22 | sum-=nums[index]; 23 | } 24 | nums[index]+=val; 25 | 26 | if(nums[index]%2==0) 27 | { 28 | sum+=nums[index]; 29 | } 30 | ans.push_back(sum); 31 | } 32 | return ans; 33 | 34 | 35 | } 36 | }; 37 | -------------------------------------------------------------------------------- /September Leetcode Challenge/Day22/557. Reverse Words in a String III.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string reverseWords(string s) { 4 | int l=0,r=0; 5 | while(l findClosestElements(vector& arr, int k, int x) { 4 | vectorans; 5 | priority_queue>pq; 6 | for(auto &a : arr){ 7 | if((pq.size() >= k) && abs(x-a) < pq.top().first) 8 | pq.pop(); 9 | else if(pq.size() >= k) 10 | continue; 11 | pq.push({abs(x-a),a}); 12 | } 13 | while(!pq.empty()){ 14 | ans.push_back(pq.top().second); 15 | pq.pop(); 16 | } 17 | sort(ans.begin(),ans.end()); 18 | return ans; 19 | 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /September Leetcode Challenge/Day3/967. Numbers With Same Consecutive Differences.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vectorans; 4 | int countDigit(int n) 5 | { 6 | int count = 0; 7 | while(n>0) 8 | { 9 | count++; 10 | n=n/10; 11 | } 12 | return count; 13 | } 14 | 15 | void findNumber(int num, int n, int k) 16 | { 17 | if(countDigit(num)==n) 18 | { 19 | ans.push_back(num); 20 | return; 21 | } 22 | for(int i=0;i<=9;i++) 23 | { 24 | int ld=num%10; 25 | if(abs(ld-i)== k) 26 | { 27 | int number = num*10+i; 28 | findNumber(number,n,k); 29 | } 30 | } 31 | } 32 | vector numsSameConsecDiff(int n, int k) { 33 | 34 | for(int i=1;i<=9;i++) 35 | { 36 | findNumber(i,n,k); 37 | } 38 | return ans; 39 | } 40 | }; 41 | -------------------------------------------------------------------------------- /September Leetcode Challenge/Day3/ans.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vectorans; 4 | int countDigit(int n) 5 | { 6 | int count = 0; 7 | while(n>0) 8 | { 9 | count++; 10 | n=n/10; 11 | } 12 | return count; 13 | } 14 | 15 | void findNumber(int num, int n, int k) 16 | { 17 | if(countDigit(num)==n) 18 | { 19 | ans.push_back(num); 20 | return; 21 | } 22 | for(int i=0;i<=9;i++) 23 | { 24 | int ld=num%10; 25 | if(abs(ld-i)== k) 26 | { 27 | findNumber(num*10+i,n,k); 28 | } 29 | } 30 | } 31 | vector numsSameConsecDiff(int n, int k) { 32 | 33 | for(int i=1;i<=9;i++) 34 | { 35 | findNumber(i,n,k); 36 | } 37 | return ans; 38 | } 39 | }; 40 | -------------------------------------------------------------------------------- /September Leetcode Challenge/Day6/814. Binary Tree Pruning.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | TreeNode* pruneTree(TreeNode* root) { 15 | 16 | if(root!=NULL) 17 | { 18 | root->left = pruneTree(root->left); 19 | root->right = pruneTree(root->right); 20 | if(root->val == 0 && !root->left && !root->right) 21 | { 22 | return NULL; 23 | } 24 | } 25 | return root; 26 | 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /September Leetcode Challenge/Day6/814. Binary Tree Pruning.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * int val; 5 | * TreeNode left; 6 | * TreeNode right; 7 | * TreeNode() {} 8 | * TreeNode(int val) { this.val = val; } 9 | * TreeNode(int val, TreeNode left, TreeNode right) { 10 | * this.val = val; 11 | * this.left = left; 12 | * this.right = right; 13 | * } 14 | * } 15 | */ 16 | class Solution { 17 | public TreeNode pruneTree(TreeNode root) { 18 | if(root!=null) 19 | { 20 | root.left = pruneTree(root.left); 21 | root.right = pruneTree(root.right); 22 | if(root.val == 0 && root.left==null && root.right==null) 23 | { 24 | return null; 25 | } 26 | } 27 | return root; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /September Leetcode Challenge/Day8/94. Binary Tree Inorder Traversal.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | void inorder(TreeNode*root , vector&ans) 15 | { 16 | if(root==NULL) 17 | { 18 | return; 19 | } 20 | inorder(root->left,ans); 21 | ans.push_back(root->val); 22 | inorder(root->right,ans); 23 | } 24 | vector inorderTraversal(TreeNode* root) { 25 | vectorans; 26 | inorder(root,ans); 27 | return ans; 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /September Leetcode Challenge/Day9/1996. The Number of Weak Characters in the Game.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool static sorted(vector&a,vector&b) 4 | { 5 | if(a[0]==b[0]) 6 | { 7 | return a[1]>b[1]; //[7,10],[7,9],[7,5] 8 | } 9 | return a[0]>& properties) { 12 | sort(properties.begin(),properties.end(),sorted); 13 | int ans = 0,maxDefVal=INT_MIN; 14 | int n = properties.size(); 15 | for(int i=n-1;i>=0;i--) 16 | { 17 | if(properties[i][1]maxDefVal) 22 | { 23 | maxDefVal = properties[i][1]; 24 | } 25 | } 26 | return ans; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /String/1614. Maximum Nesting Depth of the Parentheses.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxDepth(string s) { 4 | int count=0; 5 | int m=0; 6 | for(int i=0;i& word1, vector& word2) { 4 | string ans,ans1; 5 | for(int i=0;iv; 6 | for(int i=0;i& nums) { 6 | int n = nums.size(); 7 | int index,number; 8 | for(index=n-2;index>=0;index--) 9 | { 10 | 11 | if(nums[index]=0;j--) 26 | { 27 | if(nums[index] & nums) { 4 | int maxsum = nums[0],sum =nums[0]; 5 | for(int i=1;isum) 9 | { 10 | sum = nums[i]; 11 | } 12 | if(maxsum> generate(int numRows) { 4 | 5 | vector>ans(numRows); 6 | for(int i=0;i0 &&j>& matrix) { 4 | int m = matrix.size(); 5 | int n = matrix[0].size(); 6 | setrow; 7 | setcol; 8 | for(int i=0;i& height) { 4 | int size=height.size(); 5 | if(size==0) 6 | { 7 | return 0; 8 | } 9 | vectormaxl(size); 10 | vectormaxr(size); 11 | maxl[0]=height[0]; 12 | for(int i=1;i=0;i--) 18 | { 19 | maxr[i]=max(maxr[i+1],height[i]); 20 | } 21 | int water=0; 22 | for(int i=0;i twoSum(vector& nums, int target) { 4 | int n=nums.size(); 5 | vectorv; 6 | for(int i=0;i twoSum(vector& nums, int target) { 4 | int n=nums.size(); 5 | vectorv; 6 | for(int i=0;i& arr) { 4 | int n=arr.size(); 5 | int naxprof=0,j=0; 6 | for(int i=1;inaxprof) 10 | { 11 | naxprof=c; 12 | } 13 | if(arr[i]>& grid) { 4 | int row=grid[0].size(); 5 | int col=grid.size(); 6 | int i=0; 7 | int j=row-1; 8 | int count=0; 9 | while(j>=0&&i>s; 10 | s.push({root,sum-root->val}); 11 | while(!s.empty()) 12 | { 13 | pairtemp=s.top(); 14 | s.pop(); 15 | if(temp.first->left==NULL&&temp.first->right==NULL&&temp.second==0) 16 | { 17 | return true; 18 | } 19 | if(temp.first->left) 20 | { 21 | s.push({temp.first->left,temp.second-temp.first->left->val}); 22 | } 23 | if(temp.first->right) 24 | { 25 | s.push({temp.first->right,temp.second-temp.first->right->val}); 26 | } 27 | 28 | 29 | 30 | } 31 | return false; 32 | 33 | } 34 | }; 35 | -------------------------------------------------------------------------------- /find-positive-integer-solution-for-a-given-equation.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * // This is the custom function interface. 3 | * // You should not implement it, or speculate about its implementation 4 | * class CustomFunction { 5 | * public: 6 | * // Returns f(x, y) for any given positive integers x and y. 7 | * // Note that f(x, y) is increasing with respect to both x and y. 8 | * // i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1) 9 | * int f(int x, int y); 10 | * }; 11 | */ 12 | 13 | class Solution { 14 | public: 15 | vector> findSolution(CustomFunction& customfunction, int z) { 16 | vector>v; 17 | for(int i=1;i<=z;i++) 18 | { 19 | for(int j=1;j<=z;j++) 20 | { 21 | int a=customfunction.f(i,j); 22 | if(a==z) 23 | { 24 | v.push_back({i,j}); 25 | } 26 | } 27 | } 28 | return v; 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /maximum-subarray.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxSubArray(vector& nums) { 4 | int meh=0,msf=INT_MIN; 5 | for(int i=0;imsf) 13 | { 14 | msf=meh; 15 | } 16 | } 17 | return msf; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /merge-sorted-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void merge(vector& nums1, int m, vector& nums2, int n) { 4 | int j=0; 5 | for(int i=m;i getRow(int rowIndex) { 4 | 5 | vector> result(rowIndex+1); 6 | 7 | for(int i = 0; i < rowIndex+1; ++i) 8 | for(int j = 0; j <= i ; ++j) 9 | result[i].emplace_back( 0 < j && j < i ? result[i-1][j-1] + result[i-1][j] : 1); 10 | vectorv(result[rowIndex].begin(),result[rowIndex].end()); 11 | return v; 12 | 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /pascals-triangle.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> generate(int numRows) { 4 | vector>v; 5 | vector> result(numRows); 6 | 7 | for(int i = 0; i < numRows; ++i) 8 | for(int j = 0; j <= i ; ++j) 9 | result[i].emplace_back( 0 < j && j < i ? result[i-1][j-1] + result[i-1][j] : 1); 10 | 11 | return result; 12 | 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /peak index in a mountain array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int peakIndexInMountainArray(vector& arr) { 4 | int l=0; 5 | int r=arr.size(); 6 | while(l& nums) { 4 | sets(nums.begin(),nums.end()); 5 | nums.assign(s.begin(),s.end()); 6 | return nums.size(); 7 | 8 | } 9 | }; 10 | -------------------------------------------------------------------------------- /remove-element.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int removeElement(vector& nums, int val) { 4 | int i=0; 5 | for(auto x:nums) 6 | { 7 | if(x!=val) 8 | { 9 | nums[i++]=x; 10 | } 11 | 12 | } 13 | return i; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /search-insert-position.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int searchInsert(vector& nums, int target) { 4 | int n=nums.size(); 5 | if(n<1) 6 | { 7 | return 0; 8 | } 9 | int l=0,r=n; 10 | while(l> subsetsWithDup(vector& nums) { 4 | set> res; 5 | int n = nums.size(); 6 | for(int mask=0;mask<(1< temp; 8 | for(int i=0;i> ans; 15 | for(auto s:res){ 16 | vector temp(s.begin(),s.end()); 17 | ans.push_back(temp); 18 | } 19 | return ans; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /valid anagram.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isAnagram(string s, string t) { 4 | int n1=s.size(); 5 | int n2=t.size(); 6 | if(n1!=n2) 7 | { 8 | return false; 9 | } 10 | int count[26]={0}; 11 | for(int i=0;i