Skip to content
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

[그리디] 3월 26일 #6

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
39 changes: 39 additions & 0 deletions 06_그리디/라이브코딩/11047.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <iostream>
#include <vector>

using namespace std;

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

int n, k, cnt = 0;
vector<int>value;
cin >> n >> k;

value.assign(n, 0);
for (int i = 0; i < n; i++) {
cin >> value[i];
}

while (n--) {
cnt += k / value[n]; // n-1 -> n-2 -> ... -> 0
k %= value[n]; //나머지 계산 가능
}

cout << cnt;

//int n = 3;
//while (n--) {
// cout << n << "\n";
//}
//2 -> 1 -> 0 출력

//int n = 3;
//while (--n) {
// cout << n << "\n";
//}
//2 -> 1 출력

return 0;
}
38 changes: 38 additions & 0 deletions 06_그리디/라이브코딩/1931.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

typedef pair<int, int> meet;

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

int n, cnt = 0;
int end;
vector<meet>v;
cin >> n;
v.assign(n, { 0,0 });

for (int i = 0; i < n; i++) {
cin >> v[i].second >> v[i].first; // first에 끝나는 시간 넣어야 정렬!
}

sort(v.begin(), v.end()); // 정렬해야 가장 빨리 끝나는 회의 찾을 수 있음!
end = 0;

for (int i = 0; i < n; i++) {
if (end > v[i].second) {
continue;
}
cnt++;
end = v[i].first;
}

cout << cnt;


return 0;
}
41 changes: 41 additions & 0 deletions 06_그리디/라이브코딩/체육복.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <iostream>
#include <vector>

using namespace std;

int solution (int n, vector<int> lost, vector<int> reserve){
int answer = 0;
vector<int> clothes_cnt (n+1, 1); //옷의 개수를 저장하는 벡터. 초기 설정값 1.

//잃어버린 인덱스 -> --
for (int i = 0; i < lost.size(); i++) {
clothes_cnt[lost[i]]--;
}
//여벌 인덱스 -> ++
for (int i = 0; i < reserve.size(); i++) {
clothes_cnt[reserve[i]]++;
}

for (int i=0; i<= n; i++){
if (clothes_cnt[i] >=1){
continue;
}

if (clothes_cnt[i-1]==2){
clothes_cnt[i-1]--;
clothes_cnt[i]++;
}
else if (clothes_cnt[i+1]==2){
clothes_cnt[i+1]--;
clothes_cnt[i]++;
}
}

for (int i=1; i<= n; i++){
if (clothes_cnt[i] >= 1){
answer++;
}
}

return answer;
}
62 changes: 62 additions & 0 deletions 06_그리디/필수/1213.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <iostream>
#include <vector>
#include <string>

using namespace std;

const int ALPHA_NUM = 26;

Choose a reason for hiding this comment

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

많이 사용하는 숫자를 따로 상수로 선언해주신 점 좋아요!👍


string makePalindrome(string name, vector<int>& cnt) {
string ans = "";
char center_alpha;
int center_cnt = 0;

for (char c : name) {
cnt[c - 'A']++;
}

for (int i = 0; i < ALPHA_NUM; i++) {
// 써줘야 없는 글자 출력 안됨!
// 0도 2로 나누면 나머지 0이라 이 조건문 없으면 출력되기 때문!
if (cnt[i] == 0) {
continue;
}

if (cnt[i] % 2 != 0) {
Comment on lines +20 to +25

Choose a reason for hiding this comment

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

두개의 if문을 합치면 더 가독성이 높을 것 같네요!

center_alpha = i+'A';
center_cnt++;
if (center_cnt == 2) {
return "I'm Sorry Hansoo";
}
}

int n = cnt[i]/2;
while (n--) {
ans += i + 'A';
}

}

if (center_cnt == 1) {
ans += center_alpha;
}
for (int i = name.size()/2 - 1; i >= 0; i--) {
//이렇게 해야 name의 길이가 짝수든 홀수든 알맞게 거꾸로 출력됨
ans += ans[i];
}

return ans;
}

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

string name;
vector<int>cnt(ALPHA_NUM, 0);
cin >> name;

cout << makePalindrome(name, cnt);

Choose a reason for hiding this comment

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

함수 분리해서 구현하신거 너무 좋네요!


return 0;
}
60 changes: 60 additions & 0 deletions 06_그리디/필수/15662.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <iostream>
#include <vector>

using namespace std;

const int SAW_NUM = 8;

Choose a reason for hiding this comment

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

P3: 상수 선언 너무 좋습니다 최고~


void rotateWheel(pair<int, int>& rotate, vector<vector<int>>& v) {
int num = rotate.first;
int dir = rotate.second;
//if (num != 4) {
// if (v[num][3] != v[num+1][7]) {
//
// }
//
//}

return;
}

int printAnswer(vector<vector<int>>& v, int t) {
int cnt = 0;

for (int i = 1; i <= t; i++) {
if (v[i][1] == 1) {
cnt++;
}
}

return cnt;
}

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

int t, k;
vector<vector<int>> v;
pair<int, int>rotate;
cin >> t;

v.assign(t + 1, vector<int>(SAW_NUM + 1));


for (int i = 1; i <= t; i++) {
for (int j = 1; j <= SAW_NUM; j++) {
cin >> v[i][j];
}
}

cin >> k;
while (k--) {
cin >> rotate.first >> rotate.second;
rotateWheel(rotate, v);
}

cout << printAnswer(v, t);

return 0;
}
39 changes: 39 additions & 0 deletions 06_그리디/필수/17451.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <iostream>
#include <vector>

using namespace std;

long long findSpeed(vector<long long>& d) {
long long min = 0;
for (long long i : d) {
if (i > min) {
min = i;
}
else {
min = ((min-1) / i + 1) * i;
// min-1 안해주면 틀림!
// 그냥 min 하면 같은 속도 연속으로 주어졌을 때 오류
}
}
Comment on lines +8 to +17

Choose a reason for hiding this comment

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

문제의 조건을 잘 파악해서 최소 속도를 잘 찾아주셨네요!👍


return min;
}

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

int n;
vector<long long>d;
cin >> n;

d.assign(n, 0);

for (int i = n-1; i >= 0; i--) {
cin >> d[i];
}

cout << findSpeed(d);

return 0;
}