LintCode 249: Count of Smaller Number before itself (线段树经典题!)

RK3588多路MIPI摄像头采集与WiFi低延迟传输实战指南 本文详细介绍了基于RK3588平台实现多路MIPI摄像头同步采集并通过WiFi进行低延迟传输的完整实战方案。文章深入剖析了从V4L2驱动配置、多线程采集、环形缓冲区设计到UDP网络传输优化的核心技术,旨在帮助开发者在工业检测、智能安防等场景下构建高并发、高实时性的视频流系统,实现端到端延迟控制在100毫秒以内的目标。 阅读详情
  1. Count of Smaller Number before itself

Give you an integer array (index from 0 to n-1, where n is the size of this array, data value from 0 to 10000) . For each element Ai in the array, count the number of element before this element Ai is smaller than it and return count number array.

Example
Example 1:

Input:
1 2 7 8 5
Output:
0 1 2 3 2
Example 2:

Input:
7 8 2 1 3
Output:
0 1 0 0 2
Clarification
Before you do this, you’d better complete the following three questions: Segment Tree Build, Segment Tree Query II,and Count of Smaller Number before itself I 。

Notice
We suggest you finish problem Segment Tree Build, Segment Tree Query II and Count of Smaller Number first.

解法1:线段树
想了一下,这个题目和248不一样。好像只能用线段树和树状数组解。
代码如下:

struct SgMtTreeNode {
    int start, end, count;
    SgMtTreeNode *left, *right;
    SgMtTreeNode(int s = 0, int e = 0, int c = 0) : start(s), end(e), count(c) {
        left = NULL;
        right = NULL;
    }
};

class Solution {
public:
    /**
     * @param A: an integer array
     * @return: A list of integers includes the index of the first number and the index of the last number
     */
    vector<int> countOfSmallerNumberII(vector<int> &A) {
        int n = A.size();
        if (n <= 0) return vector<int>();
        
        vector<int> result;
        
        SgMtTreeNode * root = build(0, 10000);  
        
        for (int i = 0; i < n; ++i) {
            insert(root, A[i]);
            result.push_back(query(root, A[i] - 1));
        }
        
        return result;
    }


private:
    SgMtTreeNode * build(int start, int end) {
        if (start > end) return NULL;
        SgMtTreeNode * root = new SgMtTreeNode(start, end, 0);
        if (start == end) return root;
        int mid = start + (end -  start) / 2;
        root->left = build(start, mid);
        root->right = build(mid + 1, end);
        return root;
    }

    int query(SgMtTreeNode * root, int target) {
        if (!root) return 0;
        if (target >= root->end) return root->count;
        if (target < root->start) return 0;
        return query(root->left, target) + query(root->right, target);
    }
    
    void insert(SgMtTreeNode * root, int value) {
        if (!root) return;
        if (value == root->start && value == root->end) {
            root->count++;
            return;
        } 
        if (root->left && value <= root->left->end) {
            insert(root->left, value);
        }
        if (root->right && value >= root->right->start) {
            insert(root->right, value);
        }
        root->count++;
    }
};

注意:

  1. insert()也可以写成如下形式。稍有冗余。
    void insert(SgMtTreeNode * root, int value) {
        if (!root) return;
        if (value == root->start && value == root->end) {
            root->count++;
            return;
        } 
        if (root->left && value >= root->left->start && value <= root->left->end ) {
            insert(root->left, value);
        }
        if (root->right && value >= root->right->start && value <= root->right->end) {
            insert(root->right, value);
        }
        root->count++;
    }

2)不管是query还是insert,value都是和某个节点的start和end比,而不是和这个节点本身的count(或max,min比)。
3)在countOfSmallerNumberII()里面的query()要用A[i]-1,记住不是A[i]。
4)这题与LintCode 248唯一不同就是在countOfSmallerNumberII()里面,这题一边insert一边query,而248是一口气全部insert,然后一个一个query。

解法2:树状数组
TBD

原恒星尘埃生长的三维MHD模拟与湍流效应研究 星际尘埃演化是恒星形成过程中的关键环节,其动力学行为直接影响行星系统的构建。通过磁流体力学(MHD)模拟可以揭示尘埃颗粒从微米级到行星胚胎尺度的生长机制,其中湍流效应通过改变碰撞频率和密度结构发挥双重作用。最新研究采用COALA-RAMSES耦合框架,实现了包含自洽尘埃生长的三维非理想MHD模拟,量化了不同物理环境下尘埃尺寸分布的演化规律。这些发现不仅为解释ALMA观测数据提供理论依据,也对行星形成初期条件约束具有重要意义,特别是在湍流参数化与尘埃生长速率关系方面取得突破。 阅读详情

相关推荐

三分钟解决RKNN-Toolkit2安装失败:从模块报错到成功验证的完整记录

本文详细记录了RKNN-Toolkit2安装失败的解决方案,从环境配置到模型转换实战,特别针对常见的'No module named 'rknn''报错提供了完整修复指南。通过YOLOv8模型转换示例,展示了从ONNX到RKNN格式的完整流程,并分享了在RK3588芯片上的部署优化技巧。

html8的博客 266

LintCode 249. 统计前面比自己小的数的个数

给定一个整数数组(下标由 0 到 n-1, n 表示数组的规模,取值范围由 0 到10000)。对于数组中的每个ai元素,请计算ai前的数中比它小的元素的数量。 注意事项 We suggest you finish problemSegment Tree Build,Segment Tree Query IIandCount of Smaller Numberfirst...

weixin_30607659的博客 423

R语言实战:Scheirer–Ray–Hare检验在非正态重复测量数据分析中的应用

本文详细介绍了当重复测量数据不满足正态分布假设时,如何利用R语言中的Scheirer–Ray–Hare检验进行数据分析。该检验作为非参数方法,无需数据服从正态分布或方差齐性,适用于分析组间差异、时间效应及其交互作用。文章通过实战案例,从环境搭建、数据准备、检验执行到结果解读与可视化,提供了完整的操作指南,帮助研究者有效处理心理学、医学等领域的复杂实验数据。

salt9的博客 780

lintCode249【(树状数组)+(求逆序数)总结】(附:最后面浅说,离散化,目的是为了节省树状数组使用空间)

题目链接:https://www.lintcode.com/problem/count-of-smaller-number-before-itself/description 题目大意: 其实说白了,就是找每个数字的前面到底有多少个数比他小的。 我们看题目的例子: 输入: [1,2,7,8,5] 输出: [0,1,2,3,2] //我们依次看:首先是“输入”的第一位:“1”,这个数前面没有数,故没...

zhoupingqi2017的博客 333

别再自己写消息提示了!5分钟用WPF+Material Design打造专业级Snackbar(支持.NET 6)

本文为WPF开发者提供了一份快速集成专业级Snackbar消息提示的实战指南。通过引入MaterialDesignThemes开源库,在.NET 6 WPF项目中,仅需5分钟即可告别传统的MessageBox,实现遵循Material Design规范、动画流畅、支持队列管理的非侵入式Snackbar控件,显著提升应用的用户体验与现代化交互水平。

year5的博客 174

Allegro17.4原理图-PCB双向联动技巧:从网表生成到同步高亮

本文深入解析了Cadence Allegro 17.4中原理图与PCB的深度双向联动技巧。核心在于通过精准的网表生成与管理、实时的跨窗口交互高亮与信号追踪,以及元件与封装的智能匹配更新,构建高效协同设计工作流。掌握这些技巧能显著提升高速、高密度电路设计的效率与准确性,实现从逻辑设计到物理实现的无缝衔接。

weixin_29178069的博客 359

LintCode 249 统计前面比自己小的数的个数【线段树】

249. Count of Smaller Number before itself  Description Give you an integer array (index from 0 to n-1, where n is the size of this array, data value from 0 to 10000) . For each element Ai in the ar...

紫芝的博客 1413

风光储互补微电网在偏远地区的应用实践:从建模到仿真全解析

本文深入解析了风光储互补微电网在偏远地区的应用实践,从系统设计到仿真建模全流程。通过Simulink等工具进行动态仿真,验证了系统在多种工况下的可靠性,并结合实际案例展示了其显著的经济和环保效益。文章为从业者提供了从技术路径到实施优化的全面指导。

weixin_29047545的博客 271

LintCode 二叉树的层次遍历

