์•Œ๊ณ ๋ฆฌ์ฆ˜๐Ÿฅš/๋ฌธ์ œํ’€์ด (Python) 30

[LeetCode] 15. 3sum (Medium) 2023/5/1

๋ฌธ์ œ ์ œ๋ชฉ ์ •๋‹ต๋ฅ  ๋‚œ์ด๋„ 15. 3sum 32.6% Medium 3Sum - LeetCode Can you solve this real interview question? 3Sum - Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0. Notice that the solution set must not contain du leetcode.com ๋ฌธ์ œ์š”์•ฝ ์ •์ˆ˜ํ˜• ๋ฐฐ์—ด์ด ์ฃผ์–ด์ง€๋ฉด ํ•ฉ์ด 0์ด ๋˜๋Š” 3๊ฐœ์˜ ์ˆซ์ž์Œ์„ ๋ชจ๋‘ ๋ฐ˜ํ™˜ํ•˜์—ฌ๋ผ ์กฐ๊ฑด 1) ์›์†Œ๊ฐ€ 3๊ฐœ ์ด์ƒ 1000๊ฐœ ์ดํ•˜์ธ ์ •์ˆ˜ํ˜• ๋ฐฐ..

[python] 2. ์ฝ”๋”ฉํ…Œ์ŠคํŠธ์—์„œ ์‚ฌ์šฉ๋˜๋Š” Python ๊ธฐ๋ณธ

์ฝ”๋”ฉํ…Œ์ŠคํŠธ์—์„œ ์‚ฌ์šฉ๋˜๋Š” Python ๊ธฐ๋ณธ ๐ŸŽต์ธ๋ดํŠธ ๊ณต๋ฐฑ 4์นธ์ด ์›์น™ ๐ŸŽต๋„ค์ด๋ฐ ์›์น™ Snake Case์„ ์ง€ํ–ฅ - ์†Œ๋ฌธ์ž + ๋‹จ์–ด๋ฅผ ๋ฐ‘์ค„(_)๋กœ ๊ตฌ๋ถ„ ๐ŸŽต๋žŒ๋‹ค ํ‘œํ˜„์‹ lambda ๋งค๊ฐœ๋ณ€์ˆ˜: ํ‘œํ˜„์‹์˜ ํ˜•ํƒœ๋กœ ์‚ฌ์šฉ๋˜๋ฉฐ, ์ฃผ๋กœ map ํ•จ์ˆ˜์™€ ํ•จ๊ป˜ ์‚ฌ์šฉ๋จ map(lambda ๋งค๊ฐœ๋ณ€์ˆ˜: ํ‘œํ˜„์‹, input๊ฐ’) ๐ŸŽตList Comprehension : ๊ธฐ์กด ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์ƒˆ๋กœ์šด ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค์–ด๋‚ด๋Š” ๋ฌธ์žฅ(๋ฒ„์ „ 2.7 ์ดํ›„ ๋”•์…”๋„ˆ๋ฆฌ๋„ ๊ฐ€๋Šฅ) ๋ฌธ๋ฒ•๊ณผ ์˜๋ฏธ๋ฅผ ์ถ•์•ฝํ•˜์—ฌ ๋‚˜ํƒ€๋‚ด๊ธฐ ๋•Œ๋ฌธ์— ๋งค์šฐ ๊ฐ•๋ ฅํ•˜์ง€๋งŒ, ๊ฐ€๋…์„ฑ์„ ๋–จ์–ด๋œจ๋ฆฌ๋Š” ๋‹จ์ ์ด ์žˆ๋‹ค. ์˜ˆ์ œ) 100 ์ดํ•˜ ์ง์ˆ˜์— 100์„ ๊ณฑํ•˜์—ฌ๋ผ [n*100 for n in range(1, 100+1) if n%2==0] ๐ŸŽตGenerator : ๋ฐ˜๋ณต๋ฌธ์˜ ๋™์ž‘์„ ์ œ์–ดํ•  ์ˆ˜ ์žˆ๋Š” ๋ฃจํ‹ด ํ˜•ํƒœ ๋ฉ”๋ชจ..

[LeetCode] 1. two_sum (Easy) 2023/4/29

๋ฌธ์ œ ์ œ๋ชฉ ์ •๋‹ต๋ฅ  ๋‚œ์ด๋„ 1. two-sum 49.8% Easy Two Sum - LeetCode Can you solve this real interview question? Two Sum - Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not leetcode.com ๋ฌธ์ œ์š”์•ฝ ํƒ€๊นƒ ์ˆซ์ž๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ ๋ฐฐ์—ด ์›์†Œ์˜ ํ•ฉ์œผ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค๋ฉด ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•ด๋ผ ์กฐ๊ฑด 1) ์ •์ˆ˜ํ˜• ํƒ€๊นƒ ์ˆซ์ž์™€ ์ •์ˆ˜ํ˜• ๋ฐฐ์—ด์ด..

[LeetCode] 5. longest_palindromic_substring (Medium) 2023/4/28

๋ฌธ์ œ ์ œ๋ชฉ ์ •๋‹ต๋ฅ  ๋‚œ์ด๋„ 5. longest-palindromic-substring 32.4% Medium ํ•ด๋‹น ๋ฌธ์ž์—ด ๋ฐ˜ํ™˜ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๊ฐ€ 1์ผ ๊ฒฝ์šฐ -> ํ•ด๋‹น ๋ฌธ์ž์—ด ๋ฐ˜ํ™˜ ๋ฌธ์ž์—ด ์ž์ฒด๊ฐ€ ํŒฐ๋ฆฐ๋“œ๋กฌ ์ผ ๊ฒฝ์šฐ -> ํ•ด๋‹น ๋ฌธ์ž์—ด ๋ฐ˜ํ™˜ Step1. ์ œ์ผ ๊ธด ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ ์„ธ๋Š” ๋ฌธ์ œ์ด๋ฏ€๋กœ ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ๊ณ„์‚ฐ ๊ตฌํ˜„ (4406 ms) class Solution(object): def longestPalindrome(self, s): """ :type s: str :rtype: str """ #Step 0 if len(s)=2): for left_index in range(0, len(s)-nowlen+1): nowstr=s[left_index:left_index+nowlen] if(nowstr..

[LeetCode] 49. group-anagrams (Medium) 2023/4/28

๋ฌธ์ œ ์ œ๋ชฉ ์ •๋‹ต๋ฅ  ๋‚œ์ด๋„ 49. group-anagrams 66.8% Medium Group Anagrams - LeetCode Can you solve this real interview question? Group Anagrams - Given an array of strings strs, group the anagrams together. You can return the answer in any order. An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase leetcode.com ๋ฌธ์ œ์š”์•ฝ ๋ฌธ์ž์—ด ๋ฐฐ์—ด์„ ์• ๋„ˆ๊ทธ๋žจ ๊ธฐ์ค€์œผ๋กœ ๋ฌถ์–ด๋ผ ์กฐ๊ฑด 1) ๋ฌธ์ž์—ด ๋ฐฐ์—ด์ด ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„๋‹ค ์กฐ๊ฑด 2)..

[LeetCode] 937. reorder-data-in-log-files (Easy) 2023/4/27

๋ฌธ์ œ ์ œ๋ชฉ ์ •๋‹ต๋ฅ  ๋‚œ์ด๋„ 937. reorder-data-in-log-files 56.4% Easy Reorder Data in Log Files - LeetCode Can you solve this real interview question? Reorder Data in Log Files - You are given an array of logs. Each log is a space-delimited string of words, where the first word is the identifier. There are two types of logs: * Letter-logs: All words (except the leetcode.com ๋ฌธ์ œ ์š”์•ฝ. ๋กœ๊ทธ ํŒŒ์ผ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์žฌ์ •๋ ฌํ•ด๋ผ. ์กฐ๊ฑด 1) ๋กœ๊ทธ ..

[LeetCode] 344. reverse-string (Easy) 2023/4/27

๋ฌธ์ œ ์ œ๋ชฉ ์ •๋‹ต๋ฅ  ๋‚œ์ด๋„ 344. reverse-string 76.8% Easy Reverse String - LeetCode Can you solve this real interview question? Reverse String - Write a function that reverses a string. The input string is given as an array of characters s. You must do this by modifying the input array in-place [https://en.wikipedia.org/wiki/In-place_algo leetcode.com ๋ฌธ์ œ ์š”์•ฝ. ๋ฌธ์ž์—ด์„ ์—ญ์ˆœ์œผ๋กœ ๋ฐ”๊พธ๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด๋ผ ์กฐ๊ฑด 1) ์ž…๋ ฅ์€ ๋ฌธ์ž๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฐฐ์—ด๋กœ ์ฃผ์–ด์ง„๋‹ค..

[LeetCode] 125. valid-palindrome (Easy) 2023/4/27

๋ฌธ์ œ ์ œ๋ชฉ ์ •๋‹ต๋ฅ  ๋‚œ์ด๋„ 125. valid-palindrome 44.5% Easy Valid Palindrome - LeetCode Can you solve this real interview question? Valid Palindrome - A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric cha leetcode.com ๋ฌธ์ œ ์š”์•ฝ ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์ด ํŒฐ๋ฆฐ๋“œ๋กฌ์ด๋ฉด ์ฐธ์„, ์•„๋‹ˆ๋ผ๋ฉด ๊ฑฐ์ง“์„ ๋ฐ˜ํ™˜ํ•˜๋ผ ์กฐ๊ฑด 1) ๋ชจ๋“  ๋Œ€๋ฌธ์ž๋ฅผ..

