Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions 02_스택_큐_덱/10757.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include <iostre>
45 changes: 45 additions & 0 deletions 05_우선순위큐/14235.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <iostream>
#include <queue>

using namespace std;

int main()
{

ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);

int n,a;
priority_queue<int, vector<int>, less<int>> pq;

Choose a reason for hiding this comment

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

priority_queue는 처음 선언할 때 default로 최대 힙으로 선언되기 때문에 그냥 priority_queue<int> pq;로 작성해줘도 돼요!

//큰 것 부터 나오는 less<int>

cin >> n;

while(n--) {
cin >> a;

//아이들 만남
if(a==0) {

if (pq.empty()) { //줄 선물 없으면 -1 출력
cout << "-1\n";
} else{ //가장 가치 큰 선물이 맨 위로 정렬되어 있으니, top, pop!
cout << pq.top() << "\n";
pq.pop();
}

}

//선물 충전
else {
for(int i=0; i<a; i++) {
int gift=0;

Choose a reason for hiding this comment

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

이렇게 작성하면 반복문을 돌 때마다 gift 변수가 새로 정의되니 반복문 밖에서 한번만 정의해주는 게 더 좋을 것 같네요~!

cin >> gift;
pq.push(gift);
}
}

}

return 0;
}
49 changes: 49 additions & 0 deletions 05_우선순위큐/2075.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <iostream>
#include <queue> //큐 헤더파일

using namespace std;

/*
HINT : 상위 n개의 숫자에서 n번째 숫자는 가장 작은 숫자네요!
*/

/*
입력되는 수 중 n번째로 큰 수를 찾자!
-> 최종으로 상위 n개를 저장할 수 있는 구조 -> n개 이상일 때 가장 작은 값을 삭제할 수 있는 구조
-> 최소힙을 이용하자
-> 입력을 최소힙에 push하고 힙의 사이즈가 n보다 크다면 top값을 제거해주자
->최종적으로 상위 n개의 숫자가 저장되고 top값이 n번째 큰 수가 된다!
*/


int main() {

Choose a reason for hiding this comment

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

P3. 문제 조건에 맞게 잘 구현해주셨네요!


//속도 향상 코드
ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);


int n, num; //n과 각 숫자를 담을 변수
priority_queue<int, vector<int>, greater<int>> pq; //최소힙.
//greater : 오름차순 정렬 / 가장 작은 수가 스택 탑에!

//입력
cin >> n;

//연산
for (int i = 0; i < n * n; i++) {
cin >> num; //n^2개 숫자 입력받기
pq.push(num); //스택에 입력받은 수 넣기

//pq의 size를 n개 이하로 유지하여 메모리 초과 방지
if (pq.size() > n) {
pq.pop(); //pq에서 가장 작은 값 제거
}
}

//출력
//pq에 n * n개의 수 중 가장 큰 n개가 남았으므로 그 중 가장 작은 값(top)이 n번째로 큰 수
cout << pq.top();

return 0;
}
113 changes: 113 additions & 0 deletions 05_우선순위큐/2607.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#include <iostream>

using namespace std;

bool similarWord(string word, string str) {


//두 단어의 알파벳 갯수 저장
int alpW[26]={0};
int alpS[26]={0};

//두 단어의 알파벳 갯수 카운트
for(int i=0; i<word.length(); i++) {
//만약 word의 i번째 문자가 A면 alpW의 0번 인덱스가 +1
alpW[(int)word[i]-65]++; //'A'=65
Copy link

Choose a reason for hiding this comment

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

이렇게 형변환을 해주지 않아도 word[i]는 자동으로 정수형으로 들어갑니다!

}

for(int i=0; i<str.length(); i++) {
//만약 str의 i번째 문자가 B면 alpS의 1번 인덱스가 +1
alpS[(int)str[i]-65]++; //'A'=65
}




int sameAlp=0;
for(int i=0; i<26; i++) {
if(alpS[i]==alpW[i]) {
sameAlp++;
}
}

//아예 구성 같은 경우
if(sameAlp==26) {
return true;
}


//하나 바꿔서 같아지는 경우
else if(sameAlp==24) {
int swap[2]; //서로 바꿀 두 변수
int j=0;

for(int i=0; i<26; i++) {
if(alpS[i]!=alpW[i]) {
swap[j++]=i;
if(j==2) {
break;
}
}
}

if((alpW[swap[0]]-alpS[swap[0]]) ==1 && (alpW[swap[1]]-alpS[swap[1]]) ==-1) {
return true; //스와핑 가능
} else if((alpW[swap[0]]-alpS[swap[0]]) ==-1 && (alpW[swap[1]]-alpS[swap[1]]) ==1) {
return true; //스와핑 가능
}
}


//하나 더하거나 빼서 같아지는 경우
else if(sameAlp==25) {

int difAlp=0;

for(int i=0; i<26; i++) {
if(alpS[i]!=alpW[i]) {
difAlp=i;
break;
}
}

if(abs(alpW[difAlp]-alpS[difAlp])==1) {
return true;
//25개가 같은데 하나만 갯수가 1 차이나면 더하거나 빼면 돼.
}
}
Comment on lines +26 to +77
Copy link

Choose a reason for hiding this comment

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

아주 고민해서 문제를 풀어주신 흔적이 보입니다. 꼼꼼하게 경우를 나줘 주셨어요! 다만, sameAlp 변수를 이용해 개수에 차이가 있는 알파벳의 개수를 세고, 그 다음 구체적인 개수를 고려해 주셨는데 작성해주신 코드에선 불필요하게 한 번 더 두 배열의 원소를 처음부터 끝까지 비교하는 부분이 포함됩니다. 처음부터 각 알파벳의 개수의 차의 총합을 구하고, 그를 바탕으로 비슷한 단어인지를 판별한다면 더 간결하고 쉬운 코드가 될 것 같습니다.😊





return false; //위에서 안걸리면 비슷해질 수 없어
}

int main()
{

ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);

int n;
string word; //기준 단어
int cnt=0; //비슷한 단어 개수

cin >> n >> word;

while(n>1) {

string str;
cin >> str;

if(similarWord(word, str)) {
cnt++;
}

n--;
}

cout<<cnt;

return 0;
}