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

8-SeongHoonC #26

Merged
merged 3 commits into from
Feb 19, 2024
Merged

8-SeongHoonC #26

merged 3 commits into from
Feb 19, 2024

Conversation

SeongHoonC
Copy link
Collaborator

@SeongHoonC SeongHoonC commented Feb 14, 2024

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

์ขŒํ‘œ ์••์ถ•

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

20๋ถ„

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

์ž…๋ ฅ: 2 4 -10 4 -9

  1. ๊ฐ ์ˆซ์ž๋ฅผ index ์™€ ํ•จ๊ป˜ Pair ๋กœ ์ €์žฅํ•œ๋‹ค. (Pair: kotlin ์—์„œ ๋‘ ๊ฐ€์ง€ ๊ฐ’์„ ํ•˜๋‚˜๋กœ ๋ฌถ๋Š” ๊ฐ์ฒด)

[(2, 0), (4,1), (-10,2), (4,3), (-9,4)]

  1. ์ฒซ๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•œ๋‹ค

[(-10,2), (-9,4), (2,0), (4,1), (4,3)]

  1. 0๋ฒˆ์งธ๋ถ€ํ„ฐ ์ƒˆ๋กœ์šด ์ขŒํ‘œ๋ฅผ ๋งค๊ธด๋‹ค.

  2. -1 ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š”๋ฐ ์ตœ์†Œ๊ฐ’๋ณด๋‹ค ํฌ๋‹ค๋ฉด ์ƒˆ๋กœ์šด ์ขŒํ‘œ๋ฅผ ์ฆ๊ฐ€์‹œํ‚ค๊ณ  ์ตœ์†Œ๊ฐ’์„ ์—…๋ฐ์ดํŠธํ•œ๋‹ค.
    (-10,2) -> 0
    (-9, 4) -> 1
    (2, 0) -> 2
    (4, 1) -> 3
    (4, 3) -> 3 // ์–˜๋Š” ๊ทธ๋Œ€๋กœ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ขŒํ‘œ ๊ทธ๋Œ€๋กœ ๋Œ€์ž…

  3. ๊ฐ index ์— ์ƒˆ๋กœ์šด ์ขŒํ‘œ ๋Œ€์ž…

(-10,2) -> 2์— 0 ๋Œ€์ž…
(-9, 4) -> 4 ์— 1 ๋Œ€์ž…
...

๐Ÿ“š ํ•œ์ค„ํ‰

์–ด๋ ต์ง„ ์•Š์•˜๋Š”๋ฐ ์ •๋ ฌ ์ƒ๊ธฐ์‹œ์ผœ์„œ ์ข‹์•˜์Šต๋‹ˆ๋‹ค.

fnzksxl

This comment was marked as duplicate.

Copy link
Member

@fnzksxl fnzksxl left a comment

Choose a reason for hiding this comment

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

์˜ˆ์ „์— ํ’€์—ˆ๋˜ ๋ฌธ์ œ๋ผ ์ฝ”๋“œ๊ฐ€ ์žˆ๊ตฐ์š”!
์ฒ˜์Œ์— ํ’€์—ˆ์„ ๋•Œ๋Š” ์ถœ๋ ฅํ•  ๋•Œ ์ธ๋ฑ์Šค๋ฅผ ์ง์ ‘ ์ฐพ์•„ ์ถœ๋ ฅํ•œ๋‹ค๊ณ  ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋‚ฌ์—ˆ๋Š”๋ฐ,
๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์“ฐ๋Š” ํ’€์ด๋ฅผ ์ฐพ์•„๋ณด๊ณ  ํ’€ ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

import sys

input = sys.stdin.readline

N = int(input())

origin = list(map(int,input().split()))
compressed = list(sorted(set(origin)))

_dict = {compressed[i]:i for i in range (len(compressed))}

for i in origin:
  print(_dict[i],end=' ')

 // ์ž‘์€ ์ˆ˜ ๋ถ€ํ„ฐ 0์œผ๋กœ ์ƒˆ๋กœ์šด ์ขŒํ‘œ ๋ถ€์—ฌ
    sortedIndexedPoints.forEach {
        // ์ด์ „ ์ˆ˜๋ณด๋‹ค ๋” ํฐ ์ˆ˜๋ฉด ์ตœ์†Œ๊ฐ’ ์—…๋ฐ์ดํŠธ, ์ขŒํ‘œ++
        if (min < it.first) {
            newIndex++
            min = it.first
        }
        // ๋ณธ๋ž˜ ์ธ๋ฑ์Šค์— ์ƒˆ๋กœ์šด ์ขŒํ‘œ๊ฐ’ ๋„ฃ๊ธฐ
        answer[it.second] = newIndex
    }

    println(arr.joinToString(" "))

์ˆ˜๋„์ฝ”๋“œ๋ฅผ ์ฝ๊ณ  ํ’€์ด๋ฐฉ์‹์€ ์ดํ•ดํ–ˆ๋Š”๋ฐ ์ฝ”ํ‹€๋ฆฐ ๋ฌธ๋ฒ•์„ ๋ชฐ๋ผ์„œ ๊ทธ๋Ÿฐ๊ฐ€ ์ด ๋ถ€๋ถ„์ด ์ดํ•ด๊ฐ€ ์ž˜ ์•ˆ๋˜๋„ค์š”.. ใ…œใ…กใ…œ ํ˜น์‹œ ์„ค๋ช…ํ•ด ์ฃผ์‹ค ์ˆ˜ ์žˆ์„๊นŒ์š”?

@SeongHoonC SeongHoonC self-assigned this Feb 15, 2024
@SeongHoonC
Copy link
Collaborator Author

@fnzksxl

 // ์ž‘์€ ์ˆ˜ ๋ถ€ํ„ฐ 0์œผ๋กœ ์ƒˆ๋กœ์šด ์ขŒํ‘œ ๋ถ€์—ฌ
    sortedIndexedPoints.forEach {
        // ์ด์ „ ์ˆ˜๋ณด๋‹ค ๋” ํฐ ์ˆ˜๋ฉด ์ตœ์†Œ๊ฐ’ ์—…๋ฐ์ดํŠธ, ์ขŒํ‘œ++
        if (min < it.first) {
            newIndex++
            min = it.first
        }
        // ๋ณธ๋ž˜ ์ธ๋ฑ์Šค์— ์ƒˆ๋กœ์šด ์ขŒํ‘œ๊ฐ’ ๋„ฃ๊ธฐ
        answer[it.second] = newIndex
    }

    println(arr.joinToString(" "))

forEach ๋Š” Iterable ์˜ ํ™•์žฅ ํ•จ์ˆ˜์ธ๋ฐ ์—ฌ๊ธฐ์„  ๋ฆฌ์ŠคํŠธ ์•„์ดํ…œ์„ ํ•˜๋‚˜์”ฉ ๋Œ๋ฉด์„œ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค.

ํŒŒ์ด์ฌ์—์„œ๋Š” ์•„๋ž˜๋ž‘ ๊ฐ™์Šต๋‹ˆ๋‹น

for it in sortedIndexedPoints:
    if min < it[0]:
         newIndex++
         min = it[0]
    answer[it[1]] = newIndex

joinToString ์€ ๋ง ๊ทธ๋Œ€๋กœ ์›์†Œ๋“ค์„ ํ•˜๋‚˜์˜ ์ŠคํŠธ๋ง์œผ๋กœ ํ•ฉ์น˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
์—ฌ๊ธฐ์„  ๊ฐ ์›์†Œ ์‚ฌ์ด์— " " ๋ฅผ ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค

@alstjr7437
Copy link
Member

์˜ˆ์ „์— ํ’€์—ˆ๋˜ ๋ฌธ์ œ๋ผ ์ฝ”๋“œ๊ฐ€ ์žˆ๊ตฐ์š”! ์ฒ˜์Œ์— ํ’€์—ˆ์„ ๋•Œ๋Š” ์ถœ๋ ฅํ•  ๋•Œ ์ธ๋ฑ์Šค๋ฅผ ์ง์ ‘ ์ฐพ์•„ ์ถœ๋ ฅํ•œ๋‹ค๊ณ  ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋‚ฌ์—ˆ๋Š”๋ฐ, ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์“ฐ๋Š” ํ’€์ด๋ฅผ ์ฐพ์•„๋ณด๊ณ  ํ’€ ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

import sys

input = sys.stdin.readline

N = int(input())

origin = list(map(int,input().split()))
compressed = list(sorted(set(origin)))

_dict = {compressed[i]:i for i in range (len(compressed))}

for i in origin:
  print(_dict[i],end=' ')
 // ์ž‘์€ ์ˆ˜ ๋ถ€ํ„ฐ 0์œผ๋กœ ์ƒˆ๋กœ์šด ์ขŒํ‘œ ๋ถ€์—ฌ
    sortedIndexedPoints.forEach {
        // ์ด์ „ ์ˆ˜๋ณด๋‹ค ๋” ํฐ ์ˆ˜๋ฉด ์ตœ์†Œ๊ฐ’ ์—…๋ฐ์ดํŠธ, ์ขŒํ‘œ++
        if (min < it.first) {
            newIndex++
            min = it.first
        }
        // ๋ณธ๋ž˜ ์ธ๋ฑ์Šค์— ์ƒˆ๋กœ์šด ์ขŒํ‘œ๊ฐ’ ๋„ฃ๊ธฐ
        answer[it.second] = newIndex
    }

    println(arr.joinToString(" "))

