枚举算法-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;
    }
};
相关推荐
yyy(十一月限定版)7 分钟前
matlab矩阵的操作
算法·matlab·矩阵
努力学算法的蒟蒻23 分钟前
day58(1.9)——leetcode面试经典150
算法·leetcode·面试
txinyu的博客38 分钟前
map和unordered_map的性能对比
开发语言·数据结构·c++·算法·哈希算法·散列表
搞笑症患者1 小时前
压缩感知(Compressed Sensing, CS)
算法·最小二乘法·压缩感知·正交匹配追踪omp·迭代阈值it算法
im_AMBER1 小时前
Leetcode 101 对链表进行插入排序
数据结构·笔记·学习·算法·leetcode·排序算法
予枫的编程笔记1 小时前
【Java集合】深入浅出 Java HashMap:从链表到红黑树的“进化”之路
java·开发语言·数据结构·人工智能·链表·哈希算法
快手技术1 小时前
AAAI 2026|全面发力!快手斩获 3 篇 Oral,12 篇论文入选!
前端·后端·算法
颜酱1 小时前
前端算法必备:滑动窗口从入门到很熟练(最长/最短/计数三大类型)
前端·后端·算法
做科研的周师兄1 小时前
【MATLAB 实战】栅格数据 K-Means 聚类(分块处理版)—— 解决大数据内存溢出、运行卡顿问题
人工智能·算法·机器学习·matlab·kmeans·聚类
X在敲AI代码1 小时前
leetcodeD3
数据结构·算法