따로 공부하기
Java에서 리터럴은 _ 사용 허용
ex. 100_000은 100000 의미
형변환 묵시적 / 명시적
tcpschool.com에 잘 나와있음
스트링은 배열처럼 쓸 수 있는 이유는?
10:00 ~ 10:50
4주차 숙제 1, 2, 3
숙제1 - 농심 라면 공장
답안지
import heapq ramen_stock = 4 supply_dates = [4, 10, 15] supply_supplies = [20, 5, 10] supply_recover_k = 30 def get_minimum_count_of_overseas_supply(stock, dates, supplies, k): answer = 0 last_added_date_index = 0 max_heap = [] while stock <= k: # stock과 k가 비교할 수 있는 대상이라는 것을 보지 못했다. 문제의 큰 그림을 보는 게 가장 어렵다. while last_added_date_index < len(dates) and dates[last_added_date_index] <= stock: heapq.heappush(max_heap, supplies[last_added_date_index] * -1) # 공급 받을 수 있는 것 중 가장 큰 것을 받겠다는 의미 last_added_date_index += 1 answer += 1 heappop = heapq.heappop(max_heap) stock += heappop * -1 return answer print(get_minimum_count_of_overseas_supply(ramen_stock, supply_dates, supply_supplies, supply_recover_k))
숙제 2 - 샤오미 로봇 청소기
나의 답
current_r, current_c, current_d = 7, 4, 0 current_room_map = [ [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 1, 1, 1, 1, 0, 1], [1, 0, 0, 1, 1, 0, 0, 0, 0, 1], [1, 0, 1, 1, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 0, 1, 0, 1], [1, 0, 0, 0, 0, 0, 1, 1, 0, 1], [1, 0, 0, 0, 0, 0, 1, 1, 0, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1] ] dr = [-1, 0, 1, 0] dc = [0, 1, 0, -1] count_of_cleaned_departments = 1 location_of_robot = [r, c, d] room_map[r][c] = 1 back_count = 0 stop_count = 1 while stop_count: while back_count != 4: if d == 0: c -= 1 d = 3 if room_map[r][c] == 0: room_map[r][c] = 2 count_of_cleaned_departments += 1 back_count = 0 else: c += 1 back_count += 1 elif d == 1: r -= 1 d = 0 if room_map[r][c] == 0: room_map[r][c] = 2 count_of_cleaned_departments += 1 back_count = 0 else: r += 1 back_count += 1 elif d == 2: c += 1 d = 1 if room_map[r][c] == 0: room_map[r][c] = 2 count_of_cleaned_departments += 1 back_count = 0 else: c -= 1 back_count += 1 else: r += 1 d = 2 if room_map[r][c] == 0: room_map[r][c] = 2 count_of_cleaned_departments += 1 back_count = 0 else: r -= 1 back_count += 1 print(r, c, d, back_count, stop_count) if d == 0: r += 1 if room_map[r][c] == 1: stop_count = 0 back_count = 0 elif d == 1: c -= 1 if room_map[r][c] == 1: stop_count = 0 back_count = 0 elif d == 2: r -= 1 if room_map[r][c] == 1: stop_count = 0 back_count = 0 else: c += 1 if room_map[r][c] == 1: stop_count = 0 back_count = 0 print(r, c, d, back_count, stop_count) return count_of_cleaned_departments print(get_count_of_departments_cleaned_by_robot_vacuum(current_r, current_c, current_d, current_room_map))
답안지
current_r, current_c, current_d = 7, 4, 0 current_room_map = [ [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 1, 1, 1, 1, 0, 1], [1, 0, 0, 1, 1, 0, 0, 0, 0, 1], [1, 0, 1, 1, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 0, 1, 0, 1], [1, 0, 0, 0, 0, 0, 1, 1, 0, 1], [1, 0, 0, 0, 0, 0, 1, 1, 0, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1] ] dr = [-1, 0, 1, 0] dc = [0, 1, 0, -1] def rotate_to_left_by_direction(d): # 왼쪽으로 방향 전환 return (d + 3) % 4 def get_back_by_direction(d): # 후진 return (d + 2) % 4 def get_count_of_departments_cleaned_by_robot_vacuum(r, c, d, room_map): count_of_departments_cleaned = 1 room_map[r][c] = 2 queue = list([[r, c, d]]) # queue로 리스트 만들 때 대괄호 묶는 것 이해하기 while queue: r, c, d = queue.pop(0) # pop 할 때 형식 알아두기 temp_d = d for i in range(4): temp_d = rotate_to_left_by_direction(temp_d) new_r, new_c = r + dr[temp_d], c + dc[temp_d] if 0 <= new_r <= len(room_map) and 0 <= new_c <= len(room_map[0]) and room_map[new_r][new_c] == 0: count_of_departments_cleaned += 1 room_map[new_r][new_c] = 2 queue.append([new_r, new_c, temp_d]) break # 네 번의 회전 중 청소되지 않은 영역을 발견하면 break elif i == 3: # i==3이어도 위에 if에 갔다가 오게 됨 new_r, new_c = r + dr[get_back_by_direction(d)], c + dc[get_back_by_direction(d)] queue.append([new_r, new_c, d]) if room_map[new_r][new_c] == 1: return count_of_departments_cleaned print(get_count_of_departments_cleaned_by_robot_vacuum(current_r, current_c, current_d, current_room_map))
숙제 3 - CGV 극장 좌석 자리 구하기
나의 답
seat_count = 9 vip_seat_array = [4, 7] memo = { # 그래도 재귀를 사용할 생각을 해서 감사하다! 1: 1, 2: 2 } def new_recursion(n): # 이게 피보나치 수열이었다... if n in memo: return memo[n] nth_recursion = new_recursion(n-1) + n - 2 # 초기값 제외하고 이거랑 피보나치랑 같다 memo[n] = nth_recursion return nth_recursion def get_all_ways_of_theater_seat(total_count, fixed_seat_array): # fix를 경계로 경우의 수 계산하기, 2부터 규칙성 존재 count = 0 total = 1 for i in range(1, total_count+1): if i not in fixed_seat_array: count += 1 else: total *= new_recursion(count) count = 0 total *= new_recursion(count) return total
답안지
seat_count = 9 vip_seat_array = [4, 7] memo = { # 그래도 재귀를 사용할 생각을 해서 감사하다! 1: 1, 2: 2 } def fibo_dynamic_programming(n, memo): if n in memo: print(memo[n]) return memo[n] nth_fibo = fibo_dynamic_programming(n - 1, memo) + fibo_dynamic_programming(n - 2, memo) memo[n] = nth_fibo return nth_fibo def get_all_ways_of_theater_seat(total_count, fixed_seat_array): all_ways = 1 current_index = 0 for fixed_seat in fixed_seat_array: fixed_seat_index = fixed_seat - 1 count_of_ways = fibo_dynamic_programming(fixed_seat_index - current_index, memo) all_ways *= count_of_ways current_index = fixed_seat_index + 1 count_of_ways = fibo_dynamic_programming(total_count - current_index, memo) all_ways *= count_of_ways return all_ways print(get_all_ways_of_theater_seat(seat_count, vip_seat_array))