Today was a productive day in my DSA journey! I tackled five important problems, each teaching me something new about logic, data structures, and implementation. Some problems I cracked independently, and others with some help — but all were solid learning experiences. Here's a breakdown of what I solved and what I learned:
Problem 1: Top K Frequent Elements in Array
Problem:
Given a non-empty integer array arr[] of size n, find the top k elements which appear most frequently. If two numbers have the same frequency, prefer the larger one.
Link to problem: https://www.geeksforgeeks.org/problems/top-k-frequent-elements-in-array/1
Approach:
- Use an unordered_map to count the frequency of each number.
- Push the entries into a priority_queue (max-heap) that prioritizes higher frequency (and higher value in case of ties).
- Extract the top k elements from the heap.
Topics I learned:
- Unordered map: Helped me count frequencies in linear time—essential for frequency-based problems.
- Priority queue (max-heap): Learned how to structure a custom comparator and use a heap to get the most frequent or largest values efficiently.
Problem 2: Reduce Array Size to Half
Problem:
Given an array arr, remove as few elements as possible (completely) so that the size of the remaining array is at most half the original.
Link to problem: https://leetcode.com/problems/reduce-array-size-to-the-half/description/
Approach:
- Count element frequencies using a hashmap.
- Use a max-heap to remove the most frequent elements first.
- Keep removing until the remaining elements are ≤ half the array size.
Topics I learned:
- Greedy selection with heaps: Understood that removing elements with the highest frequency first ensures minimal removal steps.
- Hashmap + heap pattern: Reinforced how this duo helps in optimizing decisions based on frequency.
Problem 3: Remove K Digits
Problem:
Given a number as a string num and an integer k, remove k digits to form the smallest possible number.
Link to problem : https://leetcode.com/problems/remove-k-digits/
Approach:
- Traverse left to right, greedily remove digits where the left digit is larger than the right.
- If k is still > 0, remove from the end.
- Finally, trim leading zeros.
Topics I learned:
- Greedy stack-based approach: Learned how removing bigger digits early helps form a smaller number.
- String and digit manipulation: Got familiar with trimming zeros and using stack-like behavior on strings.
Problem 4: Longest Substring with Distinct Characters
Problem:
Given a string s, return the length of the longest substring with all unique characters.
Link to problem: https://www.geeksforgeeks.org/problems/longest-distinct-characters-in-string5848/1
Approach:
- Use a sliding window and an ASCII array to track last-seen indices of characters.
- Shift the window forward whenever a repeating character is found inside the current window.
Topics I learned:
- Sliding window technique: Understood how to dynamically maintain a substring using two pointers and optimize space.
- Character-index tracking: Practiced using arrays to map ASCII values for fast lookups.
Problem 5: Sum of Digits of String After Convert
Problem:
Given a lowercase string s and an integer k, convert each character to its alphabet position, sum the digits, and repeat k times.
Link to problem: https://leetcode.com/problems/sum-of-digits-of-string-after-convert/description/
Approach:
- Convert characters to numbers using 'char' - 'a' + 1 rule.
- Sum digits repeatedly k times.
Topics I learned:
- ASCII to int conversion: Learned how to map characters like 'a' to 1 using basic arithmetic.
- Digit transformation: Practiced repeatedly converting and summing numbers using string manipulation.
My code link: https://github.com/Amruta-25/dsa_chronicles.git
Top comments (1)
Love seeing you put in the reps day after day - makes me want to push more on my own stuff too