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

嵌入式学习笔记- 关于电容的纹波电流 纹波电流是导致电解电容发热、影响其寿命的关键交流电流成分。它不是一种特殊的电流,而是电路中必然存在的现象。要重视,不能忽视。必须保证电路中的纹波电流 < 电容的额定纹波电流。电容鼓包爆浆,要怀疑是否是纹波电流过大或电容ESR变大所致。希望这个解释能帮助您彻底理解这个概念!二 电容纹波电流与频率的关系在大多数情况下,频率越高,电容所能承受的额定纹波电流值越大。【个人理解:因为频率越低ESR越大,具体见第三节ESR与频率的关系】下面我为您详细解释为什么会这样,以及在实际应用中如何考虑。 阅读详情
  1. 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, return the number of element in the array that are smaller than the given integer.

Example
Example 1:

Input: array =[1,2,7,8,5] queries =[1,8,5]
Output:[0,4,2]
Example 2:

Input: array =[3,4,5,8] queries =[2,4]
Output:[0,1]
Challenge
Could you use three ways to do it.

Just loop
Sort and binary search
Build Segment Tree and Search.
Notice
We suggest you finish problem Segment Tree Build and Segment Tree Query II first.

解法1:排序+Binary Search。排序预处理O(nlogn)。每次查询O(logn)。
注意:
1)当存在多个target时,Binary Search是找排序后数组中的第一个target。这样,返回的index刚好就是小于其的个数。
2)当target不在排序数组中时,Binary Search返回小于target的元素个数。
代码如下:

class Solution {
public:
    /**
     * @param A: An integer array
     * @param queries: The query list
     * @return: The number of element in the array that are smaller that the given integer
     */
    vector<int> countOfSmallerNumber(vector<int> &A, vector<int> &queries) {
        int n = A.size();
        int m = queries.size();
        if (m <= 0) return vector<int>();
        if (n <= 0) return vector<int>(m, 0);

        sort(A.begin(), A.end());
        
        vector<int> result;
        for (int i = 0; i < m; ++i) {
            result.push_back(findPos(A, queries[i]));
        }
        
        return result;
    }
    
private:
    int findPos(vector<int> &nums, int target) {
        int n = nums.size();
        int start = 0, end = n - 1;
        while(start + 1 < end) {
            int mid = start + (end - start) / 2;
            if (nums[mid] >= target) {
                end = mid;
            } else if (nums[mid] < target) {
                start = mid;
            }
        }

        if (nums[end] == target) return end;   
        if (nums[end] < target) return end + 1;
        if (nums[start] == target) return start;
        if (nums[start] < target) return start + 1;
    }
};

2刷

class Solution {
public:
    /**
     * @param A: An integer array
     * @param queries: The query list
     * @return: The number of element in the array that are smaller that the given integer
     */
    vector<int> countOfSmallerNumber(vector<int> &A, vector<int> &queries) {
        int n = A.size();
        if (n == 0) return vector<int>(queries.size());
        map<int, int> num2Freq; //num, freq
        
        for (int i = 0; i < n; ++i) {
            num2Freq[A[i]]++;
        }

        vector<int> nums(num2Freq.size());
        int index = 0;
        for (auto a : num2Freq) {
            nums[index++] = a.first;
        }

        vector<int> preSums(nums.size());   //preSum[i] is the preSum of index2Num[i];
        preSums[0] = num2Freq[nums[0]];
        
        for (int i = 1; i < nums.size(); ++i) {
            preSums[i] = preSums[i - 1] + num2Freq[nums[i]];
        }

        vector<int> result;
        for (int i = 0; i < queries.size(); ++i) {
            int queryNum = queries[i];
            
            //find the closest index in nums[] that is smaller than queryNum
            int findIndex = -1;
            int start = 0, end = nums.size() - 1;
            while(start + 1 < end) {
                int mid = start + (end -start) / 2;
                if (nums[mid] < queryNum) {
                    start = mid;
                } else if (nums[mid] > queryNum) {
                    end = mid;
                } else {
                    //findIndex = mid;
                    end = mid;
                }
            }

            if (nums[start] == queryNum) findIndex = start - 1;
            if (nums[end] == queryNum) findIndex = end - 1;

            if (nums[end] < queryNum) findIndex = end;
            else if (nums[start] < queryNum) findIndex = start;

            if (findIndex == -1) 
                result.push_back(0);
            else 
                result.push_back(preSums[findIndex]);
        }
        
        return result;
    }
};

解法2:直接loop循环。每次用map查询。比较费时。
下次做。

解法3:线段树。预处理时间O(n),每次查询O(logn)。
代码如下:

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 = nullptr;
        right = nullptr;
    } 
};

