Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/main/java/g1701_1800/s1720_decode_xored_array/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package g1701_1800.s1720_decode_xored_array;

// #Easy #Array #Bit_Manipulation #2022_04_24_Time_1_ms_(100.00%)_Space_57.3_MB_(6.33%)

public class Solution {
public int[] decode(int[] encoded, int first) {
int[] arr = new int[encoded.length + 1];
arr[0] = first;
for (int i = 0; i < encoded.length; i++) {
arr[i + 1] = encoded[i] ^ arr[i];
}
return arr;
}
}
32 changes: 32 additions & 0 deletions src/main/java/g1701_1800/s1720_decode_xored_array/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
1720\. Decode XORed Array

Easy

There is a **hidden** integer array `arr` that consists of `n` non-negative integers.

It was encoded into another integer array `encoded` of length `n - 1`, such that `encoded[i] = arr[i] XOR arr[i + 1]`. For example, if `arr = [1,0,2,1]`, then `encoded = [1,2,3]`.

You are given the `encoded` array. You are also given an integer `first`, that is the first element of `arr`, i.e. `arr[0]`.

Return _the original array_ `arr`. It can be proved that the answer exists and is unique.

**Example 1:**

**Input:** encoded = [1,2,3], first = 1

**Output:** [1,0,2,1]

**Explanation:** If arr = [1,0,2,1], then first = 1 and encoded = [1 XOR 0, 0 XOR 2, 2 XOR 1] = [1,2,3]

**Example 2:**

**Input:** encoded = [6,2,7,3], first = 4

**Output:** [4,2,0,7,4]

**Constraints:**

* <code>2 <= n <= 10<sup>4</sup></code>
* `encoded.length == n - 1`
* <code>0 <= encoded[i] <= 10<sup>5</sup></code>
* <code>0 <= first <= 10<sup>5</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package g1701_1800.s1721_swapping_nodes_in_a_linked_list;

// #Medium #Two_Pointers #Linked_List #2022_04_24_Time_5_ms_(27.11%)_Space_163.5_MB_(78.20%)

import com_github_leetcode.ListNode;

public class Solution {
public ListNode swapNodes(ListNode head, int k) {
int length = 0;
int secondIndex;

ListNode temp1 = null;
ListNode temp2 = null;
ListNode temp3 = head;
while (temp3 != null) {
length++;
temp3 = temp3.next;
}

secondIndex = length - k + 1;
temp3 = head;
try {
for (int i = 1; i <= length; i++) {
if (i == k) {
temp1 = temp3;
}
if (i == secondIndex) {
temp2 = temp3;
}
temp3 = temp3.next;
}
int value = temp1.val;
temp1.val = temp2.val;
temp2.val = value;
} catch (Exception e) {
System.out.println(e.getMessage());
}
return head;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
1721\. Swapping Nodes in a Linked List

Medium

You are given the `head` of a linked list, and an integer `k`.

Return _the head of the linked list after **swapping** the values of the_ <code>k<sup>th</sup></code> _node from the beginning and the_ <code>k<sup>th</sup></code> _node from the end (the list is **1-indexed**)._

**Example 1:**

![](https://assets.leetcode.com/uploads/2020/09/21/linked1.jpg)

**Input:** head = [1,2,3,4,5], k = 2

**Output:** [1,4,3,2,5]

**Example 2:**

**Input:** head = [7,9,6,6,7,8,3,0,9,5], k = 5

**Output:** [7,9,6,6,8,7,3,0,9,5]

**Constraints:**

* The number of nodes in the list is `n`.
* <code>1 <= k <= n <= 10<sup>5</sup></code>
* `0 <= Node.val <= 100`
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package g1701_1800.s1722_minimize_hamming_distance_after_swap_operations;

// #Medium #Array #Depth_First_Search #Union_Find
// #2022_04_25_Time_51_ms_(94.82%)_Space_77.5_MB_(96.89%)

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Solution {
public int minimumHammingDistance(int[] source, int[] target, int[][] allowedSwaps) {
int i;
int n = source.length;
int weight = 0;
int[] parent = new int[n];
for (i = 0; i < n; i++) {
parent[i] = i;
}

for (int[] swap : allowedSwaps) {
union(swap[0], swap[1], parent);
}

HashMap<Integer, List<Integer>> components = new HashMap<>();
for (i = 0; i < n; i++) {
find(i, parent);
List<Integer> list = components.getOrDefault(parent[i], new ArrayList<>());
list.add(i);
components.put(parent[i], list);
}

for (Map.Entry<Integer, List<Integer>> entry : components.entrySet()) {
weight += getHammingDistance(source, target, entry.getValue());
}

return weight;
}

private int getHammingDistance(int[] source, int[] target, List<Integer> indices) {
HashMap<Integer, Integer> list1 = new HashMap<>();
HashMap<Integer, Integer> list2 = new HashMap<>();

for (int i : indices) {
list1.put(target[i], 1 + list1.getOrDefault(target[i], 0));
list2.put(source[i], 1 + list2.getOrDefault(source[i], 0));
}

int size = indices.size();

for (Map.Entry<Integer, Integer> entry : list1.entrySet()) {
size -= Math.min(entry.getValue(), list2.getOrDefault(entry.getKey(), 0));
}

return size;
}

private void union(int x, int y, int[] parent) {
if (x != y) {
int a = find(x, parent);
int b = find(y, parent);
if (a != b) {
parent[a] = b;
}
}
}

private int find(int x, int[] parent) {
int y = x;

while (y != parent[y]) {
y = parent[y];
}

parent[x] = y;
return y;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
1722\. Minimize Hamming Distance After Swap Operations

Medium

You are given two integer arrays, `source` and `target`, both of length `n`. You are also given an array `allowedSwaps` where each <code>allowedSwaps[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that you are allowed to swap the elements at index <code>a<sub>i</sub></code> and index <code>b<sub>i</sub></code> **(0-indexed)** of array `source`. Note that you can swap elements at a specific pair of indices **multiple** times and in **any** order.

The **Hamming distance** of two arrays of the same length, `source` and `target`, is the number of positions where the elements are different. Formally, it is the number of indices `i` for `0 <= i <= n-1` where `source[i] != target[i]` **(0-indexed)**.

Return _the **minimum Hamming distance** of_ `source` _and_ `target` _after performing **any** amount of swap operations on array_ `source`_._

**Example 1:**

**Input:** source = [1,2,3,4], target = [2,1,4,5], allowedSwaps = [[0,1],[2,3]]

**Output:** 1

**Explanation:** source can be transformed the following way:

- Swap indices 0 and 1: source = [2,1,3,4]

- Swap indices 2 and 3: source = [2,1,4,3]

The Hamming distance of source and target is 1 as they differ in 1 position: index 3.

**Example 2:**

**Input:** source = [1,2,3,4], target = [1,3,2,4], allowedSwaps = []

**Output:** 2

**Explanation:** There are no allowed swaps. The Hamming distance of source and target is 2 as they differ in 2 positions: index 1 and index 2.

**Example 3:**

**Input:** source = [5,1,2,4,3], target = [1,5,4,2,3], allowedSwaps = [[0,4],[4,2],[1,3],[1,4]]

**Output:** 0

**Constraints:**

* `n == source.length == target.length`
* <code>1 <= n <= 10<sup>5</sup></code>
* <code>1 <= source[i], target[i] <= 10<sup>5</sup></code>
* <code>0 <= allowedSwaps.length <= 10<sup>5</sup></code>
* `allowedSwaps[i].length == 2`
* <code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code>
* <code>a<sub>i</sub> != b<sub>i</sub></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package g1701_1800.s1723_find_minimum_time_to_finish_all_jobs;

// #Hard #Array #Dynamic_Programming #Bit_Manipulation #Backtracking #Bitmask
// #2022_04_25_Time_3_ms_(76.73%)_Space_41.4_MB_(72.04%)

public class Solution {
private int min = Integer.MAX_VALUE;

public int minimumTimeRequired(int[] jobs, int k) {
backtraking(jobs, jobs.length - 1, new int[k]);
return min;
}

private void backtraking(int[] jobs, int j, int[] sum) {
int max = getMax(sum);
if (max >= min) {
return;
}
if (j < 0) {
min = max;
return;
}
for (int i = 0; i < sum.length; i++) {
if (i > 0 && sum[i] == sum[i - 1]) {
continue;
}
sum[i] += jobs[j];
backtraking(jobs, j - 1, sum);
sum[i] -= jobs[j];
}
}

private int getMax(int[] sum) {
int max = Integer.MIN_VALUE;
for (int j : sum) {
max = Math.max(max, j);
}
return max;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
1723\. Find Minimum Time to Finish All Jobs

Hard

You are given an integer array `jobs`, where `jobs[i]` is the amount of time it takes to complete the <code>i<sup>th</sup></code> job.

There are `k` workers that you can assign jobs to. Each job should be assigned to **exactly** one worker. The **working time** of a worker is the sum of the time it takes to complete all jobs assigned to them. Your goal is to devise an optimal assignment such that the **maximum working time** of any worker is **minimized**.

_Return the **minimum** possible **maximum working time** of any assignment._

**Example 1:**

**Input:** jobs = [3,2,3], k = 3

**Output:** 3

**Explanation:** By assigning each person one job, the maximum time is 3.

**Example 2:**

**Input:** jobs = [1,2,4,7,8], k = 2

**Output:** 11

**Explanation:** Assign the jobs the following way:

Worker 1: 1, 2, 8 (working time = 1 + 2 + 8 = 11)

Worker 2: 4, 7 (working time = 4 + 7 = 11)

The maximum working time is 11.

**Constraints:**

* `1 <= k <= jobs.length <= 12`
* <code>1 <= jobs[i] <= 10<sup>7</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package g1701_1800.s1725_number_of_rectangles_that_can_form_the_largest_square;

// #Easy #Array #2022_04_25_Time_9_ms_(9.22%)_Space_42.6_MB_(87.06%)

import java.util.TreeMap;

public class Solution {
public int countGoodRectangles(int[][] rectangles) {
TreeMap<Integer, Integer> map = new TreeMap<>();
for (int[] rec : rectangles) {
int min = Math.min(rec[0], rec[1]);
map.put(min, map.getOrDefault(min, 0) + 1);
}
return map.lastEntry().getValue();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
1725\. Number Of Rectangles That Can Form The Largest Square

Easy

You are given an array `rectangles` where <code>rectangles[i] = [l<sub>i</sub>, w<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> rectangle of length <code>l<sub>i</sub></code> and width <code>w<sub>i</sub></code>.

You can cut the <code>i<sup>th</sup></code> rectangle to form a square with a side length of `k` if both <code>k <= l<sub>i</sub></code> and <code>k <= w<sub>i</sub></code>. For example, if you have a rectangle `[4,6]`, you can cut it to get a square with a side length of at most `4`.

Let `maxLen` be the side length of the **largest** square you can obtain from any of the given rectangles.

Return _the **number** of rectangles that can make a square with a side length of_ `maxLen`.

**Example 1:**

**Input:** rectangles = [[5,8],[3,9],[5,12],[16,5]]

**Output:** 3

**Explanation:** The largest squares you can get from each rectangle are of lengths [5,3,5,5]. The largest possible square is of length 5, and you can get it out of 3 rectangles.

**Example 2:**

**Input:** rectangles = [[2,3],[3,7],[4,3],[3,7]]

**Output:** 3

**Constraints:**

* `1 <= rectangles.length <= 1000`
* `rectangles[i].length == 2`
* <code>1 <= l<sub>i</sub>, w<sub>i</sub> <= 10<sup>9</sup></code>
* <code>l<sub>i</sub> != w<sub>i</sub></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package g1701_1800.s1720_decode_xored_array;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void decode() {
assertThat(new Solution().decode(new int[] {1, 2, 3}, 1), equalTo(new int[] {1, 0, 2, 1}));
}

@Test
void decode2() {
assertThat(
new Solution().decode(new int[] {6, 2, 7, 3}, 4),
equalTo(new int[] {4, 2, 0, 7, 4}));
}
}
Loading