Tower Breakers - The Final Battle

Sort by

recency

|

19 Discussions

|

  • + 0 comments

    Didn't see any solution in JS, so here you go.

    // Creating a memoization cache for the nmax function
    const cache = new Map();
    
    // Defining the nmax function to calculate the maximum number of piles
    function nmax(cost) {
      if (cost < 4) return 1;
      if (cache.has(cost)) return cache.get(cost);
    
      let sum = 0;
      for (let pile = 1; pile <= Math.floor(Math.sqrt(cost)); pile++) {
        sum += nmax(cost - Math.pow(pile, 2));
      }
    
      cache.set(cost, sum);
      return sum;
    }
    
    // Defining the towerBreakers function to find the minimum cost
    function towerBreakers(n) {
      for (let cost = 0; ; cost++) {
        if (nmax(cost) >= n) return cost;
      }
    }
    
  • + 0 comments

    And here's another solution. How it works is explained further down, see the entry of @kiwic.

    ''' How many stones (maximum) can you fit in for given cost? 
        -> nmax(cost)
        Obviously every pile has to give this same cost, that is 
         -> nmax(cost-1^2) + nmax(cost-2^2)+...
    '''
    
    from functools import cache
    from itertools import count
    
    @cache
    def nmax(cost):
        if cost < 4: return 1
        return sum(nmax(cost-pile**2) 
    		                      for pile in range(1,int(math.sqrt(cost))+1))
                
    def towerBreakers(n):
        for cost in count():
            if nmax(cost)>=n: return cost
    
  • + 0 comments

    Here is Tower Breakers - The Final Battle problem solution in Python Java C++ and C programming - https://programs.programmingoneonone.com/2021/07/hackerrank-tower-breakers-the-final--battle-problem-solution.html

  • + 0 comments

    This is not a hard algorithm, rather it's a horrible game, hard to understand & poorly explained by the platform.

  • + 0 comments

    Precalculating all values in range for the inverse of the function you're actually interested in feels like a weird solution until you realize said inverse grows exponentially . . . I think. Frankly I wasn't very rigorous in my argument to myself that it isn't superexponential.