forked from Garvit244/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
8 changed files
with
288 additions
and
0 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,33 @@ | ||
''' | ||
Given an integer number n, return the difference between the product of its digits and the sum of its digits. | ||
Example 1: | ||
Input: n = 234 | ||
Output: 15 | ||
Explanation: | ||
Product of digits = 2 * 3 * 4 = 24 | ||
Sum of digits = 2 + 3 + 4 = 9 | ||
Result = 24 - 9 = 15 | ||
Example 2: | ||
Input: n = 4421 | ||
Output: 21 | ||
Explanation: | ||
Product of digits = 4 * 4 * 2 * 1 = 32 | ||
Sum of digits = 4 + 4 + 2 + 1 = 11 | ||
Result = 32 - 11 = 21 | ||
''' | ||
|
||
class Solution(object): | ||
def subtractProductAndSum(self, n): | ||
""" | ||
:type n: int | ||
:rtype: int | ||
""" | ||
|
||
from functools import reduce | ||
from operator import mul | ||
digits = [int(x) for x in str(n)] | ||
return reduce(mul, digits) - sum(digits) |
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 @@ | ||
''' | ||
There are n people whose IDs go from 0 to n - 1 and each person belongs exactly to one group. Given the array groupSizes of length n telling the group size each person belongs to, return the groups there are and the people's IDs each group includes. | ||
You can return any solution in any order and the same applies for IDs. Also, it is guaranteed that there exists at least one solution. | ||
Example 1: | ||
Input: groupSizes = [3,3,3,3,3,1,3] | ||
Output: [[5],[0,1,2],[3,4,6]] | ||
Explanation: | ||
Other possible solutions are [[2,1,6],[5],[0,4,3]] and [[5],[0,6,2],[4,3,1]]. | ||
Example 2: | ||
Input: groupSizes = [2,1,3,3,3,2] | ||
Output: [[1],[0,5],[2,3,4]] | ||
''' | ||
class Solution(object): | ||
def groupThePeople(self, groupSizes): | ||
""" | ||
:type groupSizes: List[int] | ||
:rtype: List[List[int]] | ||
""" | ||
count = collections.defaultdict(list) | ||
for i, size in enumerate(groupSizes): | ||
count[size].append(i) | ||
result = [] | ||
for s, value in count.items(): | ||
for index in range(0, len(value), s): | ||
result.append(value[index:index + s]) | ||
return result |
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,43 @@ | ||
''' | ||
Given an array of integers nums and an integer threshold, we will choose a positive integer divisor and divide all the array by it and sum the result of the division. Find the smallest divisor such that the result mentioned above is less than or equal to threshold. | ||
Each result of division is rounded to the nearest integer greater than or equal to that element. (For example: 7/3 = 3 and 10/2 = 5). | ||
It is guaranteed that there will be an answer. | ||
Example 1: | ||
Input: nums = [1,2,5,9], threshold = 6 | ||
Output: 5 | ||
Explanation: We can get a sum to 17 (1+2+5+9) if the divisor is 1. | ||
If the divisor is 4 we can get a sum to 7 (1+1+2+3) and if the divisor is 5 the sum will be 5 (1+1+1+2). | ||
Example 2: | ||
Input: nums = [2,3,5,7,11], threshold = 11 | ||
Output: 3 | ||
Example 3: | ||
Input: nums = [19], threshold = 5 | ||
Output: 4 | ||
''' | ||
class Solution(object): | ||
def smallestDivisor(self, nums, threshold): | ||
""" | ||
:type nums: List[int] | ||
:type threshold: int | ||
:rtype: int | ||
""" | ||
def getSum(divisor, xs): | ||
return sum([x // divisor + 1 if x % divisor else x // divisor for x in xs]) | ||
|
||
left, right = 1, 10 ** 6 | ||
while left + 1 < right: | ||
mid = (left + right) // 2 | ||
if getSum(mid, nums) > threshold: | ||
left = mid | ||
else: | ||
right = mid | ||
|
||
return left if getSum(left, nums) <= threshold else right |
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,50 @@ | ||
''' | ||
Given head which is a reference node to a singly-linked list. The value of each node in the linked list is either 0 or 1. The linked list holds the binary representation of a number. | ||
Return the decimal value of the number in the linked list. | ||
Example 1: | ||
Input: head = [1,0,1] | ||
Output: 5 | ||
Explanation: (101) in base 2 = (5) in base 10 | ||
Example 2: | ||
Input: head = [0] | ||
Output: 0 | ||
Example 3: | ||
Input: head = [1] | ||
Output: 1 | ||
Example 4: | ||
Input: head = [1,0,0,1,0,0,1,1,1,0,0,0,0,0,0] | ||
Output: 18880 | ||
Example 5: | ||
Input: head = [0,0] | ||
Output: 0 | ||
''' | ||
|
||
# Definition for singly-linked list. | ||
# class ListNode(object): | ||
# def __init__(self, x): | ||
# self.val = x | ||
# self.next = None | ||
|
||
class Solution(object): | ||
def getDecimalValue(self, head): | ||
""" | ||
:type head: ListNode | ||
:rtype: int | ||
""" | ||
result = '' | ||
if not head: | ||
return 0 | ||
while head: | ||
result+= str(head.val) | ||
head = head.next | ||
return int(result, 2) |
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,53 @@ | ||
''' | ||
An integer has sequential digits if and only if each digit in the number is one more than the previous digit. | ||
Return a sorted list of all the integers in the range [low, high] inclusive that have sequential digits. | ||
Example 1: | ||
Input: low = 100, high = 300 | ||
Output: [123,234] | ||
Example 2: | ||
Input: low = 1000, high = 13000 | ||
Output: [1234,2345,3456,4567,5678,6789,12345] | ||
''' | ||
class Solution(object): | ||
def sequentialDigits(self, low, high): | ||
""" | ||
:type low: int | ||
:type high: int | ||
:rtype: List[int] | ||
""" | ||
result = [] | ||
start = int(str(low)[0]) | ||
for val in range(1, len(str(low))): | ||
new_val = start%10 + 1 | ||
start = start*10 + new_val | ||
if start > high: | ||
return result | ||
|
||
result.append(start) | ||
|
||
while result[-1] <= high: | ||
temp = str(result[-1]) | ||
next_elem = int(temp[-1]) + 1 | ||
|
||
if next_elem > 9: | ||
next_greater = 0 | ||
for index in range(len(temp) + 1): | ||
next_greater = next_greater*10 + (index+1) | ||
else: | ||
next_greater = int(temp[1:]) * 10 + next_elem | ||
if next_greater <= high: | ||
result.append(next_greater) | ||
else: | ||
break | ||
# print next_greater | ||
final_result = [] | ||
for val in result: | ||
if '0' not in str(val) and val >= low: | ||
final_result.append(val) | ||
return final_result |
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,29 @@ | ||
''' | ||
Given an array nums of integers, return how many of them contain an even number of digits. | ||
Example 1: | ||
Input: nums = [12,345,2,6,7896] | ||
Output: 2 | ||
Explanation: | ||
12 contains 2 digits (even number of digits). | ||
345 contains 3 digits (odd number of digits). | ||
2 contains 1 digit (odd number of digits). | ||
6 contains 1 digit (odd number of digits). | ||
7896 contains 4 digits (even number of digits). | ||
Therefore only 12 and 7896 contain an even number of digits. | ||
Example 2: | ||
Input: nums = [555,901,482,1771] | ||
Output: 1 | ||
Explanation: | ||
Only 1771 contains an even number of digits. | ||
''' | ||
class Solution(object): | ||
def findNumbers(self, nums): | ||
""" | ||
:type nums: List[int] | ||
:rtype: int | ||
""" | ||
return len([num for num in nums if len(str(num))%2 == 0]) |
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,43 @@ | ||
''' | ||
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers | ||
Return True if its possible otherwise return False. | ||
Example 1: | ||
Input: nums = [1,2,3,3,4,4,5,6], k = 4 | ||
Output: true | ||
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6]. | ||
Example 2: | ||
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3 | ||
Output: true | ||
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11]. | ||
Example 3: | ||
Input: nums = [3,3,2,2,1,1], k = 3 | ||
Output: true | ||
Example 4: | ||
Input: nums = [1,2,3,4], k = 3 | ||
Output: false | ||
Explanation: Each array should be divided in subarrays of size 3. | ||
''' | ||
class Solution(object): | ||
def isPossibleDivide(self, nums, k): | ||
""" | ||
:type nums: List[int] | ||
:type k: int | ||
:rtype: bool | ||
""" | ||
from collections import Counter | ||
count_map = Counter(nums) | ||
for num in sorted(count_map.keys()): | ||
if count_map[num] <= 0: | ||
continue | ||
for index in range(1, k): | ||
count_map[num+index] -= count_map[num] | ||
if count_map[num+index] < 0: | ||
return False | ||
return True |
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