-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.java
More file actions
69 lines (66 loc) · 2.11 KB
/
Copy pathsolution.java
File metadata and controls
69 lines (66 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// 2948. Make Lexicographically Smallest Array by Swapping Elements
// https://leetcode.com/problems/make-lexicographically-smallest-array-by-swapping-elements/
// Medium | Java | Accepted 2026-08-29
// Runtime 227 ms | Memory 238.3 MB
class Solution {
Map<Integer, Integer> parents = new HashMap<>();
Map<Integer, Integer> size = new HashMap<>();
public int[] lexicographicallySmallestArray(int[] nums, int limit) {
//I USED A UNION FIND APPROACH
//Basically union all the nodes that are within limit's reach of each other
//This makes all of the node's that are swappable and within a limit's reach of each other in the same group
//Go through this
int[] copy = nums.clone();
for(int i = 0; i<nums.length; i++)
{
parents.put(nums[i], nums[i]);
size.put(nums[i], 1);
}
Arrays.sort(nums);
for(int i = 0; i<nums.length-1; i++)
{
if(nums[i+1]-nums[i]<=limit)
{
union(nums[i], nums[i+1]);
}
}
Map<Integer, Queue<Integer>> queue = new HashMap<>();
for(int i : nums)
{
queue.computeIfAbsent(parents.get(i), k -> new LinkedList<>()).add(i);
}
for(int i = 0; i<nums.length; i++)
{
copy[i] = queue.get(parents.get(copy[i])).poll();
}
return copy;
}
public int find(int node)
{
if(parents.get(node)==node)
{
return node;
}
parents.put(node, find(parents.get(node)));
return parents.get(node);
}
public void union(int node1, int node2)
{
int parent1 = find(node1);
int parent2 = find(node2);
int tempp = node2;
if(parent1 == parent2)
{
return;
}
if(size.get(parent1)>size.get(parent2))
{
int temp = parent1;
node1 = node2;
node2 = temp;
}
parents.put(node1, node2);
int combinedSize = size.get(parent1) + size.get(parent2);
size.put(tempp, combinedSize);
}
}