Press "Enter" to skip to content

Huahua's Tech Road

花花酱 LeetCode 124. Binary Tree Maximum Path Sum

Problem:

Given a binary tree, find the maximum path sum.

For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must contain at least one node and does not need to go through the root.

For example:
Given the below binary tree,

Return 6.



Idea:

Recursion

Time complexity O(n)

Space complexity O(h)

Solution: Recursion

C++

Python3

Related Problems:

花花酱 LeetCode 37. Sudoku Solver

Problem:

Write a program to solve a Sudoku puzzle by filling the empty cells.

Empty cells are indicated by the character '.'.

You may assume that there will be only one unique solution.

A sudoku puzzle…

…and its solution numbers marked in red.

Idea:

DFS + backtracking

Solution:

C++

 

Related Problems:

花花酱 LeetCode 40. Combination Sum II

Problem:

Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.

Each number in C may only be used once in the combination.

Note:

  • All numbers (including target) will be positive integers.
  • The solution set must not contain duplicate combinations.

For example, given candidate set [10, 1, 2, 7, 6, 1, 5] and target 8,
A solution set is:

 



Idea:

DFS

Solution:

C++ / set

 

C++ / vector

 

花花酱 LeetCode 72. Edit Distance

Problem:

Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)

You have the following 3 operations permitted on a word:

a) Insert a character
b) Delete a character
c) Replace a character

Idea:

Dynamic Programming

Solution:

Recursive

Iterative

 

花花酱 LeetCode 57. Insert Interval

Problem:

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).

You may assume that the intervals were initially sorted according to their start times.

Example 1:
Given intervals [1,3],[6,9], insert and merge [2,5] in as [1,5],[6,9].

Example 2:
Given [1,2],[3,5],[6,7],[8,10],[12,16], insert and merge [4,9] in as [1,2],[3,10],[12,16].

This is because the new interval [4,9] overlaps with [3,5],[6,7],[8,10].



Idea:

Find the position of the new interval, insert it into the list and call MergeIntervals in LeetCode 56

Solution:

C++

 

Python

 

Solution 2:

C++

 

Python

 

Related problems: