-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c85a106
commit 8343b96
Showing
16 changed files
with
445 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} |
Oops, something went wrong.