Monday, July 25, 2016

HackerRank - world codesprint #5 Longest increasing subsequence arrays

July 25, 2016

Problem website:
https://www.hackerrank.com/contests/world-codesprint-5/challenges/longest-increasing-subsequence-arrays

Analysis from editorial:
Because we must use numbers from  to fill each array and we must be able to build an -element LIS for each array, we know each number from to  must appear in the array in increasing order.
We can select  positions where  to  will be placed such that  is placed in the first selected position, ,  is placed in the second position, , and so on. To avoid overcounting, we impose that there is no  before , no  between the position of  and , and so on. Note that after , we are free to place any value  we want.
For each chosen arrangement, how many ways are there to fill the remaining gaps? There are  unfilled cells and there are  values we can use to fill each position (except the segment from , which can accommodate until ). If we let , we can place .
If we loop over the values of  from  to , then:
Note that after we place the values in the last segment of length , we're left with an array of length  in which the last element must be . That's why we can only choose  integers.
This has a complexity of , provided you precalculate properly.
C# code to study:


String Construction - HackerRank - world codesprint #5

July 25, 2016

String Construction:  Easy question, score 25/25

https://gist.github.com/jianminchen/9c61868800a9835347ffc1047960ea08


Short Palindrome - HackerRank - world codesprint #5

July 25, 2016

Problem statement

Julia spent 3+ hours to score 13 out of 40 points, she enjoyed the time to work on the problem.

 A few issues: wrong answer, time out, run time error.

 More than 3 submissions, failed to solve time out issue.

 1. C# solution

 2. Add memorization using Dictionary to save time, failed to solve issues.
C# solution 

 3. add one more memorization, failed to solve issues.
C# solution

 4. Review code and then add some comment, and analyse design issues:

C# solution

Review the design, try to find the flaws - a few ideas to improve timeout, no run-time error issue.

review code and then add comment for function countingPalindromes:
/*
* use two loops
*
* get 0110 pairs
* get 1001 paris
*
* Think about DP solution - dynamic programming - not working, too complicate
*
* brute force solution:
* 0 - start char, end char, and then, count how many possibilities
* Go through O(n*n) case, for any two 0 in pseudo string, check in-between
*
* For example:
* 001010110
*
* The above case, there are 5 '0',
* position of '0' is 0, 1, 3, 5, 8,
* So, any two '0' combinations are 5*4/2*1 = 10
*
* A: 0, 1, in between, there is no chars, n/a
* B: 0, 3, 2 chars in between - "01", count how many '1' in the substring, only 1.
* C: 0, 5, 4 chars in between - "0101", two one inside, so how many combination
* of 11, only 1 choice.
* D: 0, 8, 7 chars in-between - "0101011", four one's in the substring, how many combination - 4*3/2 = 6 choices
*/

Add comment for function getPseudoString(...)
/*
* using 0 and 1 to construct the string
* 1001
* 0110
*
* There are 26 chars in alphabetic string, any two combination is 26*25/2*1 > 250
* Instead, using 0 and 1 to stand for two distinct characters.
* for example, abab
* 0101
* So, we can conclude one thing:
* ababa
* 01010
* or
* 10101
* So, use memorization, 01010 and 10101 should be same key
* See if this can solve time out issues - July 25, 2016
*
* reverse of string should be same key
*/
It took Julia more than 3 hours to gain 13/40 point, but learning experience was so great, and she enjoyed the coding experience.

Starting from brute force solution, she made some progress to gain 13 points; go through optimal solution - DP, should be next step.

Statistics:

450 score 40/40
1600  score above 12/40
Total submission: 2180

Facts about HackerRank:
aiming brute force, 30% score.

Dynamic solution:

detail from editiorial notes.  

The idea of DP from the above website: string length is n

pattern to search

xyyx

xy ends position at i - iterate from 1 to n-1,

denote l[i]

yx starts at position i - iteration from i to n-1

denote r2[i]

yx starts at position >=i

