Skip to content
Paras Gupta

Jan 4 - Daily Programming Diary

Diary, Programming2 min read

2:30

Can't sleep so I thought I'll put some work into the GitHub profile ReadMe.

4:00

So what started to be GitHub ReadMe journey turned out to be a Go tutorial. Go really has a lot of inbuilt libraries. I'm still trying to wrap my head around the syntax of the language but looks like the gorilla/mux and gin packages is fairly popular with the community. While gmux(?) is used by almost 15k projects and gin by 9.6k at the time of writing, gin claims to be 40 times faster than other Go frameworks. That's wild!

5:00

Was confused about what to do, so started Leetcode December Challenge Day 1. The question itself is pretty straightforward. Just have to calculate the maximum depth of binary tree.
Using a variable to keep track of current depth and one for max depth.

maximum-depth-of-binary-tree.cpp
1int currMax = 0, curr = 0;
2int maxDepth(TreeNode *root) {
3 if (root == NULL) {
4 return 0;
5 } else {
6 curr++;
7 currMax = max(curr, currMax);
8 }
9
10 if (root->left) {
11 maxDepth(root->left);
12 curr--;
13 }
14
15 if (root->right) {
16 maxDepth(root->right);
17 curr--;
18 }
19
20 return currMax;
21}

Time Complexity O(n) where n is the number of nodes in the tree. O(h) space complexity where h is the height of the tree.

5:30

Leetcode December Challenge Day 3. The crux was to convert a BST into a LinkedList with inorder traversal.

increasing-order-search-tree.cpp
1TreeNode *head = new TreeNode();
2TreeNode *ans = head;
3TreeNode *increasingBST(TreeNode *root) {
4 if (!root) return NULL;
5
6 increasingBST(root->left);
7 root->left = NULL;
8 head->right = root;
9 head = root;
10
11 increasingBST(root->right);
12 return ans->right;
13}

Time Complexity O(n) where n is the number of nodes in the tree. O(h) space complexity where h is the height of the tree.

Break? Sleep! *the drake meme 😂🤷‍♂️

12:30

Back on Leetcode December Challenge Day 11. The crux was to remove duplicate elements from a sorted array keeping at most 2 copies of an element.
Two pointers approach, read and write pointers. Then check the frequency of current element and write according to that.

remove-duplicates-from-sorted-array-ii.cpp
1int removeDuplicates(vector<int> &nums) {
2 int num = INT_MIN;
3 int freq = 0;
4 int write = 0;
5 for (int read = 0; read < nums.size(); read++) {
6 if (nums[read] == num) {
7 freq++;
8 if (freq <= 2) {
9 nums[write] = nums[read];
10 write++;
11 }
12 } else {
13 num = nums[read];
14 freq = 1;
15 nums[write] = nums[read];
16 write++;
17 }
18 }
19
20 return write;
21}

Time Complexity O(n) and space complexity O(1)

14:30

TIme to do the Leetcode January Challenge Day 4. Merge two sorted lists. Since I have done this before, let's see the time it takes to actually code the solution. Phew! Done. 7 minutes.

merge-two-sorted-lists.cpp
1ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
2 if (l1 == NULL) return l2;
3 if (l2 == NULL) return l1;
4
5 ListNode *head = new ListNode();
6 ListNode *ans = head;
7
8 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 }
19
20 if (l1 == NULL) {
21 head->next = l2;
22 } else {
23 head->next = l1;
24 }
25
26 return ans->next;
27}

Time complexity O(n+m) where n and m are the total number of nodes in the linked list 1 and 2 respectively. Space complexity O(1).

15:45

Worked on enabling the copy button that was bothering me for the last couple of days. Turns out, I had shadowed the file earlier and had some issues with that.
Also wanted tags for C++ and Go files since most of the code I'm uploading is in those languages. Happy with the result.

Battery 6%, taking a break!

20:30

Finally done with the GitHub Profile ReadMe. So what started at the start of the day came to a halt at the end. Talk about things that take up your life! 😛

22:00

Tried the ant problem on Codechef. For the first subtask where number of lanes = 1. Figured that the answer should be the product of ants on wither side of origin but is showing an error.
Let's see it tomorrow.

00:12

Worked on the websites that Kshitija and Mansi are making and the day ends with that.

Adios!

EOD Checklist

  • Go
  • Leetcode January Challenge
  • Codechef Long Challenge
  • Raahee
  • This blog
© 2022 by Paras Gupta. All rights reserved.
Theme by LekoArts