From 20fef5cdf6747b9d34ae1a60bc0bef8bf7fa9aa1 Mon Sep 17 00:00:00 2001 From: hyeonji0108 Date: Thu, 19 Nov 2026 13:44:56 +0000 Subject: [PATCH 1/2] Create 1802. Maximum Value at a Given Index in a Bounded Array --- ...Value at a Given Index in a Bounded Array" | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 "leetcode3/\352\263\275\355\230\204\354\247\200/1802. Maximum Value at a Given Index in a Bounded Array" diff --git "a/leetcode3/\352\263\275\355\230\204\354\247\200/1802. Maximum Value at a Given Index in a Bounded Array" "b/leetcode3/\352\263\275\355\230\204\354\247\200/1802. Maximum Value at a Given Index in a Bounded Array" new file mode 100644 index 00000000..ae5e9613 --- /dev/null +++ "b/leetcode3/\352\263\275\355\230\204\354\247\200/1802. Maximum Value at a Given Index in a Bounded Array" @@ -0,0 +1,45 @@ +class Solution { + public int maxValue(int n, int index, int maxSum) { + int left = 1; + int right = maxSum; + int answer = 1; + + // 비교하려는 대상 값보다 작을 때 + while (left <= right) { + // 좌측과 우측값의 간격을 이용하여 중간값 구하기 + int mid = left + (right - left) / 2; + + // index 위치의 값이 mid일 때 가능한 배열의 최소 합 계산 (양 옆의 숫자를 1칸씩 줄이기) + long leftSum = getSideSum(mid - 1, index); + long rightSum = getSideSum(mid - 1, n - 1 - index); + long totalSum = leftSum + mid + rightSum; + + // 총 합이 최대값보다 작을 때 + if (totalSum <= maxSum) { + answer = mid; + left = mid + 1; // 좌측 점을 우측으로 한 칸 땡기기 + } else { + right = mid - 1; // 우측 점을 좌측으로 한 칸 땡기기 + } + } + + return answer; + } + + // peak에서 시작해 1씩 감소하며 count개의 원소를 채울 때의 최소 합 + private long getSideSum(long peak, int count) { + if (peak >= count) { + // 1에 도달하기 전에 칸이 모두 채워지는 경우 (등차수열의 합) + long first = peak - count + 1; + long last = peak; + return (first + last) * count / 2; + } else { + // 1까지 감소한 뒤 남은 칸을 모두 1로 채우는 경우 + long sumToOne = (1 + peak) * peak / 2; + long remainingOnes = count - peak; + return sumToOne + remainingOnes; + } + } +} + +// 시간복잡도 : 변수가 1씩 일정하게 증가하는 걸 봐서 O(1)로 추정 From 762c59d6bd4f835bc5fe0960f6b78f88ea9fab04 Mon Sep 17 00:00:00 2001 From: hyeonji0108 Date: Thu, 19 Nov 2026 13:45:28 +0000 Subject: [PATCH 2/2] Create 1828. Queries on Number of Points Inside a Circle --- ...eries on Number of Points Inside a Circle" | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 "leetcode3/\352\263\275\355\230\204\354\247\200/1828. Queries on Number of Points Inside a Circle" diff --git "a/leetcode3/\352\263\275\355\230\204\354\247\200/1828. Queries on Number of Points Inside a Circle" "b/leetcode3/\352\263\275\355\230\204\354\247\200/1828. Queries on Number of Points Inside a Circle" new file mode 100644 index 00000000..b969eab8 --- /dev/null +++ "b/leetcode3/\352\263\275\355\230\204\354\247\200/1828. Queries on Number of Points Inside a Circle" @@ -0,0 +1,31 @@ +class Solution { + public int[] countPoints(int[][] points, int[][] queries) { + int[] answer = new int[queries.length]; // 정답 배열 생성 + + // queries 배열 = [x좌표, y좌표, 반지름] + // queries 배열 만들기 + for (int i = 0; i < queries.length; i++) { + int cx = queries[i][0]; // x좌표 + int cy = queries[i][1]; // y좌표 + int r = queries[i][2]; // 반지름 + int rSquared = r * r; // 원 만들기 + + int count = 0; + // 모든 점을 순회하기 + for (int[] point : points) { + // 원의 중심과 현재 점의 좌표 차이 + int dx = point[0] - cx; + int dy = point[1] - cy; + + // 두 점 사이의 거리가 반지름 이하여야 함 + if (dx * dx + dy * dy <= rSquared) { + count++; + } + } + answer[i] = count; + } + return answer; + } +} + +// 시간복잡도 : x축과 y축 값을 곱하는걸 봐서 O(QxP)로 추정