Python

알고리즘

[leetcode/python] 234번 Palindrome Linked List

https://leetcode.com/problems/palindrome-linked-list/ 문제 요약 : 연결 리스트가 패린드롬(앞뒤로 읽어도 같은 값)인지 검증해라 로직은 다음과 같다. 1. 연결 리스트를 돌면서, 현재 값을 origin에 쭉 저장하고, 순서를 반대로 해서 reverse에 저장한다. 2. 두개가 같은지 비교해서 리턴한다. # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def isPalindrome(self, head: Optional[ListNode]) -> bool:..

알고리즘

[leetcode/python] 561번 Array Partition

https://leetcode.com/problems/array-partition/ 문제 요약 : 배열에서 두 개씩 짝을 지었을 때의 최솟값으로 만들 수 있는 최대 합을 구하라 최솟값끼리 더한 것이 최대 합이 되려면 작은 것끼리 짝을 맞추면 된다. 로직은 다음과 같다. 1. 오름차순으로 정렬한다 2. 두개씩 짝지었을 때 짝수번째가 작은 값이 되기 때문에 이를 합산해 준다. class Solution: def arrayPairSum(self, nums: List[int]) -> int: sum = 0 for i, n in enumerate(sorted(nums)): if i % 2 == 0: sum += n return sum 시간복잡도는 O(n)이다

알고리즘

[python/leetcode] 49번 group anagrams 문제 풀이

https://leetcode.com/problems/group-anagrams 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. 같은 애너그램 형식이라면 문..

알고리즘

[python/leetcode] 819번 most common word 문제 풀이

https://leetcode.com/problems/most-common-word Most Common Word - LeetCode Can you solve this real interview question? Most Common Word - Given a string paragraph and a string array of the banned words banned, return the most frequent word that is not banned. It is guaranteed there is at least one word that is not banned, and tha leetcode.com 요약 : 금지된 단어를 제외하고, 최빈출 단어 찾기 기본 로직 1. 문장을 단어로 구분하면서 깨..

알고리즘

[python/leetcode] 937번 reorder log files 문제 풀이

https://leetcode.com/problems/reorder-data-in-log-files 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 요약 : 로그를 기준에 맞게 정렬하라 기본 로직은 다음과 같다...

알고리즘

[python/leetcode] 344번 Reverse String 문제 풀이

https://leetcode.com/problems/reverse-string/description/ 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 요약 : 리턴 없이, 리스트에 있는 문자열을 뒤집어라 기본 로직은 다음과 같다...

알고리즘

[python/leetcode] 125번 valid-palindrome 문제 풀이

https://leetcode.com/problems/valid-palindrome/ 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 valid-palindrome 문제 요약 : 앞으로 읽어도 거꾸로 읽어도 같은 문자열인지 확인..

grin-quokka
'Python' 태그의 글 목록