How to solve a LeetCode problem as a beginner (Step by step)


How to solve a Leetcode problem as a beginneer (Step by step)


When starting to solve problems on Leetcode, you may find that the questions are hard to read or even understand, and it might feel overwhelming to a beginner who is just starting. The language of the questions can definitely add to this feeling. 

So the first step is to get used to this setup. You are no longer solving if the number is even or not, here you need to apply your brain a bit. 

What I started doing is that firstly, I understand the algorithms from YouTube. You can do that from anywhere as you want. But knowing that an algorithm exist for a type of problem is half job done.

So, after understanding the concepts you should go and try the easy problems of that type. Here easy doesn't mean that I am talking about Leetcode rating of a question but rather I am talking about what you find easy to solve, or even approach. 

Choosing a question is the first step. The next and most important step is to get a pen and paper. Write down what the question is asking, as if you're teaching it to someone else.. Try to write all the required steps you need to get to the answer. 

For example let's go through the steps of one easy problem from binary search that I solved yesterday and how I approached it. 


Code:

class Solution {
public int maximumWealth(int[][] accounts) {
int max = 0;
for(int person = 0; person<accounts.length; person++){
int rowWealth = 0;
for(int acc = 0; acc<accounts[person].length; acc++){
rowWealth+=accounts[person][acc];
}
if(rowWealth>max){
max = rowWealth;
}
}
return max;
}
}

In this question, we are asked to return the maximum wealth from given array of wealth of different persons. 

  • Read the question from the given link. (QUE)
  • We need to find the max wealth from the given matrix.(Person and their accounts)
  • So, I created a Integer variable with the value 0. 
  • int max = 0;
  • Now to iterate through one array, I used the person array and I created a variable which will store the wealth of the person from the row.
  • Inside another iterative loop, I created a int acc variable to get the actual value of the wealth for the person in an account. For each person, we calculate their total wealth by summing all values in that row.
  • Now I updated the personwealth value. 
  • As we created our max variable then we can update that using condition that if the personwealth > max then update max = personwealth.
  • It will only update the value of max when the personwealth is greater than max. 
  • Then in the end just return max. 
See the approach is to have the required variables and updating it according to the question. Sometimes you may not find the correct solutions, at that time try to rename your variables and make their names meaningful. It helps a lot. 

If you are really starting then you might not know about why we just write functions and doesn't print the value or anything. This is because
The primary goal of LeetCode is to assess a user's understanding of algorithms and data structures. By isolating the problem to a specific function, the platform ensures that the evaluation is solely on the implemented logic and efficiency, rather than on peripheral coding aspects like input/output handling or program structure.

Lastly, try to solve problems. Try to visualize how things work. 

Thanks for reading and I'll meet you in the next blog.

You can connect with me here: Social







Post a Comment

0 Comments