DMITRII’s Substack

Share this post

Leetcode daily # 2.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 # 2.05.2023

[1822. Sign of the Product of an Array]

DMITRII SAMOILENKO
May 2, 2023
Share this post

Leetcode daily # 2.05.2023

dmitriisamoilenko.substack.com
Share

2.05.2023

1822. Sign of the Product of an Array easy

fun arraySign(nums: IntArray): Int = nums.fold(1) { r, t -> if (t == 0) 0 else r * (t / Math.abs(t)) }

blog post

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

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/199

Intuition

Do what is asked, but avoid overflow.

Approach

There is an sign function in kotlin, but leetcode.com doesn’t support it yet.
We can use fold.

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 # 2.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