JAVA solution shared, within 10 lines. Question insde.

  • 0

    The problem is similar to the stock buy and sell ones, but is there a given name to the specific approach to this kind of problem? I'm asking if there's a name like those DP, Divide and Conquer, Binary, etc..

    public class Solution {
    public int rob(int[] num) {
        if(num==null||num.length==0) return 0;
        if(num.length==1) return num[0];
        if(num.length==2) return Math.max(num[0],num[1]);
        int prev=num[0];
        int curr=Math.max(num[0],num[1]);
        for(int i=2;i<num.length;i++){
            int temp=Math.max(curr,num[i]+prev);
            return curr;

  • 0

    This is kind of interesting DP

  • 0

    I assume the same way, DP is like solving the problem with continuously updating the answer stage. is it?

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.