Blage's Coding Blage's Coding
Home
算法
  • 手写Spring
  • SSM
  • SpringBoot
  • JavaWeb
  • JAVA基础
  • 容器
  • Netty

    • IO模型
    • Netty初级
    • Netty原理
  • JVM
  • JUC
  • Redis基础
  • 源码分析
  • 实战应用
  • 单机缓存
  • MySQL

    • 基础部分
    • 实战与处理方案
    • 面试
  • ORM框架

    • Mybatis
    • Mybatis_Plus
  • SpringCloudAlibaba
  • MQ消息队列
  • Nginx
  • Elasticsearch
  • Gateway
  • Xxl-job
  • Feign
  • Eureka
  • 面试
  • 工具
  • 项目
  • 关于
🌏本站
🧸GitHub (opens new window)
Home
算法
  • 手写Spring
  • SSM
  • SpringBoot
  • JavaWeb
  • JAVA基础
  • 容器
  • Netty

    • IO模型
    • Netty初级
    • Netty原理
  • JVM
  • JUC
  • Redis基础
  • 源码分析
  • 实战应用
  • 单机缓存
  • MySQL

    • 基础部分
    • 实战与处理方案
    • 面试
  • ORM框架

    • Mybatis
    • Mybatis_Plus
  • SpringCloudAlibaba
  • MQ消息队列
  • Nginx
  • Elasticsearch
  • Gateway
  • Xxl-job
  • Feign
  • Eureka
  • 面试
  • 工具
  • 项目
  • 关于
🌏本站
🧸GitHub (opens new window)
  • 数组

    • 搜索

    • 二分查找

    • 排序

    • 边界判断

    • 双指针法

    • 连续子数组

    • 差分数组

      • 6919. 使数组中的所有元素都等于零
      • 6929. 数组的最大美丽值
      • 2251. 花期内花的数目
      • 1094. 拼车
        • 1.差分数组
        • 2.排序+优先级队列
      • 2132. 用邮票贴满网格图
    • 模拟

    • 区间问题

  • 链表

  • 字符串

  • 二叉树

  • 动态规划

  • 深搜回溯

  • 数学贪心

  • 堆栈队列

  • 前缀和

  • 算法设计

  • 位运算

  • WA

  • 算法
  • 数组
  • 差分数组
phan
2023-12-02
目录

1094. 拼车

# 1094. 拼车 (opens new window)

# 1.差分数组

计算出某一个时刻的乘客人数,差分数组思想。起始位置加上上车人数,终点位置减去这一批下车人数,从而表征这一区间所有人数。

class Solution {
    public boolean carPooling(int[][] trips, int capacity) {
        int[] cnt = new int[1001];
        for (int i = 0; i < trips.length; i++) {
            cnt[trips[i][1]] += trips[i][0];
            cnt[trips[i][2]] -= trips[i][0];
        }
        int space=0;
        for (int i = 0; i < cnt.length; i++) {
            space += cnt[i];
            if(space>capacity) return false;
        }
        return true;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# 2.排序+优先级队列

先根据上车位置排序。然后维护一个下车点的小顶堆,及时计算扣减下车的人数。

class Solution {
    public boolean carPooling(int[][] trips, int capacity) {
        Arrays.sort(trips, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                if (o1[1] != o2[1]) return o1[1] - o2[1];
                return o1[2] - o2[2];
            }
        });
        int space = 0;
        PriorityQueue<int[]> queue = new PriorityQueue<>(new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o1[0] - o2[0];
            }
        });
        
        for (int i = 0; i < trips.length; i++) {
            //下车
            while (!queue.isEmpty() && queue.peek()[0] <= trips[i][1]) {
                int[] poll = queue.poll();
                space -= poll[1];
            }
            //上车
            queue.offer(new int[]{trips[i][2], trips[i][0]});
            space += trips[i][0];
            if(space>capacity) return false;
        }
        return true;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
编辑 (opens new window)
#Leetcode
上次更新: 2023/12/15, 15:49:57
2251. 花期内花的数目
2132. 用邮票贴满网格图

← 2251. 花期内花的数目 2132. 用邮票贴满网格图→

Theme by Vdoing | Copyright © 2023-2024 blageCoder
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式