ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Algo Rhythm๐Ÿ•บ๐Ÿ’ƒ] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๊ณ ๋“์  Kit H-index
    Algo Rhythm๐Ÿ•บ๐Ÿ’ƒ/Programmers 2020. 6. 29. 18:02

    ๋ฌธ์ œ ์„ค๋ช…

     

    H-Index๋Š” ๊ณผํ•™์ž์˜ ์ƒ์‚ฐ์„ฑ๊ณผ ์˜ํ–ฅ๋ ฅ์„ ๋‚˜ํƒ€๋‚ด๋Š” ์ง€ํ‘œ์ž…๋‹ˆ๋‹ค. ์–ด๋Š ๊ณผํ•™์ž์˜ H-Index๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฐ’์ธ h๋ฅผ ๊ตฌํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์œ„ํ‚ค๋ฐฑ๊ณผ1์— ๋”ฐ๋ฅด๋ฉด, H-Index๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌํ•ฉ๋‹ˆ๋‹ค.

    ์–ด๋–ค ๊ณผํ•™์ž๊ฐ€ ๋ฐœํ‘œํ•œ ๋…ผ๋ฌธ nํŽธ ์ค‘, h๋ฒˆ ์ด์ƒ ์ธ์šฉ๋œ ๋…ผ๋ฌธ์ด hํŽธ ์ด์ƒ์ด๊ณ  ๋‚˜๋จธ์ง€ ๋…ผ๋ฌธ์ด h๋ฒˆ ์ดํ•˜ ์ธ์šฉ๋˜์—ˆ๋‹ค๋ฉด h์˜ ์ตœ๋Œ“๊ฐ’์ด ์ด ๊ณผํ•™์ž์˜ H-Index์ž…๋‹ˆ๋‹ค.

    ์–ด๋–ค ๊ณผํ•™์ž๊ฐ€ ๋ฐœํ‘œํ•œ ๋…ผ๋ฌธ์˜ ์ธ์šฉ ํšŸ์ˆ˜๋ฅผ ๋‹ด์€ ๋ฐฐ์—ด citations๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ์ด ๊ณผํ•™์ž์˜ H-Index๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

     

    ์ œํ•œ์‚ฌํ•ญ

    • ๊ณผํ•™์ž๊ฐ€ ๋ฐœํ‘œํ•œ ๋…ผ๋ฌธ์˜ ์ˆ˜๋Š” 1ํŽธ ์ด์ƒ 1,000ํŽธ ์ดํ•˜์ž…๋‹ˆ๋‹ค.

    • ๋…ผ๋ฌธ๋ณ„ ์ธ์šฉ ํšŸ์ˆ˜๋Š” 0ํšŒ ์ด์ƒ 10,000ํšŒ ์ดํ•˜์ž…๋‹ˆ๋‹ค.

     

    ๋‚˜๋Š” ์–ด๋–ป๊ฒŒ ํ’€์—ˆ๋‚˜

     

    import java.util.ArrayList;
    import java.util.Collections;
    
    public class H {
        int h;
        int upper = 0; // h๋ฒˆ ์ด์ƒ ์ธ์šฉ๋œ ๋…ผ๋ฌธ์˜ ์ˆ˜
        int lower = 0; // h๋ฒˆ ์ดํ•˜ ์ธ์šฉ๋œ ๋…ผ๋ฌธ์˜ ์ˆ˜
        
        public H(int h) {
            this.h = h;
        }
    }
    
    class Solution {
        public int solution(int[] citations) {
            int answer = 0;
            int n = citations.length;
            ArrayList<H> candidates = new ArrayList<H>();
            ArrayList<Integer> qualified = new ArrayList<Integer>();
            
            // initialized
            for(int i = 0; i <= n; i++) {
                candidates.add(new H(i));
            }
            
            // pre-processing
            for(H cand : candidates) {
                for(int citation : citations) {
                    if(cand.h >= citation) cand.upper++;
                    if(cand.h <= citation) cand.lower++;
                }
            }
            
            // check conditions
            for(H cand : candidates) {
                if(cand.h >= cand.upper && cand.h <= cand.lower) {
                    qualified.add(Integer.valueOf(cand.h));
                }
            }
            
            answer = (qualified.isEmpty()) ? 0 : Collections.max(qualified);
                
            return answer;
        }
    }

     

    ๋‚˜๋Š” ์™œ ์ €๋ ‡๊ฒŒ ํ’€์—ˆ๋‚˜

     

    ๋ฌธ์ œ๋ฅผ ์ฝ์–ด๋ณด๋‹ˆ 0 ์ด์ƒ n ์ดํ•˜์˜ ์ •์ˆ˜๊ฐ€ h๊ฐ€ ๋  ์ˆ˜ ์žˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ์—ˆ๋‹ค. ๋” ์ •ํ™•ํ•œ ์ดํ•ด๋ฅผ ์œ„ํ•ด [3, 0, 6, 1, 5]์ด input์œผ๋กœ ์ฃผ์–ด์ง„ ๊ฒฝ์šฐ๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์€ ํ‘œ๋กœ ์ •๋ฆฌํ–ˆ๋‹ค.

     

     

    ํ‘œ๋กœ ์ •๋ฆฌํ•˜๊ณ ๋‚˜๋‹ˆ H๊ฐ€ ๋  ์ˆ˜ ์žˆ๋Š” ์กฐ๊ฑด ๋‘ ๊ฐ€์ง€๋ฅผ ํ†ต๊ณผํ•ด์•ผ H-Index๊ฐ€ ๋  ์ˆ˜ ์žˆ๋Š” ์ž๊ฒฉ์ด ์ฃผ์–ด์ง„๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค.

     

    1) H๋ฒˆ ์ด์ƒ ์ธ์šฉ๋œ ๋…ผ๋ฌธ์ด HํŽธ ์ด์ƒ์ด์–ด์•ผ ํ•œ๋‹ค.

    2) ๋‚˜๋จธ์ง€ ๋…ผ๋ฌธ์ด H๋ฒˆ ์ดํ•˜ ์ธ์šฉ๋˜์–ด์•ผ ํ•œ๋‹ค.

     

    ๊ทธ๋ž˜์„œ ์•„๋ž˜์™€ ๊ฐ™์€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ƒ๊ฐํ–ˆ๋‹ค.

     

    1) h, h๋ฒˆ ์ด์ƒ ์ธ์šฉ๋œ ๋…ผ๋ฌธ์˜ ์ˆ˜, h๋ฒˆ ์ดํ•˜ ์ธ์šฉ๋œ ๋…ผ๋ฌธ์˜ ์ˆ˜๋ฅผ ๋ฉค๋ฒ„๋กœ ๊ฐ–๋Š” H๋ผ๋Š” ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ ๋‹ค.

    2) ๊ฐ H ํ›„๋ณด๋“ค์˜ h๋ฒˆ ์ดํ•˜ ์ธ์šฉ ๋…ผ๋ฌธ ์ˆ˜, h๋ฒˆ ์ด์ƒ ๋…ผ๋ฌธ ์ˆ˜๋ฅผ ํŒŒ์•…ํ•œ๋‹ค.

    3) H๋ฒˆ ์ด์ƒ ์ธ์šฉ๋œ ๋…ผ๋ฌธ์ด HํŽธ ์ด์ƒ์ด๊ณ  ๋‚˜๋จธ์ง€ ๋…ผ๋ฌธ์ด H๋ฒˆ ์ดํ•˜์ธ H๋“ค์„ ์ฐพ๋Š”๋‹ค.

    4) ์ž๊ฒฉ์žˆ๋Š” H๋“ค ์ค‘ ์ตœ๋Œ€๊ฐ’์„ returnํ•œ๋‹ค.

     

    import java.util.ArrayList;
    import java.util.Collections;
    
    public class H {
        int h;
        int upper = 0; // h๋ฒˆ ์ด์ƒ ์ธ์šฉ๋œ ๋…ผ๋ฌธ์˜ ์ˆ˜
        int lower = 0; // h๋ฒˆ ์ดํ•˜ ์ธ์šฉ๋œ ๋…ผ๋ฌธ์˜ ์ˆ˜
        
        public H(int h) {
            this.h = h;
        }
    }
    
    class Solution {
        public int solution(int[] citations) {
            int answer = 0;
            int n = citations.length;
            ArrayList<H> candidates = new ArrayList<H>();
            ArrayList<Integer> qualified = new ArrayList<Integer>();
            
            // initialized
            for(int i = 0; i <= n; i++) {
                candidates.add(new H(i));
            }
            
            // pre-processing
            for(H cand : candidates) {
                for(int citation : citations) {
                    if(cand.h >= citation) cand.upper++;
                    if(cand.h <= citation) cand.lower++;
                }
            }
            
            // check conditions
            for(H cand : candidates) {
                if(cand.h >= cand.upper && cand.h <= cand.lower) {
                    qualified.add(Integer.valueOf(cand.h));
                }
            }
            
            answer = Collections.max(qualified);
                
            return answer;
        }
    }

     

    ์ฒ˜์Œ์—๋Š” ์ด๋ ‡๊ฒŒ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ๋Š”๋ฐ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค 16๋ฒˆ์—์„œ ์—๋Ÿฌ๊ฐ€ ์ƒ๊ฒผ๋‹ค. ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค ์งˆ๋ฌธ์„ ํ™•์ธํ•˜๋‹ˆ๊นŒ ๋ชจ๋‘ 0์ผ ๊ฒฝ์šฐ๋ฅผ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•œ๋‹ค๊ณ  ๊ทธ๋ž˜์„œ ๋งˆ์ง€๋ง‰์— qualified ๋ฐฐ์—ด์ด ๋น„์–ด ์žˆ์„ ๊ฒฝ์šฐ 0์„ return ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ–ˆ๋‹ค.

     

    ๋‚˜๋Š” ๋ฌด์—‡์„ ๋ฐฐ์› ๋‚˜

     

    ์•„๋ž˜ ๋ธ”๋กœ๊ทธ๋ฅผ ํ†ตํ•ด ์ž๋ฐ”์—์„œ ์ตœ๋Œ€, ์ตœ์†Œ๊ฐ’์„ ์ฐพ๋Š” ์—ฌ๋Ÿฌ ๋ฐฉ๋ฒ•์„ ๋ฐฐ์› ๋‹ค.

    https://www.daleseo.com/java-min-max/

     

    ์ฒ˜์Œ์—๋Š” Stream์„ ์‚ฌ์šฉํ•ด์„œ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ํ‘œํ˜„ํ•˜๋ ค๊ณ  ํ–ˆ์ง€๋งŒ ์—๋Ÿฌ๊ฐ€ ๋– ์„œ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ด์•ผ ํ–ˆ๋‹ค.

    Java8์„ ๋” ๊ณต๋ถ€ํ•ด์„œ ์ด ๋ถ€๋ถ„์„ ๋ณด์ถฉํ•ด์•ผ๊ฒ ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค.

    ๋Œ“๊ธ€

Designed by Tistory.