Skip to main content

Poridhi: Dynamic Programming

Dynamic Programming related problems:

This blog post presents Python solutions to six classic dynamic programming problems that frequently appear in coding interviews and competitive programming. These problems include strategies for maximizing profit without choosing adjacent houses in House Robber, decoding numeric strings into alphabetic representations in Decode Ways, and counting unique paths in a grid in Unique Paths. It also covers determining reachability in an array using jumps in Jump Game, finding the longest increasing subsequence in a list of numbers in Longest Increasing Subsequence, and checking if a string can be segmented into dictionary words in Word Break. Each solution is designed using efficient DP techniques such as tabulation and memorization, helping readers understand the core ideas behind dynamic problem-solving in Python.

✅ House Robber

def rob(nums): if not nums: return 0 if len(nums) <= 2: return max(nums) dp = [0] * len(nums) dp[0], dp[1] = nums[0], max(nums[0], nums[1]) for i in range(2, len(nums)): dp[i] = max(dp[i-1], dp[i-2] + nums[i]) return dp[-1]

✅ Decode Ways

def numDecodings(s): if not s or s[0] == '0': return 0 dp = [0] * (len(s) + 1) dp[0], dp[1] = 1, 1 for i in range(2, len(s) + 1): one = int(s[i-1:i]) two = int(s[i-2:i]) if 1 <= one <= 9: dp[i] += dp[i-1] if 10 <= two <= 26: dp[i] += dp[i-2] return dp[len(s)]

✅ Unique Paths

def uniquePaths(m, n): dp = [[1]*n for _ in range(m)] for i in range(1, m): for j in range(1, n): dp[i][j] = dp[i-1][j] + dp[i][j-1] return dp[-1][-1]

✅ Jump Game

def canJump(nums): goal = len(nums) - 1 for i in range(len(nums)-2, -1, -1): if i + nums[i] >= goal: goal = i return goal == 0

✅ Longest Increasing Subsequence

def lengthOfLIS(nums): dp = [1] * len(nums) for i in range(len(nums)): for j in range(i): if nums[i] > nums[j]: dp[i] = max(dp[i], dp[j]+1) return max(dp)

✅ Word Break

def wordBreak(s, wordDict): wordSet = set(wordDict) dp = [False] * (len(s) + 1) dp[0] = True for i in range(1, len(s)+1): for j in range(i): if dp[j] and s[j:i] in wordSet: dp[i] = True break return dp[len(s)]

Comments

Popular posts from this blog

Poridhi: Stacks & Queues

  Stacks & Queues related problems: This collection of solutions tackles three classic problems often encountered in technical interviews and competitive programming. The Valid Parentheses problem checks whether a string has properly matched and ordered brackets using a stack. The Sliding Window Maximum efficiently finds the maximum value in every window of size k across an array using a deque, a popular sliding window pattern that ensures optimal performance. The Stock Span Problem simulates a real-world stock analysis scenario and calculates the number of consecutive days before today for which the stock price was less than or equal to today's, also utilizing a stack for efficient computation. These problems test understanding of stacks, queues, and sliding window techniques. ✅ Valid Parentheses def isValid ( s: str ) -> bool : stack = [] mapping = { ')' : '(' , '}' : '{' , ']' : '[' } for char in s: ...

Data Recovery: DevOps pre-state learning

  Data Backup Data backup is the practice of copying data from a primary to a secondary location, to protect it in case of equipment failure, cyberattack, natural disaster or other data loss events. This can include documents, media files, configuration files, machine images, operating systems, and registry files. Essentially, any data that we want to preserve can be stored as backup data. Data Restore Data restore is the process of copying backup data from secondary storage and restoring it to its original location or a new location. A restore is performed to return data that has been lost, stolen or damaged to its original condition or to move data to a new location. Pilot Light A pilot light approach minimizes the ongoing cost of disaster recovery by minimizing the active resources, and simplifies recovery at the time of a disaster because the core infrastructure requirements are all in place. Warm Standby The warm standby approach involves ensuring that there is a scaled down, ...

Poridhi: Basic Programming & Math

Basic Programming and math related problems: Extracting digits means breaking a number like 1234 into [1, 2, 3, 4], often using string conversion or modulo/division. Counting digits involves finding how many digits are in a number using length of a string or repeated division. Reversing an integer flips its digits (e.g., 123 becomes 321), with care for signs and 32-bit limits. A palindrome number reads the same forward and backward, often checked by comparing the original and reversed number. Armstrong numbers are those equal to the sum of their digits each raised to the number of digits, like 153 = 1³ + 5³ + 3³. To find the sum of all divisors of a number, loop through integers and check which divide the number without a remainder. Prime numbers are only divisible by 1 and themselves, and checking up to the square root is efficient. GCD, the greatest common divisor of two numbers, can be found using a simple loop or more efficiently with the Euclidean algorithm, which uses repeated mo...