[LeetCode] 1493. Longest Subarray of 1's After Deleting One Element
·
코딩테스트/Python
🔗 Problem Linkhttps://leetcode.com/problems/longest-subarray-of-1s-after-deleting-one-element/description/❔Thinking0과 1로 숫자 배열 nums가 주어질 때, 0 한 개를 제거하여 만들 수 있는 가장 긴 1 부분수열의 길이를 반환한다.[1,1,0,1] 이라면 111이므로 3 반환💻Solutiondef longestSubarray(self, nums: List[int]) -> int: delete_cnt = 1 left = 0 longest_len = 0 for right in range(len(nums)): if nums[right] == 0: delete_..
[LeetCode] 1004. Max Consecutive Ones III
·
코딩테스트/Python
🔗 Problem Linkhttps://leetcode.com/problems/max-consecutive-ones-iii/description/❔Thinking0과 1로 이루어진 숫자 배열이 주어질 때, k개의 0을 1로 바꾸어 만들 수 있는 1 부분 수열의 최대 길이를 반환한다.ex - 0 0 1 1 1 0 1, k=1 이라면 111 0 1에서 0을 1로 바꾸어 11111을 만들 수 있으므로 5 반환💻Solution1. left, right Two-Pointer를 활용한 풀이left와 right로 0을 1로 바꾼 횟수가 k를 넘지 않도록 window 구성만약 현재 0을 1로 바꾼 횟수가 k를 넘은 경우, left를 이동하여 cur_zero_cnt를 k보다 작게 유지한다.right-left+1로 w..
[LeetCode] 1456. Maximum Number of Vowels in a Substring of Given Length
·
코딩테스트/Python
🔗 Problem Linkhttps://leetcode.com/problems/maximum-number-of-vowels-in-a-substring-of-given-length/description/❔Thinking문자열 s와 정수 k가 주어질 때, k개의 문자를 가지는 s의 부분 문자열 가운데에서 모음(a,e,i,o,u)이 최대 몇 개인지 반환한다.s = 'abciiidef', k = 3 인 경우 output은 3이다. (부분 문자열 'iii')s = 'aeiou', k = 2 인 경우 output은 2이다 (어떤 부분 문자열이라도 2) 💻Solution1. two-pointer를 활용한 풀이def maxVowels(self, s: str, k: int) -> int: left = 0 ..
[LeetCode] 283. Move Zeroes
·
코딩테스트/Python
🔗 Problem Linkhttps://leetcode.com/problems/move-zeroes/description/❔Thinking숫자 배열 nums가 주어지면, 0을 오른쪽으로 옮기면서 다른 숫자의 순서는 유지해야 한다.새로운 배열을 생성하지 않아야 하기 때문에, 값을 swap하는 방식을 택한다.💻Solution1. 0이 아닌 숫자의 인덱스를 찾는 방식def moveZeroes(self, nums: List[int]) -> None: for i in range(len(nums)): if nums[i] == 0: flag = False non_zero_idx = i while non_zero_idx   2. 투 포인터..
[LeetCode] 345. Reverse Vowels of a String
·
코딩테스트/Python
🔗 Problem Linkhttps://leetcode.com/problems/reverse-vowels-of-a-string/description/❔Thinking영단어가 주어지면, 모음(a,e,i,o,u)만을 역순으로 재배치하여 반환한다. (abe -> eba)알파벳은 소문자와 대문자를 구분한다.💻Solution1. 단어에 포함된 모음을 배열에 담고, 해당 배열에서 pop 하여 순서 재배치def reverseVowels(self, s: str) -> str: vowels_set = set(['a','e','i','o','u','A','E','I','O','U']) vowels_list = [] s = list(s) for char in s: if char in v..