Skip to content

Commit aa888b5

Browse files
committed
Adding colution of 2005 and 1006
1 parent 2987da0 commit aa888b5

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

1000-1100q/1005.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'''
2+
Given an array A of integers, we must modify the array in the following way: we choose an i and replace A[i] with -A[i], and we repeat this process K times in total. (We may choose the same index i multiple times.)
3+
4+
Return the largest possible sum of the array after modifying it in this way.
5+
6+
7+
8+
Example 1:
9+
10+
Input: A = [4,2,3], K = 1
11+
Output: 5
12+
Explanation: Choose indices (1,) and A becomes [4,-2,3].
13+
Example 2:
14+
15+
Input: A = [3,-1,0,2], K = 3
16+
Output: 6
17+
Explanation: Choose indices (1, 2, 2) and A becomes [3,1,0,2].
18+
Example 3:
19+
20+
Input: A = [2,-3,-1,5,-4], K = 2
21+
Output: 13
22+
Explanation: Choose indices (1, 4) and A becomes [2,3,-1,5,4].
23+
24+
25+
Note:
26+
27+
1 <= A.length <= 10000
28+
1 <= K <= 10000
29+
-100 <= A[i] <= 100
30+
'''
31+
32+
class Solution(object):
33+
def largestSumAfterKNegations(self, A, K):
34+
"""
35+
:type A: List[int]
36+
:type K: int
37+
:rtype: int
38+
"""
39+
A.sort()
40+
index = 0
41+
while K > 0:
42+
if A[index] < 0:
43+
A[index] *= -1
44+
if A[index+1] < A[index] and index < len(A)-1:
45+
index += 1
46+
else:
47+
A[index] *= -1
48+
K -= 1
49+
return sum(A)

1000-1100q/1006.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'''
2+
Normally, the factorial of a positive integer n is the product of all positive integers less than or equal to n. For example, factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1.
3+
4+
We instead make a clumsy factorial: using the integers in decreasing order, we swap out the multiply operations for a fixed rotation of operations: multiply (*), divide (/), add (+) and subtract (-) in this order.
5+
6+
For example, clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1. However, these operations are still applied using the usual order of operations of arithmetic: we do all multiplication and division steps before any addition or subtraction steps, and multiplication and division steps are processed left to right.
7+
8+
Additionally, the division that we use is floor division such that 10 * 9 / 8 equals 11. This guarantees the result is an integer.
9+
10+
Implement the clumsy function as defined above: given an integer N, it returns the clumsy factorial of N.
11+
12+
13+
14+
Example 1:
15+
16+
Input: 4
17+
Output: 7
18+
Explanation: 7 = 4 * 3 / 2 + 1
19+
Example 2:
20+
21+
Input: 10
22+
Output: 12
23+
Explanation: 12 = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1
24+
25+
26+
Note:
27+
28+
1 <= N <= 10000
29+
-2^31 <= answer <= 2^31 - 1 (The answer is guaranteed to fit within a 32-bit integer.)
30+
'''
31+
32+
class Solution(object):
33+
def clumsy(self, N):
34+
"""
35+
:type N: int
36+
:rtype: int
37+
"""
38+
return [0, 1, 2, 6, 7][N] if N < 5 else N + [1, 2, 2, - 1][N % 4]

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ Python solution of problems from [LeetCode](https://leetcode.com/).
2727
|1009|[Complement of Base 10 Integer](https://leetcode.com/problems/complement-of-base-10-integer/) | [Python](./1000-1100q/1009.py)|Easy|
2828
|1008|[Construct Binary Search Tree from Preorder Traversal](https://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal)|[Python](./1000-1100q/1008.py)|Medium|
2929
|1007|[Minimum Domino Rotations For Equal Row](https://leetcode.com/problems/minimum-domino-rotations-for-equal-row)|[Python](./1000-1100q/1007.py)|Medium|
30+
|1006|[Clumsy Factorial](https://leetcode.com/problems/clumsy-factorial)|[Python](./1000-1100q/1006.py)|Medium|
31+
|1005|[Maximize Sum Of Array After K Negations](https://leetcode.com/problems/maximize-sum-of-array-after-k-negations)|[Python](./1000-1100q/1005.py)|Easy|
3032
|1004|[Max Consecutive Ones III](https://leetcode.com/problems/max-consecutive-ones-iii)|[Python](./1000-1100q/1004.py)|Medium|
3133
|1003|[Check If Word Is Valid After Substitutions](https://leetcode.com/problems/check-if-word-is-valid-after-substitutions)|[Python](./1000-1100q/1003.py)|Medium|
3234
|1002|[Find Common Characters](https://leetcode.com/problems/find-common-characters)|[Python](./1000-1100q/1002.py)|Easy|

0 commit comments

Comments
 (0)