Skip to content

Conversation

@MuchanKim
Copy link
Collaborator

@MuchanKim MuchanKim commented Apr 29, 2025

πŸ”— 문제 링크

μ•Œλ°”μƒ κ°•ν˜Έ

(κΈ°μ‘΄ λ¬Έμ œμ—μ„œ λ³€κ²½ν–ˆμ–΄μš”!)
image

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

30λΆ„+@

✨ μˆ˜λ„ μ½”λ“œ

κ·Έλ¦¬λ””λ‘œ ν’€μ—ˆμŠ΅λ‹ˆλ‹€.

처음 문제λ₯Ό 보고 μŠ€νƒ€λ°•μŠ€ μ†λ‹˜λ“€μ˜ 팁 계산 방법(μ›λž˜ 팁 - λŒ€κΈ° μ‹œκ°„)κ³Ό κ°•ν˜Έκ°€ μ†λ‹˜ μˆœμ„œλ₯Ό λ§ˆμŒλŒ€λ‘œ μ •ν•  수 μžˆλ‹€λŠ” 점이 μ€‘μš”ν•œ 포인트둜 λ΄€μ–΄μš”. λŒ€κΈ° μ‹œκ°„μ΄ κΈΈμ–΄μ§ˆμˆ˜λ‘ νŒμ€ κ°μ†Œν•˜λ―€λ‘œ, 팁이 큰 μ†λ‹˜μ„ λ¨Όμ € μ²˜λ¦¬ν•˜λ©΄ λŒ€κΈ° μ‹œκ°„μ— λ”°λ₯Έ 손싀을 μ΅œμ†Œν™”ν•  수 있죠. 즉, νŒμ„ λ‚΄λ¦Όμ°¨μˆœμœΌλ‘œ μ •λ ¬ν•˜μ—¬ μ²˜λ¦¬ν•΄μ€¬μ–΄μš”! = 그리디

  1. μ†λ‹˜ 수 N을 μž…λ ₯λ°›λŠ”λ‹€.
  2. 각 μ†λ‹˜μ˜ νŒμ„ μž…λ ₯λ°›μ•„ 배열에 μ €μž₯ν•œλ‹€.
  3. νŒμ„ λ‚΄λ¦Όμ°¨μˆœμœΌλ‘œ μ •λ ¬ν•œλ‹€.
  4. μ •λ ¬λœ μˆœμ„œλŒ€λ‘œ μ†λ‹˜μ„ μ²˜λ¦¬ν•˜λ©° μ΅œμ’… νŒμ„ κ³„μ‚°ν•œλ‹€:
    • i번째 μ†λ‹˜μ˜ μ‹€μ œ 팁 = μ›λž˜ 팁 - i (λŒ€κΈ° μ‹œκ°„)
    • 팁이 음수인 κ²½μš°λŠ” 0으둜 처리
  5. λͺ¨λ“  팁의 합을 ꡬ해 좜λ ₯ν•œλ‹€.

TMI 1

이틀전에 이 문제 ν’€μ—ˆλ‹€κ°€ μ’€ μ‰½κ²Œ ν‘Ό 것 κ°™μ•„ λ”΄ 문제 올리렀고 ν–ˆλŠ”λ° κ²°κ΅­ 이 문제둜 μ˜¬λ¦¬λ„€μš”..(μ‘°ν•© μ•Œκ³ λ¦¬μ¦˜ λ¬Έμ œμ˜€λŠ”λ° μ‹œκ°„ 초과 계속 뜸.. ) λͺ»ν‘Ό 문제 - λΈŒμ‹€μ΄μ˜ 뢈침번 근무

C2νšŒκ³ λ„ μ„±μž₯보닀 κ΅¬ν˜„μ— κΈ‰κΈ‰ν–ˆλ‹€κ³  μ μ—ˆλŠ”λ°, μ•Œκ³ λ¦¬μ¦˜ μŠ€ν„°λ””λ„ μ‹œκ°„μ— 쫓겨 μ‰¬μš΄ 문제둜 κ°€λŠ” 것 κ°™μ•„ 아쉬움이 μžˆλ„€μš”(이번주 μ—¬ν–‰λ§Œ 2탕..)

TMI 2

