├── 1005. Maximize Sum Of Array After K Negations.cpp ├── 1007. Minimum Domino Rotations For Equal Row.cpp ├── 1010. Pairs of Songs With Total Durations Divisible by 60.cpp ├── 1014. Best Sightseeing Pair.cpp ├── 1018. Binary Prefix Divisible By 5.cpp ├── 1021. Remove Outermost Parentheses.cpp ├── 1028. Recover a Tree From Preorder Traversal.cpp ├── 1061. Lexicographically Smallest Equivalent String.cpp ├── 1072. Flip Columns For Maximum Number of Equal Rows.cpp ├── 1079. Letter Tile Possibilities.cpp ├── 1091. Shortest Path in Binary Matrix.cpp ├── 1106. Parsing A Boolean Expression.cpp ├── 1123. Lowest Common Ancestor of Deepest Leaves.cpp ├── 1128. Number of Equivalent Domino Pairs.cpp ├── 1161. Maximum Level Sum of a Binary Tree (BSF).cpp ├── 1233. Remove Sub-Folders from the Filesystem.cpp ├── 1261. Find Elements in a Contaminated Binary Tree.cpp ├── 1267. Count Servers that Communicate.cpp ├── 128. Longest Consecutive Sequence.cpp ├── 1282. Group the People Given the Group Size They Belong To.cpp ├── 1286. Iterator for Combination.cpp ├── 1295. Find Numbers with Even Number of Digits.cpp ├── 1295. Find Numbers with Even Number of Digits.py ├── 1310. XOR Queries of a Subarray.cpp ├── 1310. XOR Queries of a Subarray.py ├── 1318. Minimum Flips to Make a OR b Equal to c.cpp ├── 1331. Rank Transform of an Array.cpp ├── 1331. Rank Transform of an Array.py ├── 1346. Check If N and Its Double Exist.cpp ├── 1346. Check If N and Its Double Exist.py ├── 135. Candy.cpp ├── 1352. Product of the Last K Numbers.cpp ├── 1358. Number of Substrings Containing All Three Characters.cpp ├── 1367. Linked List in Binary Tree.cpp ├── 1367. Linked List in Binary Tree.py ├── 1368. Minimum Cost to Make at Least One Valid Path in a Grid.cpp ├── 1371. Find the Longest Substring Containing Vowels in Even Counts.cpp ├── 1371. Find the Longest Substring Containing Vowels in Even Counts.py ├── 1387. Sort Integers by The Power Value.cpp ├── 1399. Count Largest Group.cpp ├── 1399. Count Largest Group.py ├── 1400. Construct K Palindrome Strings.cpp ├── 1408. String Matching in an Array.cpp ├── 1413. Minimum Value to Get Positive Step by Step Sum.cpp ├── 1415. The k-th Lexicographical String of All Happy Strings of Length n.cpp ├── 1422. Maximum Score After Splitting a String.cpp ├── 1422. Maximum Score After Splitting a String.py ├── 145. Binary Tree Postorder Traversal.cpp ├── 1462. Course Schedule IV.cpp ├── 1475. Final Prices With a Special Discount in a Shop.cpp ├── 1475. Final Prices With a Special Discount in a Shop.py ├── 1497. Check If Array Pairs Are Divisible by k.cpp ├── 1497. Check If Array Pairs Are Divisible by k.py ├── 1507. Reformat Date.cpp ├── 1513. Number of Substrings With Only 1s.cpp ├── 1514. Path with Maximum Probability.cpp ├── 1514. Path with Maximum Probability.py ├── 1524. Number of Sub-arrays With Odd Sum.cpp ├── 1534. Count Good Triplets.cpp ├── 1534. Count Good Triplets.py ├── 1545. Find Kth Bit in Nth Binary String.cpp ├── 1545. Find Kth Bit in Nth Binary String.py ├── 1550. Three Consecutive Odds.cpp ├── 1550. Three Consecutive Odds.py ├── 1556. Thousand Separator.cpp ├── 1590. Make Sum Divisible by P.cpp ├── 1593. Split a String Into the Max Number of Unique Substrings.cpp ├── 16. 3Sum Closest copy.cpp ├── 16. 3Sum Closest.cpp ├── 1608. Special Array With X Elements Greater Than or Equal X.cpp ├── 1639. Number of Ways to Form a Target String Given a Dictionary.cpp ├── 1641. Count Sorted Vowel Strings.cpp ├── 1652. Defuse the Bomb.cpp ├── 1652. Defuse the Bomb.py ├── 1679. Max Number of K-Sum Pairs.cpp ├── 1679. Max Number of K-Sum Pairs.py ├── 1684. Count the Number of Consistent Strings.cpp ├── 1684. Count the Number of Consistent Strings.py ├── 1688. Count of Matches in Tournament.cpp ├── 1695. Maximum Erasure Value.cpp ├── 1711. Count Good Meals.cpp ├── 1716. Calculate Money in Leetcode Bank.cpp ├── 1718. Construct the Lexicographically Largest Valid Sequence.cpp ├── 1726. Tuple with Same Product.cpp ├── 1726. Tuple with Same Product.py ├── 1742. Maximum Number of Balls in a Box.cpp ├── 1749. Maximum Absolute Sum of Any Subarray.cpp ├── 1752. Check if Array Is Sorted and Rotated.cpp ├── 1752. Check if Array Is Sorted and Rotated.py ├── 1759. Count Number of Homogenous Substrings.cpp ├── 1760. Minimum Limit of Balls in a Bag.cpp ├── 1760. Minimum Limit of Balls in a Bag.py ├── 1765. Map of Highest Peak.cpp ├── 1769. Minimum Number of Operations to Move All Balls to Each Box.cpp ├── 1769. Minimum Number of Operations to Move All Balls to Each Box.js ├── 1769. Minimum Number of Operations to Move All Balls to Each Box.py ├── 1780. Check if Number is a Sum of Powers of Three.cpp ├── 1784. Check if Binary String Has at Most One Segment of Ones.cpp ├── 179. Largest Number.cpp ├── 179. Largest Number.py ├── 1790. Check if One String Swap Can Make Strings Equal.cpp ├── 1792. Maximum Average Pass Ratio.cpp ├── 1800. Maximum Ascending Subarray Sum.cpp ├── 1829. Maximum XOR for Each Query.cpp ├── 1837. Sum of Digits in Base K.cpp ├── 1857. Largest Color Value in a Directed Graph.cpp ├── 1861. Rotating the Box.cpp ├── 1863. Sum of All Subset XOR Totals.cpp ├── 1865. Finding Pairs With a Certain Sum(Optimized).cpp ├── 1865. Finding Pairs With a Certain Sum.cpp ├── 1869. Longer Contiguous Segments of Ones than Zeros.cpp ├── 1894. Find the Student that Will Replace the Chalk.cpp ├── 1894. Find the Student that Will Replace the Chalk.js ├── 1894. Find the Student that Will Replace the Chalk.py ├── 1897. Redistribute Characters to Make All Strings Equal.cpp ├── 190. Reverse Bits.cpp ├── 1905. Count Sub Islands.cpp ├── 1905. Count Sub Islands.py ├── 1910. Remove All Occurrences of a Substring.cpp ├── 1920. Build Array from Permutation.cpp ├── 1920. Build Array from Permutation.py ├── 1922. Count Good Numbers.py ├── 1930. Unique Length-3 Palindromic Subsequences.cpp ├── 1945. Sum of Digits of String After Convert.cpp ├── 1945. Sum of Digits of String After Convert.py ├── 1957. Delete Characters to Make Fancy String.cpp ├── 1957. Delete Characters to Make Fancy String.py ├── 1962. Remove Stones to Minimize the Total.cpp ├── 1963. Minimum Number of Swaps to Make the String Balanced.cpp ├── 1963. Minimum Number of Swaps to Make the String Balanced.py ├── 1975. Maximum Matrix Sum.cpp ├── 1976. Number of Ways to Arrive at Destination.cpp ├── 1980. Find Unique Binary String.cpp ├── 1980. Find Unique Binary String.py ├── 1984. Minimum Difference Between Highest and Lowest of K Scores.cpp ├── 1995. Count Special Quadruplets.cpp ├── 2017. Grid Game.cpp ├── 2022. Convert 1D Array Into 2D Array.cpp ├── 2022. Convert 1D Array Into 2D Array.py ├── 2028. Find Missing Observations.cpp ├── 2028. Find Missing Observations.py ├── 2033. Minimum Operations to Make a Uni-Value Grid.cpp ├── 2043. Simple Bank System.cpp ├── 2044. Count Number of Maximum Bitwise-OR Subsets.cpp ├── 2054. Two Best Non-Overlapping Events.cpp ├── 2064. Minimized Maximum of Products Distributed to Any Store.cpp ├── 2070. Most Beautiful Item for Each Query.cpp ├── 2071. Maximum Number of Tasks You Can Assign.cpp ├── 2091. Removing Minimum and Maximum From Array.cpp ├── 2094. Finding 3-Digit Even Numbers.cpp ├── 2094. Finding 3-Digit Even Numbers.py ├── 2097. Valid Arrangement of Pairs.cpp ├── 2099. Find Subsequence of Length K With the Largest Sum.cpp ├── 2109. Adding Spaces to a String.cpp ├── 2109. Adding Spaces to a String.py ├── 211. Design Add and Search Words Data Structure.cpp ├── 2115. Find All Possible Recipes from Given Supplies.cpp ├── 2116. Check if a Parentheses String Can Be Valid.cpp ├── 2119. A Number After a Double Reversal.cpp ├── 2127. Maximum Employees to Be Invited to a Meeting.cpp ├── 2131. Longest Palindrome by Concatenating Two Letter Words.cpp ├── 2131. Longest Palindrome by Concatenating Two Letter Words.py ├── 2139. Minimum Moves to Reach Target Score.cpp ├── 214. Shortest Palindrome.cpp ├── 214. Shortest Palindrome.py ├── 2140. Solving Questions With Brainpower.cpp ├── 2145. Count the Hidden Sequences.cpp ├── 2161. Partition Array According to Given Pivot.cpp ├── 2161. Partition Array According to Given Pivot.py ├── 2176. Count Equal and Divisible Pairs in an Array.cpp ├── 2176. Count Equal and Divisible Pairs in an Array.py ├── 2177. Find Three Consecutive Integers That Sum to a Given Number.cpp ├── 2177. Find Three Consecutive Integers That Sum to a Given Number.js ├── 2177. Find Three Consecutive Integers That Sum to a Given Number.py ├── 2179. Count Good Triplets in an Array.cpp ├── 2182. Construct String With Repeat Limit using Map.cpp ├── 2185. Counting Words With a Given Prefix.cpp ├── 2185. Counting Words With a Given Prefix.py ├── 2195. Append K Integers With Minimal Sum.cpp ├── 2206. Divide Array Into Equal Pairs.cpp ├── 2207. Maximize Number of Subsequences in a String.cpp ├── 2208. Minimum Operations to Halve Array Sum.cpp ├── 2210. Count Hills and Valleys in an Array.cpp ├── 2220. Minimum Bit Flips to Convert Number.cpp ├── 2220. Minimum Bit Flips to Convert Number.js ├── 2220. Minimum Bit Flips to Convert Number.py ├── 2221. Find Triangular Sum of an Array.cpp ├── 2226. Maximum Candies Allocated to K Children.cpp ├── 2255. Count Prefixes of a Given String.cpp ├── 2257. Count Unguarded Cells in the Grid.cpp ├── 2260. Minimum Consecutive Cards to Pick Up.cpp ├── 2265. Count Nodes Equal to Average of Subtree.cpp ├── 2275. Largest Combination With Bitwise AND Greater Than Zero.cpp ├── 2279. Maximum Bags With Full Capacity of Rocks.cpp ├── 2284. Sender With Largest Word Count.cpp ├── 2290. Minimum Obstacle Removal to Reach Corner.cpp ├── 2302. Count Subarrays With Score Less Than K.cpp ├── 2302. Count Subarrays With Score Less Than K.py ├── 2315. Count Asterisks.cpp ├── 2317. Maximum XOR After Operations .cpp ├── 2326. Spiral Matrix IV.cpp ├── 2337. Move Pieces to Obtain a String.cpp ├── 2338. Count the Number of Ideal Arrays.cpp ├── 2342. Max Sum of a Pair With Equal Sum of Digits.cpp ├── 2342. Max Sum of a Pair With Equal Sum of Digits.py ├── 2349. Design a Number Container System.cpp ├── 2359. Find Closest Node to Given Two Nodes.cpp ├── 2364. Count Number of Bad Pairs.cpp ├── 2375. Construct Smallest Number From DI String.cpp ├── 2379. Minimum Recolors to Get K Consecutive Black Blocks.cpp ├── 2379. Minimum Recolors to Get K Consecutive Black Blocks.js ├── 2379. Minimum Recolors to Get K Consecutive Black Blocks.py ├── 2381. Shifting Letters II.cpp ├── 2401. Longest Nice Subarray.cpp ├── 2414. Length of the Longest Alphabetical Continuous Substring.cpp ├── 2414. Length of the Longest Alphabetical Continuous Substring.js ├── 2414. Length of the Longest Alphabetical Continuous Substring.py ├── 2415. Reverse Odd Levels of Binary Tree.cpp ├── 2415. Reverse Odd Levels of Binary Tree.py ├── 2416. Sum of Prefix Scores of Strings.cpp ├── 2416. Sum of Prefix Scores of Strings.py ├── 2419. Longest Subarray With Maximum Bitwise AND.cpp ├── 2419. Longest Subarray With Maximum Bitwise AND.js ├── 2419. Longest Subarray With Maximum Bitwise AND.py ├── 2425. Bitwise XOR of All Pairings.cpp ├── 2429. Minimize XOR.cpp ├── 2433. Find The Original Array of Prefix Xor.cpp ├── 2433. Find The Original Array of Prefix Xor.py ├── 2434. Using a Robot to Print the Lexicographically Smallest String.cpp ├── 2444. Count Subarrays With Fixed Bounds.cpp ├── 2444. Count Subarrays With Fixed Bounds.py ├── 2452. Words Within Two Edits of Dictionary.cpp ├── 2455. Average Value of Even Numbers That Are Divisible by Three.cpp ├── 2458. Height of Binary Tree After Subtree Removal Queries.cpp ├── 2460. Apply Operations to an Array.cpp ├── 2460. Apply Operations to an Array.py ├── 2463. Minimum Total Distance Traveled.cpp ├── 2466. Count Ways To Build Good Strings.cpp ├── 2467. Most Profitable Path in a Tree.cpp ├── 2471. Minimum Number of Operations to Sort a Binary Tree by Level.cpp ├── 2471. Minimum Number of Operations to Sort a Binary Tree by Level.py ├── 2476. Closest Nodes Queries in a Binary Search Tree.cpp ├── 2481. Minimum Cuts to Divide a Circle.cpp ├── 2490. Circular Sentence.cpp ├── 2491. Divide Players Into Teams of Equal Skill.cpp ├── 2491. Divide Players Into Teams of Equal Skill.py ├── 2503. Maximum Number of Points From Grid Queries.cpp ├── 2512. Reward Top K Students.cpp ├── 2516. Take K of Each Character From Left and Right.cpp ├── 2523. Closest Prime Numbers in Range.cpp ├── 2523. Closest Prime Numbers in Range.py ├── 2529. Maximum Count of Positive Integer and Negative Integer.cpp ├── 2530. Maximal Score After Applying K Operations.cpp ├── 2537. Count the Number of Good Subarrays.cpp ├── 2551. Put Marbles in Bags.cpp ├── 2554. Maximum Number of Integers to Choose From a Range I.cpp ├── 2554. Maximum Number of Integers to Choose From a Range I.py ├── 2558. Take Gifts From the Richest Pile.cpp ├── 2558. Take Gifts From the Richest Pile.py ├── 2559. Count Vowel Strings in Ranges.cpp ├── 2559. Count Vowel Strings in Ranges.py ├── 2560. House Robber IV.cpp ├── 2563. Count the Number of Fair Pairs.cpp ├── 2563. Count the Number of Fair Pairs.py ├── 2568. Minimum Impossible OR.cpp ├── 2570. Merge Two 2D Arrays by Summing Values.cpp ├── 2570. Merge Two 2D Arrays by Summing Values.py ├── 2577. Minimum Time to Visit a Cell In a Grid.cpp ├── 2579. Count Total Number of Colored Cells.cpp ├── 2579. Count Total Number of Colored Cells.py ├── 2583. Kth Largest Sum in a Binary Tree (BFS).cpp ├── 2583. Kth Largest Sum in a Binary Tree (DFS).cpp ├── 2593. Find Score of an Array After Marking All Elements.cpp ├── 2594. Minimum Time to Repair Cars.cpp ├── 2601. Prime Subtraction Operation.cpp ├── 2609. Find the Longest Balanced Substring of a Binary String.cpp ├── 2641. Cousins in Binary Tree II (BSF).cpp ├── 2657. Find the Prefix Common Array of Two Arrays.cpp ├── 2658. Maximum Number of Fish in a Grid.cpp ├── 2661. First Completely Painted Row or Column.cpp ├── 2683. Neighboring Bitwise XOR.cpp ├── 2683. Neighboring Bitwise XOR.py ├── 2684. Maximum Number of Moves in a Grid.cpp ├── 2685. Count the Number of Complete Components.cpp ├── 2696. Minimum String Length After Removing Substrings.cpp ├── 2698. Find the Punishment Number of an Integer.cpp ├── 2707. Extra Characters in a String.cpp ├── 2707. Extra Characters in a String.py ├── 2762. Continuous Subarrays.cpp ├── 2769. Find the Maximum Achievable Number.cpp ├── 2779. Maximum Beauty of an Array After Applying Operation.cpp ├── 2780. Minimum Index of a Valid Split.cpp ├── 2788. Split Strings by Separator.cpp ├── 2799. Count Complete Subarrays in an Array.cpp ├── 2799. Count Complete Subarrays in an Array.py ├── 2807. Insert Greatest Common Divisors in Linked List.cpp ├── 2807. Insert Greatest Common Divisors in Linked List.py ├── 2815. Max Pair Sum in an Array.cpp ├── 2818. Apply Operations to Maximize Score.cpp ├── 2825. Make String a Subsequence Using Cyclic Increments.cpp ├── 2825. Make String a Subsequence Using Cyclic Increments.py ├── 2843. Count Symmetric Integers.cpp ├── 2845. Count of Interesting Subarrays.cpp ├── 2872. Maximum Number of K-Divisible Components.cpp ├── 2873. Maximum Value of an Ordered Triplet I.cpp ├── 2874. Maximum Value of an Ordered Triplet II.cpp ├── 2894. Divisible and Non-divisible Sums Difference.cpp ├── 2894. Divisible and Non-divisible Sums Difference.py ├── 2900. Longest Unequal Adjacent Groups Subsequence I.cpp ├── 2900. Longest Unequal Adjacent Groups Subsequence I.py ├── 2908. Minimum Sum of Mountain Triplets I.cpp ├── 2909. Minimum Sum of Mountain Triplets II.cpp ├── 2913. Subarrays Distinct Element Sum of Squares I.cpp ├── 2914. Minimum Number of Changes to Make Binary String Beautiful.cpp ├── 2914. Minimum Number of Changes to Make Binary String Beautiful.py ├── 2918. Minimum Equal Sum of Two Arrays After Replacing Zeros.cpp ├── 292. Nim Game.cpp ├── 2929. Distribute Candies Among Children II.cpp ├── 2932. Maximum Strong Pair XOR I.cpp ├── 2937. Make Three Strings Equal.cpp ├── 2938. Separate Black and White Balls.cpp ├── 2938. Separate Black and White Balls.py ├── 2940. Find Building Where Alice and Bob Can Meet.cpp ├── 2942. Find Words Containing Character.cpp ├── 2942. Find Words Containing Character.py ├── 2948. Make Lexicographically Smallest Array by Swapping Elements.cpp ├── 2957. Remove Adjacent Almost-Equal Characters.cpp ├── 2960. Count Tested Devices After Test Operations.cpp ├── 2962. Count Subarrays Where Max Element Appears at Least K Times.cpp ├── 2962. Count Subarrays Where Max Element Appears at Least K Times.py ├── 2965. Find Missing and Repeated Values.cpp ├── 2965. Find Missing and Repeated Values.py ├── 2981. Find Longest Special Substring That Occurs Thrice I.cpp ├── 2999. Count the Number of Powerful Integers.cpp ├── 3011. Find if Array Can Be Sorted.cpp ├── 303. Range Sum Query - Immutable.cpp ├── 3038. Maximum Number of Operations With the Same Score I.cpp ├── 3042. Count Prefix and Suffix Pairs I.cpp ├── 3043. Find the Length of the Longest Common Prefix.cpp ├── 3043. Find the Length of the Longest Common Prefix.py ├── 3066. Minimum Operations to Exceed Threshold Value II.cpp ├── 3066. Minimum Operations to Exceed Threshold Value II.py ├── 3068. Find the Maximum Sum of Node Values.cpp ├── 3097. Shortest Subarray With OR at Least K II.cpp ├── 3105. Longest Strictly Increasing or Strictly Decreasing Subarray.cpp ├── 3105. Longest Strictly Increasing or Strictly Decreasing Subarray.py ├── 3107. Minimum Operations to Make Median of Array Equal to K.cpp ├── 3107. Minimum Operations to Make Median of Array Equal to K.js ├── 3107. Minimum Operations to Make Median of Array Equal to K.py ├── 3108. Minimum Cost Walk in Weighted Graph.cpp ├── 3133. Minimum Array End.cpp ├── 3151. Special Array I.cpp ├── 3151. Special Array I.py ├── 3152. Special Array II.cpp ├── 3152. Special Array II.py ├── 3158. Find the XOR of Numbers Which Appear Twice.cpp ├── 3160. Find the Number of Distinct Colors Among the Balls.cpp ├── 3163. String Compression III.cpp ├── 3163. String Compression III.py ├── 3169. Count Days Without Meetings.cpp ├── 3169. Count Days Without Meetings.py ├── 3170. Lexicographically Minimum String After Removing Stars.cpp ├── 3170. Lexicographically Minimum String After Removing Stars.py ├── 3174. Clear Digits.cpp ├── 3174. Clear Digits.py ├── 318. Maximum Product of Word Lengths.cpp ├── 3191. Minimum Operations to Make Binary Array Elements Equal to One I.cpp ├── 3191. Minimum Operations to Make Binary Array Elements Equal to One I.js ├── 3191. Minimum Operations to Make Binary Array Elements Equal to One I.py ├── 3203. Find Minimum Diameter After Merging Two Trees.cpp ├── 3208. Alternating Groups II.cpp ├── 3208. Alternating Groups II.py ├── 3217. Delete Nodes From Linked List Present in Array.cpp ├── 3223. Minimum Length of String After Operations.cpp ├── 3223. Minimum Length of String After Operations.py ├── 3243. Shortest Distance After Road Addition Queries I.cpp ├── 3255. Find the Power of K-Size Subarrays.cpp ├── 3255. Find the Power of K-Size Subarrays.js ├── 3255. Find the Power of K-Size Subarrays.py ├── 3258. Count Substrings That Satisfy K-Constraint I.cpp ├── 3264. Final Array State After K Multiplication Operations I.cpp ├── 3264. Final Array State After K Multiplication Operations I.py ├── 3270. Find the Key of the Numbers.cpp ├── 3270. Find the Key of the Numbers.py ├── 3271. Hash Divided String.cpp ├── 3271. Hash Divided String.py ├── 3272. Find the Count of Good Integers.cpp ├── 3274. Check if Two Chessboard Squares Have the Same Color.cpp ├── 3274. Check if Two Chessboard Squares Have the Same Color.py ├── 3280. Convert Date to Binary.cpp ├── 3282. Reach End of Array With Max Score.cpp ├── 3286. Find a Safe Walk Through a Grid.cpp ├── 3289. The Two Sneaky Numbers of Digitville.cpp ├── 3304. Find the K-th Character in String Game I.cpp ├── 3306. Count of Substrings Containing Every Vowel and K Consonants II.cpp ├── 3309. Maximum Possible Number by Binary Concatenation.cpp ├── 3324. Find the Sequence of Strings Appeared on the Screen.cpp ├── 3330. Find the Original Typed String I.cpp ├── 3335. Total Characters in String After Transformations I.cpp ├── 3335. Total Characters in String After Transformations I.py ├── 3340. Check Balanced String.cpp ├── 3341. Find Minimum Time to Reach Last Room I.cpp ├── 3342. Find Minimum Time to Reach Last Room II.cpp ├── 3343. Count Number of Balanced Permutations.cpp ├── 3345. Smallest Divisible Digit Product I.cpp ├── 3355. Zero Array Transformation I.cpp ├── 3355. Zero Array Transformation I.py ├── 3356. Zero Array Transformation II.cpp ├── 3370. Smallest Number With All Set Bits.cpp ├── 3372. Maximize the Number of Target Nodes After Connecting Trees I.cpp ├── 3373. Maximize the Number of Target Nodes After Connecting Trees II.cpp ├── 3375. Minimum Operations to Make Array Values Equal to K.cpp ├── 3392. Count Subarrays of Length Three With a Condition.cpp ├── 3394. Check if Grid can be Cut into Sections.py ├── 3396. Minimum Number of Operations to Make Elements in Array Distinct.cpp ├── 3396. Minimum Number of Operations to Make Elements in Array Distinct.py ├── 3397. Maximum Number of Distinct Elements After Operations.cpp ├── 3403. Find the Lexicographically Largest String From the Box I.cpp ├── 3407. Substring Matching Pattern.cpp ├── 3412. Find Mirror Score of a String.cpp ├── 343. Integer Break.cpp ├── 3432. Count Partitions with Even Sum Difference.cpp ├── 3446. Sort Matrix by Diagonals.cpp ├── 3461. Check If Digits Are Equal in String After Operations I.cpp ├── 3467. Transform Array by Parity.cpp ├── 3477. Fruits Into Baskets II.cpp ├── 3492. Maximum Containers on a Ship.cpp ├── 3503. Longest Palindrome After Substring Concatenation I.cpp ├── 3512. Minimum Operations to Make Array Sum Divisible by K.cpp ├── 3512. Minimum Operations to Make Array Sum Divisible by K.py ├── 3516. Find Closest Person.cpp ├── 3517. Smallest Palindromic Rearrangement I.cpp ├── 3536. Maximum Product of Two Digits.cpp ├── 3541. Find Most Frequent Vowel and Consonant.py ├── 3545. Minimum Deletions for At Most K Distinct Characters.cpp ├── 3546. Equal Sum Grid Partition I.cpp ├── 3550. Smallest Index With Digit Sum Equal to Index.cpp ├── 368. Largest Divisible Subset.cpp ├── 38. Count and Say.cpp ├── 38. Count and Say.py ├── 386. Lexicographical Numbers.cpp ├── 386. Lexicographical Numbers.py ├── 407. Trapping Rain Water II.cpp ├── 416. Partition Equal Subset Sum(Bottom Up).cpp ├── 416. Partition Equal Subset Sum(Recursion).cpp ├── 416. Partition Equal Subset Sum(Recursion).py ├── 432. All O`one Data Structure.cpp ├── 476. Number Complement.cpp ├── 48. Rotate Image.cpp ├── 494. Target Sum.cpp ├── 494. Target Sum.py ├── 515. Find Largest Value in Each Tree Row.cpp ├── 515. Find Largest Value in Each Tree Row.py ├── 524. Longest Word in Dictionary through Deleting.cpp ├── 539. Minimum Time Difference.cpp ├── 539. Minimum Time Difference.py ├── 567. Permutation in String.cpp ├── 567. Permutation in String.py ├── 590. N-ary Tree Postorder Traversal.cpp ├── 632. Smallest Range Covering Elements from K Lists.py ├── 64. Minimum Path Sum.cpp ├── 641. Design Circular Deque.cpp ├── 641. Design Circular Deque.py ├── 646. Maximum Length of Pair Chain.cpp ├── 655. Print Binary Tree.cpp ├── 658. Find K Closest Elements.cpp ├── 670. Maximum Swap.cpp ├── 676. Implement Magic Dictionary.cpp ├── 684. Redundant Connection.cpp ├── 703. Kth Largest Element in a Stream.cpp ├── 725. Split Linked List in Parts.cpp ├── 729. My Calendar I.cpp ├── 73. Set Matrix Zeroes.cpp ├── 73. Set Matrix Zeroes.py ├── 731. My Calendar II.cpp ├── 74. Search a 2D Matrix.cpp ├── 748. Shortest Completing Word.cpp ├── 75. Sort Colors.cpp ├── 75. Sort Colors.py ├── 762. Prime Number of Set Bits in Binary Representation.cpp ├── 763. Partition Labels.cpp ├── 763. Partition Labels.py ├── 767. Reorganize String.cpp ├── 769. Max Chunks To Make Sorted.cpp ├── 775. Global and Local Inversions.py ├── 781. Rabbits in Forest.cpp ├── 781. Rabbits in Forest.py ├── 788. Rotated Digits.cpp ├── 790. Domino and Tromino Tiling.cpp ├── 796. Rotate String.cpp ├── 796. Rotate String.py ├── 80. Remove Duplicates from Sorted Array II.cpp ├── 802. Find Eventual Safe States.cpp ├── 827. Making A Large Island.cpp ├── 838. Push Dominoes.cpp ├── 862. Shortest Subarray with Sum at Least K.cpp ├── 873. Length of Longest Fibonacci Subsequence.cpp ├── 874. Walking Robot Simulation.cpp ├── 884. Uncommon Words from Two Sentences.cpp ├── 884. Uncommon Words from Two Sentences.py ├── 909. Snakes and Ladders.cpp ├── 914. X of a Kind in a Deck of Cards.cpp ├── 915. Partition Array into Disjoint Intervals.cpp ├── 916. Word Subsets.cpp ├── 921. Minimum Add to Make Parentheses Valid.cpp ├── 921. Minimum Add to Make Parentheses Valid.py ├── 925. Long Pressed Name.cpp ├── 951. Flip Equivalent Binary Trees (BFS).cpp ├── 951. Flip Equivalent Binary Trees (DSF).cpp ├── 962. Maximum Width Ramp.cpp ├── 983. Minimum Cost For Tickets.cpp ├── 989. Add to Array-Form of Integer.cpp ├── 993. Cousins in Binary Tree (DFS).cpp ├── Array ├── 1295. Find Numbers with Even Number of Digits.cpp ├── 1295. Find Numbers with Even Number of Digits.py ├── 1534. Count Good Triplets.cpp ├── 1534. Count Good Triplets.py ├── 1550. Three Consecutive Odds.cpp ├── 1550. Three Consecutive Odds.py ├── 1752. Check if Array Is Sorted and Rotated.cpp ├── 1752. Check if Array Is Sorted and Rotated.py ├── 1800. Maximum Ascending Subarray Sum.cpp ├── 1995. Count Special Quadruplets.cpp ├── 2094. Finding 3-Digit Even Numbers.py ├── 2161. Partition Array According to Given Pivot.cpp ├── 2161. Partition Array According to Given Pivot.py ├── 2176. Count Equal and Divisible Pairs in an Array.cpp ├── 2176. Count Equal and Divisible Pairs in an Array.py ├── 2210. Count Hills and Valleys in an Array.cpp ├── 2342. Max Sum of a Pair With Equal Sum of Digits.py ├── 2444. Count Subarrays With Fixed Bounds.cpp ├── 2444. Count Subarrays With Fixed Bounds.py ├── 2460. Apply Operations to an Array.cpp ├── 2460. Apply Operations to an Array.py ├── 2570. Merge Two 2D Arrays by Summing Values.cpp ├── 2570. Merge Two 2D Arrays by Summing Values.py ├── 2661. First Completely Painted Row or Column.cpp ├── 2873. Maximum Value of an Ordered Triplet I.cpp ├── 2900. Longest Unequal Adjacent Groups Subsequence I.cpp ├── 2900. Longest Unequal Adjacent Groups Subsequence I.py ├── 2908. Minimum Sum of Mountain Triplets I.cpp ├── 2913. Subarrays Distinct Element Sum of Squares I.cpp ├── 2948. Make Lexicographically Smallest Array by Swapping Elements.cpp ├── 2960. Count Tested Devices After Test Operations.cpp ├── 31. Next Permutation.cpp ├── 3105. Longest Strictly Increasing or Strictly Decreasing Subarray.cpp ├── 3105. Longest Strictly Increasing or Strictly Decreasing Subarray.py ├── 3151. Special Array I.cpp ├── 3151. Special Array I.py ├── 3392. Count Subarrays of Length Three With a Condition.cpp ├── 3432. Count Partitions with Even Sum Difference.cpp ├── 3477. Fruits Into Baskets II.cpp ├── 75. Sort Colors.cpp ├── 75. Sort Colors.py └── 915. Partition Array into Disjoint Intervals.cpp ├── Backtracking ├── 1079. Letter Tile Possibilities.cpp ├── 1415. The k-th Lexicographical String of All Happy Strings of Length n.cpp ├── 1718. Construct the Lexicographically Largest Valid Sequence.cpp ├── 1980. Find Unique Binary String.cpp ├── 1980. Find Unique Binary String.py ├── 2375. Construct Smallest Number From DI String.cpp └── 2698. Find the Punishment Number of an Integer.cpp ├── Binary Search ├── 1608. Special Array With X Elements Greater Than or Equal X.cpp ├── 2071. Maximum Number of Tasks You Can Assign.cpp ├── 2226. Maximum Candies Allocated to K Children.cpp ├── 2529. Maximum Count of Positive Integer and Negative Integer.cpp ├── 2560. House Robber IV.cpp ├── 2594. Minimum Time to Repair Cars.cpp └── 3356. Zero Array Transformation II.cpp ├── Bit Manipulation ├── 1018. Binary Prefix Divisible By 5.cpp ├── 2317. Maximum XOR After Operations .cpp ├── 2932. Maximum Strong Pair XOR I.cpp ├── 3068. Find the Maximum Sum of Node Values.cpp ├── 3191. Minimum Operations to Make Binary Array Elements Equal to One I.cpp ├── 3191. Minimum Operations to Make Binary Array Elements Equal to One I.js ├── 3191. Minimum Operations to Make Binary Array Elements Equal to One I.py └── 762. Prime Number of Set Bits in Binary Representation.cpp ├── Breadth-First Search (BFS) ├── 1765. Map of Highest Peak.cpp ├── 2503. Maximum Number of Points From Grid Queries.cpp ├── 3286. Find a Safe Walk Through a Grid.cpp ├── 64. Minimum Path Sum.cpp ├── 802. Find Eventual Safe States.cpp └── 909. Snakes and Ladders.cpp ├── Combinatorics └── 2929. Distribute Candies Among Children II.cpp ├── Counting ├── 1128. Number of Equivalent Domino Pairs.cpp ├── 1399. Count Largest Group.cpp ├── 1399. Count Largest Group.py ├── 1897. Redistribute Characters to Make All Strings Equal.cpp ├── 2131. Longest Palindrome by Concatenating Two Letter Words.cpp ├── 2131. Longest Palindrome by Concatenating Two Letter Words.py ├── 2206. Divide Array Into Equal Pairs.cpp ├── 2780. Minimum Index of a Valid Split.cpp ├── 2845. Count of Interesting Subarrays.cpp ├── 3335. Total Characters in String After Transformations I.cpp ├── 3335. Total Characters in String After Transformations I.py ├── 3375. Minimum Operations to Make Array Values Equal to K.cpp ├── 3396. Minimum Number of Operations to Make Elements in Array Distinct.cpp ├── 3396. Minimum Number of Operations to Make Elements in Array Distinct.py ├── 3467. Transform Array by Parity.cpp └── 3541. Find Most Frequent Vowel and Consonant.py ├── Depth-First Search (DFS) ├── 1061. Lexicographically Smallest Equivalent String.cpp ├── 2359. Find Closest Node to Given Two Nodes.cpp ├── 2658. Maximum Number of Fish in a Grid.cpp ├── 3372. Maximize the Number of Target Nodes After Connecting Trees I.cpp ├── 3373. Maximize the Number of Target Nodes After Connecting Trees II.cpp └── 827. Making A Large Island.cpp ├── Design ├── 1865. Finding Pairs With a Certain Sum(Optimized).cpp ├── 1865. Finding Pairs With a Certain Sum.cpp ├── 2043. Simple Bank System.cpp ├── 2349. Design a Number Container System.cpp └── 676. Implement Magic Dictionary.cpp ├── Dynamic Programming ├── 1641. Count Sorted Vowel Strings.cpp ├── 1863. Sum of All Subset XOR Totals.cpp ├── 2140. Solving Questions With Brainpower.cpp ├── 2466. Count Ways To Build Good Strings.cpp ├── 3343. Count Number of Balanced Permutations.cpp ├── 343. Integer Break.cpp ├── 368. Largest Divisible Subset.cpp ├── 416. Partition Equal Subset Sum(Bottom Up).cpp ├── 416. Partition Equal Subset Sum(Recursion).cpp ├── 416. Partition Equal Subset Sum(Recursion).py ├── 790. Domino and Tromino Tiling.cpp └── Minimum Falling Path Sum II.cpp ├── Graph ├── 1462. Course Schedule IV.cpp ├── 1857. Largest Color Value in a Directed Graph.cpp ├── 1971. Find if Path Exists in Graph.cpp ├── 1992. Find All Groups of Farmland.cpp ├── 2115. Find All Possible Recipes from Given Supplies.cpp ├── 2127. Maximum Employees to Be Invited to a Meeting.cpp ├── 2368. Reachable Nodes With Restrictions.cpp ├── 2467. Most Profitable Path in a Tree.cpp ├── 2685. Count the Number of Complete Components.cpp ├── 3108. Minimum Cost Walk in Weighted Graph.cpp ├── 684. Redundant Connection.cpp └── 752. Open the Lock.cpp ├── Greedy ├── 1282. Group the People Given the Group Size They Belong To.cpp ├── 135. Candy.cpp ├── 1749. Maximum Absolute Sum of Any Subarray.cpp ├── 2091. Removing Minimum and Maximum From Array.cpp ├── 2139. Minimum Moves to Reach Target Score.cpp ├── 2434. Using a Robot to Print the Lexicographically Smallest String.cpp ├── 2874. Maximum Value of an Ordered Triplet II.cpp ├── 2909. Minimum Sum of Mountain Triplets II.cpp ├── 2918. Minimum Equal Sum of Two Arrays After Replacing Zeros.cpp ├── 3397. Maximum Number of Distinct Elements After Operations.cpp ├── 781. Rabbits in Forest.cpp ├── 781. Rabbits in Forest.py └── 873. Length of Longest Fibonacci Subsequence.cpp ├── Hash Table ├── 1524. Number of Sub-arrays With Odd Sum.cpp ├── 1711. Count Good Meals.cpp ├── 1726. Tuple with Same Product.cpp ├── 1726. Tuple with Same Product.py ├── 2364. Count Number of Bad Pairs.cpp ├── 2965. Find Missing and Repeated Values.cpp ├── 2965. Find Missing and Repeated Values.py ├── 3160. Find the Number of Distinct Colors Among the Balls.cpp └── 318. Maximum Product of Word Lengths.cpp ├── Heap (Priority Queue) ├── 2342. Max Sum of a Pair With Equal Sum of Digits.cpp ├── 2551. Put Marbles in Bags.cpp ├── 3066. Minimum Operations to Exceed Threshold Value II.cpp ├── 3066. Minimum Operations to Exceed Threshold Value II.py ├── 3545. Minimum Deletions for At Most K Distinct Characters.cpp └── 407. Trapping Rain Water II.cpp ├── January 23, 2025.md ├── Math ├── 1513. Number of Substrings With Only 1s.cpp ├── 1688. Count of Matches in Tournament.cpp ├── 1759. Count Number of Homogenous Substrings.cpp ├── 1780. Check if Number is a Sum of Powers of Three.cpp ├── 1837. Sum of Digits in Base K.cpp ├── 1922. Count Good Numbers.py ├── 2139. Minimum Moves to Reach Target Score.cpp ├── 2338. Count the Number of Ideal Arrays.cpp ├── 2455. Average Value of Even Numbers That Are Divisible by Three.cpp ├── 2481. Minimum Cuts to Divide a Circle.cpp ├── 2544. Alternating Digit Sum.cpp ├── 2579. Count Total Number of Colored Cells.cpp ├── 2579. Count Total Number of Colored Cells.py ├── 273. Integer to English Words.cpp ├── 2843. Count Symmetric Integers.cpp ├── 2894. Divisible and Non-divisible Sums Difference.cpp ├── 2894. Divisible and Non-divisible Sums Difference.py ├── 3272. Find the Count of Good Integers.cpp ├── 3492. Maximum Containers on a Ship.cpp ├── 3512. Minimum Operations to Make Array Sum Divisible by K.cpp ├── 3512. Minimum Operations to Make Array Sum Divisible by K.py ├── 3516. Find Closest Person.cpp └── 3550. Smallest Index With Digit Sum Equal to Index.cpp ├── Matrix ├── 1007. Minimum Domino Rotations For Equal Row.cpp ├── 1267. Count Servers that Communicate.cpp ├── 3446. Sort Matrix by Diagonals.cpp ├── 3546. Equal Sum Grid Partition I.cpp ├── 36. Valid Sudoku.cpp ├── 73. Set Matrix Zeroes.cpp └── 73. Set Matrix Zeroes.py ├── Monotonic Stack └── 2818. Apply Operations to Maximize Score.cpp ├── MySQL ├── 1148. Article Views I.sql ├── 1741. Find Total Time Spent by Each Employee.sql ├── 176. Second Highest Salary.sql ├── 181. Employees Earning More Than Their Managers.sql ├── 182. Duplicate Emails.sql ├── 183. Customers Who Never Order.sql ├── 184. Department Highest Salary.sql ├── 1890. The Latest Login in 2020.sql ├── 584. Find Customer Referee.sql ├── 595. Big Countries.sql └── 596. Classes More Than 5 Students.sql ├── Number Theory ├── 2523. Closest Prime Numbers in Range.cpp ├── 2523. Closest Prime Numbers in Range.py └── 914. X of a Kind in a Deck of Cards.cpp ├── Policy-Based Data Structures (PBDS) ├── 2563. Count the Number of Fair Pairs.cpp ├── 2563. Count the Number of Fair Pairs.py ├── 775. Global and Local Inversions.py └── PBDS_Templated.cpp ├── Prefix Sum ├── 1352. Product of the Last K Numbers.cpp ├── 1413. Minimum Value to Get Positive Step by Step Sum.cpp ├── 2145. Count the Hidden Sequences.cpp ├── 3355. Zero Array Transformation I.cpp └── 3355. Zero Array Transformation I.py ├── README.md ├── Recursion ├── 38. Count and Say.cpp └── 38. Count and Say.py ├── Segment Tree └── 2179. Count Good Triplets in an Array.cpp ├── Shortest Path ├── 1091. Shortest Path in Binary Matrix.cpp ├── 1976. Number of Ways to Arrive at Destination.cpp ├── 3341. Find Minimum Time to Reach Last Room I.cpp └── 3342. Find Minimum Time to Reach Last Room II.cpp ├── Simulation ├── 1920. Build Array from Permutation.cpp └── 1920. Build Array from Permutation.py ├── Sliding Window ├── 2302. Count Subarrays With Score Less Than K.cpp ├── 2302. Count Subarrays With Score Less Than K.py ├── 2379. Minimum Recolors to Get K Consecutive Black Blocks.cpp ├── 2379. Minimum Recolors to Get K Consecutive Black Blocks.js ├── 2379. Minimum Recolors to Get K Consecutive Black Blocks.py ├── 2401. Longest Nice Subarray.cpp ├── 2537. Count the Number of Good Subarrays.cpp ├── 2799. Count Complete Subarrays in an Array.cpp ├── 2799. Count Complete Subarrays in an Array.py ├── 2962. Count Subarrays Where Max Element Appears at Least K Times.cpp ├── 2962. Count Subarrays Where Max Element Appears at Least K Times.py ├── 3208. Alternating Groups II.cpp ├── 3208. Alternating Groups II.py └── 3306. Count of Substrings Containing Every Vowel and K Consonants II.cpp ├── Sorting ├── 1984. Minimum Difference Between Highest and Lowest of K Scores.cpp ├── 2033. Minimum Operations to Make a Uni-Value Grid.cpp ├── 3169. Count Days Without Meetings.cpp ├── 3169. Count Days Without Meetings.py ├── 3394. Check if Grid can be Cut into Sections.py ├── 720. Longest Word in Dictionary(Optimized).cpp └── 720. Longest Word in Dictionary.cpp ├── String ├── 1358. Number of Substrings Containing All Three Characters.cpp ├── 1507. Reformat Date.cpp ├── 1910. Remove All Occurrences of a Substring.cpp ├── 2452. Words Within Two Edits of Dictionary.cpp ├── 2942. Find Words Containing Character.cpp ├── 2942. Find Words Containing Character.py ├── 2999. Count the Number of Powerful Integers.cpp ├── 3170. Lexicographically Minimum String After Removing Stars.cpp ├── 3170. Lexicographically Minimum String After Removing Stars.py ├── 3174. Clear Digits.cpp ├── 3174. Clear Digits.py ├── 3461. Check If Digits Are Equal in String After Operations I.cpp ├── 3503. Longest Palindrome After Substring Concatenation I.cpp ├── 3517. Smallest Palindromic Rearrangement I.cpp ├── 3536. Maximum Product of Two Digits.cpp ├── 524. Longest Word in Dictionary through Deleting.cpp ├── 748. Shortest Completing Word.cpp └── 788. Rotated Digits.cpp ├── TestCase ├── TestCaseGenerate.py ├── TestCase_1000.txt ├── TestCase_2000.txt ├── TestCase_3000.txt ├── TestCase_4000.txt └── TestCase_5000.txt ├── Tree ├── 1028. Recover a Tree From Preorder Traversal.cpp ├── 1123. Lowest Common Ancestor of Deepest Leaves.cpp ├── 1261. Find Elements in a Contaminated Binary Tree.cpp ├── 2476. Closest Nodes Queries in a Binary Search Tree.cpp └── 655. Print Binary Tree.cpp ├── Two Pointer ├── 1790. Check if One String Swap Can Make Strings Equal.cpp ├── 3403. Find the Lexicographically Largest String From the Box I.cpp ├── 763. Partition Labels.cpp ├── 763. Partition Labels.py └── 838. Push Dominoes.cpp └── my_command_history.txt /1005. Maximize Sum Of Array After K Negations.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int largestSumAfterKNegations(vector& nums, int k) 4 | { 5 | priority_queue, greater>minHeap; 6 | for(auto val:nums) minHeap.push(val); 7 | 8 | while(k--) 9 | { 10 | int val = minHeap.top(); 11 | minHeap.pop(); 12 | minHeap.push(-val); 13 | } 14 | 15 | int maxSum = 0; 16 | while(!minHeap.empty()) 17 | { 18 | maxSum += minHeap.top(); 19 | minHeap.pop(); 20 | } 21 | return maxSum; 22 | } 23 | }; -------------------------------------------------------------------------------- /1014. Best Sightseeing Pair.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxScoreSightseeingPair(vector& values) 4 | { 5 | int ans = 0, max_i = values[0], n = values.size(); 6 | for(int j = 1; j < n; j++) 7 | { 8 | ans = max(ans, max_i + values[j] - j); 9 | max_i = max(max_i, values[j] + j); 10 | } 11 | return ans; 12 | } 13 | }; -------------------------------------------------------------------------------- /1018. Binary Prefix Divisible By 5.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector prefixesDivBy5(vector& nums) 4 | { 5 | long sum = 0; 6 | vector ans; 7 | for(auto &val : nums) 8 | { 9 | sum += sum + val; 10 | ans.push_back(sum % 5 == 0); 11 | sum %= 5; 12 | } 13 | return ans; 14 | } 15 | }; -------------------------------------------------------------------------------- /1021. Remove Outermost Parentheses.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string removeOuterParentheses(string s) 4 | { 5 | string ans; 6 | int cnt = 0; 7 | for(char ch:s) 8 | { 9 | if(ch == '(') 10 | { 11 | if(cnt) ans += ch; 12 | cnt++; 13 | } 14 | else 15 | { 16 | cnt--; 17 | if(cnt) ans += ch; 18 | } 19 | } 20 | return ans; 21 | } 22 | }; -------------------------------------------------------------------------------- /1128. Number of Equivalent Domino Pairs.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numEquivDominoPairs(vector>& dominoes) 4 | { 5 | int ans = 0; 6 | unordered_map count; 7 | for(auto &vec : dominoes) 8 | { 9 | if(vec[0] > vec[1]) swap(vec[0], vec[1]); 10 | ans += count[to_string(vec[0]) + to_string(vec[1])]++; 11 | } 12 | return ans; 13 | } 14 | }; -------------------------------------------------------------------------------- /1233. Remove Sub-Folders from the Filesystem.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector removeSubfolders(vector& folder) 4 | { 5 | sort(begin(folder), end(folder)); 6 | vectorans(1); 7 | ans[0] = folder[0]; 8 | 9 | int n = folder.size(); 10 | for(int i = 1; i < n; i++) 11 | { 12 | auto target = ans.back(); 13 | auto s = folder[i].substr(0, target.size()); 14 | if(target != s or folder[i][target.size()] != '/') 15 | ans.push_back(folder[i]); 16 | } 17 | return ans; 18 | } 19 | }; -------------------------------------------------------------------------------- /1261. Find Elements in a Contaminated Binary Tree.cpp: -------------------------------------------------------------------------------- 1 | class FindElements { 2 | public: 3 | unordered_set store; 4 | void DFS(TreeNode* &root, int val) 5 | { 6 | if(not root) return; 7 | store.insert(val); 8 | DFS(root->left, val * 2 + 1); 9 | DFS(root->right, val * 2 + 2); 10 | } 11 | FindElements(TreeNode* root) { 12 | store.insert(0); 13 | DFS(root->left, 1); 14 | DFS(root->right, 2); 15 | } 16 | 17 | bool find(int target) { 18 | return store.count(target); 19 | } 20 | }; -------------------------------------------------------------------------------- /1267. Count Servers that Communicate.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countServers(vector>& grid) 4 | { 5 | int n = grid.size(), m = grid[0].size(); 6 | vector> store(2, vector(255)); 7 | for(int i = 0; i < n; i++) 8 | for(int j = 0; j < m; j++) 9 | store[0][i] += grid[i][j], store[1][j] += grid[i][j]; 10 | 11 | int ans = 0; 12 | for(int i = 0; i < n; i++) 13 | for(int j = 0; j < m; j++) 14 | ans += grid[i][j] and (store[0][i] > 1 or store[1][j] > 1); 15 | 16 | return ans; 17 | } 18 | }; -------------------------------------------------------------------------------- /1295. Find Numbers with Even Number of Digits.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findNumbers(vector& nums) 4 | { 5 | int ans = 0; 6 | for(auto &val : nums) 7 | ans += to_string(val).size() % 2 == 0; 8 | return ans; 9 | } 10 | }; -------------------------------------------------------------------------------- /1295. Find Numbers with Even Number of Digits.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findNumbers(self, nums: List[int]) -> int: 3 | ans = 0 4 | for val in nums: 5 | ans += not (len(str(val)) % 2) 6 | return ans -------------------------------------------------------------------------------- /1310. XOR Queries of a Subarray.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | vector xorQueries(vector &arr, vector> &queries) 5 | { 6 | int n = arr.size(), l, r; 7 | vector pref(n + 1); 8 | pref[0] = 0; 9 | for (int i = 1; i <= n; i++) 10 | pref[i] = pref[i - 1] ^ arr[i - 1]; 11 | 12 | vector ans; 13 | for (auto &Q : queries) 14 | { 15 | l = Q[0], r = Q[1] + 1; 16 | ans.push_back(pref[r] ^ pref[l]); 17 | } 18 | return ans; 19 | } 20 | }; -------------------------------------------------------------------------------- /1310. XOR Queries of a Subarray.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def xorQueries(self, arr, queries): 3 | n = len(arr) 4 | 5 | pref = [0] * (n + 1) 6 | for i in range(1, n + 1): 7 | pref[i] = pref[i - 1] ^ arr[i - 1] 8 | 9 | ans = [] 10 | for l, r in queries: 11 | ans.append(pref[l] ^ pref[r + 1]) 12 | return ans -------------------------------------------------------------------------------- /1331. Rank Transform of an Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | vector arrayRankTransform(vector &arr) 5 | { 6 | set st(begin(arr), end(arr)); 7 | 8 | int rankNumber = 1; 9 | unordered_map rank; 10 | for (auto val : st) 11 | rank[val] = rankNumber++; 12 | 13 | for (auto &val : arr) 14 | val = rank[val]; 15 | return arr; 16 | } 17 | }; -------------------------------------------------------------------------------- /1331. Rank Transform of an Array.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def arrayRankTransform(self, arr: List[int]) -> List[int]: 3 | nums = sorted(set(arr)) 4 | 5 | Rank = 1 6 | rank = defaultdict(int) 7 | for num in nums: 8 | rank[num] = Rank 9 | Rank += 1 10 | return [rank[num] for num in arr] -------------------------------------------------------------------------------- /1346. Check If N and Its Double Exist.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def check(self, arr): 3 | store = defaultdict(int) 4 | for val in arr: 5 | print(val * 2) 6 | if store[val * 2]: 7 | return True 8 | store[val] = 1 9 | return False 10 | 11 | def checkIfExist(self, arr: List[int]) -> bool: 12 | arr.sort() 13 | if self.check(arr): 14 | return True 15 | 16 | arr.reverse() 17 | return self.check(arr) -------------------------------------------------------------------------------- /1358. Number of Substrings Containing All Three Characters.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numberOfSubstrings(string s) 4 | { 5 | int n = s.size(), ans = 0; 6 | vector lastSeen(100); 7 | for(int i = 0; i <= n; i++) 8 | { 9 | ans += min({lastSeen['a'], lastSeen['b'], lastSeen['c']}); 10 | lastSeen[s[i]] = i + 1; 11 | } 12 | return ans; 13 | } 14 | }; -------------------------------------------------------------------------------- /1387. Sort Integers by The Power Value.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int getStep(int n) 4 | { 5 | int count = 0; 6 | while(n != 1) 7 | { 8 | count += 1; 9 | if(n % 2) n = (3 * n) + 1; 10 | else n /= 2; 11 | } 12 | return count; 13 | } 14 | int getKth(int lo, int hi, int k) 15 | { 16 | vector>arr; 17 | for(int val = lo; val <= hi; val++) 18 | { 19 | int power = getStep(val); 20 | arr.push_back({power, val}); 21 | } 22 | sort(begin(arr), end(arr)); 23 | 24 | return arr[k - 1].second; 25 | } 26 | }; -------------------------------------------------------------------------------- /1399. Count Largest Group.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countLargestGroup(int n) 4 | { 5 | int max_count = 0; 6 | unordered_mapcount; 7 | for(int i = 1; i <= n; i++) 8 | { 9 | int sum = 0; 10 | string s = to_string(i); 11 | for(char ch : s) 12 | sum += ch - '0'; 13 | count[sum] += 1; 14 | 15 | max_count = max(max_count, count[sum]); 16 | } 17 | 18 | int ans = 0; 19 | for(auto [val, cnt] : count) 20 | ans += cnt == max_count; 21 | return ans; 22 | } 23 | }; -------------------------------------------------------------------------------- /1399. Count Largest Group.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countLargestGroup(self, n: int) -> int: 3 | max_count = 0 4 | count = defaultdict(int) 5 | for val in range(1, n + 1): 6 | sum = 0 7 | for ch in str(val): 8 | sum += ord(ch) - ord('0') 9 | count[sum] += 1 10 | max_count = max(max_count, count[sum]) 11 | 12 | ans = 0 13 | for val in count: 14 | ans += count[val] == max_count 15 | return ans -------------------------------------------------------------------------------- /1400. Construct K Palindrome Strings.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canConstruct(string s, int k) 4 | { 5 | vector freq(26); 6 | for(auto ch : s) freq[ch - 'a'] += 1; 7 | 8 | int oddCount = 0; 9 | for(int i = 0; i < 26; i++) 10 | oddCount += freq[i] % 2; 11 | return s.size() >= k and oddCount <= k; 12 | } 13 | }; -------------------------------------------------------------------------------- /1408. String Matching in an Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector stringMatching(vector& w) 4 | { 5 | sort(w.begin(),w.end()); 6 | int n = w.size(); 7 | settmp; 8 | for(int i=0;ians(vector(tmp.begin(),tmp.end())); 20 | return ans; 21 | } 22 | }; -------------------------------------------------------------------------------- /1413. Minimum Value to Get Positive Step by Step Sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minStartValue(vector& nums) 4 | { 5 | int sum = 0, minSum = (100 * 100); 6 | for(auto &val : nums) 7 | { 8 | sum += val; 9 | minSum = min(minSum, sum); 10 | } 11 | return abs(min(minSum, 0) - 1); 12 | } 13 | }; -------------------------------------------------------------------------------- /1422. Maximum Score After Splitting a String.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shishirRsiam/LeetCode-Solve/147b43842a42f4d94d5594419bd146678e96fa55/1422. Maximum Score After Splitting a String.cpp -------------------------------------------------------------------------------- /1422. Maximum Score After Splitting a String.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxScore(self, s: str) -> int: 3 | ans, n = 0, len(s) 4 | for i in range(1, n): 5 | left_count = s[:i].count('0') 6 | right_count = s[i:n].count('1') 7 | ans = max(ans, left_count + right_count) 8 | return ans -------------------------------------------------------------------------------- /145. Binary Tree Postorder Traversal.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | vectora; 5 | void postOrder(TreeNode*root) 6 | { 7 | if(root == NULL) return; 8 | postOrder(root->left); 9 | postOrder(root->right); 10 | a.push_back(root->val); 11 | } 12 | vector postorderTraversal(TreeNode* root) 13 | { 14 | postOrder(root); 15 | return a; 16 | } 17 | }; -------------------------------------------------------------------------------- /1475. Final Prices With a Special Discount in a Shop.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector finalPrices(vector& prices) 4 | { 5 | int n = prices.size(); 6 | vector stact, ans(n); 7 | for(int i = 0; i < n; i++) 8 | { 9 | while(!stact.empty() and prices[stact.back()] >= prices[i]) 10 | { 11 | ans[stact.back()] = prices[stact.back()] - prices[i]; 12 | stact.pop_back(); 13 | } 14 | stact.push_back(i); 15 | } 16 | 17 | for(auto &i : stact) 18 | ans[i] = prices[i]; 19 | 20 | return ans; 21 | } 22 | }; -------------------------------------------------------------------------------- /1475. Final Prices With a Special Discount in a Shop.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def finalPrices(self, prices: List[int]) -> List[int]: 3 | n = len(prices) 4 | stact, ans = [], [0] * n 5 | 6 | for i in range(n): 7 | while stact and prices[stact[-1]] >= prices[i]: 8 | ans[stact[-1]] = prices[stact[-1]] - prices[i] 9 | stact.pop() 10 | stact.append(i) 11 | 12 | for idx in stact: 13 | ans[idx] = prices[idx] 14 | 15 | return ans -------------------------------------------------------------------------------- /1497. Check If Array Pairs Are Divisible by k.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shishirRsiam/LeetCode-Solve/147b43842a42f4d94d5594419bd146678e96fa55/1497. Check If Array Pairs Are Divisible by k.cpp -------------------------------------------------------------------------------- /1513. Number of Substrings With Only 1s.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numSub(string s) 4 | { 5 | int mod = 1e9 + 7; 6 | long ans = 0, count = 0, n = s.size(); 7 | for(int i = 0; i <= n; i++) 8 | { 9 | if(s[i] == '1') count += 1; 10 | else 11 | { 12 | ans += (count * (count + 1) / 2) % mod; 13 | count = 0; 14 | } 15 | } 16 | return ans % mod; 17 | } 18 | }; -------------------------------------------------------------------------------- /1524. Number of Sub-arrays With Odd Sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numOfSubarrays(vector& nums) 4 | { 5 | int even = 1, odd = 0; 6 | long sum = 0, ans = 0, mod = 1e9 + 7; 7 | for(auto val : nums) 8 | { 9 | sum += val; 10 | sum % 2 ? odd++ : even++; 11 | if(sum % 2) ans = (ans + even) % mod; 12 | else ans += odd; 13 | } 14 | return ans % mod; 15 | } 16 | }; -------------------------------------------------------------------------------- /1534. Count Good Triplets.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countGoodTriplets(vector& arr, int a, int b, int c) 4 | { 5 | int cnt = 0, n = arr.size(); 6 | for(int i = 0; i < n-2 ;i++) 7 | { 8 | for(int j = i + 1; j < n - 1; j++) 9 | { 10 | for(int k = j + 1; k < n; k++) 11 | { 12 | if(abs(arr[i] - arr[j]) <= a && abs(arr[j] - arr[k]) <= b && abs(arr[i] - arr[k]) <= c) 13 | { 14 | cnt++; 15 | } 16 | } 17 | } 18 | } 19 | return cnt; 20 | } 21 | }; -------------------------------------------------------------------------------- /1534. Count Good Triplets.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int: 3 | ans, n = 0, len(arr) 4 | for i in range(n): 5 | for j in range(i + 1, n): 6 | for k in range(j + 1, n): 7 | if abs(arr[i] - arr[j]) > a: 8 | continue 9 | if abs(arr[j] - arr[k]) > b: 10 | continue 11 | if abs(arr[i] - arr[k]) > c: 12 | continue 13 | ans += 1 14 | return ans -------------------------------------------------------------------------------- /1545. Find Kth Bit in Nth Binary String.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | char findKthBit(int n, int k) 4 | { 5 | string bits = "0"; 6 | for(int _ = 1; _ < n; _++) 7 | { 8 | string newBits = "1"; 9 | int size = bits.size(); 10 | for(int i = size - 1; i >= 0; i--) 11 | newBits += (bits[i] == '0' ? '1' : '0'); 12 | bits += newBits; 13 | } 14 | return bits[k - 1]; 15 | } 16 | }; -------------------------------------------------------------------------------- /1545. Find Kth Bit in Nth Binary String.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findKthBit(self, n: int, k: int) -> str: 3 | bits = '0' 4 | for _ in range(n - 1): 5 | newBits = '1' 6 | size = len(bits) 7 | for i in range(size - 1, -1, -1): 8 | newBits += ('0' if bits[i] == '1' else '1') 9 | bits += newBits 10 | 11 | return bits[k - 1] -------------------------------------------------------------------------------- /1550. Three Consecutive Odds.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool threeConsecutiveOdds(vector& arr) 4 | { 5 | int n = arr.size(); 6 | for(int i = 0; i < n - 2; i++) 7 | if(arr[i] % 2 and arr[i + 1] % 2 and arr[i + 2] % 2) 8 | return true; 9 | return false; 10 | } 11 | }; -------------------------------------------------------------------------------- /1550. Three Consecutive Odds.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def threeConsecutiveOdds(self, arr: List[int]) -> bool: 3 | n = len(arr) 4 | for i in range(2, n): 5 | if arr[i] % 2 and arr[i - 1] % 2 and arr[i - 2] % 2: 6 | return True 7 | return False 8 | 9 | 10 | class Solution(object): 11 | def threeConsecutiveOdds(self, arr): 12 | n = len(arr) 13 | for i in range(n - 2): 14 | if arr[i] % 2 and arr[i + 1] % 2 and arr[i + 2] % 2: 15 | return True 16 | return False 17 | -------------------------------------------------------------------------------- /1556. Thousand Separator.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | string thousandSeparator(int n) 5 | { 6 | int count = 0; 7 | string ans, s = to_string(n); 8 | reverse(begin(s), end(s)); 9 | for (auto ch : s) 10 | { 11 | if (not(count % 3)) 12 | ans += '.'; 13 | ans += ch; 14 | count++; 15 | } 16 | reverse(begin(ans), end(ans)); 17 | ans.pop_back(); 18 | return ans; 19 | } 20 | }; -------------------------------------------------------------------------------- /16. 3Sum Closest copy.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int threeSumClosest(vector& n, int t) 4 | { 5 | sort(n.begin(),n.end()); 6 | 7 | int sum = 0, ans = n[0] + n[1] + n[2]; 8 | int sz = n.size(); 9 | for(int i=0;i List[int]: 3 | n = len(code) 4 | if k == 0: 5 | return [0] * n 6 | 7 | ans = [] 8 | nums = code + code + code 9 | for i in range(n, n + n): 10 | if k > 0: 11 | ans.append(sum(nums[i + 1 : i + k + 1])) 12 | else: 13 | ans.append(sum(nums[i + k : i])) 14 | return ans -------------------------------------------------------------------------------- /1679. Max Number of K-Sum Pairs.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int maxOperations(vector &nums, int k) 5 | { 6 | sort(begin(nums), end(nums)); 7 | int ans = 0, i = 0, j = nums.size() - 1; 8 | while (i < j) 9 | { 10 | if (nums[i] + nums[j] == k) 11 | { 12 | ans += 1; 13 | i += 1, j -= 1; 14 | } 15 | else if (nums[i] + nums[j] > k) 16 | j--; 17 | else if (nums[i] + nums[j] < k) 18 | i++; 19 | } 20 | return ans; 21 | } 22 | }; -------------------------------------------------------------------------------- /1679. Max Number of K-Sum Pairs.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxOperations(self, nums: List[int], k: int) -> int: 3 | nums.sort() 4 | 5 | ans, i, j = 0, 0, len(nums) - 1 6 | while i < j: 7 | if nums[i] + nums[j] == k: 8 | ans += 1 9 | i += 1 10 | j -= 1 11 | elif nums[i] + nums[j] > k: 12 | j -= 1 13 | elif nums[i] + nums[j] < k: 14 | i += 1 15 | return ans -------------------------------------------------------------------------------- /1688. Count of Matches in Tournament.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numberOfMatches(int n) { 4 | return n - 1; 5 | } 6 | }; -------------------------------------------------------------------------------- /1695. Maximum Erasure Value.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int maximumUniqueSubarray(vector &nums) 5 | { 6 | unordered_map mp; 7 | int maxValue = 0, sum = 0; 8 | int i = 0, j = 0, n = nums.size(); 9 | while (j < n) 10 | { 11 | sum += nums[j]; 12 | mp[nums[j]]++; 13 | while (mp[nums[j]] > 1) 14 | { 15 | mp[nums[i]]--; 16 | sum -= nums[i++]; 17 | } 18 | maxValue = max(sum, maxValue); 19 | j++; 20 | } 21 | return maxValue; 22 | } 23 | }; -------------------------------------------------------------------------------- /1711. Count Good Meals.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countPairs(vector& deliciousness) 4 | { 5 | long ans = 0, mod = 1e9 + 7; 6 | unordered_map count; 7 | for(auto &val : deliciousness) 8 | { 9 | int curPow = 1; 10 | while(curPow + curPow < 1e7) 11 | { 12 | ans += (count[(curPow - val)]); 13 | curPow += curPow; 14 | } 15 | ans %= mod; 16 | count[val] += 1; 17 | } 18 | return ans; 19 | } 20 | }; -------------------------------------------------------------------------------- /1716. Calculate Money in Leetcode Bank.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int totalMoney(int n) 4 | { 5 | int week = n / 7; 6 | int rem_days = n % 7; 7 | 8 | int ans = 0; 9 | for(int i = 0; i < week; i++) 10 | ans += 28 + (i * 7); 11 | 12 | int cur_puts = week + 1; 13 | while(rem_days--) 14 | ans += cur_puts++; 15 | 16 | return ans; 17 | } 18 | }; -------------------------------------------------------------------------------- /1726. Tuple with Same Product.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int tupleSameProduct(vector& nums) 4 | { 5 | int ans = 0, n = nums.size(); 6 | unordered_map store; 7 | for(int i = 0; i < n; i++) 8 | for(int j = i + 1; j < n; j++) 9 | store[long(nums[i]) * nums[j]] += 1; 10 | 11 | for(int i = 0; i < n; i++) 12 | for(int j = i + 1; j < n; j++) 13 | ans += (store[long(nums[i]) * nums[j]] - 1) * 4; 14 | 15 | return ans; 16 | } 17 | }; -------------------------------------------------------------------------------- /1726. Tuple with Same Product.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def tupleSameProduct(self, nums: List[int]) -> int: 3 | ans, n = 0, len(nums) 4 | store = defaultdict(int) 5 | for i in range(n): 6 | for j in range(i + 1, n): 7 | store[nums[i] * nums[j]] += 1 8 | 9 | for i in range(n): 10 | for j in range(i + 1, n): 11 | ans += (store[nums[i] * nums[j]] - 1) * 4 12 | return ans -------------------------------------------------------------------------------- /1742. Maximum Number of Balls in a Box.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countBalls(int lowLimit, int highLimit) 4 | { 5 | int ans = 0, sum; 6 | unordered_map count; 7 | for(int number = lowLimit; number <= highLimit; number++) 8 | { 9 | sum = 0; 10 | string s = to_string(number); 11 | for(auto ch : s) 12 | sum += ch - '0'; 13 | 14 | count[sum] += 1; 15 | ans = max(ans, count[sum]); 16 | } 17 | return ans; 18 | } 19 | }; -------------------------------------------------------------------------------- /1749. Maximum Absolute Sum of Any Subarray.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxAbsoluteSum(vector& nums) 4 | { 5 | int sum = 0, ans = INT_MIN; 6 | for(auto &val : nums) 7 | { 8 | sum += val; 9 | if(sum < 0) sum = 0; 10 | ans = max(ans, abs(sum)); 11 | } 12 | 13 | sum = 0; 14 | for(auto &val : nums) 15 | { 16 | sum += val; 17 | if(sum < 0) ans = max(ans, abs(sum)); 18 | else sum = 0; 19 | } 20 | return ans; 21 | } 22 | }; -------------------------------------------------------------------------------- /1752. Check if Array Is Sorted and Rotated.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool check(vector& nums) 4 | { 5 | int n = nums.size(); 6 | vectort(nums.begin(),nums.end()); 7 | sort(t.begin(),t.end()); 8 | for(int i=0;i bool: 3 | flag = False 4 | n = len(nums) 5 | for i in range(1, n): 6 | if nums[i] < nums[i - 1]: 7 | arr = nums[i:] + nums[:i] 8 | nums.sort() 9 | return nums == arr 10 | return True -------------------------------------------------------------------------------- /1759. Count Number of Homogenous Substrings.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countHomogenous(string s) 4 | { 5 | char prev = '.'; 6 | long ans = 0, count = 0; 7 | int mod = 1e9 + 7, n = s.size(); 8 | for(int i = 0; i <= n; i++) 9 | { 10 | if(s[i] == prev) count += 1; 11 | else 12 | { 13 | ans += (count * (count + 1) / 2); 14 | count = 1, prev = s[i]; 15 | } 16 | } 17 | return ans % mod; 18 | } 19 | }; -------------------------------------------------------------------------------- /1769. Minimum Number of Operations to Move All Balls to Each Box.js: -------------------------------------------------------------------------------- 1 | var minOperations = function(boxes) 2 | { 3 | let n = boxes.length 4 | let ans = Array(n).fill(0); 5 | let oneCount = 0, operation = 0; 6 | for(let i = 0; i < n; i++) 7 | { 8 | ans[i] += operation; 9 | oneCount += boxes[i] == '1'; 10 | operation += oneCount; 11 | } 12 | oneCount = 0, operation = 0; 13 | for(let i = n - 1; i >= 0; i--) 14 | { 15 | ans[i] += operation; 16 | oneCount += boxes[i] == '1'; 17 | operation += oneCount; 18 | } 19 | return ans; 20 | }; -------------------------------------------------------------------------------- /1769. Minimum Number of Operations to Move All Balls to Each Box.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def minOperations(self, boxes): 3 | n = len(boxes) 4 | ans = [0]*n 5 | count = op = 0 6 | for i in range(n): 7 | ans[i] = op 8 | count += boxes[i] == '1' 9 | op += count 10 | op = count = 0 11 | for i in range(n-1, -1, -1): 12 | ans[i] += op 13 | count += boxes[i] == '1' 14 | op += count 15 | return ans -------------------------------------------------------------------------------- /1780. Check if Number is a Sum of Powers of Three.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool checkPowersOfThree(int n) 4 | { 5 | int cur_pow = 15; 6 | while(cur_pow--) 7 | { 8 | if(pow(3, cur_pow) > n) continue; 9 | n -= pow(3, cur_pow); 10 | } 11 | return n == 0; 12 | } 13 | }; -------------------------------------------------------------------------------- /1784. Check if Binary String Has at Most One Segment of Ones.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool checkOnesSegment(string s) 4 | { 5 | bool flag = false; 6 | 7 | for(auto ch:s) 8 | { 9 | if(ch == '0') flag = true; 10 | if(flag and ch == '1') return false; 11 | } 12 | 13 | return true; 14 | } 15 | }; -------------------------------------------------------------------------------- /179. Largest Number.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def cmp(self, a, b): 3 | if a + b > b + a: 4 | return -1 5 | return a + b < b + a 6 | def largestNumber(self, nums): 7 | all_str = list(map(str, nums)) 8 | 9 | all_str.sort(key=cmp_to_key(self.cmp)) 10 | 11 | if all_str[0] == '0': 12 | return "0" 13 | 14 | return ''.join(all_str) -------------------------------------------------------------------------------- /1790. Check if One String Swap Can Make Strings Equal.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool areAlmostEqual(string s1, string s2) 4 | { 5 | if(s1 == s2) return true; 6 | 7 | int i = 0, n = max(s1.size(), s2.size()); 8 | while(i < n and s1[i] == s2[i]) i++; 9 | 10 | int j = i + 1; 11 | while(j < n and s1[j] == s2[j]) j++; 12 | 13 | swap(s1[i], s1[j]); 14 | 15 | return s1 == s2; 16 | } 17 | }; -------------------------------------------------------------------------------- /1800. Maximum Ascending Subarray Sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxAscendingSum(vector& nums) 4 | { 5 | int n = nums.size(), sum = nums[0], maxSum = nums[0]; 6 | for(int i = 1; i < n; i++) 7 | { 8 | if(nums[i] <= nums[i - 1]) sum = 0; 9 | sum += nums[i]; 10 | maxSum = max(maxSum, sum); 11 | } 12 | return maxSum; 13 | } 14 | }; -------------------------------------------------------------------------------- /1829. Maximum XOR for Each Query.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector getMaximumXor(vector& nums, int maximumBit) 4 | { 5 | string bit; 6 | while(maximumBit--) bit += '1'; 7 | int target = stoi(bit, NULL, 2); 8 | 9 | vectorans, prev; 10 | prev.push_back(0); 11 | for(auto val:nums) 12 | prev.push_back(prev.back() ^ val ); 13 | 14 | for(auto val:nums) 15 | { 16 | ans.push_back(prev.back() ^ target); 17 | prev.pop_back(); 18 | } 19 | 20 | return ans; 21 | } 22 | }; -------------------------------------------------------------------------------- /1837. Sum of Digits in Base K.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int sumBase(int n, int k) 4 | { 5 | int ans = 0; 6 | while(n) 7 | ans += n % k, n /= k; 8 | return ans; 9 | } 10 | }; -------------------------------------------------------------------------------- /1863. Sum of All Subset XOR Totals.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | auto solve(int i, int Xor, vector& nums) 4 | { 5 | if(i == nums.size()) return Xor; 6 | 7 | int pic = solve(i + 1, Xor ^ nums[i], nums); 8 | int not_pic = solve(i + 1, Xor, nums); 9 | return pic + not_pic; 10 | } 11 | int subsetXORSum(vector& nums) 12 | { 13 | return solve(0, 0, nums); 14 | } 15 | }; -------------------------------------------------------------------------------- /1869. Longer Contiguous Segments of Ones than Zeros.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool checkZeroOnes(string s) 4 | { 5 | int count = 1, n = s.size(); 6 | unordered_mapstore; 7 | for(int i = 0; i < n; i++) 8 | { 9 | count += 1; 10 | if(s[i] != s[i + 1]) count = 1; 11 | store[s[i]] = max(store[s[i]], count); 12 | } 13 | return store['1'] > store['0']; 14 | } 15 | }; -------------------------------------------------------------------------------- /1897. Redistribute Characters to Make All Strings Equal.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool makeEqual(vector& words) 4 | { 5 | unordered_map store; 6 | for(auto &word : words) 7 | for(auto &ch : word) store[ch] += 1; 8 | 9 | int n = words.size(); 10 | for(auto [ch, cnt] : store) 11 | if((cnt - n) % n) return false; 12 | return true; 13 | } 14 | }; -------------------------------------------------------------------------------- /190. Reverse Bits.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | uint32_t reverseBits(uint32_t n) 4 | { 5 | string bits = bitset<32>(n).to_string(); 6 | reverse(bits.begin(), bits.end()); 7 | 8 | int ans = stoll(bits, NULL, 2); 9 | return ans; 10 | } 11 | }; -------------------------------------------------------------------------------- /1910. Remove All Occurrences of a Substring.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string removeOccurrences(string s, string part) 4 | { 5 | string store; 6 | int n = s.size(), m = part.size(); 7 | for(int i = 0; i < n; i++) 8 | { 9 | store += s[i]; 10 | if(store.size() < m) continue; 11 | string temp = store.substr(store.size() - m); 12 | if(temp == part) store.erase(store.begin() + store.size() - m, store.end()); 13 | } 14 | return store; 15 | } 16 | }; -------------------------------------------------------------------------------- /1920. Build Array from Permutation.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector buildArray(vector& nums) 4 | { 5 | vector ans; 6 | for(int i = 0; i < nums.size(); i++) 7 | ans.push_back(nums[nums[i]]); 8 | return ans; 9 | } 10 | }; -------------------------------------------------------------------------------- /1920. Build Array from Permutation.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def buildArray(self, nums: List[int]) -> List[int]: 3 | return [nums[index] for index in nums] -------------------------------------------------------------------------------- /1922. Count Good Numbers.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countGoodNumbers(self, n: int) -> int: 3 | mod = 1000000007 4 | even, odd = n / 2, (n / 2) + (n % 2) 5 | 6 | return pow(5, int(odd), mod) * pow(4, int(even), mod) % mod -------------------------------------------------------------------------------- /1957. Delete Characters to Make Fancy String.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string makeFancyString(string s) 4 | { 5 | int n; 6 | string ans; 7 | for(auto &ch:s) 8 | { 9 | n = ans.size(); 10 | if(n > 1 and ans.back() == ch and ans.back() == ans[n - 2]) 11 | { 12 | continue; 13 | } 14 | ans += ch; 15 | } 16 | return ans; 17 | } 18 | }; -------------------------------------------------------------------------------- /1957. Delete Characters to Make Fancy String.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def makeFancyString(self, s: str) -> str: 3 | ans = "" 4 | for ch in s: 5 | if len(ans) > 1 and ch == ans[-1] == ans[-2]: 6 | continue 7 | ans += ch 8 | return ans -------------------------------------------------------------------------------- /1962. Remove Stones to Minimize the Total.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int minStoneSum(vector &piles, int k) 5 | { 6 | priority_queue pq; 7 | for (auto val : piles) 8 | pq.push(val); 9 | while (k--) 10 | { 11 | pq.push(ceil(pq.top() / 2.0)); 12 | pq.pop(); 13 | } 14 | 15 | int ans = 0; 16 | while (!pq.empty()) 17 | { 18 | ans += pq.top(); 19 | pq.pop(); 20 | } 21 | return ans; 22 | } 23 | }; -------------------------------------------------------------------------------- /1963. Minimum Number of Swaps to Make the String Balanced.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int minSwaps(string s) 5 | { 6 | stack st; 7 | int count = 0; 8 | for (char c : s) 9 | { 10 | if (c == '[') 11 | st.push(c); 12 | else 13 | { 14 | if (st.empty()) 15 | count++; 16 | else 17 | st.pop(); 18 | } 19 | } 20 | int ans = count / 2; 21 | if (count % 2) 22 | ans++; 23 | return ans; 24 | } 25 | }; -------------------------------------------------------------------------------- /1963. Minimum Number of Swaps to Make the String Balanced.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minSwaps(self, s: str) -> int: 3 | count = 0 4 | stack = [] 5 | for ch in s: 6 | if ch == '[': 7 | stack.append(ch) 8 | else: 9 | if stack: 10 | stack.pop() 11 | else: 12 | count += 1 13 | return ceil(count / 2) -------------------------------------------------------------------------------- /1975. Maximum Matrix Sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long long maxMatrixSum(vector>& matrix) 4 | { 5 | long long sum = 0; 6 | int maxValue = INT_MAX, neg = 0; 7 | for(auto vec:matrix) 8 | { 9 | for(auto val:vec) 10 | { 11 | sum += abs(val); 12 | if(val < 0) neg += 1; 13 | maxValue = min(maxValue, abs(val)); 14 | } 15 | } 16 | 17 | if(neg % 2) sum -= maxValue * 2; 18 | 19 | return sum; 20 | } 21 | }; -------------------------------------------------------------------------------- /1980. Find Unique Binary String.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findDifferentBinaryString(self, nums: List[str]) -> str: 3 | ans = [''] 4 | n = len(nums) 5 | nums = set(nums) 6 | def backtrack(cur): 7 | if ans[0]: return 8 | if len(cur) == n: 9 | if cur not in nums: 10 | ans[0] = cur 11 | return 12 | backtrack(cur + '0') 13 | backtrack(cur + '1') 14 | 15 | backtrack('') 16 | return ans[0] -------------------------------------------------------------------------------- /1984. Minimum Difference Between Highest and Lowest of K Scores.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int minimumDifference(vector &nums, int k) 5 | { 6 | sort(begin(nums), end(nums)); 7 | 8 | int ans = INT_MAX, n = nums.size(); 9 | for (int i = 0; i <= n - k; i++) 10 | ans = min(ans, (nums[i + k - 1] - nums[i])); 11 | 12 | return ans; 13 | } 14 | }; -------------------------------------------------------------------------------- /2044. Count Number of Maximum Bitwise-OR Subsets.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countMaxOrSubsets(vector& nums) 4 | { 5 | unordered_map count; 6 | int n = nums.size(), size = (1 << n), maxOr = 0;; 7 | for(int i = 0; i < size; i++) 8 | { 9 | int Or = 0; 10 | for(int j = 0; j < n; j++) 11 | if(i & (1 << j)) Or |= nums[j]; 12 | 13 | count[Or] += 1; 14 | maxOr = max(maxOr, Or); 15 | } 16 | return count[maxOr]; 17 | } 18 | }; -------------------------------------------------------------------------------- /2094. Finding 3-Digit Even Numbers.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findEvenNumbers(self, digits: List[int]) -> List[int]: 3 | n = len(digits) 4 | store = SortedList() 5 | for i in range(n): 6 | if not digits[i]: continue 7 | for j in range(n): 8 | if i == j: continue 9 | for k in range(n): 10 | if j == k or digits[k] % 2 or k == i: continue 11 | s = str(digits[i]) + str(digits[j]) + str(digits[k]) 12 | if int(s) not in store: 13 | store.add(int(s)) 14 | return list(store) -------------------------------------------------------------------------------- /2109. Adding Spaces to a String.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shishirRsiam/LeetCode-Solve/147b43842a42f4d94d5594419bd146678e96fa55/2109. Adding Spaces to a String.cpp -------------------------------------------------------------------------------- /2119. A Number After a Double Reversal.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isSameAfterReversals(int num) 4 | { 5 | string reverseNum = to_string(num); 6 | reverse(reverseNum.begin(), reverseNum.end()); 7 | reverseNum = to_string(stoi(reverseNum)); 8 | reverse(reverseNum.begin(), reverseNum.end()); 9 | return num == stoi(reverseNum); 10 | } 11 | }; -------------------------------------------------------------------------------- /2139. Minimum Moves to Reach Target Score.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minMoves(int target, int maxDoubles) 4 | { 5 | int ans = 0; 6 | while(target) 7 | { 8 | if(target % 2) 9 | { 10 | ans += 1, target -= 1; 11 | continue; 12 | } 13 | if(not maxDoubles) break; 14 | 15 | ans += 1, target /= 2; 16 | maxDoubles -= 1; 17 | } 18 | return ans + target - 1; 19 | } 20 | }; -------------------------------------------------------------------------------- /2145. Count the Hidden Sequences.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numberOfArrays(vector& differences, int lower, int upper) 4 | { 5 | long minPref = 0, maxPref = 0, pref = 0; 6 | for(auto &diff : differences) 7 | { 8 | pref += diff; 9 | minPref = min(minPref, pref); 10 | maxPref = max(maxPref, pref); 11 | } 12 | return max((upper - maxPref) - (lower - minPref) + 1, long(0)); 13 | } 14 | }; -------------------------------------------------------------------------------- /2161. Partition Array According to Given Pivot.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector pivotArray(vector& nums, int x) { 4 | vectora; 5 | vectorb; 6 | vectorc; 7 | 8 | for(int v:nums) 9 | { 10 | if(v==x) c.push_back(v); 11 | else if(v < x) a.push_back(v); 12 | else b.push_back(v); 13 | } 14 | 15 | a.insert(a.end(),c.begin(),c.end()); 16 | a.insert(a.end(),b.begin(),b.end()); 17 | return a; 18 | } 19 | }; -------------------------------------------------------------------------------- /2161. Partition Array According to Given Pivot.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def pivotArray(self, nums: List[int], pivot: int) -> List[int]: 3 | less_val, greater_val, equal_val = [], [], [] 4 | 5 | for val in nums: 6 | if val == pivot: equal_val.append(val) 7 | elif val < pivot: less_val.append(val) 8 | else: greater_val.append(val) 9 | 10 | return less_val + equal_val + greater_val -------------------------------------------------------------------------------- /2176. Count Equal and Divisible Pairs in an Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countPairs(vector& nums, int k) 4 | { 5 | int ans = 0, n = nums.size(); 6 | for(int i = 0; i < n - 1; i++) 7 | { 8 | for(int j = i + 1; j < n; j++) 9 | { 10 | ans += (nums[i] == nums[j] and (i * j) % k == 0) 11 | } 12 | } 13 | return ans; 14 | } 15 | }; -------------------------------------------------------------------------------- /2176. Count Equal and Divisible Pairs in an Array.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countPairs(self, nums: List[int], k: int) -> int: 3 | ans, n = 0, len(nums) 4 | for i in range(n): 5 | for j in range(i + 1, n): 6 | ans += not (i * j) % k and nums[i] == nums[j] 7 | return ans -------------------------------------------------------------------------------- /2177. Find Three Consecutive Integers That Sum to a Given Number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string addSpaces(string s, vector& spaces) 4 | { 5 | string ans; 6 | int i = 0, j = 0; 7 | int n = s.size(), m = spaces.size(); 8 | while(j < m) 9 | { 10 | if(i == spaces[j]) 11 | ans += ' ', j++; 12 | ans += s[i++]; 13 | } 14 | while(i < n) ans += s[i++]; 15 | return ans; 16 | } 17 | }; -------------------------------------------------------------------------------- /2177. Find Three Consecutive Integers That Sum to a Given Number.js: -------------------------------------------------------------------------------- 1 | var sumOfThree = function (num) { 2 | if (!(num % 3)) { 3 | let mod = num / 3; 4 | return [mod - 1, mod, mod + 1]; 5 | } 6 | return []; 7 | }; 8 | -------------------------------------------------------------------------------- /2177. Find Three Consecutive Integers That Sum to a Given Number.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def sumOfThree(self, num): 3 | if not num % 3: 4 | mod = num / 3 5 | return [mod - 1, mod, mod + 1] 6 | return [] -------------------------------------------------------------------------------- /2185. Counting Words With a Given Prefix.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int prefixCount(vector& words, string pref) 4 | { 5 | int ans = 0; 6 | for(auto s:words) 7 | if(s.find(pref) == 0) ans += 1; 8 | return ans; 9 | } 10 | }; -------------------------------------------------------------------------------- /2185. Counting Words With a Given Prefix.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def prefixCount(self, words: List[str], pref: str) -> int: 3 | ans = 0 4 | for word in words: 5 | ans += word[:len(pref)] == pref 6 | return ans 7 | 8 | 9 | 10 | class Solution(object): 11 | def prefixCount(self, words, pref): 12 | ans = 0 13 | for s in words: 14 | if s.startswith(pref): 15 | ans += 1 16 | return ans 17 | -------------------------------------------------------------------------------- /2206. Divide Array Into Equal Pairs.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool divideArray(vector& nums) 4 | { 5 | unordered_map store; 6 | for(auto &val : nums) store[val] += 1; 7 | for(auto [val, count] : store) 8 | if(count % 2) return false; 9 | return true; 10 | } 11 | }; -------------------------------------------------------------------------------- /2208. Minimum Operations to Halve Array Sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int halveArray(vector &nums) 5 | { 6 | double target = 0; 7 | priority_queue pq; 8 | for (auto val : nums) 9 | { 10 | target += (val / 2.0); 11 | pq.push(val); 12 | } 13 | 14 | int ans = 0; 15 | while (target > 0) 16 | { 17 | ans += 1; 18 | target -= (pq.top() / 2.0); 19 | pq.push(pq.top() / 2.0); 20 | pq.pop(); 21 | } 22 | return ans; 23 | } 24 | }; -------------------------------------------------------------------------------- /2220. Minimum Bit Flips to Convert Number.js: -------------------------------------------------------------------------------- 1 | var minBitFlips = function (start, goal) { 2 | let xor = start ^ goal; 3 | let ans = 0; 4 | while (xor) { 5 | ans += xor & 1; 6 | xor >>= 1; 7 | } 8 | return ans; 9 | }; 10 | -------------------------------------------------------------------------------- /2220. Minimum Bit Flips to Convert Number.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def minBitFlips(self, start, goal): 3 | return bin(start ^ goal).count('1') -------------------------------------------------------------------------------- /2221. Find Triangular Sum of an Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int triangularSum(vector& nums) 4 | { 5 | int n, val; 6 | vectortemp, arr = nums; 7 | while(arr.size() > 1) 8 | { 9 | n = arr.size(); 10 | for(int i=0;i& words, string s) 4 | { 5 | int ans = 0; 6 | for(auto word : words) 7 | ans += s.substr(0, word.size()) == word; 8 | return ans; 9 | } 10 | }; -------------------------------------------------------------------------------- /2260. Minimum Consecutive Cards to Pick Up.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minimumCardPickup(vector& cards) 4 | { 5 | int ans = INT_MAX; 6 | unordered_map mp; 7 | int i = 0, j = 0, n = cards.size(); 8 | while(j < n) 9 | { 10 | mp[cards[j]]++; 11 | while(mp[cards[j]] == 2) 12 | { 13 | ans = min(ans, j - i + 1); 14 | mp[cards[i++]]--; 15 | } 16 | j++; 17 | } 18 | return ans == INT_MAX ? -1 : ans; 19 | } 20 | }; -------------------------------------------------------------------------------- /2265. Count Nodes Equal to Average of Subtree.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | tuple DFS(TreeNode* root, int &ans) 4 | { 5 | if(not root) return {0, 0}; 6 | auto [leftVal, leftCount] = DFS(root->left, ans); 7 | auto [rightVal, rightCount] = DFS(root->right, ans); 8 | 9 | int count = leftCount + rightCount + 1; 10 | int sum = leftVal + rightVal + root->val; 11 | 12 | ans += ((sum / count) == root->val); 13 | return {sum, count}; 14 | } 15 | int averageOfSubtree(TreeNode* root) 16 | { 17 | int ans = 0; 18 | DFS(root, ans); 19 | return ans; 20 | } 21 | }; -------------------------------------------------------------------------------- /2275. Largest Combination With Bitwise AND Greater Than Zero.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int largestCombination(vector& candidates) 4 | { 5 | unordered_mapcountBits; 6 | for(auto val:candidates) 7 | { 8 | bitset<32>bits(val); 9 | 10 | auto bitStr = bits.to_string(); 11 | for(int i = 0; i < 32; i++) 12 | countBits[i] += bitStr[i] == '1'; 13 | } 14 | 15 | int ans = 0; 16 | for(auto [i, count] : countBits) 17 | ans = max(ans, count); 18 | return ans; 19 | } 20 | }; -------------------------------------------------------------------------------- /2279. Maximum Bags With Full Capacity of Rocks.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maximumBags(vector& capacity, vector& rocks, int additionalRocks) 4 | { 5 | vector needValue; 6 | int ans = 0, n = rocks.size(); 7 | for(int i=0;i& nums, long long k) 4 | { 5 | long long ans = 0, sum = 0; 6 | int i = 0, j = 0, n = nums.size(); 7 | while(j < n) 8 | { 9 | sum += nums[j]; 10 | while(sum * (j - i + 1) >= k) 11 | sum -= nums[i++]; 12 | ans += j - i + 1; 13 | j++; 14 | } 15 | return ans; 16 | } 17 | }; -------------------------------------------------------------------------------- /2302. Count Subarrays With Score Less Than K.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countSubarrays(self, nums: List[int], k: int) -> int: 3 | i, j, sum = 0, 0, 0 4 | ans, n = 0, len(nums) 5 | while j < n: 6 | sum += nums[j] 7 | while (j - i + 1) * sum >= k: 8 | sum -= nums[i] 9 | i += 1 10 | ans += j - i + 1 11 | j += 1 12 | return ans -------------------------------------------------------------------------------- /2315. Count Asterisks.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countAsterisks(string s) 4 | { 5 | int ans = 0, flag = true; 6 | for(char ch:s) 7 | { 8 | if(flag and ch == '*') ans++; 9 | if(ch == '|') flag = !flag; 10 | } 11 | return ans; 12 | } 13 | }; -------------------------------------------------------------------------------- /2317. Maximum XOR After Operations .cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maximumXOR(vector& nums) 4 | { 5 | int ans = 0; 6 | vector added(32); 7 | for(auto &val : nums) 8 | { 9 | bitset<32> bit(val), newbit(0); 10 | for(int i = 0; i < 32; i++) 11 | { 12 | if(bit[i] and not added[i]) 13 | { 14 | newbit[i] = 1; 15 | added[i] = true; 16 | } 17 | } 18 | ans = ans xor newbit.to_ulong(); 19 | } 20 | return ans; 21 | } 22 | }; -------------------------------------------------------------------------------- /2349. Design a Number Container System.cpp: -------------------------------------------------------------------------------- 1 | class NumberContainers { 2 | public: 3 | unordered_map> store; 4 | unordered_map index_filled; 5 | NumberContainers() {} 6 | 7 | void change(int index, int number) 8 | { 9 | if (index_filled.count(index)) 10 | store[index_filled[index]].erase(index); 11 | 12 | store[number].insert(index); 13 | index_filled[index] = number; 14 | } 15 | 16 | int find(int number) 17 | { 18 | if (store[number].empty()) 19 | return -1; 20 | 21 | return *store[number].begin(); 22 | } 23 | }; -------------------------------------------------------------------------------- /2364. Count Number of Bad Pairs.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long long countBadPairs(vector& nums) 4 | { 5 | int n = nums.size(); 6 | unordered_map store; 7 | long ans = (n * long(n - 1)) / 2; 8 | for(int i = n - 1; i >= 0; i--) 9 | { 10 | ans -= store[nums[i] - i]; 11 | store[nums[i] - i] += 1; 12 | } 13 | return ans; 14 | } 15 | }; -------------------------------------------------------------------------------- /2379. Minimum Recolors to Get K Consecutive Black Blocks.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minimumRecolors(string blocks, int k) 4 | { 5 | int cnt = 0, ans = INT_MAX; 6 | int i = 0, j = 0, n = blocks.size(); 7 | while(j < n) 8 | { 9 | if(blocks[j++] == 'W') cnt++; 10 | while(j - i > k) 11 | if(blocks[i++] == 'W') cnt--; 12 | if(j - i == k) ans = min(ans, cnt); 13 | } 14 | return ans; 15 | } 16 | }; -------------------------------------------------------------------------------- /2379. Minimum Recolors to Get K Consecutive Black Blocks.js: -------------------------------------------------------------------------------- 1 | var minimumRecolors = function(blocks, k) 2 | { 3 | let n = blocks.length; 4 | let i = 0, j = 0, ans = k, count = 0; 5 | while(j < n) 6 | { 7 | count += blocks[j++] == 'W'; 8 | if(j - i == k) 9 | { 10 | ans = Math.min(ans, count); 11 | count -= blocks[i++] == 'W'; 12 | } 13 | } 14 | return ans; 15 | }; -------------------------------------------------------------------------------- /2379. Minimum Recolors to Get K Consecutive Black Blocks.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minimumRecolors(self, blocks: str, k: int) -> int: 3 | cnt = 0 4 | ans, i, j, n, = sys.maxsize, 0, 0, len(blocks) 5 | while j < n: 6 | if blocks[j] == 'W': 7 | cnt += 1 8 | if j - i + 1 == k: 9 | ans = min(ans, cnt) 10 | if blocks[i] == 'W': 11 | cnt -= 1 12 | i += 1 13 | j += 1 14 | return ans -------------------------------------------------------------------------------- /2414. Length of the Longest Alphabetical Continuous Substring.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int longestContinuousSubstring(string s) 5 | { 6 | int ans = 1, cnt = 1, n = s.size(); 7 | for (int i = 1; i < n; i++) 8 | { 9 | if (s[i] == s[i - 1] + 1) 10 | cnt++; 11 | else 12 | cnt = 1; 13 | ans = max(ans, cnt); 14 | } 15 | return ans; 16 | } 17 | }; -------------------------------------------------------------------------------- /2414. Length of the Longest Alphabetical Continuous Substring.js: -------------------------------------------------------------------------------- 1 | var longestContinuousSubstring = function (s) { 2 | let n = s.length; 3 | let ans = 1, 4 | cnt = 1; 5 | for (let i = 1; i < n; i++) { 6 | if (s[i].charCodeAt() - 1 == s[i - 1].charCodeAt()) cnt += 1; 7 | else cnt = 1; 8 | ans = Math.max(ans, cnt); 9 | } 10 | return ans; 11 | }; 12 | -------------------------------------------------------------------------------- /2414. Length of the Longest Alphabetical Continuous Substring.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def longestContinuousSubstring(self, s): 3 | n = len(s) 4 | ans, cnt = 1, 1 5 | for i in range(1, n): 6 | if ord(s[i]) == ord(s[i - 1]) + 1: 7 | cnt += 1 8 | else: 9 | cnt = 1 10 | ans = max(ans, cnt) 11 | return ans -------------------------------------------------------------------------------- /2415. Reverse Odd Levels of Binary Tree.cpp: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | public: 4 | void swapNode(TreeNode* p, TreeNode* q, int level) 5 | { 6 | if(q == NULL or q == NULL) return; 7 | 8 | if(level % 2 == 1) swap(p->val, q->val); 9 | 10 | swapNode(p->left, q->right, level+1); 11 | swapNode(p->right, q->left, level+1); 12 | } 13 | TreeNode* reverseOddLevels(TreeNode* root) 14 | { 15 | swapNode(root->left, root->right,1); 16 | return root; 17 | } 18 | }; -------------------------------------------------------------------------------- /2415. Reverse Odd Levels of Binary Tree.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def reverseOddLevels(self, root: Optional[TreeNode]) -> Optional[TreeNode]: 3 | def swapNode(node1, node2, depth): 4 | if not node1 or not node2: return 5 | if depth % 2: 6 | node1.val, node2.val = node2.val, node1.val 7 | swapNode(node1.left, node2.right, depth + 1) 8 | swapNode(node1.right, node2.left, depth + 1) 9 | 10 | swapNode(root.left, root.right, 1) 11 | return root -------------------------------------------------------------------------------- /2419. Longest Subarray With Maximum Bitwise AND.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int longestSubarray(vector &nums) 5 | { 6 | int maxElement = *max_element(begin(nums), end(nums)); 7 | 8 | int ans = 1, cnt = 0; 9 | for (auto val : nums) 10 | { 11 | if (val == maxElement) 12 | cnt += 1; 13 | else 14 | cnt = 0; 15 | ans = max(ans, cnt); 16 | } 17 | return ans; 18 | } 19 | }; -------------------------------------------------------------------------------- /2419. Longest Subarray With Maximum Bitwise AND.js: -------------------------------------------------------------------------------- 1 | var longestSubarray = function (nums) { 2 | let ans = 0, 3 | cnt = 0; 4 | let max_element = Math.max(...nums); 5 | for (let num of nums) { 6 | if (num == max_element) cnt += 1; 7 | else cnt = 0; 8 | ans = Math.max(ans, cnt); 9 | } 10 | return ans; 11 | }; 12 | -------------------------------------------------------------------------------- /2419. Longest Subarray With Maximum Bitwise AND.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def longestSubarray(self, nums): 3 | ans, cnt = 0, 0 4 | 5 | max_element = max(nums) 6 | for num in nums: 7 | if num == max_element: 8 | cnt += 1 9 | else: 10 | cnt = 0 11 | ans = max(ans, cnt) 12 | return ans -------------------------------------------------------------------------------- /2425. Bitwise XOR of All Pairings.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int xorAllNums(vector& nums1, vector& nums2) 4 | { 5 | int Xor1 = 0, Xor2 = 0; 6 | for(auto &val : nums1) Xor1 = Xor1 xor val; 7 | for(auto &val : nums2) Xor2 = Xor2 xor val; 8 | 9 | int ans = 0; 10 | if(nums2.size() % 2) ans ^= Xor1; 11 | if(nums1.size() % 2) ans ^= Xor2; 12 | return ans; 13 | } 14 | }; -------------------------------------------------------------------------------- /2433. Find The Original Array of Prefix Xor.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | vector findArray(vector &pref) 5 | { 6 | int n = pref.size(); 7 | vector ans(n); 8 | ans[0] = pref[0]; 9 | for (int i = 1; i < n; i++) 10 | ans[i] = pref[i] ^ pref[i - 1]; 11 | return ans; 12 | } 13 | }; -------------------------------------------------------------------------------- /2433. Find The Original Array of Prefix Xor.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findArray(self, pref: List[int]) -> List[int]: 3 | n = len(pref) 4 | ans = [0] * n 5 | ans[0] = pref[0] 6 | for i in range(1, n): 7 | ans[i] = pref[i] ^ pref[i - 1] 8 | return ans -------------------------------------------------------------------------------- /2444. Count Subarrays With Fixed Bounds.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long long countSubarrays(vector& nums, int minK, int maxK) 4 | { 5 | long long ans = 0; 6 | int minpos = -1, maxpos = -1, unbound = -1, n = nums.size(); 7 | for(int i = 0; i < n; i++) 8 | { 9 | if(nums[i] < minK or nums[i] > maxK) unbound = i; 10 | if(nums[i]==minK) minpos = i; 11 | if(nums[i]==maxK) maxpos = i; 12 | 13 | int canadd = min(minpos, maxpos) - unbound; 14 | if(canadd > 0) ans += canadd; 15 | } 16 | return ans; 17 | } 18 | }; -------------------------------------------------------------------------------- /2444. Count Subarrays With Fixed Bounds.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countSubarrays(self, nums: List[int], minK: int, maxK: int) -> int: 3 | minpos, maxpos, unbound = -1, -1, -1 4 | 5 | ans, n = 0, len(nums) 6 | for i in range(n): 7 | if nums[i] < minK or nums[i] > maxK: 8 | unbound = i 9 | if nums[i] == minK: 10 | minpos = i 11 | if nums[i] == maxK: 12 | maxpos = i 13 | 14 | canadd = min(minpos, maxpos) - unbound 15 | if canadd > 0: 16 | ans += canadd 17 | 18 | return ans -------------------------------------------------------------------------------- /2455. Average Value of Even Numbers That Are Divisible by Three.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int averageValue(vector& nums) 4 | { 5 | int count = 0, sum = 0; 6 | for(auto &val : nums) 7 | if(not(val % 2) and not(val % 3)) 8 | sum += val, count += 1; 9 | return count ? sum / count : count; 10 | } 11 | }; -------------------------------------------------------------------------------- /2460. Apply Operations to an Array.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def applyOperations(self, nums: List[int]) -> List[int]: 3 | for i in range(len(nums) - 1): 4 | if nums[i] == nums[i + 1]: 5 | nums[i + 1] = 0 6 | nums[i] *= 2 7 | 8 | ans = [] 9 | count_zero = 0 10 | for num in nums: 11 | if num: ans.append(num) 12 | else: count_zero += 1 13 | 14 | while count_zero: 15 | ans.append(0) 16 | count_zero -= 1 17 | 18 | return ans -------------------------------------------------------------------------------- /2481. Minimum Cuts to Divide a Circle.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numberOfCuts(int n) 4 | { 5 | return (n > 1 and n % 2) ? n : n / 2; 6 | } 7 | }; -------------------------------------------------------------------------------- /2490. Circular Sentence.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isCircularSentence(string s) 4 | { 5 | if(s[0] != s.back()) return false; 6 | 7 | int n = s.size(); 8 | for(int i = 1; i < n - 1; i++) 9 | { 10 | if(s[i] == ' ') 11 | { 12 | if(s[i - 1] != s[i + 1]) 13 | return false; 14 | } 15 | } 16 | return true; 17 | } 18 | }; -------------------------------------------------------------------------------- /2491. Divide Players Into Teams of Equal Skill.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | long long dividePlayers(vector &skill) 5 | { 6 | int n = skill.size(); 7 | sort(begin(skill), end(skill)); 8 | long ans = 0, target = skill[0] + skill[n - 1], val1, val2; 9 | 10 | int i = 0, j = n - 1; 11 | while (i < j) 12 | { 13 | val1 = skill[i++], val2 = skill[j--]; 14 | if (val1 + val2 != target) 15 | return -1; 16 | ans += (val1 * val2); 17 | } 18 | return ans; 19 | } 20 | }; -------------------------------------------------------------------------------- /2491. Divide Players Into Teams of Equal Skill.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def dividePlayers(self, skill: List[int]) -> int: 3 | 4 | skill.sort() 5 | ans, i, j = 0, 0, len(skill) - 1 6 | target = skill[0] + skill[-1] 7 | 8 | while i < j: 9 | if skill[i] + skill[j] != target: 10 | return -1 11 | 12 | ans += (skill[i] * skill[j]) 13 | i += 1 14 | j -= 1 15 | 16 | return ans -------------------------------------------------------------------------------- /2530. Maximal Score After Applying K Operations.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | long long maxKelements(vector &nums, int k) 5 | { 6 | long ans = 0, val; 7 | priority_queue pq; 8 | for (auto val : nums) 9 | pq.push(val); 10 | while (k--) 11 | { 12 | val = pq.top(); 13 | pq.pop(); 14 | ans += val; 15 | pq.push(ceil(val / 3.0)); 16 | } 17 | return ans; 18 | } 19 | }; -------------------------------------------------------------------------------- /2537. Count the Number of Good Subarrays.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long long countGood(vector& nums, int k) 4 | { 5 | long ans = 0, pairs = 0; 6 | unordered_map count; 7 | int i = 0, j = 0, n = nums.size(); 8 | while(j < n) 9 | { 10 | pairs += count[nums[j]]; 11 | count[nums[j++]] += 1; 12 | while(pairs >= k) 13 | { 14 | ans += (n - j) + 1; 15 | pairs -= --count[nums[i++]]; 16 | } 17 | } 18 | return ans; 19 | } 20 | }; -------------------------------------------------------------------------------- /2554. Maximum Number of Integers to Choose From a Range I.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxCount(vector& banned, int n, int maxSum) 4 | { 5 | long ans = 0, sum = 0; 6 | unordered_setban(begin(banned), end(banned)); 7 | for(int i = 1; i <= n; i++) 8 | { 9 | if(ban.count(i)) continue; 10 | if(maxSum < sum + i) break; 11 | 12 | ans += 1, sum += i; 13 | } 14 | return ans; 15 | } 16 | }; -------------------------------------------------------------------------------- /2554. Maximum Number of Integers to Choose From a Range I.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxCount(self, banned: List[int], n: int, maxSum: int) -> int: 3 | ans, curSum = 0, 0 4 | banned = set(banned) 5 | for num in range(1, n + 1): 6 | if maxSum < curSum + num: break 7 | if num in banned: continue 8 | 9 | ans += 1 10 | curSum += num 11 | return ans -------------------------------------------------------------------------------- /2558. Take Gifts From the Richest Pile.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | long long pickGifts(vector &gifts, int k) 5 | { 6 | priority_queue pq; 7 | for (auto val : gifts) 8 | pq.push(val); 9 | 10 | while (k--) 11 | { 12 | pq.push(sqrt(pq.top())); 13 | pq.pop(); 14 | } 15 | 16 | long ans = 0; 17 | while (!pq.empty()) 18 | { 19 | ans += pq.top(); 20 | pq.pop(); 21 | } 22 | return ans; 23 | } 24 | }; -------------------------------------------------------------------------------- /2558. Take Gifts From the Richest Pile.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def pickGifts(self, gifts: List[int], k: int) -> int: 3 | Heap = [-gift for gift in gifts] 4 | heapify(Heap) 5 | for _ in range(k): 6 | top = -heappop(Heap) 7 | heappush(Heap, -int(math.sqrt(top))) 8 | return -sum(Heap) -------------------------------------------------------------------------------- /2559. Count Vowel Strings in Ranges.py: -------------------------------------------------------------------------------- 1 | 2 | class Solution: 3 | def vowelStrings(self, words: List[str], queries: List[List[int]]) -> List[int]: 4 | def is_vowel(char): 5 | return char.lower() in "aeiou" 6 | pref = [0] 7 | for word in words: 8 | pref.append(pref[-1] + (is_vowel(word[0]) and is_vowel(word[-1]))) 9 | 10 | ans = [] 11 | for left, right in queries: 12 | ans.append(pref[right + 1] - pref[left]) 13 | return ans -------------------------------------------------------------------------------- /2563. Count the Number of Fair Pairs.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countFairPairs(self, nums: List[int], lower: int, upper: int) -> int: 3 | ans = 0 4 | nums.sort() 5 | pbds = SortedList() 6 | for val in nums: 7 | min_val, max_val = lower - val, upper - val 8 | ans += pbds.bisect_right(max_val) - pbds.bisect_left(min_val) 9 | pbds.add(val) 10 | return ans -------------------------------------------------------------------------------- /2568. Minimum Impossible OR.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minImpossibleOR(vector& nums) 4 | { 5 | int ImpossibleOR = 1; 6 | unordered_set store(begin(nums), end(nums)); 7 | while(store.count(ImpossibleOR)) ImpossibleOR += ImpossibleOR; 8 | return ImpossibleOR; 9 | } 10 | }; -------------------------------------------------------------------------------- /2570. Merge Two 2D Arrays by Summing Values.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> mergeArrays(vector>& a, vector>& b) 4 | { 5 | mapmp; 6 | for(auto x:a) mp[x[0]] += x[1]; 7 | for(auto x:b) mp[x[0]] += x[1]; 8 | 9 | vector> ans; 10 | for(auto x:mp) ans.push_back({x.first,x.second}); 11 | return ans; 12 | } 13 | }; -------------------------------------------------------------------------------- /2570. Merge Two 2D Arrays by Summing Values.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]: 3 | store = defaultdict(int) 4 | for id, val in nums1: 5 | store[id] += val 6 | for id, val in nums2: 7 | store[id] += val 8 | 9 | ans = [] 10 | for id, val in store.items(): 11 | ans.append([id, val]) 12 | ans.sort() 13 | return ans -------------------------------------------------------------------------------- /2579. Count Total Number of Colored Cells.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long long coloredCells(int n) 4 | { 5 | long ans = 1, color = 4; 6 | for(int i = 1; i < n; i++) 7 | ans += color, color += 4; 8 | return ans; 9 | } 10 | }; -------------------------------------------------------------------------------- /2579. Count Total Number of Colored Cells.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def coloredCells(self, n: int) -> int: 3 | ans, color = 1, 4 4 | for i in range(1, n): 5 | ans += color 6 | color += 4 7 | return ans -------------------------------------------------------------------------------- /2609. Find the Longest Balanced Substring of a Binary String.cpp: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findTheLongestBalancedSubstring(self, s: str) -> int: 3 | ans = 0 4 | for i in range(1, 26): 5 | balanced_binary = '0' * i + '1' * i 6 | if balanced_binary not in s: break 7 | ans = i + i 8 | return ans -------------------------------------------------------------------------------- /2657. Find the Prefix Common Array of Two Arrays.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector findThePrefixCommonArray(vector& A, vector& B) 4 | { 5 | int n = A.size(); 6 | vector C(n), count(51); 7 | for(int i = 0; i < n; i++) 8 | { 9 | count[A[i]] += 1, count[B[i]] += 1; 10 | if(i == 0) C[i] = A[i] == B[i]; 11 | else 12 | { 13 | C[i] = C[i - 1]; 14 | if(A[i] == B[i]) C[i] += 1; 15 | else C[i] += (count[A[i]] == 2) + (count[B[i]] == 2); 16 | } 17 | } 18 | return C; 19 | } 20 | }; -------------------------------------------------------------------------------- /2683. Neighboring Bitwise XOR.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool doesValidArrayExist(vector& derived) 4 | { 5 | int count = 0; 6 | for(int &bit : derived) count += bit; 7 | return count % 2 == 0; 8 | } 9 | }; -------------------------------------------------------------------------------- /2683. Neighboring Bitwise XOR.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def doesValidArrayExist(self, derived: List[int]) -> bool: 3 | return derived.count(1) % 2 == 0 -------------------------------------------------------------------------------- /2762. Continuous Subarrays.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long long continuousSubarrays(vector& nums) 4 | { 5 | long ans = 0; 6 | multiset ml; 7 | int n = nums.size(), i = 0, j = 0; 8 | 9 | while(j < n) 10 | { 11 | ml.insert(nums[j++]); 12 | while(2 < *ml.rbegin() - *ml.begin()) 13 | { 14 | auto it = ml.find(nums[i++]); 15 | ml.erase(it); 16 | } 17 | ans += (j - i); 18 | } 19 | 20 | return ans; 21 | } 22 | }; -------------------------------------------------------------------------------- /2769. Find the Maximum Achievable Number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int theMaximumAchievableX(int num, int t) 4 | { 5 | return num + t + t; 6 | } 7 | }; -------------------------------------------------------------------------------- /2779. Maximum Beauty of an Array After Applying Operation.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maximumBeauty(vector& nums, int k) 4 | { 5 | sort(begin(nums), end(nums)); 6 | int i = 0, j = 0, ans = 0, n = nums.size(); 7 | while(j < n) 8 | { 9 | while(nums[j] - nums[i] > k + k) i++; 10 | ans = max(ans, j - i + 1); 11 | j++; 12 | } 13 | return ans; 14 | } 15 | }; -------------------------------------------------------------------------------- /2825. Make String a Subsequence Using Cyclic Increments.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void convertChar(char& ch) 4 | { 5 | if(ch == 'z') ch = 'a'; 6 | else ch += 1; 7 | } 8 | bool canMakeSubsequence(string str1, string str2) 9 | { 10 | int idx = 0; 11 | string subStr; 12 | for(auto ch : str1) 13 | { 14 | if(ch == str2[idx]) 15 | { 16 | subStr += str2[idx++]; 17 | continue; 18 | } 19 | 20 | convertChar(ch); 21 | if(ch == str2[idx]) subStr += str2[idx++]; 22 | } 23 | return str2 == subStr; 24 | } 25 | }; -------------------------------------------------------------------------------- /2825. Make String a Subsequence Using Cyclic Increments.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def canMakeSubsequence(self, str1: str, str2: str) -> bool: 3 | idx, n = 0, len(str2) 4 | 5 | for ch in str1: 6 | if idx == n: break 7 | 8 | if ch == str2[idx]: 9 | idx += 1 10 | continue 11 | 12 | next_ch = 'a' if ch == 'z' else chr(ord(ch) + 1) 13 | if next_ch == str2[idx]: 14 | idx += 1 15 | 16 | return idx == n -------------------------------------------------------------------------------- /2843. Count Symmetric Integers.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countSymmetricIntegers(int low, int high) 4 | { 5 | int ans = 0; 6 | for(int num = low; num <= high; num++) 7 | { 8 | 9 | auto s = to_string(num); 10 | int leftSum = 0, rightSum = 0, n = s.size(); 11 | if(n % 2) continue; 12 | for(int i = 0; i < n / 2; i++) 13 | { 14 | leftSum += s[i]; 15 | rightSum += s[n - i - 1]; 16 | } 17 | ans += leftSum == rightSum; 18 | } 19 | return ans; 20 | } 21 | }; -------------------------------------------------------------------------------- /2873. Maximum Value of an Ordered Triplet I.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long long maximumTripletValue(vector& nums) 4 | { 5 | long ans = 0, n = nums.size(); 6 | for(int i = 0; i < n; i++) 7 | { 8 | for(int j = i + 1; j < n; j++) 9 | { 10 | for(int k = j + 1; k < n; k++) 11 | { 12 | ans = max(ans, long(nums[i] - nums[j]) * nums[k]); 13 | } 14 | } 15 | } 16 | return ans; 17 | } 18 | }; -------------------------------------------------------------------------------- /2894. Divisible and Non-divisible Sums Difference.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int differenceOfSums(int n, int m) 4 | { 5 | int sum1 = 0, sum2 = 0; 6 | for(int num = 1; num <= n; num++) 7 | num % m ? sum1 += num : sum2 += num; 8 | return sum1 - sum2; 9 | } 10 | }; -------------------------------------------------------------------------------- /2894. Divisible and Non-divisible Sums Difference.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def differenceOfSums(self, n: int, m: int) -> int: 3 | num1 = [num for num in range(1, n + 1) if num % m] 4 | num2 = [num for num in range(1, n + 1) if not num % m] 5 | 6 | return sum(num1) - sum(num2) -------------------------------------------------------------------------------- /2900. Longest Unequal Adjacent Groups Subsequence I.py: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector getLongestSubsequence(vector& words, vector& groups) 4 | { 5 | vector ans; 6 | int i = 0, n = words.size(); 7 | while(i < n) 8 | { 9 | ans.push_back(words[i]); 10 | while(i < n - 1 and groups[i] == groups[i + 1]) 11 | i++; 12 | i++; 13 | } 14 | return ans; 15 | } 16 | }; -------------------------------------------------------------------------------- /2908. Minimum Sum of Mountain Triplets I.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minimumSum(vector& nums) 4 | { 5 | int ans = INT_MAX, n = nums.size(); 6 | for(int i = 0; i < n; i++) 7 | { 8 | for(int j = i + 1; j < n; j++) 9 | { 10 | for(int k = j + 1; k < n; k++) 11 | { 12 | if(nums[i] < nums[j] and nums[k] < nums[j]) 13 | { 14 | ans = min(ans, nums[i] + nums[j] + nums[k]); 15 | } 16 | } 17 | } 18 | } 19 | return ans == INT_MAX ? -1 : ans; 20 | } 21 | }; -------------------------------------------------------------------------------- /2913. Subarrays Distinct Element Sum of Squares I.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int sumCounts(vector& nums) 4 | { 5 | int ans = 0, n = nums.size(); 6 | for(int i = 0; i < n; i++) 7 | { 8 | for(int j = i; j < n; j++) 9 | { 10 | unordered_set store; 11 | for(int k = i; k <= j; k++) 12 | { 13 | store.insert(nums[k]); 14 | } 15 | ans += store.size() * store.size(); 16 | } 17 | } 18 | return ans; 19 | } 20 | }; -------------------------------------------------------------------------------- /2914. Minimum Number of Changes to Make Binary String Beautiful.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minChanges(string s) 4 | { 5 | int ans = 0, n = s.size(); 6 | for(int i = 0; i < n; i += 2) 7 | if(s[i] != s[i + 1]) 8 | ans += 1; 9 | 10 | return ans; 11 | } 12 | }; -------------------------------------------------------------------------------- /2914. Minimum Number of Changes to Make Binary String Beautiful.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minChanges(self, s: str) -> int: 3 | ans, n = 0, len(s) 4 | for i in range(0, n, 2): 5 | if s[i] != s[i + 1]: 6 | ans += 1 7 | return ans -------------------------------------------------------------------------------- /292. Nim Game.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | bool canWinNim(int n) 5 | { 6 | return n % 4; 7 | } 8 | }; -------------------------------------------------------------------------------- /2929. Distribute Candies Among Children II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long long distributeCandies(int n, int limit) 4 | { 5 | long ans = 0; 6 | for(int i = 0; i <= limit; i++) 7 | { 8 | long j = n - i; 9 | if(limit + limit < j or j < 0) continue; 10 | 11 | ans += 1; 12 | if(j <= limit) ans += j; 13 | else ans += limit - (j - limit); 14 | } 15 | return ans; 16 | } 17 | }; -------------------------------------------------------------------------------- /2932. Maximum Strong Pair XOR I.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int maximumStrongPairXor(vector &nums) 5 | { 6 | int n = nums.size(), ans = 0; 7 | for (int i = 0; i < n; i++) 8 | { 9 | for (int j = i + 1; j < n; j++) 10 | { 11 | if (abs(nums[i] - nums[j]) > min(nums[i], nums[j])) 12 | continue; 13 | ans = max(ans, (nums[i] ^ nums[j])); 14 | } 15 | } 16 | return ans; 17 | } 18 | }; -------------------------------------------------------------------------------- /2937. Make Three Strings Equal.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findMinimumOperations(string s1, string s2, string s3) 4 | { 5 | int allSizeSum = s1.size() + s2.size() + s3.size(); 6 | int ans = allSizeSum, n = min({s1.size(), s2.size(), s3.size()}); 7 | for(int i = 0; i < n; i++) 8 | { 9 | if(s1[i] == s2[i] and s1[i] == s3[i]) ans -= 3; 10 | else break; 11 | } 12 | return ans == allSizeSum ? -1 : ans; 13 | } 14 | }; -------------------------------------------------------------------------------- /2938. Separate Black and White Balls.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | long long minimumSteps(string s) 5 | { 6 | long long ans = 0, one = 0; 7 | for (char ch : s) 8 | ch == '1' ? one++ : ans += one; 9 | return ans; 10 | } 11 | }; -------------------------------------------------------------------------------- /2938. Separate Black and White Balls.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minimumSteps(self, s: str) -> int: 3 | ans, count = 0, 0 4 | for ch in s: 5 | count += ch == '1' 6 | if ch == '0': 7 | ans += count 8 | return ans -------------------------------------------------------------------------------- /2942. Find Words Containing Character.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector findWordsContaining(vector& words, char x) 4 | { 5 | vector ans; 6 | int n = words.size(); 7 | for(int i = 0; i < n; i++) 8 | { 9 | unordered_setst(begin(words[i]), end(words[i])); 10 | if(st.count(x)) ans.push_back(i); 11 | } 12 | return ans; 13 | } 14 | }; -------------------------------------------------------------------------------- /2942. Find Words Containing Character.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findWordsContaining(self, words: List[str], x: str) -> List[int]: 3 | return [i for i in range(len(words)) if x in words[i]] -------------------------------------------------------------------------------- /2960. Count Tested Devices After Test Operations.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countTestedDevices(vector& batteryPercentages) 4 | { 5 | int ans = 0; 6 | for(auto val : batteryPercentages) 7 | ans += ans < val; 8 | return ans; 9 | } 10 | }; -------------------------------------------------------------------------------- /2962. Count Subarrays Where Max Element Appears at Least K Times.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long long countSubarrays(vector& nums, int k) 4 | { 5 | int n = nums.size(); 6 | unordered_map count; 7 | int maxVal = *max_element(nums.begin(), nums.end()); 8 | 9 | long long ans = 0, i = 0, j = 0; 10 | while(j < n) 11 | { 12 | count[nums[j]]++; 13 | while(count[maxVal] == k and i <= j) 14 | { 15 | ans += n - j; 16 | count[nums[i++]]--; 17 | } 18 | j++; 19 | } 20 | return ans; 21 | } 22 | }; -------------------------------------------------------------------------------- /2962. Count Subarrays Where Max Element Appears at Least K Times.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countSubarrays(self, nums: List[int], k: int) -> int: 3 | ans, n = 0, len(nums) 4 | maxVal = max(nums) 5 | 6 | i, j = 0, 0 7 | count = Counter() 8 | while j < n: 9 | count[nums[j]] += 1 10 | while count[maxVal] == k: 11 | count[nums[i]] -= 1 12 | i += 1 13 | ans += i 14 | j += 1 15 | return ans -------------------------------------------------------------------------------- /2965. Find Missing and Repeated Values.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findMissingAndRepeatedValues(self, grid: List[List[int]]) -> List[int]: 3 | ans = [0, 0] 4 | store = defaultdict(int) 5 | for row in grid: 6 | for val in row: 7 | store[val] += 1 8 | 9 | val = len(grid) * len(grid) 10 | while val: 11 | if store[val] == 0: 12 | ans[1] = val 13 | elif store[val] == 2: 14 | ans[0] = val 15 | val -= 1 16 | return ans -------------------------------------------------------------------------------- /303. Range Sum Query - Immutable.cpp: -------------------------------------------------------------------------------- 1 | class NumArray { 2 | public: 3 | vectorpref; 4 | NumArray(vector& nums) 5 | { 6 | pref.push_back(0); 7 | for(auto val:nums) 8 | pref.push_back(pref.back() + val);; 9 | } 10 | 11 | int sumRange(int left, int right) { 12 | return pref[right + 1] - pref[left]; 13 | } 14 | }; 15 | 16 | /** 17 | * Your NumArray object will be instantiated and called as such: 18 | * NumArray* obj = new NumArray(nums); 19 | * int param_1 = obj->sumRange(left,right); 20 | */ -------------------------------------------------------------------------------- /3038. Maximum Number of Operations With the Same Score I.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int maxOperations(vector &nums) 5 | { 6 | int ans = 0, n = nums.size(); 7 | if (n < 2) 8 | return ans; 9 | 10 | int sum = nums[0] + nums[1]; 11 | deque dq(begin(nums), end(nums)); 12 | while (dq.size() > 1 and dq[0] + dq[1] == sum) 13 | { 14 | ans += 1; 15 | dq.pop_front(); 16 | dq.pop_front(); 17 | } 18 | return ans; 19 | } 20 | }; -------------------------------------------------------------------------------- /3042. Count Prefix and Suffix Pairs I.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPrefixAndSuffix(string s1, string s2) 4 | { 5 | if(s1.size() > s2.size()) return false; 6 | bool isPrefix = s2.substr(0, s1.size()) == s1; 7 | bool isSuffix = s2.substr(s2.size() - s1.size()) == s1; 8 | return isPrefix and isSuffix; 9 | } 10 | int countPrefixSuffixPairs(vector& words) 11 | { 12 | int ans = 0, n = words.size(); 13 | for(int i = 0; i < n; i++) 14 | for(int j = i + 1; j < n; j++) 15 | ans += isPrefixAndSuffix(words[i], words[j]); 16 | return ans; 17 | } 18 | }; -------------------------------------------------------------------------------- /3066. Minimum Operations to Exceed Threshold Value II.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minOperations(self, nums: List[int], k: int) -> int: 3 | ans = 0 4 | arr = SortedList(nums) 5 | while len(arr) > 1 and k > arr[0]: 6 | ans += 1 7 | arr.add((arr[0] * 2) + arr[1]) 8 | arr.discard(arr[1]) 9 | arr.discard(arr[0]) 10 | return ans -------------------------------------------------------------------------------- /3068. Find the Maximum Sum of Node Values.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long long maximumValueSum(vector& nums, int k, vector>& edges) 4 | { 5 | long ans = 0, count = 0, minXor = INT_MAX; 6 | for(auto &val : nums) 7 | { 8 | if(val < (val xor k)) 9 | { 10 | count += 1; 11 | ans += (val xor k); 12 | } 13 | else ans += val; 14 | minXor = min(minXor, long(abs(val - (val xor k)))); 15 | } 16 | if(count % 2) ans -= minXor; 17 | return ans; 18 | } 19 | }; -------------------------------------------------------------------------------- /3105. Longest Strictly Increasing or Strictly Decreasing Subarray.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def longestMonotonicSubarray(self, nums: List[int]) -> int: 3 | ans, count, n = 1, 1, len(nums) 4 | for i in range(1, n): 5 | if nums[i] <= nums[i - 1]: 6 | count = 0 7 | count += 1 8 | ans = max(ans, count) 9 | 10 | count = 1 11 | for i in range(1, n): 12 | if nums[i] >= nums[i - 1]: 13 | count = 0 14 | count += 1 15 | ans = max(ans, count) 16 | 17 | return ans -------------------------------------------------------------------------------- /3133. Minimum Array End.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long long minEnd(int n, int x) 4 | { 5 | long ans = x; 6 | for(int _ = 1; _ < n; _++) 7 | ans = ((ans + 1) | x); 8 | return ans; 9 | } 10 | }; -------------------------------------------------------------------------------- /3151. Special Array I.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isArraySpecial(vector& nums) 4 | { 5 | int n = nums.size(); 6 | for(int i=0;i bool: 3 | n = len(nums) 4 | for i in range(1, n): 5 | if nums[i] % 2 == nums[i - 1] % 2: 6 | return False 7 | return True -------------------------------------------------------------------------------- /3152. Special Array II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector isArraySpecial(vector& nums, vector>& queries) 4 | { 5 | int n = nums.size(); 6 | set store; 7 | for(int i = 1; i < n; i++) 8 | if((nums[i] % 2) == nums[i - 1] % 2) 9 | store.insert(i); 10 | 11 | vector ans; 12 | for(auto querie : queries) 13 | { 14 | int left = querie[0], right = querie[1]; 15 | 16 | auto it = store.upper_bound(left); 17 | ans.push_back((*it <= right and it != store.end()) ? 0 : 1); 18 | } 19 | 20 | return ans; 21 | } 22 | }; -------------------------------------------------------------------------------- /3152. Special Array II.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isArraySpecial(self, nums: List[int], queries: List[List[int]]) -> List[bool]: 3 | n = len(nums) 4 | store, ans = [], [] 5 | 6 | for i in range(1, n): 7 | if (nums[i] % 2) == (nums[i - 1] % 2): 8 | store.append(i) 9 | 10 | for left, right in queries: 11 | idx = bisect_right(store, left) 12 | 13 | if idx < len(store) and store[idx] <= right: ans.append(False) 14 | else: ans.append(True) 15 | 16 | return ans -------------------------------------------------------------------------------- /3158. Find the XOR of Numbers Which Appear Twice.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int duplicateNumbersXOR(vector &nums) 5 | { 6 | vector store, count(100); 7 | for (auto val : nums) 8 | { 9 | count[val]++; 10 | if (count[val] == 2) 11 | store.push_back(val); 12 | } 13 | int ans = 0; 14 | for (auto val : store) 15 | ans ^= val; 16 | return ans; 17 | } 18 | }; -------------------------------------------------------------------------------- /3163. String Compression III.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string compressedString(string word) 4 | { 5 | string ans; 6 | 7 | int count = 0; 8 | char prev = word[0]; 9 | for(auto ch : word) 10 | { 11 | if(prev != ch or count == 9) 12 | { 13 | ans += to_string(count); 14 | ans += prev; 15 | prev = ch; 16 | count = 0; 17 | } 18 | count += 1; 19 | } 20 | if(count) 21 | { 22 | ans += to_string(count); 23 | ans += prev; 24 | } 25 | return ans; 26 | } 27 | }; -------------------------------------------------------------------------------- /3163. String Compression III.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def compressedString(self, word: str) -> str: 3 | count, ans, prev = 0, "", word[0] 4 | for ch in word: 5 | if ch != prev or count == 9: 6 | ans += str(count) + prev 7 | prev = ch 8 | count = 0 9 | count += 1 10 | 11 | if count: 12 | ans += str(count) + prev 13 | 14 | return ans -------------------------------------------------------------------------------- /3169. Count Days Without Meetings.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countDays(self, days: int, meetings: List[List[int]]) -> int: 3 | meetings.sort() 4 | ans, last_meetings = 0, 0 5 | for start, end in meetings: 6 | if start > last_meetings: 7 | ans += start - last_meetings - 1 8 | last_meetings = max(last_meetings, end) 9 | 10 | return ans + (days - last_meetings) -------------------------------------------------------------------------------- /3174. Clear Digits.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string clearDigits(string s) 4 | { 5 | string ans; 6 | for(char ch:s) 7 | { 8 | if(isalpha(ch)) ans += ch; 9 | else if(ans.size()) ans.pop_back(); 10 | } 11 | return ans; 12 | } 13 | }; -------------------------------------------------------------------------------- /3174. Clear Digits.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def clearDigits(self, s: str) -> str: 3 | ans = [] 4 | for ch in s: 5 | if ch.isdigit() and ans: ans.pop() 6 | else: ans.append(ch) 7 | return ''.join(ans) -------------------------------------------------------------------------------- /3191. Minimum Operations to Make Binary Array Elements Equal to One I.js: -------------------------------------------------------------------------------- 1 | var minOperations = function(nums) 2 | { 3 | let ans = 0, n = nums.length; 4 | for(let i = 0; i < n; i++) 5 | { 6 | if(!nums[i] && i + 2 < n) 7 | { 8 | ans += 1; 9 | nums[i] = !nums[i]; 10 | nums[i + 1] = !nums[i + 1]; 11 | nums[i + 2] = !nums[i + 2]; 12 | } 13 | } 14 | 15 | if(nums[n - 1] && nums[n - 2] && nums[n - 3]) return ans; 16 | return -1; 17 | }; -------------------------------------------------------------------------------- /3191. Minimum Operations to Make Binary Array Elements Equal to One I.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minOperations(self, nums: List[int]) -> int: 3 | flag = True 4 | ans, n = 0, len(nums) 5 | for i in range (0, n): 6 | if not nums[i]: 7 | cnt = 0 8 | while i+cnt < n and cnt < 3: 9 | nums[i+cnt] = 0 if nums[i+cnt] else 1 10 | cnt += 1 11 | if cnt == 3: 12 | ans += 1 13 | else: 14 | flag = not flag 15 | break 16 | return ans if flag else -1 17 | -------------------------------------------------------------------------------- /3208. Alternating Groups II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numberOfAlternatingGroups(vector& nums, int k) 4 | { 5 | int cnt = 0, ans = 0; 6 | int i = 0, j = 0, n = nums.size(); 7 | for(int i=0;i= k) ans++; 14 | } 15 | return ans; 16 | } 17 | }; -------------------------------------------------------------------------------- /3217. Delete Nodes From Linked List Present in Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | ListNode* modifiedList(vector& nums, ListNode* head) 4 | { 5 | setst; 6 | for(auto num:nums) st.insert(num); 7 | 8 | ListNode* ans = new ListNode(0); 9 | ListNode* tail = ans; 10 | while(head) 11 | { 12 | int val = head->val; head = head->next; 13 | if(not st.count(val)) 14 | { 15 | tail->next = new ListNode(val); 16 | tail = tail->next; 17 | } 18 | } 19 | return ans->next; 20 | } 21 | }; -------------------------------------------------------------------------------- /3223. Minimum Length of String After Operations.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minimumLength(string s) 4 | { 5 | map mp; 6 | for(char ch:s) mp[ch]++; 7 | int ans = 0; 8 | for(auto [ch, cnt]:mp) 9 | { 10 | ans++; 11 | if(!(cnt%2)) ans++; 12 | } 13 | return ans; 14 | } 15 | }; -------------------------------------------------------------------------------- /3264. Final Array State After K Multiplication Operations I.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def getFinalState(self, nums: List[int], k: int, multiplier: int) -> List[int]: 3 | minHeap = [(val, idx) for idx, val in enumerate(nums)] 4 | heapify(minHeap) 5 | 6 | for _ in range(k): 7 | val, idx = heappop(minHeap) 8 | heappush(minHeap, (val * multiplier, idx)) 9 | 10 | ans = [0] * len(nums) 11 | while minHeap: 12 | val, idx = heappop(minHeap) 13 | ans[idx] = val 14 | return ans -------------------------------------------------------------------------------- /3274. Check if Two Chessboard Squares Have the Same Color.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | bool checkTwoChessboards(string coordinate1, string coordinate2) 5 | { 6 | int one = coordinate1[0] + coordinate1[1]; 7 | int two = coordinate2[0] + coordinate2[1]; 8 | 9 | return one % 2 == two % 2; 10 | } 11 | }; -------------------------------------------------------------------------------- /3274. Check if Two Chessboard Squares Have the Same Color.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def checkTwoChessboards(self, coordinate1, coordinate2): 3 | # Convert coordinates to ASCII and add their values 4 | one = ord(coordinate1[0]) + int(coordinate1[1]) 5 | two = ord(coordinate2[0]) + int(coordinate2[1]) 6 | 7 | # Return true if both sums have the same parity 8 | return one % 2 == two % 2 -------------------------------------------------------------------------------- /3289. The Two Sneaky Numbers of Digitville.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | vector getSneakyNumbers(vector &nums) 5 | { 6 | int n = nums.size(); 7 | vector ans, count(n); 8 | for (auto val : nums) 9 | { 10 | count[val]++; 11 | if (count[val] == 2) 12 | ans.push_back(val); 13 | } 14 | return ans; 15 | } 16 | }; -------------------------------------------------------------------------------- /3324. Find the Sequence of Strings Appeared on the Screen.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector stringSequence(string target) 4 | { 5 | string s; 6 | vectorans; 7 | int n = target.size(); 8 | for(int i = 0; i < n; i++) 9 | { 10 | s += 'a'; 11 | for(char ch = 'a'; ch <= 'z'; ch++) 12 | { 13 | s[i] = ch; 14 | ans.push_back(s); 15 | if(ch == target[i]) break; 16 | } 17 | } 18 | return ans; 19 | } 20 | }; -------------------------------------------------------------------------------- /3330. Find the Original Typed String I.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int possibleStringCount(string word) 4 | { 5 | int ans = 1; 6 | unordered_mapcount; 7 | unordered_map>idx; 8 | for(int i = 0; i < word.size(); i++) 9 | { 10 | auto ch = word[i]; 11 | if(idx[ch].empty() or idx[ch].back() + 1 == i) 12 | { 13 | count[ch] += 1; 14 | if(count[ch] > 1) ans += 1; 15 | } 16 | idx[ch].push_back(i); 17 | } 18 | return ans; 19 | } 20 | }; -------------------------------------------------------------------------------- /3340. Check Balanced String.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isBalanced(string num) 4 | { 5 | mapmp; 6 | bool flag = true; 7 | for(auto ch:num) 8 | { 9 | mp[flag] += ch - '0'; 10 | flag = !flag; 11 | } 12 | return mp[0] == mp[1]; 13 | } 14 | }; -------------------------------------------------------------------------------- /3345. Smallest Divisible Digit Product I.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int smallestNumber(int n, int t) 4 | { 5 | for(int i = n; i < 101; i++) 6 | { 7 | n = i; 8 | string s = to_string(i); 9 | int product = 1; 10 | for(auto ch : s) 11 | product *= (ch - '0'); 12 | 13 | if(product % t == 0) 14 | break; 15 | } 16 | return n; 17 | } 18 | }; 19 | 20 | 21 | -------------------------------------------------------------------------------- /3355. Zero Array Transformation I.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isZeroArray(vector& nums, vector>& queries) 4 | { 5 | int n = nums.size(); 6 | vector difArray(n + 1); 7 | for(auto &que : queries) 8 | { 9 | difArray[que[0]] += 1; 10 | difArray[que[1] + 1] -= 1; 11 | } 12 | 13 | for(int i = 1; i < n; i++) 14 | difArray[i] += difArray[i - 1]; 15 | 16 | for(int i = 0; i < n; i++) 17 | if(nums[i] > difArray[i]) return false; 18 | 19 | return true; 20 | } 21 | }; -------------------------------------------------------------------------------- /3355. Zero Array Transformation I.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isZeroArray(self, nums: List[int], queries: List[List[int]]) -> bool: 3 | n = len(nums) 4 | difArray = [0] * (n + 1) 5 | 6 | for l, r in queries: 7 | difArray[l] += 1 8 | difArray[r + 1] -= 1 9 | 10 | for i in range(1, n): 11 | difArray[i] += difArray[i - 1] 12 | 13 | for i in range(n): 14 | if nums[i] > difArray[i]: 15 | return False 16 | return True -------------------------------------------------------------------------------- /3370. Smallest Number With All Set Bits.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int smallestNumber(int n) 4 | { 5 | bitset<24>bits(n); 6 | string num, s = bits.to_string(); 7 | 8 | int idx = s.find('1'); 9 | for(int i = idx; i < 24; i++) 10 | num += '1'; 11 | 12 | int ans = stoi(num, NULL, 2); 13 | return ans; 14 | } 15 | }; -------------------------------------------------------------------------------- /3375. Minimum Operations to Make Array Values Equal to K.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minOperations(vector& nums, int k) 4 | { 5 | unordered_map store; 6 | for(auto &val : nums) 7 | { 8 | if(val < k) return -1; 9 | store[val] += 1; 10 | } 11 | 12 | int ans = 0; 13 | for(auto [val, count] : store) 14 | { 15 | if(val == k) continue; 16 | ans += 1; 17 | } 18 | return ans; 19 | } 20 | }; -------------------------------------------------------------------------------- /3392. Count Subarrays of Length Three With a Condition.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countSubarrays(vector& nums) 4 | { 5 | int ans = 0, n = nums.size(); 6 | for(int i = 1; i < n - 1; i++) 7 | { 8 | int op1 = ceil(nums[i] / 2.0); 9 | int op2 = nums[i - 1] + nums[i + 1]; 10 | int op3 = ceil((nums[i] + 1) / 2.0); 11 | ans += (op1 == op2 and op2 != op3); 12 | } 13 | return ans; 14 | } 15 | }; -------------------------------------------------------------------------------- /3396. Minimum Number of Operations to Make Elements in Array Distinct.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minimumOperations(vector& nums) 4 | { 5 | int ans = 0, n = nums.size(); 6 | unordered_map count; 7 | for(int i = n - 1; i >= 0; i--) 8 | { 9 | if(count[nums[i]]) break; 10 | count[nums[i]]++; 11 | } 12 | return ceil((n - count.size()) / 3.0); 13 | } 14 | }; -------------------------------------------------------------------------------- /3407. Substring Matching Pattern.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool hasMatch(string s, string p) 4 | { 5 | int star = p.find('*'); 6 | string s1 = p.substr(0, star), s2 = p.substr(star + 1); 7 | if(not s1.size() or s.find(s1) != -1) 8 | { 9 | s = s.substr(s.find(s1) + s1.size()); 10 | if(not s2.size() or s.find(s2) != -1) return true; 11 | } 12 | return false; 13 | } 14 | }; -------------------------------------------------------------------------------- /343. Integer Break.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long ans = 0; 4 | vector> memo; 5 | void dfs(int sum, int &n, long mul) 6 | { 7 | if(sum > n) return; 8 | if(sum == n) ans = max(ans, mul); 9 | 10 | if(memo[sum][mul]) 11 | { 12 | ans = max(ans, memo[sum][mul]); 13 | return; 14 | } 15 | 16 | for(int i = 1; i < n; i++) 17 | dfs(sum + i, n, mul * i); 18 | memo[sum][mul] = mul; 19 | } 20 | int integerBreak(int n) 21 | { 22 | memo.resize(200); 23 | dfs(0, n, 1); 24 | return ans; 25 | } 26 | }; -------------------------------------------------------------------------------- /3432. Count Partitions with Even Sum Difference.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countPartitions(vector& nums) 4 | { 5 | int ans = 0, left = 0, n = nums.size(); 6 | int right = accumulate(nums.begin(), nums.end(), 0); 7 | for(int i = 0; i < n - 1; i++) 8 | { 9 | left += nums[i], right -= nums[i]; 10 | ans += not (abs(right - left) % 2); 11 | } 12 | return ans; 13 | } 14 | }; -------------------------------------------------------------------------------- /3461. Check If Digits Are Equal in String After Operations I.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool hasSameDigits(string s) 4 | { 5 | int n = s.size(); 6 | while(n > 2) 7 | { 8 | string temp; 9 | for(int i = 1; i < n; i++) 10 | temp += to_string(((s[i] - '0') + (s[i - 1] - '0')) % 10); 11 | s = temp; 12 | n = s.size(); 13 | } 14 | return s[0] == s[1]; 15 | } 16 | }; -------------------------------------------------------------------------------- /3467. Transform Array by Parity.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector transformArray(vector& nums) 4 | { 5 | vector ans; 6 | int oddCount = 0; 7 | for(auto &val : nums) 8 | { 9 | if(val % 2) oddCount += 1; 10 | else ans.push_back(0); 11 | } 12 | 13 | for(int i = 0; i < oddCount; i++) 14 | ans.push_back(1); 15 | return ans; 16 | } 17 | }; -------------------------------------------------------------------------------- /3492. Maximum Containers on a Ship.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int maxContainers(int n, int w, int maxWeight) 5 | { 6 | n *= n; 7 | if (n * w <= maxWeight) 8 | return n; 9 | return maxWeight / w; 10 | } 11 | }; -------------------------------------------------------------------------------- /3512. Minimum Operations to Make Array Sum Divisible by K.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minOperations(vector& nums, int k) 4 | { 5 | int sum = 0; 6 | for(auto &val : nums) sum += val; 7 | return sum % k; 8 | } 9 | }; -------------------------------------------------------------------------------- /3512. Minimum Operations to Make Array Sum Divisible by K.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minOperations(self, nums: List[int], k: int) -> int: 3 | return sum(nums) % k -------------------------------------------------------------------------------- /3516. Find Closest Person.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findClosest(int x, int y, int z) 4 | { 5 | int dif = abs(z - x) - abs(z - y); 6 | if(dif) return dif < 0 ? 1 : 2; 7 | return 0; 8 | } 9 | }; -------------------------------------------------------------------------------- /3517. Smallest Palindromic Rearrangement I.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string smallestPalindrome(string s) 4 | { 5 | vector count(26); 6 | for(auto &ch : s) count[ch - 'a'] += 1; 7 | 8 | int cur = 0, n = s.size(); 9 | for(int i = 0; i < 26; i++) 10 | { 11 | if(count[i] % 2) s[n / 2] = i + 'a'; 12 | while(count[i] > 1) 13 | { 14 | s[cur] = i + 'a'; 15 | s[n - cur - 1] = i + 'a'; 16 | count[i] -= 2; 17 | cur += 1; 18 | } 19 | } 20 | return s; 21 | } 22 | }; -------------------------------------------------------------------------------- /3536. Maximum Product of Two Digits.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProduct(int n) 4 | { 5 | auto s = to_string(n); 6 | sort(begin(s), end(s)); 7 | return ((s[s.size() - 1] - '0') * (s[s.size() - 2] - '0')); 8 | } 9 | }; -------------------------------------------------------------------------------- /3541. Find Most Frequent Vowel and Consonant.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxFreqSum(self, s: str) -> int: 3 | count = Counter(s) 4 | maxVowel, maxCosonent = 0, 0 5 | for ch in range(97, 123): 6 | ch = chr(ch) 7 | if ch == 'a' or ch == 'e' or ch == 'i' or ch == 'o' or ch == 'u': 8 | maxVowel = max(maxVowel, count[ch]) 9 | else: 10 | maxCosonent = max(maxCosonent, count[ch]) 11 | return maxVowel + maxCosonent -------------------------------------------------------------------------------- /3545. Minimum Deletions for At Most K Distinct Characters.cpp: -------------------------------------------------------------------------------- 1 | #define tuple tuple 2 | class Solution { 3 | public: 4 | int minDeletion(string s, int k) 5 | { 6 | unordered_map count; 7 | for(auto &ch : s) count[ch] += 1; 8 | 9 | priority_queue, greater> minHeap; 10 | for(auto [ch, cnt] : count) 11 | minHeap.push({cnt, ch}); 12 | 13 | int ans = 0; 14 | while(minHeap.size() > k) 15 | { 16 | auto [cnt, ch] = minHeap.top(); minHeap.pop(); 17 | ans += cnt; 18 | } 19 | return ans; 20 | } 21 | }; -------------------------------------------------------------------------------- /3550. Smallest Index With Digit Sum Equal to Index.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int smallestIndex(vector& nums) 4 | { 5 | int n = nums.size(); 6 | for(int i = 0; i < n; i++) 7 | { 8 | int sum = 0; 9 | auto s = to_string(nums[i]); 10 | for(auto &ch : s) 11 | sum += ch - '0'; 12 | if(sum == i) return i; 13 | } 14 | return -1; 15 | } 16 | }; -------------------------------------------------------------------------------- /38. Count and Say.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string countAndSay(int n) 4 | { 5 | if(n==1) return "1"; 6 | 7 | string ans, say = countAndSay(n-1); 8 | 9 | int cnt = 1, nn = say.size(); 10 | char ch = say[0]; 11 | for(int i=1;i str: 3 | if n == 1: 4 | return "1" 5 | s = self.countAndSay(n - 1) 6 | 7 | count, l = 1, len(s) 8 | convert, prev = "", s[0] 9 | for i in range(1, l): 10 | if s[i] == prev: 11 | count += 1 12 | else: 13 | convert += str(count) + prev 14 | prev = s[i] 15 | count = 1 16 | return convert + str(count) + prev -------------------------------------------------------------------------------- /386. Lexicographical Numbers.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def lexicalOrder(self, n): 3 | num = 1 4 | ans = [] 5 | while len(ans) < n: 6 | ans.append(num) 7 | 8 | if num * 10 <= n: num *= 10 9 | elif num % 10 != 9 and num + 1 <= n: num += 1 10 | else: 11 | while (num / 10) % 10 == 9: 12 | num /= 10 13 | num = (num / 10) + 1 14 | return ans -------------------------------------------------------------------------------- /476. Number Complement.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findComplement(int num) 4 | { 5 | bitset<32>bit(num); 6 | string s = bit.to_string(); 7 | 8 | bool flag = false; 9 | for(auto &ch:s) 10 | { 11 | if(ch == '1') flag = true; 12 | if(flag) 13 | ch = (ch == '1' ? '0' : '1'); 14 | } 15 | int number = stoi(s, NULL, 2); 16 | return number; 17 | } 18 | }; -------------------------------------------------------------------------------- /48. Rotate Image.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void rotate(vector>& mat) 4 | { 5 | vector>tmp; 6 | int n = mat.size(); 7 | for(int i=0;it; 10 | for(int j=n-1;j>=0;j--) 11 | { 12 | t.push_back(mat[j][i]); 13 | } 14 | tmp.push_back(t); 15 | } 16 | mat=tmp; 17 | } 18 | }; -------------------------------------------------------------------------------- /494. Target Sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | map,int>dp; 4 | int solve(int n, int sum, vector& nums) 5 | { 6 | if(!n) return sum == 0; 7 | 8 | if(dp.find({n,sum}) != dp.end()) return dp[{n, sum}]; 9 | 10 | int add = solve(n - 1, sum - nums[n-1], nums); 11 | int subtract = solve(n - 1, sum + nums[n - 1], nums); 12 | return dp[{n, sum}] = add + subtract; 13 | } 14 | int findTargetSumWays(vector& nums, int target) 15 | { 16 | return solve(nums.size(), target, nums); 17 | } 18 | }; -------------------------------------------------------------------------------- /494. Target Sum.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findTargetSumWays(self, nums: List[int], target: int) -> int: 3 | dp = {} 4 | def recurtion(idx, cur_sum): 5 | if idx == len(nums): 6 | return 1 if cur_sum == target else 0 7 | if (idx, cur_sum) in dp: 8 | return dp[(idx, cur_sum)] 9 | 10 | add = recurtion(idx + 1, cur_sum + nums[idx]) 11 | subtract = recurtion(idx + 1, cur_sum - nums[idx]) 12 | dp[(idx, cur_sum)] = add + subtract 13 | return add + subtract 14 | return recurtion(0, 0) -------------------------------------------------------------------------------- /515. Find Largest Value in Each Tree Row.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def largestValues(self, root: Optional[TreeNode]) -> List[int]: 3 | store = {} 4 | def DFS(root, depth): 5 | if not root: return 6 | if depth in store: 7 | store[depth] = max(store[depth], root.val) 8 | else: store[depth] = root.val 9 | DFS(root.left, depth + 1) 10 | DFS(root.right, depth + 1) 11 | 12 | DFS(root, 0) 13 | ans = [0] * len(store) 14 | for depth in store: 15 | ans[depth] = store[depth] 16 | return ans -------------------------------------------------------------------------------- /590. N-ary Tree Postorder Traversal.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | vectora; 5 | 6 | void postOrder(Node* root) 7 | { 8 | if(root == NULL) return; 9 | for(Node* child: root->children) 10 | postOrder(child); 11 | a.push_back(root->val); 12 | } 13 | vector postorder(Node* root) 14 | { 15 | postOrder(root); 16 | return a; 17 | } 18 | }; -------------------------------------------------------------------------------- /646. Maximum Length of Pair Chain.cpp: -------------------------------------------------------------------------------- 1 | class cmp 2 | { 3 | public: 4 | bool operator()(auto &a, auto &b) 5 | { 6 | return a[1] < b[1]; 7 | } 8 | }; 9 | class Solution 10 | { 11 | public: 12 | int findLongestChain(vector> &pairs) 13 | { 14 | sort(begin(pairs), end(pairs), cmp()); 15 | 16 | int ans = 1; 17 | vector prev = pairs[0]; 18 | for (auto vec : pairs) 19 | { 20 | if (prev[1] < vec[0]) 21 | { 22 | ans += 1; 23 | prev = vec; 24 | } 25 | } 26 | return ans; 27 | } 28 | }; -------------------------------------------------------------------------------- /729. My Calendar I.cpp: -------------------------------------------------------------------------------- 1 | class MyCalendar 2 | { 3 | public: 4 | MyCalendar() {} 5 | 6 | map mp; 7 | bool book(int start, int end) 8 | { 9 | mp[start] += 1; 10 | mp[end] -= 1; 11 | int ActiveCount = 0; 12 | for (auto [value, count] : mp) 13 | { 14 | ActiveCount += count; 15 | if (ActiveCount > 1) 16 | { 17 | mp[start] -= 1; 18 | mp[end] += 1; 19 | return false; 20 | } 21 | } 22 | return true; 23 | } 24 | }; -------------------------------------------------------------------------------- /73. Set Matrix Zeroes.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def setZeroes(self, matrix: List[List[int]]) -> None: 3 | row, col = set(), set() 4 | n, m = len(matrix), len(matrix[0]) 5 | 6 | for i in range(n): 7 | for j in range(m): 8 | if not matrix[i][j]: 9 | row.add(i) 10 | col.add(j) 11 | 12 | for i in row: 13 | for j in range(m): 14 | matrix[i][j] = 0 15 | for j in col: 16 | for i in range(n): 17 | matrix[i][j] = 0 -------------------------------------------------------------------------------- /731. My Calendar II.cpp: -------------------------------------------------------------------------------- 1 | class MyCalendarTwo 2 | { 3 | public: 4 | MyCalendarTwo() {} 5 | 6 | map mp; 7 | bool book(int start, int end) 8 | { 9 | mp[start] += 1; 10 | mp[end] -= 1; 11 | int ActiveEvent = 0; 12 | for (auto [value, count] : mp) 13 | { 14 | ActiveEvent += count; 15 | if (ActiveEvent > 2) 16 | { 17 | mp[start] -= 1; 18 | mp[end] += 1; 19 | return false; 20 | } 21 | } 22 | return true; 23 | } 24 | }; 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /75. Sort Colors.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void sortColors(vector& nums) 4 | { 5 | int zero = 0, one = 0, two = 0; 6 | for(auto val:nums) 7 | { 8 | if(val == 0) zero++; 9 | else if(val == 1) one++; 10 | else two++; 11 | } 12 | for(auto &val : nums) 13 | { 14 | if(zero) val = 0, zero--; 15 | else if(one) val = 1, one--; 16 | else val = 2; 17 | } 18 | } 19 | }; -------------------------------------------------------------------------------- /75. Sort Colors.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def sortColors(self, nums: List[int]) -> None: 3 | zeroCount, oneCount, twoCount = 0, 0, 0 4 | for val in nums: 5 | zeroCount += val == 0 6 | oneCount += val == 1 7 | twoCount += val == 2 8 | 9 | cur = 0 10 | for _ in range(zeroCount): 11 | nums[cur] = 0 12 | cur += 1 13 | for _ in range(oneCount): 14 | nums[cur] = 1 15 | cur += 1 16 | for _ in range(twoCount): 17 | nums[cur] = 2 18 | cur += 1 -------------------------------------------------------------------------------- /762. Prime Number of Set Bits in Binary Representation.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countPrimeSetBits(int left, int right) 4 | { 5 | int ans = 0; 6 | 7 | unordered_set primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31}; 8 | for(int i = left; i <= right; i++) 9 | ans += primes.count(bitset<32>(i).count()); 10 | 11 | return ans; 12 | } 13 | }; -------------------------------------------------------------------------------- /763. Partition Labels.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector partitionLabels(string s) 4 | { 5 | mapmp; 6 | mp[NULL] = INT_MAX; 7 | int n = s.size(); 8 | for(int i=0;ians; 11 | int cur = -1, i = 0, mx = mp[s[0]]; 12 | while(mx < n) 13 | { 14 | while(i <= mx) 15 | mx = max(mx, mp[s[i++]]); 16 | ans.push_back(mx - cur); 17 | cur = mx; 18 | mx = mp[s[i]]; 19 | } 20 | return ans; 21 | } 22 | }; -------------------------------------------------------------------------------- /769. Max Chunks To Make Sorted.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxChunksToSorted(vector& arr) 4 | { 5 | int Chunks = 0, seen = 0, n = arr.size(); 6 | for(int i = 0; i < n; i++) 7 | { 8 | seen = max(seen, arr[i]); 9 | if(seen == i) Chunks += 1; 10 | } 11 | return Chunks; 12 | } 13 | }; -------------------------------------------------------------------------------- /775. Global and Local Inversions.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isIdealPermutation(self, nums: List[int]) -> bool: 3 | n = len(nums) 4 | 5 | local_inversion_count, global_inversion_count = 0, 0 6 | for i in range(n - 1): 7 | local_inversion_count += nums[i] > nums[i + 1] 8 | 9 | pbds = SortedList() 10 | for i in range(n): 11 | global_inversion_count += len(pbds) - pbds.bisect_left(nums[i]) 12 | if global_inversion_count > local_inversion_count: 13 | return False 14 | pbds.add(nums[i]) 15 | 16 | return local_inversion_count == global_inversion_count -------------------------------------------------------------------------------- /781. Rabbits in Forest.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numRabbits(vector& answers) 4 | { 5 | int ans = 0; 6 | unordered_map count; 7 | for(auto &val : answers) 8 | { 9 | if(count[val] > val) count[val] = 0; 10 | if(not count[val]) ans += val + 1; 11 | count[val] += 1; 12 | } 13 | return ans; 14 | } 15 | }; -------------------------------------------------------------------------------- /781. Rabbits in Forest.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numRabbits(self, answers: List[int]) -> int: 3 | ans = 0 4 | count = defaultdict(int) 5 | for val in answers: 6 | if count[val] > val: 7 | count[val] = 0 8 | if not count[val]: 9 | ans += val + 1 10 | count[val] += 1 11 | return ans -------------------------------------------------------------------------------- /790. Domino and Tromino Tiling.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int mod = 1e9 + 7; 4 | unordered_map memo; 5 | int numTilings(int n) 6 | { 7 | if (n == 0) return 1; 8 | if (n == 1) return 1; 9 | if (n == 2) return 2; 10 | 11 | if (memo.count(n)) return memo[n]; 12 | 13 | return memo[n] = (2LL * numTilings(n - 1) % mod + numTilings(n - 3) % mod) % mod; 14 | } 15 | }; -------------------------------------------------------------------------------- /796. Rotate String.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool rotateString(string s, string goal) 4 | { 5 | s += s; 6 | return s.find(goal) != -1 and s.size() / 2 == goal.size(); 7 | } 8 | }; -------------------------------------------------------------------------------- /796. Rotate String.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def rotateString(self, s: str, goal: str) -> bool: 3 | s += s 4 | return len(s) / 2 == len(goal) and goal in s -------------------------------------------------------------------------------- /80. Remove Duplicates from Sorted Array II.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int removeDuplicates(vector &nums) 5 | { 6 | int index = 0, cnt = 0, value = nums[0]; 7 | 8 | for (auto val : nums) 9 | { 10 | if (value == val) 11 | cnt += 1; 12 | else 13 | { 14 | cnt = 1; 15 | value = val; 16 | } 17 | if (cnt <= 2) 18 | nums[index++] = val; 19 | } 20 | 21 | return index; 22 | } 23 | }; -------------------------------------------------------------------------------- /884. Uncommon Words from Two Sentences.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | map mp; 5 | 6 | void stringstreeam(string s) 7 | { 8 | stringstream ss(s); 9 | string word; 10 | while (ss >> word) 11 | mp[word]++; 12 | } 13 | 14 | vector uncommonFromSentences(string s1, string s2) 15 | { 16 | stringstreeam(s1); 17 | stringstreeam(s2); 18 | 19 | vector ans; 20 | for (auto x : mp) 21 | if (x.second == 1) 22 | ans.push_back(x.first); 23 | 24 | return ans; 25 | } 26 | }; -------------------------------------------------------------------------------- /884. Uncommon Words from Two Sentences.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def uncommonFromSentences(self, s1, s2): 3 | all_string = s1.split(' ') 4 | all_string += (s2.split(' ')) 5 | counter = Counter(all_string) 6 | 7 | ans = [] 8 | for s, cnt in counter.items(): 9 | if cnt == 1: 10 | ans.append(s) 11 | return ans -------------------------------------------------------------------------------- /921. Minimum Add to Make Parentheses Valid.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int minAddToMakeValid(string s) 5 | { 6 | int count = 0, ans = 0; 7 | 8 | for (auto ch : s) 9 | { 10 | if (ch == '(') 11 | count++; 12 | else 13 | count ? count-- : ans++; 14 | } 15 | 16 | return ans + count; 17 | } 18 | }; -------------------------------------------------------------------------------- /921. Minimum Add to Make Parentheses Valid.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minAddToMakeValid(self, s: str) -> int: 3 | count, ans = 0, 0 4 | 5 | for ch in s: 6 | if ch =='(': count += 1 7 | elif count: count -= 1 8 | else: ans += 1 9 | return ans + count -------------------------------------------------------------------------------- /925. Long Pressed Name.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isLongPressedName(string name, string typed) 4 | { 5 | int i = 0, j = 0, n = name.size(), m = typed.size(); 6 | while(j < m) 7 | { 8 | if(name[i] == typed[j]) i++, j++; 9 | else if(i and typed[j] == name[i - 1]) j++; 10 | else break; 11 | } 12 | return i == n and j == m; 13 | } 14 | }; -------------------------------------------------------------------------------- /951. Flip Equivalent Binary Trees (DSF).cpp: -------------------------------------------------------------------------------- 1 | // 951. Flip Equivalent Binary Trees (DFS).cpp 2 | class Solution { 3 | public: 4 | void DFS(TreeNode* root, unordered_map>&store, int parent) 5 | { 6 | if(!root) return; 7 | store[parent].insert(root->val); 8 | DFS(root->left, store, root->val); 9 | DFS(root->right, store, root->val); 10 | } 11 | bool flipEquiv(TreeNode* root1, TreeNode* root2) 12 | { 13 | unordered_map>store1, store2; 14 | DFS(root1, store1, -1); 15 | DFS(root2, store2, -1); 16 | 17 | return store1 == store2; 18 | } 19 | }; -------------------------------------------------------------------------------- /962. Maximum Width Ramp.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int maxWidthRamp(vector &nums) 5 | { 6 | int n = nums.size(); 7 | 8 | stack st; 9 | st.push(0); 10 | for (int i = 1; i < n; i++) 11 | if (nums[i] < nums[st.top()]) 12 | st.push(i); 13 | 14 | int ans = 0; 15 | for (int i = n - 1; i >= 0; i--) 16 | { 17 | while (st.size() and nums[st.top()] <= nums[i]) 18 | { 19 | ans = max(ans, i - st.top()); 20 | st.pop(); 21 | } 22 | } 23 | return ans; 24 | } 25 | }; -------------------------------------------------------------------------------- /Array/1295. Find Numbers with Even Number of Digits.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findNumbers(vector& nums) 4 | { 5 | int ans = 0; 6 | for(auto &val : nums) 7 | ans += to_string(val).size() % 2 == 0; 8 | return ans; 9 | } 10 | }; -------------------------------------------------------------------------------- /Array/1295. Find Numbers with Even Number of Digits.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findNumbers(self, nums: List[int]) -> int: 3 | ans = 0 4 | for val in nums: 5 | ans += not (len(str(val)) % 2) 6 | return ans -------------------------------------------------------------------------------- /Array/1534. Count Good Triplets.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countGoodTriplets(vector& arr, int a, int b, int c) 4 | { 5 | int cnt = 0, n = arr.size(); 6 | for(int i = 0; i < n-2 ;i++) 7 | { 8 | for(int j = i + 1; j < n - 1; j++) 9 | { 10 | for(int k = j + 1; k < n; k++) 11 | { 12 | if(abs(arr[i] - arr[j]) <= a && abs(arr[j] - arr[k]) <= b && abs(arr[i] - arr[k]) <= c) 13 | { 14 | cnt++; 15 | } 16 | } 17 | } 18 | } 19 | return cnt; 20 | } 21 | }; -------------------------------------------------------------------------------- /Array/1534. Count Good Triplets.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int: 3 | ans, n = 0, len(arr) 4 | for i in range(n): 5 | for j in range(i + 1, n): 6 | for k in range(j + 1, n): 7 | if abs(arr[i] - arr[j]) > a: 8 | continue 9 | if abs(arr[j] - arr[k]) > b: 10 | continue 11 | if abs(arr[i] - arr[k]) > c: 12 | continue 13 | ans += 1 14 | return ans -------------------------------------------------------------------------------- /Array/1550. Three Consecutive Odds.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool threeConsecutiveOdds(vector& arr) 4 | { 5 | int n = arr.size(); 6 | for(int i = 0; i < n - 2; i++) 7 | if(arr[i] % 2 and arr[i + 1] % 2 and arr[i + 2] % 2) 8 | return true; 9 | return false; 10 | } 11 | }; -------------------------------------------------------------------------------- /Array/1550. Three Consecutive Odds.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def threeConsecutiveOdds(self, arr: List[int]) -> bool: 3 | n = len(arr) 4 | for i in range(2, n): 5 | if arr[i] % 2 and arr[i - 1] % 2 and arr[i - 2] % 2: 6 | return True 7 | return False 8 | 9 | 10 | class Solution(object): 11 | def threeConsecutiveOdds(self, arr): 12 | n = len(arr) 13 | for i in range(n - 2): 14 | if arr[i] % 2 and arr[i + 1] % 2 and arr[i + 2] % 2: 15 | return True 16 | return False 17 | -------------------------------------------------------------------------------- /Array/1752. Check if Array Is Sorted and Rotated.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool check(vector& nums) 4 | { 5 | int n = nums.size(); 6 | vectort(nums.begin(),nums.end()); 7 | sort(t.begin(),t.end()); 8 | for(int i=0;i bool: 3 | flag = False 4 | n = len(nums) 5 | for i in range(1, n): 6 | if nums[i] < nums[i - 1]: 7 | arr = nums[i:] + nums[:i] 8 | nums.sort() 9 | return nums == arr 10 | return True -------------------------------------------------------------------------------- /Array/1800. Maximum Ascending Subarray Sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxAscendingSum(vector& nums) 4 | { 5 | int n = nums.size(), sum = nums[0], maxSum = nums[0]; 6 | for(int i = 1; i < n; i++) 7 | { 8 | if(nums[i] <= nums[i - 1]) sum = 0; 9 | sum += nums[i]; 10 | maxSum = max(maxSum, sum); 11 | } 12 | return maxSum; 13 | } 14 | }; -------------------------------------------------------------------------------- /Array/2094. Finding 3-Digit Even Numbers.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findEvenNumbers(self, digits: List[int]) -> List[int]: 3 | n = len(digits) 4 | store = SortedList() 5 | for i in range(n): 6 | if not digits[i]: continue 7 | for j in range(n): 8 | if i == j: continue 9 | for k in range(n): 10 | if j == k or digits[k] % 2 or k == i: continue 11 | s = str(digits[i]) + str(digits[j]) + str(digits[k]) 12 | if int(s) not in store: 13 | store.add(int(s)) 14 | return list(store) -------------------------------------------------------------------------------- /Array/2161. Partition Array According to Given Pivot.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector pivotArray(vector& nums, int x) { 4 | vectora; 5 | vectorb; 6 | vectorc; 7 | 8 | for(int v:nums) 9 | { 10 | if(v==x) c.push_back(v); 11 | else if(v < x) a.push_back(v); 12 | else b.push_back(v); 13 | } 14 | 15 | a.insert(a.end(),c.begin(),c.end()); 16 | a.insert(a.end(),b.begin(),b.end()); 17 | return a; 18 | } 19 | }; -------------------------------------------------------------------------------- /Array/2161. Partition Array According to Given Pivot.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def pivotArray(self, nums: List[int], pivot: int) -> List[int]: 3 | less_val, greater_val, equal_val = [], [], [] 4 | 5 | for val in nums: 6 | if val == pivot: equal_val.append(val) 7 | elif val < pivot: less_val.append(val) 8 | else: greater_val.append(val) 9 | 10 | return less_val + equal_val + greater_val -------------------------------------------------------------------------------- /Array/2176. Count Equal and Divisible Pairs in an Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countPairs(vector& nums, int k) 4 | { 5 | int ans = 0, n = nums.size(); 6 | for(int i = 0; i < n - 1; i++) 7 | { 8 | for(int j = i + 1; j < n; j++) 9 | { 10 | ans += (nums[i] == nums[j] and (i * j) % k == 0) 11 | } 12 | } 13 | return ans; 14 | } 15 | }; -------------------------------------------------------------------------------- /Array/2176. Count Equal and Divisible Pairs in an Array.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countPairs(self, nums: List[int], k: int) -> int: 3 | ans, n = 0, len(nums) 4 | for i in range(n): 5 | for j in range(i + 1, n): 6 | ans += not (i * j) % k and nums[i] == nums[j] 7 | return ans -------------------------------------------------------------------------------- /Array/2444. Count Subarrays With Fixed Bounds.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long long countSubarrays(vector& nums, int minK, int maxK) 4 | { 5 | long long ans = 0; 6 | int minpos = -1, maxpos = -1, unbound = -1, n = nums.size(); 7 | for(int i = 0; i < n; i++) 8 | { 9 | if(nums[i] < minK or nums[i] > maxK) unbound = i; 10 | if(nums[i]==minK) minpos = i; 11 | if(nums[i]==maxK) maxpos = i; 12 | 13 | int canadd = min(minpos, maxpos) - unbound; 14 | if(canadd > 0) ans += canadd; 15 | } 16 | return ans; 17 | } 18 | }; -------------------------------------------------------------------------------- /Array/2444. Count Subarrays With Fixed Bounds.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countSubarrays(self, nums: List[int], minK: int, maxK: int) -> int: 3 | minpos, maxpos, unbound = -1, -1, -1 4 | 5 | ans, n = 0, len(nums) 6 | for i in range(n): 7 | if nums[i] < minK or nums[i] > maxK: 8 | unbound = i 9 | if nums[i] == minK: 10 | minpos = i 11 | if nums[i] == maxK: 12 | maxpos = i 13 | 14 | canadd = min(minpos, maxpos) - unbound 15 | if canadd > 0: 16 | ans += canadd 17 | 18 | return ans -------------------------------------------------------------------------------- /Array/2460. Apply Operations to an Array.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def applyOperations(self, nums: List[int]) -> List[int]: 3 | for i in range(len(nums) - 1): 4 | if nums[i] == nums[i + 1]: 5 | nums[i + 1] = 0 6 | nums[i] *= 2 7 | 8 | ans = [] 9 | count_zero = 0 10 | for num in nums: 11 | if num: ans.append(num) 12 | else: count_zero += 1 13 | 14 | while count_zero: 15 | ans.append(0) 16 | count_zero -= 1 17 | 18 | return ans -------------------------------------------------------------------------------- /Array/2570. Merge Two 2D Arrays by Summing Values.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> mergeArrays(vector>& a, vector>& b) 4 | { 5 | mapmp; 6 | for(auto x:a) mp[x[0]] += x[1]; 7 | for(auto x:b) mp[x[0]] += x[1]; 8 | 9 | vector> ans; 10 | for(auto x:mp) ans.push_back({x.first,x.second}); 11 | return ans; 12 | } 13 | }; -------------------------------------------------------------------------------- /Array/2570. Merge Two 2D Arrays by Summing Values.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]: 3 | store = defaultdict(int) 4 | for id, val in nums1: 5 | store[id] += val 6 | for id, val in nums2: 7 | store[id] += val 8 | 9 | ans = [] 10 | for id, val in store.items(): 11 | ans.append([id, val]) 12 | ans.sort() 13 | return ans -------------------------------------------------------------------------------- /Array/2873. Maximum Value of an Ordered Triplet I.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long long maximumTripletValue(vector& nums) 4 | { 5 | long ans = 0, n = nums.size(); 6 | for(int i = 0; i < n; i++) 7 | { 8 | for(int j = i + 1; j < n; j++) 9 | { 10 | for(int k = j + 1; k < n; k++) 11 | { 12 | ans = max(ans, long(nums[i] - nums[j]) * nums[k]); 13 | } 14 | } 15 | } 16 | return ans; 17 | } 18 | }; -------------------------------------------------------------------------------- /Array/2900. Longest Unequal Adjacent Groups Subsequence I.py: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector getLongestSubsequence(vector& words, vector& groups) 4 | { 5 | vector ans; 6 | int i = 0, n = words.size(); 7 | while(i < n) 8 | { 9 | ans.push_back(words[i]); 10 | while(i < n - 1 and groups[i] == groups[i + 1]) 11 | i++; 12 | i++; 13 | } 14 | return ans; 15 | } 16 | }; -------------------------------------------------------------------------------- /Array/2908. Minimum Sum of Mountain Triplets I.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minimumSum(vector& nums) 4 | { 5 | int ans = INT_MAX, n = nums.size(); 6 | for(int i = 0; i < n; i++) 7 | { 8 | for(int j = i + 1; j < n; j++) 9 | { 10 | for(int k = j + 1; k < n; k++) 11 | { 12 | if(nums[i] < nums[j] and nums[k] < nums[j]) 13 | { 14 | ans = min(ans, nums[i] + nums[j] + nums[k]); 15 | } 16 | } 17 | } 18 | } 19 | return ans == INT_MAX ? -1 : ans; 20 | } 21 | }; -------------------------------------------------------------------------------- /Array/2960. Count Tested Devices After Test Operations.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countTestedDevices(vector& batteryPercentages) 4 | { 5 | int ans = 0; 6 | for(auto val : batteryPercentages) 7 | ans += ans < val; 8 | return ans; 9 | } 10 | }; -------------------------------------------------------------------------------- /Array/3105. Longest Strictly Increasing or Strictly Decreasing Subarray.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def longestMonotonicSubarray(self, nums: List[int]) -> int: 3 | ans, count, n = 1, 1, len(nums) 4 | for i in range(1, n): 5 | if nums[i] <= nums[i - 1]: 6 | count = 0 7 | count += 1 8 | ans = max(ans, count) 9 | 10 | count = 1 11 | for i in range(1, n): 12 | if nums[i] >= nums[i - 1]: 13 | count = 0 14 | count += 1 15 | ans = max(ans, count) 16 | 17 | return ans -------------------------------------------------------------------------------- /Array/3151. Special Array I.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isArraySpecial(vector& nums) 4 | { 5 | int n = nums.size(); 6 | for(int i=0;i bool: 3 | n = len(nums) 4 | for i in range(1, n): 5 | if nums[i] % 2 == nums[i - 1] % 2: 6 | return False 7 | return True -------------------------------------------------------------------------------- /Array/3392. Count Subarrays of Length Three With a Condition.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countSubarrays(vector& nums) 4 | { 5 | int ans = 0, n = nums.size(); 6 | for(int i = 1; i < n - 1; i++) 7 | { 8 | int op1 = ceil(nums[i] / 2.0); 9 | int op2 = nums[i - 1] + nums[i + 1]; 10 | int op3 = ceil((nums[i] + 1) / 2.0); 11 | ans += (op1 == op2 and op2 != op3); 12 | } 13 | return ans; 14 | } 15 | }; -------------------------------------------------------------------------------- /Array/3432. Count Partitions with Even Sum Difference.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countPartitions(vector& nums) 4 | { 5 | int ans = 0, left = 0, n = nums.size(); 6 | int right = accumulate(nums.begin(), nums.end(), 0); 7 | for(int i = 0; i < n - 1; i++) 8 | { 9 | left += nums[i], right -= nums[i]; 10 | ans += not (abs(right - left) % 2); 11 | } 12 | return ans; 13 | } 14 | }; -------------------------------------------------------------------------------- /Array/75. Sort Colors.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void sortColors(vector& nums) 4 | { 5 | int zero = 0, one = 0, two = 0; 6 | for(auto val:nums) 7 | { 8 | if(val == 0) zero++; 9 | else if(val == 1) one++; 10 | else two++; 11 | } 12 | for(auto &val : nums) 13 | { 14 | if(zero) val = 0, zero--; 15 | else if(one) val = 1, one--; 16 | else val = 2; 17 | } 18 | } 19 | }; -------------------------------------------------------------------------------- /Array/75. Sort Colors.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def sortColors(self, nums: List[int]) -> None: 3 | zeroCount, oneCount, twoCount = 0, 0, 0 4 | for val in nums: 5 | zeroCount += val == 0 6 | oneCount += val == 1 7 | twoCount += val == 2 8 | 9 | cur = 0 10 | for _ in range(zeroCount): 11 | nums[cur] = 0 12 | cur += 1 13 | for _ in range(oneCount): 14 | nums[cur] = 1 15 | cur += 1 16 | for _ in range(twoCount): 17 | nums[cur] = 2 18 | cur += 1 -------------------------------------------------------------------------------- /Backtracking/1980. Find Unique Binary String.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findDifferentBinaryString(self, nums: List[str]) -> str: 3 | ans = [''] 4 | n = len(nums) 5 | nums = set(nums) 6 | def backtrack(cur): 7 | if ans[0]: return 8 | if len(cur) == n: 9 | if cur not in nums: 10 | ans[0] = cur 11 | return 12 | backtrack(cur + '0') 13 | backtrack(cur + '1') 14 | 15 | backtrack('') 16 | return ans[0] -------------------------------------------------------------------------------- /Bit Manipulation/1018. Binary Prefix Divisible By 5.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector prefixesDivBy5(vector& nums) 4 | { 5 | long sum = 0; 6 | vector ans; 7 | for(auto &val : nums) 8 | { 9 | sum += sum + val; 10 | ans.push_back(sum % 5 == 0); 11 | sum %= 5; 12 | } 13 | return ans; 14 | } 15 | }; -------------------------------------------------------------------------------- /Bit Manipulation/2317. Maximum XOR After Operations .cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maximumXOR(vector& nums) 4 | { 5 | int ans = 0; 6 | vector added(32); 7 | for(auto &val : nums) 8 | { 9 | bitset<32> bit(val), newbit(0); 10 | for(int i = 0; i < 32; i++) 11 | { 12 | if(bit[i] and not added[i]) 13 | { 14 | newbit[i] = 1; 15 | added[i] = true; 16 | } 17 | } 18 | ans = ans xor newbit.to_ulong(); 19 | } 20 | return ans; 21 | } 22 | }; -------------------------------------------------------------------------------- /Bit Manipulation/2932. Maximum Strong Pair XOR I.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int maximumStrongPairXor(vector &nums) 5 | { 6 | int n = nums.size(), ans = 0; 7 | for (int i = 0; i < n; i++) 8 | { 9 | for (int j = i + 1; j < n; j++) 10 | { 11 | if (abs(nums[i] - nums[j]) > min(nums[i], nums[j])) 12 | continue; 13 | ans = max(ans, (nums[i] ^ nums[j])); 14 | } 15 | } 16 | return ans; 17 | } 18 | }; -------------------------------------------------------------------------------- /Bit Manipulation/3068. Find the Maximum Sum of Node Values.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long long maximumValueSum(vector& nums, int k, vector>& edges) 4 | { 5 | long ans = 0, count = 0, minXor = INT_MAX; 6 | for(auto &val : nums) 7 | { 8 | if(val < (val xor k)) 9 | { 10 | count += 1; 11 | ans += (val xor k); 12 | } 13 | else ans += val; 14 | minXor = min(minXor, long(abs(val - (val xor k)))); 15 | } 16 | if(count % 2) ans -= minXor; 17 | return ans; 18 | } 19 | }; -------------------------------------------------------------------------------- /Bit Manipulation/3191. Minimum Operations to Make Binary Array Elements Equal to One I.js: -------------------------------------------------------------------------------- 1 | var minOperations = function(nums) 2 | { 3 | let ans = 0, n = nums.length; 4 | for(let i = 0; i < n; i++) 5 | { 6 | if(!nums[i] && i + 2 < n) 7 | { 8 | ans += 1; 9 | nums[i] = !nums[i]; 10 | nums[i + 1] = !nums[i + 1]; 11 | nums[i + 2] = !nums[i + 2]; 12 | } 13 | } 14 | 15 | if(nums[n - 1] && nums[n - 2] && nums[n - 3]) return ans; 16 | return -1; 17 | }; -------------------------------------------------------------------------------- /Bit Manipulation/3191. Minimum Operations to Make Binary Array Elements Equal to One I.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minOperations(self, nums: List[int]) -> int: 3 | flag = True 4 | ans, n = 0, len(nums) 5 | for i in range (0, n): 6 | if not nums[i]: 7 | cnt = 0 8 | while i+cnt < n and cnt < 3: 9 | nums[i+cnt] = 0 if nums[i+cnt] else 1 10 | cnt += 1 11 | if cnt == 3: 12 | ans += 1 13 | else: 14 | flag = not flag 15 | break 16 | return ans if flag else -1 17 | -------------------------------------------------------------------------------- /Bit Manipulation/762. Prime Number of Set Bits in Binary Representation.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countPrimeSetBits(int left, int right) 4 | { 5 | int ans = 0; 6 | 7 | unordered_set primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31}; 8 | for(int i = left; i <= right; i++) 9 | ans += primes.count(bitset<32>(i).count()); 10 | 11 | return ans; 12 | } 13 | }; -------------------------------------------------------------------------------- /Combinatorics/2929. Distribute Candies Among Children II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long long distributeCandies(int n, int limit) 4 | { 5 | long ans = 0; 6 | for(int i = 0; i <= limit; i++) 7 | { 8 | long j = n - i; 9 | if(limit + limit < j or j < 0) continue; 10 | 11 | ans += 1; 12 | if(j <= limit) ans += j; 13 | else ans += limit - (j - limit); 14 | } 15 | return ans; 16 | } 17 | }; -------------------------------------------------------------------------------- /Counting/1128. Number of Equivalent Domino Pairs.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numEquivDominoPairs(vector>& dominoes) 4 | { 5 | int ans = 0; 6 | unordered_map count; 7 | for(auto &vec : dominoes) 8 | { 9 | if(vec[0] > vec[1]) swap(vec[0], vec[1]); 10 | ans += count[to_string(vec[0]) + to_string(vec[1])]++; 11 | } 12 | return ans; 13 | } 14 | }; -------------------------------------------------------------------------------- /Counting/1399. Count Largest Group.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countLargestGroup(int n) 4 | { 5 | int max_count = 0; 6 | unordered_mapcount; 7 | for(int i = 1; i <= n; i++) 8 | { 9 | int sum = 0; 10 | string s = to_string(i); 11 | for(char ch : s) 12 | sum += ch - '0'; 13 | count[sum] += 1; 14 | 15 | max_count = max(max_count, count[sum]); 16 | } 17 | 18 | int ans = 0; 19 | for(auto [val, cnt] : count) 20 | ans += cnt == max_count; 21 | return ans; 22 | } 23 | }; -------------------------------------------------------------------------------- /Counting/1399. Count Largest Group.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countLargestGroup(self, n: int) -> int: 3 | max_count = 0 4 | count = defaultdict(int) 5 | for val in range(1, n + 1): 6 | sum = 0 7 | for ch in str(val): 8 | sum += ord(ch) - ord('0') 9 | count[sum] += 1 10 | max_count = max(max_count, count[sum]) 11 | 12 | ans = 0 13 | for val in count: 14 | ans += count[val] == max_count 15 | return ans -------------------------------------------------------------------------------- /Counting/1897. Redistribute Characters to Make All Strings Equal.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool makeEqual(vector& words) 4 | { 5 | unordered_map store; 6 | for(auto &word : words) 7 | for(auto &ch : word) store[ch] += 1; 8 | 9 | int n = words.size(); 10 | for(auto [ch, cnt] : store) 11 | if((cnt - n) % n) return false; 12 | return true; 13 | } 14 | }; -------------------------------------------------------------------------------- /Counting/2206. Divide Array Into Equal Pairs.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool divideArray(vector& nums) 4 | { 5 | unordered_map store; 6 | for(auto &val : nums) store[val] += 1; 7 | for(auto [val, count] : store) 8 | if(count % 2) return false; 9 | return true; 10 | } 11 | }; -------------------------------------------------------------------------------- /Counting/3375. Minimum Operations to Make Array Values Equal to K.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minOperations(vector& nums, int k) 4 | { 5 | unordered_map store; 6 | for(auto &val : nums) 7 | { 8 | if(val < k) return -1; 9 | store[val] += 1; 10 | } 11 | 12 | int ans = 0; 13 | for(auto [val, count] : store) 14 | { 15 | if(val == k) continue; 16 | ans += 1; 17 | } 18 | return ans; 19 | } 20 | }; -------------------------------------------------------------------------------- /Counting/3396. Minimum Number of Operations to Make Elements in Array Distinct.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minimumOperations(vector& nums) 4 | { 5 | int ans = 0, n = nums.size(); 6 | unordered_map count; 7 | for(int i = n - 1; i >= 0; i--) 8 | { 9 | if(count[nums[i]]) break; 10 | count[nums[i]]++; 11 | } 12 | return ceil((n - count.size()) / 3.0); 13 | } 14 | }; -------------------------------------------------------------------------------- /Counting/3467. Transform Array by Parity.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector transformArray(vector& nums) 4 | { 5 | vector ans; 6 | int oddCount = 0; 7 | for(auto &val : nums) 8 | { 9 | if(val % 2) oddCount += 1; 10 | else ans.push_back(0); 11 | } 12 | 13 | for(int i = 0; i < oddCount; i++) 14 | ans.push_back(1); 15 | return ans; 16 | } 17 | }; -------------------------------------------------------------------------------- /Counting/3541. Find Most Frequent Vowel and Consonant.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxFreqSum(self, s: str) -> int: 3 | count = Counter(s) 4 | maxVowel, maxCosonent = 0, 0 5 | for ch in range(97, 123): 6 | ch = chr(ch) 7 | if ch == 'a' or ch == 'e' or ch == 'i' or ch == 'o' or ch == 'u': 8 | maxVowel = max(maxVowel, count[ch]) 9 | else: 10 | maxCosonent = max(maxCosonent, count[ch]) 11 | return maxVowel + maxCosonent -------------------------------------------------------------------------------- /Design/2349. Design a Number Container System.cpp: -------------------------------------------------------------------------------- 1 | class NumberContainers { 2 | public: 3 | unordered_map> store; 4 | unordered_map index_filled; 5 | NumberContainers() {} 6 | 7 | void change(int index, int number) 8 | { 9 | if (index_filled.count(index)) 10 | store[index_filled[index]].erase(index); 11 | 12 | store[number].insert(index); 13 | index_filled[index] = number; 14 | } 15 | 16 | int find(int number) 17 | { 18 | if (store[number].empty()) 19 | return -1; 20 | 21 | return *store[number].begin(); 22 | } 23 | }; -------------------------------------------------------------------------------- /Dynamic Programming/1863. Sum of All Subset XOR Totals.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | auto solve(int i, int Xor, vector& nums) 4 | { 5 | if(i == nums.size()) return Xor; 6 | 7 | int pic = solve(i + 1, Xor ^ nums[i], nums); 8 | int not_pic = solve(i + 1, Xor, nums); 9 | return pic + not_pic; 10 | } 11 | int subsetXORSum(vector& nums) 12 | { 13 | return solve(0, 0, nums); 14 | } 15 | }; -------------------------------------------------------------------------------- /Dynamic Programming/790. Domino and Tromino Tiling.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int mod = 1e9 + 7; 4 | unordered_map memo; 5 | int numTilings(int n) 6 | { 7 | if (n == 0) return 1; 8 | if (n == 1) return 1; 9 | if (n == 2) return 2; 10 | 11 | if (memo.count(n)) return memo[n]; 12 | 13 | return memo[n] = (2LL * numTilings(n - 1) % mod + numTilings(n - 3) % mod) % mod; 14 | } 15 | }; -------------------------------------------------------------------------------- /Graph/1971. Find if Path Exists in Graph.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool visited[200005]; 4 | vector ed[200005]; 5 | void dfs(int source) 6 | { 7 | visited[source] = true; 8 | for(int child:ed[source]) 9 | if(!visited[child]) dfs(child); 10 | } 11 | bool validPath(int n, vector>& edges, int source, int destination) 12 | { 13 | for(auto v:edges) 14 | { 15 | ed[v[0]].push_back(v[1]); 16 | ed[v[1]].push_back(v[0]); 17 | } 18 | dfs(source); 19 | return visited[destination]; 20 | } 21 | }; -------------------------------------------------------------------------------- /Greedy/1749. Maximum Absolute Sum of Any Subarray.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxAbsoluteSum(vector& nums) 4 | { 5 | int sum = 0, ans = INT_MIN; 6 | for(auto &val : nums) 7 | { 8 | sum += val; 9 | if(sum < 0) sum = 0; 10 | ans = max(ans, abs(sum)); 11 | } 12 | 13 | sum = 0; 14 | for(auto &val : nums) 15 | { 16 | sum += val; 17 | if(sum < 0) ans = max(ans, abs(sum)); 18 | else sum = 0; 19 | } 20 | return ans; 21 | } 22 | }; -------------------------------------------------------------------------------- /Greedy/2139. Minimum Moves to Reach Target Score.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minMoves(int target, int maxDoubles) 4 | { 5 | int ans = 0; 6 | while(target) 7 | { 8 | if(target % 2) 9 | { 10 | ans += 1, target -= 1; 11 | continue; 12 | } 13 | if(not maxDoubles) break; 14 | 15 | ans += 1, target /= 2; 16 | maxDoubles -= 1; 17 | } 18 | return ans + target - 1; 19 | } 20 | }; -------------------------------------------------------------------------------- /Greedy/781. Rabbits in Forest.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numRabbits(vector& answers) 4 | { 5 | int ans = 0; 6 | unordered_map count; 7 | for(auto &val : answers) 8 | { 9 | if(count[val] > val) count[val] = 0; 10 | if(not count[val]) ans += val + 1; 11 | count[val] += 1; 12 | } 13 | return ans; 14 | } 15 | }; -------------------------------------------------------------------------------- /Greedy/781. Rabbits in Forest.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numRabbits(self, answers: List[int]) -> int: 3 | ans = 0 4 | count = defaultdict(int) 5 | for val in answers: 6 | if count[val] > val: 7 | count[val] = 0 8 | if not count[val]: 9 | ans += val + 1 10 | count[val] += 1 11 | return ans -------------------------------------------------------------------------------- /Hash Table/1524. Number of Sub-arrays With Odd Sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numOfSubarrays(vector& nums) 4 | { 5 | int even = 1, odd = 0; 6 | long sum = 0, ans = 0, mod = 1e9 + 7; 7 | for(auto val : nums) 8 | { 9 | sum += val; 10 | sum % 2 ? odd++ : even++; 11 | if(sum % 2) ans = (ans + even) % mod; 12 | else ans += odd; 13 | } 14 | return ans % mod; 15 | } 16 | }; -------------------------------------------------------------------------------- /Hash Table/1711. Count Good Meals.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countPairs(vector& deliciousness) 4 | { 5 | long ans = 0, mod = 1e9 + 7; 6 | unordered_map count; 7 | for(auto &val : deliciousness) 8 | { 9 | int curPow = 1; 10 | while(curPow + curPow < 1e7) 11 | { 12 | ans += (count[(curPow - val)]); 13 | curPow += curPow; 14 | } 15 | ans %= mod; 16 | count[val] += 1; 17 | } 18 | return ans; 19 | } 20 | }; -------------------------------------------------------------------------------- /Hash Table/1726. Tuple with Same Product.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int tupleSameProduct(vector& nums) 4 | { 5 | int ans = 0, n = nums.size(); 6 | unordered_map store; 7 | for(int i = 0; i < n; i++) 8 | for(int j = i + 1; j < n; j++) 9 | store[long(nums[i]) * nums[j]] += 1; 10 | 11 | for(int i = 0; i < n; i++) 12 | for(int j = i + 1; j < n; j++) 13 | ans += (store[long(nums[i]) * nums[j]] - 1) * 4; 14 | return ans; 15 | } 16 | }; -------------------------------------------------------------------------------- /Hash Table/1726. Tuple with Same Product.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def tupleSameProduct(self, nums: List[int]) -> int: 3 | ans, n = 0, len(nums) 4 | store = defaultdict(int) 5 | for i in range(n): 6 | for j in range(i + 1, n): 7 | store[nums[i] * nums[j]] += 1 8 | 9 | for i in range(n): 10 | for j in range(i + 1, n): 11 | ans += (store[nums[i] * nums[j]] - 1) * 4 12 | return ans -------------------------------------------------------------------------------- /Hash Table/2364. Count Number of Bad Pairs.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long long countBadPairs(vector& nums) 4 | { 5 | int n = nums.size(); 6 | unordered_map store; 7 | long ans = (n * long(n - 1)) / 2; 8 | for(int i = n - 1; i >= 0; i--) 9 | { 10 | ans -= store[nums[i] - i]; 11 | store[nums[i] - i] += 1; 12 | } 13 | return ans; 14 | } 15 | }; -------------------------------------------------------------------------------- /Hash Table/2965. Find Missing and Repeated Values.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findMissingAndRepeatedValues(self, grid: List[List[int]]) -> List[int]: 3 | ans = [0, 0] 4 | store = defaultdict(int) 5 | for row in grid: 6 | for val in row: 7 | store[val] += 1 8 | 9 | val = len(grid) * len(grid) 10 | while val: 11 | if store[val] == 0: 12 | ans[1] = val 13 | elif store[val] == 2: 14 | ans[0] = val 15 | val -= 1 16 | return ans -------------------------------------------------------------------------------- /Heap (Priority Queue)/3066. Minimum Operations to Exceed Threshold Value II.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minOperations(self, nums: List[int], k: int) -> int: 3 | ans = 0 4 | arr = SortedList(nums) 5 | while len(arr) > 1 and k > arr[0]: 6 | ans += 1 7 | arr.add((arr[0] * 2) + arr[1]) 8 | arr.discard(arr[1]) 9 | arr.discard(arr[0]) 10 | return ans -------------------------------------------------------------------------------- /Math/1513. Number of Substrings With Only 1s.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numSub(string s) 4 | { 5 | int mod = 1e9 + 7; 6 | long ans = 0, count = 0, n = s.size(); 7 | for(int i = 0; i <= n; i++) 8 | { 9 | if(s[i] == '1') count += 1; 10 | else 11 | { 12 | ans += (count * (count + 1) / 2) % mod; 13 | count = 0; 14 | } 15 | } 16 | return ans % mod; 17 | } 18 | }; -------------------------------------------------------------------------------- /Math/1688. Count of Matches in Tournament.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numberOfMatches(int n) { 4 | return n - 1; 5 | } 6 | }; -------------------------------------------------------------------------------- /Math/1759. Count Number of Homogenous Substrings.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countHomogenous(string s) 4 | { 5 | char prev = '.'; 6 | long ans = 0, count = 0; 7 | int mod = 1e9 + 7, n = s.size(); 8 | for(int i = 0; i <= n; i++) 9 | { 10 | if(s[i] == prev) count += 1; 11 | else 12 | { 13 | ans += (count * (count + 1) / 2); 14 | count = 1, prev = s[i]; 15 | } 16 | } 17 | return ans % mod; 18 | } 19 | }; -------------------------------------------------------------------------------- /Math/1780. Check if Number is a Sum of Powers of Three.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool checkPowersOfThree(int n) 4 | { 5 | int cur_pow = 15; 6 | while(cur_pow--) 7 | { 8 | if(pow(3, cur_pow) > n) continue; 9 | n -= pow(3, cur_pow); 10 | } 11 | return n == 0; 12 | } 13 | }; -------------------------------------------------------------------------------- /Math/1837. Sum of Digits in Base K.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int sumBase(int n, int k) 4 | { 5 | int ans = 0; 6 | while(n) 7 | ans += n % k, n /= k; 8 | return ans; 9 | } 10 | }; -------------------------------------------------------------------------------- /Math/1922. Count Good Numbers.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countGoodNumbers(self, n: int) -> int: 3 | mod = 1000000007 4 | even, odd = n / 2, (n / 2) + (n % 2) 5 | 6 | return pow(5, int(odd), mod) * pow(4, int(even), mod) % mod -------------------------------------------------------------------------------- /Math/2139. Minimum Moves to Reach Target Score.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minMoves(int target, int maxDoubles) 4 | { 5 | int ans = 0; 6 | while(target) 7 | { 8 | if(target % 2) 9 | { 10 | ans += 1, target -= 1; 11 | continue; 12 | } 13 | if(not maxDoubles) break; 14 | 15 | ans += 1, target /= 2; 16 | maxDoubles -= 1; 17 | } 18 | return ans + target - 1; 19 | } 20 | }; -------------------------------------------------------------------------------- /Math/2455. Average Value of Even Numbers That Are Divisible by Three.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int averageValue(vector& nums) 4 | { 5 | int count = 0, sum = 0; 6 | for(auto &val : nums) 7 | if(not(val % 2) and not(val % 3)) 8 | sum += val, count += 1; 9 | return count ? sum / count : count; 10 | } 11 | }; -------------------------------------------------------------------------------- /Math/2481. Minimum Cuts to Divide a Circle.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numberOfCuts(int n) 4 | { 5 | return (n > 1 and n % 2) ? n : n / 2; 6 | } 7 | }; -------------------------------------------------------------------------------- /Math/2544. Alternating Digit Sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int alternateDigitSum(int n) 4 | { 5 | string s = to_string(n); 6 | int ans = 0, flag = true, val; 7 | for(char ch:s) 8 | { 9 | val = ch - '0'; 10 | ans += (flag ? val : -val); 11 | flag = !flag; 12 | } 13 | return ans; 14 | } 15 | }; -------------------------------------------------------------------------------- /Math/2579. Count Total Number of Colored Cells.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long long coloredCells(int n) 4 | { 5 | long ans = 1, color = 4; 6 | for(int i = 1; i < n; i++) 7 | ans += color, color += 4; 8 | return ans; 9 | } 10 | }; -------------------------------------------------------------------------------- /Math/2579. Count Total Number of Colored Cells.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def coloredCells(self, n: int) -> int: 3 | ans, color = 1, 4 4 | for i in range(1, n): 5 | ans += color 6 | color += 4 7 | return ans -------------------------------------------------------------------------------- /Math/2843. Count Symmetric Integers.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countSymmetricIntegers(int low, int high) 4 | { 5 | int ans = 0; 6 | for(int num = low; num <= high; num++) 7 | { 8 | 9 | auto s = to_string(num); 10 | int leftSum = 0, rightSum = 0, n = s.size(); 11 | if(n % 2) continue; 12 | for(int i = 0; i < n / 2; i++) 13 | { 14 | leftSum += s[i]; 15 | rightSum += s[n - i - 1]; 16 | } 17 | ans += leftSum == rightSum; 18 | } 19 | return ans; 20 | } 21 | }; -------------------------------------------------------------------------------- /Math/2894. Divisible and Non-divisible Sums Difference.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int differenceOfSums(int n, int m) 4 | { 5 | int sum1 = 0, sum2 = 0; 6 | for(int num = 1; num <= n; num++) 7 | num % m ? sum1 += num : sum2 += num; 8 | return sum1 - sum2; 9 | } 10 | }; -------------------------------------------------------------------------------- /Math/2894. Divisible and Non-divisible Sums Difference.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def differenceOfSums(self, n: int, m: int) -> int: 3 | num1 = [num for num in range(1, n + 1) if num % m] 4 | num2 = [num for num in range(1, n + 1) if not num % m] 5 | 6 | return sum(num1) - sum(num2) -------------------------------------------------------------------------------- /Math/3492. Maximum Containers on a Ship.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int maxContainers(int n, int w, int maxWeight) 5 | { 6 | n *= n; 7 | if (n * w <= maxWeight) 8 | return n; 9 | return maxWeight / w; 10 | } 11 | }; -------------------------------------------------------------------------------- /Math/3512. Minimum Operations to Make Array Sum Divisible by K.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minOperations(vector& nums, int k) 4 | { 5 | int sum = 0; 6 | for(auto &val : nums) sum += val; 7 | return sum % k; 8 | } 9 | }; -------------------------------------------------------------------------------- /Math/3512. Minimum Operations to Make Array Sum Divisible by K.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minOperations(self, nums: List[int], k: int) -> int: 3 | return sum(nums) % k -------------------------------------------------------------------------------- /Math/3516. Find Closest Person.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findClosest(int x, int y, int z) 4 | { 5 | int dif = abs(z - x) - abs(z - y); 6 | if(dif) return dif < 0 ? 1 : 2; 7 | return 0; 8 | } 9 | }; -------------------------------------------------------------------------------- /Math/3550. Smallest Index With Digit Sum Equal to Index.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int smallestIndex(vector& nums) 4 | { 5 | int n = nums.size(); 6 | for(int i = 0; i < n; i++) 7 | { 8 | int sum = 0; 9 | auto s = to_string(nums[i]); 10 | for(auto &ch : s) 11 | sum += ch - '0'; 12 | if(sum == i) return i; 13 | } 14 | return -1; 15 | } 16 | }; -------------------------------------------------------------------------------- /Matrix/73. Set Matrix Zeroes.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def setZeroes(self, matrix: List[List[int]]) -> None: 3 | row, col = set(), set() 4 | n, m = len(matrix), len(matrix[0]) 5 | 6 | for i in range(n): 7 | for j in range(m): 8 | if not matrix[i][j]: 9 | row.add(i) 10 | col.add(j) 11 | 12 | for i in row: 13 | for j in range(m): 14 | matrix[i][j] = 0 15 | for j in col: 16 | for i in range(n): 17 | matrix[i][j] = 0 -------------------------------------------------------------------------------- /MySQL/1148. Article Views I.sql: -------------------------------------------------------------------------------- 1 | select distinct author_id as id from Views 2 | where author_id = viewer_id group by article_id order by id; -------------------------------------------------------------------------------- /MySQL/1741. Find Total Time Spent by Each Employee.sql: -------------------------------------------------------------------------------- 1 | select event_day as day, emp_id, SUM(out_time - in_time) as total_time 2 | from Employees group by emp_id, event_day; -------------------------------------------------------------------------------- /MySQL/176. Second Highest Salary.sql: -------------------------------------------------------------------------------- 1 | -- Code 1: 2 | select coalesce( 3 | (select max(salary) from employee 4 | where salary < (select max(salary) from employee)), null) as SecondHighestSalary; 5 | 6 | -- Code 2: 7 | select coalesce( 8 | (select distinct salary from employee 9 | order by salary desc limit 1, 1), null) as SecondHighestSalary; -------------------------------------------------------------------------------- /MySQL/181. Employees Earning More Than Their Managers.sql: -------------------------------------------------------------------------------- 1 | select EM.name as Employee from Employee as EM, Employee as MN 2 | where EM.managerId = MN.id and EM.salary > MN.salary; -------------------------------------------------------------------------------- /MySQL/182. Duplicate Emails.sql: -------------------------------------------------------------------------------- 1 | select email from Person 2 | group by email having count(email) > 1; -------------------------------------------------------------------------------- /MySQL/183. Customers Who Never Order.sql: -------------------------------------------------------------------------------- 1 | select Customers.name as Customers from Customers 2 | left join Orders on Customers.id = Orders.customerId 3 | group by Customers.id having not count(Orders.customerId) or null; -------------------------------------------------------------------------------- /MySQL/184. Department Highest Salary.sql: -------------------------------------------------------------------------------- 1 | select dp.name as Department, em.name as Employee, em.salary 2 | from Employee as em join Department as dp 3 | on em.departmentId = dp.id 4 | where em.salary = ( select max(salary) from Employee where departmentId = dp.id ) -------------------------------------------------------------------------------- /MySQL/1890. The Latest Login in 2020.sql: -------------------------------------------------------------------------------- 1 | select user_id, max(time_stamp) as last_stamp from Logins 2 | where year(time_stamp) = 2020 group by user_id; 3 | -------------------------------------------------------------------------------- /MySQL/584. Find Customer Referee.sql: -------------------------------------------------------------------------------- 1 | /* 2 | Find the names of the customer that are not referred by the customer with id = 2. 3 | 4 | Return the result table in any order. 5 | 6 | The result format is in the following example. 7 | */ 8 | 9 | select name from Customer where referee_id != 2 or referee_id is null; -------------------------------------------------------------------------------- /MySQL/595. Big Countries.sql: -------------------------------------------------------------------------------- 1 | select name, population, area from World 2 | where population >= 25000000 or area >= 3000000; -------------------------------------------------------------------------------- /MySQL/596. Classes More Than 5 Students.sql: -------------------------------------------------------------------------------- 1 | select class from Courses group by class having count(*) > 4; -------------------------------------------------------------------------------- /Policy-Based Data Structures (PBDS)/2563. Count the Number of Fair Pairs.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countFairPairs(self, nums: List[int], lower: int, upper: int) -> int: 3 | ans = 0 4 | nums.sort() 5 | pbds = SortedList() 6 | for val in nums: 7 | min_val, max_val = lower - val, upper - val 8 | ans += pbds.bisect_right(max_val) - pbds.bisect_left(min_val) 9 | pbds.add(val) 10 | return ans -------------------------------------------------------------------------------- /Policy-Based Data Structures (PBDS)/PBDS_Templated.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | using namespace __gnu_pbds; 6 | 7 | using namespace std; 8 | 9 | template using pbds = tree, rb_tree_tag, tree_order_statistics_node_update>; 10 | 11 | int main() { 12 | 13 | return 0; 14 | } -------------------------------------------------------------------------------- /Prefix Sum/1413. Minimum Value to Get Positive Step by Step Sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minStartValue(vector& nums) 4 | { 5 | int sum = 0, minSum = (100 * 100); 6 | for(auto &val : nums) 7 | { 8 | sum += val; 9 | minSum = min(minSum, sum); 10 | } 11 | return abs(min(minSum, 0) - 1); 12 | } 13 | }; -------------------------------------------------------------------------------- /Prefix Sum/2145. Count the Hidden Sequences.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numberOfArrays(vector& differences, int lower, int upper) 4 | { 5 | long minPref = 0, maxPref = 0, pref = 0; 6 | for(auto &diff : differences) 7 | { 8 | pref += diff; 9 | minPref = min(minPref, pref); 10 | maxPref = max(maxPref, pref); 11 | } 12 | return max((upper - maxPref) - (lower - minPref) + 1, long(0)); 13 | } 14 | }; -------------------------------------------------------------------------------- /Prefix Sum/3355. Zero Array Transformation I.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isZeroArray(vector& nums, vector>& queries) 4 | { 5 | int n = nums.size(); 6 | vector difArray(n + 1); 7 | for(auto &que : queries) 8 | { 9 | difArray[que[0]] += 1; 10 | difArray[que[1] + 1] -= 1; 11 | } 12 | 13 | for(int i = 1; i < n; i++) 14 | difArray[i] += difArray[i - 1]; 15 | 16 | for(int i = 0; i < n; i++) 17 | if(nums[i] > difArray[i]) return false; 18 | 19 | return true; 20 | } 21 | }; -------------------------------------------------------------------------------- /Prefix Sum/3355. Zero Array Transformation I.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isZeroArray(self, nums: List[int], queries: List[List[int]]) -> bool: 3 | n = len(nums) 4 | difArray = [0] * (n + 1) 5 | 6 | for l, r in queries: 7 | difArray[l] += 1 8 | difArray[r + 1] -= 1 9 | 10 | for i in range(1, n): 11 | difArray[i] += difArray[i - 1] 12 | 13 | for i in range(n): 14 | if nums[i] > difArray[i]: 15 | return False 16 | return True -------------------------------------------------------------------------------- /Recursion/38. Count and Say.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string countAndSay(int n) 4 | { 5 | if(n==1) return "1"; 6 | 7 | string ans, say = countAndSay(n-1); 8 | 9 | int cnt = 1, nn = say.size(); 10 | char ch = say[0]; 11 | for(int i=1;i str: 3 | if n == 1: 4 | return "1" 5 | s = self.countAndSay(n - 1) 6 | 7 | count, l = 1, len(s) 8 | convert, prev = "", s[0] 9 | for i in range(1, l): 10 | if s[i] == prev: 11 | count += 1 12 | else: 13 | convert += str(count) + prev 14 | prev = s[i] 15 | count = 1 16 | return convert + str(count) + prev -------------------------------------------------------------------------------- /Simulation/1920. Build Array from Permutation.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector buildArray(vector& nums) 4 | { 5 | vector ans; 6 | for(int i = 0; i < nums.size(); i++) 7 | ans.push_back(nums[nums[i]]); 8 | return ans; 9 | } 10 | }; -------------------------------------------------------------------------------- /Simulation/1920. Build Array from Permutation.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def buildArray(self, nums: List[int]) -> List[int]: 3 | return [nums[index] for index in nums] -------------------------------------------------------------------------------- /Sliding Window/2302. Count Subarrays With Score Less Than K.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long long countSubarrays(vector& nums, long long k) 4 | { 5 | long long ans = 0, sum = 0; 6 | int i = 0, j = 0, n = nums.size(); 7 | while(j < n) 8 | { 9 | sum += nums[j]; 10 | while(sum * (j - i + 1) >= k) 11 | sum -= nums[i++]; 12 | ans += j - i + 1; 13 | j++; 14 | } 15 | return ans; 16 | } 17 | }; -------------------------------------------------------------------------------- /Sliding Window/2302. Count Subarrays With Score Less Than K.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countSubarrays(self, nums: List[int], k: int) -> int: 3 | i, j, sum = 0, 0, 0 4 | ans, n = 0, len(nums) 5 | while j < n: 6 | sum += nums[j] 7 | while (j - i + 1) * sum >= k: 8 | sum -= nums[i] 9 | i += 1 10 | ans += j - i + 1 11 | j += 1 12 | return ans -------------------------------------------------------------------------------- /Sliding Window/2379. Minimum Recolors to Get K Consecutive Black Blocks.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minimumRecolors(string blocks, int k) 4 | { 5 | int cnt = 0, ans = INT_MAX; 6 | int i = 0, j = 0, n = blocks.size(); 7 | while(j < n) 8 | { 9 | if(blocks[j++] == 'W') cnt++; 10 | while(j - i > k) 11 | if(blocks[i++] == 'W') cnt--; 12 | if(j - i == k) ans = min(ans, cnt); 13 | } 14 | return ans; 15 | } 16 | }; -------------------------------------------------------------------------------- /Sliding Window/2379. Minimum Recolors to Get K Consecutive Black Blocks.js: -------------------------------------------------------------------------------- 1 | var minimumRecolors = function(blocks, k) 2 | { 3 | let n = blocks.length; 4 | let i = 0, j = 0, ans = k, count = 0; 5 | while(j < n) 6 | { 7 | count += blocks[j++] == 'W'; 8 | if(j - i == k) 9 | { 10 | ans = Math.min(ans, count); 11 | count -= blocks[i++] == 'W'; 12 | } 13 | } 14 | return ans; 15 | }; -------------------------------------------------------------------------------- /Sliding Window/2379. Minimum Recolors to Get K Consecutive Black Blocks.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minimumRecolors(self, blocks: str, k: int) -> int: 3 | cnt = 0 4 | ans, i, j, n, = sys.maxsize, 0, 0, len(blocks) 5 | while j < n: 6 | if blocks[j] == 'W': 7 | cnt += 1 8 | if j - i + 1 == k: 9 | ans = min(ans, cnt) 10 | if blocks[i] == 'W': 11 | cnt -= 1 12 | i += 1 13 | j += 1 14 | return ans -------------------------------------------------------------------------------- /Sliding Window/2537. Count the Number of Good Subarrays.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long long countGood(vector& nums, int k) 4 | { 5 | long ans = 0, pairs = 0; 6 | unordered_map count; 7 | int i = 0, j = 0, n = nums.size(); 8 | while(j < n) 9 | { 10 | pairs += count[nums[j]]; 11 | count[nums[j++]] += 1; 12 | while(pairs >= k) 13 | { 14 | ans += (n - j) + 1; 15 | pairs -= --count[nums[i++]]; 16 | } 17 | } 18 | return ans; 19 | } 20 | }; -------------------------------------------------------------------------------- /Sliding Window/2962. Count Subarrays Where Max Element Appears at Least K Times.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countSubarrays(self, nums: List[int], k: int) -> int: 3 | ans, n = 0, len(nums) 4 | maxVal = max(nums) 5 | 6 | i, j = 0, 0 7 | count = Counter() 8 | while j < n: 9 | count[nums[j]] += 1 10 | while count[maxVal] == k: 11 | count[nums[i]] -= 1 12 | i += 1 13 | ans += i 14 | j += 1 15 | return ans -------------------------------------------------------------------------------- /Sliding Window/3208. Alternating Groups II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numberOfAlternatingGroups(vector& nums, int k) 4 | { 5 | int cnt = 0, ans = 0; 6 | int i = 0, j = 0, n = nums.size(); 7 | for(int i=0;i= k) ans++; 14 | } 15 | return ans; 16 | } 17 | }; -------------------------------------------------------------------------------- /Sorting/1984. Minimum Difference Between Highest and Lowest of K Scores.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int minimumDifference(vector &nums, int k) 5 | { 6 | sort(begin(nums), end(nums)); 7 | 8 | int ans = INT_MAX, n = nums.size(); 9 | for (int i = 0; i <= n - k; i++) 10 | ans = min(ans, (nums[i + k - 1] - nums[i])); 11 | 12 | return ans; 13 | } 14 | }; -------------------------------------------------------------------------------- /Sorting/3169. Count Days Without Meetings.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countDays(self, days: int, meetings: List[List[int]]) -> int: 3 | meetings.sort() 4 | ans, last_meetings = 0, 0 5 | for start, end in meetings: 6 | if start > last_meetings: 7 | ans += start - last_meetings - 1 8 | last_meetings = max(last_meetings, end) 9 | 10 | return ans + (days - last_meetings) -------------------------------------------------------------------------------- /String/1358. Number of Substrings Containing All Three Characters.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numberOfSubstrings(string s) 4 | { 5 | int n = s.size(), ans = 0; 6 | vector lastSeen(100); 7 | for(int i = 0; i <= n; i++) 8 | { 9 | ans += min({lastSeen['a'], lastSeen['b'], lastSeen['c']}); 10 | lastSeen[s[i]] = i + 1; 11 | } 12 | return ans; 13 | } 14 | }; -------------------------------------------------------------------------------- /String/1910. Remove All Occurrences of a Substring.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string removeOccurrences(string s, string part) 4 | { 5 | string store; 6 | int n = s.size(), m = part.size(); 7 | for(int i = 0; i < n; i++) 8 | { 9 | store += s[i]; 10 | if(store.size() < m) continue; 11 | string temp = store.substr(store.size() - m); 12 | if(temp == part) store.erase(store.begin() + store.size() - m, store.end()); 13 | } 14 | return store; 15 | } 16 | }; -------------------------------------------------------------------------------- /String/2942. Find Words Containing Character.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector findWordsContaining(vector& words, char x) 4 | { 5 | vector ans; 6 | int n = words.size(); 7 | for(int i = 0; i < n; i++) 8 | { 9 | unordered_setst(begin(words[i]), end(words[i])); 10 | if(st.count(x)) ans.push_back(i); 11 | } 12 | return ans; 13 | } 14 | }; -------------------------------------------------------------------------------- /String/2942. Find Words Containing Character.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findWordsContaining(self, words: List[str], x: str) -> List[int]: 3 | return [i for i in range(len(words)) if x in words[i]] -------------------------------------------------------------------------------- /String/3174. Clear Digits.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string clearDigits(string s) 4 | { 5 | string ans; 6 | for(char ch:s) 7 | { 8 | if(isalpha(ch)) ans += ch; 9 | else if(ans.size()) ans.pop_back(); 10 | } 11 | return ans; 12 | } 13 | }; -------------------------------------------------------------------------------- /String/3174. Clear Digits.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def clearDigits(self, s: str) -> str: 3 | ans = [] 4 | for ch in s: 5 | if ch.isdigit() and ans: ans.pop() 6 | else: ans.append(ch) 7 | return ''.join(ans) -------------------------------------------------------------------------------- /String/3461. Check If Digits Are Equal in String After Operations I.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool hasSameDigits(string s) 4 | { 5 | int n = s.size(); 6 | while(n > 2) 7 | { 8 | string temp; 9 | for(int i = 1; i < n; i++) 10 | temp += to_string(((s[i] - '0') + (s[i - 1] - '0')) % 10); 11 | s = temp; 12 | n = s.size(); 13 | } 14 | return s[0] == s[1]; 15 | } 16 | }; -------------------------------------------------------------------------------- /String/3517. Smallest Palindromic Rearrangement I.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string smallestPalindrome(string s) 4 | { 5 | vector count(26); 6 | for(auto &ch : s) count[ch - 'a'] += 1; 7 | 8 | int cur = 0, n = s.size(); 9 | for(int i = 0; i < 26; i++) 10 | { 11 | if(count[i] % 2) s[n / 2] = i + 'a'; 12 | while(count[i] > 1) 13 | { 14 | s[cur] = i + 'a'; 15 | s[n - cur - 1] = i + 'a'; 16 | count[i] -= 2; 17 | cur += 1; 18 | } 19 | } 20 | return s; 21 | } 22 | }; -------------------------------------------------------------------------------- /String/3536. Maximum Product of Two Digits.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProduct(int n) 4 | { 5 | auto s = to_string(n); 6 | sort(begin(s), end(s)); 7 | return ((s[s.size() - 1] - '0') * (s[s.size() - 2] - '0')); 8 | } 9 | }; -------------------------------------------------------------------------------- /Tree/1261. Find Elements in a Contaminated Binary Tree.cpp: -------------------------------------------------------------------------------- 1 | class FindElements { 2 | public: 3 | unordered_set store; 4 | void DFS(TreeNode* &root, int val) 5 | { 6 | if(not root) return; 7 | store.insert(val); 8 | DFS(root->left, val * 2 + 1); 9 | DFS(root->right, val * 2 + 2); 10 | } 11 | FindElements(TreeNode* root) { 12 | store.insert(0); 13 | DFS(root->left, 1); 14 | DFS(root->right, 2); 15 | } 16 | 17 | bool find(int target) { 18 | return store.count(target); 19 | } 20 | }; -------------------------------------------------------------------------------- /Two Pointer/1790. Check if One String Swap Can Make Strings Equal.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool areAlmostEqual(string s1, string s2) 4 | { 5 | if(s1 == s2) return true; 6 | 7 | int i = 0, n = max(s1.size(), s2.size()); 8 | while(i < n and s1[i] == s2[i]) i++; 9 | 10 | int j = i + 1; 11 | while(j < n and s1[j] == s2[j]) j++; 12 | 13 | swap(s1[i], s1[j]); 14 | 15 | return s1 == s2; 16 | } 17 | }; --------------------------------------------------------------------------------