diff options
author | Jack Sangdahl <[email protected]> | 2025-03-10 00:36:13 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-03-10 00:36:13 -0600 |
commit | 7c60dee83b436e8e8a667bf9f191beced8de4fc3 (patch) | |
tree | eb7a24ee2067f92245c57bb118e85a1baf15df50 /other/k_inverse_pairs.cpp | |
parent | 5d7cdc215e4ba1ec4cbb24c084cccb5e5e641468 (diff) | |
download | leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.tar.gz leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.zip |
chore: move cpp and rust code to directory
Diffstat (limited to 'other/k_inverse_pairs.cpp')
-rw-r--r-- | other/k_inverse_pairs.cpp | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/other/k_inverse_pairs.cpp b/other/k_inverse_pairs.cpp new file mode 100644 index 0000000..5601d19 --- /dev/null +++ b/other/k_inverse_pairs.cpp @@ -0,0 +1,24 @@ +#include "leetcode.h" + +class Solution { +public: + int kInversePairs(int n, int k) { + vector<int> dp(k + 1, 0); + int mod = 1e9 + 7; + for (int i = 1; i <= n; i++) { + vector<int> tmp(k + 1, 0); + tmp[0] = 1; + for (int j = 1; j <= k; j++) { + long long val = (dp[j] + mod - ((j - i) >= 0 ? dp[j - i] : 0)) % mod; + tmp[j] = (tmp[j - 1] + val) % mod; + } + dp = tmp; + } + return (dp[k] + mod - (k > 0 ? dp[k - 1] : 0)) % mod; + } +}; + +int main() { + Solution obj; + cout << obj.kInversePairs(3, 0); +} |