Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solutions #140

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions week-1/3Sum Closest
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import java.util.Arrays;
class Solution {
public int threeSumClosest(int[] nums, int target) {
Arrays.sort(nums);
int sum=nums[0]+nums[1]+nums[2];
for(int i=0;i<nums.length-2;i++){
int j=i+1;
int x=nums.length-1;
while(j<x){
int s=nums[i]+nums[j]+nums[x];
… return sum;
}
}
19 changes: 19 additions & 0 deletions week-1/3Sum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> ans=new ArrayList<>();
Arrays.sort(nums);
for(int i=0;i<nums.length;i++){
if(i!=0 && nums[i]==nums[i-1]){
continue;
}
int j=i+1;
int k=nums.length-1;
… k--;
while(j<k && nums[j]==nums[j-1]) j++;
while(j<k && nums[k]==nums[k+1]) k--;
}
}
}
return ans;
}
}
17 changes: 17 additions & 0 deletions week-1/Assign Cookies
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public int findContentChildren(int[] g, int[] s) {
Arrays.sort(g);
Arrays.sort(s);
int c = s.length - 1;
int i = g.length - 1;
int result = 0;
while(c >= 0 && i >= 0){
if(g[i] <= s[c]){
result++;
c--;
}
i--;
}
return result;
}
}
31 changes: 31 additions & 0 deletions week-1/BuyTwoChocolates.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
public class BuyTwoChocolates {
public static void main(String[] args) {
int[] prices = {1, 2, 3, 4};
int money = 5;

BuyTwoChocolates solver = new BuyTwoChocolates();
int result = solver.buyChoco(prices, money);
System.out.println(result);
}

public int buyChoco(int[] prices, int money) {
int min = Integer.MAX_VALUE;
int smin = Integer.MAX_VALUE;

for (int i = 0; i < prices.length; i++) {
if (prices[i] < min) {
smin = min;
min = prices[i];
} else {
smin = Math.min(prices[i], smin);
}
}

int sum = min + smin;
if (sum > money) {
return money;
}

return money - sum;
}
}
16 changes: 16 additions & 0 deletions week-1/Container With Most Water
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
public int maxArea(int[] height) {
int start=0;
int end=height.length-1;
int area=0;
while(start<=end){
int newarea=(end-start)*Math.min(height[start],height[end]);
area=Math.max(area,newarea);
if(height[start]<height[end]){
start++;
}else{
end--;
}
}return area;
}
}
20 changes: 20 additions & 0 deletions week-1/Contiguous Array
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
public int findMaxLength(int[] nums) {
HashMap<Integer, Integer> map = new HashMap<>();
map.put(0, -1);
int maxLength = 0;
int count = 0;

for (int i = 0; i < nums.length; i++) {
count += (nums[i] == 1) ? 1 : -1;

if (map.containsKey(count)) {
maxLength = Math.max(maxLength, i - map.get(count));
} else {
map.put(count, i);
}
}

return maxLength;
}
}
19 changes: 19 additions & 0 deletions week-1/Continuous Subarray Sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public boolean checkSubarraySum(int[] nums, int k) {
Map<Integer, Integer> map = new HashMap<>();
map.put(0, -1);
int sum = 0;
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
int rem = sum % k;
if (map.containsKey(rem)) {
if (i - map.get(rem) >= 2) {
return true;
}
} else {
map.put(rem, i);
}
}
return false;
}
}
12 changes: 12 additions & 0 deletions week-1/Count Number of Nice Subarrays
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution {
public int numberOfSubarrays(int[] nums, int k) {
int ans=print(nums,k)-print(nums,k-1);
return ans;
}
public static int print(int[] nums, int k){
int i=0;
int j=0;
int sum=0;
int cnt=0;
… }
}
22 changes: 22 additions & 0 deletions week-1/CountElementsWithMaximumFrequency.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import java.util.HashMap;

