Skip to content

Commit 24d1a9b

Browse files
committed
Adding solution of 1012, 1013 problem
1 parent dea452c commit 24d1a9b

File tree

3 files changed

+95
-1
lines changed

3 files changed

+95
-1
lines changed

1000-1100q/1012.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'''Every non-negative integer N has a binary representation. For example, 5 can be represented as "101" in binary, 11 as "1011" in binary, and so on. Note that except for N = 0, there are no leading zeroes in any binary representation.
2+
3+
The complement of a binary representation is the number in binary you get when changing every 1 to a 0 and 0 to a 1. For example, the complement of "101" in binary is "010" in binary.
4+
5+
For a given number N in base-10, return the complement of it's binary representation as a base-10 integer.
6+
7+
8+
9+
Example 1:
10+
11+
Input: 5
12+
Output: 2
13+
Explanation: 5 is "101" in binary, with complement "010" in binary, which is 2 in base-10.
14+
Example 2:
15+
16+
Input: 7
17+
Output: 0
18+
Explanation: 7 is "111" in binary, with complement "000" in binary, which is 0 in base-10.
19+
Example 3:
20+
21+
Input: 10
22+
Output: 5
23+
Explanation: 10 is "1010" in binary, with complement "0101" in binary, which is 5 in base-10.
24+
25+
26+
Note:
27+
28+
0 <= N < 10^9
29+
'''
30+
31+
class Solution(object):
32+
def bitwiseComplement(self, N):
33+
"""
34+
:type N: int
35+
:rtype: int
36+
"""
37+
if N == 0:
38+
return 1
39+
import math
40+
bits = (int)(math.floor(math.log(N) /math.log(2))) + 1
41+
return ((1 << bits) - 1) ^ N

1013.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'''
2+
In a list of songs, the i-th song has a duration of time[i] seconds.
3+
4+
Return the number of pairs of songs for which their total duration in seconds is divisible by 60. Formally, we want the number of indices i < j with (time[i] + time[j]) % 60 == 0.
5+
6+
7+
8+
Example 1:
9+
10+
Input: [30,20,150,100,40]
11+
Output: 3
12+
Explanation: Three pairs have a total duration divisible by 60:
13+
(time[0] = 30, time[2] = 150): total duration 180
14+
(time[1] = 20, time[3] = 100): total duration 120
15+
(time[1] = 20, time[4] = 40): total duration 60
16+
Example 2:
17+
18+
Input: [60,60,60]
19+
Output: 3
20+
Explanation: All three pairs have a total duration of 120, which is divisible by 60.
21+
22+
Note:
23+
24+
1 <= time.length <= 60000
25+
1 <= time[i] <= 500
26+
'''
27+
28+
class Solution(object):
29+
def numPairsDivisibleBy60(self, time):
30+
"""
31+
:type time: List[int]
32+
:rtype: int
33+
"""
34+
count_arr = [0]*60
35+
result = 0
36+
for t in time:
37+
remainder = t%60
38+
complement = (60-remainder)%60
39+
result += count_arr[complement]
40+
count_arr[remainder] += 1
41+
return result
42+
43+
'''
44+
Explanation:
45+
Q1: why create array of size 60?
46+
it is similar to the map which store the count. Why only 60 because 60 modulo of number cannot be more than 60
47+
Q2: why we need complement?
48+
to check the pair if it exisit with given value or not example: if remainder is 20 then we need to check if we have any number with remainder 40 or not.
49+
Q3: why 60 modulo complement?
50+
for handle cause when remainder is zero
51+
'''

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ Python solution of problems from [LeetCode](https://leetcode.com/).
77
##### [Problems 1000-1100](./1000-1100q/)
88
| # | Title | Solution | Difficulty |
99
|---| ----- | -------- | ---------- |
10-
|1002|[Find Common Characters](https://leetcode.com/problems/find-common-characters)|[Python](./1000-1100q/1002.py)|Easy|
1110
|1014|[Capacity To Ship Packages Within D Days](https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/) | [Python](./1000-1100q/1014.py) | Medium|
11+
|1013|[Pairs of Songs With Total Durations Divisible by 60](https://leetcode.com/problems/pairs-of-songs-with-total-durations-divisible-by-60/)|[Python](./1000-1100q/1013.py)|Easy|
12+
|1012|[Complement of Base 10 Integer](https://leetcode.com/problems/complement-of-base-10-integer/) | [Python](./1000-1100q/1012.py)|Easy|
13+
|1002|[Find Common Characters](https://leetcode.com/problems/find-common-characters)|[Python](./1000-1100q/1002.py)|Easy|
1214

1315
##### [Problems 900-1000](./900-1000q/)
1416
| # | Title | Solution | Difficulty |

0 commit comments

Comments
 (0)