Solving Leetcode Interviews in Seconds with AI: Selling Pieces of Wood
Introduction
In this blog post, we will explore how to solve the LeetCode problem "2312" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.
Problem Statement
You are given two integers m and n that represent the height and width of a rectangular piece of wood. You are also given a 2D integer array prices, where prices[i] = [hi, wi, pricei] indicates you can sell a rectangular piece of wood of height hi and width wi for pricei dollars. To cut a piece of wood, you must make a vertical or horizontal cut across the entire height or width of the piece to split it into two smaller pieces. After cutting a piece of wood into some number of smaller pieces, you can sell pieces according to prices. You may sell multiple pieces of the same shape, and you do not have to sell all the shapes. The grain of the wood makes a difference, so you cannot rotate a piece to swap its height and width. Return the maximum money you can earn after cutting an m x n piece of wood. Note that you can cut the piece of wood as many times as you want. Example 1: Input: m = 3, n = 5, prices = [[1,4,2],[2,2,7],[2,1,3]] Output: 19 Explanation: The diagram above shows a possible scenario. It consists of: - 2 pieces of wood shaped 2 x 2, selling for a price of 2 7 = 14. - 1 piece of wood shaped 2 x 1, selling for a price of 1 3 = 3. - 1 piece of wood shaped 1 x 4, selling for a price of 1 2 = 2. This obtains a total of 14 + 3 + 2 = 19 money earned. It can be shown that 19 is the maximum amount of money that can be earned. Example 2: Input: m = 4, n = 6, prices = [[3,2,10],[1,4,2],[4,1,3]] Output: 32 Explanation: The diagram above shows a possible scenario. It consists of: - 3 pieces of wood shaped 3 x 2, selling for a price of 3 10 = 30. - 1 piece of wood shaped 1 x 4, selling for a price of 1 2 = 2. This obtains a total of 30 + 2 = 32 money earned. It can be shown that 32 is the maximum amount of money that can be earned. Notice that we cannot rotate the 1 x 4 piece of wood to obtain a 4 x 1 piece of wood. Constraints: 1 <= m, n <= 200 1 <= prices.length <= 2 104 prices[i].length == 3 1 <= hi <= m 1 <= wi <= n 1 <= pricei <= 106 All the shapes of wood (hi, wi) are pairwise distinct.
Explanation
Here's the solution to the wood cutting problem:
- Dynamic Programming: Use dynamic programming to store the maximum profit for each possible size of wood (height x width). The state
dp[h][w]represents the maximum profit achievable from a piece of wood with heighthand widthw. - Iteration and Cuts: Iterate through all possible heights and widths from 1 to
mand 1 ton, respectively. For eachdp[h][w], consider all possible horizontal and vertical cuts. Maximization: For each cut, calculate the profit from the two resulting pieces and update
dp[h][w]with the maximum profit found so far, including the profit from selling the current piece without any cuts if there is a matching price.Runtime Complexity: O(m*n*(m+n)) where m and n are the height and width of the wood, respectively.
- Storage Complexity: O(m*n)
Code
def wood_cutting(m: int, n: int, prices: list[list[int]]) -> int:
"""
Calculates the maximum money you can earn after cutting an m x n piece of wood.
Args:
m: The height of the rectangular piece of wood.
n: The width of the rectangular piece of wood.
prices: A 2D integer array where prices[i] = [hi, wi, pricei] indicates you can
sell a rectangular piece of wood of height hi and width wi for pricei dollars.
Returns:
The maximum money you can earn after cutting the piece of wood.
"""
dp = [[0] * (n + 1) for _ in range(m + 1)]
# Initialize DP table with prices for given sizes
for h, w, price in prices:
dp[h][w] = price
# Iterate through all possible sizes
for h in range(1, m + 1):
for w in range(1, n + 1):
# Check if there is a price for the current size
price_for_current_size = 0
for height, width, price in prices:
if height == h and width == w:
price_for_current_size = price
break
dp[h][w] = max(dp[h][w], price_for_current_size)
# Consider all possible horizontal cuts
for i in range(1, h):
dp[h][w] = max(dp[h][w], dp[i][w] + dp[h - i][w])
# Consider all possible vertical cuts
for j in range(1, w):
dp[h][w] = max(dp[h][w], dp[h][j] + dp[h][w - j])
return dp[m][n]