Skip to content
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

26-jung0115 #190

Merged
merged 3 commits into from
Dec 15, 2024
Merged

26-jung0115 #190

merged 3 commits into from
Dec 15, 2024

Conversation

jung0115
Copy link
Member

@jung0115 jung0115 commented Dec 2, 2024

πŸ”— 문제 링크

μˆ˜ν•™ | ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€ - μ‹œμ†Œ 짝꿍(Lv.2)

μ–΄λŠ 곡원 λ†€μ΄ν„°μ—λŠ” μ‹œμ†Œκ°€ ν•˜λ‚˜ μ„€μΉ˜λ˜μ–΄ μžˆμŠ΅λ‹ˆλ‹€. 이 μ‹œμ†ŒλŠ” μ€‘μ‹¬μœΌλ‘œλΆ€ν„° 2(m), 3(m), 4(m) 거리의 지점에 μ’Œμ„μ΄ ν•˜λ‚˜μ”© μžˆμŠ΅λ‹ˆλ‹€.
이 μ‹œμ†Œλ₯Ό 두 λͺ…이 마주 보고 탄닀고 ν•  λ•Œ, μ‹œμ†Œκ°€ ν‰ν˜•μΈ μƒνƒœμ—μ„œ 각각에 μ˜ν•΄ μ‹œμ†Œμ— κ±Έλ¦¬λŠ” ν† ν¬μ˜ 크기가 μ„œλ‘œ μƒμ‡„λ˜μ–΄ μ™„μ „ν•œ κ· ν˜•μ„ 이룰 수 μžˆλ‹€λ©΄ κ·Έ 두 μ‚¬λžŒμ„ μ‹œμ†Œ 짝꿍이라고 ν•©λ‹ˆλ‹€. 즉, νƒ‘μŠΉν•œ μ‚¬λžŒμ˜ λ¬΄κ²Œμ™€ μ‹œμ†Œ μΆ•κ³Ό μ’Œμ„ κ°„μ˜ 거리의 곱이 μ–‘μͺ½ λ‹€ κ°™λ‹€λ©΄ μ‹œμ†Œ 짝꿍이라고 ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
μ‚¬λžŒλ“€μ˜ λͺΈλ¬΄κ²Œ λͺ©λ‘ weights이 μ£Όμ–΄μ§ˆ λ•Œ, μ‹œμ†Œ 짝꿍이 λͺ‡ 쌍 μ‘΄μž¬ν•˜λŠ”μ§€ κ΅¬ν•˜μ—¬ return ν•˜λ„λ‘ solution ν•¨μˆ˜λ₯Ό μ™„μ„±ν•΄μ£Όμ„Έμš”.

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

30λΆ„

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

μš°μ„ , μ‹œμ†Œμ— 두 μ‚¬λžŒμ΄ 앉을 λ•Œ 거리 λΉ„μœ¨μ€ 1:1, 2:3, 1:2(= 2:4), 3:4 쀑 ν•˜λ‚˜μž…λ‹ˆλ‹€

κ·Έλž˜μ„œ μ²˜μŒμ—λŠ” μ‚¬λžŒλ“€μ„ λͺΈλ¬΄κ²Œ μ˜€λ¦„μ°¨μˆœμœΌλ‘œ μ •λ ¬ν•˜κ³ ,
적은 λͺΈλ¬΄κ²ŒμΈ μ‚¬λžŒλΆ€ν„° κΈ°μ€€μœΌλ‘œ μž‘μ•„μ„œ λ’·μˆœμ„œμ˜ μ‚¬λžŒκ³Ό μœ„ λΉ„μœ¨ 쀑 ν•˜λ‚˜λ‘œ μ•‰μ•„μ„œ κ· ν˜•μ„ 맞좜 수 μžˆλŠ”μ§€ μ™„μ „ νƒμƒ‰μœΌλ‘œ κ΅¬ν˜„ν–ˆμŠ΅λ‹ˆλ‹€

ν•˜μ§€λ§Œ 같은 무게인 μ‚¬λžŒλ„ μ€‘λ³΅ν•΄μ„œ 계산해 일뢀 μΌ€μ΄μŠ€μ—μ„œ μ‹œκ°„μ΄ˆκ³Όκ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€.
μ•„λž˜λŠ” μ‹œκ°„ μ΄ˆκ³Όκ°€ λ°œμƒν•œ μ½”λ“œμ˜ κ΅¬ν˜„λΆ€μž…λ‹ˆλ‹€!

❌ μ‹œκ°„ 초과 μ½”λ“œ

for(i: Int in 0..people.size - 1) {
  for(j: Int in i+1..people.size - 1) {
    if(people[i] == people[j]) {
      answer++
      continue
    }
    var checked = false
    for(m1: Int in 2..3) {
      for(m2: Int in m1+1..4) {
        if(people[i] * m2 == people[j] * m1) {
          answer++
          checked = true
          break
        }
      }
          
      if(checked) break
    }
  }
}

