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
 
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
 
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
 
Unit 2 monte carlo simulation
Unit 2 monte carlo simulationUnit 2 monte carlo simulation
Unit 2 monte carlo simulationDevaKumari Vijay
 
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
 

Similar to Leetcode Problem Solution (20)

Tsukubar8
Tsukubar8Tsukubar8
Tsukubar8
 
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
 
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
 
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
 
Unit 2 monte carlo simulation
Unit 2 monte carlo simulationUnit 2 monte carlo simulation
Unit 2 monte carlo simulation
 
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
 

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

Intelligent Agents, A discovery on How A Rational Agent Acts
Intelligent Agents, A discovery on How A Rational Agent ActsIntelligent Agents, A discovery on How A Rational Agent Acts
Intelligent Agents, A discovery on How A Rational Agent ActsSheetal Jain
 
ALCOHOL PRODUCTION- Beer Brewing Process.pdf
ALCOHOL PRODUCTION- Beer Brewing Process.pdfALCOHOL PRODUCTION- Beer Brewing Process.pdf
ALCOHOL PRODUCTION- Beer Brewing Process.pdfMadan Karki
 
Online crime reporting system project.pdf
Online crime reporting system project.pdfOnline crime reporting system project.pdf
Online crime reporting system project.pdfKamal Acharya
 
Module-III Varried Flow.pptx GVF Definition, Water Surface Profile Dynamic Eq...
Module-III Varried Flow.pptx GVF Definition, Water Surface Profile Dynamic Eq...Module-III Varried Flow.pptx GVF Definition, Water Surface Profile Dynamic Eq...
Module-III Varried Flow.pptx GVF Definition, Water Surface Profile Dynamic Eq...Nitin Sonavane
 
Online book store management system project.pdf
Online book store management system project.pdfOnline book store management system project.pdf
Online book store management system project.pdfKamal Acharya
 
Geometric constructions Engineering Drawing.pdf
Geometric constructions Engineering Drawing.pdfGeometric constructions Engineering Drawing.pdf
Geometric constructions Engineering Drawing.pdfJNTUA
 
AI in Healthcare Innovative use cases and applications.pdf
AI in Healthcare Innovative use cases and applications.pdfAI in Healthcare Innovative use cases and applications.pdf
AI in Healthcare Innovative use cases and applications.pdfmahaffeycheryld
 
Software Engineering - Modelling Concepts + Class Modelling + Building the An...
Software Engineering - Modelling Concepts + Class Modelling + Building the An...Software Engineering - Modelling Concepts + Class Modelling + Building the An...
Software Engineering - Modelling Concepts + Class Modelling + Building the An...Prakhyath Rai
 
Filters for Electromagnetic Compatibility Applications
Filters for Electromagnetic Compatibility ApplicationsFilters for Electromagnetic Compatibility Applications
Filters for Electromagnetic Compatibility ApplicationsMathias Magdowski
 
Supermarket billing system project report..pdf
Supermarket billing system project report..pdfSupermarket billing system project report..pdf
Supermarket billing system project report..pdfKamal Acharya
 
Tembisa Central Terminating Pills +27838792658 PHOMOLONG Top Abortion Pills F...
Tembisa Central Terminating Pills +27838792658 PHOMOLONG Top Abortion Pills F...Tembisa Central Terminating Pills +27838792658 PHOMOLONG Top Abortion Pills F...
Tembisa Central Terminating Pills +27838792658 PHOMOLONG Top Abortion Pills F...drjose256
 
Introduction to Artificial Intelligence and History of AI
Introduction to Artificial Intelligence and History of AIIntroduction to Artificial Intelligence and History of AI
Introduction to Artificial Intelligence and History of AISheetal Jain
 
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas SachpazisSeismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas SachpazisDr.Costas Sachpazis
 
Research Methodolgy & Intellectual Property Rights Series 1
Research Methodolgy & Intellectual Property Rights Series 1Research Methodolgy & Intellectual Property Rights Series 1
Research Methodolgy & Intellectual Property Rights Series 1T.D. Shashikala
 
Research Methodolgy & Intellectual Property Rights Series 2
Research Methodolgy & Intellectual Property Rights Series 2Research Methodolgy & Intellectual Property Rights Series 2
Research Methodolgy & Intellectual Property Rights Series 2T.D. Shashikala
 
Fabrication Of Automatic Star Delta Starter Using Relay And GSM Module By Utk...
Fabrication Of Automatic Star Delta Starter Using Relay And GSM Module By Utk...Fabrication Of Automatic Star Delta Starter Using Relay And GSM Module By Utk...
Fabrication Of Automatic Star Delta Starter Using Relay And GSM Module By Utk...ShivamTiwari995432
 
Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdf
Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdfInstruct Nirmaana 24-Smart and Lean Construction Through Technology.pdf
Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdfEr.Sonali Nasikkar
 
Artificial Intelligence Bayesian Reasoning
Artificial Intelligence Bayesian ReasoningArtificial Intelligence Bayesian Reasoning
Artificial Intelligence Bayesian Reasoninghotman30312
 
