From ebfb50a87e1f8ba19b828df1759f873f83510d20 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Tue, 22 Oct 2024 09:17:14 +0800 Subject: [PATCH] Add solution and test-cases for problem 2894 --- .../README.md | 51 +++++++++++++++++++ .../Solution.go | 13 +++++ .../Solution_test.go | 39 ++++++++++++++ 3 files changed, 103 insertions(+) create mode 100644 leetcode/2801-2900/2894.Divisible-And-Non-divisible-Sums-Difference/README.md create mode 100755 leetcode/2801-2900/2894.Divisible-And-Non-divisible-Sums-Difference/Solution.go create mode 100755 leetcode/2801-2900/2894.Divisible-And-Non-divisible-Sums-Difference/Solution_test.go diff --git a/leetcode/2801-2900/2894.Divisible-And-Non-divisible-Sums-Difference/README.md b/leetcode/2801-2900/2894.Divisible-And-Non-divisible-Sums-Difference/README.md new file mode 100644 index 00000000..b9b9396f --- /dev/null +++ b/leetcode/2801-2900/2894.Divisible-And-Non-divisible-Sums-Difference/README.md @@ -0,0 +1,51 @@ +# [2894.Divisible and Non-divisible Sums Difference][title] + +## Description +You are given positive integers `n` and `m`. + +Define two integers as follows: + +- `num1`: The sum of all integers in the range `[1, n]` (both **inclusive**) that are **not divisible** by `m`. +- `num2`: The sum of all integers in the range `[1, n]` (both **inclusive**) that are **divisible** by `m`. + +Return the integer `num1 - num2`. + +**Example 1:** + +``` +Input: n = 10, m = 3 +Output: 19 +Explanation: In the given example: +- Integers in the range [1, 10] that are not divisible by 3 are [1,2,4,5,7,8,10], num1 is the sum of those integers = 37. +- Integers in the range [1, 10] that are divisible by 3 are [3,6,9], num2 is the sum of those integers = 18. +We return 37 - 18 = 19 as the answer. +``` + +**Example 2:** + +``` +Input: n = 5, m = 6 +Output: 15 +Explanation: In the given example: +- Integers in the range [1, 5] that are not divisible by 6 are [1,2,3,4,5], num1 is the sum of those integers = 15. +- Integers in the range [1, 5] that are divisible by 6 are [], num2 is the sum of those integers = 0. +We return 15 - 0 = 15 as the answer. +``` + +**Example 3:** + +``` +Input: n = 5, m = 1 +Output: -15 +Explanation: In the given example: +- Integers in the range [1, 5] that are not divisible by 1 are [], num1 is the sum of those integers = 0. +- Integers in the range [1, 5] that are divisible by 1 are [1,2,3,4,5], num2 is the sum of those integers = 15. +We return 0 - 15 = -15 as the answer. +``` + +## 结语 + +如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] + +[title]: https://leetcode.com/problems/divisible-and-non-divisible-sums-difference +[me]: https://github.com/kylesliu/awesome-golang-algorithm diff --git a/leetcode/2801-2900/2894.Divisible-And-Non-divisible-Sums-Difference/Solution.go b/leetcode/2801-2900/2894.Divisible-And-Non-divisible-Sums-Difference/Solution.go new file mode 100755 index 00000000..3bd7b26f --- /dev/null +++ b/leetcode/2801-2900/2894.Divisible-And-Non-divisible-Sums-Difference/Solution.go @@ -0,0 +1,13 @@ +package Solution + +func Solution(n int, m int) int { + sum := 0 + for i := 1; i <= n; i++ { + if i%m != 0 { + sum += i + continue + } + sum -= i + } + return sum +} diff --git a/leetcode/2801-2900/2894.Divisible-And-Non-divisible-Sums-Difference/Solution_test.go b/leetcode/2801-2900/2894.Divisible-And-Non-divisible-Sums-Difference/Solution_test.go new file mode 100755 index 00000000..f80ca734 --- /dev/null +++ b/leetcode/2801-2900/2894.Divisible-And-Non-divisible-Sums-Difference/Solution_test.go @@ -0,0 +1,39 @@ +package Solution + +import ( + "reflect" + "strconv" + "testing" +) + +func TestSolution(t *testing.T) { + // 测试用例 + cases := []struct { + name string + n, m int + expect int + }{ + {"TestCase1", 10, 3, 19}, + {"TestCase2", 5, 6, 15}, + {"TestCase3", 5, 1, -15}, + } + + // 开始测试 + for i, c := range cases { + t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { + got := Solution(c.n, c.m) + if !reflect.DeepEqual(got, c.expect) { + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.n, c.m) + } + }) + } +} + +// 压力测试 +func BenchmarkSolution(b *testing.B) { +} + +// 使用案列 +func ExampleSolution() { +}