Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

6-g0rnn #23

Merged
merged 3 commits into from
Nov 23, 2024
Merged

6-g0rnn #23

merged 3 commits into from
Nov 23, 2024

Conversation

g0rnn
Copy link
Collaborator

@g0rnn g0rnn commented Nov 6, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

ํ†ต๊ณ„ํ•™

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

1h

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

์ €๋ฒˆ๋ถ€ํ„ฐ ๊ตฌํ˜„์ด ์•ฝํ•œ ๊ฒƒ ๊ฐ™์•„. ๊ตฌํ˜„์„ ์—ฐ์Šตํ•ด๋ณด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ„๋‹จํ•ด์„œ 10๋ถ„๋งŒ์— ํ’€๊ณ  ๋‹ค๋ฅธ ๋ฌธ์ œ ์˜ฌ๋ฆฌ๋ ค๊ณ  ํ–ˆ๋Š”๋ฐ 1์‹œ๊ฐ„์ด ๋„˜์–ด๊ฐ€๊ฒŒ ๋˜์–ด ๊ทธ๋ƒฅ ์˜ฌ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค.

ํŒŒ์ด์ฌ์—์„œ๋Š” ์–ผ๋งˆ๋‚˜ ์–ด๋ ค์šธ์ง€ ๋ชจ๋ฅด๊ฒ ์ง€๋งŒ.. cpp์—์„œ๋Š” ๊ท€์ฐฎ์€๊ฒŒ ๋ช‡๊ฐ€์ง€ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

์ €๋Š” ์ตœ๋นˆ๊ฐ’์„ ๊ตฌํ•˜๋Š”๊ฑฐ์—์„œ ์–ด๋ ค์› ์Šต๋‹ˆ๋‹ค. ๋ˆ„๊ตฌ๋Š” ๊ทธ๋ƒฅ 8000ํฌ๊ธฐ์˜ ๋ฐฐ์—ด์„ ๋งŒ๋“œ๋Š”๋ฐ, ์ €๋Š” ๊ทธ๋Ÿฐ ์Šคํƒ€์ผ์ด ์•„๋‹ˆ๋ผ์„œ ํ•จ์ˆ˜๋กœ ๊ตฌํ˜„ํ–ˆ๋Š”๋ฐ ๊ทธ๊ฑฐ๋•Œ๋ฌธ์— ์‹œ๊ฐ„์„ ์ข€ ์žก์•„๋จน์—ˆ์Šต๋‹ˆ๋‹ค...ใ…œ

๋‚˜๋จธ์ง€๋Š” ์‰ฌ์šฐ๋‹ˆ ์ตœ๋นˆ๊ฐ’์„ ํ•จ์ˆ˜๋กœ ๋งŒ๋“ค์–ด ๊ตฌํ˜„ํ•˜๋Š” ์ˆ˜๋„์ฝ”๋“œ๋งŒ ์†Œ๊ฐœํ• ๊ฒŒ์š”

0. ์ •๋ ฌ๋˜์–ด ์žˆ๋Š” ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•œ๋‹ค.
1. i๋ฒˆ์งธ์™€ i+1๋ฒˆ์งธ๊ฐ€ ๊ฐ™์€์ง€ ํ™•์ธํ•œ๋‹ค.
2. ๊ฐ™์œผ๋ฉด ๋นˆ๋„์ˆ˜๋ฅผ ์˜ฌ๋ฆฐ๋‹ค.
3. ๋‹ค๋ฅด๋ฉด ์ฐพ์€ ์ตœ๋นˆ๊ฐ’์„ ์—…๋ฐ์ดํŠธํ•˜๊ฑฐ๋‚˜ ๋„˜์–ด๊ฐ„๋‹ค.
  3-1. ์ด์ „ ๋นˆ๋„์ˆ˜ ๋ณด๋‹ค ๋” ๋งŽ์€ ๋นˆ๋„์ˆ˜๋ฅผ ์ฐพ์œผ๋ฉด ์ตœ๋นˆ๊ฐ’์„ ์—…๋ฐ์ดํŠธํ•œ๋‹ค.
    -> ํ•ญ์ƒ ์ œ์ผ ์ž‘์€ ์ตœ๋นˆ๊ฐ’์„ ์œ ์ง€ํ•œ๋‹ค.
  3-2. ์ฒ˜์Œ์œผ๋กœ ์ค‘๋ณต๋˜๋Š” ๋นˆ๋„๊ฐ’์„ ์ฐพ์œผ๋ฉด ๋‘๋ฒˆ์งธ๋กœ ์ž‘์€ ์ตœ๋นˆ๊ฐ’์„ ์—…๋ฐ์ดํŠธํ•œ๋‹ค. 

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

