O(n) C/Java codes, but times are different (idk)


  • 0

    C: 16ms

    int findPoisonedDuration(int* timeSeries, int timeSeriesSize, int duration) {
        if (timeSeriesSize == 0 || duration == 0) return 0;
        
        int count = 0;
        for (int i = 1; i < timeSeriesSize; i ++) {
            int temp = timeSeries[i] - timeSeries[i - 1];
            if (temp >= duration) count += duration;
            else count += temp;
        }
        return count + duration;
    }
    

    Java: 10ms

    public class Solution {
        public int findPoisonedDuration(int[] timeSeries, int duration) {
            if (timeSeries.length == 0 || duration == 0) return 0;
        
            int count = 0;
            for (int i = 1; i < timeSeries.length; i ++) {
                int temp = timeSeries[i] - timeSeries[i - 1];
                if (temp >= duration) count += duration;
                else count += temp;
            }
            return count + duration;
        }
    }
    

    They are exactly the same codes.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.