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

10-kangrae-jo #37

Merged
merged 2 commits into from
Dec 28, 2024
Merged

10-kangrae-jo #37

merged 2 commits into from
Dec 28, 2024

Conversation

kangrae-jo
Copy link
Collaborator

@kangrae-jo kangrae-jo commented Nov 30, 2024

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

[๊ตฌ๊ฐ„ ํ•ฉ ๊ตฌํ•˜๊ธฐ 4]

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

9m

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

๊ตฌ๊ฐ„ํ•ฉ ๋ฌธ์ œ๋ฅผ ์‚ดํŽด๋ณด๋˜์ค‘์— ์‹ค๋ฒ„3 ์ธ๋ฐ ์ •๋‹ต๋ฅ ์ด 30%๋Œ€์ธ ๋ฌธ์ œ๋ฅผ ๋ฐœ๊ฒฌํ•ด์„œ
๋“ค์–ด๊ฐ€์„œ ํ’€์–ด๋ดค์Šต๋‹ˆ๋‹ค.

์ฒ˜์Œ์—๋Š” ๊ทธ๋ƒฅ ์ž…๋ ฅ๋ฐ›๊ณ  ๋‚˜์„œ, ๊ตฌ๊ฐ„์ด ์ฃผ์–ด์ง€๋ฉด ๊ตฌ๊ฐ„๋งŒํผ ๋ˆ„์ ํ•ฉ์„ ํ•˜๋Š” ๋ฌธ์ œ๋กœ ํŒŒ์•…์„ ํ–ˆ์Šต๋‹ˆ๋‹ค.
๊ทธ๋Ÿฐ๋ฐ.... ๊ทธ๋ ‡๊ฒŒ ๊ตฌํ˜„์„ ํ•˜๋‹ˆ๊นŒ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋œจ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.
(๋ฌธ์ œ๋ฅผ ๋”ฑ ๋ณด๋ฉด ์‹œ๊ฐ„์ดˆ๊ณผ๊ฒ ๊ตฌ๋‚˜ ๋ผ๋Š” ๊ฑธ ์•Œ๊ฒŒ๋˜๋Š” ๊ฒฝ์ง€๊ฐ€ ๋˜๊ณ ์‹ถ์Šต๋‹ˆ๋‹ค.....)

์‹œ๊ฐ„์ดˆ๊ณผ๋ฅผ ํ•ด๊ฒฐํ•  ๋ฐฉ๋ฒ•์€ ๋ฐ”๋กœ ๋– ์˜ฌ๋ž์Šต๋‹ˆ๋‹ค.

์ฒ˜์Œ ์ˆซ์ž ๋ฐฐ์—ด ์ž…๋ ฅ์„ ๊ทธ๋ƒฅ ๋ฐ›์ง€๋ง๊ณ , 1๋ฒˆ์งธ๋ถ€ํ„ฐ i๋ฒˆ์งธ ๊นŒ์ง€์˜ ํ•ฉ์„ i๋ฒˆ์งธ ์ž๋ฆฌ์— ๋„ฃ๊ฒŒ๋” ์ˆ˜์ •ํ–ˆ์Šต๋‹ˆ๋‹ค.
(๊ณ„์‚ฐ์˜ ํŽธ์˜๋ฅผ ์œ„ํ•ด index๋ฅผ 1๋ถ€ํ„ฐ countํ–ˆ์Šต๋‹ˆ๋‹น)

๊ทธ๋Ÿฌ๋ฉด i๋ฒˆ์งธ ๋ถ€ํ„ฐ j๋ฒˆ์งธ ๊นŒ์ง€์˜ ๋ˆ„์ ํ•ฉ์€,
num[j] - num [i-1]
๋กœ ํ‘œํ˜„๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!

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

์ž…๋ ฅ์„ ๋ฐ›์„๋•Œ๋ถ€ํ„ฐ ์šฐ๋ฆฌ๊ฐ€ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ ๋ฆฌํ•œ ํ˜•ํƒœ๋กœ ๋ฐ”๊พธ๋Š” ๋ฐฉ์‹์„ ๊ฒฝํ—˜ํ•ด ๋ดค์Šต๋‹ˆ๋‹ค!

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.

์ €๋Š” ์ด ๋ฌธ์ œ ์ฒ˜์Œ๋ณด๊ณ  2์ฐจ์› ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์•ผํ•˜๋‚˜ ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค. i์™€ j๋ฅผ ์ž…๋ ฅ๋ฐ›๊ณ  ๋จผ์ € ๊ณ„์‚ฐํ•œ ํ›„ ๊ทธ ๊ฐ’์„ 2์ฐจ์› ๋ฐฐ์—ด [i][j] ๋ฒˆ์งธ์— ์ €์žฅํ•˜๋ ค๊ณ  ํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทผ๋ฐ ์ด๊ฒŒ ๋˜๋‚˜.? ๋ฉ”๋ชจ๋ฆฌ๋Š” ๋ ๊ฑฐ ๊ฐ™์€๋ฐ ๊ฐ•๋ž˜ ์ฝ”๋“œ๋„ ์•„๋งˆ 2์ฐจ์›์œผ๋กœ ํ–ˆ๊ฒ ์ง€? ํ•˜๋ฉด์„œ pr์„ ๋ณด๋Š”๋ฐ ์™„์ „ ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ตฌํ˜„๋˜์—ˆ๋”๊ตฐ์š”.... ใ„ทใ„ท

๊ทธ๋ž˜์„œ ์ฝ”๋“œ๋Š” ๊ฑฐ์˜ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ž…๋ ฅ๋ฐ›๋Š” ๋ถ€๋ถ„์ด ์•„์ฃผ ์•ฝ๊ฐ„ ๋‹ค๋ฅธ๊ฒƒ ๋นผ๊ณ ๋Š” ๋‹ค ๋˜‘๊ฐ™๋„ค์š”

code
    for (int i=1; i<=n; i++) {
        cin >> k;
        arr[i] = k + arr[i-1];
    }

Comment on lines +7 to +9
ios_base::sync_with_stdio(0);
cout.tie(0);
cin.tie(0);
Copy link
Collaborator

Choose a reason for hiding this comment

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

์—ฌ๊ธฐ์— 0์„ ๋„ฃ์–ด๋„ ์ž‘๋™์ด ๋˜๋Š”๊ตฐ์š”

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

์ €๋•Œ ์šด์ข‹๊ฒŒ ์ •๋‹ต ํ’€์ด๊ฐ€ ๋น ๋ฅด๊ฒŒ ๋– ์˜ฌ๋ž๋˜ ๊ฒƒ ๊ฐ™๋„ค์š”

๊ทธ๋ฆฌ๊ณ  ์ €๋Š” ์ €๊ธฐ์— NULL๋„ ์“ฐ๊ณ  0๋„ ์“ฐ๊ณ  ๊ตฌ๋ถ„์—†์ด ๊ทธ๋ƒฅ ์† ๊ฐ€๋Š”๋Œ€๋กœ ์“ฐ๋Š”๋ฐ,
c์—์„œ NULL์€ 0์œผ๋กœ ์ปดํŒŒ์ผ ๋˜๋Š” ๊ฑธ๋กœ ์•Œ๊ณ  ์žˆ์–ด์š”(๋˜๋Š” ์ •์ˆ˜ 0์•„์Šคํ‚ค๊ฐ€ NULL์ด๋ผ์„œ ๊ทธ๋Ÿด๊นŒ...์š”?)
์–ด์ฉ„๋“  ๊ทธ๋ž˜์„œ ์ž˜ ์ž‘๋™ํ•˜๋‚˜๋ด์š” ใ…‹ใ…‹ใ…‹ใ…‹

@kokeunho
Copy link
Collaborator

์ €๋„ ์šฐ์„  ํ’€์–ด๋ดค๋Š”๋ฐ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋– ์„œ
๊ฐ•๋ž˜๋‹˜ ์ˆ˜๋„์ฝ”๋“œ๋ฅผ ๋ณด๊ณ  ํ’€์—ˆ์Šต๋‹ˆ๋‹ค.
์ˆ™๋ จ๋„๊ฐ€ ์–ด๋Š์ •๋„ ์Œ“์ด๋ฉด ์ด๋Ÿฐ ์‹œ๊ฐ„ ์ œํ•œ์ด๋‚˜ ๋ฉ”๋ชจ๋ฆฌ ์ œํ•œ ๋ฌธ์ œ๋ฅผ ๋งŽ์ด ํ’€์–ด๋ด์•ผ๊ฒ ์Šต๋‹ˆ๋‹ค. (์ €๋„ ๋”ฑ ๋ณด๊ณ  ์ œํ•œ ์กฐ๊ฑด์— ๋งž์ถฐ์„œ ํ’€์ด๋ฅผ ๋‚ผ ๊ฒฝ์ง€์— ๋„๋‹ฌํ•˜๊ณ  ์‹ถ๋„ค์š”..)

