Algorithm 18

[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

그룹단어체커 [22.09.04]

https://www.acmicpc.net/problem/1316 1316번: 그룹 단어 체커 그룹 단어란 단어에 존재하는 모든 문자에 대해서, 각 문자가 연속해서 나타나는 경우만을 말한다. 예를 들면, ccazzzzbb는 c, a, z, b가 모두 연속해서 나타나고, kin도 k, i, n이 연속해서 나타나기 때 www.acmicpc.net 나의 풀이 x = int(input()) group_word = 0 for _ in range(x): word = input() #전달받는 단어 error = 0 for idx in range(len(word)-1): if word[idx] != word[idx+1]: # ex) happy 에서 h와 a가 같지않을경우 new_word = word[idx+1:] # ..

Python/algorithm 2022.09.04

커트라인 [22.09.04]

https://www.acmicpc.net/problem/25305 25305번: 커트라인 시험 응시자들 가운데 1등은 100점, 2등은 98점, 3등은 93점이다. 2등까지 상을 받으므로 커트라인은 98점이다. www.acmicpc.net 나의 풀이 n, k = map(int,input().split()) score_list = list(map(int,input().split())) def cutline(tester, _pass, pt_list): pt_list.sort(reverse=True) passing_pt = _pass - 1 cut = pt_list[passing_pt] return cut print(cutline(n,k,score_list)) 루키의 풀이 n, k = map(int, inp..

Python/algorithm 2022.09.04

[자료구조] Array

Array Array(배열) 이란 연속된 메모리 공간에서 순차적으로 저장된 데이터의 모음입니다. array는 동일타입의 데이터만 저장이 가능하며 'int'를 선언 했을때는 float,char 와 같은 다른 타입의 데이터를 저장할 수 없습니다. 기본적으로 파이썬에서는 Array를 제공하지 않기 때문에 array 모듈을 사용해서 array를 생성 할 수 있습니다. array 모듈은 두가지 매개변수를 받으며 첫번째 매개변수는 type을 나타내는 typecode 이고 두번째는 대괄호로 묶인 요소 묶음입니다. ex) exam_arr = array('i',[1,3,2,4,5]) 자세한 typecode 에 대한 설명은 파이썬 공식문서를 참고하면 좋다. https://docs.python.org/ko/3/library..

Python/algorithm 2022.09.03