CS기초/코딩테스트 14

LeetCode #424

https://leetcode.com/problems/longest-repeating-character-replacement/description/ Longest Repeating Character Replacement - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 제일 속도가 빠른 해답은 이건 데, count[s.charAt(start) - 'A']--해도 maxCount가 계산되는 것이 아직 이해가 안간다. class Solution { public in..

LeetCode 1209 (Easy) Remove All Adjacent Duplicates in String II

출처: leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/ 문제 한글 번역은 아래 더보기를 클릭해주세요. 더보기 풀이 class Solution: def removeDuplicates(self, s: str, k: int) -> str: # 문자열의 길이가 k보다 작은 경우, k만큼 중복되는 문자는 없으므로 바로 반환 if len(s) < k: return s stack = [] dupStr = '' dupCnt = 0 # 중복이 있는 지 검사하고 # 중복된 문자열을 삭제 for c in s: stack.append(c) # 문자열이 다르면 dupCtn 1로 리셋 if (dupStr != c): dupCnt = 1 dupStr = c e..

LeetCode 20 (Easy) Valid Parentheses

출처: leetcode.com/problems/valid-parentheses/ 문제 한글 번역은 아래 더보기를 클릭해주세요. 더보기 풀이 class Solution: def isValid(self, s: str) -> bool: stack = [] top = '' for b in s: # 열렸으면 push # 닫혔으면 탑과 비교하고, 짝이 맞으면 pop 짝이 안 맞으면 true if b == '(' or b == '[' or b == '{': stack.append(b) top = b elif (b == ')' and top == '(') or (b == ']' and top == '[') or (b == '}' and top == '{'): stack.pop() top = stack[-1] if le..

LeetCode 232 (Easy) Implement Queue using Stacks

출처: leetcode.com/problems/implement-queue-using-stacks/ Implement Queue using Stacks - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 문제 한글 번역은 아래 더보기를 클릭해주세요. 풀이 class MyQueue: def __init__(self): """ Initialize your data structure here. """ self.stack = list() def push(self, x: ..

이것이 취업을 위한 코딩테스트다 15장. 이진탐색 문제

29 공유기 설치 1회: 오답 - 문제에 대한 감도 못 잡음, 문제를 이해하고 나니 떡볶이 떡 문제랑 비슷하단 걸 알았다 모범답안: github.com/ndb796/python-for-coding-test/blob/master/15/3.py ndb796/python-for-coding-test [한빛미디어] "이것이 취업을 위한 코딩 테스트다 with 파이썬" 전체 소스코드 저장소입니다. - ndb796/python-for-coding-test github.com 해설 집의 좌표와 공유기의 갯수(C)를 입력 받기 집의 좌표를 정렬하기 집끼리의 최대 거리를 구하기 위한 이진탐색 수행 맨 처음에 mid는 중간점: (첫번째 집 좌표 + 마지막 집 좌표) // 2 항상 첫번째 집부터 공유기를 설치한다고 할 때,..

LeetCode 35 (Easy) Search Insert Position

출처: leetcode.com/problems/search-insert-position/ Search Insert Position - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 문제 한글 번역은 아래 더보기를 클릭해주세요. 더보기 유니크한 정수로 이루어진 배열과 임의의 값이 주어졌을 때, 임의의 값이 배열의 원소이면 해당 원소의 인덱스를 반환하고, 임의의 값이 배열의 원소가 아니면, 임의의 값이 오름차순으로 추가되었을 때의 인덱스를 반환하라. 예제 1: 입력..

LeetCode 349 (Easy) Intersection of Two Arrays

출처: leetcode.com/problems/intersection-of-two-arrays/ Intersection of Two Arrays - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 문제 한글 번역은 아래 더보기를 클릭해주세요. 더보기 주어진 두 배열의 교집합을 반환하는 함수를 작성하여라. 예제 1: 입력: nums1 = [1, 2, 2, 1], nums2 = [2, 2] 출력: [2] 예제 2: 입력: nums1 = [4, 9, 5], nums2 ..

LeetCode 278 (Easy) First Bad Version

출처: leetcode.com/problems/first-bad-version/ First Bad Version - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 문제 한글 번역은 아래 더보기를 클릭해주세요. 더보기 당신은 새 서비스를 개발하기 위해 팀을 리딩하고 있는 프로젝트 매니저이다. 안타깝게도, 서비스의 최신 버전이 품질 검사를 통과하지 못했다. 각각의 버전은 이전 버전을 기반으로 개발되기 때문에, 문제가 된 버전 이후의 모든 버전은 품질 검사를 통과하지..

LeetCode 392 (Easy) Is Subsequence

출처: https://leetcode.com/problems/is-subsequence/ 문제 한글 번역은 아래 더보기를 클릭해주세요. 더보기 문자열 s와 t가 주어졌을 때, s가 t의 부분문자열인지 구하여라. 어떤 문자열의 부분문자열이란, 어떤 문자열 내 문자들의 상대적인 순서 변경없이 일부를 삭제하여(삭제하지 않을 수도 있음) 얻을 수 있는 문자열을 말한다. (예, ace는 abcde의 부문문자열이다. aec는 abcde의 부분문자열이 아니다) 예제1: 입력: s="abc", t="ahbgdc" 출력: true 예제2: 입력: s="axc", t="ahbgdc" 출력: false 제약조건: 0 < s.length < 100 0 < t.length < 10^4 s와 t는 영문소문자로만 구성되어 있다 풀..

LeetCode 1710 (Easy) Maximum Units on a Truck

출처: leetcode.com/problems/maximum-units-on-a-truck/ Maximum Units on a Truck - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 문제 한글 번역은 아래 더보기를 클릭해주세요. 더보기 한 대의 트럭에 정해진 갯수의 박스만 실을 수 있다. boxTypes[i] = [numberOfBoxes(i), numberOfUnitsPerBox(i)]인 이차원 배열 boxTypes이 주어질 때: numberOfBoxes..