【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 - 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.

解题思路:感觉自己对“给定一个条件,输出对应排列”这一类型的题目比较没有思路。本题的解法我也是想了很久才想出来。以[1,2,3,4,5,6]为例,当前k是等于1的;假设要k=2,只需要把6插入到头部[6,1,2,3,4,5]即可。而如果要k=3,那么把6插入到1的后面,变成[1,6,2,3,4,5]。接下来就是递推了,记插入6的位置为inx,要使得k=4,只要在k=2的基础上把最后一个元素插入到inx+2的位置;同理,如果是k=5,就在k=3的基础上操作。

代码如下:

class Solution(object):
    def constructArray(self, n, k):
        """
        :type n: int
        :type k: int
        :rtype: List[int]
        """
        res = [i for i in range(1,n+1)]
        inx = 0
        if k % 2 == 1:
            inx = 1
        else:
            res.insert(0, res.pop(-1))
            k -= 1
            inx += 2
        while k > 1:
            res.insert(inx,res.pop(-1))
            inx += 2
            k -= 2
        return res

原文地址:https://www.cnblogs.com/seyjs/p/10090261.html

时间: 2024-11-07 18:11:40

【leetcode】667. Beautiful Arrangement II的相关文章

【Leetcode】Linked List Cycle II

Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up: Can you solve it without using extra space? 思路:由[Leetcode]Linked List Cycle可知,利用一快一慢两个指针能够判断出链表是否存在环路.假设两个指针相遇之前slow走了s步,则fast走了2s步,并且fast已经在长度

【LeetCode】Reverse Linked List II

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 ≤ lengt

【Leetcode】Pascal&#39;s Triangle II

题目链接:https://leetcode.com/problems/pascals-triangle-ii/ 题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? 思路: 要求空间复杂度为O

【leetcode】Pascal&#39;s Triangle II (python)

其实每一行的结果是二项式展开的系数,但是考虑到当给定的参数过大的时候,在求组合的过程中会出现溢出(中间过程要用到乘法),但是这样的算法的时间复杂度是O(N),所以在参数不太大的时候,还是不错的. 这里用迭代的方法来求,当然复杂度就高了,是O(N^2),这里主要说下迭代时候的技巧,即在一个列表(数组)里进行迭代,实现如此的操作,要求在求下一行的时候,要从后往前进行,若是从前向后,就把后面要用的变量给改掉了,产生"脏"数据.从后向前不会(为何?),因为下一行比上一行多一个.(自己写写例子看

【LeetCode】113. Path Sum II 基于Java和C++的解法及分析

113. Path Sum II Total Accepted: 80509 Total Submissions: 284188 Difficulty: Medium Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8

【LeetCode】Single Number I &amp; II

Single Number I : Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? Solution: 解法不少,贴一种: 1 cla

【LeetCode】House Robber I &amp; II 解题报告

[题目] I You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it wil

【LeetCode】Combination Sum I &amp; II 解题报告

[Combination Sum I] Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (inc

【LeetCode】Reverse Linked List II 解题报告

[题目] 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