Algorithm
[ํ์ด์ฌ PYTHON] ํ๋ก๊ทธ๋๋จธ์คใ๋ฌด์ง์ ๋จน๋ฐฉ ๋ผ์ด๋ธใ
ํ๋ก๊ทธ๋๋จธ์ค ์ฝ๋ ์ค์ฌ์ ๊ฐ๋ฐ์ ์ฑ์ฉ. ์คํ ๊ธฐ๋ฐ์ ํฌ์ง์ ๋งค์นญ. ํ๋ก๊ทธ๋๋จธ์ค์ ๊ฐ๋ฐ์ ๋ง์ถคํ ํ๋กํ์ ๋ฑ๋กํ๊ณ , ๋์ ๊ธฐ์ ๊ถํฉ์ด ์ ๋ง๋ ๊ธฐ์ ๋ค์ ๋งค์นญ ๋ฐ์ผ์ธ์. programmers.co.kr ํ์คํ, ์นด์นด์ค ์ฝํ ๊ธฐ์ถ๋ฌธ์ ๋ผ ๊ทธ๋ฐ์ง ์ด๋ ค์ ๋ค. ์ ํ์ฑ ํ ์คํธ๋, ์ฝ๊ฒ ํ ์ ์์๋๋ฐ ํจ์จ์ฑ ํ ์คํธ์์ ์ ์ ๋ฐ๋๊ฒ ๋งค์ฐ ํ๋ค์๊ณ ๊ฒฐ๊ตญ ๋ค๋ฅธ ์ฌ๋๋ค์ ํ์ด๋ฅผ ์ฐธ๊ณ ํ๋ค..ใ import heapq def solution(food_times, k): answer = -1 # ์์ ์ดํฉ ๋ณด๋ค k๊ฐ ํฌ๋ฉด ์ญ์ทจํ ์์์ด ์์ด์ง๋ฏ๋ก -1 ๋ฐํ if sum(food_times)
[ํ์ด์ฌ PYTHON · ์๋ฐ Java] LeetCodeใFind Minimum in Rotated Sorted Arrayใ
Find Minimum in Rotated Sorted Array - LeetCode Can you solve this real interview question? Find Minimum in Rotated Sorted Array - Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become: * [4,5,6,7,0,1,2] if it leetcode.com ํ์ด์ฌ ์ฝ๋ class Solution(object): def findMin(self, nums): left = 0 right = len(nums..
[ํ์ด์ฌ PYTHON · ์๋ฐ JAVA] LeetCodeใMaximum Product Subarrayใ
Maximum Product Subarray - LeetCode Can you solve this real interview question? Maximum Product Subarray - Given an integer array nums, find a subarray that has the largest product, and return the product. The test cases are generated so that the answer will fit in a 32-bit integer. Examp leetcode.com ์๋ฐ ์ฝ๋ class Solution { public int maxProduct(int[] nums) { int [] dp = new int[nums.length]; in..
[ํ์ด์ฌ PYTHON · ์๋ฐ JAVA] LeetCodeใMaximum Subarrayใ
Maximum Subarray - LeetCode Can you solve this real interview question? Maximum Subarray - Given an integer array nums, find the subarray with the largest sum, and return its sum. Example 1: Input: nums = [-2,1,-3,4,-1,2,1,-5,4] Output: 6 Explanation: The subarray [4,-1,2,1] has t leetcode.com ์๋ฐ ์ฝ๋ class Solution { public int maxSubArray(int[] nums) { int[] dp = new int[nums.length]; dp[0]=nums..
[ํ์ด์ฌ PYTHON · ์๋ฐ JAVA] LeetCodeใProduct of Array Except Selfใ
Product of Array Except Self - LeetCode Can you solve this real interview question? Product of Array Except Self - Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i]. The product of any prefix or suffix of nu leetcode.com ์๋ฐ ์ฝ๋ import java.util.*; class Solution { public int[] productExceptSelf(int[] nums) { ..
[ํ์ด์ฌ PYTHON · ์๋ฐ JAVA] LeetCodeใContains Duplicateใ
Contains Duplicate - LeetCode Can you solve this real interview question? Contains Duplicate - Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct. Example 1: Input: nums = [1,2,3,1] Output: true Ex leetcode.com ํ์ด์ฌ ์ฝ๋ class Solution(object): def containsDuplicate(self, nums): _set = set() for num in nums: if nu..
[ํ์ด์ฌ PYTHON · ์๋ฐ JAVA] LeetCodeใBest Time to Buy and Sell Stockใ
Best Time to Buy and Sell Stock - LeetCode Can you solve this real interview question? Best Time to Buy and Sell Stock - You are given an array prices where prices[i] is the price of a given stock on the ith day. You want to maximize your profit by choosing a single day to buy one stock and choosin leetcode.com ํ์ด์ฌ ์ฝ๋ class Solution(object): def maxProfit(self, prices): buy = prices[0] ans = 0 f..
[ํ์ด์ฌ PYTHON · ์๋ฐ JAVA] LeetCodeใTwo sumใ
Two Sum - LeetCode Can you solve this real interview question? Two Sum - Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not leetcode.com ํ์ด์ฌ ์ฝ๋ class Solution(object): def twoSum(self, nums, target): dic = {} for i in range(len(nums)): search = ta..
[ํ์ด์ฌ PYTHON] ๋ฐฑ์ค 2638๋ฒ ใ์น์ฆใ
import sys from collections import deque import copy row, col = map(int, input().split()) _map = [list(map(int, sys.stdin.readline().split())) for _ in range(row)] queue = deque([]) checked = [[False]*col for _ in range(row)] dr = [1, -1, 0, 0] dc = [0, 0, 1, -1] def bfs(): copiedCheck = copy.deepcopy(checked) queue.append([0, 0]) while queue: r, c = queue.popleft() for i in range(4): nr = r + d..
[ํ์ด์ฌ PYTHON] ๋ฐฑ์ค 16236๋ฒ ใ์๊ธฐ ์์ดใ
import sys import copy from collections import deque # ์์ ์ ๋ ฅ N = int(input()) _map = [[0]*N for _ in range(N)] checked = [[False]*N for _ in range(N)] queue = deque([]) for i in range(N): info = list(map(int,sys.stdin.readline().split())) for j in range(N): _map[i][j]=info[j] if info[j] == 9: _map[i][j] = 0 shark = [i,j,0] queue.append(shark) # bfs()๋ก ๋จน์ ์ ์๋ ๋จน์ด ํ์ํ๊ธฐ dr = [1,-1,0,0] dc = [0,0,1,..
[ํ์ด์ฌ PYTHON] ๋ฐฑ์ค 14502๋ฒ ใ์ฐ๊ตฌ์ใ
import copy import sys from collections import deque # ์์ ์ ๋ ฅ row,col= map(int,input().split()) _map = [[0]*col for _ in range(row)] for i in range(row): info = list(map(int,sys.stdin.readline().split())) for j in range(col): _map[i][j]=info[j] queue = deque([]) # ๋ฐ์ด๋ฌ์ค ์์น ์ ์ฅ for i in range(row): for j in range(col): if _map[i][j] == 2: queue.append([i,j]) dr = [1,-1,0,0] dc = [0,0,1,-1] ans = -1e..
[ํ์ด์ฌ PYTHON] ๋ฐฑ์ค 15686๋ฒ ใ์นํจ ๋ฐฐ๋ฌใ
import sys #์์ ์ ๋ ฅ N,M = map(int,sys.stdin.readline().split()) _map = [list(map(int,sys.stdin.readline().split())) for _ in range(N)] chickens = [] houses = [] # ์ง๊ณผ ์นํจ์ง ์ขํ ์ ์ฅ for row in range(N): for col in range(N): if _map[row][col] == 2: chickens.append([row,col]) elif _map[row][col] == 1: houses.append([row,col]) selected = [] checked = [False]*(len(chickens)) ans = 1e9 def getMinDistance(sel..
[ํ์ด์ฌ PYTHON] ๋ฐฑ์ค 15684๋ฒ ใ์ฌ๋ค๋ฆฌ ์กฐ์ใ
import sys # ์์ ์ ๋ ฅ N, M, H = map(int, sys.stdin.readline().split()) ladders = [list(map(int, sys.stdin.readline().split())) for _ in range(M)] _map = [[0] * N for _ in range(H)] # ์ฌ๋ค๋ฆฌ ์ ๋ณด ์ ์ฅ # ์ผ์ชฝ์ 1 ์ค๋ฅธ์ชฝ์ -1๋ก ์ ์ฅ for ladder in ladders: ladderR = ladder[0] - 1 ladderC = ladder[1] - 1 _map[ladderR][ladderC] = 1 _map[ladderR][ladderC + 1] = -1 # ๋ชจ๋ i๋ฒ ๊ฒฐ๊ณผ๊ฐ i๋ฒ ์ธ๊ฐ? def isEqual(): for i in range(N): locat..
[ํ์ด์ฌ PYTHON] ๋ฐฑ์ค 3190๋ฒ ใ๋ฑใ
from collections import deque boardSize = int(input()) appleNum = int(input()) # ๋งต ์์ฑ board = [[0]*boardSize for _ in range(boardSize)] # ์ฌ๊ณผ ์์น ์ ์ฅ (1๋ก ํ์) for _ in range(appleNum): row,col=map(int,input().split()) board[row-1][col-1] = 1 # ๋ช ๋ น ์ ์ฅ (key : value ๋ก) commands = {} commandNum = int(input()) for _ in range(commandNum): sec,command = map(str,input().split()) commands[int(sec)]=command # ..
[ํ์ด์ฌ PYTHON] ๋ฐฑ์ค 2812๋ฒ ใํฌ๊ฒ ๋ง๋ค๊ธฐใ
๋ฌธ์ ์ ๊ทผ ๋ฐฉ๋ฒ์ ์์ ๋ก ์ค๋ช ํด๋ณด๊ฒ ๋ค. 10 4 4177252841 ์์ ๊ฐ์ด 10๊ฐ์ ์ซ์๊ฐ ์ฃผ์ด์ง๊ณ , 4๊ฐ์ ์ซ์๋ฅผ ์ญ์ ํ ์ ์๋ค. ๊ฐ์ฅ ํฐ ์๋ฅผ ์ถ๋ ฅํ๊ธฐ ์ํด์๋, ์์ ์๋ฆฌ์๊ฐ ์ปค์ผํ๋ค. ์ ์๋ฆฌ ์๋ถํฐ ํ๋์ฉ ์คํ์ ๋ฃ์ ๊ฒ์ธ๋ฐ, ์คํ์ ๋ค์ด๊ฐ ์๊ฐ ์คํ์ ๋ค์ด์๋ ์๋ณด๋ค ํฐ ๊ฒฝ์ฐ & ์ญ์ ํ์๊ฐ ๋จ์์๋ ๊ฒฝ์ฐ ์คํ์ ์๋ ์ซ์๋ฅผ ์ง์ธ ๊ฒ์ด๋ค. ์์๋ก ๋ณด๋ฉด ํ์ฌ answer ์คํ = [] ์ ๋น์ด์๋ค. 4177252841 ์๋ฅผ ์ ์๋ฆฌ ์๋ถํฐ ๋ฃ์ ๊ฒ์ด๋ค. answer ์คํ์ ๋น๊ตํ ์๊ฐ ์์ผ๋ฏ๋ก ์ฒซ๋ฒ์งธ ์๋ฆฌ๋ ์ผ๋จ ์คํ์ ์ ๋ ฅํ๋ค. answer = [ 4 ] 4 177252841 ์ด์ 1์ ๋ฃ์ ์ฐจ๋ก์ธ๋ฐ, answer ์คํ ์์ ์๋ 4๊ฐ ๋ ํฌ๋ฏ๋ก ์ผ๋จ ์ ๋ ฅํ๋ค. answer = [ 4 1..