모든 챌린지
쉬움 1명 해결 정답률 100.0%

Min-Max Normalization

Rescale a feature so its smallest value becomes 0 and its largest becomes 1.

배우게 될 내용

  • Rescale values to the 0-1 range
  • Handle a feature where all values are equal
  • Prepare data for training

문제

번역본 보기

Features measured on different scales (age in years, income in dollars) confuse many models. Min-max normalization rescales a feature to the range 0 to 1:

x' = (x - min) / (max - min)

If all values are equal (max = min), return 0 for every value.

Implement min_max_normalize(values).

Input

values: The values of one feature.

Output

Return the normalized values in the same order.

제약 조건

1 ≤ length ≤ 10,000

-1,000,000 ≤ each value ≤ 1,000,000

예시

예시 1
입력:
values = [10.0, 20.0, 15.0, 30.0]

예상 출력:
expected = [0.0, 0.5, 0.25, 1.0]
입력:
std::vector<double> values = {10.0, 20.0, 15.0, 30.0};

예상 출력:
std::vector<double> expected = {0.0, 0.5, 0.25, 1.0};
입력:
double[] values = new double[] {10.0, 20.0, 15.0, 30.0};

예상 출력:
double[] expected = new double[] {0.0, 0.5, 0.25, 1.0};

설명

min = 10 and max = 30, so the range is 20: (10 - 10) / 20 = 0, (20 - 10) / 20 = 0.5, (15 - 10) / 20 = 0.25 and (30 - 10) / 20 = 1.

예시 2
입력:
values = [5.0, 5.0, 5.0]

예상 출력:
expected = [0.0, 0.0, 0.0]
입력:
std::vector<double> values = {5.0, 5.0, 5.0};

예상 출력:
std::vector<double> expected = {0.0, 0.0, 0.0};
입력:
double[] values = new double[] {5.0, 5.0, 5.0};

예상 출력:
double[] expected = new double[] {0.0, 0.0, 0.0};

설명

All values are equal, so the range is 0 and every value becomes 0.

접근 방법

먼저 스스로 풀어 본 뒤 힌트를 하나씩 열어 보세요.

힌트 1

First find the smallest and largest values, since every normalized result depends on the feature’s spread.

힌트 2

Compute each output from its position relative to the minimum, using one pass to build the results after scanning extremes.

힌트 3

If every value is identical, avoid dividing by zero and return zeros for all positions.

예상 복잡도

O(n) time, O(1) extra space

참고 자료

이 문제는 CodeUtility에 맞게 수정되었습니다. 위의 설명과 제약 조건을 기준으로 풀어 주세요.

코더마다 가장 좋은 통과 풀이입니다. 더 빠르고, 메모리를 덜 쓰고, 평가가 좋은 풀이가 위에 옵니다.

# 코더 언어 시간 메모리 AI 평가 풀이
1 RCRiccardo Corti Python 838 ms - 5/5 코드 보기

다른 사람의 풀이 보기

커뮤니티 풀이

스포일러: 아래에 전체 풀이 1개가 있습니다 먼저 직접 풀어보세요. 다른 개발자의 코드와 AI 리뷰도 참고할 수 있습니다. 로그인하고 문제를 풀면 토론에 참여할 수 있습니다.