From fa725d5db2ed704f612f25a889fe5e916e41a3f7 Mon Sep 17 00:00:00 2001 From: kimi Date: Mon, 19 Feb 2024 23:21:25 +0800 Subject: [PATCH] feat: 1195.Fizz-Buzz-Multithreaded --- .../README.md | 79 ++++++++++++++++++- README.md | 35 ++++---- 2 files changed, 94 insertions(+), 20 deletions(-) diff --git a/Leetcode/0238.Product-of-Array-Except-Self/README.md b/Leetcode/0238.Product-of-Array-Except-Self/README.md index 6aadbf90c..2c3445b58 100644 --- a/Leetcode/0238.Product-of-Array-Except-Self/README.md +++ b/Leetcode/0238.Product-of-Array-Except-Self/README.md @@ -10,7 +10,7 @@ description: "0238.Product-of-Array-Except-Self" license: "" images: [] -tags: [LeetCode, Go, Medium, Product of Array Except Self] +tags: [LeetCode, Go, Medium, Product of Array Except Self, Array, Prefix Sum] categories: [LeetCode] featuredImage: "" @@ -61,14 +61,39 @@ seo: # [238. Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/description/) ## 題目 +Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i]. +The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer. + +You must write an algorithm that runs in O(n) time and without using the division operation. + + + +Example 1: + +Input: nums = [1,2,3,4] +Output: [24,12,8,6] +Example 2: + +Input: nums = [-1,1,0,-3,3] +Output: [0,0,9,0,0] + + +Constraints: + +2 <= nums.length <= 105 +-30 <= nums[i] <= 30 +The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer. + + +Follow up: Can you solve the problem in O(1) extra space complexity? (The output array does not count as extra space for space complexity analysis.) ## 題目大意 ## 解題思路 ## Big O -時間複雜 : `` +時間複雜 : `O(n)` 空間複雜 : `` ## 來源 @@ -79,11 +104,59 @@ seo: https://github.com/kimi0230/LeetcodeGolang/blob/master/Leetcode/0238.Product-of-Array-Except-Self/main.go ```go +package productofarrayexceptself + +// 時間複雜 O(), 空間複雜 O() +func productExceptSelf(nums []int) []int { + result, left, right := make([]int, len(nums)), make([]int, len(nums)), make([]int, len(nums)) + + // left 為左側所有的成績 + // 索引為'0' 的元素, 因為左側沒有元素,所以left[0]=1 + left[0] = 1 + for i := 1; i < len(nums); i++ { + left[i] = left[i-1] * nums[i-1] + } + + right[len(nums)-1] = 1 + for i := len(nums) - 2; i >= 0; i-- { + right[i] = right[i+1] * nums[i+1] + } + + for i := 0; i < len(nums); i++ { + result[i] = left[i] * right[i] + } + return result +} + +func productExceptSelf2(nums []int) []int { + result := make([]int, len(nums)) + + result[0] = 1 + for i := 1; i < len(nums); i++ { + result[i] = result[i-1] * nums[i-1] + } + + rightProduct := 1 + for i := len(nums) - 1; i >= 0; i-- { + result[i] = result[i] * rightProduct + rightProduct = rightProduct * nums[i] + } + + return result +} ``` ## Benchmark ```sh - +go test -benchmem -run=none LeetcodeGolang/Leetcode/0238.Product-of-Array-Except-Self -bench=. +goos: darwin +goarch: amd64 +pkg: LeetcodeGolang/Leetcode/0238.Product-of-Array-Except-Self +cpu: Intel(R) Core(TM) i5-8259U CPU @ 2.30GHz +BenchmarkProductExceptSelf-8 12772174 158.4 ns/op 96 B/op 3 allocs/op +BenchmarkProductExceptSelf2-8 32292304 63.74 ns/op 32 B/op 1 allocs/op +PASS +ok LeetcodeGolang/Leetcode/0238.Product-of-Array-Except-Self 4.228s ``` \ No newline at end of file diff --git a/README.md b/README.md index c53cdbc8a..d5cf2fff4 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ https://kimi0230.github.io/LeetcodeGolang/ - [leetcode Content](#leetcode-content) - [Data Structure](#data-structure) - [Array \& String](#array--string) -- [| 0412 | 0412.Fizz Buzz | Go | Easy | O(n) | O(n) | Array, string |](#-0412---------------------------------------------------------------------0412fizz-buzz---------------------------------------------------go---------------------easy--------on-------------on------array-string---------) +- [| 1195 | 1195.Fizz Buzz Multithreaded | Go | Medium | O(n) | | Array, string,Concurrency |](#-1195-----------------------------------------1195fizz-buzz-multithreaded------------------------------go--------------medium------on-------------------array-stringconcurrency-) - [Matrix](#matrix) - [Linked List](#linked-list) - [HashSet \& HashMap](#hashset--hashmap) @@ -58,22 +58,23 @@ https://kimi0230.github.io/LeetcodeGolang/ #### Array & String -| No. | Title | Solution | Difficulty | Time | Space | Topic | -|----------------------------------------------------------------------------------------------------------------|:-----------------------------------------------------------------------------------------------------------------------------------:|:-------------------------------------------------------------------------------------------------------------------------:|------------|---------------|--------|-----------------------| -| [0001](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0001.Two-Sum) | [Two Sum](https://leetcode.com/problems/two-sum/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0001.Two-Sum) | Easy | O(n) | O(n) | Array | -| [0003](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0003.Longest-Substring-Without-Repeating-Characters) | [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0003.Longest-Substring-Without-Repeating-Characters) | Medium | O(n) | O(1) | Array, Sliding Window | -| [0015](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0015.3Sum) | [3 Sum](https://leetcode.com/problems/3sum/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0015.3Sum) | Medium | O(n^2) | O(n) | Array | -| [0027](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0027.Remove-Element) | [Remove Element](https://leetcode.com/problems/remove-element/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0027.Remove-Element) | Easy | O(n) | O(1) | Array | -| [0035](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0035.Search-Insert-Position) | [Search Insert Position](https://leetcode.com/problems/search-insert-position/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0035.Search-Insert-Position) | Easy | O(n), O(logn) | O(1) | Array | -| [0049](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0049.Group-Anagrams) | [Search Insert Position](https://leetcode.com/problems/group-anagrams/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0049.Group-Anagrams) | Medium | O(kn) | O(kn) | Array | -| [0059](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0059.Spiral-Matrix-II) | [Spiral Matrix II](https://leetcode.com/problems/spiral-matrix-ii/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0059.Spiral-Matrix-II) | Medium | O(n) | O(n^2) | Array | -| [0088](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0088.Merge-Sorted-Array) | [Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0088.Merge-Sorted-Array) | Easy | O(n) | O(1) | Array | -| [0217](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0217.Contains-Duplicate) | [0217.Contains Duplicate](https://leetcode.com/problems/contains-duplicate/description/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0217.Contains-Duplicate) | Easy | O(n) | O(n) | Array | -| [0242](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0242.Valid-Anagram) | [0242.Valid Anagram](https://leetcode.com/problems/valid-anagram/description/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0242.Valid-Anagram) | Easy | O(n) | O(n) | Array | -| [0409](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0409.Longest-Palindrome) | [409. Longest Palindrome](https://leetcode.com/problems/longest-palindrome/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0409.Longest-Palindrome) | Easy | O(n) | O(1) | Array | -| [0380](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0380.Insert-Delete-GetRandom-O1) | [0380.Insert Delete GetRandom O(1)](https://leetcode.com/problems/insert-delete-getrandom-o1/description/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0380.Insert-Delete-GetRandom-O1) | Medium | O(1) | O(n) | Array | -| [0381](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0381.Insert-Delete-GetRandom-O1-Duplicates-allowed) | [0381.Insert Delete GetRandom O(1) Duplicates allowed](https://leetcode.com/problems/insert-delete-getrandom-o1-duplicates-allowed) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0381.Insert-Delete-GetRandom-O1-Duplicates-allowed) | Medium | O(1) | O(n) | Array | -| [0412](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0412.Fizz-Buzz) | [0412.Fizz Buzz](https://leetcode.com/problems/fizz-buzz/description/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0412.Fizz-Buzz) | Easy | O(n) | O(n) | Array, string | +| No. | Title | Solution | Difficulty | Time | Space | Topic | +|----------------------------------------------------------------------------------------------------------------|:-----------------------------------------------------------------------------------------------------------------------------------:|:-------------------------------------------------------------------------------------------------------------------------:|------------|---------------|--------|---------------------------| +| [0001](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0001.Two-Sum) | [Two Sum](https://leetcode.com/problems/two-sum/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0001.Two-Sum) | Easy | O(n) | O(n) | Array | +| [0003](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0003.Longest-Substring-Without-Repeating-Characters) | [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0003.Longest-Substring-Without-Repeating-Characters) | Medium | O(n) | O(1) | Array, Sliding Window | +| [0015](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0015.3Sum) | [3 Sum](https://leetcode.com/problems/3sum/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0015.3Sum) | Medium | O(n^2) | O(n) | Array | +| [0027](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0027.Remove-Element) | [Remove Element](https://leetcode.com/problems/remove-element/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0027.Remove-Element) | Easy | O(n) | O(1) | Array | +| [0035](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0035.Search-Insert-Position) | [Search Insert Position](https://leetcode.com/problems/search-insert-position/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0035.Search-Insert-Position) | Easy | O(n), O(logn) | O(1) | Array | +| [0049](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0049.Group-Anagrams) | [Search Insert Position](https://leetcode.com/problems/group-anagrams/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0049.Group-Anagrams) | Medium | O(kn) | O(kn) | Array | +| [0059](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0059.Spiral-Matrix-II) | [Spiral Matrix II](https://leetcode.com/problems/spiral-matrix-ii/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0059.Spiral-Matrix-II) | Medium | O(n) | O(n^2) | Array | +| [0088](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0088.Merge-Sorted-Array) | [Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0088.Merge-Sorted-Array) | Easy | O(n) | O(1) | Array | +| [0217](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0217.Contains-Duplicate) | [0217.Contains Duplicate](https://leetcode.com/problems/contains-duplicate/description/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0217.Contains-Duplicate) | Easy | O(n) | O(n) | Array | +| [0242](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0242.Valid-Anagram) | [0242.Valid Anagram](https://leetcode.com/problems/valid-anagram/description/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0242.Valid-Anagram) | Easy | O(n) | O(n) | Array | +| [0409](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0409.Longest-Palindrome) | [409. Longest Palindrome](https://leetcode.com/problems/longest-palindrome/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0409.Longest-Palindrome) | Easy | O(n) | O(1) | Array | +| [0380](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0380.Insert-Delete-GetRandom-O1) | [0380.Insert Delete GetRandom O(1)](https://leetcode.com/problems/insert-delete-getrandom-o1/description/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0380.Insert-Delete-GetRandom-O1) | Medium | O(1) | O(n) | Array | +| [0381](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0381.Insert-Delete-GetRandom-O1-Duplicates-allowed) | [0381.Insert Delete GetRandom O(1) Duplicates allowed](https://leetcode.com/problems/insert-delete-getrandom-o1-duplicates-allowed) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0381.Insert-Delete-GetRandom-O1-Duplicates-allowed) | Medium | O(1) | O(n) | Array | +| [0412](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0412.Fizz-Buzz) | [0412.Fizz Buzz](https://leetcode.com/problems/fizz-buzz/description/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0412.Fizz-Buzz) | Easy | O(n) | O(n) | Array, string | +| [1195](https://kimi0230.github.io/LeetcodeGolang/Leetcode/1195.Fizz-Buzz-Multithreaded) | [1195.Fizz Buzz Multithreaded](https://leetcode.com/problems/fizz-buzz-multithreaded/description/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/1195.Fizz-Buzz-Multithreaded) | Medium | O(n) | | Array, string,Concurrency | --- #### Matrix