-
Notifications
You must be signed in to change notification settings - Fork 37
[투포인터] 1976435 황채원 #321
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
base: 1976435-황채원
Are you sure you want to change the base?
[투포인터] 1976435 황채원 #321
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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; | ||
| } |
| 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; // 초기화 | ||
| 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; | ||
| } | ||
| 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) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; // 후진이 불가능하면 작동 멈춤 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)); // 치운 방 표시 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 그래프 탐색에서 방문체크는 필수적이죠. 현재 n x m 크기의 |
||
|
|
||
| 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; | ||
| } | ||
| 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); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"; | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 메인 아주 깔끔하고 좋습니다👍👍 |
||
| } | ||
| return 0; | ||
| } | ||
| 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; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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++; | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| } | ||
There was a problem hiding this comment.
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의 값을 가지죠! 세 용액의 합의 최대값은 얼마일까요🤔? 자료형만 한 번 다시 살펴봐주세요😊