μ½”ν…Œ κ²½ν—˜μ΄ μžˆλŠ” λŸ¬λ„ˆλΆ„μ—κ²Œ μ½”λ“œλ₯Ό λ³΄μ—¬μ€¬λ”λ‹ˆ μ™œ μ†”λ£¨μ…˜ ν•¨μˆ˜λ₯Ό λ§Œλ“€κ³  μ™œ λͺ¨λ“ˆλ‘œ λ‚˜λˆ΄κ³  μ™œ λ³€μˆ˜λͺ…을 μ‹ κ²½μ¨μ„œ ν•˜λƒκ³  λ¬ΌμœΌμ‹œλ”λΌκ³ μš”.
μ‹€μ „μ—μ„œ 그런 것에 μ‹œκ°„ 쓸거냐? 라고.. λ¬ΌμœΌμ…¨λŠ”λ° κΈ°λŠ₯ λ³„λ‘œ κ΅¬ν˜„ν•˜κ³  μ‘°λ¦½ν•˜λŠ”κ²Œ 도움이 μ €ν•œν… 도움이 됐닀고 λ§ν–ˆκ³  κ·Έλ ‡κ²Œ μƒκ°ν•΄μš”.
μ§€κΈˆμ€ κ·Έμ € ν’€μ–΄λ³΄λŠ” 것에 의미λ₯Ό 두고 μžˆμ§€λ§Œ, νƒ€μž„μ–΄νƒ λŠλ‚ŒμœΌλ‘œλ„ ν•œλ²ˆ μ”© ν•΄λ΄μ•Όκ² λ‹€λŠ” 생각이 λ“€μ—ˆλ„€μš”.

+ μΆ”κ°€! λ­”κ°€ μ œκ°€ μ μ–΄λ†“μ€κ²Œ κ·Έ 뢄이 곡격적으둜 μ–˜κΈ°ν•œ 것 처럼 λŠκ»΄μ§€λŠ”λ° μ „ν˜€ μ•„λ‹™λ‹ˆλ‹€!! κ·Έλƒ₯ 슀슀둜 ν•œλ²ˆ μƒκ°ν•΄λ³΄λΌλŠ” 의미둜 λ§ν•΄μ£Όμ…¨μ–΄μš”!! μ˜€ν•΄ XXX

Copy link
Collaborator

Choose a reason for hiding this comment

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

μ²˜μŒμ— μ €λŠ” κ·Έλƒ₯ sortedν–ˆλ‹€κ°€ λœ¬κΈˆμ—†μ΄ ν‹€λ €μ„œ κΈ‰ν•˜κ²Œ reversed λ„£μ–΄μ£Όκ³  λ§žμ·„λŠ”λ°, byμ“°λŠ”κ±Έ κΉœλ°•ν–ˆλ„€μš”! πŸ‘ μžŠμ§€μ•Šκ² μŠ΅λ‹ˆλ‹€.

var sortedArr = arr.sorted().reversed()

Copy link
Collaborator

Choose a reason for hiding this comment

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

λ¬΄μš°λŠ” μ—­μ‹œ

Comment on lines +7 to +11
Copy link
Collaborator

@bishoe01 bishoe01 May 1, 2025

Choose a reason for hiding this comment

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

사싀상 λ™μΌν•œ 것 κ°™μŠ΅λ‹ˆλ‹€!
μ €λŠ” μ½”λ“œκΈΈμ΄ 쀄여주렀고 enumerated()λž‘ μ‚Όν•­μ—°μ‚°μž μ‚¬μš©ν–ˆμ–΄μš”!
λ¬΄μ„ μƒλ‹˜μ΄ 더 이해가 μž˜λ˜λŠ” 쒋은 μ½”λ“œλ„€μš” πŸ‘

for (idx, value) in sortedArr.enumerated() {
        answer += value - idx > 0 ? value - idx : 0
    }

Copy link
Collaborator

Choose a reason for hiding this comment

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

@bishoe01 finn쌀 저도 enumerated() μΌμ–΄μš© !!! ν•€μŒ€λ„ Hoxy~~~ python??? λ„ˆλ‘? μ•Ό λ‚˜λ‘ γ…‹γ…‹

