Skip to content

[dahyeong-yun] WEEK 09 Solutions - #2827

Open
dahyeong-yun wants to merge 4 commits into
DaleStudy:mainfrom
dahyeong-yun:week-09
Open

[dahyeong-yun] WEEK 09 Solutions#2827
dahyeong-yun wants to merge 4 commits into
DaleStudy:mainfrom
dahyeong-yun:week-09

Conversation

@dahyeong-yun

@dahyeong-yun dahyeong-yun commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

๋‹ต์•ˆ ์ œ์ถœ ๋ฌธ์ œ

์ž‘์„ฑ์ž ์ฒดํฌ ๋ฆฌ์ŠคํŠธ

  • Projects์˜ ์˜ค๋ฅธ์ชฝ ๋ฒ„ํŠผ(โ–ผ)์„ ๋ˆŒ๋Ÿฌ ํ™•์žฅํ•œ ๋’ค, Week๋ฅผ ํ˜„์žฌ ์ฃผ์ฐจ๋กœ ์„ค์ •ํ•ด์ฃผ์„ธ์š”.
  • ๋ฌธ์ œ๋ฅผ ๋ชจ๋‘ ํ‘ธ์‹œ๋ฉด ํ”„๋กœ์ ํŠธ์—์„œ Status๋ฅผ In Review๋กœ ์„ค์ •ํ•ด์ฃผ์„ธ์š”.
  • ์ฝ”๋“œ ๊ฒ€ํ† ์ž 1๋ถ„ ์ด์ƒ์œผ๋กœ๋ถ€ํ„ฐ ์Šน์ธ์„ ๋ฐ›์œผ์…จ๋‹ค๋ฉด PR์„ ๋ณ‘ํ•ฉํ•ด์ฃผ์„ธ์š”.

๊ฒ€ํ† ์ž ์ฒดํฌ ๋ฆฌ์ŠคํŠธ

Important

๋ณธ์ธ ๋‹ต์•ˆ ์ œ์ถœ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋‹ค๋ฅธ ๋ถ„ PR ํ•˜๋‚˜ ์ด์ƒ์„ ๋ฐ˜๋“œ์‹œ ๊ฒ€ํ† ๋ฅผ ํ•ด์ฃผ์…”์•ผ ํ•ฉ๋‹ˆ๋‹ค!

  • ๋ฐ”๋กœ ์ด์ „์— ์˜ฌ๋ผ์˜จ PR์— ๋ณธ์ธ์„ ์ฝ”๋“œ ๋ฆฌ๋ทฐ์–ด๋กœ ์ถ”๊ฐ€ํ•ด์ฃผ์„ธ์š”.
  • ๋ณธ์ธ์ด ๊ฒ€ํ† ํ•ด์•ผํ•˜๋Š” PR์˜ ๋‹ต์•ˆ ์ฝ”๋“œ์— ํ”ผ๋“œ๋ฐฑ์„ ์ฃผ์„ธ์š”.
  • ํ† ์š”์ผ ์ „๊นŒ์ง€ PR์„ ๋ณ‘ํ•ฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์Šน์ธํ•ด์ฃผ์„ธ์š”.

@dalestudy

dalestudy Bot commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

๐Ÿ“Š dahyeong-yun ๋‹˜์˜ ํ•™์Šต ํ˜„ํ™ฉ

์ด๋ฒˆ ์ฃผ ์ œ์ถœ ๋ฌธ์ œ

๋ฌธ์ œ ๋‚œ์ด๋„ ์œ ํ˜• ๋ถ„์„
linked-list-cycle Easy โœ… ์˜๋„ํ•œ ์œ ํ˜•
maximum-product-subarray Medium โœ… ์˜๋„ํ•œ ์œ ํ˜•
pacific-atlantic-water-flow Medium โœ… ์˜๋„ํ•œ ์œ ํ˜•
sum-of-two-integers Medium โœ… ์˜๋„ํ•œ ์œ ํ˜•

๋ˆ„์  ํ•™์Šต ์š”์•ฝ

  • ํ’€์ดํ•œ ๋ฌธ์ œ: 34 / 75๊ฐœ
  • ์ด๋ฒˆ ์ฃผ ์œ ํ˜• ์ผ์น˜์œจ: 100% (4๋ฌธ์ œ ์ค‘ 4๋ฌธ์ œ ์ผ์น˜)

๋ฌธ์ œ ํ’€์ด ํ˜„ํ™ฉ

์นดํ…Œ๊ณ ๋ฆฌ ์ง„ํ–‰๋„ ์™„๋ฃŒ
Array โ– โ– โ– โ– โ– โ– โ–ก 8 / 10 (Medium 5, Easy 3)
Dynamic Programming โ– โ– โ– โ– โ– โ–กโ–ก 8 / 11 (Easy 1, Medium 7)
String โ– โ– โ– โ– โ– โ–กโ–ก 7 / 10 (Medium 4, Easy 3)
Matrix โ– โ– โ– โ– โ–กโ–กโ–ก 2 / 4 (Medium 2)
Binary โ– โ– โ– โ–กโ–กโ–กโ–ก 2 / 5 (Easy 2)
Linked List โ– โ– โ–กโ–กโ–กโ–กโ–ก 2 / 6 (Easy 2)
Heap โ– โ– โ–กโ–กโ–กโ–กโ–ก 1 / 3 (Medium 1)
Graph โ– โ– โ–กโ–กโ–กโ–กโ–ก 2 / 8 (Medium 2)
Tree โ– โ–กโ–กโ–กโ–กโ–กโ–ก 2 / 14 (Easy 1, Medium 1)
Interval โ–กโ–กโ–กโ–กโ–กโ–กโ–ก 0 / 5 โ† ์•„์ง ์‹œ์ž‘ ์•ˆ ํ•จ

๐Ÿค– ์ด ๋Œ“๊ธ€์€ GitHub App์„ ํ†ตํ•ด ์ž๋™์œผ๋กœ ์ž‘์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ”ข API ์‚ฌ์šฉ๋Ÿ‰ (gpt-5-nano)
์š”์ฒญ ์ž…๋ ฅ ํ† ํฐ ์ถœ๋ ฅ ํ† ํฐ ํ•ฉ๊ณ„ ๋น„์šฉ
1 887 101 988 $0.000085
2 1,886 118 2,004 $0.000141
3 2,209 205 2,414 $0.000192
ํ•ฉ๊ณ„ 4,982 424 5,406 $0.000419

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

๐Ÿท๏ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํŒจํ„ด ๋ถ„์„

linked-list-cycle/dahyeong-yun.java
/**
 * TC : O(n)
 *   - ์ „์ฒด ๋ฆฌ์ŠคํŠธ๋ฅผ ํ•œ๋ฒˆ ์ˆœํšŒํ•˜๋ฏ€๋กœ O(n)
 * SC : O(n)
 *   - ์ตœ๋Œ€ n๊ฐœ์˜ ๋…ธ๋“œ๋ฅผ Set์— ์ €์žฅํ•˜๋ฏ€๋กœ O(n)
 */
public class Solution {
    public boolean hasCycle(ListNode head) {
        Set<ListNode> seen = new HashSet<>();
        for(ListNode cur = head; cur !=null; cur = cur.next) {
            if(!seen.add(cur)) return true;
        }
        return false;
    }
}

/**
 * TC : O(n)
 *   - fast๊ฐ€ slow๋ฅผ ๋”ฐ๋ผ์žก์„ ๋•Œ๊นŒ์ง€ ์ตœ์•…์˜ ๊ฒฝ์šฐ 2n๋ฒˆ ๋…ธ๋“œ๋ฅผ ์ˆœํšŒํ•˜๊ณ , slow๋Š” n๋ฒˆ ์ˆœํšŒํ•˜๋ฏ€๋กœ O(n)
 * SC : O(1)
 *   -  slow, fast๊ฐ€ ๋ชจ๋‘ ํฌ์ธํ„ฐ๋งŒ์„ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ O(1)
 */