[python] 1. ์ฝ”๋”ฉํ…Œ์ŠคํŠธ์‹œ ํ•„์ˆ˜ ์ฒดํฌ ๋ชฉ๋ก

์ฝ”๋”ฉํ…Œ์ŠคํŠธ์‹œ ํ•„์ˆ˜ ์ฒดํฌ ๋ชฉ๋ก ์ฝ”๋”ฉํ…Œ์ŠคํŠธ๋Š” ๊ธฐ์ˆ ์ง€์‹, ์ฝ”๋”ฉ ๋Šฅ๋ ฅ, ๋ฌธ์ œํ•ด๊ฒฐ ์—ญ๋Ÿ‰๋“ค์„ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•œ ์‹œํ—˜์ด๊ธฐ ๋•Œ๋ฌธ์— ์ œ์ผ ์ค‘์š”ํ•œ ๊ฒƒ์€ "๋ชจ๋“  ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋ฅผ ํ†ต๊ณผํ•˜๋„๋ก ํ’€์–ด์•ผ ํ•œ๋‹ค" ๋ชจ๋“  ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋ฅผ ํ†ต๊ณผํ•˜๊ธฐ ์œ„ํ•ด์„œ ํฌ๊ฒŒ ๋‘๊ฐ€์ง€ ์‚ฌํ•ญ์„ ๊ณ ๋ คํ•ด์•ผ ํ•œ๋‹ค. โœ”๏ธ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ๊ณ ๋ คํ•ด์•ผ ํ•œ๋‹ค. ์‹œ๊ฐ„ ๋ณต์žก๋„๊ฐ€ $O(n^{2})$์ด๋ฉด Timeout ๋ฐœ์ƒ $O(n)$ ๋˜๋Š” $O(nlog_{n})$ ์ •๋„ ๋˜์–ด์•ผ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋ฅผ ํ†ต๊ณผ ํ•˜์ง€๋งŒ ๋Œ€๋ถ€๋ถ„ python์ด C++๋‚˜ ์ž๋ฐ”๋ณด๋‹ค ์‹คํ–‰ ์†๋„๊ฐ€ ๋Š๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์— ํŠนํžˆ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ตœ์ ํ™”์— ์ค‘์ ์„ ๋‘์–ด์•ผ ํ•œ๋‹ค. โœ”๏ธ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•œ๋‹ค. ์ž…๋ ฅ๊ฐ’์ด 0์ด๊ฑฐ๋‚˜ null์ด ๋“ค์–ด์˜ค๋Š” ๊ฒฝ์šฐ ํ•ด๋‹น ๊ฐ’์— ๋Œ€ํ•œ ๊ฒ€์ฆ ๊ณผ์ •์ด ํ•„์ˆ˜์ ์œผ๋กœ ํฌํ•จ๋˜์–ด์•ผ ํ•œ๋‹ค.

[python] 0. ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ ๊ธฐ๋ณธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ (4/26 ์—…๋ฐ์ดํŠธ)

๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ๊ธฐ๋ณธ์œผ๋กœ ์ œ๊ณตํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ชฉ๋ก ๋ฆฌ์ŠคํŠธ์ด๋‹ค. ๊ณต์‹ ๋ฌธ์„œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์–ด๋– ํ•œ ๊ธฐ๋Šฅ๋“ค์„ ์ œ๊ณตํ•˜๋Š”์ง€ ์ •๋ฆฌํ•ด ๋ณด์ž. ๐Ÿช…import collections dictionary, list, set, tuple ์ž๋ฃŒํ˜•์— ๋Œ€ํ•œ ๋Œ€์•ˆ์„ ์ œ๊ณตํ•œ๋‹ค. ํ•ด๋‹น ๊ฒŒ์‹œ๊ธ€ ์ฐธ๊ณ (https://steady-eschoi.tistory.com/5) ๐Ÿช…import heapq ์ด์ง„ํŠธ๋ฆฌ ๊ธฐ๋ฐ˜์˜ ์ตœ์†Œ heap ์•Œ๊ณ ๋ฆฌ์ฆ˜(๋ถ€๋ชจ ๋…ธ๋“œ์˜ ๊ฐ’์€ ํ•ญ์ƒ ์ž์‹ ๋…ธ๋“œ๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์Œ)์„ ์ œ๊ณตํ•œ๋‹ค. ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์€ heap [0]์— ์œ„์น˜ heappush heappop heappushpop heapreplace heapify nlargest nsmallest from heapq import * #ํž™ ๋ชจ๋“ˆ์˜ ํž™ํ ๊ฐ์ฒด๋ฅผ ์œ„ํ•œ ๋ฐฐ์—ด ์„ ์–ธ h=[] #์„ ์–ธ ๋œ..

728x90