• C++二分查找算法:132 模式解法三枚举1


    本题不同解法

    分析

    时间复杂度

    两轮循环时间复杂度都是O(nlogn)。

    步骤

    第一轮,枚举3,nums(j,m_c)中小于nums[j]的元素都可以成为2,如果有多个合法2,选择最大的2。如果不存在2,设置成比最小值还小的值。[begin,it)是所有合法的2,由于是升序,最大的2就是最后一个。
    第二轮,枚举1,nums(i,m_c)中的3对应的2 大于nums[i],则有结果,否则无解。

    代码

    核心代码

    class Solution {
    public:
    	bool find132pattern(vector<int>& nums) {
    		m_c = nums.size();
    		const int iNotMayMinValue = -1000 * 1000 * 1000 - 1;
    		{
    			std::set<int> set2;
    			for (int j = m_c-1; j >= 0 ; j-- )
    			{
    				const int& iValue = nums[j];
    				auto it = set2.lower_bound(iValue);
    				m3To2[iValue] = (set2.begin() != it) ? *std::prev(it) : iNotMayMinValue;
    				set2.emplace(iValue);
    			}
    		}
    		//寻找1,即nums[i]
    		{		
    			std::set<int> set2;
    			for (int i = m_c-1 ; i >= 0 ; i-- )
    			{
    				const int& iValue = nums[i];
    				auto it = set2.upper_bound(iValue);
    				if (set2.end() != it)
    				{
    					m_iIndex1 = i;
    					return true;
    				}
    				set2.emplace(m3To2[iValue]);
    			}
    		}
    		return false;
    	}
    	std::unordered_map<int, int> m3To2;
    	int m_iIndex1 = -1;
    	int m_c;
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    测试用例

    template
    void Assert(const T& t1, const T& t2)
    {
    assert(t1 == t2);
    }

    template
    void Assert(const vector& v1, const vector& v2)
    {
    if (v1.size() != v2.size())
    {
    assert(false);
    return;
    }
    for (int i = 0; i < v1.size(); i++)
    {
    Assert(v1[i], v2[i]);
    }
    }

    int main()
    {
    vector nums;
    bool res;
    {
    Solution slu;
    nums = { 3,5,0,3,4 };
    res = slu.find132pattern(nums);
    //Assert(vector{5, 0, 5, 2, 0}, slu.m_v3To1);
    Assert(0, slu.m_iIndex1);
    Assert(true, res);
    }
    {
    nums = { 1 ,2, 3,4 };
    res = Solution().find132pattern(nums);
    Assert(false, res);
    }
    {
    Solution slu;
    nums = { 3,1,4,2 };
    res = slu.find132pattern(nums);
    //Assert(vector{4, 4, 0, 1}, slu.m_v3To1);
    Assert(1, slu.m_iIndex1);
    Assert(true, res);
    }
    {
    Solution slu;
    nums = { -1,3,2,0 };
    res = slu.find132pattern(nums);
    //Assert(vector{4, 0, 0, 0}, slu.m_v3To1);
    Assert(0, slu.m_iIndex1);
    Assert(true, res);
    }

    {
    	Solution slu;
    	nums = { 1, 0, 1, -4, -3 };
    	res = slu.find132pattern(nums);
    	//Assert(vector{4, 0, 0, 0}, slu.m_v3To1);
    	Assert(-1, slu.m_iIndex1);
    	Assert(false, res);
    }
    
    //CConsole::Out(res);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    }

    扩展阅读

    视频课程

    有效学习:明确的目标 及时的反馈 拉伸区(难度合适),可以先学简单的课程,请移步CSDN学院,听白银讲师(也就是鄙人)的讲解。
    https://edu.csdn.net/course/detail/38771

    如何你想快速形成战斗了,为老板分忧,请学习C#入职培训、C++入职培训等课程
    https://edu.csdn.net/lecturer/6176

    相关下载

    想高屋建瓴的学习算法,请下载《闻缺陷则喜算法册》doc版
    https://download.csdn.net/download/he_zhidan/88348653

    洒家想对大家说的话
    闻缺陷则喜是一个美好的愿望,早发现问题,早修改问题,给老板节约钱。
    墨家名称的来源:有所得以墨记之。
    如果程序是一条龙,那算法就是他的是睛

    测试环境

    操作系统:win7 开发环境: VS2019 C++17
    或者 操作系统:win10 开发环境: VS2022 C++17

  • 相关阅读:
    【无标题】
    # 研究杂感 × DEA-Malmquist
    FTP服务配置和使用
    not_the_same_3dsctf_2016【简单解法和mprotect解法】
    论文阅读之Multimodal Chain-of-Thought Reasoning in Language Models
    【1656. 设计有序流】
    QT环境下,easylogging++解析配置参数错误的解决记录
    halcon 与圆接近的区域例子 circularity
    MP3是如何诞生的?
    Java IO流处理 面试题汇总
  • 原文地址:https://blog.csdn.net/he_zhidan/article/details/134460077