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

5-ljedd2 #139

Merged
merged 3 commits into from
Sep 11, 2024
Merged

5-ljedd2 #139

merged 3 commits into from
Sep 11, 2024

Conversation

LJEDD2
Copy link
Collaborator

@LJEDD2 LJEDD2 commented Jul 31, 2024

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

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ๊ณ ๋“์  Kit - ๋‹ค์ด๋‚˜๋ฏนํ”„๋กœ๊ทธ๋ž˜๋ฐ | ์ •์ˆ˜ ์‚ผ๊ฐํ˜•

image

๋ฌธ์ œ ์„ค๋ช…

  • ์œ„์™€ ๊ฐ™์€ ์‚ผ๊ฐํ˜•์˜ ๊ผญ๋Œ€๊ธฐ์—์„œ ๋ฐ”๋‹ฅ๊นŒ์ง€ ์ด์–ด์ง€๋Š” ๊ฒฝ๋กœ ์ค‘, ๊ฑฐ์ณ๊ฐ„ ์ˆซ์ž์˜ ํ•ฉ์ด ๊ฐ€์žฅ ํฐ ๊ฒฝ์šฐ๋ฅผ ์ฐพ์•„์•ผ ํ•œ๋‹ค.
  • ์•„๋ž˜ ์นธ์œผ๋กœ ์ด๋™ํ•  ๋•Œ๋Š” ๋Œ€๊ฐ์„  ๋ฐฉํ–ฅ์œผ๋กœ ํ•œ ์นธ ์˜ค๋ฅธ์ชฝ ๋˜๋Š” ์™ผ์ชฝ์œผ๋กœ๋งŒ ์ด๋™ ๊ฐ€๋Šฅ
    • ์˜ˆ๋ฅผ ๋“ค์–ด 3์—์„œ๋Š” ๊ทธ ์•„๋ž˜์นธ์˜ 8 ๋˜๋Š” 1๋กœ๋งŒ ์ด๋™์ด ๊ฐ€๋Šฅ

์ œํ•œ์‚ฌํ•ญ

  • ์‚ผ๊ฐํ˜•์˜ ๋†’์ด๋Š” 1 ์ด์ƒ 500 ์ดํ•˜
  • ์‚ผ๊ฐํ˜•์„ ์ด๋ฃจ๊ณ  ์žˆ๋Š” ์ˆซ์ž๋Š” 0 ์ด์ƒ 9,999 ์ดํ•˜์˜ ์ •์ˆ˜
  • ์‚ผ๊ฐํ˜•์˜ ์ •๋ณด๊ฐ€ ๋‹ด๊ธด ๋ฐฐ์—ด triangle์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ๊ฑฐ์ณ๊ฐ„ ์ˆซ์ž์˜ ์ตœ๋Œ“๊ฐ’ ๋ฐ˜ํ™˜

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

๊ณ ๋ฏผ 1์‹œ๊ฐ„ + ํ•ด์„ค ์ฐธ๊ณ 

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

1. ๋ฌธ์ œ ์ ‘๊ทผ ๋ฐ ํ’€์ด

[0,0,0,0,7]
[0,0,0,3,8]
[0,0,8,1,0]
[0,2,7,4,4]
[4,5,2,6,5]

์ฒ˜์Œ์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ผ๊ฐํ˜•์˜ ๋นˆ ๊ณต๊ฐ„์„ 0์œผ๋กœ ์ฑ„์šด ํ–‰๋ ฌ๋กœ ๋งŒ๋“ค์–ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋ ค ํ–ˆ์ง€๋งŒ ์‚ผ๊ฐํ˜• ๊ตฌ์กฐ๋ฅผ ๊ทธ๋Œ€๋กœ ํ™œ์šฉํ•˜๋Š” ๋ฐฉ์‹์ด ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค !

  1. ์‚ผ๊ฐํ˜•์˜ ๊ฐ ํ–‰์„ ์ˆœํšŒํ•˜๋ฉด์„œ ํ˜„์žฌ ์œ„์น˜์—์„œ์˜ ์ตœ๋Œ€ ํ•ฉ์„ ๊ณ„์‚ฐํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.
  2. ๊ฐ ์œ„์น˜์—์„œ ์ด์ „ ๊ฐ’๋“ค ์ค‘ ์ตœ๋Œ“๊ฐ’์„ ์„ ํƒํ•˜์—ฌ ํ˜„์žฌ ๊ฐ’๊ณผ ๋”ํ•ด์ค๋‹ˆ๋‹ค.
    • ์ฒซ๋ฒˆ์งธ ๊ฐ’์ด๋ฉด ๋ฐ”๋กœ ์œ„ ๊ฐ™์€ ์œ„์น˜ ๊ฐ’์„ ๋”ํ•ด์ค๋‹ˆ๋‹ค.
    • ๋งˆ์ง€๋ง‰ ๊ฐ’์ด๋ฉด ๋ฐ”๋กœ ์œ„ ์ขŒํ‘œ์˜ ์™ผ์ชฝ ๊ฐ’์„ ๋”ํ•ด์ค๋‹ˆ๋‹ค.
    • ๋‚˜๋จธ์ง€๋Š” ๋ฐ”๋กœ ์œ„ row์˜ ์™ผ์ชฝ ์œ„์น˜, ํ•ด๋‹น ์œ„์น˜ ๊ฐ’์„ ๋น„๊ตํ•˜์—ฌ ํฐ ๊ฐ’์„ ๋”ํ•ฉ๋‹ˆ๋‹ค.
  3. ๋งˆ์ง€๋ง‰ ๋ฆฌ์ŠคํŠธ๋ฅผ ํ†ตํ•ด ์ตœ๋Œ“๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค !

์ด๋ ‡๊ฒŒ ๋น„๊ตํ•˜๋Š” ๊ณผ์ •์„ ์กฐ๊ฑด๋ฌธ์œผ๋กœ ๋‘๊ณ  ๊ฐ’์„ ์ €์žฅํ•ด๋‚˜๊ฐ‘๋‹ˆ๋‹ค.

if col == 0:
    triangle[row][col] += triangle[row-1][col]
elif col == row:
    triangle[row][col] += triangle[row-1][col-1]
else:
    triangle[row][col] += max(triangle[row-1][col], triangle[row-1][col-1])

2. ์ตœ์ข… ์ฝ”๋“œ

def solution(triangle):
    for row in range(1, len(triangle)):
        for col in range(row + 1):
            if col == 0:
                triangle[row][col] += triangle[row-1][col]
            elif col == row:
                triangle[row][col] += triangle[row-1][col-1]
            else:
                triangle[row][col] += max(triangle[row-1][col], triangle[row-1][col-1])
    
    return max(triangle[-1])

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

@jung0115
Copy link
Member

jung0115 commented Aug 4, 2024

import java.lang.Math;

class Solution {
    public int solution(int[][] triangle) {
        for(int i = 1; i < triangle.length; i++) {
            for(int j = 0; j <= i; j++) {
                int left = j > 0 ? triangle[i - 1][j - 1] : 0;
                int right = j < i ? triangle[i - 1][j] : 0;
                triangle[i][j] += Math.max(left, right);
            }
        }
        
        int answer = 0;
        
        for(int i = 0; i < triangle.length; i++) {
            answer = Math.max(answer, triangle[triangle.length - 1][i]);
        }
        
        return answer;
    }
}

์ €๋„ ๋น„์Šทํ•˜๊ฒŒ ํ’€์ดํ–ˆ์Šต๋‹ˆ๋‹ค!! ์™ผ์ชฝ ์œ„์™€ ์˜ค๋ฅธ์ชฝ ์œ„ ๊ฐ’์„ left, right ๋ณ€์ˆ˜๋กœ ๋นผ๊ณ  ์–‘์ชฝ ๋ index์ด๋ฉด ์—†๋Š” ๋ฐฉํ–ฅ์—๋Š” 0์„ ์ง‘์–ด๋„ฃ์–ด์„œ ํฐ ๊ฐ’์„ ์ฐพ๋„๋ก ํ–ˆ์Šต๋‹ˆ๋‹ค

