Skip to content

Commit d331b73

Browse files
committed
08/12/2020
1 parent e2b859c commit d331b73

File tree

3 files changed

+34
-79
lines changed

3 files changed

+34
-79
lines changed

Daily-challenge/Dec/07/README.md

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,16 @@ Constraints:
2222

2323
## Code
2424
```python
25-
# Definition for singly-linked list.
26-
# class ListNode:
27-
# def __init__(self, val=0, next=None):
28-
# self.val = val
29-
# self.next = next
3025
class Solution:
31-
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
32-
s1, s2 = [], []
33-
while l1:
34-
s1.append(l1.val)
35-
l1 = l1.next
36-
37-
while l2:
38-
s2.append(l2.val)
39-
l2 = l2.next
40-
41-
carry = 0
42-
head = ListNode(0)
43-
while s1 or s2 or carry:
44-
if s1:
45-
carry += s1.pop()
46-
if s2:
47-
carry += s2.pop()
48-
carry, val = divmod(carry, 10)
49-
head.next, head.next.next = ListNode(val), head.next
50-
return head.next07
26+
def generateMatrix(self, n: int) -> List[List[int]]:
27+
A = [[0] * n for _ in range(n)]
28+
i, j, di, dj = 0, 0, 0, 1
29+
for k in range(n*n):
30+
A[i][j] = k + 1
31+
if A[(i+di)%n][(j+dj)%n]:
32+
di, dj = dj, -di
33+
i += di
34+
j += dj
35+
return A
5136

5237
```

Daily-challenge/Dec/07/sol.py

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,11 @@
1-
# Definition for singly-linked list.
2-
# class ListNode:
3-
# def __init__(self, val=0, next=None):
4-
# self.val = val
5-
# self.next = next
61
class Solution:
7-
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
8-
s1, s2 = [], []
9-
while l1:
10-
s1.append(l1.val)
11-
l1 = l1.next
12-
13-
while l2:
14-
s2.append(l2.val)
15-
l2 = l2.next
16-
17-
carry = 0
18-
head = ListNode(0)
19-
while s1 or s2 or carry:
20-
if s1:
21-
carry += s1.pop()
22-
if s2:
23-
carry += s2.pop()
24-
carry, val = divmod(carry, 10)
25-
head.next, head.next.next = ListNode(val), head.next
26-
return head.next
2+
def generateMatrix(self, n: int) -> List[List[int]]:
3+
A = [[0] * n for _ in range(n)]
4+
i, j, di, dj = 0, 0, 0, 1
5+
for k in range(n*n):
6+
A[i][j] = k + 1
7+
if A[(i+di)%n][(j+dj)%n]:
8+
di, dj = dj, -di
9+
i += di
10+
j += dj
11+
return A

Daily-challenge/Dec/08/README.md

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,29 @@
1-
# Binary Tree Tilt
2-
Given the root of a binary tree, return the sum of every tree node's tilt.
1+
# Pairs of Songs With Total Durations Divisible by 60
2+
You are given a list of songs where the ith song has a duration of time[i] seconds.
33

4-
The tilt of a tree node is the absolute difference between the sum of all left subtree node values and all right subtree node values. If a node does not have a left child, then the sum of the left subtree node values is treated as 0. The rule is similar if there the node does not have a right child.
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 such that i < j with (time[i] + time[j]) % 60 == 0.
55

66

77

88
Example 1:
99

10-
11-
Input: root = [1,2,3]
12-
Output: 1
13-
Explanation:
14-
Tilt of node 2 : |0-0| = 0 (no children)
15-
Tilt of node 3 : |0-0| = 0 (no children)
16-
Tile of node 1 : |2-3| = 1 (left subtree is just left child, so sum is 2; right subtree is just right child, so sum is 3)
17-
Sum of every tilt : 0 + 0 + 1 = 1
10+
Input: time = [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
1816
Example 2:
1917

20-
21-
Input: root = [4,2,9,3,5,null,7]
22-
Output: 15
23-
Explanation:
24-
Tilt of node 3 : |0-0| = 0 (no children)
25-
Tilt of node 5 : |0-0| = 0 (no children)
26-
Tilt of node 7 : |0-0| = 0 (no children)
27-
Tilt of node 2 : |3-5| = 2 (left subtree is just left child, so sum is 3; right subtree is just right child, so sum is 5)
28-
Tilt of node 9 : |0-7| = 7 (no left child, so sum is 0; right subtree is just right child, so sum is 7)
29-
Tilt of node 4 : |(3+5+2)-(9+7)| = |10-16| = 6 (left subtree values are 3, 5, and 2, which sums to 10; right subtree values are 9 and 7, which sums to 16)
30-
Sum of every tilt : 0 + 0 + 0 + 2 + 7 + 6 = 15
31-
Example 3:
32-
33-
34-
Input: root = [21,7,14,1,1,2,2,3,3]
35-
Output: 9
18+
Input: time = [60,60,60]
19+
Output: 3
20+
Explanation: All three pairs have a total duration of 120, which is divisible by 60.
3621

3722

3823
Constraints:
3924

40-
The number of nodes in the tree is in the range [0, 104].
41-
-1000 <= Node.val <= 1000 <br>
25+
1 <= time.length <= 6 * 104
26+
1 <= time[i] <= 500 <br>
4227

4328
## Idea
4429

0 commit comments

Comments
 (0)