news 2026/10/18 7:21:47

千问 LeetCode 629. K Inverse Pairs Array Java Implement

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
千问 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.

Problem

Given 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.

Examples

  • n = 3, k = 0 → 1 (only [1,2,3])
  • n = 3, k = 1 → 2 ([1,3,2], [2,1,3])
  • n = 3, k = 3 → 0

Core Recurrence

Insert 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 Rows

class 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) meaning
1 [1, 0] [1]
2 [1, 1] [1,2], [2,1]
3 [1, 2] answer for k = 1 is 2

Complexity

  • Time: O(n × k)
  • Space: O(k)

Interview Notes

  • Mention 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?

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/10/18 7:21:08

Material Design实战指南:从设计原理到前端代码落地

这几年做界面设计&#xff0c;但凡产品、设计师、前端聚在一起聊“UI应该长什么样”&#xff0c;Material Design 这个名字就绕不开。它从2014年面世到现在&#xff0c;已经从一套“视觉风格”悄悄长成了一整套设计语言&#xff0c;覆盖了移动端、网页端、桌面端甚至车载和电视…

作者头像 李华
网站建设 2026/10/18 7:19:31

从AI答错到语义模型:企业问数项目中的业务口径梳理实战

先说一个我自己的教训。上半年做一个企业问数项目时&#xff0c;我一度天真地以为&#xff0c;只要大模型能力够强、知识库里的文档够详细&#xff0c;AI就能自动听懂业务。上线第一周&#xff0c;销售负责人问了一句“本季度华东区净利率为什么掉得这么厉害”&#xff0c;系统…

作者头像 李华
网站建设 2026/10/18 7:18:45

Ubisoft La Forge动画数据集:工业级动作泛化与跨角色迁移实战指南

简介&#xff1a;Ubisoft La Forge动画数据集&#xff08;LAFAN1&#xff09;是面向计算机图形学、动作捕捉与AI驱动动画研究者的高质量开源资源&#xff0c;适用于动作合成、运动插值、深度学习建模等方向的算法验证与实验复现。资源包共19个文件&#xff0c;含6个Python脚本&…

作者头像 李华
网站建设 2026/10/18 7:17:05

企业级AI Agent多语言翻译与本地化模块设计

1. 需求拆解&#xff1a;先认清"多语言翻译"在Agent里到底解决什么问题在企业级AI Agent的落地项目里&#xff0c;有一个经常被低估、但上线后被吐槽最多的模块——多语言翻译与本地化。很多团队一开始觉得这事儿简单&#xff1a;调个翻译API&#xff0c;把用户输入翻…

作者头像 李华
网站建设 2026/10/18 7:16:31

扫码点餐小程序多门店源码跑通指南

简介&#xff1a;这是一套基于Java后端与uniapp&#xff08;Vue3&#xff09;前端实现的在线点餐小程序完整源码&#xff0c;面向计算机、电子信息等专业学生及初级全栈开发者&#xff0c;适用于课程设计、期末大作业或毕业设计参考。系统支持扫码点餐、外卖配送与到店自取双模…

作者头像 李华
网站建设 2026/10/18 7:16:04

.NET桌面应用自动更新解决方案:从ClickOnce到自研更新器全解析

分享几种 .NET 桌面应用程序自动更新解决方案做桌面端开发的朋友应该都有过这种经历&#xff1a;产品好不容易发布出去&#xff0c;第二天就有用户反馈“功能怎么还是老的”&#xff0c;一查发现客户端根本不会自己更新&#xff0c;要么让用户手动下载安装包&#xff0c;要么远…

作者头像 李华