Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 510736c

Browse files
authored
Update 571. Find Median Given Frequency of Numbers.sql
Added New Approach
1 parent 7f0f80f commit 510736c

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

‎hard/571. Find Median Given Frequency of Numbers.sql

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,23 @@ cte2 AS(
1515
SELECT ROUND(AVG(number),2)
1616
FROM cte2
1717
WHERE (t::NUMERIC/2 BETWEEN s AND e) OR (t::NUMERIC/2+1 BETWEEN s AND e);
18+
19+
--------------------------- OR ---------------------------
20+
21+
WITH RECURSIVE cte AS (
22+
SELECT number,frequency,1 AS cnt
23+
FROM numbers_571
24+
UNION ALL
25+
SELECT number,frequency,cnt+1 AS cnt
26+
FROM cte
27+
WHERE cnt < frequency
28+
),
29+
cte2 AS (
30+
SELECT number,
31+
ROW_NUMBER() OVER (ORDER BY number) AS a,
32+
COUNT(*) OVER () c
33+
FROM cte
34+
)
35+
SELECT ROUND(AVG(number),1)
36+
FROM cte2
37+
WHERE a BETWEEN (SELECT CEIL(AVG(c)::NUMERIC/2) FROM cte2) AND (SELECT CEIL((AVG(c)+1::NUMERIC)/2) FROM cte2)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /