Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
79 views
in Technique[技术] by (71.8m points)

javascript - How to optimize code for HackerRank's Fraudulent Activity Notification problem

I have been working to solve this problem on the HackerRank site: Fraudulent Activity Notifications.

Below is the code I have written which satisfies the three sample test cases; however, it does not satisfy the larger test cases since it seems to take longer than 10 seconds.

The 10 second constraint is taken from here: HackerRank Environment.

function activityNotifications(expenditure, d) {

let notifications = 0;
let tmp = [];
let median = 0, medianEven = 0, iOfMedian = 0;

// Begin looping thru 'expenditure'
for(let i = 0; i < expenditure.length; i++) {
    
    // slice from 'expenditure' beginning at 'i' and ending at 'i + d' where d = number of days 
    // sort 'tmp' in ascending order after 
    tmp = expenditure.slice(i, i + d);
    tmp.sort();
    
    // edge case, make sure we do not exceed boundaries of 'expenditure'
    if((i + d) < expenditure.length) {
        
        // if length of 'tmp' is divisible by 2, then we have an even length
        // compute median accordingly
        if(tmp.length % 2 == 0) {
            
            medianEven = tmp.length / 2;
            median = (tmp[medianEven - 1] + tmp[medianEven]) / 2;
            
            // test if expenditures > 2 x median
            if(expenditure[i + d] >= (2 * median)) {

                notifications++;
            }
        }
        
        // otherwise, we have an odd length of numbers
        // therefore, compute median accordingly
        else {
            
            iOfMedian = (tmp.length + 1) / 2;
            
            // test if expenditures > 2 x median
            if(expenditure[i + d] >= (2 * tmp[iOfMedian - 1])) {
                
                notifications++;
            }
        }
    }
}

return notifications;

}

I am familiar with O notation for computing time complexity, so initially it seems the problem is either the excessive amount of variables declared or conditional statements used. Only one for loop is being used so I don't think the loop is where I should look to optimize the code. Unless, of course, we were to include the .sort() function used on 'tmp' which would definitely add to the time it takes to compute efficiently.

Is there anything I have not realized which is causing the code to take longer than expected? Any other hints would be greatly appreciated, thanks.

question from:https://stackoverflow.com/questions/65835784/how-to-optimize-code-for-hackerranks-fraudulent-activity-notification-problem

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...