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

Java | Leetcode Java题解之第327题区间和的个数

题目:

题解:

class Solution {public int countRangeSum(int[] nums, int lower, int upper) {long sum = 0;long[] preSum = new long[nums.length + 1];for (int i = 0; i < nums.length; ++i) {sum += nums[i];preSum[i + 1] = sum;}BalancedTree treap = new BalancedTree();int ret = 0;for (long x : preSum) {long numLeft = treap.lowerBound(x - upper);int rankLeft = (numLeft == Long.MAX_VALUE ? (int) (treap.getSize() + 1) : treap.rank(numLeft)[0]);long numRight = treap.upperBound(x - lower);int rankRight = (numRight == Long.MAX_VALUE ? (int) treap.getSize() : treap.rank(numRight)[0] - 1);ret += rankRight - rankLeft + 1;treap.insert(x);}return ret;}
}class BalancedTree {private class BalancedNode {long val;long seed;int count;int size;BalancedNode left;BalancedNode right;BalancedNode(long val, long seed) {this.val = val;this.seed = seed;this.count = 1;this.size = 1;this.left = null;this.right = null;}BalancedNode leftRotate() {int prevSize = size;int currSize = (left != null ? left.size : 0) + (right.left != null ? right.left.size : 0) + count;BalancedNode root = right;right = root.left;root.left = this;root.size = prevSize;size = currSize;return root;}BalancedNode rightRotate() {int prevSize = size;int currSize = (right != null ? right.size : 0) + (left.right != null ? left.right.size : 0) + count;BalancedNode root = left;left = root.right;root.right = this;root.size = prevSize;size = currSize;return root;}}private BalancedNode root;private int size;private Random rand;public BalancedTree() {this.root = null;this.size = 0;this.rand = new Random();}public long getSize() {return size;}public void insert(long x) {++size;root = insert(root, x);}public long lowerBound(long x) {BalancedNode node = root;long ans = Long.MAX_VALUE;while (node != null) {if (x == node.val) {return x;}if (x < node.val) {ans = node.val;node = node.left;} else {node = node.right;}}return ans;}public long upperBound(long x) {BalancedNode node = root;long ans = Long.MAX_VALUE;while (node != null) {if (x < node.val) {ans = node.val;node = node.left;} else {node = node.right;}}return ans;}public int[] rank(long x) {BalancedNode node = root;int ans = 0;while (node != null) {if (x < node.val) {node = node.left;} else {ans += (node.left != null ? node.left.size : 0) + node.count;if (x == node.val) {return new int[]{ans - node.count + 1, ans};}node = node.right;}}return new int[]{Integer.MIN_VALUE, Integer.MAX_VALUE};}private BalancedNode insert(BalancedNode node, long x) {if (node == null) {return new BalancedNode(x, rand.nextInt());}++node.size;if (x < node.val) {node.left = insert(node.left, x);if (node.left.seed > node.seed) {node = node.rightRotate();}} else if (x > node.val) {node.right = insert(node.right, x);if (node.right.seed > node.seed) {node = node.leftRotate();}} else {++node.count;}return node;}
}
http://www.lryc.cn/news/416764.html

相关文章:

  • 开发一个MutatingWebhook
  • 【leetcode详解】另一棵树的子树 (C++递归:思路精析 过程反思)
  • 物联网遇到人工智能,极快的加速物联网时代
  • Vue3+Ts项目中经常遇到导入组件,vscode报无法找到模块xxx,xxx隐式拥有 “any“ 类型解决办法~
  • 郑州轻工业大学zzulioj1151~1159合集
  • 开发框架DevExpress XAF v24.2产品路线图预览——增强跨平台性
  • 程序员短视频上瘾综合症
  • image.convert()函数转换格式及显示图像的RGB三通道图像
  • C语言 ——— 在控制台实现扫雷游戏(一次展开一片,递归实现)
  • el7升级Apache模块编译
  • Linux系统下的日志管理与ELK Stack实践
  • C++入门基础知识
  • Python爬虫技术 第28节 数据可视化
  • react中的装饰器
  • Elasticsearch:用例、架构和 6 个最佳实践
  • tcp常用网络接口 linux环境
  • 第10节课:JavaScript基础——网页交互的魔法
  • springboot+vue+mybatis汽车租赁管理+PPT+论文+讲解+售后
  • .NET C# 将文件夹压缩至 zip
  • 软考基本介绍
  • 【Vue】vue3 中使用 ResizeObserver 监听元素的尺寸宽度变化
  • 信息安全专业好吗?
  • 梧桐数据库(WuTongDB):数据库中元数据表的常见信息
  • 在 Linux 9 上安装 Oracle 19c:克服兼容性问题 (INS-08101)
  • 【踩坑】pytorch中的索引与copy_结合不会复制数据及其解决方案
  • 十六、【Python】基础教程 - 【Flask】网络编程开发
  • C#初级——List 容器
  • serial靶机教程
  • 【Linux-MISC设备】
  • 【随笔】VRRP+MSTP