— Diary, Programming — 1 min read
I'm feeling bored and I think I should work on something. Yesterday we discussed about implementing a feature so that people could post testimonials about the event that they attended. While the meeting is schedules to take place at night, I think I should get started with it.
Hurdle 1 - The Admin Pages are broken. There is an error which is making the components which rely on the <ProtectedRouteAdmin />
not being able to render properly.
After banging my head on the wall for 30 minutes, I figured out the error, the props! The component was being passed as render prop and not as a component which complicated stuff for the route handler.
Hurdle 2 - The react-slick
is working in a very weird manner. There seems to be a property bug and since I didn't implement the feature, I'm not even able to figure out the code. But I guess that's the most important part of being a developer. Understanding code written by other people, optimizing it, and making great stuff with it. I have been in support of creating a documentation since day 1 and now I feel that it is strongly required.
I finally got to implementing the testimonials feature. Pretty straightforward if you ask me. Create a map, store the UID with their testimonial and we are done.
Done with that. Wow, that was fast!
Time for the Leetcode challenge. I have never worked on a problem like this since I always thought that it requires a lot of knowledge but here I am, trying to figure out a way to solve this question.
The first approach I can think of is to combine two lists and then two, and so on. Okay, that seems to simplify the problem a bit. Writing the code for merging two lists doesn't seem to be that big a problem.
Now what?
Well, now I have to figure out a way to merge lists one by one. Let's say I have n
lists.
Merge | Nodes left |
---|---|
1 and n | 1 to n-1 |
2 and n-1 | 1 to n-2 |
3 and n-2 | 1 to n-3 |
... | ... |
1 and n/2 | 1 to (n/2) - 1 |
... | ... |
That looks great. So merge the lists one by one using 2 pointers, then when 2nd pointer gets lesser than 1st, change 1st to 0.
And that works! Nice! Woah!
1ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {2 if (!l1) return l2;3 if (!l2) return l1;45 ListNode *head = new ListNode();6 ListNode *ans = head;78 while (l1 != NULL && l2 != NULL) {9 if (l1->val < l2->val) {10 head->next = l1;11 l1 = l1->next;12 head = head->next;13 } else {14 head->next = l2;15 l2 = l2->next;16 head = head->next;17 }18 }1920 if (l1 == NULL) {21 head->next = l2;22 } else {23 head->next = l1;24 }2526 return ans->next;27}2829ListNode *mergeKLists(vector<ListNode *> &lists)30{31 int i = 0, j = lists.size() - 1;3233 if (lists.size() == 0) return NULL;3435 while (i < j) {36 lists[i] = mergeTwoLists(lists[i], lists[j]);37 i++;38 j--;39 if (i >= j) {40 if (j == 0)41 return lists[0];42 i = 0;43 }44 }45 return lists[0];46}
The meeting got cancelled, so I guess I don't have much work to do.
Going out, not working anymore!