// Name(s): // Email id(s): import java.util.*; import java.awt.*; public class WorkPlan { public static void main( String[] args ) { int[] low = { 10, 1, 10, 10, 0, 10, 1, 10, 10 }; int[] high = { 5, 50, 5, 1, 0, 5, 50, 5, 1 }; Solution solution = solve( low, high ); System.out.println( solution ); System.out.println(); System.out.println( solution.validate() ); } public static Solution solve ( int[] low, int high[] ) { int n = low.length; int[] week = new int [ n ]; String schedule = ""; int prev2Weeks = 0; int prev1Weeks = 0; for ( int i = 0; i < n; ++i ) { int poss1 = prev2Weeks + high[i]; int poss2 = prev1Weeks + low[i]; week[ i ] = ( poss1 > poss2 ) ? poss1 : poss2; prev2Weeks = prev1Weeks; prev1Weeks = week[i]; } boolean lastHigh = false; for (int i = n-1; i > 0; --i ) { if ( lastHigh ) { schedule = "-" + schedule; lastHigh = false; } else if ( week[i] == ( week[i-1] + low[i] ) ) { schedule = "L" + schedule; lastHigh = false; } else { schedule = "H" + schedule; lastHigh = true; } } if ( lastHigh ) { schedule = "-" + schedule; } else { schedule = ( low[0] < high[0] ? "H" : "L" ) + schedule; } return new Solution( low, high, week, schedule ); } } class Solution { int[] low; int[] high; int[] week; String schedule; public Solution(int[] low, int[] high, int[] week, String schedule ) { this.low = low; this.high = high; this.week = week; this.schedule = schedule; } public String toString() { String result = ""; for (int i = 0; i < low.length; ++i ) { result += String.format( "%3d ", low[i] ); } result += "\n"; for (int i = 0; i < high.length; ++i ) { result += String.format( "%3d ", high[i] ); } result += "\n\n"; for (int i = 0; i < schedule.length(); ++i ) { result += " " + schedule.charAt(i) + " "; } result += "\n\n"; for (int i = 0; i < week.length; ++i ) { result += String.format( "%3d ", week[i] ); } return result; } public boolean validate() { int n = low.length; if ( ( schedule.length() != n ) || ( high.length != n) ) { return false; } for (int i= n-1; i > 0; --i) { char c = schedule.charAt(i); char d = ( i > 1 ) ? schedule.charAt(i-1) : '-'; char e = ( i < n-1 ) ? schedule.charAt(i+1) : '+'; int wi = week[i]; int wi1 = ( i > 0 ) ? week[i-1] : 0; int wi2 = ( i > 1 ) ? week[i-2] : 0; if ( ( wi != wi1 + low[i] ) && ( wi != wi2 + high[i] ) ) { return false; } if ( ( c == 'H' ) && ( ( d != '-' ) || ( wi != wi2 + high[i] ) ) ) { return false; } if ( ( c == '-' ) && ( e != 'H' ) ) { return false; } } // seems ok return true; } }