[LEETCODE: ACTIVE]
def twoSum(nums, target):
seen = {}
for i, n in enumerate(nums):
if target - n in seen:
return [seen[target-n], i]
seen[n] = i
[ACCEPTED] 28 ms
from iste import leetcode
def twoSum(nums, target):
seen = {}
for i, n in enumerate(nums):
if target - n in seen:
return [seen[target-n], i]
seen[n] = i
[ACCEPTED] 28 ms
class Solution:
def maxProfit(self, prices):
min_p, max_p = float('inf'), 0
for p in prices:
min_p = min(min_p, p)
max_p = max(max_p, p - min_p)
return max_p
[SUCCESS 100%] Memory 14.1MB
class Solution:
def maxProfit(self, prices):
min_p, max_p = float('inf'), 0
for p in prices:
min_p = min(min_p, p)
max_p = max(max_p, p - min_p)
return max_p
struct TreeNode {
int val;
TreeNode *left, *right;
TreeNode(int x): val(x) {}
};
int maxDepth(TreeNode* root) {
if (!root) return 0;
return 1 + max(maxDepth(root->left),
maxDepth(root->right));
}
[STATUS: RUNNING...]
int maxDepth(TreeNode* root) {
if (!root) return 0;
return 1 + max(maxDepth(root->left),
maxDepth(root->right));
}
[SUBMIT -> PASS]
def isValid(s: str) -> bool:
stack = []
mp = {')': '(', '}': '{', ']': '['}
for ch in s:
if ch in mp:
top = stack.pop() if stack else '#'
if mp[ch] != top: return False
else: stack.append(ch)
return not stack
def isValid(s: str) -> bool:
stack = []
mp = {')': '(', '}': '{', ']': '['}
for ch in s:
if ch in mp:
top = stack.pop() if stack else '#'
if mp[ch] != top: return False
else: stack.append(ch)
return not stack
vector<int> productExceptSelf(vector<int>& nums) {
int n = nums.size();
vector<int> res(n, 1);
for (int i=1; i<n; i++) res[i] = res[i-1]*nums[i-1];
int R = 1;
for (int i=n-1; i>=0; i--) {
res[i] *= R; R *= nums[i];
}
return res;
}
[O(N) OPTIMAL]
vector<int> productExceptSelf(vector<int>& nums) {
int n = nums.size();
vector<int> res(n, 1);
for (int i=1; i<n; i++) res[i] = res[i-1]*nums[i-1];
int R = 1;
for (int i=n-1; i>=0; i--) {
res[i] *= R; R *= nums[i];
}
return res;
}
[LEETLAUNCH BITS]
function lengthOfLongestSubstring(s) {
let set = new Set(), l = 0, max = 0;
for (let r = 0; r < s.length; r++) {
while (set.has(s[r])) {
set.delete(s[l]); l++;
}
set.add(s[r]);
max = Math.max(max, r - l + 1);
}
return max;
}
function lengthOfLongestSubstring(s) {
let set = new Set(), l = 0, max = 0;
for (let r = 0; r < s.length; r++) {
while (set.has(s[r])) {
set.delete(s[l]); l++;
}
set.add(s[r]);
max = Math.max(max, r - l + 1);
}
return max;
}
def coinChange(coins: List[int], amount: int) -> int:
dp = [float('inf')] * (amount + 1)
dp[0] = 0
for c in coins:
for x in range(c, amount + 1):
dp[x] = min(dp[x], dp[x - c] + 1)
return dp[amount] if dp[amount] != float('inf') else -1
[DYNAMIC PROGRAMMING]
def coinChange(coins: List[int], amount: int) -> int:
dp = [float('inf')] * (amount + 1)
dp[0] = 0
for c in coins:
for x in range(c, amount + 1):
dp[x] = min(dp[x], dp[x - c] + 1)
return dp[amount] if dp[amount] != float('inf') else -1
[ASAP LAB SEPT 7-9]
bool hasCycle(ListNode *head) {
ListNode *slow = head, *fast = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
if (slow == fast) return true;
}
return false;
}
bool hasCycle(ListNode *head) {
ListNode *slow = head, *fast = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
if (slow == fast) return true;
}
return false;
}