diff --git a/solution/2800-2899/2873.Maximum Value of an Ordered Triplet I/README.md b/solution/2800-2899/2873.Maximum Value of an Ordered Triplet I/README.md index c11b140efb601..515c69c5e8861 100644 --- a/solution/2800-2899/2873.Maximum Value of an Ordered Triplet I/README.md +++ b/solution/2800-2899/2873.Maximum Value of an Ordered Triplet I/README.md @@ -68,6 +68,20 @@ ```java +class Solution { + public long maximumTripletValue(int[] nums) { + long max, maxDiff, ans; + max = 0; + maxDiff = 0; + ans = 0; + for (int num : nums) { + ans = Math.max(ans, num * maxDiff); + max = Math.max(max, num); + maxDiff = Math.max(maxDiff, max - num); + } + return ans; + } +} ``` diff --git a/solution/2800-2899/2873.Maximum Value of an Ordered Triplet I/README_EN.md b/solution/2800-2899/2873.Maximum Value of an Ordered Triplet I/README_EN.md index 20a21591ad0b8..fc4271ca21d02 100644 --- a/solution/2800-2899/2873.Maximum Value of an Ordered Triplet I/README_EN.md +++ b/solution/2800-2899/2873.Maximum Value of an Ordered Triplet I/README_EN.md @@ -58,6 +58,20 @@ It can be shown that there are no ordered triplets of indices with a value great ### **Java** ```java +class Solution { + public long maximumTripletValue(int[] nums) { + long max, maxDiff, ans; + max = 0; + maxDiff = 0; + ans = 0; + for (int num : nums) { + ans = Math.max(ans, num * maxDiff); + max = Math.max(max, num); + maxDiff = Math.max(maxDiff, max - num); + } + return ans; + } +} ``` diff --git a/solution/2800-2899/2873.Maximum Value of an Ordered Triplet I/Solution.java b/solution/2800-2899/2873.Maximum Value of an Ordered Triplet I/Solution.java new file mode 100644 index 0000000000000..2a020a1a66cca --- /dev/null +++ b/solution/2800-2899/2873.Maximum Value of an Ordered Triplet I/Solution.java @@ -0,0 +1,14 @@ +class Solution { + public long maximumTripletValue(int[] nums) { + long max, maxDiff, ans; + max = 0; + maxDiff = 0; + ans = 0; + for (int num : nums) { + ans = Math.max(ans, num * maxDiff); + max = Math.max(max, num); + maxDiff = Math.max(maxDiff, max - num); + } + return ans; + } +} \ No newline at end of file diff --git a/solution/2800-2899/2874.Maximum Value of an Ordered Triplet II/README.md b/solution/2800-2899/2874.Maximum Value of an Ordered Triplet II/README.md index 1eb14620c1f1c..5aa0fbce3d417 100644 --- a/solution/2800-2899/2874.Maximum Value of an Ordered Triplet II/README.md +++ b/solution/2800-2899/2874.Maximum Value of an Ordered Triplet II/README.md @@ -68,6 +68,20 @@ ```java +class Solution { + public long maximumTripletValue(int[] nums) { + long max, maxDiff, ans; + max = 0; + maxDiff = 0; + ans = 0; + for (int num : nums) { + ans = Math.max(ans, num * maxDiff); + max = Math.max(max, num); + maxDiff = Math.max(maxDiff, max - num); + } + return ans; + } +} ``` diff --git a/solution/2800-2899/2874.Maximum Value of an Ordered Triplet II/README_EN.md b/solution/2800-2899/2874.Maximum Value of an Ordered Triplet II/README_EN.md index 4562ad5ba8966..d8ffc3949c043 100644 --- a/solution/2800-2899/2874.Maximum Value of an Ordered Triplet II/README_EN.md +++ b/solution/2800-2899/2874.Maximum Value of an Ordered Triplet II/README_EN.md @@ -58,6 +58,20 @@ It can be shown that there are no ordered triplets of indices with a value great ### **Java** ```java +class Solution { + public long maximumTripletValue(int[] nums) { + long max, maxDiff, ans; + max = 0; + maxDiff = 0; + ans = 0; + for (int num : nums) { + ans = Math.max(ans, num * maxDiff); + max = Math.max(max, num); + maxDiff = Math.max(maxDiff, max - num); + } + return ans; + } +} ``` diff --git a/solution/2800-2899/2874.Maximum Value of an Ordered Triplet II/Solution.java b/solution/2800-2899/2874.Maximum Value of an Ordered Triplet II/Solution.java new file mode 100644 index 0000000000000..2a020a1a66cca --- /dev/null +++ b/solution/2800-2899/2874.Maximum Value of an Ordered Triplet II/Solution.java @@ -0,0 +1,14 @@ +class Solution { + public long maximumTripletValue(int[] nums) { + long max, maxDiff, ans; + max = 0; + maxDiff = 0; + ans = 0; + for (int num : nums) { + ans = Math.max(ans, num * maxDiff); + max = Math.max(max, num); + maxDiff = Math.max(maxDiff, max - num); + } + return ans; + } +} \ No newline at end of file diff --git a/solution/2800-2899/2875.Minimum Size Subarray in Infinite Array/README.md b/solution/2800-2899/2875.Minimum Size Subarray in Infinite Array/README.md index 24e8546c66108..7290df14e1115 100644 --- a/solution/2800-2899/2875.Minimum Size Subarray in Infinite Array/README.md +++ b/solution/2800-2899/2875.Minimum Size Subarray in Infinite Array/README.md @@ -71,6 +71,51 @@ ```java +class Solution { + public int shortestSubarray(int[] nums, int k) { + int n = nums.length; + + int minLength = n * 2 + 1; + int l = 0; + int sum = 0; + + for (int r = 0; r < n * 2; r++) { + int start = l % n; + int end = r % n; + sum += nums[end]; + + while (sum > k && l <= r) { + start = l % n; + sum -= nums[start]; + l++; + } + + if (sum == k) { + minLength = Math.min(minLength, r - l + 1); + start = l % n; + sum -= nums[start]; + l++; + } + } + + return minLength == n * 2 + 1 ? -1 : minLength; + } + public int minSizeSubarray(int[] nums, int target) { + int n = nums.length; + int sum = 0; + + for (int num : nums) { + sum += num; + } + int k = target % sum; + int ans = target / sum * n; + if (k == 0) { + return ans; + } + int res = shortestSubarray(nums, k); + return res == -1 ? -1 : ans + res; + } +} ``` diff --git a/solution/2800-2899/2875.Minimum Size Subarray in Infinite Array/README_EN.md b/solution/2800-2899/2875.Minimum Size Subarray in Infinite Array/README_EN.md index 6461327822400..38a51f189270f 100644 --- a/solution/2800-2899/2875.Minimum Size Subarray in Infinite Array/README_EN.md +++ b/solution/2800-2899/2875.Minimum Size Subarray in Infinite Array/README_EN.md @@ -62,6 +62,51 @@ It can be proven that there is no subarray with sum equal to target = 3. ### **Java** ```java +class Solution { + public int shortestSubarray(int[] nums, int k) { + int n = nums.length; + + int minLength = n * 2 + 1; + int l = 0; + int sum = 0; + + for (int r = 0; r < n * 2; r++) { + int start = l % n; + int end = r % n; + sum += nums[end]; + + while (sum > k && l <= r) { + start = l % n; + sum -= nums[start]; + l++; + } + + if (sum == k) { + minLength = Math.min(minLength, r - l + 1); + start = l % n; + sum -= nums[start]; + l++; + } + } + + return minLength == n * 2 + 1 ? -1 : minLength; + } + public int minSizeSubarray(int[] nums, int target) { + int n = nums.length; + int sum = 0; + + for (int num : nums) { + sum += num; + } + int k = target % sum; + int ans = target / sum * n; + if (k == 0) { + return ans; + } + int res = shortestSubarray(nums, k); + return res == -1 ? -1 : ans + res; + } +} ``` diff --git a/solution/2800-2899/2875.Minimum Size Subarray in Infinite Array/Solution.java b/solution/2800-2899/2875.Minimum Size Subarray in Infinite Array/Solution.java new file mode 100644 index 0000000000000..54aed718e4ecc --- /dev/null +++ b/solution/2800-2899/2875.Minimum Size Subarray in Infinite Array/Solution.java @@ -0,0 +1,45 @@ +class Solution { + public int shortestSubarray(int[] nums, int k) { + int n = nums.length; + int minLength = n * 2 + 1; + int l = 0; + int sum = 0; + + for (int r = 0; r < n * 2; r++) { + int start = l % n; + int end = r % n; + sum += nums[end]; + + while (sum > k && l <= r) { + start = l % n; + sum -= nums[start]; + l++; + } + + if (sum == k) { + minLength = Math.min(minLength, r - l + 1); + start = l % n; + sum -= nums[start]; + l++; + } + } + + return minLength == n * 2 + 1 ? -1 : minLength; + } + + public int minSizeSubarray(int[] nums, int target) { + int n = nums.length; + int sum = 0; + + for (int num : nums) { + sum += num; + } + int k = target % sum; + int ans = target / sum * n; + if (k == 0) { + return ans; + } + int res = shortestSubarray(nums, k); + return res == -1 ? -1 : ans + res; + } +} \ No newline at end of file diff --git a/solution/2800-2899/2876.Count Visited Nodes in a Directed Graph/README.md b/solution/2800-2899/2876.Count Visited Nodes in a Directed Graph/README.md index 3af7b84a53f2e..598d13d75c492 100644 --- a/solution/2800-2899/2876.Count Visited Nodes in a Directed Graph/README.md +++ b/solution/2800-2899/2876.Count Visited Nodes in a Directed Graph/README.md @@ -70,6 +70,42 @@ ```java +class Solution { + void dfs(int curr, List edges, int[] ans) { + + List path = new ArrayList<>(); + int prev = -1; + while (ans[curr] == 0) { + path.add(curr); + ans[curr] = prev == -1 ? -1 : ans[prev] - 1; + prev = curr; + curr = edges.get(curr); + } + int idx = path.size() - 1; + if (ans[curr] < 0) { + int cycle = ans[curr] - ans[path.get(idx)] + 1; + int start = ans[curr]; + for (; idx >= 0 && ans[path.get(idx)] <= start; idx--) { + ans[path.get(idx)] = cycle; + } + } + for (; idx >= 0; idx--) { + ans[path.get(idx)] = ans[edges.get(path.get(idx))] + 1; + } + } + + public int[] countVisitedNodes(List edges) { + int n = edges.size(); + int[] ans = new int[n]; + for (int i = 0; i < n; i++) { + if (ans[i] > 0) { + continue; + } + dfs(i, edges, ans); + } + return ans; + } +} ``` diff --git a/solution/2800-2899/2876.Count Visited Nodes in a Directed Graph/README_EN.md b/solution/2800-2899/2876.Count Visited Nodes in a Directed Graph/README_EN.md index a63b1eab76d01..6cc52c113e1b2 100644 --- a/solution/2800-2899/2876.Count Visited Nodes in a Directed Graph/README_EN.md +++ b/solution/2800-2899/2876.Count Visited Nodes in a Directed Graph/README_EN.md @@ -60,6 +60,42 @@ ### **Java** ```java +class Solution { + void dfs(int curr, List edges, int[] ans) { + + List path = new ArrayList<>(); + int prev = -1; + while (ans[curr] == 0) { + path.add(curr); + ans[curr] = prev == -1 ? -1 : ans[prev] - 1; + prev = curr; + curr = edges.get(curr); + } + int idx = path.size() - 1; + if (ans[curr] < 0) { + int cycle = ans[curr] - ans[path.get(idx)] + 1; + int start = ans[curr]; + for (; idx >= 0 && ans[path.get(idx)] <= start; idx--) { + ans[path.get(idx)] = cycle; + } + } + for (; idx >= 0; idx--) { + ans[path.get(idx)] = ans[edges.get(path.get(idx))] + 1; + } + } + + public int[] countVisitedNodes(List edges) { + int n = edges.size(); + int[] ans = new int[n]; + for (int i = 0; i < n; i++) { + if (ans[i] > 0) { + continue; + } + dfs(i, edges, ans); + } + return ans; + } +} ``` diff --git a/solution/2800-2899/2876.Count Visited Nodes in a Directed Graph/Solution.java b/solution/2800-2899/2876.Count Visited Nodes in a Directed Graph/Solution.java new file mode 100644 index 0000000000000..4bd3ed8bff1d8 --- /dev/null +++ b/solution/2800-2899/2876.Count Visited Nodes in a Directed Graph/Solution.java @@ -0,0 +1,36 @@ +class Solution { + void dfs(int curr, List edges, int[] ans) { + + List path = new ArrayList<>(); + int prev = -1; + while (ans[curr] == 0) { + path.add(curr); + ans[curr] = prev == -1 ? -1 : ans[prev] - 1; + prev = curr; + curr = edges.get(curr); + } + int idx = path.size() - 1; + if (ans[curr] < 0) { + int cycle = ans[curr] - ans[path.get(idx)] + 1; + int start = ans[curr]; + for (; idx >= 0 && ans[path.get(idx)] <= start; idx--) { + ans[path.get(idx)] = cycle; + } + } + for (; idx >= 0; idx--) { + ans[path.get(idx)] = ans[edges.get(path.get(idx))] + 1; + } + } + + public int[] countVisitedNodes(List edges) { + int n = edges.size(); + int[] ans = new int[n]; + for (int i = 0; i < n; i++) { + if (ans[i] > 0) { + continue; + } + dfs(i, edges, ans); + } + return ans; + } +} \ No newline at end of file