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

70-tgyuuAn #232

Merged
merged 2 commits into from
Aug 19, 2024
Merged

70-tgyuuAn #232

merged 2 commits into from
Aug 19, 2024

Conversation

tgyuuAn
Copy link
Member

@tgyuuAn tgyuuAn commented Aug 16, 2024

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

ํƒ‘ ๋ณด๊ธฐ

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

1์‹œ๊ฐ„ 20๋ถ„

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

image




๋ฌธ์ œ ๋ณด์ž๋งˆ์ž ํ•œ๋‹ฌ ์ „์— ํ’€์—ˆ๋˜ ์˜ฅ์ƒ ์ •์› ๊พธ๋ฏธ๊ธฐ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ฐ๋‚˜์„œ ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

์ € ๋ฌธ์ œ ์•ˆํ’€์–ด๋ดค์—ˆ์œผ๋ฉด ์‰ฝ๊ฒŒ ๋ชปํ’€์—ˆ์„ ๊ฒƒ ๊ฐ™์•„์š”.




์ผ๋‹จ ๋ฌธ์ œ ์ œํ•œ์—์„œ

1 <  N, L < 10000

์œผ๋กœ ์ฃผ์–ด์กŒ๊ธฐ ๋–„๋ฌธ์— ์ตœ๋Œ€ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” &N(log(n))& ์„ ๋„˜๊ธฐ๋ฉด ์•ˆ๋ฉ๋‹ˆ๋‹ค!

๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์ฒ˜์Œ์—๋Š” ํž™์„ ๋– ์˜ฌ๋ ธ์—ˆ์–ด์š”.

ํž™์œผ๋กœ ๊ฐ€์žฅ ๋†’์ด๊ฐ€ ์ž‘์€ ๊ฒƒ๋ถ€ํ„ฐ ํ•˜๋‚˜์”ฉ ์ฐจ๊ณก์ฐจ๊ณก ๋ฝ‘์•„์„œ ๊ฐฑ์‹ ํ•˜๋Š” ๊ต‰์žฅํ•œ ์•„์ด๋””์–ด๋ฅผ ๊ฐ€์กŒ์—ˆ๋Š”๋ฐ,

image

์•„๋ž˜์™€ ๊ฐ™์ด ๋ณผ ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ๊ฐœ์ˆ˜๋„ ๊ฐ™์ด ๋ฐ˜ํ™˜ํ•ด์•ผํ•˜๋”๋ผ๊ณ ์š”.

์ฝ”๋“œ๊นŒ์ง€ 20๋ถ„ ์ •๋„ ๊น”์ง๊ฑฐ๋ฆฌ๋‹ค๊ฐ€ ์‹œ๊ฐ„๋งŒ ๋‚ ๋ ธ์Šต๋‹ˆ๋‹ค..




๊ทธ๋ž˜์„œ ๊ทธ๋Ÿผ &N(log(n))& ์ด๋‚ด๋กœ ์ด ๋ฌธ์ œ๋ฅผ ํ’€ ์ˆ˜ ์žˆ๋Š” ํ•ด๋ฒ•์€ ๋ญ๊ฐ€ ์žˆ์„๊นŒ? ํ•˜๊ณ  ์ƒ๊ฐํ•˜๋‹ค๊ฐ€ ์ € ๋ฌธ์ œ๊ฐ€ ๋– ์˜ฌ๋ž์–ด์š”.

์Šคํƒ์„ ์‚ฌ์šฉํ•ด์„œ ์ตœ๋Œ€ &O(n)& ์œผ๋กœ ํ’€ ์ˆ˜ ์žˆ๋‹ต๋‹ˆ๋‹ค.




์ด ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ๋ฐ ํ•ต์‹ฌ ํ‚ค๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์Šคํƒ์„ ์ด์šฉํ•˜๋Š”๋ฐ, ์Šคํƒ์— ๋‹ด์„ ๋•Œ์—๋Š” ๋ฐ˜๋“œ์‹œ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ๋‹ด๋Š” ๊ฒƒ ์ž…๋‹ˆ๋‹ค.

๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง„ ์˜ˆ์‹œ๋ฅผ ๋“ค์–ด๋ณด๊ฒ ์”๋‹ˆ๋‹ค.

8
3 7 1 6 3 5 1 7




๊ฐ€์žฅ ๋จผ์ € ๋ณด์ด๋Š” ๊ฑด๋ฌผ์€ 3์ž…๋‹ˆ๋‹ค.

์ด ๋•Œ ์Šคํƒ stack = [] ์€ ๋น„์–ด์žˆ์œผ๋ฏ€๋กœ ๊ทธ๋ƒฅ ๋‹ด์•„์ค๋‹ˆ๋‹ค.




๊ทธ ๋‹ค์Œ์œผ๋กœ ๋ณด์ด๋Š” ๊ฑด๋ฌผ์€ 7์ด์—์š”.

์ด ๋•Œ ์Šคํƒ์„ ํ™•์ธํ•ด์ค๋‹ˆ๋‹ค. stack = [3]

์Šคํƒ์— ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ํ•ญ์ด 3์ด๋„ค์š”.

๊ทผ๋ฐ ์šฐ๋ฆฌ๋Š” ์•„๊นŒ ์Šคํƒ์„ ๋ฐ˜๋“œ์‹œ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ๋‹ด๋Š”๋‹ค ๋ผ๊ณ  ํ–ˆ์–ด์š”.

7์ด 3๋ณด๋‹ค ํฌ๋‹ˆ๊นŒ ์Šคํƒ์—์„œ 3์„ ์ œ๊ฑฐํ•ด์ค๋‹ˆ๋‹ค. (์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ•ญ์ด ์žˆ์„ ๊ฒฝ์šฐ 7๋ณด๋‹ค ๋” ํฐ์ˆ˜๊ฐ€ ๋‚˜์˜ฌ ๋–„ ๊นŒ์ง€ ๋งˆ์ง€๋ง‰ ํ•ญ์„ pop ํ•ด์š”)

๊ทธ๋ฆฌ๊ณ  ๋‹ค์‹œ 7์„ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค. stack = [7]




๊ทธ ๋‹ค์Œ์œผ๋กœ ๋ณด์ด๋Š” ๊ฑด๋ฌผ์€ 1 ์ž…๋‹ˆ๋‹ค.

๋‹คํ–‰ํžˆ 1์€ 7๋ณด๋‹ค ์ž‘๋„ค์š”. ์Šคํƒ์— ๊ทธ๋ƒฅ ๋„ฃ์„ ์ˆ˜ ์žˆ๊ฒ ์–ด์š”.

stack = [7, 1]

์ด ๋•Œ ์Šคํƒ์— ๋“ค์–ด๊ฐ”์œผ๋ฏ€๋กœ 3๋ฒˆ์งธ ๊ฑด๋ฌผ์€ 2๋ฒˆ์งธ ๊ฑด๋ฌผ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค๋Š” ๋œป๊ณผ๋„ ๊ฐ™์•„์š”.

๋ณผ ์ˆ˜ ์žˆ๋Š” ๊ฑด๋ฌผ๋“ค ๋ฆฌ์ŠคํŠธ๋ฅผ ๋”ฐ๋กœ ๋งŒ๋“ค์–ด์„œ 3๋ฒˆ์งธ ๊ฑด๋ฌผ์ด 2๋ฒˆ์งธ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค๊ณ  ์ €์žฅํ•ด์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค.

can_see = [0, 0, 2 ... ]




๊ทธ ๋‹ค์Œ์€ 6์ด ๋‚˜์™”๋„ค์š”.

๊ทผ๋ฐ ํ˜„์žฌ ์Šคํƒ์€ stack = [7, 1] ์ƒํƒœ๋ผ์„œ 1์€ 6๋ณด๋‹ค ์ž‘์•„์š”.

๊ทธ๋Ÿผ pop ํ•ด์•ผ๊ฒ ์ฃ ?

๊ทธ๋ž˜์„œ stack = [7, 6] ์ƒํƒœ๊ฐ€ ๋˜์—ˆ์–ด์š”.

