DEV Community

Amruta
Amruta

Posted on

DSA Chronicles Day 4: Hashing & Stacks Continued

Today was a solid day — tackled 4 more problems that deepened my understanding of hashing and stack-based logic. Covered some variety: hash frequency, prefix sum tricks, and even a stack-based temperature-wait problem. Here’s the breakdown:

Problem 1: Search in Sorted Array

Problem:
There is an integer array nums sorted in ascending order (with distinct values).

Prior to being passed to your function, nums is possibly rotated at an unknown pivot index k (1 <= k < nums.length) such that the resulting array is nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]. For example, [0,1,2,4,5,6,7] might be rotated at pivot index 3 and become [4,5,6,7,0,1,2].

Given the array nums after the possible rotation and an integer target, return the index of target if it is in nums, or -1 if it is not in nums.

You must write an algorithm with O(log n) runtime complexity.

Link to problem: https://leetcode.com/problems/search-in-rotated-sorted-array/description/

Approach:

  1. Used a modified binary search that considers the rotation.
  2. At each step, determine which half of the array is properly sorted.
  3. Based on the sorted half and the target’s value, decide whether to search left or right.
  4. This keeps the time complexity at O(log n).

Topics learned:

  1. Binary Search in rotated arrays
  2. Identifying sorted subarrays within rotated arrays
  3. Time-efficient searching (O(log n))

Problem 2: Daily Temperatures

Problem:
Given an array of integers temperatures represents the daily temperatures, return an array answer such that answer[i] is the number of days you have to wait after the ith day to get a warmer temperature. If there is no future day for which this is possible, keep answer[i] == 0 instead.

Link to problem: https://leetcode.com/problems/daily-temperatures/description/

Approach:

  1. Utilized a monotonic stack to keep track of indices of decreasing temperatures.
  2. For each temperature, pop from the stack until a warmer day is found, then calculate the difference in days.

Topics learned:

  1. Monotonic stack pattern
  2. Stack-based array traversal
  3. Efficient future-lookups in O(n) time

Problem 3: Find the Duplicate Number

Problem:
Given an array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive.

There is only one repeated number in nums, return this repeated number.

You must solve the problem without modifying the array nums and using only constant extra space.

Link to problem: https://leetcode.com/problems/find-the-duplicate-number/description/

Approach:

  1. Implemented Floyd's Cycle Detection Algorithm (Tortoise and Hare).
  2. Interpreted the array as a linked list where indices are nodes and values are pointers.
  3. The cycle formed by the duplicate is detected.
  4. Later the entry point is the repeated number.

Topics Learned:

  1. Cycle detection in arrays using Floyd’s algorithm
  2. Interpreting array problems as graph traversal

Problem 4: Longest Consecutive Sequence

Problem:
Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence.

You must write an algorithm that runs in O(n) time.

Link to problem: https://leetcode.com/problems/longest-consecutive-sequence/description/

Approach:

  1. Used a priority queue (max-heap) to sort the elements in descending order.
  2. Iterated through the queue, popping elements and counting consecutive sequences.
  3. Skipped duplicates. Maintained a running maximum length of consecutive sequences found.

Topics learned:

  1. Identifying consecutive sequences in a sorted context
  2. Understanding time complexity trade-offs (O(n log n) vs O(n))

Link to my code: https://github.com/Amruta-25/dsa_chronicles.git

Top comments (0)