Skip to content

Commit 79200a8

Browse files
committed
🎯 [06 FEB] Added POTD and leet code daily challenge
1 parent 77a97a7 commit 79200a8

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed

Diff for: geeksForGeeks/POTD/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
| # |Problem |Link |
1414
|----------------|-------------------------------|-----------------------------|
15+
|6 February 2021|✴️️️️️️️️️️️️️️ `Minimum Swaps to Sort` | [Problem Link](https://practice.geeksforgeeks.org/problems/minimum-swaps/1)|
1516
|5 February 2021|✴️️️️️️️️️️️️️️ `Transfiguration` | [Problem Link](https://practice.geeksforgeeks.org/problems/b6b3297ccfb1ad5f66a9c2b92979170417adf114/1)|
1617
|4 February 2021|🔺️️️️️️️️️️ `Shortest Unique prefix for every word` | [Problem Link](https://practice.geeksforgeeks.org/problems/shortest-unique-prefix-for-every-word/1#)|
1718
|3 February 2021|✳️️️️️️️️️️ `Excel Sheet - [part -1] ` | [Problem Link](https://practice.geeksforgeeks.org/problems/excel-sheet5448/1#)|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package geeksForGeeks.POTD._2022.FEB;
2+
3+
import java.util.*;
4+
5+
/**
6+
* @docs https://practice.geeksforgeeks.org/problems/minimum-swaps/1#
7+
* Given an array of n distinct elements. Find the minimum number of swaps required to sort the array in strictly increasing order.
8+
*
9+
*
10+
* Example 1:
11+
*
12+
* Input:
13+
* nums = {2, 8, 5, 4}
14+
* Output:
15+
* 1
16+
* Explaination:
17+
* swap 8 with 4.
18+
* Example 2:
19+
*
20+
* Input:
21+
* nums = {10, 19, 6, 3, 5}
22+
* Output:
23+
* 2
24+
* Explaination:
25+
* swap 10 with 3 and swap 19 with 5.
26+
*
27+
* Your Task:
28+
* You do not need to read input or print anything. Your task is to complete the function minSwaps()
29+
* which takes the nums as input parameter and
30+
* returns an integer denoting the minimum number of swaps required to sort the array.
31+
* If the array is already sorted, return 0.
32+
*
33+
*
34+
* Expected Time Complexity: O(nlogn)
35+
* Expected Auxiliary Space: O(n)
36+
*
37+
*
38+
* Constraints:
39+
* 1 ≤ n ≤ 10^5
40+
* 1 ≤ numsi ≤ 106
41+
*/
42+
public class _06FEB2022_MinimumSwapstoSort {
43+
public int minSwaps(int[] nums)
44+
{
45+
// Code here
46+
int n = nums.length;
47+
int[] arr = nums.clone();
48+
Arrays.sort(arr);
49+
int ans = 0;
50+
Map<Integer,Integer> map = new HashMap<>();
51+
for(int i=0;i<n;i++) map.put(arr[i],i);
52+
for(int i=0;i<n;i++){
53+
while(map.get(nums[i])!=i){
54+
ans++;
55+
int temp = nums[i];
56+
nums[i] = nums[map.get(temp)];
57+
nums[map.get(temp)] = temp;
58+
}
59+
}
60+
return ans;
61+
}
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package leetCode.DailyChallenge._2022.FEB;
2+
3+
/**
4+
* @docs https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/
5+
*
6+
* Given an integer array nums sorted in non-decreasing order,
7+
* remove some duplicates in-place such that each unique element appears at most twice.
8+
* The relative order of the elements should be kept the same.
9+
*
10+
* Since it is impossible to change the length of the array in some languages,
11+
* you must instead have the result be placed in the first part of the array nums. More formally,
12+
* if there are k elements after removing the duplicates, then the first k elements of nums should hold the final result.
13+
* It does not matter what you leave beyond the first k elements.
14+
*
15+
* Return k after placing the final result in the first k slots of nums.
16+
*
17+
* Do not allocate extra space for another array.
18+
* You must do this by modifying the input array in-place with O(1) extra memory.
19+
*
20+
* Custom Judge:
21+
*
22+
* The judge will test your solution with the following code:
23+
*
24+
* int[] nums = [...]; // Input array
25+
* int[] expectedNums = [...]; // The expected answer with correct length
26+
*
27+
* int k = removeDuplicates(nums); // Calls your implementation
28+
*
29+
* assert k == expectedNums.length;
30+
* for (int i = 0; i < k; i++) {
31+
* assert nums[i] == expectedNums[i];
32+
* }
33+
* If all assertions pass, then your solution will be accepted.
34+
*
35+
*
36+
*
37+
* Example 1:
38+
*
39+
* Input: nums = [1,1,1,2,2,3]
40+
* Output: 5, nums = [1,1,2,2,3,_]
41+
* Explanation: Your function should return k = 5, with the first five elements of nums being 1, 1, 2, 2 and 3 respectively.
42+
* It does not matter what you leave beyond the returned k (hence they are underscores).
43+
* Example 2:
44+
*
45+
* Input: nums = [0,0,1,1,1,1,2,3,3]
46+
* Output: 7, nums = [0,0,1,1,2,3,3,_,_]
47+
* Explanation: Your function should return k = 7, with the first seven elements of nums being 0, 0, 1, 1, 2, 3 and 3 respectively.
48+
* It does not matter what you leave beyond the returned k (hence they are underscores).
49+
*
50+
*
51+
* Constraints:
52+
*
53+
* 1 <= nums.length <= 3 * 10^4
54+
* -104 <= nums[i] <= 10^4
55+
* nums is sorted in non-decreasing order.
56+
*/
57+
public class _06FEB2022_RemoveDuplicatesfromSortedArrayII {
58+
public int removeDuplicates(int[] nums) {
59+
int i = 0;
60+
for (int n : nums)
61+
if (i < 2 || n > nums[i-2])
62+
nums[i++] = n;
63+
return i;
64+
}
65+
}

0 commit comments

Comments
 (0)