Create list and add 1 to list values from index [I,R].Print the Occurence of a specific number

Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
1
down vote
favorite
def add(a,b):
for i in xrange(a-1,b):
box[i]+=1
def count(k):
if box.count(k)!=0:
box.index(k)
return len(box)-box.index(k)
else:
return 0
from sys import stdin,stdout
box=[0]*int(stdin.readline()) #input the number of boxes of chocolate
for i in xrange(int(stdin.readline())): #loops for M days
a,b = stdin.readline().split() #gets the starting and ending index.
add(int(a),int(b)) #adding 1 each time to box for I,R
box.sort()
for i in xrange(int(stdin.readline())):
print count(int(stdin.readline())) #prints the number of boxes of chocolates greater than equal to the input number
For small values the program works but fails for large value.Suggest any faster method/algorithm.
Constraints:
1 <= N <= 100000
1 <= M <= 1000
1 <= l <= r <= N
1 <= Q <= 100000
1 <= K <= N
python python-2.7 time-limit-exceeded
add a comment |Â
up vote
1
down vote
favorite
def add(a,b):
for i in xrange(a-1,b):
box[i]+=1
def count(k):
if box.count(k)!=0:
box.index(k)
return len(box)-box.index(k)
else:
return 0
from sys import stdin,stdout
box=[0]*int(stdin.readline()) #input the number of boxes of chocolate
for i in xrange(int(stdin.readline())): #loops for M days
a,b = stdin.readline().split() #gets the starting and ending index.
add(int(a),int(b)) #adding 1 each time to box for I,R
box.sort()
for i in xrange(int(stdin.readline())):
print count(int(stdin.readline())) #prints the number of boxes of chocolates greater than equal to the input number
For small values the program works but fails for large value.Suggest any faster method/algorithm.
Constraints:
1 <= N <= 100000
1 <= M <= 1000
1 <= l <= r <= N
1 <= Q <= 100000
1 <= K <= N
python python-2.7 time-limit-exceeded
1
Hello, and welcome to code review. Could I please check what you mean by "fails for large values?" Is it giving the wrong answer, or just taking too long to give any answer at all?
â Josiah
Jul 18 at 6:46
1
fails for large values means Time Limit is Exceeded
â AYUSH SINGHAL
Jul 18 at 7:12
1
The comments in the code feels like this is a coding challenge. Do you mind adding the description of the challenge into the question, please?
â Mathias Ettinger
Jul 18 at 8:32
1
I also removed the python-3.x tag asxrangedoesn't exist anymore andprintas a keyword is not valid syntax.
â Mathias Ettinger
Jul 18 at 8:37
I've removed the Python 2.6 tag. You'd initially tagged this question with all Python tags but the code is written in Python 2.7. If this code HAS to run on Python 2.6, please state so explicitly in the question. The 2.7 tag will eventually be renamed to 2.x.
â Mast
Jul 30 at 7:00
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
def add(a,b):
for i in xrange(a-1,b):
box[i]+=1
def count(k):
if box.count(k)!=0:
box.index(k)
return len(box)-box.index(k)
else:
return 0
from sys import stdin,stdout
box=[0]*int(stdin.readline()) #input the number of boxes of chocolate
for i in xrange(int(stdin.readline())): #loops for M days
a,b = stdin.readline().split() #gets the starting and ending index.
add(int(a),int(b)) #adding 1 each time to box for I,R
box.sort()
for i in xrange(int(stdin.readline())):
print count(int(stdin.readline())) #prints the number of boxes of chocolates greater than equal to the input number
For small values the program works but fails for large value.Suggest any faster method/algorithm.
Constraints:
1 <= N <= 100000
1 <= M <= 1000
1 <= l <= r <= N
1 <= Q <= 100000
1 <= K <= N
python python-2.7 time-limit-exceeded
def add(a,b):
for i in xrange(a-1,b):
box[i]+=1
def count(k):
if box.count(k)!=0:
box.index(k)
return len(box)-box.index(k)
else:
return 0
from sys import stdin,stdout
box=[0]*int(stdin.readline()) #input the number of boxes of chocolate
for i in xrange(int(stdin.readline())): #loops for M days
a,b = stdin.readline().split() #gets the starting and ending index.
add(int(a),int(b)) #adding 1 each time to box for I,R
box.sort()
for i in xrange(int(stdin.readline())):
print count(int(stdin.readline())) #prints the number of boxes of chocolates greater than equal to the input number
For small values the program works but fails for large value.Suggest any faster method/algorithm.
Constraints:
1 <= N <= 100000
1 <= M <= 1000
1 <= l <= r <= N
1 <= Q <= 100000
1 <= K <= N
python python-2.7 time-limit-exceeded
edited Jul 30 at 6:58
Mast
7,30663483
7,30663483
asked Jul 18 at 6:05
user175190
61
61
1
Hello, and welcome to code review. Could I please check what you mean by "fails for large values?" Is it giving the wrong answer, or just taking too long to give any answer at all?
â Josiah
Jul 18 at 6:46
1
fails for large values means Time Limit is Exceeded
â AYUSH SINGHAL
Jul 18 at 7:12
1
The comments in the code feels like this is a coding challenge. Do you mind adding the description of the challenge into the question, please?
â Mathias Ettinger
Jul 18 at 8:32
1
I also removed the python-3.x tag asxrangedoesn't exist anymore andprintas a keyword is not valid syntax.
â Mathias Ettinger
Jul 18 at 8:37
I've removed the Python 2.6 tag. You'd initially tagged this question with all Python tags but the code is written in Python 2.7. If this code HAS to run on Python 2.6, please state so explicitly in the question. The 2.7 tag will eventually be renamed to 2.x.
â Mast
Jul 30 at 7:00
add a comment |Â
1
Hello, and welcome to code review. Could I please check what you mean by "fails for large values?" Is it giving the wrong answer, or just taking too long to give any answer at all?
â Josiah
Jul 18 at 6:46
1
fails for large values means Time Limit is Exceeded
â AYUSH SINGHAL
Jul 18 at 7:12
1
The comments in the code feels like this is a coding challenge. Do you mind adding the description of the challenge into the question, please?
â Mathias Ettinger
Jul 18 at 8:32
1
I also removed the python-3.x tag asxrangedoesn't exist anymore andprintas a keyword is not valid syntax.
â Mathias Ettinger
Jul 18 at 8:37
I've removed the Python 2.6 tag. You'd initially tagged this question with all Python tags but the code is written in Python 2.7. If this code HAS to run on Python 2.6, please state so explicitly in the question. The 2.7 tag will eventually be renamed to 2.x.
â Mast
Jul 30 at 7:00
1
1
Hello, and welcome to code review. Could I please check what you mean by "fails for large values?" Is it giving the wrong answer, or just taking too long to give any answer at all?
â Josiah
Jul 18 at 6:46
Hello, and welcome to code review. Could I please check what you mean by "fails for large values?" Is it giving the wrong answer, or just taking too long to give any answer at all?
â Josiah
Jul 18 at 6:46
1
1
fails for large values means Time Limit is Exceeded
â AYUSH SINGHAL
Jul 18 at 7:12
fails for large values means Time Limit is Exceeded
â AYUSH SINGHAL
Jul 18 at 7:12
1
1
The comments in the code feels like this is a coding challenge. Do you mind adding the description of the challenge into the question, please?
â Mathias Ettinger
Jul 18 at 8:32
The comments in the code feels like this is a coding challenge. Do you mind adding the description of the challenge into the question, please?
â Mathias Ettinger
Jul 18 at 8:32
1
1
I also removed the python-3.x tag as
xrange doesn't exist anymore and print as a keyword is not valid syntax.â Mathias Ettinger
Jul 18 at 8:37
I also removed the python-3.x tag as
xrange doesn't exist anymore and print as a keyword is not valid syntax.â Mathias Ettinger
Jul 18 at 8:37
I've removed the Python 2.6 tag. You'd initially tagged this question with all Python tags but the code is written in Python 2.7. If this code HAS to run on Python 2.6, please state so explicitly in the question. The 2.7 tag will eventually be renamed to 2.x.
â Mast
Jul 30 at 7:00
I've removed the Python 2.6 tag. You'd initially tagged this question with all Python tags but the code is written in Python 2.7. If this code HAS to run on Python 2.6, please state so explicitly in the question. The 2.7 tag will eventually be renamed to 2.x.
â Mast
Jul 30 at 7:00
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f199724%2fcreate-list-and-add-1-to-list-values-from-index-i-r-print-the-occurence-of-a-s%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
Hello, and welcome to code review. Could I please check what you mean by "fails for large values?" Is it giving the wrong answer, or just taking too long to give any answer at all?
â Josiah
Jul 18 at 6:46
1
fails for large values means Time Limit is Exceeded
â AYUSH SINGHAL
Jul 18 at 7:12
1
The comments in the code feels like this is a coding challenge. Do you mind adding the description of the challenge into the question, please?
â Mathias Ettinger
Jul 18 at 8:32
1
I also removed the python-3.x tag as
xrangedoesn't exist anymore andprintas a keyword is not valid syntax.â Mathias Ettinger
Jul 18 at 8:37
I've removed the Python 2.6 tag. You'd initially tagged this question with all Python tags but the code is written in Python 2.7. If this code HAS to run on Python 2.6, please state so explicitly in the question. The 2.7 tag will eventually be renamed to 2.x.
â Mast
Jul 30 at 7:00