Boring_Day's blog

By Boring_Day, history, 19 months ago, In English

Lets say you have a matrix. Write a function which takes cell points and a number k. (It returns sum from rows).

for ex: func(i, j, k) here i and j are cell position. And k is a number. // I want sum from continuous rows.

I want sum from mat[i][j-k] to mat[i][j+k] + sum from mat[i-1][j-(k-1)] to mat[i-1][j+(k-1)] + sum from mat[i-2][j-(k-2)] to mat[i-2][j+(k-2)] and so on. If you go out of border add zero (do nothing).

Help me please Tell me if i clearly explai the problem or not.

  • Vote: I like it
  • +6
  • Vote: I do not like it

»
19 months ago, # |
Rev. 2   Vote: I like it +1 Vote: I do not like it

It is important to mention that k is constant every time we call the function but not the i and j. So we need to do the pre computation. Help sir, tfg

»
19 months ago, # |
  Vote: I like it -11 Vote: I do not like it

You want to take the sum of this triangle: https://i.328888.xyz/2023/04/14/ixDSDx.png

We could actually just take the left half of the triangle, and then for the right half, we could just flip the whole array left and right and apply the same algorithm.

https://i.328888.xyz/2023/04/14/ixU0VN.png

So you just have to figure out the sum of all the x's and y's, the orange part, and then you can figure out the sum of the triangles.

  • »
    »
    19 months ago, # ^ |
      Vote: I like it +9 Vote: I do not like it

    What is the orange part? How to figure out x's and y's. And how is it actually giving us the sum of one part of triangle. Hard to understand 😖

    • »
      »
      »
      19 months ago, # ^ |
      Rev. 3   Vote: I like it -11 Vote: I do not like it

      You can refer to this code. triangle_sum::Get(int,int,int) will give you the sum of all the numbers in a triangle of length k with a right Angle vertex of (x,y).

      Then you just flip the input array and do it again to complete your problem. Everything is linear. There's a lot of trivial, annoying but necessary math and a lot of boundary conditions. I'm not sure I've done it correctly at all.

      Code

      pm me if you have more questions. Don't worry about disturbing me, I'm loiterers.