🚀 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
Solved Fibonacci Number in Java with Iterative DP
More Relevant Posts
-
🚀 Day 73 of #100DaysOfCode Today I solved LeetCode 3346 – Maximum Frequency of an Element After Performing Operations I 🧩 This problem was all about logical range manipulation — figuring out how many elements could be converted into the same number when each element can be modified by ±k, but with a limited number of allowed operations. At first, it felt similar to the “most frequent element” sliding window problem, but the twist here was controlling how many elements can be changed, not just the total cost. After a few failed attempts (and a battle with edge cases 😅), I finally implemented a correct solution using the difference array + prefix sum approach. 💡 Key Takeaways: Think in terms of ranges, not just differences. Prefix-sum (difference map) logic is incredibly powerful for interval counting. Always recheck constraints — “number of operations” vs “total cost” makes a huge difference! Here’s a snippet from my final Java solution: int achievable = Math.min(running, same + numOperations); ans = Math.max(ans, achievable); It’s small details like these that make problem-solving such a rewarding process 💪 #Java #LeetCode #100DaysOfCode #CodingJourney #ProblemSolving #DSA #LearningEveryday
To view or add a comment, sign in
-
-
🧩 Day 79 of #100DaysOfCode Today’s problem: LeetCode 3370 – Smallest Number With All Set Bits 💡 The challenge was to find the smallest number greater than or equal to n whose binary representation contains only set bits (1s). 💻 Language: Java ⚙️ Approach: Used bitwise operations to identify numbers whose binary form contains all 1s. The key condition used: (x & (x + 1)) == 0 This ensures that the number has all bits set. ✅ Result: Runtime – 0 ms, beating 100% of Java submissions! ⚡ 📈 Learning: This problem improved my understanding of bit manipulation and binary operations — core concepts for optimizing code performance. #Day79 #LeetCode #100DaysOfCode #CodingChallenge #JavaProgramming #ProblemSolving #BitManipulation #DeveloperJourney #CodeEveryday
To view or add a comment, sign in
-
-
23/30 days✅ Solved LeetCode Problem : #70 – Climbing Stairs The challenge was to determine how many distinct ways one can reach the top of a staircase with n steps, where at each step you can either climb 1 or 2 steps. The logic behind the problem is similar to the Fibonacci sequence, where each state depends on the sum of the previous two states. I implemented the solution in Java, using an iterative approach with three variables to optimize space complexity. Instead of using recursion or an array, the approach updates values in constant space (O(1)) while maintaining linear time complexity (O(n)). Here’s the logic in brief: Initialize a = 0, b = 1, and c = 1. For each step, compute c = a + b, then shift values forward (a = b, b = c). Return c as the total number of distinct ways to reach the top. #LeetCode #Java #DynamicProgramming #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
📌 Day 18/100 - Valid Palindrome (LeetCode 125) 🔹 Problem: Determine whether a string reads the same forward and backward, ignoring case and non-alphanumeric characters. 🔹 Approach: Implemented a two-pointer technique. Skipped all non-alphanumeric characters. Compared characters from both ends in lowercase. Returned true if all matched, otherwise false. 🔹 Key Learning: Two-pointer method keeps logic clean and efficient. Character handling is key when data isn’t uniform. Time complexity: O(n), Space complexity: O(1). Sometimes, solving elegantly is better than solving fast. ✨ #100DaysOfCode #LeetCode #Java #ProblemSolving #DSA #CodingJourney
To view or add a comment, sign in
-
-
💡 LeetCode 3467 – Transform Array 💡 Today, I solved LeetCode Problem #3467: Transform Array, which focuses on array manipulation and the use of conditional logic in Java — a neat problem that strengthens core programming fundamentals. ⚙️ 🧩 Problem Overview: You’re given an integer array nums. Your task is to: Replace even numbers with 0 Replace odd numbers with 1 Then, sort the transformed array in ascending order. 👉 Example: Input → nums = [4, 7, 2, 9] Output → [0, 0, 1, 1] 💡 Approach: 1️⃣ Iterate through the array. 2️⃣ Use a ternary operator to transform each element (even → 0, odd → 1). 3️⃣ Sort the array to arrange all zeros before ones. ⚙️ Complexity Analysis: ✅ Time Complexity: O(n log n) — due to sorting. ✅ Space Complexity: O(1) — in-place transformation. ✨ Key Takeaways: Practiced logical thinking and ternary operations in Java. Strengthened understanding of array transformations and sorting. Reinforced the value of writing clean, concise, and efficient code. 🌱 Reflection: Even simple transformation problems like this one sharpen the habit of thinking algorithmically. Consistency in small challenges leads to big growth in problem-solving skills. 🚀 #LeetCode #3467 #Java #ArrayManipulation #LogicBuilding #ProblemSolving #CodingJourney #DSA #CleanCode #ConsistencyIsKey
To view or add a comment, sign in
-
-
🚀 Day 94 of #100DaysOfCode 🧠 Solved "Reverse String II" in Java using a clear two-pointer approach with modular design. 🔹 Problem: • Given a string s and an integer k, reverse the first k characters for every block of 2k characters from the start. • If fewer than k characters remain, reverse all of them. • If between k and 2k remain, reverse only the first k. 🔹 Approach: • Convert the string into a char[] for in-place editing. • Iterate in steps of 2k. • For each block, find left and right boundaries (right = min(left + k - 1, n - 1)). • Reverse the section using a helper method with two pointers. • Return the new string after all blocks are processed. 🔹 Key Learning: • Renaming pointers to left and right improves readability for two-pointer logic. • Modularizing reversal logic into a helper method makes the code cleaner and reusable. • Handling partial segments (less than k left) becomes easy with boundary checks. 🔹 Thanks to: • Dr Bharathi Raja N — Director of Training & Placement, Dhanalakshmi College of Engineering • Sreesairam V Sir — Aptitude Guru Hem #DhanalakshmiCollegeOfEngineering #LeetCode #Java #100DaysOfCode #Day94 #TwoPointers #StringManipulation #CodingJourney
To view or add a comment, sign in
-
-
🌟 Day 94 of 100 Days of Code 🌟 Today’s challenge: Maximum Number of Operations to Move Ones to the End (LeetCode 3228) 💡 🧠 Problem: Given a binary string, you can repeatedly move '1' to the right (until it meets another '1' or the end). The goal is to find the maximum number of such operations possible. ⚙️ Approach: Traverse the string once. Track the count of '1's and increment operations based on '10' patterns. Efficient O(n) solution using simple logic and character comparisons. ✅ Result: ✔️ All test cases passed ⚡ Runtime: 7 ms — Beats 78.52% of Java submissions #100DaysOfCode #Day94 #LeetCode #Java #ProblemSolving #CodingJourney #DataStructures #Algorithms #CodeEveryday
To view or add a comment, sign in
-
-
📌 Day 13/100 - Valid Anagram (LeetCode 242) 🔹 Problem: Given two strings s and t, determine whether t is an anagram of s. An anagram is formed by rearranging the letters of one word to create another word — meaning both strings must have the same characters with the same frequency. 🔹 Approach: First, check if both strings are of equal length — if not, they can’t be anagrams. Convert both strings into character arrays. Sort both arrays and compare them using Arrays.equals(). If both sorted arrays are identical, the strings are anagrams. 🔹 Key Learning: Sorting can simplify comparison-based string problems. Always check base conditions (like length) to avoid unnecessary computation. This approach has a time complexity of O(n log n) due to sorting, which is efficient enough for this problem. Every solved challenge is another letter added to the dictionary of progress! 🚀 #100DaysOfCode #LeetCode #Java #ProblemSolving #DSA #CodingJourney #Consistency
To view or add a comment, sign in
-
-
🚀 Day 31: LeetCode 167 — Two Sum II (Two Pointer Approach) 🔹 Concept Covered: Two Pointer Technique on a Sorted Array 🔹 Problem Goal: Find two numbers such that they add up to a specific target. 🔹 Approach: Use two pointers (low at start, high at end). Calculate the sum of both pointers. If sum == target → return indices. If sum > target → move high-- (reduce sum). If sum < target → move low++ (increase sum). 💡 Time Complexity: O(n) 💡 Space Complexity: O(1) 🎯 Learning: The two-pointer approach is extremely efficient for sorted arrays — no need for nested loops or hash maps. It’s all about moving intelligently in one pass! #LeetCode #Day31 #TwoPointer #Java #CodingJourney #YeswanthCodes
To view or add a comment, sign in
-
-
💡 LeetCode #15 — 3Sum Today I solved LeetCode Problem 15: 3Sum 🔍 Problem Summary: Given an integer array nums, return all unique triplets (a, b, c) such that: a + b + c = 0 The solution must not contain duplicate triplets. Key Idea: Use sorting + two pointers to reduce the time from O(n³) to O(n²). Steps: Sort the array. Fix one number (nums[f]) in each iteration. Use two pointers (i and j) to find pairs whose sum equals -nums[f]. Skip duplicates for both the fixed index and the pointer values. This efficiently finds all unique triplets. #LeetCode #Java #DSA #TwoPointers #ProblemSolving #CodingChallenge #Algorithms
To view or add a comment, sign in
-
Explore related topics
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development