diff --git a/src/main/java/.clang-format b/src/main/java/.clang-format new file mode 100644 index 000000000000..5edfbf265e07 --- /dev/null +++ b/src/main/java/.clang-format @@ -0,0 +1,5 @@ +BasedOnStyle: Google +IndentWidth: 4 +UseTab: Never +AllowShortBlocksOnASingleLine: true +AllowShortCaseLabelsOnASingleLine: true diff --git a/src/main/java/com/thealgorithms/divideandconquer/BinaryExponentiation.java b/src/main/java/com/thealgorithms/divideandconquer/BinaryExponentiation.java index de829585891a..ef1421601979 100644 --- a/src/main/java/com/thealgorithms/divideandconquer/BinaryExponentiation.java +++ b/src/main/java/com/thealgorithms/divideandconquer/BinaryExponentiation.java @@ -13,7 +13,6 @@ */ public class BinaryExponentiation { - // recursive function to calculate a to the power of b public static long calculatePower(long x, long y) { // Base Case diff --git a/src/main/java/com/thealgorithms/divideandconquer/ClosestPair.java b/src/main/java/com/thealgorithms/divideandconquer/ClosestPair.java index aa453539ac94..a315358794b8 100644 --- a/src/main/java/com/thealgorithms/divideandconquer/ClosestPair.java +++ b/src/main/java/com/thealgorithms/divideandconquer/ClosestPair.java @@ -5,7 +5,6 @@ * class calculates the two closest points. */ public final class ClosestPair { - /** * Number of points */ @@ -52,7 +51,6 @@ public static void setSecondCount(int secondCount) { * Location class is an auxiliary type to keep points coordinates. */ public static class Location { - double x; double y; diff --git a/src/main/java/com/thealgorithms/divideandconquer/SkylineAlgorithm.java b/src/main/java/com/thealgorithms/divideandconquer/SkylineAlgorithm.java index 610b1b78a36a..4ae074962ba0 100644 --- a/src/main/java/com/thealgorithms/divideandconquer/SkylineAlgorithm.java +++ b/src/main/java/com/thealgorithms/divideandconquer/SkylineAlgorithm.java @@ -10,7 +10,6 @@ * conquer algorithm */ public class SkylineAlgorithm { - private ArrayList points; /** @@ -126,7 +125,6 @@ public ArrayList produceFinalSkyLine(ArrayList left, ArrayList { - @Override public int compare(Point a, Point b) { return Integer.compare(a.x, b.x); diff --git a/src/main/java/com/thealgorithms/divideandconquer/StrassenMatrixMultiplication.java b/src/main/java/com/thealgorithms/divideandconquer/StrassenMatrixMultiplication.java index 86a6f3e11483..89774330437c 100644 --- a/src/main/java/com/thealgorithms/divideandconquer/StrassenMatrixMultiplication.java +++ b/src/main/java/com/thealgorithms/divideandconquer/StrassenMatrixMultiplication.java @@ -16,7 +16,6 @@ */ public class StrassenMatrixMultiplication { - // Function to multiply matrices public int[][] multiply(int[][] a, int[][] b) { int n = a.length; @@ -99,9 +98,7 @@ public int[][] sub(int[][] a, int[][] b) { int[][] c = new int[n][n]; for (int i = 0; i < n; i++) { - for (int j = 0; j < n; j++) { - c[i][j] = a[i][j] - b[i][j]; - } + for (int j = 0; j < n; j++) { c[i][j] = a[i][j] - b[i][j]; } } return c; @@ -114,9 +111,7 @@ public int[][] add(int[][] a, int[][] b) { int[][] c = new int[n][n]; for (int i = 0; i < n; i++) { - for (int j = 0; j < n; j++) { - c[i][j] = a[i][j] + b[i][j]; - } + for (int j = 0; j < n; j++) { c[i][j] = a[i][j] + b[i][j]; } } return c; @@ -125,18 +120,14 @@ public int[][] add(int[][] a, int[][] b) { // Function to split parent matrix into child matrices public void split(int[][] p, int[][] c, int iB, int jB) { for (int i1 = 0, i2 = iB; i1 < c.length; i1++, i2++) { - for (int j1 = 0, j2 = jB; j1 < c.length; j1++, j2++) { - c[i1][j1] = p[i2][j2]; - } + for (int j1 = 0, j2 = jB; j1 < c.length; j1++, j2++) { c[i1][j1] = p[i2][j2]; } } } // Function to join child matrices into (to) parent matrix public void join(int[][] c, int[][] p, int iB, int jB) { for (int i1 = 0, i2 = iB; i1 < c.length; i1++, i2++) { - for (int j1 = 0, j2 = jB; j1 < c.length; j1++, j2++) { - p[i2][j2] = c[i1][j1]; - } + for (int j1 = 0, j2 = jB; j1 < c.length; j1++, j2++) { p[i2][j2] = c[i1][j1]; } } } } diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/MaxSumNonAdjacent.java b/src/main/java/com/thealgorithms/dynamicprogramming/MaxSumNonAdjacent.java new file mode 100644 index 000000000000..71cb03d4ffd0 --- /dev/null +++ b/src/main/java/com/thealgorithms/dynamicprogramming/MaxSumNonAdjacent.java @@ -0,0 +1,44 @@ +package com.thealgorithms.dynamicprogramming; + +/* +What would you like to Propose? +I would like to propose adding an implementation of the Maximum Sum of Non-Adjacent Elements +algorithm to the dynamic programming section of the repository. Issue details Problem Statement: +Given an array of integers, write a function to find the maximum sum of non-adjacent elements. The +elements can be chosen such that no two chosen elements are adjacent in the array. For example: +Input: [3, 2, 5, 10, 7] +Output: 15 (The maximum sum is obtained by selecting 3, 7, and 5) +Approach: +Use dynamic programming to maintain a running maximum sum. +For each element, decide to either include it in the sum (and skip the previous element) or exclude +it (and keep the sum up to the previous element).*/ +// Problem explanation: +// "https://medium.com/@amitrajit_bose/max-sum-of-non-adjacent-elements-a04ebc4f2602" + +public class MaxSumNonAdjacent { + static int solveUtil(int index, int[] arr, int[] dp) { + if (index < 0) { + return 0; + } + + if (index == 0) { + return arr[index]; + } + + if (dp[index] != -1) { + return dp[index]; + } + + int pick = arr[index] + solveUtil(index - 2, arr, dp); + int nonPick = solveUtil(index - 1, arr, dp); + + return dp[index] = Math.max(pick, nonPick); + } + + static int solve(int n, int[] arr) { + int[] dp = new int[n]; + for (int i = 0; i < n; i++) dp[i] = -1; + + return solveUtil(n - 1, arr, dp); + } +}