• 1 day ago
Transcript
00:00Now based on n queens, there is another optimization
00:03Another optimized n queens can be asked if we have to count ways
00:07This is the second type of n queens
00:09which we have to count total number of ways
00:11which we can place n queens in this way
00:14on the board of n by n cross n
00:17so that two queens do not attack another
00:19So count total number of ways in which we can solve n queens problem
00:23What do we do?
00:24Every time we hit the base case
00:28It means that my row of value is equal to the length of my board
00:36What do I do every time?
00:38Here I was printing my board
00:40What do I do?
00:42When I print the board, I take a variable
00:44Count
00:45I will make it in static type
00:47and when I print the board, I will count
00:50plus plus
00:51So the board is printing
00:53It means that the right solution is coming
00:55The board will not print this time
00:57It will be just my count plus plus
00:59So I am going to optimize this optimization
01:01To count the number of ways
01:03I will take a static variable
01:05Why do I take a static variable?
01:07Because every level
01:08There are levels of recursion
01:10If I pass a parameter
01:11Then there will be a new parameter
01:13Because the recursion
01:15Every time in Java
01:15There is a call by value
01:16So the recursion
01:18To pass a parameter
01:19I will make a static variable
01:21And the static variable
01:23will be plus plus
01:25So I will make a static int
01:27Count
01:27Which I will initialize
01:29With 0
01:29Now here
01:31Like the base case
01:33Hit it
01:33It means that the solution is getting
01:34And we are printing the solution
01:36To print
01:37What will I do
01:38To print it
01:38I will do this
01:39Count
01:39Here
01:41And in the last
01:42In the main function
01:44I will simply print
01:46Total ways
01:47To solve n queens
01:50Is equal to
01:53Count
01:54I have done this
01:55I have done my count
01:56First
01:57Let's look for n is equal to 4
01:58I know that
01:59For n is equal to 4
02:00There are two possible solutions
02:02I have two solutions
02:02For n is equal to 4
02:03I have two solutions
02:05I have two solutions
02:06I know that
02:06I know that
02:06N is equal to 2
02:08Or 3
02:08We don't know that
02:09There is one possible solution
02:10So the solution will be 0
02:13And for n is equal to 5
02:14I have probably 10 solutions
02:16So check it out
02:17One time
02:18The value is 10
02:19So this way
02:21If we count all possible solutions
02:23After that
02:24We can optimize the code
02:25So here
02:26I have optimized
02:27To print the solution
02:29And after that
02:29I have counted the total number of solutions
02:31I have counted the total number of solutions
02:32And I also counted the same time complexity
02:35Whether I print or not
02:37Whether I print
02:38I will remove those solutions
02:38I will have time to take away
02:41In the code
02:41I will have time to take away
02:41To remove that
02:41To remove that