Skip to content

Commit 60e1c7a

Browse files
committed
Day 28, 29
1 parent 23bbc16 commit 60e1c7a

File tree

2 files changed

+175
-0
lines changed

2 files changed

+175
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.kishan.scala.leetcode.juneChallenges
2+
3+
import java.util
4+
5+
import scala.collection.JavaConverters._
6+
import scala.collection.mutable
7+
8+
/*
9+
* Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], reconstruct the itinerary in order.
10+
* All of the tickets belong to a man who departs from JFK. Thus, the itinerary must begin with JFK.
11+
*
12+
* Note:
13+
1. If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string.
14+
For example, the itinerary ["JFK", "LGA"] has a smaller lexical order than ["JFK", "LGB"].
15+
2. All airports are represented by three capital letters (IATA code).
16+
3. You may assume all tickets form at least one valid itinerary.
17+
4. One must use all the tickets once and only once.
18+
*
19+
* */
20+
21+
/*
22+
* Input: [["MUC", "LHR"], ["JFK", "MUC"], ["SFO", "SJC"], ["LHR", "SFO"]]
23+
Output: ["JFK", "MUC", "LHR", "SFO", "SJC"]
24+
*
25+
*
26+
* Input: [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
27+
Output: ["JFK","ATL","JFK","SFO","ATL","SFO"]
28+
Explanation: Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"].
29+
But it is larger in lexical order.
30+
*
31+
* */
32+
33+
/*
34+
* Approach: DFS - Depth First Search
35+
*
36+
* 1. We have to build HashMap with Source as Key and Queue of Destination - By Implementing Priority Queue as value.
37+
* 2. We have to Start from JFK and find the city which doesn't have any more destination and add to linked list at head position.
38+
* 3. We have to recursively go through each Priority Queue and have to add each destination at the head of Linked List
39+
* 4. Finally convert LinkedList asScala List and return the result.
40+
*
41+
* */
42+
43+
object ReconstructItinerary {
44+
private var map: mutable.HashMap[String, mutable.PriorityQueue[String]] = mutable.HashMap()
45+
private var result: util.LinkedList[String] = new util.LinkedList()
46+
47+
def findItinerary(tickets: List[List[String]]): List[String] = {
48+
tickets.foreach(a => {
49+
if (!map.contains(a.head)) {
50+
var pq: mutable.PriorityQueue[String] = mutable.PriorityQueue.empty[String](Ordering.String.reverse)
51+
map += (a.head -> pq)
52+
}
53+
map(a.head).enqueue(a.last)
54+
})
55+
56+
dfs("JFK")
57+
return result.asScala.toList
58+
}
59+
60+
def dfs(startCity: String): Unit = {
61+
var arrivals = map.getOrElse(startCity, null)
62+
while (arrivals != null && arrivals.nonEmpty) {
63+
dfs(arrivals.dequeue())
64+
}
65+
result.add(0, startCity)
66+
}
67+
68+
def main(args: Array[String]): Unit = {
69+
val input1: List[List[String]] = List(List("MUC", "LHR"), List("JFK", "MUC"), List("SFO", "SJC"), List("LHR", "SFO"))
70+
val input2: List[List[String]] = List(List("JFK", "SFO"), List("JFK", "ATL"), List("SFO", "ATL"), List("ATL", "JFK"), List("ATL", "SFO"))
71+
val input3: List[List[String]] = List(List("JFK", "KUL"), List("JFK", "NRT"), List("NRT", "JFK"))
72+
val input4: List[List[String]] = List(List("EZE", "AXA"), List("TIA", "ANU"), List("ANU", "JFK"), List("JFK", "ANU"), List("ANU", "EZE"), List("TIA", "ANU"), List("AXA", "TIA"), List("TIA", "JFK"), List("ANU", "TIA"), List("JFK", "TIA"))
73+
val input5: List[List[String]] = List(List("EZE", "TIA"), List("EZE", "HBA"), List("AXA", "TIA"), List("JFK", "AXA"), List("ANU", "JFK"), List("ADL", "ANU"), List("TIA", "AUA"), List("ANU", "AUA"), List("ADL", "EZE"), List("ADL", "EZE"), List("EZE", "ADL"), List("AXA", "EZE"), List("AUA", "AXA"), List("JFK", "AXA"), List("AXA", "AUA"), List("AUA", "ADL"), List("ANU", "EZE"), List("TIA", "ADL"), List("EZE", "ANU"), List("AUA", "ANU"))
74+
val result = findItinerary(input5)
75+
println(s"Result: ${result.mkString(",")}, ${result.mkString(",") == "JFK,AXA,AUA,ADL,ANU,AUA,ANU,EZE,ADL,EZE,ANU,JFK,AXA,EZE,TIA,AUA,AXA,TIA,ADL,EZE,HBA"}")
76+
}
77+
78+
// ["JFK","ANU","EZE","AXA","TIA","ANU","JFK","TIA","ANU","TIA","JFK"]
79+
// ["JFK","AXA","AUA","ADL","ANU","AUA","ANU","EZE","ADL","EZE","ANU","JFK","AXA","EZE","TIA","AUA","AXA","TIA","ADL","EZE","HBA"]
80+
81+
82+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package com.kishan.scala.leetcode.juneChallenges
2+
3+
/*
4+
* A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).
5+
The robot can only move either down or right at any point in time.
6+
The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
7+
How many possible unique paths are there?
8+
*
9+
* */
10+
11+
/*
12+
* Input: m = 3, n = 2
13+
Output: 3
14+
Explanation:
15+
From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
16+
1. Right -> Right -> Down
17+
2. Right -> Down -> Right
18+
3. Down -> Right -> Right
19+
*
20+
* Input: m = 7, n = 3
21+
Output: 28
22+
*
23+
* Constraints:
24+
1. 1 <= m, n <= 100
25+
2. It's guaranteed that the answer will be less than or equal to 2 * 10 ^ 9.
26+
*
27+
* */
28+
29+
/*
30+
* Approach: Dynamic Programming
31+
*
32+
* 1. For i = 0 or j = 0. Maximum possible steps will be 1.
33+
* 2. For i >= 1 and j >= 1, we can reach from top and we can also reach from left. So it will be sum of top + left cell value.
34+
*
35+
* m = 3
36+
* n = 7
37+
*
38+
* Iteration:
39+
*
40+
* 0 -> 1 1 1 1 1 1 1
41+
* 1 -> 1 2 3 4 5 6 7
42+
* 2 -> 1 3 6 10 15 21 28
43+
*
44+
* return the last element of array which will be result
45+
* */
46+
47+
48+
49+
50+
51+
52+
object UniquePaths {
53+
54+
//Using Two Dimensional Array
55+
def uniquePathsTwoDimensional(m: Int, n: Int): Int = {
56+
var dp = Array.ofDim[Int](m, n)
57+
for (i <- dp.indices) {
58+
for (j <- dp(i).indices) {
59+
if (i == 0 || j == 0) {
60+
dp(i)(j) = 1
61+
}
62+
if (i >= 1 && j >= 1) {
63+
dp(i)(j) = dp(i - 1)(j) + dp(i)(j - 1)
64+
}
65+
}
66+
}
67+
return dp(m - 1)(n - 1)
68+
}
69+
70+
def main(args: Array[String]): Unit = {
71+
val m = 3
72+
val n = 7
73+
val result = uniquePaths(m, n)
74+
println(s"Result: ${result}, ${result == 28}")
75+
76+
val result1 = uniquePaths(3, 2)
77+
println(s"Result: ${result1}, ${result1 == 3}")
78+
79+
}
80+
81+
//Using One Dimensional Array
82+
def uniquePaths(m: Int, n: Int): Int = {
83+
var dp = Array.ofDim[Int](n)
84+
for (i <- 0 until m) {
85+
var j = 0
86+
while (j < n) {
87+
if (i == 0 || j == 0) dp(j) = 1 else if (i >= 1) dp(j) += dp(j - 1)
88+
j += 1
89+
}
90+
}
91+
return dp(n - 1)
92+
}
93+
}

0 commit comments

Comments
 (0)