-
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
1 parent
a879e40
commit 732ed6b
Showing
2 changed files
with
54 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,25 @@ | ||
from typing import List | ||
|
||
|
||
class NumArray: | ||
|
||
def __init__(self, nums: List[int]): | ||
self.nums = nums | ||
|
||
self.sums = [0] | ||
for i in range(0, len(nums)): | ||
self.sums.append(nums[i] + self.sums[i]) | ||
|
||
def sumRange(self, left: int, right: int) -> int: | ||
right = right + 1 | ||
res = self.sums[right] if left == 0 else self.sums[right] - self.sums[left] | ||
return res | ||
|
||
|
||
if __name__ == "__main__": | ||
obj = NumArray([-2, 0, 3, -5, 2, -1]) | ||
|
||
assert obj.sumRange(0, 2) == 1 | ||
assert obj.sumRange(2, 5) == -1 | ||
assert obj.sumRange(0, 5) == -3 | ||
|
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 @@ | ||
from collections import Counter | ||
|
||
|
||
class Solution: | ||
def canConstruct(self, ransomNote: str, magazine: str) -> bool: | ||
r = Counter(ransomNote) | ||
m = Counter(magazine) | ||
|
||
for i in ransomNote: | ||
if r[i] > m.get(i, 0): | ||
return False | ||
|
||
return True | ||
|
||
|
||
if __name__ == "__main__": | ||
obj = Solution() | ||
|
||
ransomNote = "a" | ||
magazine = "b" | ||
assert obj.canConstruct(ransomNote, magazine) is False | ||
|
||
ransomNote = "aa" | ||
magazine = "ab" | ||
assert obj.canConstruct(ransomNote, magazine) is False | ||
|
||
ransomNote = "aa" | ||
magazine = "aab" | ||
assert obj.canConstruct(ransomNote, magazine) is True |