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

Week1_Priyanka #152

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
1 change: 0 additions & 1 deletion Week1/Count Negative Nos/week1.md

This file was deleted.

21 changes: 21 additions & 0 deletions Week1/Count Negative Nos/week1_Priyanka
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public int countNegatives(int[][] grid) {

int rowLength = grid.length;
int colLength = grid[0].length;
int row = 0;
int col = colLength - 1;
int sumOfNegativeNumbers = 0;

//Binary search from top right.
while (row < rowLength && col >= 0) {
if (grid[row][col] < 0) {
sumOfNegativeNumbers += rowLength - row;
col--;
} else {
row++;
}
}
return sumOfNegativeNumbers;
}
}
1 change: 0 additions & 1 deletion Week1/Count and Say/week1.md

This file was deleted.

27 changes: 27 additions & 0 deletions Week1/Count and Say/week1_Priyanka
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution {
public String countAndSay(int n) {
if (n == 1) {
return "1";
}
String base = "1";
for (int i = 2; i <= n; ++i) {
base = word(base);
}
return base;
}

private String word(String n) {
StringBuilder sb = new StringBuilder();
int i = 0;
while (i < n.length()) {
char c = n.charAt(i);
int j = i + 1;
while (j < n.length() && n.charAt(j) == c) {
++j;
}
sb.append(j - i).append(c);
i = j;
}
return sb.toString();
}
}
1 change: 0 additions & 1 deletion Week1/Longest Substring wo repeating chars/week1.md

This file was deleted.

8 changes: 8 additions & 0 deletions Week1/Longest Substring wo repeating chars/week1_Priyanka
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Solution {
public int missingNumber(int[] a) {
int[] b = new int[a.length+1];
for(int i=0; i<a.length; i++) b[a[i]] =1;
for(int i=0; i<=b.length; i++) if(b[i]==0) return i;
return 0;
}
}
1 change: 0 additions & 1 deletion Week1/Max Subarray/week1.md

This file was deleted.

13 changes: 13 additions & 0 deletions Week1/Max Subarray/week1_Priyanka
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution {
public int maxSubArray(int[] nums) {
int maxSub = nums[0], curSum = 0;

for (int n : nums) {
if (curSum < 0)
curSum = 0;
curSum += n;
maxSub = Math.max(maxSub, curSum);
}
return maxSub;
}
}
1 change: 0 additions & 1 deletion Week1/Missing no/week1.md

This file was deleted.

8 changes: 8 additions & 0 deletions Week1/Missing no/week1_Priyanka
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Solution {
public int missingNumber(int[] a) {
int[] b = new int[a.length+1];
for(int i=0; i<a.length; i++) b[a[i]] =1;
for(int i=0; i<=b.length; i++) if(b[i]==0) return i;
return 0;
}
}
1 change: 0 additions & 1 deletion Week1/Monotonic Array/week1.md

This file was deleted.

15 changes: 15 additions & 0 deletions Week1/Monotonic Array/week1_Priyanka
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public boolean isMonotonic(int[] A) {
int ca = 0;
for(int i=0;i<A.length-1;i++)
{
if(A[i+1]>A[i]&&ca==0)
ca++;
else if(A[i+1]<A[i]&&ca==0)
ca--;
if(ca>0 && A[i+1]<A[i]||ca<0&&A[i+1]>A[i])
return false;
}
return true;
}
}
1 change: 0 additions & 1 deletion Week1/Move Zeroes/week1.md

This file was deleted.

24 changes: 24 additions & 0 deletions Week1/Move Zeroes/week1_Priyanka
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
public class Solution {

public void moveZeroes(int nums[]) {
int vacant = -1;

for (int i = 0; i < nums.length; i++) {
if (nums[i] == 0) {
if (vacant == -1) // set "vacant" if first time seeing 0
vacant = i;
} else {
if (vacant != -1) {
swap(nums, i, vacant);
vacant++;
}
}
}
}

private void swap(int nums[], int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
1 change: 0 additions & 1 deletion Week1/Pascal Triangle/week1.md

This file was deleted.

25 changes: 25 additions & 0 deletions Week1/Pascal Triangle/week1_Priyanka
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> pascals = new ArrayList<>();

if (numRows == 0)
return pascals;

List<Integer> firstRow = new ArrayList<>();
firstRow.add(1);
pascals.add(firstRow);

for (int i = 1; i < numRows; i++) {
List<Integer> prevRow = pascals.get(i-1);
List<Integer> newRow = new ArrayList<>();
newRow.add(1);
// Iterate i - 1 times (current numRow - 1)
// Starting at k = 1 because k = 0 is already added being '1'
for (int k = 1; k < i; k++) {
int sum = prevRow.get(k-1) + prevRow.get(k);
newRow.add(sum);
}
newRow.add(1);
pascals.add(newRow);
}
return pascals;
}
1 change: 0 additions & 1 deletion Week1/Pivot Index/week1.md

This file was deleted.

19 changes: 19 additions & 0 deletions Week1/Pivot Index/week1_Priyanka
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public int pivotIndex(int[] nums) {
int total=0,sum=0;

for(int n : nums){
total+=n;
}

for(int i=0;i<nums.length;i++){
if(sum*2 == total - nums[i]){

return i;
}
sum+=nums[i];
}

return -1;
}
}
1 change: 0 additions & 1 deletion Week1/Product Of Array/week1.md

This file was deleted.

21 changes: 21 additions & 0 deletions Week1/Product Of Array/week1_Priyanka
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public int[] productExceptSelf(int[] nums) {
// Product of before * product of after
int[] result = new int[nums.length];

// Product before
result[0] = 1;
for (int i = 1; i < nums.length; i ++) {
result[i] = result[i-1] * nums[i-1];
}

// Product after
int productAfter = 1;
for (int i = nums.length - 2; i >= 0; i--) {
productAfter *= nums[i+1];
result[i] = result[i] * productAfter;
}

return result;
}
}
1 change: 0 additions & 1 deletion Week1/Remove Duplicates/week1.md

This file was deleted.

21 changes: 21 additions & 0 deletions Week1/Remove Duplicates/week1_Priyanka
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
public int removeDuplicates(int[] nums) {

//if length is less than 2 return same length as new length
if(nums.length <2)
return nums.length;

//Initializing pointer 0,1
int firstPointer =0 ,secondPointer =1;

//loop till secondPointer till last index of the array
while(secondPointer< nums.length){

//if value of firstPointer and secondPointer are sane increment secondPointer
//else place secondPointer value in firstPointer+1
if(nums[firstPointer] == nums[secondPointer]) secondPointer++;
else nums[++firstPointer] = nums[secondPointer++];
}

//return firstPointer+1 as new index
return firstPointer+1;
}
1 change: 0 additions & 1 deletion Week1/Reverse Only Letters/week1.md

This file was deleted.

27 changes: 27 additions & 0 deletions Week1/Reverse Only Letters/week1_Priyanka
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution {
public String reverseOnlyLetters(String S) {

if(S.length() == 0)
return S;

String temp = "";
for(int i=S.length()-1; i>=0; i--){
if((S.charAt(i) >= 65 && S.charAt(i) <= 90) ||
(S.charAt(i) >= 97 && S.charAt(i) <= 122))
temp += S.charAt(i);
}

String result = "";
int j = 0;

for(int i=0; i<S.length(); i++){
if((S.charAt(i) >= 65 && S.charAt(i) <= 90) ||
(S.charAt(i) >= 97 && S.charAt(i) <= 122)){
result += temp.charAt(j);
j++;
}else
result += S.charAt(i);
}
return result;
}
}
1 change: 0 additions & 1 deletion Week1/Reverse vowels/week1.md

This file was deleted.

44 changes: 44 additions & 0 deletions Week1/Reverse vowels/week1_Priyanka
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class Solution {
public String reverseVowels(String s) {


char[] element = s.toCharArray();
char temp = 'a' ; // just random initialization

for(int start = 0 , end = element.length-1 ; start < end ; start++){

if(isVowel(element[start])){ // Checks if character is a vowel

while(end > start && !isVowel(element[end]) ) end--; // traverses until vowel is found

if(end > start){ // swaps them
temp = element[start];
element[start] = element[end];
element[end] = temp;
end--;
}

}
}

return String.valueOf(element);



}

public boolean isVowel(char element){

if(element == 'a' ||element == 'e' ||element == 'i' ||element == 'o' ||element == 'u')
{
return true;
}
else if(element == 'A' ||element == 'E' ||element == 'I' ||element == 'O' ||element == 'U')
{
return true;
}

return false;

}
}
1 change: 0 additions & 1 deletion Week1/Rotate Array/week1.md

This file was deleted.

12 changes: 12 additions & 0 deletions Week1/Rotate Array/week1_Priyanka
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
public void rotate(int[] nums, int k) {
int n=nums.length;
k=k%n;
while(k>0){
int last=nums[n-1];
for(int i=nums.length-1;i>=1;i--){
nums[i]=nums[i-1];
}
nums[0]=last;
k--;
}
}
1 change: 0 additions & 1 deletion Week1/Spiral Matrix/week1.md

This file was deleted.

37 changes: 37 additions & 0 deletions Week1/Spiral Matrix/week1_Priyanka
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> list = new ArrayList<>();
int rs = 0;
int re = matrix.length - 1;
int cs = 0;
int ce = matrix[0].length - 1;
while (rs <= re && cs <= ce) {
// go right
for (int i = cs; i <= ce; i++) {
list.add(matrix[rs][i]);
}
rs++;
// go down
for (int i = rs; i <= re; i++) {
list.add(matrix[i][ce]);
}
ce--;
if (rs <= re) {
// go left
for (int i = ce; i >= cs; i--) {
list.add(matrix[re][i]);
}
re--;
}
if (cs <= ce) {
// go up
for (int i = re; i >= rs; i--) {
list.add(matrix[i][cs]);
}
cs++;
}
}
return list;

}
}
1 change: 0 additions & 1 deletion Week1/Target Array/week1.md

This file was deleted.

9 changes: 9 additions & 0 deletions Week1/Target Array/week1_Priyanka
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public int[] createTargetArray(int[] nums, int[] index) {
List<Integer> temp = new ArrayList<>();
for(int i=0; i<nums.length;i++){
temp.add(index[i],nums[i]);
}
int[] result = new int[temp.size()];
for(int i=0; i<temp.size();i++)result[i]=temp.get(i);
return result;
}
1 change: 0 additions & 1 deletion Week1/Valid Palindrome/week1.md

This file was deleted.

Loading