c********w 发帖数: 2438 | 1 po个我的
public int woodCut(int[] L, int k) {
// write your code here
int n=L.length;
if(n==0)
return 0;
Arrays.sort(L);
int res=0;
int left=1, right=L[n-1];
while(left<=right){
int mid=(right-left)/2+left;
int count=0;
for(int i=n-1;i>=0;i--)
count+=(L[i]/mid);
if(count>=k){
res=mid;
left=mid+1;
}
else{
... 阅读全帖 |
|
d****n 发帖数: 397 | 2 这个简单,二分查找。[0, max(L)]区间开始,然后一直找下去,到区间长度为一。
small |
|
p****6 发帖数: 3 | 3 this is the c++ version, based on greedy method.
string DeleteDigits(string A, int k) {
// wirte your code here
int len = A.size();
if(len==k) return "0";
int idx = 0;
while(k>0){
if(A[idx]>A[idx+1]){
A.erase(A.begin()+idx);
k--;
} else{
while(A[idx] <= A[idx+1])
idx++;
A.erase(A.begin()+idx);
k--;
idx = 0;
... 阅读全帖 |
|
|
h***k 发帖数: 161 | 5 把二楼的c++版翻译了一下,能过但应该还能优化
public class Solution {
/**
*@param A: A positive integer which has N digits, A is a string.
*@param k: Remove k digits.
*@return: A string
*/
public String DeleteDigits(String A, int k) {
// write your code here
int len = A.length();
if (len == k) {
return "";
}
int idx = 0;
int[] num = new int[len];
for (int i = 0; i < len; i++) {
num[i] = A.charAt(i) - '0';
... 阅读全帖 |
|
s*******h 发帖数: 105 | 6 Java 版,这个题一次过很难那。
public String DeleteDigits(String A, int k) {
// write your code here
if(A.length()<2) return A;
int i=0;
StringBuilder s=new StringBuilder(A);
while(i
if(i>=0&&s.charAt(i)>s.charAt(i+1)){
s.deleteCharAt(i);
k--;
if(k==0) break;
i--;
}
else i++;
}
i=s.length()-1;
while(k>0&&i>-1){
s.deleteC... 阅读全帖 |
|
k*******e 发帖数: 24 | 7 class Solution {
public:
string DeleteDigits(string A, int k) {
while (k > 0) {
for (int i = 0; i < A.size(); i++) {
if (i == A.size() - 1 || A[i] > Aij+1]) {
A.erase(i, 1);
break;
}
}
k--;
}
auto it = A.begin();
while (*it == '0')
it = A.erase(it);
return A;
}
}; |
|
d****n 发帖数: 397 | 8 DP
python solution
class Solution:
"""
@param A: A positive integer which has N digits, A is a string.
@param k: Remove k digits.
@return: A string
"""
def DeleteDigits(self, A, k):
# write you code here
l = len(A)
S = [['' for j in range(k + 1)] for i in range(l + 1)]
T = ''
for j in range(0, k + 1):
S[j][j] = ''
for i in range(1, l + 1):
T += A[i - 1]
S[i][0] = T
for j in range(1... 阅读全帖 |
|
p****6 发帖数: 3 | 9 this is the c++ version, based on greedy method.
string DeleteDigits(string A, int k) {
// wirte your code here
int len = A.size();
if(len==k) return "0";
int idx = 0;
while(k>0){
if(A[idx]>A[idx+1]){
A.erase(A.begin()+idx);
k--;
} else{
while(A[idx] <= A[idx+1])
idx++;
A.erase(A.begin()+idx);
k--;
idx = 0;
... 阅读全帖 |
|
|
h***k 发帖数: 161 | 11 把二楼的c++版翻译了一下,能过但应该还能优化
public class Solution {
/**
*@param A: A positive integer which has N digits, A is a string.
*@param k: Remove k digits.
*@return: A string
*/
public String DeleteDigits(String A, int k) {
// write your code here
int len = A.length();
if (len == k) {
return "";
}
int idx = 0;
int[] num = new int[len];
for (int i = 0; i < len; i++) {
num[i] = A.charAt(i) - '0';
... 阅读全帖 |
|
s*******h 发帖数: 105 | 12 Java 版,这个题一次过很难那。
public String DeleteDigits(String A, int k) {
// write your code here
if(A.length()<2) return A;
int i=0;
StringBuilder s=new StringBuilder(A);
while(i
if(i>=0&&s.charAt(i)>s.charAt(i+1)){
s.deleteCharAt(i);
k--;
if(k==0) break;
i--;
}
else i++;
}
i=s.length()-1;
while(k>0&&i>-1){
s.deleteC... 阅读全帖 |
|
k*******e 发帖数: 24 | 13 class Solution {
public:
string DeleteDigits(string A, int k) {
while (k > 0) {
for (int i = 0; i < A.size(); i++) {
if (i == A.size() - 1 || A[i] > Aij+1]) {
A.erase(i, 1);
break;
}
}
k--;
}
auto it = A.begin();
while (*it == '0')
it = A.erase(it);
return A;
}
}; |
|
d****n 发帖数: 397 | 14 DP
python solution
class Solution:
"""
@param A: A positive integer which has N digits, A is a string.
@param k: Remove k digits.
@return: A string
"""
def DeleteDigits(self, A, k):
# write you code here
l = len(A)
S = [['' for j in range(k + 1)] for i in range(l + 1)]
T = ''
for j in range(0, k + 1):
S[j][j] = ''
for i in range(1, l + 1):
T += A[i - 1]
S[i][0] = T
for j in range(1... 阅读全帖 |
|
I*******x 发帖数: 69 | 15
赞,这个比较易懂。
1. 从最高位往下走,高位比低位大,删除,
2. 如果各位都相等,一位一位删除。
3. 处理高位的零。 |
|
|
S*******C 发帖数: 822 | 17 以下的O(n) time, O(1) space解法还能再简化吗?
Kth Largest Element
18%
Accepted
Find K-th largest element in an array.
Note
You can swap elements in the array
Example
In array [9,3,2,4,8], the 3rd largest element is 4
In array [1,2,3,4,5], the 1st largest element is 5, 2nd largest element is 4
, 3rd largest element is 3 and etc.
Challenge
O(n) time, O(1) space
class Solution {
public static void main(String args[]) {
Solution test = new Solution();
int[] a = { 5, 4, 1, 3, 2};
Arra... 阅读全帖 |
|
|
c******n 发帖数: 4965 | 19 70%重合,
lint 好处是它给了 related / prerequisite problems, 练起来从浅入深, 不会觉得
突然很难。
比如用 segment tree 的题它会让你先做简单的 segment tree build
leetcode |
|
|
|
m***2 发帖数: 595 | 22 ac的一个,不知道是不是最优
public class Solution {
/**
* @param nums: A list of integers
* @param k: As described
* @return: The majority number
*/
public int majorityNumber(ArrayList nums, int k) {
if (nums == null || nums.size() == 0 || k > nums.size() || k <= 0) {
return Integer.MAX_VALUE;
}
int[] value = new int[k];
int[] count = new int[k];
for (int i = 0; i < k; i++) {
count[i] = 0;
}
... 阅读全帖 |
|
y***n 发帖数: 1594 | 23 baidu vs google
xiaomi vs apple |
|
|
|
|
|
c******f 发帖数: 243 | 28 类似这种网站,感觉安全性很重要啊...
新网站不知道是不是用docker做的 |
|
|
|
|
r*******e 发帖数: 971 | 32 没啥好告的,哪家好用用哪家。其实两家都没啥用户。 |
|
|
|
l********g 发帖数: 372 | 35
大家不是都在刷leetcode么,他家咋会没用户?我还想再继续捐钱了…… |
|
|
|
y***n 发帖数: 1594 | 38 baidu vs google
xiaomi vs apple |
|
|
|
|
|
c******f 发帖数: 243 | 43 类似这种网站,感觉安全性很重要啊...
新网站不知道是不是用docker做的 |
|
|
|
|
r*******e 发帖数: 971 | 47 没啥好告的,哪家好用用哪家。其实两家都没啥用户。 |
|
|
|
l********g 发帖数: 372 | 50
大家不是都在刷leetcode么,他家咋会没用户?我还想再继续捐钱了…… |
|