首页 > 其他分享 >第 319 场周赛

第 319 场周赛

时间:2023-01-12 22:36:10浏览次数:54  
标签:tmp 周赛 319 right TreeNode int long ans

1 温度转换

温度转换

Solution

class Solution {
public:
    vector<double> convertTemperature(double celsius) {
        double kelvin = celsius + 273.15;
        double fahrenheit = celsius * 1.80 + 32.00;
        vector<double> ans;
        ans.push_back(kelvin);
        ans.push_back(fahrenheit);
        return ans;
    }
};

2 最小公倍数为 K 的子数组数目

最小公倍数为 K 的子数组数目

Solution

class Solution {
public:
    long long gcd(long long a,long long b)
    {
        return b > 0 ? gcd(b,a % b) : a;
    }
    long long lcm(long long a,long long b){
            return a / gcd(a, b) * b;
    }
    int subarrayLCM(vector<int>& nums, int k) {
        int n = nums.size();
        long long ans = 0;
        for(int i = 0;i < n;i++){
            long long tmp = 1;
            for(int j = i;j < n;j++){
                tmp = lcm(tmp, nums[j]);
                if(k % tmp) break;
                if(tmp == k) ans++;
            }
        }
        return ans;
    }
};

3 逐层排序二叉树所需的最少操作数目

逐层排序二叉树所需的最少操作数目

Solution

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int minimumOperations(TreeNode* root) {
        //层次遍历
        int ans = 0;
        queue<TreeNode*> q;
        q.push(root);
        while(!q.empty()){
            int len = q.size();
            //tmp存该层数据 
            vector<int> tmp(len);
            //开始存数据
            for(int i = 0;i < len;i++){
                auto x = q.front();
                tmp[i] = x->val;
                q.pop();
                if(x->left){
                    q.push(x->left);
                }
                if(x->right){
                    q.push(x->right);
                }
            }
            //置换环
            pair<int, int> arrpos[len];//一维记录数值,一维记录位置
            for(int i = 0;i < len;i++){
                arrpos[i].first = tmp[i];
                arrpos[i].second = i;
            }
            //跑次数
            sort(arrpos,arrpos + len);
            vector<int> vis(len,0);
            for(int i = 0;i < len;i++){
                int cycelsize = 0;
                //自环或者访问过
                if(vis[i] || arrpos[i].second == i) continue;
                int j = i;
                while(!vis[j]){
                    vis[j] = 1;
                    j = arrpos[j].second;
                    cycelsize += 1;
                }
                if(cycelsize > 0){
                    ans += (cycelsize - 1);
                }
            }
        }
        return ans;
    }
};

4 不重叠回文子字符串的最大数目

不重叠回文子字符串的最大数目

Solution

class Solution {
public:
    int maxPalindromes(string s, int k) {
        int n = s.size(), f[n+1];
        memset(f, 0, sizeof(f));
        for(int i = 0;i < 2 * n - 1;i++){
            int l = i / 2, r = l + i % 2;
            f[l + 1] = max(f[l + 1], f[l]);
            for(; l >= 0 && r < n && s[l] == s[r]; --l, ++r){
                if(r + 1 - l >= k){
                    f[r + 1] = max(f[r + 1], f[l] + 1);
                    break;
                }
            }
        }
        return f[n];
    }
};

标签:tmp,周赛,319,right,TreeNode,int,long,ans
From: https://www.cnblogs.com/TTS-TTS/p/17048117.html

相关文章

  • LeetCode刷题(59)~使数组中所有元素相等的最小操作数【第202场周赛:题目二】
    题目描述存在一个长度为n的数组arr,其中arr[i]=(2*i)+1(0<=i<n)。一次操作中,你可以选出两个下标,记作x和y(0<=x,y<n)并使arr[x]减去1、arr[y]......
  • 第 320 场周赛
    1.数组中不等三元组的数目数组中不等三元组的数目SolutionclassSolution{public:intunequalTriplets(vector<int>&nums){intn=nums.size();......
  • 第 95 场双周赛
    1.根据规则将箱子分类根据规则将箱子分类SolutionclassSolution{public:stringcategorizeBox(intlength,intwidth,intheight,intmass){lon......
  • AcWing杯 第85场周赛
    4791.死或生#include<bits/stdc++.h>#defineintlonglongusingnamespacestd;intn,ax,ay,bx,by;int32_tmain(){cin>>n;for(intt,x,......
  • LeetCode_单周赛_327
    目录6283.正整数和负整数的最大计数代码6285.执行K次操作后的最大分数代码6284.使字符串总不同字符的数目相等代码6283.正整数和负整数的最大计数代码直接遍历统......
  • AcWing第85场周赛
    这场周赛是手速局hh死或生某国正在以投票的方式决定2名死刑犯(编号1∼2)的生死。共有n组人员(编号1∼n)参与投票,每组10人。每组成员只参与一名死刑犯的投票,其中第......
  • 周赛题目证明+at数学题
    题目链接:https://www.acwing.com/problem/content/4795/比赛的时候感觉插入到最后面是最大的,当时只是感觉并没严格证明,y总说比赛的时候可以不用严格证明,赛后要这样做,这样......
  • AcWing杯 第 84 场周赛
    A.最大数量签到,用了结构化绑定#include<bits/stdc++.h>#defineintlonglongusingnamespacestd;intread(){...}int32_tmain(){intn=read();map......
  • Acwing第 85 场周赛 ABC
    https://www.acwing.com/activity/content/2755/4791.死或生题目大意:给定n组(10个人)对2个犯人(编号1,2)的生死评价,总数:生>=死,活下来,否则嘎了输入样例1:2155264输......
  • 第 321 场周赛
    1.找出中枢整数找出中枢整数SolutionclassSolution{public:intpivotInteger(intn){intsum=(1+n)*n/2;inttmp=(int)sqrt(sum......