public class Solution {
    public boolean hasCycle(ListNode head) {
        ListNode slow = head;   
        ListNode fast = head;

        while(fast != null && fast.next != null) {
            slow = slow.next; 
            fast = fast.next.next;

            if(slow == fast) {
                return true;
            }
        }
        return false;
    }
}
  • ํŒจํ„ด: Hash Map / Hash Set, Two Pointers, Floydโ€™s Cycle Detection
  • ์„ค๋ช…: ์ฒซ ๋ฒˆ์งธ ํ•จ์ˆ˜๋Š” ํ•ด์‹œ์…‹์œผ๋กœ ์ด๋ฏธ ๋ณธ ๋…ธ๋“œ๋ฅผ ๊ธฐ์–ตํ•˜๋Š” Hash Map / Hash Set ํŒจํ„ด์„ ์‚ฌ์šฉํ•˜๊ณ , ๋‘ ๋ฒˆ์งธ ํ•จ์ˆ˜๋Š” ๋А๋ฆฐ ํฌ์ธํ„ฐ์™€ ๋น ๋ฅธ ํฌ์ธํ„ฐ๋ฅผ ์ด์šฉํ•œ Two Pointers(ํ”Œ๋กœ์ด๋“œ ์‚ฌ์ดํด ๊ฒ€์ถœ) ๋ฐฉ์‹์œผ๋กœ ์ˆœํšŒํ•˜๋ฉฐ ์ˆœํ™˜ ์—ฌ๋ถ€๋ฅผ ํŒ๋‹จํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ“Š ์‹œ๊ฐ„/๊ณต๊ฐ„ ๋ณต์žก๋„ ๋ถ„์„

โ„น๏ธ ์ด ํŒŒ์ผ์—๋Š” 2๊ฐ€์ง€ ํ’€์ด๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์–ด ๊ฐ๊ฐ ๋ถ„์„ํ•ฉ๋‹ˆ๋‹ค.

ํ’€์ด 1: Solution.hasCycle โ€” Time: โœ… O(n) โ†’ O(n) / Space: โœ… O(n) โ†’ O(n)
์œ ์ € ๋ถ„์„ ์‹ค์ œ ๋ถ„์„ ๊ฒฐ๊ณผ
Time O(n) O(n) โœ…
Space O(n) O(n) โœ…

ํ”ผ๋“œ๋ฐฑ: ํ•ด์‹œ ์„ธํŠธ๋ฅผ ์‚ฌ์šฉํ•ด ์ด๋ฏธ ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ๋ฅผ ๊ธฐ๋กํ•˜๋ฏ€๋กœ ์ตœ์•…์˜ ๊ฒฝ์šฐ n๊ฐœ์˜ ๋…ธ๋“œ ๊ณต๊ฐ„์ด ํ•„์š”ํ•˜๋‹ค.

๊ฐœ์„  ์ œ์•ˆ: ํ˜„์žฌ ๊ตฌํ˜„์ด ์ ์ ˆํ•ด ๋ณด์ž…๋‹ˆ๋‹ค.

ํ’€์ด 2: Solution.hasCycle โ€” Time: โœ… O(n) โ†’ O(n) / Space: โœ… O(1) โ†’ O(1)
์œ ์ € ๋ถ„์„ ์‹ค์ œ ๋ถ„์„ ๊ฒฐ๊ณผ
Time O(n) O(n) โœ…
Space O(1) O(1) โœ…

ํ”ผ๋“œ๋ฐฑ: ์ถ”๊ฐ€ ์ž๋ฃŒ๊ตฌ์กฐ ์—†์ด ๊ณ ์ •๋œ ๋‘ ํฌ์ธํ„ฐ๋งŒ์œผ๋กœ ์‚ฌ์ดํด ์—ฌ๋ถ€๋ฅผ ํŒ๋‹จํ•œ๋‹ค.

