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

78-tgyuuAn #250

Merged
merged 2 commits into from
Oct 14, 2024
Merged

78-tgyuuAn #250

merged 2 commits into from
Oct 14, 2024

Conversation

tgyuuAn
Copy link
Member

@tgyuuAn tgyuuAn commented Oct 6, 2024

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

ํ’์„  ํ„ฐ๋œจ๋ฆฌ๊ธฐ

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

40๋ถ„

(์ค‘๊ฐ„์— ๋ ˆํผ๋Ÿฐ์Šค ํ•œ๋ฒˆ ์Šฌ์ฉ ๋ด„,,)

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

image

1. ์ฒซ๋ฒˆ์งธ ์‚ฌ๊ณ 

์–ด์ฐจํ”ผ ํ•œ ๋ฒˆ์€ ๊ทธ๋ƒฅ ๊ฐ’์˜ ๋น„๊ต ์—†์ด ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ,

์™ผ์ชฝ์ด๋‚˜ ์˜ค๋ฅธ์ชฝ ํ•œ ๋ฒˆ์€ ๊ทธ๋ƒฅ ์ œ๊ฑฐํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๊ณ ,

์™ผ์ชฝ, ์˜ค๋ฅธ์ชฝ ๋‘˜๋‹ค ํ•ด๋‹น ํ’์„ ๋ณด๋‹ค ํฐ ํ’์„ ์ด ์žˆ์„๊ฒฝ์šฐ ์ œ๊ฑฐํ•  ์ˆ˜ ์—†๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค.

image




๊ทธ๋ž˜์„œ ์ด์ „์— ํ’€์—ˆ๋˜ #232 ๋ฌธ์ œ์™€ ์™„์ „ ๊ฐ™์€ ๋ฌธ์ œ์•„๋‹ˆ์•ผ?!

๋ผ๊ณ  ์ƒ๊ฐ์„ํ•˜๊ณ  ์ด๋ฒˆ์—” ์ฝ”ํ‹€๋ฆฐ์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์งœ๋ดค์Šต๋‹ˆ๋‹ค.

import java.util.*

class Solution {
    fun solution(a: IntArray): Int {
        val ltrFlag = Array<Boolean>(a.size){ false }
        val ltrStack = Stack<Int>()
        for((idx,num) in a.withIndex()){
            if(ltrStack.isEmpty()){
                ltrStack.add(num)
                ltrFlag[idx] = false
                continue
            }
            
            if(ltrStack.peek() > num){
                ltrStack.add(num)
                ltrFlag[idx] = true
                continue
            }
            
            while(ltrStack.size > 0 && ltrStack.peek() < num){
                ltrStack.pop()
            }
            ltrStack.add(num)
            ltrFlag[idx] = false
        }
        
        val rtlFlag = Array<Boolean>(a.size){ false }
        val rtlStack = Stack<Int>()
        
        for((idx,num) in a.withIndex().reversed()){
            if(rtlStack.isEmpty()){
                rtlStack.add(num)
                rtlFlag[idx] = false
                continue
            }
            
            if(rtlStack.peek() > num){
                rtlStack.add(num)
                rtlFlag[idx] = true
                continue
            }
            
            while(rtlStack.size > 0 && rtlStack.peek() < num){
                rtlStack.pop()
            }
            rtlStack.add(num)
            rtlFlag[idx] = false
        }

        var answer: Int = 0
        ltrFlag.zip(rtlFlag){ ltr, rtl ->
            if(ltr == rtl && rtl == true){
                answer += 1
            }
        }
        
        return a.size - answer
    }
}

์•„๋ฟ”์‹ธ.... ํ†ต๊ณผ๊ฐ€ ์•ˆ๋˜๋”๋ผ๊ณ ์š”,,,

๋„๋Œ€์ฒด ์™œ ์•ˆ๋ ๊นŒ..? ๋ฅผ 30๋ถ„๋™์•ˆ ๊ณ ๋ฏผํ•˜๋‹ค๊ฐ€ gg์น˜๊ณ  ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ ์ทคํ–ˆ์Šต๋‹ˆ๋‹ค.

