File tree Expand file tree Collapse file tree 2 files changed +42
-0
lines changed
Expand file tree Collapse file tree 2 files changed +42
-0
lines changed Original file line number Diff line number Diff line change @@ -25,3 +25,44 @@ The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit int
2525Follow up: Can you solve the problem in O(1) extra space complexity? (The output array does not count as extra space for space complexity analysis.)
2626
2727*/
28+
29+ const getPrefixProduct = ( arr ) => {
30+ const n = arr . length ;
31+ const result = new Array ( n ) ;
32+ result [ 0 ] = arr [ 0 ] ;
33+
34+ for ( let i = 1 ; i < n ; i ++ ) {
35+ result [ i ] = result [ i - 1 ] * arr [ i ]
36+ }
37+
38+ return result ;
39+ } ;
40+
41+ const getSuffixProduct = ( arr ) => {
42+ const n = arr . length ;
43+ const result = new Array ( n ) ; // Preallocate the array with the required length
44+ result [ n - 1 ] = arr [ n - 1 ] ; // Initialize the last element
45+
46+ for ( let i = n - 2 ; i >= 0 ; i -- ) {
47+ result [ i ] = result [ i + 1 ] * arr [ i ] ;
48+ }
49+
50+ return result ;
51+ } ;
52+
53+ /**
54+ * @param {number[] } nums
55+ * @return {number[] }
56+ */
57+ const productExceptSelf = ( nums ) => {
58+ const prefixProduct = getPrefixProduct ( nums ) ;
59+ const suffixProduct = getSuffixProduct ( nums ) ;
60+
61+ const result = [ ] ;
62+
63+ for ( const num of nums ) {
64+ result . push ( ) ; // TODO calculate current element
65+ }
66+
67+ return result ;
68+ } ;
Original file line number Diff line number Diff line change @@ -28,6 +28,7 @@ LeetCode Profile: https://leetcode.com/timothyshores/
2828|[ 187. Repeated DNA Sequences] ( https://leetcode.com/problems/repeated-dna-sequences/ ) |Medium|[ JavaScript] ( 187.%20Repeated%20DNA%20Sequences.js )
2929|[ 217. Contains Duplicate] ( https://leetcode.com/problems/contains-duplicate/ ) |Easy|[ JavaScript] ( 217.%20Contains%20Duplicate.js )
3030|[ 232. Implement Queue using Stacks] ( https://leetcode.com/problems/implement-queue-using-stacks/ ) |Easy|[ JavaScript] ( 232.%20Implement%20Queue%20using%20Stacks.js )
31+ |[ 238. Product of Array Except Self] ( https://leetcode.com/problems/move-zeroes/description/ ) |Easy|[ JavaScript] ( 238.%20Product%20of%20Array%20Except%20Self.js )
3132|[ 242. Valid Anagram] ( https://leetcode.com/problems/valid-anagram/ ) |Easy|[ JavaScript] ( 242.%20Valid%20Anagram.js )
3233|[ 278. First Bad Version] ( https://leetcode.com/problems/first-bad-version/ ) |Easy|[ JavaScript] ( 278.%20First%20Bad%20Version.js )
3334|[ 283. Move Zeroes] ( https://leetcode.com/problems/move-zeroes/ ) |Easy|[ JavaScript] ( 283.%20Move%20Zeroes.js )
You can’t perform that action at this time.
0 commit comments