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

20-wkdghdwns199 #81

Merged
merged 1 commit into from
May 10, 2024
Merged

20-wkdghdwns199 #81

merged 1 commit into from
May 10, 2024

Conversation

wkdghdwns199
Copy link
Collaborator

πŸ”— 문제 링크

체윑볡

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

2μ‹œκ°„ - κ·Έλ¦¬λ””μ˜ μ΅œμ†Œ 아이디어 λ‚΄κΈ° νž˜λ“œλ„€μš”..! + μžλ°” 응애.. 🍼

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

문제λ₯Ό 작고 ν’€λ‹€κ°€ 잘 μ•ˆλ˜μ„œ 힌트λ₯Ό μ–»μ–΄μ„œ ν’€μ—ˆμŠ΅λ‹ˆλ‹€!
μš°μ„  μ œν•œ 데이터가 30개둜 그리 λ§Žμ§€ μ•Šμ•„μ„œ 이쀑 for 문으둜 ν•˜λ‚˜ ν•˜λ‚˜ λΉ„κ΅ν•˜λŠ” 방식이 ν†΅ν•˜λŠ” λ¬Έμ œμ˜€λ„€μš”!

쑰심해야 ν•  점이
μ •λ ¬λ˜μ–΄ μžˆλŠ” 데이터라고 μ ν˜€ μžˆλŠ”κ²Œ μ—†μœΌλ―€λ‘œ 데이터λ₯Ό μš°μ„  μ •λ ¬ν•΄μ£Όκ³ ,
μžƒμ–΄λ²„λ¦Όκ³Ό λ™μ‹œμ— μ—¬λ²Œμ˜ μ²΄μœ‘λ³΅μ„ κ°€μ Έμ˜¨ 학생듀을 μ°Ύκ±°λ‚˜,
λΉŒλ €μ€„ 학생을 찾으면 λ°°μ—΄μ—μ„œ κ·Έ μžλ¦¬λŠ” -1 μ΄λ‚˜ λ‹€λ₯Έ κ°’μœΌλ‘œ μ±„μ›Œμ•Ό
쀑볡 μΉ΄μš΄νŠΈκ°€ λ˜μ§€ μ•Šμ•„ μ •ν™•ν•œ 닡이 λ‚˜μ˜΅λ‹ˆλ‹€!

1. 전체 학생 μˆ˜μ—μ„œ μ²΄μœ‘λ³΅μ„ λ„λ‚œλ‹Ήν•œ 학생 수λ₯Ό λΉΌμ„œ 무쑰건 μ²΄μœ‘λ³΅μ„ μž…μ„ 수 μžˆλŠ” μ‚¬λžŒ 수λ₯Ό κ΅¬ν•œλ‹€. μ—¬κΈ°μ„œ μ²΄μœ‘λ³΅μ„ 빌렀 μž…μ„ 수 μžˆλŠ” μ‚¬λžŒλ“€μ„ μΆ”κ°€ν•΄ λ‚˜κ°ˆ 것이닀.
2. 체윑볡 μ—¬λ²Œμ„ κ°€μ Έμ˜¨ λ™μ‹œμ— λ„λ‚œλ‹Ήν•œ μ‚¬λžŒλ“€μ„ 이쀑 for문을 톡해 μ°Ύμ•„λ‚΄μ„œ answer++ λ₯Ό ν•œλ‹€. λ„λ‚œλ‹Ήν•œ 학생 수λ₯Ό λΊμ§€λ§Œ, μž…μ„ 수 μžˆλŠ” 학생을 μ°Ύμ•„μ„œ κ·Έ 수λ₯Ό λ”ν•΄μ£ΌλŠ” 것이닀.
3. 이제 μžƒμ–΄λ²„λ¦° 학생과 μ—¬λ²Œμ˜ μ˜·μ„ κ°€μ Έμ˜¨ ν•™μƒμ˜ 숫자λ₯Ό λΉ„κ΅ν•΄λ³΄λ©΄μ„œ lost[i]-1 == reserve[j] μ΄κ±°λ‚˜ lost[i]+1 == reserve[j] 이면 answer ++ λ₯Ό ν•΄μ€€λ‹€.
4. answer 에 정닡이 λ§Œλ“€μ–΄μ Έ μžˆμ„ 것이닀!
import java.util.*;

class Solution {
    public int solution(int n, int[] lost, int[] reserve) {
        Arrays.sort(lost);
        Arrays.sort(reserve);
        int answer = n-lost.length;
        for (int i=0; i<lost.length; i++){
            for (int j=0; j<reserve.length; j++){
                if (lost[i] == reserve[j]){
                    answer++;
                    lost[i] = -1;
                    reserve[j] = -1;
                    break;
                }
            }
        }
        
        for (int i=0; i<lost.length; i++){
            for (int j=0; j<reserve.length; j++){
                if (lost[i]-1 == reserve[j] || lost[i]+1 == reserve[j]){
                    answer++;
                    reserve[j]=-1;
                    break;
                }
            }
        }
        
        return answer;
    }
}

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

μžλ°”μ˜ ν΄λž˜μŠ€λ“€μ„ μ‚¬μš©ν•˜κΈ° μœ„ν•΄μ„œλŠ” importλ₯Ό 해와야 ν•΄μ„œ ν•˜λ‚˜μ”© μ•Œκ²Œ 되면 μ μ–΄κ°ˆ μ˜ˆμ •μž…λ‹ˆλ‹€..!

import java.util.*

μœ μš©ν•œ ν΄λž˜μŠ€λ“€μ΄ λ“€μ–΄μžˆλŠ” νŒ¨ν‚€μ§€μž…λ‹ˆλ‹€!
λ‚ μ§œ κ΄€λ ¨ 뿐만 μ•„λ‹ˆλΌ
List, Map, Set, Collections 같은 ν΄λž˜μŠ€λ“€μ΄ μžˆλ„€μš”!

μ•Œκ³ λ¦¬μ¦˜μ— μœ μš©ν•΄ λ³΄μ΄λŠ” ν΄λž˜μŠ€λ“€μ€ λ‹€ μ—¬κΈ° μžˆλŠ” κ±° κ°™μ•„μš”..!

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.

2 participants