|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: (Leetcode) 238 - Product of Array Except Self |
| 4 | +categories: [스터디-알고리즘] |
| 5 | +tags: [자바, java, 리트코드, Leetcode, 알고리즘, algorithm, array, 배열] |
| 6 | +date: 2024-05-07 23:59:59 +0900 |
| 7 | +toc: true |
| 8 | +--- |
| 9 | + |
| 10 | +[New Year Gift - Curated List of Top 75 LeetCode Questions to Save Your Time](https://www.teamblind.com/post/New-Year-Gift---Curated-List-of-Top-75-LeetCode-Questions-to-Save-Your-Time-OaM1orEU) |
| 11 | + |
| 12 | +위 링크에 있는 추천 문제들을 시간이 있을때마다 풀어보려고 한다. |
| 13 | + |
| 14 | +--- |
| 15 | + |
| 16 | +[https://leetcode.com/problems/product-of-array-except-self/](https://leetcode.com/problems/product-of-array-except-self/) |
| 17 | + |
| 18 | +## 첫 접근 |
| 19 | + |
| 20 | +```java |
| 21 | +public int[] productExceptSelf(int[] nums) { |
| 22 | + List<Integer> products = new ArrayList<>(); |
| 23 | + int p = 0; |
| 24 | + |
| 25 | + while (p < nums.length) { |
| 26 | + int i = 0; |
| 27 | + Integer product = null; |
| 28 | + while (i < nums.length) { |
| 29 | + if (i == p) { |
| 30 | + i++; |
| 31 | + continue; |
| 32 | + } |
| 33 | + |
| 34 | + if (product == null) { |
| 35 | + product = nums[i]; |
| 36 | + } else { |
| 37 | + product *= nums[i]; |
| 38 | + } |
| 39 | + |
| 40 | + if (product == 0) { |
| 41 | + break; |
| 42 | + } |
| 43 | + |
| 44 | + i++; |
| 45 | + } |
| 46 | + products.add(product); |
| 47 | + p++; |
| 48 | + } |
| 49 | + |
| 50 | + return products.stream().mapToInt(i -> i).toArray(); |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +순수하게 다 계산하는 방식이다. 물론 타임아웃이 발생된다. |
| 55 | + |
| 56 | +### TC, SC |
| 57 | + |
| 58 | +시간 복잡도는 O(n^2)이고 공간 복잡도는 O(n)이다. |
| 59 | + |
| 60 | +## 두 번째 접근 |
| 61 | + |
| 62 | +```java |
| 63 | +public int[] productExceptSelf(int[] nums) { |
| 64 | + List<Integer> products = new ArrayList<>(); |
| 65 | + int result = 1; |
| 66 | + int zeroCount = 0; |
| 67 | + |
| 68 | + int p = 0; |
| 69 | + while (p < nums.length) { |
| 70 | + if (nums[p] != 0) { |
| 71 | + result *= nums[p]; |
| 72 | + } else { |
| 73 | + zeroCount++; |
| 74 | + } |
| 75 | + p++; |
| 76 | + } |
| 77 | + |
| 78 | + if (zeroCount >= 2) { |
| 79 | + products.addAll(new ArrayList<>(Collections.nCopies(nums.length, 0))); |
| 80 | + } else if (zeroCount == 1) { |
| 81 | + p = 0; |
| 82 | + while (p < nums.length) { |
| 83 | + if (nums[p] == 0) { |
| 84 | + products.add(result); |
| 85 | + } else { |
| 86 | + products.add(0); |
| 87 | + } |
| 88 | + p++; |
| 89 | + } |
| 90 | + } else { |
| 91 | + p = 0; |
| 92 | + while (p < nums.length) { |
| 93 | + products.add(result / nums[p]); |
| 94 | + p++; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + return products.stream().mapToInt(i -> i).toArray(); |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +0의 개수에 따른 케이스를 분리하였다. |
| 103 | + |
| 104 | +- 0이 2개 일 때 : 모든 값이 0이다. |
| 105 | +- 0이 1개 일 때 : 0인 부분만 값이 있다. 그리고 그 값은 0의 제외한 모든 수의 곱이다. |
| 106 | +- 0이 없을 때 : 전체 곱에서 해당 부분(num[i])를 나누어준다. |
| 107 | + |
| 108 | +통과는 하였지만 결과가 아쉬웠다. |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | +### TC, SC |
| 113 | + |
| 114 | +시간 복잡도는 O(n)이고 공간 복잡도는 O(n)이다. |
| 115 | + |
| 116 | +## 더 개선해보기 |
| 117 | + |
| 118 | +```java |
| 119 | +public int[] productExceptSelf(int[] nums) { |
| 120 | + int[] products = new int[nums.length]; |
| 121 | + int result = 1; |
| 122 | + int zeroCount = 0; |
| 123 | + |
| 124 | + int p = 0; |
| 125 | + while (p < nums.length) { |
| 126 | + if (nums[p] != 0) { |
| 127 | + result *= nums[p]; |
| 128 | + } else { |
| 129 | + zeroCount++; |
| 130 | + if (zeroCount >= 2) { |
| 131 | + Arrays.fill(products, 0); |
| 132 | + return products; |
| 133 | + } |
| 134 | + } |
| 135 | + p++; |
| 136 | + } |
| 137 | + |
| 138 | + if (zeroCount == 1) { |
| 139 | + p = 0; |
| 140 | + while (p < nums.length) { |
| 141 | + if (nums[p] == 0) { |
| 142 | + products[p] = result; |
| 143 | + } |
| 144 | + p++; |
| 145 | + } |
| 146 | + } else { |
| 147 | + p = 0; |
| 148 | + while (p < nums.length) { |
| 149 | + products[p] = result / nums[p]; |
| 150 | + p++; |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + return products; |
| 155 | +} |
| 156 | +``` |
| 157 | + |
| 158 | +예시 코드도 간단함에도 7ms가 소요되길래 이건 input이 지저분한게 아니라, 그냥 기본적으로 시간이 많이 걸리는구나 싶어서 개선할 수 있는 부분들을 찾아 개선해보았다. List 로 해두면 후처리가 편할것 같아서 했는데 오히려 이 부분에서 시간을 소모했나보다 싶어서 Array로 바꾸었더니 바로 2ms로 개선되었다. |
| 159 | + |
| 160 | +### TC, SC |
| 161 | + |
| 162 | +시간 복잡도는 O(n)이고 공간 복잡도는 O(n)이다. |
0 commit comments