HUAWEI OD machine test real questions - maximum area of solar panels - 2023 OD unified test (B paper)

Title description:

To install a rectangular or square solar panel on one side of the spacecraft (the red slash area in the picture), you need to install two pillars (the black vertical bars in the picture) first, and then fix the solar panel in the middle of the pillar. However, the length of the pillars at different positions of the spacecraft is different, and the installation area of ​​the solar panel is limited by the length of the pillar on the shortest side. As shown in the picture:

Now provide a set of pillar height data of an integer array, assuming that the distance between each pillar is equal to 1 unit length, calculate how to choose two pillars to maximize the area of ​​the solar panel.

Enter a description:

10,9,8,7,6,5,4,3,2,1

Note: There are at least 2 pillars and a maximum of 10,000 pillars, and the supported height range is an integer from 1 to 10^9. The heights of the pillars are random, the decrement in the example is just a coincidence.

Output description:

The maximum solar panel area that can be supported: (between 10m high pillar and 5m high pillar)

25

Supplementary note:

The width between the 10-meter-high pillar and the 5-meter-high pillar is 5, and the height of the smaller pillar is also 5, and the area is 25. The area that can be obtained by taking the other two pillars is less than 25. So the maximum solar panel area is 25.

Example 1

enter:

10,9,8,7,6,5,4,3,2,1

output:

25

illustrate:

nums=list(map(int,input().split(',')))
l=0
r=len(nums)-1
lm,rm=nums[l],nums[r]
ans=0
while l<r:
    if lm<rm:
        ans=max(ans,lm*(r-l))
        l+=1
        lm=max(lm,nums[l])
    else:
        ans=max(ans,rm*(r-l))
        r-=1
        rm=max(rm,nums[r])
print(ans)

        
    
    

Guess you like

Origin blog.csdn.net/2301_76848549/article/details/131632926