From def001747e6efbe77ff8fcdf7b02e2961aacc374 Mon Sep 17 00:00:00 2001 From: Ming91 Date: Tue, 10 Oct 2023 19:18:17 +0000 Subject: [PATCH] feat: add new solutions to lc problems: No.2894 2896 --- .../README.md | 11 +++++++ .../README_EN.md | 11 +++++++ .../README.md | 30 +++++++++++++++++++ .../README_EN.md | 30 +++++++++++++++++++ 4 files changed, 82 insertions(+) diff --git a/solution/2800-2899/2894.Divisible and Non-divisible Sums Difference/README.md b/solution/2800-2899/2894.Divisible and Non-divisible Sums Difference/README.md index 0ecf75fa512ed..85332abdda30b 100644 --- a/solution/2800-2899/2894.Divisible and Non-divisible Sums Difference/README.md +++ b/solution/2800-2899/2894.Divisible and Non-divisible Sums Difference/README.md @@ -100,6 +100,17 @@ class Solution { } ``` +```java +class Solution { + public int differenceOfSums(int n, int m) { + int sum = n * (n + 1) / 2; + int k = n / m; + int nums2 = k * (k + 1) / 2 * m; + return sum - nums2 * 2; + } +} +``` + ### **C++** ```cpp diff --git a/solution/2800-2899/2894.Divisible and Non-divisible Sums Difference/README_EN.md b/solution/2800-2899/2894.Divisible and Non-divisible Sums Difference/README_EN.md index 71aba2f6af45a..59b689c1e3f9a 100644 --- a/solution/2800-2899/2894.Divisible and Non-divisible Sums Difference/README_EN.md +++ b/solution/2800-2899/2894.Divisible and Non-divisible Sums Difference/README_EN.md @@ -90,6 +90,17 @@ class Solution { } ``` +```java +class Solution { + public int differenceOfSums(int n, int m) { + int sum = n * (n + 1) / 2; + int k = n / m; + int nums2 = k * (k + 1) / 2 * m; + return sum - nums2 * 2; + } +} +``` + ### **C++** ```cpp diff --git a/solution/2800-2899/2896.Apply Operations to Make Two Strings Equal/README.md b/solution/2800-2899/2896.Apply Operations to Make Two Strings Equal/README.md index 36fbc3bcf466b..b8fee302da017 100644 --- a/solution/2800-2899/2896.Apply Operations to Make Two Strings Equal/README.md +++ b/solution/2800-2899/2896.Apply Operations to Make Two Strings Equal/README.md @@ -144,6 +144,36 @@ class Solution { } ``` +```java +class Solution { + public int minOperations(String s1, String s2, int x) { + int n = s1.length(); + int inf = 50_000; + int one = inf, two = inf, last = inf; + int done = 0; + for (int i = 0; i < n; i++) { + if (s1.charAt(i) == s2.charAt(i)) { + one = Math.min(one, last); + last = last + 1; + two = two + 1; + continue; + } + if (done < n) { + one = Math.min(two + 1, done + x); + last = Math.min(two + x, done); + done = two = inf; + continue; + } + done = Math.min(one + x, last + 1); + two = one; + one = last = inf; + continue; + } + return done == inf ? -1 : done; + } +} +``` + ### **C++** ```cpp diff --git a/solution/2800-2899/2896.Apply Operations to Make Two Strings Equal/README_EN.md b/solution/2800-2899/2896.Apply Operations to Make Two Strings Equal/README_EN.md index f2178e0ae9cc1..d7415354f85f1 100644 --- a/solution/2800-2899/2896.Apply Operations to Make Two Strings Equal/README_EN.md +++ b/solution/2800-2899/2896.Apply Operations to Make Two Strings Equal/README_EN.md @@ -134,6 +134,36 @@ class Solution { } ``` +```java +class Solution { + public int minOperations(String s1, String s2, int x) { + int n = s1.length(); + int inf = 50_000; + int one = inf, two = inf, last = inf; + int done = 0; + for (int i = 0; i < n; i++) { + if (s1.charAt(i) == s2.charAt(i)) { + one = Math.min(one, last); + last = last + 1; + two = two + 1; + continue; + } + if (done < n) { + one = Math.min(two + 1, done + x); + last = Math.min(two + x, done); + done = two = inf; + continue; + } + done = Math.min(one + x, last + 1); + two = one; + one = last = inf; + continue; + } + return done == inf ? -1 : done; + } +} +``` + ### **C++** ```cpp