-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
253 additions
and
14 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
interview_prep/algorithm/java/ide_handicapped/divide_two_integers/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ | ||
} | ||
} |
148 changes: 148 additions & 0 deletions
148
interview_prep/algorithm/java/ide_handicapped/fancy_sequence/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...iew_prep/algorithm/java/ide_handicapped/remove-letter-to-equalize-frequency/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |