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

27-InSange #91

Merged
merged 3 commits into from
Sep 2, 2024
Merged

27-InSange #91

merged 3 commits into from
Sep 2, 2024

Conversation

InSange
Copy link
Collaborator

@InSange InSange commented Aug 17, 2024

πŸ”— 문제 링크

number-of-senior-citizens

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

30λΆ„

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

문제 κ°œμš”

μž…λ ₯ 값인 details 배열에 폰 λ„˜λ²„, 성별, λ‚˜μ΄, μ’Œμ„ λ²ˆν˜Έκ°€ λ¬Έμžμ—΄λ‘œ μ—°κ²°λœ μƒνƒœλ‘œ 주어진닀.
7868190130M7522μ—μ„œλŠ” 7868190130이 폰 번호, M이 성별, 75κ°€ λ‚˜μ΄, 22κ°€ μ’Œμ„ λ²ˆν˜Έκ°€ λœλ‹€.
주어진 λ¬Έμžμ—΄ μ€‘μ—μ„œ λ‚˜μ΄κ°€ 60보닀 큰 μ‚¬λžŒλ“€μ˜ 수λ₯Ό λ‚˜νƒ€λ‚΄λ©΄ 됨!

풀이 방식

문제 κ°œμš”μ— μ„€λͺ…이 λ˜μ–΄ μžˆλŠ” κ²ƒμ²˜λŸΌ μ•žμ˜ 10μžλŠ” 번호, κ·Έ λ‹€μŒ 성별, λ‚˜μ΄κ°€ 주어진닀. 고둜 λ¬Έμžμ—΄μ˜ 11번, 12번 인덱슀의 값을 μ°Έμ‘°ν•˜μ—¬ ν’€λ©΄ λœλ‹€.

  1. λ¬Έμžμ—΄ 11번과 12λ²ˆμ„ κ΅¬ν•œλ‹€.
  2. λ¬Έμžμ—΄μ„ μ •μˆ˜λ‘œ λ³€ν™˜ν•œλ‹€.
  3. 60보닀 큰 μ •μˆ˜ 값듀을 μ°Ύμ•„μ„œ ans에 1μ”© 더해쀀닀.
class Solution {
public:
    int countSeniors(vector<string>& details) {
        int ans = 0;
        
        for(string detail : details)
        {
            string age = "";
            age += detail[11];
            age += detail[12];
            
            if(stoi(age) > 60) ans++; 
        }
        
        return ans;
    }
};

μ΄λŸ¬ν•œ λ°©μ‹μœΌλ‘œ ν’€μ—ˆμ„ λ•Œ
μ΅œμ ν™”κ°€ κ³ λ €λ˜μ§€ μ•Šμ•„μ„œ
image
μƒμœ„ 6%λŒ€μ— 머물게 λœλ‹€.
μ•„λ§ˆ stoi ν˜•μ‹μ΄ λ¬Έμžμ—΄ 전체λ₯Ό μˆœνšŒν•˜λ©΄μ„œ 각 문자λ₯Ό 숫자둜 λ³€ν™˜ν•˜κ³ , μžλ¦Ώμˆ˜μ— 맞게 κ³„μ‚°ν•˜κΈ°μ— μ‹œκ°„ λ³΅μž‘λ„λŠ” λ¬Έμžμ—΄μ˜ 길이에 λΉ„λ‘€ν•˜μ—¬ O(n)이기에 그런 것 κ°™λ‹€.

κ·Έ λ‹€μŒ ν’€μ΄λŠ” λžŒλ‹€ ν˜•μ‹μ„ ν™œμš©ν•΄ 정렬을 ν•œ λ’€ κ΅¬ν•΄μ£ΌλŠ” ν˜•μ‹μœΌλ‘œ 60μ΄ν•˜μΈ 수λ₯Ό λ§Œλ‚˜κ²Œλ˜λ©΄ λ°”λ‘œ 탐색을 μ’…λ£Œν•˜μ—¬ μ‹œκ°„μ„ μ€„μ—¬λ³΄κΈ°λ‘œ ν•˜μ˜€λ‹€.

class Solution {
public:
    int countSeniors(vector<string>& details) {
        int ans = 0;
        sort(details.begin(), details.end(), [](const string& a, const string& b)
             {
                 if(a[11] == b[11]) return a[12] > b[12];
                 return a[11] > b[11];
             });
        
        for(string detail : details)
        {
            cout << detail << "\n";
            if(detail[11] > '6')
            {
                ans++;
                continue;
            }
            if(detail[11] == '6' && detail[12] > '0')
            {
                ans++;
                continue;
            }
            else break;
        }
        
        return ans;
    }
};

