当前位置: 首页 > news >正文

LeetCode --- 154双周赛

题目列表

3512. 使数组和能被 K 整除的最少操作次数
3513. 不同 XOR 三元组的数目 I
3514. 不同 XOR 三元组的数目 II
3515. 带权树中的最短路径

一、使数组能被 k 整除的最小操作次数

在这里插入图片描述
本题就是问你数组元素和减去几才能成为 k 的倍数,用 取模运算 即可,代码如下

// C++
class Solution {
public:int minOperations(vector<int>& nums, int k) {return accumulate(nums.begin(), nums.end(), 0) % k;}
};
# Python
class Solution:def minOperations(self, nums: List[int], k: int) -> int:return sum(nums) % k

二、不同 XOR 三元组的数目 I

在这里插入图片描述
本题,找规律(可以打个表看看)本题和给的数组的元素顺序并没有关系,只和选择哪三个数有关系

  • n = 1 n=1 n=1 时,只有 1,只能选 3个1,异或结果只有 1

  • n = 2 n=2 n=2 时,有 1和2,异或结果可以是 1、2,共 2

  • n ≥ 3 n\ge3 n3 时, [ 0 , 2 L ) [0,2^L) [0,2L) 中的任何数都能得到,共有 2 L 2^L 2L 个,其中 L = ⌊ l o g 2 n ⌋ + 1 L= \lfloor log_2n\rfloor+1 L=log2n+1,即 n n n 的二进制长度,解释如下

    • 可以选择 1、2、3 异或为 0

    • 可以选择 3 个相同的数,异或为它们本身,可以构成 [1,n] 中的任意一个数

    • 能组成多少个 ≥ n \ge n n 的数呢?

      • 异或的结果不可能超过 2 L − 1 2^L-1 2L1
      • 对于 [ n + 1 , 2 L − 1 ] [n+1,2^L-1] [n+1,2L1] 中的任意一个数 a a a,我们可以将其拆分为 2 L − 1 ⊕ b 2^{L-1}\oplus b 2L1b,再将 b b b 拆分为 c ⊕ 1 c\oplus 1 c1,即 a = 2 L − 1 ⊕ c ⊕ 1 a=2^{L-1}\oplus c\oplus 1 a=2L1c1,特殊的,当 a = 2 L − 1 + 1 a=2^{L-1}+1 a=2L1+1 时,此时 c = 0 c=0 c=0,但是 c ∈ [ 1 , n ] c\in[1,n] c[1,n],不能取 0 0 0,可以这样构造 a = 2 L − 1 ⊕ 2 ⊕ 3 a=2^{L-1}\oplus 2\oplus 3 a=2L123

代码如下

// C++
class Solution {
public:int uniqueXorTriplets(vector<int>& nums) {int n = nums.size();if(n <= 2) return n;return 1 << bit_width((unsigned)n);}
};
# Python
class Solution:def uniqueXorTriplets(self, nums: List[int]) -> int:n = len(nums)if n <= 2: return nreturn 1 << n.bit_length()

三、不同 XOR 三元组的数目 II

在这里插入图片描述

由于 1 <= nums[i] <= 1500,故异或的结果最多只有 2 ⌊ l o g 2 ( 1500 ) ⌋ + 1 = 2048 2^{ \lfloor log_2(1500) \rfloor+1}=2048 2log2(1500)⌋+1=2048,我们可以将暴力枚举的三重循环,变成两个两层循环,来进行模拟,代码如下

// C++
class Solution {
public:int uniqueXorTriplets(vector<int>& nums) {int n = nums.size();int mx = ranges::max(nums);int N = 1 << bit_width((unsigned)mx);vector<int> hash(N);// 计算两个数的异化for(int i = 0; i < n; i++){for(int j = i; j < n; j++){hash[nums[i] ^ nums[j]] = true;}}vector<int> hash3(N);// 计算3个数的异或for(int j = 0; j < N; j++){if(hash[j]){for(int i = 0; i < n; i++){hash3[nums[i] ^ j] = true;}}}return reduce(hash3.begin(), hash3.end());}
};
#Python
class Solution:def uniqueXorTriplets(self, nums: List[int]) -> int:N = 1 << (max(nums).bit_length() + 1)a = [False] * Nfor x in nums:for y in nums:a[x ^ y] = Trueb = [0] * Nfor xy, flag in enumerate(a):if flag:for z in nums:b[xy^z] = 1return sum(b)

四、带权树中的最短路径

在这里插入图片描述

本题既需要修改边的权重,同时又要计算结点到根的路径长度。一旦我们修改一条边的权重,那么这条边连接的子树的所有结点,到根结点的距离都会有相同的变化,但是放在一棵树上我们很难去维护,如何做?

  • 观察一棵树的先序遍历结果,我们会发现,同一个子树的结点在先序遍历中是连续的,如下图
    在这里插入图片描述

