leetcode

unique element๋กœ ๊ตฌ์„ฑ๋œ nums ๋ฐฐ์—ด์ด ์ฃผ์–ด์กŒ์„ ๋•Œ,๊ฐ€๋Šฅํ•œ ๋ชจ๋“  subsets(๋ถ€๋ถ„์ง‘ํ•ฉ)์„ ๋ฆฌํ„ดํ•˜๋Š” ๋ฌธ์ œ๋‹ค.  ์ ‘๊ทผ - ๋ฐฑํŠธ๋ž˜ํ‚น๋ชจ๋“  ๋ถ€๋ถ„ ์ง‘ํ•ฉ์„ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ -> ์™„์ „ ํƒ์ƒ‰๋ฐ˜๋ณต๋ฌธ vs ์žฌ๊ท€ -> ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ๋ชจ๋“  ๊ฒฝ์šฐ๋ฅผ ํƒ์ƒ‰ํ•˜๊ธฐ ์–ด๋ ค์šฐ๋ฏ€๋กœ ์žฌ๊ท€(DFS)๋ฅผ ์ด์šฉํ•œ ํƒ์ƒ‰,nums์˜ ๊ฐ ์›์†Œ๋ฅผ ํฌํ•จํ• ์ง€ ์•ˆ ํ• ์ง€ ์„ ํƒ ํ•„์š” -> ๋ฐฑํŠธ๋ž˜ํ‚น๋ฐฑํŠธ๋ž˜ํ‚น์€ ๋ชจ๋“  ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ๋ฅผ ํƒ์ƒ‰ํ•˜๋ฉด์„œ, ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒฝ๋กœ๋Š” ๋น ๋ฅด๊ฒŒ ๋˜๋Œ์•„๊ฐ€(๋ฐฑํŠธ๋ž™) ํƒ์ƒ‰์„ ์ค„์ด๋Š” ๊ธฐ๋ฒ•์ด๋‹ค.์ผ๋ฐ˜์ ์œผ๋กœ ์žฌ๊ท€(Recursion)์„ ์ด์šฉํ•œ๋‹ค. ๋ถ€๋ถ„์ง‘ํ•ฉ ๋ฌธ์ œ์—์„œ๋Š”  nums[i]๋ฅผ ์„ ํƒํ• ์ง€ ์•ˆ ํ• ์ง€ ๊ฒฐ์ •ํ•˜๋Š” 2๊ฐ€์ง€ ์„ ํƒ์ด ๊ณ„์†ํ•ด์„œ ๋ถ„๊ธฐ๋˜๊ธฐ์— ๋ฐฑํŠธ๋ž˜ํ‚น์— ์ ํ•ฉํ•˜๋‹ค.  ํ’€์ด: ๋ฐฑํŠธ๋ž˜ํ‚นclass Solution: def subsets(self, nums: L..
๋ฌธ์ œ ์š”์•ฝ์Šคํ†ค์˜ ๋ฌด๊ฒŒ๊ฐ€ ๋‹ด๊ธด stones[i] ๋ฐฐ์—ด์ด ์žˆ์„ ๋•Œ1. ๊ฐ€์žฅ ๋ฌด๊ฑฐ์šด ๋‘ ๊ฐœ์˜ ์Šคํ†ค์„ ์„ ํƒ,2. ๊ฐ™์œผ๋ฉด ๋‘˜๋‹ค ์ œ๊ฑฐ, ๋‹ค๋ฅด๋ฉด ํฐ ๋Œ์ด (ํฐ ๊ฐ’ - ์ž‘์€ ๊ฐ’)์œผ๋กœ ๋ณ€ํ•จ3. ์ตœ๋Œ€ ํ•œ ๊ฐœ์˜ ๋Œ์ด ๋‚จ์•„์žˆ์„ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต4. ๋‚จ์€ ๋Œ์˜ ๋ฌด๊ฒŒ ๋ฐ˜ํ™˜ (์—†์œผ๋ฉด 0) ์ตœ์  ํ’€์ด : max heap ์ด์šฉmax heap์„ ์ด์šฉํ•˜๋ฉด ํ•ญ์ƒ ๊ฐ€์žฅ ํฐ ๋ฌด๊ฒŒ์˜ ๋‘ ๋Œ์ด ํ•ญ์ƒ ๋ฐฐ์—ด์˜ ๋งจ ์•ž์— ์žˆ๋Š” ๊ตฌ์กฐ๋ฅผ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค.class Solution: def lastStoneWeight(self, stones: List[int]) -> int: stones = [-s for s in stones] heapq.heapify(stones) while len(stones) > 1:..
์ •์ˆ˜ k์™€ ์ดˆ๊ธฐ ์ŠคํŠธ๋ฆผ nums๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ,์ƒˆ๋กœ์šด ๊ฐ’์ด ์ถ”๊ฐ€๋  ๋•Œ๋งˆ๋‹ค K๋ฒˆ์งธ๋กœ ํฐ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํด๋ž˜์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๋ฌธ์ œ๋‹ค.constructor๋Š” ํด๋ž˜์Šค๋ฅผ ์ฒ˜์Œ ์ƒ์„ฑํ•  ๋•Œ ์‹คํ–‰๋˜๋Š” ํ•จ์ˆ˜.Python์—์„œ๋Š” __init__()์ด constructor(์ƒ์„ฑ์ž) ์—ญํ• ์„ ํ•œ๋‹ค.   ํ’€์ด1: Min-Heap ์ด์šฉ์ด ๋ฌธ์ œ์˜ ๊ฐ€์žฅ ํšจ์œจ์ ์ธ ํ’€์ด๋Š” Min-Heap์„ ์ด์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค. import heapqclass KthLargest: def __init__(self, k: int, nums: List[int]): self.minHeap, self.k = nums, k heapq.heapify(self.minHeap) while len(self.minHeap) > k: ..
BST์˜ root์™€ k๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ,ํŠธ๋ฆฌ์—์„œ k๋ฒˆ์งธ๋กœ ์ž‘์€ ๊ฐ’์„ ๋ฆฌํ„ดํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.(1-indexed: 1๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” ์ธ๋ฑ์Šค ๊ธฐ์ค€) ํ’€์ด 1: ๋ธŒ๋ฃจํŠธํฌ์Šค(DFS) - O(n log n)๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ํ’€์ด๋กœ๋Š” dfs๋ฅผ ํ†ตํ•ด ์™„์ „ ํƒ์ƒ‰์„ ํ•˜๊ณ , ์ •๋ ฌํ•˜์—ฌ k๋ฒˆ์งธ๋กœ ์ž‘์€ ๊ฐ’์„ ๋ฆฌํ„ดํ•  ์ˆ˜ ์žˆ๋‹ค. # Definition for a binary tree node.# class TreeNode:# def __init__(self, val=0, left=None, right=None):# self.val = val# self.left = left# self.right = rightclass Solution: def kthSmallest(self, root: Opt..
๋งํฌ๋“œ๋ฆฌ์ŠคํŠธ์— ์‚ฌ์ดํด์ด ์žˆ๋Š”์ง€ ์—†๋Š”์ง€ ํŒ๋‹จํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.set์„ ์ด์šฉํ•˜์—ฌ ๊ฐ„๋‹จํžˆ ํ’€ ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ํ”Œ๋กœ์ด๋“œ์˜ ์ˆœํ™˜ ์ฐพ๊ธฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜, ํ”Œ๋กœ์ด๋“œ์˜ ํ† ๋ผ์™€ ๊ฑฐ๋ถ์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ๋„ ํ’€ ์ˆ˜ ์žˆ๋Š” ์žฌ๋ฐŒ๋Š” ๋ฌธ์ œ์˜€๋‹ค! ํ’€์ด1. set ์ด์šฉ# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution: def hasCycle(self, head: Optional[ListNode]) -> bool: seen = set() cur = head while cur: ..
Koko loves to eat bananas. There are n piles of bananas, the ith pile has piles[i] bananas. The guards have gone and will come back in h hours.Koko can decide her bananas-per-hour eating speed of k. Each hour, she chooses some pile of bananas and eats k bananas from that pile. If the pile has less than k bananas, she eats all of them instead and will not eat any more bananas during this hour.Kok..
You are given an m x n integer matrix matrix with the following two properties:Each row is sorted in non-decreasing order.The first integer of each row is greater than the last integer of the previous row.Given an integer target, return true if target is in matrix or false otherwise.You must write a solution in O(log(m * n)) time complexity.1. ๊ฐ ํ–‰์€ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋˜์–ด ์žˆ์Œ2. ํ•œ ํ–‰์˜ ์ฒซ ๋ฒˆ์งธ ๊ฐ’์€ ์ด์ „ ํ–‰์˜ ๋งˆ์ง€๋ง‰ ๊ฐ’๋ณด๋‹ค ํผO(..
You are given the heads of two sorted linked lists list1 and list2.Merge the two lists into one sorted list. The list should be made by splicing together the nodes of the first two lists.Return the head of the merged linked list.๋‘ ๊ฐœ์˜ ๋งํฌ๋“œ๋ฆฌ์ŠคํŠธ๋ฅผ ํ•˜๋‚˜์˜ sorted linked list๋กœ ๋งŒ๋“ค๊ธฐ! ํ’€์ด1: Recursion (์žฌ๊ท€)# Definition for singly-linked list.# class ListNode:# def __init__(self, val=0, next=None):# se..
Given a string s, find the length of the longest substring without repeating characters.๋ฐ˜๋ณต๋˜๋Š” ๋ฌธ์ž ์—†์ด ๊ฐ€์žฅ ๊ธด substring์˜ ๊ธธ์ด๋ฅผ ์ฐพ๋Š” ๋ฌธ์ œ!substring์€ ์‹œ์ž‘๊ณผ ๋์ด ์ •์˜๋œ ์œ ๋™์ ์ธ ๋ฒ”์œ„์ด๋ฏ€๋กœ,์ƒํ™ฉ์— ๋”ฐ๋ผ ๋ฒ”์œ„(์œˆ๋„์šฐ)๋ฅผ ๋Š˜๋ฆฌ๊ฑฐ๋‚˜ ์ค„์ด๋Š” ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ ํ’€์ด๋ฅผ ๋– ์˜ฌ๋ฆด ์ˆ˜ ์žˆ๋‹ค.   ํ’€์ด : Sliding Windowclass Solution: def lengthOfLongestSubstring(self, s: str) -> int: charSet = set() l = 0 res = 0 for r in range(len(s)): while s[..
Given head, the head of a linked list, determine if the linked list has a cycle in it.There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail's next pointer is connected to. Note that pos is not passed as a parameter.Return true if there is a cycle i..
yesolz
'leetcode' ํƒœ๊ทธ์˜ ๊ธ€ ๋ชฉ๋ก