Skip to content

Commit ada135e

Browse files
committed
Add solution #667
1 parent 0436389 commit ada135e

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,7 @@
500500
661|[Image Smoother](./0661-image-smoother.js)|Easy|
501501
662|[Maximum Width of Binary Tree](./0662-maximum-width-of-binary-tree.js)|Medium|
502502
664|[Strange Printer](./0664-strange-printer.js)|Hard|
503+
667|[Beautiful Arrangement II](./0667-beautiful-arrangement-ii.js)|Medium|
503504
680|[Valid Palindrome II](./0680-valid-palindrome-ii.js)|Easy|
504505
684|[Redundant Connection](./0684-redundant-connection.js)|Medium|
505506
686|[Repeated String Match](./0686-repeated-string-match.js)|Easy|
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* 667. Beautiful Arrangement II
3+
* https://leetcode.com/problems/beautiful-arrangement-ii/
4+
* Difficulty: Medium
5+
*
6+
* Given two integers n and k, construct a list answer that contains n different positive integers
7+
* ranging from 1 to n and obeys the following requirement:
8+
* - Suppose this list is answer = [a1, a2, a3, ... , an], then the list [|a1 - a2|, |a2 - a3|,
9+
* |a3 - a4|, ... , |an-1 - an|] has exactly k distinct integers.
10+
*
11+
* Return the list answer. If there multiple valid answers, return any of them.
12+
*/
13+
14+
/**
15+
* @param {number} n
16+
* @param {number} k
17+
* @return {number[]}
18+
*/
19+
var constructArray = function(n, k) {
20+
const result = [];
21+
22+
for (let i = 0, left = 1, right = n; i < n; i++) {
23+
if (k > 1) {
24+
result.push(k % 2 === 0 ? right-- : left++);
25+
k--;
26+
} else {
27+
result.push(left++);
28+
}
29+
}
30+
31+
return result;
32+
};

0 commit comments

Comments
 (0)