class Solution {
public:
    /**
     * @param A: An integer array
     * @param queries: The query list
     * @return: The number of element in the array that are smaller that the given integer
     */
    vector<int> countOfSmallerNumber(vector<int> &A, vector<int> &queries) {
        int n = A.size();
        int m = queries.size();
        if (n == 0) return vector<int>(m, 0);
        vector<int> result;
        SgMtTreeNode * root = build(0, 10000);
        for (auto i : A) {
            insert(root, i);
        }
        for (auto i : queries) {
            result.push_back(query(root, i - 1));   //note: i - 1
        }
        return result;
    }
    
private:
    SgMtTreeNode * build(int start, int end) {
        if (start > end) return nullptr;
        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;
    }
    
    //return the count that is smaller than target
    int query(SgMtTreeNode * root, int target) {
        if (!root) return 0;
        if (root->start > target) return 0;
        if (root->end <= target) return root->count;
        
        return query(root->left, target) + query(root->right, target);
    }
    
    void insert(SgMtTreeNode * root, int value) {
        if (!root) return;
        if (root->start == root->end) {
            root->count++;   //duplicate numbers!!
            return;
        }
        if (root->left && root->left->end >= value) {
            insert(root->left, value);
        } else if (root->right && root->right->start <= value) {
            insert(root->right, value);
        }
        root->count++;
    }
};

解法4:树状数组 TBD

SAP ABAP SSSLERR_PEER_CERT_UNTRUSTED 证书过期+证书导入+案例详解+注意事项 出于 IT 审计与安全管理,外部系统间通讯发布的 API 主流都是 HTTPS 协议,SAP 支持 SSL 证书。SSL 证书是有效期的,当证书失效时将返回错误 SSSLERR_PEER_CERT_UNTRUSTED ,此时需要导入新效期的证书。这篇文章完整讲解了证书过期处理的全过程。 阅读详情

相关推荐

【信号与系统实战解析】—— 拉普拉斯变换在电路分析中的关键应用

本文深入解析了拉普拉斯变换在电路分析中的关键应用。通过将时域微分方程转换为复频域的代数方程,系统化处理初始条件,并利用零极点图分析电路稳定性与频率响应。文章以RLC串联电路为例,详细演示了s域建模、性质运用及综合分析方法,为工程师提供了分析动态电路的强大工具与清晰视角。

weixin_29093017的博客 176

树状数组(右侧小于当前元素个数)

树状数组

qq_53457507的博客 218

Unity游戏开发实战:用Flow Field实现RTS百人军团流畅寻路(附完整代码)

本文深入探讨了在Unity中利用流场寻路技术解决RTS游戏大规模单位移动性能瓶颈的实战方案。通过解析从Dijkstra算法生成成本场、积分场到最终向量场的核心原理,并结合分块、缓存及与DOTS/ECS架构集成的优化策略,实现了百人军团的流畅群体寻路。文章提供了完整的代码示例,助力开发者构建高性能的RTS寻路系统。

weixin_28363123的博客 590

LintCode 249: 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, data value from 0 to 10000) . For each element Ai in the array, count the num...

roufoo的博客 390

主流算力卡性能对比分析

超大规模AI训练/HPC:优先选择NVIDIA H100或AMD MI300X,需关注显存带宽和多卡互联能力。企业级AI训练:NVIDIA A100性价比更高,生态成熟。国内自主可控需求:华为昇腾910B是首选,支持液冷和低功耗设计。本地AI推理/游戏:NVIDIA RTX 4090或RTX 5090性价比突出,但需注意显存容量限制。边缘计算/低功耗场景:华为昇腾310或清微智能TX536(能效比6.3TOPS/W@INT8)更合适。

m0_69867914的博客 7812

Uds诊断自动化(CANoe.diva应用)

(7)其他配置选择可以保持默认(一般情况下,DTC以及31服务、下载服务需要另外测试)。(1)打开CANoe.Diva软件,并新建Diva工程。(2)设置工程名和保存位置;在"Specification file"(3)测试案例打开后的界面如下,可以对要进行测试的案例进行勾选,(可查看下角的进度条)后;(3)等待DIva工程的生成;(4)接下来是修改Diva工程选项,如下图点击。,如用例间的时间间隔、复位等待时间等。,即可打开导入的测试工程。,即可生成测试用例。(1)在CANoe中。

LIUJIAAAAA的博客 3446

LintCode二分查找题总结

LC上二分查找那一章有这么些题: 二分查找的题经常用于考,因为它虽然看似简单,但其实要完全正确却不容易,很容易写出死循环的程序。一个二分查找的程序可以很容易判断出一个人功底扎不扎实。 457. Classical Binary Search 这是一道非常经典的二分查找题,给出一个有序数组以及一个目标值target,要求返回target在数组中的位置,若数组里不存在ta

Logan's 6190

新能源场站必看:一次调频与AGC/AVC的协同控制逻辑解析(含典型拓扑图)

本文深度解析了新能源场站中一次调频与AGC/AVC的协同控制逻辑。针对电网频率与电压稳定需求,详细阐述了三者在响应时序、控制目标上的分工与配合,并重点剖析了以RCL-0923装置为核心的指令融合架构与典型拓扑,为场站实现高效、稳定的协同运行提供了清晰的实践指导。

brown的博客 1071

LintCode_248 Count of Smaller Number

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, return the number of element in the ar

Bernini @ buffalo 387

ABAP 平台下配置 OAuth 2.0 Authorization Code 授权类型的完整指引

本文是面向ABAP管理员和SAP开发者的OAuth 2.0授权码配置实战指南。文章详细介绍了在ABAP平台启用Authorization Code的完整流程,包括前置条件检查(如TLS配置和系统用户准备)、SOAUTH2事务码中各字段配置要点(授权类型启用、Redirect URI设置、授权码有效期等)、Scope分配规则以及验证方法。通过一个销售订单OData接口的实际案例,说明了如何将抽象规范转化为具体配置。最后提供了多种验证方式(事务码测试、端点测试和Postman测试)以及关键配置参数(如授权码有效

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 299

dolphinscheduler集群服务一键安装启动实现流程剖析

通过以上分析我们基本梳理清楚了整个dolphinscheduler项目的一键安装启动逻辑, 我们现在在通过一张图正好理一下整个实现流程。关于一键停止集群服务及一件查询集群各个角色服务实例运行状态的实现也都在stop-all.sh/status-all.sh及dolphinscheduler-daemon.sh这三个脚本中, 有兴趣自行研究。写这篇文章的核心目的不仅在于给大家分享如何优秀的开源项目的一键安装部署的实现过程。

最爱嫣夜来 1208

#248 Count of Smaller Number

题目描述: 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, return the number of elem

易水居 292

Flink 运维监控与指标采集实战(Prometheus + Grafana 全流程)

Flink运维监控在实时计算场景中至关重要,因为Flink作业需要7×24小时稳定运行,对性能、资源、故障感知和状态变化的实时监控是保障系统稳定的关键。Flink自带的Metrics系统提供了基本的监控能力,但为了更全面的监控,Prometheus+Grafana成为最主流的解决方案。通过配置PrometheusReporter和Grafana仪表盘,可以实现对Flink作业的全面监控,包括CPU、内存、吞吐量、Checkpoint状态、KafkaSink性能等关键指标。此外,自定义指标采集和报警机制的配置

晴天彩虹雨的博客 1370

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模型可以实现对随机地表的模拟

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

Lintcode248 Count of Smaller Number solution 题解

【题目描述】 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...

chixian1996的博客 165

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

线段树解题模版

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

0xac001d09 279

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

分治系列——315. Count of Smaller Numbers After Self[hard]

题目 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].

xiegr的博客 363

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

[lintcode]248. 统计比给定整数小的数的个数

链接:https://www.lintcode.com/zh-cn/problem/count-of-smaller-number/ 给定一个整数数组 (下标由 0 到 n-1,其中 n 表示数组的规模,数值范围由 0 到 10000),以及一个 查询列表。对于每一个查询,将会给你一个整数,请你返回该数组中小于给定整数的元素的数量。  注意事项 在做此题前,最好先完成

xiaocong1990的博客 398

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

LeetCode 315. Count of Smaller Numbers After Self (逆序数对)

原题网址:https://leetcode.com/problems/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 coun

James Pan 7633

神奇的数字串 | 线段树&类滑动窗口

神奇的数字串 ** 题目 分析 代码 *题目 *分析 首先 我们来理解一下题目中所定义的good串的含义: 假设一个数字串长度为n,只有当这个数字串的任意前i(1<=i<=n)项和均大于0,我们才能称这个串为good串。 简单来说就是 一个串所有前缀和的最小值大于0 才能称作good串。(不明白前缀和的小伙伴可以去这里https://blog.csdn.net/weixin_45629285/article/details/111146240看看,大佬讲的敲详细) 其次 题目中还有一

谦的博客 1304

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(248)统计比给定整数小的数的个数

Description: 给定一个整数数组 (下标由 0 到 n-1,其中 n 表示数组的规模,数值范围由 0 到 10000),以及一个 查询列表。对于每一个查询,将会给你一个整数,请你返回该数组中小于给定整数的元素的数量。  注意事项 在做此题前,最好先完成 线段树的构造 and 线段树查询 II 这两道题目。 Explanation: 样例

sunday0904的博客 824

[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

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 207: Interval Sum II (线段树和树状数组经典题!!!)
下一篇: LintCode 249: Count of Smaller Number before itself (线段树经典题!)
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值