Comment on lines +17 to +25
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 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 μ•Œλ°”μƒκ°•ν˜Έ(arr: [Int]) {
    var sortedArr = arr.sorted().reversed()
    var answer = 0
    for (idx, value) in sortedArr.enumerated() {
        answer += value - idx > 0 ? value - idx : 0
    }
    print(answer)
}
  1. 큰 μˆ˜λΆ€ν„° ν•˜λ©΄ λ˜κ² λ‹€κ³  생각을 ν–ˆμ–΄μ„œ κ·Έλ¦¬λ””κ΅¬λ‚˜ν•˜κ³ , μž…λ ₯받은 배열을 μ •λ ¬ν•΄μ€€λ‹€μŒ
  2. value-idx둜 ν•΄μ€¬μŠ΅λ‹ˆλ‹€. indexλŠ” 0λΆ€ν„° μ‹œμž‘ν•΄μ„œ 이미 -1이 λ˜μ–΄μžˆλŠ” μƒνƒœλΌμ„œμš”!
  3. κ°’κ³Ό 인덱슀λ₯Ό λΉ λ₯΄κ²Œ κ°€μ Έμ˜€κΈ° μœ„ν•΄μ„œ enumerated()λ₯Ό ν™œμš©ν•΄μ€¬μŠ΅λ‹ˆλ‹€.

@sep037
Copy link
Collaborator

sep037 commented May 4, 2025

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

import Foundation

let N = Int(readLine()!)!

var tip : [Int] = []
var sum : Int = 0

for _ in 0 ..< N {
    tip.append(Int(readLine()!)!)
}

tip.sort { $0 > $1 } // λ‚΄λ¦Όμ°¨μˆœ

for i in 0 ..< N {
    if tip[i] - i < 0 {
        sum += 0
    } else {
        sum += tip[i] - i
    }
}

print(sum)



μ €λŠ” 문제 보고 κ·Έλƒ₯ μ§κ΄€μ μœΌλ‘œ ν’€μ—ˆμ–΄μš” ,,
ν•€ μ½”λ“œ λ³΄λ‹ˆκΉŒ μ—„μ²­ μ§§κ³  κ°„κ²°ν•˜λ„€μš” ! μ™œ 문제 ν‘ΈλŠ”λ° μ‚Όν•­μ—°μ‚°μžκ°€ μ•ˆ λ– μ˜€λ₯΄λŠ”μ§€ κΆκΈˆν•΄μš” μ €λŠ” .
ν•¨μˆ˜λ‘œ λ§Œλ“  것도 잘 λ΄€μ–΄μš” ~ ~

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 +3 to +15
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.

이게 κ·Έλ¦¬λ””λŠ” 같이 μ ‘κ·Όν–ˆλŠ”λ° μ €λŠ” enumrated() ν•¨μˆ˜λ‘œ index와 valueλ₯Ό ν•¨κ»˜ λ±‰μ–΄μ£ΌλŠ” ν•¨μˆ˜λ‘œ μž‘μ„±ν–ˆμŠ΅λ‹ˆλ‹€. !

import Foundation


// μ›λž˜ μ£Όλ €κ³  ν–ˆλ˜λˆ -(받은 λ“±μˆ˜ - 1) 만큼 νŒμ„ κ°•ν˜Έμ—κ²Œ μ€€λ‹€.
// μŒμˆ˜λŠ” 0
// 제일 많이 λ°›λŠ” 법은 κ°€μž₯ ν°μˆ˜κ°€ μ•žμ— 였고 0λ“±μœΌλ‘œ μ˜€λŠ”λ²•μ΄λ‹€.
let n = Int(readLine()!)!

var numbers: [Int] = []

for _ in 0..<n {
    let num = Int(readLine()!)!
    numbers.append(num)
}

numbers.sort(by:>) // λ‚΄λ¦Όμ°¨μˆœ

var maxTip = 0

for (idx,tip) in numbers.enumerated() {
    let takeTip = tip - idx
    guard takeTip > 0 else { break} // 뒀에꺼 λ‹€ λŒν•„μš”μ—†μŒ
    
    maxTip += takeTip
}

print(maxTip)

