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
48 changes: 48 additions & 0 deletions 11_투 포인터/도전/13422.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <iostream>
#include <vector>
using namespace std;

int steal(int n, int m, int k, vector<int>& money) {
int count = 0;
int sum = 0;

for (int i = 0; i < n; i++) {
if (i < m) {
sum += money[i];
}
}
if (n == m) {
count = sum < k ? 1 : 0;
}
else {
for (int j = 0; j < n; j++) {
if (sum < k) {
count++;
}
sum += money[(j + m) % n] - money[j];
}
}
return count;
}

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

for (int i = 0; i < t; i++) {
int n, m, k;
cin >> n >> m >> k;

vector<int> money(n);
for (int j = 0; j < n; j++)
cin >> money[j]; // 각 집에서 보관 중인 돈의 양

int result = steal(n, m, k, money);

// 출력
cout << result << endl;
}

return 0;
}
52 changes: 52 additions & 0 deletions 11_투 포인터/도전/2473.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
using namespace std;

vector<int> findLiquid(int n, vector<int>& values) {
sort(values.begin(), values.end()); // 특성값 정렬

int nearestSum = INT_MAX; // 초기화
Copy link
Member

Choose a reason for hiding this comment

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

지금 딱 이 부분 때문에 백준에서 통과가 되고 있지 않은 것 같습니다...!문제의 조건을 한번 더 확인해주세요! INT_MAX의 값은 약 2,000,000,000인데, 각 용액의 값은 -1,000,000,000~1,000,000,000의 값을 가지죠! 세 용액의 합의 최대값은 얼마일까요🤔? 자료형만 한 번 다시 살펴봐주세요😊

vector<int> ans;

for (int i = 0; i < n - 2; i++) {
int left = i + 1;
int right = n - 1;

while (left < right) {
int mixtureSum = values[i] + values[left] + values[right];

if (abs(mixtureSum) < abs(nearestSum)) { // 절대값비교
nearestSum = mixtureSum;
ans = { values[i], values[left], values[right] }; // 업데이트
}

if (mixtureSum == 0)
return ans;
else if (mixtureSum > 0)
right--;
else
left++;
}
}

return ans;
}

int main() {
int n;
cin >> n; // 전체 용액의 수

vector<int> values(n);
for (int i = 0; i < n; i++)
cin >> values[i]; // 용액의 특성값 입력

vector<int> result = findLiquid(n, values);

// 출력
for (int i = 0; i < 3; i++)
cout << result[i] << " ";

return 0;
}
60 changes: 60 additions & 0 deletions 11_투 포인터/필수/14503.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <iostream>
#include <vector>
using namespace std;

int dx[] = { -1, 0, 1, 0 }; // 북, 동, 남, 서
int dy[] = { 0, 1, 0, -1 };

int countCleanedArea(vector<vector<int>>& room, vector<vector<int>>& cleaned, int r, int c, int d) {
Copy link
Contributor

Choose a reason for hiding this comment

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

함수 분리 좋아요 👍

int count = 0;

while (true) {
if (room[r][c] == 0 && cleaned[r][c] == 0) { // 현재 칸이 아직 청소되지 않은 경우, 현재 칸을 청소한다.
cleaned[r][c] = 1;
count++;
}

bool flag = false;
for (int i = 0; i < 4; i++) {
d = (d + 3) % 4; // 반시계 방향으로 회전
int nx = r + dx[d];
int ny = c + dy[d];

if (room[nx][ny] == 0 && cleaned[nx][ny] == 0) {
r = nx;
c = ny;
flag = true;
break;
}
}

if (!flag) {
if (room[r - dx[d]][c - dy[d]] == 1) break; // 후진이 불가능하면 작동 멈춤
Copy link
Contributor

Choose a reason for hiding this comment

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

한 줄이더라도 중괄호 사용해주세요 🥰🥰

else {
r -= dx[d];
c -= dy[d];
}
}
}

return count;
}

int main() {
int N, M, r, c, d;
cin >> N >> M;
cin >> r >> c >> d;

vector<vector<int>> room(N, vector<int>(M)); // 방 구조
vector<vector<int>> cleaned(N, vector<int>(M, 0)); // 치운 방 표시
Copy link
Contributor

Choose a reason for hiding this comment

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

그래프 탐색에서 방문체크는 필수적이죠. 현재 n x m 크기의 cleaned 벡터를 사용하셨네요! 하지만 room벡터에 방문체크를 표현할 수 있을 것 같아요! room 벡터를 활용해서 메모리 사용량을 줄여봅시다 🥰


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

cout << countCleanedArea(room, cleaned, r, c, d) << '\n';

return 0;
}
64 changes: 64 additions & 0 deletions 11_투 포인터/필수/20437.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include <iostream>
#include <vector>
#include <climits>
#include <string>
#include <algorithm>

using namespace std;

pair<int, int> solution(const string& str, int k) {

vector<int> alphabet(26, 0);
Copy link
Member

Choose a reason for hiding this comment

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

26은 알파벳의 개수를 나타내는 숫자네요! 이런 식으로 특정한 의미를 가지고 있는 숫자는 상수화 해주시면 보다 더 숫자의미를 명확하게 전달하는 데 도움이 될 것 같습니다😁


for (int j = 0; j < str.length(); j++) {
alphabet[str[j] - 'a']++; // 알파벳 수 세기
}

int minlen = INT_MAX, maxlen = -1;

for (int j = 0; j < str.length(); j++) {

if (alphabet[str[j] - 'a'] < k) continue; // k 미만에 대해서는 패스

int count = 1;
for (int l = j + 1; l < str.length(); l++) {
if (str[j] == str[l]) {
count++;
}
if (count == k) { // 업데이트
minlen = min(minlen, l - j + 1);
maxlen = max(maxlen, l - j + 1);
break;
Copy link
Member

Choose a reason for hiding this comment

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

break문을 사용해 시간초과를 적절히 막아주셨네요😍 대신 이 풀이법은 슬라이딩 윈도우보다는 이중 for문에 가까운 것 같네요! 저희가 제공해드리는 샘플 코드는 슬라이딩 윈도우 풀이법으로 제공되고 있으니 한 번 확인해보셔도 좋을 것 같네요😄

}
}
}
return make_pair(minlen, maxlen);
}

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

int t;
cin >> t;

for (int i = 0; i < t; i++) {
string str;
int k;
cin >> str >> k;

if (k == 1) {
cout << "1 1\n";
continue;
}

pair<int, int> result = solution(str, k);
if (result.first == INT_MAX || result.second == -1) {
cout << "-1\n";
}
else {
cout << result.first << " " << result.second << "\n";
}
Copy link
Member

Choose a reason for hiding this comment

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

메인 아주 깔끔하고 좋습니다👍👍

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

int longest(int N, int K, vector<int>& arr) {
int left = 0, right = 0, maxLength = 0;
unordered_map<int, int> count;
Copy link
Member

Choose a reason for hiding this comment

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

map을 사용해서 풀이해주셨네요!👍👍 가끔 map으로 count해줄 경우 map 자체가 느려서 시간초과가 나는 경우가 있는데 이런 경우에는 vector나 배열의 크기를 입력 가능한 최대 크기로 선언해준 뒤 count값을 저장해주는 방식으로 해결할 수 있으니 참고해주세요!


// 투포인터
while (right < N) {
count[arr[right]]++; // 등장 횟수 세기

while (count[arr[right]] > K) { // k 초과 시 왼쪽 포인터 이동
count[arr[left]]--;
left++;
}
Copy link
Member

Choose a reason for hiding this comment

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

투포인터 활용 너무 좋습니다😍😍


maxLength = max(maxLength, right - left + 1); // 길이 업데이트
right++;
}

return maxLength;
}

int main() {
int N, K;
Copy link
Member

Choose a reason for hiding this comment

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

저희 튜터링에서는 변수명은 스네이크 표기법을, 상수명은 대문자를 사용해 표기할 것을 권장해드리고 있습니다! 함수명, 변수명, 상수명을 구별해서 사용해주시면 코드를 이해하는 데도 많은 도움이 되니 각각의 표기법은 구별해서 사용해주시면 좋을 것 같습니다😊

cin >> N >> K;

vector<int> arr(N); // 수열 입력
for (int i = 0; i < N; i++) {
cin >> arr[i];
}

cout << longest(N, K, arr) << '\n';

return 0;
}