Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1540676

Browse files
committed
feat: add leetcode question #lcp18
1 parent c9d7e5d commit 1540676

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.hi.dhl.algorithms.leetcode._lcp18.java;
2+
3+
/**
4+
* <pre>
5+
* author: dhl
6+
* date : 2022年7月30日
7+
* desc :
8+
* </pre>
9+
*/
10+
11+
class Solution {
12+
public int breakfastNumber(int[] staple, int[] drinks, int x) {
13+
int[] arr = new int[x + 1];
14+
15+
// int i = 0;
16+
int slen = staple.length;
17+
for (int i = 0; i < slen; i++) {
18+
if (staple[i] > x) continue;
19+
arr[staple[i]]++;
20+
}
21+
22+
for (int i = 1; i < x + 1; i++) {
23+
arr[i] += arr[i - 1];
24+
}
25+
26+
// int j = 0;
27+
int dlen = drinks.length;
28+
int ans = 0;
29+
for (int i = 0; i < dlen; i++) {
30+
if (drinks[i] > x) continue;
31+
ans += arr[x - drinks[i]];
32+
ans = ans % 1000000007;
33+
}
34+
return ans;
35+
}
36+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.hi.dhl.algorithms.leetcode._lcp18.kotlin
2+
3+
/**
4+
* <pre>
5+
* author: dhl
6+
* date : 2022年7月30日
7+
* desc :
8+
* </pre>
9+
*/
10+
11+
class Solution {
12+
fun breakfastNumber(staple: IntArray, drinks: IntArray, x: Int): Int {
13+
val arr = IntArray(x + 1)
14+
val slen = staple.size
15+
for (i in 0 until slen) {
16+
if (staple[i] > x) continue
17+
arr[staple[i]]++
18+
}
19+
20+
for (i in 1..x) {
21+
arr[i] += arr[i - 1]
22+
}
23+
24+
val dlen = drinks.size
25+
var ans = 0
26+
for (i in 0 until dlen) {
27+
if (drinks[i] > x) continue
28+
ans += arr[x - drinks[i]]
29+
ans = ans % 1000000007
30+
}
31+
return ans
32+
}
33+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /