@@ -131,5 +131,94 @@ public boolean book(int start, int end) {
131131 }
132132 }
133133
134- // V2
134+ // V2-1
135+ // IDEA : LINKEDLIST
136+ // https://leetcode.com/problems/my-calendar-i/solutions/1262570/js-python-java-c-easy-sorted-tree-linked-list-solutions-w-explanation/
137+ class ListNode {
138+ public int start , end ;
139+ public ListNode next ;
140+
141+ public ListNode (int s , int e , ListNode n ) {
142+ start = s ;
143+ end = e ;
144+ next = n ;
145+ }
146+ }
147+
148+ class MyCalendar_2_1 {
149+ ListNode calendar ;
150+
151+ public MyCalendar_2_1 () {
152+ ListNode tail = new ListNode (Integer .MAX_VALUE , Integer .MAX_VALUE , null );
153+ calendar = new ListNode (-1 , -1 , tail );
154+ }
155+
156+ public boolean book (int start , int end ) {
157+ ListNode curr = calendar , last = curr ;
158+ while (start >= curr .end ) {
159+ last = curr ;
160+ curr = curr .next ;
161+ }
162+ if (curr .start < end )
163+ return false ;
164+ last .next = new ListNode (start , end , curr );
165+ return true ;
166+ }
167+ }
168+
169+ // V3
170+ // IDEA : LINKEDLIST
171+ // https://leetcode.com/problems/my-calendar-i/solutions/2372060/java-easy-solution-100-faster-code/
172+ class Node {
173+ int start , end ;
174+ Node left ;
175+ Node right ;
176+
177+ public Node (int start , int end ) {
178+ this .start = start ;
179+ this .end = end ;
180+ left = null ;
181+ right = null ;
182+ }
183+ }
184+
185+ class MyCalendar_3 {
186+
187+ Node root ;
188+
189+ public MyCalendar_3 () {
190+ this .root = null ;
191+
192+ }
193+
194+ public boolean insert (Node parent , int s , int e ) {
195+ if (parent .start >= e ) {
196+ if (parent .left == null ) {
197+ parent .left = new Node (s , e );
198+ return true ;
199+ } else {
200+ return insert (parent .left , s , e );
201+ }
202+ } else if (parent .end <= s ) {
203+ if (parent .right == null ) {
204+ parent .right = new Node (s , e );
205+ return true ;
206+ } else {
207+ return insert (parent .right , s , e );
208+ }
209+ }
210+
211+ return false ;
212+ }
213+
214+ public boolean book (int start , int end ) {
215+ if (root == null ) {
216+ root = new Node (start , end );
217+ return true ;
218+ } else {
219+ return insert (root , start , end );
220+ }
221+ }
222+ }
223+
135224}
0 commit comments