Skip to content

leetcode: daily solved tasks #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions src/main/java/com/leetcode/array/PossibleRotatedMatrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.leetcode.array;

final class PossibleRotatedMatrix {
private PossibleRotatedMatrix() {
}

static boolean findRotation(int[][] mat, int[][] target) {
return isEqual(mat, target) || isFlipped(mat, target)
|| isTurnedClockwise(mat, target)
|| isTurnedClockwise(target, mat);
}

private static boolean isEqual(int[][] mat, int[][] target) {
int n = mat.length;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (mat[i][j] != target[i][j]) {
return false;
}
}
}
return true;
}

private static boolean isFlipped(int[][] mat, int[][] target) {
int n = mat.length;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (mat[i][j] != target[n - 1 - i][n - 1 - j]) {
return false;
}
}
}
return true;
}

private static boolean isTurnedClockwise(int[][] m, int[][] t) {
int n = m.length;
int sideSize = n;
for (int i = 0; i < n / 2; i++) {
for (int j = i; j < sideSize; j++) {
if (m[i][j] != t[j][n - 1 - i]) {
return false;
}
if (m[j][n - 1 - i] != t[n - 1 - i][n - 1 - j]) {
return false;
}

if (m[n - 1 - i][j] != t[j][i]) {
return false;
}

if (m[j][i] != t[i][n - 1 - j]) {
return false;
}
}
sideSize--;
}
return true;
}
}
23 changes: 23 additions & 0 deletions src/main/java/com/leetcode/array/SmallestRange2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.leetcode.array;

import java.util.Arrays;

final class SmallestRange2 {
private SmallestRange2() {

}

static int smallestRangeII(int[] nums, int k) {
Arrays.sort(nums);
int bestScore = nums[nums.length - 1] - nums[0];
int firstPlus = nums[0] + k;
int lastMinus = nums[nums.length - 1] - k;
for (int i = 0; i < nums.length - 1; i++) {
int lastPlus = nums[i] + k;
int firstMinus = nums[i + 1] - k;
int score = Math.max(lastPlus, lastMinus) - Math.min(firstMinus, firstPlus);
bestScore = Math.min(score, bestScore);
}
return bestScore;
}
}
40 changes: 40 additions & 0 deletions src/main/java/com/leetcode/impl/AuthenticationManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.leetcode.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

final class AuthenticationManager {
private final Map<String, Integer> storage;
private int timeToLive;

AuthenticationManager(int timeToLive) {
this.timeToLive = timeToLive;
this.storage = new HashMap<>();
}

public void generate(String tokenId, int currentTime) {
this.storage.putIfAbsent(tokenId, currentTime);
}

public void renew(String tokenId, int currentTime) {
Integer tokenStartTime = storage.get(tokenId);
if (tokenStartTime != null && tokenStartTime + timeToLive < currentTime) {
storage.put(tokenId, currentTime);
}
}

public int countUnexpiredTokens(int currentTime) {
int count = 0;
Iterator<Map.Entry<String, Integer>> it = storage.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, Integer> kv = it.next();
if (kv.getValue() + timeToLive <= currentTime) {
it.remove();
} else {
count++;
}
}
return count;
}
}
14 changes: 14 additions & 0 deletions src/main/java/com/leetcode/impl/CircularGame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.leetcode.impl;

final class CircularGame {
private CircularGame() {
}

static int findTheWinner(int n, int k) {
if (n == 1) {
return 1;
} else {
return (findTheWinner(n - 1, k) + k - 1) % n;
}
}
}
55 changes: 55 additions & 0 deletions src/main/java/com/leetcode/intervals/InsertInterval.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.leetcode.intervals;

import java.util.ArrayList;
import java.util.List;