中等 二叉树的层次遍历 查看运行结果  33% 通过 给出一棵二叉树,返回其节点值的层次遍历(逐层从左往右访问) 您在真实的面试中是否遇到过这个题?  Yes 样例 给出一棵二叉树 {3,9,20,#,#,15,7},     3    / \   9  20     /  \    15   7 返回它的

午夜飞行 2366

ROS 2 Jazzy与Gazebo Harmonic仿真实战:从零搭建机器人仿真环境与高级应用

本文详细介绍了如何使用ROS 2 Jazzy和Gazebo Harmonic从零搭建机器人仿真环境,涵盖环境配置、传感器集成、多机器人协同和GPU加速优化等高级应用。通过实战案例和性能调优技巧,帮助开发者高效构建可靠的机器人仿真系统,提升开发效率。

h9j8k7l6m5n的博客 1220

LintCode:统计比给定整数小的数的个数

LintCode:统计比给定整数小的数的个数尝试用线段树,超时。public class Solution { /** * @param A: An integer array * @return: The number of element in the array that * are smaller that the given inte

yaphone的博客 760

MATLAB基础应用精讲-【强化学习】Actor - Critic 算法(附python代码实现)

这种架构结合了策略梯度方法(Policy - Gradient Methods)和价值函数估计方法(Value Function Estimation Methods)的优点。在强化学习中,智能体(Agent)需要通过与环境(Environment)的交互来学习最优的行为策略,以获得最大的累积奖励(Cumulative Reward)。

qq_36130719的博客 366

LintCode_249 Count of Smaller Number before itself

Give you an integer array (index from 0 to n-1, where n is the size of this array, value from 0 to 10000) . For each element Ai in the array, count the number of element before this element Ai is

Bernini @ buffalo 467

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

[LintCode] Count of Smaller Number before itself

Count of Smaller Number before itself Give you an integer array (index from 0 to n-1, where n is the size of this array, value from 0 to 10000) . For each element Ai in the array, count the number of...

weixin_34354945的博客 158

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

LintCode 248. Count of Smaller Number (线段树经典题!!)

Count of Smaller Number 中文English Give you an integer array (index from 0 to n-1, where n is the size of this array, value from 0 to 10000) and an query list. For each query, give you an integer, ret...

roufoo的博客 412

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

Lintcode249 Count of Smaller Number before itself solution 题解

【题目描述】 Give you an integer array (index from 0 to n-1, where n is the size of this array, data value from 0 to 10000) . For each element Ai in the...

chixian1996的博客 199

线段树解题模版

线段树 概念:线段树就是一棵二叉树,每个节点代表一个区间,主要用于解决区间类问题。每个节点的属性根据需要可以去自定义,比如节点的属性可以是区间和、区间最大/最小值。。 一、线段树节点定义 每个node有区间的左右端点,以及左右孩子 public class SegmentTreeNode{ int start,end,val; //val根据需要定义,比如我定义为区间最大值就是max ...

0xac001d09 279

统计前面比自己小的数的个数-LintCode

给定一个整数数组(下标由 0 到 n-1, n 表示数组的规模,取值范围由 0 到10000)。对于数组中的每个 ai 元素,请计算 ai 前的数中比它小的元素的数量。注意事项: We suggest you finish problem Segment Tree Build, Segment Tree Query II and Count of Smaller Number first.样例:

kane的博客 1337

lintcode247. 线段树查询 II

对于一个数组,我们可以对其建立一棵 线段树, 每个结点存储一个额外的值 count 来代表这个结点所指代的数组区间内的元素个数. (数组中并不一定每个位置上都有元素) 实现一个 query 的方法,该方法接受三个参数 root, start 和 end, 分别代表线段树的根节点和需要查询的区间,找到数组中在区间[start, end]内的元素个数。 样例 样例 1: 输入:"[0,3,count...

sinb妃的博客 163

LintCode "Count of Smaller Number"

Fenwick Tree is perfect for this problem, though space complexity is not quite efficient. class Solution { ////////////////// // Fenwick Tree // vector<int> ft; void updat...

weixin_30699463的博客 143

LintCode "Count of Smaller Number before itself"

Warning: input could be > 10000... Solution by segment tree: struct Node { Node(int s, int e) : start(s), end(e), cnt(0), left(nullptr), right(nullptr) {}; int start; int...

weixin_30856965的博客 147

315. Count of Smaller Numbers After Self

You are given an integer array nums and you have to return a new counts array. The counts array has the property where counts[i] is the number of smaller elements to the right of nums[i]. Example

学渣记事本 347

lintcode阶梯训练第二关(九章)

一、二分位置之OOXX14、二分查找 题目 给定一个排序的整数数组(升序)和一个要查找的整数target,用O(logn)的时间查找到target第一次出现的下标(从0开始),如果target不存在于数组中,返回-1。 样例 在数组 [1, 2, 3, 3, 4, 5, 10] 中二分查找3,返回2。 代码 class Solution { public int binarySearc

Dashuai_Liu的博客 2407

Count of Smaller Number After Itself

1 public class Solution { 2 public List<Integer> countSmaller(int[] nums) { 3 List<Integer> result = new ArrayList<>(); 4 List<Integer> record = ne...

weixin_34080903的博客 123

统计前面比自己小的数

原题链接在这里http://www.lintcode.com/zh-cn/problem/count-of-smaller-number-before-itself/ 要统计数组中每个前面比自己小的元素个数,数组元素的范围是[0,10000] 显然一个一个统计的方法时间复杂度是o(n^2),提交上去会超时。 我们不妨将这个问题转化为一个区间求和的问题,用一个数组每个元素存储当前数字出现多

低端博客 1658
上一篇: LintCode 248. Count of Smaller Number (线段树经典题!!)
下一篇: C/C++ 数据输入模板
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值