第 116 场 LeetCode 双周赛题解

news/2024/5/19 0:29:32 标签: leetcode, 枚举, 动态规划, 线段树

A leetcode.cn/problems/subarrays-distinct-element-sum-of-squares-i">子数组不同元素数目的平方和 I

在这里插入图片描述

枚举枚举子数组,用集合记录当前子数组中不同元素的个数

class Solution {
public:
    using ll = long long;

    int sumCounts(vector<int> &nums) {
        ll mod = 1e9 + 7;
        int n = nums.size();
        unordered_set<int> s;
        ll res = 0;
        for (int l = 0; l < n; l++) {
            for (int r = l; r < n; r++) {//子数组nums[l,r]
                s.insert(nums[r]);
                res += s.size() * s.size();
                res %= mod;
            }
            s.clear();
        }
        return (res + mod) % mod;
    }
};

B leetcode.cn/problems/minimum-number-of-changes-to-make-binary-string-beautiful">使二进制字符串变美丽的最少修改次数

在这里插入图片描述

动态规划:设 p [ i ] [ j ] p[i][j] p[i][j] 为将 s [ 0 , i − 1 ] s[0,i-1] s[0,i1] 修改为末尾为 j j j 的美丽字符串的最少修改次数, p [ i ] [ j ] p[i][j] p[i][j] 可由 p [ i − 2 ] [ 0 ] p[i-2][0] p[i2][0] p [ i − 2 ] [ 1 ] p[i-2][1] p[i2][1] 转移得到

class Solution {
public:
    int minChanges(string s) {
        int n = s.size();
        int p[n + 1][2];
        p[0][0] = 0;
        p[0][1] = 0;
        for (int i = 2; i <= n; i += 2) {
            p[i][0] = min(p[i - 2][0], p[i - 2][1]) + (s[i - 2] == '0' ? 0 : 1) + (s[i - 1] == '0' ? 0 : 1);
            p[i][1] = min(p[i - 2][0], p[i - 2][1]) + (s[i - 2] == '1' ? 0 : 1) + (s[i - 1] == '1' ? 0 : 1);
        }
        return min(p[n][0], p[n][1]);
    }
};

C leetcode.cn/problems/length-of-the-longest-subsequence-that-sums-to-target">和为目标值的最长子序列的长度

在这里插入图片描述

动态规划:设 p [ i ] [ j ] p[i][j] p[i][j] n u m s [ 0 , i − 1 ] nums[0,i-1] nums[0,i1] 的和为 j j j 的子序列的最长长度,有状态转移方程: p [ i + 1 ] [ j ] = m i n { p [ i ] [ j ] , p [ i ] [ j − n u m s [ i ] ] + 1 } p[i+1][j]=min\{p[i][j], p[i][j-nums[i]]+1 \} p[i+1][j]=min{p[i][j],p[i][jnums[i]]+1}

class Solution {
public:
    int lengthOfLongestSubsequence(vector<int> &nums, int target) {
        int n = nums.size();
        int p[n + 1][target + 1];
        for (int i = 0; i <= n; i++)
            for (int j = 0; j <= target; j++)
                p[i][j] = -1;//初始化为无效状态
        p[0][0] = 0;//空序列

        for (int i = 0; i < n; i++) {
            for (int j = 0; j <= target; j++) {
                p[i + 1][j] = p[i][j];
                if (j - nums[i] >= 0 && p[i][j - nums[i]] != -1)
                    p[i + 1][j] = max(p[i + 1][j], p[i][j - nums[i]] + 1);
            }
        }
        return p[n][target];
    }
};

D leetcode.cn/problems/subarrays-distinct-element-sum-of-squares-ii">子数组不同元素数目的平方和 II

在这里插入图片描述

线段树:遍历数组 n u m s [ i ] nums[i] nums[i] ,设 c [ j ] c[j] c[j] 为子数组 n u m s [ j , i ] nums[j,i] nums[j,i] 中不同元素数目,考虑子数组右端点从 i − 1 i-1 i1 移动到 i i i c c c 数组的变化:

  1. n u m s [ i ] nums[i] nums[i] 为该元素第一次出现,则 c c c 数组在 [ 0 , i ] [0,i] [0,i] 区间的元素 + 1 +1 +1
  2. n u m s [ i ] nums[i] nums[i] 不是该元素第一次出现,设最近的一次出现为位置为 l e f t [ i ] − 1 left[i]-1 left[i]1,则 c c c 数组在 [ l e f t [ i ] , i ] [left[i],i] [left[i],i] 区间的元素 + 1 +1 +1

可以通过实现支持 区间加 和 查询区间平方和 的线段树来求解,因为 ( a l + d ) 2 + ⋯ + ( a r + d ) 2 = ( a l 2 + ⋯ + a r 2 ) + 2 × d × ( a l + ⋯ + a r ) + d 2 × ( r − l + 1 ) (a_l+d)^2+\cdots +(a_r+d)^2 =(a_l^2+\cdots +a_r^2)+2\times d\times(a_l+\cdots +a_r)+d^2\times (r-l+1) (al+d)2++(ar+d)2=(al2++ar2)+2×d×(al++ar)+d2×(rl+1),所以线段树内部可以通过维护区间和和区间平方和来实现。

class SegmentTree {
public:
    typedef long long ll;

    inline void push_down(ll index) {
        st[index << 1].lazy = 1;
        st[index << 1 | 1].lazy = 1;
        st[index << 1].mark += st[index].mark;
        st[index << 1 | 1].mark += st[index].mark;

        st[index << 1].s2 += st[index << 1].s * st[index].mark * 2 + (st[index << 1].tr - st[index << 1].tl + 1) * st[index].mark * st[index].mark;
        st[index << 1].s += st[index].mark * (st[index << 1].tr - st[index << 1].tl + 1);
        st[index << 1 | 1].s2 += st[index << 1 | 1].s * st[index].mark * 2 + (st[index << 1 | 1].tr - st[index << 1 | 1].tl + 1) * st[index].mark * st[index].mark;
        st[index << 1 | 1].s += st[index].mark * (st[index << 1 | 1].tr - st[index << 1 | 1].tl + 1);

        st[index].lazy = 0;
        st[index].mark = 0;
    }

    inline void push_up(ll index) {
        st[index].s = st[index << 1].s + st[index << 1 | 1].s;
        st[index].s2 = st[index << 1].s2 + st[index << 1 | 1].s2;
    }

    SegmentTree(int n) {
        st = vector<SegmentTreeNode>(n * 4 + 10);
        build(1, n);
    }

    void build(ll l, ll r, ll index = 1) {
        st[index].tl = l;
        st[index].tr = r;
        st[index].lazy = 0;
        st[index].mark = 0;
        if (l == r) {
            st[index].s = 0;
            st[index].s2 = 0;
        } else {
            ll mid = (l + r) >> 1;
            build(l, mid, index << 1);
            build(mid + 1, r, index << 1 | 1);
            push_up(index);
        }
    }

    void add(ll l, ll r, ll d, ll index = 1) {
        if (l > st[index].tr or r < st[index].tl)
            return;
        else if (l <= st[index].tl and st[index].tr <= r) {
            st[index].s2 += st[index].s * d * 2 + (st[index].tr - st[index].tl + 1) * d;
            st[index].s += (st[index].tr - st[index].tl + 1) * d;
            st[index].mark += d;
            st[index].lazy = 1;
        } else {
            if (st[index].lazy)
                push_down(index);
            add(l, r, d, index << 1);
            add(l, r, d, index << 1 | 1);
            push_up(index);
        }
    }

    ll query(ll l, ll r, ll index = 1) {
        if (l <= st[index].tl and st[index].tr <= r) {
            return st[index].s2;
        } else {
            if (st[index].lazy)
                push_down(index);
            if (r <= st[index << 1].tr)
                return query(l, r, index << 1);
            else if (l > st[index << 1].tr)
                return query(l, r, index << 1 | 1);
            return query(l, r, index << 1) + query(l, r, index << 1 | 1);
        }
    }

private:
    struct SegmentTreeNode {
        ll tl;
        ll tr;
        ll s;//区间和
        ll s2;//区间平方和
        ll mark;
        int lazy;
    };
    vector<SegmentTreeNode> st;
};
class Solution {
public:
    using ll = long long;
    ll mod = 1e9 + 7;

