본문 바로가기

전체 글

(81)
[leetcode 692] Top K Frequent Words-heapq 문제Given an array of strings words and an integer k, return the k most frequent strings.Return the answer sorted by the frequency from highest to lowest. Sort the words with the same frequency by their lexicographical order. Example 1:Input: words = ["i","love","leetcode","i","love","coding"], k = 2Output: ["i","love"]Explanation: "i" and "love" are the two most frequent words.Note that "i" comes..
[leetcode 347] Top K Frequent Elements-heapq 문제Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order. Example 1:Input: nums = [1,1,1,2,2,3], k = 2Output: [1,2]Example 2:Input: nums = [1], k = 1Output: [1]Example 3:Input: nums = [1,2,1,2,1,2,3,1,3,2], k = 2Output: [1,2] Constraints:1 첫번째 풀이: 단순하게 처리 가능함class Solution: def topKFrequent(self, nums: List[int], k: int) -> ..
[leetcode 445] Add Two Numbers II 문제You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.You may assume the two numbers do not contain any leading zero, except the number 0 itself. Example 1:Input: l1 = [7,2,4,3], l2 = [5,6,4]Output: [7,8,0,7]Example 2:Input: ..
[leetcode 2] Add Two Numbers-linked list 문제You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.You may assume the two numbers do not contain any leading zero, except the number 0 itself. Example 1:Input: l1 = [2,4,3], l2 = [5,6,4]Output: [7,0,8]Explanation: 342 + 4..
[leetcode 560] Subarray Sum Equals K - prefix sum 문제Given an array of integers nums and an integer k, return the total number of subarrays whose sum equals to k.A subarray is a contiguous non-empty sequence of elements within an array. Example 1:Input: nums = [1,1,1], k = 2Output: 2Example 2:Input: nums = [1,2,3], k = 3Output: 2 Constraints:1 첫번째 풀이: 별생각 없이 슬라이딩 윈도우를 사용하려고 했음class Solution: def subarraySum(self, nums: List[int], k: int) ->..
[leetcode 146] LRU Cache-OrderedDict 문제Design a data structure that follows the constraints of a Least Recently Used (LRU) cache.Implement the LRUCache class:LRUCache(int capacity) Initialize the LRU cache with positive size capacity.int get(int key) Return the value of the key if the key exists, otherwise return -1.void put(int key, int value) Update the value of the key if the key exists. Otherwise, add the key-value pair to the ..
[leetcode 3] Longest Substring Without Repeating Characters - 슬라이딩 윈도우 문제Given a string s, find the length of the longest substring without duplicate characters. Example 1:Input: s = "abcabcbb"Output: 3Explanation: The answer is "abc", with the length of 3. Note that "bca" and "cab" are also correct answers.Example 2:Input: s = "bbbbb"Output: 1Explanation: The answer is "b", with the length of 1.Example 3:Input: s = "pwwkew"Output: 3Explanation: The answer is "wke"..
[leetcode 209] Minimum Size Subarray Sum - 슬라이딩 윈도우 leetcode 209 문제Given an array of positive integers nums and a positive integer target, return the minimal length of a subarray whose sum is greater than or equal to target. If there is no such subarray, return 0 instead. Example 1:Input: target = 7, nums = [2,3,1,2,4,3]Output: 2Explanation: The subarray [4,3] has the minimal length under the problem constraint.Example 2:Input: target = 4, nums =..