5
\$\begingroup\$

How to make changes in my code so that it can take the least memory possible.? This is a leetcode program , I have tested all the testcases and it has passed, I tried to reduce the memory usage, but by far now, I have not been able to understand.

I want to look into this so that I can understand the usage of memory spaces better. Any input would be helpful. Thank you very much. I will also post the link to the question, but since the logic is already clear to me, The only question is that is this a good solution or memory heavy?

   class Solution {
    public int findPoisonedDuration(int[] timeSeries, int duration) {
        
        int i =0,old_goalpost=0,new_goalpost=0 ,diff=0;        
        int numberOfSeconds = duration;
       
        if (timeSeries.length >0)
            old_goalpost = timeSeries[0]+duration-1;
        else
            numberOfSeconds=0;
        
        if (timeSeries.length >0)
        for (i=1;i<timeSeries.length;i++)            
        {
            new_goalpost =  timeSeries[i]+duration-1; 
            
            diff = (new_goalpost - old_goalpost)>duration?duration:((new_goalpost - old_goalpost));
            
            
            numberOfSeconds = numberOfSeconds +diff;
            old_goalpost = new_goalpost;
            
        }
        return numberOfSeconds;
        
    }
}

the question is this enter image description here

And it has passed all the test cases.

enter image description here

If you want, this is the link to the question https://leetcode.com/problems/teemo-attacking/

this code is 99.97% faster but memory heavy

class Solution {
    public int findPoisonedDuration(int[] timeSeries, int duration) {
        
        int i =0,old_goalpost=0,new_goalpost=0 ,diff=0;        
        int numberOfSeconds = duration;
       
        if (timeSeries.length >0)
            old_goalpost = timeSeries[0]+duration-1;
        else
            numberOfSeconds=0;
        
        if (timeSeries.length >0)
        for (i=1;i<timeSeries.length;i++)            
        {
            new_goalpost =  timeSeries[i]+duration-1;                 
            diff = (new_goalpost - old_goalpost)>duration?duration:((new_goalpost - old_goalpost)); 
            numberOfSeconds = numberOfSeconds +diff;
            old_goalpost = new_goalpost;
            
        }
        return numberOfSeconds;
        
    }
}

enter image description here

\$\endgroup\$
2
  • \$\begingroup\$ can I add just one sentence more about how to reduce memory in the question? @BCdotWEB ? \$\endgroup\$
    – shubhendu
    Commented Oct 12, 2020 at 15:27
  • \$\begingroup\$ Let me know what you want to add please? @BCdotWEB \$\endgroup\$
    – shubhendu
    Commented Nov 17, 2020 at 7:40

1 Answer 1

3
\$\begingroup\$

Disclaimer: Not a Code Reviewer

  • Just tested your code, it has a 1ms runtime with 99.97% faster than other solutions.

    enter image description here

  • Don't pay too much attention to these runtime/memory data provided by LeetCode especially when:

    • it comes to memory; or
    • test cases are too limited (such as Teemo) because their benchmarkings are not so accurate.
  • Maybe instead we'd format the code, just a bit making things based on Java's conventions:

public class Solution {
    public static final int findPoisonedDuration(
        final int[] timeSeries,
        final int duration
    ) {

        int i = 0;
        int oldGoalpost = 0;
        int newGoalpost = 0;
        int diff = 0;
        int numberOfSeconds = duration;

        if (timeSeries.length > 0) {
            oldGoalpost = timeSeries[0] + duration - 1;

        } else {
            numberOfSeconds = 0;
        }

        if (timeSeries.length > 0)
            for (i = 1; i < timeSeries.length; i++) {
                newGoalpost =  timeSeries[i] + duration - 1;

                diff = (newGoalpost - oldGoalpost) > duration ? duration : ((newGoalpost - oldGoalpost));


                numberOfSeconds = numberOfSeconds + diff;
                oldGoalpost = newGoalpost;

            }

        return numberOfSeconds;

    }
}

  • Your solution has an order of N runtime with constant memory O(1).
  • Algorithmically speaking, which is a key point on solving LeetCode questions, I don't think much can be done to make it any further efficient, I might be wrong though.
public class Solution {
    public static final int findPoisonedDuration(
        final int[] series,
        final int duration
    ) {
        if (series.length == 0 || duration == 0) {
            return 0;
        }

        int totalTime = duration;

        for (int i = 1; i < series.length; i++) {
            totalTime += Math.min(series[i] - series[i - 1], duration);
        }

        return totalTime;
    }
}

\$\endgroup\$
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.