Press "Enter" to skip to content

Posts published in “Medium”

花花酱 LeetCode 729. My Calendar I

Problem:

Implement aĀ MyCalendarĀ class to store your events. A new event can be added if adding the event will not cause a double booking.

Your class will have the method,Ā book(int start, int end). Formally, this represents a booking on the half open intervalĀ [start, end), the range of real numbersĀ xĀ such thatĀ start <= x < end.

AĀ double bookingĀ happens when two events have some non-empty intersection (ie., there is some time that is common to both events.)

For each call to the methodĀ MyCalendar.book, returnĀ trueĀ if the event can be added to the calendar successfully without causing a double booking. Otherwise, returnĀ falseĀ and do not add the event to the calendar.

Your class will be called like this:Ā MyCalendar cal = new MyCalendar();Ā MyCalendar.book(start, end)

Example 1:

Note:

 

  • The number of calls toĀ MyCalendar.bookĀ per test case will be at mostĀ 1000.
  • In calls toĀ MyCalendar.book(start, end),Ā startĀ andĀ endĀ are integers in the rangeĀ [0, 10^9].

 



Idea:Ā 

Binary Search

Solution1:

Brute Force: O(n^2)

C++

Solution 2:

Binary Search O(nlogn)

C++

Java

 

Related Problems:

花花酱 LeetCode 725. Split Linked List in Parts

Problem:

Given a (singly) linked list with head nodeĀ root, write a function to split the linked list intoĀ kĀ consecutive linked list “parts”.

The length of each part should be as equal as possible: no two parts should have a size differing by more than 1. This may lead to some parts being null.

The parts should be in order of occurrence in the input list, and parts occurring earlier should always have a size greater than or equal parts occurring later.

Return a List of ListNode’s representing the linked list parts that are formed.

Examples 1->2->3->4, k = 5 // 5 equal parts [ [1], [2], [3], [4], null ]

Example 1:

Example 2:

Note:

  • The length ofĀ rootĀ will be in the rangeĀ [0, 1000].
  • Each value of a node in the input will be an integer in the rangeĀ [0, 999].
  • kĀ will be an integer in the rangeĀ [1, 50].



Idea:
List + Simulation
Solution:
C++

Java

Python

 

花花酱 LeetCode 98. Validate Binary Search Tree

Problem:

Given a binary tree, determine if it is a valid binary search tree (BST).

Assume a BST is defined as follows:

  • The left subtree of a node contains only nodes with keysĀ less thanĀ the node’s key.
  • The right subtree of a node contains only nodes with keysĀ greater thanĀ the node’s key.
  • Both the left and right subtrees must also be binary search trees.

Example 1:

Binary treeĀ [2,1,3], return true.

Example 2:

Binary treeĀ [1,2,3], return false.

Solution 1

Traverse the tree and limit the range of each subtree and check whether root’s value is in the range.

Time complexity: O(n)

Space complexity: O(n)

Note: in order to cover the range of -2^31 ~ 2^31-1, we need to use long or nullable integer.

C++/long

C++/nullable

Java/nullable

Solution 2

Do an in-order traversal, the numbers should be sorted, thus we only need to compare with the previous number.

Time complexity: O(n)

Space complexity: O(n)

C++

Java

Related Problem

花花酱 LeetCode 91. Decode Ways

é¢˜ē›®å¤§ę„ļ¼š

ē»™ä½ äø€äøŖåŠ åÆ†ēš„ę•°å­—å­—ē¬¦äø²ļ¼Œé—®ä½ äø€å…±ęœ‰å¤šå°‘ē§äøåŒēš„č§£åÆ†ę–¹å¼ć€‚

Problem:

A message containing letters fromĀ A-ZĀ is being encoded to numbers using the following mapping:

‘A’ -> 1

‘B’ -> 2

‘Z’ -> 26

Given an encoded message containing digits, determine the total number of ways to decode it.

For example,
Given encoded messageĀ "12", it could be decoded asĀ "AB"Ā (1 2) orĀ "L"Ā (12).

The number of ways decodingĀ "12"Ā is 2.

 



Idea:

Dynamic Programming

Solution:

C++

Time complexity: O(n^2)

Space complexity: O(n^2)

 

C++

Time complexity: O(n)

Space complexity: O(n)

 

 

 

C++

Time complexity: O(n)

Space complexity: O(1)

 

Related Problems:

花花酱 LeetCode 216. Combination Sum III

é¢˜ē›®å¤§ę„ļ¼šč¾“å‡ŗę‰€ęœ‰ē”ØkäøŖę•°ēš„å’Œäøŗnēš„ē»„åˆć€‚åÆä»„ä½æē”Øēš„å…ƒē“ ę˜Æ1到9怂

Problem:

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,Ā nĀ = 7

Output:

Example 2:

Input:Ā kĀ = 3,Ā nĀ = 9

Output:

 



Idea:

DFS + backtracking

bit

Solution:

C++

 

C++ / binary

 

Python

 

 

Related problems: