How to solve a Leetcode problem as a beginneer (Step by step)
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;
}
}
- 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.
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
0 Comments