|
| 1 | +packagecom.fishercoder.solutions; |
| 2 | + |
| 3 | +importjava.util.ArrayList; |
| 4 | +importjava.util.HashSet; |
| 5 | +importjava.util.List; |
| 6 | +importjava.util.Set; |
| 7 | + |
| 8 | +/** |
| 9 | + * 667. Beautiful Arrangement II |
| 10 | + * |
| 11 | + * Given two integers n and k, you need to construct a list which contains n different positive integers ranging from 1 to n |
| 12 | + * and obeys the following requirement: |
| 13 | + * Suppose this list is [a1, a2, a3, ... , an], |
| 14 | + * then the list [|a1 - a2|, |a2 - a3|, |a3 - a4|, ... , |an-1 - an|] has exactly k distinct integers. |
| 15 | + * If there are multiple answers, print any of them. |
| 16 | +
|
| 17 | + Example 1: |
| 18 | +
|
| 19 | + Input: n = 3, k = 1 |
| 20 | + Output: [1, 2, 3] |
| 21 | + Explanation: The [1, 2, 3] has three different positive integers ranging from 1 to 3, and the [1, 1] has exactly 1 distinct integer: 1. |
| 22 | +
|
| 23 | + Example 2: |
| 24 | +
|
| 25 | + Input: n = 3, k = 2 |
| 26 | + Output: [1, 3, 2] |
| 27 | + Explanation: The [1, 3, 2] has three different positive integers ranging from 1 to 3, and the [2, 1] has exactly 2 distinct integers: 1 and 2. |
| 28 | +
|
| 29 | + Note: |
| 30 | +
|
| 31 | + The n and k are in the range 1 <= k < n <= 104. |
| 32 | + */ |
| 33 | + |
| 34 | +publicclass_667 { |
| 35 | + |
| 36 | +publicstaticclassSolutoin1 { |
| 37 | +/**This brute force solution will result in TLE as soon as n = 10 and k = 4.*/ |
| 38 | +publicint[]constructArray(intn,intk) { |
| 39 | +List<List<Integer>>allPermutaions =findAllPermutations(n); |
| 40 | +int[]result =newint[n]; |
| 41 | +for (List<Integer>perm :allPermutaions) { |
| 42 | +if (isBeautifulArrangement(perm,k)) { |
| 43 | +convertListToArray(result,perm); |
| 44 | +break; |
| 45 | +} |
| 46 | +} |
| 47 | +returnresult; |
| 48 | +} |
| 49 | + |
| 50 | +privatevoidconvertListToArray(int[]result,List<Integer>perm) { |
| 51 | +for (inti =0;i <perm.size();i++) { |
| 52 | +result[i] =perm.get(i); |
| 53 | +} |
| 54 | +} |
| 55 | + |
| 56 | +privatebooleanisBeautifulArrangement(List<Integer>perm,intk) { |
| 57 | +Set<Integer>diff =newHashSet<>(); |
| 58 | +for (inti =0;i <perm.size() -1;i++) { |
| 59 | +diff.add(Math.abs(perm.get(i) -perm.get(i +1))); |
| 60 | +} |
| 61 | +returndiff.size() ==k; |
| 62 | +} |
| 63 | + |
| 64 | +privateList<List<Integer>>findAllPermutations(intn) { |
| 65 | +List<List<Integer>>result =newArrayList<>(); |
| 66 | +backtracking(newArrayList<>(),result,n); |
| 67 | +returnresult; |
| 68 | +} |
| 69 | + |
| 70 | +privatevoidbacktracking(List<Integer>list,List<List<Integer>>result,intn) { |
| 71 | +if (list.size() ==n) { |
| 72 | +result.add(newArrayList<>(list)); |
| 73 | +return; |
| 74 | +} |
| 75 | +for (inti =1;i <=n;i++) { |
| 76 | +if (list.contains(i)) { |
| 77 | +continue; |
| 78 | +} |
| 79 | +list.add(i); |
| 80 | +backtracking(list,result,n); |
| 81 | +list.remove(list.size() -1); |
| 82 | +} |
| 83 | +} |
| 84 | +} |
| 85 | + |
| 86 | +publicstaticclassSolutoin2 { |
| 87 | +/**This is a very smart solution: |
| 88 | + * First, we can see that the max value k could reach is n-1 which |
| 89 | + * comes from a sequence like this: |
| 90 | + * when n = 8, k = 5, one possible sequence is: |
| 91 | + * 1, 8, 2, 7, 3, 4, 5, 6 |
| 92 | + * absolute diffs are: |
| 93 | + * 7, 6, 5, 4, 1, 1, 1 |
| 94 | + * so, there are total 5 distinct integers. |
| 95 | + * |
| 96 | + * So, we can just form such a sequence by putting the first part first and |
| 97 | + * decrement k along the way, when k becomes 1, we just put the rest numbers in order.*/ |
| 98 | +publicint[]constructArray(intn,intk) { |
| 99 | +int[]result =newint[n]; |
| 100 | +intleft =1; |
| 101 | +intright =n; |
| 102 | +for (inti =0;i <n &&left <=right;i++) { |
| 103 | +if (k >1) { |
| 104 | +result[i] =k-- %2 !=0 ?left++ :right--; |
| 105 | +}else { |
| 106 | +result[i] =k %2 !=0 ?left++ :right--; |
| 107 | +} |
| 108 | +} |
| 109 | +returnresult; |
| 110 | +} |
| 111 | +} |
| 112 | +} |