From 55baeaae23d04bbb72fa197f8979cb344b3c9113 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Sun, 27 Oct 2024 17:22:11 +0800 Subject: [PATCH] Add solution and test-cases for problem 1006 --- .../1001-1100/1006.Clumsy-Factorial/README.md | 35 +++++++++++-------- .../1006.Clumsy-Factorial/Solution.go | 28 +++++++++++++-- .../1006.Clumsy-Factorial/Solution_test.go | 13 ++++--- 3 files changed, 53 insertions(+), 23 deletions(-) diff --git a/leetcode/1001-1100/1006.Clumsy-Factorial/README.md b/leetcode/1001-1100/1006.Clumsy-Factorial/README.md index c5ffec5b0..677f80fed 100644 --- a/leetcode/1001-1100/1006.Clumsy-Factorial/README.md +++ b/leetcode/1001-1100/1006.Clumsy-Factorial/README.md @@ -1,28 +1,35 @@ # [1006.Clumsy Factorial][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +The **factorial** of a positive integer `n` is the product of all positive integers less than or equal to `n`. + +- For example, `factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1`. + +We make a **clumsy factorial** using the integers in decreasing order by swapping out the multiply operations for a fixed rotation of operations with multiply `'*'`, divide `'/'`, add `'+'`, and subtract `'-'` in this order. + +- For example, `clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1`. + +However, these operations are still applied using the usual order of operations of arithmetic. We do all multiplication and division steps before any addition or subtraction steps, and multiplication and division steps are processed left to right. + +Additionally, the division that we use is floor division such that `10 * 9 / 8 = 90 / 8 = 11`. + +Given an integer `n`, return the clumsy factorial of `n`. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: n = 4 +Output: 7 +Explanation: 7 = 4 * 3 / 2 + 1 ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Clumsy Factorial -```go ``` - +Input: n = 10 +Output: 12 +Explanation: 12 = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1 +``` ## 结语 diff --git a/leetcode/1001-1100/1006.Clumsy-Factorial/Solution.go b/leetcode/1001-1100/1006.Clumsy-Factorial/Solution.go index d115ccf5e..60e4778ce 100644 --- a/leetcode/1001-1100/1006.Clumsy-Factorial/Solution.go +++ b/leetcode/1001-1100/1006.Clumsy-Factorial/Solution.go @@ -1,5 +1,29 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(n int) int { + ans := 0 + first := true + for ; n > 0; n -= 4 { + cur := n + if n-1 >= 1 { + cur *= (n - 1) + } + if n-2 >= 1 { + cur /= (n - 2) + } + if n-3 >= 1 { + if first { + cur += n - 3 + } else { + cur -= n - 3 + } + } + if first { + ans = cur + first = false + continue + } + ans -= cur + } + return ans } diff --git a/leetcode/1001-1100/1006.Clumsy-Factorial/Solution_test.go b/leetcode/1001-1100/1006.Clumsy-Factorial/Solution_test.go index 14ff50eb4..995ed26c1 100644 --- a/leetcode/1001-1100/1006.Clumsy-Factorial/Solution_test.go +++ b/leetcode/1001-1100/1006.Clumsy-Factorial/Solution_test.go @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", 4, 7}, + {"TestCase2", 10, 12}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }