https://school.programmers.co.kr/learn/courses/30/lessons/42578?language=python3 

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

 

 

  • 해당 의상 종류(type)를 입지 않는 경우를 포함해서 조합 계산
  • answer *= (map[type] + 1)

 

딕셔너리 사용

def solution(clothes):
    map = {}
    for name, type in clothes:
        map[type] = map.get(type, 0) + 1
    
    answer = 1
    for type in map:
        answer *= (map[type] + 1)
    
    return answer-1

 

 

카운터 사용

from collections import Counter
def solution(clothes):
    map = dict(Counter([type for name, type in clothes]))
    
    answer = 1 
    for type in map:
        answer *= (map[type] + 1)
    
    return answer-1

https://www.acmicpc.net/problem/1715

 

1715번: 카드 정렬하기

정렬된 두 묶음의 숫자 카드가 있다고 하자. 각 묶음의 카드의 수를 A, B라 하면 보통 두 묶음을 합쳐서 하나로 만드는 데에는 A+B 번의 비교를 해야 한다. 이를테면, 20장의 숫자 카드 묶음과 30장

www.acmicpc.net

 

 

  • sys로 입력받아야 속도 빨라짐
  • sum과 answer 각각 만들어줘야 함
import heapq
from sys import stdin

cards = []
answer = 0
for _ in range(int(stdin.readline())):
    heapq.heappush(cards, int(stdin.readline()))

while len(cards)>1:
    sum = heapq.heappop(cards) + heapq.heappop(cards)
    answer += sum
    heapq.heappush(cards, sum)

print(answer)

https://www.acmicpc.net/problem/10162

 

10162번: 전자레인지

3개의 시간조절용 버튼 A B C가 달린 전자레인지가 있다. 각 버튼마다 일정한 시간이 지정되어 있어 해당 버튼을 한번 누를 때마다 그 시간이 동작시간에 더해진다. 버튼 A, B, C에 지정된 시간은

www.acmicpc.net

 

내 풀이

n = int(input())

button = [300,60,10]
cnt = [0] * 3

for i in range(3):
    if n >= button[i]:
        cnt[i] = n // button[i]
        n %= button[i]

if n != 0:
    print(-1)
else:
    print(*cnt)

 

 

 

다른 사람 풀이

t = int(input())

if t%10 != 0:
    print(-1)
else:
    print(t//300,(t%300)//60,(t%60)//10)

https://school.programmers.co.kr/learn/courses/30/lessons/12899?language=python3 

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

 

내 풀이

def solution(n):
    answer = ''
    num = ['4','1','2']

    while n > 0:
        idx = n%3   # 0 1 2
        n //= 3
        answer = num[idx] + answer
        if idx == 0:
            n -= 1
    
    return answer

 

다른 사람 풀이

def solution(n):
    answer = ''
    num = ['1','2','4']

    while n > 0:
        n -= 1
        answer = num[n % 3] + answer
        n //= 3
    
    return answer

https://school.programmers.co.kr/learn/courses/30/lessons/131120?language=mysql 

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

 

  • date에서 월만 추출할 때 : month(date_of_birth)
  • date format : date_format(date_of_birth, '%Y-%m-%d')
  • 대문자Y : 1990 처럼 네 자리 출력, 소문자y : 90 처럼 두 자리 출력

 

SELECT member_id, 
        member_name,
        gender, 
        date_format(date_of_birth, '%Y-%m-%d') as date_of_birth
from member_profile
where tlno is not null
and month(date_of_birth) = 3
and gender = 'w'

 

https://school.programmers.co.kr/learn/courses/30/lessons/12980

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

def solution(n):
    ans = 0
    
    while(n > 0):
        ans += n%2
        n //= 2

    return ans


def solution(n):
    return bin(n).count('1')

https://school.programmers.co.kr/learn/courses/30/lessons/43238

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

def solution(n, times):
    answer = 0
    # right는 가장 비효율적으로 심사했을 때 걸리는 시간
    # 가장 긴 심사시간이 소요되는 심사관에게 n 명 모두 심사받는 경우
    left, right = 1, max(times) * n
    
    while left <= right:
        # mid는 이분탐색 기준. 중간부터 탐색 시작.
        mid = (left+ right) // 2
        people = 0
        for time in times:
            # people 은 모든 심사관이 mid분 동안 심사한 사람의 수
            people += mid//time
            # 모든 심사관을 거치지 않아도 mid분 동안 n명 이상의 심사를 할 수 있다면 반복문을 나간다.
            # mid 시간이 충분하다는 뜻
            if people >= n:
                break
        
        # 심사한 사람의 수가 심사 받아야할 사람의 수(n)보다 많거나 같은 경우
        if people >= n:
            answer = mid
            right = mid - 1
        # 심사한 사람의 수가 심사 받아야할 사람의 수(n)보다 적은 경우
        elif people < n:
            left = mid + 1
    
    # left > right 일 때의 answer가 최종 mid값 
    return answer

https://www.acmicpc.net/problem/2583

 

2583번: 영역 구하기

첫째 줄에 M과 N, 그리고 K가 빈칸을 사이에 두고 차례로 주어진다. M, N, K는 모두 100 이하의 자연수이다. 둘째 줄부터 K개의 줄에는 한 줄에 하나씩 직사각형의 왼쪽 아래 꼭짓점의 x, y좌표값과 오

www.acmicpc.net

 

 

  • 마지막으로 전체 for문 돌릴 때 graph[i][j] == 0이면 1로 업데이트 해줘야 함
  • 이 부분 안 하면 칸이 두 개 이상일 때 첫 칸 재방문하게 됨
  • *sorted(result) 하면 result 배열을 오름차순 정렬하고 한 줄에 내용 출력

 

from collections import deque
m,n,k = map(int, input().split())
result = []
graph = [[0] * n for _ in range(m)]

for _ in range(k):
    x1, y1, x2, y2 = map(int, input().split())
    for i in range(y1, y2):
        for j in range(x1, x2):
            graph[i][j] = 1

dx = [0,0,-1,1]
dy = [1,-1,0,0]

queue = deque()
def bfs(x,y):
    queue.append((x,y))
    cnt = 1
    while(queue):
        x,y = queue.popleft()
        
        for i in range(4):
            nx = x + dx[i]
            ny = y + dy[i]
            if 0 <= nx < m and 0 <= ny < n and graph[nx][ny] == 0:
                graph[nx][ny] = 1
                cnt += 1
                queue.append((nx, ny))

    return cnt

for i in range(m):
    for j in range(n):
        if graph[i][j] == 0:
        	#여기서 방문 업데이트 안 하면 칸이 두 개 이상일 때 
            #첫 방문칸 다시 탐색함
            graph[i][j] = 1
            result.append(bfs(i,j))

print(len(result))
print(*sorted(result))

 

 

 

+ Recent posts