ogv

Untitled

ogv
Aug 17th, 2019
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.01 KB | None | 0 0
  1. class Solution {
  2.     public boolean divisorGame(int N) {
  3.         return N % 2 == 0;
  4.     }
  5. }
  6. // normal DP, Runtime: 0 ms, faster than 100.00% of Java online submissions for Divisor Game.
  7. class Solution {
  8.     public boolean divisorGame(int N) {
  9.         if (N == 0) return true;
  10.        
  11.         byte[] cache = new byte[N + 1];
  12.         return wins(N, cache) == +1;
  13.     }
  14.    
  15.     private byte wins(int n, byte[] cache) {
  16.         if (n == 1) return -1;
  17.         if (cache[n] != 0) return cache[n];
  18.        
  19.         byte result = -1;
  20.         for (int x = 1; x * x <= n; x++ ) {
  21.             if (n % x == 0) {
  22.                 if (wins(n - x, cache) == -1) {
  23.                     result = +1;
  24.                     break;
  25.                 }
  26.                
  27.                 int y = n / x;
  28.                 if (y < n && wins(n - y, cache) == -1) {
  29.                     result = +1;
  30.                     break;
  31.                 }
  32.             }
  33.         }
  34.        
  35.         cache[n] = result;
  36.         return result;
  37.     }
  38. }
Add Comment
Please, Sign In to add comment