Skip to content

Instantly share code, notes, and snippets.

View KoichiSugi's full-sized avatar
😰

KoichiSugi

😰
View GitHub Profile
@KoichiSugi
KoichiSugi / findMinAndMaxSum.java
Last active October 28, 2021 00:10
Mini-Max Sum
//naive
public static void miniMaxSum(List < Long > arr) {
// Write your code here
long sum = 0;
List < Long > arr2 = new ArrayList < Long > ();
for (int i = 0; i < arr.size(); i++) {
for (int j = 0; j < arr.size(); j++) {
if (j == i) {
continue;
}
func isPalindrome(x int) bool {
str := strconv.Itoa(x)
if x < 0 {
return false
}
for i := 0; i < len(str)/2; i++ {
length := len(str) - 1
if str[i] != str[length-i] {
fmt.Println(str[i])
fmt.Println(str[length-i])
@KoichiSugi
KoichiSugi / factorialWorkerPool.go
Created October 4, 2021 11:34
Simplified factorial worker pool implementation
package main
import (
"fmt"
"sync"
)
type Factorial struct {
original int
fNum int
func twoSum(nums []int, target int) []int {
m := make(map[int]int)
for i, e := range nums {
_, ok := m[e]
if ok {
return []int{m[nums[i]], i}
} else {
m[target-e] = i
@KoichiSugi
KoichiSugi / countRune.go
Created September 29, 2021 11:12
Counting a rune value of rune array in Go
package main
import "fmt"
func main() {
arr := []rune{'a', 'b', 'a', 'a', 'b', 'c', 'd', 'e', 'f', 'c', 'a', 'b', 'a', 'd'}
m := make(map[rune]int)
for _, e := range arr {
_, ok := m[e] //search through map with a rune
if ok {
//Time Complexity: O(n) linear time
public class SumNumbers {
public static void main(String[] args) {
int[] ans = comp(new int[] { 0, 2, 4, 5, 6 }, 99);
if (ans == null) {
System.out.println("Not found");
} else {
for (int i : ans) {
System.out.println(i);
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<Character>();
if (s.length() % 2 != 0 ) return false;
for (char c : s.toCharArray()) {
if (c == '{' || c == '[' || c == '(') {
stack.push(c);
}else if(c == '}' && !stack.isEmpty() && stack.peek() == '{'){
stack.pop();
class Solution {
public String longestCommonPrefix(String[] strs) {
String prefix = strs[0];
for (int i = 1; i <= strs.length - 1; i++) {
//first compare 1st element and 2nd element
prefix = commonPrefixUtil(prefix, strs[i]);
}
return prefix;
}
class Solution {
public int romanToInt(String s) {
int[] num = new int[s.length()];
int sum = 0;
for (int i = 0; i < s.length(); i++) {
num[i] += getNumber(s.charAt(i));
}
for (int i = 0; i < num.length - 1; i++) {