O(1) - constant time - the number of operations stays about the same
O(logn) - logarithmc time - the number of operations incrases in a log pattern.
O(n) - linear time - the number of operations scales linearly
O(n2) - quadratic time - the number of operations scales with the square
Review from last week: Linked Lists
Let's take a closer look at the SimpleLinkedList from lecture:
publicclassSimpleLinkedListimplementsSimpleList{
privateclassItem{
private Object value; // holds the value of the itemprivate Item next; // tells you what the next value in the list
Item(Object setValue, Item setNext) {
value = setValue;
next = setNext;
}
}
It looks a lot like this:
Problem 1: Linked List Insert
Given the location of the head of a linked list, an (int) index to insert at and a value, insert a new node with the given value at the given position. give runtime as well
publicclassNode{
publicint value;
public Node next;
Node(Node setValue, Node setNext) {
value = setValue;
next = setNext;
}
}
insertAt(Node head, int data, int position) {
}
I want you all to draw out a linked list and show what inserting might look like.
What else can we make with lists?
Linked Lists are really usefull for implementing data structures like stacks and queues
Given a list of words, return the word that occurs most often. Use a map to track the word (key), and the number of occourences you see of it(value).
public String mostCommonWord(List<String> words){
Map<String, Integer> frequencyMap;
// TODO: YOUR CODE HERE
}
Hint: use put(), get(), and containsKey() (ie. read the doccumentation)
Example 3: Trick or Treat
Given a map of houses and the number of trick-or-treeters visited them last year (given as a map), return the name of the House you and your friends should visit first. Assume houses with the most visitors have the best candy, so you guys want to visit the most popular house.
public String mostPopularHouse(Map<String, Integer> visitorsByHouse){
}
@OverridepublicinthashCode(){
return Objects.hash(x, y, z);
}
(From Lecture) A hash function is any function that can be used to map data of arbitrary size to fixed-size values.
Every input in a hash function should produce a unique hash, otherwise, we have a hash collision
Birthday Paradox
The larger the hash, the less likley we are to have hash collisions (exponential)
For refference, Git uses a 160 bit hash function
Solutions
(no peeking, it's for your own good!)
(no seriously, attempt the problems with your groupmates first!)
1)
voidinsertAt(Node head, int data, int position){
Node current = head;
for (int i = 0; i < position - 1; i++) {
current = current.next;
}
Node newNode = new Node(data, current.next);
current.next = newNode;
}
2)
import java.util.Map;
import java.util.HashMap;
import java.util.List;
public String mostCommonWord(List<String> words){
Map<String, Integer> frequencyMap = new HashMap<>();
String commonWord = "";
int max = 0;
for (String word: words) {
int val = 1;
if (frequencyMap.containsKey(word)) {
val += frequencyMap.get(word);
}
frequencyMap.put(word, 1);
if (val > max) {
max = val;
commonWord = word;
}
}
return commonWord;
}
3)
public String mostPopularHouse(Map<String, Integer> visitorsByHouse){
String mostPopular = "";
int maxVisitors = 0;
for(Map.Entry<String, Integer> house : visitorsByHouse.entrySet()) {
int visitors = house.getValue();
if(visitors > maxVisitors) {
maxVisitors = visitors;
mostPopular = house.getKey();
}
}
}