From f4050b721a6da5e32044ca79df954a1a4b15f9d1 Mon Sep 17 00:00:00 2001 From: Vaidehi Khandelwal Date: Tue, 12 Nov 2024 17:26:39 +0530 Subject: [PATCH 1/2] Add files via upload --- Dsa_week1.txt | 542 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 542 insertions(+) create mode 100644 Dsa_week1.txt diff --git a/Dsa_week1.txt b/Dsa_week1.txt new file mode 100644 index 00000000..f10b9a68 --- /dev/null +++ b/Dsa_week1.txt @@ -0,0 +1,542 @@ +455.Assign Cookies: + +class Solution { + public int findContentChildren(int[] g, int[] s) { + Arrays.sort(g); + Arrays.sort(s); + int contentChildren = 0; + int cookieIndex = 0; + while (cookieIndex < s.length && contentChildren < g.length) { + if (s[cookieIndex] >= g[contentChildren]) { + contentChildren++; + } + cookieIndex++; + } + return contentChildren; + } + +} + + + +============================================================================ + +2706. Buy Two Chocolates + +class Solution { + public int buyChoco(int[] prices, int money) { + + int minCost = Integer.MAX_VALUE; + + int n = prices.length; + + for (int firstChoco = 0; firstChoco < n; firstChoco++) { + for (int secondChoco = firstChoco + 1; secondChoco < n; secondChoco++) { + int cost = prices[firstChoco] + prices[secondChoco]; + + if (cost < minCost) { + minCost = cost; + } + } + } + + if (minCost <= money) { + return money - minCost; + } else { + return money; + } + } +} + +================================================================== +3005.Count Elements With Maximum frequency + +public class Solution { + public int maxFrequencyElements(int[] nums) { + Map frequencies = new HashMap<>(); + for (int num : nums) { + frequencies.put(num, frequencies.getOrDefault(num, 0) + 1); + } + + int maxFrequency = 0; + for (int frequency : frequencies.values()) { + maxFrequency = Math.max(maxFrequency, frequency); + } + + int frequencyOfMaxFrequency = 0; + for (int frequency : frequencies.values()) { + if (frequency == maxFrequency) { + frequencyOfMaxFrequency++; + } + } + return frequencyOfMaxFrequency * maxFrequency; + } +} + +--------------------------------------------------------- + +2966.Divide Array into Arrays with max difference: + +class Solution { + public int[][] divideArray(int[] nums, int k) { + Arrays.sort(nums); + int[][] ans = new int[nums.length / 3][3]; + for (int i = 0; i < nums.length; i += 3) { + if (nums[i + 2] - nums[i] > k) { + return new int[0][0]; + } + ans[i / 3] = new int[]{nums[i], nums[i + 1], nums[i + 2]}; + } + return ans; + } +} +--------------------------------------------------------------- + +1002. find the common characters: + +class Solution { + + public List commonChars(String[] words) { + int wordsSize = words.length; + int[] commonCharacterCounts = new int[26]; + int[] currentCharacterCounts = new int[26]; + List result = new ArrayList<>(); + + for (char ch : words[0].toCharArray()) { + commonCharacterCounts[ch - 'a']++; + } + + for (int i = 1; i < wordsSize; i++) { + Arrays.fill(currentCharacterCounts, 0); + + for (char ch : words[i].toCharArray()) { + currentCharacterCounts[ch - 'a']++; + } + + for (int letter = 0; letter < 26; letter++) { + commonCharacterCounts[letter] = Math.min( + commonCharacterCounts[letter], + currentCharacterCounts[letter] + ); + } + } + + for (int letter = 0; letter < 26; letter++) { + for ( + int commonCount = 0; + commonCount < commonCharacterCounts[letter]; + commonCount++ + ) { + result.add(String.valueOf((char) (letter + 'a'))); + } + } + + return result; + } +} + +=============================================================== + +860. Lemonade change + + +class Solution { + + public boolean lemonadeChange(int[] bills) { + int fiveDollarBills = 0; + int tenDollarBills = 0; + + + for (int customerBill : bills) { + if (customerBill == 5) { + fiveDollarBills++; + } else if (customerBill == 10) { + if (fiveDollarBills > 0) { + fiveDollarBills--; + tenDollarBills++; + } else { + return false; + } + } else { // customerBill == 20 + // We need to give $15 change + if (tenDollarBills > 0 && fiveDollarBills > 0) { + fiveDollarBills--; + tenDollarBills--; + } else if (fiveDollarBills >= 3) { + fiveDollarBills -= 3; + } else { + return false; + } + } + } + } +} + +================================================================== + +2540. Minimum common Value + +class Solution { + public int getCommon(int[] a, int[] b) { + int i=0; + int j=0; + while(i> threeSum(vector& nums) { + vector> res; + sort(nums.begin(), nums.end()); + + for (int i = 0; i < nums.size(); i++) { + if (i > 0 && nums[i] == nums[i-1]) { + continue; + } + + int j = i + 1; + int k = nums.size() - 1; + + while (j < k) { + int total = nums[i] + nums[j] + nums[k]; + + if (total > 0) { + k--; + } else if (total < 0) { + j++; + } else { + res.push_back({nums[i], nums[j], nums[k]}); + j++; + + while (nums[j] == nums[j-1] && j < k) { + j++; + } + } + } + } + return res; + } +}; + +=============================================================== + +16. 3Sum Closest + +import java.util.Arrays; + +class Solution { + public int threeSumClosest(int[] nums, int target) { + Arrays.sort(nums); + int closestSum = nums[0] + nums[1] + nums[2]; // Initialize closest sum with the sum of the first three elements + + for (int i = 0; i < nums.length - 2; i++) { + int j = i + 1; + int k = nums.length - 1; + + while (j < k) { + int sum = nums[i] + nums[j] + nums[k]; + + if (Math.abs(target - sum) < Math.abs(target - closestSum)) { + closestSum = sum; // Update closest sum if the current sum is closer to the target + } + + if (sum < target) { + j++; // Increment j to increase the sum + } else { + k--; // Decrement k to decrease the sum + } + } + } + + return closestSum; + } +} + +===================================================================== + +845.Longest Mountain in Array: + + +class Solution { + public int longestMountain(int[] A) { + int N = A.length; + int ans = 0, base = 0; + while (base < N) { + int end = base; + if (end + 1 < N && A[end] < A[end + 1]) { + while (end + 1 < N && A[end] < A[end + 1]) end++; + + if (end + 1 < N && A[end] > A[end + 1]) { + while (end + 1 < N && A[end] > A[end + 1]) end++; + ans = Math.max(ans, end - base + 1); + } + } + + base = Math.max(end, base + 1); + } + + return ans; + } +} + +=================================================================== +1695.Maximum Erasure Value: + +class Solution { + public int maximumUniqueSubarray(int[] nums) { + int sum=0; + Sets=new HashSet<>(); + int max=0; + int l=0; + int r=0; + int n=nums.length; + for(r=0;rh=new HashMap<>(); + int sum=0; + int ans=0; + for(int i=0;i modSeen = new HashMap<>(); + modSeen.put(0, -1); + + for (int i = 0; i < nums.length; i++) { + prefixMod = (prefixMod + nums[i]) % k; + + if (modSeen.containsKey(prefixMod)) { + + if (i - modSeen.get(prefixMod) > 1) { + return true; + } + } else { + modSeen.put(prefixMod, i); + } + } + + return false; + } +} + + +=================================================================== +1248.Count Number of Nice Subarrays: + +class Solution { + + public int numberOfSubarrays(int[] nums, int k) { + Queue oddIndices = new LinkedList<>(); + int subarrays = 0; + int lastPopped = -1; + int initialGap = 0; + + for (int i = 0; i < nums.length; i++) { + if (nums[i] % 2 == 1) { + oddIndices.offer(i); + } + + if (oddIndices.size() > k) { + lastPopped = oddIndices.poll(); + } + if (oddIndices.size() == k) { + initialGap = oddIndices.element() - lastPopped; + subarrays += initialGap; + } + } + + return subarrays; + } +} + +====================================================================== + +724.Find Pivot Index: + +class Solution { + public int pivotIndex(int[] nums) { + int total = 0; + for (int num : nums) { + total += num; + } + + int leftTotal = 0; + for (int i = 0; i < nums.length; i++) { + int rightTotal = total - leftTotal - nums[i]; + if (rightTotal == leftTotal) { + return i; + } + leftTotal += nums[i]; + } + + return -1; + } +} + +=================================================================== +2090. k Radius Subarrays Averages: + +class Solution { +public: + vector getAverages(vector& v, int k) { + int n=v.size(); + vector ans(n,-1); + int i=0,j=0; + long long sum=0; + while(j=0;i-- ){ + pro *=nums[i]; + mx=Math.max(mx,pro); + if(pro==0) pro=1; + } + return mx; + } +} + + +======================================================================== + +560.Subarray Sum Equals k + +class Solution { + public int subarraySum(int[] nums, int k) { + int n=nums.length; + HashMap map=new HashMap<>(); + int sum=0; + int c=0; + map.put(0,1); + for(int i=0;i Date: Tue, 12 Nov 2024 23:07:15 +0530 Subject: [PATCH 2/2] Add files via upload --- DSa_task2.txt | 202 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 202 insertions(+) create mode 100644 DSa_task2.txt diff --git a/DSa_task2.txt b/DSa_task2.txt new file mode 100644 index 00000000..7774c005 --- /dev/null +++ b/DSa_task2.txt @@ -0,0 +1,202 @@ +704.Binary Search +class Solution { + public int search(int[] nums, int target) { + int high = nums.length - 1; + int low = 0; + if (low <= high) { + while (low <= high) { + int mid = (high + low) / 2; + if (target == nums[mid]) { + return mid; + } else if (target > nums[mid]) { + low = mid + 1; + } else { + high = mid - 1; + } + } + } + + return -1; + } +} +-------------------------- + +35. Search Insert Position + +class Solution { + public int searchInsert(int[] nums, int target) { + int low = 0; + int high = nums.length-1; + + while (low <= high) { + int mid = low + (high-low)/2; + if (nums[mid] == target) + return mid; + else if (nums[mid] > target) + high = mid-1; + else + low = mid+1; + } + + return low; + } +} + +----------------- +74.Search a 2D Matrix + +class Solution { + public boolean searchMatrix(int[][] matrix, int target) { + int rowStart = 0; + int rowEnd = matrix.length - 1; + int lastColumnIndex = matrix[0].length - 1; + int potentialRow = -1; + + while (rowStart <= rowEnd) { + int midRow = rowStart + (rowEnd - rowStart) / 2; + if (matrix[midRow][lastColumnIndex] == target) return true; + else if (matrix[midRow][lastColumnIndex] < target) { + rowStart = midRow + 1; + } else { + potentialRow = midRow; + rowEnd = midRow - 1; + } + } + + return potentialRow != -1 ? binarySearch(matrix[potentialRow], target) : false; + } + + private boolean binarySearch(int[] row, int target) { + int left = 0; + int right = row.length - 1; + while (left <= right) { + int mid = left + (right - left) / 2; + if (row[mid] == target) return true; + if (row[mid] < target) { + left = mid + 1; + } else { + right = mid - 1; + } + } + return false; + } +} +----------------------------- + +69. Sqrt(x) + +import java.util.*; +class Solution { + public int mySqrt(int x) { + if(x==0 || x==1){ + return x; + } + int low=1; + int high=x; + int mid=-1; + while(low<=high) { + mid= low+(high-low)/2; + if(mid*mid==x){ + return mid; + } + else if((long)mid*mid>(long)x){ + high =mid-1; + } + else{ + low=mid+1; + } + } + return Math.round(high); + } + } +------------------ + +34. Find First and Last Position of Element in Sorted Array + +class Solution { + public int[] searchRange(int[] numbers, int target) { + int left = 0; + int right = numbers.length - 1; + + while (left <= right) { + int middle = left + (right - left) / 2; + + if (numbers[middle] > target) { + right = middle - 1; + } else if (numbers[middle] < target) { + left = middle + 1; + } else { + int startIndex = middle; + int endIndex = middle; + + for (int offsetLeft = 1; offsetLeft <= middle; offsetLeft++) { + if (numbers[middle - offsetLeft] != target) break; + startIndex = middle - offsetLeft; + } + + for (int offsetRight = 1; offsetRight < numbers.length - middle; offsetRight++) { + if (numbers[middle + offsetRight] != target) break; + endIndex = middle + offsetRight; + } + + return new int[]{startIndex, endIndex}; + } + } + + return new int[]{-1, -1}; + } +} + + +------------------------------ +4. Median of Two Sorted Arrays + + +public class Solution { + public double findMedianSortedArrays(int[] array1, int[] array2) { + if (array1.length > array2.length) { + return findMedianSortedArrays(array2, array1); + } + + int length1 = array1.length; + int length2 = array2.length; + int leftPointer = 0, rightPointer = length1, halfLength = (length1 + length2 + 1) / 2; + + while (leftPointer <= rightPointer) { + int mid1 = (leftPointer + rightPointer) / 2; + int mid2 = halfLength - mid1; + + if (mid1 < rightPointer && array1[mid1] < array2[mid2 - 1]) { + leftPointer = mid1 + 1; + } else if (mid1 > leftPointer && array1[mid1 - 1] > array2[mid2]) { + rightPointer = mid1 - 1; + } else { + int maxLeft = 0; + if (mid1 == 0) { + maxLeft = array2[mid2 - 1]; + } else if (mid2 == 0) { + maxLeft = array1[mid1 - 1]; + } else { + maxLeft = Math.max(array1[mid1 - 1], array2[mid2 - 1]); + } + + if ((length1 + length2) % 2 == 1) { + return maxLeft; + } + + int minRight = 0; + if (mid1 == length1) { + minRight = array2[mid2]; + } else if (mid2 == length2) { + minRight = array1[mid1]; + } else { + minRight = Math.min(array1[mid1], array2[mid2]); + } + + return (maxLeft + minRight) / 2.0; + } + } + + return 0.0; + } +}