Medium:
Maximum Level sum of a Binary Tree
Jump Game III
Minesweeper
Binary Tree Level Order Traversal
Number of operations to make network
connected
Open the lock
Hard:
Sliding Puzzle
Trapping Rain Water II
22-02-2021 Hitesh Mohapatra 1
Leetcode
22-02-2021 Hitesh Mohapatra 2
22-02-2021 Hitesh Mohapatra 3
22-02-2021 Hitesh Mohapatra 4
22-02-2021 Hitesh Mohapatra 5
22-02-2021 Hitesh Mohapatra 6
22-02-2021 Hitesh Mohapatra 7
22-02-2021 Hitesh Mohapatra 8
22-02-2021 Hitesh Mohapatra 9
22-02-2021 Hitesh Mohapatra 10
22-02-2021 Hitesh Mohapatra 11
22-02-2021 Hitesh Mohapatra 12
22-02-2021 Hitesh Mohapatra 13
1319. Number of Operations to Make Network
Connected
• There are n computers numbered from 0 to n-1 connected
by Ethernet cables connections forming a network
where connections[i] = [a, b] represents a connection
between computers a and b. Any computer can reach any
other computer directly or indirectly through the network.
• Given an initial computer network connections. You can
extract certain cables between two directly connected
computers, and place them between any pair of
disconnected computers to make them directly connected.
Return the minimum number of times you need to do this
in order to make all the computers connected. If it's not
possible, return -1.
22-02-2021 Hitesh Mohapatra 14
22-02-2021 Hitesh Mohapatra 15
22-02-2021 Hitesh Mohapatra 16
22-02-2021 Hitesh Mohapatra 17
Algorithm
• Make Adjacency List
• Find number of components by using DFS
• Find number of redundant edges
– When(Redundant edges >= component -1)
– Then (Edges required = (C-1)
Negative Case
• Edge < number of nodes -1
• Redundant edges < component-1
22-02-2021 Hitesh Mohapatra 18
752. Open the Lock
• You have a lock in front of you with 4 circular wheels. Each
wheel has 10 slots: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'. The
wheels can rotate freely and wrap around: for example we
can turn '9' to be '0', or '0' to be '9'. Each move consists of
turning one wheel one slot.
• The lock initially starts at '0000', a string representing the
state of the 4 wheels.
• You are given a list of deadends dead ends, meaning if the
lock displays any of these codes, the wheels of the lock will
stop turning and you will be unable to open it.
• Given a target representing the value of the wheels that
will unlock the lock, return the minimum total number of
turns required to open the lock, or -1 if it is impossible.
22-02-2021 Hitesh Mohapatra 19
22-02-2021 Hitesh Mohapatra 20
Target 0202
0000 ------------------------ Initial State
0100 ------------------------1
0200 ------------------------2
0201
0210 ------------------------3
0211 ------------------------4
0212 ------------------------5
0202------------------------ 6 Target
deadends = ["0201","0101","0102","1212","2002"]
22-02-2021 Hitesh Mohapatra 21
22-02-2021 Hitesh Mohapatra 22
Algorithm
• Initiate the with 0000
• Find out potential neighbors
• Check neighbor with deadends
• Take the turn where turn != deadends
• Count the number of turn to meet the target.
Negative Side
• If initial state is == deadend ; return -1
• If all the possible moves are falls under deadends
; return -1
22-02-2021 Hitesh Mohapatra 23
773. Sliding Puzzle
• On a 2x3 board, there are 5 tiles represented by
the integers 1 through 5, and an empty square
represented by 0.
• A move consists of choosing 0 and a 4-
directionally adjacent number and swapping it.
• The state of the board is solved if and only if
the board is [[1,2,3],[4,5,0]].
• Given a puzzle board, return the least number of
moves required so that the state of the board is
solved. If it is impossible for the state of the
board to be solved, return -1.
22-02-2021 Hitesh Mohapatra 24
22-02-2021 Hitesh Mohapatra 25
22-02-2021 Hitesh Mohapatra 26

Leetcode Problem Solution

  • 1.
    Medium: Maximum Level sumof a Binary Tree Jump Game III Minesweeper Binary Tree Level Order Traversal Number of operations to make network connected Open the lock Hard: Sliding Puzzle Trapping Rain Water II 22-02-2021 Hitesh Mohapatra 1 Leetcode
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
    1319. Number ofOperations to Make Network Connected • There are n computers numbered from 0 to n-1 connected by Ethernet cables connections forming a network where connections[i] = [a, b] represents a connection between computers a and b. Any computer can reach any other computer directly or indirectly through the network. • Given an initial computer network connections. You can extract certain cables between two directly connected computers, and place them between any pair of disconnected computers to make them directly connected. Return the minimum number of times you need to do this in order to make all the computers connected. If it's not possible, return -1. 22-02-2021 Hitesh Mohapatra 14
  • 15.
  • 16.
  • 17.
  • 18.
    Algorithm • Make AdjacencyList • Find number of components by using DFS • Find number of redundant edges – When(Redundant edges >= component -1) – Then (Edges required = (C-1) Negative Case • Edge < number of nodes -1 • Redundant edges < component-1 22-02-2021 Hitesh Mohapatra 18
  • 19.
    752. Open theLock • You have a lock in front of you with 4 circular wheels. Each wheel has 10 slots: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'. The wheels can rotate freely and wrap around: for example we can turn '9' to be '0', or '0' to be '9'. Each move consists of turning one wheel one slot. • The lock initially starts at '0000', a string representing the state of the 4 wheels. • You are given a list of deadends dead ends, meaning if the lock displays any of these codes, the wheels of the lock will stop turning and you will be unable to open it. • Given a target representing the value of the wheels that will unlock the lock, return the minimum total number of turns required to open the lock, or -1 if it is impossible. 22-02-2021 Hitesh Mohapatra 19
  • 20.
  • 21.
    Target 0202 0000 ------------------------Initial State 0100 ------------------------1 0200 ------------------------2 0201 0210 ------------------------3 0211 ------------------------4 0212 ------------------------5 0202------------------------ 6 Target deadends = ["0201","0101","0102","1212","2002"] 22-02-2021 Hitesh Mohapatra 21
  • 22.
  • 23.
    Algorithm • Initiate thewith 0000 • Find out potential neighbors • Check neighbor with deadends • Take the turn where turn != deadends • Count the number of turn to meet the target. Negative Side • If initial state is == deadend ; return -1 • If all the possible moves are falls under deadends ; return -1 22-02-2021 Hitesh Mohapatra 23
  • 24.
    773. Sliding Puzzle •On a 2x3 board, there are 5 tiles represented by the integers 1 through 5, and an empty square represented by 0. • A move consists of choosing 0 and a 4- directionally adjacent number and swapping it. • The state of the board is solved if and only if the board is [[1,2,3],[4,5,0]]. • Given a puzzle board, return the least number of moves required so that the state of the board is solved. If it is impossible for the state of the board to be solved, return -1. 22-02-2021 Hitesh Mohapatra 24
  • 25.
  • 26.