Algorithm(64)
-
2389. Longest Subsequence With Limited Sum
Python class Solution: def answerQueries(self, nums: List[int], queries: List[int]) -> List[int]: incs = [] nums.sort() for num in nums: if incs: incs += incs[-1] + num, else: incs += num, res = [] for query in queries: idx = bisect.bisect_left(incs, query) if idx < len(incs) and incs[idx] == query: res += idx + 1, else: res += idx, return res Kotlin class Solution { fun answerQueries(nums: IntA..
2023.01.24 -
2395. Find Subarrays With Equal Sum
Python class Solution: def findSubarrays(self, nums: List[int]) -> bool: n = len(nums) if n < 2: return False for i in range(n - 1): tot = nums[i] + nums[i + 1] for j in range(i + 1, n - 1): if tot == nums[j] + nums[j + 1]: return True return False Kotlin class Solution { fun findSubarrays(nums: IntArray): Boolean { val n: Int = nums.size if (n < 2) { return false } for (i in 0 until n - 1) { va..
2023.01.24 -
2405. Optimal Partition of String
abacaba 위와 같은 문자열이 입력으로 주어질 때, 이 문자열을 중복되는 문자가 없는 부분 문자열들로 쪼갤 때 초대한 적게 쪼개는 수를 반환하는 문제입니다. 해결 이 문제는 사실, 문제의 본질을 이해하느냐 못하느냐를 파악하기 위한 문제 입니다. 예를 들어, 입력으로 주어진 문자열을 서로 다른 여러 방식으로 쪼개는 것들을 모두 시도해 본 후 (brute-force) 이 중 가장 적은 수로 쪼개지는 경우를 찾고자 한다면, 다음과 같이 구현하면 됩니다. def partitionString(self, s: str) -> int: n = len(s) def dfs(start, cnt, trace): if start == n: return cnt if (start, cnt) in mem: return mem[(..
2023.01.24 -
2410. Maximum Matching of Players With Trainers
Intuition Approach Complexity Time complexity: O(NlogN) Space complexity: O(1) Code class Solution: def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int: cnt = 0 trainers.sort() for player in sorted(players): idx = bisect.bisect_left(trainers, player) if idx >= len(trainers): break trainers.pop(idx) cnt += 1 return cnt
2023.01.07 -
2409. Count Days Spent Together
Intuition this problem is easy. so, the day of arrive and leave are limited in the same year! so, simplely find out the leave day of the year and the arrive day of the year for both Alice and Bob. And then find the latest arrive day and the earliest leave day. If the leave day is bigger than or equal to the arrive day, then just calculate difference between leave day and arrive day. that's the a..
2023.01.07 -
Sneak
N = int(input()) K = int(input()) # 0: empty # 1: apple grid = [[0]*N for _ in range(N)] for _ in range(K): y, x = map(int, input().split()) grid[y - 1][x - 1] = 1 next_dirs = { (0, 1): { # R 'L': (-1, 0), 'D': (1, 0) }, (0, -1): { # L 'L': (1, 0), 'D': (-1, 0) }, (1, 0): { # D 'L': (0, 1), 'D': (0, -1) }, (-1, 0): { # U 'L': (0, -1), 'D': (0, 1) } } L = int(input()) moves = [] for _ in range(L)..
2022.07.24