Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/max sum non adjacent #5515

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
5 changes: 5 additions & 0 deletions src/main/java/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
BasedOnStyle: Google
IndentWidth: 4
UseTab: Never
AllowShortBlocksOnASingleLine: true
AllowShortCaseLabelsOnASingleLine: true
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* class calculates the two closest points.
*/
public final class ClosestPair {

/**
* Number of points
*/
Expand Down Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
* conquer algorithm
*/
public class SkylineAlgorithm {

private ArrayList<Point> points;

/**
Expand Down Expand Up @@ -126,7 +125,6 @@ public ArrayList<Point> produceFinalSkyLine(ArrayList<Point> left, ArrayList<Poi
}

public static class Point {

private int x;
private int y;

Expand Down Expand Up @@ -175,7 +173,6 @@ public boolean dominates(Point p1) {
* order get sorted later.
*/
class XComparator implements Comparator<Point> {

@Override
public int compare(Point a, Point b) {
return Integer.compare(a.x, b.x);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/

public class StrassenMatrixMultiplication {

// Function to multiply matrices
public int[][] multiply(int[][] a, int[][] b) {
int n = a.length;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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]; }
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading