Seawolf 发表于 2019-8-14 06:32:09

Google foobar challenge

本帖最后由 Seawolf 于 2019-8-14 06:36 编辑

Level 3

The Grandest Staircase Of Them All

With her LAMBCHOP doomsday device finished, Commander Lambda is preparing for her debut on the galactic stage - but in order to make a grand entrance, she needs a grand staircase! As her personal assistant, you've been tasked with figuring out how to build the best staircase EVER.

Lambda has given you an overview of the types of bricks available, plus a budget. You can buy different amounts of the different types of bricks (for example, 3 little pink bricks, or 5 blue lace bricks). Commander Lambda wants to know how many different types of staircases can be built with each amount of bricks, so she can pick the one with the most options.

Each type of staircase should consist of 2 or more steps. No two steps are allowed to be at the same height - each step must be lower than the previous one. All steps must contain at least one brick. A step's height is classified as the total amount of bricks that make up that step. For example, when N = 3, you have only 1 choice of how to build the staircase, with the first step having a height of 2 and the second step having a height of 1: (# indicates a brick)

#
##
21

When N = 4, you still only have 1 staircase choice:

#
#
##
31

But when N = 5, there are two ways you can build a staircase from the given bricks. The two staircases can have heights (4, 1) or (3, 2), as shown below:

#
#
#
##
41

#
##
##
32

Write a function called answer(n) that takes a positive integer n and returns the number of different staircases that can be built from exactly n bricks. n will always be at least 3 (so you can have a staircase at all), but no more than 200, because Commander Lambda's not made of money!

Inputs:

(int) n = 3
Output:

(int) 1
Inputs:

(int) n = 200
Output:

(int) 487067745


//using divide and conquer
//divide a number into two numbers for example 7- 16,25,34
//the look at the last digit to see if it is can be divide into two numbers6 - 24
//and do it recursively, this method is not optimal, I am sure we can find a better one together

package com.company;

import java.util.ArrayList;

public class Main {

    // the container to store those solutions
    private static ArrayList<String> memo = new ArrayList<String>();

    // every single possible solution and it will be overwritten after each solution is finished
    private static String result = "";

    //total number of the solution
    private static int count = 0;

    private static int init = 0;


    public static void main(String[] args) {

      // test number
      int n = 10;

      solution1(n);
      solution1(n);
      

      //print all the solutions and the number of solutions
      print_list(memo);
      System.out.println(count);

//      return count;a

    }


    //contruct the first time situation
    private static void solution1(int n) {

      if (init == 0) {

            for (int i = 1; i < n - 1; i++) {

                if (i < n - i) {

                  int re = n - i;

                  result = result + i;

                  result = result + ",";

                  result = result + re;

                  result = result + ",";



                  if(!memo.contains(result)){

                        count++;

                        memo.add(result);


                  }

                  result = "";

                }



            }

//            print_list(memo);
            init ++;
      }else{



            for(int i = 0; i < memo.size(); i ++){

                // the string array to store each digit, adding a coma is because consider the 11 case
                // there is possible to be 1 and 10, for a string it cannot tell if it is one digit number or two
                String[] test = memo.get(i).substring(0,memo.get(i).length()-1).split(",");

//            for (String a: test) {
//
//                System.out.print(a);
//
//            }
//
//            System.out.println();

                //get last digit
                String last =test;

                //get last second digit
                String lastsec = test;

                //get former digit beside the last one
                String former = "";

                //construct the former digit
                for(int x = 0 ; x <test.length -1; x++){

                  if(x != 0){

                        former =former + ",";

                  }


                  former =former + test;

                }

//            System.out.println(last);
//
//            System.out.println(lastsec);
//
//            System.out.println("----"+ former);

                //convert last digit string to an integer
                int lst = Integer.parseInt(last);


                //convert last second digit string to an integer
                int lstsec = Integer.parseInt(lastsec);


                //construct each possible solution for more than one time situation
                if( lst >= 2* lstsec+ 3){

                  for (int j = 1; j < lst - 1; j++) {

                        if (j < lst - j && j >lstsec) {

                            int re = lst - j;

                            result = result + former;

                            result = result +",";

                            result = result + j;

                            result = result +",";

                            result = result + re;

                            result = result +",";


                            if(!memo.contains(result)){

                              count ++;

                              memo.add(result);

//                              System.out.println(result);

                            }


                            result = "";

                        }
                  }

                }

            }

      }

    }

    //print the list
    private static void print_list(ArrayList memo){

      for(int i = 0 ; i<memo.size();i++){

            System.out.println(memo.get(i).toString().replaceAll(",",""));

      }
    }



}


这个code可以算出所有的case,但是当n足够大的时候,系统会run out of memory,所以有一下solution:

import java.lang.Math;

public class PartitionCounter
{
public static void main(String[] args)

{

    int F[] = new int;

    F=1;

    F=1;

    for(int n=2; n<201; n++){


      for(int k=200; k>n-1; k--){

      F = F+F;

      }

    }

    System.out.print("[");
    for(int k=0; k<200; k++){
      System.out.print((F-1) + ", ");
    }
    System.out.print("]");
}
}
页: [1]
查看完整版本: Google foobar challenge