Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bishoe01/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
| 2์ฐจ์‹œ | 2025.04.09 | DP | [1277. Count Square Submatrices with All Ones](https://leetcode.com/problems/count-square-submatrices-with-all-ones/description/?envType=problem-list-v2&envId=dynamic-programming)|https://github.com/AlgoLeadMe/AlgoLeadMe-14/pull/11|
| 3์ฐจ์‹œ | 2025.04.30 | DP | [1267. Count Servers that Communicate](https://leetcode.com/problems/count-servers-that-communicate/description/?envType=daily-question&envId=2025-04-29)|https://github.com/AlgoLeadMe/AlgoLeadMe-14/pull/16|
| 4์ฐจ์‹œ | 2025.05.09 | ๋ฌธ์ž์—ด | [1839. Longest Substring Of All Vowels in Order](https://leetcode.com/problems/longest-substring-of-all-vowels-in-order/description/)|https://github.com/AlgoLeadMe/AlgoLeadMe-14/pull/19|
| 5์ฐจ์‹œ | 2025.05.23 | ์ •๋ ฌ, ํˆฌํฌ์ธํ„ฐ | [16. 3Sum Closest](https://leetcode.com/problems/3sum-closest/description/)|https://github.com/AlgoLeadMe/AlgoLeadMe-14/pull/28|
---
42 changes: 42 additions & 0 deletions bishoe01/์ •๋ ฌ/3Sum Closest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import Foundation

// abs(sum - target)๊ฐ€ ๊ฐ€์žฅ ์ž‘์€์•  ์ฐพ๊ธฐ

class Solution {
func threeSumClosest(_ nums: [Int], _ target: Int) -> Int {
func caculate(_ sum: Int) -> Int {
return abs(sum - target)
}
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.

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


// var answer = 3000 // ์ตœ๋Œ€๊ฐ’ ๋ฒ”์œ„ 3๊ฐœ ๋”ํ•œ ๊ฒƒ ์œผ๋กœ ํ•ด์คฌ๋Š”๋ฐ, target์ด ๋†’์•„์„œ ์ด๋ ‡๊ฒŒ ํ•ด์ฃผ๋ฉด ํ‹€๋ฆฌ๋‹ค๊ณ  ๋œธ !
// var answer = Int.max // ํ„ฐ์ง - max๊ฐ’์— +๊ฐ€ ์กฐ๊ธˆ์ด๋ผ๋„ ๋˜๋‹ˆ๊นŒ ํŽ‘ ํ„ฐ์ ธ๋ฒ„๋ฆฌ๋Š” ์ด์Šˆ๊ฐ€ ์žˆ์Œ
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์—๋„ ์žˆ์ง€๋งŒ ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ ๋ฌธ์ œ ๋ฐœ๊ฒฌํ•˜๊ณ  ์ฃผ์„์œผ๋กœ ๋‚จ๊ฒจ์ฃผ๋Š”๊ฑฐ ์ข‹๋„ค์š”! ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์Šคํ„ฐ๋””์—์„œ ์ข‹์€ ์˜ˆ๋กœ ๋ณด์ž…๋‹ˆ๋‹ค๐Ÿ‘

var answer = Int(Int32.max) // ๊ทธ๋ž˜์„œ ๊ทธ๋‚˜๋งˆ ๊ฐ’์ด ์กฐ๊ธˆ ์ž‘์€ ํฐ๊ฐ’์œผ๋กœ ํ•ด๊ฒฐ
Copy link
Member

Choose a reason for hiding this comment

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

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

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

var nums = nums
nums.sort(by: <)
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()

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

let LEN = nums.count
for i in 0 ..< LEN - 2 {
var left = i + 1
var right = LEN - 1
while left < right {
let totalSum = nums[i] + nums[left] + nums[right]
// print(nums[i], nums[left], nums[right])
// print(totalSum)
// print(caculate(totalSum))
if caculate(totalSum) < caculate(answer) {
// print(nums[i], nums[left], nums[right])
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.

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

answer = totalSum
}
if totalSum < target {
left += 1
} else if totalSum > target {
right -= 1
} else {
return target
}
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์œผ๋กœ ํƒˆ์ถœํ•˜๊ตฐ์š”!

}
}
return answer
}
}

print(Solution().threeSumClosest([1, 1, 1, 1], -100))