  • 对一个连续区间的加减操作,我们可以用 差分数组 来维护,同时由于题目还要求能快速查询结点到根节点的距离,我们可以将 差分数组树状数组 进行结合,就能快速进行修改和查询的工作了
    在这里插入图片描述

代码如下

// C++
class BIT{
public:BIT(int n): t(n + 1){}void update(int i, int val){while(i < t.size()){t[i] += val;i += i & -i;}}long long pre_sum(int i){long long res = 0;while(i){res += t[i];i -= i & -i;}return res;}
private:vector<long long> t;
};
class Solution {
public:vector<int> treeQueries(int n, vector<vector<int>>& edges, vector<vector<int>>& queries) {vector<vector<int>> g(n + 1);for(auto& e : edges){g[e[0]].push_back(e[1]);g[e[1]].push_back(e[0]);}// 用 [in[i], out[i]] 标记子树在先序遍历中的区间范围 vector<int> in(n + 1), out(n + 1);int clock = 0;auto dfs = [&](this auto&& dfs, int x, int fa)->void{in[x] = ++clock;for(int y : g[x]){if(y == fa) continue;dfs(y, x);}out[x] = clock;};dfs(1, 0);vector<int> w(n + 1);BIT t(n);auto update = [&](int x, int y, int val){if(in[x] < in[y]) swap(x, y); // 确认子树的根节点int d = val - w[x]; // 计算变化量w[x] = val; // 记录修改之后的值,方便后面计算变化量// 差分数组的维护方法t.update(in[x], d);t.update(out[x] + 1, -d);};for(auto & e : edges){update(e[0], e[1], e[2]);}vector<int> ans;for(auto& q : queries){if(q[0] == 1){update(q[1], q[2], q[3]);}else{ans.push_back(t.pre_sum(in[q[1]]));}}return ans;}
};
#Python
class BIT:def __init__(self, n:int):self.t = [0] * (n + 1)def update(self, i:int, val:int):while i < len(self.t):self.t[i] += vali += i & -idef per_sum(self, i:int)->int:res = 0while i > 0:res += self.t[i]i -= i & -ireturn res
class Solution:def treeQueries(self, n: int, edges: List[List[int]], queries: List[List[int]]) -> List[int]:g = [[] for _ in range(n + 1)]for x, y, _ in edges:g[x].append(y)g[y].append(x)in_ = [0] * (n + 1)out = [0] * (n + 1)clock = 0def dfs(x:int, fa:int)->None:nonlocal clockclock += 1in_[x] = clockfor y in g[x]:if y != fa:dfs(y, x)out[x] = clockdfs(1, 0)t = BIT(n)weight = [0] * (n + 1)def update(x:int, y:int, val:int)->None:if in_[x] < in_[y]:x, y = y, xd = val - weight[x]weight[x] = valt.update(in_[x], d)t.update(out[x] + 1, -d)for x, y, w in edges:update(x, y, w)ans = []for q in queries:if q[0] == 1:update(q[1], q[2], q[3])else:ans.append(t.per_sum(in_[q[1]]))return ans
http://www.xdnf.cn/news/35299.html

相关文章:

  • 在串口通信中使用共享指针(`std::shared_ptr`)
  • 【HDFS入门】HDFS数据冗余与容错机制解析:如何保障大数据高可靠存储?
  • Ubuntu Linux 中文输入法默认使用英文标点
  • 深入理解FreeRTOS操作系统:计数型信号量的原理与应用
  • JavaWeb 课堂笔记 —— 13 MySQL 事务
  • 2000-2017年各省城市天然气供气总量数据
  • Ubuntu 25.04 “Plucky Puffin” 正式发布
  • 多线程和线程同步
  • 非接触式水位传感器详解(STM32)
  • office软件中word里面的编号库和列表库功能
  • 06-libVLC的视频播放器:推流RTMP
  • 第三届世界科学智能大赛新能源赛道:新能源发电功率预测-数据处理心得体会1
  • Java @Serial 注解深度解析
  • day46——两数之和-输入有序数组(LeetCode-167)
  • 人工智能在智慧农业中的应用:从田间到餐桌的变革
  • 【Vue】布局解析
  • Manus技术架构、实现内幕及分布式智能体项目实战 线上高级实训班
  • 洛谷的几道题
  • 某局部三层休闲娱乐中心建筑设计与结构设计
  • 19-算法打卡-哈希表-四数相加II-leetcode(454)-第十九天
  • @EnableAsync+@Async源码学习笔记之五
  • 第十届团体程序设计天梯赛-上理赛点随笔
  • 学习笔记: Mach-O 文件
  • Datawhale AI春训营 世界科学智能大赛--合成生物赛道:蛋白质固有无序区域预测 小白经验总结
  • 【信息系统项目管理师】高分论文:论信息系统项目的风险管理(钢铁企业生产计划管理系统)
  • 支持中文对齐的命令行表格打印python库——tableprint
  • cesium中postProcessStages全面解析
  • 13.第二阶段x64游戏实战-分析人物等级和升级经验
  • JNI 学习
  • Linux基础IO(九)之软链接