๊ฐœ์„  ์ œ์•ˆ: ํ˜„์žฌ ๊ตฌํ˜„์ด ์ ์ ˆํ•ด ๋ณด์ž…๋‹ˆ๋‹ค.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

๐Ÿท๏ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํŒจํ„ด ๋ถ„์„

maximum-product-subarray/dahyeong-yun.java
/**
 * TC : O(n)
 *   - nums ๋ฐฐ์—ด์˜ ๊ธธ์ด๋งŒํผ ์ˆœํšŒํ•˜๋ฏ€๋กœ O(n)
 * SC : O(1)
 *   - int ๋ณ€์ˆ˜๋งŒ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์œผ๋ฏ€๋กœ O(1)
 */
class Solution {
    public int maxProduct(int[] nums) {
        int len = nums.length;
        if(len == 1) return nums[0];
        
        int answer = nums[0];
        int max = nums[0];
        int min = nums[0];
        
        for(int i = 1; i < len; i++) {
            int current = nums[i];
            int productFromMax = max * nums[i];
            int productFromMin = min * nums[i];

            max = Math.max(Math.max(current, productFromMax), productFromMin);
            min = Math.min(Math.min(current, productFromMax), productFromMin);

            answer = Math.max(max, answer);
        }
        
        return answer;
    }
}
  • ํŒจํ„ด: Dynamic Programming, Bit Manipulation
  • ์„ค๋ช…: ์—ฐ์† ๋ถ€๋ถ„ ๋ฐฐ์—ด์˜ ์ตœ๋Œ€ ๊ณฑ์„ ํ˜„์žฌ ์œ„์น˜์—์„œ์˜ ์ตœ๋Œ€/์ตœ์†Œ ๊ณฑ์œผ๋กœ ๊ฐฑ์‹ ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ DP ํŒจํ„ด์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ์Šคํ…์—์„œ ์ตœ๋Œ€/์ตœ์†Œ ๊ณฑ์˜ ํ›„๋ณด๋ฅผ ๋น„๊ตํ•˜์—ฌ ์ „์ง„ํ•˜๋ฉฐ, ๋ถ€๋ถ„ ๋ฌธ์ œ ๊ฒฐ๊ณผ๋ฅผ ์ด์šฉํ•ด ์ „์ฒด ํ•ด๋ฅผ ๊ตฌํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ“Š ์‹œ๊ฐ„/๊ณต๊ฐ„ ๋ณต์žก๋„ ๋ถ„์„

์œ ์ € ๋ถ„์„ ์‹ค์ œ ๋ถ„์„ ๊ฒฐ๊ณผ
Time O(n) O(n) โœ…
Space O(1) O(1) โœ…

ํ”ผ๋“œ๋ฐฑ: ํ˜„์žฌ ๋ฐฐ์—ด ๊ธธ์ด๋งŒํผ ์ˆœํšŒํ•˜๊ณ  ์ƒ์ˆ˜ ๊ณต๊ฐ„์œผ๋กœ ๋™์ž‘ํ•œ๋‹ค.

๊ฐœ์„  ์ œ์•ˆ: ํ˜„์žฌ ๊ตฌํ˜„์ด ์ ์ ˆํ•ด ๋ณด์ž…๋‹ˆ๋‹ค.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

๐Ÿท๏ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํŒจํ„ด ๋ถ„์„

pacific-atlantic-water-flow/dahyeong-yun.java
/**
 * heights ์˜ ํ–‰์˜ ๊ฐฏ์ˆ˜ m, ์—ด์˜ ๊ฐฏ์ˆ˜๋ฅผ n ์ด๋ผ ํ•  ๋•Œ,
 * 
 * TC : O
 *   - m * n ๋งŒํผ ์ˆœํšŒ ํ•˜๋ฏ€๋กœ O(m * n)
 * SC : O(m * n)
 *   - m * n ํฌ๊ธฐ์˜ boolean ๋ฐฐ์—ด์„ 2๊ฐœ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ O(m * n) 
 * 
*
 * [์ตœ์•…์˜ ์ฝœ์Šคํƒ ๊นŠ์ด O(m * n) ์˜ˆ์‹œ ํ–‰๋ ฌ (4x4, ์ด 16๊ฐœ ์…€)]
 * 
 * ์ˆซ์ž๊ฐ€ 1๋ถ€ํ„ฐ 16๊นŒ์ง€ 1์”ฉ ์ฆ๊ฐ€ํ•˜๋ฉฐ ๋‹จ ํ•˜๋‚˜์˜ ๊ฒฝ๋กœ๋กœ๋งŒ ์ด์–ด์ง€๋Š” ํ˜•ํƒœ:
 * 
 *  (0,0) [ 1] -> [ 2] -> [ 3] -> [ 4]
 *                                  |
 *  (1,0) [ 8] <- [ 7] <- [ 6] <- [ 5]
 *          |
 *  (2,0) [ 9] -> [10] -> [11] -> [12]
 *                                  |
 *  (3,0) [16] <- [15] <- [14] <- [13]
 * 
 * - (0, 0)์—์„œ Pacific DFS ์‹œ์ž‘ ์‹œ:
 *   1 -> 2 -> 3 -> ... -> 15 -> 16 ๊นŒ์ง€ ๋ฆฌํ„ด ์—†์ด ๋‹จ์ผ ๊ฒฝ๋กœ๋กœ ์ญ‰ ์žฌ๊ท€ ํ˜ธ์ถœ๋จ.
 * - ๊ฒฐ๊ณผ: 16(m * n)๊ฐœ์˜ ํ•จ์ˆ˜ ํ˜ธ์ถœ ํ”„๋ ˆ์ž„์ด ์Šคํƒ์— ๋™์‹œ์— ์Œ“์—ฌ ์ตœ๋Œ€ ๊นŠ์ด ๋„๋‹ฌ.
 */
class Solution {
    int [][] heights;
    int rowLimit = 0;
    int colLimit = 0;
    int[][] directions = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

    public List<List<Integer>> pacificAtlantic(int[][] heights) {   
        this.heights = heights;
        this.rowLimit = heights.length;
        this.colLimit = heights[0].length;    

        boolean[][] pacific = new boolean[rowLimit][colLimit];
        boolean[][] atlantic = new boolean[rowLimit][colLimit];

        for(int row = 0; row < rowLimit; row++) {
            dfs(row, 0, heights[row][0], pacific);
            dfs(row, colLimit - 1, heights[row][colLimit - 1], atlantic);
        }

        for(int col = 0; col < colLimit; col++) {
            dfs(0, col, heights[0][col], pacific);
            dfs(rowLimit - 1, col, heights[rowLimit - 1][col], atlantic);
        }

        List<List<Integer>> answer = new ArrayList<>();
        for(int row = 0; row < rowLimit; row++) {
            for(int col = 0; col < colLimit; col++) {
                if(pacific[row][col] && atlantic[row][col]) {
                    List<Integer> comb = List.of(row, col);
                    answer.add(comb);
                }
                
            }
        }
        return answer;
    }

    public void dfs(int row, int col, int prevHeight, boolean[][] oceanFlag) {
        if(row < 0 || col < 0 || row >= rowLimit || col >= colLimit) return; 
        if(oceanFlag[row][col] || this.heights[row][col] < prevHeight) return;

        oceanFlag[row][col] = true;

        for(int[] d : directions) {
            dfs(row + d[0], col + d[1], this.heights[row][col], oceanFlag);
        }
    }
}
  • ํŒจํ„ด: Depth-First Search, Backtracking
  • ์„ค๋ช…: ์ฝ”๋“œ๋Š” ๊ฐ ๋ฐ”๋‹ค์—์„œ ๋„๋‹ฌ ๊ฐ€๋Šฅํ•œ ์นธ์„ DFS๋กœ ํƒ์ƒ‰ํ•˜๊ณ , ๋‘ ํ•ด์•ˆ์œผ๋กœ ์นจํˆฌ ๊ฐ€๋Šฅํ•œ์ง€ ์—ฌ๋ถ€๋ฅผ flags์— ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค. ์žฌ๊ท€์  DFS ํ˜ธ์ถœ ๋ฐ ๋ฐฉ๋ฌธ ์ฒดํฌ๋กœ ํƒ์ƒ‰ ๊ณต๊ฐ„์„ ์ˆœ์ฐจ์ ์œผ๋กœ ํƒ์ƒ‰ํ•˜๋Š” ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค.

๐Ÿ“Š ์‹œ๊ฐ„/๊ณต๊ฐ„ ๋ณต์žก๋„ ๋ถ„์„

์œ ์ € ๋ถ„์„ ์‹ค์ œ ๋ถ„์„ ๊ฒฐ๊ณผ
Time - O(m * n) -
Space O(m * n) O(m * n) โœ…

ํ”ผ๋“œ๋ฐฑ: ๊ฐ ์…€๋งˆ๋‹ค ํ•œ ๋ฒˆ์”ฉ DFS๊ฐ€ ํ˜ธ์ถœ๋˜์–ด ๊ฐ ๋ฐฉํ–ฅ ๋ฐฐ์—ด์— ๋ฐฉ๋ฌธ ์—ฌ๋ถ€๋ฅผ ๊ธฐ๋กํ•œ๋‹ค. ๋‘ ๋ฒˆ์˜ DFS ํƒ์ƒ‰์œผ๋กœ ์ „์ฒด ๊ฒฉ์ž๋ฅผ ์ปค๋ฒ„ํ•˜๋ฏ€๋กœ ์‹œ๊ฐ„๊ณผ ๊ณต๊ฐ„ ๋ณต์žก๋„๋Š” ๊ฒฉ์ž ํฌ๊ธฐ์— ๋น„๋ก€ํ•œ๋‹ค.

๊ฐœ์„  ์ œ์•ˆ: ํ˜„์žฌ ๊ตฌํ˜„์ด ์ ์ ˆํ•ด ๋ณด์ž…๋‹ˆ๋‹ค.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

๐Ÿท๏ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํŒจํ„ด ๋ถ„์„

sum-of-two-integers/dahyeong-yun.java
/**
 * TC : O(1)
 *   - carry๊ฐ€ ๋งค๋ฒˆ ๋ฐœ์ƒํ•ด๋„ int primitives์˜ ๋น„ํŠธ ์ˆ˜๊ฐ€ 32์ด๋ฏ€๋กœ ์ตœ๋Œ€ 32๋ฒˆ ๋ฐ˜๋ณต
 * SC : O(1)
 */
class Solution {
    public int getSum(int a, int b) {
        while(b != 0) {
            int carry = (a & b) << 1;
            a = a ^ b;
            b = carry;
        }
        return a;
    }
}
  • ํŒจํ„ด: Bit Manipulation
  • ์„ค๋ช…: ๋‘ ์ •์ˆ˜์˜ ํ•ฉ์„ ๋น„ํŠธ ์—ฐ์‚ฐ์œผ๋กœ ๊ตฌํ˜„ํ•œ ์ฝ”๋“œ๋กœ, ๋น„ํŠธ ์—ฐ์‚ฐ์œผ๋กœ ์ž๋ฆฌ ์˜ฌ๋ฆผ(carry)๊ณผ ํ•ฉ์„ ๋ฐ˜๋ณต์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ๋น„ํŠธ ํŒจํ„ด์ด๋‹ค. ๋ฐ˜๋ณต์€ carry๊ฐ€ 0์ด ๋  ๋•Œ๊นŒ์ง€ ์ง„ํ–‰๋˜๋ฉฐ, ๊ฒฐ๊ตญ ํ•ฉ์ด ๋งŒ๋“ค์–ด์ง„๋‹ค.

๐Ÿ“Š ์‹œ๊ฐ„/๊ณต๊ฐ„ ๋ณต์žก๋„ ๋ถ„์„

์œ ์ € ๋ถ„์„ ์‹ค์ œ ๋ถ„์„ ๊ฒฐ๊ณผ
Time O(1) O(1) โœ…
Space O(1) O(1) โœ…

ํ”ผ๋“œ๋ฐฑ: ๋น„ํŠธ ์—ฐ์‚ฐ๋งŒ์œผ๋กœ ํ•ฉ์„ ๊ตฌํ•˜๋Š” ๋ฃจํ”„๋ฅผ ์‚ฌ์šฉํ–ˆ๊ณ , 32๋น„ํŠธ ์ •์ˆ˜ ํ•œ๊ณ„ ๋‚ด์—์„œ carry๊ฐ€ ๋๋‚˜๋„๋ก ๊ตฌํ˜„๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

๊ฐœ์„  ์ œ์•ˆ: ํ˜„์žฌ ๊ตฌํ˜„์ด ์ ์ ˆํ•ด ๋ณด์ž…๋‹ˆ๋‹ค.

* SC : O(1)
* - slow, fast๊ฐ€ ๋ชจ๋‘ ํฌ์ธํ„ฐ๋งŒ์„ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ O(1)
*/
public class Solution {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

2๊ฐ€์ง€ ๋ฐฉ๋ฒ•์œผ๋กœ ํ’€์–ด๋ณด์‹ ๊ฒŒ ์ข‹๋„ค์š”!! ์ฝ”๋“œ๋„ ๊น”๋”ํ•˜๊ตฌ์š” ๐Ÿ‘

class Solution {
public int maxProduct(int[] nums) {
int len = nums.length;
if(len == 1) return nums[0];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

์ด ์ฝ”๋“œ๊ฐ€ ์—†์–ด๋„ ์–ด์ฐจํ”ผ ๋ฐ˜๋ณต๋ฌธ์„ ์•ˆ๋Œ๊ณ  answer๊ฐ€ ๋ฐ˜ํ™˜๋  ๊ฒƒ ๊ฐ™์€๋ฐ, ๊ตณ์ด ๋„ฃ์œผ์‹  ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”?

Comment on lines +27 to +29
int [][] heights;
int rowLimit = 0;
int colLimit = 0;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

dfs์— ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋„˜๊ธฐ๋Š” ๊ฒƒ๋„ ๊ณ ๋ คํ•ด๋ณด๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
์ฝ”๋”ฉํ…Œ์ŠคํŠธ์—์„œ๋Š” ์ƒ๊ด€์—†๊ฑฐ๋‚˜ ์กฐ๊ธˆ ๊ณผํ•  ์ˆ˜๋„ ์žˆ์„ ๊ฒƒ ๊ฐ™๊ธด ํ•œ๋ฐ,

  • dfs๊ฐ€ ์™ธ๋ถ€ ์ƒํƒœ(this.heights ๋“ฑ)์— ์˜์กดํ•˜์ง€ ์•Š๊ฒŒ ๋˜์–ด์„œ, ์ด Solution ์ธ์Šคํ„ด์Šค๊ฐ€ ์žฌ์‚ฌ์šฉ๋˜๊ฑฐ๋‚˜ ์—ฌ๋Ÿฌ ์Šค๋ ˆ๋“œ์—์„œ ๋™์‹œ์— ํ˜ธ์ถœ๋  ๋•Œ ์ƒํƒœ๊ฐ€ ๊ผฌ์ผ ์œ„ํ—˜์ด ์—†์–ด์งˆ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
  • ๋˜ dfs ์‹œ๊ทธ๋‹ˆ์ฒ˜๋งŒ ๋ด๋„ ์ด ํ•จ์ˆ˜๊ฐ€ ๋ญ˜ ํ•„์š”๋กœ ํ•˜๋Š”์ง€ ๋ฐ”๋กœ ๋“œ๋Ÿฌ๋‚˜์„œ, ๋‚˜์ค‘์— ์ด ํ•จ์ˆ˜๋งŒ ๋”ฐ๋กœ ์ฝ๊ฑฐ๋‚˜ ํ…Œ์ŠคํŠธํ•  ๋•Œ๋„ ๋” ํŽธํ•  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

๋‹ค๋งŒ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ๋Š˜์–ด๋‚˜๋Š” ํŠธ๋ ˆ์ด๋“œ์˜คํ”„๋Š” ์žˆ์–ด์„œ, ์ง€๊ธˆ์ฒ˜๋Ÿผ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ์šฉ์œผ๋กœ 1ํšŒ์„ฑ ๋กœ์ง์ด๋ฉด ์ง€๊ธˆ ๋ฐฉ์‹๋„ ์ถฉ๋ถ„ํžˆ ๊ดœ์ฐฎ์„ ๊ฒƒ ๊ฐ™๋„ค์š”!

Comment on lines +1 to +24
/**
* heights ์˜ ํ–‰์˜ ๊ฐฏ์ˆ˜ m, ์—ด์˜ ๊ฐฏ์ˆ˜๋ฅผ n ์ด๋ผ ํ•  ๋•Œ,
*
* TC : O
* - m * n ๋งŒํผ ์ˆœํšŒ ํ•˜๋ฏ€๋กœ O(m * n)
* SC : O(m * n)
* - m * n ํฌ๊ธฐ์˜ boolean ๋ฐฐ์—ด์„ 2๊ฐœ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ O(m * n)
*
*
* [์ตœ์•…์˜ ์ฝœ์Šคํƒ ๊นŠ์ด O(m * n) ์˜ˆ์‹œ ํ–‰๋ ฌ (4x4, ์ด 16๊ฐœ ์…€)]
*
* ์ˆซ์ž๊ฐ€ 1๋ถ€ํ„ฐ 16๊นŒ์ง€ 1์”ฉ ์ฆ๊ฐ€ํ•˜๋ฉฐ ๋‹จ ํ•˜๋‚˜์˜ ๊ฒฝ๋กœ๋กœ๋งŒ ์ด์–ด์ง€๋Š” ํ˜•ํƒœ:
*
* (0,0) [ 1] -> [ 2] -> [ 3] -> [ 4]
* |
* (1,0) [ 8] <- [ 7] <- [ 6] <- [ 5]
* |
* (2,0) [ 9] -> [10] -> [11] -> [12]
* |
* (3,0) [16] <- [15] <- [14] <- [13]
*
* - (0, 0)์—์„œ Pacific DFS ์‹œ์ž‘ ์‹œ:
* 1 -> 2 -> 3 -> ... -> 15 -> 16 ๊นŒ์ง€ ๋ฆฌํ„ด ์—†์ด ๋‹จ์ผ ๊ฒฝ๋กœ๋กœ ์ญ‰ ์žฌ๊ท€ ํ˜ธ์ถœ๋จ.
* - ๊ฒฐ๊ณผ: 16(m * n)๊ฐœ์˜ ํ•จ์ˆ˜ ํ˜ธ์ถœ ํ”„๋ ˆ์ž„์ด ์Šคํƒ์— ๋™์‹œ์— ์Œ“์—ฌ ์ตœ๋Œ€ ๊นŠ์ด ๋„๋‹ฌ.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

์•Œ๊ณ ๋ฆฌ์ฆ˜ ์„ค๋ช…์ด ๋˜์–ด์žˆ์–ด ์ข‹๋„ค์š”! ์ˆ˜๊ณ  ๋งŽ์œผ์…จ์Šต๋‹ˆ๋‹ค!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Development

Successfully merging this pull request may close these issues.

2 participants