LeetCode-667. Beautiful Arrangement II

标签: LeetCode Array
2人阅读 评论(0) 收藏 举报
分类:

Description

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.

Solution 1(C++)

class Solution {
public:
    vector<int> constructArray(int n, int k) {
        vector<int> res(1,1);
        int flag=0, dif=k, temp;
        while(res.size()<n){
            temp=res[res.size()-1]; 
            if(dif>=1){
                if(0==flag) {res.push_back(temp+dif); flag=1;}
                else {res.push_back(temp-dif); flag=0;}
                dif--;
            }
            else{
                res.push_back(res.size()+1);
            }
        }
        return res;
    }
};

Solution 2(C++)

class Solution {
public:
    vector<int> constructArray(int n, int k) {
        vector<int> res;
        int i = 1, j = n;
        while (i <= j) {
            if (k > 1) res.push_back(k-- % 2 ? i++ : j--);
            else res.push_back(i++);
        }
        return res;
    }
};

算法分析

1~n个数,任意两个数最小的差为1,最大的差为n-1,题目要求差有k个不同,这k个不同的差值,最小也为1,最大也为n-·1。题目要求给出一种可能的方法即可,所以,尝试差值为1,2,3,…,k-1.那么数列:1,k,2,2,k-1,…直到前后两个数差值为1.这个数列的个数为k,那么比n个数少的,直接在后面补齐即可。

此外,要注意,插值从k-1降到1,这是前面的数列构成,后面的额外补充的数列,添加res.size()+1。即可。这也是一个小技巧。

程序分析

略。

查看评论

Microsoft Agent技术在Delphi中的应用

解放军合肥炮兵学院 张建军 中保财险安徽省铜陵市分公司 韩莹 ---- Microsoft Agent是微软公司于1997年9月发布的一项代理软件开发技术,而后被集成到Internet Explore...
  • deardelphi
  • deardelphi
  • 2000-08-26 10:06:00
  • 689

667. Beautiful Arrangement II

Given two integers n and k, you need to construct a list which contains n different positive integ...
  • zjucor
  • zjucor
  • 2017-08-29 21:36:21
  • 604

leetcode 667. Beautiful Arrangement II

leetcode 667. Beautiful Arrangement II
  • feifeiiong
  • feifeiiong
  • 2017-08-28 19:37:33
  • 1210

LeetCode 667. Beautiful Arrangement II

Discription给定两个整数n和k,构建一个n个元素数组,数组要满足一下要求: 假设数组[a1,a2....an]那么[|a2-a1|,|a3-a2|.....]包含k个不同的整数。如果有多个...
  • sunlanchang
  • sunlanchang
  • 2017-08-27 16:39:35
  • 1063

Beautiful Arrangement II问题及解法

667. Beautiful Arrangement II LeetCode
  • u011809767
  • u011809767
  • 2017-08-28 11:25:55
  • 281

667. Beautiful Arrangement II(Java)

Given two integers n and k, you need to construct a list which contains n different positive integer...
  • Crystal_ting
  • Crystal_ting
  • 2017-09-04 22:25:31
  • 241

leetcode 667. Beautiful Arrangement II K个间隔的数据分组 + 双指针遍历

Given two integers n and k, you need to construct a list which contains n different positive integer...
  • JackZhang_123
  • JackZhang_123
  • 2017-12-22 11:05:42
  • 42

<medium>LeetCode Problem -- 667. Beautiful Arrangement II

描述:Given two integers n and k, you need to construct a list which contains n different positive inte...
  • m0_38088298
  • m0_38088298
  • 2018-02-01 17:13:37
  • 66

LeetCode笔记:526. Beautiful Arrangement

N个整数有多少种放法来达成“漂亮的安排”
  • Cloudox_
  • Cloudox_
  • 2017-03-14 09:33:27
  • 2086

[LeetCode]526. Beautiful Arrangement

https://leetcode.com/problems/beautiful-arrangement/#/description 找出数组每个元素均满足nums[i] % i == 0 || ...
  • gqk289
  • gqk289
  • 2017-03-14 17:08:17
  • 618
    个人资料
    持之以恒
    等级:
    访问量: 1万+
    积分: 2567
    排名: 1万+
    文章存档
    最新评论