    int sumCounts(vector<int> &nums) {
        int n = nums.size();
        unordered_map<int, int> vis;
        vector<int> left(n);
        for (int i = 0; i < n; i++) {
            if (vis.count(nums[i]))
                left[i] = vis[nums[i]] + 1;
            else
                left[i] = 0;
            vis[nums[i]] = i;
        }
        SegmentTree st(n);
        ll res = 0;
        for (int i = 0; i < n; i++) {
            st.add(left[i] + 1, i + 1, 1);
            res = (res + st.query(1, i + 1)) % mod;
        }
        return (res + mod) % mod;
    }
};

http://www.niftyadmin.cn/n/5132388.html

相关文章

mathtype怎么更改编号 mathtype章节编号错乱怎么办

mathtype作为一款功能强大的公式编辑器&#xff0c;使用范围广泛&#xff0c;与多款软件兼容。但新手可能会对mathtype的操作不熟悉&#xff0c;不知道如何在mathtype中更改编号&#xff0c;以及解决章节编号错乱问题。本文将围绕mathtype怎么更改编号&#xff0c;mathtype章节…

freeRTOS学习day5

测试demo 创建两个任务 任务一 按键按下 释放二值信号量 任务二获取 并打印执行次数 #include "sys.h" #include "delay.h" #include "usart.h" #include "led.h" #include "FreeRTOS.h" #include "task.h" #in…

linux下alias别名设置说明

1&#xff1a;alias别名设置说明 我们想将某个可执行程序&#xff0c;命名为其它名称&#xff1b;比如为python指定对应的python版本 给python39指定python版本 alias python3/home/du/Downloads/Python-3.9.9/pythonduubuntu:/root$ python39 -V Python 3.9.92&#xff1a;…

C++(20):constinit

C20增加了constinit关键字&#xff0c;用于修饰变量&#xff0c;表示该变量必须在编译期进行初始化&#xff0c;不能在运行期进行初始化。但是constinit变量并不是常量&#xff0c;可以在运行期被修改。 被声明为constinit的变量&#xff0c;只能通过常量&#xff0c;constexp…

[计算机提升] Windows设置

2.1 Windows设置 Windows设置提供了一个用户界面&#xff0c;用于更改和定制Windows操作系统的各种功能和选项。通过Windows设置&#xff0c;用户可以轻松访问和修改各种系统设置&#xff0c;包括个性化选项、网络和互联网设置、设备设置、应用程序安装和管理、隐私设置等。 以…

它真的来了,如何做到全网 60 万粉丝?

最近写了两个专栏&#xff0c;每个专栏都是 11 篇文章&#xff0c;讲述了我从 13 年年底到现在&#xff0c;十年做自媒体的经验和经历&#xff0c;其实分享我我做自媒体的一些方法&#xff0c;讲述了我做自媒体的一些感悟。 一个是&#xff1a;《从零开始叫你自媒体赚钱之法》。…

一致性hash负载均衡

Hash算法的问题 今天看下一致性hash&#xff0c;常见的负载均衡可能使用过hash&#xff0c;比如nginx中&#xff0c;如果使用session最简单就是通过hash&#xff0c;比如根据用户的请求ip进行hash&#xff0c;让不同用户的请求打到同一台服务器&#xff0c;这样状态处理起来最…

华为机试题:HJ7 取近似值

目录 第一章、算法题1.1&#xff09;题目描述1.2&#xff09;第一种解题思路与答案1.3&#xff09;第二种解题思路与答案1.4&#xff09;牛客链接 友情提醒: 先看文章目录&#xff0c;大致了解文章知识点结构&#xff0c;点击文章目录可直接跳转到文章指定位置。 第一章、算法…