DEV Community

Amruta
Amruta

Posted on • Edited on

DSA Chronicles Day 1 : 5 Essential Problems I Solved

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:

  1. Use an unordered_map to count the frequency of each number.
  2. Push the entries into a priority_queue (max-heap) that prioritizes higher frequency (and higher value in case of ties).
  3. Extract the top k elements from the heap.

Topics I learned:

  1. Unordered map: Helped me count frequencies in linear time—essential for frequency-based problems.
  2. 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:

  1. Count element frequencies using a hashmap.
  2. Use a max-heap to remove the most frequent elements first.
  3. Keep removing until the remaining elements are ≤ half the array size.

Topics I learned:

  1. Greedy selection with heaps: Understood that removing elements with the highest frequency first ensures minimal removal steps.
  2. 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:

  1. Traverse left to right, greedily remove digits where the left digit is larger than the right.
  2. If k is still > 0, remove from the end.
  3. Finally, trim leading zeros.

Topics I learned:

  1. Greedy stack-based approach: Learned how removing bigger digits early helps form a smaller number.
  2. 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:

  1. Use a sliding window and an ASCII array to track last-seen indices of characters.
  2. Shift the window forward whenever a repeating character is found inside the current window.

Topics I learned:

  1. Sliding window technique: Understood how to dynamically maintain a substring using two pointers and optimize space.
  2. 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:

  1. Convert characters to numbers using 'char' - 'a' + 1 rule.
  2. Sum digits repeatedly k times.

Topics I learned:

  1. ASCII to int conversion: Learned how to map characters like 'a' to 1 using basic arithmetic.
  2. 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)

Collapse
 
nathan_tarbert profile image
Nathan Tarbert

Love seeing you put in the reps day after day - makes me want to push more on my own stuff too