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

12-kokeunho #45

Merged
merged 1 commit into from
Jan 7, 2025
Merged

12-kokeunho #45

merged 1 commit into from
Jan 7, 2025

Conversation

kokeunho
Copy link
Collaborator

πŸ”— 문제 링크

[BOJ] 크게 λ§Œλ“€κΈ° https://www.acmicpc.net/problem/2812

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

50min

✨ μˆ˜λ„ μ½”λ“œ

높은 자리 μˆ«μžκ°€ 컀질 수둝 큰 μˆ˜κ°€ λ©λ‹ˆλ‹€.
κ·ΈλŸ¬λ―€λ‘œ μž…λ ₯ 받은 숫자 λ¬Έμžμ—΄μ„ μ™Όμͺ½μ—μ„œ 였λ₯Έμͺ½μœΌλ‘œ μ΄λ™ν•˜λ©΄μ„œ
λŒ€μ†Œλ₯Ό λΉ„κ΅ν•˜μ—¬ 큰 숫자λ₯Ό λ‚¨κΉλ‹ˆλ‹€.
μ΄λŸ¬ν•œ λ™μž‘μ„ 숫자 제거 횟수λ₯Ό λ‹€ μ‚¬μš©ν•˜κ±°λ‚˜
μŠ€νƒμ΄ 빌 λ•ŒκΉŒμ§€ λ°˜λ³΅ν•©λ‹ˆλ‹€.

 while(!stack.isEmpty() && count < k && stack.peek() < c) {
                stack.pop();
                count++;
 }

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

ν’€μ΄λŠ” μ‰½κ²Œ μƒκ°ν•΄λƒˆλŠ”λ°
μŠ€νƒ ν™œμš©μ€ μƒκ°ν•˜μ§€ λͺ»ν•˜κ³  μ‹œκ°„λ§Œ 버리닀 검색 찬슀 μ‚¬μš©ν–ˆμŠ΅λ‹ˆλ‹€.
자료ꡬ쑰 곡뢀λ₯Ό μ’€ ν•΄μ•Όκ² μŠ΅λ‹ˆλ‹€...
크리슀마슀 잘 λ³΄λ‚΄μ„Έμš”

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.

이 λ¬Έμ œλ„ 자료ꡬ쑰 μ‹œκ°„μ— ν–ˆμ—ˆλ˜ 것 κ°™λ„€μš”

전체 κ΅¬μ‘°λŠ” κ·Όν˜Έλ‹˜ μ½”λ“œμ™€ λΉ„μŠ·ν•˜κΈ΄ν•œλ° while을 κ΅¬ν˜„ν•œ 뢀뢄이 쑰금 λ‹€λ¦…λ‹ˆλ‹€.
μ €λŠ” count++ν•˜λ©° 쑰건을 κ²€μ‚¬ν•œκ²Œ μ•„λ‹ˆλΌ K-- ν•˜λ©° 쑰건을 κ²€μ‚¬ν–ˆμŠ΅λ‹ˆλ‹€.

μ œκ°€ ν•œ λ°©μ‹μ—μ„œλŠ” 살짝 λ¬Έμ œκ°€ μžˆλŠ”κ²Œ...
쑰건이 잘 λ§žλŠ”λ‹€λ©΄ K개 숫자λ₯Ό λͺ¨λ‘ μ œκ±°ν•˜μ§€ λͺ»ν•œμ±„λ‘œ for문이 끝날 수 κ°€ μžˆμ—ˆμŠ΅λ‹ˆλ‹€.
( 참고 자료 )

κ·Έλž˜μ„œ for문이 λλ‚˜κ³ λ„ λ‚¨μ•„μžˆλŠ” Kλ₯Ό μ œκ±°ν•΄μ£ΌλŠ” λ‘œμ§μ„ μ μ–΄λ‘μ–΄μ•Όν–ˆμŠ΅λ‹ˆλ‹€.

그리고 좜λ ₯ν•  λ•Œ stack을 λ’€μ§‘μ–΄μ•Όν•˜λŠ”λ°... Javaμ—μ„œλŠ” stack도 인덱슀 접근이 λ˜λ‚˜λ³΄κ΅°μš”
c++μ—μ„œλŠ” 인덱슀 접근이 λΆˆκ°€λŠ₯ν•΄μ„œ topλΆ€ν„° emptyκΉŒμ§€ result에 μ €μž₯ ν›„, resultλ₯Ό 뒀집고 좜λ ₯ν•˜λŠ”....
그런 λ‘œμ§λ°–μ— 생각이 μ•ˆλ‚˜λ”λΌκ΅¬μš”

(그리고 μ•žμ—μ„œ λ§ν•œ Kλ₯Ό μ œκ±°ν•΄μ£ΌλŠ” λ‘œμ§λ„, stackμ—μ„œ 인덱슀 μ ‘κ·Όμ΄λ˜λ©΄ κ·Όν˜Έλ‹˜ 처럼 n - k 을 ν•˜λŠ” 것이 μ•„μ£Ό 쒋을 것 κ°™μ•„μš”)

CPP CODE
#include <iostream>
#include <stack>
#include <string>
#include <algorithm>

using namespace std;

int main() {
    int N, K;
    string num;

    cin >> N >> K;
    cin >> num;

    stack<int> s;
    for (int i = 0; i < N; i++) {
        int d = num[i] - '0';

        while (!s.empty() && d > s.top() && K > 0) {
            s.pop();
            K--;
        }
        s.push(d);
    }

    // λ‚¨μ•„μžˆλŠ” K만큼 제거
    while (K > 0) {
        s.pop();
        K--;
    }

    // μŠ€νƒμ„ λ’€μ§‘μ–΄μ„œ 좜λ ₯
    string result;
    while (!s.empty()) {
        result += (s.top() + '0');
        s.pop();
    }
    reverse(result.begin(), result.end());

    cout << result;

    return 0;
}

(TMI: μ˜ˆμ „μ— μ œκ°€ ν‘Ό 것을 μ°Ύμ•„λ΄€λŠ”λ° c둜 μž‘μ„±ν•΄μ„œ 직접 stack을 κ΅¬ν˜„ν•˜κ³  인덱슀 접근을 ν–ˆλ”λΌκ΅¬μš”.)

TMI CODE
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>

#define MAX 1000000

int d[MAX];
int top = -1;

void push(int num) {
	top++;
	d[top] = num;
}

void pop() {
	top--;
}

int main() {
	char n[MAX];
	scanf("%s", n);

	int k;
	scanf("%d", &k);
	int k_ = k;

	int len = strlen(n);
	for (int i = 0; i < len; i++) {
		char digit = n[i];
		int num = digit - '0';

		while (top >= 0 && num > d[top] && k > 0) {
			pop();
			k--;
		}
		push(num);
	}
	
	for (int i = 0; i < len-k_; i++)
		printf("%d", d[i]);

	return 0;
}

Copy link
Collaborator

@wnsmir wnsmir left a comment

Choose a reason for hiding this comment

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

μ²˜μŒμ—λŠ” μ•žμ˜μˆ˜κ°€ λ’€μ˜ μˆ˜λ³΄λ‹€ μž‘μœΌλ©΄ μ‚­μ œν•˜κ³  이λ₯Ό 1μ˜μžλ¦¬κΉŒμ§€ λ°˜λ³΅ν•˜λ˜, μ‚­μ œνšŸμˆ˜κ°€ Kλ₯Ό λ„˜μœΌλ©΄ μ’…λ£Œ, λκΉŒμ§€ 가도 λͺ»μ±„μšΈκ²½μš° 제일 λ’€μ—μ„œ λΆ€ν„° μ‚­μ œ 라고 μƒκ°ν•˜κ³  ν’€μ—ˆμŠ΅λ‹ˆλ‹€.

λͺ‡κ°€μ§€ ν…ŒμŠ€νŠΈλ₯Ό ν•΄λ³΄λ‹ˆ λ°”λ‘œ 잘λͺ»λ¨μ„ μ•Œμ•˜μŠ΅λ‹ˆλ‹€.
41712의 경우 1이 7보닀 μž‘μ•„ μ§€μš°κ²Œλ˜λ©΄ 4712κ°€ 되고 κ·ΈλŒ€λ‘œ λ„˜κ°€λ©΄ 1이 2보닀 μž‘μœΌλ‹ˆ 472κ°€ λ˜λŠ” 상황이 λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€.

즉 ν˜„μž¬μˆ˜κ°€ λ‹€μŒμˆ˜λ³΄λ‹€ 크면 λ‹€μŒμˆ˜λ₯Ό μ‚­μ œν•˜κ³ , λ‹€μ‹œ 처음으둜 λŒμ•„κ°€ 4와 7을 λΉ„κ΅ν•΄μ„œ 4λ₯Ό μ§€μš°λŠ” 방식을 채택해야, μ•žμžλ¦¬μˆ˜κ°€ κ°€μž₯ 큰 μˆ˜κ°€ λ˜λŠ” κ²ƒμž…λ‹ˆλ‹€.

이방식에 효율적인 μžλ£Œκ΅¬μ‘°λŠ” λ¦¬μŠ€νŠΈλ³΄λ‹¨ μŠ€νƒμ΄λΌ μƒκ°λ˜μ–΄ μŠ€νƒμ„ ν™œμš©ν•˜μ—¬ 문제λ₯Ό ν’€μ—ˆμŠ΅λ‹ˆλ‹€.

