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

leetcode 第90场双周赛

时间:2022-10-30 16:22:04浏览次数:43  
标签:space int res nums 双周 x% vector 90 leetcode

6226. 摧毁一系列目标

题意:对于数组中每一个数nums[i],可以摧毁数组中值等于nums[i]+c*space的数(c为非负整数),求摧毁最大数量时的最小nums[i]

思路:如果两个数x,y可以同时被摧毁,则x%space == y%space,用map统计同一类的数量,遍历数组求得最小值。

点击查看代码
class Solution { 
public:
    int destroyTargets(vector<int>& nums, int space) {
        map<int,int>mp;
        for(auto x:nums){
            mp[x%space]++;
        }
        map<int,int>::iterator iter;
        int ans = 1e9,res = 0;
        for(auto x:nums){
            if(mp[x%space]>res) {
                res = mp[x%space];
                ans = x;
            }
            else if(mp[x%space] == res && x<ans) ans = x;
        }
        return ans;
    }
};

6227. 下一个更大元素 IV

题意:对于数组中的每一个元素,返回大于它的第二个元素的值,若不能找到,返回为-1。

思路1:利用两个单调栈,一个栈存储已经找到一个大于的元素,一个存储还没有找到大于的数。然后对已存储的与nums[i]比较,得出结果。

点击查看代码
class Solution {
public:
    vector<int> secondGreaterElement(vector<int>& nums) {
         priority_queue<pair<int, int>, vector<pair<int,int>>, greater<pair<int,int>>> q;
         stack<int>st;
         int n = nums.size();
         vector<int>ans(n,-1);
         for(int i = 0;i<n;i++) {
             while(!q.empty() && q.top().first < nums[i]){
                ans[q.top().second] = nums[i];
                q.pop();
             }
             while(st.size() && nums[st.top()] < nums[i]){
                 q.push({nums[st.top()],st.top()});
                 st.pop();
             }
             st.push(i);
         }
         return ans;
    }
};

思路2:对-nums[i]当作第一关键字序排序,然后将对应的下标i依次入栈,如果能找到两个大于i的下标,则存在,反之则不存在。

点击查看代码
class Solution {
public:
    vector<int> secondGreaterElement(vector<int>& nums) {
         int n = nums.size();
         typedef pair<int,int> pii;
         vector<pii>vec;
         vector<int>ans(n);
         set<int>tmp;
         for(int i=0;i<n;i++){
             vec.push_back({-nums[i],i});
         }
         sort(vec.begin(),vec.end());
         for(int i=0;i<n;i++){
             auto x = tmp.upper_bound(vec[i].second);
             if(x != tmp.end() && next(x) != tmp.end()) ans[vec[i].second] = nums[*next(x)];
             else ans[vec[i].second] = -1;
             tmp.insert(vec[i].second);
         }
         return ans;
    }
};

标签:space,int,res,nums,双周,x%,vector,90,leetcode
From: https://www.cnblogs.com/voids5/p/16841544.html

相关文章

  • 【XSY3905】字符串题(lyndon串,构造)
    题面字符串题题解设所有长度不超过\(n\)的串的集合为\(S\)。考虑找到一种方法,能够对一个lyndon串\(A\),直接求出\(A\)的下一个lyndon串。方法如下:先将\(A......
  • 【XSY3904】直线(分块)
    题面直线题解注意到题目没有给什么特殊的性质,除了直线随机生成。所以考虑随机化算法或均摊算法。有一种很神奇的分块做法:考虑将整个平面分成\(B\timesB\)块,每块大......
  • 【XSY3890】【hdu5263】平衡大师(二分,上下界网络流)
    不妨令\(k=m-k\),那么题目的意思就是至多删去\(k\)条边。首先二分答案\(t\),然后求最少需要删去多少的边,如果最少需要删去的边\(\leqk\)则合法。在原图中统计每一个......
  • leetcode103-二叉树的锯齿形层序遍历
    103.二叉树的锯齿形层序遍历用两个栈来实现。先把根节点放入第一个栈。循环内部根据哪个栈为空判断新的节点放到哪个栈,确定先左还是先右。当两个栈都为空时,循环结束。......
  • LeetCode 2458. Height of Binary Tree After Subtree Removal Queries
    原题链接在这里:https://leetcode.com/problems/height-of-binary-tree-after-subtree-removal-queries/题目:Youaregiventhe root ofa binarytree with n node......
  • leetcode-1790-easy
    CheckifOneStringSwapCanMakeStringsEqualYouaregiventwostringss1ands2ofequallength.Astringswapisanoperationwhereyouchoosetwoindices......
  • 【XSY3490】线段树(广义线段树,树上莫队)
    题面线段树题解本题分两Part走。Part1我们需要解决:如何在广义线段树上快速区间定位节点。对于有\(n\)个叶子节点、共\(2n-1\)个节点的广义线段树\(A\),我们......
  • leetcode-2160-easy
    MinimumSumofFourDigitNumberAfterSplittingDigitsYouaregivenapositiveintegernumconsistingofexactlyfourdigits.Splitnumintotwonewintegers......
  • leetcode-1748-easy
    SumofUniqueElementsYouaregivenanintegerarraynums.Theuniqueelementsofanarrayaretheelementsthatappearexactlyonceinthearray.Returnthe......
  • leetcode102-二叉树的层序遍历
    102.二叉树的层序遍历有两种实现方法。第一种是递归,第二种是队列实现。第一种是看了别人的代码写出来的,第二种是自己写的。这道题的不能直接把遍历得到的数字直接塞进res......