Optimize LIS with Counting Approach

🔥 Day 82 of #100DaysOfLeetCode ✅ Problem: Number of Longest Increasing Subsequence ✅ Difficulty: Medium 💡 Key Insight: Instead of tracking only the LIS length, we also maintain the number of ways each LIS can be formed. For every element, we check all previous smaller elements and update both length and count dynamically. 🛠 Approach: Use two arrays: dp[i] → Length of LIS ending at index i cnt[i] → Number of LIS ending at index i If a longer subsequence is found → update length and copy count. If another subsequence of the same length is found → add the counts. Finally, sum counts for indices having the maximum LIS length. ⏱ Complexity: Time: O(n²) Space: O(n) #LeetCode #Java #ProblemSolving #CodingChallenge #100DaysOfCode #DSA #LearningEveryday

  • text

To view or add a comment, sign in

Explore content categories