Skip to content

Conversation

@bishoe01
Copy link
Collaborator

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

3sum-closest

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2025-05-23 แ„‹แ…ฉแ„’แ…ฎ 3 55 14

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

30min

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

Medium ์ค‘์— ์ •๋‹ต๋ฅ  50% ๋ฏธ๋งŒ์ด๋ผ์„œ, ๋‹น์—ฐํžˆ ๋ง˜ํŽธํ•˜๊ฒŒ O(n^3)๋กœ ํ’€ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฑด ๊ธฐ๋Œ€๋„ ์•ˆํ–ˆ๊ณ , ์–ด๋–ป๊ฒŒ O(n^2)๋กœ ๋Œ๋ฆด๊นŒ ๊ณ ๋ฏผํ•˜๋‹ค๊ฐ€ ํˆฌํฌ์ธํ„ฐ ์ผ์–ด์š” !

  1. for๋ฌธ์œผ๋กœ ์ฒ˜์Œ ์žก์„ ์นœ๊ตฌ ํ•˜๋‚˜ ํ”ฝ์Šคํ•˜๊ณ 
  2. ๋‚˜๋จธ์ง€ 2๊ฐœ๋ฅผ ์ด์ œ left , right ํˆฌํฌ์ธํ„ฐ๋กœ ๊ตด๋ ค์ฃผ๋ฉด์„œ ๋น„๊ต ํ•ด์คฌ์Šต๋‹ˆ๋‹ค.
  3. abs(3๊ฐœ์˜ ํ•ฉ - target)์ด ๊ฐ€์žฅ ์ž‘์€๊ฑธ ์ฐพ๋Š”๊ฒŒ ๋ชฉํ‘œ๋ผ์„œ ์ด๊ฑธ ๋”ฐ๋กœ ํ•จ์ˆ˜๋กœ ๋บ์–ด์š”.
  4. abs(3๊ฐœ์˜ํ•ฉ - target)์ด target๋ณด๋‹ค ์ž‘์œผ๋ฉด left๋ฅผ +=1 , ๋ฐ˜๋Œ€๋Š” right-=1 , else ( 0 ์ด๋ฉด ๊ทธ๊ฑฐ ์ž์ฒด๊ฐ€ ์ •๋‹ต , target์— ๊ทธ๋Œ€๋กœ ๋„์ฐฉํ–ˆ์œผ๋‹ˆ๊นŒ ) return target

answer๊ฐ’ ๋ณ€๊ฒฝ์‚ฌ

//        var answer = 3000 // ์ตœ๋Œ€๊ฐ’ ๋ฒ”์œ„ 3๊ฐœ ๋”ํ•œ ๊ฒƒ ์œผ๋กœ ํ•ด์คฌ๋Š”๋ฐ, target์ด ๋†’์•„์„œ ์ด๋ ‡๊ฒŒ ํ•ด์ฃผ๋ฉด ํ‹€๋ฆฌ๋‹ค๊ณ  ๋œธ !
//        ar answer = Int.max // ํ•˜๋‹ˆ๊นŒ ํ„ฐ์ง
        var answer = Int(Int32.max) // max๊ฐ’์— +๊ฐ€ ์กฐ๊ธˆ์ด๋ผ๋„ ๋˜๋‹ˆ๊นŒ ํŽ‘ ํ„ฐ์ ธ๋ฒ„๋ฆฌ๋Š” ์ด์Šˆ๊ฐ€ ์žˆ์Œ

var answer = 3000

  • ์ด๊ฑฐ๋Š” answer[i]์˜ ์ตœ๋Œ€๊ฐ’์ด 1000 ์ด๊ธธ๋ž˜, ๊ทธ๋ƒฅ ๊ทธ๊ฑฐ์— ๋งž๊ฒŒ ๊ฐ€์žฅ ํฌ๊ฒŒ ๋‚˜์™€๋ดค์ž 3000์ด๋ผ๊ณ  ์ƒ๊ฐ์„ ํ–ˆ๋Š”๋ฐ, ๋‹ค์Œ๊ณผ ๊ฐ™์ด target๊ฐ’์ด ์—„์ฒญ ๋†’์•„๋ฒ„๋ฆฌ๋ฉด 0๋ณด๋‹ค ๊ฐ€๊นŒ์›Œ์ง€๋‹ˆ๊นŒ ํ‹€๋ฆฌ๊ฒŒ ๋จ -> ์ž˜๋ชป๋œ max๊ฐ’์„ค์ •
    แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2025-05-23 แ„‹แ…ฉแ„’แ…ฎ 4 08 42

var answer = Int.max

  • แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2025-05-23 แ„‹แ…ฉแ„’แ…ฎ 4 11 17
  • Int.max๋กœ ํ•ด์ฃผ๋‹ˆ๊นŒ ๊ณ„์‚ฐํ•˜๋Š” ๊ณผ์ •์— ์žˆ์–ด์„œ ์—ฌ๊ธฐ์„œ + 100 ๋งŒ ๋˜๋”๋ผ๋„ ํ„ฐ์ง!! ์ธํŠธ์ดˆ๊ณผ! ๊ทธ๋ž˜์„œ ๊ทธ์ •๋„๊นŒ์ง€ ํฐ ๊ฐ’์€ ํ”ผํ•ด์•ผํ•˜๋Š” ์ƒํ™ฉ

var answer = Int(Int32.max)

  • Int64๋ณด๋‹ค ์ ์€ Int32 Max๊ฐ’์œผ๋กœ์„œ max๊ฐ’์„ ๊ฐ€์ ธ์™€์„œ ํ„ฐ์ง€์ง€ ์•Š๊ฒŒ ์กฐ์ •ํ•ด์ฃผ์—ˆ์Œ. ๋” ๊น”๋”ํ•œ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค๋ฉด ์ถ”์ฒœ์„ ๋ฐ›์Šต๋‹ˆ๋‹ค..!
แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2025-05-23 แ„‹แ…ฉแ„’แ…ฎ 3 07 59

Copy link
Member

@alstjr7437 alstjr7437 left a comment

Choose a reason for hiding this comment

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

๋น„์Šทํ•œ ๋ฐฉ์‹์ธ๋ฐ ์•ฝ๊ฐ„์”ฉ ๋‹ค๋ฅด๋„ค์š”!!

์ €๋Š”

  1. ๊ธฐ๋ณธ ๊ฐ’์„ ๋„ฃ์Œ(0 + 1 + 2 ๋ฒˆ์งธ ์ˆ˜๋ฅผ ๋”ํ•จ)
  2. ๊ฐ ๋ถ€๋ถ„์„ ๋น„๊ตํ•ด์„œ Result ํฌ๊ธฐ์— ๋”ฐ๋ผ ๋ณ€๊ฒฝํ•จ

์œ„๊ฐ€ ํ•ต์‹ฌ ๋กœ์ง์„ ์ž‘๋™ํ–ˆ์Šต๋‹ˆ๋‹ค!!

class Solution {
    func threeSumClosest(_ nums: [Int], _ target: Int) -> Int {
        let sortedNums = nums.sorted()
        var result = sortedNums[0] + sortedNums[1] + sortedNums[2]

        for i in 0..<sortedNums.count - 2 {
            var left = i + 1
            var right = sortedNums.count - 1

            while left < right {
                let currentSum = sortedNums[i] + sortedNums[left] + sortedNums[right]

                if abs(currentSum - target) < abs(result - target) {
                    result = currentSum
                }

                if currentSum < target { left += 1} 
                else { right -= 1 }
            }
        }

        return result
    }
}

Comment on lines +7 to +9
Copy link
Member

Choose a reason for hiding this comment

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

์˜คํ˜ธ abs๋กœ target๊ณผ ๋น„๊ตํ•˜๋Š”๊ฑธ calculate? ํ•จ์ˆ˜๋กœ ๋นผ์…ง๊ตฐ์š”!
caculate๋Š” ์˜คํƒ€ ๋งž์œผ์‹ค๊นŒ์š”?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

์˜ค์šฐ .. ๊ทธ๋ ‡๋„ค์š”.. ์˜คํƒ€๊ฐ€ ๋งž์Šต๋‹ˆ๋‹ค..

Comment on lines +33 to +35
Copy link
Member

Choose a reason for hiding this comment

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

๋ฐ”๋กœ target์œผ๋กœ ํƒˆ์ถœํ•˜๊ตฐ์š”!

Comment on lines +22 to +26
Copy link
Member

Choose a reason for hiding this comment

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

print๋ฌธ์€ ๋งˆ์ง€๋ง‰ commit์—์„œ ์ œ์™ธ์‹œ์ผœ๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

๋„ค ์ฐธ๊ณ ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค ! ๐Ÿ‘

Copy link
Member

@giljihun giljihun left a comment

Choose a reason for hiding this comment

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

2์ž๋ฆฟ์ˆ˜๋Œ€ ๋ฌธ์ œ๋ฒˆํ˜ธ์˜ ๋ฆฌํŠธ์ฝ”๋“œ๋Š” ์ฒ˜์Œ ๋ณด๋Š”๊ฑฐ๊ฐ™๋„ค์š” ใ…‹ใ…‹ใ…Žใ…Ž
ํˆฌํฌ์ธํ„ฐ ๋ฆฌ๋งˆ์ธ๋“œํ•˜๊ธฐ ์ข‹์€ ๋ฌธ์ œ๋„ค์š”

์ œ ์ฝ”๋“œ๋Š” ๋ณด๋‹ˆ๊นŒ ํ‚จ๋”์ฝ”๋“œ๋ž‘ ์œ ์‚ฌํ•œ๋ฐ์š”. ๋‹ค๋ฅธ ๋ถ€๋ถ„? ํ•˜๋‚˜๋งŒ ๊ฐ€์ ธ์™”์Šต๋‹ˆ๋‹ค.

if currentSum == target { return target }

์ €๋Š” ์„ธ์ˆ˜์˜ ํ•ฉ์ด target ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’๊ณผ ์ •ํ™•ํžˆ ๊ฐ™๋‹ค๋ฉด ์ข…๋ฃŒํ•˜๋Š” ์กฐ๊ฑด์„ ์ถ”๊ฐ€ํ–ˆ๋„ค์š”!
๊ฐ™์œผ๋ฉด ๋” ๊ฐ€๊นŒ์šด ์กฐํ•ฉ์€ ์—†์œผ๋‹ˆ๊นŒ, ์ด ๊ฒฝ์šฐ ๋” ๋ณผ ํ•„์š”๊ฐ€ ์—†๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ์–ด์š”!

Copy link
Member

Choose a reason for hiding this comment

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

๋””๋ฒ„๊น… ๊ณผ์ • ํฅ๋ฏธ๋กญ๊ฒŒ ๋ดค์Šต๋‹ˆ๋‹ค ~~ ์™€์šฐ ์™„์ „ ๋”ฅ๋‹ค์ด๋น™!

์ด ๋ถ€๋ถ„์€ ํ‚จ๋” ์ฝ”๋“œ์ฒ˜๋Ÿผ ์‹ค์ œ ๊ฐ’ ๊ธฐ๋ฐ˜์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•ด์ฃผ๋Š”๊ฒŒ
๊ฐ€์žฅ ์ง๊ด€์ ์ด๊ณ  ์•ˆ์ „ํ•œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
*์ €๋„ ์ฐธ๊ณ ํ–ˆ์Šต๋‹ˆ๋‹ค.

Comment on lines +14 to +15
Copy link
Member

Choose a reason for hiding this comment

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

์ด ๋ถ€๋ถ„์€ ์ฝ”๋“œ๋Œ€๋กœ ์ •๋ ฌ์„ ์œ„ํ•ด ๋ณต์‚ฌ๋ฅผ ํ•˜์‹  ๊ฒƒ ๊ฐ™์€๋ฐ

let sortedNums = nums.sorted()

ํ•œ ์ค„๋กœ ์“ฐ๋Š”๊ฒŒ ์ข‹์•„๋ณด์ž…๋‹ˆ๋‹ค!!~

Copy link
Collaborator

@MuchanKim MuchanKim left a comment

Choose a reason for hiding this comment

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

์ฝ”๋“œ ๋ฆฌ๋ทฐํ• ๋งŒํ•œ ๊ฒƒ๋“ค์€ ๊ธธ์ด ๋‹ค ํ•ด์ฃผ์…จ๋„ค์š”. ๋””๋ฒ„๊น… ๊ณผ์ • ๋‚จ๊ฒจ์ฃผ๋Š”๊ฑฐ ์•„์ฃผ ์กฐ์•„์”๋‹ˆ๋‹ค.

๋‹ค๋“ค ํˆฌํฌ์ธํ„ฐ๋กœ ๋Œ๋ฆฌ์…จ๋Š”๋ฐ ์ „ ๋ธŒ๋ฃจํŠธํฌ์Šค๋กœ 3์ค‘ for๋ฌธ ๋•Œ๋ ค์„œ ํ’€์—ˆ์–ด์š”.

class Solution {
    func threeSumClosest(_ nums: [Int], _ target: Int) -> Int {
        // ์ดˆ๊ธฐ๊ฐ’ ์„ค์ • (์ฒซ ์„ธ ๊ฐœ์˜ ์ˆซ์ž์˜ ํ•ฉ์œผ๋กœ)
        var closestSum = nums[0] + nums[1] + nums[2]
        
        // ์„ธ ๊ฐœ์˜ ์ค‘์ฒฉ๋œ ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ๋ชจ๋“  ์กฐํ•ฉ ํ™•์ธ
        for i in 0..<nums.count-2 {
            for j in i+1..<nums.count-1 {
                for k in j+1..<nums.count {
                    let currentSum = nums[i] + nums[j] + nums[k]
                    
                    // ์ •ํ™•ํžˆ target๊ณผ ๊ฐ™๋‹ค๋ฉด ๋ฐ”๋กœ ๋ฐ˜ํ™˜
                    if currentSum == target {
                        return target
                    }
                    
                    // ํ˜„์žฌ ํ•ฉ์ด target์— ๋” ๊ฐ€๊น๋‹ค๋ฉด ์—…๋ฐ์ดํŠธ
                    if abs(target - currentSum) < abs(target - closestSum) {
                        closestSum = currentSum
                    }
                }
            }
        }
        
        return closestSum
    }
}

Comment on lines +11 to +12
Copy link
Collaborator

Choose a reason for hiding this comment

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

PR์—๋„ ์žˆ์ง€๋งŒ ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ ๋ฌธ์ œ ๋ฐœ๊ฒฌํ•˜๊ณ  ์ฃผ์„์œผ๋กœ ๋‚จ๊ฒจ์ฃผ๋Š”๊ฑฐ ์ข‹๋„ค์š”! ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์Šคํ„ฐ๋””์—์„œ ์ข‹์€ ์˜ˆ๋กœ ๋ณด์ž…๋‹ˆ๋‹ค๐Ÿ‘

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.

5 participants