Caching

One of the most important concepts in Computer Science and Computer Engineering is caching. In fact, at work a lot of the improvements that we work on when it comes to performance improvements has to do with caching. Simply put, caching things on memory using a data structure such as hash tables can really go a long way in saving precious (milli)seconds.

The problem below exemplifies the caching technique. Notice the use of a static (class-level instead of object-level variable) hash table designed to keep the results of calculations for each n. Then before re-calculating it, I was checking whether the calculation has already been done. Check out how fast is runs. Hope you enjoy, code is down below, cheers, ACC.

Find the Punishment Number of an Integer - LeetCode

2698. Find the Punishment Number of an Integer
Medium

Given a positive integer n, return the punishment number of n.

The punishment number of n is defined as the sum of the squares of all integers i such that:

  • 1 <= i <= n
  • The decimal representation of i * i can be partitioned into contiguous substrings such that the sum of the integer values of these substrings equals i.

 

Example 1:

Input: n = 10
Output: 182
Explanation: There are exactly 3 integers i that satisfy the conditions in the statement:
- 1 since 1 * 1 = 1
- 9 since 9 * 9 = 81 and 81 can be partitioned into 8 + 1.
- 10 since 10 * 10 = 100 and 100 can be partitioned into 10 + 0.
Hence, the punishment number of 10 is 1 + 81 + 100 = 182

Example 2:

Input: n = 37
Output: 1478
Explanation: There are exactly 4 integers i that satisfy the conditions in the statement:
- 1 since 1 * 1 = 1. 
- 9 since 9 * 9 = 81 and 81 can be partitioned into 8 + 1. 
- 10 since 10 * 10 = 100 and 100 can be partitioned into 10 + 0. 
- 36 since 36 * 36 = 1296 and 1296 can be partitioned into 1 + 29 + 6.
Hence, the punishment number of 37 is 1 + 81 + 100 + 1296 = 1478

 

Constraints:

  • 1 <= n <= 1000

private static Hashtable htPunishmentNumbers = null;
public int PunishmentNumber(int n)
{
    if (Solution.htPunishmentNumbers == null) Solution.htPunishmentNumbers = new Hashtable();

    int retVal = 0;
    for (int i = 1; i <= n; i++)
        if (IsPunishmentNumber(i)) retVal += (i * i);

    return retVal;
}

private bool IsPunishmentNumber(int n)
{
    if (Solution.htPunishmentNumbers.ContainsKey(n)) return (bool)Solution.htPunishmentNumbers[n];

    bool bVal = IsPunishmentNumber((n * n).ToString(), 0, 0, n);
    Solution.htPunishmentNumbers.Add(n, bVal);
    return bVal;
}

private bool IsPunishmentNumber(string str,
                                int index,
                                int sum,
                                int target)
{
    if (index >= str.Length) return sum == target;

    int currentNum = 0;
    bool retVal = false;
    for (int i = index; i < str.Length; i++)
    {
        currentNum = 10 * currentNum + (int)(str[i] - '0');
        if (sum + currentNum <= target)
        {
            if (IsPunishmentNumber(str, i + 1, sum + currentNum, target))
            {
                retVal = true;
                break;
            }
        }
        else
        {
            retVal = false;
            break;
        }
    }

    return retVal;
}

Comments

Popular posts from this blog

Advent of Code - Day 6, 2024: BFS and FSM

Golang vs. C#: performance characteristics (simple case study)

Advent of Code - Day 7, 2024: Backtracking and Eval