Skip to content

Commit aa441bf

Browse files
committed
idk
1 parent b1c9563 commit aa441bf

File tree

4 files changed

+100
-97
lines changed

4 files changed

+100
-97
lines changed

Daily-challenge/2021/Jan/04/README.md

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,52 @@
1-
# The kth Factor of n
2-
Given two positive integers n and k.
1+
# Merge Two Sorted Lists
32

4-
A factor of an integer n is defined as an integer i where n % i == 0.
5-
6-
Consider a list of all factors of n sorted in ascending order, return the kth factor in this list or return -1 if n has less than k factors.
3+
Solution
4+
Merge two sorted linked lists and return it as a sorted list. The list should be made by splicing together the nodes of the first two lists.
75

86

97

108
Example 1:
119

12-
Input: n = 12, k = 3
13-
Output: 3
14-
Explanation: Factors list is [1, 2, 3, 4, 6, 12], the 3rd factor is 3.
10+
11+
Input: l1 = [1,2,4], l2 = [1,3,4]
12+
Output: [1,1,2,3,4,4]
1513
Example 2:
1614

17-
Input: n = 7, k = 2
18-
Output: 7
19-
Explanation: Factors list is [1, 7], the 2nd factor is 7.
15+
Input: l1 = [], l2 = []
16+
Output: []
2017
Example 3:
2118

22-
Input: n = 4, k = 4
23-
Output: -1
24-
Explanation: Factors list is [1, 2, 4], there is only 3 factors. We should return -1.
25-
Example 4:
26-
27-
Input: n = 1, k = 1
28-
Output: 1
29-
Explanation: Factors list is [1], the 1st factor is 1.
30-
Example 5:
31-
32-
Input: n = 1000, k = 3
33-
Output: 4
34-
Explanation: Factors list is [1, 2, 4, 5, 8, 10, 20, 25, 40, 50, 100, 125, 200, 250, 500, 1000].
19+
Input: l1 = [], l2 = [0]
20+
Output: [0]
3521

3622

3723
Constraints:
3824

39-
1 <= k <= n <= 1000 <br>
25+
The number of nodes in both lists is in the range [0, 50].
26+
-100 <= Node.val <= 100
27+
Both l1 and l2 are sorted in non-decreasing order. <br>
4028

4129
## Idea
4230
It is a really really hard one for me
4331

4432
## Code
4533
```python
34+
# Definition for singly-linked list.
35+
# class ListNode:
36+
# def __init__(self, val=0, next=None):
37+
# self.val = val
38+
# self.next = next
4639
class Solution:
47-
def kthFactor(self, n: int, k: int) -> int:
48-
lst = []
49-
50-
for i in range(1, n+1):
51-
if n % i == 0:
52-
lst += [i]
53-
if len(lst) < k:
54-
return -1
55-
return lst[k-1]
40+
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
41+
dummny = cur = ListNode(0)
42+
while l1 and l2:
43+
if l1.val < l2.val:
44+
cur.next = l1
45+
l1 = l1.next
46+
else:
47+
cur.next = l2
48+
l2 = l2.next
49+
cur = cur.next
50+
cur.next = l1 or l2
51+
return dummny.next
5652
```

Daily-challenge/2021/Jan/04/sol.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
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
16
class Solution:
2-
def kthFactor(self, n: int, k: int) -> int:
3-
lst = []
4-
5-
for i in range(1, n+1):
6-
if n % i == 0:
7-
lst += [i]
8-
if len(lst) < k:
9-
return -1
10-
return lst[k-1]
7+
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
8+
dummny = cur = ListNode(0)
9+
while l1 and l2:
10+
if l1.val < l2.val:
11+
cur.next = l1
12+
l1 = l1.next
13+
else:
14+
cur.next = l2
15+
l2 = l2.next
16+
cur = cur.next
17+
cur.next = l1 or l2
18+
return dummny.next

