DEV Community

Cover image for Learn Trapping Rain Water, Top K Frequent and Selection Sort with Step-by-Step Visualization in DSA View View 👀👀
nyaomaru
nyaomaru

Posted on AI-assisted

Learn Trapping Rain Water, Top K Frequent and Selection Sort with Step-by-Step Visualization in DSA View View 👀👀

Makes dry algorithms fun with cool visuals

Hoi hoi!

I’m @nyaomaru, a frontend engineer who has been obsessed with ramen lately. 😸🍜

Have you used DSA View View already? 👀👀

DSA View View allows you to understand DSA by visualizing how your implementation actually runs.

In the previous articles, we looked at problems like:

  • Two Sum
  • Binary Search
  • Bubble Sort
  • Valid Parentheses
  • Reverse Linked List
  • Maximum Depth of Binary Tree
  • Number of Islands
  • Invert Binary Tree
  • Course Schedule

This time, let's try three more problems:

  • Trapping Rain Water
  • Top K Frequent Elements
  • Selection Sort

These three problems introduce some very useful ways of thinking

Shrink a problem from both sides
Count first, then organize by frequency
Repeatedly select the next value
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

Once again, the implementations are not necessarily huge.

But there are several changing values that we need to keep in our heads.

So instead of only reading the final code,

Let's view what actually happens. 👀👀

View View Case Closed


🌧️ Trapping Rain Water

Let's start with Trapping Rain Water.

Suppose we have these heights

[0, 1, 0, 2, 1, 0, 1, 3]
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

If we draw them as walls, it looks roughly like this.

              █
      █       █
  █   █ █   █ █
-----------------
0 1 0 2 1 0 1 3
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

Rain falls from above.

Some water escapes.

But some water becomes trapped between taller walls.

For example

      █~~~~~~~█
  █~~~█~~~~~~~█
-----------------
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

So the question is

How much water can be trapped?

At first, I found this problem quite confusing. 😿

Because the amount of water above one position depends on walls somewhere else.

So what information do we actually need?

How Much Water Fits Above One Position?

Imagine this position

left wall     right wall
    █             █
    █      x      █
    █      █      █
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

The water level cannot be higher than the shorter side.

So the maximum possible water level is

Math.min(leftMax, rightMax);
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

Then we subtract the current height.

Conceptually:

water = min(leftMax, rightMax) - currentHeight
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

That's the basic idea.

But do we really need to calculate both sides again for every position?

No.

We can use two pointers.

Two Pointers

Here is the implementation.

function trap(height: number[]): number {
  let left = 0;
  let right = height.length - 1;

  let leftMax = 0;
  let rightMax = 0;

  let water = 0;

  while (left <= right) {
    if (height[left] <= height[right]) {
      if (height[left] >= leftMax) {
        leftMax = height[left];
      } else {
        water += leftMax - height[left];
      }

      left++;
    } else {
      if (height[right] >= rightMax) {
        rightMax = height[right];
      } else {
        water += rightMax - height[right];
      }

      right--;
    }
  }

  return water;
}
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

There are several important values.

left
right
leftMax
rightMax
water
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

This is exactly the kind of code where I understand every variable individually,

but then lose track of all of them together. 😹

Let's follow a smaller example.

[2, 0, 1, 3]
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

Start From Both Ends

At first

left = 0
right = 3

[2, 0, 1, 3]
 ↑        ↑
left    right
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

The heights are:

height[left]  = 2
height[right] = 3
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

Since

2 <= 3
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

we process the left side.

There is a wall with height 2.

So

leftMax = 2
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

Then move left.

[2, 0, 1, 3]
    ↑     ↑
   left right
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

Now We Can Trap Water

The current height is

0
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

But we already know there is a wall of height 2 on the left.

And the right side is currently at least as high as that.

So this position can hold

leftMax - height[left]
= 2
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

Therefore

water = 2
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

Move again.

[2, 0, 1, 3]
       ↑  ↑
      left right
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

Now

height[left] = 1
leftMax = 2
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

So

2 - 1 = 1
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

One more unit of water.

water = 3
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

Eventually we reach the final wall.

Done! 🎉

Why Can We Process the Shorter Side?

This part is the important idea.

Suppose

height[left] <= height[right]
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

Then we already know there is a wall on the right that is at least as tall as the current left wall.

