力扣118双周赛

第 118 场双周赛

文章目录

查找包含给定字符的单词

模拟

cpp 复制代码
class Solution {
public:
    vector<int> findWordsContaining(vector<string>& words, char x) {
        vector<int>res;
        for(int j = 0 ; j < words.size() ; j ++){
            bool f = false;
            string t = words[j];
            for(int i = 0 ; i < t.size() ; i ++){
                if(t[i] == x)f = true;
            }
            if(f)res.push_back(j);
        }
        return res;
    }
};

最大化网格图中正方形空洞的面积

长和宽分别找最长的连续部分,取最小相乘

cpp 复制代码
class Solution {
public:
    int maximizeSquareHoleArea(int n, int m, vector<int>& hBars, vector<int>& vBars) {
        ranges::sort(hBars);
        ranges::sort(vBars);
        int res = 0 , a = 0 , b = 0 , c = 0;
        int last = -1;
        for(int i = 0 ; i < hBars.size() ; i ++){
            if(last == -1){last = hBars[i] ; a = max(a , 1); c = 1;continue;}
            if(hBars[i] - last == 1){
                c ++;
            }else{
                a = max(a , c);
                c = 1;
            }
            last = hBars[i];
            
        }
        a = max(a , c);
        last = -1;
        for(int i = 0 ; i < vBars.size() ; i ++){
            if(last == -1){last = vBars[i] ; b = max(b , 1); c = 1;continue;}
            if(vBars[i] - last == 1){
                c ++;
            }else{
                b = max(b , c);
                c = 1;
            }
            last = vBars[i];          
        }
        b = max(b , c);
        res = (min(a,b) + 1) * (min(a,b) + 1);
        return res;
    }
};

购买水果需要的最少金币数

用dp[i][0]表示买前i个水果且不买第i个水果的最少金币数

用dp[i][1]表示买前i个水果且买第i个水果的最少金币数

cpp 复制代码
class Solution {
public:
    int minimumCoins(vector<int>& prices) {
        int n = prices.size();
        int dp[1005][2];
        //初始化
        for(int i = 0 ; i <= n ; i ++){
            dp[i][0] = dp[i][1] = 999999;
        }
        dp[1][1] = prices[0];
        //dp
        for(int i = 1 ; i < n ; i ++){
            //买第i个
            dp[i + 1][1] = min(dp[i][0] ,dp[i][1]) + prices[i];
            //不买第i个
            for(int j = i; j + j >= i  + 1; j --){
                dp[i + 1][0] = min(dp[i + 1][0] , dp[j][1]);
            }
        }
        return min(dp[n][0],dp[n][1]);
    }
};

找到最大非递减数组的长度

后面补题

cpp 复制代码

--

相关推荐
承渊政道2 小时前
【动态规划算法】(完全背包问题从状态定义到空间优化)
数据结构·c++·学习·算法·leetcode·动态规划·哈希算法
超级大福宝2 小时前
【力扣48. 旋转图像】超好记忆版 + 口诀
c++·算法·leetcode
爱写代码的倒霉蛋2 小时前
2023年天梯赛L1-8
数据结构·算法
lzh200409192 小时前
深入学习Linux进程间通信:共享内存
linux·c++
apollowing2 小时前
启发式算法WebApp实验室:从搜索策略到群体智能的能力进阶(上)
算法·启发式算法·web app
特种加菲猫2 小时前
多态:让代码拥有“千变万化”的能力
开发语言·c++
生物信息与育种3 小时前
黄三文院士领衔植物星球计划(PLANeT)发表Cell
人工智能·深度学习·算法·面试·transformer
aini_lovee3 小时前
WSN 四大经典无需测距定位算法
算法
人道领域3 小时前
【LeetCode刷题日记】掌握二叉树遍历:栈实现的三种绝妙方法
算法·leetcode·职场和发展