-
[Algo Rhythm๐บ๐] ํ๋ก๊ทธ๋๋จธ์ค ๊ณ ๋์ Kit H-indexAlgo 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์ ๋ ๊ณต๋ถํด์ ์ด ๋ถ๋ถ์ ๋ณด์ถฉํด์ผ๊ฒ ๋ค๊ณ ์๊ฐํ๋ค.
'Algo Rhythm๐บ๐ > Programmers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
-