image
별 λ‹€λ₯Όκ²Œ μ—†μ—ˆλ‹€..
μ•„λ¬΄λž˜λ„ μ •λ ¬ν•˜λŠ”λ° O(n)만큼 κ±Έλ¦¬λ‹€λ³΄λ‹ˆ 쑰금 더 κ±Έλ¦¬λŠ” 것을 확인할 수 μžˆλ‹€.

κ·Έ λ‹€μŒ 방식은 μ•„μŠ€ν‚€ μ½”λ“œ κ°’μœΌλ‘œ ν•΄κ²°ν•˜λŠ” λ°©μ‹μœΌλ‘œ λ¬Έμžμ— 0을 빼쀌으둜써 μ •μˆ˜ν˜• 값을 κ΅¬ν•˜λŠ” λ°©μ‹μœΌλ‘œ ν’€μ—ˆμ„ λ•Œμ΄λ‹€.

class Solution {
public:
    int countSeniors(vector<string>& details) {
        int seniorCount = 0;

        for (string& passengerInfo : details) {
            int ageTens = passengerInfo[11] - '0';
            int ageOnes = passengerInfo[12] - '0';
            int age = ageTens * 10 + ageOnes;

            if (age > 60) {
                seniorCount++;
            }
        }

        return seniorCount;
    }
};

image
많이 올라온 것을 확인할 수 μžˆλ‹€..!

λ„λŒ€μ²΄ 100%λŠ” μ–΄λ–€ μ½”λ“œμΌκΉŒ? 확인을 ν•΄λ΄€λŠ”λ°

image
별 큰게 μ—†λ‹€..

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

Copy link
Collaborator

@seongwon030 seongwon030 left a comment

Choose a reason for hiding this comment

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

class Solution:
    def countSeniors(self, details: List[str]) -> int:
        result = 0
        for i in range(len(details)):
            age = int(details[i][11:13])
            if age > 60:
                result += 1
        return result

μ €λŠ” κ°„λ‹¨ν•˜κ²Œ λ¬Έμžμ—΄ λ²”μœ„λ‘œ μͺΌκ°œ int둜 λ°”κΎΈμ–΄ κ³„μ‚°ν–ˆμŠ΅λ‹ˆλ‹€. 이건 40msκ°€ λ‚˜μ™”μŠ΅λ‹ˆλ‹€.

class Solution:
    def countSeniors(self, details: List[str]) -> int:
        return sum(1 for detail in details if int(detail[11:13]) > 60)

ν•œ 쀄 μ½”λ“œλ‘œ μž‘μ„±ν•˜λ‹ˆ 32ms둜 μ€„μ–΄λ“€μ—ˆλŠ”λ° 크게 μ°¨μ΄λŠ” μ—†λŠ” 것 κ°™μŠ΅λ‹ˆλ‹€..

PR 잘 λ΄€μŠ΅λ‹ˆλ‹€!

Copy link
Collaborator

@yuyu0830 yuyu0830 left a comment

Choose a reason for hiding this comment

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

κ°„λ‹¨ν•œ λ¬Έμ œμ§€λ§Œ μ΅œμ ν™”, 라이브러리 ν•¨μˆ˜ μ‚¬μš©μ˜ νš¨μœ¨μ— λŒ€ν•΄ λ‹€μ‹œ ν•œλ²ˆ 생각해볼 여지가 μžˆλŠ” λ¬Έμ œλ„€μš”.. κ²°κ΅­ 같은 λ¬Έμ œμ— λŒ€ν•΄ μ΅œμ ν™”λœ μ½”λ“œλŠ” λ‹€λ“€ λΉ„μŠ·ν•œ λͺ¨μ–‘을 λ„κ² μ§€λ§Œ κ·Έ 과정에 이λ₯΄κΈ° μœ„ν•΄μ„œλŠ” λ‹€μ–‘ν•œ 접근이 ν•„μš”ν•œ 것 κ°™μŠ΅λ‹ˆλ‹€

λ‹€μ‹œκΈˆ μ½”λ“œμ˜ μ •κ΅ν•¨μ˜ μ€‘μš”μ„±μ„ λŠλ‚€ λ¬Έμ œμ˜€μŠ΅λ‹ˆλ‹€

@InSange InSange merged commit 6367547 into main Sep 2, 2024
1 check passed
@InSange InSange deleted the 27-InSange branch September 2, 2024 12:52
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.

3 participants