Skip to content

Commit

Permalink
weeklyupdate
Browse files Browse the repository at this point in the history
  • Loading branch information
Akshaya-Amar committed Nov 22, 2021
1 parent c85a106 commit 8343b96
Show file tree
Hide file tree
Showing 16 changed files with 445 additions and 25 deletions.
23 changes: 23 additions & 0 deletions C++/FindNumberswithEvenNumberofDigits.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
Source: https://leetcode.com/problems/find-numbers-with-even-number-of-digits/
Time: O(n), where n is the length of the array
Space: O(1), in-place
*/

class Solution {
public:
int findNumbers(vector<int>& nums) {

int count = 0;
for(int num : nums) {
if((num >= 10 && num < 100) || (num >= 1000 && num < 10000) || (num == 100000)) {
++count;
}
}

return count;
}
};
30 changes: 30 additions & 0 deletions C++/KidsWiththeGreatestNumberofCandies.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
Source: https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/
Time: O(N), where N is the length of candies array i.e. N is the number of elements present in the array
Space: O(N), not in-place as we need a list of size equal to the size of candies array to store their corresponding boolean values
*/

class Solution {
public:
vector<bool> kidsWithCandies(vector<int>& candies, int extraCandies) {

int max = 0;
for(int candy : candies) {
if(candy > max) {
max = candy;
}
}

max -= extraCandies;
vector<bool> res;

for(int candy : candies) {
res.push_back(candy >= max);
}

return res;
}
};
14 changes: 14 additions & 0 deletions C++/NumberofGoodPairs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
public:
int numIdenticalPairs(vector<int>& nums) {

int freq[101] = {0};
int goodPairsCount = 0;

for(int num : nums) {
goodPairsCount += freq[num]++;
}

return goodPairsCount;
}
};
26 changes: 26 additions & 0 deletions C++/NumberofStepstoReduceaNumbertoZero.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
Source: https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/
Time: O(logn), where log base2 n is the number of iterations done
Space: O(1), in-place
*/

class Solution {
public:
int numberOfSteps(int num) {

if(num == 0) {
return 0;
}

int steps = 0;
while(num != 0) {
steps += (num & 1) + 1;
num >>= 1;
}

return steps - 1;
}
};
26 changes: 26 additions & 0 deletions C++/SubtracttheProductandSumofDigitsofanInteger.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
Source: https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/
Time: O(n), where n is the given number
Space: O(1), in-place
*/

class Solution {
public:
int subtractProductAndSum(int n) {

int product = 1;
int sum = 0;

while(n != 0) {
int digit = n % 10;
product *= digit;
sum += digit;
n /= 10;
}

return product - sum;
}
};
32 changes: 32 additions & 0 deletions C++/UglyNumber.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
Source: https://leetcode.com/problems/ugly-number/
Time: O(log(base2)n), where log(base2)n is the max number of iteration done for a given integer n
Space: O(1), in-place
*/

class Solution {
public:
bool isUgly(int n) {

if(n <= 0) {
return false;
}

while((n & 1) == 0) {
n >>= 1;
}

while(n % 3 == 0) {
n /= 3;
}

while(n % 5 == 0) {
n /= 5;
}

return n == 1;
}
};
47 changes: 47 additions & 0 deletions C++/UglyNumberII.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
Source: https://leetcode.com/problems/ugly-number-ii/
Time: O(n), where n is a given positive integer
Space: O(1), in-place
*/

