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-H0ngJu #164

Merged
merged 2 commits into from
Mar 27, 2024
Merged

6-H0ngJu #164

merged 2 commits into from
Mar 27, 2024

Conversation

H0ngJu
Copy link
Collaborator

@H0ngJu H0ngJu commented Mar 19, 2024

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

์˜คํฐ์ˆ˜

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

2์‹œ๊ฐ„

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

์ผ๋‹จ, ์ฒ˜์Œ์—๋Š” ๋ฌธ์ œ์—์„œ ์š”๊ตฌํ•˜๋Š” ๋ฐ”๋ฅผ ๋”ฐ๋ผ๊ฐ€๋ฉด์„œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์˜€๋‹ค.

  1. arr์„ ์ž…๋ ฅ๋ฐ›๊ณ , arr์ด ๋นŒ ๋•Œ๊นŒ์ง€ ์•„๋ž˜๋ฅผ ๋ฐ˜๋ณต
  2. arr์—์„œ popleftํ•œ ์›์†Œ์™€ ๋‚จ์€ arr ์›์†Œ๋ฅผ ๋น„๊ตํ•˜๋ฉฐ ์™ผ์ชฝ์— ์žˆ์œผ๋ฉด์„œ arr.popleft()๋ณด๋‹ค ํฐ ์ˆ˜๋ฅผ ์ฐพ๋Š”๋‹ค
    1. ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์œผ๋ฉด check์„ 1๋กœ ์„ค์ •
  3. ์กฐ๊ฑด์„ ์ถฉ์กฑํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์—†์œผ๋ฉด check = 0์ด๋ฏ€๋กœ -1๋ฅผ ๋ฐ˜ํ™˜
import sys
from collections import *

def input(): return sys.stdin.readline().strip()
N = int(input())
arr = deque(map(int, input().split()))

while arr:
    num = arr.popleft()
    answer = 0
    check = 0
    cnt = 1000001
    tmp_cnt = 0

    for _ in range(len(arr)):
        tmp = arr.popleft()
        tmp_cnt += 1
        if tmp > num and tmp_cnt < cnt:
            answer = tmp
            cnt = tmp_cnt
            check = 1
        arr.append(tmp)
    
    if not check: answer = -1

    print(answer, end=" ")

์ด ์ฝ”๋“œ์˜ ๋ฌธ์ œ์ ์€ N์ด 1000000์ด๊ณ , ์‹œ๊ฐ„ ๋ณต์žก๋„๊ฐ€ O(N^2)์ด๋ฏ€๋กœ ์‹œ๊ฐ„ ์ดˆ๊ณผ ๋ฐœ์ƒํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

์ œํ•œ ์‹œ๊ฐ„ ๋‚ด์— ํ†ต๊ณผํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” O(N logN)์ด๋‚˜ O(log N)์ด์—ฌ์•ผ ๊ฐ€๋Šฅํ•œ๋ฐ,

์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ์ค„์ด๊ธฐ์œ„ํ•ด์„œ๋Š” while ์•ˆ์˜ for๋ฌธ ๋กœ์ง์„ ๋‹ค๋ฅธ ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค.

๊ทธ๋Ÿฐ๋ฐ .. ์•„๋ฌด๋ฆฌ ์ƒ๊ฐํ•ด๋ด๋„ ์™ผ์ชฝ์— ์žˆ๋Š” ์›์†Œ๋“ค์„ ํ•˜๋‚˜์”ฉ ๋น„๊ตํ•˜๋Š” ์ˆœ๊ฐ„ O(N^2)์ด ๋˜์–ด๋ฒ„๋ฆฌ๊ณ ,

๊ทธ๋ ‡๋‹ค๊ณ  binary search๋ฅผ ํ•˜์ž๋‹ˆ, '์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ์ด ์•„๋‹Œ ์ ', '์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•œ ๋ฐฐ์—ด์„ ์ถ”๊ฐ€ ์ƒ์„ฑํ•œ๋‹ค๊ณ  ํ•˜๋”๋ผ๋„ ๊ฐ™์€ ๊ฐ’ ๋‹ค๋ฅธ ์›์†Œ์— ๋Œ€ํ•ด์„œ ๋น„๊ต๊ฐ€ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ์ '์„ ํ•ด๊ฒฐํ•  ์ˆ˜ ์—†์–ด์„œ ์ด๋ถ„ํƒ์ƒ‰๋„ ์•„๋‹ˆ๋ผ๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค.

๊ฒฐ๊ตญ ์‹œ๊ฐ„ ์ดˆ๊ณผ๋ฅผ ํ•ด๊ฒฐํ•  ๋ฐฉ๋ฒ•์„ ์ƒ๊ฐํ•˜์ง€ ๋ชปํ•ด์„œ ๋‹ค๋ฅธ ํ’€์ด๋ฅผ ์ฐธ๊ณ ํ–ˆ๋‹ค.

