Skip to content

Commit e5e2381

Browse files
authored
Merge branch 'master' into refactor/change_package
2 parents b47ff2f + f8ff6af commit e5e2381

18 files changed

+217
-244
lines changed

pmd-exclude.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ com.thealgorithms.datastructures.crdt.LWWElementSet=UselessParentheses
1212
com.thealgorithms.datastructures.crdt.Pair=UnusedPrivateField
1313
com.thealgorithms.datastructures.graphs.AStar=UselessParentheses
1414
com.thealgorithms.datastructures.graphs.AdjacencyMatrixGraph=CollapsibleIfStatements,UnnecessaryFullyQualifiedName,UselessParentheses
15-
com.thealgorithms.datastructures.graphs.BipartiteGrapfDFS=CollapsibleIfStatements
15+
com.thealgorithms.datastructures.graphs.BipartiteGraphDFS=CollapsibleIfStatements
1616
com.thealgorithms.datastructures.graphs.Kruskal=UselessParentheses
1717
com.thealgorithms.datastructures.hashmap.hashing.HashMapCuckooHashing=UselessParentheses
1818
com.thealgorithms.datastructures.heaps.FibonacciHeap=UselessParentheses

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
<dependency>
5151
<groupId>org.apache.commons</groupId>
5252
<artifactId>commons-lang3</artifactId>
53-
<version>3.16.0</version>
53+
<version>3.17.0</version>
5454
</dependency>
5555
<dependency>
5656
<groupId>org.apache.commons</groupId>

