leetcode 1605

The point is to assign each rowSum value to each cell in each row, assign each colCum value to each cell in each row. For example, need to assign 6 to m[0][1], m[1][1], m[2][1]. Check this video for detail process.  

gray code

This is the gray code. The changing rule can be: update the rightmost big for xxx100…00, it can change the digit x which is next to 100..00 See lc1611 The property of gray code, is that each two adjacent number only have 1 bit difference. For example, in decimal, if we want to update from 011… Read More »

Design Pattern summary by tech lead.

link 1. @3:00 Break programs into logically independent components, allowing component dependencies where necessary. 2. @3:10 Inspect your data objects and graph their interactions/where they flow. Keep classes simple. Keep one-way dataflow/interactions simple. @3:29 Simplicity — keep a small number of class types (controller, views, data objects) avoid creating classes that aliases those classes (managers,… Read More »

SRP

SRP, if a class is doing too much, and can’t tell what is its responsibility. Maybe it’s time for code refactoring and break down to more isolated components.

nginx map, geo

below one will handle or not handle the request based on original country. geoip_country_code is a variable provided by nginx to tell the country of the IP. # Allowed countries # map $geoip_country_code $allowed_country { default no; country_code_1 yes; country_code_2 yes; } server { listen 80 default_server; listen [::]:80 default_server; # Disallow access based on GeoIP if… Read More »

Category: web

nginx rate limiter

  http { limit_req_zone $binary_remote_addr zone=mylimit:10m rate=2r/m; upstream myserver { server 127.0.0.1:8000; } server { listen 80; server_name myserver; location /api { limit_req mylimit; proxy_pass “http://myserver”; } } } $binary_remote_addr, meaning the rate limiter is based on per IP. Each IP has 2 request per minute; zone=mylimit:10m, define rate limiter name mylimit, use 10M to… Read More »

Category: web

Singleton

Singleton with public final field Problem of this is that this can create another instance by reflection. The fix is that in private method, add a INSTANCE check. public class SingletonWithPublicFinalField { public static final SingletonWithPublicFinalField INSTANCE = new SingletonWithPublicFinalField(); private SingletonWithPublicFinalField() { // if (INSTANCE != null) { // throw new RuntimeException(“Singleton instance already… Read More »

Anti Pattern – Javabean Pattern

Call a non-argument constructor, then call a series of set method. Avoid using it. class Person { String firstName; String lastName; int age; public Person() {} public String getFirstName() { return this.firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return this.lastName; } public void setLastName(String lastName) { this.lastName… Read More »

Anti Pattern: Telescoping constructor pattern

It lists all constructors. This is an anti pattern. Avoid using it. class Person { String firstName; String lastName; int age; public Person() {} public Person(String firstName) { this.firstName = firstName; } public Person(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } public Person(String firstName, String lastName, int age) { this.firstName =… Read More »