首页 > 其他分享 >LeetCode 第 115 场双周赛

LeetCode 第 115 场双周赛

时间:2023-11-12 23:11:05浏览次数:36  
标签:cnt int res back 双周 115 vector words LeetCode

2899. 上一个遍历的整数

感觉读题比较困难

class Solution {
public:
    vector<int> lastVisitedIntegers(vector<string>& words) {
        vector<int> res , a ;
        for( int i = 0 , cnt = 0 , x ; i < words.size() ;  i ++ ){
            if( words[i] != "prev" ){
                cnt = x = 0;
                for( auto j : words[i] ) x = x * 10 + j - '0';
                a.push_back(x);
            } else {
                cnt ++;
                if( (int)a.size() - cnt < 0 ) res.push_back(-1);
                else res.push_back( a[(int)a.size() - cnt] );
            }
        }
        return res;
    }
};

2900. 最长相邻不相等子序列

枚举一下第一位要不要,然后贪心选择即可

class Solution {
public:
    vector<string> getWordsInLongestSubsequence(int n, vector<string>& words, vector<int>& groups) {
        vector<int> a , b;
        a.push_back(0);
        for( int i = 1 ; i < n ; i ++ )
            if( groups[i] != groups[ a.back() ] ) a.push_back(i);
        if( 1 < n ) b.push_back(1);
        for( int i = 2 ; i < n ; i ++ )
            if( groups[i] != groups[ b.back() ] ) b.push_back(i);
        if( a.size() < b.size() ) a.swap(b);
        vector<string> res;
        for( auto i : a )
            res.push_back( words[i] );
        return res;
    }
};

2901. 最长相邻不相等子序列 II

\(O(n^2)\)的dp转移一下,然后记录转移过来的状态的前驱,然后还原回去

class Solution {

int dis( const string & a , const string &b ){
    if( a.size() != b.size() ) return -1;
    int cnt = 0;
    for( int i = 0 ; i < a.size() ; i ++ )
        cnt += a[i] != b[i];
    return cnt;
}

public:
    vector<string> getWordsInLongestSubsequence(int n, vector<string>& words, vector<int>& groups) {
        vector<int> f(n , 1) , fa(n);
        iota( fa.begin() , fa.end() , 0);
        for( int i = 0 ; i < n ; i ++ ){
            for( int j = 0 ; j < i ; j ++ ){
                if( groups[i] == groups[j] or dis( words[i] , words[j] ) != 1 ) continue;
                if( f[i] > f[j] + 1 ) continue;
                f[i] = f[j] + 1 , fa[i] = j;
            }
        }
        int it = max_element( f.begin() , f.end() ) - f.begin();
    
        vector<string> res;
        while( it != fa[it] ) res.push_back( words[it] ) , it = fa[it];
        res.push_back( words[it] );
        reverse( res.begin() , res.end() );
        return res;

    }
};

2902. 和带限制的子多重集合的数目

多重背包\(f[i][j]\)表示前\(i\)个物品购买价值总和为\(j\)的方案数。

class Solution {

const int mod = 1e9+7;
using vi = vector<int>;

public:
    int countSubMultisets(vector<int>& nums, int l, int r) {
        unordered_map<int,int> cnt;
        int total = 0;
        for( auto i : nums )
            cnt[i] ++ , total += i;
        if( l > total ) return 0;
        r = min( r , total );
        vi f(r + 1);
        f[0] = cnt[0] + 1 , cnt.erase(0);
        int sum = 0;
        for( const auto &[ x , c ] :  cnt ){
            auto g = f;
            sum = min(sum + x * c , r);
            for( int i = x ; i <= sum ; i ++ ){
                g[i] = ( g[i] + g[i - x ]) % mod;
                if( i >= ( c + 1 ) * x )
                    g[i] = ( g[i] - f[i - (c+1) * x ] + mod ) % mod;
            }
            f.swap(g);
        }
        int res = 0;
        for( int i = l ; i <= r ; i ++ )
            res = (res + f[i])  % mod;
        return res;
    }
};

标签:cnt,int,res,back,双周,115,vector,words,LeetCode
From: https://www.cnblogs.com/PHarr/p/17828138.html

相关文章

  • 第 117 场双周赛(容斥原理,记忆化搜索,排序)
     本题我们采用隔板法+容斥原理来解决合格总方案数=总方案书-不合理的方案数=不考虑limit的方案数-不合法方案数(至少有一个小朋友>limit)任意方案数n个小球放到3个盒子中->n+2个位置,选两个位置放隔板剩下位置放球c(n+2,2)三个小朋友为:甲乙丙小朋友甲(乙丙)>l......
  • [LeetCode] 715. Range 模块
    题目Range模块是跟踪数字范围的模块。设计一个数据结构来跟踪表示为半开区间的范围并查询它们。半开区间[left,right)表示所有left<=x<right的实数x。实现RangeModule类:RangeModule()初始化数据结构的对象。voidaddRange(intleft,intright)添加半开区......
  • KubeSphere 社区双周报 | KubeSphere 3.4.1 发布 | 2023.10.27-11.09
    KubeSphere社区双周报主要整理展示新增的贡献者名单和证书、新增的讲师证书以及两周内提交过commit的贡献者,并对近期重要的PR进行解析,同时还包含了线上/线下活动和布道推广等一系列社区动态。本次双周报涵盖时间为:2023.10.27-2023.11.09。贡献者名单新晋KubeSphereCon......
  • leetcode hot100-02 字母异位词分组
    题目:字母异位词分组难度:中等地址:https://leetcode.cn/classic/problems/group-anagrams/description/描述:给你一个字符串数组,请你将 字母异位词 组合在一起。可以按任意顺序返回结果列表。字母异位词 是由重新排列源单词的所有字母得到的一个新单词。过程:1、首先啥叫......
  • leetcode hot 100-01 两数之和
    题目:两数之和难度:简单题目地址:https://leetcode.cn/classic/problems/two-sum/description/过程一,因为难度是简单,就没有仔细审题,以为返回两个数就好,使用双指针,逻辑如下:对数组排序双指针分别指向头和尾两数之和大于target,尾部指针-1两数之......
  • 第117场双周赛-3min签到题,然后做不了一点
     给你两个正整数 n 和 limit 。请你将 n 颗糖果分给 3 位小朋友,确保没有任何小朋友得到超过 limit 颗糖果,请你返回满足此条件下的 总方案数 。 示例1:输入:n=5,limit=2输出:3解释:总共有3种方法分配5颗糖果,且每位小朋友的糖果数不超过2:(1,2,2),(2......
  • Leetcode133.克隆图
     需要注意图中存在环路。JAVA:publicfinalNodecloneGraph(Nodenode){returndeepCopy(node,newHashMap<Integer,Node>());}privateNodedeepCopy(Nodenode,HashMap<Integer,Node>hisMap){if(null==node)return......
  • #yyds干货盘点# LeetCode程序员面试金典:累加数
    题目累加数是一个字符串,组成它的数字可以形成累加序列。一个有效的累加序列必须至少包含3个数。除了最开始的两个数以外,序列中的每个后续数字必须是它之前两个数字之和。给你一个只包含数字'0'-'9'的字符串,编写一个算法来判断给定输入是否是累加数。如果是,返回true......
  • #yyds干货盘点# LeetCode程序员面试金典:供暖器
    题目冬季已经来临。你的任务是设计一个有固定加热半径的供暖器向所有房屋供暖。在加热器的加热半径范围内的每个房屋都可以获得供暖。现在,给出位于一条水平线上的房屋houses和供暖器heaters的位置,请你找出并返回可以覆盖所有房屋的最小加热半径。注意:所有供暖器heaters......
  • LeetCode-88题合并两个有序数组
    给你两个按 非递减顺序 排列的整数数组 nums1 和 nums2,另有两个整数 m 和 n ,分别表示 nums1 和 nums2 中的元素数目。请你 合并 nums2 到 nums1 中,使合并后的数组同样按 非递减顺序 排列。注意:最终,合并后数组不应由函数返回,而是存储在数组 nums1 中。为了应......