์ ‘๊ทผ ๋ฐฉ๋ฒ• - ISO , 8880๋‹˜ ํ’€์ด ์ฐธ๊ณ 




2. ๋‘ ๋ฒˆ์งธ ์‚ฌ๊ณ 

์•„์ฐจ์ฐจ,,, ์ฐฉ๊ฐํ•œ ๊ฒŒ ์žˆ์—ˆ์”๋‹ˆ๋‘ฅ

image

image

๋ฌด์Šจ ๊ท€์‹ ์ด ๋“ค๋ ค์„œ 1๋ฒˆ ์‚ฌ๊ณ  ์ฒ˜๋Ÿผ ์ƒ๊ฐํ•œ ์ง€๋Š” ๋ชจ๋ฅด๊ฒ ๋Š”๋ฐ, ๊ฒฐ๊ตญ ์œ„ ๊ทธ๋ฆผ์—์„œ ํ’์„  1๋„ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.




์ด ๋ฌธ์ œ์˜ ํ•ต์‹ฌ์€ ๋‹จ ํ•œ๋ฒˆ์˜ ํ’์„ ์„ ๊ทธ๋ƒฅ ์ œ๊ฑฐ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ ์ด์—ˆ์Šต๋‹ˆ๋‹ค.

๋งŒ์•ฝ ์ด ์กฐ๊ฑด์ด ์—†์—ˆ์œผ๋ฉด ํ•ญ์ƒ ์ตœ์†Œ ๊ฐ’์„ ๊ฐ€์ง€๋Š” ํ’์„ ์ด ๋‚จ์•˜์„ ํ…๋ฐ,

์œ„ ์กฐ๊ฑด์œผ๋กœ ์ธํ•ด์„œ ํ’์„ ์„ ์ตœ๋Œ€ํ•œ ์‚ด๋ ค๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.




์–ด๋–ป๊ฒŒ ํ•˜๋Š๋ƒ?

๋‹จ ํ•œ๋ฒˆ ํ’์„ ์„ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์„ ๋ฐ˜๋“œ์‹œ ์ตœ์†Œ ๊ฐ’ ํ’์„ ์— ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

์ตœ์†Œ ๊ฐ’ ํ’์„ ์€ ์–ด๋–ค ํ’์„ ๊ณผ๋„ vs๋ฅผ ๋ถ™์œผ๋ฉด ๋‹ค์ด๊ธฐ๋‹ˆ๊นŒ ๋ง์ด์—์˜ค

์ฆ‰, ํ•ต์‹ฌ์€ ํ•œ ๋ฒˆ ์ œ๊ฑฐํ•˜๋Š” ๊ฒƒ ์—†์ด ์ตœ์†Œ๊ฐ’์ธ ํ’์„ ๊ณผ ์‚ด๋ฆฌ๋ ค๋Š” ํ’์„  ๋‹จ 2๊ฐœ๋งŒ ๋‚จ๊ธธ ์ˆ˜ ์žˆ์œผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.




์•„๋ž˜ ์˜ˆ์”จ๋ฅผ ํ•œ๋ฒˆ ๋ด…์‹œ๋‹ท๋‹ท.

image




๋จผ์ € ์™ผ์ชฝ -> ์˜ค๋ฅธ์ชฝ์˜ ๊ฒฝ์šฐ๋ฅผ ์ƒ๊ฐํ•ด๋ด…์‹œ๋‘ฅ.

image

**"์–ด? ์˜ค๋ฅธ์ชฝ ํ’์„ ์€ ๋‹ค ์–ด๋”ฐ ๋‚ ๋ ค๋จน์œผ์…จ๋‚˜์š”?"

"์ตœ์†Œ ๊ฐ’ ํ’์„  1์ด ๋‹ค ์žก์•„ ๋จน์—ˆ์”๋‹ˆ๋‹ค!"

๋งž์”๋‹ˆ๋‹ค. ์ตœ์†Œ๊ฐ’ ๊ธฐ์ค€์œผ๋กœ ํ•œ ์ชฝ์”ฉ๋งŒ ์ƒ๊ฐํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.




