【LeetCode第 332 场周赛】

简介: 笔记

6354. 找出数组的串联值


题目

4.png

思路

前后指针


代码

class Solution {
public:
    long long findTheArrayConcVal(vector<int>& nums) {
        long long res = 0;
        int n = nums.size();
        for (int i = 0, j = n - 1; i < j; i++, j--) {
            res += stol(to_string(nums[i]) + to_string(nums[j]));
        }
        if (n & 1) res += nums[n / 2];
        return res; 
    }
};

6355. 统计公平数对的数目


题目

5.png


思路


6.png


代码

class Solution {
public:
    using ll = long long;
    static constexpr ll N = 2e9 + 7;
    class BIT {
    public:
        BIT() {}
        unordered_map<ll, int> tr;
        void add(ll x, int v = 1) { for (; x < N << 1; x += x & -x) tr[x] += v; }
        ll sum(ll x) { ll res = 0; for (; x; x -= x & -x) res += tr[x]; return res; }
    }; // 切记用 BIT tr; tr.add(); tr.sum();
    long long countFairPairs(vector<int>& a, int L, int R) {
        ll res = 0;
        BIT tree;
        for (int i = a.size() - 1; i >= 0; i--) {
            res += tree.sum(R - a[i] + N) - tree.sum(L - a[i] - 1 + N);
            tree.add(a[i] + N);
        }
        return res;
    }
};

6356. 子字符串异或查询


题目

7.png

思路

映射数值对应的最小开始下标即可,数值在1e9,并且无前导 ‘0’,所以只需要 O(30n)。


代码

class Solution {
public:
    vector<vector<int>> substringXorQueries(string s, vector<vector<int>>& q) {
        vector<vector<int>> res;
        map<int, int> mp;
        int mn = 1e9;
        for (int i = 0; i < s.size(); i++) {
            int x = 0;
            for (int j = 0; j < 31 && i + j < s.size(); j++) {
                if (s[i + j] - '0' == 0 && x == 0) {
                    mn = min(mn, i);
                    break;
                }
                x = x * 2 + s[i + j] - '0';
                if (mp.count(x)) {
                    mp[x] = min(mp[x], i);
                } else {
                    mp[x] = i;
                }
            }
        }
        for (int i = 0; i < q.size(); i++) {
            int x = q[i][0] ^ q[i][1];
            int y = x;
            int cnt = 0;
            while (y) { cnt++; y /= 2; }
            vector<int> v{-1, -1};
            if (cnt == 0 && mn != 1e9) {
                v[0] = mn;
                v[1] = mn;
            }
            if (mp.count(x)) {
                v[0] = mp[x];
                v[1] = v[0] + cnt - 1;
            }
            res.push_back(v);
        }
        return res;
    }
};

6357. 最少得分子序列


题目

9.png


思路


维护前后缀数组,l[i],r[i]:表示字符串 s的前 i位子序列在字符串 t中匹配的最长前缀。r同理最长后缀。


代码

class Solution {
public:
    int minimumScore(string s, string t) {
        int n = s.size(); 
        vector<int> l(n, 0), r(n, 0); 
        int j = 0;
        for (int i = 0; i < s.size(); i++) {
            if (j < t.size() && s[i] == t[j]) ++j;
            l[i] = j;
        }
        j = t.size() - 1;
        for (int i = s.size() - 1; i >= 0; i--) {
            if (j >= 0 && s[i] == t[j]) --j;
            r[i] = t.size() - j - 1;
        }
        int res = 1e9; 
        for (int i = 0; i < n - 1; i++) {
            int L = l[i], R = r[i + 1];
            if (L + R >= t.size()) return 0; // 都能匹配上,说明t本就是s的子序列
            res = min(res, (int)((t.size() - R - 1) - (L) + 1));
        }
        res = min(res, (int)t.size() - r[0]);
        res = min(res, (int)t.size() - l[n - 1]);
        return res;
    }
};



相关文章
|
1月前
|
Go
golang力扣leetcode 第 291 场周赛
golang力扣leetcode 第 291 场周赛
43 0
|
1月前
|
Go vr&ar
golang力扣leetcode 第 288 场周赛
golang力扣leetcode 第 288 场周赛
39 0
|
1月前
|
Go
golang力扣leetcode 第 286 场周赛
golang力扣leetcode 第 286 场周赛
42 0
|
1月前
|
Go
golang力扣leetcode第 294 场周赛
golang力扣leetcode第 294 场周赛
39 0
|
1月前
|
算法 Java Go
golang力扣leetcode 第 293 场周赛
golang力扣leetcode 第 293 场周赛
63 0
|
1月前
|
Go
golang力扣leetcode 第 290 场周赛
golang力扣leetcode 第 290 场周赛
32 0
|
1月前
|
Go C++
golang力扣leetcode 第 284 场周赛
golang力扣leetcode 第 284 场周赛
44 0
|
8月前
|
算法 Android开发
LeetCode 周赛上分之旅 #48 一道简单的树上动态规划问题
学习数据结构与算法的关键在于掌握问题背后的算法思维框架,你的思考越抽象,它能覆盖的问题域就越广,理解难度也更复杂。在这个专栏里,小彭与你分享每场 LeetCode 周赛的解题报告,一起体会上分之旅。
51 1
|
1月前
|
存储
Leetcode第383场周赛
在LeetCode第383场周赛中,选手完成了3道题目。第一题是关于边界上的蚂蚁,蚂蚁根据非零整数数组nums的值移动,返回蚂蚁返回边界上的次数。解题方法是计算数组累加和为0的次数。第二题涉及计算网格的区域平均强度,给定一个灰度图像和阈值,返回每个像素所属区域的平均强度。解题关键在于理解相邻像素和区域定义,并计算平均强度。第三题是恢复单词初始状态的最短时间问题,通过移除前k个字符并添加k个字符,求恢复原词所需的最短时间。解题策略是检查去除前k个字符后的子串是否能作为原词的前缀。
15 1
Leetcode第383场周赛
|
1月前
|
存储
Leetcode第382场周赛
```markdown 给定字符串`s`,计算按键变更次数,即使用不同键的次数,不考虑大小写差异。例如,`&quot;aAbBcC&quot;`变更了2次。函数`countKeyChanges`实现此功能。另外,求满足特定模式子集最大元素数,`maximumLength`函数使用`TreeMap`统计次数,枚举并构建子集,返回最大长度。最后,Alice和Bob玩鲜花游戏,Alice要赢需满足鲜花总数奇数、顺时针在[1,n]、逆时针在[1,m],返回满足条件的(x, y)对数,可通过奇偶性分类讨论求解。 ```
19 1