Skip to content

Commit

Permalink
algoJava: c1 s3 e42
Browse files Browse the repository at this point in the history
  • Loading branch information
AliNisarAhmed committed Apr 8, 2024
1 parent 6a3d379 commit ac674a2
Show file tree
Hide file tree
Showing 9 changed files with 404 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.ali.algojava.chapter1.section3;

// 1.3.38
public class GeneralizedQueueArray<Item> {

private Item[] items;
private int first = 0;
private int last = 0;
private int size = 0;

public GeneralizedQueueArray() {
items = (Item[]) new Object[2];
}

public boolean isEmpty() {
return size == 0;
}

public int size() {
return size;
}

public void insert(Item x) {
if (last == size) {
resize(items.length * 2);
}

items[last] = x;
last++;
size++;
}

public Item delete(int k) {

int index = (first + k) % items.length;
Item result = items[index];
items[index] = null;

size--;

if (index != last) {
for (int i = index; i < last; i++) {
items[i] = items[i + 1];
}
}

last--;
if (size > 0 && size < items.length / 4) {
resize(items.length / 2);
}

return result;
}

private void resize(int newSize) {
Item[] copy = (Item[]) new Object[newSize];
for (int i = 0; i < size; i++) {
Item item = items[(first + i) % items.length];
if (item == null) {
continue;
}
copy[i] = item;
}
items = copy;
first = 0;
last = size;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.ali.algojava.chapter1.section3;

// 1.3.38
public class GeneralizedQueueLinkedList<Item> {

private Node first;
private int size;

public class Node {
public Item item;
public Node next;

public Node(Item item) {
this.item = item;
}
}

public boolean isEmpty() {
return size == 0;
}

public int size() {
return size;
}

public void insert(Item x) {
Node newNode = new Node(x);
if (isEmpty()) {
first = newNode;
} else {
newNode.next = first;
first = newNode;
}
size++;
}

public Item delete(int k) {
if (isEmpty()) {
throw new RuntimeException("queue is empty");
}

if (k >= size) {
throw new RuntimeException("not enough items");
}

Item result = null;
int target = size - k - 1;
if (size == 1 || target == 0) {
result = first.item;
first = first.next;
} else {
int index = 0;
Node current = first;

while (index < target - 1) {
current = current.next;
index++;
}

result = current.next.item;
current.next = current.next.next;
}

size--;
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.ali.algojava.chapter1.section3;

import edu.princeton.cs.algs4.StdOut;
import java.util.Arrays;

// 1.3.37
// eliminate every Mth person from a circle of N persons
public class Josephus {
public static void main(String[] args) {
int M = 2;
int N = 7;
if (args.length == 2) {
M = Integer.parseInt(args[0]);
N = Integer.parseInt(args[1]);
}
int[] output = josephus(M, N);
StdOut.println(Arrays.toString(output));
}

private static int[] josephus(int M, int N) {
int[] result = new int[N];
Queue<Integer> q = new Queue<>();
for (int i = 1; i <= N; i++) {
q.enqueue(i);
}

for (int i = 0; i < N; i++) {

for (int j = 1; j < M; j++) {
q.enqueue(q.dequeue());
}

result[i] = q.dequeue();
}

return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.ali.algojava.chapter1.section3;

// 1.3.40
public class MoveToFront<Item> {

private Node first;
private int size;

public class Node {
public Item item;
public Node next;
public Node prev;

public Node(Item item) {
this.item = item;
}
}

public int size() {
return size;
}

public boolean isEmpty() {
return size == 0;
}

public void insert(Item item) {
Node newNode = new Node(item);
if (!isEmpty()) {
newNode.next = first;
first.prev = newNode;
}
first = newNode;
size++;
if (size > 1) {
removeOtherOccurences(item);
}
}

public Item peek() {
return first == null ? null : first.item;
}

private void removeOtherOccurences(Item item) {
Node current = first.next;
while (current != null) {
if (current.item == item) {
current.prev.next = current.next;
if (current.next != null) {
current.next.prev = current.prev;
}
size--;
}
current = current.next;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@ public int size() {
return N;
}

public Queue() {
first = null;
last = null;
N = 0;
}

// 1.3.41
public Queue(Queue<Item> q) {
int size = q.size();

while (size > 0) {
Item item = q.dequeue();
enqueue(item);
q.enqueue(item);
size--;
}
}

public void enqueue(Item item) {
Node oldLast = last;
last = new Node();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
package com.ali.algojava.chapter1.section3;

import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.StdRandom;
import java.util.Arrays;
import java.util.Iterator;

// 1.3.35
public class RandomQueue<Item> {
// 1.3.35 && 1.3.36
public class RandomQueue<Item> implements Iterable<Item> {
private Item[] items;
private int size;

private static String[] cards = new String[] {
"sA", "sK", "sQ", "sJ", "s10", "s9", "s8", "s7", "s6", "s5", "s4",
"s3", "s2", "hA", "hK", "hQ", "hJ", "h10", "h9", "h8", "h7", "h6",
"h5", "h4", "h3", "h2", "dA", "dK", "dQ", "dJ", "d10", "d9", "d8",
"d7", "d6", "d5", "d4", "d3", "d2", "cA", "cK", "cQ", "cJ", "c10",
"c9", "c8", "c7", "c6", "c5", "c4", "c3", "c2" };

public RandomQueue() {
items = (Item[]) new Object[10];
size = 0;
Expand Down Expand Up @@ -52,4 +62,64 @@ private void resize(int newSize) {
}
items = newArr;
}

@Override
public Iterator<Item> iterator() {
return new RandomQueueIterator();
}

private class RandomQueueIterator implements Iterator<Item> {
private int index = 0;
private Item[] arr;

public RandomQueueIterator() {
arr = (Item[]) new Object[size];
for (int i = 0; i < size; i++) {
arr[i] = items[i];
}
StdRandom.shuffle(arr);
}

@Override
public Item next() {
Item result = arr[index];
index++;
return result;
}

@Override
public boolean hasNext() {
return index < size;
}
}

// 1.3.37
public static void main(String[] args) {
RandomQueue<String> q = new RandomQueue<>();
for (String card : cards) {
q.enqueue(card);
}

String[] hand1 = new String[13];
String[] hand2 = new String[13];
String[] hand3 = new String[13];
String[] hand4 = new String[13];

for (int i = 1; i < 53; i++) {
if (i <= 13) {
hand1[i - 1] = q.dequeue();
} else if (i > 13 && i <= 26) {
hand2[(i - 1) % 13] = q.dequeue();
} else if (i > 26 && i <= 39) {
hand3[(i - 1) % 13] = q.dequeue();
} else {
hand4[(i - 1) % 13] = q.dequeue();
}
}

StdOut.println("hand1: " + Arrays.toString(hand1));
StdOut.println("hand2: " + Arrays.toString(hand2));
StdOut.println("hand3: " + Arrays.toString(hand3));
StdOut.println("hand4: " + Arrays.toString(hand4));
}
}
Loading

0 comments on commit ac674a2

Please sign in to comment.