Press "Enter" to skip to content

Posts published in August 2018

花花酱 LeetCode 888. Uncommon Words from Two Sentences

Problem

We are given two sentencesĀ AĀ andĀ B.Ā  (AĀ sentenceĀ is a string of space separated words.Ā  EachĀ wordĀ consists only of lowercase letters.)

A word isĀ uncommonĀ if it appears exactly once in one of the sentences, and does not appear in the other sentence.

Return a list of all uncommon words.

You may return the list in any order.

Example 1:

Input: A = "this apple is sweet", B = "this apple is sour"
Output: ["sweet","sour"]

Example 2:

Input: A = "apple apple", B = "banana"
Output: ["banana"]

Note:

  1. 0 <= A.length <= 200
  2. 0 <= B.length <= 200
  3. AĀ andĀ BĀ both contain only spaces and lowercase letters.

Solution: HashTable

Time complexity: O(m+n)

Space complexity: O(m+n)

C++

 

花花酱 LeetCode 707. Design Linked List

Problem

Design yourĀ implementation of the linked list. You can choose to use the singly linked list or the doubly linked list. A node in a singlyĀ linked list should have two attributes:Ā valĀ andĀ next.Ā valĀ is the value of the current node, andĀ nextĀ isĀ aĀ pointer/reference to the next node. If you want to use the doubly linked list,Ā you will needĀ one more attributeĀ prevĀ to indicate the previous node in the linked list. Assume all nodes in the linked list are 0-indexed.

Implement these functions in your linked list class:

  • get(index) : Get the value ofĀ theĀ index-thĀ node in the linked list. If the index is invalid, returnĀ -1.
  • addAtHead(val) : Add a node of valueĀ valĀ before the first element of the linked list. After the insertion, the new node will be the first node of the linked list.
  • addAtTail(val) : Append a node of valueĀ valĀ to the last element of the linked list.
  • addAtIndex(index, val) : Add a node of valueĀ valĀ before theĀ index-thĀ node in the linked list.Ā IfĀ indexĀ equalsĀ to the length ofĀ linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted.
  • deleteAtIndex(index) : DeleteĀ theĀ index-thĀ node in the linked list, if the index is valid.

Example:

Note:

  • All values will be in the range ofĀ [1, 1000].
  • The number of operations will be in the range ofĀ [1, 1000].
  • Please do not use the built-in LinkedList library.




Solution: Single linked list

Keep tracking head and tail of the list.

Time Complexity:

addAtHead, addAtTail O(1)

addAtIndex O(index)

deleteAtIndex O(index)

Space complexity: O(1)

C++

Tracking head/tail and size of the list.

v2

Python3

Java

 

花花酱 LeetCode 638. Shopping Offers

Problem

In LeetCode Store, there are some kinds of items to sell. Each item has a price.

However, there are some special offers, and a special offer consists of one or more different kinds of items with a sale price.

You are given the each item’s price, a set of special offers, and the number we need to buy for each item. The job is to output the lowest price you have to pay forĀ exactlyĀ certain items as given, where you could make optimal use of the special offers.

Each special offer is represented in the form of an array, the last number represents the price you need to pay for this special offer, other numbers represents how many specific items you could get if you buy this offer.

You could use any of special offers as many times as you want.

Example 1:

Input: [2,5], [[3,0,5],[1,2,10]], [3,2]
Output: 14
Explanation: 
There are two kinds of items, A and B. Their prices are $2 and $5 respectively. 
In special offer 1, you can pay $5 for 3A and 0B
In special offer 2, you can pay $10 for 1A and 2B. 
You need to buy 3A and 2B, so you may pay $10 for 1A and 2B (special offer #2), and $4 for 2A.

Example 2:

Input: [2,3,4], [[1,1,0,4],[2,2,1,9]], [1,2,1]
Output: 11
Explanation: 
The price of A is $2, and $3 for B, $4 for C. 
You may pay $4 for 1A and 1B, and $9 for 2A ,2B and 1C. 
You need to buy 1A ,2B and 1C, so you may pay $4 for 1A and 1B (special offer #1), and $3 for 1B, $4 for 1C. 
You cannot add more items, though only $9 for 2A ,2B and 1C.

Note:

  1. There are at most 6 kinds of items, 100 special offers.
  2. For each item, you need to buy at most 6 of them.
  3. You areĀ notĀ allowed to buy more items than you want, even if that would lower the overall price.

Solution: Search

Try all combinations.

 

花花酱 LeetCode 592. Fraction Addition and Subtraction

Problem

Given a string representing an expression of fraction addition and subtraction, you need to return the calculation result in string format. The final result should beĀ irreducible fraction. If your final result is an integer, sayĀ 2, you need to change it to the format of fraction that has denominatorĀ 1. So in this case,Ā 2Ā should be converted toĀ 2/1.

Example 1:

Input:"-1/2+1/2"
Output: "0/1"

Example 2:

Input:"-1/2+1/2+1/3"
Output: "1/3"

Example 3:

Input:"1/3-1/2"
Output: "-1/6"

Example 4:

Input:"5/3+1/3"
Output: "2/1"

Note:

  1. The input string only containsĀ '0'Ā toĀ '9',Ā '/',Ā '+'Ā andĀ '-'. So does the output.
  2. Each fraction (input and output) has formatĀ Ā±numerator/denominator. If the first input fraction or the output is positive, thenĀ '+'Ā will be omitted.
  3. The input only contains validĀ irreducible fractions, where theĀ numeratorĀ andĀ denominatorĀ of each fraction will always be in the range [1,10]. If the denominator is 1, it means this fraction is actually an integer in a fraction format defined above.
  4. The number of given fractions will be in the range [1,10].
  5. The numerator and denominator of theĀ final resultĀ are guaranteed to be valid and in the range of 32-bit int.

Solution: Math

a/b+c/d = (a*d + b * c) / (b * d)

Time complexity: O(n)

Space complexity: O(1)

C++

C++/class