๊ทธ๋Ÿผ ๋ญ๋‹ค? 4๋ฒˆ์งธ ๊ฑด๋ฌผ์€ 2๋ฒˆ์งธ ๊ฑด๋ฌผ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค ..!

can_see = [0, 0, 2, 2 ... ]




์ด๋Ÿฐ ์‹์œผ๋กœ ์ญ‰ ๋๊นŒ์ง€ ๊ฐ€๋ฉด ๋ฉ๋‹ˆ๋‹ค.

๊ทผ๋ฐ ์—ฌ๊ธฐ์„œ ๋๋‚ด๋ฉด ์•ˆ๋˜์–ด์š”.

์šฐ๋ฆฌ๋Š” ๊ฑด๋ฌผ์„ ์™ผ์ชฝ -> ์˜ค๋ฅธ์ชฝ์œผ๋กœ๋งŒ ๋ณผ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๊ณ ,

์™ผ์ชฝ <- ์˜ค๋ฅธ์ชฝ ์œผ๋กœ๋„ ๋ณผ ์ˆ˜ ์žˆ์ž–์•„์š”.

๊ทธ๋ž˜์„œ ๋ฐ˜๋Œ€๋กœ๋„ ์™€์•ผํ•ด์š”.




๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ฐ˜๋Œ€๋กœ ์ญ‰์ญ‰ ์˜ค๋ฉด์„œ..

์Šคํƒ์— ๋“ค์–ด๊ฐˆ ๋•Œ ๋งˆ๋‹ค ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ๋ณผ ์ˆ˜ ์žˆ๋Š” ๊ฑด๋ฌผ์„ ๊ฐฑ์‹ ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค!

ํžˆํžˆํžฃ

์ด๋Ÿฐ ์œ ํ˜•์˜ ์Šคํƒ ๋ฌธ์ œ๊ฐ€ ์ž์ฃผ ๋‚˜์˜ค๋„ค์š”.

ํžˆ์Šคํ† ๊ทธ๋žจ ์—ฌ๊ธฐ์„œ๋„ ๋‚˜์™”๋˜ ๊ฒƒ ๊ฐ™์€๋ฐ..

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

@tgyuuAn tgyuuAn added tgyuuAn ํ•œ ์ค„๋กœ๋Š” ์†Œ๊ฐœํ•  ์ˆ˜ ์—†๋Š” ๋‚จ์ž. ์ž‘์„ฑ ์ค‘ โฑ๏ธ labels Aug 16, 2024
@tgyuuAn tgyuuAn requested review from alstjr7437 and H0ngJu August 16, 2024 17:21
@tgyuuAn tgyuuAn self-assigned this Aug 16, 2024
@tgyuuAn tgyuuAn marked this pull request as ready for review August 17, 2024 22:24
Copy link
Collaborator

@H0ngJu H0ngJu left a comment

Choose a reason for hiding this comment

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

์˜ค๋ฅธ์ชฝ ์™ผ์ชฝ ๊ฑด๋ฌผ ๋ณด๋Š”๊ฒŒ ๋งˆ์น˜ ์ €๋ฒˆ ๊ฐ€ํฌ์™€ ํƒ‘ ๋ฌธ์ œ ๊ฐ™์•„์„œ ๋น„์Šทํ•˜๊ฒŒ ์ ‘๊ทผํ–ˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋ƒฅ ์™ผ์ชฝ์—์„œ ๋ฐ”๋ผ๋ณธ ๊ฒฝ์šฐ์™€ ์˜ค๋ฅธ์ชฝ์—์„œ ๋ฐ”๋ผ๋ณธ ๊ฒฝ์šฐ๋ฅผ ๊ฐ๊ฐ ๊ณ„์‚ฐํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทผ๋ฐ ์—ญ์‹œ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ N^2์ด ๋˜์–ด์„œ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚˜๋”๋ผ๊ณ ์š”. ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ๋‚ฎ์ถœ ๋ฐฉ๋ฒ•์ด ๋– ์˜ค๋ฅด์ง€ ์•Š์•„์„œ PR ์ฐธ๊ณ ํ•˜์˜€์Šต๋‹ˆ๋‹ค!

