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

11-fnzksxl #56

Merged
merged 3 commits into from
Mar 16, 2024
Merged

11-fnzksxl #56

merged 3 commits into from
Mar 16, 2024

Conversation

fnzksxl
Copy link
Member

@fnzksxl fnzksxl commented Mar 10, 2024

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

๋ณด์„ ์‡ผํ•‘

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

2์‹œ๊ฐ„ ์ด์ƒ

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

ํˆฌํฌ์ธํ„ฐ๊นŒ์ง€๋Š” ๋จธ๋ฆฟ์†์— ๋– ์˜ฌ๋ ธ๋Š”๋ฐ ๊ตฌํ˜„ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์ธํ„ฐ๋„ท์—์„œ ์ฐธ๊ณ ๋ฅผ ์ข€ ํ–ˆ์Šต๋‹ˆ๋‹ค. ์‰ฝ๊ฒŒ ์•ˆ ๋– ์˜ค๋ฅด๋”๋ผ๊ตฌ์š” ใ… 

๋ฐฐ์—ด์˜ ํฌ๊ธฐ๊ฐ€ 1 ~ 100,000 ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ธŒ๋ฃจํŠธํฌ์Šค๋กœ ํ™•์ธํ•˜๋ฉด ๋ฌด์กฐ๊ฑด ํšจ์œจ์„ฑ ํ…Œ์ŠคํŠธ์—์„œ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋œน๋‹ˆ๋‹ค.
๊ทธ๋ž˜์„œ ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ ํˆฌํฌ์ธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๋ฐ์š”.

image

์ฒ˜์Œ์— head์™€ tail์„ 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•œ ์ƒํƒœ๋กœ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.

image

head ~ tail ์•ˆ์— ๋ชจ๋“  ๋ณด์„์ด ์—†๋‹ค๋ฉด tail += 1 ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ๋ฒ”์œ„๋ฅผ ๋Š˜๋ ค๊ฐ‘๋‹ˆ๋‹ค.

image

์ž, ์ด์ œ ๋ชจ๋“  ๋ณด์„์ด ๋ฒ”์œ„ ์•ˆ์— ๋“ค์–ด์™”์ฃ ?

image

์ด์ œ head ํฌ์ธํ„ฐ๋ฅผ ํ•œ ์นธ์”ฉ ์˜ฌ๋ ค๊ฐ€๋ฉฐ ํ˜„์žฌ tail๊นŒ์ง€์˜ ๋ฒ”์œ„์—์„œ ์ตœ์†Œ ๋ฒ”์œ„๋ฅผ ์ฐพ์Šต๋‹ˆ๋‹ค.

image

์ตœ์†Œ ๋ฒ”์œ„๋ฅผ ์ฐพ์€ ๋’ค ์ผ๋‹จ ์ €์žฅํ•˜๊ณ , tail์ด ์ด ๋ณด์„ํ–‰๋ ฌ์˜ ๋์œผ๋กœ ๊ฐˆ ๋•Œ๊นŒ์ง€ ์ด ์ž‘์—…์„ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค.

image

image

image

๋‹ค์‹œ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๋ฒ”์œ„๊ฐ€ ๋‚˜์™”์„ ๋•Œ, ์ด์ „์˜ ๋ฒ”์œ„์™€ ๊ธธ์ด ๋น„๊ต๋ฅผ ํ•ด์„œ ๋” ์งง์€ ๊ฒƒ์„ ์ •๋‹ต์œผ๋กœ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.


์ด๋ฅผ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์–ด๋–ป๊ฒŒ ํ• ๊นŒ ๋น„๋ฒผ๋ณด๋‹ค๊ฐ€ ์ž˜ ์•ˆ ๋ผ์„œ ์ธํ„ฐ๋„ท์„ ์ฐธ๊ณ ํ–ˆ์Šต๋‹ˆ๋‹ค.
๋”•์…”๋„ˆ๋ฆฌ์— ๋ณด์„์˜ ๊ฐœ์ˆ˜๋ฅผ ์ €์žฅํ•˜๊ณ  ๋ณด์„์˜ ๊ฐœ์ˆ˜ > 1 ์ด๋ผ๋ฉด head๋ฅผ ๋‹ค์‹œ ์˜ฌ๋ ค๊ฐ€๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ๋˜๊ฒ ์Šต๋‹ˆ๋‹ค!

def solution(gems):
    answer = []
    head, tail = 0, 0
    gem_length = len(set(gems))
    gems_dict = {}
    gem_min = len(gems) + 1
    
    while head <= tail < len(gems):
        gems_dict[gems[tail]] = gems_dict.get(gems[tail], 0) + 1
        tail+=1
    
        if len(gems_dict) == gem_length:
            while head < tail:
                if gems_dict[gems[head]] > 1:
                    gems_dict[gems[head]] -= 1
                    head += 1
                
                elif gem_min > tail - head:
                    gem_min = tail - head
                    answer = [head+1, tail]
                    break
                    
                else:
                    break
        
    return answer

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

@alstjr7437
Copy link
Member

์•„ํ•˜..! ํˆฌํฌ์ธํ„ฐ๋กœ ํ‘ธ๋Š” ๋ถ€๋ถ„์ด์˜€๊ตฐ์š”....
์ €๋Š” set์œผ๋กœ ์ค‘๋ณต ์—†์• ์„œ ๋ช‡๊ฐœ์˜ ๋ณด์„์ด ๋“ค์–ด๊ฐ€๋Š”์ง€ ๊ณ„์‚ฐ ํ•ด๋†“๊ณ 
๋”•์…”๋„ˆ๋ฆฌ์— ๋ชจ๋“  ๋ณด์„์ด ๋“ค์–ด๊ฐ€๋ฉด ๋ฉˆ์ถ”๋„๋ก ํ–ˆ๋Š”๋ฐ (์—ฅ ์ด๋ ‡๊ฒŒ ์‰ฝ๋‹ค๊ณ  ํ•˜๋ฉด์„œ ์ œ์ถœํ•˜๋Š”๋ฐ ๋ฌด์ˆ˜ํ•œ ๋นจ๊ฐ„์ƒ‰์ด....?)
๊ทธ๋ถ€๋ถ„์€ ๊ฐ€์žฅ ์งง์€ ๋ถ€๋ถ„์ด ์•„๋‹ˆ๊ณ  ๋“ค์–ด์˜ค๋ฉด ๋ฐ”๋กœ ๋๋‚˜๋ฒ„๋ฆฌ๋‹ˆ๊นŒ ํ‹€๋ ธ์—ˆ๋„ค์š”..!

a b b b c d a
- - - -
- - - -

์œ„ ํ‘œ์™€ ๊ฐ™์ด ํ•˜๋ฉด
์ •๋‹ต์€ [4,7] ์„ ์ถœ๋ ฅ ํ•ด์•ผํ•˜๋Š”๋ฐ
[1,6]์„ ์ถœ๋ ฅํ•ด์„œ ๋ฐ˜๋ก€๋ฅผ ๋งŒ๋“ค์–ด์„œ ํƒœ์›๋‹˜๊ณผ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ๋‹ค์‹œ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค!!

Copy link
Collaborator

@SeongHoonC SeongHoonC left a comment

Choose a reason for hiding this comment

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

ํ’€๋‹ค๊ฐ€ ๋‹ต์„ ๋ดค๋Š”๋ฐ๋„ ์ดํ•ดํ•˜๊ธฐ ์–ด๋ ค์› ๋„ค์š”..
์ €๋Š” end ๋ฅผ ํ•˜๋‚˜์”ฉ ์ฆ๊ฐ€์‹œํ‚ค๋ฉด์„œ ๊ฐ€๋Šฅํ•œ ์กฐํ•ฉ์„ ๋งŒ๋“ค๊ณ  ์ข…๋ฅ˜์˜ ๊ฐœ์ˆ˜์™€ ๊ฐ™๊ณ  ์ด์ „ ์ตœ์†Œ๊ฐ’๋ณด๋‹ค ์ž‘๋‹ค๋ฉด ์—…๋ฐ์ดํŠธํ•˜๋„๋ก ํ–ˆ์Šต๋‹ˆ๋‹ค!

