Groupon面经Prepare: Max Cycle Length

题目是遇到偶数/2,遇到奇数 *3 + 1的题目,然后找一个range内所有数字的max cycle length。对于一个数字,比如说44,按照题目的公式不停计算,过程是 44, 22, 11, 8, 9 ,1(瞎起的),从44到1的这个sequence的长度,叫做cycle length。然后题目是给一个range,比如[2,300],求这里面所有数字的cycle length的最大值。follow up跑1到1 million

一个数一个数慢慢算,如下算法求出每个数的cycle length

 1 package Sorting;
 2 import java.util.*;
 3
 4 public class Solution2 {
 5     public List<Integer> maxCycleLen(int min, int max) {
 6         List<Integer> maxCycle = new ArrayList<Integer>();
 7         for (int i=min; i<=max; i++) {
 8             helper(maxCycle, i);
 9         }
10         return maxCycle;
11     }
12
13     public void helper(List<Integer> maxCycle, int num) {
14         int len = 1;
15         while (num != 1) {
16             if (num%2 == 1) num = num*3+1;
17             else num = num/2;
18             len++;
19         }
20         maxCycle.add(len);
21     }
22
23     /**
24      * @param args
25      */
26     public static void main(String[] args) {
27         // TODO Auto-generated method stub
28         Solution2 sol = new Solution2();
29         List<Integer> res = sol.maxCycleLen(1, 100000);
30         System.out.println(res);
31     }
32
33 }

follow up: 建立一个Lookup table, 算过的数就不算了

 1 package Sorting;
 2 import java.util.*;
 3
 4 public class Solution3 {
 5     HashMap<Integer, Integer> map;
 6     public List<Integer> maxCycleLen(int min, int max) {
 7         List<Integer> maxCycle = new ArrayList<Integer>();
 8         map = new HashMap<Integer, Integer>();
 9         for (int i=min; i<=max; i++) {
10             helper(maxCycle, i);
11         }
12         return maxCycle;
13     }
14
15     public void helper(List<Integer> maxCycle, int num) {
16         int len = 1;
17         int numcopy = num;
18         while (num != 1) {
19             if (map.containsKey(num)) {
20                 len += map.get(num)-1;
21                 break;
22             }
23             if (num%2 == 1) num = num*3+1;
24             else num = num/2;
25             len++;
26         }
27         maxCycle.add(len);
28         map.put(numcopy, len);
29     }
30
31     /**
32      * @param args
33      */
34     public static void main(String[] args) {
35         // TODO Auto-generated method stub
36         Solution3 sol = new Solution3();
37         List<Integer> res = sol.maxCycleLen(1, 100000);
38         System.out.println(res);
39     }
40
41 }

或者

 1     public void helper(List<Integer> maxCycle, int num) {
 2         int len = 0;
 3         int numcopy = num;
 4         while (!map.containsKey(num)) {
 5             if (num == 1) {
 6                 map.put(1, 1);
 7                 maxCycle.add(1);
 8                 return;
 9             }
10             if (num%2 == 1) num = num*3+1;
11             else num = num/2;
12             len++;
13         }
14         len = len + map.get(num);
15         maxCycle.add(len);
16         map.put(numcopy, len);
17     }
时间: 2024-10-12 22:10:26

Groupon面经Prepare: Max Cycle Length的相关文章

Laravel 5.4 migrate报错:Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table `users` add unique `us ers_email_unique`(`email`))

Laravel 5.4 migrate报错:Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table `users` add unique `us     ers_email_unique`(`email`)) public function up() { Schema::create('users', function (Blu

Hive集成Mysql作为元数据时,提示错误:Specified key was too long; max key length is 767 bytes

在进行Hive集成Mysql作为元数据过程中,做完所有安装配置工作后,进入到hive模式,执行show databases:执行正常,接着执行show tables:时却报错. 关键错误信息如下: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Specified key was too long; max key length is 767 bytes) 具体操作信息如下: hive> show databases; OK

Specified key was too long; max key length is 767 bytes

启动hive后运行:SHOW TABLES 报错 hive> show tables; FAILED: Error in metadata: MetaException(message:Got exception: javax.jdo.JDODataStoreException An exception was thrown while adding/validating class(es) : Specified key was too long; max key length is 767

Specified key was too long; max key length is 767 bytes mysql

Specified key was too long; max key length is 767 bytes 说明: 执行当前 Web 请求期间,出现未经处理的异常.请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息. 异常详细信息: MySql.Data.MySqlClient.MySqlException: Specified key was too long; max key length is 767 bytes 给Context增加如下的注解 [DbConfigu

执行nova-manage db sync时出错,提示’Specified key was too long; max key length is 1000 bytes’

执行nova-manage db sync时出错: 2012-03-24 14:07:01 CRITICAL nova [-] (OperationalError) (1071, ‘Specified key was too long; max key length is 1000 bytes’) ‘\nCREATE TABLE user_project_association (\n\tcreated_at DATETIME, \n\tupdated_at DATETIME, \n\tdele

Hive创建表时报错,Specified key was too long; max key length is 767 bytes

报错信息如下: FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. MetaException(message:javax.jdo.JDODataStoreException: An exception was thrown while adding/validating class(es) : Specified key was too long; max key length

hive报错: Specified key was too long; max key length is 767 bytes

DataNucleus.Datastore (Log4JLogger.java:error(115)) - An exception was thrown while adding/validating class(es) : Specified key was too long; max key length is 767 bytescom.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Specified key was too

hive异常:创建MySQL时Specified key was too long; max key length is 1000 bytes

2015-11-13 14:44:44,681 ERROR [main]: DataNucleus.Datastore (Log4JLogger.java:error(115)) - An exception was thrown while adding/validating class(es) : Specified key was too long; max key length is 1000 bytescom.mysql.jdbc.exceptions.jdbc4.MySQLSynta

ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes

今天在MySQL 5.6版本的数据库中修改InnoDB表字段长度时遇到了"ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes"错误,第一次遇到这个错误,遂花了点学习.研究过.总结这个问题. 我们先来创建一个测试表,构造这样的错误. mysql> use MyDB; Reading table information for completion of table and colum