final class InsertInterval {
private InsertInterval() {
}

@SuppressWarnings("squid:S3012")
static int[][] insert(int[][] intervals, int[] newInterval) {
List<int[]> res = new ArrayList<>();
int i = 0;
while (i < intervals.length && !isOverlap(intervals[i], newInterval)) {
res.add(intervals[i]);
i++;
}
if (i == intervals.length) {
if (intervals.length == 0) {
res.add(newInterval);
} else {
int j = 0;
while (j < intervals.length && intervals[j][0] < newInterval[0]) {
j++;
}
res.add(j, newInterval);
}
}

if (i < intervals.length) {
int[] mergedInterval = new int[2];
mergedInterval[0] = Math.min(newInterval[0], intervals[i][0]);
mergedInterval[1] = Math.max(newInterval[1], intervals[i][1]);
i++;
while (i < intervals.length && isOverlap(intervals[i], mergedInterval)) {
mergedInterval[1] = Math.max(intervals[i][1], mergedInterval[1]);
i++;
}
res.add(mergedInterval);
}
while (i < intervals.length) {
res.add(intervals[i]);
i++;
}
return res.toArray(int[][]::new);
}

private static boolean isOverlap(int[] i1, int[] i2) {
if (i1[1] >= i2[0] && i1[1] <= i2[1]) {
return true;
}
return i2[1] >= i1[0] && i2[1] <= i1[1];
}
}

73 changes: 73 additions & 0 deletions src/main/java/com/leetcode/list/MyCircularQueue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.leetcode.list;

final class MyCircularQueue {
private final int maxSize;
private ListNode head;
private ListNode tail;
private int size = 0;

MyCircularQueue(int k) {
this.head = null;
this.tail = null;
this.maxSize = k;
}

public boolean enQueue(int value) {
if (!isFull()) {
addAtTail(value);
return true;
}
return false;
}

public boolean deQueue() {
if (!isEmpty()) {
if (head == tail) {
head = null;
tail = null;
} else {
head = head.next;
}
size--;
return true;
}
return false;
}

@SuppressWarnings( {"checkstyle:MethodName", "squid:S100"})
public int Front() {
if (head != null) {
return head.val;
}
return -1;
}

@SuppressWarnings( {"checkstyle:MethodName", "squid:S100"})
public int Rear() {
if (tail != null) {
return tail.val;
}
return -1;
}

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

public boolean isFull() {
return size == maxSize;
}


private void addAtTail(int val) {
ListNode newNode = new ListNode(val);
if (tail == null) {
tail = newNode;
head = tail;
} else {
tail.next = newNode;
tail = newNode;
}
size++;
}
}
118 changes: 118 additions & 0 deletions src/main/java/com/leetcode/list/MyLinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package com.leetcode.list;

final class MyLinkedList {
private Node head;
private Node tail;
private int size = 0;

MyLinkedList() {
this.head = null;
this.tail = null;
}

int get(int index) {
if (index > -1 && index < size) {
Node it = head;
int i = 0;
while (it != null) {
if (i == index) {
return it.val;
}
i++;
it = it.next;
}
}
return -1;
}

public void addAtHead(int val) {
Node newNode = new Node(val);
if (head == null) {
head = newNode;
tail = head;
} else {
newNode.next = head;
head = newNode;
}
size++;
}

public void addAtTail(int val) {
Node newNode = new Node(val);
if (tail == null) {
tail = newNode;
head = tail;
} else {
tail.next = newNode;
tail = newNode;
}
size++;
}

public void addAtIndex(int index, int val) {
if (index > 0 && index < size) {
Node newNode = new Node(val);
int i = 0;
Node it = head;
while (i < index) {
if (i + 1 == index) {
newNode.next = it.next;
it.next = newNode;
size++;
break;
}
i++;
it = it.next;
}
}
if (index == 0) {
addAtHead(val);
}
if (index == size) {
addAtTail(val);
}
}

public void deleteAtIndex(int index) {
if (index > 0 && index < size) {
int i = 0;
Node it = head;
while (i < index) {
if (i + 1 == index) {
Node next = it.next;
it.next = next.next;
if (next == tail) {
tail = it;
}
size--;
break;
}
i++;
it = it.next;
}
}
if (index == 0 && size > 0) {
removeHead();
}
}

private void removeHead() {
if (head == tail) {
head = null;
tail = null;
} else {
head = head.next;
}
size--;
}


private static final class Node {
private final int val;
private Node next;

private Node(int val) {
this.val = val;
}
}
}
Loading