Loading...

[swexpert] 2005. 파스칼의 삼각형 (python, java)

1 1 1 1 2 1 에서 dp의 인덱스는 j,k가 00 10 11 20 21 22 30 31 32 33 이렇게 된다 k(행)이 0이거나 j==k인 경우 1이고 아닌 경우 dp[j-1][k-1]+dp[j-1][k] 이다 21 위치에 있는 수의 경우 10에 있는 수와 11에 있는 수를 더한다 1. 파이썬 풀이 t=int(input()) for i in range(1,t+1): n=int(input()) print(f'#{i}') dp=[] for j in range(n): arr=[] for k in range(j+1): if k==0 or k==j: arr.append(1) print(1,end=' ') else: arr.append(dp[j-1][k-1]+dp[j-1][k]) print(dp[j-1][..

[swexpert] 2007. 패턴 마디의 길이 (python, java, d2)

조건이 좀 부족하긴 한데 이 문제의 경우 반복되는 문자가 등장하는 대로 바로 반환하면 된다 KK 이건 K가 반복되어 바로 1을 반환하면 된다. 최대 10글자이니 10번만 검사하면 된다 1. 파이썬 풀이 for i in range(int(input())): s=input() for j in range(1,10): if s[:j]==s[j:2*j]: print(f'#{i+1} {j}') break 2. 자바 풀이 import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); sc.nextLine(); for (i..

[swexpert] 1926번 간단한 369게임 (python, java, D2)

1. python 풀이 방금 전 다른 문제 풀면서 count메소드를 알아서 활용했다 str로 문자열로 변환해서 3, 6, 9 문자의 개수를 세준 뒤 출력해준다 n=int(input()) for x in range(1,n+1): x=str(x) sum=0 sum+=x.count('3') sum+=x.count('6') sum+=x.count('9') if sum!=0: print('-'*sum,end=' ') else: print(x,end=' ') 2. JAVA 풀이 import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc..

[swexpert] 1204. 최빈수 구하기 (python, java)

1. python 풀이 일단 개수를 반환해주는 Counter로 구했는데 arr.count(x)로 해서 찾아도 되었을 듯 하다 * 등장 개수가 같다면 더 큰 점수로 해줘야한다. 가장 큰 개수만을 구한다면 most_common을 쓰면 되는데 이 부분 때문에 for문을 한 번 더 돌아줬다 from collections import Counter t=int(input()) for _ in range(1,t+1): n=int(input()) arr=list(map(int,input().split())) counter=Counter(arr) max_cnt,letter=-1,-1 for x in counter: if counter[x]==max_cnt: if x>letter: letter=x if counter[x]..

[백준 1157번] 단어 공부 (python)

most_common은 넘겨받은 수만큼 가장 많이 등장한 값을 전해준다 배열로 변환해서 만약 길이가 1이거나 가장 많이 사용된 알파벳 개수가 여러개가 아니면 해당 값을 출력하고 가장 많이 사용된 알파벳이 여러개라면 (두 개 이상일 테니 처음 두 개의 값만 비교하면 된다) ?을 출력한다 defaultdict으로 해도 되겠지만 most_common을 사용할 수 있기에 Counter를 사용했다 import sys from collections import Counter input=sys.stdin.readline s=input().strip().upper() cnt=Counter(s) # print(len(cnt)) # print(list(cnt.most_common(2))[0][1],list(cnt.most..

[프로그래머스] 위장 (python, 해시)

조합의 경우의 수라고 볼 수 있는 것 같다 각 종류의 옷을 안입는 경우까지 포함해서 종류별 옷의 개수+1 해서 모두 곱해준 다음 모든 의상을 입지않는 경우 1을 빼준다 (적어도 하나는 입어야한다) from collections import defaultdict def solution(clothes): answer = 1 dic=defaultdict(int) for a,b in clothes: dic[b]+=1 for v in dic.values(): answer*=(v+1) return answer-1

[프로그래머스] 전화번호 목록 (python, 해시)

정렬하면 길이가 더 짧고 더 작은 수가 앞으로 온다 따라서 앞의 문자열 길이 만큼 뒤에 오는 문자열을 잘라서 앞의 문자열과 같은지 보면 된다 def solution(book): book.sort() for i in range(1,len(book)): a,b=book[i-1],book[i] if b[:len(a)]==a: # print(b[:len(a)],a) return False return True

[프로그래머스] 완주하지 못한 선수 (Python, javascript)

1. 파이썬 풀이 동명이인이 있을 수 있다 Counter로 빼주면 알아서 0은 사라지는 모양이다 남는 1명의 이름을 list로 반환해주면 된다 from collections import Counter def solution(participant, completion): answer=Counter(participant)-Counter(completion) return list(answer)[0] 2. 자바스크립트 풀이 completion[i]가 undefined가 될 때 participant[i]를 리턴한다 function solution(participant, completion) { participant.sort(); completion.sort(); for(let i=0;i

[백준 1059번] 좋은 구간 (실버 5)

어렵다.. 수학적 계산 들어가는 문제는 다 어렵다ㅠㅠ 어떡하지 a= s 집합이다 집합 가장 앞에 0을 추가하여 예외 상황 (s집합이 1개일 때, s집합에 있는 수보다 n이 작을 때 등)을 처리해준다 s집합의 원소보다 n이 작다면 right=원소-1 이고 n이 더 크다면 다음 구간도 가능한 지 찾는다 left=원소+1 1 2 3 4 5 에서 left=1, right=5, n= 3이라면 가능한 경우의 수는 3을 포함하는 경우의 수 right-left (4가지이고) (1,3) (2,3) (3,4) (3,5) 3이 경계값이 아닌 포함하는 구간은 (n-left)*(right-n)이다 (1,4) (1,5) (2,4) (2,5) import sys input=sys.stdin.readline l=int(input()..

[백준] 1010번 다리놓기 (실버)

m >=n 일 때 n개의 다리를 놓을 수 있는 경우의 수를 구하는 문제이다. n만큼 뽑아서 순서대로 놓으면 되기 때문에 조합의 수를 구하면 된다. 1. 내 풀이 5C2의 경우 n=2이고 m=5 이다. n 의 개수만큼 5*4 5부터 1씩 빼면서 곱해주고 거기서 다시 n인 2부터 n의 개수만큼만 곱한 수를 나눠주면 된다 다시 말하면 5*4 / 2*1 이 답이다. n=3인 경우는 3개씩이니까 5*4*3 / 3*2*1 이다. import sys input=sys.stdin.readline t=int(input()) for _ in range(t): n, m = map(int, input().split()) answer=1; mm=m; for _ in range(n): answer*=mm mm-=1 for i i..