์ด๋ฒˆ ์ฐจ์‹œ๋„ ์ˆ˜๊ณ ํ•˜์…จ์–ด์š”!

Copy link
Member

@janghw0126 janghw0126 left a comment

Choose a reason for hiding this comment

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

์ €๋„ ์‚ผ๊ฐํ˜• ๊ตฌ์กฐ๋ฅผ ๊ทธ๋Œ€๋กœ ํ™œ์šฉํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋‚˜๊ฐ”์Šต๋‹ˆ๋‹ค.
์ œ ์ฝ”๋“œ์™€ ๊ฑฐ์˜ ์œ ์‚ฌํ•˜๋„ค์š” :)

def solution(triangle):
    # ์ฒซ ๋ฒˆ์งธ ํ–‰์€ ๊ฑด๋„ˆ๋›ฐ๊ณ  ์‚ผ๊ฐํ˜•์˜ ๊ฐ ํ–‰์„ ์ˆœํšŒํ•จ
    for i in range(1, len(triangle)):
        for j in range(i + 1):
            # ์ฒซ ๋ฒˆ์งธ ์—ด์ธ ๊ฒฝ์šฐ
            if j == 0:
                triangle[i][j] += triangle[i-1][j]
            # ๋งˆ์ง€๋ง‰ ์—ด์ธ ๊ฒฝ์šฐ
            elif j == i:
                triangle[i][j] += triangle[i-1][j-1]
            # ๊ทธ ์™ธ์˜ ๊ฒฝ์šฐ
            else:
                triangle[i][j] += max(triangle[i-1][j-1], triangle[i-1][j])
    
    # ๋งˆ์ง€๋ง‰ ํ–‰์—์„œ ์ตœ๋Œ€ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•จ
    return max(triangle[-1])

์ •์€๋‹˜์˜ ์ˆ˜๋„์ฝ”๋“œ ๋•๋ถ„์— ๋ฌธ์ œ ์ ‘๊ทผ๋ฐฉ๋ฒ•์„ ๋” ํ™•์‹คํ•˜๊ฒŒ ์ดํ•ดํ•œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
์ด๋ฒˆ PR๋„ ์ˆ˜๊ณ ํ•˜์…จ์–ด์š”!๐Ÿถ๐Ÿ€

Copy link
Collaborator

@wonjunYou wonjunYou left a comment

Choose a reason for hiding this comment

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

์˜ˆ์ „์— ๋ฐฑ์ค€์—์„œ ํ’€์–ด๋ณธ ๊ฒฝํ—˜์ด ์žˆ์–ด์„œ ์–ด์ฉŒ๋‹ค ํ’€์ด๋ฅผ ์™ธ์šฐ๊ณ (?) ์žˆ๋Š” ๋ฌธ์ œ๋„ค์š” ใ…Ž..
DP๊ฐ€ ๋– ์˜ฌ๋ฆฌ๊ธฐ๋Š” ํž˜๋“ค์ง€๋งŒ ๋ง‰์ƒ ๋ฌธ์ œ ํ’€์ด๋Š” ์ฐธ ์‹ฌํ”Œํ•œ ๊ฒƒ ๊ฐ™์•„์š”..ใ…‹ใ…‹
์ด๋ฒˆ PR๋„ ๊ณ ์ƒ ๋งŽ์œผ์…จ์Šต๋‹ˆ๋‹ค!!

def solution(triangle):
    
    for i in range(1, len(triangle)):
        for j in range(len(triangle[i])):
            
            if j == 0:
                triangle[i][j] = triangle[i-1][j] + triangle[i][j]
            
            elif j == len(triangle[i]) - 1:
                triangle[i][j] = triangle[i-1][j-1] + triangle[i][j]
            
            else:
                triangle[i][j] = max(triangle[i-1][j-1],triangle[i-1][j]) + triangle[i][j]

    return max(triangle[len(triangle) - 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