Why does the sliding window algorithm not work for this problem statement?

The question is regarding an algorithm which I’m using to solve a problem. The problem statement is: (or view it here)

“Given arrival and departure times of all trains that reach a railway station. Find the minimum number of platforms required for the railway station so that no train is kept waiting.
Consider that all the trains arrive on the same day and leave on the same day. Arrival and departure time can never be the same for a train but we can have arrival time of one train equal to departure time of the other. At any given instance of time, same platform can not be used for both departure of a train and arrival of another train. In such cases, we need different platforms.”

Example:

Input: n = 6, arr[] = {0900, 0940, 0950, 1100, 1500, 1800}, 
            dep[] = {0910, 1200, 1120, 1130, 1900, 2000}
Output: 3
Explanation: There are three trains during the time 0940 to 1200. So we need minimum 3 platforms.

I’m trying to use an algorithm as follows:
I’m creating an array consisting of each train’s arrival and dept. time (so for the above test case, something like: [[0900,0910], [0904, 1200], …] )
Then, I sort it based on the arrival times, and if two trains have the same arrival, they’re sorted based on the departing times (ascending order for both).

I’m basically trying to find the maximum number of intervals that intersect. Consider the first interval: it has an arrival time of “a”, departure time of “b”. Now for any intervals that follow, if it’s arrival is before “b”, then I would require an extra platform for that train (since it intersects with the first interval). I keep doing this and finding more intervals, until I reach an interval that has an arrival time greater than “b”. At this point, the first interval is no longer valid, since that train would’ve left, and so I increment my first pointer, and this process repeats. At any point of iteration, all the intervals between my “i” and “j” pointers will intersect. Finally, I return the maximum number of intervals that intersected as the answer.

This algorithm works for the initial few test cases, but for some reason it fails for certain other testcases.

The code below is what I’ve written for this algorithm. You can try to submit it on the above link to get the incorrect testcase (it’s quite large, so I don’t think I should paste it here. But if you want, I can paste the invalid testcase in the comments).

C++ Code

class Solution{
    public:
    //Function to find the minimum number of platforms required at the
    //railway station such that no train waits.
    static bool comp(pair<int,int>&p1, pair<int,int>&p2) {
        if (p1.first == p2.first) return p1.second < p2.second;
        return p1.first < p2.first;
    }
    int findPlatform(int arr[], int dep[], int n)
    {
        int i = 0, j = 0;
        int mxi = 0;
        vector<pair<int,int>>mp;
        for (int k = 0; k<n; k++) {
            mp.push_back({arr[k], dep[k]});
        }
        sort(mp.begin(), mp.end());
        
        
        while (j < n) {
            while (mp[j].first > mp[i].second) {
                i++;
            }
            mxi = max(mxi, j-i+1);
            j++;
            
        }
        return mxi;
    }
};

I’m not asking for the correct algorithm (as I’ve already read it and understood it). I just want to know why is this incorrect, and for what small test case would it fail, or if I’m missing any other case.

Thanks!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật