목록분류 전체보기 (76)
one line of code at a time
https://leetcode.com/problems/max-consecutive-ones-iii 0과 1로 이루어진 배열 nums가 있다. 0을 1로 바꿀 수 있는 횟수인 k가 주어진다. 최대 k만큼 0을 1로 바꿨을 때 1이 연속적으로 나오는 subarray의 최대 길이를 구하는 문제다. nums = [1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0] 이고, k = 2일 때 5번째 인덱스와 맨 마지막 인덱스에 있는 0을 1로 바꿔서 길이가 6인 [1, 1, 1, 1, 1, 1] 배열을 만들 수 있다. 그러므로 정답은 6이다. 고민하다가 안 풀려서 다른 사람 코드를 참고했다. left pointer와 right pointer를 이용한다. left pointer는 0으로 설정해둔다. for..
https://leetcode.com/problems/longest-subarray-of-1s-after-deleting-one-element 배열이 있을 때 반드시 한 원소를 지워야 한다. 0을 하나 지웠을 때 연속된 1을 가지는 가장 긴 subarray의 길이를 구하라. 예를 들어, nums = [1, 1, 0, 1] 이런 배열이 있다고 할 때 2번째 인덱스의 0을 지워버리면 길이가 3인 [1, 1, 1]이라는 subarray를 만들 수 있다. 그러므로 답은 3이다. 내가 생각한 방법은 0이 아닌 연속된 1이 섬(island)처럼 느껴졌다. bfs 문제풀 때 독립된 섬의 개수를 구하는 문제가 오버랩되었다. 그래서 연속된 1은 더하고, 0은 -1로 처리해줬다. 즉, nums 배열을 [2, -1, 1]..
https://leetcode.com/problems/maximum-number-of-vowels-in-a-substring-of-given-length/description/ 문자열 s에서 길이가 k인 substring 중 모음(vowel) 개수가 최대일 때 그 최댓값을 구하는 문제다. s = "abciiidef", k = 3라고 할 때 iii 가 모음 3개를 가지므로 정답은 3이다. k의 길이가 fixed 되어 있으므로 sliding window로 풀면 된다. 그런데 살짝 헷갈렸던 게 window를 한 칸 씩 옮길 때 최댓값이 있으면 그 값을 업데이트 해줘야 하는데그 업데이트를 할 때 (그 다음 값이 모음이면 +1, 그 이전 값이 모음이었으면 -1) 그 이전값을 +1 하거나 -1 해줘야 한다는 것이..
sliding window 문제. https://leetcode.com/problems/maximum-average-subarray-i/ 정수 배열 nums가 있다. 여기서 길이가 k인 subarray들 중에서 가장 큰 평균값을 구하는 문제다.k는 sliding window의 길이라고 생각하면 된다. class Solution(object): def findMaxAverage(self, nums, k): maxAvg = float('-inf') currSum = 0 for i in range(k): currSum += nums[i] maxAvg = currSum / float(k) # print(maxAvg) ..
https://leetcode.com/problems/container-with-most-water Brute Force Approach문제 난이도가 medium이길래 for문 2개 쓰면 답이 아니겠거니 생각은 했지만, for 문을 2개 쓰면 시간 초과가 난다.class Solution(object): def maxArea(self, height): maxWater = -1 for i in range(len(height)): for j in range(i+1, len(height)): area = (j-i) * min(height[i], height[j]) maxWater = max(maxWater, ..
https://leetcode.com/problems/reverse-words-in-a-string/description class Solution(object): def reverseWords(self, s): s = s.strip() words = [] temp = "" for i in range(len(s)): if s[i] == " ": stripped = temp.strip() if len(stripped) > 0: words.append(temp) temp = "" else: ..
https://leetcode.com/problems/reverse-vowels-of-a-string/description class Solution(object): def reverseVowels(self, s): s = list(s) vowels = set('aeiouAEIOU') lp, rp = 0, len(s)-1 while lp
1431. Kids With the Greatest Number of Candies https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/description/ class Solution(object): def kidsWithCandies(self, candies, extraCandies): maxC = max(candies) # O(n) result = [] for c in candies: if c + extraCandies >= maxC: result.append(True) else: resul..
Microservice Architecture is a software development approach that structures an application as a collection of loosely coupled small and independently deployables services. Each service is designed to perform a specific function and communicates with other serivces through well-defined APIs or message broker. This architecture style promotes modularity, flexibility, and scalability as each servi..
https://leetcode.com/problems/merge-two-sorted-lists/ # Definition for singly-linked list.# class ListNode(object):# def __init__(self, val=0, next=None):# self.val = val# self.next = nextclass Solution(object): def mergeTwoLists(self, list1, list2): dummy = ListNode() tail = dummy # 더미 노드의 마지막을 가리키는 포인터 while list1 and list2: # while list1 and list2 ..