1596487260

It’s a website where people–mostly software engineers–practice their coding skills. It’s pretty similar to sites like HackerRank & Topcoder which will rank your code written for a particular problem against the ones submitted by other users

Leetcode gets primarily used for two use cases:

- Solve interesting data structure problems
- Prepare and practice for coding interviews 🤔

I never had a clear understanding when it comes to data structures and algorithms, so being a software engineer for the last 12 years, I thought it’s not a bad idea to at least start now. So as of Dec 2019, I became a premium member in Leetcode and committed myself to get as much value out of 150$ that I spent on the membership fees. Leetcode is like an ocean; it has closer to 2000+ questions, which keeps growing day by day.

In fact, this was my exact reaction when I open the website for the first time after looking at the number of questions and categories it had

In this post, let me explain some of the methods I developed over some time, which helped me to understand and learn about these data structures & algorithms.

Most people new to Leetcode directly jump into solving the problems without getting a proper understanding of the underlying data structures like a linked list, stack, queue, tree, etc. Trust me this is a bad idea

I would recommend spending some time understanding these data structures, which are the building blocks for solving any problems.

Even if you are not able to go through all the data structures, at least get a strong understanding of the *linked list* and _binary trees. _Fortunately, there are some excellent materials created by Standford, which can help you with this:

- http://cslibrary.stanford.edu/103/LinkedListBasics.pdf
- http://cslibrary.stanford.edu/105/LinkedListProblems.pdf

Leetcode has many categories and a whole lot of questions in each of them, so it’s essential to come up with a method that could help you to understand these concepts. Learning with LeetCode is a long process, and it will take time, so one thing that I clearly understood is you have to keep the enthusiasm and eagerness to learn high if you want to go through the journey.

If you are a beginner, then use the below flowchart to learn things and gradually increase the complexity of the problems that you solve. It also helps in understanding the concepts well so you can reapply those techniques in the subsequent problems.

Sequence for solving problems

Stages are numbers in red circles, and dotted lines are entirely optional for proceeding to the next stage. When you are done with stage 3 depending upon your motivation, you will end up either doing “*Monthly coding challenges*” or “*attending interviews*” (both are fun)

#coding #leetcode #data-structures #problem-solving #algorithms #algorithms

1596487260

It’s a website where people–mostly software engineers–practice their coding skills. It’s pretty similar to sites like HackerRank & Topcoder which will rank your code written for a particular problem against the ones submitted by other users

Leetcode gets primarily used for two use cases:

- Solve interesting data structure problems
- Prepare and practice for coding interviews 🤔

I never had a clear understanding when it comes to data structures and algorithms, so being a software engineer for the last 12 years, I thought it’s not a bad idea to at least start now. So as of Dec 2019, I became a premium member in Leetcode and committed myself to get as much value out of 150$ that I spent on the membership fees. Leetcode is like an ocean; it has closer to 2000+ questions, which keeps growing day by day.

In fact, this was my exact reaction when I open the website for the first time after looking at the number of questions and categories it had

In this post, let me explain some of the methods I developed over some time, which helped me to understand and learn about these data structures & algorithms.

Most people new to Leetcode directly jump into solving the problems without getting a proper understanding of the underlying data structures like a linked list, stack, queue, tree, etc. Trust me this is a bad idea

I would recommend spending some time understanding these data structures, which are the building blocks for solving any problems.

Even if you are not able to go through all the data structures, at least get a strong understanding of the *linked list* and _binary trees. _Fortunately, there are some excellent materials created by Standford, which can help you with this:

- http://cslibrary.stanford.edu/103/LinkedListBasics.pdf
- http://cslibrary.stanford.edu/105/LinkedListProblems.pdf

Leetcode has many categories and a whole lot of questions in each of them, so it’s essential to come up with a method that could help you to understand these concepts. Learning with LeetCode is a long process, and it will take time, so one thing that I clearly understood is you have to keep the enthusiasm and eagerness to learn high if you want to go through the journey.

If you are a beginner, then use the below flowchart to learn things and gradually increase the complexity of the problems that you solve. It also helps in understanding the concepts well so you can reapply those techniques in the subsequent problems.

Sequence for solving problems

Stages are numbers in red circles, and dotted lines are entirely optional for proceeding to the next stage. When you are done with stage 3 depending upon your motivation, you will end up either doing “*Monthly coding challenges*” or “*attending interviews*” (both are fun)

#coding #leetcode #data-structures #problem-solving #algorithms #algorithms

1623196260

I’ve been gaining confidence in my algorithm solving skills. So much so that I’ve graduated to the medium difficulty Leetcode problems. Yay! Can I effortlessly solve every problem I see? Hell no! I’m at the stage where given the problem I can normally come up with a working brute force solution. However, when it comes to finding the most optimized answer, I usually look for a few hints. But that’s ok! As long as I spend time fully understanding the solution before I move on, I will have learned a new trick or two and can apply them to the next problem.

