Category Archives: algorithm


Region, AZ, data center A region has minimum 2 data centers. Data center is physical location. 2 data centers are physically isolated. Each AZ is backed by 1 or more data centers. Abstracting things further, to distribute resources evenly across the zones in a given region, Amazon independently maps zones to identifiers for each account.… Read More »

Java version switch

# Add below in ~/.zshrc export JAVA_HOME=`/usr/libexec/java_home -v 1.8` export JAVA_HOME=`/usr/libexec/java_home -v11` # Check java version /usr/libexec/java_home -v 11

TCP read timeout(socket timeout), write timeout, connection timeout, HTTP request timeout

Socket, build communication between two process or application through TCP/IP protocol, layer 4 in OSI model . One acts as client process, another acts as server process. Server will do bind(), listen(), accept(), then read(), write(). Client do connect(), then read(), write(). Socket timeout: read timeout, client hasn’t received data from server after [READ_TIMEOUT] time.… Read More »

UML Arrows

Aggregation Books is a part of Library. Dismiss of Library, it won’t lead dismiss of books.   class Library {     Book book; } Composition Library has books. Dismiss of Library, then books will be dismissed as well. class Library {     Book book; } Generalization/Extends BankAccount implements Fixed Account class BankAccount extends… Read More »

lc 600. Non-negative Integers without Consecutive Ones

100100001000 One[i]:  1XXXXXXXXXXXXX Zero[i]: 1XXXXXXXXXXXXX Zero[i] = zero[i + 1] + one[i + 1]; One[i] = zero[i] One[i] is the number of combination without continuous 11, with MSB 1 Zero[i] is the number of combination without continuous 11, with MSB 0 Then go over from MSB to LSB. If at any position, it has XX00YY,… Read More »

lc950 Reveal Cards In Increasing Order

Count how many element it has. We don’t care about the number in array. We only care about the index. If there are 5 elements, we will need to a figure out the result for [0, 1, 2, 3, 4](which is actually [0, 4, 1, 3, 2]). Then, we sort the input array, and reorder… Read More »

lc945. Minimum Increment to Make Array Unique

Solution 1 takes O(N^2) time, O(1) space, it requires sort the array. Then iterate the element 1 by 1. The point is to memorize the previous element where it reaches. Then calculate for current is based on previous reach. // reach is the height it should reach on each element public static int minIncrementForUnique3(int[] A)… Read More »

lc964. Least Operators to Express Number

leastOpsExpressTarget(int x, int target) 1. when x > target. min{2 * target – 1, (x – target) * 2}. For example, n = 5, target = 3. First option is 5/5+5/5+5/5. Second option is 5-5/5-5/5. So, formula is 2. needs to calculate the product=x*x*…*x to approach target. int k = 1; long product = x;… Read More »

lc960. Delete Columns to Make Sorted III 1. Naive way O(n^2) to calculate longest subsequence dp[i] is the longest subsequence ending with i position. if charAt(j) <= charAt(i), then dp[i] = max(dp[i], dp[j] + 1) 2. Only cares about column i, column j, where all chartAt(j) <= charAt(i), then we update dp[i] public static int minDeletionSize(String[] A) { int[] dp =… Read More »