Skip to content

New Problems #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 85 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
85 commits
Select commit Hold shift + click to select a range
427545f
Merge pull request #7 from athakur3/master
tarun1792 Aug 30, 2020
3eb2ffb
Update README.md
tarun1792 Sep 10, 2020
329614d
Create LinkList.swift
tarun1792 Nov 5, 2020
2130676
Create Merge_Two_Linked_Lists.swift
tarun1792 Nov 5, 2020
3bacd4b
Update README.md
tarun1792 Nov 5, 2020
50b3bc7
sliding window problem
tarun1792 Oct 19, 2021
a4317e2
Update README.md
tarun1792 Oct 19, 2021
ee5a02b
longest subarray sliding window
tarun1792 Oct 19, 2021
a8277fd
Update README.md
tarun1792 Oct 19, 2021
1b719f5
Update README.md
tarun1792 Sep 10, 2020
dcb14f7
Create LinkList.swift
tarun1792 Nov 5, 2020
daccc76
Create Merge_Two_Linked_Lists.swift
tarun1792 Nov 5, 2020
63ea309
Update README.md
tarun1792 Nov 5, 2020
47759cb
sliding window problem
tarun1792 Oct 19, 2021
df63faf
Update README.md
tarun1792 Oct 19, 2021
759ad6c
longest subarray sliding window
tarun1792 Oct 19, 2021
0bf1d0f
Update README.md
tarun1792 Oct 19, 2021
1a6c022
Merge branch 'athakur3-master'
tarun1792 Jan 22, 2022
fa73d7c
Merge branch 'athakur3:master' into master
tarun1792 Feb 9, 2022
0264476
Create preorder.swift
tarun1792 Mar 27, 2022
6cdaebc
Update README.md
tarun1792 Mar 27, 2022
7091195
Update preorder.swift
tarun1792 Mar 27, 2022
2980c88
Update README.md
tarun1792 Mar 27, 2022
d879f2d
Update and rename preorder.swift to TreeTreversals.swift
tarun1792 Mar 27, 2022
90b2bc5
Update README.md
tarun1792 Mar 27, 2022
9c2add8
Update TreeTreversals.swift
tarun1792 Mar 27, 2022
e6c0716
Create lowestCommonAncestor.swift
tarun1792 Mar 27, 2022
a488475
Update README.md
tarun1792 Mar 27, 2022
c4eae96
Update lowestCommonAncestor.swift
tarun1792 Mar 27, 2022
b13415b
Create Kadane'sAlgo_maxSubarray.swift
tarun1792 Mar 27, 2022
3f4fcd5
Update README.md
tarun1792 Mar 27, 2022
dbe0e1f
Create InvertBinaryTreeAlongRoot.swift
tarun1792 Mar 28, 2022
025e870
Update README.md
tarun1792 Mar 28, 2022
0ebee62
Create printFibbonaciSeries.swift
tarun1792 Mar 29, 2022
cb1c495
Update README.md
tarun1792 Mar 29, 2022
787062f
Update BinarySearch.swift
tarun1792 Apr 2, 2022
cb49892
Update BinarySearch.swift
tarun1792 Apr 2, 2022
b7a8444
Update README.md
tarun1792 Apr 2, 2022
3542910
Update README.md
tarun1792 Apr 2, 2022
fbdfa64
Merge branch 'master' of https://github.com/tarun1792/Algorithms-With…
tarun1792 Apr 2, 2022
30cb728
fubo
tarun1792 Apr 2, 2022
1e589ae
Rename Fibbonci_Series.swift.txt to Fibbonci_Series.swift
tarun1792 Apr 2, 2022
02904ee
Update README.md
tarun1792 Apr 2, 2022
a69361f
Create tribonacci.swift
tarun1792 Apr 2, 2022
45d2961
Update README.md
tarun1792 Apr 2, 2022
8e34d52
Update README.md
tarun1792 Apr 2, 2022
0121569
Update and rename Fibbonci_Series.swift to Fibbonci&Tribonacci_Series…
tarun1792 Apr 2, 2022
614f092
Update README.md
tarun1792 Apr 2, 2022
926bf9d
Update README.md
tarun1792 Apr 2, 2022
0c3edd1
Create reverseLinkedList.swift
tarun1792 Apr 15, 2022
ecef154
Update README.md
tarun1792 Apr 15, 2022
48e7d90
Create LinkedListCycle.swift
tarun1792 Apr 15, 2022
9d372ba
Update README.md
tarun1792 Apr 15, 2022
3797d0b
Create CacheLRU.swift
tarun1792 May 15, 2022
edf32d1
Update README.md
tarun1792 May 15, 2022
d92ff5e
Update README.md
tarun1792 May 15, 2022
cf71535
Create Binary_Tree_Paths.swift
tarun1792 May 17, 2022
30e6a9e
Update README.md
tarun1792 May 17, 2022
c9c0551
Create numberOfIslands.swift
tarun1792 May 17, 2022
8c11894
Update README.md
tarun1792 May 17, 2022
4a5403c
Update README.md
tarun1792 May 17, 2022
f9e9f89
Create isValidBST.swift
tarun1792 May 17, 2022
6072a87
Update README.md
tarun1792 May 17, 2022
5f0afbf
Update numberOfIslands.swift
tarun1792 May 25, 2022
3391b9e
Create Tree_BFS_DFS.swift
tarun1792 May 25, 2022
5e478cb
Update README.md
tarun1792 May 25, 2022
dc5d377
Create LevelOrderTravesalUsingBFS.swift
tarun1792 May 25, 2022
69d583e
Update README.md
tarun1792 May 25, 2022
a4df1f8
Create BurnBinaryTreeUsingGraph.swift
tarun1792 May 26, 2022
fe9d2d9
Update README.md
tarun1792 May 26, 2022
f5a6eb9
Create ReverseALinkedListInKGroup.swift
tarun1792 Jun 3, 2022
3549621
Update README.md
tarun1792 Jun 3, 2022
e8d463e
Create ContainerWithMostWater.swift
tarun1792 Jun 11, 2022
18f732a
Update README.md
tarun1792 Jun 11, 2022
53e8609
Create rightViewOfBinaryTree.swift
tarun1792 Jun 11, 2022
2374f3e
Update README.md
tarun1792 Jun 11, 2022
e5a312e
Create MinimumStairsCost.swift
tarun1792 Jun 22, 2022
0aea5b2
Update README.md
tarun1792 Jun 22, 2022
a651b03
Update MinimumStairsCost.swift
tarun1792 Jun 22, 2022
1cb384e
Update rightViewOfBinaryTree.swift
tarun1792 Sep 26, 2022
805cf7e
Update longestSubstringWithoutRepeatingCharacters.swift
tarun1792 Sep 27, 2022
d52a7f2
Create priorityQueue.swift
tarun1792 Aug 11, 2025
d33151f
Merge pull request #9 from tarun1792/tarun1792-Dijesktra
tarun1792 Aug 11, 2025
d36bb72
Update README.md
tarun1792 Aug 11, 2025
aaf8996
Merge pull request #10 from tarun1792/tarun1792-patch-1
tarun1792 Aug 11, 2025
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
44 changes: 44 additions & 0 deletions Array/ContainerWithMostWater.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