The following is one such problem:

A string `S`

of lowercase English letters is given. We want to partition this string into as many parts as possible so that each letter appears in at most one part, and return a list of integers representing the size of these parts.

`S`

will have a length in the range`[1, 500]`

.`S`

will consist of lowercase English letters (`'a'`

to`'z'`

) only.

```
Input: S = "ababcbacadefegdehijhklij"
Output: [9,7,8]
Explanation: The partition is "ababcbaca", "defegde", "hijhklij".
```

To solve the problem, we will use a two pointer solution. See steps below:

- First we create a hashmap of the last index of each unique character in the string. This is done by looping through the string and storing the index at which each letter occurs. If a letter is repeated, the hash will update with the greater index and only the last occurrence will be saved.
- Next comes the two pointer part. First we will initialize our partitions array (this is what we will be returning) and we initialize our two pointers to the start of the string (set end and start to 0).
- Then we loop through the string, setting the end pointer. The end pointer is the bigger value between the index we are on, and the last index of the character we are on (use the hashmap!).
- Next we compare the position of our end pointer against where we are in terms of iterating over the string. If the current index is not the same as the end index, we have to keep moving and do nothing.
- However, if the end and current index are the same then we must be at the end of a partition. Basically, this character wont appear again so you should make a cut in the string to ensure we get the maximum number of partitions.
- To make a cut, push the length of the segment into the partitions array. Then we update our start pointer to one after the end of the last partition.
- After the end of the loop, we simply return the partitions array.

```
var partitionLabels = function(S) {
let lastIndex = {}
for (let i = 0; i < S.length; i++){
lastIndex[S[i]]= i
}
let partitions = []
let start = 0
let end = 0
for (let i = 0; i < S.length; i++) {
end = Math.max(end, lastIndex[S[i]])
if (i === end) {
partitions.push(i — start + 1)
start = i + 1
}
}
return partitions
}
```

Never move on from a problem until you complete the space-time complexity analysis. It can help trigger a signal to a brute force solution where a more optimized approach may exist.

For the solution code above:

**Space Complexity: O(n)** This has to do with the step where we store the last index of each character. In theory, you could have each character be unique, resulting in O(n) space needed for that hash.

**Time Complexity: O(n)** We loop through the array twice. Once to create the hash of the last indices and again to create the partitions. This results in a time complexity of 2*O(n) at the worst case. But we don’t care about coefficients and therefore we drop the 2 at the beginning.

#leetcode #leetcode-medium #algorithms

1624874940

Hello, hello! As I continue my job search, I am practicing my algorithm questions on Leetcode. So, I thought I would blog about some Leetcode problems as I solve them.

As a Bootcamp graduate, I did not get much practice in algorithms, so it has been a very fun and sometimes frustrating trial and error in recognizing patterns, optimizing my code to be faster (looking at you Big-O), learning how to break down a problem, and sometimes implementing neat math tricks to solve these algorithm problems. I have been practicing them using JavaScript.

As I’m relatively new to algorithm problems, I’ve started out with their Easy Collection of Top Interview Questions. So in **Contains Duplicate:**

Given an integer array `nums`

, return `true`

if any value appears **at least twice** in the array, and return `false`

if every element is distinct.

Examples:

```
Input: nums = [1,2,3,1]
Output: true
Input: nums = [1,2,3,4]
Output: false
Input: nums = [1,1,1,3,3,4,3,2,4,2]
Output: true
```

Simply, if any numbers in the array appear more than once in the array, I need to return `true`

. Now, I will definitely need to figure out a way to compare a number to the other numbers. Additionally, I will most likely need to loop through the array so I have access to each number to then compare it to others.

First, I thought that I could iterate through the array, and at each instance, I iterate through another loop of the remaining integers and compare them to see if any are equal (===). However, per my beginner understanding of Big-O, this would yield me an unfavorable time complexity of O(n²).

Then, I thought that I could create an object and count how many times a number in the array appears. By doing this, I could then check to see if any of the numbers had a count greater than 1. If so, then I could return `true`

else I would return `false`

.

#computer-science #leetcode #javascript #programming #algorithms #leetcodes algorithm

1615992780

I was doing some recreational programming tonight on LeetCode and I stumbled into an interesting performance case study.

The Problem

This is LeetCode Problem: 881 Boats to Save People.

Boats to Save People

The i-th person has weight people[i], and each boat can carry a maximum weight of limit.

Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit.

Return the minimum number of boats to carry every given person. (It is guaranteed each person can be carried by a boat.)

#python #leetcode-python #leetcode #recreational-programming

1610513150

In this video we will talk about how to use LeetCode Effectively.

Subscribe: https://www.youtube.com/channel/UCs6nmQViDpUw0nuIx9c_WvA

#leetcode #web-development