-
Notifications
You must be signed in to change notification settings - Fork 0
3-alstjr7437 #17
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
3-alstjr7437 #17
Conversation
* feat: 1์ฐจ์ ๋ฌธ์ * 1์ฐจ์ ๋ฌธ์ ํด๊ฒฐ * 2์ฐจ์ ๋ฌธ์ ํด๊ฒฐ * refactor: swapAt ์ถ๊ฐ
| for i in A { | ||
| result += point | ||
| for j in point..<B.count { | ||
| if i > B[j] { | ||
| point = j + 1 | ||
| result += 1 | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ ์์ฒญ ๊น๋ํ๋ค์.. ๐ ์ ๋ ์ข ๊ณ ๊ตฐ๋ถํฌํ๋ค๋ณด๋๊น ๊ฐ์ ๋ก์ง์ธ๋ฐ๋ ์ข ๋์กํ ๊ฐ์ด ์๋ค์..
๋ฐฐ์๊ฐ๋๋ท
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ํ์ค ์ปค์ ์ฐ์
์ ๊ทธ๋ฐ์ง ๋ณ์๋ช
์ ์ปค์๋ก ํ์
จ๊ตฐ์.
aItem ํน์ bIdx ๊ฐ์ ๋ณ์๋ช
์ด๋ ์ธ๋ฑ์ค ๋ช
๋ค์ด ์ ๋ ๊ฐ๋ ๊ฐ๋
์ฑ์ด ๋ฎ๋ค๊ณ ํ๋จ๋ ๋๊ฐ ์๋๋ฐ ํ์ค๊ณผ ํจ๋์ค ํน์ ์ฌ๋ฌ๋ถ๋ค์ ์ด๋ ์ ๊ฐ์ ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ง์์ ํ๋ฒ์ฉ ํ๊ฐ๋ ค์ ๋ค๋ฅธ ์ข์ ๋ณ์๋ช ์ฐพ์ผ๋ฉด์ ๊ณ ๋ฏผํ๋ค๊ฐ ๊ฒฐ๊ตญ ์ต์ํ ๊ณณ์ผ๋ก ๋์์ค๊ฒ ๋๋๋ผ๊ตฌ์ ใ ..
bishoe01
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import Foundation
func ๋จน์๊ฒ๋จนํ๊ฒ(_ arrayA: [Int], _ arrayB: [Int]) -> Int {
var answer = 0
var cursor = 0
var tmpAnswer = 0
for aItem in arrayA {
for bIdx in cursor ..< arrayB.count {
let target = arrayB[bIdx]
if aItem > target { // A๊ฐ ๋ ํด ๋ ! ์นด์ดํ
ํด์ค์ผ๋จ !
tmpAnswer += 1
cursor += 1
}
else { // ์ด์ ๋ช
๋งฅ์ด ๋๊ธด ๊ฒ
break
}
}
answer += tmpAnswer
}
return answer
}tmpAnswer์๋ค๊ฐ ๊ฐ์ ์ ์ฅํด๋๊ณ , answer์ ์ถ๊ฐํ๋์์ผ๋ก ํด์คฌ๋๋ฐ,
ํจ๋์ ์๋์ด ์ฐ์ ์ฝ๋์์๋ point๋ ๊ฐ์ ์ญํ ์ผ ๊ฒ๋๋ค ..!
๋ฌธ์ ๋ณด๊ณ ์ง๊ด์ ์ผ๋ก ํ์ด๋ ๋๊ฒ ์ง~ํ๋ค๊ฐ ์๊ฐ์ด๊ณผ๋ ์ ํ ๋ฒ ์๊ฐํ๊ฒ ํด์ค ๋ฌธ์ ๊ฐ์ต๋๋ค.!
MuchanKim
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ผ๋จ ์ํ์ผ๋ก ๋ค์ด๋ฐ์๋ค๊ฐ ๋ฐ๋ก ์๊ฐ์ด๊ณผ ๋ ์ ์ด๋ถํ์์ผ๋ก ํ์์ต๋๋ค. ํฌํฌ์ธํฐ๋ ํ์ฉ์ ํด๋ณธ์ ์ด ์์ด์ ์ด์ฐธ์ ํฌํฌ์ธํฐ๋ก ํ์ด๋ณด๋ ํด์ผ๊ฒ ์ด์!
B๋ฐฐ์ด๋ง ์ํ ํด์ฃผ๊ณ ์ํํ ๋ target๋ณด๋ค ์์ ๊ฐ์ฅ ํฐ ์์์ ์์น(์ธ๋ฑ์ค)๋ฅผ ์ฐพ๋ ๋ฐฉํฅ์ผ๋ก ๊ตฌํํ์ด์! ๊ทธ๋ฆฌ๊ณ ๋ฆฌํดํ ๋๋ +1์ ํด์ค์ ํ๊ฒ๋ณด๋ค ์์ ์์์ ๊ฐ์๋ฅผ ๊ตฌํด์คฌ์ด์! ์ด๋ฐ ๋ฐฉ์์ผ๋ก A์ ๊ฐ ์์์ ๋์ํ๋ B์์ ์์ ๊ฐ๋ค์ ๊ตฌํ๋ต๋๋ค..ใ ใ
import Foundation
func binarySearch(_ arr: [Int], _ target: Int) -> Int {
var left = 0
var right = arr.count - 1
var res = -1
while left <= right {
let mid = (left + right) / 2
if arr[mid] < target {
res = mid
left = mid + 1
} else {
right = mid - 1
}
}
return res + 1
}
let cycle = Int(readLine()!)!
for _ in 0..<cycle {
_ = readLine()!.split(separator: " ").map { Int($0)! }
let A = readLine()!.split(separator: " ").map { Int($0)! }
let B = readLine()!.split(separator: " ").map { Int($0)! }.sorted()
var res = 0
for a in A {
res += binarySearch(B, a)
}
print(res)
}์ํ์ผ๋ก ํ๋ฉด ์๊ฐ ์ด๊ณผ๋๋ ์ด์ ~~
N,M์ ์ต๋ 20000 ํฌ๊ธฐ๋ฅผ ๊ฐ์ง ์ ์์์.
์ฆ, O(N*M)์ด ๊ฐ์ง๋ ์ต์
์ ์ฐ์ฐ ํ์๋ 20000x20000 =400,000,000(4์ต)๋ฒ์ด ๋ฉ๋๋ค..
์ปดํธํ๊ฐ 1์ด์ 1์ต~10์ตํ ์ฐ์ฐ์ ํ๋ค๊ณ ํ๋๊ตฐ์. ๊ทธ๋ ๋ค๋ฉด 0.4์ด ~ 4์ด๊น์ง ๊ฑธ๋ฆฌ๊ฒ ๋ฉ๋๋ค.
์ด ๋ฌธ์ ์์ ์ ํ ์๊ฐ์ 1์ด์ด๋ ์๊ฐ ์ด๊ณผ๊ฐ ๋ ์ ๋ฐ์ ์์๊ตฐ์. ๊ฐ์ฌํฉ๋๋ค.
ํน์ ์ด ๋ถ๋ถ ์ ๊ฐ ์คํดํ๊ณ ์๋๊ฒ ์๋ค๋ฉด ๋ต๊ธ ๋ถํ๋๋ฆฝ๋๋ค!!
| var point = 0 | ||
| var result = 0 | ||
| for i in A { | ||
| result += point | ||
| for j in point..<B.count { | ||
| if i > B[j] { | ||
| point = j + 1 | ||
| result += 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
???? ์ด๋ ๊ฒ ๊ฐ๊ฒฐํ๊ฒ ํ ์๋ ์๊ตฐ์... ํฌํฌ์ธํฐ๋ฅผ ํ์ฉํด๋ณธ ์ ์ด ์์ด์ ์ด๋ถํ์์ผ๋ก ํ๋๋ฐ ์ ๋ฐฐ์๊ฐ๋๋ค!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ฑ ์จํํ ์ ๋ ๋ฌด๊บผ ๋ณด๊ณ ์ ๋ฆฌํด๋ฌ๋ผํ๋๋ฐ ํญ์ ์ ๋ ฅ๊ฐ ๋ณด๊ณ ์ํ์ ํ ์ง ์ํ ์ง ๋ฑ ์๋ ค์คฌ์ํด์ ์ฐธ๊ณ ์๋ฃ ์ฒจ๋ถํด๋ด ๋๋ค ํ์ง๋ง ์ท์์ด๋ผ์ ๊ฒ์ฆ์ด ์๋๊ฒ์ด๋ ์ ํฌ๋ผ๋ฆฌ ์ํ์ฐฉ์ค๋ฅผ ๊ฒช์ด๊ฐ๋ฉฐ ์ข ๋ ์ ๋ฐ์ดํธํด๋ณด๋๊ฒ๋ ์ข์๋ณด์ ๋๋ค.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2๋ง์ด ๋ณด์ด๋ฉด ์ํ์ด ๋ญ๊ฐ ์๋๋ค๋ ์๋ฐ์ด ๋๊ปด์ ธ์ ํ๋๋ฐ ์ ๋ ๊ฒ ๊ฐ๋ค์ ๊ฐ์ฌํฉ๋๋ค!
YooGyeongMo
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์๋์ธ์ ํจ๋ ์ ๋ ์ด์งํ์์ผ๋ก ํ์๋ด๋
| for i in A { | ||
| result += point | ||
| for j in point..<B.count { | ||
| if i > B[j] { | ||
| point = j + 1 | ||
| result += 1 | ||
| } | ||
| } | ||
| } | ||
| print(result) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ฝ๋๊ฐ ๋๊ฒ ๊น๋ํ๋ค์ ,, ํฌ์ธํฐ๊ฐ ๋๊ฐ๋ก ๋๋ฉด A๋งํผ B๋งํผ ๊ฐ๊ฐ ์๊ฐ ๋ณต์ก๋ ๊ฐ์ง๋ O(N+M)๋งํผ ๊ฐ์ง๋๊ฒ ๋ง์๊น์ ???
์์๋ณด๋ sorted๋ N(LogN) ์ด๋ค์!!
๊ฒฐ๊ตญ ์ํ์์ ๋ฐ์ ํด์ ํฌํฌ์ธํฐ๋ก ํ๋ฉด ๋๋ ๋ฌธ์ ์๊ตฐ์ ! ์ ๋ ์ด์ง ํ์์ผ๋ก ํ์์ต๋๋ค !!
import Foundation
let T = Int(readLine()!)!
for _ in 0..<T {
let nm = readLine()!.split(separator: " ").map {Int($0)!}
let N = nm[0], M = nm[1]
let A = readLine()!.split(separator:" ").map { Int($0)!}
let B = readLine()!.split(separator:" ").map { Int($0)!}.sorted()
var count = 0
for found in A {
count += check(B, found)
}
print(count)
}
// 8 1 7 3 1
// 1 3 6
// 0 < 3
// mid = 1
// arr[1] = 3 < 4
// left = 2
// 2 < 3
// mid = 2
// arr[2] 6 < 4
// right = 2
// left == 2 , right == 2
// left -> 2
func check(_ arr: [Int], _ target: Int) -> Int {
var left = 0
var right = arr.count
while left < right {
let mid = (left + right) / 2
if arr[mid] < target {
left = mid + 1
} else {
right = mid
}
}
return left
}
์ง์ ๋น๊ตํ๋ ๋ ์ข๊ตฐ์ ! ํฌ ํฌ์ธํฐ๋ ์ฐ์ตํด์ผ๊ฒ ์ด์ !
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์คํธ ์ด์ง ํ์์ ์๊ฐ ๋ชปํ์๋๋ฐ ์ด์ง ํ์์ผ๋ก ํ๋ ๋ฐฉ๋ฒ๋ ์๋ค์ ์ข๋ค์!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@alstjr7437 ๊ณ ์์ ํฅ๊ธฐ ํจ๋ .... ๊ทธ์ ์ฝ๋๋ ํด๋ฆฐ์ฝ๋์๋ค... ์บฌ ~
giljihun
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ํฌํฌ์ธํฐ ๋ฌธ์ ์์ ์ค๋๋ง์ด๋ผ ์ข์์ต๋๋ค!!!
๋ค๋ฅธ ๋ถ๋ค ์ฝ๋๋ ๋ณด๋๊น ๋ฐ์ด๋๋ฆฌ์์น๋ก ํ ์๋ ์๊ฒ ๋ค์
/* MARK: - SOLUTION
A๋ B๋ฅผ ๋จน์ด.
A๋ B๋ฅผ ์๊ธฐ๋ณด๋ค ํฌ๊ธฐ๊ฐ ์์์ผ ๋จน์ด.
A = {8, 1, 7, 3, 1}
B = {3, 6, 1} ์ด๋ฉด,
8 - 3, 8 - 6, 8 - 1
7 - 3, 7 - 6, 7 - 1
3 - 1
=> 7๊ฐ์ง
์ด๋ ๊ฒ ๋ช๊ฐ์ง์ธ์ง ๊ตฌํ์์ค~
์กฐ๊ฑด : 1 โค N, M โค 20,000)
-> ์ต์
4์ต๋ฒ ๊ฒ์ฌ;;
์ผ๋จ.. ๋ค ์ ๋ ฌ์ํค๊ณ , ํฌํฌ์ธํฐํ์
*/
import Foundation
guard let T = Int(readLine() ?? "") else { exit(0) }
var result: [Int] = []
// ํ
์คํธ ์ผ์ด์ค ์
๋ ฅ ๋ฐ๊ธฐ
(0..<T).forEach { caseNumber in
guard let AandB = readLine()?.split(separator: " "),
let N = Int(AandB[0]),
let M = Int(AandB[1]) else { exit(0) }
guard let lineA = readLine() else { exit(0) }
let A = lineA.split(separator: " ").compactMap { Int($0) }
guard A.count == N else { exit(0) }
guard let lineB = readLine() else { exit(0) }
let B = lineB.split(separator: " ").compactMap { Int($0) }
guard B.count == M else { exit(0) }
result.append(eatPairs(A, B))
}
// ๋จน๋์ ๊ตฌํ๊ธฐ
func eatPairs(_ A: [Int], _ B: [Int]) -> Int {
let sortedA = A.sorted()
let sortedB = B.sorted()
var count = 0
var bIndex = 0
for a in sortedA {
while bIndex < sortedB.count && sortedB[bIndex] < a {
bIndex += 1
}
count += bIndex
}
return count
}
// ์ถ๋ ฅ
(0..<T).forEach { caseNumber in
print(result[caseNumber])
}| let T = Int(readLine()!)! | ||
|
|
||
| for _ in 0..<T { | ||
| let NM = readLine()!.split(separator: " ").map { Int($0)! } | ||
| let A = readLine()!.split(separator: " ").map { Int($0)! }.sorted() | ||
| let B = readLine()!.split(separator: " ").map { Int($0)! }.sorted() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์๋ ํ์ธ์ ์ ์ ๋ ฅ๋ถ๋ ํ๊ฐํด์ฃผ์ธ์~
import Foundation
guard let T = Int(readLine() ?? "") else { exit(0) }
var result: [Int] = []
// ํ
์คํธ ์ผ์ด์ค ์
๋ ฅ ๋ฐ๊ธฐ
(0..<T).forEach { caseNumber in
guard let AandB = readLine()?.split(separator: " "),
let N = Int(AandB[0]),
let M = Int(AandB[1]) else { exit(0) }
guard let lineA = readLine() else { exit(0) }
let A = lineA.split(separator: " ").compactMap { Int($0) }
guard A.count == N else { exit(0) }
guard let lineB = readLine() else { exit(0) }
let B = lineB.split(separator: " ").compactMap { Int($0) }
guard B.count == M else { exit(0) }
result.append(eatPairs(A, B))
}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@giljihun ๊ธธ!!!!! ์บฌ ...... AandB ์นด๋ฉํฌํ๋ด์ค๋ฆฌ์ผ์ด์จ๊ฐ์ ์ง์ด๋ค์ !
compactmap ํ์ฉ๊น์ง ... ์์ฐ !! ์ ๊ทผ๋ฐ ํฌํฌ์ธํฐ ์๊ฐ์ ํด๋ ๋ฐ๋ก ๊ตฌํ์ด ์ฝ์ง ์์๋ฐ ๋ฉ์ง๋๋ค



๐ ๋ฌธ์ ๋งํฌ
๋จน์ ๊ฒ์ธ๊ฐ ๋จนํ ๊ฒ์ธ๊ฐ
โ๏ธ ์์๋ ์๊ฐ
15๋ถ
โจ ์๋ ์ฝ๋
์๊ฐ๋ณต์ก๋๋ก ํ๋์ผ ์ค ์๊ณ ๋ฌธ์ ๋ฅผ ๊ณจ๋๋๋ฐ ์ผ๋จ ์ ์ถํ๊ณ ๋ณด์๋ผ๊ณ ์ ์ถํ๋๋ฐ ๋ง์๋ฒ๋ ธ์ด์;;
ํฌํฌ์ธํฐ, ์ ๋ ฌ ๋ฌธ์ ์์ต๋๋ค!!
์ ๋ ฌ!
์ฐ์ ๋ฐฐ์ด๋ค์ sort, sorted๋ก ์ ๋ ฌ์ ํฉ๋๋ค!
ํฌํฌ์ธํฐ๋ก ๊ฐ ์ฐพ๊ธฐ!
๊ฒฐ๋ก
์์ ๊ฐ์ ๋ฐฉ์์ผ๋ก ์งํํ๊ฒ ๋๋ฉด
์๋ ๋ชจ๋ ๋ฐฐ์ด์ ๋น๊ตํด์ผํ์ง๋ง
์์ ์ ์ฌ์ฉํ๋ ๋น๊ต์ฐ์ฐ์ ํตํด์ ๋ถํ์ํ ์ฐ์ฐ(์ด๋ฏธ ๋์์๊ฑด ๋น๊ตํ ํ์ โ)์ ์ ๊ฑฐํด์คฌ์ต๋๋ค!
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ
Swift์์ Sort์ Sorted
๋๋ค O(n logn)
์๋ณธ ๋ฐฐ์ด ๋ณด์กด์ด ํ์ํ ๊ฒฝ์ฐ โก๏ธ Sorted()
ํ์ ์๋ ๊ฒฝ์ฐ โก๏ธ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋์ ๋๋ฐฐ๋ก ๋๋ฆฌ๊ธฐ ๋๋ฌธ์ โก๏ธ Sort()
๊ทธ๋์ ํ์ธํด๋ณด๋ ๋ง ์๊ฐ๋ณด๋ค ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋ฅ ์ ๋๋ผ๋งํฑํ ๋ณํ๋ ์์์..

(์๊ฐ sort, ์๋๊ฐ sorted)