Selectios are a subclass of array. it provides methods to manipulate selected elements, such as setting attributes and styles.


Selections are arrays of arrays of elements:

d3.select(): select a selection with one group


data is not a property of the selection, but a property of its elements.

When binding data to a selection, the data is stored in the DOM rather than in the selection.

data is persistent while selections can be considered transient.

Data is bound to elements one of several ways:

1. Joined to groups of elements via selection.data.
2. Assigned to individual elements via selection.datum.
3. Inherited from a parent via append, insert, or select.

Problem from top coder (TCO 14 Round 1A DIV 1) and my solution


Problem Statement

Manao is building a new house. He already purchased a rectangular area where he will place the house. The basement of the house should be built on a level ground, so Manao will have to level the entire area. The area is leveled if the difference between the heights of its lowest and highest square meter is at most 1. Manao wants to measure the effort he needs to put into ground leveling.You are given a String[] area. Each character in area denotes the height at the corresponding square meter of Manao’s area. Using 1 unit of effort, Manao can change the height of any square meter on his area by 1 up or down. Return the minimum total effort he needs to put to obtain a leveled area.


Class: HouseBuilding
Method: getMinimum
Parameters: String[]
Returns: int
Method signature: int getMinimum(String[] area)
(be sure your method is public)

My Code:

public class HouseBuilding
public int getMinimum(String[] area)
int min = 11;
int max = -1;

int[] c = new int[10];

for(int i = 0; i < area.length; i++)
for(int j = 0; j < area[i].length();j++)
int val = Integer.parseInt(“” + area[i].charAt(j));
if(val > max)
max = val;
if(val < min)
min = val;

if(max – min <= 1)
return 0;

int result = Integer.MAX_VALUE;
for(int i = 0; i < 9; i++)
int sum = 0;
for(int j = 0; j < 10; j++)
if( j <= i)
sum += (i – j) * c[j];
sum += (j – i -1) * c[j];

if(sum < result)
result = sum;

for(int i = 0; i < 9; i++)
int sum = 0;
for(int j = 0; j < 10; j++)
if( j <= i)
sum += (i – j) * c[j];
sum += (j – i) * c[j];

if(sum < result)
result = sum;

return result;

08/28 paper reading


Title: A Diary Study of Mobile Information Needs. CHI’08


20 participants, 2 weeks, 421 diary entries

15 without mobile internet; 5 with mobile internet

SMS first; then later fill in details on a web interfaces; afterwards interviews;


1) Categorized information needs:


2) Determining the cost/benefit of addressing a need:

1) Important needs are those that should be addressed, even if addressing them is not required immediately.

2) Urgent needs are usually related to current activities. time critical and demand immediate attention.

3) besides the importance and urgency of a need, also consider the cost of addressing a need in terms of time and expense.

when time is not critical, could consider multiple alternatives;

when in rush, method taking time might not a good choice

also monetary issue with using mobile phone services.

situational context contributes to the cost

3) When participants address their info need

At the time: 45%

Later: 25%

Not at all: 30%


4) For the need they address at the time, how they address it:

Web access: 30%

Call Source (call a service that had the desired info): 23%

Call Proxy: call a person/service that functioned as an indirect way to address their info need: 16%

Online Maps: 10%

Asked someone: found a person and asked face to face: 7%

Print Beforehand: Print the info before going mobile 7%

Went to Locations: (went to physical location to address the info need: check if post office is open at the moment or not) 5%

others: 2%

5) Why people address their info need at a later point?

No internet access: 32%

Biking/Driving: 28%

Busy with task: 20%

Would find out later: (people know that in the near future, another activity would address the info need, e,g, knowing the score of his favorite team in a game last night but was going to see a friend soon, from whom he can find out) 15%

In a meeting 6%

6) Why not need addressed?

Not important: 35%

Did not know how to address: 23%

No internet access: 23%

No time: 8%

Busy with task: 4%

Driving: 3%

In a meeting: 3%

forgot: 2%


7) Findings:

Having unlimited mobile internet can change behavior

Prefer being able to address their needs on their own without having to call someone for assistance.

Mobile internet is not sufficient: cumbersome or user’s circumstances prohibited them from using the internet.

8) Information needs are prompted by:

Location: 34.6%

Time: 27.9%

Conversation: 27.2%

Activity: 23.9%

9) Where does the information come from?

regardless of whether the information needs are satisfied or not, where they can find the right information.

Public source: can accessed by anyone: e.g. webs

Personal Data source:  can accessed only by that person. e.g. email, web history

physical data source: accessed through physical methods and not electronically

Pubic source is the most significant source: 58%

Personal Source: 24%

first pull out from personal source and then use it as key to search in public source (14%)

physical data source: 4% of the info needs can only be addressed by visiting a place itself.

08/28 Paper Reading

Title: augmenting conversations using dual-purpose speech. UIST

Authors: Kent Lyons,Christopher Skeels, Thad Starner…


problem trying to tackle: Manually entering the information encountered during the conversation is the predominant input mechanism.




One Algorithm Problem and my solution (08/27/2014)

Problem: Given an array of N numbers A[0,…,N-1], output an array M[0, N-1,], so that M[i] is the multiplication of all numbers in A except A[i]. No Division is allowed.

Let’s rephrase this problem: so M[i] = A[0] * A[1] * … * A[i-1] * A[i+1] * A[i+2]*…* A[N-1]

so it consists two parts: one is the multiplication below A[i]:  A[0] * A[1] * … A[i-1]; one is above A[i] : A[i+1] * A[i+2] * … * A[N-1].

Approach 1:

If we know any multiplication from index p to index q, then the problem can be solved by multiplying two parts: index from 0 … i-1 and i+1… N-1.

Let’s assume the input is int A[N], output will be Output[N]

First, create a temp array temp[N][N] and init each value with 1.

Second, calculate the auxiliary array values.

for(int i = 0; i < N-1; i++)


temp[i][i] = A[i];

for(int j = i+1; j < N-1; j++)


temp[i][j] = temp[i][j-1] * A[j];




Output[0] = temp[1][N-1];

for(int i = 1; i < N-1; i++)

Output[i] = temp[0][i-1]* temp[i+1][N-1];

The space complexity here is O(N^2).


Approach 2:

A close look at the above approach would discover that, it is not necessary to create the entire temp[N][N]. Actually if only we can create temp[0][i-1] and temp[i+1][N-1], then we are good to go. so here it is:

int tempBelow[N], tempAbove[N];

init tempBelow[0…N-1] with value 1;

int p = 1;

for(int i = 0; i < N-1; i++)


tempBelow[i] = p;

p *= A[i];


init tempAbove[0…N-1] with value 1;

p = 1;

for(int i = N-1; i >= 0; i–)


tempAbove[i] = p;

p *= A[i];


for(int i = 0; i < N; i++)


Output[i] = tempBelow[i] * tempAbove[i];


The space complexity is: O(N);


Approach 3:

In fact, we can also move on without the auxiliary array tempAbove and tempBelow. We can carry on the accumulated multiplications carefully.


init Output[0…N-1] with value 1;

int p = 1;

for(int i = 0; i < N; i++)


Output[i] = p;

p *= A[i]


p = 1;

for(int i = N-1; i >= 0; i–)


Output[i] *= p;

p *= A[i];


The space complexity is now constant O(1).