Skip to content

Commit acb902e

Browse files
committed
7 problems
1 parent 92f5234 commit acb902e

7 files changed

+297
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Naive DFS with exhaustive search. TLE
2+
class Solution {
3+
func isValidPalindrome(_ s: String, _ k: Int) -> Bool {
4+
guard s.count > k else {
5+
return true
6+
}
7+
return isValidPalindromeDFSHelper(Array(s), k)
8+
}
9+
10+
func isValidPalindromeDFSHelper(_ sArray: [Character], _ k: Int) -> Bool {
11+
guard sArray.count > 1 else {
12+
return true
13+
}
14+
15+
if isPalindrome(sArray) {
16+
return true
17+
} else if k > 0 {
18+
var result = [Bool]()
19+
for index in 0..<sArray.count {
20+
var isPalin = false
21+
if index == 0 {
22+
isPalin = isValidPalindromeDFSHelper(Array(sArray[(index + 1)..<sArray.count]), k - 1)
23+
} else if index == sArray.count - 1 {
24+
isPalin = isValidPalindromeDFSHelper(Array(sArray[0..<index]), k - 1)
25+
} else {
26+
isPalin = isValidPalindromeDFSHelper(Array(sArray[0..<index]) + Array(sArray[(index + 1)..<sArray.count]), k - 1)
27+
}
28+
result.append(isPalin)
29+
}
30+
return !result.allSatisfy { $0 == false }
31+
} else {
32+
return false
33+
}
34+
}
35+
36+
func isPalindrome(_ sArray: [Character]) -> Bool {
37+
var leftPtr = 0, rightPtr = sArray.count - 1
38+
while leftPtr <= rightPtr {
39+
if sArray[leftPtr] == sArray[rightPtr] {
40+
leftPtr += 1
41+
rightPtr -= 1
42+
} else {
43+
return false
44+
}
45+
}
46+
return true
47+
}
48+
}
49+
50+
// 2D DP like Longest palindrome subsequence. Accepted
51+
class Solution {
52+
func isValidPalindrome(_ s: String, _ k: Int) -> Bool {
53+
guard s.count > k else {
54+
return true
55+
}
56+
let sArray = Array(s)
57+
var dp = Array(repeating: Array(repeating: 0, count: sArray.count), count: sArray.count)
58+
59+
for i in 0..<sArray.count {
60+
dp[i][i] = 1
61+
}
62+
63+
for startIndex in stride(from: sArray.count - 2, through: 0, by: -1) {
64+
for endIndex in (startIndex + 1)..<sArray.count {
65+
if sArray[startIndex] == sArray[endIndex] {
66+
dp[startIndex][endIndex] = 2 + dp[startIndex + 1][endIndex - 1]
67+
} else {
68+
dp[startIndex][endIndex] = max(dp[startIndex + 1][endIndex], dp[startIndex][endIndex - 1])
69+
}
70+
}
71+
}
72+
73+
return (sArray.count - dp[0][sArray.count - 1]) <= k
74+
}
75+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* public var val: Int
5+
* public var left: TreeNode?
6+
* public var right: TreeNode?
7+
* public init() { self.val = 0; self.left = nil; self.right = nil; }
8+
* public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
9+
* public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
10+
* self.val = val
11+
* self.left = left
12+
* self.right = right
13+
* }
14+
* }
15+
*/
16+
17+
class Solution {
18+
func balanceBST(_ root: TreeNode?) -> TreeNode? {
19+
var sortedNodeVals = [Int]()
20+
inorderDFS(root, &sortedNodeVals)
21+
return balanceBST_HelperDFS(sortedNodeVals)
22+
}
23+
24+
func inorderDFS(_ root: TreeNode?, _ array: inout [Int] ) {
25+
guard let root = root else {
26+
return
27+
}
28+
inorderDFS(root.left, &array)
29+
array.append(root.val)
30+
inorderDFS(root.right, &array)
31+
}
32+
33+
func balanceBST_HelperDFS(_ sortedArray: [Int] ) -> TreeNode? {
34+
guard sortedArray.count > 0 else {
35+
return nil
36+
}
37+
let mid = sortedArray.count / 2
38+
let root = TreeNode(sortedArray[mid])
39+
root.left = balanceBST_HelperDFS(Array(sortedArray[0..<mid]))
40+
root.right = balanceBST_HelperDFS(Array(sortedArray[(mid + 1)..<sortedArray.count]))
41+
return root
42+
}
43+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Using dictionary. Accepted
2+
class SparseVector {
3+
var nonZeros = [Int:Int]()
4+
init(_ nums: [Int]) {
5+
for (index, item) in nums.enumerated() {
6+
if item != 0 {
7+
nonZeros[index] = item
8+
}
9+
}
10+
}
11+
12+
// Return the dotProduct of two sparse vectors
13+
func dotProduct(_ vec: SparseVector) -> Int {
14+
var result = 0
15+
for index in vec.nonZeros.keys {
16+
if let otherItem = nonZeros[index] {
17+
result += (otherItem * vec.nonZeros[index]!)
18+
}
19+
}
20+
return result
21+
}
22+
}
23+
24+
// Using 2 pointers. Accepted
25+
class SparseVector {
26+
var pairs = [(Int, Int)]() // (index, item)
27+
init(_ nums: [Int]) {
28+
for (index, item) in nums.enumerated() {
29+
if item != 0 {
30+
pairs.append((index, item))
31+
}
32+
}
33+
}
34+
35+
// Return the dotProduct of two sparse vectors
36+
func dotProduct(_ vec: SparseVector) -> Int {
37+
var result = 0, ptrOne = 0, ptrTwo = 0
38+
while ptrOne < pairs.count && ptrTwo < vec.pairs.count {
39+
if pairs[ptrOne].0 == vec.pairs[ptrTwo].0 {
40+
result += (pairs[ptrOne].1 * vec.pairs[ptrTwo].1)
41+
ptrOne += 1
42+
ptrTwo += 1
43+
} else if pairs[ptrOne].0 < vec.pairs[ptrTwo].0 {
44+
ptrOne += 1
45+
} else {
46+
ptrTwo += 1
47+
}
48+
}
49+
return result
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
func lowestCommonAncestor(_ p: Node?,_ q: Node?) -> Node? {
3+
var pDepth = getDepth(p, 0), qDepth = getDepth(q, 0)
4+
var pCopy = p, qCopy = q
5+
if pDepth > qDepth {
6+
while pDepth > qDepth {
7+
pCopy = pCopy.parent
8+
pDepth -= 1
9+
}
10+
} else if pDepth < qDepth {
11+
while pDepth < qDepth {
12+
qCopy = qCopy.parent
13+
qDepth -= 1
14+
}
15+
}
16+
17+
while pCopy != qCopy {
18+
pCopy = pCopy.parent
19+
qCopy = qCopy.parent
20+
}
21+
22+
return pCopy
23+
}
24+
25+
func getDepth(_ node: Node?, _ depth: Int) -> Int {
26+
guard let node = node else {
27+
return depth
28+
}
29+
return getDepth(node.parent, depth + 1)
30+
}
31+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
func maxLength(_ ribbons: [Int], _ k: Int) -> Int {
3+
var start = 1, end = ribbons.max()!, sum = ribbons.reduce(0, +)
4+
if k > sum {
5+
return 0
6+
}
7+
while start <= end {
8+
let mid = start + ((end - start) / 2)
9+
if iSPossibleToCut(ribbons, k, mid) {
10+
start = mid + 1
11+
} else {
12+
end = mid - 1
13+
}
14+
}
15+
return start - 1
16+
}
17+
18+
func iSPossibleToCut(_ ribbons: [Int], _ k: Int, _ targetLength: Int) -> Bool {
19+
var count = 0
20+
for ribbon in ribbons {
21+
count += (ribbon / targetLength)
22+
}
23+
return count >= k ? true : false
24+
}
25+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
class MovingAverage {
3+
4+
var queue = [Int]()
5+
var currentSum = 0
6+
let size: Int
7+
init(_ size: Int) {
8+
self.size = size
9+
}
10+
11+
func next(_ val: Int) -> Double {
12+
queue.append(val)
13+
currentSum += val
14+
if queue.count <= size {
15+
return Double(currentSum) / Double(queue.count)
16+
} else {
17+
let first = queue.removeFirst()
18+
currentSum -= first
19+
return Double(currentSum) / Double(queue.count)
20+
}
21+
}
22+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
class Solution {
2+
func largestIsland(_ grid: [[Int]]) -> Int {
3+
var connectedGrid = grid
4+
var parentID = 2, parentLengthMap = [Int:Int]()
5+
for row in 0..<grid.count {
6+
for col in 0..<grid[0].count {
7+
if connectedGrid[row][col] == 1 {
8+
var currentLength = 1
9+
largestIslandDFSHelper(row, col, &connectedGrid, parentID, &currentLength)
10+
parentLengthMap[parentID] = currentLength
11+
parentID += 1
12+
}
13+
}
14+
}
15+
16+
var maxLength = Int.min
17+
for row in 0..<grid.count {
18+
for col in 0..<grid[0].count {
19+
if connectedGrid[row][col] == 0 {
20+
var currentLength = 1, parentIdSet = Set<Int>()
21+
for (r, c) in [(0, -1), (-1, 0), (0, 1), (1, 0)] {
22+
let (newR, newC) = (row + r, col + c)
23+
if newR < 0 || newR >= connectedGrid.count || newC < 0 || newC >= connectedGrid[0].count || connectedGrid[newR][newC] == 0 {
24+
continue
25+
}
26+
let currentParentID = connectedGrid[newR][newC]
27+
if !parentIdSet.contains(currentParentID) {
28+
currentLength += parentLengthMap[currentParentID]!
29+
parentIdSet.insert(currentParentID)
30+
}
31+
}
32+
maxLength = max(maxLength, currentLength)
33+
}
34+
}
35+
}
36+
return maxLength == Int.min ? parentLengthMap.values.first! : maxLength
37+
}
38+
39+
func largestIslandDFSHelper(_ row: Int, _ col: Int, _ connectedGrid: inout [[Int]], _ parentID: Int, _ currentLength: inout Int) {
40+
connectedGrid[row][col] = parentID
41+
for (r, c) in [(0, -1), (-1, 0), (0, 1), (1, 0)] {
42+
let (newR, newC) = (row + r, col + c)
43+
if newR < 0 || newR >= connectedGrid.count || newC < 0 || newC >= connectedGrid[0].count || connectedGrid[newR][newC] != 1 {
44+
continue
45+
}
46+
currentLength += 1
47+
largestIslandDFSHelper(newR, newC, &connectedGrid, parentID, &currentLength)
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)