diff --git "a/lcp/LCP 52. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\346\237\223\350\211\262/README.md" "b/lcp/LCP 52. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\346\237\223\350\211\262/README.md" index 0acdbbff7b8c7..b9fe5e883c2c5 100644 --- "a/lcp/LCP 52. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\346\237\223\350\211\262/README.md" +++ "b/lcp/LCP 52. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\346\237\223\350\211\262/README.md" @@ -762,6 +762,63 @@ class TreeSet { } ``` +#### Swift + +```swift +/* public class TreeNode { +* public var val: Int +* public var left: TreeNode? +* public var right: TreeNode? +* public init() { self.val = 0; self.left = nil; self.right = nil; } +* public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; } +* public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) { +* self.val = val +* self.left = left +* self.right = right +* } +* } +*/ + +class Solution { + private var treeValues: [Int] = [] + + func getNumber(_ root: TreeNode?, _ ops: [[Int]]) -> Int { + collectValues(root) + + treeValues.sort() + + var ans = 0 + for op in ops.reversed() { + let t = op[0] + let x = op[1] + let y = op[2] + var indicesToRemove: [Int] = [] + + for i in 0..= x && val <= y { + indicesToRemove.append(i) + ans += t + } + } + + for index in indicesToRemove.reversed() { + treeValues.remove(at: index) + } + } + + return ans + } + + private func collectValues(_ root: TreeNode?) { + guard let root = root else { return } + treeValues.append(root.val) + collectValues(root.left) + collectValues(root.right) + } +} +``` + diff --git "a/lcp/LCP 52. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\346\237\223\350\211\262/Solution.swift" "b/lcp/LCP 52. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\346\237\223\350\211\262/Solution.swift" new file mode 100644 index 0000000000000..8889b49daaccf --- /dev/null +++ "b/lcp/LCP 52. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\346\237\223\350\211\262/Solution.swift" @@ -0,0 +1,52 @@ +/* public class TreeNode { +* public var val: Int +* public var left: TreeNode? +* public var right: TreeNode? +* public init() { self.val = 0; self.left = nil; self.right = nil; } +* public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; } +* public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) { +* self.val = val +* self.left = left +* self.right = right +* } +* } +*/ + +class Solution { + private var treeValues: [Int] = [] + + func getNumber(_ root: TreeNode?, _ ops: [[Int]]) -> Int { + collectValues(root) + + treeValues.sort() + + var ans = 0 + for op in ops.reversed() { + let t = op[0] + let x = op[1] + let y = op[2] + var indicesToRemove: [Int] = [] + + for i in 0..= x && val <= y { + indicesToRemove.append(i) + ans += t + } + } + + for index in indicesToRemove.reversed() { + treeValues.remove(at: index) + } + } + + return ans + } + + private func collectValues(_ root: TreeNode?) { + guard let root = root else { return } + treeValues.append(root.val) + collectValues(root.left) + collectValues(root.right) + } +} \ No newline at end of file