์ˆ˜๋„์ฝ”๋“œ๋ฅผ ์ฝ๊ณ  ํ’€์ด๋ฐฉ์‹์€ ์ดํ•ดํ–ˆ๋Š”๋ฐ ์ฝ”ํ‹€๋ฆฐ ๋ฌธ๋ฒ•์„ ๋ชฐ๋ผ์„œ ๊ทธ๋Ÿฐ๊ฐ€ ์ด ๋ถ€๋ถ„์ด ์ดํ•ด๊ฐ€ ์ž˜ ์•ˆ๋˜๋„ค์š”.. ใ…œใ…กใ…œ ํ˜น์‹œ ์„ค๋ช…ํ•ด ์ฃผ์‹ค ์ˆ˜ ์žˆ์„๊นŒ์š”?

์˜คํ˜ธ set์„ ์ด์šฉํ•ด์„œ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•˜๊ณ  dict๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๊ตฐ์š”....๐Ÿง

Comment on lines +23 to +31
sortedIndexedPoints.forEach {
// ์ด์ „ ์ˆ˜๋ณด๋‹ค ๋” ํฐ ์ˆ˜๋ฉด ์ตœ์†Œ๊ฐ’ ์—…๋ฐ์ดํŠธ, ์ขŒํ‘œ++
if (min < it.first) {
newIndex++
min = it.first
}
// ๋ณธ๋ž˜ ์ธ๋ฑ์Šค์— ์ƒˆ๋กœ์šด ์ขŒํ‘œ๊ฐ’ ๋„ฃ๊ธฐ
answer[it.second] = newIndex
}
Copy link
Member

Choose a reason for hiding this comment

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

forEach๋ฅผ ์ด์šฉํ•ด์„œ ์ด๋ ‡๊ฒŒ ์ฝ”๋“œ๊ฐ€ ๋‚˜์˜ฌ ์ˆ˜๋„ ์žˆ๊ตฐ์š”!!
์ €๋Š” ์„ฑํ›ˆ๋‹˜ ์ˆ˜๋„์ฝ”๋“œ๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ์„œ ์ด๋ ‡๊ฒŒ ์ฝ”๋“œ ์ž‘์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค!

N = int(input()) 
X = list(map(int, input().split()))
answer = []

# ๊ฐ ์ˆซ์ž์™€ ์ธ๋ฑ์Šค๋ฅผ ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
for i in range(N):
    answer.append([X[i], i])

# ์ˆซ์ž ๊ธฐ์ค€ ์ •๋ ฌ
answer.sort(key=lambda x: x[0])

index = -1
prev_num = float('-inf')

# ๊ฐ ์ˆซ์ž๋กœ index ์„ ์ •
for i in range(N):
    if answer[i][0] > prev_num:
        index += 1
        prev_num = answer[i][0]
    answer[i][0] = index

# ์ธ๋ฑ์Šค ๊ธฐ์ค€ ์ •๋ ฌ
answer.sort(key=lambda x: x[1])

# ๊ฒฐ๊ณผ ์ถœ๋ ฅ
for i in range(N):
    print(answer[i][0], end=" ")

Copy link
Collaborator

@wkdghdwns199 wkdghdwns199 left a comment

Choose a reason for hiding this comment

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

n = int(input())
arr = list(map(int, input().split()))
numbers = set(arr)
temp = list(numbers)
temp.sort()
dict = {}
for i in range(len(temp)) :
    dict[temp[i]] = i
for number in arr :
    print(dict[number], end='')

์ด ์ฝ”๋“œ๋กœ ํ’€๋ ค๊ณ  ํ–ˆ๋Š”๋ฐ ๊ณ„์† ํ‹€๋ ธ๋‹ค๊ณ  ํ•˜๋”๋ผ๊ตฌ์š”. ๋ญ๊ฐ€ ๋ฌธ์ œ์ผ๊นŒ์š” ใ…œใ…œ

@alstjr7437
Copy link
Member

@wkdghdwns199 ์„ ์ƒ๋‹˜ ์ถœ๋ ฅ์„ ์ž˜ ์‚ดํŽด๋ณด๋ฉด ํ•œ ์นธ์˜ ๊ณต๋ฐฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค^^

@SeongHoonC SeongHoonC merged commit 1ab2833 into main Feb 19, 2024
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.

4 participants