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
 
Irregular sequence counter
Irregular sequence counterIrregular sequence counter
Irregular sequence counterAbdullah Shiam
 
Row Pattern Matching 12c MATCH_RECOGNIZE OOW14
Row Pattern Matching 12c MATCH_RECOGNIZE OOW14Row Pattern Matching 12c MATCH_RECOGNIZE OOW14
Row Pattern Matching 12c MATCH_RECOGNIZE OOW14stewashton
 
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
 
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.
 
Impementing cellular manufacturing
Impementing cellular manufacturingImpementing cellular manufacturing
Impementing cellular manufacturingHassan Habib
 

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
 
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
 
Row Pattern Matching 12c MATCH_RECOGNIZE OOW14
Row Pattern Matching 12c MATCH_RECOGNIZE OOW14Row Pattern Matching 12c MATCH_RECOGNIZE OOW14
Row Pattern Matching 12c MATCH_RECOGNIZE OOW14
 
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
 
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
 
Impementing cellular manufacturing
Impementing cellular manufacturingImpementing cellular manufacturing
Impementing cellular manufacturing
 
Tsp branch and-bound
Tsp branch and-boundTsp branch and-bound
Tsp branch and-bound
 

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

Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsRussian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduitsrknatarajan
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...Call Girls in Nagpur High Profile
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...ranjana rawat
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdfankushspencer015
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 

Recently uploaded (20)

Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsRussian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 

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