Skip to content

[dolphinflow86] WEEK 09 Solutions - #2826

Merged
dolphinflow86 merged 6 commits into
DaleStudy:mainfrom
dolphinflow86:main
Aug 22, 2026
Merged

[dolphinflow86] WEEK 09 Solutions#2826
dolphinflow86 merged 6 commits into
DaleStudy:mainfrom
dolphinflow86:main

Conversation

@dolphinflow86

@dolphinflow86 dolphinflow86 commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

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

  • linked-list-cycle
  • maximum-product-subarray
  • minimum-window-substring
  • pacific-atlantic-water-flow
  • sum-of-two-integers

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

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

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

Important

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

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

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/dolphinflow86.py
# N is the number of nodes in the linked list.
# TC: O(N) - visits each node at most twice
# SC: O(1) - uses only two pointers without extra space

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

from typing import Optional


class Solution:

    def hasCycle(self, head: Optional[ListNode]) -> bool:
        if not head or not head.next:
            return False

        slow = head
        fast = head

        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next

            if slow == fast:
                return True

        return False
  • ํŒจํ„ด: Two Pointers, Fast & Slow Pointers
  • ์„ค๋ช…: ํ—ค๋“œ ํฌ์ธํ„ฐ์™€ ์Šฌ๋กœ์šฐ/ํŒจ์ŠคํŠธ ํฌ์ธํ„ฐ๋ฅผ ์ด์šฉํ•ด ์ˆœํ™˜ ์—ฌ๋ถ€๋ฅผ ํŒ๋‹จํ•˜๋Š” ๋Œ€ํ‘œ์  ๋ฐฉ๋ฒ•์œผ๋กœ, ํ•œ ๋…ธ๋“œ์”ฉ ๋А๋ฆฌ๊ฒŒ, ๋‘ ๋…ธ๋“œ์”ฉ ๋น ๋ฅด๊ฒŒ ์ง„ํ–‰ํ•˜๋ฉฐ ๋งŒ๋‚˜๋Š” ์ง€์ ์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ์ถ”๊ฐ€ ๊ณต๊ฐ„ ์—†์ด O(N) ์‹œ๊ฐ„๊ณผ O(1) ๊ณต๊ฐ„์œผ๋กœ ํ•ด๊ฒฐํ•ฉ๋‹ˆ๋‹ค.

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

๋ณต์žก๋„
Time O(n)
Space O(1)

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

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

๐Ÿ’ก ํ’€์ด์— ์‹œ๊ฐ„/๊ณต๊ฐ„ ๋ณต์žก๋„๋ฅผ ์ฃผ์„์œผ๋กœ ๋‚จ๊ฒจ๋ณด์„ธ์š”!

@dalestudy

dalestudy Bot commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

๐Ÿ“Š dolphinflow86 ๋‹˜์˜ ํ•™์Šต ํ˜„ํ™ฉ

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

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

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

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

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

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

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

๐Ÿ”ข API ์‚ฌ์šฉ๋Ÿ‰ (gpt-5-nano)
์š”์ฒญ ์ž…๋ ฅ ํ† ํฐ ์ถœ๋ ฅ ํ† ํฐ ํ•ฉ๊ณ„ ๋น„์šฉ
1 1,284 127 1,411 $0.000115
2 1,291 143 1,434 $0.000122
3 2,206 249 2,455 $0.000210
4 2,206 256 2,462 $0.000213
5 2,206 199 2,405 $0.000190
6 2,200 258 2,458 $0.000213
ํ•ฉ๊ณ„ 11,393 1,232 12,625 $0.001062

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/dolphinflow86.py
# N is the length of array nums.
# TC: O(N) - single pass maintaining maximum and minimum product
# SC: O(1) - uses constant extra variables

from typing import List


