@@ -4,7 +4,7 @@ public class Geegong {
44
55 /**
66 * Time complexity : O(n)
7- * Space complexity : O(1 ) (except result)
7+ * Space complexity : O(n ) (except result)
88 *
99 * 풀이 : prefix array , suffix array 를 각 배열마다 구하여 각 array에 해당되는 인덱스의 값들을 곱하여 결과값을 도출
1010 *
@@ -13,38 +13,61 @@ public class Geegong {
1313 */
1414 public int [] productExceptSelf (int [] nums ) {
1515
16- int [] result = new int [nums .length ];
17-
18- // 1. result 는 먼저 prefix 배열들의 곱으로 채운다.
19- // ( prefix 배열이란 해당되는 index 이전의 배열요소값들을 의미)
16+ int [] prefixProductArr = new int [nums .length ];
17+ int [] suffixProductArr = new int [nums .length ];
2018
21- // 앞에서부터 누적되는 총 곱의 값
22- int accumulatedProduct = 1 ;
23- for (int index =0 ; index <nums .length ; index ++) {
24- if (index == 0 ) {
25- result [index ] = 1 ;
26- continue ;
27- }
28-
29- result [index ] = accumulatedProduct * nums [index - 1 ];
30- accumulatedProduct = result [index ];
19+ prefixProductArr [0 ] = 1 ;
20+ prefixProductArr [1 ] = nums [0 ];
21+ for (int index =2 ; index <nums .length ; index ++) {
22+ prefixProductArr [index ] = nums [index - 1 ] * prefixProductArr [index - 1 ];
3123 }
3224
33- // 2. 배열의 뒤에서부터 product of suffix 값은 result 배열 하나하나에 대체한다.
34-
35- // nums 배열 안에서 뒤에서부터 누적되는 총 곱의 값
36- accumulatedProduct = 1 ;
37- for (int index =nums .length - 1 ; index >= 0 ; index --) {
38- if (index == nums .length - 1 ) {
39- accumulatedProduct = nums [index ];
40- continue ;
41- }
25+ suffixProductArr [nums .length - 1 ] = 1 ;
26+ suffixProductArr [nums .length - 2 ] = nums [nums .length - 1 ];
27+ for (int index =nums .length - 3 ; index >=0 ; index --) {
28+ suffixProductArr [index ] = nums [index + 1 ] * suffixProductArr [index + 1 ];
29+ }
4230
43- result [index ] = result [index ] * accumulatedProduct ;
44- accumulatedProduct = accumulatedProduct * nums [index ];
31+ int [] result = new int [nums .length ];
32+ for (int index =0 ; index <nums .length ; index ++) {
33+ result [index ] = prefixProductArr [index ] * suffixProductArr [index ];
4534 }
4635
4736 return result ;
37+
38+ // 아래는 예전 기수에서 풀었떤 풀이 방법
39+ // int[] result = new int[nums.length];
40+ //
41+ // // 1. result 는 먼저 prefix 배열들의 곱으로 채운다.
42+ // // ( prefix 배열이란 해당되는 index 이전의 배열요소값들을 의미)
43+ //
44+ // // 앞에서부터 누적되는 총 곱의 값
45+ // int accumulatedProduct = 1;
46+ // for (int index=0; index<nums.length; index++) {
47+ // if (index == 0) {
48+ // result[index] = 1;
49+ // continue;
50+ // }
51+ //
52+ // result[index] = accumulatedProduct * nums[index - 1];
53+ // accumulatedProduct = result[index];
54+ // }
55+ //
56+ // // 2. 배열의 뒤에서부터 product of suffix 값은 result 배열 하나하나에 대체한다.
57+ //
58+ // // nums 배열 안에서 뒤에서부터 누적되는 총 곱의 값
59+ // accumulatedProduct = 1;
60+ // for (int index=nums.length - 1; index >= 0; index--) {
61+ // if (index == nums.length - 1) {
62+ // accumulatedProduct = nums[index];
63+ // continue;
64+ // }
65+ //
66+ // result[index] = result[index] * accumulatedProduct;
67+ // accumulatedProduct = accumulatedProduct * nums[index];
68+ // }
69+ //
70+ // return result;
4871 }
4972
5073}
0 commit comments