backtracking

Return all possible placements of buildings in a city grid using backtracking

Return all possible placements of buildings in a city grid using backtracking Question: I have two inputs An NxM city grid where empty spaces represent vacant lots, and X’s represent filled lots e.g X XXX X X XXXX XX X This is in the format List[List[str]] And an integer that represents the number of buildings …

Total answers: 2

leetcode 39. Combination Sum why do I have to return after I get one solution?

leetcode 39. Combination Sum why do I have to return after I get one solution? Question: so the question is like this: Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. You may return the combinations …

Total answers: 1

Sudoku solving taking forever to run

Sudoku solving taking forever to run Question: The example below is working however, if more zeros (empty cells) are added to the sudoku grid g, it takes longer to run, if it ever finishes. Not asking for a code review here, just I might’ve overlooked something, and I’d appreciate pointing it out. def is_solved(grid): for …

Total answers: 2

Sudoku Solver – Modify a list in-place within a recursive function

Sudoku Solver – Modify a list in-place within a recursive function Question: This is leetcode #37 (Sudoku solver). I have a question regarding modifying an input list in-place within a recursive function. The code below pretty much does the job as the print(board) does print the correct solution (I’m sure the efficiency can be improved …

Total answers: 2

Leetcode 78: Subsets confused why duplicate subsets are generated

Leetcode 78: Subsets confused why duplicate subsets are generated Question: I’m working on Leetcode 78: subsets and I’ve copied over the question below. Given an integer array nums of unique elements, return all possible subsets (the power set). The solution set must not contain duplicate subsets. Return the solution in any order. I have a …

Total answers: 2

Return immediately for recursive calls

Return immediately for recursive calls Question: Problem Statement: You are given an integer array nums. You are initially positioned at the array’s first index, and each element in the array represents your maximum jump length at that position. Return true if you can reach the last index, or false otherwise. How can I change my …

Total answers: 2

Python sudoku backtracking

Python sudoku backtracking Question: I was trying out the backtracking algorithm with an easy example (sudoku). I first tried another approach where more possibilities are canceled, but after I got the same error I switched to an easier solution. look for the first unsolved spot fill in every number between 1 and 9 and backtrack …

Total answers: 3

Minimum Coin change problem – Backtracking

Minimum Coin change problem – Backtracking Question: I’m trying to solve Coin change problem with minimum number of coins using backtracking method. I’ve actually done with it, but I want to add some option that print the number of coins by its unit not only total amount. this is my python codes below. def minimum_coins(coin_list, …

Total answers: 3

How does the following recursive Sudoku solving function work?

How does the following recursive Sudoku solving function work? Question: I recently watched this video showing a recursive function to solve sudoku and this seems irrational, since at the end we always change the value back to zero in the end. How come the function worked in the video but doesnt work for me? Should …

Total answers: 2

Implementing the Prolog Unification algorithm in Python? Backtracking

Implementing the Prolog Unification algorithm in Python? Backtracking Question: I’m trying to implement Unification, but having problems.. already got dozen of examples,but all they do is to muddy the water. I get more confused than enlightened : http://www.cs.trincoll.edu/~ram/cpsc352/notes/unification.html https://www.doc.ic.ac.uk/~sgc/teaching/pre2012/v231/lecture8.html [code below is based on this intro] http://www.cs.bham.ac.uk/research/projects/poplog/paradigms_lectures/lecture20.html#representing https://norvig.com/unify-bug.pdf How can I implement the unification algorithm …

Total answers: 3