LeetCode 316 && AcWing 74 周赛总结

一、LeetCode 316 场

1、6214. 判断两个事件是否存在冲突

(1)原题链接:力扣https://leetcode.cn/contest/weekly-contest-316/problems/determine-if-two-events-have-conflict/

(2)解题思路:

        把一天的时间看作是从 0 分钟 到 24 * 60 分钟,然后判断两个区间是否存在交集即可。

(3)参考代码:

class Solution {
public:
    bool haveConflict(vector& event1, vector& event2) {
        int t1 = 0, t2 = 0;
        int e1 = 0, e2 = 0;
        
        string s1 = event1[0];
        string s2 = event1[1];
        t1 = (s1[0] - 48) * 10 * 60 + (s1[1] - 48) * 60 + (s1[3] - 48) * 10 + (s1[4] - 48);
        e1 = (s2[0] - 48) * 10 * 60 + (s2[1] - 48) * 60 + (s2[3] - 48) * 10 + (s2[4] - 48);
        
        string s3 = event2[0];
        string s4 = event2[1];
        t2 = (s3[0] - 48) * 10 * 60 + (s3[1] - 48) * 60 + (s3[3] - 48) * 10 + (s3[4] - 48);
        e2 = (s4[0] - 48) * 10 * 60 + (s4[1] - 48) * 60 + (s4[3] - 48) * 10 + (s4[4] - 48);
        
        if((e1 < t2) || (t1 > e2)) return false;
        return true;
    }
};

2、6224.最大公因数等于K的子数组数目

(1)原题链接:力扣https://leetcode.cn/contest/weekly-contest-316/problems/number-of-subarrays-with-gcd-equal-to-k/

(2)解题思路:

        1、先从前往后遍历一遍找到等于K的数,加入到答案res中。(一个数的最大公约数就等于其本身)

        2、以每个数为起点,往后判断当前数和下一个数的最大公因数tmp是否等于K(若相等则答案res加1),再判断tmp与下下个数的最大公因数是否为K(若相等则答案res加1),再以此类推。

(3)参考代码:

class Solution {
public:
    int gcd(int a, int b) 
    {
        return b ? gcd(b, a % b) : a;
    }
    int subarrayGCD(vector& nums, int k) {
        int res = 0;
        
        int n = nums.size();
        for(int i = 0; i < n; i ++ ) if(nums[i] == k) res ++;
        
        for(int i = 0; i < n; i ++ ) {
            int j = i;
            int tmp = nums[i];
            for(int l = j + 1; l < n; l ++ ) {
                tmp = gcd(nums[l], tmp);
                if(tmp == k) res ++;
            }    
        }
        return res;
    }
};

3、2448.使数组相等的最小开销

(1)原题链接:力扣icon-default.png?t=M85Bhttps://leetcode.cn/problems/minimum-cost-to-make-array-equal/

(2)解题思路:

        1、把某个数的移动的开销看作是存在有cost[i] 个 nums[i] ;

        2、找到cost数组的中位数;

        3、每个数都转变为中位数,且此时的总开销是最小的(证明:略)。

(3)参考代码:

typedef long long ll;
class Solution {
public:
    long long minCost(vector& nums, vector& cost) {
        vector > x;
        for(int i = 0; i < nums.size(); i ++ ){
            x.emplace_back(pair(nums[i], cost[i]));
        }
        sort(x.begin(), x.end(),[&](pair a, pair b){
            return a.first= mid){
                midnum = z.first;
                break;
            }
            else{
                sum += z.second;
            }
        }

        ll ans = 0;
        for(auto z: x){
            ans += ((ll) abs(z.first - midnum))*(ll)z.second;
        }

        return ans;
    }
};

 

4、2449.使数组相似的最少操作次数

(1)原题链接:力扣icon-default.png?t=M85Bhttps://leetcode.cn/problems/minimum-number-of-operations-to-make-arrays-similar/

(2)解题思路:

        1、先把题目 【+2-2】 的操作看作是【+1-1】的操作,简化一下;

        2、由于【+2-2】操作不会改变奇偶性,可以把【+2-2】操作进行【%2】分组;

        3、然后一一匹配,小的数和小的数匹配,大的数和大的数匹配。

        4、最后得到的答案需要除以4,因为每次操作的变化量都是4。

(3)参考代码:

class Solution {
public:
    long long makeSimilar(vector& nums, vector& target) {
        sort(nums.begin(), nums.end());
        sort(target.begin(), target.end());

        long long ans = 0;
        int js[2]{};
        for(int x: nums) {
            int p = x % 2, &j = js[p];
            while(target[j] % 2 != p) j ++;
            ans += abs(x - target[j ++]);
        }

        return ans / 4;
    }
};

二、AcWing 74 场

1、4707.最小身高差

(1)原题链接:4707. 最小身高差 - AcWing题库

(2)解题思路:

        直接枚举就行了。

(3)参考代码:

#include 
#include 
#include 
#include 

using namespace std;

const int N = 110;

int n;
int a[N];

int main()
{
    cin >> n;
    
    for(int i = 0; i < n; i ++ ) cin >> a[i];
    
    int res = min(INT_MAX, abs(a[0] - a[n - 1]));
    for(int i = 1; i < n; i ++ ) {
        res = min(res, abs(a[i] - a[i - 1]));
    }
    cout << res << endl;
    return 0;
}

2、4708.立方体

(1)原题链接:4708. 立方体 - AcWing题库

(2)解题重点:

        1、三维数组用利用结构体存储到队列中。

        2、六个方向的bfs()的实现。

(3)参考代码:

#include 
#include 
#include 
#include 

using namespace std;

const int N = 15;

struct point
{
    int x, y, z;
    point() {}
    point(int a, int b, int c) {
        x = a, y = b, z = c;
    }
};

int k, n, m;
int x, y;
char g[N][N][N];
bool st[N][N][N];
queue q;

int dx[] = {0, 0, 0, 0, 1, -1}, dy[] = {1, -1, 0, 0, 0, 0}, dz[] = {0, 0, 1, -1, 0, 0};

int bfs()
{
   int res = 0;
   q.push(point(1, x ,y));
   st[1][x][y] = true;
   
   while(q.size()) {
        auto t = q.front();
        q.pop(); 
        res ++;
        
        for(int i = 0; i < 6; i ++ ) {
            int a = t.x + dx[i], b = t.y + dy[i], c = t.z + dz[i];
            
            if(a < 1 || a > k || b < 1 || b > n || c < 1 || c > m || g[a][b][c] == '#' || st[a][b][c]) continue;
            q.push(point(a, b, c));
            st[a][b][c] = true;
        }
   }
   
   return res;
}

int main()
{
    cin >> k >> n >> m;
    
    for(int i = 1; i <= k; i ++ ) 
        for(int j = 1; j <= n; j ++ ) 
            for(int l = 1; l <= m; l ++ ) 
                cin >> g[i][j][l];
 
    cin >> x >> y;
    
    cout << bfs() << endl;
    
    return 0;
}

你可能感兴趣的:(leetcode,算法,c++)