IT 討論區(48)最強工種,FG25K,你!敢唔敢成功?

1001 回覆
6 Like 0 Dislike
2018-11-26 14:57:46
呢條有冇answer 想學野
2018-11-26 15:19:12
public class Solution {
	
	public int[] powerSet;
	
	public int[] solution(int[] A) {
		int length = A.length;
		if (length == 0)
			return A;
		powerSet = new int[length];
		int sum = 0;
		for (int i = 0; i < A.length; i++) {
			if (A[i] == 1) {
				int power = pow(-2, i);
				powerSet[i] = power;
				sum += power;
			}
		}
		
		int ceil;
		if (sum % 2 == 1 && sum > 0) {
			ceil = sum / 2 + 1;
		} else {
			ceil = sum / 2;
		}
		
		int index = getLeadingIndex(ceil);
		int[] array = new int[index+1];
		array[index] = 1;
		ceil -= powerSet[index];
		while (ceil != 0) {
			index = getLeadingIndex(ceil);
			array[index] = 1;
			ceil -= powerSet[index];
		}

		return array;
	}
	
	public int pow(int base, int power) {
		int number = 1;
		while (power-- > 0) {
			number *= base;
		}
		return number;
	}
	
	public int getLeadingIndex (int number) {
		int index = 0;
		int sum = 0;
		if (number > 0) {
			index = -2;
			while (sum < number) {
				index += 2;
				sum += powerSet[index];
			}
		} else if (number < 0) {
			index = -1;
			while (sum > number) {
				index += 2;
				sum += powerSet[index];
			}
		}
		return index;
	}
}

幅圖睇唔到assumption,如果length of array太大,可能要再tune
2018-11-26 15:25:13
public class Solution {
	
	public int[] powerSet;
	
	public int[] solution(int[] A) {
		int length = A.length;
		if (length == 0)
			return A;
		powerSet = new int[length];
		int sum = 0;
		for (int i = 0; i < A.length; i++) {
			if (A[i] == 1) {
				int power = pow(-2, i);
				powerSet[i] = power;
				sum += power;
			}
		}
		
		int ceil;
		if (sum % 2 == 1 && sum > 0) {
			ceil = sum / 2 + 1;
		} else {
			ceil = sum / 2;
		}
		if (ceil == 0) {
			return A;
		}

		int index = getLeadingIndex(ceil);
		int[] array = new int[index+1];
		array[index] = 1;
		ceil -= powerSet[index];
		while (ceil != 0) {
			index = getLeadingIndex(ceil);
			array[index] = 1;
			ceil -= powerSet[index];
		}

		return array;
	}
	
	public int pow(int base, int power) {
		int number = 1;
		while (power-- > 0) {
			number *= base;
		}
		return number;
	}
	
	public int getLeadingIndex (int number) {
		int index = 0;
		int sum = 0;
		if (number > 0) {
			index = -2;
			while (sum < number) {
				index += 2;
				sum += powerSet[index];
			}
		} else if (number < 0) {
			index = -1;
			while (sum > number) {
				index += 2;
				sum += powerSet[index];
			}
		}
		return index;
	}
}

漏咗個checking ceil == 0
2018-11-26 15:27:12
ceil == 0 應該return empty array而唔係A
2018-11-26 15:49:34
Brute force係2^n,你呢個solution係n^2,length of array如果4位數可能要再optimize
2018-11-26 15:51:29
Interview 冇dp已經要還神
2018-11-26 16:08:25
2018-11-26 16:09:22
都係無野,long都係得2^63,n^2夠做
2018-11-26 16:14:04
係喎,我都諗漏咗long range,咁應該唔駛再optimize
樓上貼圖位巴打可唔可以貼埋個assumption?
2018-11-26 16:17:39
拎index果到唔係0而係由a.length開始可能可以big O(N)
不過dup完都趕住debug, 邊有時間再tune
2018-11-26 16:28:10
想問ios定and機多人用
寫app寫邊邊?
2018-11-26 16:29:38
每個index都應該要check sum of former powers,最多可以減number of operations per iteration,但level of bigO應該無得減了吧?
2018-11-26 16:32:49
hybrid app
2018-11-26 16:36:20
有冇hybrid app例子
2018-11-26 16:37:42
xamarin
2018-11-26 16:39:47
最緊要係time complexity
2018-11-26 17:11:57
Thanks
仲有冇多d?
2018-11-26 17:12:49
咁都有得答錯?
2018-11-26 17:13:32
Ionic , phonegap, react
2018-11-26 17:13:53
今日又 hea 咗半日
2018-11-26 17:37:31
上面個題唔使咁做
for每個element
前一個index減1(變咗一半)
之後處理進位(進位係上一個index減1)
print返array就得
佢個數可以好大實overflow
2018-11-26 17:39:34
仲有 Flutter
2018-11-26 17:44:01
上面個題唔使咁做
for每個element
前一個index加1(變咗一半)
之後處理進位(進位係後一個index減1)
print返array就得
佢個數可以好大實overflow
2018-11-26 17:53:53
果d題目啱哂先最難,仲要限數
總有d陰濕野例如俾個array你要預埋length係1
佢sample全正數你要預埋有負數
佢sample隻隻數唔同你要預可能隻隻數一樣
甚至result唔好加到多過int max value
好多陰濕野
2018-11-26 18:19:26
btw 想知用skype online interview係點樣, 仲要正日, 唔係要請假掛
吹水台自選台熱 門最 新手機台時事台政事台World體育台娛樂台動漫台Apps台遊戲台影視台講故台健康台感情台家庭台潮流台美容台上班台財經台房屋台飲食台旅遊台學術台校園台汽車台音樂台創意台硬件台電器台攝影台玩具台寵物台軟件台活動台電訊台直播台站務台黑 洞