枚举算法-day2

1.买股票的最佳时机

题目

解析

  • 记录最小值,然后遍历即可

代码

cpp 复制代码
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        // 时间复杂度:O(n)
        // 空间复杂度:O(1)

        int n = prices.size();
        int ans = 0;

        int min_price = prices[0];
        for(int j = 1;j < n;j ++){
            ans = max(ans,prices[j] - min_price);
            min_price = min(min_price,prices[j]);
        }

        return ans;
    }
};

2.数位和相等数对的最大和

题目

解析

  • 哈希表中,键值对:对应数位和中最大的数;每次更新答案需要更新该数位和最大的数;

代码

cpp 复制代码
class Solution {
    int f(int x){ // 计算 x 的数位和
        int sum = 0;
        while(x){
            sum += x % 10;
            x /= 10;
        }
        return sum;
    }

public:
    int maximumSum(vector<int>& nums) {
        // 时间复杂度:O(nlog U),U 为 max(nums)
        // 空间复杂度:O(log n)

        int n = nums.size();
        int ans = -1;

        unordered_map<int,int> cnt;

        for(int j = 0;j < n;j ++){
            auto it = cnt.find(f(nums[j]));// 寻找前面有没有相同数位和的数
            if(it != cnt.end()){
                ans = max(ans,it -> second + nums[j]);
            }
            cnt[f(nums[j])] = max(nums[j],cnt[f(nums[j])]);// 相同数位和保留最大的
        }

        return ans;
    }
};

3.数组中的最大数对和

题目

解析

  • 同理可得;

代码

cpp 复制代码
class Solution {
    int f(int x){
        int ans = 0;
        while(x){
            ans = max(ans,x % 10);
            x /= 10;
        }
        return ans;
    }

public:
    int maxSum(vector<int>& nums) {
        // 时间复杂度:O(nlog U)
        // 空间复杂度:O(n)

        int n = nums.size();
        int ans = -1;

        unordered_map<int,int> cnt;

        for(int j = 0;j < n;j ++){
            auto it = cnt.find(f(nums[j]));
            if(it != cnt.end()){
                ans = max(ans,it -> second + nums[j]);
            }
            cnt[f(nums[j])] = max(cnt[f(nums[j])],nums[j]); 
        }   

        return ans;
    }
};

4.K 和数组对的最大数目

题目

解析

  • 用哈希表记录每个数的个数,而当 target 没有数的时候,必须 erase ;

代码

cpp 复制代码
class Solution {
public:
    int maxOperations(vector<int>& nums, int k) {
        // 时间复杂度:O(n)
        // 空间复杂度:O(n)

        int n = nums.size();
        int ans = 0;

        unordered_map<int,int> cnt;

        for(int j = 0;j < n;j ++){
            auto it = cnt.find(k - nums[j]);

            if(it != cnt.end()){ // 找到了
                ans ++;
                cnt[k - nums[j]] --;
                if(cnt[k - nums[j]] == 0) cnt.erase(k - nums[j]);// 减到 0 了
            }else { // 没找到
                cnt[nums[j]] ++;
            }
        }

        return ans;
    }
};
相关推荐
风筝在晴天搁浅21 小时前
hot100 438.找到字符串中所有字母异位词
算法
zmzb01031 天前
C++课后习题训练记录Day53
数据结构·c++·算法
老黄编程1 天前
视觉SLAM十四讲解读-(v2.p84)李代数求导
算法·slam·李群李代数·视觉slam十四讲
LYFlied1 天前
【每日算法】131. 分割回文串
前端·数据结构·算法·leetcode·面试·职场和发展
夏乌_Wx1 天前
练题100天——DAY30:下一个更大的元素+键盘行
数据结构·算法
长安er1 天前
LeetCode 300/152/416/32 动态规划进阶题型总结(最长递增子序列→最长有效括号)
数据结构·算法·leetcode·动态规划·剪枝
天赐学c语言1 天前
12.18 - 有效的括号 && C语言中static的作用
数据结构·c++·算法·leecode
2401_876221341 天前
数据结构-绪论
数据结构
季远迩1 天前
LeetCode 热题 100 Python3易懂题解(更新中)
算法·leetcode·哈希算法
CoovallyAIHub1 天前
从“模仿”到“进化”!华科&小米开源MindDrive:在线强化学习重塑「语言-动作」闭环驾驶
深度学习·算法·计算机视觉