667. Beautiful Arrangement II

淘系商品的成长密码:历史与趋势透视 本文分析了淘系商品的发展历程,从早期的市场探索到如今的行业领先地位,深入探讨了技术创新、供应链优化、用户需求洞察等关键因素在淘系商品成功中的作用。通过对平台不断完善商品生态、提升购物体验和服务的过程分析,文章总结了淘系商品在竞争激烈的市场中如何通过创新应对挑战并推动电商行业的进步。未来,随着消费趋势变化和新兴模式的兴起,淘系商品将在更大范围内发挥其市场影响力。 阅读详情

标签(空格分隔): leetcode



1.原题

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.

2.分析:

只需要输出其中一种,所以,最直接的方法就是凑K出来就行。
注意到 1 <= k < n
也就是提示,range(n)一共最多有k种这类的差值,[(1, n), (2, n-1), (3, n-2)……]

3.代码实现过程中的一些坑:

当k是奇数的时候, 在排好k-1个差值的序列后, 接下来的值需要按照升序排
当k是偶数的时候, 在排好k-1个差值的序列后,接下来的值需要按照降序排

n = 5, k = 2
[1,5] + [4, 3, 2]


n = 5, k = 3
[1,5,2] + [3, 4]

4. 代码

class Solution {
public:
    vector<int> constructArray(int n, int k) {
        vector<int> ans;
        int j = 1;
        for (int i = 1; i <= k; i++){
            if (i%2){
                ans.push_back(j);
                j++;
            }
            else {
                ans.push_back(n - j + 2);
            }
        }
        if (k%2){
            for (int i = k+1; i <= n; i++){
                ans.push_back(j++);
            }
        }
        else {
            j = n-j+1;
            for (int i = k+1; i <= n; i++){
                ans.push_back(j--);
            }
        }
        return ans;
    }
};
基础入门篇 | YOLOv8 项目【训练】【验证】【推理】最简单教程 | YOLOv8必看 | 最新更新,直接打印 FPS,mAP50,75,95 三个文件搞定YOLOv8【训练】【验证】【推理】🚀🚀🚀 阅读详情

相关推荐

【语音识别入门】特征提取(Python完整代码)

给定一段音频,请提取12维MFCC特征和23维FBank,阅读代码预加重、分帧、加窗部分,完善作业代码中FBank特征提取和MFCC特征提取部分,并给出最终的FBank特征和MFCC特征,存储在纯文本中,用默认的配置参数,无需进行修改。抽样时频率不够高,抽样出来的点既代表了信号中的低频信号的样本值,也同时代表高频信号样本值,在信号重建的时候,高频信号被低频信号代替,两种波形完全重叠在一起,形成严重失真。DFT得到了每个频带上信号的能量,但是人耳对频率的感知不是等间隔的,近似于对数函数。

weixin_51293984的博客 1万+

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,

淡然坊 398

【SOC 芯片设计 DFT 学习专栏 -- DFT DRC规则检查】

在现代半导体设计中,DFT(Design for Test)技术的重要性不言而喻。它确保了芯片在制造完成后能够进行高效和可靠的测试。DFT设计规则检查(DRC)作为DFT流程中的一个关键环节,通过一系列自动化工具检测设计中的潜在问题,从而保证设计的可测试性和整体质量。在本文中,我们将详细探讨DFT DRC的概念、重要性、检查流程以及具体的规则和应用。Tessent是业界领先的DFT解决方案之一,其DRC工具通过一系列自动化检查,确保设计符合DFT要求。

CodingCos的博客 4167

You are given two integers n and k.

E - 1000 You are given two integers n and k. You should create an array of n positive integers a1,a2,…,an such that the sum (a1+a2+⋯+an) is divisible by k and maximum element in a is minimum possible. What is the minimum possible maximum element in a? Inpu

weixin_46358119的博客 1346

西电2023秋季机器学习复习提纲

西电2023秋季机器学习复习提纲,希望对学弟学妹有帮助

weixin_62083045的博客 5633

黑金AXU9EG开发板深度学习部署全流程解析

本文详细解析了黑金AXU9EG开发板的深度学习模型部署全流程,从环境搭建、模型转换到上板验证,提供了实用的技术指导和优化技巧。该开发板基于Xilinx Zynq UltraScale+ MPSoC架构,特别适合边缘计算场景,通过FPGA加速实现高效AI推理。

weixin_29062613的博客 447

LeetCode //C - 667. Beautiful Arrangement II

【代码】LeetCode //C - 667. Beautiful Arrangement II。

Made in Code 1189

Unity如何实现3D物体拆解组装