์ด ๋ฌธ์ œ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์ž๋ฃŒ๊ตฌ์กฐ๋Š” '์Šคํƒ'์ด๋‹ค.

ํ•ต์‹ฌ ์•„์ด๋””์–ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  1. ์ž…๋ ฅ๋ฐ›์€ ์ˆ˜์—ด arr, ๋นˆ stack์„ ์ƒ์„ฑ
  2. ์ฒ˜์Œ์—๋Š” arr.pop()์„ stack์— ๋„ฃ์–ด์ค€๋‹ค.
  3. arr์ด ๋นŒ ๋•Œ๊นŒ์ง€ ๋‹ค์Œ์„ ์‹œํ–‰ํ•œ๋‹ค.
    1. arr์˜ top์ด stack์˜ top ๋ณด๋‹ค ํฌ๋ฉด, arr์„ answer[ ]์— ๋„ฃ๋Š”๋‹ค. (stack top < arr top ์ด๊ฑฐ๋‚˜ stack์ด ๋นŒ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต)
    2. arr์—์„œ popํ•˜์—ฌ stack์— ๋„ฃ๋Š”๋‹ค.

์ œ๊ฐ€ ์ฐธ๊ณ ํ•œ ๋ธ”๋กœ๊ทธ๋ฅผ ์ฒจ๋ถ€ํ•ฉ๋‹ˆ๋‹ค

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

  • ๊ณจ๋“œ 4๋Š” ,, ์–ด๋ ต๋‹ค ,, (์ด ์ •๋„์ผ์ค„์€ ๋ชฐ๋ž์Œ)

์ง€๊ธˆ ๋‹ค์‹œ ์ƒ๊ฐํ•ด๋ด๋„ ์–ด๋””์„œ ์Šคํƒ์„ ๋– ์˜ฌ๋ ค์•ผ ํ•˜๋Š”์ง€ ์˜๋ฌธ ....... (์˜๋ฌธ์„ ํ•ด๊ฒฐํ•˜๋ฉด ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ์— ์ถ”๊ฐ€ ์˜ˆ์ •)

Comment on lines +7 to +8
arr = list(map(int, input().split()))
arr.reverse()
Copy link
Collaborator

@pknujsp pknujsp Mar 20, 2024

Choose a reason for hiding this comment

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

arr์„ ๋’ค์ง‘๊ฑฐ๋‚˜ pop()ํ•ด์„œ ์ง€์šฐ๋Š” ์‹์œผ๋กœ arr์„ ํฌ๊ฒŒ ๊ฑด๋“œ๋ฆฌ์ง€ ์•Š์•„๋„ ๋ผ์š”

stack์—์„œ ์›์†Œ๋ฅผ ๊บผ๋‚ด์„œ answer์— ์˜คํฐ์ˆ˜๋ฅผ ๋„ฃ์–ด์ฃผ๋Š” ์ƒํ™ฉ์—์„œ๋Š”,
์Šคํƒ์— ์žˆ๋˜ ์›์†Œ์˜ ์ธ๋ฑ์Šค๊ฐ€ ํ˜„์žฌ ์ˆœํšŒ์ค‘์ธ arr์˜ ์›์†Œ ์ธ๋ฑ์Šค๋ณด๋‹ค ํ•ญ์ƒ ์•ž์— ์žˆ์–ด์š”

๊ทธ๋ž˜์„œ arr์„ ๊ฑด๋“œ๋ฆฌ์ง€ ์•Š๊ณ , ์ˆœ์„œ๋Œ€๋กœ ์ˆœํšŒํ•˜๋ฉด์„œ ์ธ๋ฑ์Šค๋งŒ ์Šคํƒ์— ๋„ฃ์–ด์ฃผ๋ฉด์„œ ์ง„ํ–‰ํ•˜๋ฉด ๋ผ์š”

  1. arr์„ ์ˆœ์„œ๋Œ€๋กœ ์ˆœํšŒํ•ฉ๋‹ˆ๋‹ค
  2. ํ˜„์žฌ ์›์†Œ๊ฐ€ ์Šคํƒ์— ์žˆ๋Š” ์›์†Œ๋ณด๋‹ค ํฌ๋ฉด, ๊ทธ ์›์†Œ์˜ ์˜คํฐ์ˆ˜๋กœ ์„ค์ •ํ•ด์ค๋‹ˆ๋‹ค
  3. ์Šคํƒ์ด ๋น„๊ฑฐ๋‚˜ ํ˜„์žฌ ์›์†Œ๋ณด๋‹ค ํฐ ์›์†Œ๋ฅผ ๋งŒ๋‚  ๋•Œ๊นŒ์ง€ ์ด๊ฑธ ๊ณ„์†ํ•ฉ๋‹ˆ๋‹ค
  4. ํ˜„์žฌ ์›์†Œ์˜ ์ธ๋ฑ์Šค๋ฅผ ์Šคํƒ์— ๋„ฃ์Šต๋‹ˆ๋‹ค

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด, arr์„ ๋”ฐ๋กœ ๊ฑด๋“œ๋ฆฌ์ง€ ์•Š๊ณ , ํ•œ ๋ฒˆ๋งŒ ๋ด๋„ ๋ชจ๋“  ์˜คํฐ์ˆ˜๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ์–ด์š”
๋ฉ”๋ชจ๋ฆฌ๋„ ๋”ฐ๋กœ ํŠœํ”Œ์„ ์ƒ์„ฑํ•˜๋ฉด์„œ ์ง„ํ–‰ํ•˜์ง€ ์•Š์œผ๋‹ˆ ๋งŽ์ด ์ค„์–ด๋“ค์–ด์š”

