Skip to content

Commit 686e17b

Browse files
committed
Add solution #932
1 parent c92b697 commit 686e17b

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,7 @@
741741
929|[Unique Email Addresses](./solutions/0929-unique-email-addresses.js)|Easy|
742742
930|[Binary Subarrays With Sum](./solutions/0930-binary-subarrays-with-sum.js)|Medium|
743743
931|[Minimum Falling Path Sum](./solutions/0931-minimum-falling-path-sum.js)|Medium|
744+
932|[Beautiful Array](./solutions/0932-beautiful-array.js)|Medium|
744745
933|[Number of Recent Calls](./solutions/0933-number-of-recent-calls.js)|Easy|
745746
937|[Reorder Data in Log Files](./solutions/0937-reorder-data-in-log-files.js)|Medium|
746747
966|[Vowel Spellchecker](./solutions/0966-vowel-spellchecker.js)|Medium|

Diff for: solutions/0932-beautiful-array.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* 932. Beautiful Array
3+
* https://leetcode.com/problems/beautiful-array/
4+
* Difficulty: Medium
5+
*
6+
* An array nums of length n is beautiful if:
7+
* - nums is a permutation of the integers in the range [1, n].
8+
* - For every 0 <= i < j < n, there is no index k with i < k < j where
9+
* 2 * nums[k] == nums[i] + nums[j].
10+
*
11+
* Given the integer n, return any beautiful array nums of length n.
12+
* There will be at least one valid answer for the given n.
13+
*/
14+
15+
/**
16+
* @param {number} n
17+
* @return {number[]}
18+
*/
19+
var beautifulArray = function(n) {
20+
const memo = new Map();
21+
return helper(n);
22+
23+
function helper(size) {
24+
if (memo.has(size)) return memo.get(size);
25+
if (size === 1) return [1];
26+
27+
const odds = helper(Math.ceil(size / 2));
28+
const evens = helper(Math.floor(size / 2));
29+
const result = [
30+
...odds.map(x => x * 2 - 1),
31+
...evens.map(x => x * 2)
32+
];
33+
34+
memo.set(size, result);
35+
return result;
36+
}
37+
};

0 commit comments

Comments
 (0)