class Solution {
      fun solution(gems: Array<String>): IntArray {
        val kind = gems.toHashSet().size
        val answer = IntArray(2)
        var length = Int.MAX_VALUE
        var start = 0
        val map: MutableMap<String, Int> = HashMap()

        for (end in gems.indices) {
            map[gems[end]] = (map[gems[end]] ?: 0) + 1

            while (map[gems[start]]!! > 1) {
                map[gems[start]] = map[gems[start]]!! - 1
                start++
            }
            if (map.size == kind && length > end - start) {
                length = end - start
                answer[0] = start + 1
                answer[1] = end + 1
            }
        }
        return answer
    }
}

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.

์ดํ•ด๊ฐ€ ์•ˆ๋˜๋Š” ๋ถ€๋ถ„์ด ์žˆ์—ˆ๋Š”๋ฐ, ๊ฒฐ๋ก ์ ์œผ๋กœ dictionary ์— ๊ณ„์† ๋ณด์„๋“ค์„ ์นด์šดํŠธ ํ•˜๋‹ค๊ฐ€ ๋ชจ๋“  ๋ณด์„๋“ค์ด ํ™•์ธ์ด ๋˜์—ˆ์œผ๋ฉด ํŠน์ • ๋ณด์„์ด 1 ์ด์ƒ์ด๋ฉด ๋ฌด์กฐ๊ฑด ์ตœ์†Œ ๊ตฌ๊ฐ„์ด ์•„๋‹ˆ๊ฒŒ ๋˜๋ฏ€๋กœ ํ•˜๋‚˜๋ฅผ ๋นผ๊ณ  ์‹œ์ž‘ ์ง€์ ์„ 1 ์˜ฌ๋ฆฌ๊ณ , ๋งŒ์•ฝ ๋‹ค ํ•˜๋‚˜์”ฉ์ด๋ผ๋ฉด ํ˜„์žฌ์˜ ๊ตฌ๊ฐ„์ด ์ตœ์†Œ ๊ตฌ๊ฐ„์ด๋ฉด ๊ฐฑ์‹ ํ•˜๋Š” ์‹์ด๊ตฐ์š”!

@fnzksxl
Copy link
Member Author

fnzksxl commented Mar 14, 2024

@alstjr7437

์ €๋„ ์ฒ˜์Œ์— ๋น„์Šทํ•˜๊ฒŒ tail์„ ๋๊นŒ์ง€ ์•ˆ ๋Œ๋ ธ์–ด์„œ ๋˜‘๊ฐ™์ด ํ‹€๋ ธ์—ˆ์–ด์š” ใ…‹ใ…‹ใ…‹ใ…‹

@fnzksxl
Copy link
Member Author

fnzksxl commented Mar 14, 2024

@SeongHoonC

์˜ค,, ๋กœ์ง์€ ๋น„์Šทํ•œ๋ฐ ์ฝ”ํ‹€๋ฆฐ์œผ๋กœ ์“ฐ๋‹ˆ๊นŒ ๋Š๋‚Œ์ด ์ƒ‰๋‹ฌ๋ผ๋ณด์ด๋„ค์š”

@fnzksxl
Copy link
Member Author

fnzksxl commented Mar 14, 2024

@wkdghdwns199

์‹ค์ œ๋กœ ๋”•์…”๋„ˆ๋ฆฌ ์•ˆ์˜ ๊ฐ’์ด ๋ชจ๋‘ 1์ผ ๋•Œ ์ตœ์†Œ ๊ตฌ๊ฐ„๊ณผ ๋น„๊ตํ•˜๋Š”๊ฑด ์•„๋‹ˆ๊ตฌ์šฉ head ์œ„์น˜์˜ ๋ณด์„๋งŒ 1์ด ๋˜์–ด๋„ ๋ฉ๋‹ˆ๋‹ค!

@fnzksxl fnzksxl merged commit 9333369 into main Mar 16, 2024
@fnzksxl fnzksxl deleted the 11-fnzksxl branch March 16, 2024 02:12
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