denote r[i] = r2[i] + r2[i+1]+...+r2[n-1]

So, to get a solution:

   Iterate i from 1 to n-1
 
   sum of l[i] * r[i]

And one more thing,
          r[i] = r2[i] + r[i+1]

The idea Julia tried on July 24, 2016 -
suppose that T[i] is the short palindrome like "xyyx" at end of string i, how to construct T[i+1]?

Follow up after 8 months


C# code


Follow up 


8/25/2017
I go through the code, and evaluate the code. The fact is that the code is too complicate for a medium level algorithm. Based on the past code contest experience, the code should be very short and concise.

I will put some code review on my last practice, and then write a solution to help the dynamic programming algorithm practice and learning. Make it part of 10 step series.

Camel Case - HackerRank - world codesprint #5

July 25, 2016

C# practice: Easy question, score 25/25
https://gist.github.com/jianminchen/75b984851f03cc369d1f59f41b6415f2


Saturday, July 23, 2016

HackerRank - project euler

July 23, 2016

 Favorite place to write code in June 2016 is facebook code lab. Try this one, project euler, start with 1 or 2 hours first.

https://www.hackerrank.com/contests/projecteuler/challenges

blog about euler practice:
http://cenalulu.github.io/python/euler-project-experience/

Union-Find Algorithm - an undirected graph algorithm

July 23, 2016

Julia just misses the fun time to play with HackerRank world codesprint #4 in June 2016, in order to prepare next HackerRank code sprint on July 24, 2016, she has to review undirected graph algorithm - Union-Find algorithm. She likes the challenge, but she has to discipline herself always work on the simplest algorithm first. Aim small target first, baby step; avoid dealing with complicate function in the design at the beginning.

Write her own C# code: 


GALS show - favorite time to spend 30 minutes

July 23, 2016

Spend 1 hour to watch twice, write down a few notes:

https://channel9.msdn.com/Shows/GALs/Interview-with-Kathleen-Hogan

Growth mind set - fixed mind set vs growth mind set, showing more

Julia likes to read article:
http://www.bizjournals.com/seattle/print-edition/2015/11/20/kathleen-hogan.html

https://www.linkedin.com/pulse/empower-your-employees-leverage-own-data-kathleen-hogan?trk=prof-post

https://www.linkedin.com/pulse/how-millennials-changing-workforce-three-data-trends-im-hogan?trk=prof-post


Enjoy Vancouver city video 2 minutes, and browse through Microsoft office building in Vancouver through the video.

http://blogs.microsoft.com/jobs/new-microsoft-offices-boast-ultramodern-design-stunning-views/?WT.tsrc=MSSharing


Thursday, July 21, 2016

Merge N sorted Array - merge sort from bottom-up implementation

July 21, 2016

   Merge N sorted arrays - C# practice



Time complexity analysis:

N sorted array, each array length is k, so the time complexity:

2k * N/2 + 4k * N/4 +... + 2^logN k * N/ (2^logN) = kN logN,

Today's research topic:
Coding is more of science, or just muscle memory, go for the intuition.

Review:
1. bottom-up implementation merge sort

2. Master Theorem - T(N) = 2T(N/2) + O(N)

3. Time complexity - come out kNlogN analysis

Read the article, and understand better about merge sort:

https://en.wikipedia.org/wiki/Merge_sort

Keywords:

Bitonic Mergesort
external merge sort - disk/ tape drives
external merge sort
merge sort
  - not inplace - must be allocated for the sorted output to be stored in
parallel mergesort
polyphase merge sort
natural merge sort - similar to a bottom up merge sort
stable sort,
TimSort,
tiled merge sort algorithm

Bottom-up implementation
Top-down implementation

comparison-based sorting algorithm

master theorem

average and worst-case performance

Lecture notes study:  (work on lecture notes, write down some interesting topic, do some research!)
http://algs4.cs.princeton.edu/lectures/22Mergesort.pdf

Actionable item:
Add study time 10 minutes for computer science, review theorems, lecture notes when writing a new blog.