class Solution {
public:
int nthUglyNumber(int n) {

int uglyNumbers[n];
uglyNumbers[0] = 1;
int multipleOf2 = 2;
int multipleOf3 = 3;
int multipleOf5 = 5;
int ptr2 = 0;
int ptr3 = 0;
int ptr5 = 0;
int index = 1;

while(index < n) {

int minValue = (multipleOf2 < multipleOf3) ? multipleOf2 : multipleOf3;

if(multipleOf5 < minValue) {
minValue = multipleOf5;
}

uglyNumbers[index++] = minValue;

if(minValue == multipleOf2) {
multipleOf2 = uglyNumbers[++ptr2] << 1;
}
if(minValue == multipleOf3) {
multipleOf3 = 3 * uglyNumbers[++ptr3];
}
if(minValue == multipleOf5) {
multipleOf5 = 5 * uglyNumbers[++ptr5];
}
}

return uglyNumbers[index - 1];
}
};
13 changes: 13 additions & 0 deletions Java/FindNumberswithEvenNumberofDigits.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution {
public int findNumbers(int[] nums) {

int count = 0;
for(int num : nums) {
if((num >= 10 && num < 100) || (num >= 1000 && num < 10000) || (num == 100000)) {
++count;
}
}

return count;
}
}
28 changes: 28 additions & 0 deletions Java/KidsWiththeGreatestNumberofCandies.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Source: https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/
Time: O(N), where N is the length of candies array i.e. N is the number of elements present in the array
Space: O(N), not in-place as we need a list of size equal to the size of candies array to store their corresponding boolean values
*/

class Solution {
public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {

int max = 0;
for(int candy : candies) {
if(candy > max) {
max = candy;
}
}

max -= extraCandies;
List<Boolean> res = new ArrayList<>(candies.length);
for(int candy : candies) {
res.add(candy >= max);
}

return res;
}
}
13 changes: 13 additions & 0 deletions Java/NumberofGoodPairs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution {
public int numIdenticalPairs(int[] nums) {

int[] freq = new int[101];
int goodPairsCount = 0;

for(int num : nums) {
goodPairsCount += freq[num]++;
}

return goodPairsCount;
}
}
25 changes: 25 additions & 0 deletions Java/NumberofStepstoReduceaNumbertoZero.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
Source: https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/
Time: O(logn), where log base2 n is the number of iterations done
Space: O(1), in-place
*/

class Solution {
public int numberOfSteps(int num) {

if(num == 0) {
return 0;
}

int steps = 0;
while(num != 0) {
steps += (num & 1) + 1;
num >>= 1;
}

return steps - 1;
}
}
25 changes: 25 additions & 0 deletions Java/SubtracttheProductandSumofDigitsofanInteger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
Source: https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/
Time: O(n), where n is the given number
Space: O(1), in-place
*/

class Solution {
public int subtractProductAndSum(int n) {

int product = 1;
int sum = 0;

while(n != 0) {
int digit = n % 10;
product *= digit;
sum += digit;
n /= 10;
}

return product - sum;
}
}
31 changes: 31 additions & 0 deletions Java/UglyNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
Source: https://leetcode.com/problems/ugly-number/
Time: O(log(base2)n), where log(base2)n is the max number of iteration done for a given integer n
Space: O(1), in-place
*/

class Solution {
public boolean isUgly(int n) {

if(n <= 0) {
return false;
}

while((n & 1) == 0) {
n >>= 1;
}

while(n % 3 == 0) {
n /= 3;
}

while(n % 5 == 0) {
n /= 5;
}

return n == 1;
}
}
46 changes: 46 additions & 0 deletions Java/UglyNumberII.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
Source: https://leetcode.com/problems/ugly-number-ii/
Time: O(n), where n is a given positive integer
Space: O(1), in-place
*/

class Solution {
public int nthUglyNumber(int n) {

int[] uglyNumbers = new int[n];
uglyNumbers[0] = 1;
int multipleOf2 = 2;
int multipleOf3 = 3;
int multipleOf5 = 5;
int ptr2 = 0;
int ptr3 = 0;
int ptr5 = 0;
int index = 1;

while(index < n) {

int minValue = (multipleOf2 < multipleOf3) ? multipleOf2 : multipleOf3;

if(multipleOf5 < minValue) {
minValue = multipleOf5;
}

uglyNumbers[index++] = minValue;

if(minValue == multipleOf2) {
multipleOf2 = uglyNumbers[++ptr2] << 1;
}
if(minValue == multipleOf3) {
multipleOf3 = 3 * uglyNumbers[++ptr3];
}
if(minValue == multipleOf5) {
multipleOf5 = 5 * uglyNumbers[++ptr5];
}
}

return uglyNumbers[index - 1];
}
}
Loading

0 comments on commit 8343b96

Please sign in to comment.