Skip to content

41. First Missing Positive / Hard / TypeScript #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

lynn0506
Copy link
Contributor

@lynn0506 lynn0506 commented May 18, 2023

Time Complexity O(N), Space Complexity O(1)

  • array 요소들 중에서 양수 1,2,3, .. 순차적으로 줄세웠을 때 가장 처음으로 없는 요소를 return
  • 따라서 array 요소들을 순회하면서 arr[0] = 1, arr[1] = 2, 순으로 정렬하려 한다.
    • arr[0] 요소가 3이라면, arr[2] 위치에 arr[0]이 오도록 swap 한다.
      • 처음 배열 : arr = [3, .., -1, .., ];
      • swap후 배열 : arr = [-1, .., 3, .., ..];
      • 기대 결과 : arr[0] = 1, arr[1] = 2, arr[2] = 3, arr[3] = 4 ...
  • 그 후 array를 다시 순회하면서 해당 값이 올바른 위치에 없는 경우를 찾는다 즉, 정답이 되는 것!!
    • arr[0] = 1, arr[1] = -3, arr[2] = 3, arr[3] = 4 인 경우, 정수 2가 없다는 말이므로 2가 정답.

@lynn0506 lynn0506 requested a review from yeongrok-jeong May 18, 2023 16:01
@lynn0506 lynn0506 self-assigned this May 18, 2023
Comment on lines +2 to +4
const swapNumber = (pos1:number, pos2:number) => {
[nums[pos1], nums[pos2]] = [nums[pos2], nums[pos1]]
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant