— Diary, Programming — 2 min read
Starting a bit early today. I was looking at the <Navbar />
yesterday and thought the performance on first reload could be improved by using useEffect
instead of the currently used location listener. Voila! It worked.
See, I can do UI fixes as well 🤷♂️
I was exploring the source code and figured out 2 major things -
<Link />
component. The <a/>
tags don't go well when working with SPAs.
setTimeout(() => resumeWork(), 2 * 1000 * 60 * 60);
Started with the Crio bytes. Since I already knew the basics of HTTP, REST, Linux, and EC2, I was able to zoom through the modules pretty quickly.
Starting with today's Leetcode Challenge. At first I wasn't able to understand the question. I thought I had to calculate the minimum cost of creating the sorted array which got me confused. After reading it for the n
th
time, I figured that the elements have to be inserted in an orderly manner.
It's been half an hour and the code I wrote to do the calculation is giving TLE, and I get it. The implementation is taking O(n^2) time to calculate the prefix sum. I have no idea so I'm gonna take a look at the hint.
So I studied about BIT aka Binary Indexed Tree, which calculates prefix sum in O(logn) time so the overall complexity of my program will go down to O(nlogn).
Done with the code. Wow! New gun in my arsenal.
1int bit[100002];23// Find prefix sum of a number4int prefixSum(int num) {5 int ans = 0;6 for (int i = num; i > 0; i -= (i & -i)) {7 ans += bit[i];8 }9 return ans;10}1112// Update the prefix sum values13int update(int num) {14 for (int i = num; i <= 100001; i += (i & -i)) {15 bit[i] += 1;16 }17 return 0;18}1920int createSortedArray(vector<int> &inst) {21 int ans = 0;22 for (int i = 0; i < inst.size(); i++) {23 ans += min(prefixSum(inst[i] - 1), i - prefixSum(inst[i]));24 ans %= 1000000007;25 update(inst[i]);26 }27 return ans;28}
Again, no Go for this question. I'm just gonna put Go on hold for now unless I have some work on the back-end.
break;
Attending the Crio event now for REST byte.
To be honest, after having developed a few production-ready APIs, I didn't learn anything new in the session but I guess I'll complete the byte and see what round 2 has for me.
I started with building the Raahee server and made a boilerplate. Took some time to get the modules ready. Will be working on the database models now.
Been writing models for the last 30 minutes and there are some issues. The conventions used in the document are inconsistent. imageUrl
and imageurl
in different collections. It's a bit frustrating to actually look for these things when back-end is generally the first thing built.
I'm tired now and will continue tomorrow. Will possibly work on the authentication and basic doc creation for events and such.
Okay Bye