Skip to main content
Code Review

Return to Question

added 96 characters in body; edited tags; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

How can I improve "Find the performance of this code?Min" challenge on Facebook Hacker Cup

I was solving the Find the Min"Find the Min" problem on facebook hackercupFacebook Hacker Cup.

The code below works fine for the sample inputs given there, but for input size as big as 10^9109, this takes hours to return the solution.

problem statement:

Problem statement:

After sending smileys, John decided to play with arrays. Did you know that hackers enjoy playing with arrays? John has a zero-based index array, m, which contains n non-negative integers. However, only the first k values of the array are known to him, and he wants to figure out the rest.

John knows the following: for each index i, where k <= i < n, m[i] is the minimum non-negative integer which is not contained in the previous *k* values of m.

For example, if k = 3, n = 4 and the known values of m are [2, 3, 0], he can figure out that m[3] = 1.

John is very busy making the world more open and connected, as such, he doesn't have time to figure out the rest of the array. It is your task to help him.

Given the first k values of m, calculate the nth value of this array. (i.e. m[n - 1]).

Because the values of n and k can be very large, we use a pseudo-random number generator to calculate the first k values of m. Given positive integers a, b, c and r, the known values of m can be calculated as follows:

m[0] = a
m[i] = (b * m[i - 1] + c) % r, 0 < i < k

Input

  • The first line contains an integer T (T <= 20), the number of test cases.

  • This is followed by T test cases, consisting of 2 lines each.

  • The first line of each test case contains 2 space separated integers, n, k (1 <= k <= 10^5, k < n <= 10^9).

  • The second line of each test case contains 4 space separated integers a, b, c, r (0 <= a, b, c <= 10^9, 1 <= r <= 10^9).

Problem statement:

After sending smileys, John decided to play with arrays. Did you know that hackers enjoy playing with arrays? John has a zero-based index array, m, which contains n non-negative integers. However, only the first k values of the array are known to him, and he wants to figure out the rest.

John knows the following: for each index i, where k <= i < n, m[i] is the minimum non-negative integer which is not contained in the previous *k* values of m.

For example, if k = 3, n = 4 and the known values of m are [2, 3, 0], he can figure out that m[3] = 1.

John is very busy making the world more open and connected, as such, he doesn't have time to figure out the rest of the array. It is your task to help him.

Given the first k values of m, calculate the nth value of this array. (i.e. m[n - 1]).

Because the values of n and k can be very large, we use a pseudo-random number generator to calculate the first k values of m. Given positive integers a, b, c and r, the known values of m can be calculated as follows:

m[0] = a
m[i] = (b * m[i - 1] + c) % r, 0 < i < k

Input

  • The first line contains an integer T (T <= 20), the number of test cases.

  • This is followed by T test cases, consisting of 2 lines each.

  • The first line of each test case contains 2 space separated integers, n, k (\1ドル <= k \le 10^5, k < n \le 10^9\$).

  • The second line of each test case contains 4 space separated integers a, b, c, r (\0ドル \le a, b, c \le 10^9, 1 \le r <= 10^9\$).

sampleSample input:

5
97 39
34 37 656 97
186 75
68 16 539 186
137 49
48 17 461 137
98 59
6 30 524 98
46 18
7 11 9 46
5
97 39
34 37 656 97
186 75
68 16 539 186
137 49
48 17 461 137
98 59
6 30 524 98
46 18
7 11 9 46

How can I improve the performance of this code?

I was solving the Find the Min problem on facebook hackercup.

The code below works fine for the sample inputs given there, but for input size as big as 10^9 this takes hours to return the solution.

problem statement:

Problem statement:

After sending smileys, John decided to play with arrays. Did you know that hackers enjoy playing with arrays? John has a zero-based index array, m, which contains n non-negative integers. However, only the first k values of the array are known to him, and he wants to figure out the rest.

John knows the following: for each index i, where k <= i < n, m[i] is the minimum non-negative integer which is not contained in the previous *k* values of m.

For example, if k = 3, n = 4 and the known values of m are [2, 3, 0], he can figure out that m[3] = 1.

John is very busy making the world more open and connected, as such, he doesn't have time to figure out the rest of the array. It is your task to help him.

Given the first k values of m, calculate the nth value of this array. (i.e. m[n - 1]).

Because the values of n and k can be very large, we use a pseudo-random number generator to calculate the first k values of m. Given positive integers a, b, c and r, the known values of m can be calculated as follows:

m[0] = a
m[i] = (b * m[i - 1] + c) % r, 0 < i < k

Input

  • The first line contains an integer T (T <= 20), the number of test cases.

  • This is followed by T test cases, consisting of 2 lines each.

  • The first line of each test case contains 2 space separated integers, n, k (1 <= k <= 10^5, k < n <= 10^9).

  • The second line of each test case contains 4 space separated integers a, b, c, r (0 <= a, b, c <= 10^9, 1 <= r <= 10^9).

sample input:

5
97 39
34 37 656 97
186 75
68 16 539 186
137 49
48 17 461 137
98 59
6 30 524 98
46 18
7 11 9 46

"Find the Min" challenge on Facebook Hacker Cup

I was solving the "Find the Min" problem on Facebook Hacker Cup.

The code below works fine for the sample inputs given there, but for input size as big as 109, this takes hours to return the solution.

Problem statement:

After sending smileys, John decided to play with arrays. Did you know that hackers enjoy playing with arrays? John has a zero-based index array, m, which contains n non-negative integers. However, only the first k values of the array are known to him, and he wants to figure out the rest.

John knows the following: for each index i, where k <= i < n, m[i] is the minimum non-negative integer which is not contained in the previous *k* values of m.

For example, if k = 3, n = 4 and the known values of m are [2, 3, 0], he can figure out that m[3] = 1.

John is very busy making the world more open and connected, as such, he doesn't have time to figure out the rest of the array. It is your task to help him.

Given the first k values of m, calculate the nth value of this array. (i.e. m[n - 1]).

Because the values of n and k can be very large, we use a pseudo-random number generator to calculate the first k values of m. Given positive integers a, b, c and r, the known values of m can be calculated as follows:

m[0] = a
m[i] = (b * m[i - 1] + c) % r, 0 < i < k

Input

  • The first line contains an integer T (T <= 20), the number of test cases.

  • This is followed by T test cases, consisting of 2 lines each.

  • The first line of each test case contains 2 space separated integers, n, k (\1ドル <= k \le 10^5, k < n \le 10^9\$).

  • The second line of each test case contains 4 space separated integers a, b, c, r (\0ドル \le a, b, c \le 10^9, 1 \le r <= 10^9\$).

Sample input:

5
97 39
34 37 656 97
186 75
68 16 539 186
137 49
48 17 461 137
98 59
6 30 524 98
46 18
7 11 9 46
Post Unlocked by Winston Ewert
Notice removed Content dispute by Winston Ewert
Post Locked by Winston Ewert
Notice added Content dispute by Winston Ewert
Notice removed Content dispute by Community Bot
Post Unlocked by Community Bot
Post Locked by Winston Ewert
Notice added Content dispute by Winston Ewert
added 1600 characters in body
Source Link

What changesproblem statement:

Problem statement:

After sending smileys, John decided to play with arrays. Did you know that hackers enjoy playing with arrays? John has a zero-based index array, m, which contains n non-negative integers. However, only the first k values of the array are requiredknown to makehim, and he wants to figure out the rest.

John knows the following: for each index i, where k <= i < n, m[i] is the minimum non-negative integer which is not contained in the previous *k* values of m.

For example, if k = 3, n = 4 and the known values of m are [2, 3, 0], he can figure out that m[3] = 1.

John is very busy making the world more open and connected, as such, he doesn't have time to figure out the rest of the array. It is your task to help him.

Given the first k values of m, calculate the nth value of this array. (i.e. m[n - 1]).

Because the values of n and k can be very large, we use a pseudo-random number generator to calculate the first k values of m. Given positive integers a, b, c and r, the known values of m can be calculated as follows:

m[0] = a
m[i] = (b * m[i - 1] + c) % r, 0 < i < k

Input

  • The first line contains an integer T (T <= 20), the number of test cases.

  • This is followed by T test cases, consisting of 2 lines each.

  • The first line of each test case contains 2 space separated integers, n, k (1 <= k <= 10^5, k < n <= 10^9).

  • The second line of each test case contains 4 space separated integers a, b, c, r (0 <= a, b, c <= 10^9, 1 <= r <= 10^9).

My solution better?:

What changes are required to make this solution better?

problem statement:

Problem statement:

After sending smileys, John decided to play with arrays. Did you know that hackers enjoy playing with arrays? John has a zero-based index array, m, which contains n non-negative integers. However, only the first k values of the array are known to him, and he wants to figure out the rest.

John knows the following: for each index i, where k <= i < n, m[i] is the minimum non-negative integer which is not contained in the previous *k* values of m.

For example, if k = 3, n = 4 and the known values of m are [2, 3, 0], he can figure out that m[3] = 1.

John is very busy making the world more open and connected, as such, he doesn't have time to figure out the rest of the array. It is your task to help him.

Given the first k values of m, calculate the nth value of this array. (i.e. m[n - 1]).

Because the values of n and k can be very large, we use a pseudo-random number generator to calculate the first k values of m. Given positive integers a, b, c and r, the known values of m can be calculated as follows:

m[0] = a
m[i] = (b * m[i - 1] + c) % r, 0 < i < k

Input

  • The first line contains an integer T (T <= 20), the number of test cases.

  • This is followed by T test cases, consisting of 2 lines each.

  • The first line of each test case contains 2 space separated integers, n, k (1 <= k <= 10^5, k < n <= 10^9).

  • The second line of each test case contains 4 space separated integers a, b, c, r (0 <= a, b, c <= 10^9, 1 <= r <= 10^9).

My solution:

added 2 characters in body
Source Link

I was solving the Find the Min problem on facebook hackercup.

The code below works fine for the sample inputs given there, but for input size as big as 10^9 this takes hours to return the solution.

What changes are required to make this solution better?

import sys
cases=sys.stdin.readlines()
def func(line1,line2):
 n,k=map(int,line1.split())
 a,b,c,r =map(int,line2.split())
 m=[""]*nm=[None]*n
 m[0]=a
 for i in xrange(1,k):
 m[i]= (b * m[i - 1] + c) % r
 #print m 
 for j in range(0,n-k):
 
 temp=set(m[j:k+j])
 i=-1
 while True:
 i+=1
 if i not in temp:
 m[k+j]=i
 break
 return m[-1]
for ind,case in enumerate(xrange(1,len(cases),2)):
 ans=func(cases[case],cases[case+1])
 print "Case #{0}: {1}".format(ind+1,ans) 

sample input:

5
97 39
34 37 656 97
186 75
68 16 539 186
137 49
48 17 461 137
98 59
6 30 524 98
46 18
7 11 9 46

I was solving the Find the Min problem on facebook hackercup.

The code below works fine for the sample inputs given there, but for input size as big as 10^9 this takes hours to return the solution.

What changes are required to make this solution better?

import sys
cases=sys.stdin.readlines()
def func(line1,line2):
 n,k=map(int,line1.split())
 a,b,c,r =map(int,line2.split())
 m=[""]*n
 m[0]=a
 for i in xrange(1,k):
 m[i]= (b * m[i - 1] + c) % r
 #print m 
 for j in range(0,n-k):
 
 temp=set(m[j:k+j])
 i=-1
 while True:
 i+=1
 if i not in temp:
 m[k+j]=i
 break
 return m[-1]
for ind,case in enumerate(xrange(1,len(cases),2)):
 ans=func(cases[case],cases[case+1])
 print "Case #{0}: {1}".format(ind+1,ans) 

sample input:

5
97 39
34 37 656 97
186 75
68 16 539 186
137 49
48 17 461 137
98 59
6 30 524 98
46 18
7 11 9 46

I was solving the Find the Min problem on facebook hackercup.

The code below works fine for the sample inputs given there, but for input size as big as 10^9 this takes hours to return the solution.

What changes are required to make this solution better?

import sys
cases=sys.stdin.readlines()
def func(line1,line2):
 n,k=map(int,line1.split())
 a,b,c,r =map(int,line2.split())
 m=[None]*n
 m[0]=a
 for i in xrange(1,k):
 m[i]= (b * m[i - 1] + c) % r
 #print m 
 for j in range(0,n-k):
 
 temp=set(m[j:k+j])
 i=-1
 while True:
 i+=1
 if i not in temp:
 m[k+j]=i
 break
 return m[-1]
for ind,case in enumerate(xrange(1,len(cases),2)):
 ans=func(cases[case],cases[case+1])
 print "Case #{0}: {1}".format(ind+1,ans) 

sample input:

5
97 39
34 37 656 97
186 75
68 16 539 186
137 49
48 17 461 137
98 59
6 30 524 98
46 18
7 11 9 46
deleted 7 characters in body
Source Link
Loading
deleted 30 characters in body
Source Link
Loading
edited body
Source Link
Loading
Source Link
Loading
lang-py

AltStyle によって変換されたページ (->オリジナル) /