|
| 1 | +package _20160820_1st_contest; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Collections; |
| 5 | +import java.util.Comparator; |
| 6 | +import java.util.Date; |
| 7 | +import java.util.List; |
| 8 | + |
| 9 | +import utils.CommonUtils; |
| 10 | +/** |
| 11 | + * Given an integer n, return 1 - n in lexicographical order. |
| 12 | +
|
| 13 | +For example, given 13, return: [1,10,11,12,13,2,3,4,5,6,7,8,9]. |
| 14 | +
|
| 15 | +Please optimize your algorithm to use less time and space. The input size may be as large as 5,000,000.*/ |
| 16 | +public class LexicographicalNumbers { |
| 17 | + //Radix sort doesn't apply here! Don't confuse myself! |
| 18 | + |
| 19 | + //rewrote their solution from Python to Java:https://discuss.leetcode.com/topic/54986/python-memory-limit-exceeded-for-problem-386/17 |
| 20 | + public static List<Integer> lexicalOrder(int n){ |
| 21 | + List<Integer> result = new ArrayList(); |
| 22 | + int i = 1; |
| 23 | + while(true){ |
| 24 | + result.add(i); |
| 25 | + if(i * 10 <= n){ |
| 26 | + i *= 10; |
| 27 | + } else { |
| 28 | + while(i%10 == 9 || i == n){ |
| 29 | + i /= 10; |
| 30 | + } |
| 31 | + if(i == 0) return result; |
| 32 | + i++; |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + //someone on Discuss hinted that you could use recursion to solve it in Java |
| 38 | + //then I wrote the following method, eventually, got all test cases produce the right output, but unfortunately TLE'ed by OJ |
| 39 | + public static List<Integer> lexicalOrder_LTE_by_10458(int n) { |
| 40 | + List<Integer> result = new ArrayList(); |
| 41 | + int insertPosition = 0; |
| 42 | + return addNumbers(result, 1, insertPosition, n); |
| 43 | + } |
| 44 | + |
| 45 | + private static List<Integer> addNumbers(List<Integer> result, int insertNumber, int insertPosition, int n) { |
| 46 | + int i; |
| 47 | + for(i = 0; i < 9; i++){ |
| 48 | + if(insertNumber+i > n) return result; |
| 49 | + result.add(insertPosition+i, insertNumber+i); |
| 50 | + if((insertNumber+i) % 10 == 0 && (insertNumber+i) == (insertNumber+10)) break; |
| 51 | + } |
| 52 | + while((insertNumber+i) % 10 != 0 && (insertNumber+i) <= n){ |
| 53 | + result.add(insertPosition+i, insertNumber+i); |
| 54 | + i++; |
| 55 | + } |
| 56 | + //find next insert position: |
| 57 | + insertPosition = result.indexOf((insertNumber+i)/10); |
| 58 | + return addNumbers(result, insertNumber+i, insertPosition+1, n); |
| 59 | + } |
| 60 | + |
| 61 | + public static void main(String...strings){ |
| 62 | + long lStartTime = new Date().getTime(); |
| 63 | + |
| 64 | +// CommonUtils.printList(lexicalOrder_TLE_by_23489(23489)); |
| 65 | +// List<Integer> result = lexicalOrder(1);//right |
| 66 | +// List<Integer> result = lexicalOrder(13);//right |
| 67 | +// List<Integer> result = lexicalOrder_LTE_by_10458(58); |
| 68 | +// List<Integer> result = lexicalOrder(120);//right |
| 69 | +// List<Integer> result = lexicalOrder(1200); |
| 70 | + List<Integer> result = lexicalOrder(10); |
| 71 | +// List<Integer> result = lexicalOrder_LTE_by_10458(10458); |
| 72 | +// List<Integer> result = lexicalOrder_LTE_by_10458(14959); |
| 73 | + long lEndTime = new Date().getTime(); |
| 74 | + long difference = lEndTime - lStartTime; |
| 75 | + System.out.println("Elapsed milliseconds: " + difference); |
| 76 | + System.out.println("result size is: " + result.size()); |
| 77 | + CommonUtils.printList(result); |
| 78 | + } |
| 79 | + |
| 80 | + /**The most naive way is to generate this list, sort it using a customized comparator and then return it. |
| 81 | + * Unfortunately, this results in TLE with this input: 23489*/ |
| 82 | + public static List<Integer> lexicalOrder_TLE_by_23489(int n) { |
| 83 | + List<Integer> result = new ArrayList(); |
| 84 | + for(int i = 1; i <= n; i++){ |
| 85 | + result.add(i); |
| 86 | + } |
| 87 | + Collections.sort(result, new Comparator<Integer>() { |
| 88 | + @Override |
| 89 | + public int compare(Integer o1, Integer o2) { |
| 90 | + String s1 = String.valueOf(o1); |
| 91 | + String s2 = String.valueOf(o2); |
| 92 | + return s1.compareTo(s2); |
| 93 | + } |
| 94 | + }); |
| 95 | + return result; |
| 96 | + } |
| 97 | + |
| 98 | +} |
0 commit comments