μ€‘λ³΅λ˜λŠ” 연산을 쀄이기 μœ„ν•΄ λͺΈλ¬΄κ²ŒοΏ½λ§ˆλ‹€ ν•΄λ‹Ήν•˜λŠ” μ‚¬λžŒ 수λ₯Ό μ„Έμ–΄μ£Όκ³ , κ°€λŠ₯ν•œ μŒμ„ μ—°μ‚°ν•΄μ€¬μŠ΅λ‹ˆλ‹€

λ§Œμ•½ 같은 λͺΈλ¬΄κ²ŒμΈ μ‚¬λžŒμ΄ 2λͺ… 이상일 경우,
κ·Έ μ‚¬λžŒλ“€λΌλ¦¬λŠ” λͺ¨λ‘ 1:1 거리둜 앉을 수 있기 λ•Œλ¬Έμ— μŒμ„ μ‘°ν•©ν•  수 μžˆλŠ” 수인n * (n-1) / 2을 λˆ„μ ν•΄μ€¬μŠ΅λ‹ˆλ‹€

λ‚˜λ¨Έμ§€ 2:3, 1:2, 3:4 λΉ„μœ¨μ˜ 거리둜 μ•‰λŠ” κ²½μš°λŠ” 같은 λͺΈλ¬΄κ²ŒλΌλ¦¬μ™€ 달리 μ€‘λ³΅λ˜λŠ” μ‚¬λžŒμ΄ μ—†μœΌλ―€λ‘œ
ν˜„μž¬ 기쀀이 λ˜λŠ” λͺΈλ¬΄κ²Œμ˜ μ‚¬λžŒ μˆ˜μ™€ ν•΄λ‹Ή λΉ„μœ¨λ‘œ 앉을 수 μžˆλŠ” λͺΈλ¬΄κ²Œμ˜ μ‚¬λžŒ 수λ₯Ό κ³±ν•΄μ£Όλ©΄ 쌍의 수λ₯Ό ꡬ할 수 μžˆμŠ΅λ‹ˆλ‹€.

각 λΉ„μœ¨λ§ˆλ‹€ if문으둜 체크해주고, ν•΄λ‹Ήν•  경우 경우의 수λ₯Ό λˆ„μ ν•΄μ€¬μŠ΅λ‹ˆλ‹€

λΉ„μœ¨ 체크λ₯Ό ν•œ 방법을 2:3의 λΉ„μœ¨μ„ κΈ°μ€€μœΌλ‘œ μ„€λͺ…ν•˜λ©΄,
ν˜„μž¬ 탐색 쀑인 λ¬΄κ²Œκ°€ μ™Όμͺ½, 2에 ν•΄λ‹Ήν•œλ‹€κ³  μƒκ°ν•©λ‹ˆλ‹€!

  1. λͺ¨λ“  λͺΈλ¬΄κ²Œκ°€ μ •μˆ˜μ΄κΈ° λ•Œλ¬Έμ— 2둜 λ‚˜λˆ„μ–΄ λ–¨μ–΄μ Έμ•Ό λΉ„μœ¨μ΄ μ„±λ¦½ν•˜λ―€λ‘œ λ‚˜λˆ„μ–΄ λ–¨μ–΄μ§€λŠ”μ§€λ₯Ό λ¨Όμ € κ³„μ‚°ν•©λ‹ˆλ‹€
  2. 그리고 κΈ°μ€€ λͺΈλ¬΄κ²Œμ— 3을 κ³±ν•˜κ³  2λ₯Ό λ‚˜λˆˆ 값이 μ‘΄μž¬ν•˜λŠ”μ§€λ₯Ό μ°ΎμŠ΅λ‹ˆλ‹€ (containsKey 이용)

μ΄λ ‡κ²Œ 찾은 λͺΈλ¬΄κ²Œλ₯Ό μœ„μ—μ„œ μ„€λͺ…ν•œ λ°©μ‹μœΌλ‘œ 계산해주면 μ΅œμ’…μ μœΌλ‘œ μ‹œμ†Œ 짝꿍의 쌍 수λ₯Ό 찾을 수 μžˆμŠ΅λ‹ˆλ‹€!

βœ… μ΅œμ’… μ½”λ“œ

class Solution {
  fun solution(weights: IntArray): Long {
    var answer: Long = 0
    val weightCount = mutableMapOf<Int, Long>()

    for(weight in weights) {
      weightCount[weight] = weightCount.getOrDefault(weight, 0L) + 1
    }
    
    for((weight, count) in weightCount) {
      // 같은 무게인 μ‚¬λžŒμ΄ 2λͺ… 이상 -> 1:1둜 앉을 수 있음
      if(count > 1) {
        answer += count * (count - 1) / 2
      }
      
      // 2:3 λΉ„μœ¨
      if (weight % 2 == 0 && weightCount.containsKey(weight * 3 / 2)) {
        answer += count * weightCount[weight * 3 / 2]!!
      }
      
      // 2:4 = 1:2 λΉ„μœ¨
      if (weightCount.containsKey(weight * 2)) {
        answer += count * weightCount[weight * 2]!!
      }

      // 3:4 λΉ„μœ¨
      if (weight % 3 == 0 && weightCount.containsKey(weight * 4 / 3)) {
        answer += count * weightCount[weight * 4 / 3]!!
      }
    }
    
    return answer
  }
}

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

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.

1 participant