-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cc00bb0
commit 66b7f51
Showing
5 changed files
with
37 additions
and
82 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 |
---|---|---|
@@ -1,12 +1,12 @@ | ||
def read(n: int, book: [int], target: int) -> str: | ||
def read(n: int, book: list[int], target: int) -> str: | ||
# Write your code here. | ||
d = {} | ||
for i, n in enumerate(book): | ||
rem = target - n | ||
for i, num in enumerate(book): | ||
rem = target - num | ||
if rem in d.keys(): | ||
return "YES" | ||
else: | ||
d[n] = i | ||
d[num] = i | ||
return "NO" | ||
|
||
# Link: https://www.codingninjas.com/studio/problems/reading_6845742 |
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
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 |
---|---|---|
@@ -1,86 +1,46 @@ | ||
def triplet(n: int, nums: [int]) -> [[int]]: | ||
from typing import List | ||
|
||
def triplet(n: int, nums: List[int]) -> List[List[int]]: | ||
# Write your code here. | ||
res = set() | ||
|
||
# Spliting | ||
n, p, z = [], [], [] | ||
# Splitting the input list into negative, positive, and zero lists | ||
neg, pos, zero = [], [], [] | ||
for i in nums: | ||
if i>0: | ||
p.append(i) | ||
elif i<0: | ||
n.append(i) | ||
if i > 0: | ||
pos.append(i) | ||
elif i < 0: | ||
neg.append(i) | ||
else: | ||
z.append(i) | ||
zero.append(i) | ||
|
||
N, P = set(n), set(p) | ||
# Converting lists to sets for faster lookups | ||
N, P = set(neg), set(pos) | ||
|
||
# Case1: p, 1z, n | ||
if z: | ||
# Case 1: One positive, one negative, and one zero | ||
if zero: | ||
for num in P: | ||
if -1*num in N: | ||
res.add((-1*num, 0, num)) | ||
if -num in N: | ||
res.add((-num, 0, num)) | ||
|
||
|
||
# Case2: 0, 0, 0 | ||
if len(z)>=3: | ||
res.add((0,0,0)) | ||
# Case 2: Three zeros | ||
if len(zero) >= 3: | ||
res.add((0, 0, 0)) | ||
|
||
# Case 3: 2 negatives == 1 positive | ||
for i in range(len(n)): | ||
for j in range(i+1, len(n)): | ||
t = -1*(n[i] + n[j]) | ||
# Case 3: Two negatives sum to a positive | ||
for i in range(len(neg)): | ||
for j in range(i + 1, len(neg)): | ||
t = -1 * (neg[i] + neg[j]) | ||
if t in P: | ||
res.add(tuple(sorted([n[i], n[j], t]))) | ||
res.add(tuple(sorted([neg[i], neg[j], t]))) | ||
|
||
# Case 4: 2 positive == 1 negative | ||
for i in range(len(p)): | ||
for j in range(i+1, len(p)): | ||
t = -1*(p[i] + p[j]) | ||
# Case 4: Two positives sum to a negative | ||
for i in range(len(pos)): | ||
for j in range(i + 1, len(pos)): | ||
t = -1 * (pos[i] + pos[j]) | ||
if t in N: | ||
res.add(tuple(sorted([p[i], p[j], t]))) | ||
|
||
return list(map(list, res)) | ||
|
||
# Link: https://www.codingninjas.com/studio/problems/three-sum_6922132 | ||
|
||
# Optimal Approach | ||
# def triplet(n, arr): | ||
# ans = [] | ||
# arr.sort() | ||
# for i in range(n): | ||
# # remove duplicates: | ||
# if i != 0 and arr[i] == arr[i - 1]: | ||
# continue | ||
|
||
# # moving 2 pointers: | ||
# j = i + 1 | ||
# k = n - 1 | ||
# while j < k: | ||
# total_sum = arr[i] + arr[j] + arr[k] | ||
# if total_sum < 0: | ||
# j += 1 | ||
# elif total_sum > 0: | ||
# k -= 1 | ||
# else: | ||
# temp = [arr[i], arr[j], arr[k]] | ||
# ans.append(temp) | ||
# j += 1 | ||
# k -= 1 | ||
# # skip the duplicates: | ||
# while j < k and arr[j] == arr[j - 1]: | ||
# j += 1 | ||
# while j < k and arr[k] == arr[k + 1]: | ||
# k -= 1 | ||
|
||
# return ans | ||
res.add(tuple(sorted([pos[i], pos[j], t]))) | ||
|
||
# Convert set of tuples to list of lists | ||
return [list(triplet) for triplet in res] | ||
|
||
# arr = [-1, 0, 1, 2, -1, -4] | ||
# n = len(arr) | ||
# ans = triplet(n, arr) | ||
# for it in ans: | ||
# print("[", end="") | ||
# for i in it: | ||
# print(i, end=" ") | ||
# print("]", end=" ") | ||
# print() |
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
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