DEV Community

Cover image for The Leet Code Journey
Ansari
Ansari

Posted on

The Leet Code Journey

Data Structures are necessary for designing efficient algorithms and to write code efficiently. I wanted to brush up my DS skills, so I'm challenging myself to solve DS Problem as much as I can on weekly basis and share it in this community. If you're much interested on how algorithm works and want to develop your DS skills join the journey with me.

Day 1

Problem 1 :

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

Example :
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].

What most would do:

After reading the problem instantly, we wrote a nested loops and found the right pair for the answer.I did the same and run the code.

It was success the problem has been solved but the result looked terrible. It was like operation success but patient died.

HashMap

Literally that was ~2s which is not at all a efficient code. So instead of using nested loops I used hashmap.

With hash

Map

Now the result looked like this 👆,which was insane. So just by using hashmap, the code was nearly 67 times faster than the previous one.

Fast

If you want to learn more and know such interesting thing follow me and join the journey.

Top comments (0)