Given a non-negative integer num, return the number of steps to reduce it to zero. If the current number is even, you have to divide it by 2, otherwise, you have to subtract 1 from it.
示例:
1 2 3 4 5 6 7 8 9
Input: num = 14 Output: 6 Explanation: Step 1) 14 is even; divide by 2 and obtain 7. Step 2) 7 is odd; subtract 1 and obtain 6. Step 3) 6 is even; divide by 2 and obtain 3. Step 4) 3 is odd; subtract 1 and obtain 2. Step 5) 2 is even; divide by 2 and obtain 1. Step 6) 1 is odd; subtract 1 and obtain 0.
这个题题面很简单,一个非负整数,偶数除2,奇数减1,求需要多少步到0
暴力写
1 2 3 4 5 6 7 8 9 10 11 12 13
classSolution: defnumberOfSteps(self, num: int) -> int: count = 0 if num == 0: return count result = num while result > 1: if result % 2 == 0: result = int(result / 2) else: result -= 1 count += 1 return count + 1
Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold
题面:
Given an array of integers arr and two integers k and threshold. Return the number of sub-arrays of size k and average greater than or equal to threshold.
示例:
1 2 3
Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4 Output: 3 Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold)
给定一个数组和一个长度 k,和一个阈值 threshold ,求这个数组中的所有长度为 K 且平均数大于等于阈值的子数组的个数。这个题,暴力写很简单,一个简单的数组的拆分,sum(arr[i:i+k])/k >= threshold 即可,但是这里有个问题,如果实时求和,那么时间复杂度为 O(M*K) M 为数组的长度,这个时候暴力会 T
因此需要做个小技巧的优化。可以考虑这样这样一个做法,假设当前 i 及其后 k 个数的和为 sum[i],那么有这样一个公式,sum[i]=sum[i-1]-arr[i]+arr[i+k-1],这样每次计算和都是 O(1) 的复杂度,那么整体就是一个 O(N) 的做法
classSolution: defangleClock(self, hour: int, minutes: int) -> float: hour %= 12 result = abs((minutes * 6) - (hour * 30 + minutes * 0.5)) return result if result < 360else360 - result
1345. Jump Game IV
题面:
Given an array of integers arr, you are initially positioned at the first index of the array. In one step you can jump from index i to index:
i + 1 where: i + 1 < arr.length.
i - 1 where: i - 1 >= 0.
j where: arr[i] == arr[j] and i != j. Return the minimum number of steps to reach the last index of the array. Notice that you can not jump outside of the array at any time.
示例:
1 2 3
Input: arr = [100,-23,-23,404,100,23,23,23,3,404] Output: 3 Explanation: You need three jumps from index 0 --> 4 --> 3 --> 9. Note that index 9 is the last index of the array.
还是跳格子,给定一个数组,里面会有一些具体的值,现在从 index = 0 的地方起跳,跳跃规则如下:
在 i+1 或 i-1 都在数组的范围内
如果存在 index=j 且 arr[i]==arr[j] 且 i!=j 的时候,可以直接从 i 跳到 j
求从 index=0 跳到 index=arr.length-1 最小的次数
这题我还是没 A,后面琢磨了下,一个搜索的题目
构建一个字典,值为key,index 为 value(相同的值之间可以直接跳)
利用一个 set 来保存跳过的点
从 index = 0 开始进行 BFS ,求每个点在一步之内可以跳到哪个点,然后不断的 BFS 直到到达终点