src/main/java/com/thealgorithms/datastructures/graphs/BipartiteGrapfDFS.java renamed to src/main/java/com/thealgorithms/datastructures/graphs/BipartiteGraphDFS.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
*
1515
* Output : YES
1616
*/
17-
public final class BipartiteGrapfDFS {
18-
private BipartiteGrapfDFS() {
17+
public final class BipartiteGraphDFS {
18+
private BipartiteGraphDFS() {
1919
}
2020

2121
private static boolean bipartite(int v, ArrayList<ArrayList<Integer>> adj, int[] color, int node) {

src/main/java/com/thealgorithms/datastructures/lists/MergeKSortedLinkedlist.java renamed to src/main/java/com/thealgorithms/datastructures/lists/MergeKSortedLinkedList.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/**
88
* @author Arun Pandey (https://github.com/pandeyarun709)
99
*/
10-
public class MergeKSortedLinkedlist {
10+
public class MergeKSortedLinkedList {
1111

1212
/**
1313
* This function merge K sorted LinkedList
Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,16 @@
11
package com.thealgorithms.dynamicprogramming;
22

3-
/*
4-
* this is an important Algo in which
5-
* we have starting and ending of board and we have to reach
6-
* we have to count no. of ways
7-
* that help to reach end point i.e number by rolling dice
8-
* which have 1 to 6 digits
9-
10-
Test Case:
11-
here target is 10
12-
13-
int n=10;
14-
startAlgo();
15-
System.out.println(bpR(0,n));
16-
System.out.println(endAlgo()+"ms");
17-
int[] strg=new int [n+1];
18-
startAlgo();
19-
System.out.println(bpRS(0,n,strg));
20-
System.out.println(endAlgo()+"ms");
21-
startAlgo();
22-
System.out.println(bpIS(0,n,strg));
23-
System.out.println(endAlgo()+"ms");
24-
25-
26-
27-
*/
283
public final class BoardPath {
294
private BoardPath() {
305
}
316

32-
public static long startTime;
33-
public static long endTime;
34-
35-
public static void startAlgo() {
36-
startTime = System.currentTimeMillis();
37-
}
38-
39-
public static long endAlgo() {
40-
endTime = System.currentTimeMillis();
41-
return endTime - startTime;
42-
}
43-
7+
/**
8+
* Recursive solution without memoization
9+
*
10+
* @param start - the current position
11+
* @param end - the target position
12+
* @return the number of ways to reach the end from the start
13+
*/
4414
public static int bpR(int start, int end) {
4515
if (start == end) {
4616
return 1;
@@ -54,6 +24,14 @@ public static int bpR(int start, int end) {
5424
return count;
5525
}
5626

27+
/**
28+
* Recursive solution with memoization
29+
*
30+
* @param curr - the current position
31+
* @param end - the target position
32+
* @param strg - memoization array
33+
* @return the number of ways to reach the end from the start
34+
*/
5735
public static int bpRS(int curr, int end, int[] strg) {
5836
if (curr == end) {
5937
return 1;
@@ -71,15 +49,23 @@ public static int bpRS(int curr, int end, int[] strg) {
7149
return count;
7250
}
7351

52+
/**
53+
* Iterative solution with tabulation
54+
*
55+
* @param curr - the current position (always starts from 0)
56+
* @param end - the target position
57+
* @param strg - memoization array
58+
* @return the number of ways to reach the end from the start
59+
*/
7460
public static int bpIS(int curr, int end, int[] strg) {
7561
strg[end] = 1;
7662
for (int i = end - 1; i >= 0; i--) {
7763
int count = 0;
78-
for (int dice = 1; dice <= 6 && dice + i < strg.length; dice++) {
64+
for (int dice = 1; dice <= 6 && dice + i <= end; dice++) {
7965
count += strg[i + dice];
8066
}
8167
strg[i] = count;
8268
}
83-
return strg[0];
69+
return strg[curr];
8470
}
8571
}

src/main/java/com/thealgorithms/dynamicprogramming/SubsetSum.java

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,46 +5,32 @@ private SubsetSum() {
55
}
66

77
/**
8-
* Driver Code
9-
*/
10-
public static void main(String[] args) {
11-
int[] arr = new int[] {50, 4, 10, 15, 34};
12-
assert subsetSum(arr, 64);
13-
/* 4 + 10 + 15 + 34 = 64 */
14-
assert subsetSum(arr, 99);
15-
/* 50 + 15 + 34 = 99 */
16-
assert !subsetSum(arr, 5);
17-
assert !subsetSum(arr, 66);
18-
}
19-
20-
/**
21-
* Test if a set of integers contains a subset that sum to a given integer.
8+
* Test if a set of integers contains a subset that sums to a given integer.
229
*
23-
* @param arr the array contains integers.
24-
* @param sum target sum of subset.
25-
* @return {@code true} if subset exists, otherwise {@code false}.
10+
* @param arr the array containing integers.
11+
* @param sum the target sum of the subset.
12+
* @return {@code true} if a subset exists that sums to the given value, otherwise {@code false}.
2613
*/
2714
public static boolean subsetSum(int[] arr, int sum) {
2815
int n = arr.length;
29-
boolean[][] isSum = new boolean[n + 2][sum + 1];
16+
boolean[][] isSum = new boolean[n + 1][sum + 1];
3017

31-
isSum[n + 1][0] = true;
32-
for (int i = 1; i <= sum; i++) {
33-
isSum[n + 1][i] = false;
18+
// Initialize the first column to true since a sum of 0 can always be achieved with an empty subset.
19+
for (int i = 0; i <= n; i++) {
20+
isSum[i][0] = true;
3421
}
3522

36-
for (int i = n; i > 0; i--) {
37-
isSum[i][0] = true;
38-
for (int j = 1; j <= arr[i - 1] - 1; j++) {
39-
if (j <= sum) {
40-
isSum[i][j] = isSum[i + 1][j];
23+
// Fill the subset sum matrix
24+
for (int i = 1; i <= n; i++) {
25+
for (int j = 1; j <= sum; j++) {
26+
if (arr[i - 1] <= j) {
27+
isSum[i][j] = isSum[i - 1][j] || isSum[i - 1][j - arr[i - 1]];
28+
} else {
29+
isSum[i][j] = isSum[i - 1][j];
4130
}
4231
}
43-
for (int j = arr[i - 1]; j <= sum; j++) {
44-
isSum[i][j] = (isSum[i + 1][j] || isSum[i + 1][j - arr[i - 1]]);
45-
}
4632
}
4733

48-
return isSum[1][sum];
34+
return isSum[n][sum];
4935
}
5036
}

src/main/java/com/thealgorithms/maths/LeastCommonMultiple.java

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,27 @@
11
package com.thealgorithms.maths;
22

3-
import java.util.Scanner;
4-
53
/**
64
* Is a common mathematics concept to find the smallest value number
75
* that can be divide using either number without having the remainder.
86
* https://maticschool.blogspot.com/2013/11/find-least-common-multiple-lcm.html
97
* @author LauKinHoong
108
*/
11-
129
public final class LeastCommonMultiple {
1310
private LeastCommonMultiple() {
1411
}
1512

1613
/**
17-
* Driver Code
18-
*/
19-
public static void main(String[] args) {
20-
Scanner input = new Scanner(System.in);
21-
System.out.println("Please enter first number >> ");
22-
int num1 = input.nextInt();
23-
System.out.println("Please enter second number >> ");
24-
int num2 = input.nextInt();
25-
System.out.println("The least common multiple of two numbers is >> " + lcm(num1, num2));
26-
input.close();
27-
}
28-
29-
/*
30-
* get least common multiple from two number
14+
* Finds the least common multiple of two numbers.
15+
*
16+
* @param num1 The first number.
17+
* @param num2 The second number.
18+
* @return The least common multiple of num1 and num2.
3119
*/
3220
public static int lcm(int num1, int num2) {
3321
int high;
3422
int num3;
3523
int cmv = 0;
36-
/*
37-
* value selection for the numerator
38-
*/
24+
3925
if (num1 > num2) {
4026
high = num1;
4127
num3 = num1;
Lines changed: 36 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,53 @@
11
package com.thealgorithms.others;
22

33
import java.util.HashSet;
4+
import java.util.Set;
45

5-
/*
6-
References: https://en.wikipedia.org/wiki/Streaming_algorithm
7-
* In this model, the function of interest is computing over a fixed-size window in the stream. As the stream progresses,
8-
* items from the end of the window are removed from consideration while new items from the stream take their place.
9-
* @author Swarga-codes (https://github.com/Swarga-codes)
10-
*/
6+
/**
7+
* References: https://en.wikipedia.org/wiki/Streaming_algorithm
8+
*
9+
* This model involves computing the maximum sum of subarrays of a fixed size \( K \) from a stream of integers.
10+
* As the stream progresses, elements from the end of the window are removed, and new elements from the stream are added.
11+
*
12+
* @author Swarga-codes (https://github.com/Swarga-codes)
13+
*/
1114
public final class MaximumSumOfDistinctSubarraysWithLengthK {
1215
private MaximumSumOfDistinctSubarraysWithLengthK() {
1316
}
14-
/*
15-
* Returns the maximum sum of subarray of size K consisting of distinct
16-
* elements.
17-
*
18-
* @param k size of the subarray which should be considered from the given
19-
* array.
17+
18+
/**
19+
* Finds the maximum sum of a subarray of size K consisting of distinct elements.
2020
*
21-
* @param nums is the array from which we would be finding the required
22-
* subarray.
21+
* @param k The size of the subarray.
22+
* @param nums The array from which subarrays will be considered.
2323
*
24-
* @return the maximum sum of distinct subarray of size K.
24+
* @return The maximum sum of any distinct-element subarray of size K. If no such subarray exists, returns 0.
2525
*/
2626
public static long maximumSubarraySum(int k, int... nums) {
2727
if (nums.length < k) {
2828
return 0;
2929
}
30-
long max = 0; // this will store the max sum which will be our result
31-
long s = 0; // this will store the sum of every k elements which can be used to compare with
32-
// max
33-
HashSet<Integer> set = new HashSet<>(); // this can be used to store unique elements in our subarray
34-
// Looping through k elements to get the sum of first k elements
30+
long masSum = 0; // Variable to store the maximum sum of distinct subarrays
31+
long currentSum = 0; // Variable to store the sum of the current subarray
32+
Set<Integer> currentSet = new HashSet<>(); // Set to track distinct elements in the current subarray
33+
34+
// Initialize the first window
3535
for (int i = 0; i < k; i++) {
36-
s += nums[i];
37-
set.add(nums[i]);
36+
currentSum += nums[i];
37+
currentSet.add(nums[i]);
3838
}
39-
// Checking if the first kth subarray contains unique elements or not if so then
40-
// we assign that to max
41-
if (set.size() == k) {
42-
max = s;
39+
// If the first window contains distinct elements, update maxSum
40+
if (currentSet.size() == k) {
41+
masSum = currentSum;
4342
}
44-
// Looping through the rest of the array to find different subarrays and also
45-
// utilising the sliding window algorithm to find the sum
46-
// in O(n) time complexity
43+
// Slide the window across the array
4744
for (int i = 1; i < nums.length - k + 1; i++) {
48-
s = s - nums[i - 1];
49-
s = s + nums[i + k - 1];
45+
// Update the sum by removing the element that is sliding out and adding the new element
46+
currentSum = currentSum - nums[i - 1];
47+
currentSum = currentSum + nums[i + k - 1];
5048
int j = i;
5149
boolean flag = false; // flag value which says that the subarray contains distinct elements
52-
while (j < i + k && set.size() < k) {
50+
while (j < i + k && currentSet.size() < k) {
5351
if (nums[i - 1] == nums[j]) {
5452
flag = true;
5553
break;
@@ -58,17 +56,14 @@ public static long maximumSubarraySum(int k, int... nums) {
5856
}
5957
}
6058
if (!flag) {
61-
set.remove(nums[i - 1]);
59+
currentSet.remove(nums[i - 1]);
6260
}
63-
set.add(nums[i + k - 1]);
64-
// if the subarray contains distinct elements then we compare and update the max
65-
// value
66-
if (set.size() == k) {
67-
if (max < s) {
68-
max = s;
69-
}
61+
currentSet.add(nums[i + k - 1]);
62+
// If the current window has distinct elements, compare and possibly update maxSum
63+
if (currentSet.size() == k && masSum < currentSum) {
64+
masSum = currentSum;
7065
}
7166
}
72-
return max; // the final maximum sum
67+
return masSum; // the final maximum sum
7368
}
7469
}

src/main/java/com/thealgorithms/stacks/LargestRectangle.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public final class LargestRectangle {
1111
private LargestRectangle() {
1212
}
1313

14-
public static String largestRectanglehistogram(int[] heights) {
14+
public static String largestRectangleHistogram(int[] heights) {
1515
int n = heights.length;
1616
int maxArea = 0;
1717
Stack<int[]> st = new Stack<>();
@@ -32,7 +32,7 @@ public static String largestRectanglehistogram(int[] heights) {
3232
}
3333

3434
public static void main(String[] args) {
35-
assert largestRectanglehistogram(new int[] {2, 1, 5, 6, 2, 3}).equals("10");
36-
assert largestRectanglehistogram(new int[] {2, 4}).equals("4");
35+
assert largestRectangleHistogram(new int[] {2, 1, 5, 6, 2, 3}).equals("10");
36+
assert largestRectangleHistogram(new int[] {2, 4}).equals("4");
3737
}
3838
}

src/main/java/com/thealgorithms/strings/ReverseWordsInString.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ private ReverseWordsInString() {
1313
* @param s the input string
1414
* @return A string created by reversing the order of the words in {@code s}
1515
*/
16-
1716
public static String reverseWordsInString(final String s) {
1817
var words = s.trim().split("\\s+");
1918
Collections.reverse(Arrays.asList(words));

src/main/java/com/thealgorithms/strings/Upper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ public static void main(String[] args) {
1515
}
1616

1717
/**
18-
* Converts all of the characters in this {@code String} to upper case
18+
* Converts all the characters in this {@code String} to upper case
1919
*
2020
* @param s the string to convert
2121
* @return the {@code String}, converted to uppercase.
2222
*/
2323
public static String toUpperCase(String s) {
24-
if (s == null || "".equals(s)) {
24+
if (s == null || s.isEmpty()) {
2525
return s;
2626
}
2727
char[] values = s.toCharArray();

0 commit comments

Comments
 (0)