So for the current left position, the limiting factor is the best wall we have seen from the left.

That's why we can safely calculate

leftMax - height[left];
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

without knowing every future wall.

The same logic works from the other side.

If

height[right] < height[left]
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

we process the right side using rightMax.

So the algorithm keeps shrinking the unknown area

L → → →     ← ← ← R
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

until everything has been processed.

Complexity

Each pointer only moves across the array once.

Time:  O(n)
Space: O(1)
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

👀 Let's View View It

This problem is a perfect example of why I like visualization.

The code contains

left
right
leftMax
rightMax
water
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

And they all change at different times.

When I only read

water += leftMax - height[left];
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

I might ask:

  • Why are we using leftMax here?
  • What is rightMax right now?
  • Why did we move left instead of right?
  • How much water have we already counted?
  • Which part of the array is still unprocessed?

😿

Step by step, we can actually watch the search area shrink.

L         R
↓         ↓
[2, 0, 1, 3]

    L     R
    ↓     ↓
[2, 0, 1, 3]

       L  R
       ↓  ↓
[2, 0, 1, 3]
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

And at the same time

leftMax
rightMax
water
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

keep changing.

The algorithm is really asking

Which side can I safely solve right now?

Then it solves that side and moves inward. 🌧️😸


🔢 Top K Frequent Elements

Next, let's find the Top K Frequent Elements.

Suppose we have

[1, 1, 1, 2, 2, 3]
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

and

k = 2
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

How often does each number appear?

1 → 3 times
2 → 2 times
3 → 1 time
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

So the two most frequent values are

[1, 2]
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

Simple enough.

But how should we implement it?

First, Count Everything

The first thing we need is frequency.

We can use a Map.

const frequency = new Map<number, number>();
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

Then count each value.

for (const num of nums) {
  frequency.set(num, (frequency.get(num) ?? 0) + 1);
}
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

For

[1, 1, 1, 2, 2, 3]
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

we get

frequency = {
  1 → 3
  2 → 2
  3 → 1
}
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

Nice.

But we still need the top K.

Of course, we could sort everything by frequency.

But there is another interesting approach.

Use Frequency as an Index

The maximum possible frequency is

nums.length
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

So we can create buckets.

const buckets: number[][] = Array.from({ length: nums.length + 1 }, () => []);
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

The index represents frequency.

For example

bucket[1] = values appearing 1 time
bucket[2] = values appearing 2 times
bucket[3] = values appearing 3 times
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

For our example:

frequency = {
  1 → 3
  2 → 2
  3 → 1
}
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

the buckets become

index 0 → []
index 1 → [3]
index 2 → [2]
index 3 → [1]
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

That's pretty interesting. 👀👀

Instead of asking

What is the frequency of this number?

we reverse the relationship

Which numbers have this frequency?

Implementation

Here is the full implementation:

function topKFrequent(nums: number[], k: number): number[] {
  const frequency = new Map<number, number>();

  for (const num of nums) {
    frequency.set(num, (frequency.get(num) ?? 0) + 1);
  }

  const buckets: number[][] = Array.from({ length: nums.length + 1 }, () => []);

  for (const [num, count] of frequency) {
    buckets[count].push(num);
  }

  const result: number[] = [];

  for (let count = buckets.length - 1; count >= 0; count--) {
    for (const num of buckets[count]) {
      result.push(num);

      if (result.length === k) {
        return result;
      }
    }
  }

  return result;
}
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

Let's follow it.

Step 1: Build the Frequency Map

Start

frequency = {}
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

Read the first 1. And another one and another...

1 → 1
1 → 2
1 → 3
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

Then 2. And another one.

1 → 3
2 → 1
2 → 2
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

Finally 3.

1 → 3
2 → 2
3 → 1
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

Done.

Step 2: Put Values Into Buckets

Now

buckets[count].push(num);
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

For

1 → 3
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

we do

buckets[3].push(1)
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

For

2 → 2
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

we do

buckets[2].push(2)
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

And

3 → 1
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

becomes

buckets[1].push(3)
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

So

0: []
1: [3]
2: [2]
3: [1]
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

Step 3: Read From Highest Frequency

We want the most frequent values.

So don't start at 0. Start from the end.

3 → [1]
2 → [2]
1 → [3]
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

Take 1.