Low rpm Generator for efficient energy harnessing from a two stage wind turbine
Low rpm Generator for efficient energy harnessing from a two stage wind turbineLow rpm Generator for efficient energy harnessing from a two stage wind turbine
Low rpm Generator for efficient energy harnessing from a two stage wind turbineAftabkhan575376
 
Microkernel in Operating System | Operating System
Microkernel in Operating System | Operating SystemMicrokernel in Operating System | Operating System
Microkernel in Operating System | Operating SystemSampad Kar
 

Recently uploaded (20)

Intelligent Agents, A discovery on How A Rational Agent Acts
Intelligent Agents, A discovery on How A Rational Agent ActsIntelligent Agents, A discovery on How A Rational Agent Acts
Intelligent Agents, A discovery on How A Rational Agent Acts
 
ALCOHOL PRODUCTION- Beer Brewing Process.pdf
ALCOHOL PRODUCTION- Beer Brewing Process.pdfALCOHOL PRODUCTION- Beer Brewing Process.pdf
ALCOHOL PRODUCTION- Beer Brewing Process.pdf
 
Online crime reporting system project.pdf
Online crime reporting system project.pdfOnline crime reporting system project.pdf
Online crime reporting system project.pdf
 
Module-III Varried Flow.pptx GVF Definition, Water Surface Profile Dynamic Eq...
Module-III Varried Flow.pptx GVF Definition, Water Surface Profile Dynamic Eq...Module-III Varried Flow.pptx GVF Definition, Water Surface Profile Dynamic Eq...
Module-III Varried Flow.pptx GVF Definition, Water Surface Profile Dynamic Eq...
 
Online book store management system project.pdf
Online book store management system project.pdfOnline book store management system project.pdf
Online book store management system project.pdf
 
Geometric constructions Engineering Drawing.pdf
Geometric constructions Engineering Drawing.pdfGeometric constructions Engineering Drawing.pdf
Geometric constructions Engineering Drawing.pdf
 
AI in Healthcare Innovative use cases and applications.pdf
AI in Healthcare Innovative use cases and applications.pdfAI in Healthcare Innovative use cases and applications.pdf
AI in Healthcare Innovative use cases and applications.pdf
 
Software Engineering - Modelling Concepts + Class Modelling + Building the An...
Software Engineering - Modelling Concepts + Class Modelling + Building the An...Software Engineering - Modelling Concepts + Class Modelling + Building the An...
Software Engineering - Modelling Concepts + Class Modelling + Building the An...
 
Filters for Electromagnetic Compatibility Applications
Filters for Electromagnetic Compatibility ApplicationsFilters for Electromagnetic Compatibility Applications
Filters for Electromagnetic Compatibility Applications
 
Supermarket billing system project report..pdf
Supermarket billing system project report..pdfSupermarket billing system project report..pdf
Supermarket billing system project report..pdf
 
Tembisa Central Terminating Pills +27838792658 PHOMOLONG Top Abortion Pills F...
Tembisa Central Terminating Pills +27838792658 PHOMOLONG Top Abortion Pills F...Tembisa Central Terminating Pills +27838792658 PHOMOLONG Top Abortion Pills F...
Tembisa Central Terminating Pills +27838792658 PHOMOLONG Top Abortion Pills F...
 
Introduction to Artificial Intelligence and History of AI
Introduction to Artificial Intelligence and History of AIIntroduction to Artificial Intelligence and History of AI
Introduction to Artificial Intelligence and History of AI
 
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas SachpazisSeismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
 
Research Methodolgy & Intellectual Property Rights Series 1
Research Methodolgy & Intellectual Property Rights Series 1Research Methodolgy & Intellectual Property Rights Series 1
Research Methodolgy & Intellectual Property Rights Series 1
 
Research Methodolgy & Intellectual Property Rights Series 2
Research Methodolgy & Intellectual Property Rights Series 2Research Methodolgy & Intellectual Property Rights Series 2
Research Methodolgy & Intellectual Property Rights Series 2
 
Fabrication Of Automatic Star Delta Starter Using Relay And GSM Module By Utk...
Fabrication Of Automatic Star Delta Starter Using Relay And GSM Module By Utk...Fabrication Of Automatic Star Delta Starter Using Relay And GSM Module By Utk...
Fabrication Of Automatic Star Delta Starter Using Relay And GSM Module By Utk...
 
Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdf
Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdfInstruct Nirmaana 24-Smart and Lean Construction Through Technology.pdf
Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdf
 
Artificial Intelligence Bayesian Reasoning
Artificial Intelligence Bayesian ReasoningArtificial Intelligence Bayesian Reasoning
Artificial Intelligence Bayesian Reasoning
 
Low rpm Generator for efficient energy harnessing from a two stage wind turbine
Low rpm Generator for efficient energy harnessing from a two stage wind turbineLow rpm Generator for efficient energy harnessing from a two stage wind turbine
Low rpm Generator for efficient energy harnessing from a two stage wind turbine
 
Microkernel in Operating System | Operating System
Microkernel in Operating System | Operating SystemMicrokernel in Operating System | Operating System
Microkernel in Operating System | Operating System
 

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