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
61 changes: 61 additions & 0 deletions DiagonalTraversal.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//
// Untitled.swift
// DSA-Practice
//
// Created by Paridhi Malviya on 1/6/26.
//

class DiagonalTraversal {


init() {
let input: [[Int]] = [[1,2,3], [4,5,6], [7,8,9]]
let output = diagonalTraversal(input)
print("output \(output)")
}

func diagonalTraversal(_ nums: [[Int]]) -> [Int] {
let m = nums.count
let n = nums[0].count
var result = Array(repeating: 0, count: nums.count * nums[0].count)

var r = 0
var c = 0
var flag = true //upward direction
for i in 0..<result.count {
result[i] = nums[r][c]

//get next r,c
if (flag) {
//upward direction
if (c == n - 1) {
//the right most
flag = false
r += 1
} else if (r == 0) {
//roof but not right
//flip the direction
flag = false
c += 1
} else {
r -= 1
c += 1
}
} else {
//downward direction
if (r == m - 1) {
flag = true
c += 1
} else if (c == 0) {
flag = true
r += 1
} else {
c -= 1
r += 1
}
}
}

return result
}
}
70 changes: 70 additions & 0 deletions ProductOfArrayExceptSelf.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//
// ProductOfArrayExceptSelf.swift
// DSA-Practice
//
// Created by Paridhi Malviya on 1/5/26.
//

class productOfArrayExceptSelf {

init() {
let productExceptSelfAtEachIndex = productExceptSelf(nums: [1, 2, 3, 4, 5])
print("product ** \(productExceptSelfAtEachIndex)")

let productExceptSelf = productExceptSelfBySavingSpace(nums: [1, 2, 3, 4, 5])
print("product except self \(productExceptSelf)")
}

/*
time compelxity - O(n)
space complexity - O(3n) = O(n)
*/
func productExceptSelf(nums: [Int]) -> [Int] {
//forward pass left to right
var leftToRightProduct = Array(repeating: 0, count: nums.count)
leftToRightProduct[0] = 1
for index in 1..<nums.count {
leftToRightProduct[index] = leftToRightProduct[index - 1] * nums[index - 1]
}

//forward pass right to left.
var rightToLeftProduct = Array(repeating: 0, count: nums.count)
rightToLeftProduct[nums.count - 1] = 1
//use stride to loop from back of the array to front
for index in stride(from: (nums.count - 2), through: 0, by: -1) {
rightToLeftProduct[index] = rightToLeftProduct[index + 1] * nums[index + 1]
}

var finalResultArray = Array(repeating: 0, count: nums.count)
for index in 0..<nums.count {
finalResultArray[index] = leftToRightProduct[index] * rightToLeftProduct[index]
}

return finalResultArray
}

//save space
/*
space complexity = O(1) - because input output space are not considered in auxilliary space.
*/
func productExceptSelfBySavingSpace(nums: [Int]) -> [Int] {
var rp = 1
var result = Array(repeating: 0, count: nums.count)
result[0] = 1 //
//forward pass left to right
for index in 1..<nums.count {
rp = rp * nums[index - 1]
result[index] = rp
}

//forward pass right to left.
rp = 1 //reset running product

//loop from back of the array to front
for index in stride(from: (nums.count - 2), through: 0, by: -1) {
rp = rp * nums[index + 1]
result[index] = result[index] * rp
}
return result
}
}
136 changes: 136 additions & 0 deletions SpiralMatrix.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
//
// SpiralMatrix.swift
// DSA-Practice
//
// Created by Paridhi Malviya on 1/6/26.
//

class SpiralMatrix {

init() {
let result = spiralOrder()
print("result \(result)")

spiralMatrixUsingRecursion()
}
/*
It's a recursive pattern.
When left & right or top & bottom crosses each other, we are done.
after each iteration, to print the inner spral - (left + 1, right - 1, bottom - 1, top + 1)
initially - left = 0, right - n-1, bottom- (m-1)
j -left to right - top row done.
shrink the boundary -> increment top
go on the right most boundary from top to bottom
shrink the boundary -> right = right - 1
go on the bottom row from right to left
shrink bottom - bottom - 1
go on left from bottom to top.
once left column is done, increase the left boundary. left = left + 1
If a single row at the innermost layer. then top and bottom is equal. left and right are different.
If any pair crossed, we would stop. If they are equal, we need to print it.
*/
/*
time complexity - O(mn)
space complexity - no extra space
*/
func spiralOrder() -> [Int] {
let input = [[1,2,3,4,5], [6,7,8,9,10], [11, 12, 13, 14, 15], [16, 17, 18, 19,20]]
var result = [Int]()
let m = input.count
let n = input[0].count
var top = 0
var bottom = m - 1
var left = 0
var right = n - 1
//when both pairs didn't cross each other.
while (top <= bottom && left <= right) {
//iterate on top row. for j = left to right
for j in left...right {
result.append(input[top][j])
}
//once top row is done. Ijncrease the top pointer
top += 1

//If the variables of while loop is mutating (if base is changing), check the base case again
if (top <= bottom && left <= right) {
//right column
for i in top...bottom {
result.append(input[i][right])
}
right -= 1
}

if (top <= bottom) {
//bottom row
for j in stride(from: right, through: left, by: -1) {
result.append(input[bottom][j])
}
bottom -= 1
}

if (left <= right) {
//left column
for i in stride(from: bottom, through: top, by: -1) {
result.append(input[i][left])
}
left += 1
}
}
return result
}

//Recursion stack space - no of spirals
func spiralMatrixUsingRecursion() {
let input = [[1,2,3,4,5], [6,7,8,9,10], [11, 12, 13, 14, 15], [16, 17, 18, 19,20]]
var top = 0
var bottom = input.count - 1
var left = 0
var right = input[0].count - 1
var result = [Int]()
helperForSpiralOrder(input: input, left: left, right: right, top: top, bottom: bottom, result: &result)
print("result : \(result)")
}

func helperForSpiralOrder(input: [[Int]], left: Int, right: Int, top: Int, bottom: Int, result: inout [Int]) {
var top = top
var bottom = bottom
var left = left
var right = right

if (top > bottom || left > right) {
return
}
//iterate on top row. for j = left to right
for j in left...right {
result.append(input[top][j])
}
//once top row is done. Increase the top pointer
top += 1

//right column
for i in top...bottom {
result.append(input[i][right])
}
right -= 1

//If the variables of while loop is mutating (if base case variable is changing), check the base case again

if (top <= bottom) {
//bottom row
for j in stride(from: right, through: left, by: -1) {
result.append(input[bottom][j])
}
bottom -= 1
}

if (left <= right) {
//left column
for i in stride(from: bottom, through: top, by: -1) {
result.append(input[i][left])
}
left += 1
}

helperForSpiralOrder(input: input, left: left, right: right, top: top, bottom: bottom, result: &result)
}
}