Posts

What I learned from reading << The Clean Coder >> Book (Reading notes)

  Chapter 1 1. You should plan to work 60 hours per week. 40 hours for your employer, the other 20 hours for yourself, you should work on your career in these 20 hours. 2. Santayana's curse: "Those who cannot remember the past are condemned to repeat it." . True for software development as well. 3. Continuous learning. Woe to the developers who fail to learn new disciplines and techniques- their peers will excel as they decline. Woe to the architects who stop coding – they will rapidly find themselves irrelevant. 4. Practice is when you specifically exercise your skills outside of the performance of your job for the sole purpose of refining and enhancing those skills. 5. The best way to learn is to teach. Professionals take personal responsibility for mentoring juniors.  Chapter 4 Creative output depends on creative input. Creativity breeds creativity. (read a lot and read all kinds of material!).  Watching TV does not usually help the author create, probably won't fo...

Geometry

892  Surface Area of 3D Shapes Intuition : For each tower, its surface area is  4 * v + 2 However, 2 adjacent tower will hide the area of connected part. The hidden part is  min(v1, v2)  and we need just minus this area * 2 Time Complexity : O(N^2) 963  Minimum Area Rectangle II Iterate all possible triangles and check the opposite points that creating a quadrilateral. Time complexity: O(n^3) Space complexity: O(n)

Design

705 Design HashSet Use BST tree to implement HashSet

Sort

349 Intersection of Two Arrays    (N) Sort both array nums1 and nums2 and call STL set_intersection to save result to nums1. create a set from nums1 beginning. create a vector of int out of the result set and return it. 242  Valid Anagram  (N) Solution 1: sort both the string s and t in ascending order. return s == t O(NlgN) time and O(1) space Solution 2: Use 2 hash map freq1 and freq2 to save the chars frequency of both s and t return the freq1 == freq2 350  Intersection of Two Arrays II        (Y) Solution 1: same with 349 Solution 2: Use hash Map to count the frequency of each number in the first array. In the second loop, loop each element in the second array, if freq[num]-- still > 0, add it to the result array, otherwise skip it. return the result array in the end. 524 Longest Word in Dictionary through Deleting     (Y)  Wihtout sorting: O((L + S) * D) = O(D(L + S)), O(L) space ...

Related Prolems

1. Combination Sum I, II, III, IV 2. House Rob 3. Buy Stock

Dynamic Programming (Part 2)

877 Stone Game    dp[i][j] means the biggest number of stones you can get more than opponent picking piles in piles[i] ~ piles[j]. You can first pick piles[i] or piles[j].     If you pick piles[i], your result will be piles[i] - dp[i + 1][j]     If you pick piles[j], your result will be piles[j] - dp[i][j - 1] So we get: dp[i][j] = max(piles[i] - dp[i + 1][j], piles[j] - dp[i][j - 1]) We start from smaller subarray and then we use that to calculate bigger subarray.  309 Best Time to Buy and Sell Stock with Cooldown   int n = prices.size();         // sell[i] : maximum profit when the i-th day NOT hold the stock         vector<int> sell(n, 0);         // hold[i] :  maximum profit when the i-th day hold the stock         vector<int> hold(n, 0);   ...

Greedy

Simulation: 860  Lemonade Change (N) Simply simulate each customer buy and change process by looping over each bill in the bills array and handle 3 cases: bill == 5, 10 and 20. Must greedily use $10 first before using $5  when do change for  $20. 874  Walking Robot Simulation (Y) We simulate the path of the robot step by step ( d is direction): For each cmd in the commands array do the following:            (cmd == -1) :   d = (d + 1) % 4;                (cmd == -2) :  d = (d + 3) % 4;            Otherwise: (move steps)                 while (cmd-- > 0 && !hashSet.count(make_pair(x + dirs[d][0], y + dirs[d][1])))                     x += dirs[d][0];                     y += dirs[d][1...