Milan Sahoo’s Post

🚀 LeetCode Day Problem Solving 🚀 Day-89 📌 Problem: Given a binary string s, determine whether you can reach the last index starting from index 0. 👉 You can jump from index i to any index j such that: ✔ i + minJump <= j <= i + maxJump ✔ s[j] == '0' 🧠 Example 1: Input: s = "011010" minJump = 2 maxJump = 3 ✅ Output: true 🔄 Possible Path: 0 → 3 → 5 ✔ Both positions contain '0' 🧠 Example 2: Input: s = "01101110" minJump = 2 maxJump = 3 ❌ Output: false ✔ No valid sequence of jumps can reach the last index. 💡 Key Insight (Very Important 🔥): 👉 From each index, checking every possible jump directly would be too slow ❌ Because: ⚠ Worst case becomes O(n²) ⚡ Optimization Idea: ✔ Use BFS + Sliding Window Instead of repeatedly checking the same range: ✅ Maintain how far we have already processed. This avoids duplicate work. ⚡ Core Idea: For every reachable index i: ✔ Explore indices in range: [i + minJump , i + maxJump] ✔ Only visit positions containing '0' ✔ Avoid revisiting indices 🔥 Why Sliding Window Helps? Without optimization: 👉 Same indices get scanned many times. With sliding window: ✅ Every index is processed only once. So complexity becomes linear. ⚡ Algorithm: 1️⃣ Start BFS from index 0 2️⃣ For every current index: ✔ Compute jump range 3️⃣ Traverse only unvisited positions 4️⃣ If last index is reached → return true 5️⃣ If BFS ends → return false 📊 Complexity Analysis: ⏱ Time Complexity: O(n) 📦 Space Complexity: O(n) 🧠 What I Learned: ✔ BFS on arrays ✔ Sliding window optimization ✔ Efficient range traversal ✔ Avoiding repeated computations ✅ Day 89 Completed 🚀 Improving in Graphs + BFS + Sliding Window 💪 #Leetcode #coding #learning #CodingChallenge #ProblemSolving #DSA #BFS #SlidingWindow #Graphs #Optimization #MilanSahoo 🚀

  • graphical user interface, text, application

To view or add a comment, sign in

Explore content categories