Created
August 14, 2019 03:45
-
-
Save sreeprasad/7de965ffbdae08db820dfdff266e3a0d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void findMax(int N) throws Exception { | |
final int maxN = 100007; | |
long [] freqMap = new long[maxN]; | |
for(int i=1;i<=N;i++){ | |
freqMap[getNextInt()]++; | |
} | |
long [] dp = new long[maxN]; | |
dp[0] = 0; | |
dp[1] = freqMap[1]; | |
for(int i=2;i<maxN;i++){ | |
dp[i] = Math.max(dp[i-1], dp[i-2] + freqMap[i]*i); | |
//System.out.printf("%d: dp[%d]: %d\n",i,i, dp[i]); | |
} | |
System.out.printf("%d\n", dp[maxN-1]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment