Skip to content

Instantly share code, notes, and snippets.

Desolve

Block or report user

Report or block Desolve

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View 0575 Distribute Candies.java
class Solution {
public int distributeCandies(int[] candies) {
int[] hashtable = new int[200001];
int kind = 0;
for (int i: candies)
if(++hashtable[i + 100000] == 1)
++kind;
return kind <= candies.length / 2 ? kind : candies.length / 2;
}
}
View 0575 Distribute Candies.py
class Solution:
def distributeCandies(self, candies: List[int]) -> int:
return min(len(set(candies)), len(candies) // 2)
@Desolve
Desolve / 1342 Number of Steps to Reduce a Number to Zero.java
Created Feb 10, 2020
1342 Number of Steps to Reduce a Number to Zero
View 1342 Number of Steps to Reduce a Number to Zero.java
class Solution {
public int numberOfSteps (int num) {
return Integer.bitCount(num) + Integer.toBinaryString(num).length() - 1;
}
}
/* from leetcode
class Solution {
public int numberOfSteps (int num) {
int steps = 0;
@Desolve
Desolve / 1342 Number of Steps to Reduce a Number to Zero.py
Created Feb 10, 2020
1342 Number of Steps to Reduce a Number to Zero
View 1342 Number of Steps to Reduce a Number to Zero.py
class Solution:
def numberOfSteps (self, num: int) -> int:
return bin(num).count('1') + len(bin(num)) - 3
'''
# another solution
class Solution:
def numberOfSteps (self, num: int) -> int:
res = 0
while num != 0:
res += (num & 1) + 1
@Desolve
Desolve / 1331 Rank Transform of an Array.py
Created Feb 4, 2020
1331 Rank Transform of an Array
View 1331 Rank Transform of an Array.py
class Solution:
def arrayRankTransform(self, arr: List[int]) -> List[int]:
rk = {}
for a in sorted(arr):
rk.setdefault(a, len(rk) + 1)
return map(rk.get, arr)
View 1331 Rank Transform of an Array.java
class Solution {
public int[] arrayRankTransform(int[] arr) {
int[] sArr = Arrays.copyOf(arr, arr.length);
Arrays.sort(sArr);
HashMap<Integer, Integer> rk = new HashMap<>();
for (int n: sArr) rk.putIfAbsent(n, rk.size() + 1);
for (int i = 0; i < arr.length; ++i) arr[i] = rk.get(arr[i]);
return arr;
}
}
@Desolve
Desolve / 1332 Remove Palindromic Subsequences.py
Created Jan 30, 2020
1332 Remove Palindromic Subsequences
View 1332 Remove Palindromic Subsequences.py
class Solution:
def removePalindromeSub(self, s: str) -> int:
if not s: return 0
if s == s[::-1]: return 1
return 2
@Desolve
Desolve / 1332 Remove Palindromic Subsequences.java
Created Jan 30, 2020
1332 Remove Palindromic Subsequences
View 1332 Remove Palindromic Subsequences.java
class Solution {
public int removePalindromeSub(String s) {
if (s.isEmpty()) return 0;
if (s.equals(new StringBuilder(s).reverse().toString())) return 1;
else return 2;
}
}
@Desolve
Desolve / 0116 Populating Next Right Pointers in Each Node.java
Created Jan 24, 2020
0116 Populating Next Right Pointers in Each Node
View 0116 Populating Next Right Pointers in Each Node.java
/*
// Definition for a Node.
class Node {
public int val;
public Node left;
public Node right;
public Node next;
public Node() {}
@Desolve
Desolve / 0116 Populating Next Right Pointers in Each Node.py
Created Jan 24, 2020
0116 Populating Next Right Pointers in Each Node
View 0116 Populating Next Right Pointers in Each Node.py
"""
# Definition for a Node.
class Node:
def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None):
self.val = val
self.left = left
self.right = right
self.next = next
"""
class Solution:
You can’t perform that action at this time.