Блог пользователя Boring_Day

Автор Boring_Day, история, 19 месяцев назад, По-английски

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.

  • Проголосовать: нравится
  • +6
  • Проголосовать: не нравится

»
19 месяцев назад, # |
Rev. 2   Проголосовать: нравится +1 Проголосовать: не нравится

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 месяцев назад, # |
  Проголосовать: нравится -11 Проголосовать: не нравится

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 месяцев назад, # ^ |
      Проголосовать: нравится +9 Проголосовать: не нравится

    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 месяцев назад, # ^ |
      Rev. 3   Проголосовать: нравится -11 Проголосовать: не нравится

      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.