From 77daeb705778310c66fa2bdd810dcc0b0f3212ba Mon Sep 17 00:00:00 2001 From: rixant Date: Mon, 25 Jul 2022 21:36:05 -0400 Subject: [PATCH] java: Created network delay time --- java/743-Network-Delay-Time.java | 50 ++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 java/743-Network-Delay-Time.java diff --git a/java/743-Network-Delay-Time.java b/java/743-Network-Delay-Time.java new file mode 100644 index 000000000..7568236c6 --- /dev/null +++ b/java/743-Network-Delay-Time.java @@ -0,0 +1,50 @@ +// Bellman Ford ALgorithm +// Time Complexty (n * t) | Space Complexity O(n) where t is the length of times +class Solution { + public int networkDelayTime(int[][] times, int n, int k) { + + // initialize an array with max value of size n + int[] paths = new int[n]; + Arrays.fill(paths, Integer.MAX_VALUE); + + paths[k - 1] = 0; + + for (int i = 0; i < n; i++){ + + // make a copy of paths + int[] temp = new int[n]; + temp = Arrays.copyOf(paths, paths.length); + + // loop through times + for(int j = 0; j < times.length; j ++){ + + int src = times[j][0]; // source + int tgt = times[j][1]; // target + int time = times[j][2]; // time + + if (temp[src - 1] != Integer.MAX_VALUE && temp[src - 1] + time < temp[tgt - 1]){ + temp[tgt - 1] = temp[src - 1] + time; + } + + } + + // set paths to temp + paths = temp; + + } + + int result= Integer.MIN_VALUE; + + // calculate max value + for(int i = 0; i < n;i++){ + if(paths[i] == Integer.MAX_VALUE) { + return -1; + } + result = Math.max(result, paths[i]); + } + + // return result + return result; + + } +} \ No newline at end of file pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy