Neetcode DP

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Min Cost Climbing Stairs Problem Summary:Given an array cost, where cost[i] is the cost to step on stair i, return the minimum cost to reach the top (beyond last stair), starting from step 0 or 1.

Main Idea (1 sentence):DP where dp[i] is the minimum cost to reach stair i. Steps: Initialize: dp[0] = cost[0], dp[1] = cost[1]. For i from 2 to n-1, set dp[i] = cost[i] + min(dp[i-1], dp[i-2]). Return min(dp[n-1], dp[n-2]). Runtime:O(n) time, O(n) space (can reduce to O(1)).

House Robber Problem Summary:Given an array nums of non-negative integers representing the amount of money in each house, return the maximum amount that can be robbed without robbing adjacent houses.

Main Idea (1 sentence):DP where dp[i] represents the max money robbed from the first i houses. Steps: Initialize: dp[0] = nums[0], dp[1] = max(nums[0], nums[1]). For i from 2 to n-1, set dp[i] = max(dp[i-1], dp[i-2] + nums[i]). Return dp[n-1]. Runtime:O(n) time, O(n) space (can reduce to O(1)).

House Robber II Problem Summary:Same as House Robber, but houses are arranged in a circle, so the first and last cannot both be robbed.

Main Idea (1 sentence):Run House Robber DP twice: once excluding the first house and once excluding the last. Steps: If n == 1, return nums[0]. Compute max for range [0, n-2] and [1, n-1] using House Robber logic. Return the max of those two results. Runtime:O(n) time, O(n) space.

Climbing Stairs Problem Summary:Given n steps, and the ability to climb either 1 or 2 steps at a time, return the total number of distinct ways to reach the top.

Main Idea (1 sentence):Use bottom-up DP where dp[i] represents the number of ways to reach step i. Steps: Initialize: dp[0] = 1, dp[1] = 1. For i from 2 to n, set dp[i] = dp[i-1] + dp[i-2]. Return dp[n]. Runtime:O(n) time, O(n) space (can be reduced to O(1)).

Longest Palindromic Substring Find the longest contiguous substring within a string that is a palindrome. Input: String of lowercase letters.

Main Idea: Use dynamic programming to expand around all possible substrings while tracking the longest valid palindrome. Steps: 1) Initialize a 2D DP table dp[i][j] where each cell holds the length of the palindrome s[i..j] (0 if not a palindrome). 2) Set all dp[i][i] = 1 since each character is a palindrome of length 1. 3) For all adjacent pairs where s[i] == s[i+1], set dp[i][i+1] = 2. 4) For substring lengths ≥ 3, check if s[i] == s[j] and dp[i+1][j-1] > 0. If so, set dp[i][j] = 2 + dp[i+1][j-1]. 5) Track and update the longest length and starting index as you fill the DP table. 6) Return the substring using s.substr(start, longest).

Palindromic Substrings Count how many substrings of a given string are palindromes. Input: String of lowercase letters.

Main Idea: Use dynamic programming to expand around centers or memoize substring checks to avoid recomputation. Steps: 1) Initialize a DP table `dp[i][j]` indicating whether the substring s[i..j] is a palindrome. 2) Base Case1: Every individual character is a palindrome → set `dp[i][i] = true`. 3) BaseCase2: For substrings of length 2, check if both characters are equal. 4) For length ≥ 3, set `dp[i][j] = true` if `s[i] == s[j]` and `dp[i+1][j-1]` is true. 5) Count all `(i, j)` where `dp[i][j] == true`.


Kaugnay na mga set ng pag-aaral

ATI Fundamentals Chapter 27: Vital Signs Questions

View Set

Chapter 5 Concept Checks & Book Review Questions

View Set

Chapter 2 Customer Service and Sales Fundamentals

View Set

Bone Tissue - ch 7 Smartbook Q&A

View Set