├── Easy ├── 1-Two Sum.cpp ├── 100-Same Tree.cpp ├── 1002-Find Common Characters.cpp ├── 1005-Maximize Sum Of Array After K Negations.cpp ├── 101-Symmetric Tree.cpp ├── 1013-Partition Array Into Three Parts With Equal Sum.cpp ├── 1021-Remove Outermost Parentheses.cpp ├── 1037-Valid Boomerang.cpp ├── 104-Maximum Depth of Binary Tree.cpp ├── 1046-Last Stone Weight.cpp ├── 1047-Remove All Adjacent Duplicates In String.cpp ├── 1051-Height Checker.cpp ├── 107-Binary Tree Level Order Traversal II.cpp ├── 1071-Greatest Common Divisor of Strings.cpp ├── 1078-Occurrences After Bigram.cpp ├── 1089-Duplicate Zeros.cpp ├── 110-Balanced Binary Tree.cpp ├── 1103-Distribute Candies to People.cpp ├── 111-Minimum Depth of Binary Tree.cpp ├── 1114-Print in Order.cpp ├── 112-Path Sum.cpp ├── 1122-Relative Sort Array.cpp ├── 1128-Number of Equivalent Domino Pairs.cpp ├── 1154-Day of the Year.cpp ├── 1160-Find Words That Can Be Formed by Characters.cpp ├── 1179-Reformat Department Tabl.frm ├── 118-Pascal's Triangle.cpp ├── 1185-Day of the Week.cpp ├── 1189-Maximum Number of Balloons.cpp ├── 119-Pascal's Triangle II.cpp ├── 1200-Minimum Absolute Difference.cpp ├── 1207-Unique Number of Occurrences.cpp ├── 121-Best Time to Buy and Sell Stock.cpp ├── 1217-Minimum Cost to Move Chips to The Same Position.cpp ├── 122-Best Time to Buy and Sell Stock II.cpp ├── 1232-Check If It Is a Straight Line.cpp ├── 1237-Find Positive Integer Solution for a Given Equation.cpp ├── 125-Valid Palindrome.cpp ├── 1266-Minimum Time Visiting All Points.cpp ├── 1275-Find Winner on a Tic Tac Toe Game.cpp ├── 1281-Subtract the Product and Sum of Digits of an Integer.cpp ├── 1287-Element Appearing More Than 25% In Sorted Array.cpp ├── 1290-Convert Binary Number in a Linked List to Integer.cpp ├── 1299-Replace Elements with Greatest Element on Right Side.cpp ├── 13-Roman to Integer.cpp ├── 1313-Decompress Run-Length Encoded List.cpp ├── 1317-Convert Integer to the Sum of Two No-Zero Integers.cpp ├── 1323-Maximum 69 Number.cpp ├── 1331-Rank Transform of an Array.cpp ├── 1332-Remove Palindromic Subsequences.cpp ├── 1337-The K Weakest Rows in a Matrix.cpp ├── 1342-Number of Steps to Reduce a Number to Zero.cpp ├── 1346-Check If N and Its Double Exist.cpp ├── 1356-Sort Integers by The Number of 1 Bits.cpp ├── 1360-Number of Days Between Two Dates.cpp ├── 1370-Increasing Decreasing String.cpp ├── 1374-Generate a String With Characters That Have Odd Counts.cpp ├── 1380-Lucky Numbers in a Matrix.cpp ├── 1385-Find the Distance Value Between Two Arrays.cpp ├── 1399-Count Largest Group.cpp ├── 14-Longest Common Prefix.cpp ├── 1403-Minimum Subsequence in Non-Increasing Order.cpp ├── 1408-String Matching in an Array.cpp ├── 1413-Minimum Value to Get Positive Step by Step Sum.cpp ├── 1417-Reformat The String.cpp ├── 1422-Maximum Score After Splitting a String.cpp ├── 1437-Check If All 1's Are at Least Length K Places Away.cpp ├── 1441-Build an Array With Stack Operations.cpp ├── 1446-Consecutive Characters.cpp ├── 1450-Number of Students Doing Homework at a Given Time.cpp ├── 1455-Check If a Word Occurs As a Prefix of Any Word in a Sentence.cpp ├── 1460-Make Two Arrays Equal by Reversing Sub-arrays.cpp ├── 1464-Maximum Product of Two Elements in an Array.cpp ├── 1470-Shuffle the Array.cpp ├── 1475-Final Prices With a Special Discount in a Shop.cpp ├── 1480-Running Sum of 1d Array.cpp ├── 1486-XOR Operation in an Array.cpp ├── 1491-Average Salary Excluding the Minimum and Maximum Salary.cpp ├── 1496-Path Crossing.cpp ├── 1502-Can Make Arithmetic Progression From Sequence.cpp ├── 1507-Reformat Date.cpp ├── 1512-Number of Good Pairs.cpp ├── 1518-Water Bottles.cpp ├── 1523-Count Odd Numbers in an Interval Range.cpp ├── 1528-Shuffle String.cpp ├── 1534-Count Good Triplets.cpp ├── 1539-Kth Missing Positive Number.cpp ├── 1544-Make The String Great.cpp ├── 1550-Three Consecutive Odds.cpp ├── 1556-Thousand Separator.cpp ├── 1560-Most Visited Sector in a Circular Track.cpp ├── 1566-Detect Pattern of Length M Repeated K or More Times.cpp ├── 1572-Matrix Diagonal Sum.cpp ├── 1582-Special Positions in a Binary Matrix.cpp ├── 1588-Sum of All Odd Length Subarrays.cpp ├── 1592-Rearrange Spaces Between Words.cpp ├── 1598-Crawler Log Folder.cpp ├── 160-Intersection of Two Linked Lists.cpp ├── 1603-Design Parking System.cpp ├── 1608-Special Array With X Elements Greater Than or Equal X.cpp ├── 1614-Maximum Nesting Depth of the Parentheses.cpp ├── 1619-Mean of Array After Removing Some Elements.cpp ├── 1624-Largest Substring Between Two Equal Characters.cpp ├── 1629-Slowest Key.cpp ├── 1636-Sort Array by Increasing Frequency.cpp ├── 1640-Check Array Formation Through Concatenation.cpp ├── 1646-Get Maximum in Generated Array.cpp ├── 1652-Defuse the Bomb.cpp ├── 1656-Design an Ordered Stream.cpp ├── 1662-Check If Two String Arrays are Equivalent.cpp ├── 1668-Maximum Repeating Substring.cpp ├── 167-Two Sum II - Input array is sorted.cpp ├── 1672-Richest Customer Wealth.cpp ├── 1678-Goal Parser Interpretation.cpp ├── 168-Excel Sheet Column Title.cpp ├── 1684-Count the Number of Consistent Strings.cpp ├── 1688-Count of Matches in Tournament.cpp ├── 1694-Reformat Phone Number.cpp ├── 1700-Number of Students Unable to Eat Lunch.cpp ├── 1704-Determine if String Halves Are Alike.cpp ├── 1710-Maximum Units on a Truck.cpp ├── 1716-Calculate Money in Leetcode Bank.cpp ├── 1720-Decode XORed Array.cpp ├── 1725-Number Of Rectangles That Can Form The Largest Square.cpp ├── 1732-Find the Highest Altitude.cpp ├── 1736-Latest Time by Replacing Hidden Digits.cpp ├── 1742-Maximum Number of Balls in a Box.cpp ├── 1748-Sum of Unique Elements.cpp ├── 175-Combine Two Tables.frm ├── 1752-Check if Array Is Sorted and Rotated.cpp ├── 176-Second Highest Salary.frm ├── 181-Employees Earning More Than Their Managers.frm ├── 1812-Determine Color of a Chessboard Square.cpp ├── 1816-Truncate Sentence.cpp ├── 182-Duplicate Emails.frm ├── 183-Customers Who Never Order.frm ├── 190-Reverse Bits.cpp ├── 196-Delete Duplicate Emails.frm ├── 197-Rising Temperature.frm ├── 20-Valid Parentheses.cpp ├── 203-Remove Linked List Elements.cpp ├── 205-Isomorphic Strings.cpp ├── 206-Reverse Linked List.cpp ├── 21-Merge Two Sorted Lists.cpp ├── 219-Contains Duplicate II.cpp ├── 225-Implement Stack using Queues.cpp ├── 228-Summary Ranges.cpp ├── 231-Power of Two.cpp ├── 232-Implement Queue using Stacks.cpp ├── 234-Palindrome Linked List.cpp ├── 235-Lowest Common Ancestor of a Binary Search Tree.cpp ├── 257-Binary Tree Paths.cpp ├── 258-Add Digits.cpp ├── 26-Remove Duplicates from Sorted Array.cpp ├── 263-Ugly Number.cpp ├── 268-Missing Number.cpp ├── 27-Remove Element.cpp ├── 278-First Bad Version.cpp ├── 28-Implement strStr().cpp ├── 292-Nim Game.cpp ├── 303-Range Sum Query - Immutable.cpp ├── 342. Power of Four.cpp ├── 345-Reverse Vowels of a String.cpp ├── 35-Search Insert Position.cpp ├── 367-Valid Perfect Square.cpp ├── 374-Guess Number Higher or Lower.cpp ├── 38-Count and Say.cpp ├── 383-Ransom Note.cpp ├── 392-Is Subsequence.cpp ├── 401-Binary Watch.cpp ├── 404-Sum of Left Leaves.cpp ├── 405-Convert a Number to Hexadecimal.cpp ├── 409-Longest Palindrome.cpp ├── 414-Third Maximum Number.cpp ├── 415-Add Strings.cpp ├── 434-Number of Segments in a String.cpp ├── 441-Arranging Coins.cpp ├── 447-Number of Boomerangs.cpp ├── 453-Minimum Moves to Equal Array Elements.cpp ├── 455-Assign Cookies.cpp ├── 461-Hamming Distance.cpp ├── 463-Island Perimeter.cpp ├── 482-License Key Formatting.cpp ├── 485-Max Consecutive Ones.cpp ├── 492-Construct the Rectangle.cpp ├── 496-Next Greater Element I.cpp ├── 500-Keyboard Row.cpp ├── 501-Find Mode in Binary Search Tree.cpp ├── 504-Base 7.cpp ├── 506-Relative Ranks.cpp ├── 507-Perfect Number.cpp ├── 520-Detect Capital.cpp ├── 521-Longest Uncommon Subsequence I.cpp ├── 530-Minimum Absolute Difference in BST.cpp ├── 541-Reverse String II.cpp ├── 551-Student Attendance Record I.cpp ├── 559-Maximum Depth of N-ary Tree.cpp ├── 561-Array Partition I.cpp ├── 563-Binary Tree Tilt.cpp ├── 566-Reshape the Matrix.cpp ├── 572-Subtree of Another Tree.cpp ├── 575-Distribute Candies.cpp ├── 589-N-ary Tree Preorder Traversal.cpp ├── 590-N-ary Tree Postorder Traversal.cpp ├── 594-Longest Harmonious Subsequence.cpp ├── 595-Big Countries.frm ├── 596-Classes More Than 5 Students.frm ├── 598-Range Addition II.cpp ├── 599-Minimum Index Sum of Two Lists.cpp ├── 605-Can Place Flowers.cpp ├── 606-Construct String from Binary Tree.cpp ├── 620-Not Boring Movies.frm ├── 627-Swap Salary.frm ├── 628-Maximum Product of Three Numbers.cpp ├── 637-Average of Levels in Binary Tree.cpp ├── 643-Maximum Average Subarray I.cpp ├── 645-Set Mismatch.cpp ├── 653-Two Sum IV - Input is a BST.cpp ├── 657-Robot Return to Origin.cpp ├── 66-Plus One.cpp ├── 669-Trim a Binary Search Tree.cpp ├── 671-Second Minimum Node In a Binary Tree.cpp ├── 674-Longest Continuous Increasing Subsequence.cpp ├── 680-Valid Palindrome II.cpp ├── 682-Baseball Game.cpp ├── 690-Employee Importance.cpp ├── 693-Binary Number with Alternating Bits.cpp ├── 696-Count Binary Substrings.cpp ├── 7-Reverse Integer.cpp ├── 700-Search in a Binary Search Tree.cpp ├── 703-Kth Largest Element in a Stream.cpp ├── 717-1-bit and 2-bit Characters.cpp ├── 720-Longest Word in Dictionary.cpp ├── 724-Find Pivot Index.cpp ├── 728-Self Dividing Numbers.cpp ├── 744-Find Smallest Letter Greater Than Target.cpp ├── 746-Min Cost Climbing Stairs.cpp ├── 747-Largest Number At Least Twice of Others.cpp ├── 762-Prime Number of Set Bits in Binary Representation.cpp ├── 783-Minimum Distance Between BST Nodes.cpp ├── 804-Unique Morse Code Words.cpp ├── 821-Shortest Distance to a Character.cpp ├── 832-Flipping an Image.cpp ├── 852-Peak Index in a Mountain Array.cpp ├── 876-Middle of the Linked List.cpp ├── 88-Merge Sorted Array.cpp ├── 896-Monotonic Array.cpp ├── 897-Increasing Order Search Tree.cpp ├── 9-Palindrome Number.cpp ├── 905-Sort Array By Parity.cpp ├── 908-Smallest Range I.cpp ├── 914-X of a Kind in a Deck of Cards.cpp ├── 917-Reverse Only Letters.cpp ├── 922-Sort Array By Parity II.cpp ├── 925-Long Pressed Name.cpp ├── 938-Range Sum of BST.cpp ├── 941-Valid Mountain Array.cpp ├── 942-DI String Match.cpp ├── 953-Verifying an Alien Dictionary.cpp ├── 961-N-Repeated Element in Size 2N Array.cpp ├── 965-Univalued Binary Tree.cpp ├── 970-Powerful Integers.cpp ├── 977-Squares of a Sorted Array.cpp └── 993-Cousins in Binary Tree.cpp ├── Hard ├── 10-Regular Expression Matching.cpp ├── 123-Best Time to Buy and Sell Stock III.cpp ├── 127-Word Ladder.cpp ├── 1510-Stone Game IV.cpp ├── 188- Best Time to Buy and Sell Stock IV.cpp ├── 188-Best Time to Buy and Sell Stock IV.cpp ├── 218-The Skyline Problem.cpp ├── 23-Merge k Sorted Lists.cpp ├── 239-Sliding Window Maximum.cpp ├── 25-Reverse Nodes in k-Group.cpp ├── 297-Serialize and Deserialize Binary Tree.cpp ├── 30-Substring with Concatenation of All Words.cpp ├── 312-Burst Balloons.cpp ├── 32-Longest Valid Parentheses.cpp ├── 329-Longest Increasing Path in a Matrix.cpp ├── 354-Russian Doll Envelopes.cpp ├── 37-Sudoku Solver.cpp ├── 4-Median of Two Sorted Arrays.cpp ├── 42-Trapping Rain Water.cpp ├── 44-Wildcard Matching.cpp ├── 45-Jump Game II.cpp ├── 458-Poor Pigs.java ├── 51-N-Queens.cpp ├── 52-N-Queens II.cpp ├── 60-Permutation Sequence.cpp ├── 65-Valid Number.cpp ├── 72-Edit Distance.cpp ├── 76-Minimum Window Substring.cpp ├── 768. Max Chunks To Make Sorted II.cpp ├── 84-Largest Rectangle in Histogram.cpp ├── 85-Maximal Rectangle.cpp ├── 887-Super Egg Drop.cpp ├── 895-Maximum Frequency Stack.cpp ├── 902-Numbers At Most N Given Digit Set.cpp ├── 940-Distinct Subsequences II.cpp ├── 968-Binary Tree Cameras.cpp └── 99-Recover Binary Search Tree.cpp ├── Medium ├── 1004. Max Ones III.cpp ├── 1007-Minimum Domino Rotations For Equal Row.cpp ├── 1008-Construct Binary Search Tree from Preorder Traversal.cpp ├── 1010-Pairs of Songs With Total Durations Divisible by 60.cpp ├── 1015-Smallest Integer Divisible by K.cpp ├── 102-Binary Tree Level Order Traversal.cpp ├── 1026-Maximum Difference Between Node and Ancestor.cpp ├── 103-Binary Tree Zigzag Level Order Traversal.cpp ├── 105-Construct Binary Tree from Preorder and Inorder Traversal.cpp ├── 1091-Shortest Path in Binary Matrix.cpp ├── 11-Container With Most Water.cpp ├── 1123-Lowest Common Ancestor of Deepest Leaves.cpp ├── 113-Path Sum II.cpp ├── 1137-N-th Tribonacci Number.cpp ├── 1143-Longest Common Subsequence.cpp ├── 116-Populating Next Right Pointers in Each Node.cpp ├── 1161-Maximum Level Sum of a Binary Tree.cpp ├── 117-Populating Next Right Pointers in Each Node II.cpp ├── 1190-Reverse Substrings Between Each Pair of Parentheses.cpp ├── 12-Integer to Roman.cpp ├── 1283-Find the Smallest Divisor Given a Threshold.cpp ├── 129-Sum Root to Leaf Numbers.cpp ├── 130-Surrounded Regions.cpp ├── 1302-Deepest Leaves Sum.cpp ├── 1306-Jump Game III.cpp ├── 131-Palindrome Partitioning.cpp ├── 133-Clone Graph.cpp ├── 134-Gas Station.cpp ├── 1372-Longest ZigZag Path in a Binary Tree.cpp ├── 1379-Find a Corresponding Node of a Binary Tree in a Clone of That Tree.cpp ├── 138-Copy List with Random Pointer.cpp ├── 1381-Design a Stack With Increment Operation.cpp ├── 1395-Count Number of Teams.cpp ├── 142-Linked List Cycle II.cpp ├── 1423-Maximum Points You Can Obtain from Cards.cpp ├── 143-Reorder List.cpp ├── 144-Binary Tree Preorder Traversal.cpp ├── 145-Binary Tree Postorder Traversal.cpp ├── 1457-Pseudo-Palindromic Paths in a Binary Tree.cpp ├── 147-Insertion Sort List.cpp ├── 148-Sort List.cpp ├── 1492-The kth Factor of n.cpp ├── 15-3Sum.cpp ├── 151-Reverse Words in a String.cpp ├── 153. Find Minimum in Rotated Sorted Array.cpp ├── 1551-Minimum Operations to Make Array Equal.cpp ├── 16-3Sum Closest.cpp ├── 162-Find Peak Element.cpp ├── 1641-Count Sorted Vowel Strings.cpp ├── 1658-Minimum Operations to Reduce X to Zero.cpp ├── 166. Fraction to Recurring Decimal.cpp ├── 1663-Smallest String With A Given Numeric Value.cpp ├── 1669-Merge In Between Linked Lists.cpp ├── 1673-Find the Most Competitive Subsequence.cpp ├── 1679-Max Number of K-Sum Pairs.cpp ├── 1680-Concatenation of Consecutive Binary Numbers.cpp ├── 17-Letter Combinations of a Phone Number.cpp ├── 1701-Average Waiting Time.cpp ├── 1721-Swapping Nodes in a Linked List.cpp ├── 173-Binary Search Tree Iterator.cpp ├── 1750. Minimum Length of String After Deleting Similar Ends.cpp ├── 18-4Sum.cpp ├── 187-Repeated DNA Sequences.cpp ├── 189-Rotate Array.cpp ├── 19-Remove Nth Node From End of List.cpp ├── 1905. Count Sub Islands.cpp ├── 199-Binary Tree Right Side View.cpp ├── 2-Add Two Numbers.cpp ├── 200-Number of Islands.cpp ├── 207-Course Schedule.cpp ├── 210-Course Schedule II.cpp ├── 22-Generate Parentheses.cpp ├── 227-Basic Calculator II.cpp ├── 229-Majority Element II.cpp ├── 230-Kth Smallest Element in a BST.cpp ├── 238-Product of Array Except Self.cpp ├── 24-Swap Nodes in Pairs.cpp ├── 279-Perfect Squares.cpp ├── 289-Game of life.cpp ├── 29-Divide Two Integers.cpp ├── 3-Longest Substring Without Repeating Characters.cpp ├── 300-Longest Increasing Subsequence.cpp ├── 309-Best Time to Buy and Sell Stock with Cooldown.cpp ├── 31-Next Permutation.cpp ├── 316-Remove Duplicate Letters.cpp ├── 322-Coin Change.cpp ├── 328-Odd Even Linked List.cpp ├── 33-Search in Rotated Sorted Array.cpp ├── 334-Increasing Triplet Subsequence.cpp ├── 337-House Robber III.cpp ├── 34-Find First and Last Position of Element in Sorted Array.cpp ├── 347-Top K Frequent Elements.cpp ├── 36-Valid Sudoku.cpp ├── 365-Water and Jug Problem.cpp ├── 371-Sum of Two Integers.cpp ├── 377. Combination Sum IV.cpp ├── 382-Linked List Random Node.cpp ├── 394-Decode String.cpp ├── 395-Longest Substring with At Least K Repeating Characters.cpp ├── 40-Combination Sum II.cpp ├── 413-Arithmetic Slices.cpp ├── 416-Partition Equal Subset Sum.cpp ├── 429-N-ary Tree Level Order Traversal.cpp ├── 43-Multiply Strings.cpp ├── 438. Find All Anagrams in a String.cpp ├── 443-String Compression.cpp ├── 445-Add Two Numbers II.cpp ├── 451-Sort Characters By Frequency.cpp ├── 454-4Sum II.cpp ├── 456-132 Pattern.cpp ├── 46-Permutations.cpp ├── 468-Validate IP Address.cpp ├── 47-Permutations II.cpp ├── 49-Group Anagrams.cpp ├── 5-Longest Palindromic Substring.cpp ├── 50-Pow(x, n).cpp ├── 503-Next Greater Element II.cpp ├── 516-Longest Palindromic Subsequence.cpp ├── 518-Coin Change 2.cpp ├── 523-Continuous Subarray Sum.cpp ├── 525. Contiguous Array.cpp ├── 526-Beautiful Arrangement.cpp ├── 54-Spiral Matrix.cpp ├── 542-01 Matrix.cpp ├── 55-Jump Game.cpp ├── 556-Next Greater Element III.cpp ├── 56-Merge Intervals.cpp ├── 560-Subarray Sum Equals K.cpp ├── 581-Shortest Unsorted Continuous Subarray.cpp ├── 59-Spiral Matrix II.cpp ├── 593-Valid Square.cpp ├── 6-ZigZag Conversion.cpp ├── 62-Unique Paths.cpp ├── 63-Unique Paths II.cpp ├── 64-Minimum Path Sum.cpp ├── 647-Palindromic Substrings.cpp ├── 650-2 Keys Keyboard.cpp ├── 658-Find K Closest Elements.cpp ├── 665-Non-decreasing Array.cpp ├── 670. Maximum Swap ├── 673-Number of Longest Increasing Subsequence.cpp ├── 684. Redundant Connection.cpp ├── 688-Knight Probability in Chessboard.cpp ├── 695-Max Area of Island.cpp ├── 697-Degree of an Array.cpp ├── 712-Minimum ASCII Delete Sum for Two Strings.cpp ├── 714-Best Time to Buy and Sell Stock with Transaction Fee.cpp ├── 73-Set Matrix Zeroes.cpp ├── 735-Asteroid Collision.cpp ├── 74-Search a 2D Matrix.cpp ├── 75-Sort Colors.cpp ├── 766-Toeplitz Matrix.cpp ├── 769. Max Chunks To Make Sorted.cpp ├── 77-Combinations.cpp ├── 78-Subsets.cpp ├── 781. Rabbits in Forest.cpp ├── 784-Letter Case Permutation.cpp ├── 785-Is Graph Bipartite?.cpp ├── 788-Rotated Digits.cpp ├── 79-Word Search.cpp ├── 796-Rotate String.cpp ├── 799-Champagne Tower.cpp ├── 8-String to Integer (atoi).cpp ├── 80-Remove Duplicates from Sorted Array II.cpp ├── 81-Search in Rotated Sorted Array II.cpp ├── 812-Largest Triangle Area.cpp ├── 819-Most Common Word.cpp ├── 82-Remove Duplicates from Sorted List II.cpp ├── 841-Keys and Rooms.cpp ├── 845-Longest Mountain in Array.cpp ├── 849-Maximize Distance to Closest Person.cpp ├── 858-Mirror Reflection.cpp ├── 863. All Nodes Distance K in Binary Tree.cpp ├── 865-Smallest Subtree with all the Deepest Nodes.cpp ├── 880-Decoded String at Index.cpp ├── 881-Boats to Save People.cpp ├── 90-Subsets II.cpp ├── 901-Online Stock Span.cpp ├── 91-Decode Ways.cpp ├── 916-Word Subsets.cpp ├── 92-Reverse Linked List II.cpp ├── 921-Minimum Add to Make Parentheses Valid.cpp ├── 94-Binary Tree Inorder Traversal.cpp ├── 946-Validate Stack Sequences.cpp ├── 948-Bag of Tokens.cpp ├── 95-Unique Binary Search Trees II.cpp ├── 954. Array of Doubled Pairs.cpp ├── 96-Unique Binary Search Trees.cpp ├── 974. Subarray Sums Divisible by K.cpp ├── 98-Validate Binary Search Tree.cpp ├── 987-Vertical Order Traversal of a Binary Tree.cpp ├── 991-Broken Calculator.cpp ├── 994-Rotting Oranges.cpp └── Day25-Diagonal Traverse.cpp └── README.md /Easy/1-Two Sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector twoSum(vector& nums, int target) { 4 | 5 | vector v(2,0); 6 | unordered_map m; 7 | 8 | for(int i=0;ival == q->val){ 15 | l = isSameTree(p->left,q->left); 16 | r = isSameTree(p->right,q->right); 17 | } 18 | 19 | return l&r; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /Easy/1005-Maximize Sum Of Array After K Negations.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int largestSumAfterKNegations(vector& A, int K) { 4 | 5 | sort(A.begin(), A.end()); 6 | 7 | for(int i=0; K>0 && A[i]<0 && ival == root2->val){ 8 | return isMirror(root1->left, root2->right) && isMirror(root1->right ,root2->left); 9 | } 10 | return false; 11 | } 12 | 13 | bool isSymmetric(TreeNode* root) { 14 | return isMirror(root,root); 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /Easy/1013-Partition Array Into Three Parts With Equal Sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canThreePartsEqualSum(vector& A) { 4 | 5 | auto total = accumulate(A.begin(),A.end(),0); 6 | if(total % 3 != 0) return false; 7 | 8 | int parts = 0; 9 | 10 | for(auto i=0, sum = 0; i= 3; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /Easy/1037-Valid Boomerang.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isBoomerang(vector>& points) { 4 | 5 | return (points[0][0] - points[1][0])*(points[0][1] - points[2][1]) 6 | != (points[0][0]-points[2][0])*(points[0][1]-points[1][1]); 7 | 8 | } 9 | }; 10 | -------------------------------------------------------------------------------- /Easy/104-Maximum Depth of Binary Tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | int maxDepth(TreeNode* root) { 15 | if(root==NULL) return 0; 16 | 17 | int l = maxDepth(root->left); 18 | int r = maxDepth(root->right); 19 | 20 | return 1 + max(l,r); 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Easy/1046-Last Stone Weight.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int lastStoneWeight(vector& stones) { 4 | 5 | priority_queue pq(stones.begin(), stones.end()); 6 | 7 | while(pq.size() > 1){ 8 | int x = pq.top(); pq.pop(); 9 | int y = pq.top(); pq.pop(); 10 | if(x > y) pq.push(x-y); 11 | } 12 | 13 | return pq.empty() ? 0 : pq.top(); 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /Easy/1047-Remove All Adjacent Duplicates In String.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string removeDuplicates(string S) { 4 | stack st; 5 | string ans = ""; 6 | 7 | for(int i=0 ; i& heights) { 4 | int ans = 0; 5 | 6 | vector v(heights); 7 | 8 | sort(v.begin(),v.end()); 9 | 10 | int n = heights.size(); 11 | 12 | for(int i=0;i findOcurrences(string text, string first, string second) { 4 | 5 | //lets store the words in a vector 'v', using stringstream 6 | stringstream ss(text); 7 | string word; 8 | vector v; 9 | 10 | while(ss >> word) v.push_back(word); 11 | 12 | vector ans; 13 | 14 | //iterate over every string in a vector and compare it with 'first' and 'second' 15 | for(int i=0;i& arr) { 4 | for(int i=0;i distributeCandies(int candies, int num_people) { 4 | 5 | vector v(num_people,0); 6 | 7 | for(int i = 0 ; candies > 0 ; i++){ 8 | v[i%num_people] += min(candies,i+1); 9 | candies -= i+1; 10 | } 11 | 12 | return v; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /Easy/111-Minimum Depth of Binary Tree.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | public: 4 | 5 | void helper(TreeNode* root,int count,int &finalcount) 6 | { 7 | if(root==NULL) 8 | { 9 | finalcount=min(finalcount,count); 10 | return; 11 | } 12 | 13 | if(root->left==NULL and root->right==NULL) 14 | { 15 | finalcount=min(finalcount,count+1); 16 | return; 17 | } 18 | 19 | if(root->left) 20 | helper(root->left,count+1,finalcount); 21 | 22 | if(root->right) 23 | helper(root->right,count+1,finalcount); 24 | 25 | return; 26 | } 27 | 28 | int minDepth(TreeNode* root) { 29 | 30 | int finalcount=INT_MAX; 31 | 32 | helper(root,0,finalcount); 33 | 34 | return finalcount; 35 | 36 | } 37 | 38 | }; 39 | -------------------------------------------------------------------------------- /Easy/1114-Print in Order.cpp: -------------------------------------------------------------------------------- 1 | class Foo { 2 | private: 3 | std::promise p1; 4 | std::promise p2; 5 | 6 | public: 7 | void first(function printFirst) { 8 | printFirst(); 9 | p1.set_value(); 10 | } 11 | 12 | void second(function printSecond) { 13 | p1.get_future().wait(); 14 | printSecond(); 15 | p2.set_value(); 16 | } 17 | 18 | void third(function printThird) { 19 | p2.get_future().wait(); 20 | printThird(); 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Easy/1122-Relative Sort Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector relativeSortArray(vector& arr1, vector& arr2) { 4 | 5 | int h[1001] = {0}; 6 | 7 | int a = arr1.size(); 8 | int b = arr2.size(); 9 | 10 | for(int i=0;i>& dominoes) { 4 | 5 | int ans = 0; 6 | map m; 7 | 8 | for(auto& d: dominoes){ 9 | 10 | auto val = min(d[0], d[1]) * 10 + max(d[0], d[1]); 11 | ans += m[val]; 12 | m[val]++; 13 | 14 | } 15 | 16 | return ans; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /Easy/1154-Day of the Year.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | bool isLeap(int y){ 5 | 6 | return (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0); 7 | 8 | } 9 | 10 | int getMonth(int m,int y){ 11 | 12 | if(m==1 || m==3 || m==5 || m==7 || m==8 || m==10 || m==12) return 31; 13 | if(m==2) return isLeap(y) ? 29 : 28; 14 | return 30; 15 | 16 | } 17 | 18 | int dayOfYear(string date) { 19 | 20 | int y = stoi(date.substr(0,4)); 21 | int m = stoi(date.substr(5,2)); 22 | int d = stoi(date.substr(8,2)); 23 | 24 | int days = 0; 25 | days += d; 26 | 27 | for(int month = 1; month < m ; month++){ 28 | days += getMonth(month,y); 29 | } 30 | 31 | return days; 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /Easy/1160-Find Words That Can Be Formed by Characters.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countCharacters(vector& words, string chars) { 4 | 5 | int ans = 0; 6 | vector v(26,0); 7 | 8 | for(auto& ch:chars) v[ch - 'a']++; 9 | 10 | for(auto& w:words){ 11 | 12 | vector m = v; 13 | bool isGood = true; 14 | for(auto ch:w){ 15 | if(--m[ch - 'a'] < 0){ 16 | isGood = false; 17 | break; 18 | } 19 | } 20 | 21 | if(isGood) ans += w.size(); 22 | } 23 | 24 | return ans; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /Easy/118-Pascal's Triangle.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> generate(int numRows) { 4 | vector> res; 5 | 6 | for(int i=0;i row(i+1,1); 8 | for(int j=1;j getRow(int rowIndex) { 4 | vector res{1},cur{1}; 5 | 6 | for(int i=1;i<=rowIndex;i++){ 7 | res.push_back(1); 8 | for(int j=1;j> minimumAbsDifference(vector& arr) { 4 | 5 | sort(arr.begin(),arr.end()); 6 | 7 | int diff = INT_MAX; 8 | 9 | for(int i=0;i> ans; 14 | 15 | for(int i=0;i& arr) { 4 | unordered_map m; 5 | set s; 6 | 7 | for(auto& x:arr){ 8 | m[x]++; 9 | } 10 | 11 | for(auto it = m.begin() ; it != m.end() ; it++){ 12 | s.insert(it->second); 13 | } 14 | 15 | return s.size()==m.size(); 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /Easy/121-Best Time to Buy and Sell Stock.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProfit(vector& prices) { 4 | int res =0,max=0; 5 | for(int i=1;i max) max = res; 9 | } 10 | return max; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /Easy/1217-Minimum Cost to Move Chips to The Same Position.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minCostToMoveChips(vector& position) { 4 | int odd = 0; 5 | int even = 0; 6 | 7 | for(int i=0;i& prices) { 8 | 9 | int buy = prices[0]; 10 | int sell = prices[0]; 11 | int profit = 0; 12 | 13 | for(int i = 1 ; i < prices.size() ; i++){ 14 | 15 | if(prices[i] > prices[i-1]){ 16 | sell = prices[i]; 17 | }else{ 18 | profit += sell - buy; 19 | buy = sell = prices[i]; 20 | } 21 | 22 | } 23 | 24 | profit += sell - buy; 25 | 26 | return profit; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /Easy/1232-Check If It Is a Straight Line.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool checkStraightLine(vector>& coordinates) { 4 | 5 | int dx = coordinates[1][0] - coordinates[0][0]; 6 | int dy = coordinates[1][1] - coordinates[0][1]; 7 | 8 | for(int i=2;i> findSolution(CustomFunction& customfunction, int z) { 16 | vector> res; 17 | for(int i=1;i<=z;i++){ 18 | for(int j=z;j>=1;j--){ 19 | if(customfunction.f(i,j) == z){ 20 | res.push_back({i,j}); 21 | } 22 | } 23 | } 24 | return res; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /Easy/125-Valid Palindrome.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPalindrome(string s) { 4 | int left = 0; 5 | int right = s.length()-1; 6 | 7 | while(left>& points) { 4 | int sum = 0; 5 | for(int i=0;i& arr) { 4 | 5 | int n = arr.size(); 6 | int ans = 0; 7 | unordered_map m; 8 | 9 | for(auto& x:arr){ 10 | m[x]++; 11 | } 12 | 13 | for(auto& x:m){ 14 | if(x.second > (n/4)){ 15 | ans = x.first; 16 | break; 17 | } 18 | } 19 | return ans; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /Easy/1299-Replace Elements with Greatest Element on Right Side.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector replaceElements(vector& arr) { 4 | int n = arr.size(); 5 | 6 | vector ans(n); 7 | ans[n-1] = -1; 8 | for(int i=n-2;i>=0;i--){ 9 | ans[i] = max(arr[i+1],ans[i+1]); 10 | } 11 | return ans; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /Easy/1313-Decompress Run-Length Encoded List.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector decompressRLElist(vector& nums) { 4 | int n = nums.size(); 5 | vector ans; 6 | 7 | for(int i=0;i getNoZeroIntegers(int n) { 15 | 16 | vector ans(2,-1); 17 | 18 | for(int i=1;i<=n;i++){ 19 | 20 | if(containsZero(i) && containsZero(n-i)){ 21 | ans[0] = i; 22 | ans[1] = n-i; 23 | break; 24 | } 25 | 26 | } 27 | 28 | return ans; 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /Easy/1323-Maximum 69 Number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maximum69Number (int num) { 4 | 5 | vector v; //converting num into vector of integers 6 | 7 | while(num){ 8 | v.push_back(num%10); 9 | num = num/10; 10 | } 11 | 12 | // p.s. vector 'v' contain digits reverse of num 13 | //changing first digit we encounter of value '6' to value '9' 14 | 15 | for(int i=v.size()-1;i>=0;i--){ 16 | if(v[i] == 9) continue; 17 | else{ 18 | v[i] = 9; 19 | break; 20 | } 21 | } 22 | 23 | int ans = 0; 24 | //converting back 25 | for(int i = v.size()-1;i>=0;i--){ 26 | ans = ans*10 + v[i]; 27 | } 28 | return ans; 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /Easy/1331-Rank Transform of an Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector arrayRankTransform(vector& arr) { 4 | 5 | vector a(arr); 6 | sort(a.begin(),a.end()); 7 | 8 | unordered_map m; 9 | 10 | for(auto& x: a){ 11 | m.emplace(x, m.size()+1); 12 | } 13 | 14 | for(int i=0;i kWeakestRows(vector>& mat, int k) { 4 | 5 | multimap m; 6 | 7 | for(int i=0;i(c,i)); 10 | } 11 | 12 | vector ans; 13 | for(auto i=m.begin();i!=m.end()&&k-->0;i++){ 14 | ans.push_back((int)i->second); 15 | } 16 | return ans; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /Easy/1342-Number of Steps to Reduce a Number to Zero.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numberOfSteps (int num) { 4 | 5 | int count = 0; 6 | 7 | while(num){ 8 | if(num%2 != 0){ 9 | num = num-1; 10 | }else{ 11 | num = num/2; 12 | } 13 | count++; 14 | } 15 | 16 | return count; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /Easy/1346-Check If N and Its Double Exist.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool checkIfExist(vector& arr) { 4 | 5 | unordered_map m; 6 | 7 | for(int i=0;i0 || ((arr[i]%2 ==0) && m.count(arr[i]/2) >0)) 14 | return true; 15 | 16 | } 17 | 18 | return false; 19 | } 20 | }; -------------------------------------------------------------------------------- /Easy/1356-Sort Integers by The Number of 1 Bits.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | static bool comp(const int&a , const int& b){ 5 | int c1 = __builtin_popcount(a); 6 | int c2 = __builtin_popcount(b); 7 | if(c1 == c2){ 8 | return a sortByBits(vector& arr) { 14 | sort(arr.begin(),arr.end(),comp); 15 | return arr; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /Easy/1370-Increasing Decreasing String.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string sortString(string s) { 4 | 5 | int a[26] = {0}; 6 | string ans = ""; 7 | int k=0; 8 | 9 | for(int i=0;i0){ 18 | ans += i+'a'; 19 | k--; 20 | a[i]--; 21 | } 22 | } 23 | 24 | for(int i=25;i>=0;i--){ 25 | if(a[i]>0){ 26 | ans += i+'a'; 27 | k--; 28 | a[i]--; 29 | } 30 | } 31 | 32 | } 33 | 34 | return ans; 35 | } 36 | }; 37 | -------------------------------------------------------------------------------- /Easy/1374-Generate a String With Characters That Have Odd Counts.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string generateTheString(int n) { 4 | 5 | string ans; 6 | 7 | if(n%2 != 0){ 8 | // n is odd 9 | for(int i=0;i v(37,0); 15 | for(int i=1;i<=n;i++){ 16 | v[digitSum(i)]++; 17 | } 18 | 19 | int c = 0; 20 | int m = *max_element(v.begin(),v.end()); 21 | for(auto x:v){ 22 | if(x == m) c++; 23 | } 24 | return c; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /Easy/14-Longest Common Prefix.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | string longestCommonPrefix(vector& strs) { 5 | int n = strs.size(); 6 | 7 | string ans = ""; 8 | 9 | if(n==0)return ans; 10 | 11 | int index = 0; 12 | 13 | for(auto c:strs[0]){ 14 | for(int i=1;i minSubsequence(vector& nums) { 4 | int sum=0,newsum=0; 5 | 6 | for(int i=0;i()); 11 | 12 | vector ans; 13 | 14 | for(int i=0;i sum){ 19 | break; 20 | } 21 | } 22 | 23 | return ans; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /Easy/1408-String Matching in an Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector stringMatching(vector& words) { 4 | vector ans; 5 | 6 | for(auto i:words){ 7 | for(auto j:words){ 8 | 9 | if(i == j) continue; 10 | if(j.find(i) != -1){ 11 | ans.push_back(i); 12 | break; 13 | } 14 | 15 | } 16 | } 17 | return ans; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /Easy/1413-Minimum Value to Get Positive Step by Step Sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minStartValue(vector& nums) { 4 | int ans = 0, minAns = 0; 5 | for(auto x:nums){ 6 | ans += x; 7 | if(ans < minAns) 8 | minAns = ans; 9 | } 10 | return -minAns + 1; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /Easy/1417-Reformat The String.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string reformat(string s) { 4 | string l; 5 | string d; 6 | 7 | for(auto x:s){ 8 | isalpha(x) ? l.push_back(x) : d.push_back(x); 9 | } 10 | 11 | if(abs(int(l.size()-d.size())) > 1) return ""; 12 | 13 | bool alpha = l.length() > d.length(); 14 | 15 | int i=0,j=0,k=0; 16 | while(i < s.size()){ 17 | if(alpha){ 18 | s[i++] = l[j++]; 19 | }else{ 20 | s[i++] = d[k++]; 21 | } 22 | 23 | alpha = !alpha; 24 | } 25 | 26 | return s; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /Easy/1422-Maximum Score After Splitting a String.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxScore(string s) { 4 | int ones = 0; 5 | int zeros = 0; 6 | 7 | for(auto c:s){ 8 | if(c == '1') ones++; 9 | } 10 | 11 | int score = 0; 12 | for(int i=0;i& nums, int k) { 4 | 5 | int t = 0; 6 | 7 | for(int i = 0 ; i < nums.size() ; i++){ 8 | 9 | if(nums[i] == 1){ 10 | if(i == 0){ 11 | continue; 12 | }else{ 13 | if(t < k) return false; 14 | t = 0; 15 | } 16 | }else{ 17 | t++; 18 | } 19 | 20 | } 21 | return true; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /Easy/1441-Build an Array With Stack Operations.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector buildArray(vector& target, int n) { 4 | 5 | vector s; 6 | 7 | int e = target[target.size()-1]; //find the last element in the target array 8 | 9 | for(int i=1;i<=e;i++){ 10 | 11 | if(std::find(target.begin(),target.end(),i) != target.end()){ 12 | s.push_back("Push"); 13 | }else{ 14 | s.push_back("Push"); 15 | s.push_back("Pop"); 16 | } 17 | 18 | } 19 | return s; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /Easy/1446-Consecutive Characters.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxPower(string s) { 4 | 5 | int n = s.length(); 6 | int maxAns = -1; 7 | int count; 8 | 9 | for(int i=0;i& startTime, vector& endTime, int queryTime) { 4 | int ans =0; 5 | int e=startTime.size(); 6 | 7 | for(int i=0;i= startTime[i] && (queryTime <= endTime[i])){ 9 | ans++; 10 | } 11 | } 12 | return ans; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /Easy/1455-Check If a Word Occurs As a Prefix of Any Word in a Sentence.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int isPrefixOfWord(string sentence, string searchWord) { 4 | 5 | string word; 6 | 7 | stringstream ss(sentence); 8 | 9 | int len = searchWord.length(); 10 | int i=1; 11 | 12 | while(ss >> word){ 13 | string w = word.substr(0,len); 14 | if(w == searchWord) 15 | return i; 16 | i++; 17 | } 18 | 19 | return -1; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /Easy/1464-Maximum Product of Two Elements in an Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProduct(vector& nums) { 4 | 5 | sort(nums.begin(),nums.end()); 6 | int n = nums.size()-1; 7 | return (nums[n]-1)*(nums[n-1]-1); 8 | 9 | } 10 | }; 11 | -------------------------------------------------------------------------------- /Easy/1470-Shuffle the Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector shuffle(vector& nums, int n) { 4 | vector ans; 5 | int i = 0; 6 | int j = n; 7 | while(i finalPrices(vector& prices) { 4 | 5 | int n = prices.size(); 6 | 7 | for(int i=0;i runningSum(vector& nums) { 4 | 5 | for(int i=1;i& salary) { 4 | 5 | return (accumulate(salary.begin(), salary.end(), 0.) 6 | - *min_element(salary.begin(),salary.end()) 7 | - *max_element(salary.begin(), salary.end())) / (salary.size()-2); 8 | 9 | } 10 | }; 11 | -------------------------------------------------------------------------------- /Easy/1496-Path Crossing.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPathCrossing(string path) { 4 | int x=0; 5 | int y=0; 6 | set> s; 7 | s.insert({0,0}); 8 | 9 | for(char p:path){ 10 | 11 | if(p == 'N') y++; 12 | else if(p == 'S') y--; 13 | else if(p == 'W') x--; 14 | else x++; 15 | 16 | 17 | if(s.find({x,y}) != s.end()) return true; 18 | else s.insert({x,y}); 19 | } 20 | return false; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Easy/1512-Number of Good Pairs.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numIdenticalPairs(vector& nums) { 4 | int n = nums.size(); 5 | int count=0; 6 | for(int i=0;i& indices) { 6 | string ans = s; 7 | for(int i=0;i& indices) { 19 | int i= 0; 20 | while(i!=indices.size()){ 21 | if(i == indices[i]) i++; 22 | else{ 23 | swap(s[i],s[indices[i]]); 24 | swap(indices[i], indices[indices[i]]); 25 | } 26 | } 27 | return s; 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /Easy/1534-Count Good Triplets.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countGoodTriplets(vector& arr, int a, int b, int c) { 4 | int ans=0; 5 | int n=arr.size(); 6 | 7 | for(int i=0;i& arr) { 4 | if(arr.size()<2)return false; 5 | 6 | for(int i=0;i s; 7 | string ans= ""; 8 | int count = 0; 9 | while(n>0){ 10 | if(count==3){ 11 | s.push('.'); 12 | count=0; 13 | } 14 | 15 | s.push((n%10) + '0'); 16 | count++; 17 | n=n/10; 18 | } 19 | 20 | 21 | while(!s.empty()){ 22 | ans += s.top(); 23 | s.pop(); 24 | } 25 | return ans; 26 | 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /Easy/1560-Most Visited Sector in a Circular Track.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector mostVisited(int n, vector& rounds) { 4 | vector ans; 5 | int size = rounds.size(); 6 | 7 | if(rounds[0] <= rounds[size-1]) { 8 | for(int i=rounds[0]; i<= rounds[size-1]; i++) { 9 | ans.push_back(i); 10 | } 11 | return ans; 12 | } 13 | else { 14 | for(int i=1; i<= rounds[size-1]; i++) { 15 | ans.push_back(i); 16 | } 17 | 18 | for(int i=rounds[0]; i<=n; i++) { 19 | ans.push_back(i); 20 | } 21 | } 22 | 23 | return ans; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /Easy/1566-Detect Pattern of Length M Repeated K or More Times.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool containsPattern(vector& arr, int m, int k) { 4 | 5 | int cnt=0; 6 | for(int i=0;i+m < arr.size(); i++){ 7 | 8 | if(arr[i]!=arr[i+m]){ 9 | cnt=0; 10 | } 11 | cnt += (arr[i] == arr[i+m]); 12 | if(cnt == (k-1)*m) 13 | return true; 14 | 15 | } 16 | return false; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /Easy/1572-Matrix Diagonal Sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int diagonalSum(vector>& mat) { 4 | int n = mat.size(); 5 | int ans = 0; 6 | 7 | for(int i=0;i& arr) { 4 | int result = 0; 5 | vector sum; 6 | int curr_sum = 0; 7 | 8 | for(int i=0;i=1 ? sum[left-1] : 0); 18 | right += 2; 19 | } 20 | } 21 | return result; 22 | 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /Easy/1592-Rearrange Spaces Between Words.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string reorderSpaces(string text) { 4 | string s, ans; 5 | stringstream ss (text); 6 | vector words; 7 | 8 | while(ss >> s){ 9 | words.push_back(s); 10 | } 11 | 12 | int space = count(text.begin(),text.end(),' '); 13 | int n = words.size(); 14 | 15 | int quot = n==1?0:space/(n-1); 16 | int rem = n==1?space:space%(n-1); 17 | 18 | for(auto &w:words){ 19 | if(!ans.empty()) ans += string(quot,' '); 20 | ans += w; 21 | } 22 | 23 | ans += string(rem,' '); 24 | return ans; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /Easy/1598-Crawler Log Folder.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minOperations(vector& logs) { 4 | 5 | int ans = 0; //this will keep track of our position 6 | // i.e, how far we are from main folder 7 | 8 | for(int i=0;i& nums) { 4 | 5 | int ans = -1; 6 | int n = nums.size(); 7 | 8 | for(int i=0;i<=n;i++){ 9 | 10 | int count = 0; 11 | int x = i; 12 | 13 | for(int j=0;j= x)count++; 15 | 16 | if(count == x){ 17 | ans = x; 18 | break; 19 | } 20 | 21 | } 22 | 23 | return ans; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /Easy/1614-Maximum Nesting Depth of the Parentheses.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxDepth(string s) { 4 | 5 | int currAns = 0; 6 | int finalAns = 0; 7 | int n = s.length(); 8 | 9 | for(int i=0;i& arr) { 4 | sort(arr.begin(),arr.end()); 5 | double sum=0,count=0; 6 | 7 | int size = arr.size(); 8 | 9 | for(int i = size/20 ; i& releaseTimes, string keysPressed) { 4 | 5 | char ans = keysPressed[0]; 6 | int maxTime = releaseTimes[0]; 7 | 8 | int n = keysPressed.length(); 9 | 10 | for(int i=1;i ans) ans = curr_char; 18 | 19 | }else if(curr_time > maxTime){ 20 | 21 | maxTime = curr_time; 22 | ans = curr_char; 23 | 24 | } 25 | 26 | } 27 | 28 | return ans; 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /Easy/1640-Check Array Formation Through Concatenation.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canFormArray(vector& arr, vector>& pieces) { 4 | 5 | int n = arr.size(); 6 | unordered_map> m; 7 | for(auto &piece:pieces){ 8 | m[piece[0]] = piece; 9 | } 10 | 11 | int idx = 0; 12 | 13 | while(idx nums(n+1,0); 8 | 9 | nums[0] = 0; 10 | nums[1] = 1; 11 | 12 | for(int i=1;i<=n/2;i++){ 13 | 14 | if(i*2 > n || (2*i+1 > n)) break; 15 | 16 | nums[i*2] =nums[i]; 17 | nums[(i*2)+1] = nums[i] + nums[i+1]; 18 | 19 | } 20 | 21 | return *max_element(nums.begin(),nums.end()); 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /Easy/1656-Design an Ordered Stream.cpp: -------------------------------------------------------------------------------- 1 | class OrderedStream { 2 | public: 3 | 4 | vector res; 5 | int ptr; 6 | 7 | OrderedStream(int n) { 8 | res.resize(n); 9 | ptr = 1; 10 | } 11 | 12 | vector insert(int id, string value) { 13 | res[id-1] = value; 14 | vector ans; 15 | 16 | if(id == ptr){ 17 | int i = ptr - 1; 18 | 19 | for(; i < res.size(); i++){ 20 | if(res[i] == ""){ 21 | break; 22 | } 23 | ans.push_back(res[i]); 24 | } 25 | ptr = i+1; 26 | } 27 | 28 | return ans; 29 | } 30 | }; 31 | 32 | /** 33 | * Your OrderedStream object will be instantiated and called as such: 34 | * OrderedStream* obj = new OrderedStream(n); 35 | * vector param_1 = obj->insert(id,value); 36 | */ 37 | -------------------------------------------------------------------------------- /Easy/1662-Check If Two String Arrays are Equivalent.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool arrayStringsAreEqual(vector& word1, vector& word2) { 4 | 5 | string a , b; 6 | for(auto& w:word1) a += w; 7 | for(auto& w:word2) b += w; 8 | return a==b; 9 | 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /Easy/1668-Maximum Repeating Substring.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxRepeating(string sequence, string word) { 4 | int k = 0; 5 | string temp = word; 6 | 7 | while(sequence.find(temp) != string::npos){ 8 | temp += word; 9 | k++; 10 | } 11 | 12 | return k; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /Easy/167-Two Sum II - Input array is sorted.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector twoSum(vector& numbers, int target) { 4 | int n = numbers.size(); 5 | int s = 0; 6 | int e = n-1; 7 | vector v; 8 | 9 | 10 | while(s target){ 16 | e--; 17 | }else{ 18 | s++; 19 | } 20 | } 21 | 22 | return v; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /Easy/1672-Richest Customer Wealth.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maximumWealth(vector>& accounts) { 4 | int max_wealth = INT_MIN; 5 | 6 | for(int i=0 ; i < accounts.size() ; i++){ 7 | 8 | int current_wealth = 0; 9 | 10 | for(int j = 0; j < accounts[0].size() ; j++) current_wealth += accounts[i][j]; 11 | 12 | max_wealth = max(max_wealth , current_wealth); 13 | 14 | } 15 | 16 | return max_wealth; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /Easy/1678-Goal Parser Interpretation.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string interpret(string command) { 4 | 5 | string ans = ""; 6 | 7 | for(int i = 0 ; i < command.length() ; i++){ 8 | 9 | if(command[i] == 'G') 10 | ans += "G"; 11 | 12 | else if(command[i] == '(' && command[i+1] == ')') { 13 | i++; 14 | ans += "o"; 15 | } 16 | else { 17 | ans += "al"; 18 | i = i+3; 19 | } 20 | 21 | } 22 | 23 | return ans; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /Easy/168-Excel Sheet Column Title.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string convertToTitle(int n) { 4 | 5 | string ans = ""; 6 | 7 | while(n){ 8 | 9 | int rem = n%26; 10 | 11 | if(rem == 0){ 12 | 13 | ans += 'Z'; 14 | n = (n/26) - 1; 15 | 16 | }else{ 17 | 18 | ans += (rem -1) + 'A'; 19 | n = n/26; 20 | 21 | } 22 | 23 | } 24 | 25 | reverse(ans.begin(),ans.end()); 26 | 27 | return ans; 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /Easy/1684-Count the Number of Consistent Strings.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countConsistentStrings(string allowed, vector& words) { 4 | int ans = 0; 5 | int map[26] = {0}; 6 | 7 | for(auto& x:allowed) 8 | map[x-'a']++; 9 | 10 | 11 | for(auto& word : words){ 12 | 13 | bool isConsistent = true; 14 | 15 | for(auto& ch: word){ 16 | 17 | if(map[ch - 'a'] == 0){ 18 | isConsistent = false; 19 | break; 20 | } 21 | 22 | } 23 | 24 | if(isConsistent) ans++; 25 | 26 | } 27 | 28 | return ans; 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /Easy/1688-Count of Matches in Tournament.cpp: -------------------------------------------------------------------------------- 1 | //The winning team never looses the match, therefore n - 1 match has to be played in order to loose 2 | //n -1 teams. 3 | //Therefore whether n is odd or even, n -1 matches always needed to be played. 4 | //Try learning by taking different values of n!! 5 | 6 | class Solution { 7 | public: 8 | int numberOfMatches(int n) { 9 | return n-1; 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /Easy/1700-Number of Students Unable to Eat Lunch.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countStudents(vector& students, vector& sandwiches) { 4 | 5 | int a[] = {0,0}; 6 | for(auto x:students){ 7 | a[x]++; 8 | } 9 | 10 | int k=0; 11 | while(k < sandwiches.size()){ 12 | if(a[sandwiches[k]] > 0) 13 | a[sandwiches[k]]--; 14 | else 15 | break; 16 | k++; 17 | } 18 | 19 | return sandwiches.size()-k; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /Easy/1704-Determine if String Halves Are Alike.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | bool isVowel(char ch){ 5 | if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' || ch=='A' || ch=='E'|| ch=='I'|| ch=='O' || ch== 'U') 6 | return true; 7 | 8 | return false; 9 | } 10 | 11 | bool halvesAreAlike(string s) { 12 | 13 | int n = s.size(); 14 | string a = s.substr(0,n/2); //first half 15 | string b = s.substr(n/2); //second half 16 | 17 | int v1=0, v2=0; 18 | int i = 0; 19 | 20 | while(i < n/2){ 21 | if(isVowel(a[i])) v1++; 22 | if(isVowel(b[i])) v2++; 23 | i++; 24 | } 25 | 26 | return v1 == v2; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /Easy/1710-Maximum Units on a Truck.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | 5 | static bool mycomp(vector &a,vector &b){ 6 | return a[1] >= b[1]; 7 | } 8 | 9 | int maximumUnits(vector>& boxTypes, int truckSize) { 10 | 11 | sort(boxTypes.begin(), boxTypes.end(), mycomp); 12 | 13 | long long int ans = 0; 14 | 15 | for(int i=0;i0){ 9 | 10 | int i=0; 11 | while(i < 7){ 12 | ans += startPoint + i; 13 | i++; 14 | n--; 15 | if(n==0) break; 16 | } 17 | 18 | startPoint++; 19 | } 20 | 21 | return ans; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /Easy/1720-Decode XORed Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector decode(vector& encoded, int first) { 4 | 5 | int n = encoded.size(); 6 | 7 | vector arr(n+1); 8 | arr[0] = first; 9 | 10 | for(int i=0;i>& rectangles) { 4 | 5 | int ans = 0, maxlen = 0; 6 | 7 | for(int i = 0 ; i < rectangles.size() ; i++){ 8 | 9 | int len = min(rectangles[i][0], rectangles[i][1]); 10 | maxlen = max(maxlen,len); 11 | 12 | } 13 | 14 | for(int i = 0 ; i < rectangles.size() ; i++){ 15 | 16 | int len = min(rectangles[i][0], rectangles[i][1]); 17 | if(len == maxlen) ans++; 18 | 19 | } 20 | 21 | return ans; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /Easy/1732-Find the Highest Altitude.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int largestAltitude(vector& gain) { 4 | 5 | int n = gain.size(); 6 | int max_height = 0; 7 | 8 | vector heights(n+1); //since trip starts from '0' altitute 9 | 10 | heights[0] = 0; //storing the starting point 11 | 12 | for(int i = 1 ; i < n+1 ; i++){ 13 | 14 | heights[i] = heights[i-1] + gain[i-1]; //maintaining prefix sum 15 | 16 | if(heights[i] > max_height) max_height = heights[i]; 17 | 18 | } 19 | 20 | return max_height; //returning max height 21 | } 22 | }; 23 | 24 | -------------------------------------------------------------------------------- /Easy/1736-Latest Time by Replacing Hidden Digits.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string maximumTime(string time) { 4 | 5 | time[0] = time[0] != '?' ? time[0] : (time[1] == '?' || time[1] <= '3') ? '2' : '1'; 6 | time[1] = time[1] != '?' ? time[1] : time[0] == '2' ? '3' : '9'; 7 | time[3] = time[3] != '?' ? time[3] : '5'; 8 | time[4] = time[4] != '?' ? time[4] : '9'; 9 | 10 | return time; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /Easy/1742-Maximum Number of Balls in a Box.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | int countBalls(int lowLimit, int highLimit) { 5 | 6 | vector arr(46,0); 7 | 8 | while(lowLimit <= highLimit){ 9 | 10 | int sum = 0, i = lowLimit; 11 | 12 | while(i > 0){ 13 | sum += i % 10; 14 | i = i / 10; 15 | } 16 | 17 | arr[sum]++; 18 | 19 | lowLimit++; 20 | } 21 | 22 | return *max_element(arr.begin(), arr.end()); 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /Easy/1748-Sum of Unique Elements.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int sumOfUnique(vector& nums) { 4 | 5 | int sum = 0; 6 | vector arr(101); 7 | 8 | for(auto x : nums) arr[x]++; 9 | 10 | 11 | for(int i=0;i& nums) { 4 | 5 | int n = nums.size(); 6 | int k = 0; 7 | 8 | for(int i = 0 ; i < n ; i++){ 9 | 10 | if(nums[i] > nums[(i+1) % n] && ++k > 1) return false; 11 | 12 | } 13 | 14 | return true; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /Easy/176-Second Highest Salary.frm: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select max(Salary) as SecondHighestSalary from Employee 3 | where Salary < (select max(Salary) from Employee) 4 | -------------------------------------------------------------------------------- /Easy/181-Employees Earning More Than Their Managers.frm: -------------------------------------------------------------------------------- 1 | /* Write your T-SQL query statement below */ 2 | SELECT Name Employee 3 | FROM Employee 4 | WHERE Employee.Salary > (select Salary from Employee E where E.id = Employee.ManagerId) 5 | -------------------------------------------------------------------------------- /Easy/1812-Determine Color of a Chessboard Square.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | bool squareIsWhite(string coordinates) { 5 | 6 | return (coordinates[0] + coordinates[1]) % 2; 7 | 8 | } 9 | }; 10 | -------------------------------------------------------------------------------- /Easy/1816-Truncate Sentence.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string truncateSentence(string s, int k) { 4 | 5 | for(int i=0;i 1; 6 | -------------------------------------------------------------------------------- /Easy/183-Customers Who Never Order.frm: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | SELECT A.Name as Customers from Customers A 3 | WHERE A.Id NOT IN (SELECT B.CustomerId from Orders B) 4 | -------------------------------------------------------------------------------- /Easy/190-Reverse Bits.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | uint32_t reverseBits(uint32_t n) { 4 | uint32_t res = 0; 5 | for (int i = 0; i < 31; i++) { 6 | res = (n % 2) + res << 1; 7 | n >>= 1; 8 | } 9 | return res + n % 2; 10 | } 11 | }; 12 | 13 | -------------------------------------------------------------------------------- /Easy/196-Delete Duplicate Emails.frm: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | DELETE p1 3 | FROM Person p1, Person p2 4 | WHERE p1.Email = p2.Email AND 5 | p1.Id > p2.Id 6 | -------------------------------------------------------------------------------- /Easy/197-Rising Temperature.frm: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | SELECT wt1.Id 3 | FROM Weather wt1, Weather wt2 4 | WHERE wt1.Temperature > wt2.Temperature AND 5 | TO_DAYS(wt1.recordDate)-TO_DAYS(wt2.recordDate)=1; 6 | -------------------------------------------------------------------------------- /Easy/205-Isomorphic Strings.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isIsomorphic(string s, string t) { 4 | 5 | int n = s.length(); 6 | int l = t.length(); 7 | 8 | if(n != l) return false; 9 | 10 | unordered_map m; 11 | unordered_map v; 12 | 13 | 14 | for(int i=0;inext == NULL) return head; 6 | 7 | ListNode* curr = head; 8 | ListNode* prev = NULL; 9 | ListNode* n; 10 | 11 | while(curr != NULL){ 12 | n = curr->next; 13 | curr->next = prev; 14 | prev = curr; 15 | curr = n; 16 | } 17 | 18 | return prev; 19 | 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /Easy/219-Contains Duplicate II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool containsNearbyDuplicate(vector& nums, int k) { 4 | 5 | unordered_map m; 6 | 7 | for(int i=0;i 9){ 6 | num = num/10 + num%10; 7 | } 8 | 9 | return num; 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /Easy/26-Remove Duplicates from Sorted Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int removeDuplicates(vector& nums) { 4 | int n = nums.size(); 5 | int i=0; 6 | int j=1; 7 | 8 | if(n==0) return 0; 9 | 10 | for(j=1;j p = {2,3,5}; 5 | 6 | bool isUgly(int num) { 7 | if(num < 1) return false; 8 | 9 | for(auto& x:p){ 10 | while(num%x == 0) num /=x; 11 | } 12 | return num == 1; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /Easy/268-Missing Number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int missingNumber(vector& nums) { 4 | 5 | int n = nums.size(); 6 | int ans = 0; 7 | 8 | for(int i=1;i<=n;i++){ 9 | ans ^= i; 10 | } 11 | 12 | for(auto x : nums){ 13 | ans ^= x; 14 | } 15 | 16 | return ans; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /Easy/278-First Bad Version.cpp: -------------------------------------------------------------------------------- 1 | // The API isBadVersion is defined for you. 2 | // bool isBadVersion(int version); 3 | 4 | class Solution { 5 | public: 6 | int firstBadVersion(int n) { 7 | 8 | long int s = 1; 9 | long int e = n; 10 | 11 | while(s<=e){ 12 | long int mid = (s+e)/2; 13 | if(!isBadVersion(mid)){ 14 | s = mid+1; 15 | }else 16 | e = mid-1; 17 | } 18 | 19 | return e+1; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /Easy/28-Implement strStr().cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int strStr(string haystack, string needle) { 4 | 5 | if(needle.size() == 0) return 0; 6 | 7 | int m = haystack.size(); 8 | int n = needle.size(); 9 | 10 | for(int i=0;i<(m-n+1);i++){ 11 | int j = 0; 12 | while(j mp; 6 | 7 | int m = ransomNote.length(); 8 | int n = magazine.length(); 9 | 10 | for(int i=0;i readBinaryWatch(int num) { 4 | vector res; 5 | 6 | for(int h=0;h<12;h++){ 7 | for(int m=0;m<60;m++){ 8 | if(bitset<10>(h << 6 | m).count() == num){ 9 | res.emplace_back( to_string(h) + (m < 10 ? ":0" : ":") + to_string(m)); 10 | } 11 | } 12 | } 13 | 14 | return res; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /Easy/405-Convert a Number to Hexadecimal.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string toHex(int num) { 4 | 5 | if(num == 0) return "0"; 6 | 7 | long int n = num; 8 | string ans = ""; 9 | 10 | if(num < 0){ 11 | n = pow(2,32)-abs(n); 12 | } 13 | 14 | while(n > 0){ 15 | 16 | long int temp = (n%16); 17 | 18 | if(temp <= 9){ 19 | ans = to_string(temp) + ans; 20 | }else{ 21 | ans = char(temp+87) + ans; 22 | } 23 | 24 | n = n/16; 25 | } 26 | return ans; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /Easy/409-Longest Palindrome.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int longestPalindrome(string s) { 4 | 5 | int freq[128] = {}; 6 | for(auto c:s) freq[c]++; 7 | int odd = 0; 8 | for(auto i:freq) odd += i&1; 9 | return s.size() - odd + (odd > 0); 10 | 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /Easy/414-Third Maximum Number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int thirdMax(vector& nums) { 4 | set> s(nums.begin(),nums.end()); 5 | 6 | if(s.size() < 3) 7 | return *s.begin(); 8 | return *next(begin(s),2); 9 | } 10 | }; 11 | -------------------------------------------------------------------------------- /Easy/415-Add Strings.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string addStrings(string num1, string num2) { 4 | int carry = 0; 5 | string ans; 6 | 7 | int i = num1.length()-1; 8 | int j = num2.length()-1; 9 | 10 | while(i>=0 || j>=0 || carry >=1){ 11 | int d1 = i >=0 ? num1[i--]-'0' : 0; 12 | int d2 = j >=0 ? num2[j--]-'0' : 0; 13 | int sum = d1 + d2 + carry; 14 | carry = sum/10; 15 | sum = sum%10; 16 | ans = to_string(sum) + ans; 17 | } 18 | 19 | return ans == "" ? "0" : ans; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /Easy/434-Number of Segments in a String.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countSegments(string s) { 4 | 5 | stringstream ss(s); 6 | string word; 7 | int res = 0; 8 | 9 | while(ss >> word){ 10 | res++; 11 | } 12 | return res; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /Easy/441-Arranging Coins.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int arrangeCoins(int n) { 4 | 5 | int k = 1,ans = 0; 6 | 7 | while(n >= 0){ 8 | if(n >= k) n = n-k; 9 | else break; 10 | ans++; k++; 11 | } 12 | 13 | return ans; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /Easy/447-Number of Boomerangs.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numberOfBoomerangs(vector>& points) { 4 | int result = 0; 5 | 6 | for(int i=0;i m; 9 | 10 | for(int j=0;j& nums) { 4 | int minNo = nums[0]; 5 | int count = 0; 6 | 7 | for(auto i:nums) minNo = min(minNo,i); 8 | for(auto x:nums) count += x-minNo; 9 | 10 | return count; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /Easy/455-Assign Cookies.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findContentChildren(vector& g, vector& s) { 4 | 5 | sort(g.begin(),g.end()); 6 | sort(s.begin(),s.end()); 7 | 8 | int j = 0; 9 | int i = 0; 10 | while(i < g.size() && j < s.size()){ 11 | if(s[j] >= g[i]) 12 | i++; 13 | 14 | j++; 15 | } 16 | 17 | return i; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /Easy/461-Hamming Distance.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int hammingDistance(int x, int y) { 4 | int ans = 0; 5 | int z = x^y; 6 | for(int i=0;i<32;i++){ 7 | 8 | if(z&1){ 9 | ans++; 10 | } 11 | 12 | z = z>>1; 13 | } 14 | 15 | return ans; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /Easy/482-License Key Formatting.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string licenseKeyFormatting(string S, int K) { 4 | 5 | stack s; 6 | int cur = 0; 7 | string ans; 8 | 9 | for(auto ch:S){ 10 | if(ch != '-') 11 | s.push(ch); 12 | } 13 | 14 | while(!s.empty()){ 15 | 16 | ans += toupper(s.top()); 17 | s.pop(); 18 | cur++; 19 | 20 | if(cur == K && !s.empty()){ 21 | ans += '-'; 22 | cur = 0; 23 | } 24 | 25 | } 26 | 27 | reverse(ans.begin(),ans.end()); 28 | return ans; 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /Easy/485-Max Consecutive Ones.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findMaxConsecutiveOnes(vector& nums) { 4 | int ans = 0; 5 | int count = 0; 6 | 7 | for(int i=0;i constructRectangle(int area) { 4 | 5 | int w = sqrt(area); 6 | 7 | while(area%w != 0) w--; 8 | 9 | return {area/w,w}; 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /Easy/504-Base 7.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string convertToBase7(int num) { 4 | if(num == 0) return "0"; 5 | int d = num; 6 | string s = ""; 7 | num = abs(num); 8 | 9 | while(num){ 10 | int rem = num%7; 11 | num = num/7; 12 | s += to_string(rem); 13 | } 14 | 15 | if(d < 0) s+= "-"; 16 | 17 | reverse(s.begin(),s.end()); 18 | return s; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /Easy/506-Relative Ranks.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector findRelativeRanks(vector& nums) { 4 | 5 | int n = nums.size(); 6 | vector ans(n); 7 | 8 | if(n == 0) return ans; 9 | 10 | priority_queue> pq; 11 | string s; 12 | 13 | for(int i=0;i= i) && (num%i == 0)) x += i; 17 | 18 | return x == num; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /Easy/520-Detect Capital.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool detectCapitalUse(string word) { 4 | 5 | int ucount = 0; 6 | int lcount = 0; 7 | bool flag = false; 8 | 9 | for(int i=0;i s.end() ? s.end() : s.begin()+index+k); 8 | index += 2*k; 9 | } 10 | 11 | return s; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /Easy/551-Student Attendance Record I.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool checkRecord(string s) { 4 | 5 | int acount = 0; 6 | int lcount = 0; 7 | 8 | for(int i=0;i= 2 || lcount >2) return false; 13 | } 14 | 15 | return true; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /Easy/559-Maximum Depth of N-ary Tree.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | // Definition for a Node. 3 | class Node { 4 | public: 5 | int val; 6 | vector children; 7 | 8 | Node() {} 9 | 10 | Node(int _val) { 11 | val = _val; 12 | } 13 | 14 | Node(int _val, vector _children) { 15 | val = _val; 16 | children = _children; 17 | } 18 | }; 19 | */ 20 | 21 | class Solution { 22 | public: 23 | 24 | int maxDepth(Node* root) { 25 | if(!root) return 0; 26 | 27 | int max_depth =1; 28 | 29 | for(int i=0;ichildren.size();i++){ 30 | max_depth = max(max_depth , 1 + maxDepth(root->children[i])); 31 | } 32 | return max_depth; 33 | } 34 | }; 35 | -------------------------------------------------------------------------------- /Easy/561-Array Partition I.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int arrayPairSum(vector& nums) { 4 | 5 | sort(nums.begin(),nums.end()); 6 | 7 | int ans = 0; 8 | 9 | for(int i=0; ival; 8 | 9 | int suml = helper(root->left,fsum); 10 | int sumr = helper(root->right,fsum); 11 | 12 | fsum += abs(suml-sumr); 13 | 14 | return sum+suml+sumr; 15 | } 16 | 17 | int findTilt(TreeNode* root) { 18 | int fsum = 0; 19 | helper(root,fsum); 20 | return fsum; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Easy/566-Reshape the Matrix.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> matrixReshape(vector>& nums, int r, int c) { 4 | 5 | int m = nums.size(); 6 | int n = nums[0].size(); 7 | 8 | if(m==0) return nums; 9 | 10 | if(m*n != r*c) return nums; 11 | 12 | vector> res(r,vector (c)); 13 | 14 | int row = 0,col = 0; 15 | for(int i=0;ival == t->val 19 | && isEqual(s->right,t->right) 20 | && isEqual(s->left , t->left); 21 | } 22 | 23 | bool isSubtree(TreeNode* s, TreeNode* t) { 24 | 25 | return isEqual(s,t) || (s->left && isSubtree(s->left,t))||(s->right && isSubtree(s->right,t)); 26 | 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /Easy/575-Distribute Candies.cpp: -------------------------------------------------------------------------------- 1 | //Method 1: 2 | 3 | class Solution { 4 | public: 5 | int distributeCandies(vector& candyType) { 6 | 7 | unordered_set s(candyType.begin(),candyType.end()); 8 | int n = candyType.size(); 9 | return (s.size() > n/2) ? n/2 : s.size(); 10 | } 11 | }; 12 | 13 | //Method 2: 14 | 15 | class Solution { 16 | public: 17 | int distributeCandies(vector& candyType) { 18 | 19 | unordered_map m; 20 | int n = candyType.size(); 21 | 22 | for(auto& x:candyType) m[x]++; 23 | 24 | return (m.size() > n/2) ? n/2 : m.size(); 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /Easy/594-Longest Harmonious Subsequence.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findLHS(vector& nums) { 4 | unordered_map m; 5 | 6 | int ans = 0; 7 | 8 | for(auto& x:nums){ 9 | m[x]++; 10 | 11 | if(m.count(x+1)) 12 | ans = max(ans,m[x] + m[x+1]); 13 | 14 | if(m.count(x-1)) 15 | ans = max(ans,m[x] + m[x-1]); 16 | } 17 | 18 | return ans; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /Easy/595-Big Countries.frm: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select name, population , area 3 | from World 4 | WHERE area > 3000000 OR population > 25000000 5 | -------------------------------------------------------------------------------- /Easy/596-Classes More Than 5 Students.frm: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select class from courses 3 | group by class 4 | having count(distinct student)>=5 5 | -------------------------------------------------------------------------------- /Easy/598-Range Addition II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxCount(int m, int n, vector>& ops) { 4 | 5 | if(ops.size() == 0) return m*n; 6 | 7 | int x=INT_MAX,y=INT_MAX; 8 | 9 | for(int i=0;i findRestaurant(vector& list1, vector& list2) { 4 | 5 | vector res; 6 | unordered_map m; 7 | int min = INT_MAX; 8 | 9 | for(int i=0;i& flowerbed, int n) { 4 | 5 | flowerbed.insert(flowerbed.begin(),0); 6 | flowerbed.push_back(0); 7 | 8 | for(int i=1;ival); 18 | if(t->left) s += "(" + tree2str(t->left) + ")"; 19 | else if(t->right) s+= "()"; 20 | if(t->right) s += "(" + tree2str(t->right) + ")"; 21 | return s; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /Easy/620-Not Boring Movies.frm: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select id, movie, description, rating 3 | from cinema 4 | where (id % 2 = 1) and (description <> 'boring') 5 | order by rating desc 6 | -------------------------------------------------------------------------------- /Easy/627-Swap Salary.frm: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | update salary 3 | set sex = case when sex = 'f' then 'm' 4 | else 'f' end 5 | -------------------------------------------------------------------------------- /Easy/643-Maximum Average Subarray I.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | double findMaxAverage(vector& nums, int k) { 4 | double avgsum; 5 | for(int i=0;i findErrorNums(vector& nums) { 4 | sort(nums.begin(),nums.end()); 5 | int dup = -1; 6 | 7 | for(int i=0;i& nums) { 4 | if(nums.size() <= 1) return nums.size(); 5 | int len = 1; 6 | int ans = 1; 7 | for(int i=0;i 0){ 7 | if(cur == n%2) return false; 8 | cur = n%2; 9 | n = n/2; 10 | } 11 | return true; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /Easy/696-Count Binary Substrings.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countBinarySubstrings(string s) { 4 | int cur =1; 5 | int pre =0; 6 | int res = 0; 7 | 8 | for(int i=1;i INT_MAX || num < INT_MIN ) ? 0 : num; 13 | 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /Easy/700-Search in a Binary Search Tree.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | TreeNode* searchBST(TreeNode* root, int val) { 4 | if(root == NULL) return NULL; 5 | 6 | if(root->val == val){ 7 | return root; 8 | }else if(val < root->val){ 9 | return searchBST(root->left,val); 10 | }else{ 11 | return searchBST(root->right,val); 12 | } 13 | 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /Easy/703-Kth Largest Element in a Stream.cpp: -------------------------------------------------------------------------------- 1 | class KthLargest { 2 | public: 3 | 4 | priority_queue, greater> pq; 5 | int digit; 6 | 7 | KthLargest(int k, vector& nums) { 8 | digit = k; 9 | for(int i=0;ik) pq.pop(); 12 | } 13 | } 14 | 15 | int add(int val) { 16 | pq.push(val); 17 | if(pq.size()>digit) pq.pop(); 18 | return pq.top(); 19 | } 20 | }; 21 | 22 | /** 23 | * Your KthLargest object will be instantiated and called as such: 24 | * KthLargest* obj = new KthLargest(k, nums); 25 | * int param_1 = obj->add(val); 26 | */ 27 | -------------------------------------------------------------------------------- /Easy/717-1-bit and 2-bit Characters.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isOneBitCharacter(vector& bits) { 4 | int n = bits.size(); 5 | int i; 6 | for(i=0;i& words) { 4 | 5 | sort(words.begin(), words.end()); 6 | 7 | unordered_set s; 8 | string res; 9 | 10 | for(auto& word:words){ 11 | if(word.size() == 1 || s.count(word.substr(0,word.size()-1))){ 12 | res = word.size() > res.size() ? word : res; 13 | s.insert(word); 14 | } 15 | 16 | } 17 | 18 | return res; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /Easy/724-Find Pivot Index.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int pivotIndex(vector& nums) { 4 | 5 | int n = nums.size(); 6 | int sum = 0; 7 | int index = -1; 8 | if(n == 0) return -1; 9 | 10 | for(int i=0;i selfDividingNumbers(int left, int right) { 4 | vector ans; 5 | 6 | for(int i=left;i<=right;i++){ 7 | int temp = i; 8 | 9 | while(temp){ 10 | int r = temp%10; 11 | if(r==0 || i%r != 0){ 12 | break; 13 | } 14 | temp = temp/10; 15 | } 16 | 17 | if(temp == 0){ 18 | ans.push_back(i); 19 | } 20 | } 21 | 22 | return ans; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /Easy/744-Find Smallest Letter Greater Than Target.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | char nextGreatestLetter(vector& letters, char target) { 4 | int n = letters.size(); 5 | 6 | if(target >= letters[n-1]) return letters[0]; 7 | 8 | char ans; 9 | for(auto& ch:letters){ 10 | if(target < ch) { 11 | ans = ch; break; 12 | } 13 | } 14 | return ans; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /Easy/746-Min Cost Climbing Stairs.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minCostClimbingStairs(vector& cost) { 4 | 5 | int n = cost.size(); 6 | vector dp(n+1,0); 7 | 8 | dp[0] = cost[0]; 9 | dp[1] = cost[1]; 10 | 11 | for(int i=2;i<=n;i++){ 12 | dp[i] = (i==n ? 0:cost[i]) + min(dp[i-1],dp[i-2]); 13 | } 14 | 15 | return dp[n]; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /Easy/747-Largest Number At Least Twice of Others.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int dominantIndex(vector& nums) { 4 | 5 | int index = -1; 6 | int n = nums.size(); 7 | int maxVal = INT_MIN; 8 | 9 | if(n == 1) return 0; 10 | 11 | for(int i = 0 ; i < n ; i++){ 12 | if(nums[i] > maxVal){ 13 | maxVal = nums[i]; 14 | index = i; 15 | } 16 | } 17 | 18 | for(int i = 0 ; i < n ; i++){ 19 | 20 | if(i != index){ 21 | if(nums[i] * 2 > maxVal){ 22 | return -1; 23 | } 24 | } 25 | 26 | } 27 | return index; 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /Easy/762-Prime Number of Set Bits in Binary Representation.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | int getSetBits(int n){ 5 | int ans = 0; 6 | while(n){ 7 | ans += (n%2 == 0 ? 0:1); 8 | n = n/2; 9 | } 10 | return ans; 11 | } 12 | 13 | int countPrimeSetBits(int L, int R) { 14 | 15 | set prime{ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 }; 16 | int count = 0; 17 | for(int i=L;i<=R;i++){ 18 | int bits = getSetBits(i); 19 | count += prime.count(bits); 20 | } 21 | 22 | return count; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /Easy/804-Unique Morse Code Words.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int uniqueMorseRepresentations(vector& words) { 4 | 5 | vector key{".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."}; 6 | 7 | set s; 8 | 9 | for(int i=0;i shortestToChar(string S, char C) { 4 | 5 | int n = S.size(); 6 | vector ans(n,n); 7 | int prev = -n; 8 | 9 | for(int i = 0 ; i < S.size() ; i++){ 10 | 11 | if(S[i] == C) prev = i; 12 | ans[i] = (i - prev); 13 | 14 | } 15 | 16 | for(int i = prev ; i >= 0 ; i--){ 17 | 18 | if(S[i] == C) prev = i; 19 | ans[i] = min(ans[i], prev - i); 20 | 21 | } 22 | 23 | return ans; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /Easy/832-Flipping an Image.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> flipAndInvertImage(vector>& A) { 4 | 5 | int row = A.size(); 6 | int col = A[0].size(); 7 | 8 | for(int r=0;r& arr) { 4 | 5 | return distance(arr.begin(),max_element(arr.begin(),arr.end())); 6 | 7 | } 8 | }; 9 | -------------------------------------------------------------------------------- /Easy/876-Middle of the Linked List.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | 12 | class Solution { 13 | public: 14 | ListNode* middleNode(ListNode* head) { 15 | 16 | if(head == NULL || head->next == NULL) return head; 17 | 18 | ListNode* slow = head; 19 | ListNode* fast = head; 20 | 21 | while(fast != NULL && fast->next != NULL){ 22 | slow = slow->next; 23 | fast = fast->next->next; 24 | } 25 | 26 | return slow; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /Easy/896-Monotonic Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isMonotonic(vector& A) { 4 | 5 | bool increasing = true; 6 | bool decreasing = true; 7 | 8 | //lets check if it is monotonic increasing or not!! 9 | for(int i=0;i A[i+1]){ 11 | increasing = false; 12 | break; 13 | } 14 | } 15 | 16 | //lets check if it is monotonic decreasing or not!! 17 | for(int i=0;i sortArrayByParity(vector& A) { 4 | int s = 0; 5 | int e = A.size()-1; 6 | while(s<=e){ 7 | if(A[s] % 2 != 0){ 8 | swap(A[s],A[e--]); 9 | }else{ 10 | s++; 11 | } 12 | } 13 | 14 | return A; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /Easy/908-Smallest Range I.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int smallestRangeI(vector& A, int K) { 4 | int minn = *min_element(A.begin(),A.end()); 5 | int maxx = *max_element(A.begin(),A.end()); 6 | 7 | return max(0, maxx-minn-2*K); 8 | } 9 | }; 10 | -------------------------------------------------------------------------------- /Easy/914-X of a Kind in a Deck of Cards.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool hasGroupsSizeX(vector& deck) { 4 | unordered_map m; 5 | 6 | for(auto n:deck){ 7 | m[n]++; 8 | } 9 | 10 | int x = INT_MAX; 11 | for(auto val:m){ 12 | if(val.second < x) 13 | x = val.second; 14 | } 15 | 16 | if(x < 2) return false; 17 | 18 | for(int i=2;i<=x;i++){ 19 | 20 | bool good = true; 21 | 22 | for(auto& p:m){ 23 | if(p.second % i != 0){ 24 | good = false; 25 | break; 26 | } 27 | } 28 | 29 | if(good) return true; 30 | } 31 | 32 | return false; 33 | } 34 | }; 35 | -------------------------------------------------------------------------------- /Easy/917-Reverse Only Letters.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string reverseOnlyLetters(string S) { 4 | 5 | int i=0; 6 | int j=S.length()-1; 7 | 8 | while(i 0 && name[j-1] != typed[i])) 10 | return false; 11 | } 12 | 13 | return j == name.length(); 14 | } 15 | 16 | }; 17 | 18 | -------------------------------------------------------------------------------- /Easy/941-Valid Mountain Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool validMountainArray(vector& arr) { 4 | 5 | int n = arr.size(); 6 | if(n < 3) return false; 7 | 8 | int i=0; 9 | int j=n-1; 10 | 11 | while(i0 && arr[j] < arr[j-1]) j--; 14 | 15 | return i!=0 && j!=n-1 && i==j; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /Easy/942-DI String Match.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector diStringMatch(string S) { 4 | vector arr{}; 5 | int a = S.length() + 1; 6 | int b = -1; 7 | 8 | for(auto c: S){ 9 | if(c == 'D'){ 10 | arr.push_back(a-1); 11 | a--; 12 | }else{ 13 | arr.push_back(b+1); 14 | b++; 15 | } 16 | } 17 | 18 | if(S[S.length()-1] == 'D'){ 19 | arr.push_back(a-1); 20 | }else{ 21 | arr.push_back(b+1); 22 | } 23 | return arr; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /Easy/961-N-Repeated Element in Size 2N Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int repeatedNTimes(vector& A) { 4 | int arr[10000]{0}; 5 | for(auto x:A){ 6 | arr[x]++; 7 | } 8 | for(int i=0;i<10000;i++){ 9 | if(arr[i] >0 && arr[i] == (A.size()/2)){ 10 | return i; 11 | } 12 | } 13 | return 0; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /Easy/965-Univalued 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 | 15 | bool helper(TreeNode* root,int ans){ 16 | if(!root) return true; 17 | 18 | if(root->val != ans){ 19 | return false; 20 | } 21 | 22 | return helper(root->left,ans) && helper(root->right,ans); 23 | } 24 | 25 | bool isUnivalTree(TreeNode* root) { 26 | 27 | int rootVal = root->val; 28 | return helper(root,rootVal); 29 | 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /Easy/970-Powerful Integers.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector powerfulIntegers(int x, int y, int bound) { 4 | 5 | unordered_set s; 6 | 7 | for(int i = 1 ; i <= bound ; i *= x){ 8 | for(int j = 1 ; i+j <= bound ; j *= y){ 9 | s.insert(i+j); 10 | if(y == 1) break; 11 | } 12 | if(x == 1) break; 13 | } 14 | 15 | return vector (s.begin(), s.end()); 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /Easy/977-Squares of a Sorted Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector sortedSquares(vector& A) { 4 | 5 | for(int i=0;i> dp(m+1, vector(n+1, false)); 7 | 8 | dp[0][0] = true; 9 | 10 | for(int i = 0; i <= m ; i++){ 11 | for(int j = 1;j<=n;j++){ 12 | if(p[j-1] == '*'){ 13 | dp[i][j] = dp[i][j-2] || (i && dp[i-1][j] && (s[i-1] == p[j-2] || p[j-2] == '.') ); 14 | }else{ 15 | dp[i][j] = i && dp[i-1][j-1] && (s[i-1] == p[j-1] || p[j-1] == '.'); 16 | } 17 | } 18 | } 19 | 20 | return dp[m][n]; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Hard/1510-Stone Game IV.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool winnerSquareGame(int n) { 4 | vector dp(n+1,false); 5 | 6 | for(int i=1;i<=n;i++) 7 | { 8 | for(int j=1;j*j<=i;j++) 9 | { 10 | if(!dp[i-j*j]) 11 | { 12 | dp[i]=true; 13 | break; 14 | } 15 | } 16 | } 17 | 18 | return dp[n]; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /Hard/239-Sliding Window Maximum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector maxSlidingWindow(vector& nums, int k) { 4 | deque q; 5 | vector ans; 6 | for(int i=0;i= k-1) ans.push_back(nums[q.front()]); 12 | } 13 | return ans; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /Hard/32-Longest Valid Parentheses.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int longestValidParentheses(string s) { 4 | 5 | int len = 0; 6 | 7 | stack in; 8 | in.push(-1); 9 | 10 | for(int i=0;i& nums1, vector& nums2) { 4 | 5 | int l1 = nums1.size(); 6 | int l2 = nums2.size(); 7 | vector res(l1+l2); 8 | int i=0,j=0,k=0; 9 | 10 | while(i!=l1 && j!=l2){ 11 | if(nums1[i]>=nums2[j]){ 12 | res[k++] = nums2[j++]; 13 | }else{ 14 | res[k++] = nums1[i++]; 15 | } 16 | } 17 | 18 | while(i!=l1){ 19 | res[k++] = nums1[i++]; 20 | } 21 | while(j!=l2){ 22 | res[k++] = nums2[j++]; 23 | } 24 | 25 | if(res.size() % 2){ 26 | 27 | return res[(res.size()/2)]; 28 | 29 | } 30 | 31 | return (res[(res.size()/2)] + res[(res.size()/2) - 1]) / 2.0; 32 | 33 | } 34 | }; 35 | -------------------------------------------------------------------------------- /Hard/42-Trapping Rain Water.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int trap(vector& height) { 4 | 5 | int n = height.size(); 6 | if(n == 0) return 0; 7 | int left[30000] = {0}; 8 | int right[30000] = {0}; 9 | int sum = 0; 10 | 11 | left[0] = height[0]; 12 | for(int i=1;i=0;i--){ 18 | right[i] = max(right[i+1],height[i]); 19 | } 20 | 21 | for(int i=0;i& nums) { 4 | int curReach = 0; 5 | int jumps = 0; 6 | int maxReach = 0; 7 | 8 | for(int i=0;i maxReach){ 10 | maxReach = i + nums[i]; 11 | } 12 | 13 | if(i == curReach){ 14 | jumps++; 15 | curReach = maxReach; 16 | } 17 | } 18 | 19 | return jumps; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /Hard/458-Poor Pigs.java: -------------------------------------------------------------------------------- 1 | public int poorPigs(int buckets, int minutesToDie, int minutesToTest) { 2 | int T = minutesToTest/minutesToDie; //Trial count 3 | return (int)Math.ceil(Math.log(buckets)/Math.log(T+1)); 4 | } 5 | -------------------------------------------------------------------------------- /Hard/72-Edit Distance.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minDistance(string word1, string word2) { 4 | int m = word1.size(); 5 | int n = word2.size(); 6 | 7 | int dp[m+1][n+1]; 8 | 9 | for(int i=0;i<=m;i++){ 10 | for(int j=0;j<=n;j++){ 11 | 12 | if(i == 0){ 13 | dp[i][j] = j; 14 | } 15 | 16 | else if(j == 0){ 17 | dp[i][j] = i; 18 | } 19 | 20 | else if(word1[i-1] == word2[j-1]){ 21 | dp[i][j] = dp[i-1][j-1]; 22 | } 23 | 24 | else{ 25 | dp[i][j] = 1 + min(dp[i-1][j-1], min( dp[i][j-1], dp[i-1][j])); 26 | } 27 | } 28 | } 29 | return dp[m][n]; 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /Hard/76-Minimum Window Substring.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string minWindow(string s, string t) { 4 | unordered_map m; 5 | for(auto ch:t) m[ch]++; 6 | int count = 0; 7 | int low = 0, min_len = INT_MAX, min_start = 0; 8 | for(int high = 0; high < s.size(); high++){ 9 | if(m[s[high]] > 0) count++; 10 | m[s[high]]--; 11 | if(count == t.size()){ 12 | while(low < high && m[s[low]] < 0) m[s[low]]++, low++; 13 | if(min_len > high-low) min_len = high - (min_start=low)+1; 14 | m[s[low++]]++; 15 | count--; 16 | } 17 | } 18 | 19 | return min_len == INT_MAX ? "" : s.substr(min_start, min_len); 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /Hard/768. Max Chunks To Make Sorted II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxChunksToSorted(vector& arr) { 4 | 5 | int n = arr.size(); 6 | 7 | vector rmin(n+1); 8 | 9 | rmin[n] = INT_MAX; 10 | 11 | for(int i=n-1;i>=0;i--){ 12 | rmin[i] = min(rmin[i+1], arr[i]); 13 | } 14 | 15 | int lmax = INT_MIN; 16 | int chunks = 0; 17 | 18 | for(int i=0;i> dp(n+1, vector(k+1,0)); 6 | 7 | int m = 0; 8 | 9 | while(dp[m][k] < n){ 10 | m++; 11 | for(int i=1;i<=k;++i){ 12 | dp[m][i] = dp[m-1][i] + dp[m-1][i-1] + 1; 13 | } 14 | } 15 | 16 | return m; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /Hard/895-Maximum Frequency Stack.cpp: -------------------------------------------------------------------------------- 1 | class FreqStack { 2 | 3 | priority_queue>> q; 4 | unordered_map freq; 5 | int i = 0; 6 | 7 | public: 8 | FreqStack() { 9 | 10 | } 11 | 12 | void push(int x) { 13 | q.emplace(++freq[x], make_pair(++i,x)); 14 | } 15 | 16 | int pop() { 17 | auto x = q.top(); 18 | q.pop(); 19 | 20 | int ans = x.second.second; 21 | freq[ans]--; 22 | return ans; 23 | 24 | } 25 | }; 26 | 27 | /** 28 | * Your FreqStack object will be instantiated and called as such: 29 | * FreqStack* obj = new FreqStack(); 30 | * obj->push(x); 31 | * int param_2 = obj->pop(); 32 | */ 33 | -------------------------------------------------------------------------------- /Hard/902-Numbers At Most N Given Digit Set.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int atMostNGivenDigitSet(vector& D, int N) { 4 | 5 | if(D.size() < 1 || N <= 0) 6 | return 0; 7 | string number =to_string(N); 8 | int numberofdigits = number.size(); 9 | int result = 0; 10 | for(int i = 1; i < numberofdigits; i++) 11 | result += pow(D.size(), i); 12 | int holdresult = 1; 13 | int flag = 0; 14 | for(int i =0; i m; 13 | 14 | for(int i = 1 ; i < n+1 ; i++){ 15 | 16 | dp[i] = (2 * dp[i-1]) % mod; 17 | 18 | char ch = S[i-1]; 19 | 20 | if(m.count(ch)){ 21 | 22 | int j = m[ch]; 23 | dp[i] -= dp[j-1]; 24 | dp[i] %= mod; 25 | 26 | } 27 | 28 | m[ch] = i; 29 | } 30 | 31 | dp[n]--; 32 | 33 | if(dp[n] < 0) dp[n] += mod; 34 | 35 | return dp[n]; 36 | } 37 | }; 38 | -------------------------------------------------------------------------------- /Medium/1004. Max Ones III.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int longestOnes(vector& nums, int k) { 4 | 5 | int n = nums.size(); 6 | int j = -1; 7 | int ans = 0; 8 | int ct = 0; 9 | 10 | for(int i=0;i k){ 14 | j++; 15 | if(nums[j] == 0){ 16 | ct--; 17 | } 18 | } 19 | 20 | int len = i - j; 21 | if(len > ans){ 22 | ans = len; 23 | } 24 | } 25 | 26 | return ans; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /Medium/1007-Minimum Domino Rotations For Equal Row.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | int countDomino(vector &A,vector &B, int num){ 5 | int countA =0; 6 | int countB =0; 7 | 8 | for(int i=0;i& A, vector& B) { 20 | int res1 = countDomino(A,B,A[0]); 21 | int res2 = countDomino(A,B,B[0]); 22 | return min(res1,res2)>0 ? min(res1,res2) : max(res1,res2); 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /Medium/1008-Construct Binary Search Tree from Preorder Traversal.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | int idx = 0; 5 | TreeNode* bstFromPreorder_(vector &preorder,int lr,int rr){ 6 | 7 | if(idx >= preorder.size() || preorder[idx] < lr || preorder[idx] > rr) return NULL; 8 | 9 | TreeNode* node = new TreeNode(preorder[idx++]); 10 | node->left = bstFromPreorder_(preorder, lr, node->val); 11 | node->right = bstFromPreorder_(preorder, node->val, rr); 12 | 13 | return node; 14 | } 15 | 16 | TreeNode* bstFromPreorder(vector& preorder) { 17 | 18 | int n = preorder.size(); 19 | int lr = -(int)1e9-1; 20 | int rr = (int)1e9+1; 21 | 22 | return bstFromPreorder_(preorder,lr,rr); 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /Medium/1010-Pairs of Songs With Total Durations Divisible by 60.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numPairsDivisibleBy60(vector& time) { 4 | vector c(60,0); 5 | int res = 0; 6 | for(int t : time){ 7 | res += c[(600-t)%60]; 8 | c[t % 60]++; 9 | } 10 | return res; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /Medium/1015-Smallest Integer Divisible by K.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int smallestRepunitDivByK(int K) { 4 | 5 | for(int r=0,N=1;N<=K;N++){ 6 | if((r = (r*10 + 1) % K) == 0) 7 | return N; 8 | } 9 | return -1; 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /Medium/102-Binary Tree Level Order Traversal.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> levelOrder(TreeNode* root) { 4 | 5 | vector > v; 6 | TreeNode* n; 7 | 8 | if(root==NULL)return v; 9 | 10 | queue q; 11 | q.push(root); 12 | 13 | while(!q.empty()){ 14 | 15 | int len = q.size(); 16 | vector ans; 17 | 18 | for(int i=0;ival); 22 | 23 | if(n->left)q.push(n->left); 24 | if(n->right)q.push(n->right); 25 | } 26 | v.push_back(ans); 27 | 28 | } 29 | return v; 30 | 31 | } 32 | }; -------------------------------------------------------------------------------- /Medium/1026-Maximum Difference Between Node and Ancestor.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | int helper(TreeNode* root,int curmin,int curmax){ 5 | 6 | if(root==NULL) return curmax-curmin; 7 | 8 | curmax = max(curmax,root->val); 9 | curmin = min(curmin,root->val); 10 | 11 | int l = helper(root->left,curmin,curmax); 12 | int r = helper(root->right,curmin,curmax); 13 | 14 | return max(l,r); 15 | } 16 | 17 | int maxAncestorDiff(TreeNode* root) { 18 | 19 | if(root==NULL) return 0; 20 | 21 | return helper(root,root->val, root->val); 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /Medium/105-Construct Binary Tree from Preorder and Inorder Traversal.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | TreeNode* buildTree_(vector &preorder, int psi, int pei, vector &inorder, int isi, int iei){ 5 | 6 | if(isi > iei) return NULL; 7 | 8 | int idx = isi; 9 | while(inorder[idx] != preorder[psi]) idx++; 10 | int colse = idx - isi; 11 | 12 | TreeNode* node = new TreeNode(preorder[psi]); 13 | 14 | node->left = buildTree_(preorder, psi+1, psi+colse, inorder, isi, idx-1); 15 | node->right = buildTree_(preorder, psi+colse+1, pei, inorder, idx+1, iei); 16 | 17 | return node; 18 | } 19 | 20 | TreeNode* buildTree(vector& preorder, vector& inorder) { 21 | 22 | int n = preorder.size(); 23 | return buildTree_(preorder, 0, n-1, inorder, 0, n-1); 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /Medium/11-Container With Most Water.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxArea(vector& height) { 4 | int i=0; 5 | int j=height.size()-1; 6 | 7 | int maxAns = INT_MIN; 8 | 9 | while(i maxAns){ 18 | maxAns = area; 19 | } 20 | } 21 | 22 | return maxAns; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /Medium/1137-N-th Tribonacci Number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int tribonacci(int n) { 4 | 5 | int f[38] = {0}; 6 | 7 | f[0] = 0; f[1] = f[2] = 1; 8 | 9 | for(int i=3;i<=n;i++) 10 | f[i] = f[i-1] + f[i-2] + f[i-3]; 11 | 12 | return f[n]; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /Medium/1143-Longest Common Subsequence.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int longestCommonSubsequence(string text1, string text2) { 4 | 5 | int n1 = text1.size(); 6 | int n2 = text2.size(); 7 | 8 | int dp[n1 + 1][n2 + 1]; 9 | memset(dp, 0, sizeof dp); 10 | 11 | for(int i = n1 - 1 ; i >= 0 ; i--){ 12 | for(int j = n2 - 1 ; j >= 0 ; j--){ 13 | 14 | if(text1[i] == text2[j]){ 15 | dp[i][j] = 1 + dp[i+1][j+1]; 16 | } 17 | else{ 18 | dp[i][j] = max(dp[i+1][j], dp[i][j+1]); 19 | } 20 | 21 | } 22 | } 23 | 24 | return dp[0][0]; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /Medium/1190-Reverse Substrings Between Each Pair of Parentheses.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string reverseParentheses(string s) { 4 | 5 | stack st; 6 | string res; 7 | 8 | for(int i=0;i= val[i]){ 12 | num -= val[i]; 13 | res += sym[i]; 14 | } 15 | 16 | } 17 | 18 | return res; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /Medium/1283-Find the Smallest Divisor Given a Threshold.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int smallestDivisor(vector& nums, int threshold) { 4 | int l = 1; 5 | int r = 1000001; 6 | int ans = 0; 7 | 8 | while(l<=r){ 9 | int mid = l + (r-l)/2; 10 | long long int sum = 0; 11 | for(int i=0;ithreshold){ 20 | l = mid+1; 21 | }else{ 22 | ans = mid; 23 | r = mid-1; 24 | } 25 | } 26 | 27 | return ans; 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /Medium/1302-Deepest Leaves Sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int deepestLeavesSum(TreeNode* root) { 4 | 5 | queue q; 6 | int res = 0, i; 7 | q.push(root); 8 | 9 | while(q.size() > 0){ 10 | 11 | for(i = q.size()-1, res = 0; i >= 0 ; i--){ 12 | auto f = q.front(); 13 | q.pop(); 14 | res += f->val; 15 | 16 | if(f->left) q.push(f->left); 17 | if(f->right) q.push(f->right); 18 | } 19 | 20 | } 21 | 22 | return res; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /Medium/1306-Jump Game III.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | bool helper(vector &arr,int s,vector &v){ 5 | if((s < 0) || (s >= arr.size()) || v[s]) return false; 6 | if(arr[s] == 0) return true; 7 | v[s] = true; 8 | return helper(arr, s + arr[s], v) || helper(arr, s - arr[s], v); 9 | } 10 | 11 | bool canReach(vector& arr, int start) { 12 | vector v(arr.size(),false); 13 | return helper(arr,start,v); 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /Medium/134-Gas Station.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int canCompleteCircuit(vector& gas, vector& cost) { 4 | int sum =0; 5 | for(int i=0;i longestZigZag_(TreeNode* root){ 7 | 8 | if(root == NULL) return{-1,-1}; 9 | 10 | pair l = longestZigZag_(root->left); 11 | pair r = longestZigZag_(root->right); 12 | 13 | pair m; 14 | 15 | maxlen = max(maxlen, max(l.second, r.first) + 1); 16 | m.first = 1 + l.second; 17 | m.second = 1 + r.first; 18 | return m; 19 | } 20 | 21 | int longestZigZag(TreeNode* root) { 22 | 23 | pair p = longestZigZag_(root); 24 | return maxlen; 25 | 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /Medium/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 | 14 | TreeNode* getTargetCopy(TreeNode* original, TreeNode* cloned, TreeNode* target) { 15 | 16 | if(original == NULL || target == NULL) return NULL; 17 | 18 | if(original == target) return cloned; 19 | 20 | auto left = getTargetCopy(original->left,cloned->left,target); 21 | 22 | return left ? left : getTargetCopy(original->right, cloned->right,target); 23 | 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /Medium/138-Copy List with Random Pointer.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | Node* copyRandomList(Node* head) { 4 | if(!head) 5 | return NULL; 6 | 7 | unordered_map m; 8 | Node* temp = head; 9 | while(temp){ 10 | m[temp] = new Node(temp->val); 11 | temp = temp->next; 12 | } 13 | 14 | temp = head; 15 | 16 | while(temp){ 17 | m[temp]->next = m[temp->next]; 18 | m[temp]->random = m[temp->random]; 19 | temp = temp->next; 20 | } 21 | return m[head]; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /Medium/1423-Maximum Points You Can Obtain from Cards.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxScore(vector& cardPoints, int k) { 4 | 5 | int best_score = 0; 6 | 7 | for(int i=0;i=0;i--){ 16 | 17 | score -= cardPoints[i]; 18 | score += cardPoints[j]; 19 | 20 | if(score > best_score){ 21 | best_score = score; 22 | } 23 | 24 | j--; 25 | } 26 | 27 | return best_score; 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /Medium/1492-The kth Factor of n.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int kthFactor(int n, int k) { 4 | vector factors; 5 | int i = 1; 6 | while ((factors.size() <= k) && (i <= n)) { 7 | if (n % i == 0) 8 | factors.push_back(i); 9 | i++; 10 | } 11 | return factors.size() >= k ? factors[k-1] : -1; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /Medium/153. Find Minimum in Rotated Sorted Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findMin(vector& nums) { 4 | int n = nums.size(); 5 | int s = 0; 6 | int e = n-1; 7 | 8 | if(nums[s] <= nums[e]){ 9 | return nums[s]; 10 | } 11 | 12 | while(s <= e){ 13 | int m = (s+e)/2; 14 | 15 | if(nums[m] > nums[m+1]){ 16 | return nums[m+1]; 17 | }else if(nums[m] < nums[m-1]){ 18 | return nums[m]; 19 | }else if(nums[s] <= nums[m]){ 20 | s = m +1; 21 | }else if(nums[m] <= nums[e]){ 22 | e = m -1; 23 | } 24 | } 25 | 26 | return -1; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /Medium/1551-Minimum Operations to Make Array Equal.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minOperations(int n) { 4 | 5 | return n * n / 4 ; 6 | 7 | } 8 | }; 9 | -------------------------------------------------------------------------------- /Medium/16-3Sum Closest.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int threeSumClosest(vector& nums, int target) { 4 | 5 | sort(nums.begin(),nums.end()); 6 | 7 | int first = 0; 8 | int sum = nums[0]+nums[1]+nums[2]; 9 | 10 | while(first < nums.size()-2){ 11 | 12 | int s = first+1; 13 | int e = nums.size()-1; 14 | 15 | while(s < e){ 16 | int curr = nums[first] + nums[s] + nums[e]; 17 | if( abs(target-sum) > abs(target-curr)) sum = curr; 18 | if(curr > target) e--; 19 | else if(curr < target) s++; 20 | else return target; 21 | } 22 | 23 | first++; 24 | 25 | } 26 | 27 | return sum; 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /Medium/162-Find Peak Element.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findPeakElement(vector& nums) { 4 | int n = nums.size(); 5 | 6 | //if there is only one element in an array 7 | if(nums.size()==1)return 0; 8 | 9 | //peak is at first 10 | if(nums[0] > nums[1]) return 0; 11 | 12 | //peak is at last element 13 | if(nums.back() > nums[nums.size()-2]) return nums.size()-1; 14 | 15 | //iterate to find peak 16 | for(int i=0;i nums[max(0,i-1)]) && (nums[i] > nums[i+1])) return i; 18 | } 19 | 20 | return -1; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Medium/1641-Count Sorted Vowel Strings.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countVowelStrings(int n) { 4 | 5 | vector ans(5,1); 6 | 7 | for(int i=2;i<=n;i++){ 8 | for(int j=3;j>=0;j--){ 9 | ans[j] += ans[j+1]; 10 | } 11 | } 12 | 13 | int result = 0; 14 | for(auto x:ans){ 15 | result += x; 16 | } 17 | 18 | return result; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /Medium/1658-Minimum Operations to Reduce X to Zero.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minOperations(vector& nums, int x) { 4 | int total = 0, n = nums.size(); 5 | 6 | for(auto x:nums) total += x; 7 | 8 | int left = 0, right = 0, curr = 0, ans = -1; 9 | 10 | for( ; right < n ; right++){ 11 | curr += nums[right]; 12 | while(curr > total-x && left<=right){ 13 | curr -= nums[left]; 14 | left++; 15 | } 16 | if(curr == total-x) ans = max(ans, right-left+1); 17 | } 18 | 19 | if(ans == -1) return ans; 20 | return n-ans; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Medium/1663-Smallest String With A Given Numeric Value.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string getSmallestString(int n, int k) { 4 | string res(n, 'a'); 5 | k -= n; 6 | 7 | int i = res.size()-1; 8 | 9 | while(k > 0){ 10 | int t = min(25,k); 11 | res[i] += t; 12 | k -= t; 13 | i--; 14 | } 15 | return res; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /Medium/1669-Merge In Between Linked Lists.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* mergeInBetween(ListNode* list1, int a, int b, ListNode* list2) { 14 | 15 | ListNode* f = list1; 16 | ListNode* e = list1; 17 | 18 | while(a > 1) f = f->next, a--; 19 | while(b--) e = e->next; 20 | 21 | f->next = list2; 22 | 23 | while(list2->next != NULL) list2 = list2->next; 24 | 25 | list2->next = e->next; 26 | 27 | return list1; 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /Medium/1673-Find the Most Competitive Subsequence.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector mostCompetitive(vector& nums, int k) { 4 | 5 | vector ans; 6 | 7 | int rem = nums.size()-k; 8 | 9 | for(auto a:nums){ 10 | while(ans.size() && rem && a keyboard = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", 5 | "tuv", "wxyz"}; 6 | 7 | vector ans; 8 | 9 | void helper(string digits,string out,int i){ 10 | 11 | if(i == digits.size()){ 12 | ans.push_back(out); 13 | return; 14 | } 15 | 16 | for(int j=0;j letterCombinations(string digits) { 23 | 24 | if(digits.size() == 0)return ans; 25 | helper(digits,"",0); 26 | return ans; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /Medium/1701-Average Waiting Time.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | double averageWaitingTime(vector>& customers) { 4 | 5 | int starttime = customers[0][0]; 6 | int endtime = customers[0][0] + customers[0][1]; 7 | 8 | double ans = endtime - starttime; 9 | 10 | int n = customers.size(); 11 | 12 | for(int i=1 ; i < n ;i ++){ 13 | 14 | if(customers[i][0] > endtime){ 15 | endtime = customers[i][0] + customers[i][1]; 16 | }else{ 17 | endtime = endtime + customers[i][1]; 18 | } 19 | 20 | ans += endtime - customers[i][0]; 21 | 22 | } 23 | 24 | return ans/n; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /Medium/1750. Minimum Length of String After Deleting Similar Ends.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minimumLength(string s) { 4 | 5 | int n = s.size(); 6 | int i = 0; 7 | int j = n-1; 8 | 9 | if(s[i] != s[j]) return n; 10 | 11 | while(i < j){ 12 | 13 | if(s[i] != s[j]) break; 14 | 15 | while(i < j && s[i] == s[i+1]) i++; 16 | 17 | while(i < j && s[j] == s[i]) j--; 18 | 19 | i++; 20 | } 21 | 22 | return j - i + 1; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /Medium/187-Repeated DNA Sequences.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector findRepeatedDnaSequences(string s) { 4 | if(s.empty() || s.length()<11) 5 | return vector(); 6 | 7 | unordered_map m; 8 | vector res; 9 | 10 | for(int i=0;i0){ 13 | res.push_back(s.substr(i,10)); 14 | m[s.substr(i,10)] *= -1; 15 | } 16 | }else{ 17 | m[s.substr(i,10)]++; 18 | } 19 | } 20 | 21 | return res; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /Medium/189-Rotate Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void rotate(vector& nums, int k) { 4 | int n = nums.size(); 5 | k %= n; 6 | for(int i=0;i ans; 4 | 5 | void helper(string s,int open,int close){ 6 | if(open==0 && close==0){ 7 | ans.push_back(s); 8 | return; 9 | } 10 | 11 | if(open>0) 12 | helper(s+"(",open-1,close); 13 | if(open generateParenthesis(int n) { 18 | ans.clear(); 19 | helper("",n,n); 20 | return ans; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Medium/230-Kth Smallest Element in a BST.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | int ans,l=0; 5 | 6 | void bst(TreeNode* root,int k){ 7 | if(root){ 8 | bst(root->left,k); 9 | l++; 10 | if(l==k){ 11 | ans = root->val; 12 | return; 13 | } 14 | bst(root->right,k); 15 | } 16 | } 17 | 18 | int kthSmallest(TreeNode* root, int k) { 19 | bst(root,k); 20 | return ans; 21 | } 22 | }; -------------------------------------------------------------------------------- /Medium/238-Product of Array Except Self.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector productExceptSelf(vector& nums) { 4 | vector lhs(nums.size(), 1); 5 | int rhs = 1; 6 | for(int i = 1; i= 1 ; i--){ 10 | rhs *= nums[i]; 11 | lhs[i-1] *= rhs; 12 | } 13 | return lhs; 14 | } 15 | }; -------------------------------------------------------------------------------- /Medium/24-Swap Nodes in Pairs.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* swapPairs(ListNode* head) { 14 | ListNode* dummy = new ListNode(-1); 15 | ListNode* tail = dummy; 16 | tail->next = head; 17 | while(head && head->next){ 18 | ListNode* nxtptr = head->next->next; 19 | tail->next = head->next; 20 | (head->next)->next=head; 21 | tail = head; 22 | tail->next = nxtptr; 23 | head =nxtptr; 24 | } 25 | head=dummy->next; 26 | delete dummy; 27 | return head; 28 | 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /Medium/279-Perfect Squares.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numSquares(int n) { 4 | vector dp(n+1); 5 | 6 | dp[0] = 0; 7 | dp[1] = 1; 8 | 9 | for(int i = 2 ; i <= n ; i++){ 10 | int min_val = INT_MAX; 11 | 12 | for(int j = 1 ; j * j <= i ; j++){ 13 | 14 | int rem = i - j * j; 15 | 16 | if(dp[rem] < min_val){ 17 | min_val = dp[rem]; 18 | } 19 | 20 | } 21 | 22 | dp[i] = min_val + 1; 23 | } 24 | 25 | return dp[n]; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /Medium/29-Divide Two Integers.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int divide(int dividend, int divisor) { 4 | int sign = (dividend<0 ^ divisor<0)? -1:1; 5 | 6 | long ldividend = labs(dividend); 7 | long ldivisor = labs(divisor); 8 | 9 | long ans = exp(log(ldividend)-log(ldivisor)); 10 | 11 | if(ans>INT_MAX){ 12 | return sign==1 ? INT_MAX:INT_MIN; 13 | } 14 | return sign*(int)ans; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /Medium/300-Longest Increasing Subsequence.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int lengthOfLIS(vector& nums) { 4 | 5 | int ans = 0; 6 | int n = nums.size(); 7 | 8 | vector dp(n,0); 9 | 10 | for(int i=0;i max)){ 15 | max = dp[j]; 16 | } 17 | } 18 | 19 | dp[i] = max+1; 20 | 21 | if(dp[i] > ans){ 22 | ans = dp[i]; 23 | } 24 | } 25 | 26 | return ans; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /Medium/31-Next Permutation.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void nextPermutation(vector& nums) { 4 | 5 | int n = nums.size(), k,l; 6 | 7 | for(k=n-2;k>=0;k--){ 8 | if(nums[k] < nums[k+1]) break; 9 | } 10 | 11 | if(k < 0){ 12 | reverse(nums.begin(), nums.end()); 13 | }else{ 14 | 15 | for(l = n-1;l>k;l--){ 16 | if(nums[l] > nums[k]) break; 17 | } 18 | swap(nums[l],nums[k]); 19 | reverse(nums.begin()+k+1,nums.end()); 20 | } 21 | 22 | return; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /Medium/328-Odd Even Linked List.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | ListNode* oddEvenList(ListNode* head) { 4 | if(head==NULL)return NULL; 5 | 6 | ListNode* even = head->next; 7 | ListNode* odd = head; 8 | ListNode* evenHead = even; 9 | 10 | while(odd->next && even->next){ 11 | odd->next = even->next; 12 | odd = odd->next; 13 | even->next = odd->next; 14 | even = even->next; 15 | } 16 | 17 | odd->next = evenHead; 18 | return head; 19 | } 20 | }; -------------------------------------------------------------------------------- /Medium/334-Increasing Triplet Subsequence.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool increasingTriplet(vector& nums) { 4 | 5 | int c1 = INT_MAX; 6 | int c2 = INT_MAX; 7 | 8 | for(auto x:nums){ 9 | if(x <= c1){ 10 | c1 = x; 11 | }else if(x <= c2){ 12 | c2 = x; 13 | }else{ 14 | return true; 15 | } 16 | } 17 | 18 | return false; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /Medium/347-Top K Frequent Elements.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | bool static myComp(pair &a,pair&b){ 5 | return a.second>b.second; 6 | } 7 | 8 | vector topKFrequent(vector& nums, int k) { 9 | 10 | vector ans; 11 | unordered_map m; 12 | vector> v; 13 | 14 | for(auto x:nums){ 15 | m[x]++; 16 | } 17 | 18 | for(auto x:m){ 19 | v.push_back({x.first,x.second}); 20 | } 21 | 22 | sort(v.begin(),v.end(),myComp); 23 | 24 | if(k>m.size())return ans; 25 | 26 | for(int i=0;i>& board) { 4 | 5 | int used1[9][9] = {0}, used2[9][9] = {0}, used3[9][9] = {0}; 6 | 7 | for(int i=0;i y) { 9 | int temp = x; 10 | x = y; 11 | y = temp; 12 | } 13 | int volume = 0; 14 | while (1) { 15 | if (volume < x) 16 | volume += y; 17 | else 18 | volume -= x; 19 | if (volume == z) 20 | return true; 21 | if (volume == 0) 22 | return false; 23 | } 24 | 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /Medium/371-Sum of Two Integers.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int getSum(int a, int b) { 4 | if(b==0)return a; 5 | 6 | int s = a^b; 7 | int carry = (unsigned int)(a & b)<<1; 8 | 9 | return getSum(s,carry); 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /Medium/377. Combination Sum IV.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int combinationSum4(vector& nums, int target) { 4 | 5 | sort(nums.begin(), nums.end()); 6 | 7 | int n = nums.size(); 8 | vector dp(target+1,0); 9 | 10 | dp[0] = 1; 11 | 12 | for(int i=1;i<=target;i++){ 13 | int ways = 0; 14 | for(int j=0;j s.size()) return 0; 5 | if(k == 0) return s.size(); 6 | 7 | int map[26] = {0}; 8 | 9 | for(int i=0;i= k) idx++; 15 | if(idx == s.size()) return s.size(); 16 | 17 | int left = longestSubstring(s.substr(0,idx), k); 18 | int right = longestSubstring(s.substr(idx+1), k); 19 | 20 | return max(left,right); 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Medium/40-Combination Sum II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | void dfs(vector& candidates,int index,vector> &res,vector &v,int target){ 5 | if(target < 0) return; 6 | if(target == 0){ 7 | res.push_back(v); 8 | return; 9 | } 10 | 11 | for(int i=index;i index && candidates[i] == candidates[i-1]) continue; 13 | v.push_back(candidates[i]); 14 | dfs(candidates,i+1,res,v,target-candidates[i]); 15 | v.pop_back(); 16 | } 17 | } 18 | 19 | vector> combinationSum2(vector& candidates, int target) { 20 | sort(candidates.begin(), candidates.end()); 21 | vector> res; 22 | vector v; 23 | dfs(candidates, 0, res, v, target); 24 | return res; 25 | } 26 | };40. Combination Sum II.cpp 27 | -------------------------------------------------------------------------------- /Medium/413-Arithmetic Slices.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numberOfArithmeticSlices(vector& A) { 4 | 5 | int n = A.size(); 6 | if(n < 3) return 0; 7 | vector dp(n,0); 8 | 9 | if(A[2]-A[1] == A[1]-A[0]) dp[2] = 1; 10 | 11 | int result = dp[2]; 12 | 13 | for(int i=3;i& nums) { 4 | int sum = 0; 5 | 6 | for(auto x:nums) 7 | sum += x; 8 | 9 | if(sum%2) return false; 10 | 11 | sum /= 2; 12 | 13 | vector dp(sum+1, false); 14 | 15 | dp[0] = true; 16 | 17 | for(auto a:nums){ 18 | for(int i = sum; i >= a ; i--){ 19 | dp[i] = dp[i] || dp[i-a]; 20 | } 21 | } 22 | return dp[sum]; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /Medium/429-N-ary Tree Level Order Traversal.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> levelOrder(Node* root) { 4 | 5 | vector> ans; 6 | if(root == NULL) return ans; 7 | 8 | queue q; 9 | q.push(root); 10 | 11 | while(q.size() > 0){ 12 | int current_size = q.size(); 13 | 14 | vector v; 15 | while(current_size-- > 0){ 16 | auto node = q.front(); 17 | q.pop(); 18 | v.push_back(node->val); 19 | 20 | for(Node* child: node->children){ 21 | q.push(child); 22 | } 23 | 24 | } 25 | 26 | ans.push_back(v); 27 | } 28 | 29 | return ans; 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /Medium/443-String Compression.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int compress(vector& chars) { 4 | 5 | if(chars.size() < 2) return chars.size(); 6 | 7 | int i = 0; 8 | int j = 0; 9 | 10 | while(i < chars.size()){ 11 | 12 | chars[j] = chars[i]; 13 | int cnt = 0; 14 | while(i < chars.size() && chars[i] == chars[j]){ 15 | i++; 16 | cnt++; 17 | } 18 | 19 | if(cnt == 1){ 20 | j++; 21 | }else{ 22 | 23 | string s = to_string(cnt); 24 | for(auto ch:s) 25 | chars[++j] = ch; 26 | j++; 27 | } 28 | 29 | } 30 | 31 | return j; 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /Medium/445-Add Two Numbers II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { 4 | string a, b; 5 | ListNode *result = nullptr; 6 | while(l1) { a.push_back(l1->val+'0'); l1 = l1->next;} 7 | while(l2) { b.push_back(l2->val+'0'); l2 = l2->next;} 8 | int l = a.size()-1, r = b.size()-1, carry = 0; 9 | while(l >= 0 || r >= 0 || carry == 1) { 10 | int c = (l >= 0 ? a[l--]-'0' : 0) + ( r >= 0 ? b[r--]-'0' : 0) + carry; 11 | ListNode *temp = new ListNode(c%10); 12 | temp->next = result; 13 | result = temp; 14 | carry = c/10; 15 | } 16 | return result; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /Medium/454-4Sum II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int fourSumCount(vector& A, vector& B, vector& C, vector& D) { 4 | 5 | unordered_map m; 6 | int count = 0; 7 | 8 | for(auto a:A){ 9 | for(auto b:B) 10 | m[a+b]++; 11 | } 12 | 13 | for(auto c:C){ 14 | for(auto d:D){ 15 | auto it = m.find(0 - c - d); 16 | if(it != m.end()) count += it->second; 17 | } 18 | } 19 | 20 | return count; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Medium/456-132 Pattern.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool find132pattern(vector& nums) { 4 | 5 | stack stack; 6 | int second = -2147483648; 7 | for (int i = nums.size() - 1; i >= 0; i--) { 8 | if (nums [i] < second) 9 | return true; 10 | while (stack.size() >0 && nums [i] > stack.top()){ 11 | second = stack.top (); 12 | stack.pop(); 13 | } 14 | 15 | stack.push (nums [i]); 16 | } 17 | return false; 18 | 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /Medium/468-Validate IP Address.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string validIPAddress(string IP) { 4 | regex ipv4("(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])"), ipv6("((([0-9a-fA-F]){1,4})\\:){7}([0-9a-fA-F]){1,4}"); // create regex object for regulare expression 5 | if(regex_match(IP, ipv4)) // match regex expression with given IP string for IPv4 6 | return "IPv4"; 7 | else if(regex_match(IP, ipv6)) // match regex expression with given IP string for IPv6 8 | return "IPv6"; 9 | return "Neither"; // Otherwise return "Neither" 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /Medium/47-Permutations II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> permuteUnique(vector& nums) { 4 | vector> allPermutations {}; 5 | sort(nums.begin(), nums.end()); 6 | int index = 0; 7 | do { 8 | allPermutations.insert(allPermutations.begin() + (index++), nums); 9 | } while (next_permutation(nums.begin(), nums.end())); 10 | return allPermutations; 11 | 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /Medium/49-Group Anagrams.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> groupAnagrams(vector& strs) { 4 | 5 | vector> ans; 6 | 7 | unordered_map> m; 8 | 9 | for(auto w:strs){ 10 | string tmp = w; // store the string in a tmp variable 11 | sort(tmp.begin(), tmp.end()); //sort it 12 | m[tmp].push_back(w); //and push it in a map 13 | } 14 | 15 | for(auto x:m){ 16 | ans.push_back(x.second); 17 | } 18 | 19 | return ans; 20 | } 21 | }; 22 | 23 | //For eg: 24 | //before sorting: ["eat","tea","tan","ate","nat","bat"] 25 | //after sorting : ["aet","aet","ant","aet","ant","abt"] 26 | //it is clearly observed that, how we have to store the string in which particular bucket (map) 27 | -------------------------------------------------------------------------------- /Medium/50-Pow(x, n).cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | double myPow(double x, int n) { 4 | double res = 1; 5 | while(n){ 6 | if(n % 2) res = n > 0 ? res * x : res / x; 7 | x = x*x; 8 | n = n/2; 9 | } 10 | return res; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /Medium/503-Next Greater Element II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector nextGreaterElements(vector& nums) { 4 | 5 | int n = nums.size(); 6 | vector ans(n,0); 7 | 8 | stack s; 9 | for(int i=n-2;i>=0;i--){ 10 | while(s.size() > 0 && s.top() <= nums[i]) s.pop(); 11 | s.push(nums[i]); 12 | } 13 | 14 | for(int i=n-1;i>=0;i--){ 15 | while(s.size() > 0 && s.top() <= nums[i]) s.pop(); 16 | ans[i] = s.size() > 0 ? s.top() : -1; 17 | s.push(nums[i]); 18 | } 19 | 20 | return ans; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Medium/518-Coin Change 2.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int change(int amount, vector& coins) { 4 | 5 | int n = coins.size(); 6 | 7 | int dp[amount+1]; 8 | memset(dp, 0, sizeof dp); 9 | 10 | dp[0] = 1; 11 | 12 | for(auto coin: coins){ 13 | for(int i = coin ; i <= amount ; i++){ 14 | dp[i] += dp[i-coin]; 15 | } 16 | } 17 | 18 | return dp[amount]; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /Medium/526-Beautiful Arrangement.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | int count = 0; 5 | 6 | void getCount(vector v,int index){ 7 | if(index == v.size()){ 8 | count++; 9 | return; 10 | } 11 | 12 | for(int i=index;i v; 24 | for(int i=1;i<=n;i++){ 25 | v.push_back(i); 26 | } 27 | 28 | getCount(v,0); 29 | return count; 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /Medium/55-Jump Game.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canJump(vector& nums) { 4 | int n = nums.size(); 5 | 6 | int can_reach = 0; 7 | 8 | for(int i=0;i<=can_reach;i++){ 9 | if(i == n-1){ 10 | return true; 11 | } 12 | can_reach = max(can_reach , i+nums[i]); 13 | } 14 | 15 | return false; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /Medium/556-Next Greater Element III.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | bool nextpermutation(string& nums){ 5 | int i = nums.size()-1; 6 | while(i > 0 && nums[i-1] >= nums[i]) i--; 7 | if(i==0) return false; 8 | 9 | int j = nums.size()-1; 10 | while (j > 0 && nums[j] <= nums[i-1]) j--; 11 | swap(nums[j], nums[i-1]); 12 | reverse(nums.begin()+i, nums.end()); 13 | return true; 14 | } 15 | 16 | int nextGreaterElement(int n) { 17 | 18 | string num = to_string(n); 19 | bool res = nextpermutation(num); 20 | size_t ans = stoll(num); 21 | return(!res || ans > INT_MAX) ? -1: ans; 22 | 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /Medium/560-Subarray Sum Equals K.cpp: -------------------------------------------------------------------------------- 1 | 2 | //Problem 560: Subarray Sum Equals K (Medium) 3 | 4 | class Solution { 5 | public: 6 | int subarraySum(vector& nums, int k) { 7 | int count =0; 8 | int pre=0; 9 | 10 | unordered_map m; 11 | m[0]=1; 12 | 13 | for(int i=0;i& nums) { 4 | 5 | vector sorted(nums); 6 | sort(sorted.begin(), sorted.end()); 7 | 8 | int n = nums.size(); 9 | int i = 0; 10 | int j = n-1; 11 | 12 | while(i < n && nums[i] == sorted[i]) i++; 13 | while(j > i && nums[j] == sorted[j]) j--; 14 | 15 | return j-i+1; 16 | 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /Medium/59-Spiral Matrix II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> generateMatrix(int n) { 4 | 5 | vector> ret(n, vector(n)); 6 | int k=1,i=0; 7 | 8 | while(k <= n*n){ 9 | 10 | int j = i; 11 | while(j < n-i) 12 | ret[i][j++] = k++; 13 | 14 | j = i+1; 15 | while(j < n-i) 16 | ret[j++][n-i-1] = k++; 17 | 18 | j = n-i-2; 19 | while(j > i) 20 | ret[n-i-1][j--] = k++; 21 | 22 | j = n-i-1; 23 | while(j > i) 24 | ret[j--][i] = k++; 25 | 26 | i++; 27 | } 28 | 29 | return ret; 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /Medium/593-Valid Square.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | long long int dist(vector& p1, vector& p2){ 5 | 6 | return pow(p1[0]-p2[0],2) + pow(p1[1]-p2[1],2); 7 | 8 | } 9 | 10 | bool check(vector& p1, vector& p2, vector& p3, vector& p4){ 11 | 12 | return dist(p1,p2)>0 and dist(p1,p2)==dist(p2,p3) and dist(p2,p3)==dist(p3,p4) and dist(p3,p4)==dist(p4,p1) and dist(p1,p3)==dist(p2,p4); 13 | 14 | } 15 | 16 | 17 | bool validSquare(vector& p1, vector& p2, vector& p3, vector& p4) { 18 | 19 | return check(p1,p2,p3,p4) or check(p1,p2,p4,p3) or check(p1,p3,p2,p4); 20 | 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Medium/6-ZigZag Conversion.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string convert(string s, int numRows) { 4 | if(numRows == 1) return s; 5 | vector res(numRows); 6 | int row = 0; 7 | int increase = -1; 8 | 9 | for(int i=0;i& nums) { 4 | 5 | int count = 0; 6 | int n = nums.size(); 7 | 8 | for(int i=1;i nums[i]){ 10 | count++; 11 | if(i-2<0 || nums[i-2] <= nums[i]) nums[i-1] = nums[i]; 12 | else nums[i] = nums[i-1]; 13 | } 14 | } 15 | 16 | return count<=1; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /Medium/670. Maximum Swap: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maximumSwap(int num) { 4 | string str = to_string(num); 5 | 6 | int maxDigit = -1, maxIndex = -1; 7 | int leftIndex = -1, rightIndex = -1; 8 | 9 | for(int i=str.size()-1;i>=0;i--){ 10 | 11 | if(str[i] > maxDigit){ 12 | maxDigit = str[i]; 13 | maxIndex = i; 14 | continue; 15 | } 16 | 17 | if(str[i] < maxDigit){ 18 | leftIndex = i; 19 | rightIndex = maxIndex; 20 | } 21 | } 22 | 23 | if(leftIndex == -1) return num; 24 | 25 | swap(str[leftIndex], str[rightIndex]); 26 | 27 | num = stoi(str); 28 | 29 | return num; 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /Medium/684. Redundant Connection.cpp: -------------------------------------------------------------------------------- 1 | // 684 2 | 3 | class Solution 4 | { 5 | public: 6 | vector par; 7 | 8 | int findPar(int u) 9 | { 10 | return par[u] == u ? u : (par[u] = findPar(par[u])); 11 | } 12 | 13 | vector findRedundantConnection(vector> &edges) 14 | { 15 | 16 | int n = edges.size() + 1; 17 | 18 | for (int i = 0; i < n; i++) 19 | { 20 | par.push_back(i); 21 | } 22 | 23 | for (vector &e : edges) 24 | { 25 | int u = e[0], v = e[1]; 26 | int p1 = findPar(u), p2 = findPar(v); 27 | 28 | if (p1 != p2) 29 | { 30 | par[p1] = min(p1, p2); 31 | par[p2] = min(p1, p2); 32 | } 33 | else 34 | { 35 | return e; 36 | } 37 | } 38 | 39 | return {}; 40 | } 41 | }; 42 | -------------------------------------------------------------------------------- /Medium/74-Search a 2D Matrix.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool searchMatrix(vector>& matrix, int target) { 4 | if(matrix.size()==0)return false; 5 | 6 | int m = matrix.size(); 7 | int n = matrix[0].size(); 8 | int l=0; 9 | int r = m*n -1; 10 | 11 | while(l<=r){ 12 | int mid = l + (r-l)/2; 13 | int val = matrix[mid/n][mid%n]; 14 | if(val > target) r = mid-1; 15 | else if (val < target) l = mid+1; 16 | else return true; 17 | } 18 | return false; 19 | } 20 | }; -------------------------------------------------------------------------------- /Medium/75-Sort Colors.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void sortColors(vector& nums) { 4 | int p0 = 0; //tracking the position of '0' 5 | int p1 = 0; //for tracking the position of '1' 6 | int p2 = nums.size()-1; //for tracking the position of '2' 7 | 8 | while(p1<=p2){ 9 | 10 | if(nums[p1]==1){ 11 | 12 | p1++; 13 | 14 | }else if(nums[p1]==0){ 15 | 16 | swap(nums[p0++],nums[p1++]); 17 | 18 | }else if(nums[p1]==2){ 19 | 20 | swap(nums[p1],nums[p2]); 21 | p2--; 22 | 23 | } 24 | } 25 | 26 | return; 27 | } 28 | }; -------------------------------------------------------------------------------- /Medium/766-Toeplitz Matrix.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isToeplitzMatrix(vector>& matrix) { 4 | for(int i=1;i& arr) { 4 | 5 | int maxValueSoFar = 0, chunks = 0; 6 | 7 | for(int i=0;i> combine(int n, int k) { 4 | vector> res; 5 | vector v; 6 | dfs(res,v,1,n,k); 7 | return res; 8 | } 9 | 10 | void dfs(vector> &res,vector &v,int index,int n,int k){ 11 | if(v.size() == k){ 12 | res.push_back(v); 13 | return; 14 | } 15 | for(int i=index;i<=n;i++){ 16 | v.push_back(i); 17 | dfs(res,v,i+1,n,k); 18 | v.pop_back(); 19 | } 20 | return; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Medium/781. Rabbits in Forest.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numRabbits(vector& answers) { 4 | 5 | int n = answers.size(); 6 | 7 | unordered_map m; 8 | 9 | for(auto x : answers){ 10 | m[x]++; 11 | } 12 | 13 | int ans = 0; 14 | for(auto x : m){ 15 | int gs = x.first+1; 16 | int reportees = x.second; 17 | int ng = (int)(ceil(reportees * 1.0 / gs * 1.0)); 18 | ans += ng * gs; 19 | } 20 | 21 | return ans; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /Medium/784-Letter Case Permutation.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | void dfs(string s,int idx,vector &v){ 5 | 6 | v.push_back(s); 7 | 8 | if(idx >= s.size()) return; 9 | 10 | for(int i = idx; i < s.size(); i++){ 11 | 12 | if(isalpha(s[i])){ 13 | s[i] ^= 32; 14 | dfs(s,i+1,v); 15 | s[i] ^= 32; 16 | } 17 | 18 | } 19 | } 20 | 21 | vector letterCasePermutation(string S) { 22 | 23 | vector res; 24 | dfs(S,0,res); 25 | return res; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /Medium/788-Rotated Digits.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int rotatedDigits(int N) { 4 | int count = 0; 5 | 6 | for(int i=1;i<=N;i++){ 7 | bool flag = false; 8 | int num = i; 9 | 10 | while(num > 0){ 11 | int d = num%10; 12 | 13 | if(d==3 || d==4 || d==7){ 14 | flag = false; 15 | break; 16 | } 17 | 18 | if(d==2 || d==5 || d==6 || d==9){ 19 | flag = true; 20 | } 21 | 22 | num=num/10; 23 | } 24 | 25 | if(flag) count++; 26 | } 27 | 28 | return count; 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /Medium/796-Rotate String.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool rotateString(string A, string B) { 4 | if(A.length() != B.length()) return false; 5 | 6 | A += A; 7 | 8 | size_t found = A.find(B); 9 | 10 | if(found != string::npos){ 11 | return true; 12 | } 13 | return false; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /Medium/799-Champagne Tower.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | double champagneTower(int poured, int query_row, int query_glass) { 4 | 5 | vector> v(102,vector(102,0)); 6 | 7 | v[0][0] = poured; 8 | 9 | for(int i=0;i0){ 15 | v[i+1][j] += flow; 16 | v[i+1][j+1] += flow; 17 | } 18 | 19 | } 20 | } 21 | 22 | return min(1.0,v[query_row][query_glass]); 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /Medium/80-Remove Duplicates from Sorted Array II.cpp: -------------------------------------------------------------------------------- 1 | //Method 1: 2 | 3 | class Solution { 4 | public: 5 | int removeDuplicates(vector& nums) { 6 | 7 | int i = 0; 8 | for(int n:nums){ 9 | if(i<2 || n > nums[i-2]){ 10 | nums[i++] = n; 11 | } 12 | } 13 | 14 | return i; 15 | } 16 | }; 17 | 18 | //Method 2: 19 | 20 | class Solution { 21 | public: 22 | int removeDuplicates(vector& nums) { 23 | 24 | int n = nums.size(); 25 | int count = 0; 26 | 27 | for(int i=2;i>& points) { 4 | double area = 0; 5 | for(int i=0;i& banned) { 4 | for(auto& c:paragraph){ 5 | c = ispunct(c) ? ' ' : tolower(c); 6 | } 7 | 8 | unordered_map freq; 9 | unordered_set b(banned.begin(),banned.end()); 10 | string str,res; 11 | stringstream ss(paragraph); 12 | 13 | while(ss >> str){ 14 | if(b.count(str) == 0 && freq[res] < ++freq[str]){ 15 | res = str; 16 | } 17 | } 18 | 19 | return res; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /Medium/841-Keys and Rooms.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canVisitAllRooms(vector>& rooms) { 4 | 5 | stack s; 6 | s.push(0); 7 | 8 | unordered_set st = {0}; 9 | 10 | while(!s.empty()){ 11 | int i = s.top(); 12 | s.pop(); 13 | for(int j : rooms[i]){ 14 | if(st.count(j) == 0){ 15 | st.insert(j); 16 | s.push(j); 17 | if(rooms.size() == st.size()) return true; 18 | } 19 | } 20 | } 21 | 22 | return rooms.size() == st.size(); 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /Medium/845-Longest Mountain in Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int longestMountain(vector& a) { 4 | int n=a.size();int i=0,start=0,top=-1,len=0; 5 | while(i=a[i+1]) i++,start++; 8 | if(i==n-1) break; 9 | //see if the peak is really the peak 10 | while(ia[i+1]) i++; 13 | //calculate the total length 14 | if(top=0;i--){ 14 | K = K % size; 15 | if(K == 0 && isalpha(S[i])){ 16 | ans += S[i]; 17 | break; 18 | } 19 | 20 | if(!isalpha(S[i])){ 21 | size = size / (S[i]-'0'); 22 | }else 23 | size--; 24 | } 25 | 26 | return ans; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /Medium/881-Boats to Save People.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numRescueBoats(vector& people, int limit) { 4 | 5 | int boats = 0; 6 | sort(people.begin(), people.end()); 7 | 8 | int i=0; 9 | int j = people.size()-1; 10 | 11 | while(i <= j){ 12 | if((people[i] + people[j]) <= limit){ //if weight of two persons are within the limits 13 | boats++; 14 | i++; 15 | j--; 16 | } 17 | else if((people[i] + people[j]) > limit){ 18 | boats++; 19 | j--; 20 | } 21 | } 22 | 23 | return boats; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /Medium/90-Subsets II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | void dfs(vector &nums,vector> &res,vector &v,int index){ 5 | res.push_back(v); 6 | for(int i=index;i index && nums[i] == nums[i-1]) continue; 8 | v.push_back(nums[i]); 9 | dfs(nums,res,v,i+1); 10 | v.pop_back(); 11 | } 12 | return; 13 | } 14 | 15 | vector> subsetsWithDup(vector& nums) { 16 | sort(nums.begin(), nums.end()); 17 | vector> res; 18 | vector v; 19 | dfs(nums,res,v,0); 20 | return res; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Medium/901-Online Stock Span.cpp: -------------------------------------------------------------------------------- 1 | class StockSpanner { 2 | 3 | stack> s; 4 | //val , index 5 | int index; 6 | 7 | public: 8 | StockSpanner() { 9 | index = 0; 10 | //base value to stack 11 | s.push(make_pair(1e6, -1)); 12 | } 13 | 14 | int next(int price) { 15 | 16 | pair p; 17 | p.first = price; 18 | p.second = index; 19 | index++; 20 | 21 | while(s.size() > 0 && s.top().first <= price){ 22 | s.pop(); 23 | } 24 | 25 | int ans = p.second - s.top().second; 26 | 27 | s.push(p); 28 | 29 | return ans; 30 | } 31 | }; 32 | 33 | /** 34 | * Your StockSpanner object will be instantiated and called as such: 35 | * StockSpanner* obj = new StockSpanner(); 36 | * int param_1 = obj->next(price); 37 | */ 38 | -------------------------------------------------------------------------------- /Medium/946-Validate Stack Sequences.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool validateStackSequences(vector& pushed, vector& popped) { 4 | 5 | stack s; 6 | 7 | int i = 0; 8 | 9 | for(auto x : pushed){ 10 | 11 | s.push(x); 12 | 13 | while(!s.empty() && s.top() == popped[i]){ 14 | s.pop(); 15 | i++; 16 | } 17 | 18 | } 19 | 20 | return s.empty(); 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Medium/948-Bag of Tokens.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int bagOfTokensScore(vector& tokens, int P) { 4 | int length = tokens.size(); 5 | if(length==0)return 0; 6 | 7 | sort(tokens.begin(),tokens.end()); 8 | 9 | if(tokens[0]>P)return 0; 10 | int res=0; 11 | int j = length-1; 12 | for(int i=0;i<=j;i++){ 13 | P -= tokens[i]; 14 | if(P>=0) 15 | res++; 16 | else 17 | P += tokens[j--]; 18 | } 19 | return res; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /Medium/96-Unique Binary Search Trees.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numTrees(int n) { 4 | int dp[n+1]; 5 | 6 | memset(dp,0,sizeof dp); 7 | 8 | dp[0] = dp[1] = 1; 9 | 10 | for(int i=2;i<=n;i++){ 11 | for(int j=1;j<=i;j++){ 12 | dp[i] += dp[j-1] * dp[i-j]; 13 | } 14 | } 15 | 16 | return dp[n]; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /Medium/974. Subarray Sums Divisible by K.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int subarraysDivByK(vector& nums, int k) { 4 | 5 | int sum = 0; 6 | int ans = 0; 7 | unordered_map m; 8 | 9 | m[0] = 1; 10 | 11 | for(int i=0;i >>m; 4 | 5 | void solve(TreeNode* root,int h_level,int v_level){ 6 | if(root==NULL)return; 7 | m[h_level].insert({v_level,root->val}); 8 | solve(root->left,h_level-1,v_level+1); 9 | solve(root->right,h_level+1,v_level+1); 10 | return; 11 | } 12 | 13 | vector> verticalTraversal(TreeNode* root) { 14 | solve(root,0,0); 15 | vector> res; 16 | for(auto x:m){ 17 | vector v; 18 | for(auto val:x.second) 19 | v.push_back(val.second); 20 | res.push_back(v); 21 | } 22 | return res; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /Medium/991-Broken Calculator.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int brokenCalc(int X, int Y) { 4 | int res = 0; 5 | while (Y > X){ 6 | Y = Y % 2 > 0 ? Y + 1 : Y / 2; 7 | res++; 8 | } 9 | 10 | return res + X - Y; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LeetCode_Solutions 2 | This repository maintains the solution to LeetCode problems.
3 | Check out my leetCode Profile. [ClickHere](https://leetcode.com/pranjalmittal21/)
4 | Wanna check solutions to interviewBit problems? [ClickHere](https://github.com/pranjal021/InterviewBit-solutions) 5 | --------------------------------------------------------------------------------