μ—¬κΈ°μ„œ μ €λŠ” 2κ°€μ§€λ₯Ό κΉ¨λ‹«λŠ”λ°μš”.

  1. μΈλ±μŠ€μ΄λ―€λ‘œ λ“±μˆ˜μ—μ„œ - 1 ν•˜λŠ” 연산을 쀄이기 μœ„ν•΄μ„œλŠ” 인덱슀 κ·ΈλŒ€λ‘œ μ“°λ©΄λœλ‹€ !
  2. guard문이 μžˆμœΌλ―€λ‘œ 전체 쑰건을 λ‹€ 돌 ν•„μš”μ—†κ³ , λ¬Έμ œμ—μ„œ μš”κ΅¬ν•˜λŠ” 쑰건 밑은 체크할 ν•„μš”μ—†μœΌλ―€λ‘œ guard문을 μ‚¬μš©ν•˜μž.

사싀 무 μ½”λ“œλ₯Ό λ³΄λ‹ˆ γ…‹γ…‹ enumrated()λŠ” λ„ˆλ¬΄ λΉ‘λŒ€κ°ˆ λ§Ήν‚€λ‘œ λ³΄λŠ”λ°λ‘œ κ΅¬ν˜„μ΄μ§€ μ•Šμ•˜λ‚˜μ‹Άμ–΄μš”.


++ μ €μ˜ 개인적인 μƒκ°μž…λ‹ˆλ‹€.

무가 μ½”ν…Œ κ²½ν—˜μ΄ μžˆμœΌμ‹  λŸ¬λ„ˆλΆ„μ—κ²Œ μ—¬μ­€λ³΄μ…¨λ‹€ν–ˆλŠ”λ°, μ €μ˜ 생각은 쑰금 λ‹€λ¦…λ‹ˆλ‹€.

기좜문제만 보아도, API μž‘μ„± λ¬Έμ œκ°€ 많고 μ΄λ²ˆμ— μ½”ν…Œ ν‘Ό 문제 2λ²ˆμ€ 아에 λŒ€λ†“κ³  APIμž‘μ„±λ¬Έμ œ ( 카카였, 넀이버식) -> 이 말은 즉, ν•¨μˆ˜λ³„λ‘œ λ‚˜λˆ μ„œ νŒŒλΌλ―Έν„°λ₯Ό 잘 μ΄μš©ν•˜λŠλƒ. (νŒŒλΌλ―Έν„° ν•¨μˆ˜ ν”„λ ˆμž„ 골격 μˆ˜μ •κ°€λŠ₯ν•˜λŠλƒ ? -> 삼성에선 λΆˆκ°€λŠ₯ν–ˆμŠ΅λ‹ˆλ‹€. ) κ·Έλž˜μ„œ ν•¨μˆ˜λ₯Ό λ§Œλ“€κ³  λ‘œμ§λ³„λ‘œ 잘 λ‚˜λˆ„λŠ”κ²ƒμ€ ν•„μš”ν•˜λ‹€ μƒκ°ν•©λ‹ˆλ‹€.

κ·ΈλŸ¬λ‚˜ λ§Œμ•½ λ¬Έμ œμ— λŒ€ν•œ 이해도도 μ—†λŠ”λ° λ‚˜λˆ„κ³  μžˆλŠ”κ²ƒμ€ λΆ„λͺ… λΉ„νš¨μœ¨μ μΈκ²ƒμ€ λ§žμŠ΅λ‹ˆλ‹€. κ·ΈλŸ¬λ‚˜ μ—°μŠ΅ν• λ•Œ μ‘°μ°¨ κ·Έλƒ₯ 맨땅에 쀄쀄 μ λŠ”κ²ƒμ€ μ•ˆμ’‹μ€ μŠ΅κ΄€μΈκ±° κ°™μŠ΅λ‹ˆλ‹€.

μ € λ˜ν•œ ν•¨μˆ˜λ‘œ 적지 μ•Šκ³  μ΄λ²ˆμ— κ·Έλƒ₯ μ œμΆœν•˜μ˜€μœΌλ‚˜, ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€ + ν…Œν¬ κΈ°μ—… + 사기업 쑰차도 λ‹€ API μš”κ΅¬ 쑰건이며 μ œκ°€ μ³€μ—ˆλ˜ μ‚Όμ„± μ½”ν…Œ 쑰차도 정말 λ³΅μž‘ν•œ κ΅¬ν˜„μ΄κ³  반볡적인 create , delete , updateλ₯Ό μ•Œκ³ λ¦¬μ¦˜μ˜ μ§€μ‹μœΌλ‘œ μ–Όλ§ˆλ‚˜ μ΅œμ ν™”μ‹œν‚€λŠλƒμ˜€μŠ΅λ‹ˆλ‹€.(1번 λ¬Έμ œκ°€ λΉ‘κ΅¬ν˜„μ΄μ§€λ§Œ κ²°κ΅­ μ•Œκ³ λ³΄λ©΄ create, delete, update μ˜€μŠ΅λ‹ˆλ‹€. ) κ·Έλž˜μ„œ μ‚Όμ„±μ—μ„œλŠ” 2문제λ₯Ό 4μ‹œκ°„μ„ μ€€λ‹€κ³  μƒκ°ν•©λ‹ˆλ‹€. 반볡적인 연산은 즉, ν•¨μˆ˜λ₯Ό 적극 ν™œμš©ν•˜λΌλΌλŠ” 뜻으둜 ν•΄μ„λ©λ‹ˆλ‹€.

λ˜ν•œ, μ œκ°€ μ³€μ—ˆλ˜ μ†Œλ§ˆ μ½”ν…Œμ—μ„œλŠ” μ•ž 2λ¬Έμ œμ—μ„œλŠ” 되게 μ‰½κ²Œ ν’€λ Έμ§€λ§Œ λ’·λ¬Έμ œλ“€μ€ ν•¨μˆ˜λ₯Ό κ΅¬λΆ„μ§“κ±°λ‚˜ λ‚΄κ°€ μ΄ν•΄ν•œλ°”κ°€ μ •ν™•ν•˜κ²Œ λ‘œμ§λ³„λ‘œ λΆ„λ¦¬μ‹œμΌœλ†“μ§€ μ•Šκ³  쀄쀄푼닀면 μ ˆλŒ€ 풀리지 μ•Šμ„ λ¬Έμ œλ“€μ΄μ—ˆμŠ΅λ‹ˆλ‹€. -> SQL 문제 λ˜ν•œ CTE 가상 ν…Œμ΄λΈ” ν‘œν˜„μœΌλ‘œ ν’€μ—ˆμ„λ•Œλ„ μ–΄λ–»κ²Œλ³΄λ©΄ ν•¨μˆ˜μ²˜λŸΌ λ‚΄κ°€ μƒκ°ν•œ 것듀을 λͺ¨λ“ˆν™”λ₯Ό 잘 μ‹œν‚€λŠλƒμ˜€μŠ΅λ‹ˆλ‹€.

μ €λŠ” λ¬΄λ‚˜ κΈΈ μ½”λ“œλ₯Ό λ³΄λ©΄μ„œ λ°˜μ„± 많이 ν•©λ‹ˆλ‹€. 길은 주석을 정말 꼼꼼히 μž‘μ„±ν•˜κ³  그것도 MARKλ₯Ό ν™œμš©ν•˜μ—¬, + μŠ€μœ„ν”„ν‹°ν•˜κ²Œ guard문을 ν™œμš©ν•œλ‹€λŠ”κ²ƒ μ§€κΈˆμ€ 되게 좔가적인 κΈ°νšŒλΉ„μš©μ΄ λ“€μ–΄κ°€μ§€λ§Œ μ΄ν›„μ—λŠ” 정말 빛을 λ°œν• κ²ƒμž…λ‹ˆλ‹€. λ˜ν•œ 무가 μž‘μ„±ν•˜λŠ” ν•¨μˆ˜λ³„λ‘œ λ‘œμ§λΆ„λ¦¬, 이게 μ§€κΈˆ λ‹Ήμž₯ μ½”ν…Œμ˜ 연속이라면 λ³„λ‘œλ‹€ ! 라고 ν•  수 μžˆκ² μŠ΅λ‹ˆλ‹€λ§Œ...( 사싀 μ™œ λ³„λ‘œμΈμ§€ μ €λŠ” 잘 λͺ¨λ₯΄κ² μŠ΅λ‹ˆλ‹€... γ…‹... ) μ €λŠ” μ—°μŠ΅λ•Œ μ €λ ‡κ²Œ ν•΄λ†“μœΌλ©΄ 좔후에 문제λ₯Ό λ³΄μ•˜μ„λ•Œ μ–΄λ–»κ²Œ λΆ„λ¦¬μ‹œν‚¬μ§€ λ˜ν•œ λ””λ²„κΉ…μ‹œμ— μ‹œκ°„μ„ 정말 λ‹¨μΆ•μ‹œν‚¬ 수 μžˆλ‹€λŠ” μ μ—μ„œ μ €λŠ” 무쑰건 적극 μ°¬μ„±μž…λ‹ˆλ‹€. -> μ•± μ „ν˜•μ€ μ½”ν…Œλ„ μžˆμ§€λ§Œ 과제 μ „ν˜•λ„ μžˆκΈ°μ—..

