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

[투포인터] 11월 14일 #9

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
63 changes: 63 additions & 0 deletions 11_투포인터/14503.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include <iostream>
#include <vector>

using namespace std;
const int max_n = 50;
const int max_m = 50;
Comment on lines +5 to +6

Choose a reason for hiding this comment

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

상수는 대문자로 작성해주세요~! 그리고 max_n과 max_m이 같은 값을 가지고 있는 이 경우에는 const int MAX = 50;으로 한 번만 선언해줘도 될 것 같네요!


const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};

int cleanRoom(int n, int m, int robotr, int robotc, int robotf, int room[max_n][max_m])
{
int count = 0;

while (true) {
if (room[robotr][robotc] == 0) {
room[robotr][robotc] = -1;
count++;
}

if (room[robotr - 1][robotc] != 0 && room[robotr][robotc + 1] != 0 &&
room[robotr + 1][robotc] != 0 && room[robotr][robotc - 1] != 0) {
Comment on lines +21 to +22

Choose a reason for hiding this comment

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

주변 4개의 방향 탐색을 하나의 조건문으로 한 번에 진행해주었군요! 이 방법도 좋지만 미리 상수로 선언해 놓은 방향 배열을 사용해서 더 가독성 좋은 코드를 작성할 수 있을 것 같아요!

int backr = robotr - dx[robotf];
int backc = robotc - dy[robotf];
if (room[backr][backc] == 1) {
break;
}
robotr = backr;
robotc = backc;
} else {
robotf = (robotf + 3) % 4;
int nextr = robotr + dx[robotf];
int nextc = robotc + dy[robotf];

if (room[nextr][nextc] == 0) {
robotr = nextr;
robotc = nextc;
}
}
}

return count;
}


int main()
{
int n, m, result, robot_r, robot_c, robot_f;
int room[max_n][max_m];
cin >> n >> m;
cin >> robot_r >> robot_c >> robot_f;

for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
cin >> room[i][j];
}
}

result = cleanRoom(n, m, robot_r, robot_c, robot_f, room);
cout << result;

return 0;
}
40 changes: 40 additions & 0 deletions 11_투포인터/20922.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <iostream>
#include <vector>

using namespace std;
const int max_num = 100001;

Choose a reason for hiding this comment

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

P3. 숫자 리터럴을 상수로 선언해 사용하신 것 정말 좋습니다!! 상수는 보통 네이밍할 때 대문자를 사용하니까 대문자로 작성해주시면 더 좋을 것 같아요


int maxLength(int n, int k, vector<int>& list)
{
vector<int> count(max_num, 0);
int max_length = 0;
int left = 0;

for(int right = 0; right < n; right++){
count[list[right]] += 1;

while(count[list[right]] > k){
count[list[left]] -= 1;
left++;
}
Comment on lines +16 to +19

Choose a reason for hiding this comment

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

left 포인터를 이동해야 하는 조건을 잘 캐치하고 구현해주신 것 같습니다 👍 👍

max_length = max(max_length, right-left+1);
}
return max_length;
}


int main()
{
int n, k, result;
cin >> n >> k;

vector<int> list(n);
for(int i = 0; i < n; i++){s
cin >> list[i];
}

result = maxLength(n, k, list);
cout << result;

return 0;
}
57 changes: 57 additions & 0 deletions 11_투포인터/2531.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include <iostream>
#include <vector>

using namespace std;

int maxSushi(int n, int d, int k, int c, vector<int>& belt)
{
vector<int> count(d+1, 0);
int plate = 0;
int max_plate = 0;

for(int i = 0; i < k; i++){
if(count[belt[i]] == 0){
plate += 1;
}
count[belt[i]] += 1;
}

for(int i = 0; i < n; i++){
count[belt[i]] -= 1;
if(count[belt[i]] == 0){
plate -= 1;
}

int temp = (i+k) % n;
count[belt[temp]] += 1;
if(count[belt[temp]] == 1){
plate += 1;
}
Comment on lines +20 to +29

Choose a reason for hiding this comment

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

포인터 계산이나 두 포인터를 이용해서 plate를 계산하는 로직 모두 잘 구현해 주신 것 같습니다!🤗🤗


if(count[c] == 0){
max_plate = max(max_plate, plate + 1);
}
else{
max_plate = max(max_plate, plate);
}
Comment on lines +31 to +36

Choose a reason for hiding this comment

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

P3. 쿠폰으로 먹은 초밥을 미리 초기화해두면 어떨까요? 그러면 if문을 생략할 수 있어 보입니다 :)


}
return max_plate;
}


int main()
{
int n, d, k, c, result;
cin >> n >> d >> k >> c;

vector<int> belt(n);
for(int i = 0; i < n; i++){
cin >> belt[i];
}

result = maxSushi(n, d, k, c, belt);

Choose a reason for hiding this comment

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

연산부를 별도 함수로 분리해주셨네요! 덕분에 코드가 정말 깔끔해보이는 것 같아 좋습니다 👍👍👍

cout << result;

return 0;
}