Daily-challenge/2021/Jan/05/README.md

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,49 @@
1-
# Can Place Flowers
2-
3-
Solution
4-
You have a long flowerbed in which some of the plots are planted, and some are not. However, flowers cannot be planted in adjacent plots.
5-
6-
Given an integer array flowerbed containing 0's and 1's, where 0 means empty and 1 means not empty, and an integer n, return if n new flowers can be planted in the flowerbed without violating the no-adjacent-flowers rule.
1+
# Remove Duplicates from Sorted List II
2+
Given the head of a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. Return the linked list sorted as well.
73

84

95

106
Example 1:
117

12-
Input: flowerbed = [1,0,0,0,1], n = 1
13-
Output: true
8+
9+
Input: head = [1,2,3,3,4,4,5]
10+
Output: [1,2,5]
1411
Example 2:
1512

16-
Input: flowerbed = [1,0,0,0,1], n = 2
17-
Output: false
13+
14+
Input: head = [1,1,1,2,3]
15+
Output: [2,3]
1816

1917

2018
Constraints:
2119

22-
1 <= flowerbed.length <= 2 * 104
23-
flowerbed[i] is 0 or 1.
24-
There are no two adjacent flowers in flowerbed.
25-
0 <= n <= flowerbed.length<br>
20+
The number of nodes in the list is in the range [0, 300].
21+
-100 <= Node.val <= 100
22+
The list is guaranteed to be sorted in ascending order.<br>
2623
## Idea
2724
It is really intestring, it is not a good question. THere is a very very simple solution but u need to be really abstract
2825
## Code
2926
```python
27+
# Definition for singly-linked list.
28+
# class ListNode:
29+
# def __init__(self, val=0, next=None):
30+
# self.val = val
31+
# self.next = next
3032
class Solution:
31-
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
32-
i = 0
33-
lenz = len(flowerbed)
34-
count = 0
35-
36-
while i < lenz and count < n:
37-
if flowerbed[i] == 0:
38-
if i == lenz - 1:
39-
nex = 0
40-
else: nex = flowerbed[i+1]
41-
if i == 0:
42-
prev = 0
43-
else:
44-
prev = flowerbed[i-1]
45-
if nex == 0 and prev == 0:
46-
flowerbed[i] = 1
47-
count += 1
48-
49-
i += 1
50-
return count == n
33+
def deleteDuplicates(self, head: ListNode) -> ListNode:
34+
dummy = ListNode(0);
35+
dummy.next = head
36+
37+
pre = dummy
38+
cur = head
39+
while cur:
40+
if cur.next and cur.val == cur.next.val:
41+
while cur and cur.next and cur.val == cur.next.val:
42+
cur = cur.next
43+
pre.next = cur.next
44+
45+
else:
46+
pre = pre.next
47+
cur = cur.next
48+
return dummy.next
5149
```

Daily-challenge/2021/Jan/05/sol.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
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
16
class Solution:
2-
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
3-
i = 0
4-
lenz = len(flowerbed)
5-
count = 0
6-
7-
while i < lenz and count < n:
8-
if flowerbed[i] == 0:
9-
if i == lenz - 1:
10-
nex = 0
11-
else: nex = flowerbed[i+1]
12-
if i == 0:
13-
prev = 0
14-
else:
15-
prev = flowerbed[i-1]
16-
if nex == 0 and prev == 0:
17-
flowerbed[i] = 1
18-
count += 1
19-
20-
i += 1
21-
return count == n
7+
def deleteDuplicates(self, head: ListNode) -> ListNode:
8+
dummy = ListNode(0);
9+
dummy.next = head
10+
11+
pre = dummy
12+
cur = head
13+
while cur:
14+
if cur.next and cur.val == cur.next.val:
15+
while cur and cur.next and cur.val == cur.next.val:
16+
cur = cur.next
17+
pre.next = cur.next
18+
19+
else:
20+
pre = pre.next
21+
cur = cur.next
22+
return dummy.next

0 commit comments

Comments
 (0)