Algorithm
本周选择的算法题是:Smallest Range I
规则如下:
Given an array A
of integers, for each integer A[i]
we may choose any x
with -K <= x <= K
, and add x
to A[i]
.
After this process, we have some array B
.
Return the smallest possible difference between the maximum value of B
and the minimum value of B
.
Example 1:
Input: A = [1], K = 0
Output: 0
Explanation: B = [1]
Example 2:
Input: A = [0,10], K = 2
Output: 6
Explanation: B = [2,8]
Example 3:
Input: A = [1,3,6], K = 3
Output: 0
Explanation: B = [3,3,3] or B = [4,4,4]
Note:
1 <= A.length <= 10000
0 <= A[i] <= 10000
0 <= K <= 10000
Solution
我实现的方案:
Runtime:132 ms,快过 86.95%。
Memory:14.8 MB,低于 11.11%。
class Solution:
def smallestRangeI(self, A: List[int], K: int) -> int:
# if not A:
# return 0
# minimum, maximum = A[0], A[0]
# for i in range(1, len(A)):
# minimum, maximum = min(minimum, A[i]), max(maximum, A[i])
# return max(0, maximum - minimum - 2 * K)
return max(0, max(A) - min(A) - 2 * K)
Review
Backwards compatibility for iOS 13 system colors
两篇关于 iOS 13 适配的文章,Dark Mode 主要需要对色值进行管理。
前者花了些篇幅使用脚本找出需要调整的地方,如从 .swift
文件中找:
$ find . -name '*.swift' \
-exec sed -i '' -E 's/#colorLiteral\(red: (.*), green: (.*), blue: (.*), alpha: (.*)\)/UIColor(red: \1, green: \2, blue: \3, alpha: \4)/ {} \;
从 xib
或 storyboard
中找:
$ find . -name '*.xib' -or -name '*.storyboard' \
-exec echo {} \; \
-exec xmlstarlet sel -t \
-m "//color[@colorSpace='custom']" -c . -n {} \;
Main.storyboard
<color red="1" green="0.69019607843137254" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
通过工具可以高效的找出需要修改的地方。
后面那篇主要是关于兼容性的,作者很巧妙的通过一个 ColorCompatibility
类来实现版本间的兼容:
enum ColorCompatibility {
static var label: UIColor {
if #available(iOS 13, *) {
return .label
}
return UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
}
static var secondaryLabel: UIColor {
if #available(iOS 13, *) {
return .secondaryLabel
}
return UIColor(red: 0.9215686274509803, green: 0.9215686274509803, blue: 0.9607843137254902, alpha: 0.6)
}
// ... 21 more definitions: full code at the bottom of this post
}
通过给 iOS 13 以下的系统实现相似的功能,做到业务端最少的修改量,还能后向兼容。
Tip
通过 travis.ci 的 matrix 可以为同一个项目/工程执行不同的 build 计划:
matrix:
include:
- osx_image: xcode11
xcode_sdk: iphonesimulator12.2
env: DESTINATION="platform=iOS Simulator,name=iPhone 11"
- osx_image: xcode11
xcode_sdk: iphonesimulator13.0
env: DESTINATION="platform=iOS Simulator,name=iPhone 11"
before_install:
- rvm use system
- brew install ruby
- sudo gem install cocoapods -v '1.8.0'
- pod repo update
script:
- xcodebuild clean build test -workspace Example/DKImagePickerControllerDemo.xcworkspace -scheme DKImagePickerControllerDemo -destination "$DESTINATION"
env
可以用在为不同的 sdk、image 指定不同的环境变量
Share
分享一篇关于坑位曝光实现方案:关于坑位曝的光检测方案