Skip to content

Instantly share code, notes, and snippets.

@AhmadElsagheer
Created December 31, 2017 11:19
Show Gist options
  • Save AhmadElsagheer/4f00eda3ebc3869cbf1b725240c4a483 to your computer and use it in GitHub Desktop.
Save AhmadElsagheer/4f00eda3ebc3869cbf1b725240c4a483 to your computer and use it in GitHub Desktop.
Players Showering
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
// Input: Number of players, showering time per player
public class Shower {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
time = new int[N];
int s = 0;
for(int i = 0; i < N; ++i)
s += time[i] = sc.nextInt();
System.out.println("Min time = " + solve(0, 0, 0, 0));
}
static int[] time;
static int solve(int player, int sh1, int sh2, int sh3)
{
if(player == time.length)
return Math.max(Math.max(sh1, sh2), sh3);
int goInShower1 = solve(player + 1, sh1 + time[player], sh2, sh3);
int goInShower2 = solve(player + 1, sh1, sh2 + time[player], sh3);
int goInShower3 = solve(player + 1, sh1, sh2, sh3 + time[player]);
return Math.min(goInShower1, Math.min(goInShower2, goInShower3));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment