๋ฌธ์ 1๏ธโฃ (๐ ๋ฆฌํธ ์ฝ๋ _ ๋ฐฐ์ด ์๊ธฐ)
๐ก IDEA : ๋ ๋ฐฐ์ด์ ์ ๋ ฌํ ๋ค x ๊ฐ๋ค์ 2*n ์ธ๋ฑ์ค์ y ๊ฐ๋ค์ 2*n+1 ์ธ๋ฑ์ค์ ๋ฃ์ด ์๋ก์ด ๋ฐฐ์ด์ ์์ฑํ๋ค.
class Solution {
public int[] shuffle(int[] nums, int n) {
int[] answer = new int[nums.length];
for (int i=0; i<n; i++) {
answer[2*i] = nums[i];
answer[2*i+1] = nums[i+n];
}
return answer;
}
}
/*
x -> 0, 2, 4, 6 -> 2n
y -> 1, 3, 5, 7 -> 2n + 1;
*/
๋ฌธ์ 2๏ธโฃ (๐ ๋ฆฌํธ ์ฝ๋ _ ์ง์ฌ๊ฐํ ์ฟผ๋ฆฌ๋ค)
๐ก IDEA : ํฌ๊ฒ ์์ด๋์ด๋ผ ํ ๊ฒ ์์ด ์ฃผ์ด์ง ์กฐ๊ฑด๋๋ก ์์ฑํ์๋ค.
class SubrectangleQueries {
int[][] rectangle;
public SubrectangleQueries(int[][] rectangle) {
this.rectangle = rectangle;
}
public void updateSubrectangle(int row1, int col1, int row2, int col2, int newValue) {
for (int i=row1; i<=row2; i++) {
for (int j=col1; j<=col2; j++) {
rectangle[i][j] = newValue;
}
}
}
public int getValue(int row, int col) {
return rectangle[row][col];
}
}
๋ฌธ์ 3๏ธโฃ (๐ ํ๋ก๊ทธ๋๋จธ์ค _ H-Index)
๐ก IDEA : H ๊ฐ์ด ๋ ์ ์๋ ์ต์, ์ต๋๊ฐ์ ๊ฒฐ์ ํ ํ ์ด๋ถ ํ์์ ํตํด ์กฐ๊ฑด์ ๋ง์กฑํ๋ ์ต๋๊ฐ์ ๊ตฌํ๋ค.
โ H ํ๋ณด๊ฐ ๋ฒ์ ์ค์
โก๏ธ H ํ๋ณด๊ฐ์ ์ต์๊ฐ์ ์ฃผ์ด์ง citations ๋ฐฐ์ด์ ์ต์๊ฐ ๋๋ ๋ฐฐ์ด์ ๊ธธ์ด ์ด๋ค. ๋ฐ๋ผ์ ๋ ๊ฐ์ค Min ๊ฐ์ ์ต์๊ฐ์ผ๋ก ๊ฒฐ์ ํ๋ค. H ํ๋ณด๊ฐ์ ์ต๋๊ฐ์ citations ๋ฐฐ์ด์ ์ต๋๊ฐ์ผ๋ก ์ค์ ํ์๋ค.
โก ์ด๋ถ ํ์ ์ํ
โก๏ธ ์ด๋ถ ํ์ ์ํ์ ํตํด ์กฐ๊ฑด์ ๋ง์กฑํ๋ ๊ฐ๋ค ์ค ์ต๋๊ฐ์ ๊ตฌํ๋ค. ์ด๋ฅผ ๊ตฌํ๊ธฐ ์ํด ์ด๋ถํ์์ Bound ๊ฐ๋ ์ ์ ์ฉ์์ผฐ๋ค.
โข isPossible ์ ์ญํจ์ ์์ฑ
โก๏ธ ์ด๋ถ ํ์ ๊ณผ์ ์์ ์กฐ๊ฑด์ ๋ง์กฑํ๋์ง ํ์ธํ๊ธฐ ์ํ isPossible ํจ์๋ฅผ ์์ฑํ์๋ค. ์ด ํจ์๋ ์ํ๋ h ๊ฐ์ด ์กฐ๊ฑด์ ๋ง์กฑํ๋์ง๋ฅผ citations ๋ฐฐ์ด์ ๋งจ ๋ค๊ฐ (= ์ต๋๊ฐ) ๋ถํฐ ํ์ธํ์ฌ ์กฐ๊ฑด ๋ง์กฑ ์ฌ๋ถ์ ๋ฐ๋ผ true ๋๋ false ๋ฅผ ๋ฐํํ๋ค.
import java.util.Arrays;
class Solution {
public int solution(int[] citations) {
// (1) ๋ฐฐ์ด ์ ๋ ฌ
Arrays.sort(citations);
// (2) ์ด๋ถ ํ์ ์ํ
int left = Math.min(citations[0], citations.length);
int right = citations[citations.length-1];
int answer = 0;
while (left <= right) {
int mid = (left + right) / 2;
if (isPossible(mid, citations)) {
answer = Math.max(answer, mid);
left = mid+1;
}
else {
right = mid-1;
}
}
return answer;
}
/*
ํด๋น h ๊ฐ์ด ์กฐ๊ฑด์ ๋ง์กฑํ๋์ง ํ์ธํ๋ ํจ์
int h : h ๊ฐ
int[] citations : ์ฃผ์ด์ง citations ๋ฐฐ์ด
*/
public static boolean isPossible(int h, int[] citations) {
if (h > citations.length) return false;
int min = Math.max(0, citations.length-h);
for (int i=citations.length-1; i>=min; i--) {
if (citations[i] < h) return false;
}
return true;
}
}
'TIL ๐' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| TIL (2024.06.16) (0) | 2024.06.17 |
|---|---|
| TIL (2024.06.15) (0) | 2024.06.16 |
| TIL (2024.06.13) (1) | 2024.06.15 |
| TIL (2024.06.12) (0) | 2024.06.13 |
| TIL (2024.06.11) _ (Programmers) ์ ๊ตญ ์ฌ์ฌ (0) | 2024.06.11 |