较难算法美丽塔时间复杂度O(n)

题目

给你一个长度为 n 下标从 0 开始的整数数组 maxHeights 。你的任务是在坐标轴上建 n 座塔。第 i 座塔的下标为 i ,高度为 heightsi

如果以下条件满足,我们称这些塔是 美丽 的:

1 <= heightsi <= maxHeightsi

heights 是一个 山状 数组。

如果存在下标 i 满足以下条件,那么我们称数组 heights 是一个 山状 数组:

对于所有 0 < j <= i ,都有 heightsj - 1 <= heightsj

对于所有 i <= k < n - 1 ,都有 heightsk + 1 <= heightsk

请你返回满足 美丽塔 要求的方案中,高度和的最大值 。

时间复杂度

O(nlogn)

典型样例分析

当i是山顶时,Lefti记录0,i的最大高度和,Righti记录[i,n)的最大高度和。

笨办法

由于赛场时间紧,压力大。所以只想到一个笨办法。从小到处理最大高度。下面以Lefti为例来说明。如果不存在j(0<=j<i)使得maxHeightj < maxHeighti ,那么0,i的高度全为)maxHeighti,如{5,4,3,2,1};如果只存在唯一的j,则0,j高度不变,(j,i]的高度为maxHeighti,如{3,1,2}。如果存在多个j,以j最大的为准,如{2,1,3}。如果maxHeightj == maxHeighti,则heightj变和不变的结果都一样。

代码

核心代码

cpp 复制代码
class Solution {
public:
    long long maximumSumOfHeights(vector<int>& maxHeights) {
        m_c = maxHeights.size();
        std::multimap<int, int> mHeightIndex;
        for (int i = 0; i < m_c; i++)
        {
            mHeightIndex.emplace(maxHeights[i], i);
        }
        
        for (const auto& [h, i] : mHeightIndex)
        {
            {//计算m_mLeft
                auto it = m_mLeft.lower_bound(i);
                if (m_mLeft.begin() == it)
                {
                    m_mLeft[i] = (long long)h * (i + 1);
                }
                else
                {
                    auto pre = std::prev(it);
                    m_mLeft[i] = pre->second + (long long)h * (i - pre->first);
                }
            }
            {//计算m_mRight
                auto it = m_mRight.upper_bound(i);
                if (m_mRight.end() == it)
                {
                    m_mRight[i] = (long long)h * (m_c - i);
                }
                else
                {
                    m_mRight[i] = (long long)it->second + (long long)h * (it->first - i);
                }
            }
        }
        long long llRet = 0;
        for (int i = 0; i < m_c; i++)
        {//假定i是山顶            
            long long llCur = m_mLeft[i] + m_mRight[i] - maxHeights[i];
            llRet = max(llRet, llCur);
        }        
        return llRet;
    }
    int m_c;
    std::map<int, long long> m_mLeft, m_mRight;
};

测试用代码

class CDebug : public Solution

{

public:

long long maximumSumOfHeights( vector<int>& maxHeights, vector<int>& vLeft, vector<int>& vRight)

{

long long llRet = Solution::maximumSumOfHeights(maxHeights);

for (const auto& it : m_mLeft)

{

assert(it.second == vLeftit.first);

}

for (const auto& it : m_mRight)

{

assert(it.second == vRightit.first);

}

//调试用代码

std::cout << "Left: ";

for (int i = 0; i < m_c; i++)

{

std::cout << m_mLefti << " ";

}

std::cout << std::endl;

std::cout << "Right: ";

for (int i = 0; i < m_c; i++)

{

std::cout << m_mRighti << " ";

}

std::cout << std::endl;

return llRet;

}

};

int main()

{

vector < vector<vector<int>>> param = { {{1,2,3,4,5} ,{1,3,6,10,15},{5,8,9,8,5}} ,

{{5,4,3,2,1},{5,8,9,8,5},{15,10,6,3,1}} ,

{{1,2,4,3,5},{1,3,7,9,14},{5,8,10,6,5}},

{{3,1,2}, {3,2,4},{5,2,2}},

{{2,1,3},{2,2,5},{4,2,3}} };

for ( auto& vv : param)

{

auto res = CDebug().maximumSumOfHeights(vv0,vv1,vv2);

}

//auto res = Solution().maxPalindromes("rire", 3);

//CConsole::Out(res);

}

测试环境

Win10,VS2022 C++17

相关下载

word版讲解
https://download.csdn.net/download/he_zhidan/88348653

源码及测试用例
https://download.csdn.net/download/he_zhidan/88370053

相关推荐
feng_you_ying_li1 分钟前
Linux 之线程封装,线程的同步与互斥,互斥锁的介绍
linux·c++·算法
星恒随风2 分钟前
C++入门(二):函数重载、引用、const引用和 inline 内联函数
开发语言·c++·笔记·学习
basketball6168 分钟前
C++ 高级编程:1. 多线程基本操作
开发语言·c++
十五年专注C++开发8 分钟前
std::vector<T>到QVector<T>的数据复制方案
c++·vector·iterator模式·qvector
HZ·湘怡1 小时前
二叉树 2 堆
算法
wabs6668 小时前
关于贪心算法的思考
算法·贪心算法
社交怪人9 小时前
【判断大小】信息学奥赛一本通C语言解法(题号1043)
算法
Snasph9 小时前
GNU Make 用户手册(中文版)
服务器·算法·gnu
江澎涌9 小时前
拆解与 AI 的一次对话
人工智能·算法·程序员
sheeta19989 小时前
LeetCode 每日一题笔记 日期:2026.06.02 题目:3635. 最早完成陆地和水上游乐设施的时间 II
笔记·算法·leetcode