@@ -139,14 +139,15 @@ private void merge(int[][] arr, Integer[] count, int l, int m, int r) {
|
139 | 139 | /**
|
140 | 140 | * https://discuss.leetcode.com/topic/31405/9ms-short-java-bst-solution-get-answer-when-building-bst
|
141 | 141 | */
|
142 |
| -public List<Integer> countSmaller(int[] nums) { |
| 142 | +public List<Integer> countSmaller4(int[] nums) { |
143 | 143 | Integer[] ans = new Integer[nums.length];
|
144 | 144 | Node root = null;
|
145 | 145 | for (int i = nums.length - 1; i >= 0; i--) {
|
146 | 146 | root = insert(nums[i], root, ans, i, 0);
|
147 | 147 | }
|
148 | 148 | return Arrays.asList(ans);
|
149 | 149 | }
|
| 150 | + |
150 | 151 | private Node insert(int num, Node node, Integer[] ans, int i, int preSum) {
|
151 | 152 | if (node == null) {
|
152 | 153 | node = new Node(num, 0);
|
@@ -175,7 +176,7 @@ public Node(int v, int s) {
|
175 | 176 | /**
|
176 | 177 | * https://discuss.leetcode.com/topic/31173/my-simple-ac-java-binary-search-code
|
177 | 178 | */
|
178 |
| -public List<Integer> countSmaller(int[] nums) { |
| 179 | +public List<Integer> countSmaller5(int[] nums) { |
179 | 180 | Integer[] ans = new Integer[nums.length];
|
180 | 181 | List<Integer> sorted = new ArrayList<Integer>();
|
181 | 182 | for (int i = nums.length - 1; i >= 0; i--) {
|
@@ -185,6 +186,7 @@ public List<Integer> countSmaller(int[] nums) {
|
185 | 186 | }
|
186 | 187 | return Arrays.asList(ans);
|
187 | 188 | }
|
| 189 | + |
188 | 190 | private int findIndex(List<Integer> sorted, int target) {
|
189 | 191 | if (sorted.size() == 0) return 0;
|
190 | 192 | int start = 0;
|
@@ -203,4 +205,105 @@ private int findIndex(List<Integer> sorted, int target) {
|
203 | 205 | return end;
|
204 | 206 | }
|
205 | 207 |
|
| 208 | + |
| 209 | +public List<Integer> countSmaller6(int[] nums) { |
| 210 | +LinkedList<Integer> res = new LinkedList<>(); |
| 211 | +if (nums == null || nums.length == 0) return res; |
| 212 | +int N = nums.length; |
| 213 | +int min = Integer.MAX_VALUE; |
| 214 | +int max = Integer.MIN_VALUE; |
| 215 | +for (int n: nums) { |
| 216 | +min = Math.min(min, n); |
| 217 | +} |
| 218 | +int[] ranks = new int[N]; |
| 219 | +for (int i = 0; i < N; i++) { |
| 220 | +ranks[i] = nums[i] - min + 1; |
| 221 | +max = Math.max(ranks[i], max); |
| 222 | +} |
| 223 | + |
| 224 | +BinaryIndexedTree bit = new BinaryIndexedTree(max); |
| 225 | +for (int i=ranks.length-1; i>=0 ;i--) { |
| 226 | +res.addFirst(bit.query(ranks[i] - 1)); |
| 227 | +bit.update(ranks[i], 1); |
| 228 | +} |
| 229 | +return res; |
| 230 | +} |
| 231 | + |
| 232 | +class BinaryIndexedTree { |
| 233 | +int[] tree; |
| 234 | +int N; |
| 235 | + |
| 236 | +BinaryIndexedTree(int N) { |
| 237 | +this.N = N; |
| 238 | +this.tree = new int[N+1]; |
| 239 | +} |
| 240 | + |
| 241 | +void update(int i, int delta) { |
| 242 | +int k = i + 1; |
| 243 | +while (k <= this.N) { |
| 244 | +this.tree[k] += delta; |
| 245 | +k += lowBit(k); |
| 246 | +} |
| 247 | +} |
| 248 | + |
| 249 | +int query(int i) { |
| 250 | +int k = i + 1; |
| 251 | +int res = 0; |
| 252 | +while (k > 0) { |
| 253 | +res += this.tree[k]; |
| 254 | +k -= lowBit(k); |
| 255 | +} |
| 256 | +return res; |
| 257 | +} |
| 258 | + |
| 259 | +private int lowBit(int x) { |
| 260 | +return x & (-x); |
| 261 | +} |
| 262 | +} |
| 263 | + |
| 264 | + |
| 265 | +public List<Integer> countSmaller7(int[] nums) { |
| 266 | +LinkedList<Integer> res = new LinkedList<>(); |
| 267 | +if (nums == null || nums.length == 0) return res; |
| 268 | +int N = nums.length; |
| 269 | +Node root = new Node(nums[N-1]); |
| 270 | +res.add(0); |
| 271 | +for (int i=N-2; i>=0; i--) { |
| 272 | +res.addFirst(insert(root, nums[i])); |
| 273 | +} |
| 274 | +return res; |
| 275 | +} |
| 276 | + |
| 277 | +private int insert(Node root, int val) { |
| 278 | +if (root.val == val) { |
| 279 | +root.count++; |
| 280 | +return root.leftCount; |
| 281 | +} else if (root.val > val) { |
| 282 | +root.leftCount++; |
| 283 | +if (root.left == null) { |
| 284 | +root.left = new Node(val); |
| 285 | +return 0; |
| 286 | +} |
| 287 | +return insert(root.left, val); |
| 288 | +} else { |
| 289 | +if (root.right == null) { |
| 290 | +root.right = new Node(val); |
| 291 | +return root.count + root.leftCount; |
| 292 | +} |
| 293 | +return root.count + root.leftCount + insert(root.right, val); |
| 294 | +} |
| 295 | + |
| 296 | +} |
| 297 | + |
| 298 | +class Node { |
| 299 | +Node left; |
| 300 | +Node right; |
| 301 | +int val; |
| 302 | +int count = 1; |
| 303 | +int leftCount = 0; |
| 304 | +Node(int v) { |
| 305 | +this.val = v; |
| 306 | +} |
| 307 | +} |
| 308 | + |
206 | 309 | }
|
0 commit comments