Skip to content

Commit

Permalink
add 1093
Browse files Browse the repository at this point in the history
  • Loading branch information
luliyucoordinate committed Jun 23, 2019
1 parent 81ddf02 commit f1412e0
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -422,4 +422,5 @@ LeetCode
|1087|[Brace Expansion](https://leetcode.com/contest/biweekly-contest-2/problems/brace-expansion/) | c | [c++](./src/1087-Brace-Expansion/1087.cpp) |[python](./src/1087-Brace-Expansion/1087.py)|||Medium|
|1089|[Duplicate Zeros](https://leetcode.com/problems/duplicate-zeros/) | c | [c++](./src/1089-Duplicate-Zeros/1089.cpp) |[python](./src/1089-Duplicate-Zeros/1089.py)|||Easy|
|1090|[Largest Values From Labels](https://leetcode.com/problems/largest-values-from-labels/) | c | [c++](./src/1090-Largest-Values-From-Labels/1090.cpp) |[python](./src/1090-Largest-Values-From-Labels/1090.py)|||Medium|
|1091|[Shortest Path in Binary Matrix](https://leetcode.com/problems/shortest-path-in-binary-matrix/) | c | [c++](./src/1091-Shortest-Path-in-Binary-Matrix/1091.cpp) |[python](./src/1091-Shortest-Path-in-Binary-Matrix/1091.py)|||Medium|
|1091|[Shortest Path in Binary Matrix](https://leetcode.com/problems/shortest-path-in-binary-matrix/) | c | [c++](./src/1091-Shortest-Path-in-Binary-Matrix/1091.cpp) |[python](./src/1091-Shortest-Path-in-Binary-Matrix/1091.py)|||Medium|
|1093|[Statistics from a Large Sample](https://leetcode.com/problems/statistics-from-a-large-sample/) | c | [c++](./src/1093-Statistics-from-a-Large-Sample/1093.cpp) |[python](./src/1093-Statistics-from-a-Large-Sample/1093.py)|[go](./src/1093-Statistics-from-a-Large-Sample/1093.go)||Medium|
23 changes: 23 additions & 0 deletions src/1093-Statistics-from-a-Large-Sample/1093.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution
{
public:
vector<double> sampleStats(vector<int>& count)
{
vector<double> res = { 255, 0, 0, 0, 0 };
int sum_all = accumulate(begin(count), end(count), 0), m_cnt = 0;
int l = sum_all / 2, r = l + (sum_all & 1 ? 0 : 1);
for (int i = 0, cnt = 0; i < count.size(); cnt += count[i++])
{
res[0] = count[i] ? min((int)res[0], i) : res[0];
res[1] = count[i] ? i : res[1];
res[2] += (double)i * count[i] / sum_all;

if (cnt < l && cnt + count[i] >= l) res[3] += (double)i / 2;
if (cnt < r && cnt + count[i] >= r) res[3] += (double)i / 2;

res[4] = m_cnt < count[i] ? i : res[4];
m_cnt = max(m_cnt, count[i]);
}
return res;
}
};
37 changes: 37 additions & 0 deletions src/1093-Statistics-from-a-Large-Sample/1093.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
func sampleStats(count []int) []float64 {
res := make([]float64, 5)
res[0] = 255
sum_all := 0
for _, val := range count {
sum_all += val
}

l, r := sum_all/2, sum_all/2
if (sum_all % 2) == 0 {
r++
}

pre, lc := 0, 0
for i, val := range count {
if val > 0 {
if i < int(res[0]) {
res[0] = float64(i)
}
res[1] = float64(i)
}
res[2] += float64(i * val)/float64(sum_all)
if pre < l && pre + val >= l {
res[3] += float64(i)/2.0
}
if pre < r && pre + val >= r {
res[3] += float64(i)/2.0
}
pre += val

if val > lc {
lc = val
res[4] = float64(i)
}
}
return res
}
25 changes: 25 additions & 0 deletions src/1093-Statistics-from-a-Large-Sample/1093.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution:
def sampleStats(self, count: List[int]) -> List[float]:
res = [255, 0, 0, 0, 0]
sum_all = sum(count)
l, r = sum_all//2, sum_all//2
if not (sum_all & 1):
r += 1

pre, lc = 0, 0
for i, val in enumerate(count):
if val:
res[0] = min(res[0], i)
res[1] = i
res[2] += i * val/sum_all

if pre < l and pre + val >= l:
res[3] += i/2
if pre < r and pre + val >= r:
res[3] += i/2
pre += val

if val > lc:
lc = val
res[4] = i
return [float(i) for i in res]

0 comments on commit f1412e0

Please sign in to comment.