Skip to content

comitanigiacomo/leetcode

Repository files navigation

LeetCode Problem Solutions

Welcome to my repository of LeetCode problem solutions. Here, you can find my solutions to various coding problems from LeetCode, along with explanations and code snippets.

Leetcode Stats

You can view my LeetCode account by clicking here

LeetCode Progress Tracker 📅

🎯 Problem ID 📌 Title 🏷️ Tags ⚡ Difficulty 📝 Solution
2849 determine if a cell is reachable at a given time Math Medium solution
2785 sort vowels in a string String Sorting Medium solution
2433 find the original array of prefix xor Array Bit Manipulation Medium solution
2391 minimum amount of time to collect garbage Array String Prefix Sum Medium solution
2265 count nodes equal to average of subtree Tree Depth-First Search Binary Tree Medium solution
1887 reduction operations to make the array elements equal Array Sorting Medium solution
1877 minimize maximum pair sum in array Array Two Pointers Greedy Sorting Medium solution
1846 maximum element after decreasing and rearranging Array Greedy Sorting Medium solution
1845 seat reservation manager Design Heap (Priority Queue) Medium solution
1838 frequency of the most frequent element Array Binary Search Greedy Sliding Window Sorting Prefix Sum Medium solution
1833 maximum ice cream bars Array Greedy Sorting Counting Sort Medium solution
1535 find the winner of an array game Array Simulation Medium solution
1503 last moment before all ants fall out of a plank Array Brainteaser Simulation Medium solution
1441 build an array with stack operations Array Stack Simulation Medium solution
1356 sort integers by the number of 1 bits Array Bit Manipulation Sorting Counting Easy solution
815 bus routes Array Hash Table Breadth-First Search Hard solution
501 find mode in binary search tree Tree Depth-First Search Binary Search Tree Binary Tree Easy solution
1814 count nice pairs in an array Array Hash Table Math Counting Medium solution
1424 diagonal traverse II Array Sorting Heap (Priority Queue) Medium solution
1630 arithmetic subarrays Array Hash Table Sorting Medium solution
1561 maximum number of coins you can get Array Math Greedy Sorting Game Theory Medium solution
1727 largest submatrix with rearrangements Array Greedy Sorting Matrix Medium solution
935 knight dialer Dynamic Programming Medium solution
2147 number of ways to divide a long corridor Math String Dynamic Programming Hard solution
191 number of 1 bits Divide and Conquer Bit Manipulation Easy solution
1685 sum of absolute differences in a sorted array Array Math Prefix Sum Medium solution
1611 minimum one bit operations to make integers zero Dynamic Programming Bit Manipulation Memoization Hard solution
1160 find words that can be formed by characters Array Hash Table String Counting Easy solution
1266 minimum time visiting all points Array Math Geometry Easy solution
2264 largest 3-same-digit number in string String Easy solution
1688 Count of matches in tournament Math Simulation Easy solution
1716 calculate money in leetcode bank Math Easy solution
1903 largest odd number in string Math String Greedy Easy solution
606 construct string from binary tree String Tree Depth-First Search Binary Tree Medium solution
94 binary tree inorder traversal Stack Tree Depth-First Search Binary Tree Easy solution
867 transpose matrix Array Matrix Simulation Easy solution
1287 element appearing more than 25 In sorted array Array Easy solution
1464 maximum product of two elements in an array Array Sorting Heap (Priority Queue) Easy solution
1582 special positions in a binary matrix Array Matrix Easy solution
2482 difference between ones and zeros in row and column Array Matrix Simulation Medium solution
1436 destination city Array Hash Table String Easy solution
242 valid anagram Hash Table String Sorting Easy solution
1913 maximum product difference between two pairs Array Sorting Easy solution
2706 buy two chocolates Array Greedy Sorting Easy solution
1637 widest vertical area between two points containing no points Array Sorting Easy solution
1422 maximum score after splitting a string String Prefix Sum Easy solution
1496 path crossing Hash Table String Easy solution
1758 minimum changes to make alternating binary string String Easy solution
938 range sum of BST Tree Depth-First Search Binary Search Tree Binary Tree Easy solution
872 leaf-Similar trees Tree Depth-First Search Binary Tree Easy solution
2385 amount of time for binary tree to be infected Hash Table Tree Depth-First Search Breadth-First Search Binary Tree Medium solution
1704 determine if string halves are alike String Counting Easy solution
2225 find players with zero or one losses Array Hash Table Sorting Counting Medium solution
1207 unique number of occurrences Array Hash Table Easy solution
70 climbing stairs Math Dynamic Programming Memoization Easy solution
931 minimum falling path sum Array Dynamic Programming Matrix Medium solution
2108 find first palindromic string in the array Array Two Pointers String Easy solution
513 find bottom left tree value Tree Depth-First Search Breadth-First Search Binary Tree Medium solution
1609 even odd tree Tree Breadth-First Search Binary Tree Medium solution
2864 maximum odd binary number Math String Greedy Easy solution
19 remove nth node from end of list Linked List Two Pointers Medium solution
977 squares of a sorted array Array Two Pointers Sorting Easy solution
948 bag of tokens Array Two Pointers Greedy Sorting Medium solution
1750 minimum length of string after deleting similar ends Two Pointers String Medium solution
141 linked list cycle Hash Table Linked List Two Pointers Easy solution
876 middle of the linked list Linked List Two Pointers Easy solution
3005 count elements with maximum frequency Array Hash Table Counting Easy solution
791 custom sort string Hash Table String Sorting Medium solution
1171 remove zero sum consecutive nodes from linked list Hash Table Linked List Medium solution
2485 find the pivot integer Math Prefix Sum Easy solution
930 binary subarrays with sum Array Hash Table Sliding Window Prefix Sum Medium solution
238 product of array except self Array Prefix Sum Medium solution
57 insert interval Array Medium solution
442 find all duplicates in an array Array Hash Table Medium solution
42 trapping rain water Array Two Pointers Dynamic Programming Stack Monotonic Stack Hard solution
404 sum of left leaves Tree Depth-First Search Breadth-First Search Binary Tree Easy solution
129 sum root to leaf numbers Tree Depth-First Search Binary Tree Medium solution
623 add one row to tree Tree Depth-First Search Breadth-First Search Binary Tree Medium solution
988 smallest string starting from leaf String Backtracking Tree Depth-First Search Binary Tree Medium solution
463 island perimeter Array Depth-First Search Breadth-First Search Matrix Easy solution
200 number of islands Array Depth-First Search Breadth-First Search Union Find Matrix Medium solution
1971 find if path exists in graph Depth-First Search Breadth-First Search Union Find Graph Easy solution
752 open the lock Array Hash Table String Breadth-First Search Medium solution
310 minimum height Trees Depth-First Search Breadth-First Search Graph Topological Sort Medium solution
1137 n-th tribonacci number Math Dynamic Programming Memoization Easy solution
1289 minimum falling path sum II Array Dynamic Programming Matrix Hard solution
2997 minimum number of operations to make array XOR equal to k Array Bit Manipulation Medium solution
2441 largest positive integer that exists with its negative Array Hash Table Two Pointers Sorting Easy solution
165 compare version numbers Two Pointers String Medium solution
881 boats to save people Array Two Pointers Greedy Sorting Medium solution
237 delete node in a linked list Linked List Medium solution
2487 remove nodes from linked list Linked List Stack Recursion Monotonic Stack Medium solution
2816 double a number represented as a linked list Linked List Math Stack Medium solution
506 relative ranks Array Sorting Heap (Priority Queue) Easy solution
3075 maximize happiness of selected children Array Greedy Sorting Medium solution
786 k-th smallest prime fraction Array Two Pointers Binary Search Sorting Heap (Priority Queue) Medium solution
857 minimum cost to hire k workers Array Greedy Sorting Heap (Priority Queue) Hard solution
861 score after flipping matrix Array Greedy Bit Manipulation Matrix Medium solution
1219 path with maximum gold Array Backtracking Matrix Medium solution
2331 evaluate boolean binary tree Tree Depth-First Search Binary Tree Easy solution
1325 delete leaves with a given value Tree Depth-First Search Binary Tree Medium solution
1863 sum of all subset xor totals Array Math Backtracking Bit Manipulation Combinatorics Enumeration Easy solution
78 subsets Array Backtracking Bit Manipulation Medium solution
131 palindrome partitioning String Dynamic Programming Backtracking Medium solution
2597 the number of beautiful subsets Array Hash Table Math Dynamic Programming Backtracking Sorting Combinatorics Medium solution
1255 maximum score words formed by letters Array String Dynamic Programming Backtracking Bit Manipulation Bitmask Hard solution
552 student attendance record II Dynamic Programming Hard solution
9 palindrome number Math Easy solution
13 roman to integer Hash Table Math String Easy solution
1608 special array with x elements greater than or equal x Array Binary Search Sorting Easy solution
1208 get equal substrings within budget String Binary Search Sliding Window Prefix Sum Medium solution
1442 count triplets that can form two arrays of equal xor Array Hash Table Math Bit Manipulation Prefix Sum Medium solution
260 single number III Array Bit Manipulation Medium solution
3110 score of a string String Easy solution
344 reverse string Two Pointers String Easy solution
2486 append characters to string to make subsequence Two Pointers String Greedy Medium solution
409 longest palindrome Hash Table String Greedy Easy solution
1002 find common characters Array Hash Table String Easy solution
846 hand of straights Array Hash Table Greedy Sorting Medium solution
648 replace words Array Hash Table String Trie Medium solution
523 continuous subarray sum Array Hash Table Math Prefix Sum Medium solution
974 subarray sums divisible by k Array Hash Table Prefix Sum Medium solution
1051 height checker Array Sorting Counting Sort Easy solution
1222 relative sort array Array Hash Table Sorting Counting Sort Easy solution
2037 minimum number of moves to seat everyone Array Greedy Sorting Counting Sort Easy solution
945 minimum increment to make array unique Array Greedy Sorting Counting Medium solution
502 IPO Array Greedy Sorting Heap (Priority Queue) Hard solution
633 sum of square numbers Math Two Pointers Binary Search Medium solution
826 most profit assigning work Array Two Pointers Binary Search Greedy Sorting Medium solution
1482 minimum number of days to make m bouquets Array Binary Search Medium solution
1552 magnetic force between two balls Array Binary Search Sorting Medium solution
1052 grumpy bookstore owner Array Sliding Window Medium solution
1038 binary search tree to greater sum tree Tree Depth-First Search Binary Search Tree Binary Tree Medium solution
1382 balance a binary search tree Divide and Conquer Greedy Tree Depth-First Search Binary Search Tree Binary Tree Medium solution
1791 find center of star graph Graph Easy solution
2285 maximum total importance of roads Greedy Graph Sorting Heap (Priority Queue) Medium solution
2192 all ancestors of a node in a directed acyclic graph Depth-First Search Breadth-First Search Graph Topological Sort Medium solution
1550 three consecutive odds Array Easy solution
350 intersection of two arrays II Array Hash Table Two Pointers Binary Search Sorting Easy solution
1509 minimum difference between largest and smallest value in three moves Array Greedy Sorting Medium solution
2181 merge nodes in between zeros Linked List Simulation Medium solution
2582 pass the pillow Math Simulation Easy solution
1518 water bottles Math Simulation Easy solution
1190 reverse substrings between each pair of parentheses String Stack Medium solution
40 combination sum II Array Backtracking Medium solution
624 maximum distance in arrays Array Greedy Medium solution
590 n-ary tree postorder traversal Stack Tree Depth-First Search Easy solution
145 binary tree postorder traversal Stack Tree Depth-First Search Binary Tree Easy solution
2022 convert 1D Array Into 2D array Array Matrix Simulation Easy solution
1894 find the student that will replace the chalk Array Binary Search Simulation Prefix Sum Medium solution
1945 sum of digits of string after convert String Simulation Easy solution
874 walking robot simulation Array Hash Table Simulation Medium solution
2028 find missing observations Array Math Simulation Medium solution
3217 delete nodes from linked list present in array Array Hash Table Linked List Medium solution
2326 spiral matrix IV Array Linked List Matrix Simulation Medium solution
2807 insert greatest common divisors in linked list Linked List Math Number Theory Medium solution
2220 minimum bit flips to convert number Bit Manipulation Easy solution
1684 count the number of consistent strings Array Hash Table String Bit Manipulation Counting Easy solution
1310 xor queries of a subarray Array Bit Manipulation Prefix Sum Medium solution
2419 longest subarray with maximum bitwise and Array Bit Manipulation Brainteaser Medium solution
1371 find the longest substring containing vowels in even counts Hash Table String Bit Manipulation Prefix Sum Medium solution
179 largest number Array String Greedy Sorting Medium solution
241 different ways to add parentheses Math String Dynamic Programming Recursion Memoization Medium solution
214 shortest palindrome String Rolling Hash String Matching Hash Function Hard solution
2707 extra characters in a string Array Hash Table String Dynamic Programming Trie Medium solution
118 pascals triangle Array Dynamic Programming Easy solution
729 my calendar I Array Binary Search Design Segment Tree Ordered Set Medium solution
731 my calendar II Array Binary Search Design Segment Tree Prefix Sum Ordered Set Medium solution
1381 design a stack with increment operation Array Stack Design Medium solution
884 uncommon words from two sentences Hash Table String Counting Easy solution
725 split linked list in parts Linked List Medium solution
1497 check if array pairs are divisible by k Array Hash Table Counting Medium solution
1331 rank transform of an array Array Hash Table Sorting Easy solution
1590 make sum divisible by p Array Hash Table Prefix Sum Medium solution
2696 minimum string length after removing substrings String Stack Simulation Easy solution
1963 minimum number of swaps to make the string balanced Two Pointers String Stack Greedy Medium solution
921 minimum add to make parentheses valid String Stack Greedy Medium solution
962 maximum width ramp Array Two Pointers Stack Monotonic Stack Medium solution
2914 minimum number of changes to make binary string beautiful String Medium solution
2684 maximum number of moves in a grid Array Dynamic Programming Matrix Medium solution
1415 the k-th lexicographical string of all happy strings of length n String Backtracking Medium solution
1980 find unique binary string Array Hash Table String Backtracking Medium solution
1261 find elements in a contaminated binary tree Hash Table Tree Depth-First Search Breadth-First Search Design Binary Tree Medium solution
1524 number of sub-arrays with odd sum Array Math Dynamic Programming Prefix Sum Medium solution
2460 apply operations to an array Array Two Pointers Simulation Easy solution
2570 merge two 2D arrays by summing values Array Hash Table Two Pointers Easy solution
2161 partition array according to given pivot Array Two Pointers Simulation Medium solution
3011 find if array can be sorted Array Bit Manipulation Sorting Medium solution
1652 defuse the bomb Array Sliding Window Easy solution
2461 maximum sum of distinct subarrays with length K Array Hash Table Sliding Window Medium solution
2516 take k of each character from left and right Hash Table String Sliding Window Medium solution
1346 check if n and its double exist Array Hash Table Two Pointers Binary Search Sorting Easy solution
1455 check if a word occurs as a prefix of any word in a sentence Two Pointers String String Matching Easy solution
2109 adding spaces to a string Array Two Pointers String Simulation Medium solution
2825 make string a subsequence using cyclic increments Two Pointers String Medium solution
3254 find the power of k size subarrays i Array Sliding Window Medium solution
1662 check if two string arrays are equivalent Array String Easy solution
2058 find the minimum and maximum number of nodes between critical points Linked List Medium solution
1780 check if number is a sum of powers of three Math Medium solution
2965 find missing and repeated values Array Hash Table Math Matrix Easy solution
2579 count total number of colored cells Math Medium solution
3355 zero array transformation i Array Prefix Sum Medium solution
2529 maximum count of positive integer and negative integer Array Binary Search Counting Easy solution
73 set matrix zeroes Array Hash Table Matrix Medium solution
560 subarray sum equals k Array Hash Table Prefix Sum Medium solution
2379 minimum recolors to get k consecutive black blocks String Sliding Window Easy solution
2226 maximum candies allocated to k children Array Binary Search Medium solution
303 range sum query immutable Array Design Prefix Sum Easy solution
1920 build array from permutation Array Simulation Easy solution
2523 closest prime numbers in range Math Number Theory Medium solution

Solutions

Here’s a list of some solutions on LeetCode, along with my thought process for each problem

Badges:

Contributors 2

  •  
  •  

Languages