SlideShare a Scribd company logo
1 of 33
Download to read offline
Week 5
• Cheapest Flights within k stops
• Merge K sorted Lists
• Race Car
Cheapest Flights within k stops
• There are n cities connected by m flights. Each
flight starts from city u and arrives at v with a
price w.
• Now given all the cities and flights, together
with starting city src and the destination dst,
your task is to find the cheapest price
from src to dst with up to k stops. If there is no
such route, output -1.
Example:
The cheapest price from city 0 to city 2 with at most 1 stop costs 200, as
marked red in the picture.
DFS + Pruning
Example
Example
// Adjacency Matrix
// Calling DFS through solve function
// Graph Generation
// Visited Array
// Keep track of the minimum cost
// Cost Matrix
Merge K sorted Lists
• You are given an array of k linked-lists lists,
each linked-list is sorted in ascending order.
• Merge all the linked-lists into one sorted
linked-list and return it.
10 20 22 25 32 54 64
5 7 11 19
4 6 8 15 17
Priority Queue:10,5, 4
Dummy: {4,
10 20 22 25 32 54 64
5 7 11 19
4 6 8 15 17
Priority Queue: 10,5, 6
4, 5,
10 20 22 25 32 54 64
5 7 11 19
4 6 8 15 17
Priority Queue: 10,6,7
4, 5, 6,
10 20 22 25 32 54 64
5 7 11 19
4 6 8 15 17
Priority Queue: 10,7,8
4, 5, 6, 7,
10 20 22 25 32 54 64
5 7 11 19
4 6 8 15 17
Priority Queue: 10, 8, 11
4, 5, 6, 7, 8,
10 20 22 25 32 54 64
5 7 11 19
4 6 8 15 17
Priority Queue: 10, 11,15
4, 5, 6, 7, 8, 10
10 20 22 25 32 54 64
5 7 11 19
4 6 8 15 17
Priority Queue: 11,15, 20
4, 5, 6, 7, 8, 10,11,
10 20 22 25 32 54 64
5 7 11 19
4 6 8 15 17
Priority Queue: 15, 20,19
4, 5, 6, 7, 8, 10,11, 15,
10 20 22 25 32 54 64
5 7 11 19
4 6 8 15 17
Priority Queue: 20,19, 17
4, 5, 6, 7, 8, 10,11, 15, 17
10 20 22 25 32 54 64
5 7 11 19
4 6 8 15 17
Priority Queue: 20, 22
4, 5, 6, 7, 8, 10,11, 15, 17, 19, 20
10 20 22 25 32 54 64
5 7 11 19
4 6 8 15 17
Priority Queue: 22,25
4, 5, 6, 7, 8, 10,11, 15, 17, 19, 20,22
10 20 22 25 32 54 64
5 7 11 19
4 6 8 15 17
Priority Queue: 25,32
4, 5, 6, 7, 8, 10,11, 15, 17, 19, 20,22
10 20 22 25 32 54 64
5 7 11 19
4 6 8 15 17
Priority Queue: 32, 54
4, 5, 6, 7, 8, 10,11, 15, 17, 19, 20,22,25,32,
10 20 22 25 32 54 64
5 7 11 19
4 6 8 15 17
Priority Queue: 64
4, 5, 6, 7, 8, 10,11, 15, 17, 19, 20,22,25,32,54
10 20 22 25 32 54 64
5 7 11 19
4 6 8 15 17
Priority Queue: --
4, 5, 6, 7, 8, 10,11, 15, 17, 19, 20,22,25,32,54,64
Algorithm:
• 10,20,30,40 ---L0
• 5,9,12,18,32 ---L1
• 11,15,17 ---L2
1-0-5
0-0-10 0-0-10
1-0-5 1-1-9
2-0-11 2-0-11
5,
Interface
CompareTo()
Priority Queue
CompareTo(P1,P2)=
C.O1=C(P1)
C.O2=C(P2)
If(O1.CompareTo(O2)<0)
+, This >
-, This <
0, Both =
Race Car
• Your car starts at position 0 and speed +1 on an infinite number
line. (Your car can go into negative positions.)
• Your car drives automatically according to a sequence of
instructions A (accelerate) and R (reverse).
• When you get an instruction "A", your car does the
following: position += speed, speed *= 2.
• When you get an instruction "R", your car does the following: if
your speed is positive then speed = -1 , otherwise speed = 1. (Your
position stays the same.)
• For example, after commands "AAR", your car goes to positions 0-
>1->3->3, and your speed goes to 1->2->4->-1.
• Now for some target position, say the length of the shortest
sequence of instructions to get there.
Solution
• T=6 // Even // Target = Target/2
• Step 1 = 6/2 = 3 (Temp_T)
// Odd Taregt = Target+1
• Step 2 = 3+ 1= 4
• Step 3= 4/2 = 2
• Step 4 = 2/2=1
• Step 5 = 0
0,1,2,4,3,6
AAARA
• T =15
• Step 1 = 15 + 1 =16
• Step 2: 16/2 = 8
• Step 3 : 8/2= 4
• Step 4: 4/2= 2
• Step 5: 2/2 =1
• Step 6: 0
0,1,2,4,8,16,15
A,A,A,A,A,R
Thank You

More Related Content

Similar to Leetcode Problem Solution

Knowledge Area By Project Phase Excel Array Formulas
Knowledge Area By Project Phase   Excel Array FormulasKnowledge Area By Project Phase   Excel Array Formulas
Knowledge Area By Project Phase Excel Array FormulasForrest Kovach, MBA, PMP
 
Exercise Problems for Chapter 5Numerical example on page 203Pe.docx
Exercise Problems for Chapter 5Numerical example on page 203Pe.docxExercise Problems for Chapter 5Numerical example on page 203Pe.docx
Exercise Problems for Chapter 5Numerical example on page 203Pe.docxgitagrimston
 
Irregular sequence counter
Irregular sequence counterIrregular sequence counter
Irregular sequence counterAbdullah Shiam
 
Informs_Xiaonan_03Oct2013
Informs_Xiaonan_03Oct2013Informs_Xiaonan_03Oct2013
Informs_Xiaonan_03Oct2013Xiaonan Zhou
 
R Programming: Transform/Reshape Data In R
R Programming: Transform/Reshape Data In RR Programming: Transform/Reshape Data In R
R Programming: Transform/Reshape Data In RRsquared Academy
 
Analysis and design of 15 storey office and
Analysis and design of 15 storey office andAnalysis and design of 15 storey office and
Analysis and design of 15 storey office andMasroor Alam
 
Analysis and design of 15 storey office and
Analysis and design of 15 storey office andAnalysis and design of 15 storey office and
Analysis and design of 15 storey office andMasroor Alam
 
Linear programming.pptx
Linear programming.pptxLinear programming.pptx
Linear programming.pptxPrabin Pandit
 
Applied Regression Analysis using R
Applied Regression Analysis using RApplied Regression Analysis using R
Applied Regression Analysis using RTarek Dib
 
VVC AUTO 85.5 - Smog Technician - Level 1 - Mode 6
VVC AUTO 85.5 - Smog Technician - Level 1 - Mode 6VVC AUTO 85.5 - Smog Technician - Level 1 - Mode 6
VVC AUTO 85.5 - Smog Technician - Level 1 - Mode 6Justin Gatewood
 
Bis 345-final-exam-guide-set-1-new
Bis 345-final-exam-guide-set-1-newBis 345-final-exam-guide-set-1-new
Bis 345-final-exam-guide-set-1-newassignmentcloud85
 
Reliable multimedia transmission under noisy condition
Reliable multimedia transmission under noisy conditionReliable multimedia transmission under noisy condition
Reliable multimedia transmission under noisy conditionShahrukh Ali Khan
 
Row patternmatching12ctech14
Row patternmatching12ctech14Row patternmatching12ctech14
Row patternmatching12ctech14stewashton
 
Proyecto final curso – Electrónica Digital I
Proyecto final curso – Electrónica Digital IProyecto final curso – Electrónica Digital I
Proyecto final curso – Electrónica Digital IDaniel A. Lopez Ch.
 

Similar to Leetcode Problem Solution (20)

Knowledge Area By Project Phase Excel Array Formulas
Knowledge Area By Project Phase   Excel Array FormulasKnowledge Area By Project Phase   Excel Array Formulas
Knowledge Area By Project Phase Excel Array Formulas
 
Exercise Problems for Chapter 5Numerical example on page 203Pe.docx
Exercise Problems for Chapter 5Numerical example on page 203Pe.docxExercise Problems for Chapter 5Numerical example on page 203Pe.docx
Exercise Problems for Chapter 5Numerical example on page 203Pe.docx
 
MRP
MRPMRP
MRP
 
Irregular sequence counter
Irregular sequence counterIrregular sequence counter
Irregular sequence counter
 
Binary system ppt
Binary system pptBinary system ppt
Binary system ppt
 
Informs_Xiaonan_03Oct2013
Informs_Xiaonan_03Oct2013Informs_Xiaonan_03Oct2013
Informs_Xiaonan_03Oct2013
 
Assign transportation
Assign transportationAssign transportation
Assign transportation
 
R Programming: Transform/Reshape Data In R
R Programming: Transform/Reshape Data In RR Programming: Transform/Reshape Data In R
R Programming: Transform/Reshape Data In R
 
Analysis and design of 15 storey office and
Analysis and design of 15 storey office andAnalysis and design of 15 storey office and
Analysis and design of 15 storey office and
 
Analysis and design of 15 storey office and
Analysis and design of 15 storey office andAnalysis and design of 15 storey office and
Analysis and design of 15 storey office and
 
Linear programming.pptx
Linear programming.pptxLinear programming.pptx
Linear programming.pptx
 
Applied Regression Analysis using R
Applied Regression Analysis using RApplied Regression Analysis using R
Applied Regression Analysis using R
 
chapter3
chapter3chapter3
chapter3
 
VVC AUTO 85.5 - Smog Technician - Level 1 - Mode 6
VVC AUTO 85.5 - Smog Technician - Level 1 - Mode 6VVC AUTO 85.5 - Smog Technician - Level 1 - Mode 6
VVC AUTO 85.5 - Smog Technician - Level 1 - Mode 6
 
Bis 345-final-exam-guide-set-1-new
Bis 345-final-exam-guide-set-1-newBis 345-final-exam-guide-set-1-new
Bis 345-final-exam-guide-set-1-new
 
Informe display 7 segmentos
Informe display 7 segmentosInforme display 7 segmentos
Informe display 7 segmentos
 
Reliable multimedia transmission under noisy condition
Reliable multimedia transmission under noisy conditionReliable multimedia transmission under noisy condition
Reliable multimedia transmission under noisy condition
 
Row patternmatching12ctech14
Row patternmatching12ctech14Row patternmatching12ctech14
Row patternmatching12ctech14
 
B25 Reducing redo by Julian Dyke
B25 Reducing redo by Julian DykeB25 Reducing redo by Julian Dyke
B25 Reducing redo by Julian Dyke
 
Proyecto final curso – Electrónica Digital I
Proyecto final curso – Electrónica Digital IProyecto final curso – Electrónica Digital I
Proyecto final curso – Electrónica Digital I
 

More from Hitesh Mohapatra

Virtualization: A Key to Efficient Cloud Computing
Virtualization: A Key to Efficient Cloud ComputingVirtualization: A Key to Efficient Cloud Computing
Virtualization: A Key to Efficient Cloud ComputingHitesh Mohapatra
 
Automating the Cloud: A Deep Dive into Virtual Machine Provisioning
Automating the Cloud: A Deep Dive into Virtual Machine ProvisioningAutomating the Cloud: A Deep Dive into Virtual Machine Provisioning
Automating the Cloud: A Deep Dive into Virtual Machine ProvisioningHitesh Mohapatra
 
Harnessing the Power of Google Cloud Platform: Strategies and Applications
Harnessing the Power of Google Cloud Platform: Strategies and ApplicationsHarnessing the Power of Google Cloud Platform: Strategies and Applications
Harnessing the Power of Google Cloud Platform: Strategies and ApplicationsHitesh Mohapatra
 
Scheduling in Cloud Computing
Scheduling in Cloud ComputingScheduling in Cloud Computing
Scheduling in Cloud ComputingHitesh Mohapatra
 
Load balancing in cloud computing.pptx
Load balancing in cloud computing.pptxLoad balancing in cloud computing.pptx
Load balancing in cloud computing.pptxHitesh Mohapatra
 
ITU-T requirement for cloud and cloud deployment model
ITU-T requirement for cloud and cloud deployment modelITU-T requirement for cloud and cloud deployment model
ITU-T requirement for cloud and cloud deployment modelHitesh Mohapatra
 
Reviewing basic concepts of relational database
Reviewing basic concepts of relational databaseReviewing basic concepts of relational database
Reviewing basic concepts of relational databaseHitesh Mohapatra
 
Advanced database protocols
Advanced database protocolsAdvanced database protocols
Advanced database protocolsHitesh Mohapatra
 
Involvement of WSN in Smart Cities
Involvement of WSN in Smart CitiesInvolvement of WSN in Smart Cities
Involvement of WSN in Smart CitiesHitesh Mohapatra
 
Data Structure and its Fundamentals
Data Structure and its FundamentalsData Structure and its Fundamentals
Data Structure and its FundamentalsHitesh Mohapatra
 
WORKING WITH FILE AND PIPELINE PARAMETER BINDING
WORKING WITH FILE AND PIPELINE PARAMETER BINDINGWORKING WITH FILE AND PIPELINE PARAMETER BINDING
WORKING WITH FILE AND PIPELINE PARAMETER BINDINGHitesh Mohapatra
 
Basic commands for powershell : Configuring Windows PowerShell and working wi...
Basic commands for powershell : Configuring Windows PowerShell and working wi...Basic commands for powershell : Configuring Windows PowerShell and working wi...
Basic commands for powershell : Configuring Windows PowerShell and working wi...Hitesh Mohapatra
 
WINDOWS ADMINISTRATION AND WORKING WITH OBJECTS : PowerShell ISE
WINDOWS ADMINISTRATION AND WORKING WITH OBJECTS : PowerShell ISEWINDOWS ADMINISTRATION AND WORKING WITH OBJECTS : PowerShell ISE
WINDOWS ADMINISTRATION AND WORKING WITH OBJECTS : PowerShell ISEHitesh Mohapatra
 

More from Hitesh Mohapatra (20)

Virtualization: A Key to Efficient Cloud Computing
Virtualization: A Key to Efficient Cloud ComputingVirtualization: A Key to Efficient Cloud Computing
Virtualization: A Key to Efficient Cloud Computing
 
Automating the Cloud: A Deep Dive into Virtual Machine Provisioning
Automating the Cloud: A Deep Dive into Virtual Machine ProvisioningAutomating the Cloud: A Deep Dive into Virtual Machine Provisioning
Automating the Cloud: A Deep Dive into Virtual Machine Provisioning
 
Harnessing the Power of Google Cloud Platform: Strategies and Applications
Harnessing the Power of Google Cloud Platform: Strategies and ApplicationsHarnessing the Power of Google Cloud Platform: Strategies and Applications
Harnessing the Power of Google Cloud Platform: Strategies and Applications
 
Scheduling in Cloud Computing
Scheduling in Cloud ComputingScheduling in Cloud Computing
Scheduling in Cloud Computing
 
Cloud-Case study
Cloud-Case study Cloud-Case study
Cloud-Case study
 
RAID
RAIDRAID
RAID
 
Load balancing in cloud computing.pptx
Load balancing in cloud computing.pptxLoad balancing in cloud computing.pptx
Load balancing in cloud computing.pptx
 
Cluster Computing
Cluster ComputingCluster Computing
Cluster Computing
 
ITU-T requirement for cloud and cloud deployment model
ITU-T requirement for cloud and cloud deployment modelITU-T requirement for cloud and cloud deployment model
ITU-T requirement for cloud and cloud deployment model
 
Leetcode Problem Solution
Leetcode Problem SolutionLeetcode Problem Solution
Leetcode Problem Solution
 
Trie Data Structure
Trie Data Structure Trie Data Structure
Trie Data Structure
 
Reviewing basic concepts of relational database
Reviewing basic concepts of relational databaseReviewing basic concepts of relational database
Reviewing basic concepts of relational database
 
Reviewing SQL Concepts
Reviewing SQL ConceptsReviewing SQL Concepts
Reviewing SQL Concepts
 
Advanced database protocols
Advanced database protocolsAdvanced database protocols
Advanced database protocols
 
Measures of query cost
Measures of query costMeasures of query cost
Measures of query cost
 
Involvement of WSN in Smart Cities
Involvement of WSN in Smart CitiesInvolvement of WSN in Smart Cities
Involvement of WSN in Smart Cities
 
Data Structure and its Fundamentals
Data Structure and its FundamentalsData Structure and its Fundamentals
Data Structure and its Fundamentals
 
WORKING WITH FILE AND PIPELINE PARAMETER BINDING
WORKING WITH FILE AND PIPELINE PARAMETER BINDINGWORKING WITH FILE AND PIPELINE PARAMETER BINDING
WORKING WITH FILE AND PIPELINE PARAMETER BINDING
 
Basic commands for powershell : Configuring Windows PowerShell and working wi...
Basic commands for powershell : Configuring Windows PowerShell and working wi...Basic commands for powershell : Configuring Windows PowerShell and working wi...
Basic commands for powershell : Configuring Windows PowerShell and working wi...
 
WINDOWS ADMINISTRATION AND WORKING WITH OBJECTS : PowerShell ISE
WINDOWS ADMINISTRATION AND WORKING WITH OBJECTS : PowerShell ISEWINDOWS ADMINISTRATION AND WORKING WITH OBJECTS : PowerShell ISE
WINDOWS ADMINISTRATION AND WORKING WITH OBJECTS : PowerShell ISE
 

Recently uploaded

Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and usesDevarapalliHaritha
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEroselinkalist12
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 

Recently uploaded (20)

Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and uses
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 

Leetcode Problem Solution

  • 1. Week 5 • Cheapest Flights within k stops • Merge K sorted Lists • Race Car
  • 2. Cheapest Flights within k stops • There are n cities connected by m flights. Each flight starts from city u and arrives at v with a price w. • Now given all the cities and flights, together with starting city src and the destination dst, your task is to find the cheapest price from src to dst with up to k stops. If there is no such route, output -1.
  • 3. Example: The cheapest price from city 0 to city 2 with at most 1 stop costs 200, as marked red in the picture.
  • 4.
  • 8. // Adjacency Matrix // Calling DFS through solve function // Graph Generation // Visited Array // Keep track of the minimum cost // Cost Matrix
  • 9.
  • 10. Merge K sorted Lists • You are given an array of k linked-lists lists, each linked-list is sorted in ascending order. • Merge all the linked-lists into one sorted linked-list and return it.
  • 11.
  • 12. 10 20 22 25 32 54 64 5 7 11 19 4 6 8 15 17 Priority Queue:10,5, 4 Dummy: {4,
  • 13. 10 20 22 25 32 54 64 5 7 11 19 4 6 8 15 17 Priority Queue: 10,5, 6 4, 5,
  • 14. 10 20 22 25 32 54 64 5 7 11 19 4 6 8 15 17 Priority Queue: 10,6,7 4, 5, 6,
  • 15. 10 20 22 25 32 54 64 5 7 11 19 4 6 8 15 17 Priority Queue: 10,7,8 4, 5, 6, 7,
  • 16. 10 20 22 25 32 54 64 5 7 11 19 4 6 8 15 17 Priority Queue: 10, 8, 11 4, 5, 6, 7, 8,
  • 17. 10 20 22 25 32 54 64 5 7 11 19 4 6 8 15 17 Priority Queue: 10, 11,15 4, 5, 6, 7, 8, 10
  • 18. 10 20 22 25 32 54 64 5 7 11 19 4 6 8 15 17 Priority Queue: 11,15, 20 4, 5, 6, 7, 8, 10,11,
  • 19. 10 20 22 25 32 54 64 5 7 11 19 4 6 8 15 17 Priority Queue: 15, 20,19 4, 5, 6, 7, 8, 10,11, 15,
  • 20. 10 20 22 25 32 54 64 5 7 11 19 4 6 8 15 17 Priority Queue: 20,19, 17 4, 5, 6, 7, 8, 10,11, 15, 17
  • 21. 10 20 22 25 32 54 64 5 7 11 19 4 6 8 15 17 Priority Queue: 20, 22 4, 5, 6, 7, 8, 10,11, 15, 17, 19, 20
  • 22. 10 20 22 25 32 54 64 5 7 11 19 4 6 8 15 17 Priority Queue: 22,25 4, 5, 6, 7, 8, 10,11, 15, 17, 19, 20,22
  • 23. 10 20 22 25 32 54 64 5 7 11 19 4 6 8 15 17 Priority Queue: 25,32 4, 5, 6, 7, 8, 10,11, 15, 17, 19, 20,22
  • 24. 10 20 22 25 32 54 64 5 7 11 19 4 6 8 15 17 Priority Queue: 32, 54 4, 5, 6, 7, 8, 10,11, 15, 17, 19, 20,22,25,32,
  • 25. 10 20 22 25 32 54 64 5 7 11 19 4 6 8 15 17 Priority Queue: 64 4, 5, 6, 7, 8, 10,11, 15, 17, 19, 20,22,25,32,54
  • 26. 10 20 22 25 32 54 64 5 7 11 19 4 6 8 15 17 Priority Queue: -- 4, 5, 6, 7, 8, 10,11, 15, 17, 19, 20,22,25,32,54,64
  • 27. Algorithm: • 10,20,30,40 ---L0 • 5,9,12,18,32 ---L1 • 11,15,17 ---L2 1-0-5 0-0-10 0-0-10 1-0-5 1-1-9 2-0-11 2-0-11 5,
  • 29. Race Car • Your car starts at position 0 and speed +1 on an infinite number line. (Your car can go into negative positions.) • Your car drives automatically according to a sequence of instructions A (accelerate) and R (reverse). • When you get an instruction "A", your car does the following: position += speed, speed *= 2. • When you get an instruction "R", your car does the following: if your speed is positive then speed = -1 , otherwise speed = 1. (Your position stays the same.) • For example, after commands "AAR", your car goes to positions 0- >1->3->3, and your speed goes to 1->2->4->-1. • Now for some target position, say the length of the shortest sequence of instructions to get there.
  • 30.
  • 31. Solution • T=6 // Even // Target = Target/2 • Step 1 = 6/2 = 3 (Temp_T) // Odd Taregt = Target+1 • Step 2 = 3+ 1= 4 • Step 3= 4/2 = 2 • Step 4 = 2/2=1 • Step 5 = 0 0,1,2,4,3,6 AAARA
  • 32. • T =15 • Step 1 = 15 + 1 =16 • Step 2: 16/2 = 8 • Step 3 : 8/2= 4 • Step 4: 4/2= 2 • Step 5: 2/2 =1 • Step 6: 0 0,1,2,4,8,16,15 A,A,A,A,A,R