图解「差分」入门("前缀和" 到 "差分" 丝滑过渡)

题目描述

这是 LeetCode 上的 1094. 拼车 ,难度为 中等

Tag : 「差分」、「前缀和」

车上最初有 capacity 个空座位,车只能向一个方向行驶(不允许掉头或改变方向)。

给定整数 capacity 和一个数组 trips, <math xmlns="http://www.w3.org/1998/Math/MathML"> t r i p [ i ] = [ n u m P a s s e n g e r s i , f r o m i , t o i ] trip[i] = [numPassengers_{i}, from_{i}, to_{i}] </math>trip[i]=[numPassengersi,fromi,toi] 表示第 i 次旅行有 <math xmlns="http://www.w3.org/1998/Math/MathML"> n u m P a s s e n g e r s i numPassengers_{i} </math>numPassengersi 乘客,接他们和放他们的位置分别是 <math xmlns="http://www.w3.org/1998/Math/MathML"> f r o m i from_{i} </math>fromi 和 <math xmlns="http://www.w3.org/1998/Math/MathML"> t o i to_{i} </math>toi 。

这些位置是从汽车的初始位置向东的公里数。

当且仅当你可以在所有给定的行程中接送所有乘客时,返回 true,否则请返回 false

示例 1:

lua 复制代码
输入:trips = [[2,1,5],[3,3,7]], capacity = 4

输出:false

示例 2:

lua 复制代码
输入:trips = [[2,1,5],[3,3,7]], capacity = 5

输出:true

提示:

  • <math xmlns="http://www.w3.org/1998/Math/MathML"> 1 < = t r i p s . l e n g t h < = 1000 1 <= trips.length <= 1000 </math>1<=trips.length<=1000
  • <math xmlns="http://www.w3.org/1998/Math/MathML"> t r i p s [ i ] . l e n g t h = 3 trips[i].length = 3 </math>trips[i].length=3
  • <math xmlns="http://www.w3.org/1998/Math/MathML"> 1 < = n u m P a s s e n g e r s i < = 100 1 <= numPassengers_{i} <= 100 </math>1<=numPassengersi<=100
  • <math xmlns="http://www.w3.org/1998/Math/MathML"> 0 < = f r o m i < t o i < = 1000 0 <= from_{i} < to_{i} <= 1000 </math>0<=fromi<toi<=1000
  • <math xmlns="http://www.w3.org/1998/Math/MathML"> 1 < = c a p a c i t y < = 1 0 5 1 <= capacity <= 10^5 </math>1<=capacity<=105

差分

从朴素的想法开始:创建一个数组 cnt,用于存储从某个站点出发时,车上的乘客数量。

例如 <math xmlns="http://www.w3.org/1998/Math/MathML"> c n t [ x ] = c cnt[x] = c </math>cnt[x]=c 含义为在站点 <math xmlns="http://www.w3.org/1998/Math/MathML"> x x </math>x 出发时(在该站点的下车和上车均完成),车上乘客数为 <math xmlns="http://www.w3.org/1998/Math/MathML"> c c </math>c 个。

对于每个 <math xmlns="http://www.w3.org/1998/Math/MathML"> t r i p s [ i ] = ( c , a , b ) trips[i] = (c, a, b) </math>trips[i]=(c,a,b),我们需要对 <math xmlns="http://www.w3.org/1998/Math/MathML"> [ a , b ) [a, b) </math>[a,b) 范围内的 <math xmlns="http://www.w3.org/1998/Math/MathML"> c n t [ j ] cnt[j] </math>cnt[j] 进行加 <math xmlns="http://www.w3.org/1998/Math/MathML"> c c </math>c 操作。

处理完 trips 后,检查所有站点的乘客人数,根据是否满足 capacity 限制返回答案。

因此,这是一个关于「区间修改,单点查询」的经典问题,可使用「差分」求解。

所谓"差分",是指 原数组中每个元素与前一元素之差所形成的数组,与之相对应的是"前缀和"。

我们知道,对原数组进行诸位累加(前缀计算操作),所得到的数组为前缀和数组。差分数组,则是对其执行前缀计算后,能够得到原数组的那个数组 🤣 。

关于「差分数组 - 原数组 - 前缀和数组」三者关系如图所示:

前缀和数组的主要作用,是利用「容斥原理」快速求解某段之和。例如要查询原数组 nums 中下标范围 <math xmlns="http://www.w3.org/1998/Math/MathML"> [ l , r ] [l, r] </math>[l,r] 的和,可通过 <math xmlns="http://www.w3.org/1998/Math/MathML"> s u m [ r ] − s u m [ l − 1 ] sum[r] - sum[l - 1] </math>sum[r]−sum[l−1] 快速求解。

差分数组的主要作用,是帮助快速修改某段区间。

由于差分数组执行「前缀计算」后得到的是原数组,因此在差分数组上修改某个值,会对原数组某段后缀产生相同的影响。

因此,当我们想要对原数组的 <math xmlns="http://www.w3.org/1998/Math/MathML"> [ l , r ] [l, r] </math>[l,r] 进行整体修改时,只需要对差分数组的 <math xmlns="http://www.w3.org/1998/Math/MathML"> l l </math>l 和 <math xmlns="http://www.w3.org/1998/Math/MathML"> r + 1 r + 1 </math>r+1 位置执行相应操作即可