result = [1]
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

We still need one more. Move down.

Take 2.

result = [1, 2]
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

Now

result.length === k
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

So return.

Done! 🎉

Why Is This Interesting?

I like this solution because the second structure changes our perspective.

The Map says

value → frequency
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

The buckets say

frequency → values
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

Same information. But different direction.

And suddenly finding the most frequent values becomes easy.

We just walk backward through the buckets.

Complexity

We count every number once.

We distribute every unique number into a bucket.

Then we walk through the buckets.

Time:  O(n)
Space: O(n)
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

👀 Let's View View It

There are two transformations happening here.

First

nums
 ↓
frequency Map
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

Then

frequency Map
 ↓
buckets
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

Then

buckets
 ↓
result
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

Reading the final implementation, it can be easy to miss why we're building two different data structures.

With the runtime visible, we can follow the data changing shape.

[1, 1, 1, 2, 2, 3]
        ↓ count
1 → 3
2 → 2
3 → 1
        ↓ bucket
1: [3]
2: [2]
3: [1]
        ↓ highest first
[1, 2]
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

That's the part I like.

We don't magically find the top K.

We reorganize the information until the answer becomes easy to read. 🔢😸


👉 Selection Sort

Finally, let's sort something again!

We already looked at Bubble Sort in a previous article.

This time, let's try Selection Sort.

Suppose we have

[5, 3, 4, 1, 2]
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

We want

[1, 2, 3, 4, 5]
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

Selection Sort follows a very simple idea

Find the smallest remaining value and move it to the front.

Then repeat.

First Pass

Start

[5, 3, 4, 1, 2]
 ↑
 i
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

Assume the first value is currently the smallest.

minIndex = 0
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

Then scan everything to the right.

5 vs 3
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

3 is smaller.

So:

minIndex = 1
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

Then:

3 vs 4
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

No change.

Then

3 vs 1
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

1 is smaller.

minIndex = 3
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

Finally

1 vs 2
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

Still 1.

So the smallest value is at index 3.

Swap

[5, 3, 4, 1, 2]
 ↑        ↑
 i       min
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

↓

[1, 3, 4, 5, 2]
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

Now the first position is finished.

[1 | 3, 4, 5, 2]
 ↑
sorted
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

Repeat

Next, start from index 1.

[1 | 3, 4, 5, 2]
     ↑
     i
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

Find the smallest value in

[3, 4, 5, 2]
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

That's 2.

Swap.

[1, 2 | 4, 5, 3]
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

Again.

Find the smallest remaining value.

3
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

Eventually

[1, 2, 3, 4, 5]
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

Sorted! 🎉

Implementation

function selectionSort(nums: number[]): number[] {
  for (let i = 0; i < nums.length - 1; i++) {
    let minIndex = i;

    for (let j = i + 1; j < nums.length; j++) {
      if (nums[j] < nums[minIndex]) {
        minIndex = j;
      }
    }

    if (minIndex !== i) {
      [nums[i], nums[minIndex]] = [nums[minIndex], nums[i]];
    }
  }

  return nums;
}
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

There are two important indexes.

i
minIndex
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

And also

j
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

which searches through the unsorted area.

Why Is It Called Selection Sort?

Because each pass selects the smallest remaining value.

Find smallest
  ↓
Select it
  ↓
Move it to the front
  ↓
Repeat
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

That's basically the whole algorithm.

Complexity

For every position, we search through the remaining values.

So

Time: O(n²)
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

We sort the array in place.

Space: O(1)
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

Selection Sort is not something I would normally choose for sorting a huge production dataset. 😹

But as a learning algorithm, it is wonderfully visual.

👀 Let's View View It

The implementation contains nested loops.

