Skip to content

Commit b398857

Browse files
authored
Merge branch 'main' into main
2 parents 7f53de3 + 1c77dd5 commit b398857

File tree

6 files changed

+215
-0
lines changed

6 files changed

+215
-0
lines changed

docs/SQL/image.png

-10.2 KB
Loading

docs/dsa/algorithms/image-7.png

-2.63 KB
Loading
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
---
2+
id: fair-candy-swap
3+
title: Fair Candy Swap
4+
sidebar_label: 888- Fair Candy Swap
5+
tags:
6+
- Array
7+
- Hash Table
8+
- Sorting
9+
description: Find one pair of candy boxes, one from Alice and one from Bob, to swap so they both end up with the same total number of candies.
10+
sidebar_position: 0888
11+
---
12+
13+
## Problem Description
14+
15+
Alice and Bob have a different total number of candies. You are given two integer arrays `aliceSizes` and bobSizes where `aliceSizes[i]` is the number of candies of the ith box of candy that Alice has and `bobSizes[j]` is the number of candies of the `jth` box of candy that Bob has.
16+
17+
Since they are friends, they would like to exchange one candy box each so that after the exchange, they both have the same total amount of candy. The total amount of candy a person has is the sum of the number of candies in each box they have.
18+
19+
Return an integer array answer where `answer[0]` is the number of candies in the box that Alice must exchange, and `answer[1]` is the number of candies in the box that Bob must exchange. If there are multiple answers, you may return any one of them. It is guaranteed that at least one answer exists.
20+
21+
### Example 1
22+
23+
- **Input:** `aliceSizes = [1,1], bobSizes = [2,2]`
24+
- **Output:** `[1,2]`
25+
26+
27+
### Constraints
28+
29+
- `1 <= aliceSizes.length, bobSizes.length <= 104`
30+
- `1 <= aliceSizes[i], bobSizes[j] <= 105`
31+
32+
## Approach
33+
34+
The solution first calculates the total number of candies each Alice and Bob have. Then, it sorts Bob's candy boxes for efficient searching. It iterates over Alice's candy boxes, checking if swapping a box with Bob can balance their total candies. For each box of Alice, it calculates the required box from Bob and uses binary search to find it in Bob's sorted array. If a valid swap is found, it updates the answer and returns it.
35+
36+
#### Java
37+
```Java
38+
class Solution {
39+
public int[] fairCandySwap(int[] aliceSizes, int[] bobSizes) {
40+
Arrays.sort(bobSizes);
41+
int aliceSum=0;
42+
int bobSum=0;
43+
for(int i=0;i<aliceSizes.length;i++){
44+
aliceSum+=aliceSizes[i];
45+
}
46+
for(int i=0;i<bobSizes.length;i++){
47+
bobSum+=bobSizes[i];
48+
}
49+
int [] ans=new int [2];
50+
51+
for(int i=0;i<aliceSizes.length;i++){
52+
if((bobSum-aliceSum+(2*aliceSizes[i]))%2!=0){
53+
break;
54+
}
55+
if(Arrays.binarySearch(bobSizes,(bobSum-aliceSum+(2*aliceSizes[i]))/2)>=0){
56+
ans[0]=aliceSizes[i];
57+
ans[1]=(bobSum-aliceSum+(2*aliceSizes[i]))/2;
58+
}
59+
else if(aliceSizes[i]==(bobSum-aliceSum+(2*aliceSizes[i]))/2){
60+
ans[0]=aliceSizes[i];
61+
ans[1]=aliceSizes[i];
62+
}
63+
}
64+
return ans;
65+
}
66+
}
67+
```
68+
69+
- Time Complexity
70+
The time complexity is $o(n)$.
71+
72+
- Space Complexity
73+
The space complexity is $O(1)$.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
---
2+
id: longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit
3+
title: Longest Continuous Subarray with Absolute Diff Less Than or Equal to Limit
4+
level: hard
5+
sidebar_label: Longest Continuous Subarray with Absolute Diff Less Than or Equal to Limit
6+
tags:
7+
- Array
8+
- Sliding Window
9+
- Deque
10+
- Java
11+
description: "This document provides solutions for the Longest Continuous Subarray with Absolute Diff Less Than or Equal to Limit problem."
12+
---
13+
14+
## Problem Statement
15+
16+
Given an array of integers `nums` and an integer `limit`, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to `limit`.
17+
18+
**Example 1:**
19+
20+
Input: `nums = [8,2,4,7]`, `limit = 4`
21+
22+
Output: `2`
23+
24+
Explanation: All subarrays are:
25+
- `[8]` with maximum absolute diff `|8-8| = 0 <= 4`.
26+
- `[8,2]` with maximum absolute diff `|8-2| = 6 > 4`.
27+
- `[8,2,4]` with maximum absolute diff `|8-2| = 6 > 4`.
28+
- `[8,2,4,7]` with maximum absolute diff `|8-2| = 6 > 4`.
29+
- `[2]` with maximum absolute diff `|2-2| = 0 <= 4`.
30+
- `[2,4]` with maximum absolute diff `|2-4| = 2 <= 4`.
31+
- `[2,4,7]` with maximum absolute diff `|2-7| = 5 > 4`.
32+
- `[4]` with maximum absolute diff `|4-4| = 0 <= 4`.
33+
- `[4,7]` with maximum absolute diff `|4-7| = 3 <= 4`.
34+
- `[7]` with maximum absolute diff `|7-7| = 0 <= 4`.
35+
36+
Therefore, the size of the longest subarray is 2.
37+
38+
**Example 2:**
39+
40+
Input: `nums = [10,1,2,4,7,2]`, `limit = 5`
41+
42+
Output: `4`
43+
44+
Explanation: The subarray `[2,4,7,2]` is the longest since the maximum absolute diff is `|2-7| = 5 <= 5`.
45+
46+
**Example 3:**
47+
48+
Input: `nums = [4,2,2,2,4,4,2,2]`, `limit = 0`
49+
50+
Output: `3`
51+
52+
**Constraints:**
53+
54+
- `1 <= nums.length <= 10^5`
55+
- `1 <= nums[i] <= 10^9`
56+
- `0 <= limit <= 10^9`
57+
58+
## Solutions
59+
60+
### Approach
61+
62+
To determine the length of the longest subarray where the absolute difference between any two elements is less than or equal to `limit`, follow these steps:
63+
64+
1. **Sliding Window with Deque:**
65+
- Use two deques to maintain the maximum and minimum values in the current window.
66+
- Slide the window across the array and adjust the window size to ensure the absolute difference condition is met.
67+
68+
### Java
69+
70+
```java
71+
72+
73+
class Solution {
74+
public int longestSubarray(int[] nums, int limit) {
75+
LinkedList<Integer> increase = new LinkedList<>();
76+
LinkedList<Integer> decrease = new LinkedList<>();
77+
78+
int max = 0;
79+
int left = 0;
80+
81+
for (int i = 0; i < nums.length; i++) {
82+
int n = nums[i];
83+
84+
while (!increase.isEmpty() && n < increase.getLast()) {
85+
increase.removeLast();
86+
}
87+
increase.add(n);
88+
89+
while (!decrease.isEmpty() && n > decrease.getLast()) {
90+
decrease.removeLast();
91+
}
92+
decrease.add(n);
93+
94+
while (decrease.getFirst() - increase.getFirst() > limit) {
95+
if (nums[left] == decrease.getFirst()) {
96+
decrease.removeFirst();
97+
}
98+
if (nums[left] == increase.getFirst()) {
99+
increase.removeFirst();
100+
}
101+
left++;
102+
}
103+
104+
int size = i - left + 1;
105+
max = Math.max(max, size);
106+
}
107+
108+
return max;
109+
}
110+
}
111+
```
112+
### Python
113+
```Python
114+
class Solution:
115+
def longestSubarray(self, nums: List[int], limit: int) -> int:
116+
increase = deque()
117+
decrease = deque()
118+
max_length = 0
119+
left = 0
120+
121+
for i in range(len(nums)):
122+
n = nums[i]
123+
124+
while increase and n < increase[-1]:
125+
increase.pop()
126+
increase.append(n)
127+
128+
while decrease and n > decrease[-1]:
129+
decrease.pop()
130+
decrease.append(n)
131+
132+
while decrease[0] - increase[0] > limit:
133+
if nums[left] == decrease[0]:
134+
decrease.popleft()
135+
if nums[left] == increase[0]:
136+
increase.popleft()
137+
left += 1
138+
139+
max_length = max(max_length, i - left + 1)
140+
141+
return max_length
142+
```

dsa/Algorithms/Tree/inorder_1-1.jpg

-208 Bytes
Loading

dsa/Algorithms/Tree/inorder_1.jpg

-208 Bytes
Loading

0 commit comments

Comments
 (0)