문제
Find the position (counting from 0) of the first negative number in the list.
Implement first_negative_index(numbers).
Input
numbers: The list of integers.
Output
Return the index, or -1 if there are no negative numbers.
제약 조건
0 ≤ length ≤ 100,000
예시
예시 1
입력:
numbers = [4, 0, -2, -7]
예상 출력:
expected = 2
입력:
std::vector<long long> numbers = {4, 0, -2, -7};
예상 출력:
long long expected = 2;
입력:
long[] numbers = new long[] {4, 0, -2, -7};
예상 출력:
long expected = 2;
설명
-2 at index 2 is the first negative value.
예시 2
입력:
numbers = [1, 2, 3]
예상 출력:
expected = -1
입력:
std::vector<long long> numbers = {1, 2, 3};
예상 출력:
long long expected = -1;
입력:
long[] numbers = new long[] {1, 2, 3};
예상 출력:
long expected = -1;
설명
There are no negative numbers, so the answer is -1.
접근 방법
먼저 스스로 풀어 본 뒤 힌트를 하나씩 열어 보세요.
힌트 1
0보다 작은 값을 처음 만나는 위치만 찾으면 됩니다.
힌트 2
왼쪽에서 오른쪽으로 훑다가 처음 발견하면 바로 멈추세요.
힌트 3
음수 값이 없으면 누락된 경우의 값을 반환해야 합니다.
예상 복잡도
시간 O(n), 추가 공간 O(1)
참고 자료
이 문제는 CodeUtility에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.