for (let i = 0; i < nums.length - 1; i++) {
  let minIndex = i;

  for (let j = i + 1; j < nums.length; j++) {
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

Reading it, I might lose track of:

  • Which area is already sorted?
  • Where is i?
  • Where is j?
  • What does minIndex currently point to?
  • When exactly does the swap happen?

When we visualize it, the basic pattern becomes obvious.

[5, 3, 4, 1, 2]
          ↑
       smallest

[1 | 3, 4, 5, 2]
              ↑
           smallest

[1, 2 | 4, 5, 3]
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

The algorithm is continuously growing a finished area from left to right.

That's Selection Sort.

Pick the smallest remaining value.

Put it next. And repeat. 🍥😸


🧠 What Did We Actually Learn?

Again, these three problems look completely different.

But each one introduces a useful way of thinking.

Trapping Rain Water

Use information from both sides to decide which part can already be solved safely.

Which side do I know enough about right now?
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

Top K Frequent Elements

Sometimes counting the data is only the first step.

Reorganize it into a structure where the answer becomes easy to retrieve.

Can I reorganize this information around what I actually need?
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

Selection Sort

Build the answer one permanent position at a time.

What value belongs in this position next?
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

So this time we saw

Two pointers
Frequency buckets
Selection
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

Three different mental models again.

And just like the previous problems, the difficult part is often not the syntax.

It's the changing state.

Which pointer moved?
What is the maximum now?
What is inside the Map?
Which bucket changed?
Where is minIndex?
Which part is already finished?
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

That's a lot to keep in our heads.

So instead, I want to view it. 👀👀


🎯 Conclusion

In this article, we looked at:

  • Trapping Rain Water with two pointers
  • Top K Frequent Elements with frequency buckets
  • Selection Sort

And more importantly, we followed how their state changes while they run.

For Trapping Rain Water, we watched two pointers move inward while leftMax, rightMax, and water changed.

left →       ← right
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

For Top K Frequent Elements, we watched the same data change representation.

array
  ↓
frequency Map
  ↓
buckets
  ↓
result
🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด 🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด

For Selection Sort, we watched the sorted area grow one position at a time.

This is exactly the kind of thing I built DSA View View for.

You can write or load a TypeScript implementation, run it with your own inputs, and move backward and forward through the runtime.

If you are learning DSA too, try viewing one of these problems step by step.

And if there is a DSA problem you want me to cover next, please let me know in the comments!

I still have many algorithms to learn myself. 😸

Let's train our DSA muscles together! 💪

If you like DSA View View, please give it a star ⭐

GitHub logo nyaomaru / dsa-view-view

DSA View View allows you to understand DSA to see the data flow. 👀👀 Of course, it's free.

DSA View View

DSA View View

DSA View View turns TypeScript algorithm functions into step-by-step visual stories. 👀👀

DSA View View TV

Write code, run it with structured inputs, and see the arrays, matrices trees, lists, stacks, pointers, and return values move as the function executes.

It is built for those moments when reading the code is not enough and you want to view why the answer changes.

DSA View View demo

Why Try It?

  • 🧠 Step through real TypeScript
    Paste or edit a function, validate it, then run the exact code in the browser.

  • 🧩 Views that match the data
    Arrays become bars, matrices become grids, trees become node graphs, linked lists become chains, and two-pointer area problems get their own visual view.

  • 🌳 DSA-friendly inputs out of the box
    TreeNode, ListNode, MinHeap, MaxHeap, PriorityQueue, nested arrays matrices, strings, numbers, and class-style inputs are supported without ceremony.

  • 🔎 39 built-in examples
    Search by name, browse…

See you in the next article!

Top comments (29)

🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด
 
onizuka profile image
Onizuka •

The two-pointer approach on Trapping Rain Water is one of those problems where visualization actually matters — I've seen people memorize the code without understanding why left < right even works, then freeze on variants like "trapping rain water II" on a 2D grid. The "count first, then organize by frequency" framing for Top K is solid, but worth noting that the bucket-sort approach only wins when k is small relative to n — a heap is the boring but correct answer more often than people admit.

🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด
 
nyaomaru profile image
nyaomaru •

Thank you! 😸

I also think visualization helps turn these problems from something you memorize into something you actually understand, so I’d be really happy if you give DSA View View a try! 👀

And thanks for pointing out the trade-off with the bucket approach too. The bucket version gives us linear-time behavior, but it also allocates space based on n, while a heap can be a better practical choice when k is small or memory matters.

Handling the size of the data structure is definitely an important part of the design too.

🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด
 
koda2026 profile image
Harun - solo dev •

The step-by-step visualization on Trapping Rain Water is clean. The moment where you track the left/right max arrays side-by-side is exactly the mental model beginners miss. Building something similar for my KODA LEARN lessons — thanks for the reference. 🙌

🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด
 
nyaomaru profile image
nyaomaru •

DSA is fundamental, but I think it really supports the way engineers think and solve problems.

So I think making it a core part of your learning content is a great idea!

Good luck with KODA LEARN! 💪😸

🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด
 
xulingfeng profile image
xulingfeng •

Wow, this is awesome! You managed to make those dry, boring algorithms so engaging. Between us, back when I tried reading algorithm books, I’d doze off the second I closed my eyes. I never picked them up again, haha😂. If the tutorials I had back then were this fun, I would’ve been way better at this stuff.🤣

🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด
 
nyaomaru profile image
nyaomaru •

Thx! 😸

Honestly, DSA isn’t always the most enjoyable thing to learn, so I tried to make it as fun and approachable as possible. 👀✨

I’m really happy you enjoyed it! 😹

🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด
 
xulingfeng profile image
xulingfeng •

Nice work! Visuals make DSA much easier to learn👏

🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด
 
publiflow profile image
PubliFlow •

Interesting TypeScript approach. I'd emphasize that strict null checks and noUncheckedIndexedAccess are non-negotiable for production code — the number of runtime errors they prevent is remarkable.

🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด
 
nyaomaru profile image
nyaomaru •

Thank you! I agree. 😸

I also think strictNullChecks and noUncheckedIndexedAccess are really important for production code.

And for values that still need runtime validation, I like using reusable type guards too. That’s actually one of the reasons I built is-kit. 👀

is-kit.dev/

🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด
 
publiflow profile image
PubliFlow •

Nice TypeScript patterns. Side note: we shipped 12 AI tools under tools.shopveigo.com using strict TS end-to-end. The discriminated union pattern you mentioned was key for our API routing.

🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด
 
publiflow profile image
PubliFlow •

Nice TypeScript write-up. The utility types (Pick, Omit, Extract, Exclude) are underrated — proper use of conditional types can eliminate a lot of boilerplate while maintaining type safety.

🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด
 
richard_smith_154156d471ef profile image
Richard Smith •

Love the step-backward feature. Being able to rewind mid-loop is so clutch when you lose track of a variable and don't want to restart everything.

🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด
 
nyaomaru profile image
nyaomaru •

Thx! 😸

Actually, this feature has been improved based on user feedback!

github.com/nyaomaru/dsa-view-view/...

So I’m really happy to hear that you found the step-back feature useful. 👀

If you have any requests or run into any issues, feel free to reach out anytime!

🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด
 
technogamerz profile image
𝐓𝐡𝐞 𝐋𝐚𝐳𝐲 𝐆𝐢𝐫𝐥 •

Excellent!

🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด
 
nyaomaru profile image
nyaomaru •

Thx! 😸

🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด
 
technogamerz profile image
𝐓𝐡𝐞 𝐋𝐚𝐳𝐲 𝐆𝐢𝐫𝐥 •

Welcome 🐈

🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด
 
byteninja01 profile image
Ujjwal Sharma •

I tried , its amazing ! but more amazing to see that you have not missed any commit on GitHub from 6th Jan 2025 ! Amazing consistency

🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด
 
nyaomaru profile image
nyaomaru •

Thanks for trying DSA View View! I’m really happy to hear that 😸

I traveled around Europe, and after that I realized that consistency is one of the most important things in life.

So I try to keep developing something every day as much as I can 👍

🔴 Hot News: nyaomaru/learn trapping rain water top k frequent and selection sort with step by step visualization in dsa flg - สรุปดราม่าล่าสุด
 
shieldxbot profile image
shieldx •

Visualization làm rõ được cái "sự thật" của two-pointer trong Trapping Rain Water — thấy left_max/right_max co lại từng bước thì mới hiểu tại sao không cần mảng phụ hay stack. Top K Frequent dùng min-heap size k là classic, nhưng thực tế gặp stream data lớn thì bucket sort (frequency → list of values) thường nhanh hơn nhờ O(n) và cache-friendly. Selection Sort thì... haha, giữ làm bài tập thôi, production chưa bao giờ thấy nó đâu. Bài viết trình bày rất sạch, đặc biệt phần step-by-step giúp người mới bắt pattern matching dễ dàng hơn nhiều so với đọc pseudo-code khô khan PS: the tool I meant is on labagent .tech

Some comments may only be visible to logged-in visitors. Sign in to view all comments.