Skip to content

Commit 5ef4407

Browse files
committed
Find the maximum height of mud segment between walls
1 parent 06a4bcf commit 5ef4407

1 file changed

Lines changed: 98 additions & 0 deletions

File tree

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
"""
2+
Problem Statement
3+
A child likes to build mud walls by placing mud between sticks positioned on
4+
a number line. The gap between sticks will be referred to as a cell, and
5+
each cell will contain one segment of wall. The height of mud in a segment
6+
cannot exceed 1 unit above an adjacent stick or mud segment. Given the
7+
placement of a number of sticks and their heights, determine the maximum
8+
height segment of mud that can be built. If no mud segment can be built,
9+
return 0.
10+
11+
For example, there are n = 3 sticks at stickPositions = [1, 2, 4, 7] with s
12+
tickHeights = [4, 5, 7, 11]. There is no space between the first two sticks,
13+
so there is no cell for mud. Between positions 2 and 4, there is one cell.
14+
Heights of the surrounding sticks are 5 and 7, so the maximum height of mud
15+
is 5 + 1 = 6. Between positions 4 and 7 there are two cells. The heights of
16+
surrounding sticks are 7 and 11. The maximum height mud segment next to the
17+
stick of height 7 is 8. The maximum height mud next to a mud segment of
18+
height 8 and a stick of height 11 is 9. Mud segment heights are 6, 8 and 9,
19+
and the maximum height is 9. In the table below, digits are in the columns
20+
of sticks and M is in the mud segments.
21+
22+
7
23+
7
24+
M7
25+
MM7
26+
4MM7
27+
M4MM7
28+
2M4MM7
29+
12M4MM7
30+
12M4MM7
31+
12M4MM7
32+
12M4MM7
33+
Function Description
34+
Complete the function maxHeight in the editor below. The function must
35+
return an integer, the maximum height mud segment that can be built.
36+
37+
maxHeight has the following parameter(s):
38+
stickPositions[stickPositions[0],…stickPositions[n-1]]: an array of integers
39+
stickHeights[stickHeights[0],…stickHeights[n-1]]: an array of integers
40+
41+
Constraints
42+
1 ≤ n ≤ 105
43+
1 ≤ stickPositions[i] ≤ 109 (where 0 ≤ i < n)
44+
1 ≤ stickHeights[i] ≤ 109 (where 0 ≤ i < n)
45+
46+
Sample Input For Custom Testing
47+
48+
3
49+
1
50+
3
51+
7
52+
3
53+
4
54+
3
55+
3
56+
Sample Output
57+
58+
5
59+
Explanation
60+
61+
M
62+
1M MMM
63+
1M3MMM7
64+
1M3MMM7
65+
1M3MMM7
66+
Here stickPositions = [1, 3, 7] and stickHeights = [4, 3, 3]. There can be a
67+
segment of height 4 at position 2 supported by sticks of heights 4 and 3.
68+
Between positions 3 and 7, there can be a segment of height 4 at positions 4
69+
and 6. Between them, a segment can be built of height 5 at position 5.
70+
71+
72+
"""
73+
74+
def maxHeight(wallPositions,wallHeights):
75+
n = len(wallPositions)
76+
maxim = 0
77+
for i in range(n-1):
78+
if wallPositions[i] < wallPositions[i+1]-1:
79+
heightDiff = abs(wallHeights[i+1]-wallHeights[i])
80+
gapLen = wallPositions[i+1]-wallPositions[i]-1
81+
localMax = 0
82+
if gapLen > heightDiff:
83+
low = max(wallHeights[i+1],wallHeights[i]) + 1
84+
remainingGap = gapLen - heightDiff - 1
85+
localMax = low + remainingGap//2
86+
else:
87+
localMax = min(wallHeights[i+1],wallHeights[i]) + gapLen
88+
maxim = max(maxim,localMax)
89+
return maxim
90+
91+
92+
if __name__ == '__main__':
93+
wallPositions = [1,2,4,7]
94+
wallHeights = [4,6,8,11]
95+
print (maxHeight(wallPositions=wallPositions,wallHeights=wallHeights))
96+
wallPositions = [1,3,7]
97+
wallHeights = [4,3,3]
98+
print (maxHeight(wallPositions,wallHeights))

0 commit comments

Comments
 (0)