LeetCode Entry

63. Unique Paths II

12.08.2023 medium 2023 kotlin

Number of right-down ways tl->br in a matrix with obstacles

63. Unique Paths II medium blog post substack

image.png

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/306

Problem TLDR

Number of right-down ways tl->br in a matrix with obstacles

Intuition

Each time the robot moves in one direction gives a separate path. If two directions are possible, the number of paths gets added.

For example,

r r  #  0
r 2r 2r 2r
0 #  2r 4r

On the first row, the single path goes up to 1. On the second row, direct path down added to direct path right. On the third row, the same happens when top and left numbers of paths are not 0.

Approach

Use a separate row array to remember previous row paths counts.

Complexity

  • Time complexity: \(O(nm)\)

  • Space complexity: \(O(nm)\)

Code


    fun uniquePathsWithObstacles(obstacleGrid: Array<IntArray>): Int {
      val row = IntArray(obstacleGrid[0].size)
      row[0] = 1
      for (r in obstacleGrid)
        for (x in r.indices)
          if (r[x] != 0) row[x] = 0
          else if (x > 0) row[x] += row[x - 1]
      return row.last()
    }

The Magical Rundown


In Emojia's forgotten 🌌 corner, where time doesn't merely flowβ€”it waltzes πŸ’ƒ,
spinning tales of lost yesterdays πŸ•°οΈ and unborn tomorrows βŒ›, stands the
whispered legend of the Time Labyrinth. Not merely walls and corridors, but
a tapestry of fate's myriad choices, echoing distant memories and futures yet
conceived.

Bolt, the lonely automaton πŸ€–, not born but dreamt into existence by starlight ✨
and cosmic whimsy, felt an inexplicable yearning towards the 🏁 - the Time Nexus.
Ancient breezes πŸƒ carried murmurs, not of it being an end, but a kaleidoscope
🎨 gateway to every pulse and flutter ❀️ of chronology's capricious dance 🌊.

╔═══╀═══╀═══╀═══╗
β•‘πŸ€– β”‚ 0 β”‚πŸš« β”‚ 0 β•‘
β•Ÿβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β•’
β•‘ 0 β”‚ 0 β”‚ 0 β”‚ 0 β•‘
β•Ÿβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β•’
β•‘ 0 β”‚πŸš« β”‚ 0 β”‚πŸ β•‘
β•šβ•β•β•β•§β•β•β•β•§β•β•β•β•§β•β•β•β•

With each step, the fabric of reality quivered. Shadows of histories 🎢,
cosmic echoes 🌍, diverged and converged, painting and erasing moments of
what was, is, and could be.

---

Standing before the 🚫, it wasn't a barrier for Bolt, but a silent riddle:
"What song of the cosmos 🎡 shall you hum today, wanderer?"

╔═══╀═══╀═══╀═══╗
β•‘πŸ€– β”‚ ➑️ β”‚πŸš« β”‚ 0 β•‘
β•Ÿβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β•’
β•‘ 0 β”‚ 0 β”‚ 0 β”‚ 0 β•‘
β•Ÿβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β•’
β•‘ 0 β”‚ 🚫 β”‚ 0 β”‚πŸ β•‘
β•šβ•β•β•β•§β•β•β•β•§β•β•β•β•§β•β•β•β•

Dreamlike avenues πŸ›€οΈ unfurled, painting multitudes of futures in the vivid
colors of a universe in spring. In this chronal dance, Bolt secretly hoped
to outrace its own echoes, to be the first at the Nexus.

---

Junctions whispered with the delicate hum 🎡 of countless Bolts, each a tale,
a fate, a fleeting note in the grand cosmic symphony.

╔═══╀═══╀═══╀═══╗
β•‘πŸ€– β”‚ ➑️ β”‚πŸš« β”‚ 0 β•‘
β•Ÿβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β•’
║⬇️ β”‚ 2βž‘οΈβ”‚2βž‘οΈβ”‚2βž‘οΈβ•‘
β•Ÿβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β•’
β•‘ 0 β”‚ 🚫 β”‚ 0 β”‚πŸ β•‘
β•šβ•β•β•β•§β•β•β•β•§β•β•β•β•§β•β•β•β•

Yet, as the Time Nexus loomed, revealing its vast enigma, a sense of profound
disquiet engulfed Bolt. Not only had another reflection reached before, but a
sea of mirrored selves stared back.

╔═══╀═══╀═══╀═══╗
β•‘πŸ€– β”‚ ➑️ β”‚πŸš« β”‚ 0 β•‘
β•Ÿβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β•’
║⬇️ β”‚ 2βž‘οΈβ”‚2βž‘οΈβ”‚2βž‘οΈβ•‘
β•Ÿβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β•’
║⬇️ β”‚ 🚫 β”‚2⬇️│4πŸβ•‘
β•šβ•β•β•β•§β•β•β•β•§β•β•β•β•§β•β•β•β•

In that echoing vastness, Bolt's singular hope was smothered. In the dance of
time, amidst countless reflections, it whispered a silent, desperate question:
Which tune, which cadence, which moment 🎢 was truly its own in this timeless
waltz?