Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- javascript
- 알고리즘
- NPM
- 스택
- 안드로이드
- 타입스크립트
- TS
- nodejs
- 최적화
- Android
- react
- HTML
- 자바스크립트
- Python
- 백준 스택 시간초과 python
- stdin vs input
- kotlin
- 리액트
- next Link
- C++
- k for k
- 프론트엔드
- firebase
- 파이썬
- JS
- 파이어베이스
- CSS
- typescript
- 코딩테스트
- 백준 스택
Archives
- Today
- Total
sooleeandtomas
[day40] 프로그래머스 lv1.명예의 전당. 삽입정렬로 풀면 쉬움(c++) 본문
문제 🛶
https://school.programmers.co.kr/learn/courses/30/lessons/138477
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
문제 포인트 🛶
문제는 삽입 정렬로 풀면 된다.
정렬을 위한 배열 A의 크키는 k까지 이다. (A의 메모리 사용은 4byte*k만큼만 쓸 수 있다.) (int = 4byte)
정렬을 위한 배열 A의 크키는 k까지 이다. 다른말로 해보자면, A의 메모리 사용은 4byte*k만큼만 쓸 수 있다. *int = 4byte |
if (y >= k) { temp = k - 1; if (score[y] > A[temp]) { A[temp] = score[y]; } } else { temp = y; A.push_back(score[y]); } |
내림차순으로 정렬해야 한다. | if (A[x - 1] < A[x]) { swap(A[x - 1], A[x]); } |
나의 풀이 🛶
// 삽입정렬
/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, OCaml, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
#include <string>
#include <vector>
#include <iostream>
using namespace std;
vector<int> solution(int k, vector<int> score)
{
vector<int> A;
vector<int> result;
int temp = 0;
for (int y = 0; y < score.size(); y++)
{
if (y >= k) //A에는 k 크기까지만 값을 저장할 수 있다. 따라서 y가 k이상이라면 A[k-1]에 score[y]를 저장해야 한다.
{
temp = k - 1;
if (score[y] > A[temp]) // A에 삽입할 수가 A[temp]보다 작다면 버림.
{
A[temp] = score[y];
}
}
else
{
temp = y;
A.push_back(score[y]);
}
for (int x = temp; x > 0; x--)
{
if (A[x - 1] < A[x])
{
swap(A[x - 1], A[x]);
}
}
int lastIndex = A.size() - 1;
result.push_back(A[lastIndex]);
}
for (int i = 0; i < score.size(); i++)
{
cout << result[i] << ", ";
}
return result;
}
int main()
{
solution(3, {10, 100, 20, 150, 1, 100, 200});
return 0;
}
'코딩테스트 알고리즘 > 정렬' 카테고리의 다른 글
[day 35] 정렬 pivot (c++) (0) | 2023.04.05 |
---|
Comments