From 0eb627e574bf961c6d7bff1c2d02ea4425ab9d4e Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sat, 21 Sep 2024 12:39:01 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.1968 No.1968.Array With Elements Not Equal to Average of Neighbors --- .../README.md | 57 ++++++++---------- .../README_EN.md | 59 +++++++++---------- .../Solution.cpp | 8 ++- .../Solution.go | 7 +-- .../Solution.py | 2 +- .../Solution.ts | 13 ++++ .../Solution2.go | 13 ---- 7 files changed, 75 insertions(+), 84 deletions(-) create mode 100644 solution/1900-1999/1968.Array With Elements Not Equal to Average of Neighbors/Solution.ts delete mode 100644 solution/1900-1999/1968.Array With Elements Not Equal to Average of Neighbors/Solution2.go diff --git a/solution/1900-1999/1968.Array With Elements Not Equal to Average of Neighbors/README.md b/solution/1900-1999/1968.Array With Elements Not Equal to Average of Neighbors/README.md index 547074165ff9..4cbe043e0a03 100644 --- a/solution/1900-1999/1968.Array With Elements Not Equal to Average of Neighbors/README.md +++ b/solution/1900-1999/1968.Array With Elements Not Equal to Average of Neighbors/README.md @@ -65,6 +65,10 @@ i=3, nums[i] = 2, 两相邻元素平均值为 (6+0) / 2 = 3 ### 方法一:排序 +由于数组中的元素是互不相同的,我们可以先对数组进行排序,然后将数组分成两部分,将前一半的元素放到答案数组中的偶数位置,将后一半的元素放到答案数组中的奇数位置。这样,对于每个元素,它的两个相邻元素都不会等于它的平均值。 + +时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $\textit{nums}$ 的长度。 + #### Python3 @@ -74,7 +78,7 @@ class Solution: def rearrangeArray(self, nums: List[int]) -> List[int]: nums.sort() n = len(nums) - m = (n + 1) >> 1 + m = (n + 1) // 2 ans = [] for i in range(m): ans.append(nums[i]) @@ -109,13 +113,15 @@ class Solution { class Solution { public: vector rearrangeArray(vector& nums) { - sort(nums.begin(), nums.end()); + ranges::sort(nums); vector ans; int n = nums.size(); int m = (n + 1) >> 1; for (int i = 0; i < m; ++i) { ans.push_back(nums[i]); - if (i + m < n) ans.push_back(nums[i + m]); + if (i + m < n) { + ans.push_back(nums[i + m]); + } } return ans; } @@ -125,46 +131,35 @@ public: #### Go ```go -func rearrangeArray(nums []int) []int { +func rearrangeArray(nums []int) (ans []int) { sort.Ints(nums) n := len(nums) m := (n + 1) >> 1 - var ans []int for i := 0; i < m; i++ { ans = append(ans, nums[i]) if i+m < n { ans = append(ans, nums[i+m]) } } - return ans + return } ``` - - - - - - -### 方法二:随机打乱 - - - -#### Go - -```go -func rearrangeArray(nums []int) []int { - rand.Seed(time.Now().UnixNano()) -outer: - for { - rand.Shuffle(len(nums), func(i, j int) { nums[i], nums[j] = nums[j], nums[i] }) - for i := 1; i < len(nums)-1; i++ { - if nums[i]*2 == nums[i-1]+nums[i+1] { - continue outer - } - } - return nums - } +#### TypeScript + +```ts +function rearrangeArray(nums: number[]): number[] { + nums.sort((a, b) => a - b); + const n = nums.length; + const m = (n + 1) >> 1; + const ans: number[] = []; + for (let i = 0; i < m; i++) { + ans.push(nums[i]); + if (i + m < n) { + ans.push(nums[i + m]); + } + } + return ans; } ``` diff --git a/solution/1900-1999/1968.Array With Elements Not Equal to Average of Neighbors/README_EN.md b/solution/1900-1999/1968.Array With Elements Not Equal to Average of Neighbors/README_EN.md index f3d560a2b533..4e59907a586c 100644 --- a/solution/1900-1999/1968.Array With Elements Not Equal to Average of Neighbors/README_EN.md +++ b/solution/1900-1999/1968.Array With Elements Not Equal to Average of Neighbors/README_EN.md @@ -63,7 +63,11 @@ When i=3, nums[i] = 2, and the average of its neighbors is (6+0) / 2 = 3. -### Solution 1 +### Solution 1: Sorting + +Since the elements in the array are distinct, we can first sort the array, then divide the array into two parts. Place the first half of the elements in the even positions of the answer array, and the second half of the elements in the odd positions of the answer array. In this way, for each element, its two adjacent elements will not be equal to its average value. + +The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{nums}$. @@ -74,7 +78,7 @@ class Solution: def rearrangeArray(self, nums: List[int]) -> List[int]: nums.sort() n = len(nums) - m = (n + 1) >> 1 + m = (n + 1) // 2 ans = [] for i in range(m): ans.append(nums[i]) @@ -109,13 +113,15 @@ class Solution { class Solution { public: vector rearrangeArray(vector& nums) { - sort(nums.begin(), nums.end()); + ranges::sort(nums); vector ans; int n = nums.size(); int m = (n + 1) >> 1; for (int i = 0; i < m; ++i) { ans.push_back(nums[i]); - if (i + m < n) ans.push_back(nums[i + m]); + if (i + m < n) { + ans.push_back(nums[i + m]); + } } return ans; } @@ -125,46 +131,35 @@ public: #### Go ```go -func rearrangeArray(nums []int) []int { +func rearrangeArray(nums []int) (ans []int) { sort.Ints(nums) n := len(nums) m := (n + 1) >> 1 - var ans []int for i := 0; i < m; i++ { ans = append(ans, nums[i]) if i+m < n { ans = append(ans, nums[i+m]) } } - return ans + return } ``` - - - - - - -### Solution 2 - - - -#### Go - -```go -func rearrangeArray(nums []int) []int { - rand.Seed(time.Now().UnixNano()) -outer: - for { - rand.Shuffle(len(nums), func(i, j int) { nums[i], nums[j] = nums[j], nums[i] }) - for i := 1; i < len(nums)-1; i++ { - if nums[i]*2 == nums[i-1]+nums[i+1] { - continue outer - } - } - return nums - } +#### TypeScript + +```ts +function rearrangeArray(nums: number[]): number[] { + nums.sort((a, b) => a - b); + const n = nums.length; + const m = (n + 1) >> 1; + const ans: number[] = []; + for (let i = 0; i < m; i++) { + ans.push(nums[i]); + if (i + m < n) { + ans.push(nums[i + m]); + } + } + return ans; } ``` diff --git a/solution/1900-1999/1968.Array With Elements Not Equal to Average of Neighbors/Solution.cpp b/solution/1900-1999/1968.Array With Elements Not Equal to Average of Neighbors/Solution.cpp index cfce7a330831..67dda92cbce5 100644 --- a/solution/1900-1999/1968.Array With Elements Not Equal to Average of Neighbors/Solution.cpp +++ b/solution/1900-1999/1968.Array With Elements Not Equal to Average of Neighbors/Solution.cpp @@ -1,14 +1,16 @@ class Solution { public: vector rearrangeArray(vector& nums) { - sort(nums.begin(), nums.end()); + ranges::sort(nums); vector ans; int n = nums.size(); int m = (n + 1) >> 1; for (int i = 0; i < m; ++i) { ans.push_back(nums[i]); - if (i + m < n) ans.push_back(nums[i + m]); + if (i + m < n) { + ans.push_back(nums[i + m]); + } } return ans; } -}; \ No newline at end of file +}; diff --git a/solution/1900-1999/1968.Array With Elements Not Equal to Average of Neighbors/Solution.go b/solution/1900-1999/1968.Array With Elements Not Equal to Average of Neighbors/Solution.go index d60f45616168..50504e03025d 100644 --- a/solution/1900-1999/1968.Array With Elements Not Equal to Average of Neighbors/Solution.go +++ b/solution/1900-1999/1968.Array With Elements Not Equal to Average of Neighbors/Solution.go @@ -1,13 +1,12 @@ -func rearrangeArray(nums []int) []int { +func rearrangeArray(nums []int) (ans []int) { sort.Ints(nums) n := len(nums) m := (n + 1) >> 1 - var ans []int for i := 0; i < m; i++ { ans = append(ans, nums[i]) if i+m < n { ans = append(ans, nums[i+m]) } } - return ans -} \ No newline at end of file + return +} diff --git a/solution/1900-1999/1968.Array With Elements Not Equal to Average of Neighbors/Solution.py b/solution/1900-1999/1968.Array With Elements Not Equal to Average of Neighbors/Solution.py index 16bdf8c3fe6a..86ec750b7d25 100644 --- a/solution/1900-1999/1968.Array With Elements Not Equal to Average of Neighbors/Solution.py +++ b/solution/1900-1999/1968.Array With Elements Not Equal to Average of Neighbors/Solution.py @@ -2,7 +2,7 @@ class Solution: def rearrangeArray(self, nums: List[int]) -> List[int]: nums.sort() n = len(nums) - m = (n + 1) >> 1 + m = (n + 1) // 2 ans = [] for i in range(m): ans.append(nums[i]) diff --git a/solution/1900-1999/1968.Array With Elements Not Equal to Average of Neighbors/Solution.ts b/solution/1900-1999/1968.Array With Elements Not Equal to Average of Neighbors/Solution.ts new file mode 100644 index 000000000000..6d052569aba2 --- /dev/null +++ b/solution/1900-1999/1968.Array With Elements Not Equal to Average of Neighbors/Solution.ts @@ -0,0 +1,13 @@ +function rearrangeArray(nums: number[]): number[] { + nums.sort((a, b) => a - b); + const n = nums.length; + const m = (n + 1) >> 1; + const ans: number[] = []; + for (let i = 0; i < m; i++) { + ans.push(nums[i]); + if (i + m < n) { + ans.push(nums[i + m]); + } + } + return ans; +} diff --git a/solution/1900-1999/1968.Array With Elements Not Equal to Average of Neighbors/Solution2.go b/solution/1900-1999/1968.Array With Elements Not Equal to Average of Neighbors/Solution2.go deleted file mode 100644 index b60065c95963..000000000000 --- a/solution/1900-1999/1968.Array With Elements Not Equal to Average of Neighbors/Solution2.go +++ /dev/null @@ -1,13 +0,0 @@ -func rearrangeArray(nums []int) []int { - rand.Seed(time.Now().UnixNano()) -outer: - for { - rand.Shuffle(len(nums), func(i, j int) { nums[i], nums[j] = nums[j], nums[i] }) - for i := 1; i < len(nums)-1; i++ { - if nums[i]*2 == nums[i-1]+nums[i+1] { - continue outer - } - } - return nums - } -} \ No newline at end of file