Skip to content

Commit

Permalink
Time: 83 ms (71.72%), Space: 86.6 MB (5.05%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
Suvraneel committed Jan 25, 2025
1 parent bb68323 commit f2fed92
Showing 1 changed file with 82 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
class Solution {
public int[] lexicographicallySmallestArray(int[] nums, int limit) {
int n = nums.length, grpNum = 0;
int[] numsSorted = nums.clone(), ans = numsSorted;
Arrays.sort(numsSorted);
Map<Integer, Integer> groupIdx = new HashMap<>();
Map<Integer, Queue<Integer>> groups = new HashMap<>();
groupIdx.put(numsSorted[0], 0);
groups.put(0, new LinkedList<>(Arrays.asList(numsSorted[0])));
for(int i=1; i<n; i++){
groupIdx.put(numsSorted[i], (numsSorted[i]-numsSorted[i-1]<=limit) ? grpNum: ++grpNum);
groups.putIfAbsent(grpNum, new LinkedList<>());
groups.get(grpNum).add(numsSorted[i]);
}
for(int i=0; i<n; i++)
ans[i] = groups.get(groupIdx.get(nums[i])).remove();
return ans;
}
}




// class Solution {
// private class UnionFind {
// int[] root;
// int[] rank;

// UnionFind(int size) {
// root = new int[size];
// rank = new int[size];
// for (int i = 0; i < size; i++) {
// root[i] = i;
// rank[i] = 1;
// }
// }

// int find(int x) {
// if (root[x] == x)
// return x;
// return root[x] = find(root[x]);
// }

// void union(int x, int y) {
// int rootX = find(x);
// int rootY = find(y);
// if (rootX != rootY) {
// if (rank[rootX] < rank[rootY])
// root[x] = rootY;
// else if (rank[rootX] > rank[rootY])
// root[y] = rootX;
// else {
// root[y] = rootX;
// rank[rootX]++;
// }
// }
// }

// boolean connected(int x, int y) {
// return find(x) == find(y);
// }
// }

// public int[] lexicographicallySmallestArray(int[] nums, int limit) {
// int n = nums.length;
// UnionFind uf = new UnionFind(n);
// for (int i = 0; i < n; i++)
// for (int j = 0; j < n; j++)
// if (i!=j && Math.abs(nums[i] - nums[j]) <= limit)
// uf.union(i, j);
// Map<Integer, Queue<Integer>> sets = new HashMap<>();
// for (int i = 0; i < n; i++) {
// int root = uf.find(i);
// sets.putIfAbsent(root, new PriorityQueue<>());
// sets.get(root).offer(nums[i]);
// }
// int[] ans = new int[n];
// for (int i = 0; i < n; i++)
// ans[i] = sets.get(uf.find(i)).poll();
// return ans;
// }
// }

0 comments on commit f2fed92

Please sign in to comment.