|
| 1 | +package com.fishercoder.solutions; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.HashSet; |
| 5 | +import java.util.List; |
| 6 | +import java.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 | +public class _667 { |
| 35 | + |
| 36 | + public static class Solutoin1 { |
| 37 | + /**This brute force solution will result in TLE as soon as n = 10 and k = 4.*/ |
| 38 | + public int[] constructArray(int n, int k) { |
| 39 | + List<List<Integer>> allPermutaions = findAllPermutations(n); |
| 40 | + int[] result = new int[n]; |
| 41 | + for (List<Integer> perm : allPermutaions) { |
| 42 | + if (isBeautifulArrangement(perm, k)) { |
| 43 | + convertListToArray(result, perm); |
| 44 | + break; |
| 45 | + } |
| 46 | + } |
| 47 | + return result; |
| 48 | + } |
| 49 | + |
| 50 | + private void convertListToArray(int[] result, List<Integer> perm) { |
| 51 | + for (int i = 0; i < perm.size(); i++) { |
| 52 | + result[i] = perm.get(i); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + private boolean isBeautifulArrangement(List<Integer> perm, int k) { |
| 57 | + Set<Integer> diff = new HashSet<>(); |
| 58 | + for (int i = 0; i < perm.size() - 1; i++) { |
| 59 | + diff.add(Math.abs(perm.get(i) - perm.get(i + 1))); |
| 60 | + } |
| 61 | + return diff.size() == k; |
| 62 | + } |
| 63 | + |
| 64 | + private List<List<Integer>> findAllPermutations(int n) { |
| 65 | + List<List<Integer>> result = new ArrayList<>(); |
| 66 | + backtracking(new ArrayList<>(), result, n); |
| 67 | + return result; |
| 68 | + } |
| 69 | + |
| 70 | + private void backtracking(List<Integer> list, List<List<Integer>> result, int n) { |
| 71 | + if (list.size() == n) { |
| 72 | + result.add(new ArrayList<>(list)); |
| 73 | + return; |
| 74 | + } |
| 75 | + for (int i = 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 | + public static class Solutoin2 { |
| 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 | + public int[] constructArray(int n, int k) { |
| 99 | + int[] result = new int[n]; |
| 100 | + int left = 1; |
| 101 | + int right = n; |
| 102 | + for (int i = 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 | + return result; |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments