Skip to content

Commit cdb1945

Browse files
committed
2 parents 20bffa9 + c63b4c3 commit cdb1945

File tree

9 files changed

+1185
-16
lines changed

9 files changed

+1185
-16
lines changed

.github/workflows/greetings.yml

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,61 @@
1-
name: Greetings
1+
name: 'Greetings'
22

3-
on: [pull_request_target, issues]
3+
on:
4+
issues:
5+
types: [opened]
6+
pull_request_target:
7+
types: [opened]
8+
9+
permissions:
10+
issues: write
11+
pull-requests: write
412

513
jobs:
6-
greeting:
14+
welcome:
715
runs-on: ubuntu-latest
8-
permissions:
9-
issues: write
10-
pull-requests: write
16+
1117
steps:
18+
- name: Check out repository
19+
uses: actions/checkout@v4
20+
1221
- name: Greet first-time contributors
1322
id: greet
1423
uses: actions/first-interaction@v1
1524
with:
1625
repo-token: ${{ secrets.GITHUB_TOKEN }}
1726
issue-message: |
18-
Hi there! 👋 Thank you for opening your first issue on CodeHarborHub. We're excited to hear your thoughts and help you out. Please provide as much detail as you can so we can assist you effectively.
27+
Hi there! 👋 Thank you for opening your issue on CodeHarborHub. We're excited to hear your thoughts and help you out. You've raised a great topic! Please provide as much detail as you can so we can assist you effectively. Welcome aboard!
1928
pr-message: |
20-
Hi there! 👋 Thank you for submitting your first pull request to CodeHarborHub. We appreciate your contribution! Our team will review it soon. If you have any questions or need further assistance, feel free to reach out.
29+
Hi there! 👋 Thank you for submitting your pull request to CodeHarborHub. Great job on the contribution! 🎉 We appreciate your efforts and our team will review it soon. If you have any questions or need further assistance, feel free to reach out. Keep up the great work!
30+
31+
- name: Assign issue or pull request to team member
32+
if: github.event_name == 'issues' || github.event_name == 'pull_request_target'
33+
run: |
34+
ISSUE_NUMBER=${{ github.event.issue.number || github.event.pull_request.number }}
35+
curl -X POST -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
36+
-d '{"assignees":["team-member"]}' \
37+
"https://api.github.com/repos/${{ github.repository }}/issues/${ISSUE_NUMBER}"
38+
39+
- name: Welcome message for community contributors
40+
if: github.event_name == 'issues' || github.event_name == 'pull_request_target'
41+
uses: EddieHubCommunity/gh-action-community/src/welcome@main
42+
with:
43+
github-token: ${{ secrets.GITHUB_TOKEN }}
44+
issue-message: "Hi @${{ github.actor }}! Thanks for opening this issue. We appreciate your contribution to this open-source project. Your input is valuable and we aim to respond or assign your issue as soon as possible. Thanks again!"
45+
pr-message: "Great job, @${{ github.actor }}! 🎉 Thank you for submitting your pull request to CodeHarborHub. We appreciate your contribution and enthusiasm! Our team will review it soon. If you have any questions or need further assistance, feel free to reach out. Thanks for contributing!"
46+
47+
- name: Label first-time issues
48+
if: github.event_name == 'issues'
49+
run: |
50+
ISSUE_NUMBER=${{ github.event.issue.number }}
51+
curl -X POST -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
52+
-d '{"labels":["CodeHarborHub - Thanks for creating an issue!"]}' \
53+
"https://api.github.com/repos/${{ github.repository }}/issues/${ISSUE_NUMBER}"
54+
55+
- name: Label first-time pull requests
56+
if: github.event_name == 'pull_request_target'
57+
run: |
58+
PR_NUMBER=${{ github.event.pull_request.number }}
59+
curl -X POST -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
60+
-d '{"labels":["CodeHarborHub - Thanks for creating a pull request!"]}' \
61+
"https://api.github.com/repos/${{ github.repository }}/issues/${PR_NUMBER}"

dsa-problems/leetcode-problems/0200-0299.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export const problems = [
3838
"problemName": "204. Count Primes",
3939
"difficulty": "Medium",
4040
"leetCodeLink": "https://leetcode.com/problems/count-primes",
41-
"solutionLink": "#"
41+
"solutionLink": "/dsa-solutions/lc-solutions/0200-0299/count-primes"
4242
},
4343
{
4444
"problemName": "205. Isomorphic Strings",

dsa-problems/leetcode-problems/0400-0499.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ export const problems = [
194194
"problemName": "430. Flatten a Multilevel Doubly Linked List",
195195
"difficulty": "Medium",
196196
"leetCodeLink": "https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list",
197-
"solutionLink": "#"
197+
"solutionLink": "/dsa-solutions/lc-solutions/0400-0499/flatten-a-multilevel-doubly-linked-list"
198198
},
199199
{
200200
"problemName": "431. Encode N-ary Tree to Binary Tree",

dsa-solutions/lc-solutions/0000-0099/0018-4Sum.md

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ sidebar_label: 0018-FourSum
55
tags:
66
- Two Pointers
77
description: "Given an array nums of n integers, return an array of all the unique quadruplets [nums[a], nums[b], nums[c], nums[d]]"
8+
89
---
910

1011
## Problem Description
1112

1213
| Problem Statement | Solution Link | LeetCode Profile |
1314
| :------------------------------------------------------ | :------------------------------------------------------------------------- | :------------------------------------------------------ |
14-
| [4Sum](https://leetcode.com/problems/4sum/description/) | [4Sum Solution on LeetCode](https://leetcode.com/problems/4sum/solutions/) | [Abhinash Singh](https://leetcode.com/u/singhabhinash/) |
15+
| [4Sum](https://leetcode.com/problems/4sum/description/) | [4Sum Solution on LeetCode](https://leetcode.com/problems/4sum/solutions/) | [Nikita Saini](https://leetcode.com/u/Saini_Nikita/) |
1516

1617
### Problem Description
1718

@@ -26,12 +27,10 @@ You may return the answer in any order.
2627
### Examples
2728

2829
#### Example 1
29-
3030
- **Input:** `nums = [1,0,-1,0,-2,2], target = 0`
3131
- **Output:** `[[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]`
3232

3333
#### Example 2
34-
3534
- **Input:** `nums = [2,2,2,2,2], target = 8`
3635
- **Output:** `[[2,2,2,2]]`
3736

@@ -57,7 +56,6 @@ To solve the problem, we can use the following approach:
5756
10: Skip duplicate values of left and right pointers to avoid duplicate quadruplets.
5857
11: Return the result.
5958

60-
6159
### Solution Code
6260

6361
#### Python
@@ -87,14 +85,27 @@ class Solution(object):
8785
right -= 1
8886
else:
8987
quadruplets.append([nums[i], nums[j], nums[left], nums[right]])
90-
# Skip duplicates
9188
while left < right and nums[left] == nums[left + 1]:
9289
left += 1
9390
while left < right and nums[right] == nums[right - 1]:
9491
right -= 1
9592
left += 1
9693
right -= 1
97-
return quadruplets
94+
return result
95+
96+
while left < right and nums[left] == nums[left + 1]:
97+
left += 1
98+
while left < right and nums[right] == nums[right - 1]:
99+
right -= 1
100+
101+
left += 1
102+
right -= 1
103+
elif sum_ < target:
104+
left += 1
105+
else:
106+
right -= 1
107+
108+
return result
98109
```
99110

100111
#### C++
@@ -233,3 +244,23 @@ var fourSum = function(nums, target) {
233244
### Conclusion
234245

235246
The given solution sorts the input list and uses a nested loop structure with two pointers to find all unique quadruplets that sum up to the target. By using a set to store the quadruplets, it ensures that duplicates are avoided. The solution efficiently narrows down potential combinations by adjusting the pointers based on the current sum relative to the target. This approach is effective for generating the required output while maintaining uniqueness.
247+
248+
## Step-by-Step Algorithm
249+
250+
1. **Input Validation**: Check if the array is null or has fewer than 4 elements. If so, return an empty list.
251+
2. **Sort the Array**: Sort the input array `nums`.
252+
3. **Outer Loop**: Iterate over the array with index `i` from `0` to `n-4`.
253+
- Skip duplicate elements by checking if `nums[i] == nums[i-1]` (for `i > 0`).
254+
4. **Inner Loop**: For each `i`, iterate with index `j` from `i+1` to `n-3`.
255+
- Skip duplicate elements by checking if `nums[j] == nums[j-1]` (for `j > i+1`).
256+
5. **Two Pointers**: Initialize two pointers `left = j+1` and `right = n-1`.
257+
6. **Finding Quadruplets**:
258+
- While `left < right`:
259+
- Calculate the sum of the quadruplet: `sum = nums[i] + nums[j] + nums[left] + nums[right]`.
260+
- If the sum equals the target:
261+
- Add `[nums[i], nums[j], nums[left], nums[right]]` to the result.
262+
- Move `left` to the right, skipping duplicates.
263+
- Move `right` to the left, skipping duplicates.
264+
- If the sum is less than the target, move `left` to the right.
265+
- If the sum is greater than the target, move `right` to the left.
266+
7. **Return the Result**: After processing all elements, return the list of quadruplets.
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
---
2+
id: rotate-array
3+
title: Rotate Array
4+
sidebar_label: 0189 Rotate Array
5+
tags:
6+
- Array
7+
- LeetCode
8+
- Java
9+
- Python
10+
- C++
11+
description: "This is a solution to the Rotate Array problem on LeetCode."
12+
---
13+
14+
## Problem Description
15+
16+
Given an integer array nums, rotate the array to the right by k steps, where k is non-negative.
17+
18+
### Examples
19+
20+
**Example 1:**
21+
22+
```
23+
Input: nums = [1,2,3,4,5,6,7], k = 3
24+
Output: [5,6,7,1,2,3,4]
25+
Explanation:
26+
rotate 1 steps to the right: [7,1,2,3,4,5,6]
27+
rotate 2 steps to the right: [6,7,1,2,3,4,5]
28+
rotate 3 steps to the right: [5,6,7,1,2,3,4]
29+
30+
```
31+
32+
**Example 2:**
33+
34+
```
35+
Input: nums = [-1,-100,3,99], k = 2
36+
Output: [3,99,-1,-100]
37+
Explanation:
38+
rotate 1 steps to the right: [99,-1,-100,3]
39+
rotate 2 steps to the right: [3,99,-1,-100]
40+
```
41+
42+
### Constraints
43+
44+
- $1 <= nums.length <= 105$
45+
- $-231 <= nums[i] <= 231 - 1$
46+
- $0 <= k <= 105$
47+
48+
49+
## Solution for Candy Distribution Problem
50+
51+
### Intuition And Approach
52+
53+
To rotate an array to the right by k steps, we need to move the last k elements to the front and shift the rest of the elements to the right. A straightforward way to achieve this in-place (without using extra space for another array) is to use the reversal method.
54+
55+
Here is the step-by-step approach:
56+
57+
1. Adjust k:
58+
59+
If k is greater than the length of the array, rotating by k steps is the same as rotating by k % n steps (where n is the length of the array). This is because rotating by the length of the array brings it back to the original position.
60+
Calculate k = k % n.
61+
62+
2. Reverse the entire array:
63+
64+
By reversing the entire array, the last k elements (which we want to move to the front) will be at the beginning, but in reverse order.
65+
For example, reversing [1, 2, 3, 4, 5, 6, 7] gives [7, 6, 5, 4, 3, 2, 1].
66+
67+
3. Reverse the first k elements:
68+
69+
The first k elements are now the elements that were originally at the end of the array. Reverse these to restore their original order.
70+
Continuing the example, reversing the first 3 elements of [7, 6, 5, 4, 3, 2, 1] gives [5, 6, 7, 4, 3, 2, 1].
71+
72+
4. Reverse the remaining n - k elements:
73+
74+
Finally, reverse the rest of the array (from the k-th element to the end) to restore their order.
75+
In the example, reversing the elements from index 3 to 6 of [5, 6, 7, 4, 3, 2, 1] gives [5, 6, 7, 1, 2, 3, 4].
76+
77+
78+
79+
#### Code in Different Languages
80+
81+
<Tabs>
82+
<TabItem value="Java" label="Java">
83+
<SolutionAuthor name="@mahek0620"/>
84+
```java
85+
86+
class Solution {
87+
public void rotate(int[] nums, int k) {
88+
// Ensure k is within the bounds of the array length
89+
k = k % nums.length;
90+
91+
// Reverse the entire array
92+
reverse(nums, 0, nums.length - 1);
93+
94+
// Reverse the first k elements
95+
reverse(nums, 0, k - 1);
96+
97+
// Reverse the remaining elements
98+
reverse(nums, k, nums.length - 1);
99+
}
100+
101+
// Helper function to reverse elements in the array from start to end
102+
private void reverse(int[] nums, int start, int end) {
103+
while (start < end) {
104+
int temp = nums[start];
105+
nums[start] = nums[end];
106+
nums[end] = temp;
107+
start++;
108+
end--;
109+
}
110+
}
111+
112+
```
113+
</TabItem>
114+
<TabItem value="Python" label="Python">
115+
<SolutionAuthor name="@mahek0620"/>
116+
```python
117+
118+
class Solution(object):
119+
def rotate(self, nums, k):
120+
k = k % len(nums)
121+
122+
# Reverse the entire array
123+
self.reverse(nums, 0, len(nums) - 1)
124+
125+
# Reverse the first k elements
126+
self.reverse(nums, 0, k - 1)
127+
128+
# Reverse the remaining elements
129+
self.reverse(nums, k, len(nums) - 1)
130+
131+
def reverse(self, nums, start, end):
132+
while start < end:
133+
nums[start], nums[end] = nums[end], nums[start]
134+
start += 1
135+
end -= 1
136+
137+
```
138+
</TabItem>
139+
140+
141+
<TabItem value="C++" label="C++">
142+
<SolutionAuthor name="@mahek0620"/>
143+
```cpp
144+
145+
#include <vector>
146+
using namespace std;
147+
148+
class Solution {
149+
public:
150+
void rotate(vector<int>& nums, int k) {
151+
// Ensure k is within the bounds of the array length
152+
k = k % nums.size();
153+
154+
// Reverse the entire array
155+
reverse(nums, 0, nums.size() - 1);
156+
157+
// Reverse the first k elements
158+
reverse(nums, 0, k - 1);
159+
160+
// Reverse the remaining elements
161+
reverse(nums, k, nums.size() - 1);
162+
}
163+
164+
private:
165+
void reverse(vector<int>& nums, int start, int end) {
166+
while (start < end) {
167+
int temp = nums[start];
168+
nums[start] = nums[end];
169+
nums[end] = temp;
170+
start++;
171+
end--;
172+
}
173+
}
174+
};
175+
```
176+
</TabItem>
177+
</Tabs>
178+
179+
180+
181+
## References
182+
183+
- **LeetCode Problem:** [Rotate Array Problem](https://leetcode.com/problems/rotate-array/)
184+
- **Solution Link:** [Rotate Array Solution on LeetCode](https://leetcode.com/problems/rotate-array/solutions/5273312/rotate-array-solution)
185+
- **Authors GeeksforGeeks Profile:** [Mahek Patel](https://leetcode.com/u/mahekrpatel611/)

0 commit comments

Comments
 (0)