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

6-kokeunho #20

Merged
merged 3 commits into from
Nov 23, 2024
Merged

6-kokeunho #20

merged 3 commits into from
Nov 23, 2024

Conversation

kokeunho
Copy link
Collaborator

@kokeunho kokeunho commented Nov 5, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

[BOJ] ์š”์„ธํ‘ธ์Šค ๋ฌธ์ œ (https://www.acmicpc.net/problem/1158)

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

30min

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

  1. n, k๋ฅผ ์ž…๋ ฅ ๋ฐ›๊ณ 
  2. LinkedList๋กœ queue๋ฅผ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.
  3. queue์— 1~n๊นŒ์ง€์˜ ์ •์ˆ˜๋ฅผ ๋„ฃ์Šต๋‹ˆ๋‹ค.
  4. queue๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ ํ์˜ 1~k-1๋ฒˆ์งธ ๊ฐ’์„ ๋นผ์„œ ๋’ค๋กœ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค.
  5. k๋ฒˆ์งธ ๊ฐ’์€ ์ถœ๋ ฅ ํ›„ ๋ฒ„๋ ค์ค๋‹ˆ๋‹ค.
  6. 4~5๋ฒˆ์„ queue๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

ํŒŒ์ด์ฌํ•˜๋‹ค๊ฐ€ ์ž๋ฐ”๋กœ ๋„˜์–ด์™€์„œ ๋งค์šฐ ๊ฐ„๋‹จํ•œ ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค.
์•„๋งˆ ๋‹น๋ถ„๊ฐ„ ์‹ค๋ฒ„ ์ •๋„ ๋˜๋Š” ๋ฌธ์ œ ํ’€๋ฉด์„œ ์ž๋ฐ”๋ฅผ ์ตํ˜€๋‚˜๊ฐˆ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
๋‹ค๋ฅธ ์Šคํ„ฐ๋””์›๋“คํ•œํ…Œ๋Š” ํฌ๊ฒŒ ๋„์›€์ด ๋  ๋ฌธ์ œ๊ฐ€ ์•„๋‹ ๊ฒƒ ๊ฐ™์•„ ์ฃ„์†กํ•œ ๋งˆ์Œ์ด ๋“œ๋„ค์š”..
์ตœ๋Œ€ํ•œ ๋น ๋ฅด๊ฒŒ ์ตํžˆ๊ณ  ๋ฌธ์ œ ๋‚œ์ด๋„๋ฅผ ์˜ฌ๋ ค๊ฐ€๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!
์ž๋ฐ”์—์„œ queue๋ฅผ ๋งŒ๋“ค ๋•Œ, ์—ฌ๋Ÿฌ ๋ชฉ์ ์— ๋”ฐ๋ผ ๊ตฌํ˜„ ๋ฐฉ๋ฒ•์ด ๋‹ฌ๋ผ์ง€๋”๊ตฐ์š”.
LinkedList, ArrayDeque, PriorityQueue ๋“ฑ๋“ฑ...
LinkedList๋Š” FIFO ๋ฐฉ์‹์˜ ํ๋กœ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ, ์ค‘๊ฐ„์— ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ์‚ญ์ œํ•  ์ผ์ด ์ ์€ ๊ฒฝ์šฐ ์œ ์šฉํ•˜๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

Java Queue ์ธํ„ฐํŽ˜์ด์Šค ๋ฉ”์„œ๋“œ

queue.add(10);  //ํ ๋์— ์š”์†Œ ์ถ”๊ฐ€, ์„ฑ๊ณต ์‹œ true ๋ฐ˜ํ™˜, ํ ๊ฐ€๋“ ์ฐฌ ๊ฒฝ์šฐ ์˜ˆ์™ธ ๋ฐœ์ƒ

queue.offer(10); // ํ ๋์— ์š”์†Œ ์ถ”๊ฐ€, ์„ฑ๊ณต ์‹œ true ๋ฐ˜ํ™˜, ์‹คํŒจ ์‹œ false ๋ฐ˜ํ™˜, ์˜ˆ์™ธ X

Integer element = queue.remove(); //ํ ์•ž ์š”์†Œ ์ œ๊ฑฐ ํ›„ ๋ฐ˜ํ™˜, ํ ๋น„์–ด์žˆ์œผ๋ฉด ์˜ˆ์™ธ ๋ฐœ์ƒ

Integer element = queue.poll(); //ํ ์•ž ์š”์†Œ ์ œ๊ฑฐ ํ›„ ๋ฐ˜ํ™˜, ๋น„์–ด์žˆ์œผ๋ฉด null ๋ฐ˜ํ™˜, ์˜ˆ์™ธ X

Integer element = queue.element(); //ํ ์•ž ์š”์†Œ ๋ฐ˜ํ™˜, ํ๊ฐ€ ๋น„์–ด์žˆ์œผ๋ฉด ์˜ˆ์™ธ ๋ฐœ์ƒ

Integer element = queue.peek(); //ํ ์•ž ์š”์†Œ ๋ฐ˜ํ™˜, ํ๊ฐ€ ๋น„์–ด์žˆ์œผ๋ฉด null ๋ฐ˜ํ™˜, ์˜ˆ์™ธ X

Copy link
Collaborator

@kangrae-jo kangrae-jo left a comment

Choose a reason for hiding this comment

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

ํ๋ฅผ ํ•™์Šตํ•˜๊ธฐ์— ์ข‹์€ ๋ฌธ์ œ์ธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!

๊ทธ์น˜๋งŒ ๊ผญ ํ๊ฐ€ ์•„๋‹ˆ์–ด๋„ ๋ฐฐ์—ด์—์„œ ๋ชจ๋“ˆ๋Ÿฌ ์—ฐ์‚ฐ์„ ํ†ตํ•ด์„œ,
์ธ๋ฑ์Šค๋ฅผ 0~N-1 ๊นŒ์ง€ ๋Œ๋ฆฌ๋Š” ๋ฐฉ์‹๋„ ์žˆ์–ด๋ณด์ด๋„ค์š”!
(๋ผ๊ณ  ์ƒ๊ฐํ–ˆ๋Š”๋ฐ ๊ฐ€๋Šฅํ• ์ง€๋Š” ๋ชจ๋ฅด๊ฒ ๋„ค์š”...)

์ €๋„ ๊ทผํ˜ธ๋‹˜ ์ฝ”๋“œ ๋ณด๊ธฐ์ „์— ํ’€์–ด๋ดค๋Š”๋ฐ ๋งค์šฐ๋งค์šฐ ๋น„์Šทํ•˜๋„ค์š”

CPP CODE
#include <iostream>
#include <queue>

using namespace std;

int main() {
    int N, K;
    cin >> N >> K;

    queue<int> q;
    for (int i = 1; i <= N; i++) {
        q.push(i);
    }

    cout << "<";
    while (!q.empty()) {
        for (int i = 0; i < K - 1; i++) {
            q.push(q.front());
            q.pop();
        }
        cout << q.front();
        q.pop();
        if (!q.empty()) {
            cout << ", ";
        }
    }
    cout << ">";

    return 0;
}

Comment on lines +26 to +28
if (!queue.isEmpty()) {
System.out.print(", ");
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

์ด๋Ÿฐ ๋ฐฉ๋ฒ•์ด ์žˆ์—ˆ๊ตฐ์š”!
์ €๋Š” ์ฒ˜์Œ์— '\b'๋กœ ๋งˆ์ง€๋ง‰ ์ฝค๋งˆ๋ฅผ ์ง€์šฐ๋ ค๊ณ  ํ–ˆ๋Š”๋ฐ ํ‹€๋ฆฐ๋‹ต์ด๋ผ๊ณ  ๋– ์„œ,
์ด๋ ‡๊ฒŒ ๊ณ ์น˜๋‹ˆ๊นŒ ๋งž๋„ค์š”!

Copy link
Collaborator

@g0rnn g0rnn left a comment

Choose a reason for hiding this comment

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

์ฒ˜์Œ์— ํ’€์—ˆ์„ ๋•Œ queue๋ฅผ ์ƒ๊ฐํ•ด๋‚ด์ง€ ๋ชปํ•ด์„œ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋–ด์—ˆ์Šต๋‹ˆ๋‹ค..
์ผ๋ฐ˜์ ์œผ๋กœ ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ’€๋ฉด ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ O(N * k) ๋ผ์„œ ์ž…๋ ฅ๋˜๋Š” ๊ฐ’์— ๋”ฐ๋ผ ์ฐจ์ด๊ฐ€ ์—„์ฒญ ์‹ฌํ•˜๋”๊ตฐ์š”..

์ด๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๊ฐ’์„ ์ œ๊ฑฐํ•˜๋Š” ์‹œ๊ฐ„์„ ์ตœ๋Œ€ํ•œ์œผ๋กœ ์ค„์ด๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.! ๊ทธ๋ž˜์„œ ์ผ๋ฐ˜ ๋ฐฐ์—ด๋กœ ํ’€๋ ค๋ฉด linkedList ์‚ฌ์šฉ์„ ์ถ”์ฒœ๋“œ๋ฆฝ๋‹ˆ๋‹ค

ํ•˜์ง€๋งŒ ์ €๋Š” ๊ทธ๋ƒฅ ์ฝ”๋“œ ๋ณด๊ณ  ํ๋กœ ํ’€์—ˆ๋„ค์š”..

code
#include <iostream>
#include <queue>
using namespace std;

int nums[5005] = { 0, };
queue<int> q;

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

	for (int i = 1; i <= n; i++)
		q.push(i);

	cout << "<";
	while (!q.empty()) {
		for (int i = 1; i < k; i++) {
			q.push(q.front());
			q.pop();
		}

		cout << q.front();
		q.pop();
		if (!q.empty()) cout << ", ";
	}
	cout << ">";
	return 0;
}
time out code
int main() {
	int n, k;
	cin >> n >> k;

	int i = 0, limit = 0;
	int cnt = 0;
	for (int i = 1; i <= n; i++)
		nums[i] = i;

	cout << "<";
	while (limit != n) {
		i = (i % n) + 1;
		if (nums[i] == 0) continue;
		++cnt;
		if (cnt == k) {
			cout << nums[i];
			nums[i] = 0;
			cnt = 0;
			limit++;
			if (limit != n) cout << ", ";
		}
	}
	cout << ">\n";
	return 0;
}

int k = scanner.nextInt();
scanner.close();

Queue<Integer> queue = new LinkedList<>();
Copy link
Collaborator

Choose a reason for hiding this comment

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

์ €๋Š” ์ด๋ฌธ์ œ๋ฅผ ๋ณด๊ณ  queue๋ฅผ ์ƒ๊ฐํ•˜๋Š”๊ฒŒ ์ ค ์–ด๋ ค์› ๋„ค์š”...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants