Similar Problems
Similar Problems not available
Selling Pieces Of Wood - Leetcode Solution
Companies:
LeetCode: Selling Pieces Of Wood Leetcode Solution
Difficulty: Hard
Topics: dynamic-programming array
Problem Statement:
You have a wooden plank of length n units. Some operations can be performed on the plank:
- Cut: Cut the plank into two pieces at a certain position.
- Add: Add a wooden plank with the given length to the plank with a certain position.
You are given a sequence of operations to be performed on the plank. You need to determine whether it is possible to obtain the target plank configuration by performing the given operations.
Input: The input to the function is specified as follows:
- A string operations of length m (1 ≤ m ≤ 103)
- operations[i] is either 'C' (for Cut) or 'A' (for Add)
- 0 ≤ position ≤ n for the Cut operation.
- 1 ≤ length ≤ n for the Add operation.
- 0 ≤ n ≤ 105
Output: Return true if it is possible to obtain the target plank configuration by performing operations, otherwise, return false.
Solution:
The problem can be approached using a greedy approach. We can simulate all the operations in the given order and check whether the final plank configuration matches with the target configuration.
To simulate the operations, we can maintain a list of intervals representing the pieces of plank at any given time. Initially, the list contains a single interval representing the whole plank. For each operation, we apply the respective modification to all intervals that intersect with the affected range.
For the Cut operation, we split the interval containing the cut position into two intervals. For the Add operation, we add a new interval to the list at the desired position.
After simulating all the operations, we check whether the final plank configuration matches the target configuration. We can do this by checking whether each interval in the final configuration matches an interval in the target configuration. If any interval is missing in the final configuration or has a different length, we know that the target configuration cannot be obtained.
Code:
Here is the Python code that implements the above solution:
def can_obtain_target_plank(operations: str, n: int, target: List[Tuple[int, int]]) -> bool: # Initialize the intervals list with a single interval representing the whole plank intervals = [(0, n)]
# Simulate all operations in the given order
for op in operations:
if op == 'C':
# Cut operation: Split the interval containing the cut position into two intervals
cut_pos = random.randint(0, n)
for i in range(len(intervals)):
left, right = intervals[i]
if left <= cut_pos <= right:
intervals.pop(i)
intervals.insert(i, (left, cut_pos))
intervals.insert(i+1, (cut_pos, right))
break
elif op == 'A':
# Add operation: Add a new interval to the list at the desired position
pos = random.randint(0, n)
length = random.randint(1, n-pos+1)
intervals.append((pos, pos+length))
# Check whether the final plank configuration matches the target configuration
for target_interval in target:
if target_interval not in intervals:
return False
for interval in intervals:
if interval not in target:
return False
return True
This code randomly generates cut and add positions and lengths for testing purposes. In the actual problem, these values are given in the input.
Selling Pieces Of Wood Solution Code
1