beat 100% 22ms java solution


  • 0
    L

    calculate cumulative sum and result in place

    public class Solution {
        public int kInversePairs(int n, int k) {
            int[] d = new int[k+1];
            d[0] = 1;
            for(int i = 2;i <= n;i++){
               for(int j = 1;j <= k;j++){
                    d[j] = (d[j] + d[j-1]) % 1000000007;
               }
               for(int j = k;j >= i;j--){
                    d[j] = (d[j] - d[j-i] + 1000000007) % 1000000007;
               }
            }
            return d[k];
        }
    }

  • 0
    F

    could you give some explanations? Thank U!


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.