File tree 2 files changed +38
-0
lines changed
2 files changed +38
-0
lines changed Original file line number Diff line number Diff line change 741
741
929|[ Unique Email Addresses] ( ./solutions/0929-unique-email-addresses.js ) |Easy|
742
742
930|[ Binary Subarrays With Sum] ( ./solutions/0930-binary-subarrays-with-sum.js ) |Medium|
743
743
931|[ Minimum Falling Path Sum] ( ./solutions/0931-minimum-falling-path-sum.js ) |Medium|
744
+ 932|[ Beautiful Array] ( ./solutions/0932-beautiful-array.js ) |Medium|
744
745
933|[ Number of Recent Calls] ( ./solutions/0933-number-of-recent-calls.js ) |Easy|
745
746
937|[ Reorder Data in Log Files] ( ./solutions/0937-reorder-data-in-log-files.js ) |Medium|
746
747
966|[ Vowel Spellchecker] ( ./solutions/0966-vowel-spellchecker.js ) |Medium|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments