LeetCode 74. Search a 2D Matrix

LeetCode 74. Search a 2D Matrix ๐Ÿ—“๏ธ Daily LeetCoding Challenge August, Day 7 Simple Search Think from-simple-to-complex! First, I just simply search over the matrix and check if target value is in each line. Fortunately, this code didnโ€™t trigger TLE. class Solution: def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: for i in range(len(matrix)): if matrix[i][-1] >= target: if target in matrix[i]: return True else: return False Binary Search However in order to improve runtime, itโ€™s better to use binary search than just checking with in. Similar to the previous simple search code. ...

2023-8-7 ยท 2 min ยท 286 words ยท Junha

LeetCode 1870. Minimum Speed to Arrive on Time

LeetCode 1870. Minimum Speed to Arrive on Time ๐Ÿ—“๏ธ Daily LeetCoding Challenge July, Day 26 (23.07.26) ์œผ์•„์•„โ€ฆ bruteforce๋กœ ํ’€์—ˆ๋”๋‹ˆ TLE (Time Limit Exceeded)โ€ฆ ๊ทธ๋ž˜์„œ binary-search๋กœ ํ’€์–ด๋„ ์—ฌ์ „ํžˆ TLE. ํ•˜โ€ฆ C++๋‚˜ Java๋Š” ์ž˜๋งŒ ํ†ต๊ณผํ•˜๋Š”๊ฑฐ ๊ฐ™์€๋ฐ โ€˜[1,1,10000000]โ€™ ๊ฐ™์€ ์˜ˆ์‹œ๊ฐ€ ๋‚˜์˜ค๋ฉด time complexity๊ฐ€ $ O(10^7) $ ์ด ๋˜์–ด๋ฒ„๋ ค์„œ ํŒŒ์ด์ฌ์€ ์‚๋น…- ์‹คํŒจ!๋ฅผ ์™ธ์นœ๋‹คใ…œใ…œ binary search ์ด๋ถ„ ํƒ์ƒ‰ ํ’€์ด ์•„๋ž˜ ์ฝ”๋“œ์ฒ˜๋Ÿผ ๊น”๋”ํ•˜๊ฒŒ ํ’€์—ˆ๋Š”๋ฐ๋„ TLE ์˜ค๋ฅ˜๊ฐ€ ๋–ด๋‹ค. ์•„๋ฌด๋ž˜๋„ ํ•จ์ˆ˜ ๋”ฐ๋กœ ํ•˜๋Š” ๊ฒƒ ์กฐ์ฐจ๋„ ์‹œ๊ฐ„ ๋ณต์žก๋„์— ๊ธฐ์—ฌํ•˜๋Š”๋“ฏ, ํ•œ๋ฒˆ์— ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ์‹์œผ๋กœ ๋‹ค์‹œ ์žฌ๊ตฌ์„ฑํ•ด๋ณด๊ธฐ๋กœ ํ–ˆ๋‹ค. ...

2023-7-26 ยท 2 min ยท 245 words ยท Junha

LeetCode 852. Peak Index in a Mountain Array

LeetCode 852. Peak Index in a Mountain Array ๐Ÿ—“๏ธ Daily LeetCoding Challenge July, Day 25 (23.07.25) LeetCode์—์„œ๋Š” ์ผ๋ช… Daily Challenge๋ฅผ ์ œ๊ณตํ•˜๋Š”๋ฐ, ํ•˜๋ฃจ์— ํ•˜๋‚˜์”ฉ ํ’€์–ด๋‚˜๊ฐ€๋ฉด ๋‹ค์–‘ํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ๋“ค์„ ํ’€์–ด๋ณผ ์ˆ˜ ์žˆ์–ด์„œ ์œ ์ตํ•˜๋‹ค. ๋ณดํ†ต ๋‹ค๋ฅธ ๋ฌธ์ œ๋“ค์€ Editoral ๋ถ€๋ถ„์ด ์ž ๊ธˆ๋˜์–ด์žˆ์–ด Premium User๊ฐ€ ์•„๋‹ˆ๋ฉด ์ฝ์ง€ ๋ชปํ•˜๋Š”๋ฐ, Daily Challenge๋งŒํผ์€ ๊ณต๊ฐœ๋˜์–ด ์ž์‹ ์˜ ํ’€์ด์™€ ๋น„๊ตํ•ด๋ณผ ์ˆ˜ ์žˆ๋‹ค. LeetCode์— ์ž…๋ฌธํ•œ์ง€ ์ด์ œ ๊ฒจ์šฐ 3์ผ์ด์ง€๋งŒ, ๋‹ค๋ฅธ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์‚ฌ์ดํŠธ์— ๋น„ํ•ด์„œ ์ฒด๊ณ„๊ฐ€ ํ™•์‹คํžˆ ์ž˜ ์žกํ˜€์žˆ์–ด์„œ ์œ ์ €๋“ค์—๊ฒŒ ๋งค์šฐ ํŽธ๋ฆฌํ•˜๊ฒŒ ๋‹ค๊ฐ€์˜จ๋‹ค. ์ด๋ฒˆ ๋ฌธ์ œ๋Š” ๋ฐฐ์—ด์˜ ์ฆ๊ฐ€ํ•˜๋‹ค๊ฐ€ ๊ฐ์†Œํ•˜๋Š” ์ˆ˜์—ด์˜ ๊ทน๊ฐ’์˜ index์„ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ๋‹ค. ๋ณด์ž๋งˆ์ž ์ด๋ถ„ํƒ์ƒ‰(binary-search) ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•ด์•ผ๊ฒ ๋‹ค๊ณ  ์ƒ๊ฐ์ด ๋“ค์—ˆ๋‹ค. ๋ฌธ์ œ ์กฐ๊ฑด์—์„œ๋„ O(logn) ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ์‚ฌ์šฉํ•˜๋ผ๊ณ  ์ œ์‹œ๋˜์–ด์žˆ๊ธฐ์— ํ™•์‹ ์„ ๊ฐ€์ง€๊ณ  ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ๋‹ค. ...

2023-7-25 ยท 2 min ยท 251 words ยท Junha

๋ฐฑ์ค€ 1654๋ฒˆ - ๋žœ์„  ์ž๋ฅด๊ธฐ

๋ฐฑ์ค€ 1654๋ฒˆ ๋ฐ”๋กœ๊ฐ€๊ธฐ ๋‚˜์˜ ํ’€์ด # ์ž…๋ ฅ n, m = map(int,input().split()) lines = [] for _ in range(n): lines.append(int(input())) start,end = 1,max(lines) # ์ฒ˜๋ฆฌ while start <= end: mid = (start + end) // 2 temp = 0 for l in lines: temp += (l // mid) if temp >= m: start = mid + 1 else: end = mid-1 print(end) CODE REVIEW ์ด๋ถ„ํƒ์ƒ‰์„ ์‘์šฉํ•ด์„œ ํ•ด๊ฒฐํ•˜๋Š” ๋ฌธ์ œ. ์ฒ˜์Œ ๋ดค์„ ๋•Œ์—๋Š” ๋ง‰๋ง‰ํ–ˆ๋Š”๋ฐ, ์ตœ์ข… ์ž๋ฅธ ์„ ์˜ ๊ธธ์ด๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ด๋ถ„ ํƒ์ƒ‰์„ ์‹ค์‹œํ•ด์„œ ๋‹ต์„ ์ฐพ์•„๋‚ด์—ˆ๋‹ค. start=1, end=max(lines)๋กœ ์„ค์ •ํ–ˆ๋Š”๋ฐ, ๋งŒ์ผ end=min(lines)๋กœ ์„ค์ •ํ•˜๋ฉด [1,100,100,100,100]๊ฐ™์€ input์˜ ๊ฒฝ์šฐ 1์„ ์ถœ๋ ฅํ•ด์„œ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

2023-5-29 ยท 1 min ยท 94 words ยท Junha

๋ฐฑ์ค€ 2805๋ฒˆ - ๋‚˜๋ฌด ์ž๋ฅด๊ธฐ

๋ฐฑ์ค€ 2805๋ฒˆ ๋ฐ”๋กœ๊ฐ€๊ธฐ ์ฒซ ๋ฒˆ์งธ ํ’€์ด # ์ž…๋ ฅ n, m = map(int,input().split()) tree = list(map(int,input().split())) h = max(tree) current = 0 # ํ•จ์ˆ˜ def relu(x): if x>0: return x else: return 0 # ์ฒ˜๋ฆฌ while current < m: temp = 0 for t in tree: temp += relu(t-h) current = temp h -= 1 print(h+1) ๋‘ ๋ฒˆ์งธ ํ’€์ด # ์ž…๋ ฅ n, m = map(int,input().split()) tree = list(map(int,input().split())) start,end = 1,sum(tree) # ํ•จ์ˆ˜ def relu(x): if x>0: return x else: return 0 # ์ฒ˜๋ฆฌ while start <= end: mid = (start + end) // 2 temp = 0 for t in tree: temp += relu(t-mid) if temp >= m: start = mid + 1 else: end = mid-1 print(end) CODE REVIEW ์ฒซ ๋ฒˆ์จฐ ํ’€์ด์—์„œ๋Š” ๋ธŒ๋ฃจํŠธํฌ์Šค๋กœ h๋ฅผ max(tree)์—์„œ 1์”ฉ ๋‚ด๋ ค๊ฐ€๋ฉฐ ํ™•์ธํ•˜๋Š” ๋ฐฉ์‹์„ ์ทจํ–ˆ๋‹ค. ์‹œ๊ฐ„์ดˆ๊ณผ ์—๋Ÿฌ๋กœ ํƒˆ๋ฝโ€ฆ ์ด๋ถ„ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ™œ์šฉํ•ด๋ณด์ž. ๋‘ ๋ฒˆ์งธ ํ’€์ด์—์„œ๋Š” ์ด๋ถ„ํƒ์ƒ‰์„ ํ™œ์šฉํ•ด์„œ ํ•ด๊ฒฐํ–ˆ๋‹ค. h์˜ ๋ฒ”์œ„๋ฅผ start,end๋กœ ์žก๊ณ  start=end๋  ๋•Œ ๊นŒ์ง€ ๋ฐ˜๋ณตํ–ˆ๋‹ค. ์ž๋ฅธ ๋‚˜๋ฌด์˜ ๊ธธ์ด๋ฅผ ์–ด๋–ป๊ฒŒ ๊ตฌํ˜„ํ• ๊นŒ ๊ณ ๋ฏผํ•˜๋‹ค๊ฐ€, ๋จธ์‹ ๋Ÿฌ๋‹์—์„œ activation function ์ค‘ ํ•˜๋‚˜์ธ relu๋ฅผ ๊ฐ€์ง€๊ณ  ๊ตฌํ˜„ํ–ˆ๋‹ค. ๊ทผ๋ฐ ๋ฉ”๋ชจ๋ฆฌ ์•„๋ผ๊ธฐ ์œ„ํ•ด์„œ๋Š” relu()ํ•จ์ˆ˜ ๋Œ€์‹  if t>h: temp+=t-mid๋กœ ๊ตฌํ˜„ํ•˜๋Š”๊ฒŒ ๋‚˜์„ ๊ฒƒ ๊ฐ™๊ธด ํ•˜๋‹ค.

2023-5-28 ยท 1 min ยท 179 words ยท Junha