
BackTracking
文章平均质量分 73
flyatcmu
这个作者很懒,什么都没留下…
展开
-
Closest Dessert Cost
You would like to make dessert and are preparing to buy the ingredients. You havenice cream base flavors andmtypes of toppings to choose from. You must follow these rules when making your dessert:There must beexactly oneice cream base. You can add...原创 2022-03-21 00:54:34 · 326 阅读 · 0 评论 -
Expression Add Operators
Given a string that contains only digits0-9and a target value, return all possibilities to addbinaryoperators (not unary)+,-, or*between the digits so they evaluate to the target value.原创 2016-09-22 14:56:37 · 298 阅读 · 0 评论 -
Maximum Good People Based on Statements
There are two types of persons:Thegood person: The person who always tells the truth. Thebad person: The person who might tell the truth and might lie.You are given a0-indexed2D integer arraystatementsof sizen x nthat represents the statements ...原创 2022-01-25 03:17:21 · 196 阅读 · 0 评论 -
Step-By-Step Directions From a Binary Tree Node to Another
You are given therootof abinary treewithnnodes. Each node is uniquely assigned a value from1ton. You are also given an integerstartValuerepresenting the value of the start nodes, and a different integerdestValuerepresenting the value of the d...原创 2021-12-06 05:07:13 · 245 阅读 · 0 评论 -
Finding 3-Digit Even Numbers
You are given an integer arraydigits, where each element is a digit. The array may contain duplicates.You need to findalltheuniqueintegers that follow the given requirements:The integer consists of theconcatenationofthreeelements fromdigitsi...原创 2021-12-05 12:46:06 · 264 阅读 · 0 评论 -
Maximum Number of Achievable Transfer Requests
We havenbuildings numbered from0ton - 1. Each building has a number of employees. It's transfer season, and some employees want to change the building they reside in.You are given an arrayrequestswhererequests[i] = [fromi, toi]represents an empl...原创 2020-09-27 12:42:21 · 297 阅读 · 0 评论 -
Split a String Into the Max Number of Unique Substrings
Given a strings,returnthe maximumnumber of unique substrings that the given string can be split into.You can split stringsinto any list ofnon-empty substrings, where the concatenation of the substrings forms the original string.However, you must ...原创 2020-09-20 13:29:38 · 252 阅读 · 0 评论 -
Number of Distinct Islands
Given a non-empty 2D arraygridof 0's and 1's, anislandis a group of1's (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrou...原创 2020-04-11 05:49:24 · 266 阅读 · 0 评论 -
Smallest Sufficient Team
In a project, you have a list of required skillsreq_skills,and a list ofpeople. The i-th personpeople[i]contains a list of skills that person has.Consider asufficient team: a set of people such that for every required skill inreq_skills, there is...原创 2020-06-28 04:55:50 · 283 阅读 · 0 评论 -
Tiling a Rectangle with the Fewest Squares
Given a rectangle of sizenxm, find the minimum number of integer-sided squares that tile the rectangle.Example 1:Input: n = 2, m = 3Output: 3Explanation: 3 squares are necessary to cover the rectangle.2(squares of 1x1)1(square of 2x2)Example ...原创 2020-06-26 14:03:29 · 350 阅读 · 0 评论 -
Android Unlock Patterns
Given an Android3x3key lock screen and two integersmandn, where 1 ≤ m ≤ n ≤ 9, count the total number of unlock patterns of the Android lock screen, which consist of minimum ofmkeys and maximumnkeys.Rules for a valid pattern:Each pattern must ...原创 2020-06-20 11:32:05 · 212 阅读 · 0 评论 -
Unique Paths III
On a 2-dimensionalgrid, there are 4 types of squares:1represents the starting square. There is exactly one starting square. 2represents the ending square. There is exactly one ending square. 0represents empty squares we can walk over. -1represe...原创 2020-06-10 14:17:57 · 188 阅读 · 0 评论 -
Pseudo-Palindromic Paths in a Binary Tree
Given a binary tree where node values are digits from 1 to 9. A path in the binary tree is said to bepseudo-palindromicif at least one permutation of the node values in the path is a palindrome.Return the number ofpseudo-palindromicpaths going from t...原创 2020-05-25 02:51:53 · 282 阅读 · 0 评论 -
Implement Magic Dictionary
Implement a magic directory withbuildDict, andsearchmethods.For the methodbuildDict, you'll be given a list of non-repetitive words to build a dictionary.For the methodsearch, you'll be given a word, and judge whether if you modifyexactlyone cha...原创 2020-05-22 12:00:25 · 156 阅读 · 0 评论 -
Partition to K Equal Sum Subsets
Given an array of integersnumsand a positive integerk, find whether it's possible to divide this array intoknon-empty subsets whose sums are all equal.Example 1:Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4Output: TrueExplanation: It's possible...原创 2020-05-20 12:36:50 · 336 阅读 · 0 评论 -
Increasing Subsequences
Given an integer array, your task is to find all the different possible increasing subsequences of the given array, and the length of an increasing subsequence should be at least 2.Example:Input: [4, 6, 7, 7]Output: [[4, 6], [4, 7], [4, 6, 7], [4, 6,原创 2020-05-20 11:34:14 · 206 阅读 · 0 评论 -
Valid sudoku
Determine if a Sudoku is valid, according to:Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty cells are filled with the character'.'.A partially filled sud...原创 2014-03-04 15:43:22 · 570 阅读 · 0 评论 -
Next Closest Time
Given a time represented in the format "HH:MM", form the next closest time by reusing the current digits. There is no limit on how many times a digit can be reused.You may assume the given input str...原创 2020-05-06 12:20:28 · 395 阅读 · 0 评论 -
Optimal Account Balancing
A group of friends went on holiday and sometimes lent each other money. For example, Alice paid for Bill's lunch for $10. Then later Chris gave Alice $5 for a taxi ride. We can model each transaction ...原创 2020-04-28 14:18:40 · 276 阅读 · 0 评论 -
Confusing Number II
We can rotate digits by 180 degrees to form new digits. When 0, 1, 6, 8, 9 are rotated 180 degrees, they become 0, 1, 9, 8, 6 respectively. When 2, 3, 4, 5 and 7 are rotated 180 degrees, they become i...原创 2020-03-28 02:38:04 · 292 阅读 · 0 评论 -
24 Game
You have 4 cards each containing a number from 1 to 9. You need to judge whether they could operated through*,/,+,-,(,)to get the value of 24.Example 1:Input: [4, 1, 8, 7]Output: TrueExp...原创 2020-02-24 12:14:03 · 224 阅读 · 0 评论 -
Brace Expansion
A stringSrepresents a list of words.Each letter in the word has 1 or more options. If there is one option, the letter is represented as is. If there is more than one option, then curly braces de...原创 2020-01-16 11:53:58 · 371 阅读 · 0 评论 -
K Sum II
Given n unique postive integers, number k (1<=k<=n) and target.Find all possible k integers where their sum is target.ExampleExample 1:Input: [1,2,3,4], k = 2, target = 5Output: [[1,4...原创 2019-12-30 06:43:53 · 129 阅读 · 0 评论 -
Path Sum II
Your are given a binary tree in which each node contains a value. Design an algorithm to get all paths which sum to a given value. The path does not need to start or end at the root or a leaf, but it ...原创 2019-12-25 14:18:02 · 107 阅读 · 0 评论 -
Binary Tree Path Sum
Given a binary tree, find all paths that sum of the nodes in the path equals to a given numbertarget.A valid path is from root node to any of the leaf nodes.ExampleExample 1:Input:{1,2,4,2,...原创 2019-12-25 13:48:26 · 154 阅读 · 0 评论 -
Word Pattern II
Given apatternand a stringstr, find ifstrfollows the same pattern.Herefollowmeans a full match, such that there is abijectionbetween a letter inpatternand anon-emptysubstring instr.(i....原创 2019-11-13 15:57:09 · 202 阅读 · 0 评论 -
N-Queens
Then-queens puzzle is the problem of placingnqueens on ann×nchessboard such that no two queens attack each other.Given an integern, return all distinct solutions to then-queens puzzle.Each ...原创 2014-12-08 09:43:01 · 712 阅读 · 0 评论 -
Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is given below.Input:Digit ...原创 2014-12-11 07:44:54 · 466 阅读 · 0 评论 -
Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome.Return all possible palindrome partitioning of s.For example, given s = "aab",Return [ ["aa","b"],...原创 2014-02-06 14:00:41 · 533 阅读 · 0 评论 -
Combinations
Given two integersnandk. Return all possible combinations ofknumbers out of1, 2, ... , n.ExampleExample 1:Input: n = 4, k = 2Output: [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]Example 2:...原创 2014-12-08 12:58:07 · 527 阅读 · 0 评论 -
Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.Example 1:Input: k = 3...原创 2016-08-07 12:34:38 · 271 阅读 · 0 评论 -
Permutations
Given a collection of numbers, return all possible permutations.For example,[1,2,3] have the following permutations:[1,2,3], [1,3,2], [2,1,3], [2,3,1],[3,1,2], and[3,2,1].思路:经典的backtracking. 不要...原创 2014-01-23 12:55:36 · 590 阅读 · 0 评论 -
Subsets II
Given a collection of integers that might contain duplicates, S, return all possible subsets.Note:Elements in a subset must be in non-descending order. The solution set must not contain dup...原创 2014-02-05 14:59:17 · 490 阅读 · 0 评论 -
Subsets
Given a set of distinct integers, S, return all possible subsets.Note:Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets.For example,If...原创 2014-02-04 14:26:14 · 662 阅读 · 0 评论 -
Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:"((()))", "(()())", "(())()", "()(())", "()()(...原创 2014-01-22 13:37:44 · 531 阅读 · 0 评论 -
N-Queens II
Follow up for N-Queens problem.Now, instead outputting board configurations, return the total number of distinct solutions.Show TagsHave you met this question in a real int原创 2014-12-08 09:54:02 · 505 阅读 · 0 评论 -
Generalized Abbreviation
Write a function to generate the generalized abbreviations of a word.Example:Given word = "word", return the following list (order does not matter):["word", "1ord", "w1rd", "wo1d", "wor1", "原创 2016-10-10 06:57:35 · 287 阅读 · 0 评论 -
Binary Watch
A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom represent the minutes (0-59).Each LED represents a zero or one, with the least significant bit o原创 2016-09-19 06:51:03 · 1985 阅读 · 0 评论 -
Letter Case Permutation
Given a string S, we can transform every letter individually to be lowercase or uppercase to create another string. Return a list of all possible strings we could create.Examples:Input: S = "a1b2...原创 2018-11-19 14:08:27 · 221 阅读 · 0 评论 -
Combination Sum II
Given an arraynumand a numbertarget. Find all unique combinations innumwhere the numbers sum totarget.ExampleExample 1:Input: num = [7,1,2,5,1,6,10], target = 8Output: [[1,1,6],[1,2,5],[...原创 2014-12-08 14:27:11 · 447 阅读 · 0 评论