Showing posts with label hashset. Show all posts
Showing posts with label hashset. Show all posts

Monday, January 30, 2017

Code review - Hackerank Queen's Attack II

January 30, 2017

Queen's Attack II

Julia learned the hard way, she spent 3 hours in the contest and then worked out a solution. She wrote directions array and define 8 directions. But, she did not push her through to avoid any redundant code, spent time on intermediate results. Near 3 hours, she put together the C# code here in the contest.

She reviewed the statistics, people can finish the algorithm in less than 20 minutes.
The idea is to enumerate each direction by going through the array, and encapsulate all directions’ info inside the array, open for extension but close for change. All direction information update are limited to the two arrays only.

Avoid writing if statement for each direction.

Write again in less than 20 minutes, C# code.

Write again with two changes, C# code.
1. use implicit type local variable var
2. use HashSet<Tuple<int,int>> instead of HashSet<string>

Code review on stackexchange.com is here. 

Actionable Items:

Always spend extra 30 minutes to one hour, share the practice with the community on code review on stackexchange.com. Therefore, Julia has time to share her thoughts how she learns through her experience first, and then collect feedback. 

Most of important, Julia has to learn to work closely with the community, and be open in 2017. Do not be a  lone worker. It is your job to get involved with multiple communities. Find those communities first. Learn from WTA double ranking top 1 - Bethany.  Julia recently wrote a blog about Safrova's coach Rob Steckly, and watched her video about her coach's coaching style.

Bethany Mattek-Sands - be aggressive, and have fun - Safarova

Usually in tennis sports, double player is more open compared to the single player. Because the double players have to learn how to work with each other. Here is the dance she likes the two double champions most, they learn to work with each other, be aggressive and be happy, no matter the results. 

Julia also learns how good they are sharing how they work together. Here are 4 minutes talk.

Julia likes the marketing skills and styles, top 1 ranking WTA double player in January 2017, Bethany Mattek-Sands, she watched her practice in Beijing in Oct. 2016.



Wednesday, February 3, 2016

Algorithm: Count the number of palindromes in a string

Count the number of palindromes in  a string

January 28, 2016


    Write down ideas:
    1. First of all, do not count duplicate.
    2. Brute force solution:
 any substring of O(N^2) substrings to see if it is a palindrome;
 Add the substring of palindrome to a hashset if it is not in the hashset.
  And return the length of hashset
    3. Use recursive solution - using subproblem to solve. Cannot filter out duplicate - not good
    4. Better solution - use center point of string - 2n + 1, and then, go over each one, add all palindromes substring.

  Requirement: write a C# code in 10 minutes for the solution, using brute force one.

  https://github.com/jianminchen/AlgorithmsPractice/blob/master/NumberOfDistinctPalindromes.cs

Thursday, July 16, 2015

leetcode: longest substring without repeating characters

July 16, 2015 

Problem statement: 

Longest Substring Without Repeating Characters



Read the blog:


这算法写了很多次, 时间太长, 半年前, 有一次写了二个小时, 写不下去, 想法不好, 没有办法收场; 接着, 又写了四天, 每天二小时, 把上次代码拿出来看, 有什么问题, 改写.  过后想, 这样学习, 时间是浪费的, 打的是疲劳站; 工作中没有这么复杂的问题, 解决问题, 一定要评估复杂程度.

改变学习方式, 看以上的网页, 看Java的代码, 改写C#; 然后, 增加一些测试的内容, 帮助自己记忆算法的主要思想, 然后, 再改写; 用最简化的测试案例手工检测代码. 二三个小时搞定, 比半年前写的C#简单很多.

关键是用别人的想法, 又加快改写代码的速度, 改写后的代码更方便自己记忆. 这算法用移动窗口, 所以, 在代码中定义窗口起点, 长度, 什么时候决定更新窗口起点; 用一个数组(256字符)记录每个字符上次所在的位置, 然后, 对当前点, 窗口最右点, 判断是在窗口中出现没有; 间接判断, 看上次的位置在滑动窗口外还是里面.

学习别人的代码, 又练习自己改写代码, 写测试的内容, 多练习总是有新的体会.

Julia发现改写代码, 从其他语言到C#, 让她有机会快速练习写代码, 学习C#语言; 同时, 体会和其他语言的不同, 开阔眼界; 读的再多, 想法很好, 不会写代码实现, 或超过时间范围, 或写起来头痛, 太多错误, 都通过Leetcode训练, 有所提高. 

Share C# code:


First blog about the same problem:

http://juliachencoding.blogspot.ca/2015/06/longest-substring-without-repeating.html

"撑死胆大的,饿死胆小的. ", 虽然是个歇后语, 但是, 这道题目, 如果只考虑抽象思维, Hashset, 不具体讨论256字符等具体内容, 确实可以10 - 15分钟写出代码. 这是Julia练习的代码, 在第一次训练几个月之后. 

https://github.com/jianminchen/Leetcode_C-/blob/master/3LongestSubstringWithoutRepeating.cs