├── .clang-format ├── .github ├── CODEOWNERS ├── ISSUE_TEMPLATE │ ├── config.yml │ ├── feature_request.yml │ └── other-issue.md ├── pull_request_template.md └── workflows │ └── clang-format-lint.yml ├── Blind 75 LeetCode Questions Solution └── README.md ├── Contribution Guidelines ├── How to Add LeetCode Question.md └── How to Add a new Solution Code.md ├── LeetCode Problems ├── 1 to 100 │ ├── 1. Two Sum │ │ ├── 1. Two Sum.cpp │ │ ├── 1. Two Sum.cs │ │ ├── 1. Two Sum.java │ │ └── README.md │ ├── 100. Same Tree │ │ ├── 100. Same Tree.cpp │ │ ├── 100. Same Tree.java │ │ ├── 100. Same Tree.js │ │ ├── 100. Same Tree.kt │ │ ├── 100. Same Tree.py │ │ └── README.md │ ├── 11. Container With Most Water │ │ ├── 11. Container With Most Water.cpp │ │ └── README.md │ ├── 12. Integer to Roman │ │ ├── 12. Integer to Roman.cpp │ │ └── README.md │ ├── 13. Roman to Integer │ │ ├── 13. Roman to Integer.cpp │ │ ├── 13. Roman to Integer.java │ │ └── README.md │ ├── 14. Longest Common Prefix │ │ ├── 14. Longest Common Prefix.cpp │ │ ├── 14. Longest Common Prefix.java │ │ └── README.md │ ├── 15. 3Sum │ │ ├── 15. 3Sum.cpp │ │ └── README.md │ ├── 17. Letter Combinations of a Phone Number │ │ ├── 17. Letter Combinations of a Phone Number.cpp │ │ ├── 17. Letter Combinations of a Phone Number.java │ │ └── README.md │ ├── 18. 4Sum │ │ ├── 18. 4Sum.cpp │ │ ├── 18. 4Sum.java │ │ └── README.md │ ├── 19. Remove Nth Node From End of List │ │ ├── 19. Remove Nth Node From End of List.cpp │ │ └── README.md │ ├── 2. Add Two Numbers │ │ ├── 2. Add Two Numbers.cpp │ │ ├── 2. Add Two Numbers.java │ │ └── README.md │ ├── 20. Valid Parentheses │ │ ├── 20. Valid Parentheses.cpp │ │ ├── 20. Valid Parentheses.cs │ │ └── README.md │ ├── 21. Merge Two Sorted Lists │ │ ├── 21. Merge Two Sorted Lists.cpp │ │ ├── 21. Merge Two Sorted Lists.java │ │ └── README.md │ ├── 23. Merge k Sorted Lists │ │ ├── 23. Merge k Sorted Lists.cpp │ │ ├── 23. Merge k Sorted Lists.java │ │ └── README.md │ ├── 24. Swap Nodes in Pairs │ │ ├── 24. Swap Nodes in Pairs.cpp │ │ └── README.md │ ├── 26. Remove Duplicates from Sorted Array │ │ ├── 26. Remove Duplicates from Sorted Array.cpp │ │ ├── 26. Remove Duplicates from Sorted Array.java │ │ └── README.md │ ├── 27. Remove Element │ │ ├── 27. Remove Element.cpp │ │ ├── 27. Remove Element.java │ │ └── README.md │ ├── 28. Find the Index of the First Occurrence in a String │ │ ├── 28. Find the Index of the First Occurrence in a String.java │ │ └── README.md │ ├── 29. Divide Two Integers │ │ ├── 29. Divide Two Integers.cpp │ │ └── README.md │ ├── 3. Longest Substring Without Repeating Characters │ │ ├── 3. Longest Substring Without Repeating Characters.cpp │ │ └── README.md │ ├── 31. Next Permutation │ │ ├── 31. Next Permutation.cpp │ │ ├── 31. Next Permutation.java │ │ └── README.md │ ├── 32. Longest Valid Parentheses │ │ ├── 32. Longest Valid Parentheses.cpp │ │ └── README.md │ ├── 33. Search in Rotated Sorted Array │ │ ├── 33. Search in Rotated Sorted Array.cpp │ │ ├── 33. Search in Rotated Sorted Array.java │ │ └── README.md │ ├── 34. Find First and Last Position of Element in Sorted Array │ │ ├── 34. Find First and Last Position of Element in Sorted Array.cpp │ │ └── README.md │ ├── 35. Search Insert Position │ │ ├── 35. Search Insert Position.cpp │ │ ├── 35. Search Insert Position.java │ │ └── README.md │ ├── 37. Sudoku Solver │ │ └── 37. Sudoku Solver.cpp │ ├── 38. Count and Say │ │ ├── 38. Count and Say.cpp │ │ └── README.md │ ├── 39. Combination Sum │ │ ├── 39. Combination Sum.cpp │ │ ├── 39. Combination Sum.java │ │ ├── 39. Combination Sum.py │ │ └── README.md │ ├── 4. Median of Two Sorted Arrays │ │ ├── 4. Median of Two Sorted Arrays.cpp │ │ ├── 4. Median of Two Sorted Arrays.java │ │ └── README.md │ ├── 41. First Missing Positive │ │ ├── 41. First Missing Positive.cpp │ │ ├── 41. First Missing Positive.java │ │ └── README.md │ ├── 45. Jump Game II │ │ ├── 45. Jump Game II.cpp │ │ ├── 45. Jump Game II.java │ │ └── README.md │ ├── 46. Permutations │ │ ├── 46. Permutations.cpp │ │ └── README.md │ ├── 48. Rotate Image │ │ ├── 48. Rotate Image.cpp │ │ └── README.md │ ├── 49. Group Anagrams │ │ ├── 49. Group Anagrams.cpp │ │ └── README.md │ ├── 5. Longest Palindromic Substring │ │ ├── 5. Longest Palindromic Substring.cpp │ │ └── README.md │ ├── 50. Pow(x, n) │ │ ├── 50. Pow(x, n).cpp │ │ └── README.md │ ├── 51. N-Queens │ │ └── 51. N-Queens.py │ ├── 53. Maximum Subarray │ │ └── 53. Maximum Subarray.cpp │ ├── 54. Spiral Matrix │ │ ├── 54. Spiral Matrix.cpp │ │ └── README.md │ ├── 55. Jump Game │ │ ├── 55. Jump Game.cpp │ │ ├── 55. Jump Game.java │ │ └── README.md │ ├── 56. Merge Intervals │ │ ├── 56. Merge Intervals.cpp │ │ └── 56. Merge Intervals.java │ ├── 57. Insert Interval │ │ ├── 57. Insert Interval.cpp │ │ └── 57. Insert Interval.java │ ├── 59. Spiral Matrix 2 │ │ └── 59. Spiral Matrix 2.cpp │ ├── 6. ZigZag Conversion │ │ ├── 6. ZigZag Conversion.cpp │ │ ├── 6. Zigzag Conversion.java │ │ └── README.md │ ├── 62. Unique Paths │ │ ├── 62. Unique Paths.cpp │ │ └── 62. Unique Paths.java │ ├── 64. Minimum Path Sum │ │ ├── 64. Minimum Path Sum.cpp │ │ └── README.md │ ├── 66. Plus One │ │ ├── 66. Plus One.cpp │ │ └── 66. Plus One.java │ ├── 67. Add Binary │ │ ├── 67. Add Binary.cpp │ │ └── README.md │ ├── 69. Sqrt(x) │ │ └── 69. Sqrt(x).cpp │ ├── 7. Reverse Integer │ │ ├── 7. Reverse Integer.cpp │ │ ├── 7. Reverse Integer.cs │ │ └── README.md │ ├── 70. Climbing Stairs │ │ ├── 70. Climbing Stairs.cpp │ │ ├── 70. Climbing Stairs.java │ │ └── README.md │ ├── 73. Set Matrix Zeroes │ │ ├── 73. Set Matrix Zeroes.cpp │ │ ├── 73. Set Matrix Zeroes.java │ │ └── Readme.md │ ├── 74. Search a 2D Matrix │ │ ├── 74. Search a 2D Matrix.cpp │ │ ├── 74. Search a 2D Matrix.java │ │ └── README.md │ ├── 75. Sort Colors │ │ └── 75. Sort Colors.cpp │ ├── 76. Minimum Window Substring │ │ └── 76. Minimum Window Substring.cpp │ ├── 77. Combinations │ │ ├── 77. Combinations.cpp │ │ ├── 77. Combinations.java │ │ └── README.md │ ├── 79. Word Search │ │ ├── 79. Word Search.cpp │ │ └── 79. Word Search.java │ ├── 8. String to Integer (atoi) │ │ ├── 8. String to Integer (atoi).cpp │ │ └── README.md │ ├── 80. Remove Duplicates from Sorted Array II │ │ ├── 80. Remove Duplicates from Sorted Array II.cpp │ │ ├── 80. Remove Duplicates from Sorted Array II.java │ │ └── README.md │ ├── 84. Largest Rectangle in Histogram │ │ ├── 84. Largest Rectangle in Histogram.cpp │ │ └── README.md │ ├── 88. Merge Sorted Array │ │ ├── 88. Merge Sorted Array.cpp │ │ ├── 88. Merge Sorted Array.java │ │ └── README.md │ ├── 9. Palindrome Number │ │ ├── 9. Palindrome Number.cpp │ │ └── README.md │ ├── 91. Decode Ways │ │ ├── 91. Decode Ways.cpp │ │ └── 91. Decode Ways.java │ ├── 92. Reverse Linked List II │ │ └── 92. Reverse Linked List II.cpp │ ├── 94. Binary Tree Inorder Traversal │ │ ├── 94. Binary Tree Inorder Traversal.cpp │ │ ├── 94. Binary Tree Inorder Traversal.java │ │ ├── 94. Binary Tree Inorder Traversal.py │ │ └── README.md │ ├── 97. Interleaving String │ │ └── 97. Interleaving String.cpp │ └── 98.validate-binary-search-tree │ │ └── 98.validate-binary-search-tree.cpp ├── 1001 to 1100 │ ├── 1008. Construct Binary Search Tree from Preorder Traversal │ │ ├── 1008. Construct Binary Search Tree from Preorder Traversal.cpp │ │ ├── 1008. Construct Binary Search Tree from Preorder Traversal.java │ │ └── README.md │ ├── 1022. Sum of Root To Leaf Binary Numbers │ │ └── 1022. Sum of Root To Leaf Binary Numbers.cpp │ ├── 1026. Maximum Difference Between Node and Ancestor │ │ ├── 1026. Maximum Difference Between Node and Ancestor.cpp │ │ └── 1026. Maximum Difference Between Node and Ancestor.java │ ├── 1029. Two City Scheduling │ │ └── 1029. Two City Scheduling.cpp │ ├── 1042. Flower Planting With No Adjacent │ │ └── 1042. Flower Planting With No Adjacent.java │ ├── 1047. Remove All Adjacent Duplicates In String │ │ └── 1047. Remove All Adjacent Duplicates In String.cpp │ ├── 1071. Greatest Common Divisor of Strings │ │ ├── 1071. Greatest Common Divisor of Strings.cpp │ │ ├── 1071. Greatest Common Divisor of Strings.java │ │ └── README.md │ ├── 1079. Letter Tile Possibilities │ │ └── 1079. Letter Tile Possibilities.java │ └── 1089. Duplicate Zeros │ │ └── 1089. Duplicate Zeros.cpp ├── 101 to 200 │ ├── 101. Symmetric Tree │ │ ├── 101. Symmetric Tree.cpp │ │ ├── 101. Symmetric Tree.java │ │ └── README.md │ ├── 102. Binary Tree Level Order Traversal │ │ ├── 102. Binary Tree Level Order Traversal.cpp │ │ ├── 102. Binary Tree Level Order Traversal.java │ │ └── README.md │ ├── 104. Maximum Depth of Binary Tree │ │ ├── 104. Maximum Depth of Binary Tree.cpp │ │ ├── 104. Maximum Depth of Binary Tree.java │ │ ├── 104. Maximum Depth of Binary Tree.kt │ │ ├── 104. Maximum Depth of Binary Tree.py │ │ └── README.md │ ├── 105. Construct Binary Tree from Preorder and Inorder Traversal │ │ ├── 105. Construct Binary Tree from Preorder and Inorder Traversal.cpp │ │ ├── 105. Construct Binary Tree from Preorder and Inorder Traversal.java │ │ └── README.md │ ├── 120. Triangle │ │ └── 120. Triangle.cpp │ ├── 121. Best Time to Buy and Sell Stock │ │ ├── 121. Best Time to Buy and Sell Stock.cpp │ │ ├── 121. Best Time to Buy and Sell Stock.java │ │ └── README.md │ ├── 124. Binary Tree Maximum Path Sum │ │ ├── 124. Binary Tree Maximum Path Sum.cpp │ │ └── 124. Binary Tree Maximum Path Sum.java │ ├── 125. Valid Palindrome │ │ ├── 125. Valid Palindrome.cpp │ │ └── 125. Valid Palindrome.java │ ├── 128. Longest Consecutive Sequence │ │ └── 128. Longest Consecutive Sequence.cpp │ ├── 129. Sum Root to Leaf Numbers │ │ ├── 129. Sum Root to Leaf Numbers.cpp │ │ ├── 129. Sum Root to Leaf Numbers.java │ │ └── README.md │ ├── 131. Palindrome Partitioning │ │ ├── 131. Palindrome Partitioning.cpp │ │ └── README.md │ ├── 133. Clone Graph │ │ └── 133. Clone Graph.cpp │ ├── 134. Gas Station │ │ ├── 134. Gas Station.cpp │ │ └── 134. Gas Station.java │ ├── 136. Single Number │ │ ├── 136.Single Number.cpp │ │ └── README.md │ ├── 137. Single Number II │ │ ├── 137. Single Number II.cpp │ │ ├── 137. Single Number II.java │ │ └── README.md │ ├── 139. Word Break │ │ ├── 139. Word Break.cpp │ │ ├── 139. Word Break.java │ │ ├── 139. Word Break.py │ │ └── README.md │ ├── 141. Linked List Cycle │ │ ├── 141. Linked List Cycle.cpp │ │ ├── 141. Linked List Cycle.java │ │ └── README.md │ ├── 143. Reorder List │ │ ├── 143. Reorder List.cpp │ │ ├── 143. Reorder List.java │ │ └── README.md │ ├── 144. Binary Tree Preorder Traversal │ │ ├── 144. Binary Tree Preorder Traversal.cpp │ │ ├── 144. Binary Tree Preorder Traversal.java │ │ ├── 144. Binary Tree Preorder Traversal.kt │ │ ├── 144. Binary Tree Preorder Traversal.py │ │ └── README.md │ ├── 150. Evaluate Reverse Polish Notation │ │ └── 150. Evaluate Reverse Polish Notation.cpp │ ├── 151. Reverse Words in a String │ │ ├── 151. Reverse Words in a String .cpp │ │ └── 151. Reverse Words in a String.java │ ├── 152. Maximum Product Subarray │ │ └── 152. Maximum Product Subarray.cpp │ ├── 153. Find Minimum in Rotated Sorted Array │ │ └── 153. Find Minimum in Rotated Sorted Array.cpp │ ├── 154. Find Minimum in Rotated Sorted Array II │ │ └── 154. Find Minimum in Rotated Sorted Array II.cpp │ ├── 155. Min Stack │ │ └── 155. Min Stack.cpp │ ├── 160. Intersection of Two Linked Lists │ │ └── 160. Intersection of Two Linked Lists.cpp │ ├── 162. Find Peak Element │ │ └── 162. Find Peak Element.cpp │ ├── 164. Maximum Gap │ │ └── 164. Maximum Gap.cpp │ ├── 167. Two Sum II - Input Array Is Sorted │ │ └── 167. Two Sum II - Input Array Is Sorted.cpp │ ├── 168. Excel Sheet Column Title │ │ ├── 168. Excel Sheet Column Title.cpp │ │ └── README.md │ ├── 169. Majority Element │ │ ├── 169. Majority Element.java │ │ └── README.md │ ├── 189. Rotate Array │ │ ├── 189. Rotate Array.cpp │ │ ├── 189. Rotate Array.java │ │ └── README.md │ ├── 190. Reverse Bits │ │ └── 190. Reverse Bits.cpp │ ├── 191. Number of 1 Bits │ │ └── 191. Number of 1 Bits.cpp │ ├── 198. House Robber │ │ ├── 198. House Robber.cpp │ │ └── 198. House Robber.java │ └── 200. Number of Islands │ │ └── 200. Number of Islands.cpp ├── 1101 to 1200 │ ├── 1137. N-th Tribonacci Number │ │ ├── 1137. N-th Tribonacci Number.cpp │ │ ├── 1137. N-th Tribonacci Number.java │ │ ├── 1137. N-th Tribonacci Number.kt │ │ ├── 1137. N-th Tribonacci Number.py │ │ └── README.md │ ├── 1143. Longest Common Subsequence │ │ ├── 1143. Longest Common Subsequence.cpp │ │ └── 1143. Longest Common Subsequence.java │ ├── 1160. Find Words That Can Be Formed by Characters │ │ ├── 1160. Find Words That Can Be Formed by Characters.cpp │ │ └── README.md │ ├── 1169. Invalid Transactions │ │ └── 1169. Invalid Transactions.cpp │ └── 1192. Critical Connections in a Network │ │ └── 1192. Critical Connections in a Network.java ├── 1201 to 1300 │ ├── 1207. Unique Number of Occurrences │ │ ├── 1207. Unique Number of Occurrences.cpp │ │ └── 1207. Unique Number of Occurrences.java │ ├── 1266. Minimum Time Visiting All Points │ │ ├── 1266. Minimum Time Visiting All Points.cpp │ │ ├── 1266. Minimum Time Visiting All Points.java │ │ └── README.md │ ├── 1268. Search Suggestions System │ │ └── 1268. Search Suggestions System.cpp │ ├── 1287. Element Appearing More Than 25% In Sorted Array │ │ ├── 1287. Element Appearing More Than 25% In Sorted Array.cpp │ │ ├── 1287. Element Appearing More Than 25% In Sorted Array.java │ │ └── README.md │ ├── 1295. Find Numbers with Even Number of Digits │ │ └── 1295. Find Numbers with Even Number of Digits.cpp │ └── 1299. Replace Elements with Greatest Element on Right Side │ │ └── 1299. Replace Elements with Greatest Element on Right Side.cpp ├── 1301 to 1400 │ ├── 1323. Maximum 69 Number │ │ └── 1323. Maximum 69 Number.cpp │ ├── 1332. Remove Palindromic Subsequences │ │ └── 1332. Remove Palindromic Subsequences.cpp │ ├── 1342. Number of Steps to Reduce a Number to Zero │ │ └── 1342. Number of Steps to Reduce a Number to Zero.cpp │ └── 1346. Check If N and Its Double Exist │ │ └── 1346. Check If N and Its Double Exist.cpp ├── 1401 to 1500 │ ├── 1413. Minimum Value to Get Positive Step by Step Sum │ │ └── 1413. Minimum Value to Get Positive Step by Step Sum.cpp │ ├── 1423. Maximum Points You Can Obtain from Cards │ │ └── 1423. Maximum Points You Can Obtain from Cards.cpp │ ├── 1443. Minimum Time to Collect All Apples in a Tree │ │ ├── 1443. Minimum Time to Collect All Apples in a Tree.cpp │ │ └── README.md │ ├── 1446. Consecutive Characters │ │ └── 1446. Consecutive Characters.cpp │ ├── 1448. Count Good Nodes in Binary Tree │ │ └── 1448. Count Good Nodes in Binary Tree.cpp │ ├── 1461. Check If a String Contains All Binary Codes of Size K │ │ └── 1461. Check If a String Contains All Binary Codes of Size K.cpp │ ├── 1464. Maximum Product of Two Elements in an Array │ │ ├── 1464. Maximum Product of Two Elements in an Array.cpp │ │ └── README.md │ ├── 1470. Shuffle the Array │ │ ├── 1470. Shuffle the Array.cpp │ │ └── README.md │ ├── 1480. Running Sum of 1d Array │ │ ├── 1480. Running Sum of 1d Array.cpp │ │ └── 1480. Running Sum of 1d Array.py │ ├── 1492. The kth Factor of n │ │ └── 1492. The kth Factor of n.cpp │ └── 1493. Longest Subarray of 1's After Deleting One Element │ │ └── README.md ├── 1501 to 1600 │ ├── 1502. Can Make Arithmetic Progression From Sequence │ │ ├── 1502. Can Make Arithmetic Progression From Sequence.cpp │ │ ├── 1502. Can Make Arithmetic Progression From Sequence.java │ │ └── README.md │ ├── 1512. Number of Good Pairs │ │ ├── 1512. Number of Good Pairs.cpp │ │ ├── 1512. Number of Good Pairs.java │ │ └── README.md │ ├── 1544. Make The String Great │ │ ├── 1544. Make The String Great.cpp │ │ ├── 1544. Make The String Great.java │ │ └── 1544. Make The String Great.py │ └── 1551. Minimum Operations to Make Array Equal │ │ └── 1551. Minimum Operations to Make Array Equal.cpp ├── 1601 to 1700 │ ├── 1629. Slowest Key │ │ └── 1629. Slowest Key.cpp │ ├── 1647. Minimum Deletions to Make Character Frequencies Unique │ │ ├── 1647. Minimum Deletions to Make Character Frequencies Unique.cpp │ │ ├── 1647. Minimum Deletions to Make Character Frequencies Unique.java │ │ └── README.md │ ├── 1657. Determine if Two Strings Are Close │ │ └── 1657. Determine if Two Strings Are Close.cpp │ ├── 1658. Minimum Operations to Reduce X to Zero │ │ └── 1658. Minimum Operations to Reduce X to Zero.cpp │ ├── 1662. Check If Two String Arrays are Equivalent │ │ ├── 1662. Check If Two String Arrays are Equivalent.cpp │ │ └── README.md │ ├── 1672. Richest Customer Wealth │ │ └── 1672. Richest Customer Wealth.cpp │ ├── 1685. Sum of Absolute Differences in a Sorted Array │ │ ├── 1685. Sum of Absolute Differences in a Sorted Array.cpp │ │ └── README.md │ ├── 1688. Count of Matches in Tournament │ │ ├── 1688. Count of Matches in Tournament.cpp │ │ └── README.md │ ├── 1690. Stone Game VII │ │ └── 1690. Stone Game VII.java │ ├── 1694. Reformat Phone Number │ │ └── 1694. Reformat Phone Number.cpp │ └── 1695. Maximum Erasure Value │ │ └── 1695. Maximum Erasure Value.cpp ├── 1701 to 1800 │ ├── 1704. Determine if String Halves Are Alike │ │ ├── 1704. Determine if String Halves Are Alike.cpp │ │ ├── 1704. Determine if String Halves Are Alike.java │ │ └── 1704. Determine if String Halves Are Alike.py │ ├── 1716. Calculate Money in Leetcode Bank │ │ ├── 1716. Calculate Money in Leetcode Bank.cpp │ │ ├── 1716. Calculate Money in Leetcode Bank.java │ │ └── README.md │ ├── 1721. Swapping Nodes in a Linked List │ │ └── 1721. Swapping Nodes in a Linked List.cpp │ ├── 1736. Latest Time by Replacing Hidden Digits │ │ └── 1736. Latest Time by Replacing Hidden Digits.cpp │ ├── 1748. Sum of Unique Elements │ │ └── 1748. Sum of Unique Elements.cpp │ ├── 1749. Maximum Absolute Sum of Any Subarray │ │ └── 1749. Maximum Absolute Sum of Any Subarray.cpp │ └── 1768. Merge Strings Alternately │ │ ├── 1768. Merge Strings Alternately.cpp │ │ └── README.md ├── 1801 to 1900 │ ├── 1814. Count Nice Pairs in an Array │ │ ├── 1814. Count Nice Pairs in an Array.cpp │ │ └── README.md │ ├── 1822. Sign of the Product of an Array │ │ ├── 1822. Sign of the Product of an Array.cpp │ │ └── README.md │ ├── 1832. Check if the Sentence Is Pangram │ │ └── 1832. Check if the Sentence Is Pangram.cpp │ ├── 1833. Maximum Ice Cream Bars │ │ ├── 1833. Maximum Ice Cream Bars.cpp │ │ └── 1833. Maximum Ice Cream Bars.java │ ├── 1837. Sum of Digits in Base K │ │ └── 1837. Sum of Digits in Base K.cpp │ └── 1877. Minimize Maximum Pair Sum in Array │ │ ├── 1877. Minimize Maximum Pair Sum in Array.cpp │ │ ├── 1877. Minimize Maximum Pair Sum in Array.java │ │ └── README.md ├── 1901 to 2000 │ ├── 1903. Largest Odd Number in String │ │ ├── 1903. Largest Odd Number in String.cpp │ │ ├── 1903. Largest Odd Number in String.java │ │ └── README.md │ ├── 1913. Maximum Product Difference Between Two Pairs │ │ ├── 1913. Maximum Product Difference Between Two Pairs.cpp │ │ └── README.md │ ├── 1925. Count Square Sum Triples │ │ └── 1925. Count Square Sum Triples.cpp │ ├── 1929. Concatenation of Array │ │ └── 1929. Concatenation of Array.cpp │ └── 2000. Reverse Prefix of Word │ │ └── 2000. Reverse Prefix of Word.cpp ├── 2001 to 2100 │ ├── 2001. Number of Pairs of Interchangeable Rectangles │ │ └── 2001. Number of Pairs of Interchangeable Rectangles.cpp │ ├── 2006. Count Number of Pairs With Absolute Difference K │ │ └── 2006. Count Number of Pairs With Absolute Difference K.cpp │ ├── 2007. Find Original Array From Doubled Array │ │ └── 2007. Find Original Array From Doubled Array.cpp │ ├── 2011. Final Value of Variable After Performing Operations │ │ └── 2011. Final Value of Variable After Performing Operations.cpp │ ├── 2022. Convert 1D Array Into 2D Array │ │ └── 2022. Convert 1D Array Into 2D Array.cpp │ ├── 2023. Number of Pairs of Strings With Concatenation Equal to Target │ │ └── 2023. Number of Pairs of Strings With Concatenation Equal to Target.cpp │ ├── 2024. Maximize the Confusion of an Exam │ │ ├── 2024. Maximize the Confusion of an Exam.cpp │ │ └── README.md │ └── 2038. Remove Colored Pieces if Both Neighbors are the Same Color │ │ ├── 2038. Remove Colored Pieces if Both Neighbors are the Same Color.cpp │ │ ├── 2038. Remove Colored Pieces if Both Neighbors are the Same Color.java │ │ └── README.md ├── 201 to 300 │ ├── 203. Remove Linked List Elements │ │ └── 203. Remove Linked List Elements.cpp │ ├── 206. Reverse Linked List │ │ └── 206. Reverse Linked List.cpp │ ├── 207. Course Schedule │ │ └── 207. Course Schedule.java │ ├── 208. Implement Trie (Prefix Tree) │ │ ├── 208. Implement Trie (Prefix Tree).cpp │ │ └── 208. Implement Trie (Prefix Tree).java │ ├── 209. Minimum Size Subarray Sum │ │ ├── 209. Minimum Size Subarray Sum.cpp │ │ ├── 209. Minimum Size Subarray Sum.java │ │ └── README.md │ ├── 211. Design Add and Search Words Data Structure │ │ └── 211. Design Add and Search Words Data Structure.java │ ├── 212. Word Search II │ │ ├── 212. Word Search II.cpp │ │ └── 212. Word Search II.java │ ├── 213. House Robber II │ │ └── 213. House Robber II.java │ ├── 217. Contains Duplicate │ │ └── 217. Contains Duplicate.java │ ├── 219. Contains Duplicate II │ │ └── 219. Contains Duplicate II.cpp │ ├── 222. Count Complete Tree Nodes │ │ ├── 222. Count Complete Tree Nodes.cpp │ │ └── 222. Count Complete Tree Nodes.java │ ├── 223. Rectangle Area │ │ ├── 223. Rectangle Area.cpp │ │ ├── 223. Rectangle Area.java │ │ └── 223. Rectangle Area.py │ ├── 225. Implement Stack using Queues │ │ ├── 225. Implement Stack using Queues.cpp │ │ └── README.md │ ├── 226. Invert Binary Tree │ │ ├── 226. Invert Binary Tree.cpp │ │ ├── 226. Invert Binary Tree.java │ │ └── README.md │ ├── 228. Summary Ranges │ │ ├── 228. Summary Ranges.cpp │ │ ├── 228. Summary Ranges.java │ │ └── README.md │ ├── 230. Kth Smallest Element in a BST │ │ └── 230. Kth Smallest Element in a BST.java │ ├── 234. Palindrome Linked List │ │ └── 234. Palindrome Linked List.cpp │ ├── 235. Lowest Common Ancestor of a Binary Search Tree │ │ └── 235. Lowest Common Ancestor of a Binary Search Tree.cpp │ ├── 236. Lowest Common Ancestor of a Binary Tree │ │ └── 236. Lowest Common Ancestor of a Binary Tree.java │ ├── 238. Product of Array Except Self │ │ └── 238. Product of Array Except Self.java │ ├── 242. Valid Anagram │ │ ├── 242. Valid Anagram.cpp │ │ └── README.md │ ├── 252. Meeting Rooms │ │ └── README.md │ ├── 253. Meeting Rooms II │ │ └── README.md │ ├── 258. Add Digits │ │ ├── 258. Add Digits.cpp │ │ ├── 258. Add Digits.java │ │ └── README.md │ ├── 261. Graph Valid Tree │ │ └── README.md │ ├── 263. Ugly Number │ │ ├── 263. Ugly Number.cpp │ │ ├── 263. Ugly Number.java │ │ └── 263. Ugly Number.py │ ├── 268. Missing Number │ │ └── 268. Missing Number.cpp │ ├── 269. Alien Dictionary │ │ └── README.md │ ├── 271. Encode and Decode Strings │ │ └── README.md │ ├── 278. First Bad Version │ │ └── 278. First Bad Version.cpp │ ├── 279. Perfect Squares │ │ ├── 279. Perfect Squares.cpp │ │ └── 279. Perfect Squares.java │ ├── 283. Move Zeroes │ │ ├── 283. Move Zeroes.cpp │ │ ├── 283. Move Zeroes.java │ │ └── README.md │ ├── 290. Word Pattern │ │ ├── 290. Word Pattern.cpp │ │ └── 290. Word Pattern.java │ ├── 295. Find Median from Data Stream │ │ └── 295. Find Median from Data Stream.cpp │ └── 300. Longest Increasing Subsequence │ │ └── 300. Longest Increasing Subsequence.java ├── 2201 to 2300 │ ├── 2225. Find Players With Zero or One Losses │ │ └── 2225. Find Players With Zero or One Losses.cpp │ ├── 2244. Minimum Rounds to Complete All Tasks │ │ ├── 2244. Minimum Rounds to Complete All Tasks.cpp │ │ └── 2244. Minimum Rounds to Complete All Tasks.java │ ├── 2256. Minimum Average Difference │ │ ├── 2256. Minimum Average Difference.cpp │ │ └── 2256. Minimum Average Difference.java │ ├── 2264. Largest 3-Same-Digit Number in String │ │ ├── 2264. Largest 3-Same-Digit Number in String.cpp │ │ └── README.md │ ├── 2278. Percentage of Letter in String │ │ └── 2278. Percentage of Letter in String.cpp │ ├── 2279. Maximum Bags With Full Capacity of Rocks │ │ └── 2279. Maximum Bags With Full Capacity of Rocks.cpp │ ├── 2287. Rearrange Characters to Make Target String │ │ └── 2287. Rearrange Characters to Make Target String.cpp │ ├── 2293. Min Max Game │ │ └── 2293. Min Max Game.cpp │ ├── 2294. Partition Array Such That Maximum Difference Is K │ │ └── 2294. Partition Array Such That Maximum Difference Is K.cpp │ └── 2295. Replace Elements in an Array │ │ └── 2295. Replace Elements in an Array.cpp ├── 2301 to 2400 │ ├── 2303. Calculate Amount Paid in Taxes │ │ └── 2303. Calculate Amount Paid in Taxes.cpp │ ├── 2304. Minimum Path Cost in a Grid │ │ └── 2304. Minimum Path Cost in a Grid.cpp │ ├── 2305. Fair Distribution of Cookies │ │ ├── 2305. Fair Distribution of Cookies.cpp │ │ └── README.md │ ├── 2352. Equal Row and Column Pairs │ │ ├── 2352. Equal Row and Column Pairs.cpp │ │ └── README.md │ ├── 2389. Longest Subsequence With Limited Sum │ │ ├── 2389. Longest Subsequence With Limited Sum.cpp │ │ └── 2389. Longest Subsequence With Limited Sum.java │ └── 2390. Removing Stars From a String │ │ ├── 2390. Removing Stars From a String.cpp │ │ └── README.md ├── 2401 to 2500 │ ├── 2405. Optimal Partition of String │ │ ├── 2405. Optimal Partition of String.cpp │ │ └── README.md │ ├── 2483. Minimum Penalty for a Shop │ │ ├── 2483. Minimum Penalty for a Shop.cpp │ │ └── README.md │ ├── 2485. Find the Pivot Integer │ │ └── 2485. Find the Pivot Integer.cpp │ ├── 2486. Append Characters to String to Make Subsequence │ │ └── 2486. Append Characters to String to Make Subsequence.cpp │ ├── 2487. Remove Nodes From Linked List │ │ └── 2487. Remove Nodes From Linked List.cpp │ └── 2488. Count Subarrays With Median K │ │ ├── 2488. Count Subarrays With Median K.cpp │ │ └── 2488. Count Subarrays With Median K.java ├── 2701 to 2800 │ └── 2785. Sort Vowels in a String │ │ ├── 2785. Sort Vowels in a String.cpp │ │ └── README.md ├── 3001 to 3100 │ └── 3075. Maximize Happiness of Selected Children │ │ ├── 3075. Maximize Happiness of Selected Children.cpp │ │ └── README.md ├── 301 to 400 │ ├── 304. Range Sum Query 2D - Immutable │ │ └── 304. Range Sum Query 2D - Immutable.cpp │ ├── 307. Range Sum Query - Mutable │ │ └── 307. Range Sum Query - Mutable.cpp │ ├── 318. Maximum Product of Word Lengths │ │ └── 318. Maximum Product of Word Lengths.cpp │ ├── 322. Coin Change │ │ ├── 322. Coin Change.cpp │ │ ├── 322. Coin Change.java │ │ └── README.md │ ├── 323. Number of Connected Components in an Undirected Graph │ │ └── README.md │ ├── 326. Power of Three │ │ └── 326. Power of Three.cpp │ ├── 327. Count of Range Sum │ │ └── 327. Count of Range Sum.cpp │ ├── 328. Odd Even Linked List │ │ ├── 328. Odd Even Linked List.cpp │ │ └── 328. Odd Even Linked List.java │ ├── 331. Verify Preorder Serialization of a Binary Tree │ │ ├── 331. Verify Preorder Serialization of a Binary Tree.cpp │ │ └── README.md │ ├── 332. Reconstruct Itinerary │ │ ├── 332. Reconstruct Itinerary.cpp │ │ └── README.md │ ├── 338. Counting Bits │ │ └── 338. Counting Bits.cpp │ ├── 343. Integer Break │ │ ├── 343. Integer Break.c │ │ ├── 343. Integer Break.cpp │ │ ├── 343. Integer Break.cs │ │ ├── 343. Integer Break.java │ │ ├── 343. Integer Break.js │ │ ├── 343. Integer Break.kt │ │ ├── 343. Integer Break.py │ │ └── README.md │ ├── 345. Reverse Vowels of a String │ │ └── 345. Reverse Vowels of a String.cpp │ ├── 347. Top K Frequent Elements │ │ └── 347. Top K Frequent Elements.cpp │ ├── 350. Intersection of Two Arrays II │ │ └── 350. Intersection of Two Arrays II.cpp │ ├── 371. Sum of Two Integers │ │ ├── 371. Sum of Two Integers.java │ │ └── README.md │ ├── 374. Guess Number Higher or Lower │ │ ├── 374. Guess Number Higher or Lower.cpp │ │ ├── 374. Guess Number Higher or Lower.java │ │ └── 374. Guess Number Higher or Lower.py │ ├── 378. Kth Smallest Element in a Sorted Matrix │ │ └── 378. Kth Smallest Element in a Sorted Matrix.cpp │ ├── 383. Ransom Note │ │ └── 383. Ransom Note.cpp │ └── 392. Is Subsequence │ │ ├── 392. Is Subsequence.cpp │ │ ├── 392. Is Subsequence.java │ │ └── README.md ├── 401 to 500 │ ├── 404. Sum of Left Leaves │ │ └── 404. Sum of Left Leaves.cpp │ ├── 412. Fizz Buzz │ │ └── 412. Fizz Buzz.cpp │ ├── 416. Partition Equal Subset Sum │ │ └── 416. Partition Equal Subset Sum.cpp │ ├── 417. Pacific Atlantic Water Flow │ │ └── 417. Pacific Atlantic Water Flow.cpp │ ├── 424. Longest Repeating Character Replacement │ │ ├── 424. Longest Repeating Character Replacement.cpp │ │ ├── 424. Longest Repeating Character Replacement.java │ │ └── README.md │ ├── 435. Non-overlapping Intervals │ │ └── 435. Non-overlapping Intervals.cpp │ ├── 441. Arranging Coins │ │ └── 441. Arranging Coins.cpp │ ├── 449. Serialize and Deserialize BST │ │ └── 449. Serialize and Deserialize BST.cpp │ ├── 451. Sort Characters By Frequency │ │ └── 451. Sort Characters By Frequency.cpp │ ├── 452. Minimum Number of Arrows to Burst Balloons │ │ ├── 452. Minimum Number of Arrows to Burst Balloons.cpp │ │ └── 452. Minimum Number of Arrows to Burst Balloons.java │ ├── 485. Max Consecutive Ones │ │ └── 485. Max Consecutive Ones.cpp │ └── 496. Next Greater Element I │ │ └── 496. Next Greater Element I.cpp ├── 501 to 600 │ ├── 506. Relative Ranks │ │ ├── 506. Relative Ranks.cpp │ │ └── README.md │ ├── 509. Fibonacci Number │ │ └── 509. Fibonacci Number.cpp │ ├── 518. Coin Change II │ │ ├── 518. Coin Change II.cpp │ │ ├── 518. Coin Change II.java │ │ └── README.md │ ├── 520. Detect Capital │ │ ├── 520. Detect Capital.cpp │ │ ├── 520. Detect Capital.java │ │ └── 520. Detect Capital.py │ ├── 535. Encode and Decode TinyURL │ │ └── 535. Encode and Decode TinyURL.cpp │ ├── 540. Single Element in a Sorted Array │ │ ├── 540. Single Element in a Sorted Array.cpp │ │ ├── 540. Single Element in a Sorted Array.java │ │ └── README.md │ ├── 541. Reverse String II │ │ └── 541. Reverse String II.java │ ├── 547. Number of provinces │ │ └── 547. Number of provinces.cpp │ ├── 557. Reverse Words in a String III │ │ ├── 557. Reverse Words in a String III.cpp │ │ ├── 557. Reverse Words in a String III.java │ │ └── README.md │ ├── 572. Subtree of Another Tree │ │ └── 572. Subtree of Another Tree.java │ ├── 575. Distribute Candies │ │ └── 575. Distribute Candies.cpp │ └── 589. N-ary Tree Preorder Traversal │ │ └── 589. N-ary Tree Preorder Traversal.cpp ├── 601 to 700 │ ├── 606. Construct String from Binary Tree │ │ ├── 606. Construct String from Binary Tree.java │ │ └── README.md │ ├── 611. Valid Triangle Number │ │ └── 611. Valid Triangle Number.cpp │ ├── 622. Design Circular Queue │ │ └── 622. Design Circular Queue.cpp │ ├── 623. Add One Row to Tree │ │ └── 623. Add One Row to Tree.cpp │ ├── 637. Average of Levels in Binary Tree │ │ └── 637. Average of Levels in Binary Tree.cpp │ ├── 645. Set Mismatch │ │ └── 645. Set Mismatch.cpp │ ├── 647. Palindromic Substrings │ │ └── 647. Palindromic Substrings.cpp │ ├── 653. Two Sum IV - Input is a BST │ │ └── 653. Two Sum IV - Input is a BST.cpp │ ├── 661. Image Smoother │ │ ├── 661. Image Smoother.cpp │ │ └── README.md │ ├── 667. Beautiful Arrangement II │ │ └── 667. Beautiful Arrangement II.cpp │ ├── 669. Trim a Binary Search Tree │ │ └── 669. Trim a Binary Search Tree.cpp │ ├── 690. Employee Importance │ │ └── 690. Employee Importance.java │ ├── 692. Top K Frequent Words │ │ └── 692. Top K Frequent Words.cpp │ └── 696. Count Binary Substrings │ │ └── 696. Count Binary Substrings.cpp ├── 701 to 800 │ ├── 704. Binary Search │ │ ├── 704. Binary Search.cpp │ │ ├── 704. BinarySearch.java │ │ └── README.md │ ├── 705. Design HashSet │ │ ├── 705. Design HashSet.java │ │ └── README.md │ ├── 706. Design HashMap │ │ └── 706. Design HashMap.cpp │ ├── 725. Split Linked List in Parts │ │ └── 725. Split Linked List in Parts.cpp │ ├── 729. My Calendar I │ │ └── 729. My Calendar I.cpp │ ├── 744. Find Smallest Letter Greater Than Target │ │ ├── 744. Find Smallest Letter Greater Than Target.cpp │ │ └── README.md │ ├── 746. Min Cost Climbing Stairs │ │ └── 746. Min Cost Climbing Stairs.cpp │ ├── 763. Partition Labels │ │ ├── 763. Partition Labels.cpp │ │ ├── 763. Partition Labels.java │ │ └── README.md │ └── 786. K-th Smallest Prime Fraction │ │ ├── 786. K-th Smallest Prime Fraction.cpp │ │ └── README.md ├── 801 to 900 │ ├── 804. Unique Morse Code Words │ │ └── 804. Unique Morse Code Words.cpp │ ├── 823. Binary Trees With Factors │ │ └── 823. Binary Trees With Factors.cpp │ ├── 841. Keys and Rooms │ │ └── 841. Keys and Rooms.cpp │ ├── 848. Shifting Letters │ │ └── 848. Shifting Letters.cpp │ ├── 867. Transpose Matrix │ │ ├── 867. Transpose Matrix.cpp │ │ ├── 867. Transpose Matrix.java │ │ └── README.md │ ├── 872. Leaf-Similar Trees │ │ ├── 872. Leaf-Similar Trees.cpp │ │ └── 872. Leaf-Similar Trees.java │ ├── 876. Middle of the Linked List │ │ ├── 876. Middle of the Linked List.cpp │ │ └── 876. Middle of the Linked List.java │ ├── 890. Find and Replace Pattern │ │ └── 890. Find and Replace Pattern.cpp │ ├── 896. Monotonic Array │ │ ├── 896. Monotonic Array.cpp │ │ ├── 896. Monotonic Array.java │ │ └── README.md │ └── 899. Orderly Queue │ │ └── 899. Orderly Queue.cpp └── 901 to 1000 │ ├── 901. Online Stock Span │ ├── 901. Online Stock Span.cpp │ ├── 901. Online Stock Span.java │ └── 901. Online Stock Span.py │ ├── 912. Sort an Array │ ├── 912. Sort an Array.cpp │ └── README.md │ ├── 915. Partition Array into Disjoint Intervals │ └── 915. Partition Array into Disjoint Intervals.java │ ├── 917. Reverse Only Letters │ └── 917. Reverse Only Letters.cpp │ ├── 921. Minimum Add to Make Parentheses Valid │ ├── 921. Minimum Add to Make Parentheses Valid.cpp │ ├── 921. Minimum Add to Make Parentheses Valid.java │ └── README.md │ ├── 922. Sort Array By Parity II │ └── 922. Sort Array By Parity II.cpp │ ├── 931. Minimum Falling Path Sum │ ├── 931. Minimum Falling Path Sum.cpp │ └── README.md │ ├── 938. Range Sum of BST │ ├── 938. Range Sum of BST.cpp │ └── 938. Range Sum of BST.java │ ├── 941. Valid Mountain Array │ └── 941. Valid Mountain Array.cpp │ ├── 944. Delete Columns to Make Sorted │ ├── 944. Delete Columns to Make Sorted.cpp │ └── 944. Delete Columns to Make Sorted.java │ ├── 947. Most Stones Removed with Same Row or Column │ ├── 947. Most Stones Removed with Same Row or Column.cpp │ ├── 947. Most Stones Removed with Same Row or Column.java │ └── 947. Most Stones Removed with Same Row or Column.py │ ├── 953. Verifying an Alien Dictionary │ ├── 953. Verifying an Alien Dictionary.cpp │ └── README.md │ ├── 958. Check Completeness of a Binary Tree │ ├── 958. Check Completeness of a Binary Tree.cpp │ ├── 958. Check Completeness of a Binary Tree.java │ └── README.md │ ├── 974. Subarray Sums Divisible by K │ ├── 974. Subarray Sums Divisible by K.cpp │ ├── 974. Subarray Sums Divisible by K.java │ ├── 974. Subarray Sums Divisible by K.kt │ └── README.md │ ├── 977. Squares of a Sorted Array │ └── 977. Squares of a Sorted Array.cpp │ └── 993. Cousins in Binary Tree │ └── 993. Cousins in Binary Tree.cpp ├── README.md └── SDE Interview Questions ├── American Express └── README.md ├── Apple └── README.md ├── Bloomberg └── README.md ├── Google └── README.md ├── Microsoft └── README.md ├── Top Interview 150 └── README.md ├── Uber └── README.md └── Yahoo └── README.md /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | * @md-shamim-ahmad 2 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/config.yml: -------------------------------------------------------------------------------- 1 | blank_issues_enabled: false 2 | contact_links: 3 | - name: Community Support 4 | url: https://github.com/md-shamim-ahmad/LeetCode-Problem-Solution/discussions 5 | about: Please ask and answer questions here. 6 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/other-issue.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Other issue 3 | about: " Use this for any other issues. Do NOT create blank issues " 4 | title: '' 5 | labels: bug 6 | assignees: '' 7 | 8 | --- 9 | 10 | 11 | 12 | 13 | 14 | **This is a(n):** 15 | 16 | - [ ] New solution 17 | - [ ] Update to an existing solution 18 | - [ ] Error 19 | 20 | **Details:** 21 | 22 | -------------------------------------------------------------------------------- /.github/pull_request_template.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | - [ ] I have read [CONTRIBUTING.md](https://github.com/md-shamim-ahmad/LeetCode-Problem-Solution/blob/main/Contribution%20Guidelines/How%20to%20Add%20a%20new%20Solution%20Code.md). 4 | - [ ] This pull request is all my own work -- I have not plagiarized it. 5 | - [ ] All functions and variable names follow **Clean Code** naming conventions. 6 | - [ ] This code follows the clang-format style. 7 | -------------------------------------------------------------------------------- /.github/workflows/clang-format-lint.yml: -------------------------------------------------------------------------------- 1 | name: Clang format linter 2 | on: 3 | push: {} 4 | pull_request: {} 5 | 6 | jobs: 7 | build: 8 | runs-on: ubuntu-latest 9 | 10 | steps: 11 | - uses: actions/checkout@v4 12 | - uses: DoozyX/clang-format-lint-action@v0.16.2 13 | with: 14 | source: './' 15 | extensions: 'cpp,java,cs' 16 | clangFormatVersion: 16 17 | -------------------------------------------------------------------------------- /Contribution Guidelines/How to Add a new Solution Code.md: -------------------------------------------------------------------------------- 1 | # How to Add a new Solution Code 2 | #### Follow the steps below: 3 | * Create an [**issue**](https://github.com/md-shamim-ahmad/LeetCode-Problem-Solution/issues) for the problem if the issue does not exist 4 | * Create a ```folder``` in the correct location path and the ```folder name``` and ```solution title name``` will be the same name as the ```leetcode problem name```. 5 | * Add your correct solution code in any language. 6 | * Add ```README.md``` file for that Question. Follow these [**instructions**](https://github.com/Md-Shamim-Ahmmed/LeetCode-Problem-Solution/blob/main/Contribution%20Guidlines/How%20to%20Add%20LeetCode%20Question.md) to add the ```README.md``` 7 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/1. Two Sum/1. Two Sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector twoSum(vector &nums, int target) { 4 | vector> arr; 5 | for (int i = 0; i < nums.size(); i++) 6 | arr.emplace_back(nums[i], i); 7 | sort(arr.begin(), arr.end()); 8 | 9 | int l = 0, r = nums.size() - 1; 10 | while (l <= r) { 11 | int sum = arr[l].first + arr[r].first; 12 | if (sum == target) { 13 | return {arr[l].second, arr[r].second}; 14 | } else if (sum < target) 15 | l++; 16 | else 17 | r--; 18 | } 19 | return {-1, -1}; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/1. Two Sum/1. Two Sum.cs: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int[] TwoSum(int[] nums, int target) { 3 | if (nums == null || nums.Length == 0) throw new ArgumentException("Invalid array provided"); 4 | 5 | Dictionary set = new Dictionary(); 6 | 7 | for (int i = 0; i < nums.Length; i++) { 8 | int compliment = target - nums[i]; 9 | if (set.ContainsKey(compliment)) { 10 | return new[] { set[compliment], i }; 11 | } else { 12 | if (!set.ContainsKey(nums[i])) { // To avoid duplicates 13 | set.Add(nums[i], i); 14 | } 15 | } 16 | } 17 | 18 | return new int[] {}; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/100. Same Tree/100. Same Tree.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * int val; 5 | * TreeNode left; 6 | * TreeNode right; 7 | * TreeNode() {} 8 | * TreeNode(int val) { this.val = val; } 9 | * TreeNode(int val, TreeNode left, TreeNode right) { 10 | * this.val = val; 11 | * this.left = left; 12 | * this.right = right; 13 | * } 14 | * } 15 | */ 16 | class Solution { 17 | public boolean isSameTree(TreeNode p, TreeNode q) { 18 | if (p == null && q == null) return true; 19 | if (p == null || q == null) return false; 20 | if (p.val != q.val) return false; 21 | return isSameTree(p.left, q.left) && isSameTree(p.right, q.right); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/100. Same Tree/100. Same Tree.js: -------------------------------------------------------------------------------- 1 | /* function TreeNode(val, left, right) { 2 | this.val = val === undefined ? 0 : val; 3 | this.left = left === undefined ? null : left; 4 | this.right = right === undefined ? null : right; 5 | } */ 6 | 7 | /** 8 | * @param {TreeNode} p 9 | * @param {TreeNode} q 10 | * @return {boolean} 11 | */ 12 | const isSameTree = (p, q) => { 13 | const traverse = (node, acc = []) => { 14 | acc.push(node?.val || null); 15 | if (node) traverse(node?.left, acc); 16 | if (node) traverse(node?.right, acc); 17 | return acc; 18 | }; 19 | 20 | const array1 = traverse(p); 21 | const array2 = traverse(q); 22 | 23 | return JSON.stringify(array1) === JSON.stringify(array2); 24 | }; 25 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/100. Same Tree/100. Same Tree.kt: -------------------------------------------------------------------------------- 1 | /** 2 | * Example: 3 | * var ti = TreeNode(5) 4 | * var v = ti.`val` 5 | * Definition for a binary tree node. 6 | * class TreeNode(var `val`: Int) { 7 | * var left: TreeNode? = null 8 | * var right: TreeNode? = null 9 | * } 10 | */ 11 | class Solution { 12 | fun isSameTree(p: TreeNode?, q: TreeNode?): Boolean { 13 | if(p == null && q == null) return true; 14 | if(p == null || q == null) return false; 15 | if(p.`val` != q.`val`) return false; 16 | return isSameTree(p.left, q.left) && isSameTree(p.right, q.right); 17 | } 18 | } -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/100. Same Tree/100. Same Tree.py: -------------------------------------------------------------------------------- 1 | # Definition for a binary tree node. 2 | # class TreeNode: 3 | # def __init__(self, val=0, left=None, right=None): 4 | # self.val = val 5 | # self.left = left 6 | # self.right = right 7 | class Solution: 8 | def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool: 9 | if p is None and q is None: 10 | return True 11 | if p is None or q is None: 12 | return False 13 | if p.val != q.val: 14 | return False 15 | return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right) -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/11. Container With Most Water/11. Container With Most Water.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxArea(vector &height) { 4 | int l = 0, r = height.size() - 1; 5 | int ans = INT_MIN; 6 | while (l < r) { 7 | int left = height[l], right = height[r]; 8 | ans = max(ans, min(left, right) * abs(l - r)); 9 | if (height[l] < height[r]) { 10 | while (l < r && height[l] <= left) l++; 11 | } else { 12 | while (l < r && height[r] <= right) r--; 13 | } 14 | } 15 | return ans; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/13. Roman to Integer/13. Roman to Integer.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int romanToInt(String s) { 3 | Map map = new HashMap<>(); 4 | 5 | map.put('I', 1); 6 | map.put('V', 5); 7 | map.put('X', 10); 8 | map.put('L', 50); 9 | map.put('C', 100); 10 | map.put('D', 500); 11 | map.put('M', 1000); 12 | 13 | int ans = 0; 14 | 15 | for (int i = 0; i < s.length(); i++) { 16 | if (i < s.length() - 1 && map.get(s.charAt(i)) < map.get(s.charAt(i + 1))) { 17 | ans -= map.get(s.charAt(i)); 18 | } else { 19 | ans += map.get(s.charAt(i)); 20 | } 21 | } 22 | return ans; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/14. Longest Common Prefix/14. Longest Common Prefix.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public String longestCommonPrefix(String[] strs) { 3 | Arrays.sort(strs); 4 | String s1 = strs[0]; 5 | String s2 = strs[strs.length - 1]; 6 | int idx = 0; 7 | while (idx < s1.length() && idx < s2.length()) { 8 | if (s1.charAt(idx) == s2.charAt(idx)) { 9 | idx++; 10 | } else { 11 | break; 12 | } 13 | } 14 | return s1.substring(0, idx); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/20. Valid Parentheses/20. Valid Parentheses.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isValid(string s) { 4 | stack stc; 5 | for (int i = 0; i < s.size(); i++) { 6 | if (s[i] == '(' || s[i] == '{' || s[i] == '[') { 7 | stc.push(s[i]); 8 | continue; 9 | } 10 | if (stc.empty()) return false; 11 | else if (s[i] == ')' && stc.top() != '(') 12 | return false; 13 | else if (s[i] == '}' && stc.top() != '{') 14 | return false; 15 | else if (s[i] == ']' && stc.top() != '[') 16 | return false; 17 | stc.pop(); 18 | } 19 | return stc.empty(); 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/21. Merge Two Sorted Lists/21. Merge Two Sorted Lists.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | ListNode mergeTwoLists(ListNode list1, ListNode list2) { 4 | if (list1 == null) return list2; 5 | if (list2 == null) return list1; 6 | 7 | if (list1.val < list2.val) { 8 | list1.next = mergeTwoLists(list1.next, list2); 9 | return list1; 10 | } else { 11 | list2.next = mergeTwoLists(list1, list2.next); 12 | return list2; 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/24. Swap Nodes in Pairs/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 | 12 | class Solution { 13 | public: 14 | ListNode *swapPairs(ListNode *head) { 15 | ListNode *temp = head; 16 | while (temp != nullptr) { 17 | if (temp->next != nullptr) { 18 | swap(temp->val, temp->next->val); 19 | temp = temp->next->next; 20 | } else { 21 | temp = temp->next; 22 | } 23 | } 24 | return head; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/26. Remove Duplicates from Sorted Array/26. Remove Duplicates from Sorted Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int removeDuplicates(vector &nums) { 4 | if (nums.empty()) { 5 | return 0; 6 | } 7 | for (int i = 0; i < nums.size() - 1; i++) { 8 | if (nums[i] == nums[i + 1]) { 9 | nums.erase(nums.begin() + i); 10 | i--; 11 | } 12 | } 13 | return nums.size(); 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/26. Remove Duplicates from Sorted Array/26. Remove Duplicates from Sorted Array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int removeDuplicates(int[] nums) { 3 | List list = new ArrayList<>(); 4 | for (int i = 1; i < nums.length; i++) 5 | if (nums[i] != nums[i - 1]) list.add(nums[i - 1]); 6 | list.add(nums[nums.length - 1]); 7 | int n = list.size(); 8 | for (int i = 0; i < n; i++) nums[i] = list.get(i); 9 | return n; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/27. Remove Element/27. Remove Element.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int removeElement(vector &nums, int val) { 4 | for (int i = 0; i < nums.size(); i++) { 5 | if (nums[i] == val) { 6 | nums.erase(remove(nums.begin(), nums.end(), val), nums.end()); 7 | } 8 | } 9 | return nums.size(); 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/27. Remove Element/27. Remove Element.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int removeElement(int[] nums, int val) { 3 | List list = new ArrayList<>(); 4 | for (int ele : nums) { 5 | if (ele != val) list.add(ele); 6 | } 7 | int n = list.size(); 8 | for (int i = 0; i < n; i++) nums[i] = list.get(i); 9 | return n; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/28. Find the Index of the First Occurrence in a String/28. Find the Index of the First Occurrence in a String.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int strStr(String haystack, String needle) { 3 | return haystack.indexOf(needle); 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/29. Divide Two Integers/29. Divide Two Integers.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int divide(int dividend, int divisor) { 4 | if (dividend < 0 && divisor < 0) { 5 | if (divisor == INT_MIN) 6 | divisor += 1; 7 | divisor *= -1; 8 | if (dividend == INT_MIN) 9 | dividend += 1; 10 | dividend *= -1; 11 | } 12 | return (dividend / divisor); 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/3. Longest Substring Without Repeating Characters/3. Longest Substring Without Repeating Characters.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int lengthOfLongestSubstring(string s) { 4 | if (s.size() < 1) 5 | return 0; 6 | int ans = 0; 7 | int i = 0, j = 0; 8 | set dc; 9 | while (i < s.length()) { 10 | char ch = s[i]; 11 | while (dc.find(ch) != dc.end()) { 12 | dc.erase(s[j]); 13 | ++j; 14 | } 15 | dc.insert(ch); 16 | ans = max(ans, i - j + 1); 17 | ++i; 18 | } 19 | return ans; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/31. Next Permutation/31. Next Permutation.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void nextPermutation(vector &nums) { 4 | next_permutation(nums.begin(), nums.end()); 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/32. Longest Valid Parentheses/32. Longest Valid Parentheses.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int longestValidParentheses(string s) { 4 | int l = 0, r = 0, ans = 0; 5 | for (int i = 0; i < s.size(); i++) { 6 | if (s[i] == '(') l++; 7 | if (s[i] == ')') r++; 8 | if (l == r) 9 | ans = max(ans, 2 * l); 10 | if (r > l) l = 0, r = 0; 11 | } 12 | l = 0, r = 0; 13 | for (int i = s.size() - 1; i >= 0; i--) { 14 | if (s[i] == '(') l++; 15 | if (s[i] == ')') r++; 16 | if (l == r) 17 | ans = max(ans, 2 * r); 18 | if (l > r) l = 0, r = 0; 19 | } 20 | return ans; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/33. Search in Rotated Sorted Array/33. Search in Rotated Sorted Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int search(vector &nums, int target) { 4 | for (int i = 0; i < nums.size(); i++) { 5 | if (nums[i] == target) 6 | return i; 7 | } 8 | return -1; 9 | } 10 | }; 11 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/33. Search in Rotated Sorted Array/33. Search in Rotated Sorted Array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int search(int[] nums, int target) { 3 | for (int i = 0; i < nums.length; i++) 4 | if (target == nums[i]) return i; 5 | return -1; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/34. Find First and Last Position of Element in Sorted Array/34. Find First and Last Position of Element in Sorted Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector searchRange(vector &nums, int target) { 4 | int f = -1, l = -1; 5 | int cnt = 0; 6 | for (int i = 0; i < nums.size(); i++) { 7 | if (nums[i] == target && cnt <= 0) { 8 | ++cnt; 9 | f = i; 10 | continue; 11 | } 12 | if (nums[i] == target && cnt >= 1) 13 | l = i; 14 | } 15 | if (l == -1) 16 | l = f; 17 | return {f, l}; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/35. Search Insert Position/35. Search Insert Position.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int searchInsert(vector &nums, int target) { 4 | int f = -1, l = nums.size(); 5 | while (l > f + 1) { 6 | int m = (l + f) / 2; 7 | if (nums[m] == target) 8 | return m; 9 | else if (nums[m] < target) 10 | f = m; 11 | else 12 | l = m; 13 | } 14 | return l; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/35. Search Insert Position/35. Search Insert Position.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int searchInsert(int[] nums, int target) { 3 | int f = -1, l = nums.length; 4 | while (l > f + 1) { 5 | int m = (l + f) / 2; 6 | if (nums[m] == target) 7 | return m; 8 | else if (nums[m] < target) 9 | f = m; 10 | else 11 | l = m; 12 | } 13 | return l; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/38. Count and Say/38. Count and Say.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string countAndSay(int n) { 4 | if (n == 1) 5 | return "1"; 6 | else { 7 | string s = countAndSay(n - 1), generate_str = ""; 8 | int cnt = 1, i = 0; 9 | for (i = 1; i < s.size(); i++) { 10 | if (s[i] != s[i - 1]) { 11 | generate_str += to_string(cnt) + s[i - 1]; 12 | cnt = 1; 13 | } else 14 | cnt++; 15 | } 16 | if (s[i] != s[i - 1]) { 17 | generate_str += to_string(cnt) + s[i - 1]; 18 | cnt = 1; 19 | } 20 | return generate_str; 21 | } 22 | } 23 | }; -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/39. Combination Sum/39. Combination Sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> combinationSum(vector &candidates, int target) { 4 | } 5 | }; 6 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/39. Combination Sum/39. Combination Sum.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: 3 | def backtrack(remaining, current, start): 4 | if remaining == 0: 5 | result.append(current[:]) 6 | return 7 | if remaining < 0: 8 | return 9 | 10 | for i in range(start, len(candidates)): 11 | current.append(candidates[i]) 12 | backtrack(remaining - candidates[i], current, i) # Recurse with the same candidate allowed 13 | current.pop() 14 | 15 | result = [] 16 | backtrack(target, [], 0) 17 | return result 18 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/4. Median of Two Sorted Arrays/4. Median of Two Sorted Arrays.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | double findMedianSortedArrays(vector nums1, vector &nums2) { 4 | for (int i = 0; i < nums2.size(); i++) 5 | nums1.push_back(nums2[i]); 6 | sort(nums1.begin(), nums1.end()); 7 | if (nums1.size() & 1) 8 | return nums1[nums1.size() / 2]; 9 | else 10 | return (double) (nums1[nums1.size() / 2 - 1] + nums1[nums1.size() / 2]) / 2; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/41. First Missing Positive/41. First Missing Positive.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int firstMissingPositive(vector &nums) { 4 | int n = nums.size(); 5 | 6 | for (int i = 0; i < n; i++) { 7 | if (nums[i] <= 0) { 8 | nums[i] = n + 2; 9 | } 10 | } 11 | 12 | for (int i = 0; i < n; i++) { 13 | if (abs(nums[i]) <= n) { 14 | nums[abs(nums[i]) - 1] = -(abs(nums[abs(nums[i]) - 1])); 15 | } 16 | } 17 | 18 | for (int i = 0; i < n; i++) { 19 | if (nums[i] >= 0) { 20 | return i + 1; 21 | } 22 | } 23 | 24 | return n + 1; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/45. Jump Game II/45. Jump Game II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int jump(vector &nums) { 4 | for (int i = 1; i < nums.size(); i++) { 5 | nums[i] = max(nums[i] + i, nums[i - 1]); 6 | } 7 | int ind = 0; 8 | int ans = 0; 9 | while (ind < nums.size() - 1) { 10 | ans++; 11 | ind = nums[ind]; 12 | } 13 | return ans; 14 | } 15 | }; -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/45. Jump Game II/45. Jump Game II.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int jump(int[] nums) { 3 | int farthest = 0, jump = 0, lastPossiblePathIndex = 0; 4 | for (int index = 0; index < nums.length - 1; index++) { 5 | final int maxPossibleDistance = nums[index] + index; 6 | if (farthest < maxPossibleDistance) { 7 | farthest = maxPossibleDistance; 8 | } 9 | if (index == lastPossiblePathIndex) { 10 | lastPossiblePathIndex = farthest; 11 | jump++; 12 | } 13 | } 14 | return jump; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/46. Permutations/46. Permutations.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> permute(vector &nums) { 4 | int n = nums.size(); 5 | sort(nums.begin(), nums.end()); 6 | vector> permuteArr; 7 | do { 8 | vector a; 9 | for (int i = 0; i < n; i++) 10 | a.push_back(nums[i]); 11 | permuteArr.push_back(a); 12 | } while (next_permutation(nums.begin(), nums.end())); 13 | return permuteArr; 14 | } 15 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/48. Rotate Image/48. Rotate Image.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void rotate(vector> &matrix) { 4 | vector> arr(matrix.size(), vector(matrix.size())); 5 | for (int i = 0; i < matrix.size(); i++) { 6 | int k = 0; 7 | for (int j = matrix[i].size() - 1; j >= 0; j--) { 8 | arr[i][k++] = matrix[j][i]; 9 | } 10 | } 11 | for (int i = 0; i < matrix.size(); i++) { 12 | for (int j = 0; j < matrix.size(); j++) { 13 | matrix[i][j] = arr[i][j]; 14 | } 15 | } 16 | } 17 | }; -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/49. Group Anagrams/49. Group Anagrams.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> groupAnagrams(vector &strs) { 4 | map> mp; 5 | for (int i = 0; i < strs.size(); i++) { 6 | string s = strs[i]; 7 | sort(s.begin(), s.end()); 8 | mp[s].push_back(strs[i]); 9 | } 10 | vector> result; 11 | for (auto item: mp) { 12 | result.push_back(item.second); 13 | } 14 | return result; 15 | } 16 | }; -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/50. Pow(x, n)/50. Pow(x, n).cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | double myPow(double x, int n) { 4 | return powf64x(x, n); 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/51. N-Queens/51. N-Queens.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def solveNQueens(self, n): 3 | def DFS(queens, xy_dif, xy_sum): 4 | p = len(queens) 5 | if p==n: 6 | result.append(queens) 7 | return None 8 | for q in range(n): 9 | if q not in queens and p-q not in xy_dif and p+q not in xy_sum: 10 | DFS(queens+[q], xy_dif+[p-q], xy_sum+[p+q]) 11 | result = [] 12 | DFS([],[],[]) 13 | return [ ["."*i + "Q" + "."*(n-i-1) for i in sol] for sol in result] 14 | 15 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/53. Maximum Subarray/53. Maximum Subarray.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxSubArray(vector &arr) { 4 | int current_maximum = arr[0]; 5 | int maximum_so_far = arr[0]; 6 | 7 | for (int i = 1; i < arr.size(); i++) { 8 | current_maximum = max(arr[i], current_maximum + arr[i]); 9 | maximum_so_far = max(maximum_so_far, current_maximum); 10 | } 11 | return maximum_so_far; 12 | } 13 | }; -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/54. Spiral Matrix/README.md: -------------------------------------------------------------------------------- 1 | # [54. Spiral Matrix](https://leetcode.com/problems/spiral-matrix/description/) 2 | 3 | Given an ```m x n``` ```matrix```, return all elements of the ```matrix``` in spiral order. 4 | 5 | #### Example 1: 6 | ![](https://assets.leetcode.com/uploads/2020/11/13/spiral1.jpg) 7 | ``` 8 | Input: matrix = [[1,2,3],[4,5,6],[7,8,9]] 9 | Output: [1,2,3,6,9,8,7,4,5] 10 | ``` 11 | 12 | #### Example 2: 13 | ![](https://assets.leetcode.com/uploads/2020/11/13/spiral.jpg) 14 | ``` 15 | Input: matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]] 16 | Output: [1,2,3,4,8,12,11,10,9,5,6,7] 17 | ``` 18 | 19 | #### Constraints: 20 | * ```m == matrix.length``` 21 | * ```n == matrix[i].length``` 22 | * ```1 <= m, n <= 10``` 23 | * ```-100 <= matrix[i][j] <= 100``` -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/55. Jump Game/55. Jump Game.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canJump(vector &nums) { 4 | int max = 0; 5 | for (int i = 0; i <= max; i++) { 6 | max = std::max(i + nums[i], max); 7 | if (max >= nums.size() - 1) return true; 8 | } 9 | return false; 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/55. Jump Game/55. Jump Game.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean canJump(int[] nums) { 3 | int n = nums.length; 4 | int indx = n - 1; 5 | for (int i = n - 2; i >= 0; i--) { 6 | if (i + nums[i] >= indx) indx = i; 7 | } 8 | return indx == 0; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/56. Merge Intervals/56. Merge Intervals.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> merge(vector> &intervals) { 4 | } 5 | }; 6 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/56. Merge Intervals/56. Merge Intervals.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[][] merge(int[][] intervals) { 3 | } 4 | } 5 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/57. Insert Interval/57. Insert Interval.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> insert(vector> &intervals, vector &newInterval) { 4 | vector> ans; 5 | for (auto &&i: intervals) { 6 | if (i[1] < newInterval[0]) ans.push_back(i); 7 | else if (newInterval[1] < i[0]) { 8 | ans.push_back(newInterval); 9 | newInterval = i; 10 | } else { 11 | newInterval[0] = min(newInterval[0], i[0]); 12 | newInterval[1] = max(newInterval[1], i[1]); 13 | } 14 | } 15 | ans.push_back(newInterval); 16 | return ans; 17 | } 18 | }; -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/57. Insert Interval/57. Insert Interval.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[][] insert(int[][] intervals, int[] newInterval) { 3 | } 4 | } 5 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/6. ZigZag Conversion/6. ZigZag Conversion.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string convert(string s, int numRows) { 4 | if (s.size() < 3 || numRows < 2) 5 | return s; 6 | vector ar[numRows]; 7 | int row = 0, direct = 1; 8 | for (int i = 0; i < s.size(); i++) { 9 | ar[row].push_back(i); 10 | row += direct; 11 | if (row == numRows - 1 || row == 0) 12 | direct *= -1; 13 | } 14 | string zigzagStr = ""; 15 | for (int i = 0; i < numRows; i++) { 16 | for (int j = 0; j < ar[i].size(); j++) 17 | zigzagStr += s[ar[i][j]]; 18 | } 19 | return zigzagStr; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/62. Unique Paths/62. Unique Paths.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int uniquePaths(int m, int n) { 4 | } 5 | }; 6 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/62. Unique Paths/62. Unique Paths.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int uniquePaths(int m, int n) { 3 | } 4 | } 5 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/64. Minimum Path Sum/64. Minimum Path Sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int help(vector> &arr, int i, int j, vector> &dp) { 4 | if (i == 0 && j == 0) 5 | return arr[i][j]; 6 | else if (i < 0 || j < 0) 7 | return 1e8; 8 | if (dp[i][j] != -1) return dp[i][j]; 9 | return dp[i][j] = arr[i][j] + min(help(arr, i, j - 1, dp), help(arr, i - 1, j, dp)); 10 | } 11 | int minPathSum(vector> &grid) { 12 | vector> dp(grid.size(), vector(grid[0].size(), -1)); 13 | return help(grid, grid.size() - 1, grid[0].size() - 1, dp); 14 | } 15 | }; -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/66. Plus One/66. Plus One.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector plusOne(vector &digits) { 4 | for (int i = digits.size() - 1; i >= 0; i--) { 5 | if (digits[i] == 9) { 6 | if (i > 0) { 7 | digits[i] = 0; 8 | if (digits[i - 1] != 9) { 9 | digits[i - 1] += 1; 10 | break; 11 | } 12 | continue; 13 | } 14 | digits[i] = 0; 15 | digits.insert(digits.begin(), 1); 16 | break; 17 | } 18 | digits[i] += 1; 19 | break; 20 | } 21 | return digits; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/67. Add Binary/67. Add Binary.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string addBinary(string a, string b) { 4 | string result; 5 | int carry = 0; 6 | for (int i = a.size() - 1, j = b.size() - 1; i >= 0 || j >= 0 || carry; --i, --j) { 7 | carry += i >= 0 ? a[i] - '0' : 0; 8 | carry += j >= 0 ? b[j] - '0' : 0; 9 | result += (carry & 1) + '0'; 10 | carry >>= 1; 11 | } 12 | std::reverse(result.begin(), result.end()); 13 | return result; 14 | } 15 | }; -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/67. Add Binary/README.md: -------------------------------------------------------------------------------- 1 | # 67. Add Binary 2 | ## [Original Question Link](https://leetcode.com/problems/add-binary/) 3 | 4 | Given two binary strings ```a``` and ```b```, return their sum as a binary string. 5 | 6 | #### Example 1: 7 | ``` 8 | Input: a = "11", b = "1" 9 | Output: "100" 10 | ``` 11 | 12 | #### Example 2: 13 | ``` 14 | Input: a = "1010", b = "1011" 15 | Output: "10101" 16 | ``` 17 | 18 | #### Constraints: 19 | * ```1 <= a.length, b.length <= 10^4``` 20 | * ```a``` and ```b``` consist only of ```'0'``` or ```'1'``` characters. 21 | * Each string does not contain leading zeros except for the zero itself. -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/69. Sqrt(x)/69. Sqrt(x).cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int mySqrt(int x) { 4 | return sqrt(x); 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/7. Reverse Integer/7. Reverse Integer.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int reverse(int x) { 4 | long long rev_x = 0; 5 | while (x != 0) { 6 | int rem = x % 10; 7 | rev_x = rev_x * 10 + rem; 8 | x /= 10; 9 | if (rev_x > INT_MAX || rev_x < INT_MIN) 10 | return 0; 11 | } 12 | return rev_x; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/7. Reverse Integer/7. Reverse Integer.cs: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int Reverse(int x) { 3 | int num = 0; 4 | bool isNumberNegative = false; 5 | if (x < 0) { 6 | isNumberNegative = true; 7 | x = x * (-1); 8 | } 9 | 10 | string number = x.ToString(); 11 | var array = number.ToCharArray().Reverse(); 12 | number = string.Join("", array); 13 | 14 | try { 15 | num = Convert.ToInt32(number); 16 | } catch (Exception e) { 17 | num = 0; 18 | } 19 | 20 | if (isNumberNegative) num = num * (-1); 21 | 22 | return num; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/70. Climbing Stairs/70. Climbing Stairs.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int climbStairs(int n) { 4 | vector dp(n + 1); 5 | dp[0] = dp[1] = 1; 6 | for (int i = 2; i <= n; i++) { 7 | dp[i] = dp[i - 1] + dp[i - 2]; 8 | } 9 | return dp[n]; 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/70. Climbing Stairs/70. Climbing Stairs.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int climbStairs(int n) { 3 | int dp[] = new int[n + 1]; 4 | dp[0] = dp[1] = 1; 5 | for (int i = 2; i <= n; i++) { 6 | dp[i] = dp[i - 1] + dp[i - 2]; 7 | } 8 | return dp[n]; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/74. Search a 2D Matrix/74. Search a 2D Matrix.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool searchMatrix(vector> &matrix, int target) { 4 | int n = matrix.size(); 5 | int m = matrix[0].size(); 6 | int l = 0, r = n * m - 1; 7 | while (l <= r) { 8 | int mid = r + (l - r) / 2; 9 | int ele = matrix[mid / m][mid % m]; 10 | if (ele == target) 11 | return true; 12 | else if (ele < target) 13 | l = mid + 1; 14 | else 15 | r = mid - 1; 16 | } 17 | return false; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/74. Search a 2D Matrix/74. Search a 2D Matrix.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean searchMatrix(int[][] matrix, int target) { 3 | int n = matrix.length; 4 | int m = matrix[0].length; 5 | int l = 0, r = n * m - 1; 6 | while (l <= r) { 7 | int mid = r + (l - r) / 2; 8 | int ele = matrix[mid / m][mid % m]; 9 | if (ele == target) 10 | return true; 11 | else if (ele < target) 12 | l = mid + 1; 13 | else 14 | r = mid - 1; 15 | } 16 | return false; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/75. Sort Colors/75. Sort Colors.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void sortColors(vector &nums) { 4 | sort(nums.begin(), nums.end()); 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/77. Combinations/77. Combinations.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | private: 3 | vector> list; 4 | vector combination; 5 | 6 | public: 7 | vector> combine(int n, int k) { 8 | generator(n, k, 1); 9 | return list; 10 | } 11 | void generator(int n, int k, int s) { 12 | if (combination.size() == k) { 13 | list.push_back(combination); 14 | return; 15 | } 16 | if (s > n) return; 17 | for (int i = s; i <= n; i++) { 18 | combination.push_back(i); 19 | generator(n, k, i + 1); 20 | combination.pop_back(); 21 | } 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/77. Combinations/77. Combinations.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | private List> list = new ArrayList>(); 3 | private List combination = new ArrayList<>(); 4 | 5 | public List> combine(int n, int k) { 6 | generator(n, k, 1); 7 | return list; 8 | } 9 | void generator(int n, int k, int s) { 10 | if (combination.size() == k) { 11 | list.add(new ArrayList<>(combination)); 12 | return; 13 | } 14 | if (s > n) return; 15 | for (int i = s; i <= n; i++) { 16 | combination.add(i); 17 | generator(n, k, i + 1); 18 | combination.remove(combination.size() - 1); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/80. Remove Duplicates from Sorted Array II/80. Remove Duplicates from Sorted Array II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int removeDuplicates(vector &nums) { 4 | map mp; 5 | for (auto it: nums) 6 | mp[it]++; 7 | int sum = 0; 8 | nums.clear(); 9 | for (auto it: mp) { 10 | sum += min(2, it.second); 11 | for (int i = 0; i < min(2, it.second); i++) { 12 | nums.push_back(it.first); 13 | } 14 | } 15 | return sum; 16 | } 17 | }; -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/80. Remove Duplicates from Sorted Array II/80. Remove Duplicates from Sorted Array II.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int removeDuplicates(int[] nums) { 3 | Map map = new LinkedHashMap<>(); 4 | for (int ele : nums) { 5 | map.merge(ele, 1, Integer::sum); 6 | } 7 | int k = 0; 8 | for (Map.Entry entry : map.entrySet()) { 9 | for (int i = 0; i < Math.min(2, entry.getValue()); i++) { 10 | nums[k++] = entry.getKey(); 11 | } 12 | } 13 | return k; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/88. Merge Sorted Array/88. Merge Sorted Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void merge(vector &nums1, int m, vector &nums2, int n) { 4 | for (int i = m, j = 0; i < n + m; i++) { 5 | nums1[i] = nums2[j++]; 6 | } 7 | sort(nums1.begin(), nums1.end()); 8 | } 9 | }; 10 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/88. Merge Sorted Array/88. Merge Sorted Array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public void merge(int[] nums1, int m, int[] nums2, int n) { 3 | int num[] = new int[m + n]; 4 | int k = 0, i = 0, j = 0; 5 | while (i < m && j < n) { 6 | if (nums1[i] < nums2[j]) 7 | num[k++] = nums1[i++]; 8 | else 9 | num[k++] = nums2[j++]; 10 | } 11 | while (i < m) num[k++] = nums1[i++]; 12 | while (j < n) num[k++] = nums2[j++]; 13 | for (i = 0; i < n + m; i++) nums1[i] = num[i]; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/9. Palindrome Number/9. Palindrome Number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPalindrome(int x) { 4 | string str = to_string(x); 5 | string rev = str; 6 | reverse(rev.begin(), rev.end()); 7 | return (str == rev ? true : false); 8 | } 9 | }; 10 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/91. Decode Ways/91. Decode Ways.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numDecodings(string s) { 4 | } 5 | }; 6 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/91. Decode Ways/91. Decode Ways.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numDecodings(String s) { 3 | } 4 | } 5 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/94. Binary Tree Inorder Traversal/94. Binary Tree Inorder Traversal.py: -------------------------------------------------------------------------------- 1 | # Definition for a binary tree node. 2 | # class TreeNode: 3 | # def __init__(self, val=0, left=None, right=None): 4 | # self.val = val 5 | # self.left = left 6 | # self.right = right 7 | class Solution: 8 | def inorderTraversal(self, root: TreeNode) -> List[int]: 9 | return self.inorderTraversal(root.left)+[root.val]+self.inorderTraversal(root.right) if root else [] 10 | -------------------------------------------------------------------------------- /LeetCode Problems/1 to 100/97. Interleaving String/97. Interleaving String.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | #define S1 (dp[j] && s1[i - 1] == s3[i + j - 1]) 3 | #define S2 (dp[j - 1] && s2[j - 1] == s3[i + j - 1]) 4 | public: 5 | bool isInterleave(string s1, string s2, string s3) { 6 | int n = s1.size(), m = s2.size(), k = s3.size(); 7 | if (n + m != k) return false; 8 | vector dp(m + 1, 0); 9 | for (int i = 0; i <= n; i++) { 10 | for (int j = 0; j <= m; j++) { 11 | dp[j] = (i == 0 && j == 0) ? true : i == 0 ? S2 12 | : j == 0 ? S1 13 | : S1 || S2; 14 | } 15 | } 16 | return dp[m]; 17 | } 18 | }; -------------------------------------------------------------------------------- /LeetCode Problems/1001 to 1100/1029. Two City Scheduling/1029. Two City Scheduling.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int twoCitySchedCost(vector> &cs, int res = 0) { 4 | nth_element(begin(cs), begin(cs) + cs.size() / 2, end(cs), [](vector &a, vector &b) { 5 | return (a[0] - a[1] < b[0] - b[1]); 6 | }); 7 | for (auto i = 0; i < cs.size() / 2; ++i) { 8 | res += cs[i][0] + cs[i + cs.size() / 2][1]; 9 | } 10 | return res; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /LeetCode Problems/1001 to 1100/1047. Remove All Adjacent Duplicates In String/1047. Remove All Adjacent Duplicates In String.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string removeDuplicates(string s) { 4 | stack unique; 5 | unique.push(s.at(0)); 6 | for (int i = 1; i < s.size(); i++) { 7 | if (!unique.empty() && unique.top() == s[i]) { 8 | unique.pop(); 9 | continue; 10 | } 11 | unique.push(s.at(i)); 12 | } 13 | string ans = ""; 14 | while (!unique.empty()) { 15 | ans += unique.top(); 16 | unique.pop(); 17 | } 18 | reverse(ans.begin(), ans.end()); 19 | return ans; 20 | } 21 | }; -------------------------------------------------------------------------------- /LeetCode Problems/1001 to 1100/1071. Greatest Common Divisor of Strings/1071. Greatest Common Divisor of Strings.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string gcdOfStrings(string str1, string str2) { 4 | return (str1 + str2 == str2 + str1) ? str1.substr(0, gcd(size(str1), size(str2))) : ""; 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /LeetCode Problems/1001 to 1100/1071. Greatest Common Divisor of Strings/1071. Greatest Common Divisor of Strings.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | private int gcd(int a, int b) { 3 | if (b == 0) return a; 4 | return gcd(b, a % b); 5 | } 6 | public String gcdOfStrings(String str1, String str2) { 7 | if ((str1 + str2).equals(str2 + str1)) 8 | return str1.substring(0, gcd(str1.length(), str2.length())); 9 | else 10 | return ""; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /LeetCode Problems/1001 to 1100/1079. Letter Tile Possibilities/1079. Letter Tile Possibilities.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | private static final int R = 26; 3 | public int numTilePossibilities(String tiles) { 4 | if (tiles == null || tiles.length() == 0) return 0; 5 | 6 | final int[] count = new int[R]; 7 | for (char c : tiles.toCharArray()) { 8 | count[c - 'A']++; 9 | } 10 | return backtrack(count); 11 | } 12 | private static int backtrack(int[] nums) { 13 | int sum = 0; 14 | for (int i = 0; i < R; i++) { 15 | if (nums[i] == 0) continue; 16 | sum++; 17 | nums[i]--; 18 | sum += backtrack(nums); 19 | nums[i]++; 20 | } 21 | return sum; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /LeetCode Problems/1001 to 1100/1089. Duplicate Zeros/1089. Duplicate Zeros.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void duplicateZeros(vector &arr) { 4 | int n = arr.size(), num = 0, cnt = 0; 5 | for (int i = 0; i < n - 1; i++) { 6 | if (!arr[i]) { 7 | for (int j = n - 1; j > i + 1; j--) 8 | arr[j] = arr[j - 1]; 9 | arr[i + 1] = arr[i]; 10 | i++; 11 | } 12 | } 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /LeetCode Problems/101 to 200/101. Symmetric Tree/README.md: -------------------------------------------------------------------------------- 1 | # [101. Symmetric Tree](https://leetcode.com/problems/symmetric-tree/description/) 2 | 3 | Given the ```root``` of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center). 4 | 5 | #### Example 1: 6 | ![](https://assets.leetcode.com/uploads/2021/02/19/symtree1.jpg) 7 | ``` 8 | Input: root = [1,2,2,3,4,4,3] 9 | Output: true 10 | ``` 11 | 12 | #### Example 2: 13 | ![](https://assets.leetcode.com/uploads/2021/02/19/symtree2.jpg) 14 | ``` 15 | Input: root = [1,2,2,null,3,null,3] 16 | Output: false 17 | ``` 18 | 19 | #### Constraints: 20 | * The number of nodes in the tree is in the range ```[1, 1000]```. 21 | * ```-100 <= Node.val <= 100``` 22 | 23 | **Follow up:** Could you solve it both recursively and iteratively? -------------------------------------------------------------------------------- /LeetCode Problems/101 to 200/102. Binary Tree Level Order Traversal/102. Binary Tree Level Order Traversal.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | vector> levelOrder(TreeNode *root) { 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /LeetCode Problems/101 to 200/104. Maximum Depth of Binary Tree/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 == nullptr) 16 | return 0; 17 | return max(maxDepth(root->left), maxDepth(root->right)) + 1; 18 | } 19 | }; -------------------------------------------------------------------------------- /LeetCode Problems/101 to 200/104. Maximum Depth of Binary Tree/104. Maximum Depth of Binary Tree.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * int val; 5 | * TreeNode left; 6 | * TreeNode right; 7 | * TreeNode() {} 8 | * TreeNode(int val) { this.val = val; } 9 | * TreeNode(int val, TreeNode left, TreeNode right) { 10 | * this.val = val; 11 | * this.left = left; 12 | * this.right = right; 13 | * } 14 | * } 15 | */ 16 | class Solution { 17 | public int maxDepth(TreeNode root) { 18 | if (root == null) return 0; 19 | return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /LeetCode Problems/101 to 200/104. Maximum Depth of Binary Tree/104. Maximum Depth of Binary Tree.kt: -------------------------------------------------------------------------------- 1 | /** 2 | * Example: 3 | * var ti = TreeNode(5) 4 | * var v = ti.`val` 5 | * Definition for a binary tree node. 6 | * class TreeNode(var `val`: Int) { 7 | * var left: TreeNode? = null 8 | * var right: TreeNode? = null 9 | * } 10 | */ 11 | class Solution { 12 | fun maxDepth(root: TreeNode?): Int { 13 | if(root == null) 14 | return 0; 15 | return Math.max(maxDepth(root.`left`), maxDepth(root.`right`)) + 1; 16 | } 17 | } -------------------------------------------------------------------------------- /LeetCode Problems/101 to 200/104. Maximum Depth of Binary Tree/104. Maximum Depth of Binary Tree.py: -------------------------------------------------------------------------------- 1 | # Definition for a binary tree node. 2 | # class TreeNode: 3 | # def __init__(self, val=0, left=None, right=None): 4 | # self.val = val 5 | # self.left = left 6 | # self.right = right 7 | class Solution: 8 | def maxDepth(self, root: Optional[TreeNode]) -> int: 9 | if root is None: 10 | return 0 11 | return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1 -------------------------------------------------------------------------------- /LeetCode Problems/101 to 200/104. Maximum Depth of Binary Tree/README.md: -------------------------------------------------------------------------------- 1 | # 104. Maximum Depth of Binary Tree 2 | ## [Original Question Link](https://leetcode.com/problems/maximum-depth-of-binary-tree/) 3 | 4 | Given the ```root``` of a binary tree, return its maximum depth. 5 | 6 | A binary tree's **maximum depth** is the number of nodes along the longest path from the root node down to the farthest leaf node. 7 | 8 | #### Example 1: 9 | ![](https://assets.leetcode.com/uploads/2020/11/26/tmp-tree.jpg) 10 | ``` 11 | Input: root = [3,9,20,null,null,15,7] 12 | Output: 3 13 | ``` 14 | 15 | #### Example 2: 16 | ``` 17 | Input: root = [1,null,2] 18 | Output: 2 19 | ``` 20 | 21 | #### Constraints : 22 | * The number of nodes in the tree is in the range ```[0, 10^4]```. 23 | * ```-100 <= Node.val <= 100``` -------------------------------------------------------------------------------- /LeetCode Problems/101 to 200/120. Triangle/120. Triangle.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minimumTotal(vector> &triangle) { 4 | for (int i = triangle.size() - 2; i >= 0; i--) { 5 | for (int j = 0; j < triangle[i].size(); j++) { 6 | triangle[i][j] += min(triangle[i + 1][j], triangle[i + 1][j + 1]); 7 | } 8 | } 9 | return triangle[0][0]; 10 | } 11 | }; -------------------------------------------------------------------------------- /LeetCode Problems/101 to 200/121. Best Time to Buy and Sell Stock/121. Best Time to Buy and Sell Stock.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProfit(vector &prices) { 4 | int l = 0, mx = 0, r = 1; 5 | while (r < prices.size()) { 6 | if (prices[r] < prices[l]) 7 | l = r; 8 | else 9 | mx = max(mx, prices[r] - prices[l]); 10 | r += 1; 11 | } 12 | return mx; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /LeetCode Problems/101 to 200/121. Best Time to Buy and Sell Stock/121. Best Time to Buy and Sell Stock.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxProfit(int[] prices) { 3 | int mx = 0, l = 0, r = 1; 4 | while (r < prices.length) { 5 | if (prices[r] <= prices[l]) 6 | l = r; 7 | else 8 | mx = Math.max(mx, prices[r] - prices[l]); 9 | r++; 10 | } 11 | return mx; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /LeetCode Problems/101 to 200/124. Binary Tree Maximum Path Sum/124. Binary Tree Maximum Path Sum.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 maxPathSum(TreeNode *root) { 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /LeetCode Problems/101 to 200/125. Valid Palindrome/125. Valid Palindrome.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPalindrome(string s) { 4 | } 5 | }; 6 | -------------------------------------------------------------------------------- /LeetCode Problems/101 to 200/131. Palindrome Partitioning/README.md: -------------------------------------------------------------------------------- 1 | # 131. Palindrome Partitioning 2 | ### [Original Question Link](https://leetcode.com/problems/palindrome-partitioning/) 3 | 4 | Given a string ```s```, partition ```s``` such that every 5 | **substring** of the partition is a **palindrome**. Return all possible palindrome partitioning of ```s```. 6 | 7 | #### Example 1: 8 | ``` 9 | Input: s = "aab" 10 | Output: [["a","a","b"],["aa","b"]] 11 | ``` 12 | #### Example 2: 13 | ``` 14 | Input: s = "a" 15 | Output: [["a"]] 16 | ``` 17 | 18 | #### Constraints: 19 | 20 | * ```1 <= s.length <= 16``` 21 | * ```s``` contains only lowercase English letters. -------------------------------------------------------------------------------- /LeetCode Problems/101 to 200/133. Clone Graph/133. Clone Graph.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | // Definition for a Node. 3 | class Node { 4 | public: 5 | int val; 6 | vector neighbors; 7 | Node() { 8 | val = 0; 9 | neighbors = vector(); 10 | } 11 | Node(int _val) { 12 | val = _val; 13 | neighbors = vector(); 14 | } 15 | Node(int _val, vector _neighbors) { 16 | val = _val; 17 | neighbors = _neighbors; 18 | } 19 | }; 20 | */ 21 | 22 | class Solution { 23 | public: 24 | Node *cloneGraph(Node *node) { 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /LeetCode Problems/101 to 200/134. Gas Station/134. Gas Station.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int canCompleteCircuit(vector &gas, vector &cost) { 4 | int total = 0, startStation = 0, current_gas = 0; 5 | for (int i = 0; i < gas.size(); i++) { 6 | if (current_gas < 0) { 7 | current_gas = 0; 8 | startStation = i; 9 | } 10 | current_gas += (gas[i] - cost[i]); 11 | total += (gas[i] - cost[i]); 12 | } 13 | return (total < 0 ? -1 : startStation); 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /LeetCode Problems/101 to 200/134. Gas Station/134. Gas Station.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int canCompleteCircuit(int[] gas, int[] cost) { 3 | int total = 0, startStation = 0, current_gas = 0; 4 | for (int i = 0; i < gas.length; i++) { 5 | if (current_gas < 0) { 6 | current_gas = 0; 7 | startStation = i; 8 | } 9 | current_gas += (gas[i] - cost[i]); 10 | total += (gas[i] - cost[i]); 11 | } 12 | return (total < 0 ? -1 : startStation); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /LeetCode Problems/101 to 200/137. Single Number II/137. Single Number II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int singleNumber(vector &nums) { 4 | map mp; 5 | for (auto ele: nums) { 6 | mp[ele]++; 7 | } 8 | for (auto it: mp) { 9 | if (it.second == 1) 10 | return it.first; 11 | } 12 | return 0; 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode Problems/101 to 200/137. Single Number II/137. Single Number II.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int singleNumber(int[] nums) { 3 | Map map = new HashMap<>(); 4 | for (int ele : nums) { 5 | map.merge(ele, 1, Integer::sum); 6 | } 7 | int ans = -1; 8 | for (Map.Entry entry : map.entrySet()) { 9 | if (entry.getValue() == 1) ans = entry.getKey(); 10 | } 11 | return ans; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /LeetCode Problems/101 to 200/139. Word Break/139. Word Break.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool wordBreak(string s, vector &wordDict) { 4 | int n = s.length(); 5 | vector dp(n + 1, false); 6 | dp[n] = true; 7 | 8 | for (int i = n - 1; i >= 0; i--) { 9 | for (const std::string &w: wordDict) { 10 | if (i + w.length() <= n && s.substr(i, w.length()) == w) { 11 | dp[i] = dp[i + w.length()]; 12 | } 13 | if (dp[i]) { 14 | break; 15 | } 16 | } 17 | } 18 | 19 | return dp[0]; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /LeetCode Problems/101 to 200/139. Word Break/139. Word Break.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean wordBreak(String s, List wordDict) { 3 | int n = s.length(); 4 | boolean[] dp = new boolean[n + 1]; 5 | Arrays.fill(dp, false); 6 | dp[n] = true; 7 | for (int i = n - 1; i >= 0; i--) { 8 | for (String w : wordDict) { 9 | if (i + w.length() <= n && s.substring(i, i + w.length()).equals(w)) { 10 | dp[i] = dp[i + w.length()]; 11 | } 12 | if (dp[i]) { 13 | break; 14 | } 15 | } 16 | } 17 | return dp[0]; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /LeetCode Problems/101 to 200/139. Word Break/139. Word Break.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def wordBreak(self, s: str, wordDict: List[str]) -> bool: 3 | n = len(s) 4 | dp = [False] * (n + 1) 5 | dp[n] = True 6 | for i in range(n, -1, -1): 7 | for w in wordDict: 8 | if (i + len(w)) <= n and s[i:i+len(w)] == w: 9 | dp[i] = dp[i + len(w)] 10 | if(dp[i]): 11 | break 12 | return dp[0] 13 | -------------------------------------------------------------------------------- /LeetCode Problems/101 to 200/143. Reorder List/143. Reorder List.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | void reorderList(ListNode *head) { 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /LeetCode Problems/101 to 200/144. Binary Tree Preorder Traversal/144. Binary Tree Preorder Traversal.kt: -------------------------------------------------------------------------------- 1 | /** 2 | * Example: 3 | * var ti = TreeNode(5) 4 | * var v = ti.`val` 5 | * Definition for a binary tree node. 6 | * class TreeNode(var `val`: Int) { 7 | * var left: TreeNode? = null 8 | * var right: TreeNode? = null 9 | * } 10 | */ 11 | class Solution { 12 | var arr = ArrayList(); 13 | private fun PreorderTraversal(root: TreeNode?) { 14 | if(root == null) 15 | return; 16 | arr.add(root.`val`); 17 | PreorderTraversal(root.left); 18 | PreorderTraversal(root.right); 19 | } 20 | fun preorderTraversal(root: TreeNode?): List { 21 | PreorderTraversal(root); 22 | return arr; 23 | } 24 | } -------------------------------------------------------------------------------- /LeetCode Problems/101 to 200/144. Binary Tree Preorder Traversal/144. Binary Tree Preorder Traversal.py: -------------------------------------------------------------------------------- 1 | # Definition for a binary tree node. 2 | # class TreeNode: 3 | # def __init__(self, val=0, left=None, right=None): 4 | # self.val = val 5 | # self.left = left 6 | # self.right = right 7 | class Solution: 8 | def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]: 9 | return [] if not root else ([root.val]+ 10 | self.preorderTraversal(root.left)+ 11 | self.preorderTraversal(root.right)) -------------------------------------------------------------------------------- /LeetCode Problems/101 to 200/151. Reverse Words in a String/151. Reverse Words in a String.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String reverseWords(String s) { 3 | s = s.trim(); 4 | String words[] = s.split(" "); 5 | s = ""; 6 | for (int i = words.length - 1; i >= 0; i--) { 7 | if (words[i].trim().length() == 0) continue; 8 | s += words[i] + (i == 0 ? "" : " "); 9 | } 10 | return s; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /LeetCode Problems/101 to 200/152. Maximum Product Subarray/152. Maximum Product Subarray.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProduct(vector &nums) { 4 | } 5 | }; 6 | -------------------------------------------------------------------------------- /LeetCode Problems/101 to 200/153. Find Minimum in Rotated Sorted Array/153. Find Minimum in Rotated Sorted Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findMin(vector &nums) { 4 | } 5 | }; 6 | -------------------------------------------------------------------------------- /LeetCode Problems/101 to 200/154. Find Minimum in Rotated Sorted Array II/154. Find Minimum in Rotated Sorted Array II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findMin(vector &nums) { 4 | int min = INT_MAX; 5 | for (int i = 0; i < nums.size(); i++) { 6 | min = std::min(nums[i], min); 7 | } 8 | return min; 9 | } 10 | }; 11 | -------------------------------------------------------------------------------- /LeetCode Problems/101 to 200/162. Find Peak Element/162. Find Peak Element.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findPeakElement(vector &nums) { 4 | if (nums.size() == 1 || nums[0] > nums[1]) 5 | return 0; 6 | if (nums[nums.size() - 1] > nums[nums.size() - 2]) 7 | return nums.size() - 1; 8 | int f = 1, l = nums.size() - 2; 9 | while (f <= l) { 10 | int m = (f + l) / 2; 11 | if (nums[m] > nums[m - 1] && nums[m] > nums[m + 1]) 12 | return m; 13 | if (nums[m + 1] < nums[m - 1]) { 14 | l = m - 1; 15 | } else { 16 | f = m + 1; 17 | } 18 | } 19 | return -1; 20 | } 21 | }; -------------------------------------------------------------------------------- /LeetCode Problems/101 to 200/164. Maximum Gap/164. Maximum Gap.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maximumGap(vector &nums) { 4 | sort(nums.begin(), nums.end()); 5 | int ans = 0; 6 | for (int i = 0; i < nums.size() - 1; i++) { 7 | ans = max(ans, nums[i + 1] - nums[i]); 8 | } 9 | return ans; 10 | } 11 | }; -------------------------------------------------------------------------------- /LeetCode Problems/101 to 200/167. Two Sum II - Input Array Is Sorted/167. Two Sum II - Input Array Is Sorted.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector twoSum(vector &num, int target) { 4 | vector ans(2); 5 | int l = 0, r = num.size() - 1; 6 | while (l <= r) { 7 | int sum = num[l] + num[r]; 8 | if (sum == target) { 9 | ans = {l + 1, r + 1}; 10 | break; 11 | } else if (sum > target) 12 | r--; 13 | else 14 | l++; 15 | } 16 | return ans; 17 | } 18 | }; -------------------------------------------------------------------------------- /LeetCode Problems/101 to 200/168. Excel Sheet Column Title/168. Excel Sheet Column Title.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string convertToTitle(int columnNumber) { 4 | string s = "", str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 5 | while (columnNumber > 0) { 6 | s += str[(columnNumber - 1) % 26]; 7 | columnNumber = (columnNumber - 1) / 26; 8 | } 9 | reverse(s.begin(), s.end()); 10 | return s; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /LeetCode Problems/101 to 200/169. Majority Element/169. Majority Element.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int majorityElement(int[] nums) { 3 | Map map = new HashMap<>(); 4 | for (int ele : nums) map.merge(ele, 1, Integer::sum); 5 | int element = 0, value = Integer.MIN_VALUE; 6 | for (Map.Entry entry : map.entrySet()) { 7 | if (value < entry.getValue()) { 8 | element = entry.getKey(); 9 | value = entry.getValue(); 10 | } 11 | } 12 | return element; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /LeetCode Problems/101 to 200/189. Rotate Array/189. Rotate Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void rotate(vector &nums, int k) { 4 | k %= nums.size(); 5 | vector arr; 6 | for (int i = nums.size() - k; i < nums.size(); i++) 7 | arr.push_back(nums[i]); 8 | for (int i = 0; i < nums.size() - k; i++) 9 | arr.push_back(nums[i]); 10 | for (int i = 0; i < nums.size(); i++) { 11 | nums[i] = arr[i]; 12 | } 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /LeetCode Problems/101 to 200/189. Rotate Array/189. Rotate Array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | private void reverse(int[] nums, int l, int r) { 3 | while (l <= r) { 4 | int temp = nums[l]; 5 | nums[l] = nums[r]; 6 | nums[r] = temp; 7 | l++; 8 | r--; 9 | } 10 | } 11 | 12 | public void rotate(int[] nums, int k) { 13 | int n = nums.length; 14 | k %= n; 15 | reverse(nums, 0, n - 1); 16 | reverse(nums, 0, k - 1); 17 | reverse(nums, k, n - 1); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /LeetCode Problems/101 to 200/190. Reverse Bits/190. Reverse Bits.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | uint32_t reverseBits(uint32_t n) { 4 | } 5 | }; 6 | -------------------------------------------------------------------------------- /LeetCode Problems/101 to 200/191. Number of 1 Bits/191. Number of 1 Bits.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int hammingWeight(uint32_t n) { 4 | int cnt = 0; 5 | while (n != 0) { 6 | cnt += n % 2; 7 | n /= 2; 8 | } 9 | return cnt; 10 | } 11 | }; -------------------------------------------------------------------------------- /LeetCode Problems/101 to 200/198. House Robber/198. House Robber.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int rob(vector &nums) { 4 | int n = nums.size(); 5 | if (n <= 1) 6 | return nums[0]; 7 | int prev = nums[0], curr = max(nums[1], nums[0]); 8 | for (int i = 2; i < n; i++) { 9 | int temp = max(prev + nums[i], curr); 10 | prev = curr; 11 | curr = temp; 12 | } 13 | return curr; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /LeetCode Problems/101 to 200/198. House Robber/198. House Robber.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int rob(int[] nums) { 3 | int n = nums.length; 4 | if (n <= 1) return nums[0]; 5 | int prev = nums[0], curr = Math.max(nums[1], nums[0]); 6 | for (int i = 2; i < n; i++) { 7 | int temp = Math.max(prev + nums[i], curr); 8 | prev = curr; 9 | curr = temp; 10 | } 11 | return curr; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /LeetCode Problems/101 to 200/200. Number of Islands/200. Number of Islands.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numIslands(vector> &grid) { 4 | } 5 | }; 6 | -------------------------------------------------------------------------------- /LeetCode Problems/1101 to 1200/1137. N-th Tribonacci Number/1137. N-th Tribonacci Number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int tribonacci(int n) { 4 | vector dp(n + 3); 5 | dp[0] = 0; 6 | dp[1] = dp[2] = 1; 7 | for (int i = 3; i <= n; i++) { 8 | dp[i] = dp[i - 3] + dp[i - 2] + dp[i - 1]; 9 | } 10 | return dp[n]; 11 | } 12 | }; -------------------------------------------------------------------------------- /LeetCode Problems/1101 to 1200/1137. N-th Tribonacci Number/1137. N-th Tribonacci Number.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int tribonacci(int n) { 3 | int dp[] = new int[38]; 4 | dp[0] = 0; 5 | dp[1] = 1; 6 | dp[2] = 1; 7 | for (int i = 3; i <= n; i++) dp[i] = dp[i - 1] + dp[i - 2] + dp[i - 3]; 8 | return dp[n]; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /LeetCode Problems/1101 to 1200/1137. N-th Tribonacci Number/1137. N-th Tribonacci Number.kt: -------------------------------------------------------------------------------- 1 | class Solution { 2 | fun tribonacci(n: Int): Int { 3 | if(n == 0) return 0; 4 | else if (n <= 2) return 1; 5 | var a = 0; var b = 1; var c = 1; var ans = 0; 6 | for (i in 3..n) { 7 | ans = a + b + c; 8 | a = b; b = c; c = ans; 9 | } 10 | return ans; 11 | } 12 | } -------------------------------------------------------------------------------- /LeetCode Problems/1101 to 1200/1137. N-th Tribonacci Number/1137. N-th Tribonacci Number.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def tribonacci(self, n: int) -> int: 3 | if n == 0: return 0 4 | elif n <= 2: return 1 5 | a = 0;b = 1;c = 1;ans = 0 6 | for i in range (3, n+1): 7 | ans = a + b + c 8 | a = b;b = c;c = ans 9 | return ans -------------------------------------------------------------------------------- /LeetCode Problems/1101 to 1200/1137. N-th Tribonacci Number/README.md: -------------------------------------------------------------------------------- 1 | # 1137. N-th Tribonacci Number 2 | 3 | ### [Original Question link](https://leetcode.com/problems/n-th-tribonacci-number/) 4 | 5 | The Tribonacci sequence T(n) is defined as follows: 6 | 7 | T(0) = 0, T(1) = 1, T(2) = 1, and T(n+3) = T(n) + T(n+1) + T(n+2) for n >= 0. 8 | 9 | Given ```n```, return the value of T(n). 10 | #### Example 1: 11 | ``` 12 | Input: n = 4 13 | Output: 4 14 | Explanation: 15 | T_3 = 0 + 1 + 1 = 2 16 | T_4 = 1 + 1 + 2 = 4 17 | ``` 18 | 19 | #### Example 2: 20 | ``` 21 | Input: n = 25 22 | Output: 1389537 23 | ``` 24 | 25 | #### Constraints: 26 | * ```0 <= n <= 37``` 27 | * The answer is guaranteed to fit within a 32-bit integer, ie.
28 | ```answer <= 2^31 - 1```. -------------------------------------------------------------------------------- /LeetCode Problems/1101 to 1200/1143. Longest Common Subsequence/1143. Longest Common Subsequence.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int longestCommonSubsequence(string text1, string text2) { 4 | vector> dp(text1.size() + 1, vector(text2.size() + 1)); 5 | for (int i = 0; i < text1.size(); i++) { 6 | for (int j = 0; j < text2.size(); j++) { 7 | if (text1[i] == text2[j]) { 8 | dp[i + 1][j + 1] = dp[i][j] + 1; 9 | } else { 10 | dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]); 11 | } 12 | } 13 | } 14 | return dp[dp.size() - 1][dp[0].size() - 1]; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /LeetCode Problems/1101 to 1200/1143. Longest Common Subsequence/1143. Longest Common Subsequence.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int longestCommonSubsequence(String text1, String text2) { 3 | int dp[][] = new int[text1.length() + 1][text2.length() + 1]; 4 | for (int i = 0; i < text1.length(); i++) { 5 | for (int j = 0; j < text2.length(); j++) { 6 | if (text1.charAt(i) == text2.charAt(j)) 7 | dp[i + 1][j + 1] = dp[i][j] + 1; 8 | else 9 | dp[i + 1][j + 1] = Math.max(dp[i][j + 1], dp[i + 1][j]); 10 | } 11 | } 12 | return dp[text1.length()][text2.length()]; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /LeetCode Problems/1201 to 1300/1207. Unique Number of Occurrences/1207. Unique Number of Occurrences.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool uniqueOccurrences(vector &arr) { 4 | map mp; 5 | for (int i = 0; i < arr.size(); i++) { 6 | mp[arr[i]]++; 7 | } 8 | set s; 9 | for (auto it: mp) { 10 | s.insert(it.second); 11 | } 12 | return s.size() == mp.size(); 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode Problems/1201 to 1300/1207. Unique Number of Occurrences/1207. Unique Number of Occurrences.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean uniqueOccurrences(int[] arr) { 3 | HashMap mp = new HashMap<>(); 4 | for (int i = 0; i < arr.length; i++) mp.put(arr[i], 0); 5 | for (int i = 0; i < arr.length; i++) mp.put(arr[i], (mp.get(arr[i]) + 1)); 6 | HashMap track = new HashMap<>(); 7 | for (Integer it : mp.keySet()) { 8 | if (track.get(mp.get(it)) == null || track.get(mp.get(it)) == 0) { 9 | track.put(mp.get(it), (mp.get(it) + 1)); 10 | continue; 11 | } 12 | if (track.get(mp.get(it)) > 1) return false; 13 | } 14 | return true; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode Problems/1201 to 1300/1266. Minimum Time Visiting All Points/1266. Minimum Time Visiting All Points.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minTimeToVisitAllPoints(vector> &points) { 4 | int cst = 0; 5 | for (int i = 0; i < points.size() - 1; i++) { 6 | int x1 = points[i][0], y1 = points[i][1]; 7 | int x2 = points[i + 1][0], y2 = points[i + 1][1]; 8 | int dif_x = abs(x1 - x2), dif_y = abs(y1 - y2); 9 | cst += max(dif_x, dif_y); 10 | } 11 | return cst; 12 | } 13 | }; -------------------------------------------------------------------------------- /LeetCode Problems/1201 to 1300/1266. Minimum Time Visiting All Points/1266. Minimum Time Visiting All Points.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minTimeToVisitAllPoints(int[][] points) { 3 | int cst = 0; 4 | for (int i = 0; i < points.length - 1; i++) { 5 | int x1 = points[i][0], y1 = points[i][1]; 6 | int x2 = points[i + 1][0], y2 = points[i + 1][1]; 7 | int dif_x = Math.abs(x1 - x2), dif_y = Math.abs(y1 - y2); 8 | cst += Math.max(dif_x, dif_y); 9 | } 10 | return cst; 11 | } 12 | } -------------------------------------------------------------------------------- /LeetCode Problems/1201 to 1300/1268. Search Suggestions System/1268. Search Suggestions System.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> suggestedProducts(vector &P, string S) { 4 | sort(P.begin(), P.end()); 5 | vector> ans; 6 | int left = 0, right = P.size() - 1; 7 | for (int i = 0; i < S.length(); i++) { 8 | vector res; 9 | char c = S[i]; 10 | while (left <= right && (P[left].length() == i || P[left][i] < c)) left++; 11 | while (left <= right && (P[right].length() == i || P[right][i] > c)) right--; 12 | for (int j = 0; j < 3 && left + j <= right; j++) 13 | res.push_back(P[left + j]); 14 | ans.push_back(res); 15 | } 16 | return ans; 17 | } 18 | }; -------------------------------------------------------------------------------- /LeetCode Problems/1201 to 1300/1287. Element Appearing More Than 25% In Sorted Array/1287. Element Appearing More Than 25% In Sorted Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findSpecialInteger(vector &arr) { 4 | int mx = -1, pos = 0, n = arr.size(); 5 | for (int i = 1; i < n; i++) { 6 | int cnt = 0; 7 | while (i + 1 <= n && arr[i] == arr[i - 1]) { 8 | cnt++, i++; 9 | } 10 | if (cnt > mx) { 11 | mx = cnt; 12 | pos = i - 1; 13 | } 14 | } 15 | return arr[pos]; 16 | } 17 | }; -------------------------------------------------------------------------------- /LeetCode Problems/1201 to 1300/1287. Element Appearing More Than 25% In Sorted Array/1287. Element Appearing More Than 25% In Sorted Array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findSpecialInteger(int[] arr) { 3 | int mx = -1, pos = 0, n = arr.length; 4 | for (int i = 1; i < n; i++) { 5 | int cnt = 0; 6 | while (i + 1 <= n && arr[i] == arr[i - 1]) { 7 | cnt++; 8 | i++; 9 | } 10 | if (cnt > mx) { 11 | mx = cnt; 12 | pos = i - 1; 13 | } 14 | } 15 | return arr[pos]; 16 | } 17 | } -------------------------------------------------------------------------------- /LeetCode Problems/1201 to 1300/1295. Find Numbers with Even Number of Digits/1295. Find Numbers with Even Number of Digits.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findNumbers(vector &nums) { 4 | int ans = 0; 5 | for (int i = 0; i < nums.size(); i++) { 6 | int cnt = 0, num = nums[i]; 7 | while (num != 0) { 8 | num /= 10; 9 | cnt++; 10 | } 11 | if (!(cnt & 1)) 12 | ans++; 13 | } 14 | return ans; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /LeetCode Problems/1201 to 1300/1299. Replace Elements with Greatest Element on Right Side/1299. Replace Elements with Greatest Element on Right Side.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector replaceElements(vector &arr) { 4 | int val = -1; 5 | for (int i = arr.size() - 1; i >= 0; i--) { 6 | int temp = arr[i]; 7 | arr[i] = val; 8 | val = max(arr[i], temp); 9 | } 10 | return arr; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /LeetCode Problems/1301 to 1400/1323. Maximum 69 Number/1323. Maximum 69 Number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maximum69Number(int num) { 4 | string str = to_string(num); 5 | for (int i = 0; i < str.size(); i++) { 6 | if (str[i] == '6') { 7 | str[i] = '9'; 8 | break; 9 | } 10 | } 11 | return stoi(str); 12 | } 13 | }; -------------------------------------------------------------------------------- /LeetCode Problems/1301 to 1400/1332. Remove Palindromic Subsequences/1332. Remove Palindromic Subsequences.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int removePalindromeSub(string s) { 4 | string str = s; 5 | if (s.size() < 0) 6 | return 0; 7 | reverse(s.begin(), s.end()); 8 | if (str == s) return 1; 9 | else 10 | return 2; 11 | } 12 | }; -------------------------------------------------------------------------------- /LeetCode Problems/1301 to 1400/1342. Number of Steps to Reduce a Number to Zero/1342. Number of Steps to Reduce a Number to Zero.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numberOfSteps(int num) { 4 | int cnt = 0; 5 | while (num != 0) { 6 | cnt++; 7 | if (num % 2 == 0) num /= 2; 8 | else 9 | num -= 1; 10 | } 11 | return cnt; 12 | } 13 | }; -------------------------------------------------------------------------------- /LeetCode Problems/1301 to 1400/1346. Check If N and Its Double Exist/1346. Check If N and Its Double Exist.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool checkIfExist(vector &arr) { 4 | for (int i = 0; i < arr.size(); i++) { 5 | int n = arr[i] * 2, j; 6 | if (n == 0) 7 | j = i + 1; 8 | else 9 | j = 0; 10 | for (; j < arr.size(); j++) { 11 | if (arr[j] == n) 12 | return true; 13 | } 14 | } 15 | return false; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /LeetCode Problems/1401 to 1500/1413. Minimum Value to Get Positive Step by Step Sum/1413. Minimum Value to Get Positive Step by Step Sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minStartValue(vector &nums) { 4 | for (int i = 1; i < INT_MAX; i++) { 5 | int startValue = i; 6 | bool check = true; 7 | for (int j = 0; j < nums.size(); j++) { 8 | startValue += nums[j]; 9 | if (startValue <= 0) { 10 | check = false; 11 | break; 12 | } 13 | } 14 | if (check == true) { 15 | return i; 16 | } 17 | } 18 | return 0; 19 | } 20 | }; -------------------------------------------------------------------------------- /LeetCode Problems/1401 to 1500/1423. Maximum Points You Can Obtain from Cards/1423. Maximum Points You Can Obtain from Cards.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxScore(vector &cardPoints, int k) { 4 | int n = cardPoints.size(), i, j; 5 | int answer, curr_answer = 0; 6 | 7 | ios_base::sync_with_stdio(false); 8 | cin.tie(NULL); 9 | cout.tie(NULL); 10 | 11 | for (i = 0; i < k; i++) curr_answer += cardPoints[i]; 12 | answer = curr_answer; 13 | 14 | for (i = k - 1, j = n - 1; i >= 0; i--, j--) { 15 | curr_answer += cardPoints[j] - cardPoints[i]; 16 | answer = max(answer, curr_answer); 17 | } 18 | 19 | return answer; 20 | } 21 | }; -------------------------------------------------------------------------------- /LeetCode Problems/1401 to 1500/1446. Consecutive Characters/1446. Consecutive Characters.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxPower(string s) { 4 | int ans = 1; 5 | for (int i = 0; i < s.size() - 1;) { 6 | if (s[i] == s[i + 1]) { 7 | int cnt = 1; 8 | while (s[i] == s[i + 1]) { 9 | cnt++, i++; 10 | } 11 | ans = max(ans, cnt); 12 | } else 13 | i++; 14 | } 15 | return ans; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /LeetCode Problems/1401 to 1500/1461. Check If a String Contains All Binary Codes of Size K/1461. Check If a String Contains All Binary Codes of Size K.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool hasAllCodes(string S, int K) { 4 | int len = S.size(), count = 1 << K; 5 | if (K > len) return false; 6 | int num = K > 1 ? stoi(S.substr(len - K + 1), 0, 2) << 1 : 0; 7 | vector seen(count, false); 8 | for (int i = len - K; ~i; i--) { 9 | num = (((S[i] - '0') << K) + num) >> 1; 10 | if (!seen[num]) seen[num] = true, count--; 11 | if (!count) return true; 12 | if (i < count) return false; 13 | } 14 | return false; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /LeetCode Problems/1401 to 1500/1464. Maximum Product of Two Elements in an Array/1464. Maximum Product of Two Elements in an Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProduct(vector &nums) { 4 | sort(nums.begin(), nums.end()); 5 | return (nums[nums.size() - 1] - 1) * (nums[nums.size() - 2] - 1); 6 | } 7 | }; -------------------------------------------------------------------------------- /LeetCode Problems/1401 to 1500/1470. Shuffle the Array/1470. Shuffle the Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector shuffle(vector &nums, int n) { 4 | int j = n, k = 0, i = 0; 5 | vector ans(nums.size()); 6 | for (i = 0; i < nums.size() && k < n && j < nums.size(); i++) { 7 | ans[i] = nums[k]; 8 | ans[++i] = nums[j]; 9 | j++, k++; 10 | } 11 | return ans; 12 | } 13 | }; -------------------------------------------------------------------------------- /LeetCode Problems/1401 to 1500/1480. Running Sum of 1d Array/1480. Running Sum of 1d Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector runningSum(vector &nums) { 4 | for (int i = 1; i < nums.size(); i++) { 5 | nums[i] += nums[i - 1]; 6 | } 7 | return nums; 8 | } 9 | }; -------------------------------------------------------------------------------- /LeetCode Problems/1401 to 1500/1480. Running Sum of 1d Array/1480. Running Sum of 1d Array.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def runningSum(self, nums: List[int]) -> List[int]: 3 | for i in range(1, len(nums)): 4 | nums[i] += nums[i - 1] 5 | return nums -------------------------------------------------------------------------------- /LeetCode Problems/1401 to 1500/1492. The kth Factor of n/1492. The kth Factor of n.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int kthFactor(int n, int k) { 4 | int ans = 0, i = 1; 5 | while (k != 0) { 6 | if (i > n) 7 | return -1; 8 | if (n % i == 0) { 9 | i++; 10 | k--; 11 | } else 12 | i++; 13 | } 14 | return i - 1; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /LeetCode Problems/1501 to 1600/1502. Can Make Arithmetic Progression From Sequence/1502. Can Make Arithmetic Progression From Sequence.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canMakeArithmeticProgression(vector &arr) { 4 | sort(arr.begin(), arr.end()); 5 | int temp = arr[1] - arr[0]; 6 | for (int i = 1; i < arr.size() - 1; i++) { 7 | if (arr[i + 1] - arr[i] != temp) 8 | return false; 9 | } 10 | return true; 11 | } 12 | }; -------------------------------------------------------------------------------- /LeetCode Problems/1501 to 1600/1502. Can Make Arithmetic Progression From Sequence/1502. Can Make Arithmetic Progression From Sequence.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean canMakeArithmeticProgression(int[] arr) { 3 | Arrays.sort(arr); 4 | int temp = arr[1] - arr[0]; 5 | for (int i = 1; i < arr.length - 1; i++) { 6 | if (arr[i + 1] - arr[i] != temp) return false; 7 | } 8 | return true; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /LeetCode Problems/1501 to 1600/1512. Number of Good Pairs/1512. Number of Good Pairs.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numIdenticalPairs(vector &nums) { 4 | int cnt = 0; 5 | for (int i = 0; i < nums.size(); i++) { 6 | for (int j = i + 1; j < nums.size(); j++) { 7 | if (nums[i] == nums[j]) 8 | cnt++; 9 | } 10 | } 11 | return cnt; 12 | } 13 | }; -------------------------------------------------------------------------------- /LeetCode Problems/1501 to 1600/1512. Number of Good Pairs/1512. Number of Good Pairs.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numIdenticalPairs(int[] nums) { 3 | int cnt = 0; 4 | for (int i = 0; i < nums.length; i++) { 5 | for (int j = i + 1; j < nums.length; j++) { 6 | if (nums[i] == nums[j]) cnt++; 7 | } 8 | } 9 | return cnt; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /LeetCode Problems/1501 to 1600/1544. Make The String Great/1544. Make The String Great.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string makeGood(string s) { 4 | stack stc; 5 | for (int i = 0; i < s.size(); i++) { 6 | if (stc.empty()) { 7 | stc.push(s[i]); 8 | } else { 9 | if ((stc.top() == tolower(s[i]) || stc.top() == toupper(s[i])) && stc.top() != s[i]) { 10 | stc.pop(); 11 | } else { 12 | stc.push(s[i]); 13 | } 14 | } 15 | } 16 | s = ""; 17 | while (!stc.empty()) { 18 | s += stc.top(); 19 | stc.pop(); 20 | } 21 | reverse(s.begin(), s.end()); 22 | return s; 23 | } 24 | }; -------------------------------------------------------------------------------- /LeetCode Problems/1501 to 1600/1544. Make The String Great/1544. Make The String Great.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String makeGood(String s) { 3 | StringBuffer str = new StringBuffer(s); 4 | int i = 0; 5 | while (i < str.length() - 1) { 6 | if (Character.toLowerCase(str.charAt(i)) == Character.toLowerCase(str.charAt(i + 1)) && (Character.isUpperCase(str.charAt(i)) == Character.isLowerCase(str.charAt(i + 1)))) { 7 | str.deleteCharAt(i); 8 | str.deleteCharAt(i); 9 | if (i > 0) 10 | i--; 11 | else 12 | i = 0; 13 | continue; 14 | } 15 | i += 1; 16 | } 17 | return str.toString(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /LeetCode Problems/1501 to 1600/1544. Make The String Great/1544. Make The String Great.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def makeGood(self, s: str) -> str: 3 | s = list(s) 4 | i = 0 5 | while i < len(s) - 1: 6 | if s[i].lower() == s[i+1].lower() and s[i].isupper() == s[i + 1].islower(): 7 | s.pop(i) 8 | s.pop(i) 9 | i -= 1 if i > 0 else 0 10 | continue 11 | i += 1 12 | return "".join(s); -------------------------------------------------------------------------------- /LeetCode Problems/1501 to 1600/1551. Minimum Operations to Make Array Equal/1551. Minimum Operations to Make Array Equal.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minOperations(int n) { 4 | vector arr(n); 5 | for (int i = 0; i < n; i++) { 6 | arr[i] = 2 * i + 1; 7 | } 8 | int l = 0, r = n - 1, cnt = 0; 9 | while (l <= r) { 10 | cnt += abs(arr[l] - arr[r]) / 2; 11 | l++, r--; 12 | } 13 | return cnt; 14 | } 15 | }; -------------------------------------------------------------------------------- /LeetCode Problems/1601 to 1700/1657. Determine if Two Strings Are Close/1657. Determine if Two Strings Are Close.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool closeStrings(string word1, string word2) { 4 | vector val1(26), val2(26); 5 | set s1, s2; 6 | for (auto it: word1) { 7 | val1[it - 'a']++; 8 | s1.insert(it); 9 | } 10 | for (auto it: word2) { 11 | val2[it - 'a']++; 12 | s2.insert(it); 13 | } 14 | sort(val1.begin(), val1.end()), sort(val2.begin(), val2.end()); 15 | return val1 == val2 && s1 == s2; 16 | } 17 | }; -------------------------------------------------------------------------------- /LeetCode Problems/1601 to 1700/1658. Minimum Operations to Reduce X to Zero/1658. Minimum Operations to Reduce X to Zero.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minOperations(vector &nums, int x) { 4 | int sum = accumulate(nums.begin(), nums.end(), 0); 5 | if (sum < x) return -1; 6 | if (sum == x) return nums.size(); 7 | int diff = sum - x, j = 0, ans = 0, curr_sum = 0; 8 | for (int i = 0; i < nums.size(); i++) { 9 | curr_sum += nums[i]; 10 | while (curr_sum > diff) { 11 | curr_sum -= nums[j++]; 12 | } 13 | if (curr_sum == diff) 14 | ans = max(ans, i - j + 1); 15 | } 16 | return (ans == 0) ? -1 : nums.size() - ans; 17 | } 18 | }; -------------------------------------------------------------------------------- /LeetCode Problems/1601 to 1700/1662. Check If Two String Arrays are Equivalent/1662. Check If Two String Arrays are Equivalent.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool arrayStringsAreEqual(vector &word1, vector &word2) { 4 | string s1 = "", s2 = ""; 5 | for (auto s: word1) s1 += s; 6 | for (auto s: word2) s2 += s; 7 | return s1 == s2; 8 | } 9 | }; -------------------------------------------------------------------------------- /LeetCode Problems/1601 to 1700/1672. Richest Customer Wealth/1672. Richest Customer Wealth.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maximumWealth(vector> &accounts) { 4 | int ans = INT_MIN, n = accounts.size(); 5 | for (int i = 0; i < n; i++) { 6 | int sum = 0; 7 | for (int j = 0; j < accounts[i].size(); j++) { 8 | sum += accounts[i][j]; 9 | } 10 | ans = max(ans, sum); 11 | } 12 | return ans; 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode Problems/1601 to 1700/1685. Sum of Absolute Differences in a Sorted Array/1685. Sum of Absolute Differences in a Sorted Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector getSumAbsoluteDifferences(vector &nums) { 4 | int n = nums.size(); 5 | vector prefix(n + 1), ans; 6 | prefix[0] = 0; 7 | 8 | for (int i = 0; i < n; i++) { 9 | prefix[i + 1] = prefix[i] + nums[i]; 10 | } 11 | for (int i = 0; i < n; i++) { 12 | int p = nums[i] * i - prefix[i]; 13 | int q = prefix[n] - prefix[i + 1] - nums[i] * (n - i - 1); 14 | ans.push_back(p + q); 15 | } 16 | return ans; 17 | } 18 | }; -------------------------------------------------------------------------------- /LeetCode Problems/1601 to 1700/1688. Count of Matches in Tournament/1688. Count of Matches in Tournament.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numberOfMatches(int n) { 4 | return n - 1; 5 | } 6 | }; -------------------------------------------------------------------------------- /LeetCode Problems/1601 to 1700/1690. Stone Game VII/1690. Stone Game VII.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int stoneGameVII(int[] S) { 3 | int N = S.length; 4 | int[] dpCurr = new int[N], dpLast = new int[N]; 5 | for (int i = N - 2; i >= 0; i--) { 6 | int total = S[i]; 7 | int[] temp = dpLast; 8 | dpLast = dpCurr; 9 | dpCurr = temp; 10 | for (int j = i + 1; j < N; j++) { 11 | total += S[j]; 12 | dpCurr[j] = Math.max(total - S[i] - dpLast[j], total - S[j] - dpCurr[j - 1]); 13 | } 14 | } 15 | return dpCurr[N - 1]; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /LeetCode Problems/1601 to 1700/1695. Maximum Erasure Value/1695. Maximum Erasure Value.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maximumUniqueSubarray(vector &nums) { 4 | unordered_set mp; 5 | int curr_sum = 0, sum = 0, i = 0, j = 0; 6 | while (i < nums.size() && j < nums.size()) { 7 | if (mp.find(nums[i]) == mp.end()) { 8 | mp.insert(nums[i]); 9 | curr_sum += nums[i]; 10 | sum = max(curr_sum, sum); 11 | i++; 12 | } else { 13 | mp.erase(nums[j]); 14 | curr_sum -= nums[j]; 15 | j++; 16 | } 17 | } 18 | return sum; 19 | } 20 | }; -------------------------------------------------------------------------------- /LeetCode Problems/1701 to 1800/1704. Determine if String Halves Are Alike/1704. Determine if String Halves Are Alike.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | inline bool isVowel(char ch) { 3 | ch = tolower(ch); 4 | if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') return true; 5 | return false; 6 | } 7 | 8 | public: 9 | bool halvesAreAlike(string s) { 10 | int l = s.size() / 2, cnt_half = 0, cnt_another_half = 0; 11 | for (int i = 0, j = l; i < l && j < s.size(); i++, j++) { 12 | if (isVowel(s[i])) cnt_half++; 13 | if (isVowel(s[j])) cnt_another_half++; 14 | } 15 | return cnt_another_half == cnt_half; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /LeetCode Problems/1701 to 1800/1704. Determine if String Halves Are Alike/1704. Determine if String Halves Are Alike.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | private boolean isVowel(char ch) { 3 | ch = Character.toLowerCase(ch); 4 | if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') return true; 5 | return false; 6 | } 7 | public boolean halvesAreAlike(String s) { 8 | int l = s.length() / 2, cnt_half = 0, cnt_another_half = 0; 9 | for (int i = 0, j = l; i < l && j < s.length(); i++, j++) { 10 | if (isVowel(s.charAt(i))) cnt_half++; 11 | if (isVowel(s.charAt(j))) cnt_another_half++; 12 | } 13 | return cnt_another_half == cnt_half; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /LeetCode Problems/1701 to 1800/1704. Determine if String Halves Are Alike/1704. Determine if String Halves Are Alike.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def halvesAreAlike(self, s, vwls=set(list("aeiouAEIOU"))): 3 | return (sum(c in vwls for c in s[:len(s)//2]) == sum(c in vwls for c in s[len(s)//2:])) 4 | -------------------------------------------------------------------------------- /LeetCode Problems/1701 to 1800/1716. Calculate Money in Leetcode Bank/1716. Calculate Money in Leetcode Bank.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | int totalSum(int n) { 3 | return (long long) (n * (long long) (n + 1)) / 2; 4 | } 5 | 6 | public: 7 | int totalMoney(int n) { 8 | int cnt = 1, sum = 0; 9 | while (n > 0) { 10 | sum += (totalSum(cnt + min(n - 1, 6)) - totalSum(cnt - 1)); 11 | cnt++; 12 | n -= 7; 13 | } 14 | return sum; 15 | } 16 | }; -------------------------------------------------------------------------------- /LeetCode Problems/1701 to 1800/1716. Calculate Money in Leetcode Bank/1716. Calculate Money in Leetcode Bank.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | private int totalSum(Integer n) { 3 | return (n * (n + 1)) / 2; 4 | } 5 | public int totalMoney(int n) { 6 | int cnt = 1, sum = 0; 7 | while (n > 0) { 8 | sum += (totalSum(cnt + Math.min(n - 1, 6)) - totalSum(cnt - 1)); 9 | cnt++; 10 | n -= 7; 11 | } 12 | return sum; 13 | } 14 | } -------------------------------------------------------------------------------- /LeetCode Problems/1701 to 1800/1748. Sum of Unique Elements/1748. Sum of Unique Elements.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int sumOfUnique(vector &nums) { 4 | map arr; 5 | for (auto it: nums) { 6 | arr[it]++; 7 | } 8 | int sum = 0; 9 | for (auto it: arr) { 10 | if (it.second == 1) { 11 | sum += it.first; 12 | } 13 | } 14 | return sum; 15 | } 16 | }; -------------------------------------------------------------------------------- /LeetCode Problems/1701 to 1800/1749. Maximum Absolute Sum of Any Subarray/1749. Maximum Absolute Sum of Any Subarray.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxAbsoluteSum(vector &nums) { 4 | int result = 0, mn = 0, mx = 0; 5 | for (int i = 0; i < nums.size(); i++) { 6 | mn += nums[i]; 7 | result = max(result, abs(mn)); 8 | mn = min(mn, 0); 9 | mx += nums[i]; 10 | result = max(result, abs(mx)); 11 | mx = max(mx, 0); 12 | } 13 | return result; 14 | } 15 | }; -------------------------------------------------------------------------------- /LeetCode Problems/1701 to 1800/1768. Merge Strings Alternately/1768. Merge Strings Alternately.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string mergeAlternately(string word1, string word2) { 4 | string str = ""; 5 | bool check = true; 6 | int i = 0, j = 0; 7 | for (; i < word1.size() && j < word2.size();) { 8 | if (check) { 9 | str += word1[i++]; 10 | check = false; 11 | } else { 12 | str += word2[j++]; 13 | check = true; 14 | } 15 | } 16 | for (; i < word1.size(); i++) 17 | str += word1[i]; 18 | for (; j < word2.size(); j++) 19 | str += word2[j]; 20 | 21 | return str; 22 | } 23 | }; -------------------------------------------------------------------------------- /LeetCode Problems/1801 to 1900/1822. Sign of the Product of an Array/1822. Sign of the Product of an Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int arraySign(vector &nums) { 4 | int sign = 0; 5 | for (auto it: nums) { 6 | if (it == 0) return 0; 7 | if (it < 0) sign++; 8 | } 9 | return sign % 2 == 0 ? 1 : -1; 10 | } 11 | }; -------------------------------------------------------------------------------- /LeetCode Problems/1801 to 1900/1832. Check if the Sentence Is Pangram/1832. Check if the Sentence Is Pangram.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool checkIfPangram(string sentence) { 4 | vector alphabet(26); 5 | for (int i = 0; i < sentence.size(); i++) { 6 | alphabet[sentence[i] - 'a']++; 7 | } 8 | for (auto it: alphabet) { 9 | if (it == 0) 10 | return false; 11 | } 12 | return true; 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode Problems/1801 to 1900/1833. Maximum Ice Cream Bars/1833. Maximum Ice Cream Bars.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxIceCream(vector &costs, int coins) { 4 | sort(costs.begin(), costs.end()); 5 | int sum = 0, cnt = 0; 6 | for (int i = 0; i < costs.size(); i++) { 7 | if (sum + costs[i] <= coins) { 8 | sum += costs[i]; 9 | cnt++; 10 | } 11 | } 12 | return cnt; 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode Problems/1801 to 1900/1833. Maximum Ice Cream Bars/1833. Maximum Ice Cream Bars.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxIceCream(int[] costs, int coins) { 3 | Arrays.sort(costs); 4 | int cost = 0, cnt = 0; 5 | for (int i = 0; i < costs.length; i++) { 6 | if (costs[i] + cost <= coins) { 7 | cost += costs[i]; 8 | cnt++; 9 | } 10 | } 11 | return cnt; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /LeetCode Problems/1801 to 1900/1837. Sum of Digits in Base K/1837. Sum of Digits in Base K.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int sumBase(int n, int k) { 4 | int num = 0, cnt = 0; 5 | while (n != 0) { 6 | int rem = n % k; 7 | int c = pow(10, cnt); 8 | num += rem * c; 9 | n /= k; 10 | cnt++; 11 | } 12 | int sum = 0; 13 | while (num != 0) { 14 | sum += (num % 10); 15 | num /= 10; 16 | } 17 | return sum; 18 | } 19 | }; -------------------------------------------------------------------------------- /LeetCode Problems/1801 to 1900/1877. Minimize Maximum Pair Sum in Array/1877. Minimize Maximum Pair Sum in Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minPairSum(vector &nums) { 4 | ios::sync_with_stdio(false); 5 | cin.tie(0); 6 | cout.tie(0); 7 | sort(nums.begin(), nums.end()); 8 | int ans = INT_MIN; 9 | for (int l = 0, r = nums.size() - 1; l <= r; l++, r--) { 10 | ans = max(ans, nums[l] + nums[r]); 11 | } 12 | return ans; 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode Problems/1801 to 1900/1877. Minimize Maximum Pair Sum in Array/1877. Minimize Maximum Pair Sum in Array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minPairSum(int[] nums) { 3 | Arrays.sort(nums); 4 | int ans = -1; 5 | for (int l = 0, r = nums.length - 1; l <= r; l++, r--) { 6 | ans = Math.max(ans, nums[l] + nums[r]); 7 | } 8 | return ans; 9 | } 10 | } -------------------------------------------------------------------------------- /LeetCode Problems/1901 to 2000/1903. Largest Odd Number in String/1903. Largest Odd Number in String.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | bool isOdd(int num) { 3 | return num % 2; 4 | } 5 | 6 | public: 7 | string largestOddNumber(string num) { 8 | int pos = -1; 9 | for (int i = 0; i < num.size(); i++) { 10 | if (isOdd(num[i] - '0')) { 11 | pos = i; 12 | } 13 | } 14 | return num.substr(0, pos + 1); 15 | } 16 | }; -------------------------------------------------------------------------------- /LeetCode Problems/1901 to 2000/1903. Largest Odd Number in String/1903. Largest Odd Number in String.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String largestOddNumber(String num) { 3 | int pos = -1; 4 | for (int i = 0; i < num.length(); i++) { 5 | if ((num.charAt(i) - '0') % 2 == 1) { 6 | pos = i; 7 | } 8 | } 9 | if (pos < 0) return ""; 10 | return num.substring(0, pos + 1); 11 | } 12 | } -------------------------------------------------------------------------------- /LeetCode Problems/1901 to 2000/1913. Maximum Product Difference Between Two Pairs/1913. Maximum Product Difference Between Two Pairs.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProductDifference(vector &nums) { 4 | sort(nums.begin(), nums.end()); 5 | int n = nums.size() - 1; 6 | return (nums[n] * nums[n - 1] - nums[0] * nums[1]); 7 | } 8 | }; -------------------------------------------------------------------------------- /LeetCode Problems/1901 to 2000/1925. Count Square Sum Triples/1925. Count Square Sum Triples.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countTriples(int n) { 4 | int cnt = 0; 5 | for (int i = 1; i < n; i++) { 6 | for (int j = i + 1; j < n; j++) { 7 | for (int k = j + 1; k <= n; k++) { 8 | if (i * i + j * j == (k) * (k)) 9 | cnt++; 10 | } 11 | } 12 | } 13 | return cnt * 2; 14 | } 15 | }; -------------------------------------------------------------------------------- /LeetCode Problems/1901 to 2000/1929. Concatenation of Array/1929. Concatenation of Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector getConcatenation(vector &nums) { 4 | vector arr(2 * nums.size()); 5 | for (int i = 0; i < nums.size(); i++) { 6 | arr[i] = nums[i]; 7 | arr[i + nums.size()] = nums[i]; 8 | } 9 | return arr; 10 | } 11 | }; -------------------------------------------------------------------------------- /LeetCode Problems/1901 to 2000/2000. Reverse Prefix of Word/2000. Reverse Prefix of Word.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string reversePrefix(string word, char ch) { 4 | string ans = ""; 5 | bool check = true; 6 | for (int i = 0; i < word.size(); i++) { 7 | ans += word[i]; 8 | if (word[i] == ch && check == true) { 9 | reverse(ans.begin(), ans.end()); 10 | check = false; 11 | } 12 | } 13 | return ans; 14 | } 15 | }; -------------------------------------------------------------------------------- /LeetCode Problems/2001 to 2100/2001. Number of Pairs of Interchangeable Rectangles/2001. Number of Pairs of Interchangeable Rectangles.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long long interchangeableRectangles(vector> &rectangles) { 4 | map ratio;//(rectangles.size()); 5 | for (int i = 0; i < rectangles.size(); i++) { 6 | double cal = (double) rectangles[i][0] / rectangles[i][1]; 7 | ratio[cal]++; 8 | } 9 | long long cnt = 0; 10 | for (auto it: ratio) { 11 | if (it.second > 1) { 12 | cnt += ((1 + (it.second - 1)) * (it.second - 1)) / 2; 13 | } 14 | } 15 | return cnt; 16 | } 17 | }; -------------------------------------------------------------------------------- /LeetCode Problems/2001 to 2100/2006. Count Number of Pairs With Absolute Difference K/2006. Count Number of Pairs With Absolute Difference K.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countKDifference(vector &nums, int k) { 4 | int cnt = 0; 5 | for (int i = 0; i < nums.size(); i++) { 6 | for (int j = i + 1; j < nums.size(); j++) { 7 | if (abs(nums[i] - nums[j]) == k) 8 | cnt++; 9 | } 10 | } 11 | return cnt; 12 | } 13 | }; -------------------------------------------------------------------------------- /LeetCode Problems/2001 to 2100/2011. Final Value of Variable After Performing Operations/2011. Final Value of Variable After Performing Operations.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int finalValueAfterOperations(vector &operations) { 4 | int cnt = 0; 5 | for (int i = 0; i < operations.size(); i++) { 6 | if (operations[i] == "++X") { 7 | ++cnt; 8 | } else if (operations[i] == "X++") { 9 | cnt++; 10 | } else if (operations[i] == "--X") { 11 | --cnt; 12 | } else if (operations[i] == "X--") { 13 | cnt--; 14 | } 15 | } 16 | return cnt; 17 | } 18 | }; -------------------------------------------------------------------------------- /LeetCode Problems/2001 to 2100/2022. Convert 1D Array Into 2D Array/2022. Convert 1D Array Into 2D Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> construct2DArray(vector &original, int m, int n) { 4 | if (m * n != original.size()) 5 | return {}; 6 | vector> arr(m, vector(n)); 7 | for (int i = 0, k = 0; i < m; i++) { 8 | for (int j = 0; j < n; j++) { 9 | arr[i][j] = original[k++]; 10 | } 11 | } 12 | return arr; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /LeetCode Problems/2001 to 2100/2023. Number of Pairs of Strings With Concatenation Equal to Target/2023. Number of Pairs of Strings With Concatenation Equal to Target.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numOfPairs(vector &nums, string target) { 4 | int cnt = 0; 5 | for (int i = 0; i < nums.size(); i++) { 6 | for (int j = 0; j < nums.size(); j++) { 7 | if (i == j) 8 | continue; 9 | if (nums[i] + nums[j] == target) 10 | cnt++; 11 | } 12 | } 13 | return cnt; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /LeetCode Problems/2001 to 2100/2038. Remove Colored Pieces if Both Neighbors are the Same Color/2038. Remove Colored Pieces if Both Neighbors are the Same Color.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool winnerOfGame(string colors) { 4 | int A = 0, B = 0; 5 | for (int i = 1; i < colors.size() - 1; i++) { 6 | if (colors[i] == colors[i - 1] && colors[i] == colors[i + 1]) { 7 | if (colors[i] == 'A') 8 | A++; 9 | else 10 | B++; 11 | } 12 | } 13 | if (A > B) 14 | return true; 15 | return false; 16 | } 17 | }; -------------------------------------------------------------------------------- /LeetCode Problems/2001 to 2100/2038. Remove Colored Pieces if Both Neighbors are the Same Color/2038. Remove Colored Pieces if Both Neighbors are the Same Color.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | private String s; 3 | private boolean check(int a, int b) { 4 | return s.charAt(a) == s.charAt(b); 5 | } 6 | public boolean winnerOfGame(String colors) { 7 | int A = 0, B = 0; 8 | s = colors; 9 | for (int i = 1; i < colors.length() - 1; i++) { 10 | if (check(i, i - 1) && check(i, i + 1)) { 11 | if (colors.charAt(i) == 'A') 12 | A++; 13 | else 14 | B++; 15 | } 16 | } 17 | if (A > B) return true; 18 | return false; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /LeetCode Problems/201 to 300/206. Reverse Linked List/206. Reverse Linked List.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | // ListNode *temp2 = new ListNode; 12 | class Solution { 13 | public: 14 | ListNode *reverseList(ListNode *head) { 15 | if (!head || !head->next) 16 | return head; 17 | ListNode *reverse = reverseList(head->next); 18 | head->next->next = head; 19 | head->next = nullptr; 20 | return reverse; 21 | } 22 | }; -------------------------------------------------------------------------------- /LeetCode Problems/201 to 300/207. Course Schedule/207. Course Schedule.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean canFinish(int numCourses, int[][] prerequisites) { 3 | } 4 | } 5 | -------------------------------------------------------------------------------- /LeetCode Problems/201 to 300/209. Minimum Size Subarray Sum/209. Minimum Size Subarray Sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minSubArrayLen(int target, vector &nums) { 4 | int sum = 0, length = INT_MAX, l = 0; 5 | for (int r = 0; r < nums.size(); r++) { 6 | sum += nums[r]; 7 | while (sum >= target) { 8 | length = min(length, r - l + 1); 9 | sum -= nums[l]; 10 | l++; 11 | } 12 | } 13 | return length == INT_MAX ? 0 : length; 14 | } 15 | }; -------------------------------------------------------------------------------- /LeetCode Problems/201 to 300/209. Minimum Size Subarray Sum/209. Minimum Size Subarray Sum.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minSubArrayLen(int target, int[] nums) { 3 | int sum = 0, l = 0, length = 1000000007; 4 | for (int r = 0; r < nums.length; r++) { 5 | sum += nums[r]; 6 | while (sum >= target) { 7 | length = Math.min(length, r - l + 1); 8 | sum -= nums[l]; 9 | l++; 10 | } 11 | } 12 | return length == 1000000007 ? 0 : length; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /LeetCode Problems/201 to 300/211. Design Add and Search Words Data Structure/211. Design Add and Search Words Data Structure.java: -------------------------------------------------------------------------------- 1 | class WordDictionary { 2 | 3 | public WordDictionary() { 4 | } 5 | 6 | public void addWord(String word) { 7 | } 8 | 9 | public boolean search(String word) { 10 | } 11 | } 12 | 13 | /** 14 | * Your WordDictionary object will be instantiated and called as such: 15 | * WordDictionary obj = new WordDictionary(); 16 | * obj.addWord(word); 17 | * boolean param_2 = obj.search(word); 18 | */ 19 | -------------------------------------------------------------------------------- /LeetCode Problems/201 to 300/213. House Robber II/213. House Robber II.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int rob(int[] nums) { 3 | } 4 | } 5 | -------------------------------------------------------------------------------- /LeetCode Problems/201 to 300/217. Contains Duplicate/217. Contains Duplicate.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean containsDuplicate(int[] nums) { 3 | } 4 | } 5 | -------------------------------------------------------------------------------- /LeetCode Problems/201 to 300/219. Contains Duplicate II/219. Contains Duplicate II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool containsNearbyDuplicate(vector &nums, int k) { 4 | map mp; 5 | for (int i = 0; i < nums.size(); i++) { 6 | if (mp.find(nums[i]) != mp.end()) { 7 | if (abs(i - mp[nums[i]]) <= k) 8 | return true; 9 | } 10 | mp[nums[i]] = i; 11 | } 12 | return false; 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode Problems/201 to 300/222. Count Complete Tree Nodes/222. Count Complete Tree Nodes.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 countNodes(TreeNode *root) { 15 | if (root == nullptr) { 16 | return 0; 17 | } 18 | return 1 + countNodes(root->left) + countNodes(root->right); 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /LeetCode Problems/201 to 300/222. Count Complete Tree Nodes/222. Count Complete Tree Nodes.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * int val; 5 | * TreeNode left; 6 | * TreeNode right; 7 | * TreeNode() {} 8 | * TreeNode(int val) { this.val = val; } 9 | * TreeNode(int val, TreeNode left, TreeNode right) { 10 | * this.val = val; 11 | * this.left = left; 12 | * this.right = right; 13 | * } 14 | * } 15 | */ 16 | class Solution { 17 | private int cnt = 0; 18 | public int countNodes(TreeNode root) { 19 | if (root == null) return 0; 20 | if (root.left != null) countNodes(root.left); 21 | cnt += 1; 22 | if (root.right != null) countNodes(root.right); 23 | return cnt; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /LeetCode Problems/201 to 300/223. Rectangle Area/223. Rectangle Area.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) { 4 | int areaA = abs(ax1 - ax2) * abs(ay1 - ay2); 5 | int areaB = abs(bx1 - bx2) * abs(by1 - by2); 6 | 7 | int overlapX = min(ax2, bx2) - max(ax1, bx1); 8 | overlapX = overlapX > 0 ? overlapX : 0; 9 | 10 | int overlapY = min(ay2, by2) - max(ay1, by1); 11 | overlapY = overlapY > 0 ? overlapY : 0; 12 | 13 | return areaA + areaB - (overlapX * overlapY); 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /LeetCode Problems/201 to 300/223. Rectangle Area/223. Rectangle Area.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) { 3 | int areaA = Math.abs(ax1 - ax2) * Math.abs(ay1 - ay2); 4 | int areaB = Math.abs(bx1 - bx2) * Math.abs(by1 - by2); 5 | 6 | int overlapX = Math.min(ax2, bx2) - Math.max(ax1, bx1); 7 | overlapX = overlapX > 0 ? overlapX : 0; 8 | 9 | int overlapY = Math.min(ay2, by2) - Math.max(ay1, by1); 10 | overlapY = overlapY > 0 ? overlapY : 0; 11 | 12 | return areaA + areaB - (overlapX * overlapY); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /LeetCode Problems/201 to 300/223. Rectangle Area/223. Rectangle Area.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def computeArea(self, ax1: int, ay1: int, ax2: int, ay2: int, bx1: int, by1: int, bx2: int, by2: int) -> int: 3 | areaA = abs(ax1-ax2) * abs(ay1 - ay2) 4 | areaB = abs(bx1-bx2) * abs(by1 - by2) 5 | 6 | overlapX = min(bx2, ax2) - max(ax1, bx1) 7 | overlapX = max(overlapX, 0) 8 | 9 | overlapY = min(by2, ay2) - max(ay1, by1) 10 | overlapY = max(overlapY, 0) 11 | 12 | return areaA + areaB - (overlapX * overlapY) 13 | -------------------------------------------------------------------------------- /LeetCode Problems/201 to 300/225. Implement Stack using Queues/225. Implement Stack using Queues.cpp: -------------------------------------------------------------------------------- 1 | class MyStack { 2 | private: 3 | queue q; 4 | 5 | public: 6 | MyStack() { 7 | } 8 | 9 | void push(int x) { 10 | q.push(x); 11 | for (int i = 0; i < q.size() - 1; i++) { 12 | q.push(q.front()); 13 | q.pop(); 14 | } 15 | } 16 | 17 | int pop() { 18 | int val = q.front(); 19 | q.pop(); 20 | return val; 21 | } 22 | 23 | int top() { 24 | return q.front(); 25 | } 26 | 27 | bool empty() { 28 | return q.empty(); 29 | } 30 | }; -------------------------------------------------------------------------------- /LeetCode Problems/201 to 300/226. Invert Binary Tree/README.md: -------------------------------------------------------------------------------- 1 | # 226. Invert Binary Tree 2 | ## [Original Question Link](https://leetcode.com/problems/invert-binary-tree/) 3 | 4 | Given the ```root``` of a binary tree, invert the tree, and return its root. 5 | 6 | #### Example 1: 7 | ![](https://assets.leetcode.com/uploads/2021/03/14/invert1-tree.jpg) 8 | 9 | ``` 10 | Input: root = [4,2,7,1,3,6,9] 11 | Output: [4,7,2,9,6,3,1] 12 | ``` 13 | 14 | #### Example 2: 15 | ![](https://assets.leetcode.com/uploads/2021/03/14/invert2-tree.jpg) 16 | ``` 17 | Input: root = [2,1,3] 18 | Output: [2,3,1] 19 | ``` 20 | 21 | #### Example 3: 22 | ``` 23 | Input: root = [] 24 | Output: [] 25 | ``` 26 | 27 | #### Constraints: 28 | * The number of nodes in the tree is in the range ```[0, 100]```. 29 | * ```-100 <= Node.val <= 100``` -------------------------------------------------------------------------------- /LeetCode Problems/201 to 300/230. Kth Smallest Element in a BST/230. Kth Smallest Element in a BST.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * int val; 5 | * TreeNode left; 6 | * TreeNode right; 7 | * TreeNode() {} 8 | * TreeNode(int val) { this.val = val; } 9 | * TreeNode(int val, TreeNode left, TreeNode right) { 10 | * this.val = val; 11 | * this.left = left; 12 | * this.right = right; 13 | * } 14 | * } 15 | */ 16 | class Solution { 17 | public int kthSmallest(TreeNode root, int k) { 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /LeetCode Problems/201 to 300/235. Lowest Common Ancestor of a Binary Search Tree/235. Lowest Common Ancestor of a Binary Search Tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 | * }; 9 | */ 10 | 11 | class Solution { 12 | public: 13 | TreeNode *lowestCommonAncestor(TreeNode *root, TreeNode *p, TreeNode *q) { 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /LeetCode Problems/201 to 300/236. Lowest Common Ancestor of a Binary Tree/236. Lowest Common Ancestor of a Binary Tree.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * int val; 5 | * TreeNode left; 6 | * TreeNode right; 7 | * TreeNode(int x) { val = x; } 8 | * } 9 | */ 10 | class Solution { 11 | public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /LeetCode Problems/201 to 300/238. Product of Array Except Self/238. Product of Array Except Self.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] productExceptSelf(int[] nums) { 3 | } 4 | } 5 | -------------------------------------------------------------------------------- /LeetCode Problems/201 to 300/242. Valid Anagram/242. Valid Anagram.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isAnagram(string s, string t) { 4 | 5 | if (s.size() != t.size()) { 6 | return false; 7 | } 8 | 9 | int alphabets[26] = {0}; 10 | 11 | for (int i = 0; i < s.size(); i++) { 12 | alphabets[s[i] - 'a']++; 13 | alphabets[t[i] - 'a']--; 14 | } 15 | 16 | bool ans = true; 17 | 18 | for (int i = 0; i < 26; i++) { 19 | if (alphabets[i]) { 20 | ans = false; 21 | break; 22 | } 23 | } 24 | 25 | return ans; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /LeetCode Problems/201 to 300/252. Meeting Rooms/README.md: -------------------------------------------------------------------------------- 1 | ## 252. Meeting Rooms 2 | ### Question 3 | 4 | Given an array of meeting time intervals consisting of start and end times ```[[s1,e1],[s2,e2],...] (si < ei)```, determine if a person could attend all meetings. 5 | 6 | 7 | ##### Example 1: 8 | ``` 9 | Input: [[0,30],[5,10],[15,20]] 10 | Output: false 11 | 12 | Explanation: 13 | (0,30), (5,10) and (0,30),(15,20) will conflict 14 | ``` 15 | 16 | ##### Example 2: 17 | ``` 18 | Input: [[7,10],[2,4]] 19 | Output: true 20 | 21 | Explanation: 22 | Two times will not conflict 23 | ``` 24 | 25 | ### Original Question [link here](https://leetcode.com/problems/meeting-rooms/) 26 | -------------------------------------------------------------------------------- /LeetCode Problems/201 to 300/253. Meeting Rooms II/README.md: -------------------------------------------------------------------------------- 1 | ## 253. Meeting Rooms II 2 | 3 | ### Question 4 | Given an array of meeting time intervals consisting of start and end times ```[[s1,e1],[s2,e2],...] (si < ei)```, 5 | find the minimum number of conference rooms required. 6 | 7 | ##### Example 1: 8 | ``` 9 | Input: intervals = [(0,30),(5,10),(15,20)] 10 | Output: 2 11 | 12 | Explanation: 13 | We need two meeting rooms 14 | room1: (0,30) 15 | room2: (5,10),(15,20) 16 | ``` 17 | 18 | ##### Example 2: 19 | ``` 20 | Input: intervals = [(2,7)] 21 | Output: 1 22 | 23 | Explanation: 24 | Only need one meeting room 25 | ``` 26 | 27 | ### Original Question [link here](https://leetcode.com/problems/meeting-rooms-ii/) 28 | -------------------------------------------------------------------------------- /LeetCode Problems/201 to 300/258. Add Digits/258. Add Digits.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int DigitsSum(int num) { 4 | int sum = 0; 5 | while (num != 0) { 6 | sum += num % 10; 7 | num /= 10; 8 | } 9 | return sum; 10 | } 11 | int addDigits(int num) { 12 | int dSum = DigitsSum(num); 13 | cout << dSum << '\n'; 14 | while (true) { 15 | if (dSum <= 9) { 16 | return dSum; 17 | } 18 | dSum = DigitsSum(dSum); 19 | } 20 | return 0; 21 | } 22 | }; -------------------------------------------------------------------------------- /LeetCode Problems/201 to 300/258. Add Digits/258. Add Digits.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int addDigits(int num) { 3 | int sum = 0; 4 | while (num > 0) { 5 | sum += num % 10; 6 | num /= 10; 7 | } 8 | if (sum < 10) { 9 | return sum; 10 | } else { 11 | return addDigits(sum); 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /LeetCode Problems/201 to 300/261. Graph Valid Tree/README.md: -------------------------------------------------------------------------------- 1 | ## 261. Graph Valid Tree 2 | 3 | ### Question 4 | 5 | Given ```n``` nodes labeled from ```0``` to ```n - 1``` and a list of ```undirected``` edges (each edge is a pair of nodes), 6 |
write a function to check whether these edges make up a valid tree. 7 | 8 | ##### Example 1: 9 | 10 | ``` 11 | Input: n = 5 edges = [[0, 1], [0, 2], [0, 3], [1, 4]] 12 | Output: true. 13 | ``` 14 | 15 | ##### Example 2: 16 | ``` 17 | Input: n = 5 edges = [[0, 1], [1, 2], [2, 3], [1, 3], [1, 4]] 18 | Output: false. 19 | ``` 20 | 21 | ### Original Question [link here](https://leetcode.com/problems/graph-valid-tree) 22 | -------------------------------------------------------------------------------- /LeetCode Problems/201 to 300/263. Ugly Number/263. Ugly Number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isUgly(int n) { 4 | if (n <= 0) return false; 5 | while (n % 2 == 0) 6 | n /= 2; 7 | while (n % 3 == 0) 8 | n /= 3; 9 | while (n % 5 == 0) 10 | n /= 5; 11 | return n == 1; 12 | } 13 | }; -------------------------------------------------------------------------------- /LeetCode Problems/201 to 300/263. Ugly Number/263. Ugly Number.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isUgly(int n) { 3 | if (n <= 0) return false; 4 | while (n % 2 == 0) n /= 2; 5 | while (n % 3 == 0) n /= 3; 6 | while (n % 5 == 0) n /= 5; 7 | return n == 1; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /LeetCode Problems/201 to 300/263. Ugly Number/263. Ugly Number.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isUgly(self, n: int) -> bool: 3 | if n <= 0: return 0; 4 | while (n % 2 == 0): 5 | n //= 2; 6 | while (n % 3 == 0): 7 | n //= 3; 8 | while (n % 5 == 0): 9 | n //= 5; 10 | return n == 1; -------------------------------------------------------------------------------- /LeetCode Problems/201 to 300/268. Missing Number/268. Missing Number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int missingNumber(vector &nums) { 4 | int sum = 0; 5 | for (auto num: nums) 6 | sum += num; 7 | int numsLength = nums.size(); 8 | int total = numsLength * (numsLength + 1) / 2; 9 | return total - sum; 10 | } 11 | }; -------------------------------------------------------------------------------- /LeetCode Problems/201 to 300/278. First Bad Version/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 search(long l, long r) { 7 | while (l <= r) { 8 | long long m = (l + r) / 2; 9 | if (isBadVersion(m) && !isBadVersion(m - 1)) { 10 | return (int) m; 11 | } 12 | if (!isBadVersion(m)) { 13 | return search(m + 1, r); 14 | } 15 | return search(l, m); 16 | } 17 | return l; 18 | } 19 | int firstBadVersion(int n) { 20 | return search(1, (long long) n); 21 | } 22 | }; -------------------------------------------------------------------------------- /LeetCode Problems/201 to 300/279. Perfect Squares/279. Perfect Squares.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | vector dp; 3 | const int inf = 10e4 + 7; 4 | 5 | public: 6 | int numSquares(int n) { 7 | dp.assign(n + 1, inf); 8 | dp[0] = 0; 9 | for (int i = 1; i <= n; i++) { 10 | for (int j = 0; j * j <= i; j++) 11 | dp[i] = min(dp[i], 1 + dp[i - j * j]); 12 | } 13 | return dp[n]; 14 | } 15 | }; -------------------------------------------------------------------------------- /LeetCode Problems/201 to 300/279. Perfect Squares/279. Perfect Squares.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numSquares(int n) { 3 | ArrayList dp = new ArrayList<>(); 4 | dp.add(0); 5 | for (int i = 1; i <= n; i++) dp.add(100007); 6 | for (int i = 1; i <= n; i++) { 7 | for (int j = 0; j * j <= i; j++) dp.set(i, Math.min(dp.get(i), 1 + dp.get(i - j * j))); 8 | } 9 | return dp.get(n); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /LeetCode Problems/201 to 300/283. Move Zeroes/283. Move Zeroes.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void moveZeroes(vector &nums) { 4 | int n = nums.size(), cnt = 0; 5 | vector ans; 6 | for (int i = 0; i < n; i++) { 7 | if (nums[i] == 0) 8 | cnt++; 9 | else 10 | ans.push_back(nums[i]); 11 | } 12 | for (int i = 0; i < cnt; i++) 13 | ans.push_back(0); 14 | nums = ans; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /LeetCode Problems/201 to 300/283. Move Zeroes/283. Move Zeroes.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public void moveZeroes(int[] nums) { 3 | int k = 0; 4 | for (int i = 0; i < nums.length; i++) { 5 | if (nums[i] != 0) { 6 | nums[k++] = nums[i]; 7 | } 8 | } 9 | for (int i = k; i < nums.length; i++) { 10 | nums[i] = 0; 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /LeetCode Problems/201 to 300/295. Find Median from Data Stream/295. Find Median from Data Stream.cpp: -------------------------------------------------------------------------------- 1 | class MedianFinder { 2 | multiset list; 3 | multiset::iterator median = list.begin(); 4 | 5 | public: 6 | MedianFinder() { 7 | list.clear(); 8 | } 9 | void addNum(int num) { 10 | list.insert(num); 11 | if (list.size() % 2) ++median; 12 | if (num < *median) --median; 13 | } 14 | double findMedian() { 15 | if (list.size() % 2 != 0) return *(median); 16 | return (*median + *next(median)) / 2.0; 17 | } 18 | }; -------------------------------------------------------------------------------- /LeetCode Problems/201 to 300/300. Longest Increasing Subsequence/300. Longest Increasing Subsequence.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int lengthOfLIS(int[] nums) { 3 | } 4 | } 5 | -------------------------------------------------------------------------------- /LeetCode Problems/2201 to 2300/2225. Find Players With Zero or One Losses/2225. Find Players With Zero or One Losses.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> findWinners(vector> &matches) { 4 | map lose, wine; 5 | for (int i = 0; i < matches.size(); i++) { 6 | wine[matches[i][0]]++, lose[matches[i][1]]++; 7 | } 8 | vector> ans(2); 9 | for (auto it: wine) { 10 | if (lose[it.first] == 0) 11 | ans[0].push_back(it.first); 12 | } 13 | for (auto it: lose) { 14 | if (it.second == 1) 15 | ans[1].push_back(it.first); 16 | } 17 | return ans; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /LeetCode Problems/2201 to 2300/2244. Minimum Rounds to Complete All Tasks/2244. Minimum Rounds to Complete All Tasks.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minimumRounds(vector &tasks) { 4 | map mp; 5 | for (int i = 0; i < tasks.size(); i++) { 6 | mp[tasks[i]]++; 7 | } 8 | int cnt = 0; 9 | for (auto it: mp) { 10 | if (it.second <= 1) 11 | return -1; 12 | cnt += it.second % 3 == 0 ? it.second / 3 : (it.second / 3) + 1; 13 | } 14 | return cnt; 15 | } 16 | }; -------------------------------------------------------------------------------- /LeetCode Problems/2201 to 2300/2244. Minimum Rounds to Complete All Tasks/2244. Minimum Rounds to Complete All Tasks.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minimumRounds(int[] tasks) { 3 | HashMap mp = new HashMap<>(); 4 | for (int i = 0; i < tasks.length; i++) { 5 | if (mp.get(tasks[i]) == null) 6 | mp.put(tasks[i], 1); 7 | else 8 | mp.put(tasks[i], mp.get(tasks[i]) + 1); 9 | } 10 | int cnt = 0; 11 | for (Map.Entry item : mp.entrySet()) { 12 | int value = item.getValue(); 13 | if (value <= 1) return -1; 14 | cnt += value % 3 == 0 ? value / 3 : (value / 3) + 1; 15 | } 16 | return cnt; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /LeetCode Problems/2201 to 2300/2278. Percentage of Letter in String/2278. Percentage of Letter in String.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int percentageLetter(string s, char letter) { 4 | int n = s.size(), cnt = 0; 5 | for (int i = 0; i < n; i++) 6 | if (s[i] == letter) 7 | cnt++; 8 | return (cnt * 100) / n; 9 | } 10 | }; -------------------------------------------------------------------------------- /LeetCode Problems/2201 to 2300/2293. Min Max Game/2293. Min Max Game.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minMaxGame(vector &nums) { 4 | int n = nums.size(); 5 | while (n != 1) { 6 | vector arr; 7 | int k = 0; 8 | for (int i = 0; i < nums.size() / 2; i++) { 9 | if (i % 2 == 0) { 10 | arr.push_back(min(nums[2 * i], nums[2 * i + 1])); 11 | } else { 12 | arr.push_back(max(nums[2 * i], nums[2 * i + 1])); 13 | } 14 | } 15 | n /= 2; 16 | nums = arr; 17 | } 18 | return nums[0]; 19 | } 20 | }; -------------------------------------------------------------------------------- /LeetCode Problems/2201 to 2300/2294. Partition Array Such That Maximum Difference Is K/2294. Partition Array Such That Maximum Difference Is K.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int partitionArray(vector &nums, int k) { 4 | sort(nums.begin(), nums.end(), greater()); 5 | int cnt = 0, start = nums[0]; 6 | for (int i = 1; i < nums.size(); i++) { 7 | if (start - nums[i] > k) { 8 | start = nums[i]; 9 | cnt++; 10 | } 11 | } 12 | return cnt + 1; 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode Problems/2201 to 2300/2295. Replace Elements in an Array/2295. Replace Elements in an Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector arrayChange(vector &nums, vector> &operations) { 4 | map mp; 5 | for (int i = 0; i < nums.size(); i++) { 6 | mp[nums[i]] = i; 7 | } 8 | for (int i = 0; i < operations.size(); i++) { 9 | int x = operations[i][0], y = operations[i][1]; 10 | int idx = mp[x]; 11 | nums[idx] = y; 12 | mp.erase(x); 13 | mp[y] = idx; 14 | } 15 | return nums; 16 | } 17 | }; -------------------------------------------------------------------------------- /LeetCode Problems/2301 to 2400/2303. Calculate Amount Paid in Taxes/2303. Calculate Amount Paid in Taxes.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | double calculateTax(vector> &brackets, int income) { 4 | double tax = 0; 5 | int prev = 0; 6 | for (auto it: brackets) { 7 | int rep = min(income, it[0]); 8 | tax += double(rep - prev) * (double) it[1] / 100.0; 9 | prev = it[0]; 10 | if (income <= it[0]) 11 | break; 12 | } 13 | return tax; 14 | } 15 | }; -------------------------------------------------------------------------------- /LeetCode Problems/2301 to 2400/2305. Fair Distribution of Cookies/2305. Fair Distribution of Cookies.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | int sum[10] = {}; 3 | int ans = 1000000000; 4 | 5 | public: 6 | void calculate(vector &arr, int n, int k) { 7 | if (n == arr.size()) { 8 | int max = 0; 9 | for (int i = 0; i < k; i++) { 10 | max = std::max(max, sum[i]); 11 | } 12 | ans = min(max, ans); 13 | return; 14 | } 15 | for (int i = 0; i < k; i++) { 16 | sum[i] += arr[n]; 17 | calculate(arr, n + 1, k); 18 | sum[i] -= arr[n]; 19 | } 20 | } 21 | int distributeCookies(vector &cookies, int k) { 22 | calculate(cookies, 0, k); 23 | return ans; 24 | } 25 | }; -------------------------------------------------------------------------------- /LeetCode Problems/2301 to 2400/2389. Longest Subsequence With Limited Sum/2389. Longest Subsequence With Limited Sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector answerQueries(vector &nums, vector &queries) { 4 | sort(nums.begin(), nums.end()); 5 | for (int i = 1; i < nums.size(); i++) 6 | nums[i] += nums[i - 1]; 7 | vector ans; 8 | for (auto ele: queries) { 9 | int idx = upper_bound(nums.begin(), nums.end(), ele) - nums.begin(); 10 | ans.push_back(idx); 11 | } 12 | return ans; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /LeetCode Problems/2301 to 2400/2390. Removing Stars From a String/2390. Removing Stars From a String.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string removeStars(string s) { 4 | string st = ""; 5 | for (int i = 0; i < s.size(); i++) { 6 | if (s[i] == '*') 7 | st.pop_back(); 8 | else 9 | st.push_back(s[i]); 10 | } 11 | return st; 12 | } 13 | }; -------------------------------------------------------------------------------- /LeetCode Problems/2401 to 2500/2405. Optimal Partition of String/2405. Optimal Partition of String.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int partitionString(string s) { 4 | map mp; 5 | int ans = 1; 6 | for (char ch: s) { 7 | if (mp[ch] >= 1) { 8 | ans++; 9 | mp.clear(); 10 | } 11 | mp[ch]++; 12 | } 13 | return ans; 14 | } 15 | }; -------------------------------------------------------------------------------- /LeetCode Problems/2401 to 2500/2485. Find the Pivot Integer/2485. Find the Pivot Integer.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int pivotInteger(int n) { 4 | vector sum_l(n), sum_r(n); 5 | sum_l[0] = 1, sum_r[n - 1] = n; 6 | for (int i = 1; i < n; i++) 7 | sum_l[i] = sum_l[i - 1] + (i + 1); 8 | for (int i = n - 2; i >= 0; i--) 9 | sum_r[i] = sum_r[i + 1] + (i + 1); 10 | for (int i = 0; i < n; i++) 11 | if (sum_l[i] == sum_r[i]) 12 | return i + 1; 13 | return -1; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /LeetCode Problems/2401 to 2500/2486. Append Characters to String to Make Subsequence/2486. Append Characters to String to Make Subsequence.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int appendCharacters(string s, string t) { 4 | int j = 0; 5 | for (int i = 0; i < s.size() && j < t.size(); i++) { 6 | if (s[i] == t[j]) 7 | j++; 8 | } 9 | return t.size() - j; 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /LeetCode Problems/2701 to 2800/2785. Sort Vowels in a String/2785. Sort Vowels in a String.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | bool isVowel(char ch) { 3 | ch = tolower(ch); 4 | if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') 5 | return true; 6 | return false; 7 | } 8 | 9 | public: 10 | string sortVowels(string s) { 11 | vector vowels; 12 | for (char ch: s) { 13 | if (isVowel(ch)) 14 | vowels.push_back(ch); 15 | } 16 | sort(vowels.begin(), vowels.end()); 17 | for (int i = 0, j = 0; i < s.size() && j < vowels.size(); i++) { 18 | if (isVowel(s[i])) { 19 | s[i] = vowels[j++]; 20 | } 21 | } 22 | 23 | return s; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /LeetCode Problems/3001 to 3100/3075. Maximize Happiness of Selected Children/3075. Maximize Happiness of Selected Children.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long long maximumHappinessSum(vector &happiness, int k) { 4 | long long sum = 0; 5 | sort(happiness.begin(), happiness.end(), greater()); 6 | for (int i = 0; i < k; i++) { 7 | if ((happiness[i] - i) > 0) 8 | sum += (happiness[i] - i); 9 | } 10 | return sum; 11 | } 12 | }; -------------------------------------------------------------------------------- /LeetCode Problems/301 to 400/322. Coin Change/322. Coin Change.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int coinChange(vector &coins, int amount) { 4 | int n = coins.size(); 5 | vector> dp(n + 1, vector(amount + 1)); 6 | for (int i = 0; i <= n; i++) { 7 | for (int j = 0; j <= amount; j++) { 8 | if (i == 0) dp[i][j] = 10e5; 9 | else if (j == 0) 10 | dp[i][j] = 0; 11 | else if (coins[i - 1] > j) 12 | dp[i][j] = dp[i - 1][j]; 13 | else 14 | dp[i][j] = min(dp[i - 1][j], 1 + dp[i][j - coins[i - 1]]); 15 | } 16 | } 17 | return dp[n][amount] == 10e5 ? -1 : dp[n][amount]; 18 | } 19 | }; -------------------------------------------------------------------------------- /LeetCode Problems/301 to 400/322. Coin Change/322. Coin Change.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int coinChange(int[] coins, int amount) { 3 | int dp[] = new int[amount + 1]; 4 | for (int i = 0; i < amount + 1; i++) { 5 | dp[i] = amount + 1; 6 | } 7 | dp[0] = 0; 8 | int a; 9 | for (a = 1; a < amount + 1; a++) { 10 | for (int c : coins) { 11 | if (a - c >= 0) { 12 | dp[a] = Math.min(dp[a], 1 + dp[a - c]); 13 | } 14 | } 15 | } 16 | if (dp[amount] != amount + 1) { 17 | return dp[amount]; 18 | } else { 19 | return -1; 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /LeetCode Problems/301 to 400/326. Power of Three/326. Power of Three.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPowerOfThree(int n) { 4 | if (n > 1) { 5 | while (n % 3 == 0) { 6 | n = n / 3; 7 | } 8 | } 9 | return n == 1; 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /LeetCode Problems/301 to 400/327. Count of Range Sum/327. Count of Range Sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countRangeSum(vector &nums, int lower, int upper) { 4 | int res = 0; 5 | long long sum = 0; 6 | multiset sums; 7 | sums.insert(0); 8 | for (int i = 0; i < nums.size(); ++i) { 9 | sum += nums[i]; 10 | res += distance(sums.lower_bound(sum - upper), sums.upper_bound(sum - lower)); 11 | sums.insert(sum); 12 | } 13 | return res; 14 | } 15 | }; -------------------------------------------------------------------------------- /LeetCode Problems/301 to 400/338. Counting Bits/338. Counting Bits.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int binaryNum(int n) { 4 | int res = 0; 5 | while (n != 0) { 6 | int r = n % 2; 7 | res += r; 8 | n /= 2; 9 | } 10 | return res; 11 | } 12 | vector countBits(int n) { 13 | vector ans(n + 1); 14 | for (int i = 0; i <= n; i++) { 15 | ans[i] = binaryNum(i); 16 | } 17 | return ans; 18 | } 19 | }; -------------------------------------------------------------------------------- /LeetCode Problems/301 to 400/343. Integer Break/343. Integer Break.c: -------------------------------------------------------------------------------- 1 | int integerBreak(int n) { 2 | if (n <= 3) 3 | return n - 1; 4 | int ans = 1; 5 | while (n > 4) { 6 | ans *= 3; 7 | n -= 3; 8 | } 9 | return n * ans; 10 | } -------------------------------------------------------------------------------- /LeetCode Problems/301 to 400/343. Integer Break/343. Integer Break.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int integerBreak(int n) { 4 | if (n <= 3) 5 | return n - 1; 6 | int ans = 1; 7 | while (n > 4) { 8 | ans *= 3; 9 | n -= 3; 10 | } 11 | return n * ans; 12 | } 13 | }; -------------------------------------------------------------------------------- /LeetCode Problems/301 to 400/343. Integer Break/343. Integer Break.cs: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int IntegerBreak(int n) { 3 | if (n <= 3) return n - 1; 4 | int ans = 1; 5 | while (n > 4) { 6 | ans *= 3; 7 | n -= 3; 8 | } 9 | return n * ans; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /LeetCode Problems/301 to 400/343. Integer Break/343. Integer Break.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int integerBreak(int n) { 3 | if (n <= 3) return n - 1; 4 | int ans = 1; 5 | while (n > 4) { 6 | ans *= 3; 7 | n -= 3; 8 | } 9 | return n * ans; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /LeetCode Problems/301 to 400/343. Integer Break/343. Integer Break.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number} n 3 | * @return {number} 4 | */ 5 | var integerBreak = function(n) { 6 | if (n <= 3) 7 | return n - 1 8 | var ans = 1 9 | while (n > 4) { 10 | n -= 3 11 | ans *= 3 12 | } 13 | return ans * n 14 | }; -------------------------------------------------------------------------------- /LeetCode Problems/301 to 400/343. Integer Break/343. Integer Break.kt: -------------------------------------------------------------------------------- 1 | class Solution { 2 | fun integerBreak(n: Int): Int { 3 | if (n <= 3) 4 | return n - 1; 5 | var ans = 1; 6 | var num = n; 7 | while (num > 4) { 8 | ans *= 3; 9 | num -= 3; 10 | } 11 | return ans * num; 12 | } 13 | } -------------------------------------------------------------------------------- /LeetCode Problems/301 to 400/343. Integer Break/343. Integer Break.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def integerBreak(self, n: int) -> int: 3 | if n <= 3: 4 | return n - 1 5 | ans = 1 6 | while n > 4: 7 | ans *= 3 8 | n -= 3 9 | return ans * n 10 | -------------------------------------------------------------------------------- /LeetCode Problems/301 to 400/345. Reverse Vowels of a String/345. Reverse Vowels of a String.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isVowel(char ch) { 4 | if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') 5 | return true; 6 | if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') 7 | return true; 8 | return false; 9 | } 10 | string reverseVowels(string s) { 11 | int l = 0, r = s.size() - 1; 12 | while (l < r) { 13 | if (isVowel(s[l]) && isVowel(s[r])) { 14 | swap(s[l], s[r]); 15 | l++, r--; 16 | } 17 | if (!isVowel(s[l])) 18 | l++; 19 | if (!isVowel(s[r])) 20 | r--; 21 | } 22 | return s; 23 | } 24 | }; -------------------------------------------------------------------------------- /LeetCode Problems/301 to 400/347. Top K Frequent Elements/347. Top K Frequent Elements.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector topKFrequent(vector &nums, int k) { 4 | } 5 | }; 6 | -------------------------------------------------------------------------------- /LeetCode Problems/301 to 400/350. Intersection of Two Arrays II/350. Intersection of Two Arrays II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector intersect(vector &nums1, vector &nums2) { 4 | sort(nums1.begin(), nums1.end()); 5 | sort(nums2.begin(), nums2.end()); 6 | vector vct; 7 | int i = 0, j = 0; 8 | while (i < nums1.size() && j < nums2.size()) { 9 | if (nums1[i] < nums2[j]) 10 | i++; 11 | else if (nums1[i] > nums2[j]) 12 | j++; 13 | else { 14 | vct.push_back(nums1[i]); 15 | i++; 16 | j++; 17 | } 18 | } 19 | return vct; 20 | } 21 | }; -------------------------------------------------------------------------------- /LeetCode Problems/301 to 400/371. Sum of Two Integers/371. Sum of Two Integers.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int getSum(int a, int b) { 3 | if (a == 0) return b; 4 | if (b == 0) return a; 5 | while (b != 0) { 6 | int carry = (a & b) << 1; 7 | a = a ^ b; 8 | b = carry; 9 | } 10 | return a; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /LeetCode Problems/301 to 400/371. Sum of Two Integers/README.md: -------------------------------------------------------------------------------- 1 | # [371. Sum of Two Integers](https://leetcode.com/problems/sum-of-two-integers/) 2 | 3 |

Given two integers a and b, return the sum of the two integers without using the operators + and -.

4 | 5 |

 

6 |

Example 1:

7 |
Input: a = 1, b = 2
 8 | Output: 3
 9 | 

Example 2:

10 |
Input: a = 2, b = 3
11 | Output: 5
12 | 
13 |

 

14 |

Constraints:

15 | 16 |
    17 |
  • -1000 <= a, b <= 1000
  • 18 |
19 |
20 | -------------------------------------------------------------------------------- /LeetCode Problems/301 to 400/374. Guess Number Higher or Lower/374. Guess Number Higher or Lower.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Forward declaration of guess API. 3 | * @param num your guess 4 | * @return -1 if num is lower than the guess number 5 | * 1 if num is higher than the guess number 6 | * otherwise return 0 7 | * int guess(int num); 8 | */ 9 | typedef long long int ll; 10 | class Solution { 11 | public: 12 | int guessNumber(int n) { 13 | ll f = 1; 14 | while (f <= n) { 15 | int m = (f + n) / 2; 16 | int gues = guess(m); 17 | if (gues == 0) { 18 | return m; 19 | } else if (gues == 1) { 20 | f = m + 1; 21 | } else 22 | n = m - 1; 23 | } 24 | return 0; 25 | } 26 | }; -------------------------------------------------------------------------------- /LeetCode Problems/301 to 400/374. Guess Number Higher or Lower/374. Guess Number Higher or Lower.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Forward declaration of guess API. 3 | * @param num your guess 4 | * @return -1 if num is higher than the picked number 5 | * 1 if num is lower than the picked number 6 | * otherwise return 0 7 | * int guess(int num); 8 | */ 9 | 10 | public class Solution extends GuessGame { 11 | public int guessNumber(int n) { 12 | int l = 1, m, pick; 13 | while (l <= n) { 14 | m = l + (n - l) / 2; 15 | pick = guess(m); 16 | if (pick == 0) 17 | return m; 18 | else if (pick == -1) 19 | n = m - 1; 20 | else 21 | l = m + 1; 22 | } 23 | return 0; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /LeetCode Problems/301 to 400/374. Guess Number Higher or Lower/374. Guess Number Higher or Lower.py: -------------------------------------------------------------------------------- 1 | # The guess API is already defined for you. 2 | # @param num, your guess 3 | # @return -1 if num is higher than the picked number 4 | # 1 if num is lower than the picked number 5 | # otherwise return 0 6 | # def guess(num: int) -> int: 7 | 8 | class Solution: 9 | def guessNumber(self, n): 10 | l = 1 11 | while l <= n: 12 | m = l + (n - l) // 2 13 | pick = guess(m) 14 | if pick == 0: 15 | return m 16 | elif pick == -1: 17 | n = m - 1 18 | else: 19 | l = m + 1 20 | return 0 -------------------------------------------------------------------------------- /LeetCode Problems/301 to 400/378. Kth Smallest Element in a Sorted Matrix/378. Kth Smallest Element in a Sorted Matrix.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int kthSmallest(vector> &matrix, int k) { 4 | int l = matrix[0][0], n = matrix.size(), r = matrix[n - 1][n - 1]; 5 | while (l <= r) { 6 | int m = (l + r) / 2, result = 0; 7 | for (int i = 0; i < n; i++) { 8 | result += upper_bound(matrix[i].begin(), matrix[i].end(), m) - matrix[i].begin(); 9 | } 10 | if (result < k) 11 | l = m + 1; 12 | else 13 | r = m - 1; 14 | } 15 | return l; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /LeetCode Problems/301 to 400/383. Ransom Note/383. Ransom Note.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canConstruct(string ransomNote, string magazine) { 4 | map mp; 5 | for (int i = 0; i < ransomNote.size(); i++) { 6 | mp[ransomNote[i]]++; 7 | } 8 | int cnt = 0; 9 | for (int i = 0; i < magazine.size(); i++) { 10 | if (mp[magazine[i]] > 0) 11 | mp[magazine[i]]--; 12 | } 13 | for (auto it: mp) { 14 | cnt += it.second; 15 | } 16 | return cnt == 0 ? true : false; 17 | } 18 | }; -------------------------------------------------------------------------------- /LeetCode Problems/301 to 400/392. Is Subsequence/392. Is Subsequence.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isSubsequence(string s, string t) { 4 | int pos = 0; 5 | for (int i = 0; i < t.size(); i++) { 6 | if (t[i] == s[pos]) pos++; 7 | } 8 | if (pos == s.size()) 9 | return true; 10 | return false; 11 | } 12 | }; -------------------------------------------------------------------------------- /LeetCode Problems/301 to 400/392. Is Subsequence/392. Is Subsequence.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isSubsequence(String s, String t) { 3 | int j = 0; 4 | for (int i = 0; i < t.length() && j < s.length(); i++) { 5 | if (t.charAt(i) == s.charAt(j)) j++; 6 | } 7 | if (j == s.length()) return true; 8 | return false; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /LeetCode Problems/401 to 500/412. Fizz Buzz/412. Fizz Buzz.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector fizzBuzz(int n) { 4 | vector ans; 5 | for (int i = 1; i <= n; i++) { 6 | if (i % 3 == 0 && i % 5 == 0) { 7 | ans.push_back("FizzBuzz"); 8 | } else if (i % 3 == 0) { 9 | ans.push_back("Fizz"); 10 | } else if (i % 5 == 0) { 11 | ans.push_back("Buzz"); 12 | } else { 13 | ans.push_back(to_string(i)); 14 | } 15 | } 16 | return ans; 17 | } 18 | }; -------------------------------------------------------------------------------- /LeetCode Problems/401 to 500/416. Partition Equal Subset Sum/416. Partition Equal Subset Sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canPartition(vector &nums) { 4 | if (nums.size() == 1) return false; 5 | int sum = 0; 6 | for (auto it: nums) sum += it; 7 | if (sum % 2 == 0) { 8 | bitset<20001> bits(1); 9 | for (auto it: nums) { 10 | bits |= bits << it; 11 | } 12 | return bits[sum / 2]; 13 | } else { 14 | return false; 15 | } 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /LeetCode Problems/401 to 500/417. Pacific Atlantic Water Flow/417. Pacific Atlantic Water Flow.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> pacificAtlantic(vector> &heights) { 4 | } 5 | }; 6 | -------------------------------------------------------------------------------- /LeetCode Problems/401 to 500/424. Longest Repeating Character Replacement/424. Longest Repeating Character Replacement.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int characterReplacement(string s, int k) { 4 | } 5 | }; 6 | -------------------------------------------------------------------------------- /LeetCode Problems/401 to 500/435. Non-overlapping Intervals/435. Non-overlapping Intervals.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int eraseOverlapIntervals(vector> &intervals) { 4 | } 5 | }; 6 | -------------------------------------------------------------------------------- /LeetCode Problems/401 to 500/441. Arranging Coins/441. Arranging Coins.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int arrangeCoins(int s) { 4 | return (int) (-1 + sqrt(1 + 8 * (long long) s)) / 2; 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /LeetCode Problems/401 to 500/449. Serialize and Deserialize BST/449. Serialize and Deserialize BST.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 | * }; 9 | */ 10 | class Codec { 11 | public: 12 | // Encodes a tree to a single string. 13 | string serialize(TreeNode *root) { 14 | } 15 | 16 | // Decodes your encoded data to tree. 17 | TreeNode *deserialize(string data) { 18 | } 19 | }; 20 | 21 | // Your Codec object will be instantiated and called as such: 22 | // Codec* ser = new Codec(); 23 | // Codec* deser = new Codec(); 24 | // string tree = ser->serialize(root); 25 | // TreeNode* ans = deser->deserialize(tree); 26 | // return ans; 27 | -------------------------------------------------------------------------------- /LeetCode Problems/401 to 500/452. Minimum Number of Arrows to Burst Balloons/452. Minimum Number of Arrows to Burst Balloons.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | private: 3 | static bool cmp(vector &a, vector &b) { 4 | return a[1] < b[1]; 5 | } 6 | 7 | public: 8 | int findMinArrowShots(vector> &points) { 9 | sort(points.begin(), points.end(), cmp); 10 | 11 | int ans = 0, rep = 0; 12 | for (int i = 0; i < points.size(); i++) { 13 | if (ans == 0 || points[i][0] > rep) { 14 | ans++; 15 | rep = points[i][1]; 16 | } 17 | } 18 | return ans; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /LeetCode Problems/401 to 500/452. Minimum Number of Arrows to Burst Balloons/452. Minimum Number of Arrows to Burst Balloons.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findMinArrowShots(int[][] points) { 3 | Arrays.sort(points, (int[] a, int[] b) -> Integer.compare(a[1], b[1])); 4 | int ans = 0, res = 0; 5 | for (int i = 0; i < points.length; i++) { 6 | if (ans == 0 || points[i][0] > res) { 7 | ans++; 8 | res = points[i][1]; 9 | } 10 | } 11 | return ans; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /LeetCode Problems/401 to 500/485. Max Consecutive Ones/485. Max Consecutive Ones.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findMaxConsecutiveOnes(vector &nums) { 4 | int cnt = 0, ans = 0; 5 | for (int i = 0; i < nums.size(); i++) { 6 | if (nums[i] == 0) { 7 | ans = max(cnt, ans); 8 | cnt = 0; 9 | } else { 10 | cnt++; 11 | } 12 | } 13 | ans = max(ans, cnt); 14 | return ans; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /LeetCode Problems/501 to 600/509. Fibonacci Number/509. Fibonacci Number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int fib(int n) { 4 | if (n == 0) 5 | return 0; 6 | int a = 1, b = 1, sum = 1; 7 | for (int i = 2; i < n; i++) { 8 | sum = a + b; 9 | b = a; 10 | a = sum; 11 | } 12 | return sum; 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode Problems/501 to 600/518. Coin Change II/518. Coin Change II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int change(int amount, vector &coins) { 4 | int n = coins.size(); 5 | vector> dp(n + 1, vector(amount + 1)); 6 | dp[0][0] = 1; 7 | for (int i = 1; i <= n; i++) { 8 | for (int j = 0; j <= amount; j++) { 9 | if (coins[i - 1] > j) 10 | dp[i][j] = dp[i - 1][j]; 11 | else 12 | dp[i][j] = dp[i - 1][j] + dp[i][j - coins[i - 1]]; 13 | } 14 | } 15 | return dp[n][amount]; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /LeetCode Problems/501 to 600/518. Coin Change II/518. Coin Change II.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int change(int amount, int[] coins) { 3 | int n = coins.length; 4 | int[][] dp = new int[n + 1][amount + 1]; 5 | dp[0][0] = 1; 6 | for (int i = 1; i <= n; i++) { 7 | for (int j = 0; j <= amount; j++) { 8 | if (coins[i - 1] > j) 9 | dp[i][j] = dp[i - 1][j]; 10 | else 11 | dp[i][j] = dp[i - 1][j] + dp[i][j - coins[i - 1]]; 12 | } 13 | } 14 | return dp[n][amount]; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode Problems/501 to 600/520. Detect Capital/520. Detect Capital.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool detectCapitalUse(string word) { 4 | int cnt = 0; 5 | for (int i = 0; i < word.size(); i++) 6 | if (word[i] >= 'A' && word[i] <= 'Z') 7 | cnt++; 8 | // (cnt == word.size()) -> mean's all letters in this word are uppercase. 9 | // (cnt == 0) -> mean's all letters in this word are lowercase. 10 | // (cnt == 1 && (word[0] >= 'A' && word[0] <= 'Z') -> mean's only first letter in this word are uppercase. 11 | if (cnt == word.size() || cnt == 0 || (cnt == 1 && (word[0] >= 'A' && word[0] <= 'Z'))) 12 | return true; 13 | return false; 14 | } 15 | }; -------------------------------------------------------------------------------- /LeetCode Problems/501 to 600/520. Detect Capital/520. Detect Capital.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean detectCapitalUse(String word) { 3 | int cnt = 0; 4 | for (int i = 0; i < word.length(); i++) 5 | if (word.charAt(i) >= 'A' && word.charAt(i) <= 'Z') cnt++; 6 | // (cnt == word.size()) -> mean's all letters in this word are uppercase. 7 | // (cnt == 0) -> mean's all letters in this word are lowercase. 8 | // (cnt == 1 && (word[0] >= 'A' && word[0] <= 'Z') -> mean's only first letter in this word are uppercase. 9 | if (cnt == word.length() || cnt == 0 || (cnt == 1 && (word.charAt(0) >= 'A' && word.charAt(0) <= 'Z'))) return true; 10 | return false; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /LeetCode Problems/501 to 600/520. Detect Capital/520. Detect Capital.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def detectCapitalUse(self, word: str) -> bool: 3 | return word.isupper() or word.islower() or word.istitle() -------------------------------------------------------------------------------- /LeetCode Problems/501 to 600/535. Encode and Decode TinyURL/535. Encode and Decode TinyURL.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | string url; 3 | 4 | public: 5 | // Encodes a URL to a shortened URL. 6 | string encode(string longUrl) { 7 | url = longUrl; 8 | return url; 9 | } 10 | 11 | // Decodes a shortened URL to its original URL. 12 | string decode(string shortUrl) { 13 | return url; 14 | } 15 | }; 16 | 17 | // Your Solution object will be instantiated and called as such: 18 | // Solution solution; 19 | // solution.decode(solution.encode(url)); -------------------------------------------------------------------------------- /LeetCode Problems/501 to 600/540. Single Element in a Sorted Array/540. Single Element in a Sorted Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int singleNonDuplicate(vector &nums) { 4 | int l = 0, r = nums.size() - 1; 5 | while (l < r) { 6 | int m = (l + r) / 2; 7 | if (m % 2 == 1) 8 | m--; 9 | if (nums[m] != nums[m + 1]) 10 | r = m; 11 | else 12 | l = m + 2; 13 | } 14 | return nums[l]; 15 | } 16 | }; -------------------------------------------------------------------------------- /LeetCode Problems/501 to 600/540. Single Element in a Sorted Array/540. Single Element in a Sorted Array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int singleNonDuplicate(int[] nums) { 3 | int l = 0, r = nums.length - 1; 4 | while (l < r) { 5 | int m = (l + r) / 2; 6 | if (m % 2 == 1) m--; 7 | if (nums[m] != nums[m + 1]) 8 | r = m; 9 | else 10 | l = m + 2; 11 | } 12 | return nums[l]; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /LeetCode Problems/501 to 600/540. Single Element in a Sorted Array/README.md: -------------------------------------------------------------------------------- 1 | # 540. Single Element in a Sorted Array 2 | 3 | ### [Original Question Link](https://leetcode.com/problems/single-element-in-a-sorted-array/) 4 | 5 | You are given a sorted array consisting of only integers where every element appears exactly twice, except for one element which appears exactly once. 6 | 7 | Return the single element that appears only once. 8 | 9 | Your solution must run in ```O(log n)``` time and ```O(1)``` space. 10 | 11 | 12 | 13 | #### Example 1: 14 | ``` 15 | Input: nums = [1,1,2,3,3,4,4,8,8] 16 | Output: 2 17 | ``` 18 | 19 | #### Example 2: 20 | ``` 21 | Input: nums = [3,3,7,7,10,11,11] 22 | Output: 10 23 | ``` 24 | 25 | #### Constraints: 26 | * ```1 <= nums.length <= 10^5``` 27 | * ```0 <= nums[i] <= 10^5``` -------------------------------------------------------------------------------- /LeetCode Problems/501 to 600/557. Reverse Words in a String III/557. Reverse Words in a String III.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string reverseWords(string s) { 4 | string res = "", word = ""; 5 | s += ' '; 6 | for (int i = 0; i < s.size(); i++) { 7 | if (s[i] == ' ') { 8 | reverse(word.begin(), word.end()); 9 | res += word; 10 | if (i != s.size() - 1) res += ' '; 11 | word = ""; 12 | } else 13 | word += s[i]; 14 | } 15 | return res; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /LeetCode Problems/501 to 600/557. Reverse Words in a String III/557. Reverse Words in a String III.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | private void reverse(char[] s, int l, int r) { 3 | while (l < r) { 4 | char temp = s[l]; 5 | s[l++] = s[r]; 6 | s[r--] = temp; 7 | } 8 | } 9 | public String reverseWords(String s) { 10 | char[] res = s.toCharArray(); 11 | for (int i = 0, l = 0; i < res.length; i++) { 12 | if (res[i] == ' ') { 13 | reverse(res, l, i - 1); 14 | l = i + 1; 15 | } else if (i == res.length - 1) { 16 | reverse(res, l, i); 17 | } 18 | } 19 | return new String(res); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /LeetCode Problems/501 to 600/572. Subtree of Another Tree/572. Subtree of Another Tree.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * int val; 5 | * TreeNode left; 6 | * TreeNode right; 7 | * TreeNode() {} 8 | * TreeNode(int val) { this.val = val; } 9 | * TreeNode(int val, TreeNode left, TreeNode right) { 10 | * this.val = val; 11 | * this.left = left; 12 | * this.right = right; 13 | * } 14 | * } 15 | */ 16 | class Solution { 17 | public boolean isSubtree(TreeNode root, TreeNode subRoot) { 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /LeetCode Problems/501 to 600/575. Distribute Candies/575. Distribute Candies.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int distributeCandies(vector &candyType) { 4 | set st; 5 | for (auto it: candyType) 6 | st.insert(it); 7 | int n = candyType.size(); 8 | candyType.clear(); 9 | if (n / 2 > st.size()) 10 | return st.size(); 11 | else 12 | return n / 2; 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode Problems/601 to 700/611. Valid Triangle Number/611. Valid Triangle Number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int triangleNumber(vector &nums) { 4 | sort(nums.begin(), nums.end()); 5 | int cnt = 0, n = nums.size(); 6 | for (int i = 0; i < n; i++) { 7 | if (nums[i] == 0) 8 | continue; 9 | int k = i + 2; 10 | for (int j = i + 1; j < n - 1; j++) { 11 | int sum = nums[i] + nums[j]; 12 | while (k < n && sum > nums[k]) 13 | k++; 14 | cnt += k - j - 1; 15 | } 16 | } 17 | return cnt; 18 | } 19 | }; -------------------------------------------------------------------------------- /LeetCode Problems/601 to 700/645. Set Mismatch/645. Set Mismatch.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector findErrorNums(vector &nums) { 4 | int n = nums.size(); 5 | vector vct(n + 1, 0); 6 | for (int i = 1; i <= n; i++) { 7 | vct[nums[i - 1]]++; 8 | } 9 | int missing_num, duplicate_num; 10 | for (int i = 1; i <= n; i++) { 11 | if (vct[i] == 0) { 12 | missing_num = i; 13 | } 14 | if (vct[i] > 1) { 15 | duplicate_num = i; 16 | } 17 | } 18 | return {duplicate_num, missing_num}; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /LeetCode Problems/601 to 700/647. Palindromic Substrings/647. Palindromic Substrings.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countSubstrings(string s) { 4 | } 5 | }; 6 | -------------------------------------------------------------------------------- /LeetCode Problems/601 to 700/661. Image Smoother/661. Image Smoother.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> imageSmoother(vector> &img) { 4 | int n = img.size(), m = img[0].size(); 5 | vector> res(n, vector(m, 0)); 6 | for (int i = 0; i < n; i++) { 7 | for (int j = 0; j < m; j++) { 8 | int sum = 0, cnt = 0; 9 | for (int k = max(0, i - 1); k < min(n, i + 2); k++) { 10 | for (int l = max(0, j - 1); l < min(m, j + 2); l++) { 11 | sum += img[k][l]; 12 | cnt++; 13 | } 14 | } 15 | res[i][j] = sum / cnt; 16 | } 17 | } 18 | return res; 19 | } 20 | }; -------------------------------------------------------------------------------- /LeetCode Problems/601 to 700/667. Beautiful Arrangement II/667. Beautiful Arrangement II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector constructArray(int n, int k) { 4 | vector ans(n); 5 | for (int i = 0, a = 1, z = k + 1; i <= k; i++) 6 | ans[i] = i % 2 ? z-- : a++; 7 | for (int i = k + 1; i < n; i++) 8 | ans[i] = i + 1; 9 | return ans; 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /LeetCode Problems/601 to 700/690. Employee Importance/690. Employee Importance.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int getImportance(List employees, int id) { 3 | HashMap map = new HashMap<>(); 4 | for (Employee emp : employees) map.put(emp.id, emp); 5 | if (!map.containsKey(id)) return 0; 6 | Queue q = new LinkedList<>(); 7 | q.add(map.get(id)); 8 | int res = 0; 9 | while (!q.isEmpty()) { 10 | Employee emp = q.poll(); 11 | res += emp.importance; 12 | for (Integer empId : emp.subordinates) q.add(map.get(empId)); 13 | } 14 | return res; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode Problems/601 to 700/692. Top K Frequent Words/692. Top K Frequent Words.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector topKFrequent(vector &words, int k) { 4 | map mp; 5 | for (auto ele: words) { 6 | mp[ele]++; 7 | } 8 | vector> frequent; 9 | for (auto it: mp) { 10 | frequent.push_back({it.first, it.second}); 11 | } 12 | sort(frequent.begin(), frequent.end(), [](auto a, auto b) { 13 | if (a.second == b.second) 14 | return a.first < b.first; 15 | return a.second > b.second; 16 | }); 17 | vector ans; 18 | for (int i = 0; i < k; i++) 19 | ans.push_back(frequent[i].first); 20 | return ans; 21 | } 22 | }; -------------------------------------------------------------------------------- /LeetCode Problems/601 to 700/696. Count Binary Substrings/696. Count Binary Substrings.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countBinarySubstrings(string s) { 4 | int curr = 1, prev = 0, ans = 0; 5 | for (int i = 1; i < s.length(); i++) 6 | if (s[i] == s[i - 1]) curr++; 7 | else 8 | ans += min(curr, prev), prev = curr, curr = 1; 9 | return ans + min(curr, prev); 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /LeetCode Problems/701 to 800/704. Binary Search/704. Binary Search.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int search(vector &nums, int target) { 4 | int f = -1, l = nums.size(); 5 | while (l > f + 1) { 6 | int m = (l + f) / 2; 7 | if (nums[m] == target) 8 | return m; 9 | else if (nums[m] < target) 10 | f = m; 11 | else 12 | l = m; 13 | } 14 | return -1; 15 | } 16 | }; -------------------------------------------------------------------------------- /LeetCode Problems/701 to 800/704. Binary Search/704. BinarySearch.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public static int search(int[] arr, int target) { 3 | return findRequiredElement(arr, target, 0, arr.length - 1); 4 | } 5 | 6 | static int findRequiredElement(int[] arr, int target, int start, int end) { 7 | if (start <= end) { 8 | int mid = start + (end - start) / 2; 9 | if (target == arr[mid]) { 10 | return mid; 11 | } else if (target < arr[mid]) { 12 | return findRequiredElement(arr, target, start, mid - 1); 13 | } else { 14 | return findRequiredElement(arr, target, mid + 1, end); 15 | } 16 | } 17 | return -1; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /LeetCode Problems/701 to 800/729. My Calendar I/729. My Calendar I.cpp: -------------------------------------------------------------------------------- 1 | class MyCalendar { 2 | vector> dic; 3 | 4 | public: 5 | bool book(int start, int end) { 6 | for (auto it: dic) { 7 | if (max(it.first, start) < min(it.second, end)) 8 | return false; 9 | } 10 | dic.push_back({start, end}); 11 | return true; 12 | } 13 | }; 14 | 15 | /** 16 | * Your MyCalendar object will be instantiated and called as such: 17 | * MyCalendar* obj = new MyCalendar(); 18 | * bool param_1 = obj->book(start,end); 19 | */ -------------------------------------------------------------------------------- /LeetCode Problems/701 to 800/744. Find Smallest Letter Greater Than Target/744. Find Smallest Letter Greater Than Target.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | char nextGreatestLetter(vector &letters, char target) { 4 | int pos = (upper_bound(letters.begin(), letters.end(), target)) - letters.begin(); 5 | if (pos == letters.size()) 6 | return letters[0]; 7 | return letters[pos]; 8 | } 9 | }; -------------------------------------------------------------------------------- /LeetCode Problems/701 to 800/746. Min Cost Climbing Stairs/746. Min Cost Climbing Stairs.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minCostClimbingStairs(vector &cost) { 4 | vector dp(cost.size()); 5 | for (int i = 0; i < cost.size(); i++) { 6 | if (i < 2) { 7 | dp[i] = cost[i]; 8 | } else { 9 | dp[i] = cost[i] + min(dp[i - 1], dp[i - 2]); 10 | } 11 | } 12 | return min(dp[cost.size() - 1], dp[cost.size() - 2]); 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode Problems/701 to 800/763. Partition Labels/763. Partition Labels.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector partitionLabels(string s) { 4 | int map[26]; 5 | for (int i = 0; i < s.size(); i++) { 6 | map[s[i] - 'a'] = i + 1; 7 | } 8 | int size = 0, r = 0; 9 | vector arr; 10 | for (int l = 0; l < s.size(); l++) { 11 | if (l == r && l != 0) { 12 | arr.push_back(size); 13 | size = 0; 14 | } 15 | if (l <= r) { 16 | r = max(r, map[s[l] - 'a']); 17 | size++; 18 | } 19 | } 20 | arr.push_back(size); 21 | return arr; 22 | } 23 | }; -------------------------------------------------------------------------------- /LeetCode Problems/701 to 800/763. Partition Labels/763. Partition Labels.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List partitionLabels(String s) { 3 | int[] map = new int[27]; 4 | for (int i = 0; i < s.length(); i++) { 5 | map[s.charAt(i) - 'a'] = i + 1; 6 | } 7 | int size = 0, r = 0; 8 | List arr = new ArrayList<>(); 9 | for (int l = 0; l < s.length(); l++) { 10 | if (l == r && l != 0) { 11 | arr.add(size); 12 | size = 0; 13 | } 14 | if (l <= r) { 15 | r = Math.max(r, map[s.charAt(l) - 'a']); 16 | size++; 17 | } 18 | } 19 | arr.add(size); 20 | return arr; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /LeetCode Problems/701 to 800/786. K-th Smallest Prime Fraction/786. K-th Smallest Prime Fraction.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector kthSmallestPrimeFraction(vector &arr, int k) { 4 | vector>> res; 5 | for (int i = 0; i < arr.size(); i++) { 6 | for (int j = i + 1; j < arr.size(); j++) { 7 | res.push_back({arr[i] / (float) arr[j], {arr[i], arr[j]}}); 8 | } 9 | } 10 | sort(res.begin(), res.end()); 11 | return {res[k - 1].second.first, res[k - 1].second.second}; 12 | } 13 | }; -------------------------------------------------------------------------------- /LeetCode Problems/801 to 900/804. Unique Morse Code Words/804. Unique Morse Code Words.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int uniqueMorseRepresentations(vector &words) { 4 | vector key{".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."}; 5 | map mp; 6 | for (int i = 0; i < words.size(); i++) { 7 | string s = words[i], generate_str = ""; 8 | for (int j = 0; j < s.size(); j++) { 9 | generate_str += key[s[j] - 'a']; 10 | } 11 | mp[generate_str]++; 12 | } 13 | return mp.size(); 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /LeetCode Problems/801 to 900/848. Shifting Letters/848. Shifting Letters.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string shiftingLetters(string s, vector &shifts) { 4 | for (int i = shifts.size() - 2; i >= 0; i--) { 5 | shifts[i] = (shifts[i] % 26 + shifts[i + 1] % 26) % 26; 6 | } 7 | for (int i = 0; i < s.size(); i++) { 8 | int int_val = (int) s[i] + (shifts[i] % 26); 9 | if (int_val > 122) { 10 | int_val -= 26; 11 | } 12 | s[i] = (char) int_val; 13 | } 14 | return s; 15 | } 16 | }; -------------------------------------------------------------------------------- /LeetCode Problems/801 to 900/867. Transpose Matrix/867. Transpose Matrix.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> transpose(vector> &matrix) { 4 | int n = matrix.size(); 5 | vector> transpos(matrix[0].size(), vector(n)); 6 | for (int i = 0; i < matrix[0].size(); i++) { 7 | for (int j = 0; j < n; j++) { 8 | transpos[i][j] = matrix[j][i]; 9 | } 10 | } 11 | return transpos; 12 | } 13 | }; -------------------------------------------------------------------------------- /LeetCode Problems/801 to 900/867. Transpose Matrix/867. Transpose Matrix.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[][] transpose(int[][] matrix) { 3 | int[][] arr = new int[matrix[0].length][matrix.length]; 4 | for (int i = 0; i < matrix[0].length; i++) { 5 | for (int j = 0; j < matrix.length; j++) { 6 | arr[i][j] = matrix[j][i]; 7 | } 8 | } 9 | return arr; 10 | } 11 | } -------------------------------------------------------------------------------- /LeetCode Problems/801 to 900/876. Middle of the Linked List/876. Middle of the Linked List.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | ListNode *middleNode(ListNode *head) { 4 | ListNode *middle = head; 5 | int i = 0; 6 | while (head != nullptr) { 7 | i++; 8 | if (i % 2 == 0) { 9 | middle = middle->next; 10 | i = 0; 11 | } 12 | head = head->next; 13 | } 14 | return middle; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /LeetCode Problems/801 to 900/876. Middle of the Linked List/876. Middle of the Linked List.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * public class ListNode { 4 | * int val; 5 | * ListNode next; 6 | * ListNode() {} 7 | * ListNode(int val) { this.val = val; } 8 | * ListNode(int val, ListNode next) { this.val = val; this.next = next; } 9 | * } 10 | */ 11 | class Solution { 12 | public ListNode middleNode(ListNode head) { 13 | int cnt = 1; 14 | ListNode curr = head, prev = head; 15 | while (curr != null) { 16 | if (cnt == 2) { 17 | prev = prev.next; 18 | cnt = 0; 19 | } 20 | curr = curr.next; 21 | cnt++; 22 | } 23 | return prev; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /LeetCode Problems/801 to 900/896. Monotonic Array/896. Monotonic Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isMonotonic(vector &nums) { 4 | bool check = true; 5 | for (int i = 1; i < nums.size(); i++) { 6 | if (nums[i - 1] >= nums[i]) 7 | check = true; 8 | else { 9 | check = false; 10 | break; 11 | } 12 | } 13 | if (check == true) return check; 14 | for (int i = 1; i < nums.size(); i++) { 15 | if (nums[i - 1] <= nums[i]) 16 | check = true; 17 | else { 18 | check = false; 19 | break; 20 | } 21 | } 22 | return check; 23 | } 24 | }; -------------------------------------------------------------------------------- /LeetCode Problems/801 to 900/896. Monotonic Array/896. Monotonic Array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isMonotonic(int[] nums) { 3 | boolean check = true; 4 | for (int i = 1; i < nums.length; i++) { 5 | if (nums[i - 1] >= nums[i]) 6 | check = true; 7 | else { 8 | check = false; 9 | break; 10 | } 11 | } 12 | if (check == true) return check; 13 | for (int i = 1; i < nums.length; i++) { 14 | if (nums[i - 1] <= nums[i]) 15 | check = true; 16 | else { 17 | check = false; 18 | break; 19 | } 20 | } 21 | return check; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /LeetCode Problems/801 to 900/899. Orderly Queue/899. Orderly Queue.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string orderlyQueue(string s, int k) { 4 | if (k >= 2) { 5 | sort(s.begin(), s.end()); 6 | return s; 7 | } else { 8 | vector arr; 9 | arr.push_back(s); 10 | for (int i = 0; i < s.size(); i++) { 11 | s += s[0]; 12 | s.erase(0, 1); 13 | arr.push_back(s); 14 | } 15 | sort(arr.begin(), arr.end()); 16 | return arr[0]; 17 | } 18 | } 19 | }; -------------------------------------------------------------------------------- /LeetCode Problems/901 to 1000/901. Online Stock Span/901. Online Stock Span.cpp: -------------------------------------------------------------------------------- 1 | class StockSpanner { 2 | stack> arr; 3 | 4 | public: 5 | int next(int price) { 6 | int cnt = 1; 7 | while (!arr.empty() && arr.top().first <= price) { 8 | cnt += arr.top().second; 9 | arr.pop(); 10 | } 11 | arr.push({price, cnt}); 12 | return cnt; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /LeetCode Problems/901 to 1000/901. Online Stock Span/901. Online Stock Span.java: -------------------------------------------------------------------------------- 1 | class StockSpanner { 2 | private ArrayList arr = new ArrayList<>(); 3 | public int next(int price) { 4 | arr.add(price); 5 | int cnt = 0; 6 | for (int i = arr.size() - 1; i >= 0; i--) { 7 | if (arr.get(i) > price) break; 8 | cnt++; 9 | } 10 | return cnt; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /LeetCode Problems/901 to 1000/901. Online Stock Span/901. Online Stock Span.py: -------------------------------------------------------------------------------- 1 | class StockSpanner: 2 | def __init__(self): 3 | self.list = [] 4 | 5 | def next(self, price): 6 | cnt = 1 7 | while self.list and self.list[-1][0] <= price: 8 | cnt += self.list[-1][1] 9 | self.list.pop() 10 | self.list.append((price, cnt)) 11 | return cnt 12 | 13 | 14 | # Your StockSpanner object will be instantiated and called as such: 15 | # obj = StockSpanner() 16 | # param_1 = obj.next(price) -------------------------------------------------------------------------------- /LeetCode Problems/901 to 1000/915. Partition Array into Disjoint Intervals/915. Partition Array into Disjoint Intervals.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int partitionDisjoint(int[] A) { 3 | int N = A.length; 4 | int[] maxleft = new int[N]; 5 | int[] minright = new int[N]; 6 | 7 | int m = A[0]; 8 | for (int i = 0; i < N; ++i) { 9 | m = Math.max(m, A[i]); 10 | maxleft[i] = m; 11 | } 12 | 13 | m = A[N - 1]; 14 | for (int i = N - 1; i >= 0; --i) { 15 | m = Math.min(m, A[i]); 16 | minright[i] = m; 17 | } 18 | 19 | for (int i = 1; i < N; ++i) 20 | if (maxleft[i - 1] <= minright[i]) return i; 21 | 22 | throw null; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode Problems/901 to 1000/917. Reverse Only Letters/917. Reverse Only Letters.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string reverseOnlyLetters(string s) { 4 | string rev_s = ""; 5 | stack que; 6 | for (int i = 0; i < s.size(); i++) { 7 | if ((s[i] >= 'A' && s[i] <= 'Z') || (s[i] >= 'a' && s[i] <= 'z')) { 8 | que.push(s[i]); 9 | } 10 | } 11 | for (int i = 0, j = 0; i < s.size(); i++) { 12 | if ((s[i] >= 'A' && s[i] <= 'Z') || (s[i] >= 'a' && s[i] <= 'z')) { 13 | rev_s += que.top(); 14 | que.pop(); 15 | } else { 16 | rev_s += s[i]; 17 | } 18 | } 19 | return rev_s; 20 | } 21 | }; -------------------------------------------------------------------------------- /LeetCode Problems/901 to 1000/921. Minimum Add to Make Parentheses Valid/921. Minimum Add to Make Parentheses Valid.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minAddToMakeValid(string s) { 4 | stack parentheses; 5 | int cnt = 0; 6 | for (char ch: s) { 7 | if (ch == '(') 8 | parentheses.push(ch); 9 | else { 10 | if (!parentheses.empty()) 11 | parentheses.pop(); 12 | else 13 | cnt++; 14 | } 15 | } 16 | return cnt + parentheses.size(); 17 | } 18 | }; -------------------------------------------------------------------------------- /LeetCode Problems/901 to 1000/921. Minimum Add to Make Parentheses Valid/921. Minimum Add to Make Parentheses Valid.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minAddToMakeValid(String s) { 3 | int cnt = 0; 4 | String str = ""; 5 | for (int i = 0; i < s.length(); i++) { 6 | if (s.charAt(i) == '(') 7 | str += s.charAt(i); 8 | else { 9 | if (s.charAt(i) == ')' && str.length() > 0) { 10 | StringBuilder sb = new StringBuilder(str); 11 | sb.deleteCharAt(0); 12 | str = sb.toString(); 13 | } else 14 | cnt++; 15 | } 16 | } 17 | return cnt + str.length(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /LeetCode Problems/901 to 1000/922. Sort Array By Parity II/922. Sort Array By Parity II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector sortArrayByParityII(vector &nums) { 4 | vector odd, even; 5 | for (int i = 0; i < nums.size(); i++) { 6 | if (nums[i] % 2 != 0) 7 | odd.push_back(nums[i]); 8 | else 9 | even.push_back(nums[i]); 10 | } 11 | for (int i = 0, j = 0, k = 0; i < nums.size(); i++) { 12 | if ((i + 1) % 2 == 0) 13 | nums[i] = odd[j++]; 14 | else 15 | nums[i] = even[k++]; 16 | } 17 | return nums; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /LeetCode Problems/901 to 1000/931. Minimum Falling Path Sum/931. Minimum Falling Path Sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minFallingPathSum(vector> &A) { 4 | for (auto i = 1; i < A.size(); ++i) 5 | for (auto j = 0; j < A.size(); ++j) 6 | A[i][j] += min({A[i - 1][j], A[i - 1][max(0, j - 1)], A[i - 1][min((int) A.size() - 1, j + 1)]}); 7 | return *min_element(begin(A[A.size() - 1]), end(A[A.size() - 1])); 8 | } 9 | }; -------------------------------------------------------------------------------- /LeetCode Problems/901 to 1000/941. Valid Mountain Array/941. Valid Mountain Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool validMountainArray(vector &arr) { 4 | bool check = false; 5 | int i = 0, cntA = 0, cntB = 0; 6 | for (i = 0; i < arr.size() - 1; i++) { 7 | if (arr[i] < arr[i + 1]) 8 | cntA++; 9 | else 10 | break; 11 | } 12 | for (; i < arr.size() - 1; i++) { 13 | if (arr[i] > arr[i + 1]) 14 | cntB++; 15 | else 16 | break; 17 | } 18 | if (cntA != 0 && cntB != 0 && (cntA + cntB) == arr.size() - 1) 19 | return true; 20 | else 21 | return false; 22 | } 23 | }; -------------------------------------------------------------------------------- /LeetCode Problems/901 to 1000/944. Delete Columns to Make Sorted/944. Delete Columns to Make Sorted.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minDeletionSize(vector &strs) { 4 | int cnt = 0, n = strs[0].size(), m = strs.size(); 5 | for (int i = 0; i < n; i++) { 6 | for (int j = 0; j < m - 1; j++) { 7 | if (strs[j][i] > strs[j + 1][i]) { 8 | cnt++; 9 | break; 10 | } 11 | } 12 | } 13 | return cnt; 14 | } 15 | }; -------------------------------------------------------------------------------- /LeetCode Problems/901 to 1000/944. Delete Columns to Make Sorted/944. Delete Columns to Make Sorted.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minDeletionSize(String[] strs) { 3 | int cnt = 0, n = strs[0].length(), m = strs.length; 4 | for (int i = 0; i < n; i++) { 5 | for (int j = 0; j < m - 1; j++) { 6 | if (strs[j].charAt(i) > strs[j + 1].charAt(i)) { 7 | cnt++; 8 | break; 9 | } 10 | } 11 | } 12 | return cnt; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /LeetCode Problems/901 to 1000/947. Most Stones Removed with Same Row or Column/947. Most Stones Removed with Same Row or Column.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | unordered_map f; 3 | int res = 0; 4 | 5 | public: 6 | int removeStones(vector> &stones) { 7 | for (int i = 0; i < stones.size(); i++) 8 | Union(stones[i][0], ~stones[i][1]); 9 | return stones.size() - res; 10 | } 11 | int find(int x) { 12 | if (!f.count(x)) 13 | f[x] = x, res++; 14 | if (x != f[x]) 15 | f[x] = find(f[x]); 16 | return f[x]; 17 | } 18 | void Union(int x, int y) { 19 | x = find(x), y = find(y); 20 | if (x != y) 21 | f[x] = y, res--; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /LeetCode Problems/901 to 1000/947. Most Stones Removed with Same Row or Column/947. Most Stones Removed with Same Row or Column.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def removeStones(self, points): 3 | f = {} 4 | def find(x): 5 | if x != f.setdefault(x, x): 6 | f[x] = find(f[x]) 7 | return f[x] 8 | for i, j in points: 9 | f[find(i)] = find(~j) 10 | return len(points) - len({find(x) for x in f}) 11 | -------------------------------------------------------------------------------- /LeetCode Problems/901 to 1000/974. Subarray Sums Divisible by K/974. Subarray Sums Divisible by K.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int subarraysDivByK(vector &nums, int k) { 4 | int sum = 0, cnt = 0, rem; 5 | vector prefix(k, 0); 6 | prefix[0]++; 7 | for (int i = 0; i < nums.size(); i++) { 8 | sum += nums[i]; 9 | rem = (sum % k + k) % k; 10 | cnt += prefix[rem]; 11 | prefix[rem]++; 12 | } 13 | return cnt; 14 | } 15 | }; -------------------------------------------------------------------------------- /LeetCode Problems/901 to 1000/974. Subarray Sums Divisible by K/974. Subarray Sums Divisible by K.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int subarraysDivByK(int[] nums, int k) { 3 | int sum = 0, cnt = 0, rem; 4 | int[] prefix = new int[k]; 5 | prefix[0]++; 6 | for (int i = 0; i < nums.length; i++) { 7 | sum += nums[i]; 8 | rem = (sum % k + k) % k; 9 | cnt += prefix[rem]; 10 | prefix[rem]++; 11 | } 12 | return cnt; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /LeetCode Problems/901 to 1000/974. Subarray Sums Divisible by K/974. Subarray Sums Divisible by K.kt: -------------------------------------------------------------------------------- 1 | class Solution { 2 | fun subarraysDivByK(nums: IntArray, k: Int): Int { 3 | var sum:Int = 0; var cnt:Int = 0; var rem:Int = 0; 4 | val prefix = IntArray(k); 5 | prefix[0]++; 6 | for (items in nums) { 7 | sum += items; 8 | rem = (sum % k + k)% k; 9 | cnt += prefix[rem]; 10 | prefix[rem]++; 11 | } 12 | return cnt; 13 | } 14 | } -------------------------------------------------------------------------------- /LeetCode Problems/901 to 1000/977. Squares of a Sorted Array/977. Squares of a Sorted Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector sortedSquares(vector &A) { 4 | for (int i = 0; i < A.size(); i++) { 5 | A[i] *= A[i]; 6 | } 7 | sort(A.begin(), A.end()); 8 | return A; 9 | } 10 | }; 11 | --------------------------------------------------------------------------------