본문 바로가기

Python/PS in Python55

실패율 : 코딩테스트 연습 / Python / Programmers / Level1 실패율 문제는 제목 링크에서 확인이 가능하며, 입출력의 예시는 아래와 같다. N: 스테이지 개수 stages: stage의 길이 (User의 명 수, 각 User가 어느 Stage까지 도달했는지) Stage No. 2 1 2 6 2 4 3 3 실패율 1 Clear X Clear Clear Clear Clear Clear Clear 1/8 (13%) 2 X - X Clear X Clear Clear Clear 3/7 (43%) 3 - - - Clear - Clear X X 2/4 (50%) 4 - - - Clear - X - - 1/2 (50%) 5 - - - Clear - - - - 0/1 (0%) N+1 마지막 스테이지까지 클리어 스테지 실패율 높은 순 : (실패율이 같은 스테이지가 있다면 작은 번호의.. 2020. 11. 3.
LeetCode 1342. Number of Steps to Reduce a Number to Zero - Python LeetCode 1342. Number of Steps to Reduce a Number to Zero Given a non-negative integer num, return the number of steps to reduce it to zero. If the current number is even, you have to divide it by 2, otherwise, you have to subtract 1 from it. Example : Input: num = 14 Output: 6 Explanation: Step 1) 14 is even; divide by 2 and obtain 7. Step 2) 7 is odd; subtract 1 and obtain 6. Step 3) 6 is even.. 2020. 10. 25.
크레인 인형뽑기 게임 : 코딩테스트 연습 / Python / Programmers / Level1 크레인 인형뽑기 게임 문제는 제목 링크에서 확인이 가능하며, 입출력의 예시는 아래와 같다. #Input board = [[0,0,0,0,0],[0,0,1,0,3],[0,2,5,0,1],[4,2,4,4,2],[3,5,1,3,1]] moves = [1,5,3,5,1,2,1,4] #Output (사라진 인형 개수) 4 #7개 뽑았으니까 5개 찰 때 2개 다시 5개 찰 때 2개사 사라져서 4개가 사라짐 Solution: def solution(boards, moves): #1번일경우 배열에서 0번에 접근하므로 -1씩 한다 moves = list(map(lambda mv : mv -1, moves)) stack = [0] #0을 안 넣을 경우 IndexError cnt = 0 for i in moves: #mov.. 2020. 10. 21.
LeetCode 1351. Count Negative Numbers in a Sorted Matrix - Python LeetCode 1351. Count Negative Numbers in a Sorted Matrix Given a m * n matrix grid which is sorted in non-increasing order both row-wise and column-wise. Return the number of negative numbers in grid. m == grid.length n == grid[i].length 1 2020. 10. 21.
LeetCode 771. Jewels and Stones - Python LeetCode 771. Jewels and Stones You're given strings J representing the types of stones that are jewels, and S representing the stones you have. Each character in S is a type of stone you have. You want to know how many of the stones you have are also jewels. The letters in J are guaranteed distinct, and all characters in J and S are letters. Letters are case sensitive, so "a" is considered a di.. 2020. 10. 19.
[1차] 다트 게임 : 코딩테스트 연습 / Python / Programmers / Level1 [1차] 다트 게임 문제는 위의 링크에서 확인이 가능하며, 입출력의 예시는 아래와 같다. Solution 1: #new 배열 정의 및 10을 @으로 대체한 방법 def dart(dartResult): answer = 0 if "10" in dartResult: dartResult = dartResult.replace("10","@") new = [] for i in dartResult: if i == "S": i = new[-1]**(1) new.pop() if i == "D": i = new[-1]**(2) new.pop() if i == "T": i = new[-1]**(3) new.pop() if i == "*": i = new[-1]*2 if len(new)>1: new[-2] = new[-2]*.. 2020. 10. 16.
[1차] 비밀지도 : 코딩테스트 연습 / Python / Programmers / Level1 [1차] 비밀지도 문제는 위의 링크에서 확인이 가능하며, 입출력의 예시는 아래와 같다. Example1: #Input : n(변의 크기), arr1(지도1), arr2(지도2) n = 5 arr1 = [9, 20, 28, 18, 11] arr2 = [30, 1, 21, 17, 28] #Output ["#####","# # #", "### #", "# ##", "#####"] Example2: #Input n = 6 arr1 = [46, 33, 33 ,22, 31, 50] arr2 = [27 ,56, 19, 14, 14, 10] #Output ["######", "### #", "## ##", " #### ", " #####", "### # "] Solution def solution(n, arr1, ar.. 2020. 10. 9.
LeetCode 581. Shortest Unsorted Continuous Subarray - Python 581. Shortest Unsorted Continuous Subarray Given an integer array, you need to find one continuous subarray that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order, too. You need to find the shortest such subarray and output its length. Example: Input: [2, 6, 4, 8, 10, 9, 15] Output: 5 Explanation: You need to sort [6, 4, 8, 10, 9] in ascend.. 2020. 9. 22.
LeetCode 20. Valid Parentheses - Python LeetCode 20. Valid Parentheses - Python Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brackets. Open brackets must be closed in the correct order. Example 1: Input: s = "()" Output: true Example 2: Input: s = "()[]{}" Output: true Example 3: Inp.. 2020. 9. 21.
LeetCode 234. Palindrome Linked List - Python LeetCode 234. Palindrome Linked List - Python Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false Example 2: Input: 1->2->2->1 Output: true 문제 정의: 회문(=거꾸로 읽어도 똑같은 결과가 나오는) 연결 리스트인지 확인하라. 풀이 - fast 포인터와 slow 포인터를 설정한다. fast포인터는 slow 포인터보다 2배 빠르다. - 2배 빠르게 움직이는 fast가 연결리스트 끝(null)에 가면 slow 포인터는 중간 노드에 위치한다. - slow 포인터가 위치한 중앙에서 나머지 오른 쪽 노드들의 순서를 거꾸로 뒤집.. 2020. 9. 18.