forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to lc problem: No.1685. Sum of Absolute Differences
in a Sorted Array
- Loading branch information
Showing
6 changed files
with
182 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
solution/1600-1699/1685.Sum of Absolute Differences in a Sorted Array/Solution.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class Solution { | ||
public: | ||
vector<int> getSumAbsoluteDifferences(vector<int>& nums) { | ||
int n = nums.size(); | ||
vector<int> presum(n + 1); | ||
for (int i = 0; i < n; ++i) { | ||
presum[i + 1] = presum[i] + nums[i]; | ||
} | ||
vector<int> res; | ||
for (int i = 0; i < n; ++i) { | ||
int t = nums[i] * i - presum[i] + presum[n] - presum[i + 1] - nums[i] * (n - i - 1); | ||
res.push_back(t); | ||
} | ||
return res; | ||
} | ||
}; |
13 changes: 13 additions & 0 deletions
13
solution/1600-1699/1685.Sum of Absolute Differences in a Sorted Array/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
func getSumAbsoluteDifferences(nums []int) []int { | ||
n := len(nums) | ||
presum := make([]int, n+1) | ||
for i := 0; i < n; i++ { | ||
presum[i+1] = presum[i] + nums[i] | ||
} | ||
var res []int | ||
for i := 0; i < n; i++ { | ||
t := nums[i]*i - presum[i] + presum[n] - presum[i+1] - nums[i]*(n-i-1) | ||
res = append(res, t) | ||
} | ||
return res | ||
} |
14 changes: 14 additions & 0 deletions
14
solution/1600-1699/1685.Sum of Absolute Differences in a Sorted Array/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class Solution { | ||
public int[] getSumAbsoluteDifferences(int[] nums) { | ||
int n = nums.length; | ||
int[] presum = new int[n + 1]; | ||
for (int i = 0; i < n; ++i) { | ||
presum[i + 1] = presum[i] + nums[i]; | ||
} | ||
int[] res = new int[n]; | ||
for (int i = 0; i < n; ++i) { | ||
res[i] = nums[i] * i - presum[i] + presum[n] - presum[i + 1] - nums[i] * (n - i - 1); | ||
} | ||
return res; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
solution/1600-1699/1685.Sum of Absolute Differences in a Sorted Array/Solution.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class Solution: | ||
def getSumAbsoluteDifferences(self, nums: List[int]) -> List[int]: | ||
n = len(nums) | ||
presum = [0] * (n + 1) | ||
for i in range(n): | ||
presum[i + 1] = presum[i] + nums[i] | ||
res = [] | ||
for i, num in enumerate(nums): | ||
t = num * i - presum[i] + presum[n] - presum[i + 1] - num * (n - i - 1) | ||
res.append(t) | ||
return res |