cpp์—์„œ๋Š” ๋ถ€๋™ ์†Œ์ˆ˜์ ์„ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— -0์ด ์กด์žฌํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ round๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ์ฃผ์˜ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

Copy link
Collaborator

@kangrae-jo kangrae-jo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ €๋„ ์ž ์ž๊ธฐ ์ „์— ๋นจ๋ฆฌ ๋ฆฌ๋ทฐํ•˜๊ณ  ์ž์•ผ๊ฒ ๋‹ค๋Š” ์ƒ๊ฐ์ด์—ˆ๋Š”๋ฐ์š”....
์ตœ๋นˆ๊ฐ’์ด ๊ฐ€์ง€๋ง๋ผ๊ณ  ํ•˜๋„ค์š”...

์ €๋„ ์ถ”๊ฐ€ ๋ฐฐ์—ด์„ ์•ˆ์“ฐ๊ณ  ์‹ถ์—ˆ์ง€๋งŒ ๊ฒฐ๊ตญ ์ผ์Šต๋‹ˆ๋‹ค.

์‚ด์ง ๋จธ๋ฆฌ๋ฅผ ๊ตด๋ ค์„œ {count, index} ๋กœ ์ €์žฅ์„ ํ–ˆ์Šต๋‹ˆ๋‹ค.
์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋น„๊ต๋Š” count๋กœ, ๋‚˜์ค‘์— return๊ฐ’์€ index๋กœ ํ•ด์„œ ๋‹ต์„ ์‰ฝ๊ฒŒ ์ฐพ์„ ์ˆ˜ ์žˆ์–ด์š”.

๊ทธ๋ฆฌ๊ณ  ์‚ด์ง ์–ด๋ ค์›€์ด ์žˆ์—ˆ๋˜๊ฑด compareTo๋ฅผ ์ •์˜ํ•˜๋Š” ๋ถ€๋ถ„์ธ๋ฐ์š”.
๋กœ์ง์€ ๋จธ๋ฆฌ์— ์žˆ์—ˆ์ง€๋งŒ ํ‰์†Œ์— ์ž˜ ์จ๋ณด์ง€ ์•Š์•„์„œ ์ง€ํ”ผํ‹ฐ ์ฐฌ์Šค ์ผ์Šต๋‹ˆ๋‹ค.
๋‹ค์Œ์—๋Š” ๊ฒ€์ƒ‰ ์—†์ด ์จ๋จน์„ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๋„ค์š”.

๋˜ ์Œ์ˆ˜์—์„œ ๋ฐ˜์˜ฌ๋ฆผํ•˜๋Š” ์ถ”๊ฐ€์ ์ธ ๋กœ์ง์ด ํ•„์š”ํ•  ์ค„ ์•Œ์•˜๋Š”๋ฐ ๊ทธ๋ƒฅ round ํ•˜๋ฉด ๋˜๋„ค์š”.

๊ณ ์ƒํ•˜์…จ์Šต๋‹ˆ๋‹ค.

CPP CODE
#include <algorithm>
#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

int N;

bool compareTo(pair<int, int> a, pair<int, int> b) {
    if (a.first == b.first) return a.second < b.second;
    return a.first > b.first;
}

int solution1(vector<int> s) {
    int sum = 0;
    for (int i : s) sum += i;

    float average = (float) sum / N;
    return round(average); 
}

int solution2(vector<int> s) {
    return s[N / 2];
}

int solution3(vector<int> s) {
    vector<pair<int, int>> temp(8001, {0, 0});
    for (int i : s) {
        temp[i + 4000].first++;
        temp[i + 4000].second = i;
    }

    sort(temp.begin(), temp.end(), compareTo);

    if (temp[0].first == temp[1].first) return temp[1].second;
    return temp[0].second;
}

int solution4(vector<int> s) {
    return s[N - 1] - s[0];
}

int main() {
    cin >> N;
    vector<int> s(N);
    for (int i = 0; i < N; i++) cin >> s[i];
    
    sort(s.begin(), s.end());
    cout << solution1(s) << endl;
    cout << solution2(s) << endl;
    cout << solution3(s) << endl;
    cout << solution4(s) << endl;

    return 0;
}

Comment on lines +27 to +33
if (arr[i] == arr[i + 1])
{
tmp++;
}
else
{
tmp += 1;
Copy link
Collaborator

@kangrae-jo kangrae-jo Nov 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ฒ˜์Œ์— tmp๋ฅผ arr[0]๋กœ ์ดˆ๊ธฐํ™” ํ•˜๋ฉด, for๋ฌธ์„ 1๋ถ€ํ„ฐ ๋Œ๋ฆด ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์š”.
๋”ฐ๋ผ์„œ arr[i] == arr[i-1]๋กœ ๋น„๊ตํ•  ์ˆ˜ ์žˆ๊ณ , else์ธ ์ƒํ™ฉ์—์„œ tmp+=1 ์„ ์•ˆํ•ด๋„ ๋์„ ๊ฒƒ ๊ฐ™์•„์š”.
(์ •ํ™•ํ•œ๊ฑด ๋’ค์˜ ๋กœ์ง๋„ ์ดํ•ดํ•ด์•ผ ํ•˜์ง€๋งŒ.. ์กฐ๊ธˆ ํŒจ์Šคํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค ใ…Žใ…Ž)
if์™€ else๋ชจ๋‘ tmp๋ฅผ ์ฆ๊ฐ€์‹œํ‚ค๋‹ˆ๊นŒ ์‚ด~์ง ๋” ๋ณต์žกํ•ด์ง„๋“ฏ ํ•ฉ๋‹ˆ๋‹ค.

๊ตฌํ˜„ ์™„์ „ ๋นก์„ธ๋„ค์š”...
์ €๋„ ๋ฐฐ์—ด ๋งŒ๋“œ๋Š”๊ฑด ์ œ ํƒ€์ž…์ด ์•„๋‹ˆ์–ด์„œ ๊ท ํ˜ธ๋‹˜์ฒ˜๋Ÿผ ์‹œ๋„ํ•ด๋ดค๋Š”๋ฐ,
์ž๊พธ ๊ณ ๋ คํ•ด์•ผํ•  ์‚ฌํ•ญ๋“ค์ด ์ƒ๊ธฐ๊ธธ๋ž˜ ํฌ๊ธฐํ–ˆ์Šต๋‹ˆ๋‹ค.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

float๊ณผ double ๋ชจ๋‘ ๋ถ€๋™์†Œ์ˆ˜์ ์œผ๋ฅด ์‚ฌ์šฉํ•˜์—ฌ -0์ด ์กด์žฌํ•˜๊ณ  ์ฝ˜์†”์— 0์ด ์•„๋‹ˆ๋ผ -0์œผ๋กœ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.
ํ•˜์ง€๋งŒ solution1์˜ ๋ฐ˜ํ™˜ ๊ฐ’์ด intํ˜•์ด์–ด์„œ ์ž๋™ ํ˜•๋ณ€ํ™˜์œผ๋กœ ์ธํ•ด 0์œผ๋กœ ๋ณ€ํ™˜๋˜์–ด ์ถœ๋ ฅ๋œ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. :)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์—‡ ๊ทธ๋ ‡๊ตฐ์š”. -0์ด ์žˆ๋Š” ๊ฑธ ๋ชจ๋ฅด๊ณ  ์žˆ์—ˆ๋„ค์š”.
๊ทธ๋ฆฌ๊ณ  ํ•จ์ˆ˜ ๋ฐ˜ํ™˜ ํƒ€์ž…๋„ ๋ฌด์‹œํ•˜๊ณ  ์žˆ์—ˆ๋„ค์š”. ..๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค~

Copy link
Collaborator

