LeetCode2865. Beautiful Towers I

文章目录

一、题目

You are given a 0-indexed array maxHeights of n integers.

You are tasked with building n towers in the coordinate line. The ith tower is built at coordinate i and has a height of heights[i].

A configuration of towers is beautiful if the following conditions hold:

1 <= heights[i] <= maxHeights[i]
heights is a mountain array.
Array heights is a mountain if there exists an index i such that:

For all 0 < j <= i, heights[j - 1] <= heights[j]
For all i <= k < n - 1, heights[k + 1] <= heights[k]
Return the maximum possible sum of heights of a beautiful configuration of towers.

Example 1:

Input: maxHeights = [5,3,4,1,1]
Output: 13
Explanation: One beautiful configuration with a maximum sum is heights = [5,3,3,1,1]. This configuration is beautiful since:

  • 1 <= heights[i] <= maxHeights[i]
  • heights is a mountain of peak i = 0.
    It can be shown that there exists no other beautiful configuration with a sum of heights greater than 13.
    Example 2:

Input: maxHeights = [6,5,3,9,2,7]
Output: 22
Explanation: One beautiful configuration with a maximum sum is heights = [3,3,3,9,2,2]. This configuration is beautiful since:

  • 1 <= heights[i] <= maxHeights[i]
  • heights is a mountain of peak i = 3.
    It can be shown that there exists no other beautiful configuration with a sum of heights greater than 22.
    Example 3:

Input: maxHeights = [3,2,5,5,2,3]
Output: 18
Explanation: One beautiful configuration with a maximum sum is heights = [2,2,5,5,2,2]. This configuration is beautiful since:

  • 1 <= heights[i] <= maxHeights[i]
  • heights is a mountain of peak i = 2.
    Note that, for this configuration, i = 3 can also be considered a peak.
    It can be shown that there exists no other beautiful configuration with a sum of heights greater than 18.

Constraints:

1 <= n == maxHeights <= 103
1 <= maxHeights[i] <= 109

二、题解

class Solution {
   
public:
    long long maximumSumOfHeights(vector<int>& maxHeights) {
   
        long long res = 0;
        int n = maxHeights.size();
        for(int i = 0;i < n;i++){
   
            int pre = maxHeights[i];
            long long sum = pre;
            for(int j = i - 1;j >= 0;j--){
   
                pre = min(pre,maxHeights[j]);
                sum += pre;
            }
            int suf = maxHeights[i];
            for(int j = i + 1;j < n;j++){
   
                suf = min(suf,maxHeights[j]);
                sum += suf;
            }
            res = max(res,sum);
        }
        return res;
    }
};

相关推荐

  1. LeetCode2865. Beautiful Towers I

    2024-01-25 19:38:01       56 阅读
  2. LeetCode解法汇总2865. 美丽塔 I

    2024-01-25 19:38:01       58 阅读
  3. LeetCode 2866. 美丽塔 II

    2024-01-25 19:38:01       69 阅读
  4. LeetCode2765. Longest Alternating Subarray

    2024-01-25 19:38:01       46 阅读
  5. LeetCode 2865. 美丽塔 I,前后缀分离+单调栈

    2024-01-25 19:38:01       63 阅读
  6. LeetCode解法汇总2866. 美丽塔 II

    2024-01-25 19:38:01       68 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-25 19:38:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-25 19:38:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-25 19:38:01       82 阅读
  4. Python语言-面向对象

    2024-01-25 19:38:01       91 阅读

热门阅读

  1. 什么是IDE?新手用哪个IDE比较好?

    2024-01-25 19:38:01       61 阅读
  2. P8597 [蓝桥杯 2013 省 B] 翻硬币

    2024-01-25 19:38:01       54 阅读
  3. 连接两个链表。

    2024-01-25 19:38:01       63 阅读
  4. 【issue—成像系统】1. 机器视觉照明技术基础

    2024-01-25 19:38:01       60 阅读
  5. 粒子群算法和模因算法的关系?

    2024-01-25 19:38:01       60 阅读
  6. 【MongoDB】mongodb安装及启动踩坑点

    2024-01-25 19:38:01       57 阅读