How might I optimize these two mutually recursive functions in Java?

J.D. Rudie :

I have to write methods that sum and provide lists of the outputs of these composite mutually recursive functions, but their execution keeps timing out with my current implementation:

public static long fAnn(long n) {
      if (n == 0) return 1;
      else return n - fJohn(fAnn(n-1));
    }

    public static long fJohn(long n) {
      if (n <= 0) return 0;
      else return n - fAnn(fJohn(n-1));
    }
public static List<Long> john(long n) {
    List<Long> res = new ArrayList<Long>();

    for (long i = 0; i < n; i++) {
      res.add(fJohn(i));
    }

    return res;       
}

public static long sumJohn(long n) {
    long sum = 0;
    for (long i = 0; i < n; i++) sum += fJohn(i);
    return sum;
}
public static long sumAnn(long n) {
    long sum = 0;
    for (long i = 0; i < n; i++) sum += fAnn(i);
    return sum;
}

I've thought of passing the last value of the function back to the function, but I'm not really sure how I could do that.

J.D. Rudie :

I took the advice of @Tim Beigeleisen and decided to learn more about dynamic programming to improve the naive recursive approach I took to this function instead of looking for answers here first.

Here's the code I came up with:

import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


public class Johnann {
    private static Map<Long, Long> jMem;
    private static Map<Long, Long> aMem;


    public static long fAnn(long n) {
      if (n < 2) {
        aMem = new HashMap<Long, Long>();
        aMem.put(n, (long)1);
        return 1;
      }
      else if (aMem.keySet().contains(n)) {
        return aMem.get(n);
      }
      else {
        long res = n - fJohn(fAnn(n-1));
        aMem.put(n, res);
        return res;
      }
    }


    public static long fJohn(long n) {
      if (n < 2) {
        jMem = new HashMap<Long, Long>();
        jMem.put(n, (long)0);
        return 0;
      }
      else if (jMem.keySet().contains(n)) {
        return jMem.get(n);
      }
      else {
        long res = n - fAnn(fJohn(n-1));
        jMem.put(n, res);
        return res;
      }
    }

    public static List<Long> john(long n) {
        List<Long> res = new ArrayList<Long>();

        for (long i = 0; i < n; i++) {
          res.add(fJohn(i));
        }

        return res;
    }

    public static List<Long> ann(long n) {
        System.out.println(n);
        List<Long> res = new ArrayList<Long>();

        for (long i = 0; i < n; i++) {
          res.add(fAnn(i));
        }

        return res;
    }


    public static long sumJohn(long n) {
        if (n == 0) return 0;
        else if (n < 2) return 1;
        long sum = 0;
        for (long i = 0; i < n; i++) sum += fJohn(i);
        return sum;
    }
    public static long sumAnn(long n) {
        if (n == 0) return 0;
        else if (n < 2) return 1;
        long sum = 0;
        for (long i = 0; i < n; i++) sum += fAnn(i);
        return sum;
    }
}

I saw other, better implementations that used an ArrayList instead of a map, for instance:

import java.util.*;

public class Johnann {

    private enum Person {JOHN, ANN}

    private static List<Long> getListForName(Person person, long n) {
        List<Long> ann = new ArrayList<>(Arrays.asList(1L));
        List<Long> john = new ArrayList<>(Arrays.asList(0L));

        for (int dayAnn = 1, dayJohn = 1; dayAnn < n || dayJohn < n; ) {
            if (john.size() > ann.get(dayAnn - 1)) {
                ann.add(dayAnn - john.get(Math.toIntExact(ann.get(dayAnn - 1))));
                dayAnn++;
            }
            if (ann.size() > john.get(dayJohn - 1)) {
                john.add(dayJohn - ann.get(Math.toIntExact(john.get(dayJohn - 1))));
                dayJohn++;
            }
        }
        return (person == Person.JOHN ? john : ann).subList(0, Math.toIntExact(n));
    }

    public static List<Long> john(long n) {
        return getListForName(Person.JOHN, n);
    }

    public static List<Long> ann(long n) {
        return getListForName(Person.ANN, n);
    }

    public static long sumJohn(long n) {
        return john(n).stream().mapToLong(Long::longValue).sum();
    }

    public static long sumAnn(long n) {
        return ann(n).stream().mapToLong(Long::longValue).sum();
    }
}

This was probably way faster, but I'm just happy that I learned a lot about dynamic programming and optimizing recursive calls through this problem.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=109026&siteId=1