image

์—ฌ๊ธฐ์„œ 6์„ ์‚ด๋ฆด ์ˆ˜ ์žˆ๋‚˜์š”?

๋„ค 5์™€ 1์„ ๋ถ™์ด๋ฉด ๋ฉ๋‹ˆ๋‹ท.


์—ฌ๊ธฐ์„œ 5๋ฅผ ์‚ด๋ฆด ์ˆ˜ ์žˆ๋‚˜์š”?

๋„ค 5์™€ 6์„ ๋ถ™์ด๋ฉด ๋ฉ๋‹ˆ๋‹ท.



๊ทธ๋Ÿผ ์ด 2๊ฐœ๊ฐ€ ๋ฉ๋‹ˆ๋‹ท




์ž ์ด๋ฒˆ์—” ์˜ค๋ฅธ์ชฝ -> ์™ผ์ชฝ.

image

์—ฌ๊ธฐ์„œ 3์„ ์‚ด๋ฆด ์ˆ˜ ์žˆ๋‚˜์š”?

๋„ค 2์™€ 4๋ฅผ 1์— ๋ถ™์ด๋ฉด ๋ฉ๋‹ˆ๋‹ท.


์—ฌ๊ธฐ์„œ 2๋ฅผ ์‚ด๋ฆด ์ˆ˜ ์žˆ๋‚˜์š”?

๋„ค 4์™€ 3์„ ๋ถ™๊ณ  2์™€ 3์„ ๋ถ™์ด๋ฉด ๋ฉ๋‹ˆ๋‹ท.


๊ทผ๋ฐ, ์—ฌ๊ธฐ์„œ 4๋ฅผ ์‚ด๋ฆด ์ˆ˜ ์žˆ๋‚˜์š”?

์–ด... ๋ถˆ๊ฐ€๋Šฅ ํ•˜๋„ค์š”




์ฆ‰, ๋์—์„œ ์ซ˜๋ผ๋ฝ ๋‚ด๋ ค์˜ค๋ฉด์„œ ์ตœ์†Œ๊ฐ’์„ ๊ฐฑ์‹ ํ•ด์ฃผ๋Š” ๋…€์„๋“ค๋งŒ ์ท์ท ์นด์šดํŒ…ํ•ด์ฃผ๋ฉด ๋œ๋‹ค๋Š” ๋ง์”€.




๊ฑฐ๊ธฐ๋‹ค ์ ค ๋งˆ์ง€๋ง‰์— ์ตœ์†Œ๊ฐ’ ํ’์„ ๋„ ์‚ด๋ฆด ์ˆ˜ ์žˆ์œผ๋‹ˆ +1์„ ์žŠ์ง€๋ง์•„์ฃผ์Ÿˆ

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

@tgyuuAn tgyuuAn added tgyuuAn ํ•œ ์ค„๋กœ๋Š” ์†Œ๊ฐœํ•  ์ˆ˜ ์—†๋Š” ๋‚จ์ž. ์ž‘์„ฑ ์ค‘ โฑ๏ธ labels Oct 6, 2024
@tgyuuAn tgyuuAn requested review from alstjr7437 and H0ngJu October 6, 2024 02:32
@tgyuuAn tgyuuAn self-assigned this Oct 6, 2024
@tgyuuAn tgyuuAn marked this pull request as ready for review October 6, 2024 05:12
@tgyuuAn tgyuuAn removed the request for review from alstjr7437 October 10, 2024 15:05
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.

์™€ ๋ฌธ์ œ ์–ด๋ ต๋„ค์š” ใ„ทใ„ท
๋ฌธ์ œ ํ•ด์„ํ•˜๋Š”๋ฐ๋„ ์˜ค๋ž˜ ๊ฑธ๋ ธ๊ณ , ํ’€์ด ์ดํ•ดํ•˜๋Š”๋ฐ๋„ ์‹œ๊ฐ„์ด ๊ฑธ๋ ท์–ด์š”

