Fastest algorithm to perform N*M iterations in Python

Multi tool use
up vote
4
down vote
favorite
I am not an algorithm person, so pardon the naivety of the question.
I have a list A containing elements 100K elements. I have another list B containing 100K elements. Let's say a is an element from list A, b is the element from list B. I want to find out those (a, b) combinations whose sum is less than 100.
One obvious way to do this is following:
results =
for a in A:
for b in B:
if (a+b) < 100:
results.append((a,b))
but the time complexity of this approach is O(n*m) = 100K * 100K which is quite large. Is there any fast algorithm which can compute the desired output more efficiently - in terms of memory and time. If yes, can it be implemented in python?
python algorithm data-structures
|
show 7 more comments
up vote
4
down vote
favorite
I am not an algorithm person, so pardon the naivety of the question.
I have a list A containing elements 100K elements. I have another list B containing 100K elements. Let's say a is an element from list A, b is the element from list B. I want to find out those (a, b) combinations whose sum is less than 100.
One obvious way to do this is following:
results =
for a in A:
for b in B:
if (a+b) < 100:
results.append((a,b))
but the time complexity of this approach is O(n*m) = 100K * 100K which is quite large. Is there any fast algorithm which can compute the desired output more efficiently - in terms of memory and time. If yes, can it be implemented in python?
python algorithm data-structures
1
If you want to find all the actual pairs, then there is no faster method as the final output can be as big as 100k * 100k.
– merlyn
Nov 9 at 16:15
1
@merlyn There are 5 positive integers smaller than 6, but I didn't have to think about all 5 to come up with that. Well, unless you want the complete list of them, indeed.
– Nelfeal
Nov 9 at 16:17
1
@Nelfeal That's kind of my point. You can find the number of solutions, not the solutions themselves. That could very well be what the OP actually wanted, but in the question, he is calculating the actual list.
– merlyn
Nov 9 at 16:19
1
@userxxx , you should not ask a follow up question in the comments. Instead, ask a new question and reference this question if necessary.
– Joseph Wood
Nov 9 at 17:07
1
@userxxx Finding all pairs of stringsa, b
such thatconcatenation(a, b) = target
is even easier: only somea
s can serve as prefix, and only someb
s can serve as suffix. But the problem is quite different, so the solution is also quite different.
– Nelfeal
Nov 9 at 17:07
|
show 7 more comments
up vote
4
down vote
favorite
up vote
4
down vote
favorite
I am not an algorithm person, so pardon the naivety of the question.
I have a list A containing elements 100K elements. I have another list B containing 100K elements. Let's say a is an element from list A, b is the element from list B. I want to find out those (a, b) combinations whose sum is less than 100.
One obvious way to do this is following:
results =
for a in A:
for b in B:
if (a+b) < 100:
results.append((a,b))
but the time complexity of this approach is O(n*m) = 100K * 100K which is quite large. Is there any fast algorithm which can compute the desired output more efficiently - in terms of memory and time. If yes, can it be implemented in python?
python algorithm data-structures
I am not an algorithm person, so pardon the naivety of the question.
I have a list A containing elements 100K elements. I have another list B containing 100K elements. Let's say a is an element from list A, b is the element from list B. I want to find out those (a, b) combinations whose sum is less than 100.
One obvious way to do this is following:
results =
for a in A:
for b in B:
if (a+b) < 100:
results.append((a,b))
but the time complexity of this approach is O(n*m) = 100K * 100K which is quite large. Is there any fast algorithm which can compute the desired output more efficiently - in terms of memory and time. If yes, can it be implemented in python?
python algorithm data-structures
python algorithm data-structures
asked Nov 9 at 16:07
userxxx
401515
401515
1
If you want to find all the actual pairs, then there is no faster method as the final output can be as big as 100k * 100k.
– merlyn
Nov 9 at 16:15
1
@merlyn There are 5 positive integers smaller than 6, but I didn't have to think about all 5 to come up with that. Well, unless you want the complete list of them, indeed.
– Nelfeal
Nov 9 at 16:17
1
@Nelfeal That's kind of my point. You can find the number of solutions, not the solutions themselves. That could very well be what the OP actually wanted, but in the question, he is calculating the actual list.
– merlyn
Nov 9 at 16:19
1
@userxxx , you should not ask a follow up question in the comments. Instead, ask a new question and reference this question if necessary.
– Joseph Wood
Nov 9 at 17:07
1
@userxxx Finding all pairs of stringsa, b
such thatconcatenation(a, b) = target
is even easier: only somea
s can serve as prefix, and only someb
s can serve as suffix. But the problem is quite different, so the solution is also quite different.
– Nelfeal
Nov 9 at 17:07
|
show 7 more comments
1
If you want to find all the actual pairs, then there is no faster method as the final output can be as big as 100k * 100k.
– merlyn
Nov 9 at 16:15
1
@merlyn There are 5 positive integers smaller than 6, but I didn't have to think about all 5 to come up with that. Well, unless you want the complete list of them, indeed.
– Nelfeal
Nov 9 at 16:17
1
@Nelfeal That's kind of my point. You can find the number of solutions, not the solutions themselves. That could very well be what the OP actually wanted, but in the question, he is calculating the actual list.
– merlyn
Nov 9 at 16:19
1
@userxxx , you should not ask a follow up question in the comments. Instead, ask a new question and reference this question if necessary.
– Joseph Wood
Nov 9 at 17:07
1
@userxxx Finding all pairs of stringsa, b
such thatconcatenation(a, b) = target
is even easier: only somea
s can serve as prefix, and only someb
s can serve as suffix. But the problem is quite different, so the solution is also quite different.
– Nelfeal
Nov 9 at 17:07
1
1
If you want to find all the actual pairs, then there is no faster method as the final output can be as big as 100k * 100k.
– merlyn
Nov 9 at 16:15
If you want to find all the actual pairs, then there is no faster method as the final output can be as big as 100k * 100k.
– merlyn
Nov 9 at 16:15
1
1
@merlyn There are 5 positive integers smaller than 6, but I didn't have to think about all 5 to come up with that. Well, unless you want the complete list of them, indeed.
– Nelfeal
Nov 9 at 16:17
@merlyn There are 5 positive integers smaller than 6, but I didn't have to think about all 5 to come up with that. Well, unless you want the complete list of them, indeed.
– Nelfeal
Nov 9 at 16:17
1
1
@Nelfeal That's kind of my point. You can find the number of solutions, not the solutions themselves. That could very well be what the OP actually wanted, but in the question, he is calculating the actual list.
– merlyn
Nov 9 at 16:19
@Nelfeal That's kind of my point. You can find the number of solutions, not the solutions themselves. That could very well be what the OP actually wanted, but in the question, he is calculating the actual list.
– merlyn
Nov 9 at 16:19
1
1
@userxxx , you should not ask a follow up question in the comments. Instead, ask a new question and reference this question if necessary.
– Joseph Wood
Nov 9 at 17:07
@userxxx , you should not ask a follow up question in the comments. Instead, ask a new question and reference this question if necessary.
– Joseph Wood
Nov 9 at 17:07
1
1
@userxxx Finding all pairs of strings
a, b
such that concatenation(a, b) = target
is even easier: only some a
s can serve as prefix, and only some b
s can serve as suffix. But the problem is quite different, so the solution is also quite different.– Nelfeal
Nov 9 at 17:07
@userxxx Finding all pairs of strings
a, b
such that concatenation(a, b) = target
is even easier: only some a
s can serve as prefix, and only some b
s can serve as suffix. But the problem is quite different, so the solution is also quite different.– Nelfeal
Nov 9 at 17:07
|
show 7 more comments
3 Answers
3
active
oldest
votes
up vote
9
down vote
Sort both lists (O(n log n)
and O(m log m)
, maybe less if the values are constrained).
Then, you can simply find, for each a
in A
, the largest b
in B
such that (a+b) < 100
. Every smaller b
will also satisfy that condition.
Finding the largest b
for some a
can be done using binary search to find a lower bound in B
. And by starting with the largest a
going down, you can preserve the list of b
s corresponding to the previous a
, since the sum is going to be smaller.
yeah, knapsack problem all right. Needs special bisect implementation. That's the way.
– Jean-François Fabre
Nov 9 at 16:14
"Every smaller b will also satisfy that condition." this can beO(m)
to append them all to a list. That statement just obfuscates that part of the time complexity.
– Alex
Nov 9 at 16:22
appending would, but a slice should be O(1), correct?
– C.Nivs
Nov 9 at 16:22
@C.Nivs when you combine slices for each element in A, you still hitO(m)
.
– Alex
Nov 9 at 16:24
@Alex Well of course you can't go below q operations if you have q solutions and want them all in a list. I figure you either group them, or you know constraints that imply q isn't too large.
– Nelfeal
Nov 9 at 16:28
|
show 5 more comments
up vote
1
down vote
No you can't get better than that in the worst case. Consider a pathological case where every combination of A and B must be appended to the list:
A = list(range(0, -n, -1))
B = list(range(0, -m, -1))
Because each pair must be appended, you are doing O(m * n)
operations.
If you only needed to count the number of combinations this could be a different story.
add a comment |
up vote
0
down vote
As your criteria is that a + b < 100
that can be defined as range. In this case I defined it as wanted_result_range = range(0, 100)
as your lists A and B are containing only positive numbers so results will be only positive values.
Run the following script and you will see how much faster is my approach than "frontal" one that you have mentioned. My solution will be bad if wider wanted_result_range is defined.
import timeit
# http://docs.python.org/3.3/library/timeit.html
print "nTiming execution timesn"
version1="""
results1 =
list_A = range(1000)
list_B = range(1000)
wanted_result_range = range(0, 100)
d1 =
l3 =
for key in list_A:
d1[key] = True # Value associated to key is not important and can be any value
for wanted_result in wanted_result_range:
for key2 in list_B:
if ((wanted_result-key2) in d1):
results1.append((key2,wanted_result-key2))
"""
print("Version 1 - Hash table (dictionary) approach")
print(timeit.repeat(version1, number=10, repeat=4))
print('END Version 1n')
version2="""
results2 =
list_A = range(1000)
list_B = range(1000)
for a in list_A:
for b in list_B:
if (a+b) < 100:
results2.append((a,b))
"""
print("Version 2 - Frontal approach")
print(timeit.repeat(version2, number=10, repeat=4))
print('END Version 2n')
Nowhere in the question is stated thatA
andB
contain only positive numbers.
– Nelfeal
Nov 10 at 7:07
@Nelfeal This can be used with negative numbers too.
– jaskowitchious
Nov 10 at 21:28
Really? Good luck making it work with the "wanted result range" being[-inf, 100]
.
– Nelfeal
Nov 11 at 7:09
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
9
down vote
Sort both lists (O(n log n)
and O(m log m)
, maybe less if the values are constrained).
Then, you can simply find, for each a
in A
, the largest b
in B
such that (a+b) < 100
. Every smaller b
will also satisfy that condition.
Finding the largest b
for some a
can be done using binary search to find a lower bound in B
. And by starting with the largest a
going down, you can preserve the list of b
s corresponding to the previous a
, since the sum is going to be smaller.
yeah, knapsack problem all right. Needs special bisect implementation. That's the way.
– Jean-François Fabre
Nov 9 at 16:14
"Every smaller b will also satisfy that condition." this can beO(m)
to append them all to a list. That statement just obfuscates that part of the time complexity.
– Alex
Nov 9 at 16:22
appending would, but a slice should be O(1), correct?
– C.Nivs
Nov 9 at 16:22
@C.Nivs when you combine slices for each element in A, you still hitO(m)
.
– Alex
Nov 9 at 16:24
@Alex Well of course you can't go below q operations if you have q solutions and want them all in a list. I figure you either group them, or you know constraints that imply q isn't too large.
– Nelfeal
Nov 9 at 16:28
|
show 5 more comments
up vote
9
down vote
Sort both lists (O(n log n)
and O(m log m)
, maybe less if the values are constrained).
Then, you can simply find, for each a
in A
, the largest b
in B
such that (a+b) < 100
. Every smaller b
will also satisfy that condition.
Finding the largest b
for some a
can be done using binary search to find a lower bound in B
. And by starting with the largest a
going down, you can preserve the list of b
s corresponding to the previous a
, since the sum is going to be smaller.
yeah, knapsack problem all right. Needs special bisect implementation. That's the way.
– Jean-François Fabre
Nov 9 at 16:14
"Every smaller b will also satisfy that condition." this can beO(m)
to append them all to a list. That statement just obfuscates that part of the time complexity.
– Alex
Nov 9 at 16:22
appending would, but a slice should be O(1), correct?
– C.Nivs
Nov 9 at 16:22
@C.Nivs when you combine slices for each element in A, you still hitO(m)
.
– Alex
Nov 9 at 16:24
@Alex Well of course you can't go below q operations if you have q solutions and want them all in a list. I figure you either group them, or you know constraints that imply q isn't too large.
– Nelfeal
Nov 9 at 16:28
|
show 5 more comments
up vote
9
down vote
up vote
9
down vote
Sort both lists (O(n log n)
and O(m log m)
, maybe less if the values are constrained).
Then, you can simply find, for each a
in A
, the largest b
in B
such that (a+b) < 100
. Every smaller b
will also satisfy that condition.
Finding the largest b
for some a
can be done using binary search to find a lower bound in B
. And by starting with the largest a
going down, you can preserve the list of b
s corresponding to the previous a
, since the sum is going to be smaller.
Sort both lists (O(n log n)
and O(m log m)
, maybe less if the values are constrained).
Then, you can simply find, for each a
in A
, the largest b
in B
such that (a+b) < 100
. Every smaller b
will also satisfy that condition.
Finding the largest b
for some a
can be done using binary search to find a lower bound in B
. And by starting with the largest a
going down, you can preserve the list of b
s corresponding to the previous a
, since the sum is going to be smaller.
edited Nov 9 at 18:02
answered Nov 9 at 16:13
Nelfeal
3,773621
3,773621
yeah, knapsack problem all right. Needs special bisect implementation. That's the way.
– Jean-François Fabre
Nov 9 at 16:14
"Every smaller b will also satisfy that condition." this can beO(m)
to append them all to a list. That statement just obfuscates that part of the time complexity.
– Alex
Nov 9 at 16:22
appending would, but a slice should be O(1), correct?
– C.Nivs
Nov 9 at 16:22
@C.Nivs when you combine slices for each element in A, you still hitO(m)
.
– Alex
Nov 9 at 16:24
@Alex Well of course you can't go below q operations if you have q solutions and want them all in a list. I figure you either group them, or you know constraints that imply q isn't too large.
– Nelfeal
Nov 9 at 16:28
|
show 5 more comments
yeah, knapsack problem all right. Needs special bisect implementation. That's the way.
– Jean-François Fabre
Nov 9 at 16:14
"Every smaller b will also satisfy that condition." this can beO(m)
to append them all to a list. That statement just obfuscates that part of the time complexity.
– Alex
Nov 9 at 16:22
appending would, but a slice should be O(1), correct?
– C.Nivs
Nov 9 at 16:22
@C.Nivs when you combine slices for each element in A, you still hitO(m)
.
– Alex
Nov 9 at 16:24
@Alex Well of course you can't go below q operations if you have q solutions and want them all in a list. I figure you either group them, or you know constraints that imply q isn't too large.
– Nelfeal
Nov 9 at 16:28
yeah, knapsack problem all right. Needs special bisect implementation. That's the way.
– Jean-François Fabre
Nov 9 at 16:14
yeah, knapsack problem all right. Needs special bisect implementation. That's the way.
– Jean-François Fabre
Nov 9 at 16:14
"Every smaller b will also satisfy that condition." this can be
O(m)
to append them all to a list. That statement just obfuscates that part of the time complexity.– Alex
Nov 9 at 16:22
"Every smaller b will also satisfy that condition." this can be
O(m)
to append them all to a list. That statement just obfuscates that part of the time complexity.– Alex
Nov 9 at 16:22
appending would, but a slice should be O(1), correct?
– C.Nivs
Nov 9 at 16:22
appending would, but a slice should be O(1), correct?
– C.Nivs
Nov 9 at 16:22
@C.Nivs when you combine slices for each element in A, you still hit
O(m)
.– Alex
Nov 9 at 16:24
@C.Nivs when you combine slices for each element in A, you still hit
O(m)
.– Alex
Nov 9 at 16:24
@Alex Well of course you can't go below q operations if you have q solutions and want them all in a list. I figure you either group them, or you know constraints that imply q isn't too large.
– Nelfeal
Nov 9 at 16:28
@Alex Well of course you can't go below q operations if you have q solutions and want them all in a list. I figure you either group them, or you know constraints that imply q isn't too large.
– Nelfeal
Nov 9 at 16:28
|
show 5 more comments
up vote
1
down vote
No you can't get better than that in the worst case. Consider a pathological case where every combination of A and B must be appended to the list:
A = list(range(0, -n, -1))
B = list(range(0, -m, -1))
Because each pair must be appended, you are doing O(m * n)
operations.
If you only needed to count the number of combinations this could be a different story.
add a comment |
up vote
1
down vote
No you can't get better than that in the worst case. Consider a pathological case where every combination of A and B must be appended to the list:
A = list(range(0, -n, -1))
B = list(range(0, -m, -1))
Because each pair must be appended, you are doing O(m * n)
operations.
If you only needed to count the number of combinations this could be a different story.
add a comment |
up vote
1
down vote
up vote
1
down vote
No you can't get better than that in the worst case. Consider a pathological case where every combination of A and B must be appended to the list:
A = list(range(0, -n, -1))
B = list(range(0, -m, -1))
Because each pair must be appended, you are doing O(m * n)
operations.
If you only needed to count the number of combinations this could be a different story.
No you can't get better than that in the worst case. Consider a pathological case where every combination of A and B must be appended to the list:
A = list(range(0, -n, -1))
B = list(range(0, -m, -1))
Because each pair must be appended, you are doing O(m * n)
operations.
If you only needed to count the number of combinations this could be a different story.
answered Nov 9 at 16:36
Alex
10.2k32354
10.2k32354
add a comment |
add a comment |
up vote
0
down vote
As your criteria is that a + b < 100
that can be defined as range. In this case I defined it as wanted_result_range = range(0, 100)
as your lists A and B are containing only positive numbers so results will be only positive values.
Run the following script and you will see how much faster is my approach than "frontal" one that you have mentioned. My solution will be bad if wider wanted_result_range is defined.
import timeit
# http://docs.python.org/3.3/library/timeit.html
print "nTiming execution timesn"
version1="""
results1 =
list_A = range(1000)
list_B = range(1000)
wanted_result_range = range(0, 100)
d1 =
l3 =
for key in list_A:
d1[key] = True # Value associated to key is not important and can be any value
for wanted_result in wanted_result_range:
for key2 in list_B:
if ((wanted_result-key2) in d1):
results1.append((key2,wanted_result-key2))
"""
print("Version 1 - Hash table (dictionary) approach")
print(timeit.repeat(version1, number=10, repeat=4))
print('END Version 1n')
version2="""
results2 =
list_A = range(1000)
list_B = range(1000)
for a in list_A:
for b in list_B:
if (a+b) < 100:
results2.append((a,b))
"""
print("Version 2 - Frontal approach")
print(timeit.repeat(version2, number=10, repeat=4))
print('END Version 2n')
Nowhere in the question is stated thatA
andB
contain only positive numbers.
– Nelfeal
Nov 10 at 7:07
@Nelfeal This can be used with negative numbers too.
– jaskowitchious
Nov 10 at 21:28
Really? Good luck making it work with the "wanted result range" being[-inf, 100]
.
– Nelfeal
Nov 11 at 7:09
add a comment |
up vote
0
down vote
As your criteria is that a + b < 100
that can be defined as range. In this case I defined it as wanted_result_range = range(0, 100)
as your lists A and B are containing only positive numbers so results will be only positive values.
Run the following script and you will see how much faster is my approach than "frontal" one that you have mentioned. My solution will be bad if wider wanted_result_range is defined.
import timeit
# http://docs.python.org/3.3/library/timeit.html
print "nTiming execution timesn"
version1="""
results1 =
list_A = range(1000)
list_B = range(1000)
wanted_result_range = range(0, 100)
d1 =
l3 =
for key in list_A:
d1[key] = True # Value associated to key is not important and can be any value
for wanted_result in wanted_result_range:
for key2 in list_B:
if ((wanted_result-key2) in d1):
results1.append((key2,wanted_result-key2))
"""
print("Version 1 - Hash table (dictionary) approach")
print(timeit.repeat(version1, number=10, repeat=4))
print('END Version 1n')
version2="""
results2 =
list_A = range(1000)
list_B = range(1000)
for a in list_A:
for b in list_B:
if (a+b) < 100:
results2.append((a,b))
"""
print("Version 2 - Frontal approach")
print(timeit.repeat(version2, number=10, repeat=4))
print('END Version 2n')
Nowhere in the question is stated thatA
andB
contain only positive numbers.
– Nelfeal
Nov 10 at 7:07
@Nelfeal This can be used with negative numbers too.
– jaskowitchious
Nov 10 at 21:28
Really? Good luck making it work with the "wanted result range" being[-inf, 100]
.
– Nelfeal
Nov 11 at 7:09
add a comment |
up vote
0
down vote
up vote
0
down vote
As your criteria is that a + b < 100
that can be defined as range. In this case I defined it as wanted_result_range = range(0, 100)
as your lists A and B are containing only positive numbers so results will be only positive values.
Run the following script and you will see how much faster is my approach than "frontal" one that you have mentioned. My solution will be bad if wider wanted_result_range is defined.
import timeit
# http://docs.python.org/3.3/library/timeit.html
print "nTiming execution timesn"
version1="""
results1 =
list_A = range(1000)
list_B = range(1000)
wanted_result_range = range(0, 100)
d1 =
l3 =
for key in list_A:
d1[key] = True # Value associated to key is not important and can be any value
for wanted_result in wanted_result_range:
for key2 in list_B:
if ((wanted_result-key2) in d1):
results1.append((key2,wanted_result-key2))
"""
print("Version 1 - Hash table (dictionary) approach")
print(timeit.repeat(version1, number=10, repeat=4))
print('END Version 1n')
version2="""
results2 =
list_A = range(1000)
list_B = range(1000)
for a in list_A:
for b in list_B:
if (a+b) < 100:
results2.append((a,b))
"""
print("Version 2 - Frontal approach")
print(timeit.repeat(version2, number=10, repeat=4))
print('END Version 2n')
As your criteria is that a + b < 100
that can be defined as range. In this case I defined it as wanted_result_range = range(0, 100)
as your lists A and B are containing only positive numbers so results will be only positive values.
Run the following script and you will see how much faster is my approach than "frontal" one that you have mentioned. My solution will be bad if wider wanted_result_range is defined.
import timeit
# http://docs.python.org/3.3/library/timeit.html
print "nTiming execution timesn"
version1="""
results1 =
list_A = range(1000)
list_B = range(1000)
wanted_result_range = range(0, 100)
d1 =
l3 =
for key in list_A:
d1[key] = True # Value associated to key is not important and can be any value
for wanted_result in wanted_result_range:
for key2 in list_B:
if ((wanted_result-key2) in d1):
results1.append((key2,wanted_result-key2))
"""
print("Version 1 - Hash table (dictionary) approach")
print(timeit.repeat(version1, number=10, repeat=4))
print('END Version 1n')
version2="""
results2 =
list_A = range(1000)
list_B = range(1000)
for a in list_A:
for b in list_B:
if (a+b) < 100:
results2.append((a,b))
"""
print("Version 2 - Frontal approach")
print(timeit.repeat(version2, number=10, repeat=4))
print('END Version 2n')
answered Nov 9 at 20:50
jaskowitchious
61
61
Nowhere in the question is stated thatA
andB
contain only positive numbers.
– Nelfeal
Nov 10 at 7:07
@Nelfeal This can be used with negative numbers too.
– jaskowitchious
Nov 10 at 21:28
Really? Good luck making it work with the "wanted result range" being[-inf, 100]
.
– Nelfeal
Nov 11 at 7:09
add a comment |
Nowhere in the question is stated thatA
andB
contain only positive numbers.
– Nelfeal
Nov 10 at 7:07
@Nelfeal This can be used with negative numbers too.
– jaskowitchious
Nov 10 at 21:28
Really? Good luck making it work with the "wanted result range" being[-inf, 100]
.
– Nelfeal
Nov 11 at 7:09
Nowhere in the question is stated that
A
and B
contain only positive numbers.– Nelfeal
Nov 10 at 7:07
Nowhere in the question is stated that
A
and B
contain only positive numbers.– Nelfeal
Nov 10 at 7:07
@Nelfeal This can be used with negative numbers too.
– jaskowitchious
Nov 10 at 21:28
@Nelfeal This can be used with negative numbers too.
– jaskowitchious
Nov 10 at 21:28
Really? Good luck making it work with the "wanted result range" being
[-inf, 100]
.– Nelfeal
Nov 11 at 7:09
Really? Good luck making it work with the "wanted result range" being
[-inf, 100]
.– Nelfeal
Nov 11 at 7:09
add a comment |
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53229317%2ffastest-algorithm-to-perform-nm-iterations-in-python%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
yHQ9iZJGi9oR4mbaBb2egxq,AiZa6p,NOhq,o1BO6oC2AkKkeLJjTl Uzz4l4N G,CxjOhPnqDzxYBt
1
If you want to find all the actual pairs, then there is no faster method as the final output can be as big as 100k * 100k.
– merlyn
Nov 9 at 16:15
1
@merlyn There are 5 positive integers smaller than 6, but I didn't have to think about all 5 to come up with that. Well, unless you want the complete list of them, indeed.
– Nelfeal
Nov 9 at 16:17
1
@Nelfeal That's kind of my point. You can find the number of solutions, not the solutions themselves. That could very well be what the OP actually wanted, but in the question, he is calculating the actual list.
– merlyn
Nov 9 at 16:19
1
@userxxx , you should not ask a follow up question in the comments. Instead, ask a new question and reference this question if necessary.
– Joseph Wood
Nov 9 at 17:07
1
@userxxx Finding all pairs of strings
a, b
such thatconcatenation(a, b) = target
is even easier: only somea
s can serve as prefix, and only someb
s can serve as suffix. But the problem is quite different, so the solution is also quite different.– Nelfeal
Nov 9 at 17:07