鱼C论坛

 找回密码
 立即注册

扫一扫,访问微社区

查看: 38|回复: 0

[学习笔记] Google foobar challenge

[复制链接]
最佳答案
36 
发表于 7 天前 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
本帖最后由 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)

  1. #
  2. ##
  3. 21
复制代码


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

  1. #
  2. #
  3. ##
  4. 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:

  1. #
  2. #
  3. #
  4. ##
  5. 41

  6. #
  7. ##
  8. ##
  9. 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


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

  5. package com.company;

  6. import java.util.ArrayList;

  7. public class Main {

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

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

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

  14.     private static int init = 0;


  15.     public static void main(String[] args) {

  16.         // test number
  17.         int n = 10;

  18.         solution1(n);
  19.         solution1(n);
  20.         

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

  24. //        return count;a

  25.     }


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

  28.         if (init == 0) {

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

  30.                 if (i < n - i) {

  31.                     int re = n - i;

  32.                     result = result + i;

  33.                     result = result + ",";

  34.                     result = result + re;

  35.                     result = result + ",";



  36.                     if(!memo.contains(result)){

  37.                         count++;

  38.                         memo.add(result);


  39.                     }

  40.                     result = "";

  41.                 }



  42.             }

  43. //            print_list(memo);
  44.             init ++;
  45.         }else{



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

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

  50. //            for (String a: test) {
  51. //
  52. //                System.out.print(a);
  53. //
  54. //            }
  55. //
  56. //            System.out.println();

  57.                 //get last digit
  58.                 String last =  test[test.length-1];

  59.                 //get last second digit
  60.                 String lastsec = test[test.length-2];

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

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

  65.                     if(x != 0){

  66.                         former =former + ",";

  67.                     }


  68.                     former =former + test[x];

  69.                 }

  70. //            System.out.println(last);
  71. //
  72. //            System.out.println(lastsec);
  73. //
  74. //            System.out.println("----"+ former);

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


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


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

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

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

  83.                             int re = lst - j;

  84.                             result = result + former;

  85.                             result = result +  ",";

  86.                             result = result + j;

  87.                             result = result +  ",";

  88.                             result = result + re;

  89.                             result = result +  ",";


  90.                             if(!memo.contains(result)){

  91.                                 count ++;

  92.                                 memo.add(result);

  93. //                                System.out.println(result);

  94.                             }


  95.                             result = "";

  96.                         }
  97.                     }

  98.                 }

  99.             }

  100.         }

  101.     }

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

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

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

  106.         }
  107.     }



  108. }
复制代码


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

  1. import java.lang.Math;

  2. public class PartitionCounter
  3. {
  4.   public static void main(String[] args)

  5.   {

  6.     int F[] = new int[201];

  7.     F[0]=1;

  8.     F[1]=1;

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


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

  11.         F[k] = F[k]+F[k-n];

  12.       }

  13.     }

  14.     System.out.print("[");
  15.     for(int k=0; k<200; k++){
  16.         System.out.print((F[k]-1) + ", ");
  17.     }
  18.     System.out.print("]");
  19.   }
  20. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

小甲鱼强烈推荐上一条 /1 下一条

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号

GMT+8, 2019-8-21 08:59

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表