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

28-SeongHoonC #205

Merged
merged 2 commits into from
Jun 10, 2024
Merged

28-SeongHoonC #205

merged 2 commits into from
Jun 10, 2024

Conversation

SeongHoonC
Copy link
Collaborator

@SeongHoonC SeongHoonC commented Jun 3, 2024

πŸ”— 문제 링크

λΆ€λΆ„ν•©

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

30λΆ„

였늘의 지

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

문제λ₯Ό 보자마자 μ΅μˆ™ν•¨μ΄ λŠκ»΄μ‘Œλ‹€.
head, tail 와 head λΆ€ν„° tail μ΄μ „κΉŒμ§€μ˜ 합을 μ €μž₯ν•˜λŠ” sum 을 λ§Œλ“ λ‹€.

  1. sum 이 s 보닀 μž‘μœΌλ©΄ tail 을 μ¦κ°€μ‹œν‚€κ³  sum 에 arr[tail] 을 λ”ν•œλ‹€.
  2. sum 이 s 보닀 ν¬κ±°λ‚˜ κ°™μœΌλ©΄
    2-1. κ°œμˆ˜κ°€ μ΅œμ†ŒμΈμ§€ ν™•μΈν•˜κ³  μ΅œμ†Œκ°’μœΌλ‘œ 닡을 μ—…λ°μ΄νŠΈν•œλ‹€.
    2-2. head λ₯Ό μ¦κ°€μ‹œν‚€κ³  sum 에 arr[head] λ₯Ό λΊ€λ‹€.

μœ„ 과정을 head < tail 이고 head, tail 이 λ°°μ—΄μ˜ μ‚¬μ΄μ¦ˆλ₯Ό λ„˜κΈ°μ§€ μ•Šμ„ λ•ŒκΉŒμ§€ λ°˜λ³΅ν•œλ‹€.

import java.io.BufferedReader
import java.io.InputStreamReader
import kotlin.math.min

private lateinit var br: BufferedReader

fun main() {
    br = BufferedReader(InputStreamReader(System.`in`))
    val (n, s) = br.readLine().split(" ").map { it.toInt() }
    val arr = br.readLine().split(" ").map { it.toInt() }

    var minCount = 100_001
    var head = 0
    var tail = 1
    var sum = arr[head]

    while (true) {
        if (head >= arr.size) {
            break
        }

        // s 보닀 ν¬κ±°λ‚˜ κ°™λ‹€λ©΄
        if (sum >= s && tail > head) {
            minCount = min(minCount, tail - head)
            sum -= arr[head]
            head++
            continue
        }

        // s 보닀 μž‘λ‹€λ©΄
        if (tail >= arr.size) break
        sum += arr[tail]
        tail++
    }
    println(if (minCount == 100_001) 0 else minCount)
}

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

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.

import sys

N, S = map(int,sys.stdin.readline().split())
numbers = list(map(int,sys.stdin.readline().split()))

start = 0
end = 0
accumulate = numbers[0]
answer = int(1e9)
while start <= end:
    if accumulate >= S:
        answer = min(answer, end-start+1)
        
        accumulate -= numbers[start]
        start+=1

    elif accumulate < S:
        if end == N-1: break
        else:
            end+=1
            accumulate += numbers[end]

if answer == int(1e9): print(0)
else: print(answer)

이 문제 정말 투 포인터 μž…λ¬Έν•˜κΈ° 쒋은 문제라고 μƒκ°λ©λ‹ˆλ‹€!

문제 μΆ”μ²œ κ²Œμ‹œνŒμ— κΈ€ ν•˜λ‚˜ μ˜¬λ €μ£Όμ‹€ 수 μžˆλ‚˜μš” ?!

@SeongHoonC
Copy link
Collaborator Author

@tgyuuAn 올리렀고 ν–ˆμœΌλ‚˜ κ΅¬κ΅μˆ˜λ‹˜μ΄ 이미 μ˜¬λ¦¬μ…¨κ΅°μš”

Copy link
Collaborator

@H0ngJu H0ngJu left a comment

Choose a reason for hiding this comment

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

처음 이 문제λ₯Ό 보고 푼적이 μ—†λŠ”λ° μ–΄λ””μ„ κ°€ ν’€μ–΄λ³Έ λŠλ‚ŒμΈλ°? ν–ˆλ”λ‹ˆ
μ €λ²ˆμ— μ˜¬λ¦¬μ…¨λ˜ μ†Œμˆ˜μ˜ 연속합 λ¬Έμ œλž‘ λΉ„μŠ·ν•˜λ„€μš” ~!

투 포인터 λ³΅μŠ΅ν–ˆμŠ΅λ‹ˆλ‹€ :)

문제 ν‘ΈλŠλΌ μˆ˜κ³ ν•˜μ…¨μŠ΅λ‹ˆλ‹€ !!

import sys

def input() : return sys.stdin.readline().rstrip()

N, S = map(int, input().split())
arr = list(map(int, input().split()))

start = 0
end = 0
sum = arr[0]
total = int(1e9)

while start<=end:
    if end >= len(arr) or start>= len(arr):
        break
    if sum >= S:
        total = min(total, end-start+1)
        sum -= arr[start]
        start += 1
    else:
        end += 1
        if end < N: sum += arr[end]

if total == int(1e9):
    print(0)
else:
    print(total)

@SeongHoonC SeongHoonC merged commit 23d41be into main Jun 10, 2024
5 checks passed
@SeongHoonC SeongHoonC deleted the 28-SeongHoonC branch June 10, 2024 11:37
@H0ngJu H0ngJu mentioned this pull request Aug 19, 2024
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