@wnsmir wnsmir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ํŒŒ์ด์ฌ์—์„œ๋Š” collections.Counter๋ฅผ ํ™œ์šฉํ•ด ์ตœ๋นˆ๊ฐ’์„ ๊ตฌํ•˜๋Š” ๊ฒƒ์ด ๋งค์šฐ ํŽธ๋ฆฌํ–ˆ์Šต๋‹ˆ๋‹ค. C++์—์„œ 8000 ํฌ๊ธฐ์˜ ๋ฐฐ์—ด์„ ์ง์ ‘ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๊ณ ์ •๋œ ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜๋Š” ๋Œ€์‹ , ํŒŒ์ด์ฌ์€ ๋ฐ์ดํ„ฐ์˜ ๋ฒ”์œ„๋ฅผ ๋ชฐ๋ผ๋„ ๋™์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ์žฅ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ €๋Š” ์ด ์ ์„ ํ™œ์šฉํ•ด ํšจ์œจ์ ์œผ๋กœ ์ตœ๋นˆ๊ฐ’์„ ๊ณ„์‚ฐํ–ˆ์Šต๋‹ˆ๋‹ค.

from collections import Counter
import sys

input = sys.stdin.read
data = input().split()
N = int(data[0])
numbers = list(map(int, data[1:]))

# 1. ์‚ฐ์ˆ ํ‰๊ท 
mean = round(sum(numbers) / N)

# 2. ์ค‘์•™๊ฐ’
numbers.sort()
median = numbers[N // 2]

# 3. ์ตœ๋นˆ๊ฐ’
count = Counter(numbers)
most_common = count.most_common()
most_common.sort(key=lambda x: (-x[1], x[0]))  # ๋นˆ๋„์ˆœ ์ •๋ ฌ ํ›„ ๊ฐ’ ๊ธฐ์ค€ ์ •๋ ฌ

if len(most_common) > 1 and most_common[0][1] == most_common[1][1]:
    mode = most_common[1][0]  # ๋‘ ๋ฒˆ์งธ๋กœ ์ž‘์€ ์ตœ๋นˆ๊ฐ’
else:
    mode = most_common[0][0]  # ์ฒซ ๋ฒˆ์งธ ์ตœ๋นˆ๊ฐ’

# 4. ๋ฒ”์œ„
range_value = numbers[-1] - numbers[0]

# ๊ฒฐ๊ณผ ์ถœ๋ ฅ
print(mean)
print(median)
print(mode)
print(range_value)
  1. Counter์˜ ์œ ์šฉ์„ฑ
    ์ตœ๋นˆ๊ฐ’ ๊ณ„์‚ฐ์—์„œ collections.Counter๊ฐ€ ๋งค์šฐ ์œ ์šฉํ•˜๋‹ค๊ณ  ๋Š๊ผˆ์Šต๋‹ˆ๋‹ค. C++์ฒ˜๋Ÿผ ์ง์ ‘ ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•˜๊ฑฐ๋‚˜ ๋ฐ˜๋ณต๋ฌธ์„ ์ด์šฉํ•ด ๋นˆ๋„๋ฅผ ์„ธ๋Š” ๊ณผ์ • ์—†์ด ๋‹จ์ˆœํžˆ Counter๋ฅผ ์‚ฌ์šฉํ•ด ๋น ๋ฅด๊ฒŒ ๋นˆ๋„๋ฅผ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

  2. ๋ถ€๋™ ์†Œ์ˆ˜์  ์ฒ˜๋ฆฌ
    ํŒŒ์ด์ฌ์—์„œ๋Š” round() ํ•จ์ˆ˜๊ฐ€ ์Œ์ˆ˜๋ฅผ ์ฒ˜๋ฆฌํ•  ๋•Œ๋„ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๋ฐ˜์˜ฌ๋ฆผ๋˜๋ฏ€๋กœ ๋”ฐ๋กœ ๋ถ€ํ˜ธ๋ฅผ ํ™•์ธํ•˜๊ฑฐ๋‚˜ ์ถ”๊ฐ€์ ์ธ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜์ง€ ์•Š์•„๋„ ๋์Šต๋‹ˆ๋‹ค.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants