bephrem1 / interviewpen Goto Github PK
View Code? Open in Web Editor NEWCode samples for Back to Back SWE lessons (archive).
Home Page: https://interviewpen.com
Code samples for Back to Back SWE lessons (archive).
Home Page: https://interviewpen.com
The updated declaration of class "UndirectedGraphNode" is as follows:
class Node {
public int val;
public List<Node> neighbors;
public Node() {}
public Node(int _val,List<Node> _neighbors) {
val = _val;
neighbors = _neighbors;
}
};
At line 58, the new number should be pushed into lowerHalf when higherHalf is empty.
it seems correct but idk why it's giving wrong answer for input 875
// Skip non-empty entries. They already have a value in them.
if (board[row][col] == EMPTY_ENTRY) {
return solveSudokuCell(row, col + 1, board);
}
Sir, according to me you have made a typo error in writing the formula for g(n) in the inner loop, as the formula should be g(i)+=g(j-1)*g(n-j) but you have write g(i)+=g(j-i)*g(i-j), sir if I am wrong then please make me clear, else kindly edit the code
Thanks
Love your videos!
Right now your isPalindrome function is taking O(n) work to determine if a string is a palindrome. However, we don't need to do that because the function can be built recursively and pre-computed. In other words once you know that "bcedfcb" is not a palindrome, you don't need to take O(n) time for the sequence "abcedfcba" (since it's the same sequence with two letters outside.
I believe this change takes your big O complexity from O(n*2^n) to just O(2^n) since there are 2^n ways to partition a string of length n and its now doing constant time work in each partition (previously it was doing O(n) work). Essentially you have O(n^2) to precompute all palindromes and O(2^n) to calculate all partitions. O(n^2 + 2^n) -> O(2^N)
Basically I'm suggesting to use the isPalindrome function from this question: https://leetcode.com/problems/longest-palindromic-substring/solution/
Here is my code:
class Solution:
def partition(self, s: str) -> List[List[str]]:
n = len(s)
# This precomputers all palindromes in O(n^2) time given total complexity O(n^2 + 2^n) = O(2^n)
# Your solutions does n work (instead of O(1) on each backtrace) making it O(n*2^n) in the worst case
is_palindrome = [[None] * n for _ in range(n)]
for i in range(n - 1, -1, -1):
for j in range(i, n):
if i == j:
is_palindrome[i][j] = True
elif i + 1 == j:
is_palindrome[i][j] = s[i] == s[j]
else:
if s[i] != s[j]:
is_palindrome[i][j] = False
else:
is_palindrome[i][j] = is_palindrome[i + 1][j - 1]
solutions = []
def _split(path, start):
if start == n:
solutions.append(path[:])
return
for end in range(start, n):
if is_palindrome[start][end]:
path.append(s[start:end+1])
_split(path, end + 1)
path.pop()
_split([], 0)
return solutions
Balanced Parentheses: https://www.youtube.com/watch?v=CCyEXcNamC4
How To Permute A String: https://www.youtube.com/watch?v=GCm7m5671Ps
IP Address Decomposition: https://www.youtube.com/watch?v=KU7Ae2513h0
KMP Substring Search: https://www.youtube.com/watch?v=BXCEFAzhxGY
Test If A Binary Tree Is Symmetric: https://www.youtube.com/watch?v=XV7Sg2hJO3Q
Dutch National Flag Problem: https://www.youtube.com/watch?v=ER4ivZosqCg
0/1 Knapsack Problem: https://www.youtube.com/watch?v=xCbYmUPvc2Q
Depth First & Breadth First Graph Search: https://www.youtube.com/watch?v=TIbUeeksXcI
Reverse A Singly Linked List: https://www.youtube.com/watch?v=O0By4Zq0OFc
Middle of A Linked List: https://www.youtube.com/watch?v=UitXxwVeOrk
Compute All Mnemonics For A Phone Number: https://www.youtube.com/watch?v=a-sMgZ7HGW0
Reverse The Digits of An Integer: https://www.youtube.com/watch?v=ZQQoHr-2stA
For https://github.com/bephrem1/backtobackswe/blob/master/Graphs/CloneAGraph/CloneAGraph.java#L57
Change the constructor to take the ArrayList of the newNode as the argument to the constructor along with val.
While boxed type Integer is stored in a heap, it is still immutable. You can see in the Integer class sources that the primitive value it wraps is declared as final
here.
Another thing is that objects are indeed passed to methods as references, but that references are copied to a local variable (parameter) of a method.
So currentOrder += 1
is the same as currentOrder = currentOrder + 1
, and JVM, in that case, unboxes currentOrder
to a primitive value, performs addition and then boxes result into a new instance of Integer. Then the reference to this new object is assigned to currentOrder
, overriding the initial value of it. But it held just a copy of a reference passed, so the original variable used as an argument remains the same, pointing to an old boxing object instance.
This can be observed from a simple example here or by using the following list to run the program:
ListNode a = new ListNode();
ListNode b = new ListNode();
ListNode c = new ListNode();
a.next = b;
b.next = c;
a.jump = c;
Result should be a:0 b:2 c:1
, but it is a:0 b:1 c:1
.
Request to add UndirectedGraphNode definition.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.