public class CountElementsWithMaximumFrequency {
public static void main(String[] args){
int[] nums={1,2,2,1,3,4};
HashMap<Integer,Integer> map=new HashMap<>();
for(int i:nums){
map.put(i,map.getOrDefault(i, 0)+1);
}
int max=Integer.MIN_VALUE;
int cnt=0;
for(int i:map.values()){
max=Math.max(i, max);
}
for(int i:map.values()){
if(i==max){
cnt++;
}
}
System.err.println(cnt*max);
}
}
25 changes: 25 additions & 0 deletions week-1/Divide Array Into Arrays With Max Difference
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution {
public int[][] divideArray(int[] nums, int k) {
int ans[][]=new int[nums.length/3][3];
int ind=0;
Arrays.sort(nums);
for(int i=0;i<nums.length;i+=3)
{
int first=nums[i];
int second=nums[i+1];
int third=nums[i+2];
if(third-first <= k)
{
ans[ind][0]=first;
ans[ind][1]=second;
ans[ind][2]=third;
ind++;
}
else
{
return new int[0][0];
}
}
return ans;
}
}
15 changes: 15 additions & 0 deletions week-1/Excel Sheet Column Title
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public String convertToTitle(int columnNumber) {
StringBuilder result = new StringBuilder();

while(columnNumber > 0) {
columnNumber--;
int remainder = columnNumber%26;
char currentChar = (char)(remainder + 'A');
result.append(currentChar);
columnNumber/=26;
}

return result.reverse().toString();
}
}
17 changes: 17 additions & 0 deletions week-1/Find All Numbers Disappeared in an Array
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public List<Integer> findDisappearedNumbers(int[] nums) {
List<Integer> ans=new ArrayList<>();
for(int i=0;i<nums.length;i++){
int ele=Math.abs(nums[i]);
if(nums[ele-1]>0){
nums[ele-1]=-1*Math.abs(nums[ele-1]);
}
}
for(int i=0;i<nums.length;i++){
if(nums[i]>0){
ans.add(i+1);
}
}
return ans;
}
}
21 changes: 21 additions & 0 deletions week-1/Find Common Characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public List<String> commonChars(String[] words) {
int n = words.length, j;
int[][] freq = new int[n-1][26];
for (int i = 1; i < n; i++)
for (char c : words[i].toCharArray())
freq[i-1][c-'a']++;
List<String> res = new ArrayList<>();
for (char c : words[0].toCharArray()){
for (j = 1; j < n; j++){
if (freq[j-1][c-'a'] > 0)
freq[j-1][c-'a']--;
else
break;
}
if (j == n)
res.add(c + "");
}
return res;
}
}
17 changes: 17 additions & 0 deletions week-1/Find Pivot Index
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public int pivotIndex(int[] nums) {
int lsum=0;
int tsum=0;
for(int i:nums){
tsum+=i;
}
for(int i=0;i<nums.length;i++){
int rsum=tsum-lsum-nums[i];
if(rsum==lsum){
return i;
}
lsum+=nums[i];
}
return -1;
}
}
31 changes: 31 additions & 0 deletions week-1/Lemonade Change.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Solution {
public boolean lemonadeChange(int[] bills) {
int five=0;
int ten=0;
for(int i=0;i<bills.length;i++){
if(bills[i]==5){
five++;
}
else if(bills[i]==10){//if customer gave 10 rs
if(five==0){
return false;
}
five--;//we will give him 5 back
ten++;
}
else{//if he has given 20 rs
if(five>0 && ten>0){//giving 15
five--;
ten--;
}
else if(five>=3){//giving 15 using only 5 rs
five-=3;
}
else{
return false;//if no condition satisfied
}
}
}
return true;
}
}
15 changes: 15 additions & 0 deletions week-1/Longest Common Prefix
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import java.util.Arrays;
class Solution {
public String longestCommonPrefix(String[] strs) {
Arrays.sort(strs);
String str1=strs[0];
String str2=strs[strs.length-1];
int index=0;
while(index<str1.length()){
if(str1.charAt(index)==str2.charAt(index)){
index++;
}else{
break ;
}}
return index==0?"":str1.substring(0,index);}
}
12 changes: 12 additions & 0 deletions week-1/Longest Mountain in Array
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution {
public int longestMountain(int[] arr) {
if(arr.length<3){
return 0;
}
int max=0;
for(int i=1;i<arr.length-1;i++){
if(arr[i]>arr[i-1] && arr[i]>arr[i+1]){
int left=i;
int right=i;
… }
}
11 changes: 11 additions & 0 deletions week-1/Maximum Erasure Value
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution {
public int maximumUniqueSubarray(int[] nums) {
HashSet<Integer> set=new HashSet<>();
int left=0;
int right=0;
int sum=0;
int max=0;
while(right<nums.length){
while(set.contains(nums[right])){
set.remove(nums[left]);
…}
19 changes: 19 additions & 0 deletions week-1/Maximum Product Subarray
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public int maxProduct(int[] nums) {
double prefix=1;
double suffix=1;
double max=Integer.MIN_VALUE;
for(int i=0;i<nums.length;i++){
if(prefix==0){
prefix=1;
}
if(suffix==0){
suffix=1;
}
prefix=(double)prefix*nums[i];
suffix=(double)suffix*nums[nums.length-i-1];
max=Math.max(max,Math.max(prefix,suffix));
}
return (int)max;
}
}
Loading