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
68 changes: 68 additions & 0 deletions 11/14503.c++
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <iostream>
#include <vector>

using namespace std;
typedef pair<int,int> p;

int cnt = 0;

p direction(int d){

Choose a reason for hiding this comment

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

방향 반환하는 함수 만든거 좋아요👍

switch(d){
case(0): //북
return {-1,0};
case(1): //동
return {0,1};
case(2): //남
return {1,0};
case(3): //서
return {0,-1};
}
}

//특정 경로가 있는 탐색이므로 dfs를 사용하였다.
void move(int r, int c,int d, vector<vector<int>>& v){
if(v[r][c]==0){
cnt++;
v[r][c]=-1;
}

for(int i=0;i<4;i++){
d--;
if(d<0) d=3; //반시계 방향 회전 (북-서-남-동)
p dd = direction(d);

int next_r = r + dd.first;
int next_c = c + dd.second;

if(v[next_r][next_c]==0){ //청소할 블록이 있는 경우 dfs
move(next_r, next_c, d, v);
}
}
//주변에 청소할 블록이 없는 경우 후진
p dd = direction(d);
int next_r = r - dd.first;
int next_c = c - dd.second;

if(v[next_r][next_c]!=1) { //후진할 블록이 벽이 아닌 경우 dfs
move(next_r, next_c, d, v);
}

else cout<< cnt;

Choose a reason for hiding this comment

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

P2. 입력과 출력을 함수에서 관리하게 될 경우, 함수의 용도가 굉장히 불명확해져요. 따라서 알튜비튜에서는 입출력은 메인에서만 하도록 권장드리고 있습니다~!

exit(0);
}

int main(){
int n, m, r, c, d;
cin >> n >> m >> r >> c >> d;

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

move(r,c,d,v);

return 0;
}
53 changes: 53 additions & 0 deletions 11/20437.c++
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <iostream>
#include <vector>
#include <map>

using namespace std;
typedef pair<int,int> p;

int cnt = 0;

void find(int k, vector<vector<int>>& num, int min){
int max_length=0, min_length=min+1;
int length=0;

for(int i=0;i<27;i++){
if(num[i].size()>=k){
for(int j=0;j<=num[i].size()-k;j++){
length = num[i][j+k-1] - num[i][j] + 1;
if(length>max_length) max_length=length;
if(length<min_length) min_length=length;
}
}
}
if(min_length==min+1){
cout << -1 << "\n";
return;
}
else{
cout<< min_length << " " << max_length << "\n";
}
}

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

int t, k;
string s;

cin >> t;
while(t--){
cin >> s >> k;
vector<int> v;
vector<vector<int>> num(27,vector<int>(0,0));

for(int i=0;i<s.length();i++){
num[s[i]-'a'].push_back(i);
}
find(k, num, s.length());

}

return 0;
}
46 changes: 46 additions & 0 deletions 11/20922.c++
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <iostream>
#include <vector>

using namespace std;
const int NUM=100001;

int findMax(int k, vector<int>& v){
vector<int> num(NUM, 0); //윈도우에 들어온 수를 인덱스 번호로 저장

int max=0, length=0, start=0; //최장 길이, 현재 길이, 수열 시작 포인터터
for(int i=0;i<v.size();i++){
num[v[i]]++;

if(num[v[i]]<=k){
length++;
if(length>max) max = length;

Choose a reason for hiding this comment

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

P3. if문 내에 코드가 한 줄이더라도 중괄호로 감싸주는 게 좋습니다! 이렇게 하면 추후에 코드를 수정할 일이 있을 때 편하고 가독성도 더 좋아요 :)

continue;
}
//중복 수가 k개 이상일 경우 max값 업데이트 후 초기화
for(int j=start; j<=i;j++){
length--;
num[v[j]]--;
if(v[j]==v[i]){
start=j+1;
length++;
break;
}
}
}
Comment on lines +11 to +29

Choose a reason for hiding this comment

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

두 포인터를 각각 어떤 조건에서 이동시켜야 할지 잘 캐치해서 구현해주신 것 같습니다 👍👍


return max;
}

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

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

cout << findMax(k,v);

Choose a reason for hiding this comment

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

연산 로직을 메인 메소드의 입출력과 분리해서 구현해주셨네요! 덕분에 코드가 더 깔끔한 것 같아 좋습니다 ^^ 👍


return 0;
}
62 changes: 62 additions & 0 deletions 11/2531.c++
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <iostream>
#include <vector>

using namespace std;

// 1. "회전" 초밥이므로 회전하여 만들 수 있는 묶음도 생각한다.
//k개의 묶음을 확인하려고 한다면, 0부터 k-1까지의 초밥을 vector의 뒷부분에 넣어준다. //즉, 총 n+k크기의 vector에 초밥 정보를 저장하게 된다.

//2. 슬라이딩 윈도우 방식을 사용해서 연속 k개의 초밥 중 다른 종류의 초밥 수(cnt)를 샌다.
//이를 위해 초밥 종류에 대한 벡터 vector<int> susi(d,0)를 사용한다.
//초밥 수를 샌 후에 쿠폰 초밥의 value가 0이라면 cnt 를 1증가해준 후에 max값에 반영해주고 다시 1을 빼준다.

//3. max를 출력한다.
Comment on lines +6 to +13

Choose a reason for hiding this comment

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

알고리즘을 글로 적어주신 점 정말 좋습니다! 로직이 헷갈릴 때는 이렇게 자연어로 적어두고 구현을 하는게 도움이 많이 되는 것 같습니다 ☺️


int findMax(int k, int c, int d, vector<int>& v){
vector<int> susi(d+1,0);
int cnt=0;
for(int i=0;i<k;i++){
if(susi[v[i]]==0){
cnt++;
}
susi[v[i]]++;
}
if(susi[c]==0) cnt++;
int max = cnt;

for(int i=1;i<=v.size()-k;i++){
if(susi[c]==0) cnt--;

int old_s = v[i-1];
int new_s = v[i+k-1];

if(susi[old_s]==1) cnt--;
susi[old_s]--;
if(susi[new_s]==0) cnt++;
susi[new_s]++;

if(susi[c]==0) cnt++;
Comment on lines +28 to +38

Choose a reason for hiding this comment

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

P2. 쿠폰으로는 항상 하나의 초밥을 먹을 수 있죠. 그러면 susi[c]가 0이 될 필요가 있을까요? 항상 쿠폰을 사용한다고 생각하고 위에 초기화 과정을 수정하는 것도 한 번 고민해 보시면 좋을 것 같습니다! 이렇게 하면 쿠폰을 확인하는 if문이 여러 번 등장하는데 이 if문들을 생략할 수 있어 보입니다 :)

if(cnt>max) max=cnt;
}
return max;
}

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

//입력
vector<int> v(n+k,0);
for(int i=0;i<k;i++){
int num;
cin >> num;
v[i]=v[n+i]=num;
}
Comment on lines +49 to +54

Choose a reason for hiding this comment

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

벡터에 처음부터 n+k 크기를 할당해주셨네요! 이 풀이도 정말 좋은 것 같습니다 👍 다른 방식으로 인덱스 계산에 mod 연산을 활용하는 풀이도 한 번 생각해 보시면 더 좋을 것 같아요. 이렇게 하면 메모리 공간에서도 약간의 이점을 얻을 수 있습니다 :)

for(int i=k;i<n;i++){
cin >> v[i];
}

cout << findMax(k,c,d,v);

return 0;
}