Skip to content

Commit b383b21

Browse files
committed
add 300, 367, 383, 567, 1952, 2200, 2367, 2706 update 1512 at swift
1 parent 37266a5 commit b383b21

9 files changed

+177
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* 300. Longest Increasing Subsequence
3+
* https://leetcode.com/problems/longest-increasing-subsequence/
4+
**/
5+
6+
class Solution {
7+
func lengthOfLIS(_ nums: [Int]) -> Int {
8+
var dp: [Int] = Array(repeating: 1, count: nums.count)
9+
10+
for i in 0 ..< nums.count {
11+
for j in 0 ..< i {
12+
if nums[i] > nums[j] {
13+
dp[i] = max(dp[i], dp[j] + 1)
14+
}
15+
}
16+
}
17+
18+
return dp.max()!
19+
}
20+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* 367. Valid Perfect Square
3+
* https://leetcode.com/problems/valid-perfect-square/
4+
**/
5+
6+
class Solution {
7+
func isPerfectSquare(_ num: Int) -> Bool {
8+
for i in 1 ... num where i * i >= num {
9+
return i * i == num
10+
}
11+
return false
12+
}
13+
}

swift/0383-ransom-note.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* 383. Ransom Note
3+
* https://leetcode.com/problems/ransom-note/
4+
**/
5+
6+
class Solution {
7+
func canConstruct(_ ransomNote: String, _ magazine: String) -> Bool {
8+
var dict: [Character: (note: Int, magazine: Int)] = [:]
9+
10+
for ch in ransomNote {
11+
dict[ch, default: (0, 0)].note += 1
12+
}
13+
14+
for ch in magazine {
15+
dict[ch, default: (0, 0)].magazine += 1
16+
}
17+
18+
for (note, magaz) in dict.values where note > magaz {
19+
return false
20+
}
21+
22+
return true
23+
}
24+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* 567. Permutation in String
3+
* https://leetcode.com/problems/permutation-in-string/
4+
**/
5+
6+
class Solution {
7+
func checkInclusion(_ s1: String, _ s2: String) -> Bool {
8+
guard s1.count <= s2.count else { return false }
9+
10+
let s2arr = Array(s2)
11+
let s1count = s1.count, s2count = s2.count
12+
var dict: [Character: Int] = [:]
13+
14+
for ch in s1 {
15+
dict[ch, default: 0] += 1
16+
}
17+
18+
var l = 0, r = -1
19+
while l + r + 1 < s1count {
20+
r += 1
21+
dict[s2arr[r], default: 0] -= 1
22+
if dict[s2arr[r]] == 0 { dict[s2arr[r]] = nil }
23+
}
24+
25+
while !dict.isEmpty && r < s2count - 1 {
26+
r += 1
27+
dict[s2arr[r], default: 0] -= 1
28+
if dict[s2arr[r]] == 0 { dict[s2arr[r]] = nil }
29+
dict[s2arr[l], default: 0] += 1
30+
if dict[s2arr[l]] == 0 { dict[s2arr[l]] = nil }
31+
l += 1
32+
}
33+
34+
return dict.isEmpty
35+
}
36+
}

swift/1512-number-of-good-pairs.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,24 @@ class Solution {
1313

1414
return prev.filter{$0.value > 1}.map{$0.value * ($0.value-1) / 2}.reduce(0, +)
1515
}
16+
}
17+
18+
class Solution2 {
19+
func numIdenticalPairs(_ nums: [Int]) -> Int {
20+
var dict: [Int: Int] = [:], ans = 0
21+
22+
for (i, v) in nums.enumerated() {
23+
dict[v, default: 0] += 1
24+
}
25+
26+
for pairCount in dict.values where pairCount > 1 {
27+
var temp = pairCount
28+
while temp > 1 {
29+
ans += temp - 1
30+
temp -= 1
31+
}
32+
}
33+
34+
return ans
35+
}
1636
}

swift/1952-three-divisors.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* 1952. Three Divisors
3+
* https://leetcode.com/problems/three-divisors/
4+
**/
5+
6+
class Solution {
7+
func isThree(_ n: Int) -> Bool {
8+
var divisors = 0
9+
10+
for i in 1 ... n where n % i == 0 {
11+
divisors += 1
12+
if divisors > 3 { return false }
13+
}
14+
15+
return divisors == 3
16+
}
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* 2200. Find All K-Distant Indices in an Array
3+
* https://leetcode.com/problems/find-all-k-distant-indices-in-an-array/
4+
**/
5+
6+
class Solution {
7+
func findKDistantIndices(_ nums: [Int], _ key: Int, _ k: Int) -> [Int] {
8+
var ans: [Int] = []
9+
var from = 0
10+
for i in 0 ..< nums.count where nums[i] == key {
11+
for j in max(from, i - k) ... i + k where j < nums.count {
12+
ans.append(j)
13+
}
14+
from = i + k + 1
15+
}
16+
return ans
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* 2367. Number of Arithmetic Triplets
3+
* https://leetcode.com/problems/number-of-arithmetic-triplets/
4+
**/
5+
6+
class Solution {
7+
func arithmeticTriplets(_ nums: [Int], _ diff: Int) -> Int {
8+
var ans = 0
9+
for (i, iv) in nums.enumerated() {
10+
for (j, jv) in nums.enumerated() where j > i && jv - iv == diff {
11+
for (k, kv) in nums.enumerated() where k > j && kv - jv == diff {
12+
ans += 1
13+
}
14+
}
15+
}
16+
return ans
17+
}
18+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* 2706. Buy Two Chocolates
3+
* https://leetcode.com/problems/buy-two-chocolates/
4+
**/
5+
6+
class Solution {
7+
func buyChoco(_ prices: [Int], _ money: Int) -> Int {
8+
let prices = prices.sorted()
9+
return prices[0] + prices[1] <= money ? money - prices[0] - prices[1] : money
10+
}
11+
}

0 commit comments

Comments
 (0)