Python/algorithm 38

[Leetcode] 1431. Kids With the Greatest Number of Candies

Problem 이번 문제는 캔디갯수를 가진 배열 candies 와 , 더할 캔디 extraCandies를 주고 candies 각 캔디 갯수에 extraCandies를 더한값이 candies의 최대 값이면 True, 아니면 False로 바꾸어 반환한다 나의 풀이 class Solution: def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]: return [i+extraCandies>=max(candies) for i in candies] i+extraCandies 가 candies의 최대값과 크거나 같으면 True , 아니면 False 가 나오게 문제를 풀고 리스트 컴프리헨션을 사용했다

Python/algorithm 2023.01.02

[Leetcode] 1603. Design Parking System

Problem 이번 문제는 좀 길게 나왔다. 하지만 문제는 간단한 편이다 . 주차시스템이있고 [1,1,0] 앞에서부터 big, medium,small 의 차량이 들어갈 수있는 수가 담긴 배열이 있고 addcar로 각 사이즈별 1 = big, 2 = medium, 3 = small 의 차량을 주차시키고 True를 반환하고 차량을 주차시킬수 있는 칸이없으면 False를 반환한다 나의 풀이 class ParkingSystem: def __init__(self, big: int, medium: int, small: int): self.park = [big,medium, small] def addCar(self, carType: int) -> bool: carType -= 1 if self.park[carType..

Python/algorithm 2022.12.30

[Leetcode] 771. Jewels and Stones

Problem 이번 문제는 jewels 와 존 스톤스를 준다 존 스톤스 안에 jewels 가 몇개인지를 결과로 내줘야 하는 문제이다 나의 풀이 class Solution: def numJewelsInStones(self, jewels: str, stones: str) -> int: res = 0 for i in jewels: res += stones.count(i) return res jewels 를 하나씩 돌려주면서 존스톤스 안에 몇개있는지 count를 사용하여 문제를 해결했다 다른 사람의 존스톤스 풀이를 보자 class Solution: def numJewelsInStones (self, J: str, S: str) -> int: jset = set(J) count = 0 for i in S: if ..

Python/algorithm 2022.12.27

[Leetcode] 1672. Richest Customer WealthEasy2.9K305

Problem 이번 문제는 리스트 안에 리스트 의 합 중에서 최대 값을 return 하는 쉬운 문제이다 나의 풀이 class Solution: def maximumWealth(self, accounts: List[List[int]]) -> int: res = [sum(account) for account in accounts] return max(res) 기본적인 sum 과 max를 이용하여 문제를 풀었다 당연히 다른사람의 머리속을 들어가 봐야한다. class Solution: def maximumWealth(self, accounts: List[List[int]]) -> int: return max(map(sum, accounts)) 오..... 굉장하다 map을 사용하여 정말 간결하게 코드를 구현했다..

Python/algorithm 2022.12.26

[Leetcode] 1512. Number of Good Pairs

Problem 이번 문제는 배열 안에 같은 값을가지는 ( i,j ) 쌍이 몇가지 있는지 찾는 문제이다 나의 풀이 class Solution: def numIdenticalPairs(self, nums: List[int]) -> int: res = 0 import itertools a_list = itertools.combinations(nums,2) for i in a_list: if i[0] == i[1]: res += 1 return res 이전에 itertools에 있는 combinations를 사용한 기억을 되살려 저걸 활용해서 문제를 풀었다 itertools.combinations(iterable , r) combinations는 iterable 한 객체를 r 의 길이를 가진 조합들을 뽑아내준다..

Python/algorithm 2022.12.25

[Leetcode] 1470. Shuffle the Array

Problem 이번 문제는 굉장히 메마른 영어지식에 문제를 조금 헷갈렸다 ㅋㅋ 쉽게 2n 길이의 array가 주어지고 각 X1, X2 ...Xn ,Y1 , Y2 ... Yn 이렇게 되어 있다고 보면되고 output에서는 X,Y,X,Y ..... X,Y 이런식으로 교차로 섞인 array를 원하고있다! 이전과 다른 약간 짧은 고민 후에 푼 나의 코드 ( 고민을 했다는 것이 중요하다 ) class Solution: def shuffle(self, nums: List[int], n: int) -> List[int]: result = list() for i in range(len(nums)): result.append(nums[i]) result.append(nums[i+n]) if len(a) == len(nu..

Python/algorithm 2022.12.21

[Leetcode] 2011. Final Value of Variable After Performing Operations

Problem - 리스트 안에 ++ 포함 값은 시작값 0 에 1을 더하고 -- 포함 값은 1을 빼서 최종 값을 내는 문제이다 class Solution: def finalValueAfterOperations(self, operations: List[str]) -> int: result = 0 for i in operations: if "--" in i: result -= 1 else: result += 1 return result 그냥 딱 바로 생각나고 대부분 다 이렇게 풀었다고한다. class Solution: def finalValueAfterOperations(self, operations: List[str]) -> int: A=operations.count("++X") B=operations.co..

Python/algorithm 2022.12.20

[Leetcode] 480. Running Sum of 1d Array

Problem - 이 문제는 누적 합을 구하는 문제에용 시원하게 생각나는대로 풀어버린 나의 풀이 class Solution: def runningSum(self, nums: List[int]) -> List[int]: return[sum(nums[:idx+1]) for idx,n in enumerate(nums)] 다른 코드들에 비해 런타임이 길고 메모리사용량이 높아서 너무 궁금해서 다른 풀이 두가지를 찾아보았다 평균적인 런타임 수치의 풀이 class Solution: def runningSum(self, nums: List[int]) -> List[int]: return list(accumulate(nums)) itertools 에 있는 accumulate를 이용하여 값을 뽑았다 ! 누적합을 구해주는 ..

Python/algorithm 2022.12.19