## maximum sum of k contiguous sub-matrices

The question that arises is this: given an array of size n: array () and a number "m", our goal now is to find AT MOST m contiguous sub-matrices so that the sum of all these under -matrices be the biggest. It is also required that none of these subnets can overlap.

Example: for an array (1, 2, 3, -2, 3, -10, 3), if m = 2, the sub-tables we take are (1, 2, 3, -2, 3) and ( 3) and their total sum is 10.
If m = 4, given the same array, then the subnets we end up taking are (1, 2, 3), (3) and (3) and their total sum is 12 (we did not no need for the 4th sub-array here).

Someone has a solution?

Thank you!

## Should a camera have a contiguous housing?

The group of lenses on this image would have its own protective tubes, like that of a camera, but one for each group of lenses. What problem would there be a gap between the lens group and the flare if it was used at night?

contiguous

Related: Can a camera go one kilometer?

## algorithms – Split a table into contiguous sub-tables of approximately the same sum

You are looking for an algorithm to divide an array of $$n$$ positive numbers in $$N$$ contiguous subcategories ($$N ) approximately the same:

$$min _ { text {splits}} , ( max_j S_j – min_j S_j),$$
or $$S_j$$ is the sum of the numbers in $$j$$-the subnet.

For example, better splitting of $$100, 1, 1, 103, 90$$ in three subnets is $$100,1,1 | 103 | 90$$.

Typically $$n ~ about 10 ^ 6-10 ^ 7$$, $$N approximately 100$$.

I suspect it would be a greedy approach …

## Algorithm – Python Implementation of Conway's Contiguous Arrow

A year ago, I implemented an algorithm to evaluate Conway's spotted log in Python. I would like my code to be commented, especially regarding readability and how pythonically it is. I would also like to know if there is a bug. I'm not too concerned about performance because this notation is faster in intractable memory problems than in long loops. For example, `solve (3-> 3-> 2-> 2 & # 39;)` would have to build a list with 7.6 billion channels if it did not raise a `MemoryError` manually before.

``````# coding: utf-8

MAX_BYTES = 1 << 30

def do_brackets_match (chain):

open_brackets = 0
for c in the chain:
if c == (& # 39 ;:
open_brackets + = 1
if c == & # 39;)
open_brackets - = 1
if open_brackets < 0:
return False
return open_brackets == 0

def split_by_inner_bracket(chain):

start, part3 = chain.partition(')')[::2]
start, middle = start.rpartition('(')[::2]
return [start, middle, part3]

def is_arrow_chain(chain):

return '->& # 39; chain

def resol_pure (string):

state "(not in the chain and & # 39;) & # 39;) & # 39; not in the chain

# delete spaces
chain = chain.replace (& # 39; & # 39;)
chain = chain.replace (& # 39;  t, & # 39;)

# get values
str_values ​​= chain.split (& # 39; -> & # 39;)

# manipulate short strings
length = len (values_str)
if length == 1:
returns str_values[0]
b = int (values_str[-2])
n = int (str_values[-1])
if length == 2:
if n.bit_length ()> 1024 or b.bit_length () * 8 * n> MAX_BYTES:
increase MemoryError
return str (b ** n)

if b> MAX_BYTES:
increase MemoryError

# removes everything after a 1
for i, s to enumerate (str_values):
if str_values[i] == & # 39; 1 & # 39 ;:
str_values ​​= str_values[:i]
Pause

# builds the next iteration step
Leading_chain = & # 39; ->. join (str_values[:-2])
resol_chain = & # 39; -> (& # 39 ;. join ([leading_chain] * b)
resol_chain + = (& # 39;) -> & # 39; + str (n-1)) * (b-1)
resol_chain = resol_chain.replace (& # 39; -> 1) & # 39;) & # 39;) & # 39;)
If resol_chain.endswith (& # 39; -> 1 & # 39;):
resol_chain = resol_chain[:-3]
returns resol_chain

def resol (chain, show_progress = false, depth = 0):

while is_arrow_chain (string):
if show_progress:
print ('depth' + string)
if & # 39; in the chain or & # 39;) & # 39; in the chain:
share = split_by_inner_bracket (chain)
if is_arrow_chain (part[1]):
part[1] = solve (part[1], show_progress, depth + 1)
chain = & # 39; .join (part)
other:
chain = resol_pure (chain)
return chain

def ():

print (solve (2-> 3-> 3 & # 39; True))
print (& # 39; ---------------- & # 39;)
print (solve (2-> 2-> 3-> 3 & # 39; True))
print (& # 39; ---------------- & # 39;)
print (solve (& # 39; 3-> 2-> 3 & # 39; True))

if __name__ == __ __ hand __:

main ()
``````

Exit:

``````2-> 3-> 3
2 -> (2 -> (2) -> 2) -> 2
2 -> (2-> 2-> 2) -> 2
2-> 2-> 2
2 -> (2)
2-> 2
2-> 4-> 2
2 -> (2 -> (2 -> (2)))
2 -> (2 -> (2-> 2))
2-> 2
2 -> (2-> 4)
2-> 4
2-> 16
65536
----------------
2-> 2-> 3-> 3
2-> 2 -> (2-> 2 -> (2-> 2) -> 2) -> 2
2-> 2
2-> 2 -> (2-> 2-> 4-> 2) -> 2
2-> 2-> 4-> 2
2-> 2 -> (2-> 2 -> (2-> 2 -> (2-> 2)))
2-> 2
2-> 2 -> (2-> 2 -> (2-> 2-> 4))
2-> 2-> 4
2 -> (2) -> 3
2-> 2-> 3
2 -> (2) -> 2
2-> 2-> 2
2 -> (2)
2-> 2
2-> 2 -> (2-> 2-> 4)
2-> 2-> 4
2 -> (2) -> 3
2-> 2-> 3
2 -> (2) -> 2
2-> 2-> 2
2 -> (2)
2-> 2
2-> 2-> 4
2 -> (2) -> 3
2-> 2-> 3
2 -> (2) -> 2
2-> 2-> 2
2 -> (2)
2-> 2
2-> 2-> 4-> 2
2-> 2 -> (2-> 2 -> (2-> 2 -> (2-> 2)))
2-> 2
2-> 2 -> (2-> 2 -> (2-> 2-> 4))
2-> 2-> 4
2 -> (2) -> 3
2-> 2-> 3
2 -> (2) -> 2
2-> 2-> 2
2 -> (2)
2-> 2
2-> 2 -> (2-> 2-> 4)
2-> 2-> 4
2 -> (2) -> 3
2-> 2-> 3
2 -> (2) -> 2
2-> 2-> 2
2 -> (2)
2-> 2
2-> 2-> 4
2 -> (2) -> 3
2-> 2-> 3
2 -> (2) -> 2
2-> 2-> 2
2 -> (2)
2-> 2
4
----------------
3-> 2-> 3
3 -> (3) -> 2
3-> 3-> 2
3 -> (3 -> (3))
3 -> (3-> 3)
3-> 3
3-> 27
7625597484987
``````

## data structures – Number of contiguous subsequences adding a given target

Here is a simple randomized $$O (n)$$ time algorithm. We begin by slightly restating your problem. Suppose the origin chart is $$a_1, ldots, a_n$$. Form a new table $$b_0, ldots, b_n$$ containing current amounts from the previous table:
$$b_0 = 0, qquad b_i = a_1 + cdots + a_i.$$
Note that $$a_i + cdots + a_j = b_j – b_ {i-1}$$. Therefore, we can reformulate the problem as follows:

Given a table $$b_0, ldots, b_n$$ and a target $$t$$, find the number of pairs $$i such as $$b_j = b_i + t$$.

For example, if $$A = 2.5,6, -1$$ then $$B = 0.2.7,13.12$$, and for $$t = 5$$ we have two solutions: $$7 – 2 = 12 – 7 = 5$$.

The idea is now very simple: we are digitizing $$B$$ from left to right, and for each $$b_j$$, we count the number of values $$b_j-t$$ that have occurred previously, by storing them using a hash table.

1. Initialize the number of solutions: $$N gets 0$$.
2. Initialize a hash table $$H$$ containing only one entry: $$B[b_0] gets 1$$.
3. For $$j = 1, ldots, n$$:
• Yes $$b_j – t in B$$, let $$N gets N + H[b_j – t]$$.
• Yes $$b_j notin H$$, let $$H[b_j] gets 1$$and leave otherwise $$H[b_j] gets H[b_j] + 1$$.
4. Return $$N$$.

The same algorithm can be implemented in deterministic $$O (n log n)$$ time using a self-balancing binary search tree.

## Number of sums of contiguous subsequences totaling a given number

I could not find an effective way (better than O (n * n)) to count all contiguous subsequences of the sum of an array of both positive and negative integers totaling a number k given.

E, g if A = {2,5,6, -1} and k = 5 then years = 2.

## javascript – Constructs a 2D array of contiguous elements comparing master and incident arrays

Requirement:

Based on `mainArray` which contains the master list of articles, and `compArray` who has objects of interest; construct a third 2-dimensional array containing the array of contiguous elements.

The purpose of this requirement is to draw the polyline on the map whenever an incident occurs contiguously, traced with the help of the `final table` places.

Example:

``````const mainArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
const compArray = [1, 2, 3, 6, 7, 11, 12, 13, 15];

// finalArray product
[[ 1, 2, 3 ],[ 6, 7 ],[ 11, 12, 13 ],[ 15 ]]
``````

My current solution:

``````console.clear ();

const mainArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
const compArray = [1, 2, 3, 6, 7, 11, 12, 13, 15];
let compArrayIndex = 0;
leave finalArray = [];
let currentArray = null;

for (let i = 0; i <mainArray.length; i ++) {
const e = mainArray[i];

if (e === compArray[compArrayIndex]) {
if (! currentArray) {
currentArray = [];
}
currentArray.push (e);
compArrayIndex ++;
} other {
if (currentArray) {
finalArray.push ([...currentArray])
currentArray = null;
}
}
}

if (currentArray) {
finalArray.push ([...currentArray])
currentArray = null;
}

console.log (mainArray);
console.log (compArray);
console.log (& # 39; finl arr: & # 39;);
for (let i = 0; i <finalArray.length; i ++) {
const e = finalArray[i];
console.log (e);
}
``````