Algorithm 18

[Leetcode] 1528. Shuffle String

Problem 이번 문제는 s 라는 문자열이 주어지고 indices에 s 문자열 각자리의 알맞는 인덱스가 들어가있고 정렬해서 return 해주는 문제이다. 나의 풀이 class Solution: def restoreString(self, s: str, indices: List[int]) -> str: res = '' zipped = sorted(zip(indices, s), key=lambda x: x[0]) for idx,char in zipped: res += char return res zip으로 두개를 묶고 , 인덱스를 기준으로 정렬한 뒤에 res에 더해주고 return 해 주었어용 다른 사람의 풀이 두가지를 보자 class Solution: def restoreString(self, s: str..

Python/algorithm 2023.01.12

[Leetcode] 2236. Root Equals Sum of Children

Problem 이번 문제는 이진 트리를 주고 ? 루트, 왼쪽, 오른쪽 자식 값이 존재한다. left , right 의 합이 root와 같으면 True 아니면 false를 return 한다. 나의 풀이 # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def checkTree(self, root: Optional[TreeNode]) -> bool: #print(root.left, root.left.val) #print(root.right,..

Python/algorithm 2023.01.11

[Leetcode] 1365. How Many Numbers Are Smaller Than the Current Number

Problem 이번 문제는 nums 각 값을 nums의 다른 값과 비교하여 작은값의 갯수를 배열로 return 해주는 문제이다 나의 풀이 class Solution: def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]: res = list() for i in nums: cnt = 0 for j in nums: if i > j: cnt += 1 res.append(cnt) return res 열심히 짱구를 굴려봤는데 for 두번을 사용하지않고는 문제가 풀리지 않았다. 다른 사람의 풀이 class Solution: def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]: l1 = ..

Python/algorithm 2023.01.05

[Leetcode] 1281. Subtract the Product and Sum of Digits of an Integer

Problem 이번 문제는 주어진 수 n의 각 자리수를 곱한 값에 더한 값을 뺀 값을 결과로 낸다 나의 풀이 from functools import reduce class Solution: def subtractProductAndSum(self, n: int) -> int: a = list(map(int,str(n))) return reduce((lambda x,y: x*y), a) - sum(a) list로 바꿔주고 functools 에 있는 reduce를 사용하여 문제를 풀었다 더한 값의 경우는 sum 을 사용하면 되지만 곱한 값의 경우는 sum 같은 부분이 없다 ㅠㅠ reduce(sum,[1,2,3,4,5,6])

Python/algorithm 2023.01.04

[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