diff --git a/MinimumCostsForTickets.swift b/MinimumCostsForTickets.swift new file mode 100644 index 0000000..747e723 --- /dev/null +++ b/MinimumCostsForTickets.swift @@ -0,0 +1,95 @@ +// +// MinimumCostsForTickets.swift +// DSA-Practice +// +// Created by Paridhi Malviya on 2/23/26. +// + +class MinimumCostsForTickets { + + init() { + // let minCost = mincostTickets([1,4,6,7,8,20], [2,7,15]) + // print("minCost \(minCost)") + + let minCost = mincostTicketsUsingRecursion([1,4,6,7,8,20], [2,7,15]) + print("minCost \(minCost)") + + } + + func mincostTicketsUsingRecursion(_ days: [Int], _ costs: [Int]) -> Int { + var memo = Array(repeating: -1, count: days.count) + return helperUsingRecursion(days, costs, passes: [1, 7, 30], pivot: days.count - 1, memo: &memo) + } + + func helperUsingRecursion(_ days: [Int], _ costs: [Int], passes: [Int], pivot: Int, memo: inout [Int]) -> Int { + //base + if (pivot < 0) { + return 0 + } + if (memo[pivot] != -1) { + return memo[pivot] + } + //logic + for i in stride(from: pivot, through: 0, by: -1) { + + //loop through all passes. check boundary conditions, take the cost. costs[passDay] + cost for j - 1 index in day + //loop through the typesofpassea. + + for (index, passDay) in passes.enumerated() { + //1, 7 30 + var j = i + + while (j >= 0 && days[i] - days[j] + 1 <= passDay) { + j -= 1 + } + var costForPassDay = costs[index] + if (j >= 0) { + costForPassDay = costs[index] + helperUsingRecursion(days, costs, passes: passes, pivot: j, memo: &memo) + } + if (memo[i] < 0) { + memo[i] = costForPassDay + } else { + memo[i] = min(memo[i], costForPassDay) + } + } + } + return memo[pivot] + } + + + + //MARK: Tabulation + func mincostTickets(_ days: [Int], _ costs: [Int]) -> Int { + + return helper(days, costs, passes: [1, 7, 30]) + } + + func helper(_ days: [Int], _ costs: [Int], passes: [Int]) -> Int { + + var dp = Array(repeating: 0, count: days.count) + var minPass = Int.max + for cost in costs { + minPass = min(minPass, cost) + } + dp[0] = minPass + for i in 1..= 0 && days[i] - days[j] + 1 <= passDay) { + j -= 1 + } + + var costForThisPass = costs[index] + if (j >= 0) { + costForThisPass = costForThisPass + dp[j] + } + minCost = min(minCost, costForThisPass) + } + + dp[i] = minCost + } + return dp[days.count - 1] + } +} diff --git a/WordLadder.swift b/WordLadder.swift new file mode 100644 index 0000000..dc182b8 --- /dev/null +++ b/WordLadder.swift @@ -0,0 +1,106 @@ +// +// WordLadder.swift +// DSA-Practice +// +// Created by Paridhi Malviya on 2/22/26. +// + +class WordLadder { + + class QueueUsingLinkedList { + + private class LinkedListNode { + var value: T + var next: LinkedListNode? + init(value: T) { + self.value = value + } + } + + private var front: LinkedListNode? + private var rear: LinkedListNode? + private var count = 0 + + func enqueue(value: T) { + let newNode = LinkedListNode(value: value) + if (front == nil) { + front = newNode + rear = newNode + } else { + rear?.next = newNode + rear = newNode + } + count += 1 + } + + func dequeue() -> T? { + if (front == nil) { + return nil + } + let value = front?.value + front = front?.next + if (front == nil) { + rear = nil + } + count -= 1 + return value + } + + var isEmpty: Bool { + return count == 0 + } + + var size: Int { + return count + } + } + + init() { + let length = ladderLength("hit", "cog", ["hot","dot","dog","lot","log","cog"]) + print("length \(length)") + } + + func ladderLength(_ beginWord: String, _ endWord: String, _ wordList: [String]) -> Int { + + if (beginWord == endWord) { + return 0 + } + let aToZ = "abcdefghijklmnopqrstuvwxyz" + + var queue = QueueUsingLinkedList() + queue.enqueue(value: beginWord) + var level = 1 + var visitedStrSet = Set() + visitedStrSet.insert(beginWord) + + while (!queue.isEmpty) { + let size = queue.size + for s in 0..