# Tag Info

1

The algorithm is slow since it creates paths in all 4 directions at each step. The algorithm is also using recursion, which is also slower than a simple for loop. Consider a 5x5 matrix A: [[1 1 1 1 0] [1 1 1 1 1] [1 1 1 1 1] [1 0 1 1 1] [1 1 1 1 1]] to find the distance of the top-left cell, the algorithm first moves down, then up, then right, and then ...

2

private Object ensureType(Class<?> expectedClass, Object value) { Your names could be slightly better, given that this function does not only ensure something, but also converts it. private Object convert(Class<?> targetClass, Object value) { throw new ClassCastException( "argument " + value + " of type " + ...

3

This will be pretty similar to superb rain's great answer. Write functions Writing functions will help you to write easier to maintain code. Also, for the algorithmic challenges, it will help you to focus on the algorithm itself instead of dealing with input/output. Write tests Once you have a function, it is easier to write tests. (You could also write the ...

5

An obvious improvement is to not split the strings and convert the parts to int over and over again. Do it once, at the start: def main(points): points = [tuple(map(int, point.split())) for point in points] largest = 0 for cx, cy in points: for lx, ly in points: for wx, wy in points: if lx == cx and wy == cy: area = abs(ly-...

7

Mathematical approach Let us assume the following notation: $$\mathbb{B} = \sum_{i = 1}^{n - 1} b_i = b_1 + b_2 + \cdots + b_{n-1}$$ As mentioned earlier in comments, you have set of interdependent equations as follows:  \begin{align} b_1 &= a_1 + a_2 \\ b_2 &= a_2 + a_3 \\ b_3 &= a_3 + a_4 \\ \vdots \\ b_{n - 1} &= a_{n - 1} + a_n \\ \...

4

No need to compute permutations As you are provided all the b1,b2,…,bN−1 values, the problem boils down to finding the first value a1 and everything can be computed from there. In particular, you have only N different values to try. We can try see them like this: n = 5 b_array = [4, 6, 7, 6] for a in range(1, n+1): a_array = [a] for b in b_array: ...

Top 50 recent answers are included