Posts

Showing posts from June, 2022

From N! to N^2 (Dynamic Programming) - Part II

Image
I tried this problem using brute-force but it was clear that it wouldn't work with an input of num=3000. Complexity for the brute-force was 3000 factorial. Instead, this was a problem similar to counting coins or solving Diophantine equations: if you manage to have all the solutions from 0..num-1, you can find the solution for num. Notice that the solution for num will require an extra loop from i=1..num where you're going to compare the current solution for num dp[num] to all solutions dp[num-i], assuming that i ends with the digit k. The two loops together lead to an num^2 solution, which runs very quick given the small num. Solution is down below, Merci, ACC. Sum of Numbers With Units Digit K - LeetCode 2310. Sum of Numbers With Units Digit K Medium 166 203 Add to List Share Given two integers  num  and  k , consider a set of positive integers with the following properties: The units digit of each integer is  k . The sum of the integers is  num . Return  the  minimum  possib

The power of pruning in backtracking

Image
This problem, given the small constraints and the hint, is asking to generate all the possibilities. The recursion becomes exponential fairly quickly. Basically at each iteration, you have to: 1/ Simulate adding the current cookie to the current child, and recursive to the next child 2/ Simulate adding the current cookie to the current child, and recursive to the current child 3/ Simulate skipping adding at all, and go to the next child In order to avoid TLE like it happened below, add the following pruning rule: a/ The moment that the unfairnness is higher than the min, return and break the search That does the trick. Code is down below, cheers, ACC. Fair Distribution of Cookies - LeetCode 2305. Fair Distribution of Cookies Medium 336 27 Add to List Share You are given an integer array  cookies , where  cookies[i]  denotes the number of cookies in the  i th  bag. You are also given an integer  k  that denotes the number of children to distribute  all  the bags of cookies to. All the c

Ramanujan Third Problem

Image
A person born here would revolutionize mathematics forever. Would give a different perspective to the way we interpret infinite sums and series. Would come up with novel ways to think about hard mathematical problems such as the Partition Problem. A person born here, raised by his mother alone, with no higher education, no computers to assist him, just thru hard relentless work, would accomplish so much in such a short period of time. I continue to be amazed by the work and life of Srinivasa Ramanujan . I think one of his most important contributions was to come up with a formula for the Partition Problem: What fascinates me the most is that mathematicians like him never had the power of computers at their disposal. One of the anecdotes that is very interesting from Ramanujan happened when he was already very ill in England and was visited by G. H. Hardy who rode a taxi numbered 1729. When he mentioned this to Ramanujan, he hoped that this wasn't a sign of bad luck given that "