[英]Similarity Measure in Python
I am working on this coding challenge named Similarity Measure .我正在处理这个名为Similarity Measure的编码挑战。 Now the problem is my code works fine for some test cases, and failed due to the Time Limit Exceed problem.
现在的问题是我的代码在某些测试用例中运行良好,但由于 Time Limit Exceed 问题而失败。 However, my code is not wrong, takes more than 25 sec for input of range 10^4 .
但是,我的代码没有错,输入范围 10^4需要超过 25 秒。
I need to know what I can do to make it more efficient, I cannot think on any better solution than my code.我需要知道我可以做些什么来提高效率,我想不出比我的代码更好的解决方案。
Question goes like this:问题是这样的:
Problems states that given an array of positive integers, and now we have to answer based upon the Q queries.
问题说明给定一个正整数数组,现在我们必须根据 Q 查询来回答。
Query: Given two indices L,R, determine the maximum absolute difference of index of two same elements lies between L and R
查询:给定两个索引L,R,确定两个相同元素的索引的最大绝对差值在L和R之间
If in a range, there are no two same inputs then return 0
如果在一个范围内,没有两个相同的输入,则返回 0
INPUT FORMAT
输入格式
The first line contains N, no.
第一行包含 N,没有。 of elements in the array A The Second line contains N space separated integers that are elements of the array A The third line contains Q the number of queries Each of the Q lines contains L, R
数组 A 中的元素个数 第二行包含 N 个空格分隔的整数,它们是数组 A 的元素 第三行包含 Q 查询数 每 Q 行包含 L,R
CONSTRAINTS
约束
1 <= N, Q <= 10^4 1 <= Ai <= 10^4 1 <= L, R <= N
OUTPUT FORMAT
OUTPUT 格式
For each query, print the ans in a new line
对于每个查询,在新行中打印 ans
Sample Input
样本输入
5 1 1 2 1 2 5 2 3 3 4 2 4 3 5 1 5
Sample Output
样品 Output
0 0 2 2 3
Explanation
解释
[2,3] - No two elements are same [3,4] - No two elements are same [2,4] - there are two 1's so ans = |4-2| = 2 [3,5] - there are two 2's so ans = |5-3| = 2 [1,5] - there are three 1's and two 2's so ans = max(|4-2|, |5-3|, |4-1|, |2-1|) = 3
Here is my algorithm:这是我的算法:
- To take the input and test the range in a different method
以不同的方法获取输入并测试范围
- Input will be L, R and the Array
输入将是 L、R 和数组
- For difference between L and R equal to 1, check if the next element is equal, return 1 else return 0
L和R的差等于1,检查下一个元素是否相等,返回1否则返回0
- For difference more than 1, loop through array
对于大于 1 的差异,遍历数组
- Make a nested loop to check for the same element, if yes, store the difference into maxVal variable
做一个嵌套循环来检查相同的元素,如果是,将差异存储到 maxVal 变量中
- Return maxVal
返回最大值
My Code:我的代码:
def ansArray(L, R, arr):
maxVal = 0
if abs(R - L) == 1:
if arr[L-1] == arr[R-1]: return 1
else: return 0
else:
for i in range(L-1, R):
for j in range(i+1, R):
if arr[i] == arr[j]:
if (j-i) > maxVal: maxVal = j-i
return maxVal
if __name__ == '__main__':
input()
arr = (input().split())
for i in range(int(input())):
L, R = input().split()
print(ansArray(int(L), int(R), arr))
Please help me with this.请帮我解决一下这个。 I really want to learn a different and a more efficient way to solve this problem.
我真的很想学习一种不同的、更有效的方法来解决这个问题。 Need to pass all the TEST CASES.
需要通过所有的测试用例。 :)
:)
You can try this code:你可以试试这段代码:
import collections
def ansArray(L, R, arr):
dct = collections.defaultdict(list)
for index in range(L - 1, R):
dct[arr[index]].append(index)
return max(lst[-1] - lst[0] for lst in dct.values())
if __name__ == '__main__':
input()
arr = (input().split())
for i in range(int(input())):
L, R = input().split()
print(ansArray(int(L), int(R), arr))
Explanation:解释:
dct
is a dictionary that for every seen number keeps a list of indices. dct
是一个字典,它为每个看到的数字保留一个索引列表。 The list is sorted so lst[-1] - lst[0]
will give maximum absolute difference for this number.该列表已排序,因此
lst[-1] - lst[0]
将给出该数字的最大绝对差。 Applying max
to all this differences you get the answer.将
max
应用于所有这些差异,您将得到答案。 Code complexity is O(R - L).代码复杂度为 O(R - L)。
This can be solved as O(N) approximately the following way:这可以通过以下方式近似解决为 O(N):
from collections import defaultdict
def ansArray(L, R, arr) :
# collect the positions and save them into the dictionary
positions = defaultdict(list)
for i,j in enumerate(arr[L:R+1]) :
positions[j].append(i)
# create the list of the max differences in index
max_diff = list()
for vals in positions.values() :
max_diff.append( max(vals) - min(vals) )
# now return the max element from the list we have just created
if len(max_diff) :
return max(max_diff)
else :
return 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.