LeetCode双周赛#35】的更多相关文章

1589. 所有排列中的最大和 #差分 #贪心 题目链接 题意 给定整数数组nums,以及查询数组requests,其中requests[i] = [starti, endi] .第i个查询求 nums[starti] + nums[starti + 1] + ... + nums[endi - 1] + nums[endi] 的结果 . 你可以任意排列 nums 中的数字,请你返回所有查询结果之和的最大值,请将答案对 109 + 7 取余 后返回. 分析 我们先离线获得需要查询的区间,并统计该…
一个坐标可以从 -infinity 延伸到 +infinity 的 无限大的 棋盘上,你的 骑士 驻扎在坐标为 [0, 0] 的方格里. 骑士的走法和中国象棋中的马相似,走 “日” 字:即先向左(或右)走 1 格,再向上(或下)走 2 格:或先向左(或右)走 2 格,再向上(或下)走 1 格. 每次移动,他都可以按八个方向之一前进. 现在,骑士需要前去征服坐标为 [x, y] 的部落,请你为他规划路线. 最后返回所需的最小移动次数即可.本题确保答案是一定存在的. 示例 : 输入:x = , y…
给你一个矩阵 mat,其中每一行的元素都已经按 递增 顺序排好了.请你帮忙找出在所有这些行中 最小的公共元素. 如果矩阵中没有这样的公共元素,就请返回 -1. 示例: 输入:mat = [[,,,,],[,,,,],[,,,,],[,,,,]] 输出: 解法: 暴力解法  就是使用哈希记录每行 然后比较 代码 class Solution { public: unordered_map<]; int smallestCommonElement(vector<vector<int>&…
题目链接 给你一个整数数组 arr 和一个整数值 target . 请你在 arr 中找 两个互不重叠的子数组 且它们的和都等于 target .可能会有多种方案,请你返回满足要求的两个子数组长度和的 最小值 . 请返回满足要求的最小长度和,如果无法找到这样的两个子数组,请返回 -1 . 一个map用来保存从0-index i 的前缀和以及索引 ------mp[前缀和] = 索引 一个dp用来保存不大于目前索引i的最小长度的子数组长度, 如果不存在, 则为maxn 用一个sum做累加, 同时对…
1604. 警告一小时内使用相同员工卡大于等于三次的人 题目链接 题意 给定两个字符串数组keyName和keyTime,分别表示名字为keytime[i]的人,在某一天内使用员工卡的时间(格式为24小时制,"HH:MM").你要找出一小时内使用员工卡大于等于3的人,名字按字典序升序排列.注意,"23:51"-"00:10"不被视为一小时内,因为系统记录的是某一天内的使用情况 分析 给每个人创建一个数组,记录所有的打卡时间,然后将每个人名字字符串…
5492. 分割字符串的方案数 #组合公式 #乘法原理 #区间分割 题目链接 题意 给定01二进制串\(s\),可将\(s\)分割为三个非空 字符串\(s_1,s_2,s_3\),即(\(s_1+s_2+s_3=s\)).现要你求出分割\(s\)的方案数,保证\(s_1,s_2,s_3\)中字符1的数目相同(对\(1e9+7\)取模),他们的长度不一定相等 分析 举个例子,01100011000101串,可以知道三个子串必须包含2个'1',我们观察到左子串的界限,可以如下划分:011|00011…
5480. 可以到达所有点的最少点数目 #贪心 题目链接 题意 给定有向无环图,编号从0到n-1,一个边集数组edges(表示从某个顶点到另一顶点的有向边),现要找到最小的顶点集合,使得从这些点出发,能够到达图中所有顶点. 样例 输出为[0, 2, 3].从这三个顶点出发即能访问所有顶点. 分析 实际上,只需要将所有入度为0的顶点加入解集即可.因为:1.入度为0的顶点若不加入解集,则除了它以外,没有其他顶点能够沿途访问到它.2.入度不为0的顶点一定能被某个顶点沿途访问到,为了保证解集尽可能小,那…
1540 K次操作转变字符串 #计数 题目链接 题意 给定两字符串\(s\)和\(t\),要求你在\(k\)次操作以内将字符串\(s\)转变为\(t\),其中第\(i\)次操作时,可选择如下操作: 选择字符串\(s\)中满足 \(1 \leq j \leq s.length\) 且之前未被选过的任意下标 \(j\)(下标从1开始),并将此位置的字符恰好切换 \(i\) 次.切换 1 次字符即用字母表中该字母的下一个字母替换它(字母表环状接起来,所以$'z'切换后会变成 \('a'\)). 请记住…
这次我只做对一题. 原因是题目返回值类型有误,写的是 String[] ,实际上应该返回 List<String> . 好吧,只能自认倒霉.就当涨涨经验. 5068. 前后拼接 解题思路 大致思路是用两层循环检查每两个短语是否能前后拼接成新短语.如果可以前后拼接,那么将它们拼接成新短语,并添加到结果列表.最后将列表升序返回. 由于需要第一个单词和最后一个单词,因此需要先进行分割处理,将得到的第一个单词和最后一个单词分别保存.由于可以确定大小,因此直接用二维数组保存即可. // ht[i][0]…
1287.有序数组中出现次数超过25%的元素 1288.删除被覆盖区间 1286.字母组合迭代器 1289.下降路径最小和 II 下降和不能只保留原数组中最小的两个,hacked. 1287.有序数组中出现次数超过25%的元素 1287.有序数组中出现次数超过25%的元素 给你一个非递减的 有序 整数数组,已知这个数组中恰好有一个整数,它的出现次数超过数组元素总数的 25%. 请你找到并返回这个整数 示例: **输入:** arr = [1,2,2,6,6,6,6,7,10] **输出:** 6…
基础的 api 还是不够熟悉啊 5112. 十六进制魔术数字 class Solution { public: char *lltoa(long long num, char *str, int radix) { const char index[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; unsigned long long unum; int i = 0, j, k; if(radix == 10 && num <…
第一题 用一个新数组newSalary保存去掉最低和最高工资的工资列表,然后遍历newSalary,计算总和,除以元素个数,就得到了平均值. class Solution { public: double average(vector<int>& salary) { sort(salary.begin(), salary.end()); vector<int> newSalary; for(int i = 1; i < salary.size() - 1; ++i)…
这套题不算难,但是因为是昨天晚上太晚了,好久没有大晚上写过代码了,有点不适应,今天上午一看还是挺简单的 5177. 转变日期格式   给你一个字符串 date ,它的格式为 Day Month Year ,其中: Day 是集合 {"1st", "2nd", "3rd", "4th", ..., "30th", "31st"} 中的一个元素. Month 是集合 {"Jan&q…
LeetCode第28题 Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Example 1: Input: haystack = "hello", needle = "ll" Output: 2 Example 2: Input: haystack = "aaaaa", needle…
If you understand the comments below, never will you make mistakes with binary search! thanks to A simple CPP solution with lower_bound and C++ O(logn) Binary Search that handles duplicate, thanks to phu1ku 's answer on the second post. http://en.cpp…
这道题是从优先队列的难题里面找到的一个题目.可是解法并不是优先队列,而是双项队列deque 其实只要知道思路,这一道题直接写没有太大的问题.我们看看题 给定一个数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧.你只可以看到在滑动窗口 k 内的数字.滑动窗口每次只向右移动一位. 返回滑动窗口最大值. 示例: 输入: nums = [1,3,-1,-3,5,3,6,7], 和 k = 3 输出: [3,3,5,5,6,7] 解释: 滑动窗口的位置 最大值 --------…
Description Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Example 1: Input: [,,,], Output: Example…
1.题目 35. Search Insert Position Easy 1781214Add to ListShare Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in th…
题意: 给你一个数组 arr ,该数组表示一个从 1 到 n 的数字排列.有一个长度为 n 的二进制字符串,该字符串上的所有位最初都设置为 0 . 在从 1 到 n 的每个步骤 i 中(假设二进制字符串和 arr 都是从 1 开始索引的情况下),二进制字符串上位于位置 arr[i] 的位将会设为 1 . 给你一个整数 m ,请你找出二进制字符串上存在长度为 m 的一组 1 的最后步骤.一组 1 是一个连续的.由 1 组成的子串,且左右两边不再有可以延伸的 1 . 返回存在长度 恰好 为 m 的…
@author: ZZQ @software: PyCharm @file: searchInsert.py @time: 2018/11/07 19:20 要求:给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元素. 示例 1: 输入: [1,3,5,6], 5 输出: 2 示例 2: 输入: [1,3,5,6], 2 输出: 1 示例 3: 输入: [1,3,5,6], 7 输出: 4 示例 4:…
这道题难度较低,没有必要作说明. Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Here are few examples.[1,3,5,6], 5…
凉凉,看来想做好一个题还不容易啊... 有点难受... 1.看看题目吧 Given a sorted array and a target value, return the index if the target is found. If not,return the index where it would be if it were inserted in order. You may assume no duplicates in the array. 给定一个有序数组,依旧是二分查找,…
题目描述: 方法: class CombinationIterator: def __init__(self, characters: str, combinationLength: int): self.s = characters self.pos = [x for x in range(combinationLength)] self.finished = False def next(self) -> str: ans = "".join([self.s[p] for p…
题目描述: 方法一:二分法 class Solution: def findSpecialInteger(self, arr: List[int]) -> int: span = len(arr)//4 + 1 for i in range(0,len(arr),span): a = bisect.bisect_left(arr,arr[i]) b = bisect.bisect_right(arr,arr[i]) if b - a >= span: return arr[i] return…
题目描述: 方法一:排序O(Nlogn) class Solution: def removeCoveredIntervals(self, intervals: List[List[int]]) -> int: intervals.sort(key = lambda x:(x[0],-x[1])) res,rmax = len(intervals),intervals[0][1] for i in range(1,len(intervals)): if i > 0 and intervals[…
题目描述: 方法一:动态规划 O(N^3) class Solution: def minFallingPathSum(self, arr: List[List[int]]) -> int: n = len(arr) for i in range(1,n): for j in range(n): arr[i][j] = arr[i][j] + min(arr[i-1][0:j] + arr[i-1][j+1:]) return min(arr[-1]) 优化:O(n^2logn) class S…
题目描述: 自己的提交: # """ # This is Sea's API interface. # You should not implement it, or speculate about its implementation # """ #class Sea(object): # def hasShips(self, topRight: 'Point', bottomLeft: 'Point') -> bool: # #clas…
题目描述: 自己的提交:动态规划 class Solution: def deleteTreeNodes(self, nodes: int, parent: List[int], value: List[int]) -> int: dp = [[0, 0]for _ in range(nodes)] p = collections.defaultdict(list) for i, v in enumerate(parent): p[v].append(i) for i in range(node…
题目描述: 自己的提交: class Solution: def removeInterval(self, intervals: List[List[int]], toBeRemoved: List[int]) -> List[List[int]]: res = [] for i in intervals: if i[0] >= toBeRemoved[1] or i[1] <= toBeRemoved[0]: res.append(i) else: if i[0] < toBeR…
自己的提交: class Solution: def toHexspeak(self, num: str) -> str: num = hex(int(num)) num = str(num)[2:] res = "" for i in num: ": i = "O" ": i = "I" i = i.upper() if i not in {"A", "B", "C&q…