์ตœ๊ทผ์— ํ‘ผ ๋ฌธ์ œ ์ค‘์—์„œ๋„ ํŠนํžˆ ์ ‘๊ทผ๋ฒ•์กฐ์ฐจ ์ƒ๊ฐ์ด ์•ˆ ๋‚˜๋Š” ๋ฌธ์ œ์˜€์–ด์š”.
๊ทธ๋ž˜์„œ ๊ฒฐ๊ตญ ๋ ˆํผ๋Ÿฐ์Šค๋“ค ์ฐธ๊ณ ํ–ˆ์Šต๋‹ˆ๋‹ค;;ใ…Žใ…ํ•˜

๋ฌธ์ œํ‘ธ๋Š๋ผ ์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค !!

def solution(a):
    answer = 0
    left = [1000001] * len(a)
    right = [1000001] * len(a)
    
    left[0] = a[0]
    
    for i in range(1, len(a)):
        left[i] = min(left[i-1], a[i])
        
    right[-1] = a[-1]
    
    for i in range(len(a)-2, -1, -1):
        right[i] = min(right[i+1], a[i])
        
    for i in range(len(a)): # ์™ผ์ชฝ, ์˜ค๋ฅธ์ชฝ์ด ํฐ ๊ฒฝ์šฐ์—๋งŒ ์‚ด์•„๋‚จ์Œ
        if left[i] < a[i] and right[i] < a[i]:
            answer += 1
            
    return len(a) - answer

@tgyuuAn
Copy link
Member Author

tgyuuAn commented Oct 14, 2024

์™€ ๋ฌธ์ œ ์–ด๋ ต๋„ค์š” ใ„ทใ„ท ๋ฌธ์ œ ํ•ด์„ํ•˜๋Š”๋ฐ๋„ ์˜ค๋ž˜ ๊ฑธ๋ ธ๊ณ , ํ’€์ด ์ดํ•ดํ•˜๋Š”๋ฐ๋„ ์‹œ๊ฐ„์ด ๊ฑธ๋ ท์–ด์š”

์ตœ๊ทผ์— ํ‘ผ ๋ฌธ์ œ ์ค‘์—์„œ๋„ ํŠนํžˆ ์ ‘๊ทผ๋ฒ•์กฐ์ฐจ ์ƒ๊ฐ์ด ์•ˆ ๋‚˜๋Š” ๋ฌธ์ œ์˜€์–ด์š”. ๊ทธ๋ž˜์„œ ๊ฒฐ๊ตญ ๋ ˆํผ๋Ÿฐ์Šค๋“ค ์ฐธ๊ณ ํ–ˆ์Šต๋‹ˆ๋‹ค;;ใ…Žใ…ํ•˜

๋ฌธ์ œํ‘ธ๋Š๋ผ ์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค !!

def solution(a):
    answer = 0
    left = [1000001] * len(a)
    right = [1000001] * len(a)
    
    left[0] = a[0]
    
    for i in range(1, len(a)):
        left[i] = min(left[i-1], a[i])
        
    right[-1] = a[-1]
    
    for i in range(len(a)-2, -1, -1):
        right[i] = min(right[i+1], a[i])
        
    for i in range(len(a)): # ์™ผ์ชฝ, ์˜ค๋ฅธ์ชฝ์ด ํฐ ๊ฒฝ์šฐ์—๋งŒ ์‚ด์•„๋‚จ์Œ
        if left[i] < a[i] and right[i] < a[i]:
            answer += 1
            
    return len(a) - answer

์•„๋‹ˆ ๊ทผ๋ฐ ์ง„์งœ ์–ด๋ ต๊ธดํ•ด์š” ,,, ๋ฌธ์ œ ํ’€์ด ์ด๊ฑฐ ์ง„์งœ ์–ด๋–ป๊ฒŒ ๋– ์˜ฌ๋ฆฌ์ง€ ?

@tgyuuAn tgyuuAn merged commit 6ca77b2 into main Oct 14, 2024
@tgyuuAn tgyuuAn deleted the 78-tgyuuAn branch October 14, 2024 01:42
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.

2 participants