Category Archives: algorithm

lc1553

Solution 1, just like frog jump. public static int minDays(int n) { int[] dp = new int[n + 1]; Arrays.fill(dp, Integer.MAX_VALUE); dp[0] = 0; for (int i = 0; i <= n; i++) { if (i * 2 <= n) { dp[i * 2] = Math.min(dp[i * 2], dp[i] + 1); } if (i *… Read More »

GoReplay traffic pattern change

After testing with GoReplay, it shows that GoReplay will change the traffic pattern. When setting a fix RPS rate traffic, GoReplay will just replay the traffic with fix RPS. For some caching use case, this may fail. Let’s say request 1, 2, 3, 4 are all same. We want to cache for request 1, then… Read More »

AWS

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 »