最近有一个需求,是做一个发动机的拆卸和安装功能,其实是一个很简单的功能,但是其中有一个点我觉的非常有意思,就是拖拽组装时,物体如何精准拖到目标位置,思路有了,但是我一直找不到实现方式,早晨刷牙时无意间想到了叉乘,我才有了解决方案。就凭这一次的灵光乍现,也很有必要写篇博客来记录一下。首先,我们先来看下我们要实现的效果,就是对一个发动机(任意多组件物体,如:车)的360°预览查看,手动拖拽拆解,手动拖拽组装。360°旋转视角远近拉伸视角点击拖拽拆卸拖拽组装。

TxNet 1万+

【Leetcode】667. 数组相邻差值的个数(Beautiful Arrangement II)

Leetcode - 667 Beautiful Arrangement II (Medium) 题目描述:数组元素的取值范围为[1, n],构建新的数组,使得相邻元素差值不相同的个数为 k。 Input: n = 3, k = 1 Output: [1, 2, 3] Explanation: The [1, 2, 3] has three different positive integers ...

厉兵秣码 1113

ABAP 7.40 新语法介绍系列之六:使用 FILTER 关键字对 ABAP 内表进行过滤

笔者之前写过的关于 ABAP 740 新语法的文章:本文介绍另一个新的关键字:FILTER.FILTER 是 ABAP 740 中引入的一个关键字,用于简化数据操作,它能够将内部表(Internal Table)中的数据按特定的条件进行过滤,并生成一个新的内部表。FILTER 的引入旨在使代码更加现代化和高效,尤其是在面对大量数据需要过滤的情况下。传统上,开发人员可能需要使用 LOOP AT 结合条件判断来实现,但使用 FILTER 关键字后,代码显得更加简洁且易于维护。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 1167

Prometheus+Grafana+alertmanager+PrometheusAlert监控告警 实现飞书告警

以上 是以grafana做的邮件告警,但是感觉邮件很少人看。下面是以webhook 可以接入。启动alertmanager (设置 开机在启动)下载prometheusalter 配置文件。这里的告警是基于grafana上搭建的。我们启动PrometheusAlert。写完之后进行重启Prometheus。prometheus 的告警规则。测试 基于我们生产环境做的文档。然后在grafana上导模板。上导入模板就可以 直接引用。访问 grafana页面。重启一下grafana。

墨痕诉清风的博客 856

[leetcode] 667. Beautiful Arrangement II

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

Learning from the mistakes 292

TIA protal与SCL从入门到精通(4)——软件单元使用

平台: TIA portal 15.1 1.添加软件单元 添加软件单元如下图: 关于软件单元使用基本限制,看如下文章即可 TIA protal与SCL从入门到精通(3)——软件单元基本介绍 每个软件单元是一个独立结构,所以每个软件单元的运行方式与实际PLC类似,所以首先添加主函数块: 注意:无论系统是否是具有并发性(这里不要混淆并行与并发的概念),在PLC所有主函数执行总是按主函数依次完成...

苦走一口田 7375

Leetcode——667. Beautiful Arrangement II

题目原址 https://leetcode.com/problems/beautiful-arrangement-ii/description/ 题目描述 Given two integers n and k, you need to construct a list which contains n different positive integers ranging from 1 ...

想当厨子的程序媛 232

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

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],

u010370157的博客 809

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

LeetCode 667——Beautiful Arrangement II

Given two integersnandk, you need to construct a list which containsndifferent positive integers ranging from1tonand obeys the following requirement: Suppose this list is [a1, a2, a3, ... , ...

CrazyOnes的博客 270

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

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,

几百个测试用例一致通过 265

Leetcode 667. Beautiful Arrangement II

题目 链接:https://leetcode.com/problems/beautiful-arrangement-ii/ Level: Medium Discription: Given two integers n and k, you need to construct a list which contains n different positive integers ranging ...

weixin_30562507的博客 117

[LeetCode] (medium) 667. Beautiful Arrangement II

https://leetcode.com/problems/beautiful-arrangement-ii/ Given two integersnandk, you need to construct a list which containsndifferent positive integers ranging from1tonand obeys the follow...

Frontier_Setter的博客 224

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],

繁城落叶 761

leetcode 667. Beautiful Arrangement II | 667. 优美的排列 II(Java)

题目 https://leetcode.com/problems/beautiful-arrangement-ii/ 题解 偏智力题,评论区有人说,A typical question testing whether you’re “smart” rather than “taking algorithm class”. 官方 O(n) 题解没看懂(没仔细看),评论区 看到一个 O(n^2) 的解法,比较清晰。 数据量 n=10^4,O(n^2) 的解能通过,说明在力扣上指令条数 1*10^8 是可以通过

寒泉 4万+
上一篇: 561. Array Partition I
下一篇: # 324. Wiggle Sort II
lifi_sysu
博客等级 码龄9年 0粉丝 20原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值