Leetcode 362. wöchentliche Wettbewerbsproblemlösung

Leetcode 362. wöchentliche Wettbewerbsproblemlösung

Thema 1: 2848. Schnittpunkt mit dem Auto

Ideen

Hash.

Code

/*
 * @lc app=leetcode.cn id=2848 lang=cpp
 *
 * [2848] 与车相交的点
 */

// @lc code=start
class Solution
{
    
    
public:
    int numberOfPoints(vector<vector<int>> &nums)
    {
    
    
        vector<bool> seat(101, false);
        for (const vector<int> &num : nums)
        {
    
    
            int start = num[0], end = num[1];
            for (int i = start; i <= end; i++)
                seat[i] = true;
        }
        int count = 0;
        for (int i = 1; i <= 100; i++)
            if (seat[i])
                count++;
        return count;
    }
};
// @lc code=end

Komplexitätsanalyse

Zeitkomplexität: O(n), wobei n die Länge der Array-Nummern ist.

Raumkomplexität: O(L), die Länge des Hilfsarrays, L = 100 entsprechend der Frage.

Frage 2: 2849. Bestimmen Sie, ob die Zelle zu einem bestimmten Zeitpunkt erreichbar ist

Ideen

Rätsel.

Habe ein paar gierige Gedanken.

Code

class Solution
{
    
    
public:
    bool isReachableAtTime(int sx, int sy, int fx, int fy, int t)
    {
    
    
        if (t == 1 && sx == fx && sy == fy)
            return false;
        return abs(sx - fx) <= t && abs(sy - fy) <= t;
    }
};

Komplexitätsanalyse

Zeitkomplexität: O(1).

Raumkomplexität: O(1), keine Hilfsvariablen.

Frage 3: 2850. Mindestanzahl an Zügen, um Steine ​​auf dem Gitter zu verteilen

Ideen

Zählen Sie alle Anordnungen gewaltsam auf, berechnen Sie jedes Mal eine Manhattan-Distanz und aktualisieren Sie den Mindestwert als Mindestanzahl an Zügen.

Code

/*
 * @lc app=leetcode.cn id=2850 lang=cpp
 *
 * [2850] 将石头分散到网格图的最少移动次数
 */

// @lc code=start
class Solution
{
    
    
public:
    int minimumMoves(vector<vector<int>> &grid)
    {
    
    
        int m = grid.size(), n = m ? grid[0].size() : 0; // m = n = 3
        // 所有移走的石子个数 = 所有移入的石子个数(grid[i][j] = 0)
        vector<pair<int, int>> from; // 移走石子坐标数组
        vector<pair<int, int>> to;   // 移入石子坐标数组
        // 构建 from 和 to 数组
        for (int i = 0; i < 3; i++)
            for (int j = 0; j < 3; j++)
            {
    
    
                if (grid[i][j] > 1)
                {
    
    
                    // 有 grid[i][j] - 1 个可以移走的石子
                    for (int k = 0; k < grid[i][j] - 1; k++)
                        from.push_back(make_pair(i, j));
                }
                else if (grid[i][j] == 0)
                    to.push_back(make_pair(i, j));
            }
        // 枚举 from 的全部排列可能,与 to 匹配,求 from[i] 和 to[i] 的曼哈顿距离之和,最小值即为答案
        int minCount = __INT_MAX__; // 最少移动次数
        // 使用 next_permutation 枚举全排列必须先对数组进行排序
        sort(from.begin(), from.end());
        do
        {
    
    
            int count = 0;
            for (int i = 0; i < from.size(); i++)
            {
    
    
                // 计算曼哈顿距离
                count += abs(from[i].first - to[i].first) + abs(from[i].second - to[i].second);
            }
            minCount = min(minCount, count); // 更新答案
        } while (next_permutation(from.begin(), from.end()));
        return minCount;
    }
};
// @lc code=end

Komplexitätsanalyse

Zeitkomplexität: O(m×n×(m×n)!). Die Zeitkomplexität der Verwendung der STL-Funktion next_permutation für die vollständige Permutation beträgt O((m×n)!). Die Zeit für eine einzelne Berechnung der Manhattan-Distanz wird innerhalb der Schleife berechnet. Die Komplexität beträgt O(m×n), wobei m und n die Länge bzw. Breite des Matrixgürtels sind, m = n = 3.

Raumkomplexität: O(mn), das ist der Raum der Hilfsarrays von und nach, wobei m und n die Länge bzw. Breite des Matrixgürtels sind, m = n = 3.

Thema 4: 2851. String-Konvertierung

Über die Möglichkeiten hinaus.

Ideen

Matrix-Schnellleistungsoptimierung DP (Matrix-Schnellleistung + dynamische Programmierung + KMP)

Video-Erklärung:

https://www.bilibili.com/video/BV1U34y1N7Pe/?vd_source=df165d34990cd0aa2cacb2c452e99aad

Code

/*
 * @lc app=leetcode.cn id=2851 lang=cpp
 *
 * [2851] 字符串转换
 */

// @lc code=start

// 矩阵快速幂优化 DP

class Solution
{
    
    
public:
    int numberOfWays(string s, string t, long long k)
    {
    
    
        int n = s.size();
        int c = kmp_search(s + s.substr(0, n - 1), t);
        vector<vector<long long>> m = {
    
    
            {
    
    c - 1, c},
            {
    
    n - c, n - 1 - c}};
        m = pow(m, k);
        return m[0][s != t];
    }

private:
    // KMP 模板
    vector<int> calc_max_match(string s)
    {
    
    
        vector<int> match(s.size());
        int c = 0;
        for (int i = 1; i < s.size(); i++)
        {
    
    
            char v = s[i];
            while (c && s[c] != v)
                c = match[c - 1];
            if (s[c] == v)
                c++;
            match[i] = c;
        }
        return match;
    }

    // KMP 模板
    // 返回 text 中出现了多少次 pattern(允许 pattern 重叠)
    int kmp_search(string text, string pattern)
    {
    
    
        vector<int> match = calc_max_match(pattern);
        int match_cnt = 0, c = 0;
        for (int i = 0; i < text.size(); i++)
        {
    
    
            char v = text[i];
            while (c && pattern[c] != v)
                c = match[c - 1];
            if (pattern[c] == v)
                c++;
            if (c == pattern.size())
            {
    
    
                match_cnt++;
                c = match[c - 1];
            }
        }
        return match_cnt;
    }

    const long long MOD = 1e9 + 7;

    // 矩阵乘法
    vector<vector<long long>> multiply(vector<vector<long long>> &a, vector<vector<long long>> &b)
    {
    
    
        vector<vector<long long>> c(2, vector<long long>(2));
        for (int i = 0; i < 2; i++)
            for (int j = 0; j < 2; j++)
                c[i][j] = (a[i][0] * b[0][j] + a[i][1] * b[1][j]) % MOD;
        return c;
    }

    // 矩阵快速幂
    vector<vector<long long>> pow(vector<vector<long long>> &a, long long n)
    {
    
    
        vector<vector<long long>> res = {
    
    {
    
    1, 0}, {
    
    0, 1}};
        for (; n; n /= 2)
        {
    
    
            if (n % 2)
                res = multiply(res, a);
            a = multiply(a, a);
        }
        return res;
    }
};
// @lc code=end

Komplexitätsanalyse

Zeitkomplexität: O(n+logk), wobei n die Länge der Zeichenfolge s ist.

Raumkomplexität: O(n), wobei n die Länge der Zeichenfolge s ist.

Guess you like

Origin blog.csdn.net/ProgramNovice/article/details/132817373