首页 > 其他分享 >leetcode第 109 场双周赛

leetcode第 109 场双周赛

时间:2023-07-23 18:55:05浏览次数:42  
标签:num nums int 双周 109 long return leetcode dp

6930. 检查数组是否是好的 - 力扣(LeetCode)

首先判断数组长度是不是最大值 + 1, 然后排个序,判断0到n - 2是不是都是1到最大值的一个排列,满足这些返回true就行了

class Solution {
public:
    bool isGood(vector<int>& num) {
        int ma = 0;
        for(auto i : num){
            ma = max(ma,  i);
        }
        
        if(ma + 1!= num.size())
            return false;
        
        sort(num.begin(), num.end());
        
        for(int i = 0;i < num.size() - 2;i ++){
            if(num[i] + 1 != num[i + 1])
                return false;
        }
        
        return true;
        
    }
};

6926. 将字符串中的元音字母排序 - 力扣(LeetCode)

先把s里的元音字母取出来,然后排序,最后替换掉s中的元音字母即可

class Solution {
public:
    string sortVowels(string s) {
    string os = "";
    string yuan = "aeiouAEIOU";

    for(auto i : s ){
        if(yuan.find(i) != -1){
            os += i;
        }
    }

    sort(os.begin(),os.end());

    int cnt = 0;
    for(int i = 0;i < s.size();i ++){
        if(yuan.find(s[i]) != -1){
            s[i] = os[cnt++];
        }
    }
        return s;
    }
};

6931. 访问数组中的位置使分数最大 - 力扣(LeetCode)

从\(i\)可以跳掉任意\(i < j\)的\(j\)的位置,要使得\(i\)处分数最大,则就是求\(j\)处的分数最大,所以我们可以从最后转移到初始位置,分别从奇数和偶数转移

class Solution {
public:
    long long maxScore(vector<int>& nums, int x) {
        long long n = nums.size();
        vector<vector<long long>> dp(2, vector<long long> (n , 0));

        dp[nums[n - 1] & 1][n - 1] = nums[n - 1];
        dp[!(nums[n - 1] & 1)][n - 1] = max(0,nums[n - 1] - x);

        for(long long i = n - 2;i >= 0;i --){
            dp[0][i] = dp[0][i + 1];
            dp[1][i] = dp[1][i + 1];

            dp[nums[i] & 1][i] += nums[i];

            dp[0][i] = max(dp[0][i], dp[1][i] - x);
            dp[1][i] = max(dp[1][i], dp[0][i] - x);

        }

        return dp[nums[0] & 1][0];

    }
};

6922. 将一个数字表示成幂的和的方案数 - 力扣(LeetCode)

01 背包模板题。把正整数的 \(x\) 次幂看成一个物品,求的就是把 \(n\) 件物品凑成 \(n\) 的方案数。复杂度 \(\mathcal{O}(n^2)\)

class Solution {
public:
    int numberOfWays(int n, int x) {

        const int mod = 1e9 + 7;
        vector<int> p(n + 1);
        for(int i = 1;i <= n;i ++){
            long long res = pow(i, x);
            p[i] = res % mod;
        }

        vector<int> dp(n + 1);
        dp[0] = 1;

        for(int i = 1;i <= n;i ++){
            for(int j = n;j >= p[i];j--){
                dp[j] = (dp[j] + dp[j - p[i]]) % mod;
            }
        }

        return dp[n];
    }
};

标签:num,nums,int,双周,109,long,return,leetcode,dp
From: https://www.cnblogs.com/Kescholar/p/17575725.html

相关文章

  • java实现leetcode 22 括号的生成
    Java实现Leetcode22-括号的生成本文将介绍如何使用Java实现Leetcode22题-括号的生成,并提供相应的代码示例。括号的生成是一个经典的递归问题,通过理解递归的思想和括号生成的规律,我们可以很容易地解决这个问题。题目描述给定一个整数n,表示生成括号的对数,编写一个函数来生成......
  • 【优先队列】【堆排序实现优先队列】[1054. 距离相等的条形码](https://leetcode.cn/p
    【优先队列】【堆排序实现优先队列】1054.距离相等的条形码在一个仓库里,有一排条形码,其中第i个条形码为barcodes[i]。请你重新排列这些条形码,使其中任意两个相邻的条形码不能相等。你可以返回任何满足该要求的答案,此题保证存在答案。示例1:输入:barcodes=[1,1,1,2,2,2]......
  • [LeetCode] 894. All Possible Full Binary Trees
    Givenaninteger n,return alistofallpossible fullbinarytrees with n nodes.Eachnodeofeachtreeintheanswermusthave Node.val==0.Eachelementoftheansweristherootnodeofonepossibletree.Youmayreturnthefinallistoftreesin......
  • leetcode-2582-easy
    PassthePillowTherearenpeoplestandinginalinelabeledfrom1ton.Thefirstpersoninthelineisholdingapillowinitially.Everysecond,thepersonholdingthepillowpassesittothenextpersonstandingintheline.Oncethepillowreachest......
  • Leetcode394. 字符串解码
    classSolution{public:stringdfs(strings,int&idx){stringstr;while(idx<s.size()){if(s[idx]==']'){idx++;returnstr;}......
  • leetcode 栈与队列 232 225
    目录基本介绍四个问题232225基本介绍栈,先进后出队列,先进先出四个问题C++中stack是容器么?我们使用的stack是属于哪个版本的STL?我们使用的STL中stack是如何实现的?stack提供迭代器来遍历stack空间么?首先大家要知道栈和队列是STL(C++标准库)里面的两个数据结构。C++标准......
  • [LeetCode] 1349. Maximum Students Taking Exam 参加考试的最大学生数
    Givena m *n matrix seats  thatrepresentseatsdistributions inaclassroom. Ifaseat is broken,itisdenotedby '#' characterotherwiseitisdenotedbya '.' character.Studentscanseetheanswersofthosesittingnexttothele......
  • codility 和 leetcode 对比
    根据网上的信息,codility和leetcode都是用于评估编程技能的在线平台,它们都提供了不同难度和类型的编程挑战,支持多种编程语言,并可以用于招聘和面试的过程中。不过,它们也有一些区别,比如:codility更专注于工程团队的技能评估,它提供了CodeCheck,CodeLive,和CodeEvent三个功......
  • leetcode872叶相似树
    这道题是考虑的深度优先搜索,使用递归vecotr和queue入队操作并不相同:vector只能使用push_back();queue既可以使用push()还可以使用push_back()voidFindLeaf(TreeNode*root,vector<int>&v){if(!root->left&&!root->right){v.push_back(root->val);re......
  • LeetCode 347. 前 K 个高频元素
    快排思想注意,这里是倒序排序,因此应该while(nums[i].cnt>x);classSolution{public:structelement{intval,cnt;element(inta,intb){val=a;cnt=b;}};vector<int>res;voidquick......