简体   繁体   中英

Fast way to check consecutive subsequences for total

I have a list (up to 10,000 long) of numbers 0, 1, or 2. I need to see how many consecutive subsequences have a total which is NOT 1. My current method is to for each list do:

cons = 0
for i in range(seqlen+1):
    for j in range(i+1, seqlen+1):
        if sum(twos[i:j]) != 1:
            cons += 1

So an example input would be:

[0, 1, 2, 0]

and the output would be

cons = 8

as the 8 working subsequences are:

[0] [2] [0] [1,2] [2, 0] [0, 1, 2] [1, 2, 0] [0, 1, 2, 0]

The issue is that simply going through all these subsequences (the i in range, j in range) takes almost more time than is allowed, and when the if statement is added, the code takes far too long to run on the server. (To be clear, this is only a small part of a larger problem, I'm not just asking for the solution to an entire problem). Anyway, is there any other way to check faster? I can't think of anything that wouldn't result in more operations needing to happen every time.

I think I see the problem: your terminology is incorrect / redundant. By definition, a sub-sequence is a series of consecutive elements.

Do not sum every candidate. Instead, identify every candidate whose sum is 1 , and then subtract that total from the computed quantity of all sub-sequences (simple algebra).

All of the 1-sum candidates are of the regular expression form 0*10* : a 1 surrounded by any quantity of 0 s on either or both sides.

Identify all such maximal-length strings. FOr instance, in

210002020001002011

you will pick out 1000 , 000100 , 01 , and 1 . For each string compute the quantity of substrings that contain the 1 (a simple equation on the lengths of the 0 s on each side). Add up those quantities. Subtract from the total for the entire input. There's you answer.

Use sliding window technique to solve these type of problem. Take two variable to track first and last to track the scope of window. So you start with sum equal to first element. If the sum is larger than required value you subtract the 'first' element from sum and increment sum by 1. If the sum is smaller than required you add next element of 'last' pointer and increment last by 1. Every time sum is equal to required increment some counter.

As for NOT, count number of sub-sequence having '1' sum and then subtract from total number of sub-sequence possible, ie n * (n + 1) / 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM