Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions Java/Circular Subarray Median Finder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import java.util.*;

public class CircularMedian {
public static List<Double> medianSlidingWindow(int[] nums, int k) {
List<Double> res = new ArrayList<>();
TreeMap<Integer, Integer> window = new TreeMap<>();
for (int i = 0; i < nums.length + k - 1; i++) {
int num = nums[i % nums.length];
window.put(num, window.getOrDefault(num, 0) + 1);
if (i >= k) {
int out = nums[(i - k) % nums.length];
window.put(out, window.get(out) - 1);
if (window.get(out) == 0) window.remove(out);
}
if (i >= k - 1)
res.add(findMedian(window, k));
}
return res;
}

private static double findMedian(TreeMap<Integer, Integer> map, int k) {
int count = 0;
for (int key : map.keySet()) {
count += map.get(key);
if (count >= (k + 1) / 2)
return k % 2 == 1 ? key : (key + map.higherKey(key)) / 2.0;
}
return 0;
}

public static void main(String[] args) {
int[] arr = {2, 1, 5, 7, 2, 0, 5};
System.out.println(medianSlidingWindow(arr, 3));
}
}
35 changes: 35 additions & 0 deletions Java/Clone a Graph With Weighted Cycles and Cross-Edges.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import java.util.*;

class GraphNode {
int val;
Map<GraphNode, Integer> neighbors = new HashMap<>();
GraphNode(int v) { val = v; }
}

public class CloneGraph {
static GraphNode cloneGraph(GraphNode node, Map<GraphNode, GraphNode> map) {
if (node == null) return null;
if (map.containsKey(node)) return map.get(node);

GraphNode clone = new GraphNode(node.val);
map.put(node, clone);

for (GraphNode neigh : node.neighbors.keySet())
clone.neighbors.put(cloneGraph(neigh, map), node.neighbors.get(neigh));

return clone;
}

public static void main(String[] args) {
GraphNode a = new GraphNode(1);
GraphNode b = new GraphNode(2);
GraphNode c = new GraphNode(3);
a.neighbors.put(b, 5);
b.neighbors.put(c, 2);
c.neighbors.put(a, 1);

GraphNode clone = cloneGraph(a, new HashMap<>());
System.out.println("Cloned node value: " + clone.val);
System.out.println("Neighbor count: " + clone.neighbors.size());
}
}
34 changes: 34 additions & 0 deletions Java/Lexicographically Smallest String After K Swaps.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
public class SmallestString {
static String minStr;

static void findMin(char[] str, int k) {
if (k == 0) return;
int n = str.length;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (str[j] < str[i]) {
swap(str, i, j);
String curr = new String(str);
if (curr.compareTo(minStr) < 0)
minStr = curr;
findMin(str, k - 1);
swap(str, i, j);
}
}
}
}

static void swap(char[] arr, int i, int j) {
char t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}

public static void main(String[] args) {
String s = "cbad";
int k = 2;
minStr = s;
findMin(s.toCharArray(), k);
System.out.println("Smallest: " + minStr);
}
}
34 changes: 34 additions & 0 deletions Java/Maximum XOR Path in a Binary Tree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import java.util.*;

class Node {
int val;
Node left, right;
Node(int v) { val = v; }
}

public class MaxXORPath {
static int maxXor = 0;
static List<Integer> pathVals = new ArrayList<>();

static void dfs(Node node, int currXor) {
if (node == null) return;
currXor ^= node.val;
pathVals.add(currXor);
maxXor = Math.max(maxXor, currXor);
dfs(node.left, currXor);
dfs(node.right, currXor);
}

public static void main(String[] args) {
Node root = new Node(5);
root.left = new Node(1);
root.right = new Node(7);
root.left.left = new Node(3);
root.right.left = new Node(6);
dfs(root, 0);
for (int i : pathVals)
for (int j : pathVals)
maxXor = Math.max(maxXor, i ^ j);
System.out.println("Max XOR Path Value: " + maxXor);
}
}
33 changes: 33 additions & 0 deletions Java/Rebuild a Binary Tree from Random Traversal Pairs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import java.util.*;

class TreeNode {
int val;
TreeNode left, right;
TreeNode(int v) { val = v; }
}

public class RebuildTree {
static TreeNode buildTree(int[] preorder, int[] random) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < random.length; i++)
map.put(random[i], i);
return helper(preorder, 0, preorder.length - 1, random, 0, random.length - 1, map);
}

static TreeNode helper(int[] pre, int ps, int pe, int[] rand, int rs, int re, Map<Integer, Integer> map) {
if (ps > pe || rs > re) return null;
TreeNode root = new TreeNode(pre[ps]);
int idx = map.get(pre[ps]);
int leftSize = idx - rs;
root.left = helper(pre, ps + 1, ps + leftSize, rand, rs, idx - 1, map);
root.right = helper(pre, ps + leftSize + 1, pe, rand, idx + 1, re, map);
return root;
}

public static void main(String[] args) {
int[] preorder = {1,2,4,5,3,6,7};
int[] random = {4,2,5,1,6,3,7};
TreeNode root = buildTree(preorder, random);
System.out.println("Root: " + root.val);
}
}
Loading