。私のカレンダー I
729. My Calendar I
Difficulty: Medium
Topics: Array, Binary Search, Design, Segment Tree, Ordered Set
You are implementing a program to use as your calendar. We can add a new event if adding the event will not cause a double booking.
A double booking happens when two events have some non-empty intersection (i.e., some moment is common to both events.).
The event can be represented as a pair of integers start and end that represents a booking on the half-open interval [start, end), the range of real numbers x such that start <= x < end.
Implement the MyCalendar class:
- MyCalendar() Initializes the calendar object.
- boolean book(int start, int end) Returns true if the event can be added to the calendar successfully without causing a double booking. Otherwise, return false and do not add the event to the calendar.
Example 1:
- Input:
["MyCalendar", "book", "book", "book"] [[], [10, 20], [15, 25], [20, 30]]
- Output:
[null, true, false, true]
- Explanation:
MyCalendar myCalendar = new MyCalendar(); myCalendar.book(10, 20); // return True myCalendar.book(15, 25); // return False, It can not be booked because time 15 is already booked by another event. myCalendar.book(20, 30); // return True, The event can be booked, as the first event takes every time less than 20, but not including 20.
Constraints:
- 0 <= start < end <= 109
- At most 1000 calls will be made to book.
Hint:
- Store the events as a sorted list of intervals. If none of the events conflict, then the new event can be added.
Solution:
We need to store each event and check if the new event conflicts with any of the existing events before booking it. Since at most 1000 calls to book are allowed, we can store the events in a list and iterate through them to check for overlaps when booking new events.
Plan:
- Storing Events: We'll maintain a list where each entry is a pair [start, end] representing the booked time intervals.
- Check for Conflicts: Before adding a new event, we'll iterate through the list of booked events and check if the new event conflicts with any existing event. An overlap occurs if the new event's start time is less than the end time of an existing event and the new event's end time is greater than the start time of an existing event.
- Book Event: If no conflicts are found, we add the new event to our list of bookings.
Let's implement this solution in PHP: 729. My Calendar I
book($start, $end); */ // Example Usage: $myCalendar = new MyCalendar(); var_dump($myCalendar->book(10, 20)); // true, no conflicts, booking added var_dump($myCalendar->book(15, 25)); // false, conflict with [10, 20] var_dump($myCalendar->book(20, 30)); // true, no conflicts, booking added ?>Explanation:
Constructor (__construct): Initializes an empty array $events to keep track of all booked events.
Booking Function (book):
- It takes the start and end of a new event.
- It iterates through the list of previously booked events and checks for any overlap:
- An overlap happens if the new event starts before an existing event ends ($start < $bookedEnd) and ends after an existing event starts ($end > $bookedStart).
- If any overlap is found, the function returns false, meaning the event cannot be booked.
- If no conflicts are found, the event is added to the $events array, and the function returns true to indicate successful booking.
Time Complexity:
- Booking an Event: Each call to book involves checking the new event against all previously booked events. This leads to a time complexity of O(n) for each booking operation, where n is the number of previously booked events.
- Space Complexity: The space complexity is O(n) because we store up to n events in the array.
Example Walkthrough:
-
First Booking (book(10, 20)):
- No previous events, so the event [10, 20] is successfully booked.
- Output: true
-
Second Booking (book(15, 25)):
- The new event [15, 25] conflicts with the previously booked event [10, 20] because there is an overlap in the time interval (15 is between 10 and 20).
- Output: false
-
Third Booking (book(20, 30)):
- The new event [20, 30] does not overlap with [10, 20] because the start time of the new event is exactly when the first event ends (no overlap since it's a half-open interval).
- Output: true
This simple approach efficiently handles up to 1000 events while maintaining clarity and correctness.
Contact Links
このシリーズが役立つと思われた場合は、GitHub で リポジトリ にスターを付けるか、お気に入りのソーシャル ネットワークで投稿を共有することを検討してください。あなたのサポートは私にとって大きな意味を持ちます!
このような役立つコンテンツがさらに必要な場合は、お気軽にフォローしてください:
- GitHub
以上が。私のカレンダー Iの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック











JWTは、JSONに基づくオープン標準であり、主にアイデンティティ認証と情報交換のために、当事者間で情報を安全に送信するために使用されます。 1。JWTは、ヘッダー、ペイロード、署名の3つの部分で構成されています。 2。JWTの実用的な原則には、JWTの生成、JWTの検証、ペイロードの解析という3つのステップが含まれます。 3. PHPでの認証にJWTを使用する場合、JWTを生成および検証でき、ユーザーの役割と許可情報を高度な使用に含めることができます。 4.一般的なエラーには、署名検証障害、トークンの有効期限、およびペイロードが大きくなります。デバッグスキルには、デバッグツールの使用とロギングが含まれます。 5.パフォーマンスの最適化とベストプラクティスには、適切な署名アルゴリズムの使用、有効期間を合理的に設定することが含まれます。

php8.1の列挙関数は、指定された定数を定義することにより、コードの明確さとタイプの安全性を高めます。 1)列挙は、整数、文字列、またはオブジェクトであり、コードの読みやすさとタイプの安全性を向上させることができます。 2)列挙はクラスに基づいており、トラバーサルや反射などのオブジェクト指向の機能をサポートします。 3)列挙を比較と割り当てに使用して、タイプの安全性を確保できます。 4)列挙は、複雑なロジックを実装するためのメソッドの追加をサポートします。 5)厳密なタイプのチェックとエラー処理は、一般的なエラーを回避できます。 6)列挙は魔法の価値を低下させ、保守性を向上させますが、パフォーマンスの最適化に注意してください。

