#Leetcode Code review

1 messages · Page 1 of 1 (latest)

toxic adder
#

Q Link : https://leetcode.com/problems/maximum-population-year/

My Code:

class Solution {
    public int maximumPopulation(int[][] logs) {
        int max = 0, year = logs[0][0],i,j,c = 0;

        for(i = 0; i < logs.length - 1; i++)
        {
            c = 0;
            for(j = 0; j < logs.length; j++)
            {
                if(i != j)
                {
                    if(logs[i][1] - 1  >= logs[j][0])
                        c++;
                }
            }
                if(c > max)
                {
                    max = c;
                    year = logs[i][0];
                }
        }
        return year;
    }
}```

am quite sure that the issue with my code is something related to lifespan of ith person but not able to rectify the issue.
LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

magic basinBOT
#

<@&987246883653156906> please have a look, thanks.

magic basinBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

opal monolith
#

@toxic adder why are you convinced that only the first year entry will be "max year", what if it's a death year for person 1 but still 3 folks were alive during that time out of 4. Which could mean that 2nd entry in inside array is also a possibility? Could be one of the edge cases.

opal monolith
#

actually nvm, i think im wrong here. We might be able to cover that in birth year of other entries then.

rigid totem
rigid totem
#

If you want to know my array sum query approach where we will use prefix sum, then I can tell you how to do it, we don't have any query, so we change it a little bit

magic basinBOT
#

Closed the thread due to inactivity.

If your question was not resolved yet, feel free to just post a message to reopen it, or create a new thread. But try to improve the quality of your question to make it easier to help you 👍

toxic adder
toxic adder
rigid totem
rigid totem
#

And if someone died in 2040 and other in 2045, then in 2040, population is only 1 right ?? Cause according to question, if someone died in 2040, he is not considered in this year alive

#

That's the mere logic, i can say

#

We can implement something which just counts number of alive in a year, and use a prefix sum for it

formal birch
magic basinBOT
formal birch
#

notice how { is on the same line

#

that's common amongst all java code

#

also this is more of a personal preference thing but doing

if (a > 5)
    System.out.println("a > 5");

is bad imo since if you want to add another line inside the if block you have to fight with your IDE a bit to add the open and close curly brace

noble smelt
#

and often people dont even notice and if they dont use a proper IDE, they will have a bug they never notice

#
if (a > 5)
  foo
  bar
#

wondering why bar is always executed

formal birch
#

Also your indentation in the highlighted section is off

#

and I'm not entirely sure what's going on here

int max = 0, year = logs[0][0],i,j,c = 0;
#

other than that this entire piece of code is like 24 lines so there isn't much to criticize

toxic adder
toxic adder
toxic adder
formal birch
formal birch
toxic adder
formal birch
#

e.g.

// correct indentation
if (a > 5) {
    Foo.bar();
    Bar.foo();
}

// incorrect indentation (less readable)
if (a > 5) {
Foo.bar();
Bar.foo();
}
toxic adder
toxic adder
formal birch
toxic adder
formal birch
#

there aren't spaces before the Foo.bar() and Bar.foo() lines in the second one

toxic adder
#

but I dont think that's the case with my code

formal birch
toxic adder
#

anyways help me out with the logic instead
would appreciate that

glossy arch
#

@toxic adder maybe repost the entire code cleaned up and more readable, code like that ( although it looks simple ) is not always quick to understand

#

( esp. when using variables like i/j/c )

toxic adder
#
class Solution 
{
    public int maximumPopulation(int[][] logs)
    {
        int max = 0, year = logs[0][0],i,j,c = 0;

        for(i = 0; i < logs.length - 1; i++)
        {
            c = 0;

            for(j = 0; j < logs.length; j++)
            {
                if(i != j)
                {
                    if(logs[i][1] - 1  >= logs[j][0])
                        c++;
                }
            }
            
            if(c > max)
            {
                max = c;
                year = logs[i][0];
            }
        }
        return year;
    }
}```
magic basinBOT
toxic adder
glossy arch
#

make your c> max print out what it finds. They way your doing it at the moment, is based on the 1st number in the array

#

so your initial is 2033

#

then its 2039,2047