Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. public class Solution {
  2.  
  3. public static String solution(String x, String y) {
  4. String cycles = minCycles(1, 1, Integer.valueOf(x), Integer.valueOf(y));
  5. if(cycles == null)
  6. return "impossible";
  7. else
  8. return cycles;
  9. }
  10.  
  11. public static String minCycles(int x, int y, int m, int f){
  12. if(x == m && y == f) {
  13. return "0";
  14. }
  15.  
  16. if(x > m || y > f)
  17. return null;
  18.  
  19. String result1 = minCycles(x, x+y, m, f);
  20. String result2 = minCycles(x+y, y, m, f);
  21.  
  22. if (result1 == null && result2 == null) {
  23. return null;
  24. } else if (result1 == null || result2 == null) {
  25. if(result1 == null) {
  26. int a = Integer.valueOf(result2);
  27. return String.valueOf(++a);
  28. }
  29. else {
  30. int b = Integer.valueOf(result1);
  31. return String.valueOf(++b);
  32. }
  33. } else {
  34. int a = Integer.valueOf(result1);
  35. int b = Integer.valueOf(result2);
  36. int c = Math.min(a, b);
  37. return String.valueOf(++c);
  38. }
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement