-
Notifications
You must be signed in to change notification settings - Fork 0
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
12-kokeunho #45
Conversation
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.
μ΄ λ¬Έμ λ μλ£κ΅¬μ‘° μκ°μ νμλ κ² κ°λ€μ
μ 체 ꡬ쑰λ κ·ΌνΈλ μ½λμ λΉμ·νκΈ΄νλ° 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;
}
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.
μ²μμλ μμμκ° λ€μ μλ³΄λ€ μμΌλ©΄ μμ νκ³ μ΄λ₯Ό 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)
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.
μμ μ νμ΄λ΄€λ λ¬Έμ λΌ λ°κ°μ μ΅λλ€! κ·Όλ° νμ΄κ° κΈ°μ΅μ΄ μλμ μ€λ«λμ νμμ΅λλ€γ γ
μ λ κ°λλμ²λΌ 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();
}
for(int i = 0; i < n - k; i++) { | ||
result.append(stack.get(i)); | ||
} |
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.
μμ forλ¬Έμ λ³΄κ³ λ΄λ¦Όμ°¨μμΌλ‘ μ λ ¬λ μλ μ¬λ°λ₯΄κ² μ κ±°νμ§ λͺ»νμ§ μλ? λΌκ³ μκ°νμλλ° n-kκΉμ§λ§ μΆλ ₯νμ¬ μμ°μ€λ μ κ±°νμ ¨λ€μ! μ λ λ°λ³΅λ¬Έ νλ² λ μΌλλ° μ΄ λ°©λ²μ΄ λ ν¨μ¨μ μΈκ² κ°μ΅λλ€. π π
π λ¬Έμ λ§ν¬
[BOJ] ν¬κ² λ§λ€κΈ° https://www.acmicpc.net/problem/2812
βοΈ μμλ μκ°
50min
β¨ μλ μ½λ
λμ μ리 μ«μκ° μ»€μ§ μλ‘ ν° μκ° λ©λλ€.
κ·Έλ¬λ―λ‘ μ λ ₯ λ°μ μ«μ λ¬Έμμ΄μ μΌμͺ½μμ μ€λ₯Έμͺ½μΌλ‘ μ΄λνλ©΄μ
λμλ₯Ό λΉκ΅νμ¬ ν° μ«μλ₯Ό λ¨κΉλλ€.
μ΄λ¬ν λμμ μ«μ μ κ±° νμλ₯Ό λ€ μ¬μ©νκ±°λ
μ€νμ΄ λΉ λκΉμ§ λ°λ³΅ν©λλ€.
π μλ‘κ² μκ²λ λ΄μ©
νμ΄λ μ½κ² μκ°ν΄λλλ°
μ€ν νμ©μ μκ°νμ§ λͺ»νκ³ μκ°λ§ λ²λ¦¬λ€ κ²μ μ°¬μ€ μ¬μ©νμ΅λλ€.
μλ£κ΅¬μ‘° 곡λΆλ₯Ό μ’ ν΄μΌκ² μ΅λλ€...
ν¬λ¦¬μ€λ§μ€ μ 보λ΄μΈμ