code
   import java.util.*;

   public class Main {
       public static void main(String[] args) {
           Scanner sc = new Scanner(System.in);
           int n = sc.nextInt();
           int m = sc.nextInt();
           int[] nums = new int[n+1];
           int[] result = new int[m];

           nums[0] = 0;

           for(int i = 1; i <= n; i++) {
               nums[i] = nums[i-1] + sc.nextInt();
           }

           for(int i = 0; i < m; i++) {
               int s = sc.nextInt();
               int e = sc.nextInt();

               result[i] = nums[e] - nums[s-1];
           }

           for(int i = 0; i < m; i++) {
               System.out.println(result[i]);
           }
       }
   }

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.

์•„๋ฌด์ƒ๊ฐ์—†์ด

N, M = map(int, input().split())

sum = 0
num = list(map(int, input().split()))

for i in range(M):
    start, end = map(int, input().split())
    for j in range(end-start+1):
        atom = num[start -1+j]
        sum += atom
    print(sum)
    sum = 0

์ด๋ ‡๊ฒŒ ํ’€์—ˆ๋Š”๋ฐ
๋ฐ”๋กœ ์‹œ๊ฐ„์ดˆ๊ณผ๋œจ๋”๋ผ๊ตฌ์š” ใ…Ž,,

๊ทผ๋ฐ ์ œ๊ฐ€ํ‘ผ๊ฑธ ๋ณด๋‹ˆ N๊ฐ’์„ ์‚ฌ์šฉ๋„ ์•ˆํ–ˆ์Šต๋‹ˆ๋‹ค ใ…‹ใ…‹ใ…‹

๋ญ”๊ฐ€ ์ž˜๋ชป๋จ์„ ์ง๊ฐํ•˜๊ณ  ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ์–ด๋–ป๊ฒŒ ์ค„์ผ์ง€ ๊ณ ๋ฏผํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.

pr 5678๋ฌธ์ œ๋กœ ๋™์ ๋ฐฐ์—ด์„ ํ’€์—ˆ์—ˆ๋Š”๋ฐ
๊ทธ๋•Œ ๋ฏธ๋ฆฌ ๋™์ ๋ฐฐ์—ด์— ๊ฐ’๋“ค์„ ๊ณ„์‚ฐํ•ด๋‚˜๊ฐ€๋ฉฐ ๋‹ค ์ €์žฅํ•ด๋†“์œผ๋ฉด
๊ตณ์ด ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ฉฐ ๊ตฌ๊ฐ„ํ•ฉ์„ ๋”ํ•˜๊ณ ์žˆ์„ ํ•„์š”๊ฐ€์—†๊ณ  ์ฐพ๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

๋™์ ๋ฐฐ์—ด ์ฆ‰ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฏธ๋ฆฌ ๋งŒ๋“ค์–ด๋†”์•ผํ•˜๋‹ˆ N๊ฐ’์ด ์ด์ œ์•ผ ์“ฐ์ด๋”๊ตฐ์š” ..

N, M = map(int, input().split())

# ์ž…๋ ฅ ๋ฐฐ์—ด ๋ฐ ๋ˆ„์  ํ•ฉ ๋ฐฐ์—ด ์ƒ์„ฑ
nums = list(map(int, input().split()))
prefix = [0] * (N + 1)  # ๋™์  ๋ฐฐ์—ด(๋ฆฌ์ŠคํŠธ) ์ดˆ๊ธฐํ™”

# ๋ˆ„์  ํ•ฉ ๋ฐฐ์—ด ์ฑ„์šฐ๊ธฐ
for i in range(1, N + 1):
    prefix[i] = prefix[i - 1] + nums[i - 1]

# ๊ฐ ๊ตฌ๊ฐ„ ํ•ฉ ๊ณ„์‚ฐ
for _ in range(M):
    start, end = map(int, input().split())
    print(prefix[end] - prefix[start - 1])

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