Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions MinimumCostsForTickets.swift
Original file line number Diff line number Diff line change
@@ -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..<days.count {
//days ...
var minCost = Int.max
for (index, passDay) in passes.enumerated() {
var j = i
while (j >= 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]
}
}
106 changes: 106 additions & 0 deletions WordLadder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
//
// WordLadder.swift
// DSA-Practice
//
// Created by Paridhi Malviya on 2/22/26.
//

class WordLadder {

class QueueUsingLinkedList<T> {

private class LinkedListNode<T> {
var value: T
var next: LinkedListNode<T>?
init(value: T) {
self.value = value
}
}

private var front: LinkedListNode<T>?
private var rear: LinkedListNode<T>?
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<String>()
queue.enqueue(value: beginWord)
var level = 1
var visitedStrSet = Set<String>()
visitedStrSet.insert(beginWord)

while (!queue.isEmpty) {
let size = queue.size
for s in 0..<size {
let currStr = queue.dequeue()

if let currStr = currStr {
var currArray: [Character] = Array(currStr)
for i in 0..<currArray.count {
var intermediateCurrArray = currArray
for char in aToZ {
intermediateCurrArray[i] = char
let currChangedStr = String(intermediateCurrArray)
if (wordList.contains(currChangedStr)) {
if (currChangedStr == endWord) {
return level + 1
}
if (!visitedStrSet.contains(currChangedStr)) {
queue.enqueue(value: currChangedStr)
visitedStrSet.insert(currChangedStr)
}
}
}
}
}
}
level += 1
}
return 0

}
}