枚举算法-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;
    }
};
相关推荐
Morwit25 分钟前
【力扣hot100】 1. 两数之和
数据结构·c++·算法·leetcode·职场和发展
py有趣33 分钟前
力扣热门100题之岛屿的数量(DFS/BFS经典题)
leetcode·深度优先·宽度优先
无小道1 小时前
算法——暴力+优化
算法·优化·暴力
Free Tester1 小时前
如何判断 LeakCanary 报告的严重程度
java·jvm·算法
zyq99101_12 小时前
DFS算法实战:经典例题代码解析
python·算法·蓝桥杯·深度优先
智者知已应修善业2 小时前
【51单片机单按键切换广告屏】2023-5-17
c++·经验分享·笔记·算法·51单片机
广州灵眸科技有限公司2 小时前
为RK3588注入澎湃算力:RK1820 AI加速卡完整适配与评测指南
linux·网络·人工智能·物联网·算法
qinian_ztc2 小时前
frida 14.2.18 安装报错解决
算法·leetcode·职场和发展
AI应用实战 | RE2 小时前
012、检索器(Retrievers)核心:从向量库中智能查找信息
人工智能·算法·机器学习·langchain
凤年徐2 小时前
C++手撕红黑树:从0到200行,拿下STL map底层核心
c++·后端·算法