MrainW's Home

All things come to those who wait!

0%

Question

Given a characters array tasks, representing the tasks a CPU needs to do, where each letter represents a different task. Tasks could be done in any order. Each task is done in one unit of time. For each unit of time, the CPU could complete either one task or just be idle.

However, there is a non-negative integer n that represents the cooldown period between two same tasks (the same letter in the array), that is that there must be at least n units of time between any two same tasks.

Return the least number of units of times that the CPU will take to finish all the given tasks.

https://leetcode.com/problems/task-scheduler/

  • Solution1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {

public int leastInterval(char[] tasks, int n) {
int[] record = new int[26];
for(char a : tasks){
record[a-'A']++;
}
Arrays.sort(record);
int max_n = record[25]-1;
int space = max_n* n;

for(int i = 24; i >=0&& record[i]>0; i--){
space -= Math.min(max_n,record[i]);
}
return space>0?tasks.length+space:tasks.length;

}
}

Complexity:

Time complexity: O(n)

Space complexity: O(1)

Question

Given an integer array nums, move all 0‘s to the end of it while maintaining the relative order of the non-zero elements.

https://leetcode.com/problems/move-zeroes/

  • Solution1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public void moveZeroes(int[] nums) {
if (nums == null || nums.length == 0) return;

int insertPos = 0;
for (int num: nums) {
if (num != 0) nums[insertPos++] = num;
}

while (insertPos < nums.length) {
nums[insertPos++] = 0;
}
}
}

Complexity:

Time complexity: O(n)

Space complexity: O(1)

Question

Write a function that reverses a string. The input string is given as an array of characters s.

https://leetcode.com/problems/reverse-string/

  • Solution1
1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public void reverseString(char[] s) {
int i = 0, j = s.length - 1;
while (i < j){
char tmp = s[i];
s[i] = s[j];
s[j] = tmp;
i++;
j--;
}
return;
}
}

Complexity:

Time complexity: O(n)

Space complexity: O(1)

Question

Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same.

Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array nums. More formally, if there are k elements after removing the duplicates, then the first k elements of nums should hold the final result. It does not matter what you leave beyond the first k elements.

Return k after placing the final result in the first k slots of nums.

Do not allocate extra space for another array. You must do this by modifying the input array in-place with O(1) extra memory.

https://leetcode.com/problems/remove-duplicates-from-sorted-array/

  • Solution1
1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public int removeDuplicates(int[] nums) {
//slow 是下一个要放的位置,比index大1,所以return时不需要加1
int slow = 1;
for (int fast = 1; fast < nums.length; fast++) {
if (nums[fast - 1] != nums[fast]) {
nums[slow++] = nums[fast];
}
}
return slow;
}
}

Complexity:

Time complexity: O(n)

Space complexity: O(1)

Question

Given an integer array nums and an integer k, return the kth largest element in the array.

Note that it is the kth largest element in the sorted order, not the kth distinct element.

https://leetcode.com/problems/kth-largest-element-in-an-array/

  • Solution1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public int findKthLargest(int[] nums, int k) {
//shuffle(nums);
//divide(nums, 0, nums.length - 1, k);
//return nums[nums.length - k];
PriorityQueue<Integer> heap = new PriorityQueue<>();
for (int x : nums){
if (heap.size() < k || x >= heap.peek()){
heap.offer(x);
}
if (heap.size() > k) {
heap.poll();
}
}
return heap.peek();
}

Complexity:

Time complexity: O(nlog(k))

Space complexity: O(k)

Question

Given an array of integers nums and an integer k, return the total number of continuous subarrays whose sum equals to k.

https://leetcode.com/problems/subarray-sum-equals-k/

  • Solution1
1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public int subarraySum(int[] nums, int k) {
Map<Integer, Integer> map = new HashMap<>();
int sum = 0, res = 0;
map.put(0, 1);
for (int num : nums){
sum += num;
if (map.containsKey(sum - k)) res += map.get(sum - k);
map.put(sum, map.getOrDefault(sum, 0) + 1);
}
return res;
}
}

Complexity:

Time complexity: O(n)

Space complexity: O(n)

Question

A linked list of length n is given such that each node contains an additional random pointer, which could point to any node in the list, or null.

Construct a deep copy of the list. The deep copy should consist of exactly n brand new nodes, where each new node has its value set to the value of its corresponding original node. Both the next and random pointer of the new nodes should point to new nodes in the copied list such that the pointers in the original list and copied list represent the same list state. None of the pointers in the new list should point to nodes in the original list.

https://leetcode.com/problems/copy-list-with-random-pointer/

  • Solution1
1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public Node copyRandomList(Node head) {
Map<Node, Node> map = new HashMap<>();
for(Node cur = head; cur != null; cur = cur.next)
map.put(cur, new Node(cur.val));
for (Node cur = head; cur != null; cur = cur.next){
map.get(cur).next = map.get(cur.next);
map.get(cur).random = map.get(cur.random);
}
return map.get(head);
}
}

Complexity:

Time complexity: O(n)

Space complexity: O(n)

Question

Given the heads of two singly linked-lists headA and headB, return the node at which the two lists intersect. If the two linked lists have no intersection at all, return null.

https://leetcode.com/problems/intersection-of-two-linked-lists/

  • Solution1
1
2
3
4
5
6
7
8
9
10
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode a = headA, b = headB;
while(a != b){
a = a == null ? headB : a.next;
b = b == null ? headA : b.next;
}
return a;
}
}

Complexity:

Time complexity: O(n)

Space complexity: O(1)

Question

Given the head of a singly linked list, return true if it is a palindrome.

https://leetcode.com/problems/palindrome-linked-list/

  • Solution1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Solution {
public boolean isPalindrome(ListNode head) {
ListNode fast = head, slow = head;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
}
if (fast != null) { // odd nodes: let right half smaller
slow = slow.next;
}
slow = reverse(slow);
fast = head;

while (slow != null) {
if (fast.val != slow.val) {
return false;
}
fast = fast.next;
slow = slow.next;
}
return true;
}

public ListNode reverse(ListNode head) {
ListNode prev = null;
while (head != null) {
ListNode next = head.next;
head.next = prev;
prev = head;
head = next;
}
return prev;
}
}

Complexity:

Time complexity: O(n)

Space complexity: O(1)

Question

Given the head of a linked list, we repeatedly delete consecutive sequences of nodes that sum to 0 until there are no such sequences.

After doing so, return the head of the final linked list. You may return any such answer.

(Note that in the examples below, all sequences are serializations of ListNode objects.)

https://leetcode.com/problems/remove-zero-sum-consecutive-nodes-from-linked-list/

  • Solution1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public ListNode removeZeroSumSublists(ListNode head) {
int preFix = 0;
ListNode dummy = new ListNode(0);
dummy.next = head;
Map<Integer, ListNode> map = new HashMap<>();
map.put(0, dummy);
for(ListNode i = dummy; i != null; i= i.next){
preFix += i.val;
map.put(preFix, i);
}
preFix = 0;
for(ListNode i = dummy; i != null; i = i.next){
preFix += i.val;
i.next = map.get(preFix).next;
}
return dummy.next;
}
}

Complexity:

Time complexity: O(n)

Space complexity: O(n)