Skip to content

Commit 2d6636c

Browse files
committed
Add 494
1 parent 09b86eb commit 2d6636c

File tree

3 files changed

+65
-10
lines changed

3 files changed

+65
-10
lines changed

Readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ https://leetcode.com/jummyegg/
160160
| 474 | Ones and Zeroes | Medium | O(K\*M\*N) | O(K\*M\*N) | Dynamic Programming | | |
161161
| 476 | Number Complement | Easy | O(N) | O(1) | Bit Manipulation | | |
162162
| 478 | Generate Random Point in a Circle | Medium | O(1) | O(1) | Math, Random, Rejection Sampling | | |
163+
| 494 | Target Sum | Medium | O(N\*T) | O(T) | Dynamic Programming, DFS | Template for DP | |
163164
| 495 | Teemo Attacking | Medium | O(N) | O(1) | Array | | |
164165
| 497 | Random Point in Non-overlapping Rectangles | Medium | O(logN) | O(N) | Design, Binary Search | | |
165166
| 498 | Diagonal Traverse | Medium | O(R\*C) | O(R\*C) | Array | | |

src/1552.magnetic-force-between-two-balls.py

+15-10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
# @lc code=start
88
# TAGS: Binary Search
99
# REVIEWME: Very Tricky problem, Template for Binary Search
10+
11+
1012
class Solution:
1113
"""
1214
Template for Binary Search:
@@ -29,23 +31,25 @@ class Solution:
2931
"""
3032
# Inspired from https://leetcode.com/problems/magnetic-force-between-two-balls/discuss/794070/Python-Binary-search-solution-with-explanation-and-similar-questions
3133
# Time: O(NlogN). Space: O(1). This is usually used for bisect_left
34+
3235
def maxDistance(self, position: List[int], m: int) -> int:
3336
position.sort()
37+
3438
def count(d):
3539
prev = position[0]
3640
cnt = 1
3741
for val in position[1:]:
38-
if val - prev >= d:
39-
cnt += 1
42+
if val - prev >= d:
43+
cnt += 1
4044
prev = val
4145
return cnt
42-
46+
4347
lo, hi = 0, position[-1] - position[0]
4448
while lo < hi:
4549
mid = (lo + hi) // 2
46-
if count(mid) >= m: # means balls are too tightly packed. we can increase space between them
50+
if count(mid) >= m: # means balls are too tightly packed. we can increase space between them
4751
lo = mid + 1
48-
else: # means space are too large, we should decrease packing space.
52+
else: # means space are too large, we should decrease packing space.
4953
hi = mid
5054
if count(lo) == m:
5155
return lo
@@ -54,22 +58,23 @@ def count(d):
5458
# 1308 ms, 87.47 %. Similar to bisect_right
5559
def maxDistance(self, position: List[int], m: int) -> int:
5660
position.sort()
61+
5762
def count(d):
5863
prev = position[0]
5964
cnt = 1
6065
for val in position[1:]:
61-
if val - prev >= d:
62-
cnt += 1
66+
if val - prev >= d:
67+
cnt += 1
6368
prev = val
6469
return cnt
65-
70+
6671
lo, hi = 0, position[-1] - position[0]
6772
while lo < hi:
6873
mid = hi - (hi - lo) // 2
69-
if count(mid) >= m:
74+
if count(mid) >= m:
7075
lo = mid
7176
else:
7277
hi = mid - 1
7378
return lo
74-
79+
7580
# @lc code=end

src/494.target-sum.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#
2+
# @lc app=leetcode id=494 lang=python3
3+
#
4+
# [494] Target Sum
5+
#
6+
7+
# @lc code=start
8+
# TAGS: Dynamic Programming, DFS
9+
# REVIEWME: Dynamic Programming, Template for DP
10+
import collections
11+
12+
13+
class Solution:
14+
# 256 ms, 68.25%. Time and Space: O(N*T). Top down approach, dfs with memo
15+
def findTargetSumWays(self, nums: List[int], target: int) -> int:
16+
@cache
17+
def dfs(i=0, sofar=0):
18+
if i == len(nums):
19+
return sofar == target
20+
add = dfs(i + 1, sofar + nums[i])
21+
sub = dfs(i + 1, sofar - nums[i])
22+
return add + sub
23+
return dfs()
24+
25+
# 3120 ms, 5.03%. Time and Space: O(N*T). Bottom Up approach.
26+
def findTargetSumWays(self, nums: List[int], target: int) -> int:
27+
dp = collections.defaultdict(int)
28+
dp[(0, 0)] = 1
29+
for i, val in enumerate(nums, 1):
30+
# Try all possible sum
31+
for sm in range(-1000, 1001):
32+
dp[(i, sm + val)] += dp[(i - 1, sm)]
33+
dp[(i, sm - val)] += dp[(i - 1, sm)]
34+
return dp[len(nums), target]
35+
36+
# 192 ms, 84.11%. Time: O(N*T). Space: O(T) Bottom up approach with optimized space
37+
def findTargetSumWays(self, nums: List[int], target: int) -> int:
38+
dp = {0: 1}
39+
for i, val in enumerate(nums):
40+
nxt = collections.defaultdict(int)
41+
# Only try sum that are in dp
42+
for sofar in dp:
43+
nxt[sofar + val] += dp[sofar]
44+
nxt[sofar - val] += dp[sofar]
45+
dp = nxt
46+
return dp[target]
47+
48+
49+
# @lc code=end

0 commit comments

Comments
 (0)