class Solution:

    def maxProduct(self, nums: List[int]) -> int:
        if not nums:
            return 0

        res = nums[0]
        cur_max = nums[0]
        cur_min = nums[0]

        for i in range(1, len(nums)):
            num = nums[i]

            if num < 0:
                tmp = cur_max
                cur_max = cur_min
                cur_min = tmp

            cur_max = max(num, cur_max * num)
            cur_min = min(num, cur_min * num)

            res = max(res, cur_max)

        return res
  • ํŒจํ„ด: Two Pointers, Dynamic Programming, Bit Manipulation
  • ์„ค๋ช…: ์—ฐ์† ๋ถ€๋ถ„๋ฐฐ์—ด์˜ ๊ณฑ์˜ ์ตœ๋Œ€๊ฐ’์„ ์ฐพ๊ธฐ ์œ„ํ•ด ํ˜„์žฌ ์œ„์น˜์—์„œ ์–‘์ˆ˜/์Œ์ˆ˜ ์ „ํ™˜ ์‹œ ๋ถ€ํ˜ธ๋ฅผ ๋ฐ”๊ฟ” ๊ฐ€์žฅ ํฐ/์ž‘์€ ๊ณฑ์„ ์œ ์ง€ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ํ•œ ๋ฒˆ์˜ ์ˆœํšŒ๋กœ ํ•ด๊ฒฐํ•˜๋Š” DP ์„ฑ๊ฒฉ์˜ ํ’€์ด์ด๋ฉฐ, ๋ถ€ํ˜ธ ๋ณ€ํ™”์— ๋”ฐ๋ผ cur_max์™€ cur_min์„ ํŠธ๋ž™ํ‚นํ•˜๋Š” ์•„์ด๋””์–ด๊ฐ€ ํ•ต์‹ฌ์ด๋‹ค.

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

๋ณต์žก๋„
Time O(n)
Space O(1)

ํ”ผ๋“œ๋ฐฑ: ๋ถ€ํ˜ธ๊ฐ€ ๋’ค์ง‘ํžˆ๋Š” ๊ฒฝ์šฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด cur_max์™€ cur_min์„ ๊ต์ฐจ๋กœ ๊ด€๋ฆฌํ•œ๋‹ค.

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

๐Ÿ’ก ํ’€์ด์— ์‹œ๊ฐ„/๊ณต๊ฐ„ ๋ณต์žก๋„๋ฅผ ์ฃผ์„์œผ๋กœ ๋‚จ๊ฒจ๋ณด์„ธ์š”!

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.

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

minimum-window-substring/dolphinflow86.py
# S and T are the lengths of strings s and t respectively.
# TC: O(S + T) - expanding and contracting sliding window with frequency counts
# SC: O(S + T) - hash tables store frequency counts of unique characters

from collections import Counter


class Solution:

    def minWindow(self, s: str, t: str) -> str:
        if not s or not t:
            return ""

        target_counts = Counter(t)
        window_counts = {}

        have = 0
        need = len(target_counts)

        res = [-1, -1]
        res_len = float("inf")
        left = 0

        for right in range(len(s)):
            char = s[right]
            window_counts[char] = window_counts.get(char, 0) + 1

            if char in target_counts and window_counts[char] == target_counts[char]:
                have += 1

            while have == need:
                if (right - left + 1) < res_len:
                    res = [left, right]
                    res_len = right - left + 1

                left_char = s[left]
                window_counts[left_char] -= 1

                if left_char in target_counts and window_counts[left_char] < target_counts[left_char]:
                    have -= 1

                left += 1

        l, r = res
        return s[l : r + 1] if res_len != float("inf") else ""
  • ํŒจํ„ด: Sliding Window, Hash Map / Hash Set
  • ์„ค๋ช…: ๋ฌธ์ž์—ด s์˜ ๋ถ€๋ถ„์ฐฝ์„ ์ขŒ์šฐ๋กœ ํ™•์žฅ/์ถ•์†Œํ•˜๋ฉด์„œ ํ•„์š”ํ•œ ๋ฌธ์ž ๋นˆ๋„์ˆ˜๋ฅผ ์ถ”์ ํ•˜๋Š” ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ ํŒจํ„ด๊ณผ, ๋ฌธ์ž ๋นˆ๋„์ˆ˜๋ฅผ ์ €์žฅํ•˜๋Š” ํ•ด์‹œ ๋งต ์‚ฌ์šฉ์œผ๋กœ ํŒจํ„ด์— ํ•ด๋‹นํ•ฉ๋‹ˆ๋‹ค.

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

