Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problems: No.0846,1296 #4282

Merged
merged 1 commit into from
Mar 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
718 changes: 604 additions & 114 deletions solution/0800-0899/0846.Hand of Straights/README.md

Large diffs are not rendered by default.

722 changes: 612 additions & 110 deletions solution/0800-0899/0846.Hand of Straights/README_EN.md

Large diffs are not rendered by default.

23 changes: 14 additions & 9 deletions solution/0800-0899/0846.Hand of Straights/Solution.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
class Solution {
public:
bool isNStraightHand(vector<int>& hand, int groupSize) {
if (hand.size() % groupSize) {
return false;
}
ranges::sort(hand);
unordered_map<int, int> cnt;
for (int& v : hand) ++cnt[v];
sort(hand.begin(), hand.end());
for (int& v : hand) {
if (cnt.count(v)) {
for (int x = v; x < v + groupSize; ++x) {
if (!cnt.count(x)) {
for (int x : hand) {
++cnt[x];
}
for (int x : hand) {
if (cnt.contains(x)) {
for (int y = x; y < x + groupSize; ++y) {
if (!cnt.contains(y)) {
return false;
}
if (--cnt[x] == 0) {
cnt.erase(x);
if (--cnt[y] == 0) {
cnt.erase(y);
}
}
}
}
return true;
}
};
};
24 changes: 12 additions & 12 deletions solution/0800-0899/0846.Hand of Straights/Solution.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
func isNStraightHand(hand []int, groupSize int) bool {
cnt := map[int]int{}
for _, v := range hand {
cnt[v]++
if len(hand)%groupSize != 0 {
return false
}
sort.Ints(hand)
for _, v := range hand {
if _, ok := cnt[v]; ok {
for x := v; x < v+groupSize; x++ {
if _, ok := cnt[x]; !ok {
cnt := map[int]int{}
for _, x := range hand {
cnt[x]++
}
for _, x := range hand {
if cnt[x] > 0 {
for y := x; y < x+groupSize; y++ {
if cnt[y] == 0 {
return false
}
cnt[x]--
if cnt[x] == 0 {
delete(cnt, x)
}
cnt[y]--
}
}
}
return true
}
}
23 changes: 11 additions & 12 deletions solution/0800-0899/0846.Hand of Straights/Solution.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
class Solution {
public boolean isNStraightHand(int[] hand, int groupSize) {
Map<Integer, Integer> cnt = new HashMap<>();
for (int v : hand) {
cnt.put(v, cnt.getOrDefault(v, 0) + 1);
if (hand.length % groupSize != 0) {
return false;
}
Arrays.sort(hand);
for (int v : hand) {
if (cnt.containsKey(v)) {
for (int x = v; x < v + groupSize; ++x) {
if (!cnt.containsKey(x)) {
Map<Integer, Integer> cnt = new HashMap<>();
for (int x : hand) {
cnt.merge(x, 1, Integer::sum);
}
for (int x : hand) {
if (cnt.getOrDefault(x, 0) > 0) {
for (int y = x; y < x + groupSize; ++y) {
if (cnt.merge(y, -1, Integer::sum) < 0) {
return false;
}
cnt.put(x, cnt.get(x) - 1);
if (cnt.get(x) == 0) {
cnt.remove(x);
}
}
}
}
return true;
}
}
}
14 changes: 7 additions & 7 deletions solution/0800-0899/0846.Hand of Straights/Solution.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
class Solution:
def isNStraightHand(self, hand: List[int], groupSize: int) -> bool:
if len(hand) % groupSize:
return False
cnt = Counter(hand)
for v in sorted(hand):
if cnt[v]:
for x in range(v, v + groupSize):
if cnt[x] == 0:
for x in sorted(hand):
if cnt[x]:
for y in range(x, x + groupSize):
if cnt[y] == 0:
return False
cnt[x] -= 1
if cnt[x] == 0:
cnt.pop(x)
cnt[y] -= 1
return True
25 changes: 13 additions & 12 deletions solution/0800-0899/0846.Hand of Straights/Solution.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
function isNStraightHand(hand: number[], groupSize: number) {
const cnt: Record<number, number> = {};
for (const i of hand) {
cnt[i] = (cnt[i] ?? 0) + 1;
function isNStraightHand(hand: number[], groupSize: number): boolean {
if (hand.length % groupSize !== 0) {
return false;
}

const keys = Object.keys(cnt).map(Number);
for (const i of keys) {
while (cnt[i]) {
for (let j = i; j < groupSize + i; j++) {
if (!cnt[j]) {
const cnt = new Map<number, number>();
for (const x of hand) {
cnt.set(x, (cnt.get(x) || 0) + 1);
}
hand.sort((a, b) => a - b);
for (const x of hand) {
if (cnt.get(x)! > 0) {
for (let y = x; y < x + groupSize; y++) {
if ((cnt.get(y) || 0) === 0) {
return false;
}
cnt[j]--;
cnt.set(y, cnt.get(y)! - 1);
}
}
}

return true;
}
25 changes: 15 additions & 10 deletions solution/0800-0899/0846.Hand of Straights/Solution2.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
class Solution {
public:
bool isNStraightHand(vector<int>& hand, int groupSize) {
if (hand.size() % groupSize != 0) return false;
if (hand.size() % groupSize) {
return false;
}
map<int, int> mp;
for (int& h : hand) mp[h] += 1;
for (int x : hand) {
++mp[x];
}
while (!mp.empty()) {
int v = mp.begin()->first;
for (int i = v; i < v + groupSize; ++i) {
if (!mp.count(i)) return false;
if (mp[i] == 1)
mp.erase(i);
else
mp[i] -= 1;
int x = mp.begin()->first;
for (int y = x; y < x + groupSize; ++y) {
if (!mp.contains(y)) {
return false;
}
if (--mp[y] == 0) {
mp.erase(y);
}
}
}
return true;
}
};
};
31 changes: 16 additions & 15 deletions solution/0800-0899/0846.Hand of Straights/Solution2.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,27 @@ func isNStraightHand(hand []int, groupSize int) bool {
if len(hand)%groupSize != 0 {
return false
}
m := treemap.NewWithIntComparator()
for _, h := range hand {
if v, ok := m.Get(h); ok {
m.Put(h, v.(int)+1)
tm := treemap.NewWithIntComparator()
for _, x := range hand {
if v, ok := tm.Get(x); ok {
tm.Put(x, v.(int)+1)
} else {
m.Put(h, 1)
tm.Put(x, 1)
}
}
for !m.Empty() {
v, _ := m.Min()
for i := v.(int); i < v.(int)+groupSize; i++ {
if _, ok := m.Get(i); !ok {
return false
}
if v, _ := m.Get(i); v.(int) == 1 {
m.Remove(i)
for !tm.Empty() {
x, _ := tm.Min()
for y := x.(int); y < x.(int)+groupSize; y++ {
if v, ok := tm.Get(y); ok {
if v.(int) == 1 {
tm.Remove(y)
} else {
tm.Put(y, v.(int)-1)
}
} else {
m.Put(i, v.(int)-1)
return false
}
}
}
return true
}
}
19 changes: 9 additions & 10 deletions solution/0800-0899/0846.Hand of Straights/Solution2.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,21 @@ public boolean isNStraightHand(int[] hand, int groupSize) {
return false;
}
TreeMap<Integer, Integer> tm = new TreeMap<>();
for (int h : hand) {
tm.put(h, tm.getOrDefault(h, 0) + 1);
for (int x : hand) {
tm.merge(x, 1, Integer::sum);
}
while (!tm.isEmpty()) {
int v = tm.firstKey();
for (int i = v; i < v + groupSize; ++i) {
if (!tm.containsKey(i)) {
int x = tm.firstKey();
for (int y = x; y < x + groupSize; ++y) {
int t = tm.merge(y, -1, Integer::sum);
if (t < 0) {
return false;
}
if (tm.get(i) == 1) {
tm.remove(i);
} else {
tm.put(i, tm.get(i) - 1);
if (t == 0) {
tm.remove(y);
}
}
}
return true;
}
}
}
22 changes: 9 additions & 13 deletions solution/0800-0899/0846.Hand of Straights/Solution2.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
class Solution:
def isNStraightHand(self, hand: List[int], groupSize: int) -> bool:
if len(hand) % groupSize != 0:
if len(hand) % groupSize:
return False
sd = SortedDict()
for h in hand:
if h in sd:
sd[h] += 1
else:
sd[h] = 1
cnt = Counter(hand)
sd = SortedDict(cnt)
while sd:
v = sd.peekitem(0)[0]
for i in range(v, v + groupSize):
if i not in sd:
x = next(iter(sd))
for y in range(x, x + groupSize):
if y not in sd:
return False
if sd[i] == 1:
sd.pop(i)
if sd[y] == 1:
del sd[y]
else:
sd[i] -= 1
sd[y] -= 1
return True
Loading