Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
s50600822 committed Mar 12, 2024
1 parent 5741c07 commit fb114f7
Show file tree
Hide file tree
Showing 8 changed files with 253 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

class Solution {
public int divide(int a, int b) {
if (b == 1) {
return a;
}
if (a == Integer.MIN_VALUE && b == -1) {
return Integer.MAX_VALUE;
}
boolean sign = (a > 0 && b > 0) || (a < 0 && b < 0);
a = a > 0 ? -a : a;
b = b > 0 ? -b : b;
int ans = 0;
while (a <= b) {
int x = b;
int cnt = 1;
while (x >= (Integer.MIN_VALUE >> 1) && a <= (x << 1)) {
x <<= 1;
cnt <<= 1;
}
ans += cnt;
a -= x;
}
return sign ? ans : -ans;
}

public static void main(String[] args) {
//https://leetcode.com/problems/divide-two-integers/
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
class Solution {
class Node {
Node left;
Node right;
int l;
int r;
int mid;
long v;
long add;
long mul = 1;

public Node(int l, int r) {
this.l = l;
this.r = r;
this.mid = (l + r) / 2;
}
}

class SegmentTree {
private Node root = new Node(1, (int) 1e5 + 1);
private static final int MOD = (int) 1e9 + 7;

public SegmentTree() {
}

public void modifyAdd(int l, int r, int inc) {
modifyAdd(l, r, inc, root);
}

public void modifyAdd(int l, int r, int inc, Node node) {
if (l > r) {
return;
}
if (node.l >= l && node.r <= r) {
node.v = (node.v + (node.r - node.l + 1) * inc) % MOD;
node.add = (node.add + inc) % MOD;
return;
}
pushdown(node);
if (l <= node.mid) {
modifyAdd(l, r, inc, node.left);
}
if (r > node.mid) {
modifyAdd(l, r, inc, node.right);
}
pushup(node);
}

public void modifyMul(int l, int r, int m) {
modifyMul(l, r, m, root);
}

public void modifyMul(int l, int r, int m, Node node) {
if (l > r) {
return;
}
if (node.l >= l && node.r <= r) {
node.v = (node.v * m) % MOD;
node.add = (node.add * m) % MOD;
node.mul = (node.mul * m) % MOD;
return;
}
pushdown(node);
if (l <= node.mid) {
modifyMul(l, r, m, node.left);
}
if (r > node.mid) {
modifyMul(l, r, m, node.right);
}
pushup(node);
}

public int query(int l, int r) {
return query(l, r, root);
}

public int query(int l, int r, Node node) {
if (l > r) {
return 0;
}
if (node.l >= l && node.r <= r) {
return (int) node.v;
}
pushdown(node);
int v = 0;
if (l <= node.mid) {
v = (v + query(l, r, node.left)) % MOD;
}
if (r > node.mid) {
v = (v + query(l, r, node.right)) % MOD;
}
return v;
}

public void pushup(Node node) {
node.v = (node.left.v + node.right.v) % MOD;
}

public void pushdown(Node node) {
if (node.left == null) {
node.left = new Node(node.l, node.mid);
}
if (node.right == null) {
node.right = new Node(node.mid + 1, node.r);
}
if (node.add != 0 || node.mul != 1) {
Node left = node.left, right = node.right;
left.v = (left.v * node.mul + (left.r - left.l + 1) * node.add) % MOD;
right.v = (right.v * node.mul + (right.r - right.l + 1) * node.add) % MOD;
left.add = (left.add * node.mul + node.add) % MOD;
right.add = (right.add * node.mul + node.add) % MOD;
left.mul = (left.mul * node.mul) % MOD;
right.mul = (right.mul * node.mul) % MOD;
node.add = 0;
node.mul = 1;
}
}
}

class Fancy {
private int n;
private SegmentTree tree = new SegmentTree();

public Fancy() {
}

public void append(int val) {
++n;
tree.modifyAdd(n, n, val);
}

public void addAll(int inc) {
tree.modifyAdd(1, n, inc);
}

public void multAll(int m) {
tree.modifyMul(1, n, m);
}

public int getIndex(int idx) {
return idx >= n ? -1 : tree.query(idx + 1, idx + 1);
}
}

public static void main(String[] args) {
//https://leetcode.com/problems/fancy-sequence/
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,6 @@ private List<String> build(int[][] grid, int n){
return solution;
}

public static void main(String[] args) {
//https://leetcode.com/problems/n-queens/
int boardSize = 15;
final Solution solution = new Solution();
final List<List<String>> solutions = solution.solveNQueens(boardSize);

// for (List<String> solutionBoard : solutions) {
// for (String row : solutionBoard) {
// System.out.println(row);
// }
// System.out.println();
// }
}

private int[][] newEmptyBoard(int size){
int[][] grid = new int[size][size];
result = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Solution {
public boolean equalFrequency(String word) {
int[] cnt = new int[26];
for (int i = 0; i < word.length(); ++i) {
++cnt[word.charAt(i) - 'a'];
}
for (int i = 0; i < 26; ++i) {
if (cnt[i] > 0) {
--cnt[i];
int x = 0;
boolean ok = true;
for (int v : cnt) {
if (v == 0) {
continue;
}
if (x > 0 && v != x) {
ok = false;
break;
}
x = v;
}
if (ok) {
return true;
}
++cnt[i];
}
}
return false;
}

public static void main(String[] args) {
//https://leetcode.com/problems/remove-letter-to-equalize-frequency/

}
}
10 changes: 10 additions & 0 deletions vs/go/cgroup/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module sys

go 1.22.0

require (
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/shirou/gopsutil v3.21.11+incompatible // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
golang.org/x/sys v0.17.0 // indirect
)
9 changes: 9 additions & 0 deletions vs/go/cgroup/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
github.com/shirou/gopsutil v3.21.11+incompatible h1:+1+c1VGhc88SSonWP6foOcLhvnKlUeu/erjjvaPEYiI=
github.com/shirou/gopsutil v3.21.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0=
github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
Binary file added vs/go/cgroup/sys
Binary file not shown.
21 changes: 21 additions & 0 deletions vs/go/cgroup/sys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
"fmt"
"runtime"

"github.com/shirou/gopsutil/mem"
)

func main() {
availableProcessors := runtime.NumCPU()
fmt.Println("Available Processor Count:", availableProcessors)
memInfo, err := mem.VirtualMemory()
if err != nil {
panic(err)
}
totalMemoryMB := memInfo.Total / 1024 / 1024
freeMemoryMB := memInfo.Free / 1024 / 1024
fmt.Println("Available Memory (Total):", totalMemoryMB, "MB")
fmt.Println("Available Memory (Free):", freeMemoryMB, "MB")
}

0 comments on commit fb114f7

Please sign in to comment.