— Diary, Programming — 1 min read
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.
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.
Done! Yay!
1int findLHS(vector<int> &nums) {2 if (nums.size() < 2) return 0;34 map<int, int> m;5 int mi = INT_MAX;67 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 }1516 int n, cnt, ans = 0;1718 for (auto a : m) {19 if (a.first == mi) {20 n = a.first;21 cnt = a.second;22 continue;23 }2425 if (a.first - n == 1) {26 ans = max(ans, a.second + cnt);27 }2829 n = a.first;30 cnt = a.second;31 }3233 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.
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.
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.
1public:2string simplifyPath(string path) {3 vector<string> tokens;4 stringstream temp(path);5 string intermediate;67 while (getline(temp, intermediate, '/')) {8 tokens.push_back(intermediate);9 }1011 stack<string> stk;1213 for (auto a : tokens) {14 if (a.length() == 0) continue;1516 if (a == ".") continue;1718 if (a == "..") {19 if (!stk.empty()) {20 stk.pop();21 }22 continue;23 }2425 stk.push(a);26 }2728 path = "";2930 while (!stk.empty()) {31 path = "/" + stk.top() + path;32 stk.pop();33 }3435 if (path.length() == 0) return "/";3637 return path;38}
Time complexity O(n)
, space O(n)
for the stack.
Workshop revamp done. Looks identical to the Figma design. Also changed the HashHeader to use Sen
instead of Roboto Mono
.
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.
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!