Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8030915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:55:22+00:00 2026-06-05T00:55:22+00:00

Here is my solution to the Lead Game problem on Codechef . It runs

  • 0

Here is my solution to the Lead Game problem on Codechef. It runs fine, but took 2.63 sec and 3.8M memory, while I saw many C programs that had completed in 0.08 seconds and 1.6M memory. How can I make it faster?

import sys
cnt = int(sys.stdin.readline())
match = [[int(x) for x in sys.stdin.readline().split()] for i in range(cnt)]
diff=[]
for i in range(cnt):
      if i!=0:
             match[i]=[sum(vals) for vals in zip(match[i-1],match[i])]
      diff.append([1 if max(match[i])==match[i][0] else 2,abs(match[i][0]-match[i][1])])
maxval = max(diff,key=lambda x:x[1])
sys.stdout.write(str(maxval[0]) + ' ' + str(maxval[1]))  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T00:55:24+00:00Added an answer on June 5, 2026 at 12:55 am

    I wouldn’t worry about the memory footprint (Python data structures take a little more space, and it’s normal) and also it’s hard to expect a Python script to beat a C program in terms of speed.

    Edit: no need to keep leads history

    My O(n) algorithm ran in 1.18 seconds:

    import sys
    
    rounds = int(sys.stdin.readline())
    
    score = [0,0]
    leads = [0,0]
    while rounds > 0:
        results = map(int, sys.stdin.readline().split())
        score[0] += results[0]
        score[1] += results[1]
        lead = score[0] - score[1]
        if (lead < 0 and leads[1] < -lead): leads[1] = -lead
        if (lead > 0 and leads[0] < lead): leads[0] = lead
        rounds -= 1
    
    if (leads[0] > leads[1]): print 1, leads[0]
    else: print 2, leads[1]
    

    Edit

    To see where your algorithm spends most time you can use:

    cat inputfile | python -m cProfile yourScript.py
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I saw here a solution, but i don't want wait until the key is
I've tried the solution here , but it doesn't work. My table is like
There might be an easy solution here, but it seems to have me tripped
I'm guessing there is a simple solution here, but I've tried some things and
I'm pretty sure there is a very simple solution here but I can't seem
the solution here: Jeditable CANCEL callback from AJAX callback? using onreset() doesnt seem to
I'm searching for the cleanest solution here. I would like to code a stored
gem install heroku failed with following message and I have tried the solution here
I am trying to use the solution given here -> C# ADO.NET: nulls and
I am aware that there is a similar question here with no solution. I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.