Skip to content
Paras Gupta

Feb 5 - Daily Programming Diary

Diary, Programming1 min read

11:30

Feeling a lot better today so I'll get some work done. My streak has broken so it's time to get back on track. I have 2 hours to complete yesterday's Leetcode challenge.

12:00

The problem seemed weird at first but then returning just the length of the subsequence looks pretty straightforward. The approach is to store all the frequencies in a map and then iterate over the map and calculate the sun if the previous number exists.

12:20

Done! Yay!

longest-harmonious-subsequence.cpp
1int findLHS(vector<int> &nums) {
2 if (nums.size() < 2) return 0;
3
4 map<int, int> m;
5 int mi = INT_MAX;
6
7 for (int i = 0; i < nums.size(); i++) {
8 if (m.find(nums[i]) != m.end()) {
9 m[nums[i]]++;
10 } else {
11 m[nums[i]] = 1;
12 mi = min(mi, nums[i]);
13 }
14 }
15
16 int n, cnt, ans = 0;
17
18 for (auto a : m) {
19 if (a.first == mi) {
20 n = a.first;
21 cnt = a.second;
22 continue;
23 }
24
25 if (a.first - n == 1) {
26 ans = max(ans, a.second + cnt);
27 }
28
29 n = a.first;
30 cnt = a.second;
31 }
32
33 return ans;
34}

Time complexity O(n) for iterating over the vector as well as the map. Space complexity O(n) for the map.
Space complexity could've been reduced to O(1) by sorting the list and using something like a sliding window.

16:00

Received instructions from Apoorv to change the Workshop and FAQ section on the HackOn website. Time to get started on that. I think working on the tracks section yesterday is helping a lot since I'm able to think about the flex properties and all that will be required. Good for me!

The workshop section requires the cards to be modified to contain addition information along with layout changes.
The FAQ section has to be implemented from scratch so that's new.

My main focus for now would be to just focus on the layout and then work on the minor things like font and spacing.

16:30

Let's do today's Leetcode challenge. Right of the bat, seems like a stack based problem. Simplifying a path, seems like an obvious problem but let's try.

The first step is to split the string at / and then pushing into stack for all names except the ones which are empty or point to the parent/current (../.) directories. Then get everything from stack and append at the beginning of the string.

simplify-path.cpp
1public:
2string simplifyPath(string path) {
3 vector<string> tokens;
4 stringstream temp(path);
5 string intermediate;
6
7 while (getline(temp, intermediate, '/')) {
8 tokens.push_back(intermediate);
9 }
10
11 stack<string> stk;
12
13 for (auto a : tokens) {
14 if (a.length() == 0) continue;
15
16 if (a == ".") continue;
17
18 if (a == "..") {
19 if (!stk.empty()) {
20 stk.pop();
21 }
22 continue;
23 }
24
25 stk.push(a);
26 }
27
28 path = "";
29
30 while (!stk.empty()) {
31 path = "/" + stk.top() + path;
32 stk.pop();
33 }
34
35 if (path.length() == 0) return "/";
36
37 return path;
38}

Time complexity O(n), space O(n) for the stack.

17:40

Workshop revamp done. Looks identical to the Figma design. Also changed the HashHeader to use Sen instead of Roboto Mono.

20:00

Finally the workshop and the FAQ sections are done. Made the PR. Took me a lot of time to set up the click listeners on the accordion, but I'm happy with the result.

21:00

Now I have to implement pop up for workshops. I feel like Apoorv is giving me way too much work. There is a small issue with the opacity of some text which is triggering weird results with the backdrop.

Bye!

EOD Checklist

  • Leetcode February Challenge
  • This blog
© 2022 by Paras Gupta. All rights reserved.
Theme by LekoArts