千问 LeetCode 629. K Inverse Pairs Array Java Implement

发布时间:2026/10/18 7:21:48
千问    LeetCode 629. K Inverse Pairs Array Java Implement Here’s the Java solution for LeetCode 629. K Inverse Pairs Array, including both the standard prefix-sum version and a rolling-window variant.ProblemGiven n and k, count the number of permutations of 1…n that contain exactly k inverse pairs (a pair (i, j) with i j and nums[i] nums[j]). Return the result modulo 10^9 7.Examplesn 3, k 0 → 1 (only [1,2,3])n 3, k 1 → 2 ([1,3,2], [2,1,3])n 3, k 3 → 0Core RecurrenceInsert the largest number i into a permutation of 1…i-1. Placing it at position p from the end creates exactly p new inverse pairs, where p ranges over 0 … i-1. Therefore:dp[i][j] dp[i-1][j] dp[i-1][j-1] … dp[i-1][j-(i-1)]Naively this is O(n·k²). Using a prefix sum (or sliding window) turns each transition into O(1), giving O(n·k).Solution 1 — Prefix Sum Two Rowsclass Solution {private static final int MOD 1_000_000_007;public int kInversePairs(int n, int k) { long[] prev new long[k 1]; prev[0] 1; // n 1 - only 0 inverse pairs for (int i 2; i n; i) { long[] prefix new long[k 1]; prefix[0] prev[0]; for (int j 1; j k; j) { prefix[j] (prefix[j - 1] prev[j]) % MOD; } long[] curr new long[k 1]; for (int j 0; j k; j) { long sum prefix[j]; // sum of prev[0..j] if (j - i 0) { // subtract prev[0..j-i] sum (sum - prefix[j - i] MOD) % MOD; } curr[j] sum; } prev curr; } return (int) prev[k]; }}Solution 2 — Sliding Window (no prefix array)class Solution {private static final int MOD 1_000_000_007;public int kInversePairs(int n, int k) { long[] dp new long[k 1]; dp[0] 1; for (int i 2; i n; i) { long[] next new long[k 1]; long window 0; for (int j 0; j k; j) { window (window dp[j]) % MOD; // add right end if (j - i 0) { window (window - dp[j - i] MOD) % MOD; // drop left end } next[j] window; } dp next; } return (int) dp[k]; }}Walkthrough (n 3, k 1)i dp array (j 0…1) meaning1 [1, 0] [1]2 [1, 1] [1,2], [2,1]3 [1, 2] answer for k 1 is 2ComplexityTime: O(n × k)Space: O(k)Interview NotesMention the naive O(n·k²) version first, then explain the prefix-sum / sliding-window optimization — that transition is usually what the interviewer is looking for.Use long for intermediate values to avoid overflow before taking the modulo, and add MOD before % when subtracting to keep values non-negative.A useful early exit: if k n * (n - 1) / 2, the answer is 0.Want me to add a version that also prints the actual permutations for small n, or a top-down memoized variant for comparison?

关于本文作者

来自尧图内容编辑团队

尧图内容编辑团队 内容团队

尧图内容编辑团队

本文由尧图网络内容编辑团队执笔。团队由资深项目经理、前端工程师与设计师组成,所有内容均来自亲手交付的真实项目,先讲清问题、再给出可落地的解法。尧图深耕北京网站建设十年,服务过京华建材集团、智造科技等各行业客户,把一线经验沉淀为可复用的行业观察。

  • 十年建站经验,覆盖建材、制造、服务、文创等
  • 项目经理把关选题与事实准确性
  • 工程师与设计师联合撰写专业细节
  • 统一编辑规范,保证文风与排版一致
  • 每月复盘转化数据,迭代选题方向

延伸阅读

相关资讯与近期热门内容

深度阅读推荐

建站决策前值得细读的三篇

网站改版的5个关键决策
2024-08-12

网站改版的5个关键决策

什么时候该改版、改到什么程度、如何避免流量掉光,京华建材集团改版复盘给出答案。

获取专属建站方案

看完文章,把您的行业与预算告诉我们,免费获取一份量身定制的官网建设方案与报价。

立即免费咨询