DMITRII’s Substack

Share this post

Leetcode daily # 8.05.2023

dmitriisamoilenko.substack.com

Discover more from DMITRII’s Substack

I post a problem that I solve every single day
Continue reading
Sign in

Leetcode daily # 8.05.2023

[1572. Matrix Diagonal Sum]

DMITRII SAMOILENKO
May 8, 2023
Share this post

Leetcode daily # 8.05.2023

dmitriisamoilenko.substack.com
Share

8.05.2023

1572. Matrix Diagonal Sum easy

fun diagonalSum(mat: Array<IntArray>): Int =
    (0..mat.lastIndex).sumBy {
        mat[it][it] + mat[it][mat.lastIndex - it]
    }!! - if (mat.size % 2 == 0) 0 else mat[mat.size / 2][mat.size / 2]

blog post

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/206

Intuition

Just do what is asked.

Approach

  • avoid double counting of the center element

Complexity

  • Time complexity:
    O(n)

  • Space complexity:
    O(1)

Thanks for reading DMITRII’s Substack! Subscribe for free to receive new posts and support my work.

Share this post

Leetcode daily # 8.05.2023

dmitriisamoilenko.substack.com
Share
Previous
Next
Comments
Top
New

No posts

Ready for more?

© 2023 DMITRII SAMOILENKO
Privacy ∙ Terms ∙ Collection notice
Start WritingGet the app
Substack is the home for great writing