// You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]).

// Find two lines that together with the x-axis form a container, such that the container contains the most water.

// Return the maximum amount of water a container can store.

// Notice that you may not slant the container.

// Input: height = [1,8,6,2,5,4,8,3,7]
// Output: 49
// Explanation: The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.
// Example 2:

// Input: height = [1,1]
// Output: 1
// https://leetcode.com/problems/container-with-most-water/



class Solution {
func maxArea(_ height: [Int]) -> Int {
var maxArea = 0
var startPointer = 0
var endPointer = height.count - 1

while (startPointer < endPointer) {
let minHeight = min(height[startPointer],height[endPointer])
let distance = endPointer - startPointer
let area = minHeight * distance
if area > maxArea {
maxArea = area
}

if height[startPointer] < height[endPointer] {
startPointer += 1
}else{
endPointer -= 1
}
}

return maxArea
}
}
30 changes: 30 additions & 0 deletions Array/Kadane'sAlgo_maxSubarray.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
A subarray is a contiguous part of an array.

Example 1:
Input: nums = [-2,1,-3,4,-1,2,1,-5,4]
Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.

Kadanes algo
https://medium.com/@rsinghal757/kadanes-algorithm-dynamic-programming-how-and-why-does-it-work-3fd8849ed73d
*/


class Solution {
func maxSubArray(_ nums: [Int]) -> Int {
let size = nums.count
var localMax = 0
var globalMax = Int.min

for i in 0 ..< size {
localMax = max(nums[i],nums[i] + localMax)
if localMax > globalMax{
globalMax = localMax
}
}

return globalMax
}
}
40 changes: 40 additions & 0 deletions Array/LongestSubArraySlidingWindow.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// LongestSubArraySlidingWindow.swift
//
//
// Created by Tarun Kaushik on 19/10/21.
//

func findLongestSubArrayUsingSlidingWindow(array:[Int],s: Int) -> [Int] {
var window = [Int]()
var arraySum = 0
var largestArray = [Int]()

for i in array {
// update the array sum and append on window
arraySum += i
window.append(i)

// check if the arraysum become more than the required sum
if arraySum > s {
// if yes than remove the first element from the window till the array sum is greater than required sum
while arraySum > s {
let firstElement = window[0]
arraySum -= firstElement
window.removeFirst()
}
}

// check if sum is equal than update the largest array
if arraySum == s {
if largestArray.count < window.count {
largestArray = window
}
}
}

return largestArray
}

print(findLongestSubArrayUsingSlidingWindow(array: [1,2,4,3,0,0,0,0,10], s: 10))

126 changes: 126 additions & 0 deletions DataStructure/LinkedList/CacheLRU.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
typealias LinkedListNode<T> = DoublyLinkedList<T>.Node<T>

final class DoublyLinkedList<T>{

final class Node<T> {
var payload: T
var previous: Node<T>?
var next: Node<T>?

init(payload: T){
self.payload = payload
}
}

private(set) var count:Int = 0
private(set) var head: Node<T>?
private(set) var tail: Node<T>?

func addToHead(_ payload: T) -> Node<T>{
let node = Node(payload: payload)
defer{
head = node
count += 1
}

guard let head = head else {
tail = node
return node
}

head.previous = node

node.next = head
node.previous = nil

return node
}

func moveToHead(node: Node<T>?) {
guard node !== head else {return}
let next = node?.next
let previous = node?.previous

previous?.next = next
next?.previous = previous

node?.previous = nil
node?.next = head

if tail === node {
tail = previous
}

self.head = node
}

func removeLast() -> Node<T>?{
guard let tail = tail else {return nil}

let previous = tail.previous
previous?.next = nil
self.tail = previous

if count == 1{
head = nil
}

count -= 1

return tail
}
}

final class CacheLRU<Key: Hashable,Value>{

private struct CachePayload{
let key: Key
let value: Value
}

private let list = DoublyLinkedList<CachePayload>()
private var nodeDict = [Key: LinkedListNode<CachePayload>]()

private let capacity: Int

init(capacity: Int){
self.capacity = capacity
}

func setValue(_ value: Value,for key: Key) {
let payload = CachePayload(key:key,value:value)
let node = LinkedListNode(payload:payload)

if let existingNode = nodeDict[key] {
existingNode.payload = payload
list.moveToHead(node:existingNode)
}else{
list.addToHead(payload)
nodeDict[key] = node
}

if list.count > capacity{
let removedNode = list.removeLast()
if let key = removedNode?.payload.key{
nodeDict[key] = nil
}
}
}

func getValue(for key: Key) -> Value?{
guard let node = nodeDict[key] else {return nil}
list.moveToHead(node:node)
return node.payload.value
}
}

let cache = CacheLRU<Int, String>(capacity: 2)

cache.getValue(for: 5) // nil

cache.setValue("One", for: 1)
cache.setValue("Eleven", for: 11)
cache.setValue("Twenty", for: 20)

cache.getValue(for: 1) // nil. We exceeded the capacity with the previous `setValue` and `1` was the last element.
cache.getValue(for: 11) // Eleven
136 changes: 136 additions & 0 deletions DataStructure/LinkedList/LinkList.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
class LinkedListNode<T> {
var value: T
weak var previous: LinkedListNode?
var next: LinkedListNode?

init(value: T) {
self.value = value
}
}


class LinkList<T> {
typealias Node = LinkedListNode<T>

var head: Node?
var tail: Node? {
if last === head {
return nil
}
return last
}

var isEmpty: Bool {
return head == nil
}

var first: Node? {
return head
}

var last: Node? {
guard var node = head else {return nil}

while let next = node.next {
node = next
}

return node
}

var count: Int {
guard var node = head else { return 0}

var count = 1

while let next = node.next{
node = next
count += 1
}

return count
}

func print() {
guard var node = head else {Swift.print("empty"); return}
var str = "Head(\(node.value))"

while let next = node.next {
node = next
str += next.next == nil ? " -> Tail(\(next.value))" : " -> (\(next.value))"
}

Swift.print(str)
}

func append(value : T) {
let newNode = Node(value: value)

if let lastNode = last {
newNode.previous = lastNode
lastNode.next = newNode
} else {
head = newNode
}
}

func reverse() {
var node = head

while let currentNode = node {
node = currentNode.next
// swap the previous and next references
let previous = currentNode.previous
currentNode.previous = node
currentNode.next = previous
head = currentNode
}
}

func insert(atIndex index: Int, value: T) {
let newNode = Node(value: value)

if index == 0 {
newNode.next = head
head?.previous = newNode
head = newNode
} else {
let prev = self.node(atIndex: index - 1 )
let next = prev.next

newNode.previous = prev
newNode.next = next
prev.next = newNode
next?.previous = newNode
}
}

func node(atIndex index: Int) -> Node {
if index == 0 {
return head!
} else {
var node = head!.next

for _ in 1 ..< index {
node = node?.next
if node == nil {
break
}
}

return node!
}
}
}

let list = LinkList<String>()
list.isEmpty
list.first
list.append(value: "Test1")
list.append(value: "Test2")
list.append(value: "Test3")
list.last?.value
list.count
//list.insert(atIndex: 1, value: "Func")
//list.node(atIndex: 2)
//list.print()
Loading