Solved Fibonacci Number in Java with Iterative DP

🚀 Day 95 of #100DaysOfCode 🧠 Solved "Fibonacci Number" in Java using a space-optimized iterative approach. 🔹 Problem: Given an integer n (0 ≤ n ≤ 30), compute the nth Fibonacci number where F(0)=0, F(1)=1 and F(n)=F(n-1)+F(n-2) for n>1. 🔹 Approach: • Handle base cases (n=0 or n=1).   • Use two variables (prev=0, curr=1) and iterate from i=2 to n.   • In each step compute next=prev+curr, then update prev=curr and curr=next.   • Return curr as the result. 🔹 Key Learning: • Iterative dynamic programming with constant space is ideal when only the last two states are needed.   • Avoid using full arrays when you don’t need all historical values.   • Clear, readable, and efficient code works well in interviews and production alike. 🔹 Thanks to: • Dr Bharathi Raja N — Director of Training & Placement, Dhanalakshmi College of Engineering   • Sreesairam V Sir — Mentor from Aptitude Guru Hem #DhanalakshmiCollegeOfEngineering #LeetCode   #Java #100DaysOfCode #Day95 #DynamicProgramming #CodingJourney

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories