Skip to content

Conversation

@alstjr7437
Copy link
Member

@alstjr7437 alstjr7437 commented Apr 30, 2025

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

๋จน์„ ๊ฒƒ์ธ๊ฐ€ ๋จนํž ๊ฒƒ์ธ๊ฐ€

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

15๋ถ„






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

์‹œ๊ฐ„๋ณต์žก๋„๋กœ ํ—ˆ๋•์ผ ์ค„ ์•Œ๊ณ  ๋ฌธ์ œ๋ฅผ ๊ณจ๋ž๋Š”๋ฐ ์ผ๋‹จ ์ œ์ถœํ•˜๊ณ  ๋ณด์ž๋ผ๊ณ  ์ œ์ถœํ–ˆ๋Š”๋ฐ ๋งž์•„๋ฒ„๋ ธ์–ด์š”;;

ํˆฌํฌ์ธํ„ฐ, ์ •๋ ฌ ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค!!



์ •๋ ฌ!

์šฐ์„  ๋ฐฐ์—ด๋“ค์„ sort, sorted๋กœ ์ •๋ ฌ์„ ํ•ฉ๋‹ˆ๋‹ค!



ํˆฌํฌ์ธํ„ฐ๋กœ ๊ฐ’ ์ฐพ๊ธฐ!

  • ์•ž์ชฝ์— x๋Š” ๋” ์ž‘์€๊ฒŒ ์—†์—ˆ๋˜ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.
  • 3์€ 1๋ณด๋‹ค ํฝ๋‹ˆ๋‹ค.
    • ๊ฒฐ๊ณผ ๊ฐ’ + 1
    • point = 1
  • 7์€ ์ด์ „ ๋‚ด์šฉ์ธ 3๋ณด๋‹ค ํฌ๋ฏ€๋กœ point๋ฅผ ๋”ํ•˜๊ณ  ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.
    • ๋น„๊ตํ•  ๋ฐฐ์—ด์— 3๋ณด๋‹ค ํฌ๋‹ˆ ๊ฒฐ๊ณผ ๊ฐ’ + 1, point = 2
  • 8์€ ์•ž์— 7๋ณด๋‹ค ํฌ๋‹ˆ point์ธ 2๋ฅผ ๋”ํ•˜๊ณ  ์‹œ์ž‘
    • 6๋ณด๋‹ค ํฌ๋‹ˆ ๊ฒฐ๊ณผ ๊ฐ’ + 1
image



๊ฒฐ๋ก 

์œ„์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์ง„ํ–‰ํ•˜๊ฒŒ ๋˜๋ฉด
์›๋ž˜ ๋ชจ๋“  ๋ฐฐ์—ด์„ ๋น„๊ตํ•ด์•ผํ–ˆ์ง€๋งŒ

์•ž์ „์— ์‚ฌ์šฉํ–ˆ๋˜ ๋น„๊ต์—ฐ์‚ฐ์„ ํ†ตํ•ด์„œ ๋ถˆํ•„์š”ํ•œ ์—ฐ์‚ฐ(์ด๋ฏธ ๋”์ž‘์€๊ฑด ๋น„๊ตํ•  ํ•„์š” โŒ)์„ ์ œ๊ฑฐํ•ด์คฌ์Šต๋‹ˆ๋‹ค!




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

Swift์—์„œ Sort์™€ Sorted

Sort
๋ฐฐ์—ด์˜ ์ž๋ฆฌ๋“ค์„ ๋ณ€๊ฒฝํ•ด์„œ ์›๋ณธ์˜ ๊ฐ’๋“ค์„ ๋ณ€๊ฒฝํ•˜๊ธฐ ๋•Œ๋ฌธ์— Mutableํ•ด์•ผํ•จ

Sorted
์ •๋ ฌ๋œ ๋ฐฐ์—ด์„ ๋ฆฌํ„ดํ•ด์„œ ๋ณต์ œ๋œ ์ •๋ ฌ ๊ฐ’์„ ๊ฐ€์ ธ์˜ด

๋‘˜๋‹ค O(n logn)

์›๋ณธ ๋ฐฐ์—ด ๋ณด์กด์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ โžก๏ธ Sorted()
ํ•„์š” ์—†๋Š” ๊ฒฝ์šฐ โžก๏ธ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์„ ๋‘๋ฐฐ๋กœ ๋Š˜๋ฆฌ๊ธฐ ๋–„๋ฌธ์— โžก๏ธ Sort()

๊ทธ๋ž˜์„œ ํ™•์ธํ•ด๋ณด๋‹ˆ ๋ง‰ ์ƒ๊ฐ๋ณด๋‹ค ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋ฅ ์— ๋“œ๋ผ๋งˆํ‹ฑํ•œ ๋ณ€ํ™”๋Š” ์—†์—ˆ์Œ..
image
(์œ„๊ฐ€ sort, ์•„๋ž˜๊ฐ€ sorted)

* feat: 1์ฐจ์‹œ ๋ฌธ์ œ

* 1์ฐจ์‹œ ๋ฌธ์ œ ํ•ด๊ฒฐ

* 2์ฐจ์‹œ ๋ฌธ์ œ ํ•ด๊ฒฐ

* refactor: swapAt ์ถ”๊ฐ€
Comment on lines +10 to +18
for i in A {
result += point
for j in point..<B.count {
if i > B[j] {
point = j + 1
result += 1
}
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

์™€ ์—„์ฒญ ๊น”๋”ํ•˜๋„ค์š”.. ๐Ÿ‘ ์ €๋Š” ์ข€ ๊ณ ๊ตฐ๋ถ„ํˆฌํ•˜๋‹ค๋ณด๋‹ˆ๊นŒ ๊ฐ™์€ ๋กœ์ง์ธ๋ฐ๋„ ์ข€ ๋‚œ์žกํ•œ ๊ฐ์ด ์žˆ๋„ค์š”..
๋ฐฐ์›Œ๊ฐ‘๋‹ˆ๋‹ท

Copy link
Collaborator

Choose a reason for hiding this comment

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

ํ•€์Œค ์ปค์„œ ์“ฐ์…”์„œ ๊ทธ๋Ÿฐ์ง€ ๋ณ€์ˆ˜๋ช…์„ ์ปค์„œ๋กœ ํ•˜์…จ๊ตฐ์š”.
aItem ํ˜น์€ bIdx ๊ฐ™์€ ๋ณ€์ˆ˜๋ช…์ด๋‚˜ ์ธ๋ฑ์Šค ๋ช…๋“ค์ด ์ €๋Š” ๊ฐ€๋” ๊ฐ€๋…์„ฑ์ด ๋‚ฎ๋‹ค๊ณ  ํŒ๋‹จ๋  ๋•Œ๊ฐ€ ์žˆ๋Š”๋ฐ ํ•€์Œค๊ณผ ํ‚จ๋”์Œค ํ˜น์€ ์—ฌ๋Ÿฌ๋ถ„๋“ค์€ ์–ด๋– ์‹ ๊ฐ€์š” ?

Copy link
Member Author

Choose a reason for hiding this comment

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

๋งž์•„์š” ํ•œ๋ฒˆ์”ฉ ํ–‡๊ฐˆ๋ ค์„œ ๋‹ค๋ฅธ ์ข‹์€ ๋ณ€์ˆ˜๋ช… ์ฐพ์œผ๋ฉด์„œ ๊ณ ๋ฏผํ•˜๋‹ค๊ฐ€ ๊ฒฐ๊ตญ ์ต์ˆ™ํ•œ ๊ณณ์œผ๋กœ ๋Œ์•„์˜ค๊ฒŒ ๋˜๋”๋ผ๊ตฌ์š” ใ… ..

Copy link
Collaborator

@bishoe01 bishoe01 left a 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๋ž‘ ๊ฐ™์€ ์—ญํ• ์ผ ๊ฒ๋‹ˆ๋‹ค ..!

๋ฌธ์ œ๋ณด๊ณ  ์ง๊ด€์ ์œผ๋กœ ํ’€์–ด๋„ ๋˜๊ฒ ์ง€~ํ–ˆ๋‹ค๊ฐ€ ์‹œ๊ฐ„์ดˆ๊ณผ๋– ์„œ ํ•œ ๋ฒˆ ์ƒ๊ฐํ•˜๊ฒŒ ํ•ด์ค€ ๋ฌธ์ œ๊ฐ™์Šต๋‹ˆ๋‹ค.!

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.

image

์ผ๋‹จ ์™„ํƒ์œผ๋กœ ๋“ค์ด๋ฐ•์•˜๋‹ค๊ฐ€ ๋ฐ”๋กœ ์‹œ๊ฐ„์ดˆ๊ณผ ๋– ์„œ ์ด๋ถ„ํƒ์ƒ‰์œผ๋กœ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค. ํˆฌํฌ์ธํ„ฐ๋Š” ํ™œ์šฉ์„ ํ•ด๋ณธ์ ์ด ์—†์–ด์„œ ์ด์ฐธ์— ํˆฌํฌ์ธํ„ฐ๋กœ ํ’€์–ด๋ณด๋“  ํ•ด์•ผ๊ฒ ์–ด์š”!

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์ดˆ์ด๋‹ˆ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋‚  ์ˆ˜ ๋ฐ–์— ์—†์—ˆ๊ตฐ์š”. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

ํ˜น์‹œ ์ด ๋ถ€๋ถ„ ์ œ๊ฐ€ ์˜คํ•ดํ•˜๊ณ  ์žˆ๋Š”๊ฒŒ ์žˆ๋‹ค๋ฉด ๋‹ต๊ธ€ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค!!

Comment on lines +8 to +15
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
Copy link
Collaborator

Choose a reason for hiding this comment

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

???? ์ด๋ ‡๊ฒŒ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ํ’€ ์ˆ˜๋„ ์žˆ๊ตฐ์š”... ํˆฌํฌ์ธํ„ฐ๋ฅผ ํ™œ์šฉํ•ด๋ณธ ์ ์ด ์—†์–ด์„œ ์ด๋ถ„ํƒ์ƒ‰์œผ๋กœ ํ–ˆ๋Š”๋ฐ ์ž˜ ๋ฐฐ์›Œ๊ฐ‘๋‹ˆ๋‹ค!

Copy link
Collaborator

@YooGyeongMo YooGyeongMo May 6, 2025

Choose a reason for hiding this comment

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

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2025-05-06 แ„‹แ…ฉแ„’แ…ฎ 5 22 48

์ฑ— ์‰จํ•œํ…Œ ์ €๋„ ๋ฌด๊บผ ๋ณด๊ณ  ์ •๋ฆฌํ•ด๋‹ฌ๋ผํ–ˆ๋Š”๋ฐ ํ•ญ์ƒ ์ž…๋ ฅ๊ฐ’ ๋ณด๊ณ  ์™„ํƒ์„ ํ• ์ง€ ์•ˆํ• ์ง€ ๋”ฑ ์•Œ๋ ค์คฌ์Œํ•ด์„œ ์ฐธ๊ณ ์ž๋ฃŒ ์ฒจ๋ถ€ํ•ด๋ด…๋‹ˆ๋‹ค ํ•˜์ง€๋งŒ ์ทŸ์‰‘์ด๋ผ์„œ ๊ฒ€์ฆ์ด ์•ˆ๋œ๊ฒƒ์ด๋‹ˆ ์ €ํฌ๋ผ๋ฆฌ ์‹œํ–‰์ฐฉ์˜ค๋ฅผ ๊ฒช์–ด๊ฐ€๋ฉฐ ์ข€ ๋” ์—…๋ฐ์ดํŠธํ•ด๋ณด๋Š”๊ฒƒ๋„ ์ข‹์•„๋ณด์ž…๋‹ˆ๋‹ค.

Copy link
Member Author

Choose a reason for hiding this comment

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

2๋งŒ์ด ๋ณด์ด๋ฉด ์™„ํƒ์ด ๋ญ”๊ฐ€ ์•ˆ๋œ๋‹ค๋Š” ์••๋ฐ•์ด ๋А๊ปด์ ธ์„œ ํ–ˆ๋Š”๋ฐ ์ž˜ ๋œ ๊ฒƒ ๊ฐ™๋„ค์š” ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!

@sep037
Copy link
Collaborator

sep037 commented May 6, 2025

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2025-05-06 10 52 30

์‹œ๊ฐ„ ์ดˆ๊ณผ 22์ž…์ž…๋‹ˆ๋‹ค . . ์ €๋„ ๊ทธ๋ƒฅ ๋•Œ๋ ค ๋ฐ•์€ ์ฝ”๋“œ๋ž‘ ํ‚จ๋” ํžŒํŠธ ์–ป๊ณ  ํˆฌํฌ์ธํ„ฐ๋กœ ํ’€์–ด์™”์–ด์š” !

//
//  main.swift
//  7795
//
//  Created by Seungeun Park on 5/5/25.
//

import Foundation


let N = Int(readLine()!)! // ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ์ˆ˜

for _ in 0 ..< N {
    let counts = readLine()!.split(separator: " ").map { Int($0)! }
    let countA = counts[0]
    let countB = counts[1]
    
    let Aarray = readLine()!.split(separator: " ").map { Int($0)! }
    let Barray = readLine()!.split(separator: " ").map { Int($0)! }

    print(comparison(A: Aarray, B: Barray))
}

//func comparison(A: [Int], B: [Int]) -> Int {
//    var count = 0
//    for a in A {
//        for b in B {
//            if a > b {
//                count += 1
//            }
//        }
//    }
//    return count
//}
// ์‹œ๊ฐ„ ์ดˆ๊ณผ ใ…‹ใ…‹

func comparison(A: [Int], B: [Int]) -> Int {
    let sortedA = A.sorted()
    let sortedB = B.sorted() // ์ผ๋‹จ ์ •๋ ฌ
    var count = 0
    
    var j = 0

    for a in sortedA {
        while j < sortedB.count && sortedB[j] < a {
            j += 1
        }
        count += j
    }
    return count
}


Copy link
Collaborator

@YooGyeongMo YooGyeongMo left a comment

Choose a reason for hiding this comment

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

์•ˆ๋ƒ์„ธ์š” ํ‚จ๋” ์ €๋Š” ์ด์ง„ํƒ์ƒ‰์œผ๋กœ ํ’€์—ˆ๋‹ด๋ 

Comment on lines +10 to +20
for i in A {
result += point
for j in point..<B.count {
if i > B[j] {
point = j + 1
result += 1
}
}
}
print(result)
}
Copy link
Collaborator

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
}

