From 5d93878da5090116d4d94672043433cc53e2e1e2 Mon Sep 17 00:00:00 2001 From: Anton Dryakhlykh Date: Tue, 25 Apr 2023 20:02:11 +0300 Subject: [PATCH 1/5] Create 0036-valid-sudoku.swift --- swift/0036-valid-sudoku.swift | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 swift/0036-valid-sudoku.swift diff --git a/swift/0036-valid-sudoku.swift b/swift/0036-valid-sudoku.swift new file mode 100644 index 000000000..96468ab74 --- /dev/null +++ b/swift/0036-valid-sudoku.swift @@ -0,0 +1,32 @@ +/** + * Question Link: https://leetcode.com/problems/valid-sudoku/ + */ + +class ValidSudoku { + func isValidSudoku(_ board: [[Character]]) -> Bool { + var rows = [Set](repeating: .init(), count: 9) + var columns = [Set](repeating: .init(), count: 9) + var squares = [[Int]: Set]() + + for i in 0..()].insert(board[i][j]) + } + } + return true + } +} \ No newline at end of file From ab5308193a089dce16c04d44e3942d9543485264 Mon Sep 17 00:00:00 2001 From: Anton Dryakhlykh Date: Sat, 13 May 2023 22:30:06 +0300 Subject: [PATCH 2/5] Create 0853-car-fleet.swift --- swift/0853-car-fleet.swift | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 swift/0853-car-fleet.swift diff --git a/swift/0853-car-fleet.swift b/swift/0853-car-fleet.swift new file mode 100644 index 000000000..d59fdd7aa --- /dev/null +++ b/swift/0853-car-fleet.swift @@ -0,0 +1,26 @@ +/** + * Question Link: https://leetcode.com/problems/car-fleet/ + */ + +class Solution { + func carFleet(_ target: Int, _ position: [Int], _ speed: [Int]) -> Int { + var stack = [Double]() + + let pair = zip(position, speed) + .map { ($0, $1) } + .sorted { $0.0 > $1.0 } + + for (p, s) in pair { + stack.append(Double(target - p) / Double(s)) + + guard stack.count >= 2 else { continue } + + let suffix = stack.suffix(2) + if suffix.last! <= suffix.first! { + stack.removeLast() + } + } + + return stack.count + } +} \ No newline at end of file From c9d498d5cd2da9f28b43664790e9d0da954a8600 Mon Sep 17 00:00:00 2001 From: Anton Dryakhlykh <15323858+drxlx@users.noreply.github.com> Date: Sat, 13 May 2023 22:35:58 +0300 Subject: [PATCH 3/5] Delete 0853-car-fleet.swift --- swift/0853-car-fleet.swift | 26 -------------------------- 1 file changed, 26 deletions(-) delete mode 100644 swift/0853-car-fleet.swift diff --git a/swift/0853-car-fleet.swift b/swift/0853-car-fleet.swift deleted file mode 100644 index d59fdd7aa..000000000 --- a/swift/0853-car-fleet.swift +++ /dev/null @@ -1,26 +0,0 @@ -/** - * Question Link: https://leetcode.com/problems/car-fleet/ - */ - -class Solution { - func carFleet(_ target: Int, _ position: [Int], _ speed: [Int]) -> Int { - var stack = [Double]() - - let pair = zip(position, speed) - .map { ($0, $1) } - .sorted { $0.0 > $1.0 } - - for (p, s) in pair { - stack.append(Double(target - p) / Double(s)) - - guard stack.count >= 2 else { continue } - - let suffix = stack.suffix(2) - if suffix.last! <= suffix.first! { - stack.removeLast() - } - } - - return stack.count - } -} \ No newline at end of file From eb9e5ff988f53ee5f109f8d326c40b2b850af2da Mon Sep 17 00:00:00 2001 From: Anton Dryakhlykh Date: Sun, 3 Sep 2023 11:21:51 +0300 Subject: [PATCH 4/5] Create 0025-reverse-nodes-in-k-group --- swift/0025-reverse-nodes-in-k-group.swift | 57 +++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 swift/0025-reverse-nodes-in-k-group.swift diff --git a/swift/0025-reverse-nodes-in-k-group.swift b/swift/0025-reverse-nodes-in-k-group.swift new file mode 100644 index 000000000..75323f65b --- /dev/null +++ b/swift/0025-reverse-nodes-in-k-group.swift @@ -0,0 +1,57 @@ +/** + * Question Link: https://leetcode.com/problems/reverse-nodes-in-k-group/ + */ + +/** + * Definition for singly-linked list. + * public class ListNode { + * public var val: Int + * public var next: ListNode? + * public init() { self.val = 0; self.next = nil; } + * public init(_ val: Int) { self.val = val; self.next = nil; } + * public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; } + * } + */ + +class ReverseKGroup { + func reverseKGroup(_ head: ListNode?, _ k: Int) -> ListNode? { + var dummy = ListNode(0, head) + var groupPrev: ListNode? = dummy + + while true { + let kth = getKth(current: groupPrev, k: k) + if kth == nil { + break + } + + let groupNext = kth?.next + + var prev = kth?.next + var current = groupPrev?.next + while current !== groupNext { + let temp = current?.next + current?.next = prev + prev = current + current = temp + } + + let temp = groupPrev?.next + groupPrev?.next = kth + groupPrev = temp + } + + return dummy.next + } + + func getKth(current: ListNode?, k: Int) -> ListNode? { + var current = current + var k = k + + while current != nil && k > 0 { + current = current?.next + k -= 1 + } + + return current + } +} \ No newline at end of file From 3bcc17da799c6084b4264bdec6f91ca559707ca2 Mon Sep 17 00:00:00 2001 From: Anton Dryakhlykh <15323858+drxlx@users.noreply.github.com> Date: Sun, 3 Sep 2023 11:23:42 +0300 Subject: [PATCH 5/5] Delete swift/0036-valid-sudoku.swift --- swift/0036-valid-sudoku.swift | 32 -------------------------------- 1 file changed, 32 deletions(-) delete mode 100644 swift/0036-valid-sudoku.swift diff --git a/swift/0036-valid-sudoku.swift b/swift/0036-valid-sudoku.swift deleted file mode 100644 index 96468ab74..000000000 --- a/swift/0036-valid-sudoku.swift +++ /dev/null @@ -1,32 +0,0 @@ -/** - * Question Link: https://leetcode.com/problems/valid-sudoku/ - */ - -class ValidSudoku { - func isValidSudoku(_ board: [[Character]]) -> Bool { - var rows = [Set](repeating: .init(), count: 9) - var columns = [Set](repeating: .init(), count: 9) - var squares = [[Int]: Set]() - - for i in 0..()].insert(board[i][j]) - } - } - return true - } -} \ No newline at end of file