λ¬Όλ‘  μ € 말씀을 ν•˜μ‹  λŸ¬λ„ˆ λΆ„μ˜ 말씀도 이해가 κ°€μ§€λ§Œ μ €λŠ” μ € 말을 κ²½κ³„ν•˜κ³  μ½”ν…Œλ₯Ό μ€€λΉ„ν•˜λŠ” μž…μž₯μ—μ„œλŠ” μ‘°μ‹¬μŠ€λ ˆ μ˜κ²¬μ„ λ‚΄λ΄…λ‹ˆλ‹€.

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
Member

Choose a reason for hiding this comment

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

쒋은 κΈ€ κ°μ‚¬ν•©λ‹ˆλ‹€.
저도.. ν˜Ήμ‹œ 도움이 λ κΉŒν•˜μ—¬ TMI에 λŒ€ν•œ 제 생각을 μ–˜κΈ°ν•΄λ³΄μžλ©΄
(사싀 μ œκ°€ 이런 μ–˜κΈ°λ₯Ό ν•  μžκ²©μ€ μ—†μŠ΅λ‹ˆλ‹€..)

가독성과 ꡬ쑰 vs μ‹€μ „ 속도

κ°€ λ”± 고민의 주제라고 생각이 λ“€μ—ˆμ–΄μš”..

"이걸 ꡳ이 ν•¨μˆ˜λ‘œ λΊ„κΉŒ?"
"λ³€μˆ˜λͺ… μ‹ κ²½ μ“°λŠ”κ²Œ 의미 μžˆλ‚˜?" λΌλŠ” 고민도 의미 μžˆλ‹€κ³  λ΄μš”.

ν•˜μ§€λ§Œ μ €λŠ” λ‹¨μˆœν•˜κ²Œ "μ–΄μ°¨ν”Ό 디버깅 해야돼" λΌλŠ” 생각을 κ°€μ§€κ³  μžˆμŠ΅λ‹ˆλ‹€.

생각해보면, line1λΆ€ν„° print()κΉŒμ§€ μˆ˜μ • μ•ˆν•΄λ³Έ μ½”λ“œλŠ” μ—†λŠ” 것 κ°™κ΅¬μš”..
λ””λ²„κΉ…ν•˜λ €λ©΄... 가독성과 ꡬ쑰가 μ—†μœΌλ©΄.. λΆˆκ°€λŠ₯..μ΄λ‹ˆκΉŒμš”..

그리고 μš°λ¦¬κ°€ μž˜ν•˜κ³ μž ν•˜λŠ” 것은 버그 ZERO μ½”λ“œλ₯Ό μ§œλŠ”κ²Œ μ•„λ‹Œ,
가독성과 ꡬ쑰, 그리고 버그가 적은 μ½”λ“œλ₯Ό μ§œλŠ”κ²Œ μ•„λ‹Œκ°€ μ‹ΆμŠ΅λ‹ˆλ‹€!!


그리고 νšŒμ‚¬μ—μ„œλ„ 개발만 μž˜ν•˜λŠ”κ²Œ μ•„λ‹Œ 버그λ₯Ό 잘 νŒŒμ•…ν•˜κ³  μž‘λŠ” μ‚¬λžŒμ„ μ›ν•˜μ§€ μ•Šμ„κΉŒ μ‹Άλ„€μš” ~!!
(그러렀면 μ½”ν…Œλ„ 정성껏. 풀닀보면. 속도도 λŠ˜μ–΄λ‚˜μ§€ μ•Šμ„κΉŒμš”~~??)

Copy link
Collaborator

Choose a reason for hiding this comment

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

@giljihun @일둠머슀크 μ—¬κΈ° μΈμž¬μžˆμ–΄μ˜ˆ... @timcook μ—¬κΈ° μΈμž¬μžˆμ–΄μ˜ˆ

Copy link
Collaborator Author

@MuchanKim MuchanKim May 6, 2025

Choose a reason for hiding this comment

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

image

크... 진심어린 μ‘°μ–Έ κ°μ‚¬ν•©λ‹ˆλ‹€ λ‹€λ“€..! μΌ€μ–΄μ•€μ„œν¬νŠΈ μ§€λŒ€λ£¨λ„€μš”. 이런 TMI자주 μ˜¬λ €μ•Όκ² μ–΄μš”.

μ’‹κ²Œ λ΄μ£Όμ‹œλ‹ˆ λ‹€ν–‰μž…λ‹ˆλ‹€!

λ§ν•˜μ‹ λŒ€λ‘œ λͺ¨λ“ˆν™”ν•˜λŠ” 것이 μ €λŠ” μ’‹λ‹€κ³  μƒκ°ν•˜κ³  μžˆμ–΄μš”.
ν•œλ²ˆμ— ν’€λ¦¬λŠ” κ²½μš°λŠ” λ“œλ¬ΌκΈ° λ•Œλ¬Έμ— λͺ¨λ“ˆμ„ λ‚˜λˆ„λŠ”κ²Œ κ²°κ΅­μ—” 더 빨리 ν‘ΈλŠ” 방법이라고 μƒκ°ν•΄μš”. ν•˜λ“œμ½”λ”©ν•˜λ©΄ μ–΄λ””μ„œλΆ€ν„° 고쳐야할지 λ§‰λ§‰ν•˜μž–μ•„μš”? 가독성도 λ–¨μ–΄μ§€κ³ !

ν•˜μ§€λ§Œ 문제λ₯Ό ν’ˆμ— μžˆμ–΄μ„œ λΆˆν•„μš”ν•œ μš”μ†Œλ“€. ν•¨μˆ˜λͺ…을 κ³ λ―Όν•œλ‹€λ˜μ§€, μž‘μ€ κΈ°λŠ₯이라도 λ‹€ ν•¨μˆ˜λ‘œ μͺΌκ° λ‹€λ˜μ§€. 이런 뢀뢄듀을 λ‹€μ‹œν•œλ²ˆ 고민해봐야할 것 κ°™κΈ΄ν•΄μš”. μ‹€μ œλ‘œ 문제 ν’€ λ•Œ 넀이밍을 κ³ λ―Όν•˜λŠ” νŽΈμ΄κΈ΄ν•©λ‹ˆλ‹€..

λ‹€ λͺΈμœΌλ‘œ μ²΄λ“ν•˜λŠ” 과정이라 μƒκ°ν•˜κ³  μ΄λ ‡κ²Œλ„ 해보고 μ €λ ‡κ²Œλ„ ν•΄λ³΄λ €κ³ μš”! κ·Έλ ‡κ²Œ μ €λ§Œμ˜ 기쀀을 λ§Œλ“€μ–΄κ°€λ©΄ λ˜λŠ” 것 μ•„λ‹ˆκ² μŠ΅λ‹ˆκΉŒ?

λͺ¨λ‘ κ°μ‚¬ν•΄μš”!

Copy link
Collaborator

Choose a reason for hiding this comment

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

@MuchanKim 무!! μš”μ‹œ !! 이쿠죠 !

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
 
 μŠ€νƒ€λ°•μŠ€ κ·€μ—½ γ…‹γ…‹
 
 μˆœλ²ˆμ— 따라 νŒμ„ λ‹€λ₯Έ μ•‘μˆ˜λ‘œ 쀌
 
 팁 = μ›λž˜ μ£Όλ €κ³  μƒκ°ν–ˆλ˜ 돈 - (받은 λ“±μˆ˜ - 1)
 -> 음수면 0원 처리
 
 λ‹¨μˆœν•˜κ²Œ 팁이 subtraction으둜만 κ³„μ‚°λ˜λ‹ˆκΉŒ
 음수만 μ•ˆλœ¨λ©΄ λ˜‘κ°™μ€κ±° μ•„λ‹Œκ°€? μ•„λ‹˜λ§κ³  ☠️
 
 */
import Foundation

guard let N = Int(readLine() ?? "") else { exit(0) }

var customers: [Int] = []

// tip μž…λ ₯λ°›κΈ°
(0..<N).forEach { _ in
    guard let tip = Int(readLine() ?? "") else { exit(0) }
    customers.append(tip)
}

// λ‚΄λ¦Όμ°¨μˆœ γ„±
customers.sort(by: >)

// 팁 계산 ν•¨μˆ˜
func calculateTip(_ tip: Int, _ order: Int) -> Int {
    var realTip = tip - (order - 1)
    
    realTip = realTip < 0 ? 0 : realTip
    
    return realTip
}

// 팁 μ΅œλŒ“κ°’ κ΅¬ν•˜κΈ°
var tipSum = 0

(0..<N).forEach { order in
    tipSum += calculateTip(customers[order], order + 1)
}

print(tipSum)

저도 μ΅œλŒ€ν•œ ν’€μ΄μ‹œκ°„μ„ 생각해보렀고 ν•˜κ³  μžˆμ–΄μš”. (잘 μ•ˆλ˜μ§€λ§Œ..)
κ·Έλž˜μ„œ μœ„μ²˜λŸΌ μ™„μ „ 직관적인 γ…‹γ…‹γ…‹γ…‹ cμ–Έμ–΄ 같은.. μœ„μ—μ„œ μ•„λž˜λ‘œ μ­‰. λ‚΄λ €μ˜€λŠ” μ½”λ“œλ„ μ§œλ΄€λ„€μš”.

μ½”λ“œ 볡기

λ¨Όμ €, μ‚Όν•­ μ—°μ‚°μžλ‘œ 잘 μ²˜λ¦¬ν–ˆλ‹€κ³  생각은 ν–ˆλŠ”λ°.
λ‹€λ₯Έ λΆ„λ“€ μ½”λ©˜νŠΈλž‘ 무 μ½”λ“œλ„ λ³΄λ‹€λ³΄λ‹ˆ, max λ₯Ό. μ“°λŠ”κ²Œ 더 swiftyν•œ 것 같기도 ν•˜κ΅¬μš”.

func calculateTip(_ tip: Int, _ order: Int) -> Int {
    return max(tip - (order - 1), 0)
}

Comment on lines +3 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.

solution!
ν™•μ‹€νžˆ κ°€λ…μ„±μ΄λ‚˜ ꡬ쑰가 λͺ…ν™•ν•΄μ„œ λ„ˆλ¬΄ μ’‹μ€λ°μš”?

무 문제λ₯Ό ν’€λ•Œλ©΄
저도 잘 λ‚˜λˆ„μ§€μ•Šλ˜(?) λͺ¨μŠ΅μ„ λ°˜μ„±ν•˜κ³ μž
μž‘κ²Œλ‚˜λ§ˆ ν•¨μˆ˜λ₯Ό λ‚˜λˆ λ³΄κ³ μž ν•˜λ„€μš”~!

// 팁 계산 ν•¨μˆ˜
func calculateTip(_ tip: Int, _ order: Int) -> Int {
    var realTip = tip - (order - 1)
    
    realTip = realTip < 0 ? 0 : realTip
    
    return realTip
}

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.

μ €λŠ” 무와 거의 λΉ„μŠ·ν•˜κ²Œ ν‘Ό 것 κ°™μ•„μš”!!
사싀 곡간 λ³΅μž‘λ„λ₯Ό 생각할 ν•„μš”κ°€ μ—†κΈ΄ν•˜μ§€λ§Œ
λ§€κ°œλ³€μˆ˜μ™€ sorted둜 μΈν•΄μ„œ 곡간 λ³΅μž‘λ„ 차이가 λ‚ κΉŒ ν–ˆλŠ”λ° 생각보닀 쑰금 λ‚˜λŠ” 것 κ°™μ•„μš”!

var visitor: [Int] = []

for _ in 0..<Int(readLine()!)! {
    visitor.append(Int(readLine()!)!)
}

visitor.sort(by: >)

var result = 0

for i in 0..<visitor.count {
    let money = visitor[i] - ((i + 1) - 1)
    if 0 < money { result += money }
}

print(result)
image

@MuchanKim MuchanKim merged commit 4995966 into main May 7, 2025
9 checks passed
@alstjr7437 alstjr7437 deleted the 3-MuchanKim 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