Useful Notes

Before practicing in Leetcode and taking up any contest or interviews I refer to the below notes.

These notes contain basic details such as how to declare, initiate, access(add/remove), and iterate arrays.

๐€๐ซ๐ซ๐š๐ฒ๐ฌ

stores values of the same data type. Address of elements are stored consecutively in memory
length is fixed. inserting or deleting an element in middle is not easy.

๐ƒ๐ž๐œ๐ฅ๐š๐ซ๐š๐ญ๐ข๐จ๐ง:

int[] nums = {1,2,4} //initializing with values
int[] nums = new int[10] //initializing with size. 0 will be initialized in all indexes if we declare without variables.

๐€๐œ๐œ๐ž๐ฌ๐ฌ๐ข๐ง๐  ๐š๐ง ๐ž๐ฅ๐ž๐ฆ๐ž๐ง๐ญ:

Array index starts from 0โ€ฆlength-1
int[] nums = {1,2,4}
nums[0] โ€” 1
nums[1] โ€” 2
nums[2] โ€” 4

๐’๐จ๐ซ๐ญ๐ข๐ง๐  ๐š๐ง ๐ž๐ฅ๐ž๐ฆ๐ž๐ง๐ญ:
๐€๐ซ๐ซ๐š๐ฒ๐ฌ.๐ฌ๐จ๐ซ๐ญ(๐ง๐ฎ๐ฆ๐ฌ);

Filling array element with some values:

Arrays.fill(nums, -1) โ†’ this will assign all values in the array as -1

Binary search โ€” Arrays.binarySearch(array,value to find) โ†’ returns index

๐‹๐ข๐ฌ๐ญ:

size is dynamic

  1. ๐€๐ซ๐ซ๐š๐ฒ๐‹๐ข๐ฌ๐ญ & ๐‹๐ข๐ง๐ค๐ž๐ ๐‹๐ข๐ฌ๐ญ

They store in the order we provide and it can have duplicates

List a1 = new ArrayList();
a1.add(โ€œZaraโ€);
a1.add(โ€œMahnazโ€);
a1.add(โ€œAyanโ€);
System.out.println(โ€œ ArrayList Elementsโ€);
System.out.print(โ€œ\tโ€ + a1);

List l1 = new LinkedList<>();
l1.add(โ€œZaraโ€);
l1.add(โ€œMahnazโ€);
l1.add(โ€œAyanโ€);
System.out.println();
System.out.println(โ€œ LinkedList Elementsโ€);
System.out.print(โ€œ\tโ€ + l1);

๐จ๐ฎ๐ญ๐ฉ๐ฎ๐ญ:

ArrayList Elements
[Zara, Mahnaz, Ayan]
LinkedList Elements
[Zara, Mahnaz, Ayan]

๐ƒ๐ข๐Ÿ๐Ÿ๐ž๐ซ๐ž๐ง๐œ๐ž ๐›๐ž๐ญ๐ฐ๐ž๐ž๐ง ๐€๐ซ๐ซ๐š๐ฒ ๐‹๐ข๐ฌ๐ญ & ๐‹๐ข๐ง๐ค๐ž๐ ๐‹๐ข๐ฌ๐ญ:

Array List stores values sequentially, so if we add or remove a value it needs more time to shift its elements and adjust

LinkedList stores randomly in memory and links by pointers. inserting and deleting takes less time here

๐ฆ๐ž๐ญ๐ก๐จ๐๐ฌ -

add(value), add(index, value),remove(value),get(key),
contains(value) returns true or false
isEmpty() return true or false,
clear(),
size() returns int
removeLast()
removeFirst()

#dsa #dsacoding #leetcode #cp

1 Like

๐‘ช๐’๐’๐’•๐’Š๐’๐’–๐’‚๐’•๐’Š๐’๐’ ๐’•๐’ ๐’๐’‚๐’”๐’• ๐’‘๐’๐’”๐’•โ€ฆ
๐‘ป๐’๐’‘๐’Š๐’„๐’”: ๐‘บ๐’†๐’•,๐‘ด๐’‚๐’‘๐’”, ๐‘บ๐’•๐’‚๐’„๐’Œ ๐’‚๐’๐’… ๐‘ธ๐’–๐’†๐’–๐’†

๐’๐ž๐ญ:

Set canโ€™t have duplicates

Hashset โ€” stores in order we give
Treeset โ€” stores in asc order

๐ƒ๐ž๐œ๐ฅ๐š๐ญ๐š๐ญ๐ข๐จ๐ง ๐š๐ง๐ ๐ข๐ง๐ข๐ญ๐ข๐š๐ฅ๐ข๐ฌ๐š๐ญ๐ข๐จ๐ง:

Set set = new HashSet<>();

set.add(5);
set.add(18);

๐ฆ๐ž๐ญ๐ก๐จ๐๐ฌ -
add(value) โ€” returns t/f,
remove(value),
contains(value) โ€” returns t/f,
isEmpty() return true or false,
clear(),
size() returns int

๐Œ๐š๐ฉ๐ฌ:

stores unique key, value pair
Hash Map โ€” stores in order we provide
Tree Map โ€” stores in keyโ€™s ascending order
Map<String,String> m1 = new HashMap();
m1.put(โ€œZaraโ€, โ€œ8โ€);
m1.put(โ€œMahnazโ€, โ€œ31โ€);
m1.put(โ€œAyanโ€, โ€œ12โ€);
m1.get(โ€œZaraโ€);

๐Œ๐ž๐ญ๐ก๐จ๐๐ฌ:
-put(key,value), get(key)
-containsKey(key) โ€” returns true or false
-containsValue(value) โ€” returns true or false
-isEmpty() โ€” returns True or false
-size() returns int
-values() returns list of values
-keys() returns list of keys
-putIfAbsent(key,new value)
-getOrDefault(existing,inititalize value)
-ceilingKey(key) โ†’ gives greater element to the key
-lowerKey or floorkey(key) โ†’ gives smaller element to the key

Iterating hash map

for(int value : map.values()){
}
for(int key : map.keys()){
}
for(Map.Entry<String,String> entry : map.entrySet()){
System.out.println(โ€œKey = โ€œ + entry.getKey() +
โ€œ, Value = โ€œ + entry.getValue());
}

๐’๐ญ๐š๐œ๐ค:

Last In First Out policy

Stack st = new Stack();

๐ฆ๐ž๐ญ๐ก๐จ๐๐ฌ:
โ€” push(), pop()
โ€” size(), empty()
-contains(value) returns true or false
โ€” stack.remove(index)

๐๐ฎ๐ž๐ฎ๐ž: (Interface and not class)

FIFO policy

  1. Queue ll = new LinkedList(); //stores in order we insert

  2. Queue pQueue = new PriorityQueue(); //it stores in Asc order. This can also be used as Min heap

  3. Queue pQueue = new PriorityQueue((a,b) โ†’ b-a); //it stores in desc order. This can also be used as max heap

๐Œ๐ž๐ญ๐ก๐จ๐๐ฌ:
offer(element) used to initialize queue
add(element),

remove(element) /poll() returns top element
peek()/element() returns top element that will be polled

You should add a priority queue, its very useful.