๋ณต์žก๋„
Time O(
Space O(

ํ”ผ๋“œ๋ฐฑ: ํƒ€๊ฒŸ ๋ฌธ์ž ๋นˆ๋„์™€ ํ˜„์žฌ ์œˆ๋„์šฐ์˜ ๋นˆ๋„๋ฅผ ์ถ”์ ํ•˜๊ณ  ํ•„์š”ํ•œ ๋ฌธ์ž ์ˆ˜๋ฅผ ๊ด€๋ฆฌํ•œ๋‹ค.

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

๐Ÿ’ก ํ’€์ด์— ์‹œ๊ฐ„/๊ณต๊ฐ„ ๋ณต์žก๋„๋ฅผ ์ฃผ์„์œผ๋กœ ๋‚จ๊ฒจ๋ณด์„ธ์š”!

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/dolphinflow86.py
# N is the number of nodes in the linked list.
# TC: O(N) - visits each node at most twice
# SC: O(1) - uses only two pointers without extra space

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None


class Solution:

    def hasCycle(self, head) -> bool:
        if not head or not head.next:
            return False

        slow = head
        fast = head

        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next

            if slow == fast:
                return True

        return False
  • ํŒจํ„ด: Two Pointers, Fast & Slow Pointers
  • ์„ค๋ช…: ํ•ด๋‹น ์ฝ”๋“œ๋Š” ๋‘ ํฌ์ธํ„ฐ(slow์™€ fast)๋ฅผ ์ด์šฉํ•ด ์ˆœํ™˜ ์—ฌ๋ถ€๋ฅผ ํŒ๋ณ„ํ•˜๋Š” Fast & Slow Pointers ๊ธฐ๋ฒ•์œผ๋กœ ๊ตฌํ˜„๋˜์—ˆ๊ณ , ๊ฐ™์€ ๋…ธ๋“œ๋ฅผ ๋งŒ๋‚˜๋ฉด ์‚ฌ์ดํด์ด ์กด์žฌํ•œ๋‹ค๋Š” ์›๋ฆฌ๋กœ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค. ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(N), ๊ณต๊ฐ„ ๋ณต์žก๋„๋Š” O(1)์ž…๋‹ˆ๋‹ค.

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

๋ณต์žก๋„
Time O(n)
Space O(1)

ํ”ผ๋“œ๋ฐฑ: ํ•ด๋‹น ๊ตฌํ˜„์€ Floyd์˜ ์‚ฌ์ดํด ํƒ์ง€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•˜์—ฌ ํ•œ ๋ฒˆ์˜ ๋ฐฐ์—ด ์ˆœํšŒ๋ฅผ ํ†ตํ•ด ์‚ฌ์ดํด ์—ฌ๋ถ€๋ฅผ ํŒ๋‹จํ•ฉ๋‹ˆ๋‹ค.

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

๐Ÿ’ก ํ’€์ด์— ์‹œ๊ฐ„/๊ณต๊ฐ„ ๋ณต์žก๋„๋ฅผ ์ฃผ์„์œผ๋กœ ๋‚จ๊ฒจ๋ณด์„ธ์š”!

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/dolphinflow86.py
# N is the length of array nums.
# TC: O(N) - single pass maintaining maximum and minimum product
# SC: O(1) - uses constant extra variables


class Solution:

    def maxProduct(self, nums) -> int:
        if not nums:
            return 0

        res = nums[0]
        cur_max = nums[0]
        cur_min = nums[0]

        for i in range(1, len(nums)):
            num = nums[i]

            if num < 0:
                tmp = cur_max
                cur_max = cur_min
                cur_min = tmp

            cur_max = max(num, cur_max * num)
            cur_min = min(num, cur_min * num)

            res = max(res, cur_max)

        return res
  • ํŒจํ„ด: Two Pointers, Dynamic Programming, Greedy, Bit Manipulation, Hash Map / Hash Set, Divide and Conquer, Monotonic Stack, Binary Search, BFS, DFS, Backtracking, Union Find, Trie, Heap / Priority Queue
  • ์„ค๋ช…: ์ตœ๋Œ€ ๋ถ€๋ถ„๋ฐฐ์—ด ๊ณฑ์˜ ์ตœ๋Œ€๊ฐ’์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด ํ˜„์žฌ ์œ„์น˜์—์„œ์˜ ์ตœ๋Œ€/์ตœ์†Œ ๊ณฑ์„ ํ•œ ๋ฒˆ์˜ ์ˆœํšŒ๋กœ ๊ฐฑ์‹ ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ, ์Œ์ˆ˜ ์ฒ˜๋ฆฌ ์‹œ ๋ถ€ํ˜ธ๋ฅผ ๋ฐ”๊ฟ” ๋‘ ๊ฐ’์„ ๊ต์ฐจ ๋น„๊ตํ•˜๋Š” ํŒจํ„ด์ด ํ•ต์‹ฌ์ด๋‹ค.

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

๋ณต์žก๋„
Time O(n)
Space 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.

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

minimum-window-substring/dolphinflow86.py
# S and T are the lengths of strings s and t respectively.
# TC: O(S + T) - expanding and contracting sliding window with frequency counts
# SC: O(S + T) - hash tables store frequency counts of unique characters


class Solution:

    def minWindow(self, s: str, t: str) -> str:
        if not s or not t:
            return ""

        target_counts = {}
        for char in t:
            target_counts[char] = target_counts.get(char, 0) + 1

        window_counts = {}

        have = 0
        need = len(target_counts)

        res = [-1, -1]
        res_len = float("inf")
        left = 0

        for right in range(len(s)):
            char = s[right]
            window_counts[char] = window_counts.get(char, 0) + 1

            if char in target_counts and window_counts[char] == target_counts[char]:
                have += 1

            while have == need:
                if (right - left + 1) < res_len:
                    res = [left, right]
                    res_len = right - left + 1

                left_char = s[left]
                window_counts[left_char] -= 1

                if (
                    left_char in target_counts
                    and window_counts[left_char] < target_counts[left_char]
                ):
                    have -= 1

                left += 1

        l, r = res
        return s[l : r + 1] if res_len != float("inf") else ""
  • ํŒจํ„ด: Sliding Window, Hash Map / Hash Set
  • ์„ค๋ช…: ๋ฌธ์ž์—ด s์—์„œ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด ์ฐฝ์„ ์ขŒ์šฐ๋กœ ํ™•์žฅ/์ถ•์†Œํ•˜๋Š” ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ ๊ธฐ๋ฒ•๊ณผ, ๋ฌธ์ž ๋“ฑ์žฅ ํšŸ์ˆ˜๋ฅผ ์„ธ๋Š” ํ•ด์‹œ ๋งต์„ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜์—ฌ ํ•„์š”ํ•œ ๋ฌธ์ž ์ง‘ํ•ฉ๊ณผ ํ˜„์žฌ ์ฐฝ์˜ ์ƒํƒœ๋ฅผ ์ถ”์ ํ•ฉ๋‹ˆ๋‹ค.

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

๋ณต์žก๋„
Time O(n + m)
Space O(K)

ํ”ผ๋“œ๋ฐฑ: ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ์™€ ํ•ด์‹œ๋งต์„ ํ™œ์šฉํ•ด ํ•„์š”ํ•œ ๋ฌธ์ž ์ˆ˜๋ฅผ ๋งŒ์กฑํ•˜๋Š” ์ตœ์†Œ ์ฐฝ์„ ์ฐพ๋Š”๋‹ค.

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

๐Ÿ’ก ํ’€์ด์— ์‹œ๊ฐ„/๊ณต๊ฐ„ ๋ณต์žก๋„๋ฅผ ์ฃผ์„์œผ๋กœ ๋‚จ๊ฒจ๋ณด์„ธ์š”!

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/dolphinflow86.py
# M is the number of rows, and N is the number of columns.
# TC: O(M * N) - visits each cell constant number of times via DFS
# SC: O(M * N) - stores visited sets and recursion stack for both oceans


class Solution:

    def pacificAtlantic(self, heights):
        if not heights or not heights[0]:
            return []

        rows, cols = len(heights), len(heights[0])
        pacific = set()
        atlantic = set()

        def dfs(r, c, visited, prev_height):
            if r < 0 or r >= rows or c < 0 or c >= cols:
                return

            if (r, c) in visited or heights[r][c] < prev_height:
                return

            visited.add((r, c))

            dfs(r + 1, c, visited, heights[r][c])
            dfs(r - 1, c, visited, heights[r][c])
            dfs(r, c + 1, visited, heights[r][c])
            dfs(r, c - 1, visited, heights[r][c])

        for c in range(cols):
            dfs(0, c, pacific, heights[0][c])
            dfs(rows - 1, c, atlantic, heights[rows - 1][c])

        for r in range(rows):
            dfs(r, 0, pacific, heights[r][0])
            dfs(r, cols - 1, atlantic, heights[r][cols - 1])

        return [[r, c] for r, c in pacific & atlantic]
  • ํŒจํ„ด: DFS, Backtracking, Graph Traversal
  • ์„ค๋ช…: ์ฝ”๋“œ๋Š” ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰(DFS)๋กœ ๋‘ ๋ฐ”๋‹ค์— ๋„๋‹ฌ ๊ฐ€๋Šฅํ•œ ์…€์„ ํƒ์ƒ‰ํ•ฉ๋‹ˆ๋‹ค. ๋ฐฉ๋ฌธ ์ง‘ํ•ฉ์„ ์ด์šฉํ•ด ์ค‘๋ณต ํƒ์ƒ‰์„ ํ”ผํ•˜๊ณ , ์ธ์ ‘ ์…€๋กœ์˜ ์žฌ๊ท€ ํ˜ธ์ถœ์„ ํ†ตํ•ด ์—ฐ๊ฒฐ์„ฑ์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ๋‘ ์ง‘ํ•ฉ์˜ ๊ต์ง‘ํ•ฉ์œผ๋กœ ์ตœ์ข… ๊ฒฐ๊ณผ๋ฅผ ๋„์ถœํ•ฉ๋‹ˆ๋‹ค.

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

๋ณต์žก๋„
Time O(mn)
Space O(mn)

ํ”ผ๋“œ๋ฐฑ: 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/dolphinflow86.py
# TC: O(1) - loops at most 32 times for 32-bit integer addition
# SC: O(1) - uses constant extra space


class Solution:

    def getSum(self, a: int, b: int) -> int:
        mask = 0xFFFFFFFF
        max_int = 0x7FFFFFFF

        while b != 0:
            carry = (a & b) << 1
            a = (a ^ b) & mask
            b = carry & mask

        return a if a <= max_int else ~(a ^ mask)
  • ํŒจํ„ด: Bit Manipulation
  • ์„ค๋ช…: ๋‘ ์ •์ˆ˜์˜ ํ•ฉ์„ ๋น„ํŠธ ์—ฐ์‚ฐ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋ฉฐ, XOR๋กœ ํ•ฉ์„, AND์™€ ์™ผ์‹œํ”„ํŠธ๋กœ ์บ๋ฆฌ๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„๋˜์–ด ์žˆ๋‹ค. ์ƒ์ˆ˜ ์‹œ๊ฐ„/๊ณต๊ฐ„์˜ ํŠน์„ฑ๊ณผ ๋น„ํŠธ ์—ฐ์‚ฐ ๊ธฐ๋ฐ˜์˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํŒจํ„ด์ด ํ•ต์‹ฌ์ด๋‹ค.

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

๋ณต์žก๋„
Time O(1)
Space O(1)

ํ”ผ๋“œ๋ฐฑ: ๋น„ํŠธ ์—ฐ์‚ฐ๊ณผ ๋งˆ์Šคํฌ๋ฅผ ์ด์šฉํ•ด ์‹œํ”„ํŠธ ๋ฐ ์บ๋ฆฌ ์ฒ˜๋ฆฌ๋ฅผ ๋ฐ˜๋ณต์ ์œผ๋กœ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ƒ์ˆ˜ ์‹œ๊ฐ„์— ์ž‘๋™ํ•˜๋„๋ก ์„ค๊ณ„๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

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

๐Ÿ’ก ํ’€์ด์— ์‹œ๊ฐ„/๊ณต๊ฐ„ ๋ณต์žก๋„๋ฅผ ์ฃผ์„์œผ๋กœ ๋‚จ๊ฒจ๋ณด์„ธ์š”!

@dahyeong-yun dahyeong-yun left a comment

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.

๊ณ ์ƒํ•˜์…จ์Šต๋‹ˆ๋‹ค. ์ œ ํ’€์ด์™€ ์œ ์‚ฌํ•œ ๋ถ€๋ถ„ ๋”๋Ÿฌ ์žˆ์–ด์„œ ์ œ๊ฐ€ ์“ฐ๋Š” java์™€ ํ‘œํ˜„ ์ƒ์˜ ์ฐจ์ด๋ฅผ ๋” ๋А๊ผˆ๋˜๊ฑฐ ๊ฐ™์•„์š”. ๋ณ€์ˆ˜๋ช…์ด๋‚˜ ์ฝ”๋“œ ํ๋ฆ„์ด ์ง๊ด€์ ์ด๋ผ์„œ ์ฝ๊ธฐ๊ฐ€ ํŽธํ–ˆ์Šต๋‹ˆ๋‹ค! ์ฃผ์ฐจ๋Š” ์ œ๊ฐ€ 9์ฃผ์ฐจ๋กœ ์„ค์ •์„ ํ–ˆ๋Š”๋ฐ ํ™•์ธ ํ•œ๋ฒˆ ๋ถ€ํƒ๋“œ๋ ค์š”.

Comment thread maximum-product-subarray/dolphinflow86.py Outdated
class Solution:

def getSum(self, a: int, b: int) -> int:
mask = 0xFFFFFFFF

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.

ํŒŒ์ด์ฌ์€ ์ •์ˆ˜ ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ๊ฐ€ ์—†์–ด์„œ, ๋ณ„๋„ ์ฒดํฌ๊ฐ€ ํ•„์š”ํ•˜๊ตฐ์š”!

@@ -0,0 +1,49 @@
# S and T are the lengths of strings s and t respectively.

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.

๋ชป ํ’€์—ˆ๋˜ ๋ฌธ์ œ์ธ๋ฐ ์ง๊ด€์ ์œผ๋กœ ํ’€์ด ๋•๋ถ„์— ์ดํ•ด๊ฐ€ ๋์Šต๋‹ˆ๋Œœ! ๋‘ .0ํฌ์ธํ„ฐ๋ฅผ ๊ฐ™์€ ๋ฐฉํ–ฅ์œผ๋กœ ๋ณด๋‚ด๋ฉด์„œ left๊ฐ€ right๋ฅผ ๋”ฐ๋ผ ์žก์•„ ๊ฐ€๋Š” ์ค‘์— ๋ฌธ์ž์˜ ์นด์šดํŠธ๋ฅผ ๋น„๊ตํ•˜๋Š” ๊ฑฐ๋„ค์š”.

Co-authored-by: polymorph <33543196+dahyeong-yun@users.noreply.github.com>

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/dolphinflow86.py
# N is the length of array nums.
# TC: O(N) - single pass maintaining maximum and minimum product
# SC: O(1) - uses constant extra variables


class Solution:

    def maxProduct(self, nums) -> int:
        if not nums:
            return 0

        res = nums[0]
        cur_max = nums[0]
        cur_min = nums[0]

        for i in range(1, len(nums)):
            num = nums[i]

            if num < 0:
                cur_max, cur_min = cur_min, cur_max

            cur_max = max(num, cur_max * num)
            cur_min = min(num, cur_min * num)

            res = max(res, cur_max)

        return res
  • ํŒจํ„ด: Two Pointers, Dynamic Programming, Hash Map / Hash Set
  • ์„ค๋ช…: ํ•ฉ์„ฑ๊ณฑ์„ ์—ฐ์† ๊ตฌ๊ฐ„์˜ ์ตœ๋Œ€ ๊ณฑ์œผ๋กœ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ๋ถ€ํ˜ธ๊ฐ€ ๋ฐ”๋€Œ๋Š” ๊ฒฝ์šฐ๋ฅผ ๊ด€๋ฆฌํ•˜๋ฉฐ ํ•œ ๋ฒˆ์˜ ์ˆœํšŒ๋กœ ํ˜„์žฌ ์ตœ๋Œ€/์ตœ์†Œ ๊ณฑ์„ ๊ฐฑ์‹ ํ•˜๋ฏ€๋กœ ๋™์  ๊ณ„ํš์˜ ์•„์ด๋””์–ด์™€ ๋‘ ๋ณ€์ˆ˜๋กœ ์—ฐ์† ๊ตฌ๊ฐ„์„ ์ถ”์ ํ•˜๋Š” ํŒจํ„ด์ด ๋ณด์ž…๋‹ˆ๋‹ค.

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

๋ณต์žก๋„
Time O(n)
Space O(1)

ํ”ผ๋“œ๋ฐฑ: ์Œ์ˆ˜์˜ ๊ณฑ์œผ๋กœ ์ตœ๋Œ€/์ตœ์†Œ๊ฐ€ ๋ฐ”๋€” ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๋ถ€ํ˜ธ๋ฅผ ๊ตํ™˜ํ•˜๋ฉฐ ๊ฐฑ์‹ ํ•œ๋‹ค.

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

๐Ÿ’ก ํ’€์ด์— ์‹œ๊ฐ„/๊ณต๊ฐ„ ๋ณต์žก๋„๋ฅผ ์ฃผ์„์œผ๋กœ ๋‚จ๊ฒจ๋ณด์„ธ์š”!

@dolphinflow86

Copy link
Copy Markdown
Contributor Author

๊ณ ์ƒํ•˜์…จ์Šต๋‹ˆ๋‹ค. ์ œ ํ’€์ด์™€ ์œ ์‚ฌํ•œ ๋ถ€๋ถ„ ๋”๋Ÿฌ ์žˆ์–ด์„œ ์ œ๊ฐ€ ์“ฐ๋Š” java์™€ ํ‘œํ˜„ ์ƒ์˜ ์ฐจ์ด๋ฅผ ๋” ๋А๊ผˆ๋˜๊ฑฐ ๊ฐ™์•„์š”. ๋ณ€์ˆ˜๋ช…์ด๋‚˜ ์ฝ”๋“œ ํ๋ฆ„์ด ์ง๊ด€์ ์ด๋ผ์„œ ์ฝ๊ธฐ๊ฐ€ ํŽธํ–ˆ์Šต๋‹ˆ๋‹ค! ์ฃผ์ฐจ๋Š” ์ œ๊ฐ€ 9์ฃผ์ฐจ๋กœ ์„ค์ •์„ ํ–ˆ๋Š”๋ฐ ํ™•์ธ ํ•œ๋ฒˆ ๋ถ€ํƒ๋“œ๋ ค์š”.

์ฃผ์ฐจ ์„ค์ •์ด ์•ˆ๋˜์–ด์—ˆ๋„ค์š”. ์„ธํŒ… ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค! ๊ทธ๋ฆฌ๊ณ  ๋ฆฌ๋ทฐ๋„ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.
ํ•œ ์ฃผ๊ฐ„ ๊ณ ์ƒํ•˜์…จ์Šต๋‹ˆ๋‹ค!

@dolphinflow86
dolphinflow86 merged commit fba9f57 into DaleStudy:main Aug 22, 2026
1 check passed
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