[leetcode-667-Beautiful Arrangement II]

Given two integers n and k, you need to construct a list which contains n different positive integers ranging from 1 to n and obeys the following requirement: 
Suppose this list is [a1, a2, a3, ... , an], then the list [|a1 - a2|, |a2 - a3|, |a3 - a4|, ... , |an-1 - an|] has exactly k distinct integers.

If there are multiple answers, print any of them.

Example 1:

Input: n = 3, k = 1
Output: [1, 2, 3]
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.

Example 2:

Input: n = 3, k = 2
Output: [1, 3, 2]
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.

Note:

  1. The n and k are in the range 1 <= k < n <= 104.

思路:

The requirement of k distinct distance can be achieved from 1, 2, ..., k+1 (<= n), by the following strategy:

1, k+1, 2, k, 3, k-1 ...;
The distance of this sequence is k, k-1, k-2, ..., 2, 1

Then append the remaining numbers to the list.

vector<int> constructArray(int n, int k) {
        int l = 1, r = k+1;
        vector<int> ans;
        while (l <= r) {
            ans.push_back(l++);
            if (l <= r) ans.push_back(r--);
        }
        for (int i = k+2; i <= n; i++)
            ans.push_back(i);
        return ans;
    }

参考:

https://discuss.leetcode.com/topic/101135/c-concise-code-o-n

时间: 2024-10-01 05:45:04

[leetcode-667-Beautiful Arrangement II]的相关文章

【leetcode】667. Beautiful Arrangement II

题目如下: Given two integers n and k, you need to construct a list which contains ndifferent positive integers ranging from 1 to n and obeys the following requirement: Suppose this list is [a1, a2, a3, ... , an], then the list [|a1 - a2|, |a2 - a3|, |a3 

667. Beautiful Arrangement II

Given two integers n and k, you need to construct a list which contains n different positive integers ranging from 1 to n and obeys the following requirement: Suppose this list is [a1, a2, a3, ... , an], then the list [|a1 - a2|, |a2 - a3|, |a3 - a4|

667. Beautiful Arrangement II-- 类比526 但不能用back tracking

667 是很坑爹的一个题目,乍一看和 526 如出一辙, 526. Beautiful Arrangement 题意: 构造 [1,n]的排列,让每个 a[index] % index ==0 或者 index %a[index] ==0,  基本和 46 题一样,就是构造排列. 667题意: 给定两个整数n和k,构建一个n个元素数组,数组要满足一下要求: 假设数组[a1,a2....an]那么[|a2-a1|,|a3-a2|.....]包含k个不同的整数.如果有多个数组输出任意一个数组.1<=

Beautiful Arrangement II

这是一个中等题 题目: 思路: 我是创建一个新列表,列表最开始按一个最低,一个最高排列,如果p==k-1了,那么就停止,把剩下元素依次排序,比如n=10,k=3,我就先排1,然后把10加进去,这时p=1,再把2加进去,这时p=2,满足了p=k-1,然后把剩下元素按2,3,4,5,6,7,8,9排序加入列表中.之所以这么做是因为他的差值不会重复,1和10的差最大了,2和10的次大....... 代码: class Solution(object): def constructArray(self,

[LeetCode] Beautiful Arrangement

Suppose you have N integers from 1 to N. We define a beautiful arrangement as an array that is constructed by these N numbers successfully if one of the following is true for the ith position (1 <= i <= N) in this array: The number at the ith positi

LeetCode:Spiral Matrix II - 将元素1-n^2以螺旋序填充到矩阵

1.题目名称 Spiral Matrix(螺旋输出矩阵中的元素) 2.题目地址 https://leetcode.com/problems/spiral-matrix-ii/ 3.题目内容 英文:Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. 中文:给出一个整数n,生成一个矩阵,使用数字1到n^2以螺旋顺序填充这个矩阵 例如:给出n=3,则生成如下矩阵:

leetcode——Reverse Linked List II 选择链表中部分节点逆序(AC)

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n ≤ le

LeetCode——Pascal&#39;s Triangle II

Description: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. public class Solution { public List<Integer> getRow(int rowIndex) { List<List<Integer>> list = new ArrayList<List&

LeetCode --- 63. Unique Paths II

题目链接:Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one

LeetCode Linked List Cycle II

/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode *detectCycle(ListNode *head) { ListNode* fast = head; ListNode* slow = head;