From b08c4aab482e509a7acf4127dc405bdb1cc7a1b1 Mon Sep 17 00:00:00 2001 From: Namita <46557775+namita0210@users.noreply.github.com> Date: Wed, 17 Jan 2024 19:07:37 +0000 Subject: [PATCH 1/4] Day 18 Q2 add java code --- .../namita0210_java.java | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Day-18/q2: Search in Rotated Sorted Array/namita0210_java.java diff --git a/Day-18/q2: Search in Rotated Sorted Array/namita0210_java.java b/Day-18/q2: Search in Rotated Sorted Array/namita0210_java.java new file mode 100644 index 00000000..5640e0ac --- /dev/null +++ b/Day-18/q2: Search in Rotated Sorted Array/namita0210_java.java @@ -0,0 +1,35 @@ +public class namita0210_java { + + public static int search(int[] nums, int target) { + int left = 0; + int right = nums.length - 1; + + while (left <= right) { + int mid = left + (right - left) / 2; + + if (nums[mid] == target) { + return mid; + } + + if (nums[left] <= nums[mid]) { + // Left half is sorted + if (nums[left] <= target && target < nums[mid]) { + right = mid - 1; + } else { + left = mid + 1; + } + } else { + // Right half is sorted + if (nums[mid] < target && target <= nums[right]) { + left = mid + 1; + } else { + right = mid - 1; + } + } + } + + return -1; // Target not found + } + + +} From 6036556d78597fcc33d51506333a9cd1edad6c90 Mon Sep 17 00:00:00 2001 From: Namita <46557775+namita0210@users.noreply.github.com> Date: Tue, 23 Jan 2024 16:25:21 +0000 Subject: [PATCH 2/4] Day-18 Q2 : reformat and rectify mistakes --- .../namita0210--java.md | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Day-18/q2: Search in Rotated Sorted Array/namita0210--java.md diff --git a/Day-18/q2: Search in Rotated Sorted Array/namita0210--java.md b/Day-18/q2: Search in Rotated Sorted Array/namita0210--java.md new file mode 100644 index 00000000..5640e0ac --- /dev/null +++ b/Day-18/q2: Search in Rotated Sorted Array/namita0210--java.md @@ -0,0 +1,35 @@ +public class namita0210_java { + + public static int search(int[] nums, int target) { + int left = 0; + int right = nums.length - 1; + + while (left <= right) { + int mid = left + (right - left) / 2; + + if (nums[mid] == target) { + return mid; + } + + if (nums[left] <= nums[mid]) { + // Left half is sorted + if (nums[left] <= target && target < nums[mid]) { + right = mid - 1; + } else { + left = mid + 1; + } + } else { + // Right half is sorted + if (nums[mid] < target && target <= nums[right]) { + left = mid + 1; + } else { + right = mid - 1; + } + } + } + + return -1; // Target not found + } + + +} From 03216433be36abe4c123bcdd802bbd46f5cc1131 Mon Sep 17 00:00:00 2001 From: Namita <46557775+namita0210@users.noreply.github.com> Date: Tue, 23 Jan 2024 16:25:40 +0000 Subject: [PATCH 3/4] Day 18 Q2 --- .../namita0210_java.java | 35 ------------------- 1 file changed, 35 deletions(-) delete mode 100644 Day-18/q2: Search in Rotated Sorted Array/namita0210_java.java diff --git a/Day-18/q2: Search in Rotated Sorted Array/namita0210_java.java b/Day-18/q2: Search in Rotated Sorted Array/namita0210_java.java deleted file mode 100644 index 5640e0ac..00000000 --- a/Day-18/q2: Search in Rotated Sorted Array/namita0210_java.java +++ /dev/null @@ -1,35 +0,0 @@ -public class namita0210_java { - - public static int search(int[] nums, int target) { - int left = 0; - int right = nums.length - 1; - - while (left <= right) { - int mid = left + (right - left) / 2; - - if (nums[mid] == target) { - return mid; - } - - if (nums[left] <= nums[mid]) { - // Left half is sorted - if (nums[left] <= target && target < nums[mid]) { - right = mid - 1; - } else { - left = mid + 1; - } - } else { - // Right half is sorted - if (nums[mid] < target && target <= nums[right]) { - left = mid + 1; - } else { - right = mid - 1; - } - } - } - - return -1; // Target not found - } - - -} From 23da721e225dabc933f07a8532dd56d7b0d56f96 Mon Sep 17 00:00:00 2001 From: Namita <46557775+namita0210@users.noreply.github.com> Date: Tue, 23 Jan 2024 16:31:52 +0000 Subject: [PATCH 4/4] Day4 Q3 - Add java code --- .../q3-Network Delay Time/namita0210--java.md | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 Day-4/q3-Network Delay Time/namita0210--java.md diff --git a/Day-4/q3-Network Delay Time/namita0210--java.md b/Day-4/q3-Network Delay Time/namita0210--java.md new file mode 100644 index 00000000..d36e6c13 --- /dev/null +++ b/Day-4/q3-Network Delay Time/namita0210--java.md @@ -0,0 +1,68 @@ +import java.util.*; + +public class NetworkDelayTime { + public int networkDelayTime(int[][] times, int n, int k) { + // Create a graph to represent the network + Map> graph = new HashMap<>(); + for (int[] time : times) { + graph.computeIfAbsent(time[0], key -> new ArrayList<>()).add(new int[]{time[1], time[2]}); + } + + // Priority queue to store nodes with their distances + PriorityQueue pq = new PriorityQueue<>(Comparator.comparingInt(a -> a[1])); + pq.offer(new int[]{k, 0}); + + // Distance array to store the minimum distance from source to each node + int[] distance = new int[n + 1]; + Arrays.fill(distance, Integer.MAX_VALUE); + distance[k] = 0; + + while (!pq.isEmpty()) { + int[] current = pq.poll(); + int node = current[0]; + int time = current[1]; + + if (time > distance[node]) { + continue; + } + + if (graph.containsKey(node)) { + for (int[] edge : graph.get(node)) { + int neighbor = edge[0]; + int newTime = time + edge[1]; + + if (newTime < distance[neighbor]) { + distance[neighbor] = newTime; + pq.offer(new int[]{neighbor, newTime}); + } + } + } + } + + int maxTime = Arrays.stream(distance).max().getAsInt(); + + return maxTime == Integer.MAX_VALUE ? -1 : maxTime; + } + + public static void main(String[] args) { + NetworkDelayTime solution = new NetworkDelayTime(); + + // Example 1 + int[][] times1 = {{2, 1, 1}, {2, 3, 1}, {3, 4, 1}}; + int n1 = 4; + int k1 = 2; + System.out.println(solution.networkDelayTime(times1, n1, k1)); // Output: 2 + + // Example 2 + int[][] times2 = {{1, 2, 1}}; + int n2 = 2; + int k2 = 1; + System.out.println(solution.networkDelayTime(times2, n2, k2)); // Output: 1 + + // Example 3 + int[][] times3 = {{1, 2, 1}}; + int n3 = 2; + int k3 = 2; + System.out.println(solution.networkDelayTime(times3, n3, k3)); // Output: -1 + } +}