์ง์ ‘ ๋น„๊ตํ•˜๋‹ˆ ๋” ์ข‹๊ตฐ์š” ! ํˆฌ ํฌ์ธํ„ฐ๋„ ์—ฐ์Šตํ•ด์•ผ๊ฒ ์–ด์š” !

Copy link
Member Author

Choose a reason for hiding this comment

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

์˜คํ˜ธ ์ด์ง„ ํƒ์ƒ‰์€ ์ƒ๊ฐ ๋ชปํ–ˆ์—ˆ๋Š”๋ฐ ์ด์ง„ ํƒ์ƒ‰์œผ๋กœ ํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๋„ค์š” ์ข‹๋„ค์š”!

Copy link
Collaborator

Choose a reason for hiding this comment

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

@alstjr7437 ๊ณ ์ˆ˜์˜ ํ–ฅ๊ธฐ ํ‚จ๋” .... ๊ทธ์˜ ์ฝ”๋“œ๋Š” ํด๋ฆฐ์ฝ”๋“œ์˜€๋‹ค... ์บฌ ~

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.

ํˆฌํฌ์ธํ„ฐ ๋ฌธ์ œ ์™„์ „ ์˜ค๋žœ๋งŒ์ด๋ผ ์ข‹์•˜์Šต๋‹ˆ๋‹ค!!!
๋‹ค๋ฅธ ๋ถ„๋“ค ์ฝ”๋“œ๋„ ๋ณด๋‹ˆ๊นŒ ๋ฐ”์ด๋„ˆ๋ฆฌ์„œ์น˜๋กœ ํ’€ ์ˆ˜๋„ ์žˆ๊ฒ ๋„ค์š”

/* 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])
}

Comment on lines +1 to +6
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()
Copy link
Member

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))
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

@giljihun ๊ธธ!!!!! ์บฌ ...... AandB ์นด๋ฉœํˆฌํƒ‘๋ด‰์˜ค๋ฆฌ์ผ€์ด์Šจ๊ฐ€์˜ˆ ์ง์ด๋„ค์˜ˆ !

compactmap ํ™œ์šฉ๊นŒ์ง€ ... ์™€์šฐ !! ์™€ ๊ทผ๋ฐ ํˆฌํฌ์ธํ„ฐ ์ƒ๊ฐ์€ ํ•ด๋„ ๋ฐ”๋กœ ๊ตฌํ˜„์ด ์‰ฝ์ง€ ์•Š์€๋ฐ ๋ฉ‹์ง‘๋‹ˆ๋‹ค

@alstjr7437 alstjr7437 merged commit 3028619 into main May 9, 2025
5 checks passed
@alstjr7437 alstjr7437 deleted the 3-alstjr7437 branch May 23, 2025 06:13
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.

7 participants