N = int(input())
A = list(map(int, input().split()))

stack = deque()
result = [-1] * N

for i in range(N):
    while stack and A[stack[-1]] < A[i]:
        result[stack.pop()] = A[i]
        
    stack.append(i)

print(*result)

Copy link
Member

@tgyuuAn tgyuuAn left a comment

Choose a reason for hiding this comment

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

์‚ฌ์‹ค ๋‚ด๋ถ€ ๊ตฌํ˜„๋งŒ ๋‹ค๋ฅผ ๋ฟ,

ํฐ ๊ตฌ์กฐ๋Š” ๊ฐ™์„ ๊ฑฐ๋ผ๊ณ  ์ƒ๊ฐ์ด ๋ฉ๋‹ˆ๋‹ค.

total_numbers_count = int(input())
numbers = list(map(int,input().split()))

answer = [-1 for _ in range(total_numbers_count)]
temp = []

for idx, i in enumerate(range(len(numbers))):
    now = numbers[i]

    while temp:
        if temp[-1][1] < now:
            temp2 = temp.pop()
            answer[temp2[0]] = now
        else:
            break

    temp.append([idx,now])
        
print(*answer)

์ด ๋ฌธ์ œ ์ •๋ง ์ž๋ฃŒ๊ตฌ์กฐ์— ๋Œ€ํ•ด ์ž˜ ์•Œ๊ณ  ์žˆ๋Š๋ƒ๋ฅผ ๋ฌผ์–ด๋ณด๋Š” ๋ฌธ์ œ์ธ ๊ฒƒ ๊ฐ™์•„์š”.




์ด ๋ฌธ์ œ ๊ธฐ์–ตํ•ด๋‘์…จ๋‹ค๊ฐ€, ๋‚˜์ค‘์— ์ž๋ฃŒ๊ตฌ์กฐ ์ต์ˆ™ํ•ด์ง€์‹œ๋ฉด ๋‹ค์‹œ ํ’€์–ด๋ณด๋Š” ๊ฒƒ์„ ์ถ”์ฒœ๋“œ๋ฆฝ๋‹ˆ๋‹ค!!!

@Munbin-Lee
Copy link
Member

#include <iostream>
#include <vector>
#include <stack>

using namespace std;

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

    int N;
    cin >> N;

    vector<int> A(N), answer(N, -1);

    for (int &num: A) {
        cin >> num;
    }

    stack<int> st;

    for (int i = N - 1; i >= 0; i--) {
        while (!st.empty() && st.top() <= A[i]) {
            st.pop();
        }

        if (!st.empty()) {
            answer[i] = st.top();
        }

        st.emplace(A[i]);
    }

    for (int num: answer) {
        cout << num << ' ';
    }

    return 0;
}

๊ณ ์ƒํ•˜์…จ์Šต๋‹ˆ๋‹ค.

์ œ๊ฐ€ ๊ฑฐ์นœ ์‚ฌ๊ณ ๋ฐฉ์‹์€

์•ž์—์„œ๋ถ€ํ„ฐ ์ฒ˜๋ฆฌํ•˜๋ฉด ๋Œ€์†Œ๊ฐ€ ๊ฐ€์น˜์— ์˜ํ–ฅ์„ ๋ฏธ์น˜์ง€ ์•Š๋Š”๋‹ค

-> ๋’ค์—์„œ๋ถ€ํ„ฐ ์ฒ˜๋ฆฌํ•˜๋ฉด ์ด๋ฅธ ์ˆซ์ž๊ฐ€ ๋” ํด ๊ฒฝ์šฐ ๋” ๊ฐ€์น˜๊ฐ€ ๋†’๋‹ค

-> ์Šคํƒ์„ ์‚ฌ์šฉํ•˜์—ฌ ์‹œ๊ฐ„๋ณต์žก๋„ ์ค„์ด๊ธฐ

์ด๋ ‡๊ฒŒ ํ’€์—ˆ์–ด์š”.

@H0ngJu H0ngJu merged commit b06f1cb into main Mar 27, 2024
@H0ngJu H0ngJu deleted the 6-H0ngJu branch March 27, 2024 16:49
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