セッションハイジャックは、次の手順で達成できます。1。セッションIDを取得します。2。セッションIDを使用します。3。セッションをアクティブに保ちます。 PHPでのセッションハイジャックを防ぐための方法には次のものが含まれます。1。セッション_regenerate_id()関数を使用して、セッションIDを再生します。2。データベースを介してストアセッションデータを3。

PHP開発における固体原理の適用には、次のものが含まれます。1。単一責任原則(SRP):各クラスは1つの機能のみを担当します。 2。オープンおよびクローズ原理(OCP):変更は、変更ではなく拡張によって達成されます。 3。Lischの代替原則(LSP):サブクラスは、プログラムの精度に影響を与えることなく、基本クラスを置き換えることができます。 4。インターフェイス分離原理(ISP):依存関係や未使用の方法を避けるために、細粒インターフェイスを使用します。 5。依存関係の反転原理(DIP):高レベルのモジュールと低レベルのモジュールは抽象化に依存し、依存関係噴射を通じて実装されます。

静的結合(静的::) PHPで後期静的結合(LSB)を実装し、クラスを定義するのではなく、静的コンテキストで呼び出しクラスを参照できるようにします。 1)解析プロセスは実行時に実行されます。2)継承関係のコールクラスを検索します。3)パフォーマンスオーバーヘッドをもたらす可能性があります。

Restapiの設計原則には、リソース定義、URI設計、HTTPメソッドの使用、ステータスコードの使用、バージョンコントロール、およびHATEOASが含まれます。 1。リソースは名詞で表され、階層で維持される必要があります。 2。HTTPメソッドは、GETを使用してリソースを取得するなど、セマンティクスに準拠する必要があります。 3.ステータスコードは、404など、リソースが存在しないことを意味します。 4。バージョン制御は、URIまたはヘッダーを介して実装できます。 5。それに応じてリンクを介してhateoasブーツクライアント操作をブーツします。

PHPでは、Try、Catch、最後にキーワードをスローすることにより、例外処理が達成されます。 1)TRYブロックは、例外をスローする可能性のあるコードを囲みます。 2)キャッチブロックは例外を処理します。 3)最後にブロックは、コードが常に実行されることを保証します。 4)スローは、例外を手動でスローするために使用されます。これらのメカニズムは、コードの堅牢性と保守性を向上させるのに役立ちます。

PHPの匿名クラスの主な機能は、1回限りのオブジェクトを作成することです。 1.匿名クラスでは、名前のないクラスをコードで直接定義することができます。これは、一時的な要件に適しています。 2。クラスを継承したり、インターフェイスを実装して柔軟性を高めることができます。 3.使用時にパフォーマンスとコードの読みやすさに注意し、同じ匿名のクラスを繰り返し定義しないようにします。