举个 🌰,假设想对原数组 nums 的 <math xmlns="http://www.w3.org/1998/Math/MathML"> [ l , r ] [l, r] </math>[l,r] 进行整体"加一"操作,那么可转换为对差分数组 c[l] 的加一操作(等价对原数组的 <math xmlns="http://www.w3.org/1998/Math/MathML"> [ l , n − 1 ] [l, n - 1] </math>[l,n−1] 进行加一),以及对差分数组 c[r + 1] 的减一操作(等价于对原数组的 <math xmlns="http://www.w3.org/1998/Math/MathML"> [ r + 1 , n − 1 ] [r + 1, n - 1] </math>[r+1,n−1] 进行减一,最终只有 <math xmlns="http://www.w3.org/1998/Math/MathML"> [ l , r ] [l, r] </math>[l,r] 有加一效果)。

至此,我们完成了对「差分」的基本学习:将原数组的区间修改等价为差分数组的特定位置修改

回到本题,起始先用 nums 来作为差分数组,对于 <math xmlns="http://www.w3.org/1998/Math/MathML"> t r i p s [ i ] = ( c , a , b ) trips[i] = (c, a, b) </math>trips[i]=(c,a,b),有 <math xmlns="http://www.w3.org/1998/Math/MathML"> c c </math>c 个乘客在 <math xmlns="http://www.w3.org/1998/Math/MathML"> a a </math>a 点上车,在 <math xmlns="http://www.w3.org/1998/Math/MathML"> b b </math>b 点下车,因此对 <math xmlns="http://www.w3.org/1998/Math/MathML"> [ a , b ) [a, b) </math>[a,b) 进行整体加 <math xmlns="http://www.w3.org/1998/Math/MathML"> c c </math>c 操作,对应差分数组操作 nums[a] += c; nums[b] -= c

处理完 trips 后,对差分数组 nums 进行前缀计算(可直接复用 nums,进行原地计算),便可得到各个站点的乘客数量,与 capacity 比较得出答案。

一些细节:为了方便,人为规定站点编号从 <math xmlns="http://www.w3.org/1998/Math/MathML"> 1 1 </math>1 开始。

Java 代码:

Java 复制代码
class Solution {
    public boolean carPooling(int[][] trips, int capacity) {
        int[] nums = new int[1010];
        for (int[] t : trips) {
            int c = t[0], a = t[1], b = t[2];
            nums[a + 1] += c; nums[b + 1] -= c;
        }
        for (int i = 1; i <= 1000; i++) {
            nums[i] += nums[i - 1];
            if (nums[i] > capacity) return false;
        }
        return true;
    }
}

C++ 代码:

C++ 复制代码
class Solution {
public:
    bool carPooling(vector<vector<int>>& trips, int capacity) {
        vector<int> nums(1010, 0);
        for (const auto& t : trips) {
            int c = t[0], a = t[1], b = t[2];
            nums[a + 1] += c; nums[b + 1] -= c;
        }
        for (int i = 1; i <= 1000; i++) {
            nums[i] += nums[i - 1];
            if (nums[i] > capacity) return false;
        }
        return true;
    }
};

Python 代码:

Python 复制代码
class Solution:
    def carPooling(self, trips: List[List[int]], capacity: int) -> bool:
        nums = [0] * 1010
        for t in trips:
            c, a, b = t[0], t[1], t[2]
            nums[a + 1] += c
            nums[b + 1] -= c
        for i in range(1, 1001):
            nums[i] += nums[i - 1]
            if nums[i] > capacity: return False
        return True

TypeScript 代码:

TypeScript 复制代码
function carPooling(trips: number[][], capacity: number): boolean {
    const nums = new Array(1010).fill(0);
    for (const t of trips) {
        const c = t[0], a = t[1], b = t[2];
        nums[a + 1] += c; nums[b + 1] -= c;
    }
    for (let i = 1; i <= 1000; i++) {
        nums[i] += nums[i - 1];
        if (nums[i] > capacity) return false;
    }
    return true;
};
  • 时间复杂度: <math xmlns="http://www.w3.org/1998/Math/MathML"> O ( n + m ) O(n + m) </math>O(n+m),其中 <math xmlns="http://www.w3.org/1998/Math/MathML"> n n </math>n 为数组 trips 大小; <math xmlns="http://www.w3.org/1998/Math/MathML"> m = 1000 m = 1000 </math>m=1000 为位置值域大小
  • 空间复杂度: <math xmlns="http://www.w3.org/1998/Math/MathML"> O ( m ) O(m) </math>O(m)

最后

这是我们「刷穿 LeetCode」系列文章的第 No.1094 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。

在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。

为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:github.com/SharingSour...

在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。

更多更全更热门的「笔试/面试」相关资料可访问排版精美的 合集新基地 🎉🎉

相关推荐
小白小白从不日白5 分钟前
react hooks--useReducer
前端·javascript·react.js
zero_one_Machel5 分钟前
leetcode73矩阵置零
算法·leetcode·矩阵
下雪天的夏风17 分钟前
TS - tsconfig.json 和 tsconfig.node.json 的关系,如何在TS 中使用 JS 不报错
前端·javascript·typescript
diygwcom29 分钟前
electron-updater实现electron全量版本更新
前端·javascript·electron
wn53133 分钟前
【Go - 类型断言】
服务器·开发语言·后端·golang
青椒大仙KI1137 分钟前
24/9/19 算法笔记 kaggle BankChurn数据分类
笔记·算法·分类
^^为欢几何^^41 分钟前
lodash中_.difference如何过滤数组
javascript·数据结构·算法
豆浩宇41 分钟前
Halcon OCR检测 免训练版
c++·人工智能·opencv·算法·计算机视觉·ocr
Hello-Mr.Wang1 小时前
vue3中开发引导页的方法
开发语言·前端·javascript
浅念同学1 小时前
算法.图论-并查集上
java·算法·图论