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.
Solution of zigzag iterator, segment tree, running average, BT longes…
…t consecutive chain, senetence fitting
- Loading branch information
Showing
6 changed files
with
287 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,35 @@ | ||
''' | ||
Given two 1d vectors, implement an iterator to return their elements alternately. | ||
For example, given two 1d vectors: | ||
v1 = [1, 2] | ||
v2 = [3, 4, 5, 6] | ||
By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1, 3, 2, 4, 5, 6]. | ||
''' | ||
|
||
class Solution(object): | ||
def __init__(self, v1, v2): | ||
self.v1 = v1 | ||
self.v2 = v2 | ||
self.index_v1 = 0 | ||
self.index_v2 = 0 | ||
|
||
def next(self): | ||
result = -1 | ||
if self.index_v1 != len(self.v1) and self.index_v1 <= self.index_v2: | ||
result = self.v1[self.index_v1] | ||
self.index_v1 += 1 | ||
else: | ||
result = self.v2[self.index_v2] | ||
self.index_v2 += 1 | ||
|
||
return result | ||
|
||
def hasNext(self): | ||
return self.index_v1 < len(self.v1) or self.index_v2 < len(self.v2) | ||
|
||
|
||
solution = Solution([1, 2], [3, 4, 5, 6]) | ||
while solution.hasNext(): | ||
print solution.next() |
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,41 @@ | ||
''' | ||
Given a binary tree, find the length of the longest consecutive sequence path. | ||
The path refers to any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The longest consecutive path need to be from parent to child (cannot be the reverse). | ||
For example, | ||
1 | ||
\ | ||
3 | ||
/ \ | ||
2 4 | ||
\ | ||
5 | ||
Longest consecutive sequence path is 3-4-5, so return 3. | ||
2 | ||
\ | ||
3 | ||
/ | ||
2 | ||
/ | ||
1 | ||
Longest consecutive sequence path is 2-3,not3-2-1, so return 2. | ||
''' | ||
|
||
|
||
class Solution(object): | ||
def dfs(curr, parent, length): | ||
if not curr: | ||
return length | ||
if parent: | ||
length = length + 1 if curr.val == parent.val + 1 | ||
else: | ||
length = 1 | ||
|
||
return max(length, max(dfs(curr.left, curr, length), dfs(curr.right, curr, length))) | ||
|
||
def longestConsecutive(TreeNode root): | ||
if not root: | ||
return 0 | ||
|
||
return dfs(root, null, 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,93 @@ | ||
''' | ||
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. | ||
The update(i, val) function modifies nums by updating the element at index i to val. | ||
Example: | ||
Given nums = [1, 3, 5] | ||
sumRange(0, 2) -> 9 | ||
update(1, 2) | ||
sumRange(0, 2) -> 8 | ||
Note: | ||
The array is only modifiable by the update function. | ||
You may assume the number of calls to update and sumRange function is distributed evenly. | ||
''' | ||
|
||
class Node(object): | ||
def __init__(self, val ,start, end): | ||
self.sum = val | ||
self.right, self.left = None, None | ||
self.range= [start, end] | ||
|
||
class SegementTree(object): | ||
def __init__(self, size): | ||
self.root = self._build_segment_tree(0, size-1) | ||
|
||
def _build_segment_tree(self, start, end): | ||
if start > end: | ||
return None | ||
node = Node(0, start, end) | ||
if start == end: | ||
return node | ||
mid = (start+end)/2 | ||
node.left, node.right = self._build_segment_tree(start, mid), self._build_segment_tree(mid+1, end) | ||
return node | ||
|
||
def update(self, index, val, root=None): | ||
root = root or self.root | ||
if index < root.range[0] or index > root.range[1]: | ||
return | ||
root.sum += val | ||
if index == root.range[0] == root.range[1]: | ||
return | ||
self.update(index, val, root.left) | ||
self.update(index, val, root.right) | ||
|
||
def range_sum(self, start, end, root=None): | ||
root = root or self.root | ||
if end < root.range[0] or start > root.range[1]: | ||
return 0 | ||
if start <= root.range[0] and end >= root.range[1]: | ||
return root.sum | ||
return self.range_sum(start, end, root.left) + self.range_sum(start, end, root.right) | ||
|
||
|
||
class NumArray(object): | ||
|
||
def __init__(self, nums): | ||
""" | ||
:type nums: List[int] | ||
""" | ||
self.nums = nums | ||
self.segment_tree = SegementTree(len(nums)) | ||
for index, num in enumerate(nums): | ||
self.segment_tree.update(index, num) | ||
|
||
|
||
def update(self, i, val): | ||
""" | ||
:type i: int | ||
:type val: int | ||
:rtype: None | ||
""" | ||
diff = val-self.nums[i] | ||
self.segment_tree.update(i, diff) | ||
self.nums[i] = val | ||
|
||
|
||
def sumRange(self, i, j): | ||
""" | ||
:type i: int | ||
:type j: int | ||
:rtype: int | ||
""" | ||
return self.segment_tree.range_sum(i, j) | ||
|
||
|
||
# Your NumArray object will be instantiated and called as such: | ||
# obj = NumArray(nums) | ||
# obj.update(i,val) | ||
# param_2 = obj.sumRange(i,j) |
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 a stream of integers and a window size, calculate the moving average of all integers in the sliding window. | ||
For example, | ||
MovingAverage m = new MovingAverage(3); | ||
m.next(1) = 1 | ||
m.next(10) = (1 + 10) / 2 | ||
m.next(3) = (1 + 10 + 3) / 3 | ||
m.next(5) = (10 + 3 + 5) / 3 | ||
''' | ||
|
||
class Solution(object): | ||
def __init__(self): | ||
self.queue = [] | ||
self.curr_sum = 0 | ||
|
||
def movingAverage(self, num, size): | ||
if len(self.queue) >= size: | ||
val = self.queue.pop(0) | ||
self.curr_sum -= val | ||
|
||
self.curr_sum += num | ||
self.queue.append(num) | ||
return float(self.curr_sum)/len(self.queue) | ||
|
||
|
||
|
||
solution = Solution() | ||
window_size = int(input()) | ||
num = int(input()) | ||
while num != -1: | ||
print solution.movingAverage(num, window_size) | ||
num = int(input()) |
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,80 @@ | ||
''' | ||
Given a rows x cols screen and a sentence represented by a list of non-empty words, find how many times the given sentence can be fitted on the screen. | ||
Note: | ||
A word cannot be split into two lines. | ||
The order of words in the sentence must remain unchanged. | ||
Two consecutive words in a line must be separated by a single space. | ||
Total words in the sentence won't exceed 100. | ||
Length of each word is greater than 0 and won't exceed 10. | ||
1 ≤ rows, cols ≤ 20,000. | ||
Example 1: | ||
Input: | ||
rows = 2, cols = 8, sentence = ["hello", "world"] | ||
Output: | ||
1 | ||
Explanation: | ||
hello--- | ||
world--- | ||
The character '-' signifies an empty space on the screen. | ||
Example 2: | ||
Input: | ||
rows = 3, cols = 6, sentence = ["a", "bcd", "e"] | ||
Output: | ||
2 | ||
Explanation: | ||
a-bcd- | ||
e-a--- | ||
bcd-e- | ||
The character '-' signifies an empty space on the screen. | ||
Example 3: | ||
Input: | ||
rows = 4, cols = 5, sentence = ["I", "had", "apple", "pie"] | ||
Output: | ||
1 | ||
Explanation: | ||
I-had | ||
apple | ||
pie-I | ||
had-- | ||
The character '-' signifies an empty space on the screen. | ||
''' | ||
|
||
class Solution(object): | ||
def wordsTyping(self, sentences, rows, cols): | ||
"""" | ||
:sentences List<String> | ||
:rows int | ||
:cols int | ||
""" | ||
sentence = '-'.join(sentences) | ||
sentence += '-' | ||
|
||
index_in_sentence = 0 | ||
for row in range(rows): | ||
index_in_sentence += cols | ||
if sentence[(index_in_sentence%len(sentence))] == '-': | ||
index_in_sentence += 1 | ||
else: | ||
while index_in_sentence > 0 and sentence[((index_in_sentence - 1)%len(sentence))] != '-': | ||
index_in_sentence -= 1 | ||
|
||
return index_in_sentence/len(sentence) | ||
|
||
solution = Solution() | ||
row, col = 3, 6 | ||
sentences = ["a", "bcd", "e"] | ||
print solution.wordsTyping(sentences=sentences, rows=row, cols=col) |
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