leetcode 14

[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