惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
I
InfoQ
Engineering at Meta
Engineering at Meta
D
DataBreaches.Net
L
LangChain Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Recent Announcements
Recent Announcements
GbyAI
GbyAI
爱范儿
爱范儿
Microsoft Security Blog
Microsoft Security Blog
腾讯CDC
美团技术团队
罗磊的独立博客
Microsoft Azure Blog
Microsoft Azure Blog
WordPress大学
WordPress大学
T
The Blog of Author Tim Ferriss
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
雷峰网
雷峰网
M
MIT News - Artificial intelligence
D
Docker
MongoDB | Blog
MongoDB | Blog
F
Fortinet All Blogs
博客园 - 叶小钗

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Minimum Number of Platforms Required | Greedy + Two Pointers
Jaspreet singh · 2026-06-16 · via DEV Community

Jaspreet singh

Problem Statement

Given arrival and departure times of trains, find the minimum number of platforms required so that no train waits.


Brute Force Intuition

For every train, check how many other trains overlap with it at the station. The maximum overlap at any point gives the answer.

This works but requires checking every train against every other train.

Complexity

  • Time Complexity: O(N²)
  • Space Complexity: O(1)

Brute Force Snippet

int platforms = 1;

for (int i = 0; i < n; i++) {
    int count = 1;

    for (int j = i + 1; j < n; j++) {
        if (!(dep[i] < arr[j] || dep[j] < arr[i])) {
            count++;
        }
    }

    platforms = Math.max(platforms, count);
}


Moving Towards the Optimal Approach

Instead of checking overlaps manually, we can sort arrival and departure times separately.

Whenever a train arrives before the earliest departure, we need a new platform.

Whenever a train departs first, a platform becomes free.


Greedy Pattern Recognition

Whenever you see:

  • Overlapping intervals
  • Simultaneous events
  • Maximum active elements at a time

Think:

Sort + Two Pointers


Optimal Java Solution

import java.util.*;

class Solution {
    static int findPlatform(int arr[], int dep[]) {
        int n = arr.length;

        Arrays.sort(arr);
        Arrays.sort(dep);

        int platforms = 1;
        int maxPlatforms = 1;

        int i = 1, j = 0;

        while (i < n && j < n) {

            if (arr[i] <= dep[j]) {
                platforms++;
                maxPlatforms = Math.max(maxPlatforms, platforms);
                i++;
            } else {
                platforms--;
                j++;
            }
        }

        return maxPlatforms;
    }
}


Dry Run

arr = [900, 940, 950, 1100, 1500, 1800]
dep = [910,1200,1120,1130,1900,2000]

900 Arrives -> 1

940 Arrives -> 2
950 Arrives -> 3

910 Departs -> 2

Max = 3

Result

Minimum Platforms Required = 3


Complexity Analysis

Metric Complexity
Time O(N log N)
Space O(1)

Interview One-Liner

Sort arrivals and departures separately, then use two pointers to track currently occupied platforms.