"public static boolean isPalindrome(String str){
boolean flag = true;
int len = str.length()-1;
int j = len;
for(int i=0;i<=len/2;i++){
if(str.charAt(i)!=str.charAt(j--)){
flag = false;
break;
}
}
return flag;
}"
Sravanthi M. - "public static boolean isPalindrome(String str){
boolean flag = true;
int len = str.length()-1;
int j = len;
for(int i=0;i<=len/2;i++){
if(str.charAt(i)!=str.charAt(j--)){
flag = false;
break;
}
}
return flag;
}"See full answer
"
Compare alternate houses i.e for each house starting from the third, calculate the maximum money that can be stolen up to that house by choosing between:
Skipping the current house and taking the maximum money stolen up to the previous house.
Robbing the current house and adding its value to the maximum money stolen up to the house two steps back.
package main
import (
"fmt"
)
// rob function calculates the maximum money a robber can steal
func maxRob(nums []int) int {
ln"
VContaineers - "
Compare alternate houses i.e for each house starting from the third, calculate the maximum money that can be stolen up to that house by choosing between:
Skipping the current house and taking the maximum money stolen up to the previous house.
Robbing the current house and adding its value to the maximum money stolen up to the house two steps back.
package main
import (
"fmt"
)
// rob function calculates the maximum money a robber can steal
func maxRob(nums []int) int {
ln"See full answer
"We can use dictionary to store cache items so that our read / write operations will be O(1).
Each time we read or update an existing record, we have to ensure the item is moved to the back of the cache. This will allow us to evict the first item in the cache whenever the cache is full and we need to add new records also making our eviction O(1)
Instead of normal dictionary, we will use ordered dictionary to store cache items. This will allow us to efficiently move items to back of the cache a"
Alfred O. - "We can use dictionary to store cache items so that our read / write operations will be O(1).
Each time we read or update an existing record, we have to ensure the item is moved to the back of the cache. This will allow us to evict the first item in the cache whenever the cache is full and we need to add new records also making our eviction O(1)
Instead of normal dictionary, we will use ordered dictionary to store cache items. This will allow us to efficiently move items to back of the cache a"See full answer
"Use a representative of each, e.g. sort the string and add it to the value of a hashmap> where we put all the words that belong to the same anagram together."
Gaston B. - "Use a representative of each, e.g. sort the string and add it to the value of a hashmap> where we put all the words that belong to the same anagram together."See full answer
Data Scientist
Coding
+4 more
🧠Want an expert answer to a question? Saving questions lets us know what content to make next.
"
def is_valid(s: str) -> bool:
openBracket = set()
openBracket.add('{')
openBracket.add('(')
openBracket.add('[')
stack = []
for c in s:
if stack and (c == ')' and stack[len(stack)-1] == '(')\
or\
(c == '}' and stack[len(stack)-1] == '{')\
or\
(c == ']' and stack[len(stack)-1] == '['):
stack.pop()
elif c in openBracket:
stack.append(c)
else:
retu"
Aikya S. - "
def is_valid(s: str) -> bool:
openBracket = set()
openBracket.add('{')
openBracket.add('(')
openBracket.add('[')
stack = []
for c in s:
if stack and (c == ')' and stack[len(stack)-1] == '(')\
or\
(c == '}' and stack[len(stack)-1] == '{')\
or\
(c == ']' and stack[len(stack)-1] == '['):
stack.pop()
elif c in openBracket:
stack.append(c)
else:
retu"See full answer
"\# Definition for a binary tree node.
class TreeNode:
def init(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def maxPathSum(self, root: TreeNode) -> int:
self.max_sum = float('-inf')"
Jerry O. - "\# Definition for a binary tree node.
class TreeNode:
def init(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def maxPathSum(self, root: TreeNode) -> int:
self.max_sum = float('-inf')"See full answer
"
class ListNode:
def init(self, val=0, next=None):
self.val = val
self.next = next
def has_cycle(head: ListNode) -> bool:
pass # your code goes here
if head is None:
return False
previousNodes = set()
iter = head
while iter:
if iter.val in previousNodes:
return True
previousNodes.add(iter.val)
iter = iter.next;
return False
debug your code below
node1 = ListNode(1)
node2 = ListNode(2)
n"
Cagdas A. - "
class ListNode:
def init(self, val=0, next=None):
self.val = val
self.next = next
def has_cycle(head: ListNode) -> bool:
pass # your code goes here
if head is None:
return False
previousNodes = set()
iter = head
while iter:
if iter.val in previousNodes:
return True
previousNodes.add(iter.val)
iter = iter.next;
return False
debug your code below
node1 = ListNode(1)
node2 = ListNode(2)
n"See full answer
"def traprainwater(height: List[int]) -> int:
n = len(height)
totalwaterlevel = 0
for i in range(n):
j = i+1
while j = n:
break
rows = j - i -1
intrwaterlevel = min(height[j], height[i]) * rows
for k in range(i+1, j):
intrwaterlevel -= height[k]
totalwaterlevel += intrwaterlevel
i = j
return totalwaterlevel"
Manoj R. - "def traprainwater(height: List[int]) -> int:
n = len(height)
totalwaterlevel = 0
for i in range(n):
j = i+1
while j = n:
break
rows = j - i -1
intrwaterlevel = min(height[j], height[i]) * rows
for k in range(i+1, j):
intrwaterlevel -= height[k]
totalwaterlevel += intrwaterlevel
i = j
return totalwaterlevel"See full answer