Skip to content

Commit dcf97ef

Browse files
committed
Runtime: 5 ms (Top 15.48%) | Memory: 5.9 MB (Top 26.64%)
1 parent 9536ee3 commit dcf97ef

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

scripts/algorithms/U/Ugly Number III/Ugly Number III.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1+
// Runtime: 5 ms (Top 15.48%) | Memory: 5.9 MB (Top 26.64%)
12
// This is a typical Binary Search Problem Here I did Binary Search and Optimized my lcm function a lot.
2-
// Here Number of Ugly numbers for any number is
3+
// Here Number of Ugly numbers for any number is
34
// that number/a + that number/b + that number/c + that number/lcm(a,b,c) - that number/lcm(a,b) - that number/lcm(b,c) - that number/(a,c) and howzz that??
4-
// See Lets suppose that number is 17 for which you are checking values and a = 2 , b=3 and c= 4 now figure out
5-
// all the possible values for a = 2,4,6,8,10,12,14,16
6-
// b = 3,6,9,12,15
7-
// c = 4,8,12,16
8-
// Now if we add them all we can see 4,6,8,16 are coming twice and 12 is coming thrice so we do lcm(2,3) = 6
9-
// then we are basically multiple occurance of numbers divisible by 6 simlarly for lcm(2,4) & lcm(3,4)
10-
// but any number which is divisble by all three of them we have deleted it 3 times we need at least so we are adding numbers which are divisble by lcm(2,3,4) which is 12 here So if suppose we are countering more numbers than n then h = mid-1 we need to move backward else we need to forward.
5+
// See Lets suppose that number is 17 for which you are checking values and a = 2 , b=3 and c= 4 now figure out
6+
// all the possible values for a = 2,4,6,8,10,12,14,16
7+
// b = 3,6,9,12,15
8+
// c = 4,8,12,16
9+
// Now if we add them all we can see 4,6,8,16 are coming twice and 12 is coming thrice so we do lcm(2,3) = 6
10+
// then we are basically multiple occurance of numbers divisible by 6 simlarly for lcm(2,4) & lcm(3,4)
11+
// but any number which is divisble by all three of them we have deleted it 3 times we need at least so we are adding numbers which are divisble by lcm(2,3,4) which is 12 here So if suppose we are countering more numbers than n then h = mid-1 we need to move backward else we need to forward.
1112
class Solution {
1213
public:
1314
#define ll long long
@@ -44,7 +45,7 @@ class Solution {
4445
ll h = INT_MAX;
4546
while(l<=h)
4647
{
47-
ll mid = l + (h-l)/2;
48+
ll mid = l + (h-l)/2;
4849
if(check(mid,a,b,c,n) && (mid%a==0 || mid%b==0 || mid%c==0))
4950
{
5051
return mid;
@@ -63,4 +64,4 @@ class Solution {
6364
}
6465
return 1;
6566
}
66-
};
67+
};

0 commit comments

Comments
 (0)