January 1, 2017
Plan to attend 8:00 - 11:00am hackerearth.com
January Easy '17 contest
Work on the algorithm:
January Clash '2017
- One of 6 algorithms: Simple Function
In contest performance, C# solution, pass test case 1, but timeout all other cases. (9:00am - 11:00am)
C# solution, passed 5 test cases, fail last one. (after the contest, 2+ hours)
Study code C++
Write C# code using the above solution. (Time spent: 4 + hours, completed at 6:37pm)
Code Review Link is here. Julia spent more than one hour on January 3, 2016 to add her calculation of cache size reduction from 2MB to 80KB based on the comment from Paparazzi
January 7, 2016
Continue to do some research on C# Dictionary comparison to self-defined hash function to do pre-processing.
Using Dictionary<string, int>
C# with Dictionary code:
Test result from hackerearth.com:
The time complexity analysis:
function CalculateSumOfEvenNumbers(), inside two loops, function GetLastDigit(...)
is called; inside GetLastDigit() function, there is a HashSet to prevent
duplicate calculation, and then, GetLastDigit() is called; inside GetLastDigit(),
two calls of the function GetDigits(). Actually, we only care about two integer
number, biggest same digit, and we do not care about how many digit inside
each integer. And this approach goes over each digit in the integer.
Self-defined hash function
Comparison to the self-defined hash function - code is here:
The performance:
Time analysis:
For each query, process two baskets of integers, two separate loops, O(N1 + N2), detail see function ProcessInput(...); For each number, call Hash function to process, any number is at most 4 digits, each digit, only do one calculation to determine the digit from 1 to 9. Just a minus arithmetic calculation. Detail see the function Hash(char[] cache, int nthNumber, int serialNo)
To calculate the even number count, call function CheckNumberIsEven(...) N1 * N2 times, each function call, only need to look up at most 10 times, find a match then break the loop.
So, overall, time complexity is O(N1+N2) + O( N1*N2), is around O(10^6) calculation of CheckNumberIsEven function which includes a few of arithmetic calculations; the self-defined hash function is almost less than 1 percent of time to compare calculation the even number count.
Digits Internal Class
Another C# version, no Dictionary, just use int[10] to store an integer, with digits. No string manipulation to get the substring. Pass all test cases. Code is here.
Performance:
HashedInteger Class
Digits class name is not very meaningful, so HashedInteger class name is chosen to replace Digits.
And also Save API is not clear, add Convert, ConvertAll APIs instead.
The code is here.
From January 2015, she started to practice leetcode questions; she trains herself to stay focus, develops "muscle" memory when she practices those questions one by one. 2015年初, Julia开始参与做Leetcode, 开通自己第一个博客. 刷Leet code的题目, 她看了很多的代码, 每个人那学一点, 也开通Github, 发表自己的代码, 尝试写自己的一些体会. She learns from her favorite sports – tennis, 10,000 serves practice builds up good memory for a great serve. Just keep going. Hard work beats talent when talent fails to work hard.
Showing posts with label HackerEarth. Show all posts
Showing posts with label HackerEarth. Show all posts
Sunday, January 1, 2017
Monday, September 26, 2016
HackerEarth: algorithm tutorial and problems
Sept. 26, 2016
Julia has some difficulty to handle medium, advanced, hard and difficult algorithm on HackerRank contest world code sprint, so she is actively seeking ways to improve.
Try this one - HackerEarth, at least read each algorithm and find typical problem sets; if need, she can quickly jump into the code. She tries to shorten the time from learning the algorithm to working out an algorithm in 8 hours. So she can compete in the contest.
It is hard to compete in the contest right now, to cover her weakness, she has to dedicate a lot of time for a medium level algorithm. She has to give up weekend sports activities, has to focus on the contest whole weekend, nonstop.
One algorithm a time. One contest a time.
It does not matter how hard Julia has to work on contest, at the end of day - more than 8 hours work, she has to be able to come out a solution for a medium algorithm on HackerRank contest.
The contest problem solving is very similar to the work environment; 48 hours is much better than 24 hours. Julia likes the challenges, she just likes to compete, also she thinks that it is very healthy to compete, so she can improve performance in short time. She could not learn these from the work.
HackerRank is very strict on scoring, therefore, Julia is pushed so hard to bring out her best talent, relentless trial and errors, patiently through 8 hours a time.
Here is the website:
https://www.hackerearth.com/practice/algorithms/dynamic-programming/introduction-to-dynamic-programming-1/tutorial/
Recommended by
https://goo.gl/K3iYRY
Julia has some difficulty to handle medium, advanced, hard and difficult algorithm on HackerRank contest world code sprint, so she is actively seeking ways to improve.
Try this one - HackerEarth, at least read each algorithm and find typical problem sets; if need, she can quickly jump into the code. She tries to shorten the time from learning the algorithm to working out an algorithm in 8 hours. So she can compete in the contest.
It is hard to compete in the contest right now, to cover her weakness, she has to dedicate a lot of time for a medium level algorithm. She has to give up weekend sports activities, has to focus on the contest whole weekend, nonstop.
One algorithm a time. One contest a time.
It does not matter how hard Julia has to work on contest, at the end of day - more than 8 hours work, she has to be able to come out a solution for a medium algorithm on HackerRank contest.
The contest problem solving is very similar to the work environment; 48 hours is much better than 24 hours. Julia likes the challenges, she just likes to compete, also she thinks that it is very healthy to compete, so she can improve performance in short time. She could not learn these from the work.
HackerRank is very strict on scoring, therefore, Julia is pushed so hard to bring out her best talent, relentless trial and errors, patiently through 8 hours a time.
Here is the website:
https://www.hackerearth.com/practice/algorithms/dynamic-programming/introduction-to-dynamic-programming-1/tutorial/
Recommended by
https://goo.gl/K3iYRY
Saturday, February 27, 2016
HackerEarth: first algorithm practice - Milly Chocolate
Feb. 26, 2016
Spent more than 1 hour to work on an algorithm problem on HackerEarth.
Problem statement:
https://www.hackerearth.com/druva-sdet-hiring-challenge/algorithm/milly-and-chocolates-4/
Milly loves to eat chocolates. She buys only those food items which contain some amount or percentage of chocolate in it. She has purchased N such food items and now she is planning to make a new food item by her own. She will take equal proportions of all of these N food items and mix them. Now she is confused about the percentage of chocolate that this new food item will have. Since she is busy in eating the chocolates so you have to help her in this task.
Note : Your answer should be exactly up to 8 decimal places which means that if your answer is 2.357 then you have to print 2.35700000 or if your answer is 2.66666666 .... then you have to print 2.66666667
Julia's practice:
1 second for each file, but, the time is over 1 second. Need to work on speed! (Julia likes the challenge! )
https://github.com/jianminchen/hackerEarth/blob/master/MillyandChocolates/MillyAndChocolate.cs
Julia read editorial of algorithm, and know the better solution:
https://www.hackerearth.com/problem/algorithm/milly-and-chocolates-4/editorial/
(will try her own version as well later. Definitely, the code will take less time to write! Good learning tool - Thanks, hackerEarth!)
So, to fix TLE error ( more than 1 second for each file), Julia wrote a bug free version:
https://github.com/jianminchen/hackerEarth/blob/master/MillyandChocolates/MillyAndChocolate_BugFix.cs
Read this web page:
https://www.hackerearth.com/@pranjuldb/activity/hackerearth/
Julia's hackerEarth profile:
https://www.hackerearth.com/@jianminchen.fl
Spent more than 1 hour to work on an algorithm problem on HackerEarth.
Problem statement:
https://www.hackerearth.com/druva-sdet-hiring-challenge/algorithm/milly-and-chocolates-4/
Milly loves to eat chocolates. She buys only those food items which contain some amount or percentage of chocolate in it. She has purchased N such food items and now she is planning to make a new food item by her own. She will take equal proportions of all of these N food items and mix them. Now she is confused about the percentage of chocolate that this new food item will have. Since she is busy in eating the chocolates so you have to help her in this task.
Input
First line of the input will contain T (no. of test cases). Every test case will contain two lines. First line will contain N (no. of food items) and the second line will contain N space separated Pi values denoting the percentage of chocolate in ith food item.Output
For every test case, print the percentage of chocolate that will be present in the new food item.Note : Your answer should be exactly up to 8 decimal places which means that if your answer is 2.357 then you have to print 2.35700000 or if your answer is 2.66666666 .... then you have to print 2.66666667
Constraints
1 <= T <= 5
1 <= N <= 5*105
0 <= Pi <= 100
1 <= N <= 5*105
0 <= Pi <= 100
Time Limit: 1 sec(s) for each input file.
Memory Limit: 256 MB
Source Limit: 1024 KB
Marking Scheme: Marks are awarded if any testcase passes.
Allowed Languages: C, CPP, CLOJURE, CSHARP, GO, HASKELL, JAVA, JAVASCRIPT, JAVASCRIPT_NODE, LISP, OBJECTIVEC, PASCAL, PERL, PHP, PYTHON, RUBY, R, RUST, SCALA
1 second for each file, but, the time is over 1 second. Need to work on speed! (Julia likes the challenge! )
https://github.com/jianminchen/hackerEarth/blob/master/MillyandChocolates/MillyAndChocolate.cs
Julia read editorial of algorithm, and know the better solution:
https://www.hackerearth.com/problem/algorithm/milly-and-chocolates-4/editorial/
(will try her own version as well later. Definitely, the code will take less time to write! Good learning tool - Thanks, hackerEarth!)
So, to fix TLE error ( more than 1 second for each file), Julia wrote a bug free version:
https://github.com/jianminchen/hackerEarth/blob/master/MillyandChocolates/MillyAndChocolate_BugFix.cs
Read this web page:
https://www.hackerearth.com/@pranjuldb/activity/hackerearth/
Julia's hackerEarth profile:
https://www.hackerearth.com/@jianminchen.fl
Subscribe to:
Posts (Atom)