def remove_digits(number, K):
    num_str = list(str(number))  # 숫자λ₯Ό λ¬Έμžμ—΄λ‘œ λ³€ν™˜ν•˜μ—¬ 리슀트둜 λ§Œλ“¦
    stack = []  # μŠ€νƒμ„ μ‚¬μš©ν•˜μ—¬ 숫자 비ꡐ 및 μ‚­μ œ μž‘μ—… 진행

    for digit in num_str:
        # μŠ€νƒμ˜ λ§ˆμ§€λ§‰ μˆ«μžμ™€ ν˜„μž¬ 숫자λ₯Ό λΉ„κ΅ν•˜μ—¬ μ§€μšΈμ§€ κ²°μ •
        while stack and K > 0 and stack[-1] < digit:
            stack.pop()  # μž‘μ€ μˆ«μžλŠ” μ‚­μ œ
            K -= 1  # μ‚­μ œ 횟수 1번 κ°μ†Œ
        stack.append(digit)  # ν˜„μž¬ μˆ«μžλŠ” μŠ€νƒμ— μΆ”κ°€

    # K번 μ‚­μ œκ°€ λλ‚˜μ§€ μ•Šμ•˜μœΌλ©΄ λ’€μ—μ„œλΆ€ν„° μ‚­μ œ
    while K > 0:
        stack.pop()
        K -= 1

    # μŠ€νƒμ— λ‚¨μ•„μžˆλŠ” 숫자λ₯Ό ν•˜λ‚˜λ‘œ ν•©μ³μ„œ λ°˜ν™˜
    return ''.join(stack)

N, K = int(input().split())
number = list(map(int, input()))
print(remove_digits(number, K)

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.

μ˜ˆμ „μ— ν’€μ–΄λ΄€λ˜ 문제라 λ°˜κ°€μ› μŠ΅λ‹ˆλ‹€! 근데 풀이가 기얡이 μ•ˆλ‚˜μ„œ μ˜€λž«λ™μ•ˆ ν’€μ—ˆμŠ΅λ‹ˆλ‹€γ…œγ…œ

저도 κ°•λž˜λ‹˜μ²˜λŸΌ cppμ—μ„œ stack을 μ‚¬μš©ν•΄μ„œ 닡을 좜λ ₯ν•  λ•Œ μ–΄λ–»κ²Œ 해야할지 κ³ λ―Όν–ˆμ—ˆλŠ”λ° κ·Έλƒ₯ deque둜 λ°”κΎΈμ–΄μ„œ ν’€μ—ˆμŠ΅λ‹ˆλ‹€.

리뷰λ₯Ό 적을 λ•Œ λ‹€μ‹œ μƒκ°ν•΄λ³΄λ‹ˆ κ·Έλƒ₯ vectorλ₯Ό μ‚¬μš©ν•˜μ—¬ push_backκ³Ό pop_back으둜 stack처럼 μ‚¬μš©ν•˜λ©΄ λμ—ˆλ„€μš”..;

for (int i = 0; i < n; i++) {
    int num = raw[i] - '0';
    while (k > 0 && !dq.empty() && dq.back() < num) {
        dq.pop_back();
        k--;
    }
    dq.push_back(num);
}

// kκ°€ λ‚¨μ•˜μœΌλ©΄ λ’€μ˜ 숫자λ₯Ό μ—†μ•° ex) input: 54321, k: 2 -> ans: 543
while (k-- > 0) dq.pop_back();
while (!dq.empty()) {
    cout << dq.front();
    dq.pop_front();
}

Comment on lines +26 to +28
for(int i = 0; i < n - k; i++) {
result.append(stack.get(i));
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

μœ„μ˜ for문을 보고 λ‚΄λ¦Όμ°¨μˆœμœΌλ‘œ μ •λ ¬λœ μˆ˜λŠ” μ˜¬λ°”λ₯΄κ²Œ μ œκ±°ν•˜μ§€ λͺ»ν•˜μ§€ μ•Šλ‚˜? 라고 μƒκ°ν–ˆμ—ˆλŠ”λ° n-kκΉŒμ§€λ§Œ 좜λ ₯ν•˜μ—¬ μžμ—°μŠ€λ ˆ μ œκ±°ν•˜μ…¨λ„€μš”! μ €λŠ” 반볡문 ν•œλ²ˆ 더 μΌλŠ”λ° 이 방법이 더 νš¨μœ¨μ μΈκ²ƒ κ°™μŠ΅λ‹ˆλ‹€. πŸ‘ πŸ‘

Base automatically changed from 11-kokeunho to main January 6, 2025 17:08
@kokeunho kokeunho merged commit 99dae05 into main Jan 7, 2025
1 check passed
@g0rnn g0rnn deleted the 12-kokeunho branch January 7, 2025 13:45
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.

4 participants