fishercoder1534 / leetcode Goto Github PK
View Code? Open in Web Editor NEWSolutions to LeetCode problems; updated daily. Subscribe to my YouTube channel for more.
Home Page: https://youtube.com/FisherCoder
License: Apache License 2.0
Solutions to LeetCode problems; updated daily. Subscribe to my YouTube channel for more.
Home Page: https://youtube.com/FisherCoder
License: Apache License 2.0
I want to add solutions of some problems. If is it still open then can I work on it?
I'd like to add codes in Java for some left out problems.
class Solution {
public int minCostToMoveChips(int[] position) {
int even = 0;
int odd = 0;
for(int i : position) {
if(i % 2 == 0) even++;
else odd++;
}
return Math.min(even, odd);
}
}
This solution is much easier to understand. Actually i am learning and building my skills for open source so please assign me this issue to help me grow !
Hi Jiahuan,
I looked at your code for problem 222, very nice!
To improve it, you may want to do it iteratively instead of using recursion.
Shuo
When you run it against the provided test case it even fails
Input
1
Output
["0:01","0:02","0:04","0:08","0:16","0:32","1:04","2:08","4:16","8:32"]
Expected
["0:01","0:02","0:04","0:08","0:16","0:32","1:00","2:00","4:00","8:00"]
You can't treat hour like 60 minutes here, it's not a regular clock.
I think solution for _325 would not work for below input
{1,3,7,2} and Sum 5
Expected output {2,3} = 2 but solution is giving 0
Let me know your suggestion on this. :)
YouTube real subscription
import java.util.ArrayList;
import java.util.List;
class Solution
{
public List<Integer> spiralOrder(int[][] A)
{
if ((A == null) || (A.length == 0) || (A[0].length == 0))
{
return (new ArrayList <Integer> ());
}
int lr = 0, ur = A.length, lc = 0, uc = A[0].length;
List <Integer> arr = new ArrayList <Integer> ();
int count = 0, t = 0, x = 0, y = 0;
int offset [] = {0, 1};
while (count < (A.length * A[0].length))
{
if (((y + offset[1]) == uc) || ((x + offset[0]) == ur) || ((y + offset[1]) < lc) || ((x + offset[0]) < lr))
{
if (((y + offset[1]) == uc))
{
lr += 1;
}
else if (((x + offset[0]) == ur))
{
uc -= 1;
}
else if (((y + offset[1]) < lc))
{
ur -= 1;
}
else if (((x + offset[0]) < lr))
{
lc += 1;
}
t = offset[0];
offset[0] = offset[1];
offset[1] = -t;
}
arr.add(A[x][y]);
x += offset[0];
y += offset[1];
count += 1;
}
return (arr);
}
}
Let me know what you think sir, ๐ ๐ .
If you don't consider the array to be returned and just print the values as they are, space complexity will be O(1).
Hi, I like your solutions. I thought of a different approach to the code for Perfect Number,
public boolean isPerfectNumber(int number){
int temp = 0;
for(int i=1;i<=number/2;i++){
if(number%i == 0){
temp += i;
}
}
if(temp == number){
System.out.println(""It is a perfect number"");
return true;
} else {
System.out.println(""It is not a perfect number"");
return false;
}
I believe that you have switched places the "Tag" and the "Difficulty" header
Input types were changed on April 15, 2019.
The solution in _149.java doesn't work anymore.
Below is the new solution: (credits)
class Solution {
public int maxPoints(int[][] points) {
if(points.length < 3)return points.length;
int max = 0;
HashMap<Long, Integer> map = new HashMap<Long, Integer>();
for(int i = 0;i < points.length;i++) {
int dup = 1;
map.clear();
for(int j = i + 1;j < points.length;j++) {
int dx = points[j][0] - points[i][0], dy = points[j][1] - points[i][1];
if(dx == 0 && dy == 0)dup++;
else {
int gcd = getGcd(dx, dy);
long slope = ((long)(dy / gcd) << 32) + (dx / gcd);
map.put(slope, map.getOrDefault(slope, 0) + 1);
}
}
max = Math.max(max, dup);
for(Map.Entry<Long, Integer> entry : map.entrySet())
max = Math.max(max, entry.getValue() + dup);
}
return max;
}
int getGcd(int a, int b) {
return b == 0 ? a : getGcd(b, a % b);
}
}
We are not allowed to use .toLowerCase() ๐ (even i did the same).....
You can close this issue without any changes ๐
Hi,
Can you please share your thoughts on why pivot index = 0 is a right answer?
I can see that in your solution at this line, you have purposely coded this condition (the first condition of OR construct).
The case for which this happens is:
[ -1, -1, -1, 0, 1, 1]
Would request you to reply over this discussion thread and share your thoughts. Lot of people are having same doubt.
Thanks.
Hi,
the test case written for _49 does not check if anagrams are grouped together correctly. It only cares if all the elements given in input are a part of the actual ArrayList. Eg: if I have incorrect grouping as follows for expected , the test case will still pass:
{
{"eat", "ate", "tea"},
{"tan"},
{"nat","bat"}
}
assertEquals(expected.containsAll(actual), actual.containsAll(expected));
Please let me know if I missed anything
Regards,
Prashant
1619's solution is same as 1620...i guess there is some thing wrong with the links...
Some values for the Tag / Difficulty are switched. Last 6 problems
It is the solution for #518 which is the same coin change question but requires a different answer for minimum required vs number of combinations. Comment also links to #518.
The URL is currently to the problem name for this one not the # . _434
@yaodehaung
Hi,
I saw your JS code from 05fa6b, console.log(two Sum([2,7,11,15],9)), is it needs semicolon?
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.