์ด๋Ÿฐ๋ฅ˜์˜ ๋ฌธ์ œ๋„ ์Šคํƒ์œผ๋กœ ํ’€ ์ˆ˜ ์žˆ๊ตฐ์š” .. ํ  ์Šคํƒ ๋ฌธ์ œ๋ฅผ ๋‹ค์‹œ ๋Œ์•„๋ณผ ํ•„์š”์„ฑ์„ ๋Š๊ผˆ์Šต๋‹ˆ๋‹ค. ์˜ฅ์ƒ์ •์› ๊พธ๋ฏธ๊ธฐ๋„ ํ•œ๋ฒˆ ๋„์ „ํ•ด๋ณด๊ฒ ์Šด๋‹ค!

์‹œ๊ฐ„ ์ดˆ๊ณผ๋‚œ ์ฝ”๋“œ
import sys

def input() : return sys.stdin.readline().rstrip()

N = int(input())
buildings = list(map(int, input().split()))

def cnt_buildings(n):
    can_view = []
    building_height = buildings[n]

    # ์™ผ์ชฝ
    for i in range(n-1, -1, -1):
        if buildings[i] > building_height:
            can_view.append(i)
            building_height = buildings[i]
    # ์˜ค๋ฅธ์ชฝ
    building_height = buildings[n]
    for i in range(n+1, N):
        if buildings[i] > building_height:
            can_view.append(i)
            building_height = buildings[i]

    tmp = float('inf')
    if not can_view:
        return 0,
    else:
        for b in can_view:
            if abs(b-n) < tmp:
                tmp = abs(b-n)
                min_num_building = b
        return len(can_view), min_num_building+1

for i in range(N):
    print(*cnt_buildings(i))

@alstjr7437
Copy link
Member

์•„๋‹ˆ ์™ผ์ชฝ ์˜ค๋ฅธ์ชฝ ํ•œ๋ฒˆ์”ฉ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ค˜์•ผ ํ–ˆ๊ตฐ์š”..

์ฒ˜์Œ์—๋Š” ๊ฐ ํƒ‘๋“ค์ด ์™ผ์ชฝ ์˜ค๋ฅธ์ชฝ ํ™•์ธํ•ด์„œ ํ•˜๋ฉด ๋˜์ง€ ์•Š๋‚˜ 5์ดˆ ์ •๋„ ์ƒ๊ฐํ•˜๋‹ค๊ฐ€ ์ œํ•œ ๋ณด๊ณ  ๋ฐ”๋กœ ๋‹ค๋ฅธ๊ฑฐ ์ƒ๊ฐํ•˜๋Š”๋ฐ

๋„์ €ํžˆ ์ƒ๊ฐ์ด ์•ˆ๋‚˜๊ณ  ๊ทธ๋ฆผ๋งŒ ๊ทธ๋ฆฌ๋‹ค๊ฐ€ ์‹œ๊ฐ„์ด 20๋ถ„ ์ง€๋‚˜์„œ ํ’€์ด ์ฐธ๊ณ ํ–ˆ๋„ค์š”...

๋ธ”๋กœ๊ทธ์™€ ๊ฐ™์ด ๋ฐ”๋กœ ํ™•์ธํ•˜๊ณ  ๋นผ๋ฒ„๋ฆฌ๋ฉด ์กฐ๊ธˆ ๋” ๋ณด๊ธฐ ํŽธํ•ด์ง„ ๊ฒƒ ๊ฐ™์•„์š”!

@tgyuuAn tgyuuAn merged commit 3f1e50d into main Aug 19, 2024
9 checks passed
@tgyuuAn tgyuuAn deleted the 70-tgyuuAn branch August 19, 2024 21:11
@tgyuuAn tgyuuAn mentioned this pull request Oct 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tgyuuAn ํ•œ ์ค„๋กœ๋Š” ์†Œ๊ฐœํ•  ์ˆ˜ ์—†๋Š” ๋‚จ์ž. ๋ฆฌ๋ทฐ ์™„๋ฃŒ โœ”๏ธ
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants